forked from tobie/ua-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUAParser.php
406 lines (347 loc) · 14.6 KB
/
UAParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
/*!
* ua-parser-php v1.5.0
*
* Copyright (c) 2011-2012 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* ua-parser-php is the PHP library for the ua-parser project. Learn more about the ua-parser project at:
*
* https://github.com/tobie/ua-parser
*
* The user agents data from the ua-parser project is licensed under the Apache license.
* spyc-0.5, for loading the YAML, is licensed under the MIT license.
* The initial list of generic feature phones & smartphones came from Mobile Web OSP under the MIT license
* The initial list of spiders was taken from Yiibu's profile project under the MIT license.
*
* Many thanks to the following major contributors:
*
* - Bryan Shelton
* - Michael Bond
* - @rjd22 (https://github.com/rjd22)
* - Timo Tijhof (https://github.com/Krinkle)
* - Marcus Bointon (https://github.com/Synchro)
*
*/
// address 5.2 compatibility
if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
// address 5.1 compatibility
if (!function_exists('json_decode') || !function_exists('json_encode')) {
require_once(__DIR__."/lib/json/jsonwrapper.php");
}
// load spyc as a YAML loader
require_once(__DIR__."/lib/spyc-0.5/spyc.php");
class UA {
private static $ua;
private static $accept;
private static $regexes;
private static $debug = false; // log requests
/**
* Sets up some standard variables as well as starts the user agent parsing process
*
* @param string $ua An optional user agent string to test - if omitted uses current browser
* @return object|\stdClass {Object} the result of the user agent parsing
*/
public static function parse($ua = null) {
$sua = isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : "";
self::$ua = ($ua != null) ? $ua : strip_tags($sua);
self::$accept = empty($_SERVER["HTTP_ACCEPT"]) ? '' : strip_tags($_SERVER["HTTP_ACCEPT"]);
if (empty(self::$regexes)) {
if (file_exists(__DIR__."/resources/regexes.yaml")) {
self::$regexes = Spyc::YAMLLoad(__DIR__."/resources/regexes.yaml");
} else {
print "<h1>Error</h1>
<p>Please download the regexes.yaml file before using UAParser.php.</p>
<p>You can type the following at the command line to download the latest version:</p>
<blockquote>
<code>%: cd /path/to/UAParser/</code><br />
<code>%: php uaparser-cli.php -g</code>
</blockquote>";
exit;
}
}
// Defaults
$result = (object) array(
'family' => 'Other',
'major' => '',
'minor' => '',
'patch' => '',
'version' => '',
'browserFull' => '',
'os' => 'Other',
'osMajor' => '',
'osMinor' => '',
'osPatch' => '',
'osFull' => '',
'full' => '',
'device' => '',
'deviceMajor' => '',
'deviceMinor' => '',
'deviceVersion' => '',
'deviceFull' => '',
'isMobileDevice' => false,
'isMobile' => false,
'isSpider' => false,
'isTablet' => false,
'isComputer' => true,
);
$result->uaOriginal = self::$ua;
// run the regexes to match things up
$uaRegexes = self::$regexes['user_agent_parsers'];
foreach ($uaRegexes as $uaRegex) {
if ($uaObj = self::uaParser($uaRegex)) {
$result = (object) array_merge((array) $result, (array) $uaObj);
break;
}
}
// if no browser was found check to see if it can be matched at least against a device (e.g. spider, generic feature phone or generic smartphone)
if (!$uaObj) {
if (($uaObj = self::deviceParser()) && ($uaObj->device != 'Spider')) {
$result = (object) array_merge((array) $result, (array) $uaObj);
$result->isMobile = true;
$result->isMobileDevice = true;
$result->uaOriginal = self::$ua;
} else if (isset($uaObj) && isset($uaObj->device) && ($uaObj->device == "Spider")) {
$result = (object) array_merge((array) $result, (array) $uaObj);
$result->isMobile = false;
$result->isSpider = true;
$result->uaOriginal = self::$ua;
}
}
// still false?! see if it's a really dumb feature phone, if not just mark it as unknown
if (!$uaObj) {
if ((strpos(self::$accept,'text/vnd.wap.wml') > 0) || (strpos(self::$accept,'application/vnd.wap.xhtml+xml') > 0) || isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
$result->device = "Generic Feature Phone";
$result->deviceFull = "Generic Feature Phone";
$result->isMobile = true;
$result->isMobileDevice = true;
$result->uaOriginal = self::$ua;
} else {
$result->device = "Unknown";
$result->deviceFull = "Unknown";
$result->isMobile = false;
$result->isMobileDevice = false;
$result->isComputer = true;
$result->uaOriginal = self::$ua;
}
}
// Aliases
$result->browser = $result->family;
$result->build = $result->patch;
$result->osBuild = $result->osPatch;
// log the results when testing
if (self::$debug) {
self::log($result);
}
return $result;
}
/**
* Attempts to see if the user agent matches the regex for this test. If so it populates an obj
* with properties based on the user agent. Will also try to fetch OS & device properties
* @param array $regex The regex to be tested as well as any extra variables that need to be swapped
* @return bool|stdClass The result of the user agent parsing
*/
private static function uaParser($regex) {
// tests the supplied regex against the user agent
if (preg_match("/".str_replace("/","\/",str_replace("\/","/",$regex['regex']))."/", self::$ua, $matches)) {
// build the obj that will be returned
$obj = new stdClass;
// defaults
$obj->isMobile = false;
$obj->isMobileDevice = false;
// build the version numbers for the browser
if (isset($matches[2]) || isset($regex['v1_replacement'])) {
$obj->major = isset($regex['v1_replacement']) ? $regex['v1_replacement'] : $matches[2];
} else {
$obj->major = '';
}
if (isset($matches[3]) || isset($regex['v2_replacement'])) {
$obj->minor = isset($regex['v2_replacement']) ? $regex['v2_replacement'] : $matches[3];
}
if (isset($matches[4])) {
$obj->patch = $matches[4];
}
if (isset($matches[5])) {
$obj->revision = $matches[5];
}
// pull out the browser family. replace the version number if necessary
if (isset($regex['family_replacement']) && strstr($regex['family_replacement'],"$1")) {
$obj->family = str_replace("$1", $matches[1], $regex['family_replacement']);
} else if (isset($regex['family_replacement'])) {
$obj->family = $regex['family_replacement'];
} else {
$obj->family = $matches[1];
}
// set-up a clean version number
$obj->version = isset($obj->major) ? $obj->major : "";
$obj->version = isset($obj->minor) ? $obj->version.'.'.$obj->minor : $obj->version;
$obj->version = isset($obj->patch) ? $obj->version.'.'.$obj->patch : $obj->version;
$obj->version = isset($obj->revision) ? $obj->version.'.'.$obj->revision : $obj->version;
// prettify
$obj->browserFull = $obj->family;
if ($obj->version != '') {
$obj->browserFull .= " ".$obj->version;
}
// detect if this is a uiwebview call on iOS
$obj->isUIWebview = (($obj->family == 'Mobile Safari') && !strstr(self::$ua, 'Safari')) ? true : false;
// check to see if this is a mobile browser
$mobileBrowsers = array('Firefox Mobile','Opera Mobile','Opera Mini','Mobile Safari','webOS','IE Mobile','Playstation Portable',
'Nokia','Blackberry','Palm','Silk','Android','Maemo','Obigo','Netfront','AvantGo','Teleca','SEMC-Browser',
'Bolt','Iris','UP.Browser','Symphony','Minimo','Bunjaloo','Jasmine','Dolfin','Polaris','BREW','Chrome Mobile',
'UC Browser','Tizen Browser');
foreach ($mobileBrowsers as $mobileBrowser) {
if (stristr($obj->family, $mobileBrowser)) {
$obj->isMobile = true;
break;
}
}
// figure out the OS for the browser, if possible
if ($osObj = self::osParser()) {
$obj = (object) array_merge((array) $obj, (array) $osObj);
}
// create an attribute combinining browser and os
if (isset($obj->osFull) && $obj->osFull) {
$obj->full = $obj->browserFull . "/" . $obj->osFull;
}
// figure out the device name for the browser, if possible
if ($deviceObj = self::deviceParser()) {
$obj = (object) array_merge((array) $obj, (array) $deviceObj);
}
// if this is a mobile browser make sure mobile device is set to true
if ($obj->isMobile) {
$obj->isMobileDevice = true; // this is going to catch android devices
} else if ($obj->isMobileDevice) {
$obj->isMobile = true; // this will catch some weird htc devices
}
// if OS is Android check to see if this is a tablet. won't work on UA strings less than Android 3.0
// based on: http://googlewebmastercentral.blogspot.com/2011/03/mo-better-to-also-detect-mobile-user.html
// opera doesn't follow this convention though...
if ((isset($obj->os) && $obj->os == 'Android') && !strstr(self::$ua, 'Mobile') && !strstr(self::$ua, 'Opera')) {
$obj->isTablet = true;
$obj->isMobile = false;
}
// some select mobile OSs report a desktop browser. make sure we note they're mobile
$mobileOSs = array('Windows Phone 6.5','Windows CE','Symbian OS');
if (isset($obj->os) && in_array($obj->os,$mobileOSs)) {
$obj->isMobile = true;
$obj->isMobileDevice = true;
}
if (stristr(self::$ua,"tablet")) {
$obj->isTablet = true;
$obj->isMobileDevice = true;
$obj->isMobile = false;
}
// record if this is a spider
$obj->isSpider = (isset($obj->device) && $obj->device == "Spider") ? true : false;
// record if this is a computer
$obj->isComputer = (!$obj->isMobile && !$obj->isSpider && !$obj->isMobileDevice) ? true : false;
return $obj;
}
return false;
}
/**
* If the user agent is matched in uaParser() it also tries to check the OS and get properties
*
* @return bool|\stdClass The result of the os parsing
*/
private static function osParser() {
// build the obj that will be returned
$osObj = new stdClass;
// run the regexes to match things up
$osRegexes = self::$regexes['os_parsers'];
foreach ($osRegexes as $osRegex) {
if (preg_match("/".str_replace("/","\/",str_replace("\/","/",$osRegex['regex']))."/",self::$ua,$matches)) {
// Make sure matches 2 and 3 are at least set to null for setting
// Major and Minor defaults
if (!isset($matches[1])) { $matches[1] = null; }
if (!isset($matches[2])) { $matches[2] = null; }
if (!isset($matches[3])) { $matches[3] = null; }
// basic properties
$osObj->osMajor = isset($osRegex['os_v1_replacement']) ? $osRegex['os_v1_replacement'] : $matches[2];
$osObj->osMinor = isset($osRegex['os_v2_replacement']) ? $osRegex['os_v2_replacement'] : $matches[3];
if (isset($matches[4])) {
$osObj->osPatch = $matches[4];
}
if (isset($matches[5])) {
$osObj->osRevision = $matches[5];
}
$osObj->os = isset($osRegex['os_replacement']) ? str_replace("$1",$osObj->osMajor,$osRegex['os_replacement']) : $matches[1];
// os version
$osObj->osVersion = isset($osObj->osMajor) ? $osObj->osMajor : "";
$osObj->osVersion = isset($osObj->osMinor) ? $osObj->osVersion.'.'.$osObj->osMinor : $osObj->osVersion;
$osObj->osVersion = isset($osObj->osPatch) ? $osObj->osVersion.'.'.$osObj->osPatch : $osObj->osVersion;
$osObj->osVersion = isset($osObj->osRevision) ? $osObj->osVersion.'.'.$osObj->osRevision : $osObj->osVersion;
// prettify
$osObj->osFull = $osObj->os." ".$osObj->osVersion;
return $osObj;
}
}
return false;
}
/**
* If the user agent is matched in uaParser() it also tries to check the device and get its properties
*
* @return bool|\stdClass The result of the device parsing
*/
private static function deviceParser() {
// build the obj that will be returned
$deviceObj = new stdClass();
// defaults
$deviceObj->isMobileDevice = false;
$deviceObj->isTablet = false;
// run the regexes to match things up
$deviceRegexes = self::$regexes['device_parsers'];
foreach ($deviceRegexes as $deviceRegex) {
if (preg_match("/".str_replace("/","\/",str_replace("\/","/",$deviceRegex['regex']))."/i",self::$ua,$matches)) {
// Make sure device matches are null
// Device Name, Major and Minor defaults
if (!isset($matches[1])) { $matches[1] = null; }
if (!isset($matches[2])) { $matches[2] = null; }
if (!isset($matches[3])) { $matches[3] = null; }
// basic properties
$deviceObj->deviceMajor = isset($deviceRegex['device_v1_replacement']) ? $deviceRegex['device_v1_replacement'] : $matches[2];
$deviceObj->deviceMinor = isset($deviceRegex['device_v2_replacement']) ? $deviceRegex['device_v2_replacement'] : $matches[3];
$deviceObj->device = isset($deviceRegex['device_replacement']) ? str_replace("$1",$matches[1],$deviceRegex['device_replacement']) : str_replace("_"," ",$matches[1]);
// device version?
$deviceObj->deviceVersion = isset($deviceObj->deviceMajor) ? $deviceObj->deviceMajor : "";
$deviceObj->deviceVersion = isset($deviceObj->deviceMinor) ? $deviceObj->deviceVersion.'.'.$deviceObj->deviceMinor : $deviceObj->deviceVersion;
// prettify
$deviceObj->deviceFull = $deviceObj->device." ".$deviceObj->deviceVersion;
// check to see if this is a mobile device
// this isn't really needed because if it matches a mobile browser it'll automatically mark it as a mobile device
$mobileDevices = array("iPhone","iPod","iPad","HTC","Kindle","Lumia","Amoi","Asus","Bird","Dell","DoCoMo","Huawei","i-mate","Kyocera",
"Lenovo","LG","Kin","Motorola","Philips","Samsung","Softbank","Palm","HP ","Generic Feature Phone","Generic Smartphone",
"Nintendo DSi","Nintendo 3DS","PlayStation Vita");
foreach ($mobileDevices as $mobileDevice) {
if (stristr($deviceObj->device, $mobileDevice)) {
$deviceObj->isMobileDevice = true;
break;
}
}
// check to see if this is a tablet (not perfect)
$tablets = array("Kindle","iPad","Playbook","TouchPad","Dell Streak","Galaxy Tab","Xoom");
foreach ($tablets as $tablet) {
if (stristr($deviceObj->device, $tablet)) {
$deviceObj->isTablet = true;
break;
}
}
return $deviceObj;
}
}
return false;
}
/**
* Logs the user agent info
*/
private static function log($data) {
if (!$data) {
$data = new stdClass();
$data->ua = self::$ua;
}
$jsonData = json_encode($data);
$fp = fopen(__DIR__."/log/user_agents.log", "a");
fwrite($fp, $jsonData."\r\n");
fclose($fp);
}
}