PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.10.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.10.0
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / v1 / includes / Core / Util / IpTool.php

IpTool.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.10.0, at v1/includes/Core/Util/IpTool.php

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