1
<?php
2
/**
3
* User Agent Library
4
*
5
* @author Zhou Yuan <yuanzhou19@gmail.com>
6
* @link http://www.infopotato.com/
7
* @copyright Copyright © 2009-2011 Zhou Yuan
8
* @license http://www.opensource.org/licenses/mit-license.php MIT Licence
9
*/
10
class User_Agent_Library {
11
12
/**
13
* Contents of the User-Agent: header from the current request, if there is one.
14
* A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586).
15
*
16
* @var string
17
*/
18
public $agent = NULL;
19
20
/**
21
* If the current request id from a browser
22
*
23
* @var boolean
24
*/
25
public $is_browser = FALSE;
26
27
/**
28
* If the current request id from a robot
29
*
30
* @var boolean
31
*/
32
public $is_robot = FALSE;
33
34
/**
35
* If the current request id from a mobile device
36
*
37
* @var boolean
38
*/
39
public $is_mobile = FALSE;
40
41
/**
42
* The accepted languages
43
*
44
* @var array
45
*/
46
public $languages = array();
47
48
/**
49
* The accepted Character Sets
50
*
51
* @var array
52
*/
53
public $charsets = array();
54
55
/**
56
* User agent platform information
57
*
58
* @var array
59
*/
60
public $platforms = array();
61
62
/**
63
* User agent browsers definitions
64
*
65
* @var array
66
*/
67
public $browsers = array();
68
69
/**
70
* User agent mobile definitions
71
*
72
* @var array
73
*/
74
public $mobiles = array();
75
76
/**
77
* User agent robots definitions
78
*
79
* @var integer
80
*/
81
public $robots = array();
82
83
/**
84
* Platform info
85
*
86
* @var string
87
*/
88
public $platform = '';
89
90
/**
91
* Browser name
92
*
93
* @var string
94
*/
95
public $browser = '';
96
97
/**
98
* Browser version
99
*
100
* @var string
101
*/
102
public $version = '';
103
104
/**
105
* Mobile name
106
*
107
* @var string
108
*/
109
public $mobile = '';
110
111
/**
112
* Robot name
113
*
114
* @var string
115
*/
116
public $robot = '';
117
118
119
/**
120
* Constructor
121
*
122
* Sets the User Agent and runs the compilation routine
123
*
124
* @return void
125
*/
126
public function __construct() {
127
if (isset($_SERVER['HTTP_USER_AGENT'])) {
128
$this->agent = trim($_SERVER['HTTP_USER_AGENT']);
129
}
130
131
if ( ! is_null($this->agent)) {
132
$this->platforms = array (
133
'windows nt 6.0' => 'Windows Longhorn',
134
'windows nt 5.2' => 'Windows 2003',
135
'windows nt 5.0' => 'Windows 2000',
136
'windows nt 5.1' => 'Windows XP',
137
'windows nt 4.0' => 'Windows NT 4.0',
138
'winnt4.0' => 'Windows NT 4.0',
139
'winnt 4.0' => 'Windows NT',
140
'winnt' => 'Windows NT',
141
'windows 98' => 'Windows 98',
142
'win98' => 'Windows 98',
143
'windows 95' => 'Windows 95',
144
'win95' => 'Windows 95',
145
'windows' => 'Unknown Windows OS',
146
'os x' => 'Mac OS X',
147
'ppc mac' => 'Power PC Mac',
148
'freebsd' => 'FreeBSD',
149
'ppc' => 'Macintosh',
150
'linux' => 'Linux',
151
'debian' => 'Debian',
152
'sunos' => 'Sun Solaris',
153
'beos' => 'BeOS',
154
'apachebench' => 'ApacheBench',
155
'aix' => 'AIX',
156
'irix' => 'Irix',
157
'osf' => 'DEC OSF',
158
'hp-ux' => 'HP-UX',
159
'netbsd' => 'NetBSD',
160
'bsdi' => 'BSDi',
161
'openbsd' => 'OpenBSD',
162
'gnu' => 'GNU/Linux',
163
'unix' => 'Unknown Unix OS'
164
);
165
166
167
// The order of this array should NOT be changed. Many browsers return
168
// multiple browser types so we want to identify the sub-type first.
169
$this->browsers = array(
170
'Flock' => 'Flock',
171
'Chrome' => 'Chrome',
172
'Opera' => 'Opera',
173
'MSIE' => 'Internet Explorer',
174
'Internet Explorer' => 'Internet Explorer',
175
'Shiira' => 'Shiira',
176
'Firefox' => 'Firefox',
177
'Chimera' => 'Chimera',
178
'Phoenix' => 'Phoenix',
179
'Firebird' => 'Firebird',
180
'Camino' => 'Camino',
181
'Netscape' => 'Netscape',
182
'OmniWeb' => 'OmniWeb',
183
'Safari' => 'Safari',
184
'Mozilla' => 'Mozilla',
185
'Konqueror' => 'Konqueror',
186
'icab' => 'iCab',
187
'Lynx' => 'Lynx',
188
'Links' => 'Links',
189
'hotjava' => 'HotJava',
190
'amaya' => 'Amaya',
191
'IBrowse' => 'IBrowse'
192
);
193
194
$this->mobiles = array(
195
// legacy array, old values commented out
196
'mobileexplorer' => 'Mobile Explorer',
197
// 'openwave' => 'Open Wave',
198
// 'opera mini' => 'Opera Mini',
199
// 'operamini' => 'Opera Mini',
200
// 'elaine' => 'Palm',
201
'palmsource' => 'Palm',
202
// 'digital paths' => 'Palm',
203
// 'avantgo' => 'Avantgo',
204
// 'xiino' => 'Xiino',
205
'palmscape' => 'Palmscape',
206
// 'nokia' => 'Nokia',
207
// 'ericsson' => 'Ericsson',
208
// 'blackberry' => 'BlackBerry',
209
// 'motorola' => 'Motorola'
210
211
// Phones and Manufacturers
212
'motorola' => "Motorola",
213
'nokia' => "Nokia",
214
'palm' => "Palm",
215
'iphone' => "Apple iPhone",
216
'ipad' => "iPad",
217
'ipod' => "Apple iPod Touch",
218
'sony' => "Sony Ericsson",
219
'ericsson' => "Sony Ericsson",
220
'blackberry' => "BlackBerry",
221
'cocoon' => "O2 Cocoon",
222
'blazer' => "Treo",
223
'lg' => "LG",
224
'amoi' => "Amoi",
225
'xda' => "XDA",
226
'mda' => "MDA",
227
'vario' => "Vario",
228
'htc' => "HTC",
229
'samsung' => "Samsung",
230
'sharp' => "Sharp",
231
'sie-' => "Siemens",
232
'alcatel' => "Alcatel",
233
'benq' => "BenQ",
234
'ipaq' => "HP iPaq",
235
'mot-' => "Motorola",
236
'playstation portable' => "PlayStation Portable",
237
'hiptop' => "Danger Hiptop",
238
'nec-' => "NEC",
239
'panasonic' => "Panasonic",
240
'philips' => "Philips",
241
'sagem' => "Sagem",
242
'sanyo' => "Sanyo",
243
'spv' => "SPV",
244
'zte' => "ZTE",
245
'sendo' => "Sendo",
246
247
// Operating Systems
248
'symbian' => "Symbian",
249
'SymbianOS' => "SymbianOS",
250
'elaine' => "Palm",
251
'palm' => "Palm",
252
'series60' => "Symbian S60",
253
'windows ce' => "Windows CE",
254
255
// Browsers
256
'obigo' => "Obigo",
257
'netfront' => "Netfront Browser",
258
'openwave' => "Openwave Browser",
259
'mobilexplorer' => "Mobile Explorer",
260
'operamini' => "Opera Mini",
261
'opera mini' => "Opera Mini",
262
263
// Other
264
'digital paths' => "Digital Paths",
265
'avantgo' => "AvantGo",
266
'xiino' => "Xiino",
267
'novarra' => "Novarra Transcoder",
268
'vodafone' => "Vodafone",
269
'docomo' => "NTT DoCoMo",
270
'o2' => "O2",
271
272
// Fallback
273
'mobile' => "Generic Mobile",
274
'wireless' => "Generic Mobile",
275
'j2me' => "Generic Mobile",
276
'midp' => "Generic Mobile",
277
'cldc' => "Generic Mobile",
278
'up.link' => "Generic Mobile",
279
'up.browser' => "Generic Mobile",
280
'smartphone' => "Generic Mobile",
281
'cellphone' => "Generic Mobile"
282
);
283
284
// There are hundreds of bots but these are the most common.
285
$this->robots = array(
286
'googlebot' => 'Googlebot',
287
'msnbot' => 'MSNBot',
288
'slurp' => 'Inktomi Slurp',
289
'yahoo' => 'Yahoo',
290
'askjeeves' => 'AskJeeves',
291
'fastcrawler' => 'FastCrawler',
292
'infoseek' => 'InfoSeek Robot 1.0',
293
'lycos' => 'Lycos'
294
);
295
296
$this->_compile_data();
297
}
298
}
299
300
301
/**
302
* Compile the User Agent Data
303
*
304
* @return bool
305
*/
306
private function _compile_data() {
307
// Set platform info
308
$this->_set_platform();
309
310
foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function) {
311
if ($this->$function() === TRUE) {
312
break;
313
}
314
}
315
}
316
317
/**
318
* Set the Platform
319
*
320
* @return mixed
321
*/
322
private function _set_platform() {
323
foreach ($this->platforms as $key => $val) {
324
if (preg_match("|".preg_quote($key)."|i", $this->agent)) {
325
$this->platform = $val;
326
return TRUE;
327
}
328
}
329
$this->platform = 'Unknown Platform';
330
}
331
332
/**
333
* Set the Browser
334
*
335
* @return bool
336
*/
337
private function _set_browser() {
338
foreach ($this->browsers as $key => $val) {
339
if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match)) {
340
$this->is_browser = TRUE;
341
$this->version = $match[1];
342
$this->browser = $val;
343
$this->_set_mobile();
344
return TRUE;
345
}
346
}
347
return FALSE;
348
}
349
350
351
/**
352
* Set the Robot
353
*
354
* @return bool
355
*/
356
private function _set_robot() {
357
foreach ($this->robots as $key => $val) {
358
if (preg_match("|".preg_quote($key)."|i", $this->agent)) {
359
$this->is_robot = TRUE;
360
$this->robot = $val;
361
return TRUE;
362
}
363
}
364
return FALSE;
365
}
366
367
368
/**
369
* Set the Mobile Device
370
*
371
* @return bool
372
*/
373
private function _set_mobile() {
374
foreach ($this->mobiles as $key => $val) {
375
if (strpos(strtolower($this->agent), $key) !== FALSE) {
376
$this->is_mobile = TRUE;
377
$this->mobile = $val;
378
return TRUE;
379
}
380
}
381
return FALSE;
382
}
383
384
385
/**
386
* Set the accepted languages
387
*
388
* @return void
389
*/
390
private function _set_languages() {
391
if ((count($this->languages) == 0) && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '') {
392
$languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
393
394
$this->languages = explode(',', $languages);
395
}
396
397
if (count($this->languages) == 0) {
398
$this->languages = array('Undefined');
399
}
400
}
401
402
403
/**
404
* Set the accepted character sets
405
*
406
* @return void
407
*/
408
private function _set_charsets() {
409
if ((count($this->charsets) == 0) && isset($_SERVER['HTTP_ACCEPT_CHARSET']) && $_SERVER['HTTP_ACCEPT_CHARSET'] != '') {
410
$charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
411
412
$this->charsets = explode(',', $charsets);
413
}
414
415
if (count($this->charsets) == 0) {
416
$this->charsets = array('Undefined');
417
}
418
}
419
420
421
/**
422
* Is Browser
423
*
424
* @return bool
425
*/
426
public function is_browser($key = NULL) {
427
if ( ! $this->is_browser) {
428
return FALSE;
429
}
430
431
// No need to be specific, it's a browser
432
if ($key === NULL) {
433
return TRUE;
434
}
435
436
// Check for a specific browser
437
return array_key_exists($key, $this->browsers) && ($this->browser === $this->browsers[$key]);
438
}
439
440
441
/**
442
* Is Robot
443
*
444
* @return bool
445
*/
446
public function is_robot($key = NULL) {
447
if ( ! $this->is_robot) {
448
return FALSE;
449
}
450
451
// No need to be specific, it's a robot
452
if ($key === NULL) {
453
return TRUE;
454
}
455
456
// Check for a specific robot
457
return array_key_exists($key, $this->robots) && ($this->robot === $this->robots[$key]);
458
}
459
460
461
/**
462
* Is Mobile
463
*
464
* @return bool
465
*/
466
public function is_mobile($key = NULL) {
467
if ( ! $this->is_mobile) {
468
return FALSE;
469
}
470
471
// No need to be specific, it's a mobile
472
if ($key === NULL) {
473
return TRUE;
474
}
475
476
// Check for a specific robot
477
return array_key_exists($key, $this->mobiles) && ($this->mobile === $this->mobiles[$key]);
478
}
479
480
481
/**
482
* Is this a referral from another site?
483
*
484
* @return bool
485
*/
486
public function is_referral() {
487
return ( ! isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
488
}
489
490
491
/**
492
* Returns a string containing the full user agent string. Typically it will be something like this:
493
* Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2
494
*
495
* @return string
496
*/
497
public function agent_string() {
498
return $this->agent;
499
}
500
501
502
/**
503
* Get Platform
504
*
505
* @return string
506
*/
507
public function platform() {
508
return $this->platform;
509
}
510
511
512
/**
513
* Get Browser Name
514
*
515
* @return string
516
*/
517
public function browser() {
518
return $this->browser;
519
}
520
521
522
/**
523
* Get the Browser Version
524
*
525
* @return string
526
*/
527
public function version() {
528
return $this->version;
529
}
530
531
532
/**
533
* Get The Robot Name
534
*
535
* @return string
536
*/
537
public function robot() {
538
return $this->robot;
539
}
540
541
/**
542
* Get the Mobile Device
543
*
544
* @return string
545
*/
546
public function mobile() {
547
return $this->mobile;
548
}
549
550
551
/**
552
* Get the referrer
553
*
554
* @return bool
555
*/
556
public function referrer() {
557
return ( ! isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
558
}
559
560
561
/**
562
* Get the accepted languages
563
*
564
* @return array
565
*/
566
public function languages() {
567
if (count($this->languages) == 0) {
568
$this->_set_languages();
569
}
570
571
return $this->languages;
572
}
573
574
575
/**
576
* Get the accepted Character Sets
577
*
578
* @return array
579
*/
580
public function charsets() {
581
if (count($this->charsets) == 0) {
582
$this->_set_charsets();
583
}
584
585
return $this->charsets;
586
}
587
588
589
/**
590
* Test for a particular language
591
*
592
* @return bool
593
*/
594
public function accept_lang($lang = 'en') {
595
return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
596
}
597
598
599
/**
600
* Test for a particular character set
601
*
602
* @return bool
603
*/
604
public function accept_charset($charset = 'utf-8') {
605
return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
606
}
607
608
}
609
610
611
/* End of file: ./system/libraries/user_agent/user_agent_library.php */
Page URI: http://www.infopotato.com/index.php/code/library/user_agent/
