PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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.0, at includes/Core/Util/IpTool.php

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