| @@ -7,299 +7,333 @@ | ||
| 7 | 7 | namespace BitCode\BitForm\Core\Util; |
| 8 | 8 | |
| 9 | 9 | final class IpTool |
| 10 | 10 | { |
| 11 | - /** | |
| 12 | - * Check ip address | |
| 13 | - * | |
| 14 | - * @return ip_addr IP address of current visitor | |
| 15 | - */ | |
| 16 | - private static function _checkIP() | |
| 17 | - { | |
| 18 | - if (getenv('HTTP_CLIENT_IP')) { | |
| 19 | - $ip = getenv('HTTP_CLIENT_IP'); | |
| 20 | - } elseif (getenv('HTTP_X_FORWARDED_FOR')) { | |
| 21 | - $ip = getenv('HTTP_X_FORWARDED_FOR'); | |
| 22 | - } elseif (getenv('HTTP_X_FORWARDED')) { | |
| 23 | - $ip = getenv('HTTP_X_FORWARDED'); | |
| 24 | - } elseif (getenv('HTTP_FORWARDED_FOR')) { | |
| 25 | - $ip = getenv('HTTP_FORWARDED_FOR'); | |
| 26 | - } elseif (getenv('HTTP_FORWARDED')) { | |
| 27 | - $ip = getenv('HTTP_FORWARDED'); | |
| 28 | - } else { | |
| 29 | - $ip = $_SERVER['REMOTE_ADDR']; | |
| 30 | - } | |
| 31 | - return $ip; | |
| 11 | + /** | |
| 12 | + * Check ip address | |
| 13 | + * | |
| 14 | + * @return string IP address of current visitor | |
| 15 | + */ | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * | |
| 19 | + ipaddress converted to number digit | |
| 20 | + */ | |
| 21 | + private static function ip2Number($ipAdr) | |
| 22 | + { | |
| 23 | + $ipTobytes = @inet_pton($ipAdr); | |
| 24 | + | |
| 25 | + if (!$ipTobytes) { | |
| 26 | + return false; | |
| 32 | 27 | } |
| 33 | - /** | |
| 34 | - * Check device info | |
| 35 | - * | |
| 36 | - * @return void | |
| 37 | - */ | |
| 38 | - private static function _checkDevice() | |
| 39 | - { | |
| 40 | - if (isset($_SERVER)) { | |
| 41 | - $user_agent = $_SERVER['HTTP_USER_AGENT']; | |
| 42 | - } else { | |
| 43 | - global $HTTP_SERVER_VARS; | |
| 44 | - if (isset($HTTP_SERVER_VARS)) { | |
| 45 | - $user_agent = $HTTP_SERVER_VARS['HTTP_USER_AGENT']; | |
| 46 | - } else { | |
| 47 | - global $HTTP_USER_AGENT; | |
| 48 | - $user_agent = $HTTP_USER_AGENT; | |
| 49 | - } | |
| 50 | - } | |
| 51 | - return IpTool::_getBrowserName($user_agent) . '|' . IpTool::_getOS($user_agent); | |
| 28 | + | |
| 29 | + $number = ''; | |
| 30 | + foreach (unpack('C*', $ipTobytes) as $byte) { | |
| 31 | + $number .= str_pad(decbin($byte), 8, '0', STR_PAD_LEFT); | |
| 52 | 32 | } |
| 53 | - /** | |
| 54 | - * Get browser name | |
| 55 | - * | |
| 56 | - * @link https://stackoverflow.com/questions/18070154/get-operating-system-info | |
| 57 | - * | |
| 58 | - * @param string $user_agent $_SERVER['HTTP_USER_AGENT'] | |
| 59 | - * | |
| 60 | - * @return void | |
| 61 | - */ | |
| 62 | - private static function _getBrowserName($user_agent) | |
| 63 | - { | |
| 64 | - // Make case insensitive. | |
| 65 | - $t = strtolower($user_agent); | |
| 66 | 33 | |
| 67 | - // If the string *starts* with the string, strpos returns 0 (i.e., FALSE). Do a ghetto hack and start with a space. | |
| 68 | - // "[strpos()] may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE." | |
| 69 | - // http://php.net/manual/en/function.strpos.php | |
| 70 | - $t = " " . $t; | |
| 34 | + return base_convert(ltrim($number, '0'), 2, 10); | |
| 35 | + } | |
| 71 | 36 | |
| 72 | - // Humans / Regular Users | |
| 73 | - if (strpos($t, 'opera') || strpos($t, 'opr/')) { | |
| 74 | - return 'Opera'; | |
| 75 | - } elseif (strpos($t, 'edge')) { | |
| 76 | - return 'Edge'; | |
| 77 | - } elseif (strpos($t, 'Edg')) { | |
| 78 | - return 'Edge'; | |
| 79 | - } elseif (strpos($t, 'chrome')) { | |
| 80 | - return 'Chrome'; | |
| 81 | - } elseif (strpos($t, 'safari')) { | |
| 82 | - return 'Safari'; | |
| 83 | - } elseif (strpos($t, 'firefox')) { | |
| 84 | - return 'Firefox'; | |
| 85 | - } elseif (strpos($t, 'msie') || strpos($t, 'trident/7')) { | |
| 86 | - return 'Internet Explorer'; | |
| 87 | - } elseif (strpos($t, 'google')) { | |
| 88 | - return 'Googlebot'; | |
| 89 | - } elseif (strpos($t, 'bing')) { | |
| 90 | - return 'Bingbot'; | |
| 91 | - } elseif (strpos($t, 'slurp')) { | |
| 92 | - return 'Yahoo! Slurp'; | |
| 93 | - } elseif (strpos($t, 'duckduckgo')) { | |
| 94 | - return 'DuckDuckBot'; | |
| 95 | - } elseif (strpos($t, 'baidu')) { | |
| 96 | - return 'Baidu'; | |
| 97 | - } elseif (strpos($t, 'yandex')) { | |
| 98 | - return 'Yandex'; | |
| 99 | - } elseif (strpos($t, 'sogou')) { | |
| 100 | - return 'Sogou'; | |
| 101 | - } elseif (strpos($t, 'exabot')) { | |
| 102 | - return 'Exabot'; | |
| 103 | - } elseif (strpos($t, 'msn')) { | |
| 104 | - return 'MSN'; | |
| 105 | - } | |
| 37 | + private static function validateIpAddress($ip) | |
| 38 | + { | |
| 39 | + $ipv4Pattern = '/(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}/'; | |
| 40 | + $ipv6Pattern = '/([A-Fa-f0-9]{1,4})\:([A-Fa-f0-9]{1,4})\:([A-Fa-f0-9]{1,4})\:([A-Fa-f0-9]{1,4})\:([A-Fa-f0-9]{1,4})\:([A-Fa-f0-9]{1,4})\:([A-Fa-f0-9]{1,4})\:([A-Fa-f0-9]{1,4})/'; | |
| 106 | 41 | |
| 107 | - // Common Tools and Bots | |
| 108 | - elseif (strpos($t, 'mj12bot')) { | |
| 109 | - return 'Majestic'; | |
| 110 | - } elseif (strpos($t, 'ahrefs')) { | |
| 111 | - return 'Ahrefs'; | |
| 112 | - } elseif (strpos($t, 'semrush')) { | |
| 113 | - return 'SEMRush'; | |
| 114 | - } elseif (strpos($t, 'rogerbot') || strpos($t, 'dotbot')) { | |
| 115 | - return 'Moz'; | |
| 116 | - } elseif (strpos($t, 'frog') || strpos($t, 'screaming')) { | |
| 117 | - return 'Screaming Frog'; | |
| 118 | - } elseif (strpos($t, 'facebook')) { | |
| 119 | - return 'Facebook'; | |
| 120 | - } elseif (strpos($t, 'pinterest')) { | |
| 121 | - return 'Pinterest'; | |
| 122 | - } elseif ( | |
| 123 | - strpos($t, 'crawler') || strpos($t, 'api') | |
| 124 | - || strpos($t, 'spider') || strpos($t, 'http') | |
| 125 | - || strpos($t, 'bot') || strpos($t, 'archive') | |
| 126 | - || strpos($t, 'info') || strpos($t, 'data') | |
| 127 | - ) { | |
| 128 | - return 'Bot'; | |
| 129 | - } | |
| 42 | + if (preg_match($ipv4Pattern, $ip, $patternMatchIp)) { | |
| 43 | + $ip = $patternMatchIp[0]; | |
| 44 | + } elseif (preg_match($ipv6Pattern, $ip, $patternMatchIp)) { | |
| 45 | + $ip = $patternMatchIp[0]; | |
| 46 | + } | |
| 130 | 47 | |
| 131 | - return 'Other (Unknown)'; | |
| 48 | + return $ip; | |
| 49 | + } | |
| 50 | + | |
| 51 | + private static function _checkIP() | |
| 52 | + { | |
| 53 | + if (getenv('HTTP_CLIENT_IP')) { | |
| 54 | + $ip = getenv('HTTP_CLIENT_IP'); | |
| 55 | + } elseif (getenv('HTTP_X_FORWARDED_FOR')) { | |
| 56 | + $ip = getenv('HTTP_X_FORWARDED_FOR'); | |
| 57 | + } elseif (getenv('HTTP_X_FORWARDED')) { | |
| 58 | + $ip = getenv('HTTP_X_FORWARDED'); | |
| 59 | + } elseif (getenv('HTTP_FORWARDED_FOR')) { | |
| 60 | + $ip = getenv('HTTP_FORWARDED_FOR'); | |
| 61 | + } elseif (getenv('HTTP_FORWARDED')) { | |
| 62 | + $ip = getenv('HTTP_FORWARDED'); | |
| 63 | + } else { | |
| 64 | + $ip = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : ''; | |
| 132 | 65 | } |
| 133 | - /** | |
| 134 | - * Provide Operating System Information of User | |
| 135 | - * | |
| 136 | - * @link https://stackoverflow.com/questions/18070154/get-operating-system-info | |
| 137 | - * | |
| 138 | - * @return void | |
| 139 | - */ | |
| 140 | - private static function _getOS($user_agent) | |
| 141 | - { | |
| 142 | - $ros[] = array('Windows XP', 'Windows XP'); | |
| 143 | - $ros[] = array('Windows NT 5.1|Windows NT5.1', 'Windows XP'); | |
| 144 | - $ros[] = array('Windows 2000', 'Windows 2000'); | |
| 145 | - $ros[] = array('Windows NT 5.0', 'Windows 2000'); | |
| 146 | - $ros[] = array('Windows NT 4.0|WinNT4.0', 'Windows NT'); | |
| 147 | - $ros[] = array('Windows NT 5.2', 'Windows Server 2003'); | |
| 148 | - $ros[] = array('Windows NT 6.0', 'Windows Vista'); | |
| 149 | - $ros[] = array('Windows NT 7.0', 'Windows 7'); | |
| 150 | - $ros[] = array('Windows CE', 'Windows CE'); | |
| 151 | - $ros[] = array( | |
| 152 | - '(media center pc).([0-9]{1,2}\.[0-9]{1,2})', | |
| 153 | - 'Windows Media Center' | |
| 154 | - ); | |
| 155 | - $ros[] = array('(win)([0-9]{1,2}\.[0-9x]{1,2})', 'Windows'); | |
| 156 | - $ros[] = array('(win)([0-9]{2})', 'Windows'); | |
| 157 | - $ros[] = array('(windows)([0-9x]{2})', 'Windows'); | |
| 158 | - // Doesn't seem like these are necessary...not totally sure though.. | |
| 159 | - //$ros[] = array('(winnt)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'Windows NT'); | |
| 160 | - //$ros[] = array('(windows nt)(([0-9]{1,2}\.[0-9]{1,2}){0,1})', 'Windows NT'); // fix by bg | |
| 161 | - $ros[] = array('Windows ME', 'Windows ME'); | |
| 162 | - $ros[] = array('Win 9x 4.90', 'Windows ME'); | |
| 163 | - $ros[] = array('Windows 98|Win98', 'Windows 98'); | |
| 164 | - $ros[] = array('Windows 95', 'Windows 95'); | |
| 165 | - $ros[] = array('(windows)([0-9]{1,2}\.[0-9]{1,2})', 'Windows'); | |
| 166 | - $ros[] = array('win32', 'Windows'); | |
| 167 | - $ros[] = array('(java)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2})', 'Java'); | |
| 168 | - $ros[] = array('(Solaris)([0-9]{1,2}\.[0-9x]{1,2}){0,1}', 'Solaris'); | |
| 169 | - $ros[] = array('dos x86', 'DOS'); | |
| 170 | - $ros[] = array('unix', 'Unix'); | |
| 171 | - //Android | |
| 172 | - $ros[] = array('SM', 'Samsung'); | |
| 173 | - $ros[] = array('HTC', 'HTC'); | |
| 174 | - $ros[] = array('LG', 'LG'); | |
| 175 | - $ros[] = array('Microsoft', 'Microsoft'); | |
| 176 | - $ros[] = array('Pixel', 'Pixel'); | |
| 177 | - $ros[] = array('MI', 'Xiaomi'); | |
| 178 | - $ros[] = array('Xiaomi', 'Xiaomi'); | |
| 179 | - $ros[] = array('Android', 'Android'); | |
| 180 | - $ros[] = array('android', 'Android'); | |
| 66 | + return IpTool::validateIpAddress($ip); | |
| 67 | + } | |
| 181 | 68 | |
| 182 | - //iPhone | |
| 183 | - $ros[] = array('iPhone', 'iPhone'); | |
| 69 | + /** | |
| 70 | + * Check device info | |
| 71 | + * | |
| 72 | + * @return void | |
| 73 | + */ | |
| 74 | + private static function _checkDevice() | |
| 75 | + { | |
| 76 | + if (isset($_SERVER, $_SERVER['HTTP_USER_AGENT'])) { | |
| 77 | + $user_agent = sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])); | |
| 78 | + } else { | |
| 79 | + $user_agent = ''; | |
| 80 | + } | |
| 81 | + return IpTool::_getBrowserName($user_agent) . '|' . IpTool::_getOS($user_agent); | |
| 82 | + } | |
| 184 | 83 | |
| 185 | - $ros[] = array('Mac OS X', 'Mac OS X'); | |
| 186 | - $ros[] = array('Mac OS X Puma', 'Mac OS X 10.1[^0-9]'); | |
| 187 | - $ros[] = array('Mac_PowerPC', 'Macintosh PowerPC'); | |
| 188 | - $ros[] = array('(mac|Macintosh)', 'Mac OS'); | |
| 189 | - $ros[] = array('(sunos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'SunOS'); | |
| 190 | - $ros[] = array('(beos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'BeOS'); | |
| 191 | - $ros[] = array('(risc os)([0-9]{1,2}\.[0-9]{1,2})', 'RISC OS'); | |
| 192 | - $ros[] = array('os\/2', 'OS/2'); | |
| 193 | - $ros[] = array('freebsd', 'FreeBSD'); | |
| 194 | - $ros[] = array('openbsd', 'OpenBSD'); | |
| 195 | - $ros[] = array('netbsd', 'NetBSD'); | |
| 196 | - $ros[] = array('irix', 'IRIX'); | |
| 197 | - $ros[] = array('plan9', 'Plan9'); | |
| 198 | - $ros[] = array('osf', 'OSF'); | |
| 199 | - $ros[] = array('aix', 'AIX'); | |
| 200 | - $ros[] = array('GNU Hurd', 'GNU Hurd'); | |
| 201 | - $ros[] = array('(fedora)', 'Linux - Fedora'); | |
| 202 | - $ros[] = array('(kubuntu)', 'Linux - Kubuntu'); | |
| 203 | - $ros[] = array('(ubuntu)', 'Linux - Ubuntu'); | |
| 204 | - $ros[] = array('(debian)', 'Linux - Debian'); | |
| 205 | - $ros[] = array('(CentOS)', 'Linux - CentOS'); | |
| 206 | - $ros[] = array( | |
| 207 | - '(Mandriva).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)', | |
| 208 | - 'Linux - Mandriva' | |
| 209 | - ); | |
| 210 | - $ros[] = array( | |
| 211 | - '(SUSE).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)', | |
| 212 | - 'Linux - SUSE' | |
| 213 | - ); | |
| 214 | - $ros[] = array('(Dropline)', 'Linux - Slackware (Dropline GNOME)'); | |
| 215 | - $ros[] = array('(ASPLinux)', 'Linux - ASPLinux'); | |
| 216 | - $ros[] = array('(Red Hat)', 'Linux - Red Hat'); | |
| 217 | - // Loads of Linux machines will be detected as unix. | |
| 218 | - // Actually, all of the linux machines I've checked have the 'X11' in the User Agent. | |
| 219 | - //$ros[] = array('X11', 'Unix'); | |
| 220 | - $ros[] = array('(linux)', 'Linux'); | |
| 221 | - $ros[] = array('(amigaos)([0-9]{1,2}\.[0-9]{1,2})', 'AmigaOS'); | |
| 222 | - $ros[] = array('amiga-aweb', 'AmigaOS'); | |
| 223 | - $ros[] = array('amiga', 'Amiga'); | |
| 224 | - $ros[] = array('AvantGo', 'PalmOS'); | |
| 225 | - //$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1}-([0-9]{1,2}) i([0-9]{1})86){1}', 'Linux'); | |
| 226 | - //$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1} i([0-9]{1}86)){1}', 'Linux'); | |
| 227 | - //$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1})', 'Linux'); | |
| 228 | - $ros[] = array('[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3})', 'Linux'); | |
| 229 | - $ros[] = array('(webtv)/([0-9]{1,2}\.[0-9]{1,2})', 'WebTV'); | |
| 230 | - $ros[] = array('Dreamcast', 'Dreamcast OS'); | |
| 231 | - $ros[] = array('GetRight', 'Windows'); | |
| 232 | - $ros[] = array('go!zilla', 'Windows'); | |
| 233 | - $ros[] = array('gozilla', 'Windows'); | |
| 234 | - $ros[] = array('gulliver', 'Windows'); | |
| 235 | - $ros[] = array('ia archiver', 'Windows'); | |
| 236 | - $ros[] = array('NetPositive', 'Windows'); | |
| 237 | - $ros[] = array('mass downloader', 'Windows'); | |
| 238 | - $ros[] = array('microsoft', 'Windows'); | |
| 239 | - $ros[] = array('offline explorer', 'Windows'); | |
| 240 | - $ros[] = array('teleport', 'Windows'); | |
| 241 | - $ros[] = array('web downloader', 'Windows'); | |
| 242 | - $ros[] = array('webcapture', 'Windows'); | |
| 243 | - $ros[] = array('webcollage', 'Windows'); | |
| 244 | - $ros[] = array('webcopier', 'Windows'); | |
| 245 | - $ros[] = array('webstripper', 'Windows'); | |
| 246 | - $ros[] = array('webzip', 'Windows'); | |
| 247 | - $ros[] = array('wget', 'Windows'); | |
| 248 | - $ros[] = array('Java', 'Unknown'); | |
| 249 | - $ros[] = array('flashget', 'Windows'); | |
| 250 | - // delete next line if the script show not the right OS | |
| 251 | - //$ros[] = array('(PHP)/([0-9]{1,2}.[0-9]{1,2})', 'PHP'); | |
| 252 | - $ros[] = array('MS FrontPage', 'Windows'); | |
| 253 | - $ros[] = array('(msproxy)/([0-9]{1,2}.[0-9]{1,2})', 'Windows'); | |
| 254 | - $ros[] = array('(msie)([0-9]{1,2}.[0-9]{1,2})', 'Windows'); | |
| 255 | - $ros[] = array('libwww-perl', 'Unix'); | |
| 256 | - $ros[] = array('UP.Browser', 'Windows CE'); | |
| 257 | - $ros[] = array('NetAnts', 'Windows'); | |
| 258 | - $ros[] = array('Android', 'Android'); | |
| 259 | - $file = count($ros); | |
| 260 | - $os = ''; | |
| 261 | - for ($n = 0; $n < $file; $n++) { | |
| 262 | - if (@preg_match('/' . $ros[$n][0] . '/i', $user_agent)) { | |
| 263 | - $os = @$ros[$n][1]; | |
| 264 | - break; | |
| 265 | - } | |
| 266 | - } | |
| 267 | - return trim($os); | |
| 268 | - } | |
| 84 | + /** | |
| 85 | + * Get browser name | |
| 86 | + * | |
| 87 | + * @link https://stackoverflow.com/questions/18070154/get-operating-system-info | |
| 88 | + * | |
| 89 | + * @param string $user_agent $_SERVER['HTTP_USER_AGENT'] | |
| 90 | + * | |
| 91 | + * @return void | |
| 92 | + */ | |
| 93 | + private static function _getBrowserName($user_agent) | |
| 94 | + { | |
| 95 | + // Make case insensitive. | |
| 96 | + $t = strtolower($user_agent); | |
| 269 | 97 | |
| 270 | - /** | |
| 271 | - * Set user details ip,cdevice, user_id, user's visited page, current mysql formatted time | |
| 272 | - * | |
| 273 | - * @return Array of user details | |
| 274 | - */ | |
| 275 | - private static function _setUserDetail() | |
| 276 | - { | |
| 277 | - $user_details['ip'] = ip2long(IpTool::_checkIP()); | |
| 278 | - $user_details['device'] = IpTool::_checkDevice(); | |
| 279 | - $user_details['id'] = get_current_user_id(); | |
| 280 | - $user_details['page'] = is_object(get_post()) ? get_permalink(get_post()->ID) : null; | |
| 281 | - $user_details['time'] = current_time("mysql"); | |
| 98 | + // If the string *starts* with the string, strpos returns 0 (i.e., FALSE). Do a ghetto hack and start with a space. | |
| 99 | + // "[strpos()] may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE." | |
| 100 | + // http://php.net/manual/en/function.strpos.php | |
| 101 | + $t = ' ' . $t; | |
| 282 | 102 | |
| 283 | - return $user_details; | |
| 103 | + // Humans / Regular Users | |
| 104 | + if (strpos($t, 'opera') || strpos($t, 'opr/')) { | |
| 105 | + return 'Opera'; | |
| 106 | + } elseif (strpos($t, 'edge')) { | |
| 107 | + return 'Edge'; | |
| 108 | + } elseif (strpos($t, 'Edg')) { | |
| 109 | + return 'Edge'; | |
| 110 | + } elseif (strpos($t, 'chrome')) { | |
| 111 | + return 'Chrome'; | |
| 112 | + } elseif (strpos($t, 'safari')) { | |
| 113 | + return 'Safari'; | |
| 114 | + } elseif (strpos($t, 'firefox')) { | |
| 115 | + return 'Firefox'; | |
| 116 | + } elseif (strpos($t, 'msie') || strpos($t, 'trident/7')) { | |
| 117 | + return 'Internet Explorer'; | |
| 118 | + } elseif (strpos($t, 'google')) { | |
| 119 | + return 'Googlebot'; | |
| 120 | + } elseif (strpos($t, 'bing')) { | |
| 121 | + return 'Bingbot'; | |
| 122 | + } elseif (strpos($t, 'slurp')) { | |
| 123 | + return 'Yahoo! Slurp'; | |
| 124 | + } elseif (strpos($t, 'duckduckgo')) { | |
| 125 | + return 'DuckDuckBot'; | |
| 126 | + } elseif (strpos($t, 'baidu')) { | |
| 127 | + return 'Baidu'; | |
| 128 | + } elseif (strpos($t, 'yandex')) { | |
| 129 | + return 'Yandex'; | |
| 130 | + } elseif (strpos($t, 'sogou')) { | |
| 131 | + return 'Sogou'; | |
| 132 | + } elseif (strpos($t, 'exabot')) { | |
| 133 | + return 'Exabot'; | |
| 134 | + } elseif (strpos($t, 'msn')) { | |
| 135 | + return 'MSN'; | |
| 284 | 136 | } |
| 285 | 137 | |
| 286 | - /** | |
| 287 | - * Provide user details | |
| 288 | - * | |
| 289 | - * @return _setUserDetail user details array | |
| 290 | - */ | |
| 291 | - public static function getUserDetail() | |
| 292 | - { | |
| 293 | - return IpTool::_setUserDetail(); | |
| 138 | + // Common Tools and Bots | |
| 139 | + elseif (strpos($t, 'mj12bot')) { | |
| 140 | + return 'Majestic'; | |
| 141 | + } elseif (strpos($t, 'ahrefs')) { | |
| 142 | + return 'Ahrefs'; | |
| 143 | + } elseif (strpos($t, 'semrush')) { | |
| 144 | + return 'SEMRush'; | |
| 145 | + } elseif (strpos($t, 'rogerbot') || strpos($t, 'dotbot')) { | |
| 146 | + return 'Moz'; | |
| 147 | + } elseif (strpos($t, 'frog') || strpos($t, 'screaming')) { | |
| 148 | + return 'Screaming Frog'; | |
| 149 | + } elseif (strpos($t, 'facebook')) { | |
| 150 | + return 'Facebook'; | |
| 151 | + } elseif (strpos($t, 'pinterest')) { | |
| 152 | + return 'Pinterest'; | |
| 153 | + } elseif ( | |
| 154 | + strpos($t, 'crawler') || strpos($t, 'api') | |
| 155 | + || strpos($t, 'spider') || strpos($t, 'http') | |
| 156 | + || strpos($t, 'bot') || strpos($t, 'archive') | |
| 157 | + || strpos($t, 'info') || strpos($t, 'data') | |
| 158 | + ) { | |
| 159 | + return 'Bot'; | |
| 294 | 160 | } |
| 295 | 161 | |
| 296 | - /** | |
| 297 | - * Provide user IP address | |
| 298 | - * | |
| 299 | - * @return ip | |
| 300 | - */ | |
| 301 | - public static function getIP() | |
| 302 | - { | |
| 303 | - return IpTool::_checkIP(); | |
| 162 | + return 'Other (Unknown)'; | |
| 163 | + } | |
| 164 | + | |
| 165 | + /** | |
| 166 | + * Provide Operating System Information of User | |
| 167 | + * | |
| 168 | + * @link https://stackoverflow.com/questions/18070154/get-operating-system-info | |
| 169 | + * | |
| 170 | + * @return void | |
| 171 | + */ | |
| 172 | + private static function _getOS($user_agent) | |
| 173 | + { | |
| 174 | + $ros[] = ['Windows XP', 'Windows XP', false]; | |
| 175 | + $ros[] = ['Windows NT 5.1|Windows NT5.1', 'Windows XP', true]; | |
| 176 | + $ros[] = ['Windows 2000', 'Windows 2000', false]; | |
| 177 | + $ros[] = ['Windows NT 5.0', 'Windows 2000', false]; | |
| 178 | + $ros[] = ['Windows NT 4.0|WinNT4.0', 'Windows NT', true]; | |
| 179 | + $ros[] = ['Windows NT 5.2', 'Windows Server 2003', false]; | |
| 180 | + $ros[] = ['Windows NT 6.0', 'Windows Vista', false]; | |
| 181 | + $ros[] = ['Windows NT 7.0', 'Windows 7', false]; | |
| 182 | + $ros[] = ['Windows CE', 'Windows CE', false]; | |
| 183 | + $ros[] = ['(media center pc).([0-9]{1,2}\.[0-9]{1,2})', 'Windows Media Center', true]; | |
| 184 | + $ros[] = ['(win)([0-9]{1,2}\.[0-9x]{1,2})', 'Windows', true]; | |
| 185 | + $ros[] = ['(win)([0-9]{2})', 'Windows', true]; | |
| 186 | + $ros[] = ['(windows)([0-9x]{2})', 'Windows', true]; | |
| 187 | + // Doesn't seem like these are necessary...not totally sure though.. | |
| 188 | + //$ros[] = array('(winnt)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'Windows NT'); | |
| 189 | + //$ros[] = array('(windows nt)(([0-9]{1,2}\.[0-9]{1,2}){0,1})', 'Windows NT'); // fix by bg | |
| 190 | + $ros[] = ['Windows ME', 'Windows ME', false]; | |
| 191 | + $ros[] = ['Win 9x 4.90', 'Windows ME', false]; | |
| 192 | + $ros[] = ['Windows 98|Win98', 'Windows 98', true]; | |
| 193 | + $ros[] = ['Windows 95', 'Windows 95', false]; | |
| 194 | + $ros[] = ['(windows)([0-9]{1,2}\.[0-9]{1,2})', 'Windows', true]; | |
| 195 | + $ros[] = ['win32', 'Windows', false]; | |
| 196 | + $ros[] = ['(java)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2})', 'Java', true]; | |
| 197 | + $ros[] = ['(Solaris)([0-9]{1,2}\.[0-9x]{1,2}){0,1}', 'Solaris', true]; | |
| 198 | + $ros[] = ['dos x86', 'DOS', false]; | |
| 199 | + $ros[] = ['unix', 'Unix', false]; | |
| 200 | + //Android | |
| 201 | + $ros[] = ['SM', 'Samsung', false]; | |
| 202 | + $ros[] = ['HTC', 'HTC', false]; | |
| 203 | + $ros[] = ['LG', 'LG', false]; | |
| 204 | + $ros[] = ['Microsoft', 'Microsoft', false]; | |
| 205 | + $ros[] = ['Pixel', 'Pixel', false]; | |
| 206 | + $ros[] = ['MI', 'Xiaomi', false]; | |
| 207 | + $ros[] = ['Xiaomi', 'Xiaomi', false]; | |
| 208 | + $ros[] = ['Android', 'Android', false]; | |
| 209 | + $ros[] = ['android', 'Android', false]; | |
| 210 | + | |
| 211 | + //iPhone | |
| 212 | + $ros[] = ['iPhone', 'iPhone', false]; | |
| 213 | + | |
| 214 | + $ros[] = ['Mac OS X', 'Mac OS X', false]; | |
| 215 | + $ros[] = ['Mac OS X Puma', 'Mac OS X 10.1[^0-9]', true]; | |
| 216 | + $ros[] = ['Mac_PowerPC', 'Macintosh PowerPC', false]; | |
| 217 | + $ros[] = ['(mac|Macintosh)', 'Mac OS', true]; | |
| 218 | + $ros[] = ['(sunos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'SunOS', true]; | |
| 219 | + $ros[] = ['(beos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'BeOS', true]; | |
| 220 | + $ros[] = ['(risc os)([0-9]{1,2}\.[0-9]{1,2})', 'RISC OS', true]; | |
| 221 | + $ros[] = ['os\/2', 'OS/2', true]; | |
| 222 | + $ros[] = ['freebsd', 'FreeBSD', false]; | |
| 223 | + $ros[] = ['openbsd', 'OpenBSD', false]; | |
| 224 | + $ros[] = ['netbsd', 'NetBSD', false]; | |
| 225 | + $ros[] = ['irix', 'IRIX', false]; | |
| 226 | + $ros[] = ['plan9', 'Plan9', false]; | |
| 227 | + $ros[] = ['osf', 'OSF', false]; | |
| 228 | + $ros[] = ['aix', 'AIX', false]; | |
| 229 | + $ros[] = ['GNU Hurd', 'GNU Hurd', false]; | |
| 230 | + $ros[] = ['(fedora)', 'Linux - Fedora', true]; | |
| 231 | + $ros[] = ['(kubuntu)', 'Linux - Kubuntu', true]; | |
| 232 | + $ros[] = ['(ubuntu)', 'Linux - Ubuntu', true]; | |
| 233 | + $ros[] = ['(debian)', 'Linux - Debian', true]; | |
| 234 | + $ros[] = ['(CentOS)', 'Linux - CentOS', true]; | |
| 235 | + $ros[] = ['(Mandriva).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)', 'Linux - Mandriva', true]; | |
| 236 | + $ros[] = ['(SUSE).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)', 'Linux - SUSE', true]; | |
| 237 | + $ros[] = ['(Dropline)', 'Linux - Slackware (Dropline GNOME)', true]; | |
| 238 | + $ros[] = ['(ASPLinux)', 'Linux - ASPLinux', true]; | |
| 239 | + $ros[] = ['(Red Hat)', 'Linux - Red Hat', true]; | |
| 240 | + // Loads of Linux machines will be detected as unix. | |
| 241 | + // Actually, all of the linux machines I've checked have the 'X11' in the User Agent. | |
| 242 | + //$ros[] = array('X11', 'Unix'); | |
| 243 | + $ros[] = ['(linux)', 'Linux', true]; | |
| 244 | + $ros[] = ['(amigaos)([0-9]{1,2}\.[0-9]{1,2})', 'AmigaOS', true]; | |
| 245 | + $ros[] = ['amiga-aweb', 'AmigaOS', false]; | |
| 246 | + $ros[] = ['amiga', 'Amiga', false]; | |
| 247 | + $ros[] = ['AvantGo', 'PalmOS', false]; | |
| 248 | + //$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1}-([0-9]{1,2}) i([0-9]{1})86){1}', 'Linux'); | |
| 249 | + //$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1} i([0-9]{1}86)){1}', 'Linux'); | |
| 250 | + //$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1})', 'Linux'); | |
| 251 | + $ros[] = ['[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}', 'Linux', true]; | |
| 252 | + $ros[] = ['(webtv)/([0-9]{1,2}\.[0-9]{1,2})', 'WebTV', true]; | |
| 253 | + $ros[] = ['Dreamcast', 'Dreamcast OS', false]; | |
| 254 | + $ros[] = ['GetRight', 'Windows', false]; | |
| 255 | + $ros[] = ['go!zilla', 'Windows', false]; | |
| 256 | + $ros[] = ['gozilla', 'Windows', false]; | |
| 257 | + $ros[] = ['gulliver', 'Windows', false]; | |
| 258 | + $ros[] = ['ia archiver', 'Windows', false]; | |
| 259 | + $ros[] = ['NetPositive', 'Windows', false]; | |
| 260 | + $ros[] = ['mass downloader', 'Windows', false]; | |
| 261 | + $ros[] = ['microsoft', 'Windows', false]; | |
| 262 | + $ros[] = ['offline explorer', 'Windows', false]; | |
| 263 | + $ros[] = ['teleport', 'Windows', false]; | |
| 264 | + $ros[] = ['web downloader', 'Windows', false]; | |
| 265 | + $ros[] = ['webcapture', 'Windows', false]; | |
| 266 | + $ros[] = ['webcollage', 'Windows', false]; | |
| 267 | + $ros[] = ['webcopier', 'Windows', false]; | |
| 268 | + $ros[] = ['webstripper', 'Windows', false]; | |
| 269 | + $ros[] = ['webzip', 'Windows', false]; | |
| 270 | + $ros[] = ['wget', 'Windows', false]; | |
| 271 | + $ros[] = ['Java', 'Unknown', false]; | |
| 272 | + $ros[] = ['flashget', 'Windows', false]; | |
| 273 | + // delete next line if the script show not the right OS | |
| 274 | + //$ros[] = array('(PHP)/([0-9]{1,2}.[0-9]{1,2})', 'PHP'); | |
| 275 | + $ros[] = ['MS FrontPage', 'Windows', false]; | |
| 276 | + $ros[] = ['(msproxy)/([0-9]{1,2}\.[0-9]{1,2})', 'Windows', true]; | |
| 277 | + $ros[] = ['(msie)([0-9]{1,2}\.[0-9]{1,2})', 'Windows', true]; | |
| 278 | + $ros[] = ['libwww-perl', 'Unix', false]; | |
| 279 | + $ros[] = ['UP.Browser', 'Windows CE', false]; | |
| 280 | + $ros[] = ['NetAnts', 'Windows', false]; | |
| 281 | + $ros[] = ['Android', 'Android', false]; | |
| 282 | + $file = count($ros); | |
| 283 | + $os = ''; | |
| 284 | + for ($n = 0; $n < $file; $n++) { | |
| 285 | + $isRegex = isset($ros[$n][2]) && true === $ros[$n][2]; | |
| 286 | + $pattern = $ros[$n][0]; | |
| 287 | + | |
| 288 | + if ($isRegex) { | |
| 289 | + if (@preg_match('/' . $pattern . '/i', $user_agent)) { | |
| 290 | + $os = $ros[$n][1]; | |
| 291 | + break; | |
| 292 | + } | |
| 293 | + } else { | |
| 294 | + if (false !== stripos($user_agent, $pattern)) { | |
| 295 | + $os = $ros[$n][1]; | |
| 296 | + break; | |
| 297 | + } | |
| 298 | + } | |
| 304 | 299 | } |
| 300 | + return trim($os); | |
| 301 | + } | |
| 302 | + | |
| 303 | + /** | |
| 304 | + * Set user details ip,cdevice, user_id, user's visited page, current mysql formatted time | |
| 305 | + * | |
| 306 | + * @return array of user details | |
| 307 | + */ | |
| 308 | + private static function _setUserDetail() | |
| 309 | + { | |
| 310 | + $referer = wp_get_referer(); | |
| 311 | + $user_details['ip'] = IpTool::ip2Number(IpTool::_checkIP()); | |
| 312 | + $user_details['device'] = IpTool::_checkDevice(); | |
| 313 | + $user_details['id'] = get_current_user_id(); | |
| 314 | + $user_details['page'] = $referer ? $referer : ''; | |
| 315 | + $user_details['time'] = current_time('mysql'); | |
| 316 | + | |
| 317 | + return $user_details; | |
| 318 | + } | |
| 319 | + | |
| 320 | + /** | |
| 321 | + * Provide user details | |
| 322 | + * | |
| 323 | + * @return array user details array | |
| 324 | + */ | |
| 325 | + public static function getUserDetail() | |
| 326 | + { | |
| 327 | + return IpTool::_setUserDetail(); | |
| 328 | + } | |
| 329 | + | |
| 330 | + /** | |
| 331 | + * Provide user IP address | |
| 332 | + * | |
| 333 | + * @return ip | |
| 334 | + */ | |
| 335 | + public static function getIP() | |
| 336 | + { | |
| 337 | + return IpTool::_checkIP(); | |
| 338 | + } | |
| 305 | 339 | } |