This repository has been archived by the owner on Nov 26, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
browser.php
667 lines (614 loc) · 14.8 KB
/
browser.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<?php
/**
* @package Joomla.Platform
* @subpackage Environment
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* Browser class, provides capability information about the current web client.
*
* Browser identification is performed by examining the HTTP_USER_AGENT
* environment variable provided by the web server.
*
* This class has many influences from the lib/Browser.php code in
* version 3 of Horde by Chuck Hagenbuch and Jon Parise.
*
* @package Joomla.Platform
* @subpackage Environment
* @since 11.1
*/
class JBrowser
{
/**
* @var integer Major version number
* @since 12.1
*/
protected $majorVersion = 0;
/**
* @var integer Minor version number
* @since 12.1
*/
protected $minorVersion = 0;
/**
* @var string Browser name.
* @since 12.1
*/
protected $browser = '';
/**
* @var string Full user agent string.
* @since 12.1
*/
protected $agent = '';
/**
* @var string Lower-case user agent string
* @since 12.1
*/
protected $lowerAgent = '';
/**
* @var string HTTP_ACCEPT string.
* @since 12.1
*/
protected $accept = '';
/**
* @var array Parsed HTTP_ACCEPT string
* @since 12.1
*/
protected $acceptParsed = array();
/**
* @var string Platform the browser is running on
* @since 12.1
*/
protected $platform = '';
/**
* @var array Known robots.
* @since 12.1
*/
protected $robots = array(
/* The most common ones. */
'Googlebot',
'msnbot',
'Slurp',
'Yahoo',
/* The rest alphabetically. */
'Arachnoidea',
'ArchitextSpider',
'Ask Jeeves',
'B-l-i-t-z-Bot',
'Baiduspider',
'BecomeBot',
'cfetch',
'ConveraCrawler',
'ExtractorPro',
'FAST-WebCrawler',
'FDSE robot',
'fido',
'geckobot',
'Gigabot',
'Girafabot',
'grub-client',
'Gulliver',
'HTTrack',
'ia_archiver',
'InfoSeek',
'kinjabot',
'KIT-Fireball',
'larbin',
'LEIA',
'lmspider',
'Lycos_Spider',
'Mediapartners-Google',
'MuscatFerret',
'NaverBot',
'OmniExplorer_Bot',
'polybot',
'Pompos',
'Scooter',
'Teoma',
'TheSuBot',
'TurnitinBot',
'Ultraseek',
'ViolaBot',
'webbandit',
'www.almaden.ibm.com/cs/crawler',
'ZyBorg');
/**
* @var boolean Is this a mobile browser?
* @since 12.1
*/
protected $mobile = false;
/**
* List of viewable image MIME subtypes.
* This list of viewable images works for IE and Netscape/Mozilla.
*
* @var array
* @since 12.1
*/
protected $images = array('jpeg', 'gif', 'png', 'pjpeg', 'x-png', 'bmp');
/**
* @var array JBrowser instances container.
* @since 11.3
*/
protected static $instances = array();
/**
* Create a browser instance (constructor).
*
* @param string $userAgent The browser string to parse.
* @param string $accept The HTTP_ACCEPT settings to use.
*
* @since 11.1
*/
public function __construct($userAgent = null, $accept = null)
{
$this->match($userAgent, $accept);
}
/**
* Returns the global Browser object, only creating it
* if it doesn't already exist.
*
* @param string $userAgent The browser string to parse.
* @param string $accept The HTTP_ACCEPT settings to use.
*
* @return JBrowser The Browser object.
*
* @since 11.1
*/
static public function getInstance($userAgent = null, $accept = null)
{
$signature = serialize(array($userAgent, $accept));
if (empty(self::$instances[$signature]))
{
self::$instances[$signature] = new JBrowser($userAgent, $accept);
}
return self::$instances[$signature];
}
/**
* Parses the user agent string and inititializes the object with
* all the known features and quirks for the given browser.
*
* @param string $userAgent The browser string to parse.
* @param string $accept The HTTP_ACCEPT settings to use.
*
* @return void
*
* @since 11.1
*/
public function match($userAgent = null, $accept = null)
{
// Set our agent string.
if (is_null($userAgent))
{
if (isset($_SERVER['HTTP_USER_AGENT']))
{
$this->agent = trim($_SERVER['HTTP_USER_AGENT']);
}
}
else
{
$this->agent = $userAgent;
}
$this->lowerAgent = strtolower($this->agent);
// Set our accept string.
if (is_null($accept))
{
if (isset($_SERVER['HTTP_ACCEPT']))
{
$this->accept = strtolower(trim($_SERVER['HTTP_ACCEPT']));
}
}
else
{
$this->accept = strtolower($accept);
}
if (!empty($this->agent))
{
$this->_setPlatform();
if (strpos($this->lowerAgent, 'mobileexplorer') !== false
|| strpos($this->lowerAgent, 'openwave') !== false
|| strpos($this->lowerAgent, 'opera mini') !== false
|| strpos($this->lowerAgent, 'opera mobi') !== false
|| strpos($this->lowerAgent, 'operamini') !== false)
{
$this->mobile = true;
}
elseif (preg_match('|Opera[/ ]([0-9.]+)|', $this->agent, $version))
{
$this->setBrowser('opera');
list ($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
/* Due to changes in Opera UA, we need to check Version/xx.yy,
* but only if version is > 9.80. See: http://dev.opera.com/articles/view/opera-ua-string-changes/ */
if ($this->majorVersion == 9 && $this->minorVersion >= 80)
{
$this->identifyBrowserVersion();
}
}
elseif (preg_match('|Chrome[/ ]([0-9.]+)|', $this->agent, $version))
{
$this->setBrowser('chrome');
list ($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
}
elseif (preg_match('|CrMo[/ ]([0-9.]+)|', $this->agent, $version))
{
$this->setBrowser('chrome');
list ($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
}
elseif (strpos($this->lowerAgent, 'elaine/') !== false
|| strpos($this->lowerAgent, 'palmsource') !== false
|| strpos($this->lowerAgent, 'digital paths') !== false)
{
$this->setBrowser('palm');
$this->mobile = true;
}
elseif ((preg_match('|MSIE ([0-9.]+)|', $this->agent, $version)) || (preg_match('|Internet Explorer/([0-9.]+)|', $this->agent, $version)))
{
$this->setBrowser('msie');
if (strpos($version[1], '.') !== false)
{
list ($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
}
else
{
$this->majorVersion = $version[1];
$this->minorVersion = 0;
}
/* Some Handhelds have their screen resolution in the
* user agent string, which we can use to look for
* mobile agents.
*/
if (preg_match('/; (120x160|240x280|240x320|320x320)\)/', $this->agent))
{
$this->mobile = true;
}
}
elseif (preg_match('|amaya/([0-9.]+)|', $this->agent, $version))
{
$this->setBrowser('amaya');
$this->majorVersion = $version[1];
if (isset($version[2]))
{
$this->minorVersion = $version[2];
}
}
elseif (preg_match('|ANTFresco/([0-9]+)|', $this->agent, $version))
{
$this->setBrowser('fresco');
}
elseif (strpos($this->lowerAgent, 'avantgo') !== false)
{
$this->setBrowser('avantgo');
$this->mobile = true;
}
elseif (preg_match('|Konqueror/([0-9]+)|', $this->agent, $version) || preg_match('|Safari/([0-9]+)\.?([0-9]+)?|', $this->agent, $version))
{
// Konqueror and Apple's Safari both use the KHTML
// rendering engine.
$this->setBrowser('konqueror');
$this->majorVersion = $version[1];
if (isset($version[2]))
{
$this->minorVersion = $version[2];
}
if (strpos($this->agent, 'Safari') !== false && $this->majorVersion >= 60)
{
// Safari.
$this->setBrowser('safari');
$this->identifyBrowserVersion();
}
}
elseif (preg_match('|Mozilla/([0-9.]+)|', $this->agent, $version))
{
$this->setBrowser('mozilla');
list ($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
}
elseif (preg_match('|Lynx/([0-9]+)|', $this->agent, $version))
{
$this->setBrowser('lynx');
}
elseif (preg_match('|Links \(([0-9]+)|', $this->agent, $version))
{
$this->setBrowser('links');
}
elseif (preg_match('|HotJava/([0-9]+)|', $this->agent, $version))
{
$this->setBrowser('hotjava');
}
elseif (strpos($this->agent, 'UP/') !== false || strpos($this->agent, 'UP.B') !== false || strpos($this->agent, 'UP.L') !== false)
{
$this->setBrowser('up');
$this->mobile = true;
}
elseif (strpos($this->agent, 'Xiino/') !== false)
{
$this->setBrowser('xiino');
$this->mobile = true;
}
elseif (strpos($this->agent, 'Palmscape/') !== false)
{
$this->setBrowser('palmscape');
$this->mobile = true;
}
elseif (strpos($this->agent, 'Nokia') !== false)
{
$this->setBrowser('nokia');
$this->mobile = true;
}
elseif (strpos($this->agent, 'Ericsson') !== false)
{
$this->setBrowser('ericsson');
$this->mobile = true;
}
elseif (strpos($this->lowerAgent, 'wap') !== false)
{
$this->setBrowser('wap');
$this->mobile = true;
}
elseif (strpos($this->lowerAgent, 'docomo') !== false || strpos($this->lowerAgent, 'portalmmm') !== false)
{
$this->setBrowser('imode');
$this->mobile = true;
}
elseif (strpos($this->agent, 'BlackBerry') !== false)
{
$this->setBrowser('blackberry');
$this->mobile = true;
}
elseif (strpos($this->agent, 'MOT-') !== false)
{
$this->setBrowser('motorola');
$this->mobile = true;
}
elseif (strpos($this->lowerAgent, 'j-') !== false)
{
$this->setBrowser('mml');
$this->mobile = true;
}
}
}
/**
* Match the platform of the browser.
*
* This is a pretty simplistic implementation, but it's intended
* to let us tell what line breaks to send, so it's good enough
* for its purpose.
*
* @return void
*
* @since 11.1
*/
protected function _setPlatform()
{
if (strpos($this->lowerAgent, 'wind') !== false)
{
$this->platform = 'win';
}
elseif (strpos($this->lowerAgent, 'mac') !== false)
{
$this->platform = 'mac';
}
else
{
$this->platform = 'unix';
}
}
/**
* Return the currently matched platform.
*
* @return string The user's platform.
*
* @since 11.1
*/
public function getPlatform()
{
return $this->platform;
}
/**
* Set browser version, not by engine version
* Fallback to use when no other method identify the engine version
*
* @return void
*/
protected function identifyBrowserVersion()
{
if (preg_match('|Version[/ ]([0-9.]+)|', $this->agent, $version))
{
list ($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
return;
}
// Can't identify browser version
$this->majorVersion = 0;
$this->minorVersion = 0;
JLog::add("Can't identify browser version. Agent: " . $this->agent, JLog::NOTICE);
}
/**
* Sets the current browser.
*
* @param string $browser The browser to set as current.
*
* @return void
*
* @since 11.1
*/
public function setBrowser($browser)
{
$this->browser = $browser;
}
/**
* Retrieve the current browser.
*
* @return string The current browser.
*
* @since 11.1
*/
public function getBrowser()
{
return $this->browser;
}
/**
* Retrieve the current browser's major version.
*
* @return integer The current browser's major version
*
* @since 11.1.
*/
public function getMajor()
{
return $this->majorVersion;
}
/**
* Retrieve the current browser's minor version.
*
* @return integer The current browser's minor version.
*
* @since 11.1
*/
public function getMinor()
{
return $this->minorVersion;
}
/**
* Retrieve the current browser's version.
*
* @return string The current browser's version.
*
* @since 11.1
*/
public function getVersion()
{
return $this->majorVersion . '.' . $this->minorVersion;
}
/**
* Return the full browser agent string.
*
* @return string The browser agent string
*
* @since 11.1
*/
public function getAgentString()
{
return $this->agent;
}
/**
* Returns the server protocol in use on the current server.
*
* @return string The HTTP server protocol version.
*
* @since 11.1
*/
public function getHTTPProtocol()
{
if (isset($_SERVER['SERVER_PROTOCOL']))
{
if (($pos = strrpos($_SERVER['SERVER_PROTOCOL'], '/')))
{
return substr($_SERVER['SERVER_PROTOCOL'], $pos + 1);
}
}
return null;
}
/**
* Determines if a browser can display a given MIME type.
*
* Note that image/jpeg and image/pjpeg *appear* to be the same
* entity, but Mozilla doesn't seem to want to accept the latter.
* For our purposes, we will treat them the same.
*
* @param string $mimetype The MIME type to check.
*
* @return boolean True if the browser can display the MIME type.
*
* @since 11.1
*/
public function isViewable($mimetype)
{
$mimetype = strtolower($mimetype);
list ($type, $subtype) = explode('/', $mimetype);
if (!empty($this->accept))
{
$wildcard_match = false;
if (strpos($this->accept, $mimetype) !== false)
{
return true;
}
if (strpos($this->accept, '*/*') !== false)
{
$wildcard_match = true;
if ($type != 'image')
{
return true;
}
}
// Deal with Mozilla pjpeg/jpeg issue
if ($this->isBrowser('mozilla') && ($mimetype == 'image/pjpeg') && (strpos($this->accept, 'image/jpeg') !== false))
{
return true;
}
if (!$wildcard_match)
{
return false;
}
}
if (!$this->hasFeature('images') || ($type != 'image'))
{
return false;
}
return (in_array($subtype, $this->images));
}
/**
* Determine if the given browser is the same as the current.
*
* @param string $browser The browser to check.
*
* @return boolean Is the given browser the same as the current?
*
* @since 11.1
*/
public function isBrowser($browser)
{
return ($this->browser === $browser);
}
/**
* Determines if the browser is a robot or not.
*
* @return boolean True if browser is a known robot.
*
* @since 11.1
*/
public function isRobot()
{
foreach ($this->robots as $robot)
{
if (strpos($this->agent, $robot) !== false)
{
return true;
}
}
return false;
}
/**
* Determines if the browser is mobile version or not.
*
* @return boolean True if browser is a known mobile version.
*
* @since 11.1
*/
public function isMobile()
{
return $this->mobile;
}
/**
* Determine if we are using a secure (SSL) connection.
*
* @return boolean True if using SSL, false if not.
*
* @since 11.1
* @deprecated 13.3 Use the isSSLConnection method on the application object.
*/
public function isSSLConnection()
{
JLog::add('JBrowser::isSSLConnection() is deprecated. Use the isSSLConnection method on the application object instead.',
JLog::WARNING, 'deprecated');
return ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) || getenv('SSL_PROTOCOL_VERSION'));
}
}