10 defined(
'JPATH_PLATFORM') or die;
31 protected $majorVersion = 0;
37 protected $minorVersion = 0;
43 protected $browser =
'';
49 protected $agent =
'';
55 protected $lowerAgent =
'';
61 protected $accept =
'';
67 protected $acceptParsed = array();
73 protected $platform =
'';
79 protected $robots = array(
112 'Mediapartners-Google',
125 'www.almaden.ibm.com/cs/crawler',
132 protected $mobile =
false;
141 protected $images = array(
'jpeg',
'gif',
'png',
'pjpeg',
'x-png',
'bmp');
147 protected static $instances = array();
157 public function __construct($userAgent = null, $accept = null)
159 $this->match($userAgent, $accept);
173 public static function getInstance($userAgent = null, $accept = null)
175 $signature = serialize(array($userAgent, $accept));
177 if (empty(self::$instances[$signature]))
179 self::$instances[$signature] =
new JBrowser($userAgent, $accept);
182 return self::$instances[$signature];
196 public function match($userAgent = null, $accept = null)
199 if (is_null($userAgent))
201 if (isset($_SERVER[
'HTTP_USER_AGENT']))
203 $this->agent = trim($_SERVER[
'HTTP_USER_AGENT']);
208 $this->agent = $userAgent;
211 $this->lowerAgent = strtolower($this->agent);
214 if (is_null($accept))
216 if (isset($_SERVER[
'HTTP_ACCEPT']))
218 $this->accept = strtolower(trim($_SERVER[
'HTTP_ACCEPT']));
223 $this->accept = strtolower($accept);
226 if (!empty($this->agent))
228 $this->_setPlatform();
230 if (strpos($this->lowerAgent,
'mobileexplorer') !==
false
231 || strpos($this->lowerAgent,
'openwave') !==
false
232 || strpos($this->lowerAgent,
'opera mini') !==
false
233 || strpos($this->lowerAgent,
'opera mobi') !==
false
234 || strpos($this->lowerAgent,
'operamini') !==
false)
236 $this->mobile =
true;
238 elseif (preg_match(
'|Opera[/ ]([0-9.]+)|', $this->agent, $version))
240 $this->setBrowser(
'opera');
241 list ($this->majorVersion, $this->minorVersion) = explode(
'.', $version[1]);
245 if ($this->majorVersion == 9 && $this->minorVersion >= 80)
247 $this->identifyBrowserVersion();
250 elseif (preg_match(
'|Chrome[/ ]([0-9.]+)|', $this->agent, $version))
252 $this->setBrowser(
'chrome');
253 list ($this->majorVersion, $this->minorVersion) = explode(
'.', $version[1]);
255 elseif (preg_match(
'|CrMo[/ ]([0-9.]+)|', $this->agent, $version))
257 $this->setBrowser(
'chrome');
258 list ($this->majorVersion, $this->minorVersion) = explode(
'.', $version[1]);
260 elseif (preg_match(
'|CriOS[/ ]([0-9.]+)|', $this->agent, $version))
262 $this->setBrowser(
'chrome');
263 list ($this->majorVersion, $this->minorVersion) = explode(
'.', $version[1]);
264 $this->mobile =
true;
266 elseif (strpos($this->lowerAgent,
'elaine/') !==
false
267 || strpos($this->lowerAgent,
'palmsource') !==
false
268 || strpos($this->lowerAgent,
'digital paths') !==
false)
270 $this->setBrowser(
'palm');
271 $this->mobile =
true;
273 elseif ((preg_match(
'|MSIE ([0-9.]+)|', $this->agent, $version)) || (preg_match(
'|Internet Explorer/([0-9.]+)|', $this->agent, $version)))
275 $this->setBrowser(
'msie');
277 if (strpos($version[1],
'.') !==
false)
279 list ($this->majorVersion, $this->minorVersion) = explode(
'.', $version[1]);
283 $this->majorVersion = $version[1];
284 $this->minorVersion = 0;
291 if (preg_match(
'/; (120x160|240x280|240x320|320x320)\)/', $this->agent))
293 $this->mobile =
true;
296 elseif (preg_match(
'|amaya/([0-9.]+)|', $this->agent, $version))
298 $this->setBrowser(
'amaya');
299 $this->majorVersion = $version[1];
301 if (isset($version[2]))
303 $this->minorVersion = $version[2];
306 elseif (preg_match(
'|ANTFresco/([0-9]+)|', $this->agent, $version))
308 $this->setBrowser(
'fresco');
310 elseif (strpos($this->lowerAgent,
'avantgo') !==
false)
312 $this->setBrowser(
'avantgo');
313 $this->mobile =
true;
315 elseif (preg_match(
'|Konqueror/([0-9]+)|', $this->agent, $version) || preg_match(
'|Safari/([0-9]+)\.?([0-9]+)?|', $this->agent, $version))
319 $this->setBrowser(
'konqueror');
320 $this->majorVersion = $version[1];
322 if (isset($version[2]))
324 $this->minorVersion = $version[2];
327 if (strpos($this->agent,
'Safari') !==
false && $this->majorVersion >= 60)
330 $this->setBrowser(
'safari');
331 $this->identifyBrowserVersion();
334 elseif (preg_match(
'|Mozilla/([0-9.]+)|', $this->agent, $version))
336 $this->setBrowser(
'mozilla');
338 list ($this->majorVersion, $this->minorVersion) = explode(
'.', $version[1]);
340 elseif (preg_match(
'|Lynx/([0-9]+)|', $this->agent, $version))
342 $this->setBrowser(
'lynx');
344 elseif (preg_match(
'|Links \(([0-9]+)|', $this->agent, $version))
346 $this->setBrowser(
'links');
348 elseif (preg_match(
'|HotJava/([0-9]+)|', $this->agent, $version))
350 $this->setBrowser(
'hotjava');
352 elseif (strpos($this->agent,
'UP/') !==
false || strpos($this->agent,
'UP.B') !==
false || strpos($this->agent,
'UP.L') !==
false)
354 $this->setBrowser(
'up');
355 $this->mobile =
true;
357 elseif (strpos($this->agent,
'Xiino/') !==
false)
359 $this->setBrowser(
'xiino');
360 $this->mobile =
true;
362 elseif (strpos($this->agent,
'Palmscape/') !==
false)
364 $this->setBrowser(
'palmscape');
365 $this->mobile =
true;
367 elseif (strpos($this->agent,
'Nokia') !==
false)
369 $this->setBrowser(
'nokia');
370 $this->mobile =
true;
372 elseif (strpos($this->agent,
'Ericsson') !==
false)
374 $this->setBrowser(
'ericsson');
375 $this->mobile =
true;
377 elseif (strpos($this->lowerAgent,
'wap') !==
false)
379 $this->setBrowser(
'wap');
380 $this->mobile =
true;
382 elseif (strpos($this->lowerAgent,
'docomo') !==
false || strpos($this->lowerAgent,
'portalmmm') !==
false)
384 $this->setBrowser(
'imode');
385 $this->mobile =
true;
387 elseif (strpos($this->agent,
'BlackBerry') !==
false)
389 $this->setBrowser(
'blackberry');
390 $this->mobile =
true;
392 elseif (strpos($this->agent,
'MOT-') !==
false)
394 $this->setBrowser(
'motorola');
395 $this->mobile =
true;
397 elseif (strpos($this->lowerAgent,
'j-') !==
false)
399 $this->setBrowser(
'mml');
400 $this->mobile =
true;
416 protected function _setPlatform()
418 if (strpos($this->lowerAgent,
'wind') !==
false)
420 $this->platform =
'win';
422 elseif (strpos($this->lowerAgent,
'mac') !==
false)
424 $this->platform =
'mac';
428 $this->platform =
'unix';
439 public function getPlatform()
441 return $this->platform;
452 protected function identifyBrowserVersion()
454 if (preg_match(
'|Version[/ ]([0-9.]+)|', $this->agent, $version))
456 list ($this->majorVersion, $this->minorVersion) = explode(
'.', $version[1]);
462 $this->majorVersion = 0;
463 $this->minorVersion = 0;
476 public function setBrowser($browser)
478 $this->browser = $browser;
488 public function getBrowser()
490 return $this->browser;
500 public function getMajor()
502 return $this->majorVersion;
512 public function getMinor()
514 return $this->minorVersion;
524 public function getVersion()
526 return $this->majorVersion .
'.' . $this->minorVersion;
536 public function getAgentString()
548 public function getHTTPProtocol()
550 if (isset($_SERVER[
'SERVER_PROTOCOL']))
552 if (($pos = strrpos($_SERVER[
'SERVER_PROTOCOL'],
'/')))
554 return substr($_SERVER[
'SERVER_PROTOCOL'], $pos + 1);
574 public function isViewable($mimetype)
576 $mimetype = strtolower($mimetype);
577 list ($type, $subtype) = explode(
'/', $mimetype);
579 if (!empty($this->accept))
581 $wildcard_match =
false;
583 if (strpos($this->accept, $mimetype) !==
false)
588 if (strpos($this->accept,
'*/*') !==
false)
590 $wildcard_match =
true;
592 if ($type !=
'image')
599 if ($this->isBrowser(
'mozilla') && ($mimetype ==
'image/pjpeg') && (strpos($this->accept,
'image/jpeg') !==
false))
604 if (!$wildcard_match)
610 if (!$this->hasFeature(
'images') || ($type !=
'image'))
615 return (in_array($subtype, $this->images));
627 public function isBrowser($browser)
629 return ($this->browser === $browser);
639 public function isRobot()
641 foreach ($this->robots as $robot)
643 if (strpos($this->agent, $robot) !==
false)
659 public function isMobile()
661 return $this->mobile;
672 public function isSSLConnection()
674 JLog::add(
'JBrowser::isSSLConnection() is deprecated. Use the isSSLConnection method on the application object instead.',
677 return ((isset($_SERVER[
'HTTPS']) && ($_SERVER[
'HTTPS'] ==
'on')) || getenv(
'SSL_PROTOCOL_VERSION'));