PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.9.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.9.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 2.10.2 All 137 releases
bit-form / includes / Core / Util / IpTool.php

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

339 lines 10.8 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) {
26 return false;
27 }
28
29 $number = '';
30 foreach (unpack('C*', $ipTobytes) as $byte) {
31 $number .= str_pad(decbin($byte), 8, '0', STR_PAD_LEFT);
32 }
33
34 return base_convert(ltrim($number, '0'), 2, 10);
35 }
36
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})/';
41
42 if (preg_match($ipv4Pattern, $ip, $patternMatchIp)) {
43 $ip = $patternMatchIp[0];
44 } elseif (preg_match($ipv6Pattern, $ip, $patternMatchIp)) {
45 $ip = $patternMatchIp[0];
46 }
47
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 = $_SERVER['REMOTE_ADDR'];
65 }
66 return IpTool::validateIpAddress($ip);
67 }
68
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 = $_SERVER['HTTP_USER_AGENT'];
78 } else {
79 $user_agent = '';
80 }
81 return IpTool::_getBrowserName($user_agent) . '|' . IpTool::_getOS($user_agent);
82 }
83
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);
97
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;
102
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';
136 }
137
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';
160 }
161
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'];
175 $ros[] = ['Windows NT 5.1|Windows NT5.1', 'Windows XP'];
176 $ros[] = ['Windows 2000', 'Windows 2000'];
177 $ros[] = ['Windows NT 5.0', 'Windows 2000'];
178 $ros[] = ['Windows NT 4.0|WinNT4.0', 'Windows NT'];
179 $ros[] = ['Windows NT 5.2', 'Windows Server 2003'];
180 $ros[] = ['Windows NT 6.0', 'Windows Vista'];
181 $ros[] = ['Windows NT 7.0', 'Windows 7'];
182 $ros[] = ['Windows CE', 'Windows CE'];
183 $ros[] = [
184 '(media center pc).([0-9]{1,2}\.[0-9]{1,2})',
185 'Windows Media Center',
186 ];
187 $ros[] = ['(win)([0-9]{1,2}\.[0-9x]{1,2})', 'Windows'];
188 $ros[] = ['(win)([0-9]{2})', 'Windows'];
189 $ros[] = ['(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[] = ['Windows ME', 'Windows ME'];
194 $ros[] = ['Win 9x 4.90', 'Windows ME'];
195 $ros[] = ['Windows 98|Win98', 'Windows 98'];
196 $ros[] = ['Windows 95', 'Windows 95'];
197 $ros[] = ['(windows)([0-9]{1,2}\.[0-9]{1,2})', 'Windows'];
198 $ros[] = ['win32', 'Windows'];
199 $ros[] = ['(java)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2})', 'Java'];
200 $ros[] = ['(Solaris)([0-9]{1,2}\.[0-9x]{1,2}){0,1}', 'Solaris'];
201 $ros[] = ['dos x86', 'DOS'];
202 $ros[] = ['unix', 'Unix'];
203 //Android
204 $ros[] = ['SM', 'Samsung'];
205 $ros[] = ['HTC', 'HTC'];
206 $ros[] = ['LG', 'LG'];
207 $ros[] = ['Microsoft', 'Microsoft'];
208 $ros[] = ['Pixel', 'Pixel'];
209 $ros[] = ['MI', 'Xiaomi'];
210 $ros[] = ['Xiaomi', 'Xiaomi'];
211 $ros[] = ['Android', 'Android'];
212 $ros[] = ['android', 'Android'];
213
214 //iPhone
215 $ros[] = ['iPhone', 'iPhone'];
216
217 $ros[] = ['Mac OS X', 'Mac OS X'];
218 $ros[] = ['Mac OS X Puma', 'Mac OS X 10.1[^0-9]'];
219 $ros[] = ['Mac_PowerPC', 'Macintosh PowerPC'];
220 $ros[] = ['(mac|Macintosh)', 'Mac OS'];
221 $ros[] = ['(sunos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'SunOS'];
222 $ros[] = ['(beos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'BeOS'];
223 $ros[] = ['(risc os)([0-9]{1,2}\.[0-9]{1,2})', 'RISC OS'];
224 $ros[] = ['os\/2', 'OS/2'];
225 $ros[] = ['freebsd', 'FreeBSD'];
226 $ros[] = ['openbsd', 'OpenBSD'];
227 $ros[] = ['netbsd', 'NetBSD'];
228 $ros[] = ['irix', 'IRIX'];
229 $ros[] = ['plan9', 'Plan9'];
230 $ros[] = ['osf', 'OSF'];
231 $ros[] = ['aix', 'AIX'];
232 $ros[] = ['GNU Hurd', 'GNU Hurd'];
233 $ros[] = ['(fedora)', 'Linux - Fedora'];
234 $ros[] = ['(kubuntu)', 'Linux - Kubuntu'];
235 $ros[] = ['(ubuntu)', 'Linux - Ubuntu'];
236 $ros[] = ['(debian)', 'Linux - Debian'];
237 $ros[] = ['(CentOS)', 'Linux - CentOS'];
238 $ros[] = [
239 '(Mandriva).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)',
240 'Linux - Mandriva',
241 ];
242 $ros[] = [
243 '(SUSE).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)',
244 'Linux - SUSE',
245 ];
246 $ros[] = ['(Dropline)', 'Linux - Slackware (Dropline GNOME)'];
247 $ros[] = ['(ASPLinux)', 'Linux - ASPLinux'];
248 $ros[] = ['(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[] = ['(linux)', 'Linux'];
253 $ros[] = ['(amigaos)([0-9]{1,2}\.[0-9]{1,2})', 'AmigaOS'];
254 $ros[] = ['amiga-aweb', 'AmigaOS'];
255 $ros[] = ['amiga', 'Amiga'];
256 $ros[] = ['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[] = ['[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3})', 'Linux'];
261 $ros[] = ['(webtv)/([0-9]{1,2}\.[0-9]{1,2})', 'WebTV'];
262 $ros[] = ['Dreamcast', 'Dreamcast OS'];
263 $ros[] = ['GetRight', 'Windows'];
264 $ros[] = ['go!zilla', 'Windows'];
265 $ros[] = ['gozilla', 'Windows'];
266 $ros[] = ['gulliver', 'Windows'];
267 $ros[] = ['ia archiver', 'Windows'];
268 $ros[] = ['NetPositive', 'Windows'];
269 $ros[] = ['mass downloader', 'Windows'];
270 $ros[] = ['microsoft', 'Windows'];
271 $ros[] = ['offline explorer', 'Windows'];
272 $ros[] = ['teleport', 'Windows'];
273 $ros[] = ['web downloader', 'Windows'];
274 $ros[] = ['webcapture', 'Windows'];
275 $ros[] = ['webcollage', 'Windows'];
276 $ros[] = ['webcopier', 'Windows'];
277 $ros[] = ['webstripper', 'Windows'];
278 $ros[] = ['webzip', 'Windows'];
279 $ros[] = ['wget', 'Windows'];
280 $ros[] = ['Java', 'Unknown'];
281 $ros[] = ['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[] = ['MS FrontPage', 'Windows'];
285 $ros[] = ['(msproxy)/([0-9]{1,2}.[0-9]{1,2})', 'Windows'];
286 $ros[] = ['(msie)([0-9]{1,2}.[0-9]{1,2})', 'Windows'];
287 $ros[] = ['libwww-perl', 'Unix'];
288 $ros[] = ['UP.Browser', 'Windows CE'];
289 $ros[] = ['NetAnts', 'Windows'];
290 $ros[] = ['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