Browser_Detection
[ class tree: Browser_Detection ] [ index: Browser_Detection ] [ all elements ]

Source for file BrowserDetection.php

Documentation is available at BrowserDetection.php

  1. <?php
  2.  
  3. /**
  4.  * Browser detection class file.
  5.  * This file contains everything required to use the BrowserDetection class. Tested with PHP 5.3.29 - 7.2.4.
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
  8.  * Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any
  9.  * later version (if any).
  10.  *
  11.  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12.  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13.  * details at: http://www.gnu.org/licenses/lgpl.html
  14.  *
  15.  * @package Browser_Detection
  16.  * @version 2.9.0
  17.  * @last-modified July 15, 2018
  18.  * @author Alexandre Valiquette
  19.  * @copyright Copyright (c) 2018, Wolfcast
  20.  * @link https://wolfcast.com/
  21.  */
  22.  
  23.  
  24. namespace Wolfcast;
  25.  
  26.  
  27. /**
  28.  * The BrowserDetection class facilitates the identification of the user's environment such as Web browser, version,
  29.  * platform and device type.
  30.  *
  31.  * Typical usage:
  32.  *
  33.  * $browser = new Wolfcast\BrowserDetection();
  34.  * if ($browser->getName() == Wolfcast\BrowserDetection::BROWSER_FIREFOX &&
  35.  *     $browser->compareVersions($browser->getVersion(), '5.0') >= 0) {
  36.  *     echo 'You are using FireFox version 5 or greater.';
  37.  * }
  38.  *
  39.  * The class is a rewrite of Chris Schuld's Browser class version 1.9 which is mostly unmaintained since August 20th,
  40.  * 2010. Chris' class was based on the original work from Gary White.
  41.  *
  42.  * Updates:
  43.  *
  44.  * 2018-07-15: Version 2.9.0
  45.  *  + WARNING! Breaking change: new Wolfcast namespace. Use new Wolfcast\BrowserDetection().
  46.  *  + iPad, iPhone and iPod are all under iOS now.
  47.  *  + Added Android Oreo detection.
  48.  *  + Added macOS High Sierra detection.
  49.  *  + Added UC Browser detection.
  50.  *  + Improved regular expressions (even less false positives).
  51.  *  + Removed AOL detection.
  52.  *  + Removed the following Web browsers detection: Amaya, Galeon, NetPositive, OmniWeb, Vivaldi detection (use
  53.  *    addCustomBrowserDetection()).
  54.  *  + Removed the following legacy platforms detection: BeOS, OS/2, SunOS (use addCustomPlatformDetection()).
  55.  *
  56.  * 2016-11-28: Version 2.5.1
  57.  *  + Better detection of 64-bit platforms.
  58.  *
  59.  * 2016-08-19: Version 2.5.0
  60.  *  + Platform version and platform version name are now supported for Mac.
  61.  *  + Fixed platform version name for Android.
  62.  *
  63.  * 2016-08-02: Version 2.4.0
  64.  *  + Platform version and platform version name are now supported for Android.
  65.  *  + Added support for the Samsung Internet browser.
  66.  *  + Added support for the Vivaldi browser.
  67.  *  + Better support for legacy Windows versions.
  68.  *
  69.  * 2016-02-11: Version 2.3.0
  70.  *  + WARNING! Breaking change: public method getBrowser() is renamed to getName().
  71.  *  + WARNING! Breaking change: changed the compareVersions() return values to be more in line with other libraries.
  72.  *  + You can now get the exact platform version (name or version numbers) on which the browser is run on with
  73.  *    getPlatformVersion(). Only working with Windows operating systems at the moment.
  74.  *  + You can now determine if the browser is executed from a 64-bit platform with is64bitPlatform().
  75.  *  + Better detection of mobile platform for Googlebot.
  76.  *
  77.  * 2016-01-04: Version 2.2.0
  78.  *  + Added support for Microsoft Edge.
  79.  *
  80.  * 2014-12-30: Version 2.1.2
  81.  *  + Better detection of Opera.
  82.  *
  83.  * 2014-07-11: Version 2.1.1
  84.  *  + Better detection of mobile devices and platforms.
  85.  *
  86.  * 2014-06-04: Version 2.1.0
  87.  *  + Added support for IE 11+.
  88.  *
  89.  * 2013-05-27: Version 2.0.0 which is (almost) a complete rewrite based on Chris Schuld's Browser class version 1.9 plus
  90.  * changes below.
  91.  *  + Added support for Opera Mobile
  92.  *  + Added support for the Windows Phone (formerly Windows Mobile) platform
  93.  *  + Added support for BlackBerry Tablet OS and BlackBerry 10
  94.  *  + Added support for the Symbian platform
  95.  *  + Added support for Bingbot
  96.  *  + Added support for the Yahoo! Multimedia crawler
  97.  *  + Removed iPhone/iPad/iPod browsers since there are not browsers but platforms - test them with getPlatform()
  98.  *  + Removed support for Shiretoko (Firefox 3.5 alpha/beta) and MSN Browser
  99.  *  + Merged Nokia and Nokia S60
  100.  *  + Updated some deprecated browser names
  101.  *  + Many public methods are now protected
  102.  *  + Documentation updated
  103.  *
  104.  * 2010-07-04:
  105.  *  + Added detection of IE compatibility view - test with getIECompatibilityView()
  106.  *  + Added support for all (deprecated) Netscape versions
  107.  *  + Added support for Safari < 3.0
  108.  *  + Better Firefox version parsing
  109.  *  + Better Opera version parsing
  110.  *  + Better Mozilla detection
  111.  *
  112.  * @package Browser_Detection
  113.  * @version 2.9.0
  114.  * @last-modified July 15, 2018
  115.  * @author Alexandre Valiquette, Chris Schuld, Gary White
  116.  * @copyright Copyright (c) 2018, Wolfcast
  117.  * @license http://www.gnu.org/licenses/lgpl.html
  118.  * @link https://wolfcast.com/
  119.  * @link https://wolfcast.com/open-source/browser-detection/tutorial.php
  120.  * @link http://chrisschuld.com/
  121.  * @link http://www.apptools.com/phptools/browser/
  122.  */
  123. {
  124.  
  125.     /**#@+
  126.      * Constant for the name of the Web browser.
  127.      */
  128.     const BROWSER_ANDROID 'Android';
  129.     const BROWSER_BINGBOT 'Bingbot';
  130.     const BROWSER_BLACKBERRY 'BlackBerry';
  131.     const BROWSER_CHROME 'Chrome';
  132.     const BROWSER_EDGE 'Edge';
  133.     const BROWSER_FIREBIRD 'Firebird';
  134.     const BROWSER_FIREFOX 'Firefox';
  135.     const BROWSER_GOOGLEBOT 'Googlebot';
  136.     const BROWSER_ICAB 'iCab';
  137.     const BROWSER_ICECAT 'GNU IceCat';
  138.     const BROWSER_ICEWEASEL 'GNU IceWeasel';
  139.     const BROWSER_IE 'Internet Explorer';
  140.     const BROWSER_IE_MOBILE 'Internet Explorer Mobile';
  141.     const BROWSER_KONQUEROR 'Konqueror';
  142.     const BROWSER_LYNX 'Lynx';
  143.     const BROWSER_MOZILLA 'Mozilla';
  144.     const BROWSER_MSNBOT 'MSNBot';
  145.     const BROWSER_MSNTV 'MSN TV';
  146.     const BROWSER_NETSCAPE 'Netscape';
  147.     const BROWSER_NOKIA 'Nokia Browser';
  148.     const BROWSER_OPERA 'Opera';
  149.     const BROWSER_OPERA_MINI 'Opera Mini';
  150.     const BROWSER_OPERA_MOBILE 'Opera Mobile';
  151.     const BROWSER_PHOENIX 'Phoenix';
  152.     const BROWSER_SAFARI 'Safari';
  153.     const BROWSER_SAMSUNG 'Samsung Internet';
  154.     const BROWSER_SLURP 'Yahoo! Slurp';
  155.     const BROWSER_TABLET_OS 'BlackBerry Tablet OS';
  156.     const BROWSER_UC 'UC Browser';
  157.     const BROWSER_UNKNOWN 'unknown';
  158.     const BROWSER_W3CVALIDATOR 'W3C Validator';
  159.     const BROWSER_YAHOO_MM 'Yahoo! Multimedia';
  160.     /**#@-*/
  161.  
  162.     /**#@+
  163.      * Constant for the name of the platform on which the Web browser runs.
  164.      */
  165.     const PLATFORM_ANDROID 'Android';
  166.     const PLATFORM_BLACKBERRY 'BlackBerry';
  167.     const PLATFORM_FREEBSD 'FreeBSD';
  168.     const PLATFORM_IOS 'iOS';
  169.     const PLATFORM_LINUX 'Linux';
  170.     const PLATFORM_MACINTOSH 'Macintosh';
  171.     const PLATFORM_NETBSD 'NetBSD';
  172.     const PLATFORM_NOKIA 'Nokia';
  173.     const PLATFORM_OPENBSD 'OpenBSD';
  174.     const PLATFORM_OPENSOLARIS 'OpenSolaris';
  175.     const PLATFORM_SYMBIAN 'Symbian';
  176.     const PLATFORM_UNKNOWN 'unknown';
  177.     const PLATFORM_VERSION_UNKNOWN 'unknown';
  178.     const PLATFORM_WINDOWS 'Windows';
  179.     const PLATFORM_WINDOWS_CE 'Windows CE';
  180.     const PLATFORM_WINDOWS_PHONE 'Windows Phone';
  181.     /**#@-*/
  182.  
  183.     /**
  184.      * Version unknown constant.
  185.      */
  186.     const VERSION_UNKNOWN 'unknown';
  187.  
  188.  
  189.     /**
  190.      * @var string 
  191.      * @access private
  192.      */
  193.     private $_agent = '';
  194.  
  195.     /**
  196.      * @var string 
  197.      * @access private
  198.      */
  199.     private $_browserName = '';
  200.  
  201.     /**
  202.      * @var string 
  203.      * @access private
  204.      */
  205.     private $_compatibilityViewName = '';
  206.  
  207.     /**
  208.      * @var string 
  209.      * @access private
  210.      */
  211.     private $_compatibilityViewVer = '';
  212.  
  213.     /**
  214.      * @var array 
  215.      * @access private
  216.      */
  217.     private $_customBrowserDetection = array();
  218.  
  219.     /**
  220.      * @var array 
  221.      * @access private
  222.      */
  223.     private $_customPlatformDetection = array();
  224.  
  225.     /**
  226.      * @var boolean 
  227.      * @access private
  228.      */
  229.     private $_is64bit = false;
  230.  
  231.     /**
  232.      * @var boolean 
  233.      * @access private
  234.      */
  235.     private $_isMobile = false;
  236.  
  237.     /**
  238.      * @var boolean 
  239.      * @access private
  240.      */
  241.     private $_isRobot = false;
  242.  
  243.     /**
  244.      * @var string 
  245.      * @access private
  246.      */
  247.     private $_platform = '';
  248.  
  249.     /**
  250.      * @var string 
  251.      * @access private
  252.      */
  253.     private $_platformVersion = '';
  254.  
  255.     /**
  256.      * @var string 
  257.      * @access private
  258.      */
  259.     private $_version = '';
  260.  
  261.  
  262.     //--- MAGIC METHODS ------------------------------------------------------------------------------------------------
  263.  
  264.  
  265.     /**
  266.      * BrowserDetection class constructor.
  267.      * @param string $useragent (optional) The user agent to work with. Leave empty for the current user agent
  268.      *  (contained in $_SERVER['HTTP_USER_AGENT']).
  269.      */
  270.     public function __construct($useragent '')
  271.     {
  272.         $this->setUserAgent($useragent);
  273.     }
  274.  
  275.     /**
  276.      * Determine how the class will react when it is treated like a string.
  277.      * @return string Returns an HTML formatted string with a summary of the browser informations.
  278.      */
  279.     public function __toString()
  280.     {
  281.         $result '';
  282.  
  283.         $values array();
  284.         $values[array('label' => 'User agent''value' => $this->getUserAgent());
  285.         $values[array('label' => 'Browser name''value' => $this->getName());
  286.         $values[array('label' => 'Browser version''value' => $this->getVersion());
  287.         $values[array('label' => 'Platform family''value' => $this->getPlatform());
  288.         $values[array('label' => 'Platform version''value' => $this->getPlatformVersion(true));
  289.         $values[array('label' => 'Platform version name''value' => $this->getPlatformVersion());
  290.         $values[array('label' => 'Platform is 64-bit''value' => $this->is64bitPlatform('true' 'false');
  291.         $values[array('label' => 'Is mobile''value' => $this->isMobile('true' 'false');
  292.         $values[array('label' => 'Is robot''value' => $this->isRobot('true' 'false');
  293.         $values[array('label' => 'IE is in compatibility view''value' => $this->isInIECompatibilityView('true' 'false');
  294.         $values[array('label' => 'Emulated IE version''value' => $this->isInIECompatibilityView($this->getIECompatibilityView('Not applicable');
  295.         $values[array('label' => 'Is Chrome Frame''value' => $this->isChromeFrame('true' 'false');
  296.  
  297.         foreach ($values as $currVal{
  298.             $result .= '<strong>' htmlspecialchars($currVal['label']ENT_NOQUOTES':</strong> ' $currVal['value''<br />' PHP_EOL;
  299.         }
  300.  
  301.         return $result;
  302.     }
  303.  
  304.  
  305.     //--- PUBLIC MEMBERS -----------------------------------------------------------------------------------------------
  306.  
  307.  
  308.     /**
  309.      * Dynamically add support for a new Web browser.
  310.      * @param string $browserName The Web browser name (used for display).
  311.      * @param mixed $uaNameToLookFor (optional) The string (or array of strings) representing the browser name to find
  312.      *  in the user agent. If omitted, $browserName will be used.
  313.      * @param boolean $isMobile (optional) Determines if the browser is from a mobile device.
  314.      * @param boolean $isRobot (optional) Determines if the browser is a robot or not.
  315.      * @param string $separator (optional) The separator string used to split the browser name and the version number in
  316.      *  the user agent.
  317.      * @param boolean $uaNameFindWords (optional) Determines if the browser name to find should match a word instead of
  318.      *  a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
  319.      *  When set to false, the browser name can be found anywhere in the user agent string.
  320.      * @see removeCustomBrowserDetection()
  321.      * @return boolean Returns true if the custom rule has been added, false otherwise.
  322.      */
  323.     public function addCustomBrowserDetection($browserName$uaNameToLookFor ''$isMobile false$isRobot false$separator '/'$uaNameFindWords true)
  324.     {
  325.         if ($browserName == ''{
  326.             return false;
  327.         }
  328.         if (array_key_exists($browserName$this->_customBrowserDetection)) {
  329.             unset($this->_customBrowserDetection[$browserName]);
  330.         }
  331.         if ($uaNameToLookFor == ''{
  332.             $uaNameToLookFor $browserName;
  333.         }
  334.         $this->_customBrowserDetection[$browserNamearray('uaNameToLookFor' => $uaNameToLookFor'isMobile' => $isMobile == true'isRobot' => $isRobot == true,
  335.                                                              'separator' => $separator'uaNameFindWords' => $uaNameFindWords == true);
  336.         return true;
  337.     }
  338.  
  339.     /**
  340.      * Dynamically add support for a new platform.
  341.      * @param string $platformName The platform name (used for display).
  342.      * @param mixed $platformNameToLookFor (optional) The string (or array of strings) representing the platform name to
  343.      *  find in the user agent. If omitted, $platformName will be used.
  344.      * @param boolean $isMobile (optional) Determines if the platform is from a mobile device.
  345.      * @see removeCustomPlatformDetection()
  346.      * @return boolean Returns true if the custom rule has been added, false otherwise.
  347.      */
  348.     public function addCustomPlatformDetection($platformName$platformNameToLookFor ''$isMobile false)
  349.     {
  350.         if ($platformName == ''{
  351.             return false;
  352.         }
  353.         if (array_key_exists($platformName$this->_customPlatformDetection)) {
  354.             unset($this->_customPlatformDetection[$platformName]);
  355.         }
  356.         if ($platformNameToLookFor == ''{
  357.             $platformNameToLookFor $platformName;
  358.         }
  359.         $this->_customPlatformDetection[$platformNamearray('platformNameToLookFor' => $platformNameToLookFor'isMobile' => $isMobile == true);
  360.         return true;
  361.     }
  362.  
  363.     /**
  364.      * Compare two version number strings.
  365.      * @param string $sourceVer The source version number.
  366.      * @param string $compareVer The version number to compare with the source version number.
  367.      * @return int Returns -1 if $sourceVer < $compareVer, 0 if $sourceVer == $compareVer or 1 if $sourceVer >
  368.      *  $compareVer.
  369.      */
  370.     public function compareVersions($sourceVer$compareVer)
  371.     {
  372.         $sourceVer explode('.'$sourceVer);
  373.         foreach ($sourceVer as $k => $v{
  374.             $sourceVer[$k$this->parseInt($v);
  375.         }
  376.  
  377.         $compareVer explode('.'$compareVer);
  378.         foreach ($compareVer as $k => $v{
  379.             $compareVer[$k$this->parseInt($v);
  380.         }
  381.  
  382.         if (count($sourceVer!= count($compareVer)) {
  383.             if (count($sourceVercount($compareVer)) {
  384.                 for ($i count($compareVer)$i count($sourceVer)$i++{
  385.                     $compareVer[$i0;
  386.                 }
  387.             else {
  388.                 for ($i count($sourceVer)$i count($compareVer)$i++{
  389.                     $sourceVer[$i0;
  390.                 }
  391.             }
  392.         }
  393.  
  394.         foreach ($sourceVer as $i => $srcVerPart{
  395.             if ($srcVerPart $compareVer[$i]{
  396.                 return 1;
  397.             else {
  398.                 if ($srcVerPart $compareVer[$i]{
  399.                     return -1;
  400.                 }
  401.             }
  402.         }
  403.  
  404.         return 0;
  405.     }
  406.  
  407.     /**
  408.      * Get the name of the browser. All of the return values are class constants. You can compare them like this:
  409.      * $myBrowserInstance->getName() == BrowserDetection::BROWSER_FIREFOX.
  410.      * @return string Returns the name of the browser.
  411.      */
  412.     public function getName()
  413.     {
  414.         return $this->_browserName;
  415.     }
  416.  
  417.     /**
  418.      * Get the name and version of the browser emulated in the compatibility view mode (if any). Since Internet
  419.      * Explorer 8, IE can be put in compatibility mode to make websites that were created for older browsers, especially
  420.      * IE 6 and 7, look better in IE 8+ which renders web pages closer to the standards and thus differently from those
  421.      * older versions of IE.
  422.      * @param boolean $asArray (optional) Determines if the return value must be an array (true) or a string (false).
  423.      * @return mixed If a string was requested, the function returns the name and version of the browser emulated in
  424.      *  the compatibility view mode or an empty string if the browser is not in compatibility view mode. If an array was
  425.      *  requested, an array with the keys 'browser' and 'version' is returned.
  426.      */
  427.     public function getIECompatibilityView($asArray false)
  428.     {
  429.         if ($asArray{
  430.             return array('browser' => $this->_compatibilityViewName'version' => $this->_compatibilityViewVer);
  431.         else {
  432.             return trim($this->_compatibilityViewName . ' ' $this->_compatibilityViewVer);
  433.         }
  434.     }
  435.  
  436.     /**
  437.      * Return the BrowserDetection class version.
  438.      * @return string Returns the version as a sting with the #.#.# format.
  439.      */
  440.     public function getLibVersion()
  441.     {
  442.         return '2.9.0';
  443.     }
  444.  
  445.     /**
  446.      * Get the name of the platform family on which the browser is run on (such as Windows, Apple, etc.). All of
  447.      * the return values are class constants. You can compare them like this:
  448.      * $myBrowserInstance->getPlatform() == BrowserDetection::PLATFORM_ANDROID.
  449.      * @return string Returns the name of the platform or BrowserDetection::PLATFORM_UNKNOWN if unknown.
  450.      */
  451.     public function getPlatform()
  452.     {
  453.         return $this->_platform;
  454.     }
  455.  
  456.     /**
  457.      * Get the platform version on which the browser is run on. It can be returned as a string number like 'NT 6.3' or
  458.      * as a name like 'Windows 8.1'. When returning version string numbers for Windows NT OS families the number is
  459.      * prefixed by 'NT ' to differentiate from older Windows 3.x & 9x release. At the moment only the Windows and
  460.      * Android operating systems are supported.
  461.      * @param boolean $returnVersionNumbers (optional) Determines if the return value must be versions numbers as a
  462.      *  string (true) or the version name (false).
  463.      * @param boolean $returnServerFlavor (optional) Since some Windows NT versions have the same values, this flag
  464.      *  determines if the Server flavor is returned or not. For instance Windows 8.1 and Windows Server 2012 R2 both use
  465.      *  version 6.3. This parameter is only useful when testing for Windows.
  466.      * @return string Returns the version name/version numbers of the platform or the constant PLATFORM_VERSION_UNKNOWN
  467.      *  if unknown.
  468.      */
  469.     public function getPlatformVersion($returnVersionNumbers false$returnServerFlavor false)
  470.     {
  471.         if ($this->_platformVersion == self::PLATFORM_VERSION_UNKNOWN || $this->_platformVersion == ''{
  472.             return self::PLATFORM_VERSION_UNKNOWN;
  473.         }
  474.  
  475.         if ($returnVersionNumbers{
  476.             return $this->_platformVersion;
  477.         else {
  478.             switch ($this->getPlatform()) {
  479.                 case self::PLATFORM_WINDOWS:
  480.                     if (substr($this->_platformVersion03== 'NT '{
  481.                         return $this->windowsNTVerToStr(substr($this->_platformVersion3)$returnServerFlavor);
  482.                     else {
  483.                         return $this->windowsVerToStr($this->_platformVersion);
  484.                     }
  485.                     break;
  486.  
  487.                 case self::PLATFORM_MACINTOSH:
  488.                     return $this->macVerToStr($this->_platformVersion);
  489.                     break;
  490.  
  491.                 case self::PLATFORM_ANDROID:
  492.                     return $this->androidVerToStr($this->_platformVersion);
  493.                     break;
  494.  
  495.                 case self::PLATFORM_IOS:
  496.                     return $this->iOSVerToStr($this->_platformVersion);
  497.                     break;
  498.  
  499.                 defaultreturn self::PLATFORM_VERSION_UNKNOWN;
  500.             }
  501.         }
  502.     }
  503.  
  504.     /**
  505.      * Get the user agent value used by the class to determine the browser details.
  506.      * @return string The user agent string.
  507.      */
  508.     public function getUserAgent()
  509.     {
  510.         return $this->_agent;
  511.     }
  512.  
  513.     /**
  514.      * Get the version of the browser.
  515.      * @return string Returns the version of the browser or BrowserDetection::VERSION_UNKNOWN if unknown.
  516.      */
  517.     public function getVersion()
  518.     {
  519.         return $this->_version;
  520.     }
  521.  
  522.     /**
  523.      * Determine if the browser is executed from a 64-bit platform. Keep in mind that not all platforms/browsers report
  524.      * this and the result may not always be accurate.
  525.      * @return boolean Returns true if the browser is executed from a 64-bit platform.
  526.      */
  527.     public function is64bitPlatform()
  528.     {
  529.         return $this->_is64bit;
  530.     }
  531.  
  532.     /**
  533.      * Determine if the browser runs Google Chrome Frame (it's a plug-in designed for Internet Explorer 6+ based on the
  534.      * open-source Chromium project - it's like a Chrome browser within IE).
  535.      * @return boolean Returns true if the browser is using Google Chrome Frame, false otherwise.
  536.      */
  537.     public function isChromeFrame()
  538.     {
  539.         return $this->containString($this->_agent'chromeframe');
  540.     }
  541.  
  542.     /**
  543.      * Determine if the browser is in compatibility view or not. Since Internet Explorer 8, IE can be put in
  544.      * compatibility mode to make websites that were created for older browsers, especially IE 6 and 7, look better in
  545.      * IE 8+ which renders web pages closer to the standards and thus differently from those older versions of IE.
  546.      * @return boolean Returns true if the browser is in compatibility view, false otherwise.
  547.      */
  548.     public function isInIECompatibilityView()
  549.     {
  550.         return ($this->_compatibilityViewName != ''|| ($this->_compatibilityViewVer != '');
  551.     }
  552.  
  553.     /**
  554.      * Determine if the browser is from a mobile device or not.
  555.      * @return boolean Returns true if the browser is from a mobile device, false otherwise.
  556.      */
  557.     public function isMobile()
  558.     {
  559.         return $this->_isMobile;
  560.     }
  561.  
  562.     /**
  563.      * Determine if the browser is a robot (Googlebot, Bingbot, Yahoo! Slurp...) or not.
  564.      * @return boolean Returns true if the browser is a robot, false otherwise.
  565.      */
  566.     public function isRobot()
  567.     {
  568.         return $this->_isRobot;
  569.     }
  570.  
  571.     /**
  572.      * Remove support for a previously added Web browser.
  573.      * @param string $browserName The Web browser name as used when added.
  574.      * @see addCustomBrowserDetection()
  575.      * @return boolean Returns true if the custom rule has been found and removed, false otherwise.
  576.      */
  577.     public function removeCustomBrowserDetection($browserName)
  578.     {
  579.         if (array_key_exists($browserName$this->_customBrowserDetection)) {
  580.             unset($this->_customBrowserDetection[$browserName]);
  581.             return true;
  582.         }
  583.  
  584.         return false;
  585.     }
  586.  
  587.     /**
  588.      * Remove support for a previously added platform.
  589.      * @param string $platformName The platform name as used when added.
  590.      * @see addCustomPlatformDetection()
  591.      * @return boolean Returns true if the custom rule has been found and removed, false otherwise.
  592.      */
  593.     public function removeCustomPlatformDetection($platformName)
  594.     {
  595.         if (array_key_exists($platformName$this->_customPlatformDetection)) {
  596.             unset($this->_customPlatformDetection[$platformName]);
  597.             return true;
  598.         }
  599.  
  600.         return false;
  601.     }
  602.  
  603.     /**
  604.      * Set the user agent to use with the class.
  605.      * @param string $agentString (optional) The value of the user agent. If an empty string is sent (default),
  606.      *  $_SERVER['HTTP_USER_AGENT'] will be used.
  607.      */
  608.     public function setUserAgent($agentString '')
  609.     {
  610.         if (!is_string($agentString|| trim($agentString== ''{
  611.             if (array_key_exists('HTTP_USER_AGENT'$_SERVER&& is_string($_SERVER['HTTP_USER_AGENT'])) {
  612.                 $agentString $_SERVER['HTTP_USER_AGENT'];
  613.             else {
  614.                 $agentString '';
  615.             }
  616.         }
  617.  
  618.         $this->reset();
  619.         $this->_agent = $agentString;
  620.         $this->detect();
  621.     }
  622.  
  623.  
  624.     //--- PROTECTED MEMBERS --------------------------------------------------------------------------------------------
  625.  
  626.  
  627.     /**
  628.      * Convert the Android version numbers to the operating system name. For instance '1.6' returns 'Donut'.
  629.      * @access protected
  630.      * @param string $androidVer The Android version numbers as a string.
  631.      * @return string The operating system name or the constant PLATFORM_VERSION_UNKNOWN if nothing match the version
  632.      *  numbers.
  633.      */
  634.     protected function androidVerToStr($androidVer)
  635.     {
  636.         //https://en.wikipedia.org/wiki/Android_version_history
  637.  
  638.         if ($this->compareVersions($androidVer'8'>= && $this->compareVersions($androidVer'9'0{
  639.             return 'Oreo';
  640.         else if ($this->compareVersions($androidVer'7'>= && $this->compareVersions($androidVer'8'0{
  641.             return 'Nougat';
  642.         else if ($this->compareVersions($androidVer'6'>= && $this->compareVersions($androidVer'7'0{
  643.             return 'Marshmallow';
  644.         else if ($this->compareVersions($androidVer'5'>= && $this->compareVersions($androidVer'6'0{
  645.             return 'Lollipop';
  646.         else if ($this->compareVersions($androidVer'4.4'>= && $this->compareVersions($androidVer'5'0{
  647.             return 'KitKat';
  648.         else if ($this->compareVersions($androidVer'4.1'>= && $this->compareVersions($androidVer'4.4'0{
  649.             return 'Jelly Bean';
  650.         else if ($this->compareVersions($androidVer'4'>= && $this->compareVersions($androidVer'4.1'0{
  651.             return 'Ice Cream Sandwich';
  652.         else if ($this->compareVersions($androidVer'3'>= && $this->compareVersions($androidVer'4'0{
  653.             return 'Honeycomb';
  654.         else if ($this->compareVersions($androidVer'2.3'>= && $this->compareVersions($androidVer'3'0{
  655.             return 'Gingerbread';
  656.         else if ($this->compareVersions($androidVer'2.2'>= && $this->compareVersions($androidVer'2.3'0{
  657.             return 'Froyo';
  658.         else if ($this->compareVersions($androidVer'2'>= && $this->compareVersions($androidVer'2.2'0{
  659.             return 'Eclair';
  660.         else if ($this->compareVersions($androidVer'1.6'>= && $this->compareVersions($androidVer'2'0{
  661.             return 'Donut';
  662.         else if ($this->compareVersions($androidVer'1.5'>= && $this->compareVersions($androidVer'1.6'0{
  663.             return 'Cupcake';
  664.         else {
  665.             return self::PLATFORM_VERSION_UNKNOWN//Unknown/unnamed Android version
  666.         }
  667.     }
  668.  
  669.     /**
  670.      * Determine if the browser is the Android browser (based on the WebKit layout engine and coupled with Chrome's
  671.      * JavaScript engine) or not.
  672.      * @access protected
  673.      * @return boolean Returns true if the browser is the Android browser, false otherwise.
  674.      */
  675.     protected function checkBrowserAndroid()
  676.     {
  677.         //Android don't use the standard "Android/1.0", it uses "Android 1.0;" instead
  678.         return $this->checkSimpleBrowserUA('Android'$this->_agentself::BROWSER_ANDROIDtrue);
  679.     }
  680.  
  681.     /**
  682.      * Determine if the browser is the Bingbot crawler or not.
  683.      * @access protected
  684.      * @link http://www.bing.com/webmaster/help/which-crawlers-does-bing-use-8c184ec0
  685.      * @return boolean Returns true if the browser is Bingbot, false otherwise.
  686.      */
  687.     protected function checkBrowserBingbot()
  688.     {
  689.         return $this->checkSimpleBrowserUA('bingbot'$this->_agentself::BROWSER_BINGBOTfalsetrue);
  690.     }
  691.  
  692.     /**
  693.      * Determine if the browser is the BlackBerry browser or not.
  694.      * @access protected
  695.      * @link http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/How-to-detect-the-BlackBerry-Browser/ta-p/559862
  696.      * @return boolean Returns true if the browser is the BlackBerry browser, false otherwise.
  697.      */
  698.     protected function checkBrowserBlackBerry()
  699.     {
  700.         $found false;
  701.  
  702.         //Tablet OS check
  703.         if ($this->checkSimpleBrowserUA('RIM Tablet OS'$this->_agentself::BROWSER_TABLET_OStrue)) {
  704.             return true;
  705.         }
  706.  
  707.         //Version 6, 7 & 10 check (versions 8 & 9 does not exists)
  708.         if ($this->checkBrowserUAWithVersion(array('BlackBerry''BB10')$this->_agentself::BROWSER_BLACKBERRYtrue)) {
  709.             if ($this->getVersion(== self::VERSION_UNKNOWN{
  710.                 $found true;
  711.             else {
  712.                 return true;
  713.             }
  714.         }
  715.  
  716.         //Version 4.2 to 5.0 check
  717.         if ($this->checkSimpleBrowserUA('BlackBerry'$this->_agentself::BROWSER_BLACKBERRYtruefalse'/'false)) {
  718.             if ($this->getVersion(== self::VERSION_UNKNOWN{
  719.                 $found true;
  720.             else {
  721.                 return true;
  722.             }
  723.         }
  724.  
  725.         return $found;
  726.     }
  727.  
  728.     /**
  729.      * Determine if the browser is Chrome or not.
  730.      * @access protected
  731.      * @link http://www.google.com/chrome/
  732.      * @return boolean Returns true if the browser is Chrome, false otherwise.
  733.      */
  734.     protected function checkBrowserChrome()
  735.     {
  736.         return $this->checkSimpleBrowserUA('Chrome'$this->_agentself::BROWSER_CHROME);
  737.     }
  738.  
  739.     /**
  740.      * Determine if the browser is among the custom browser rules or not. Rules are checked in the order they were
  741.      * added.
  742.      * @access protected
  743.      * @return boolean Returns true if we found the browser we were looking for in the custom rules, false otherwise.
  744.      */
  745.     protected function checkBrowserCustom()
  746.     {
  747.         foreach ($this->_customBrowserDetection as $browserName => $customBrowser{
  748.             $uaNameToLookFor $customBrowser['uaNameToLookFor'];
  749.             $isMobile $customBrowser['isMobile'];
  750.             $isRobot $customBrowser['isRobot'];
  751.             $separator $customBrowser['separator'];
  752.             $uaNameFindWords $customBrowser['uaNameFindWords'];
  753.             if ($this->checkSimpleBrowserUA($uaNameToLookFor$this->_agent$browserName$isMobile$isRobot$separator$uaNameFindWords)) {
  754.                 return true;
  755.             }
  756.         }
  757.         return false;
  758.     }
  759.  
  760.     /**
  761.      * Determine if the browser is Edge or not.
  762.      * @access protected
  763.      * @return boolean Returns true if the browser is Edge, false otherwise.
  764.      */
  765.     protected function checkBrowserEdge()
  766.     {
  767.         return $this->checkSimpleBrowserUA('Edge'$this->_agentself::BROWSER_EDGE);
  768.     }
  769.  
  770.     /**
  771.      * Determine if the browser is Firebird or not. Firebird was the name of Firefox from version 0.6 to 0.7.1.
  772.      * @access protected
  773.      * @return boolean Returns true if the browser is Firebird, false otherwise.
  774.      */
  775.     protected function checkBrowserFirebird()
  776.     {
  777.         return $this->checkSimpleBrowserUA('Firebird'$this->_agentself::BROWSER_FIREBIRD);
  778.     }
  779.  
  780.     /**
  781.      * Determine if the browser is Firefox or not.
  782.      * @access protected
  783.      * @link http://www.mozilla.org/en-US/firefox/new/
  784.      * @return boolean Returns true if the browser is Firefox, false otherwise.
  785.      */
  786.     protected function checkBrowserFirefox()
  787.     {
  788.         //Safari heavily matches with Firefox, ensure that Safari is filtered out...
  789.         if (preg_match('/.*Firefox[ (\/]*([a-z0-9.-]*)/i'$this->_agent$matches&&
  790.                 !$this->containString($this->_agent'Safari')) {
  791.             $this->setBrowser(self::BROWSER_FIREFOX);
  792.             $this->setVersion($matches[1]);
  793.             $this->setMobile(false);
  794.             $this->setRobot(false);
  795.  
  796.             return true;
  797.         }
  798.  
  799.         return false;
  800.     }
  801.  
  802.     /**
  803.      * Determine if the browser is the Googlebot crawler or not.
  804.      * @access protected
  805.      * @return boolean Returns true if the browser is Googlebot, false otherwise.
  806.      */
  807.     protected function checkBrowserGooglebot()
  808.     {
  809.         if ($this->checkSimpleBrowserUA('Googlebot'$this->_agentself::BROWSER_GOOGLEBOTfalsetrue)) {
  810.  
  811.             if ($this->containString($this->_agent'googlebot-mobile')) {
  812.                 $this->setMobile(true);
  813.             }
  814.  
  815.             return true;
  816.         }
  817.  
  818.         return false;
  819.     }
  820.  
  821.     /**
  822.      * Determine if the browser is iCab or not.
  823.      * @access protected
  824.      * @link http://www.icab.de/
  825.      * @return boolean Returns true if the browser is iCab, false otherwise.
  826.      */
  827.     protected function checkBrowserIcab()
  828.     {
  829.         //Some (early) iCab versions don't use the standard "iCab/1.0", they uses "iCab 1.0;" instead
  830.         return $this->checkSimpleBrowserUA('iCab'$this->_agentself::BROWSER_ICAB);
  831.     }
  832.  
  833.     /**
  834.      * Determine if the browser is GNU IceCat (formerly known as GNU IceWeasel) or not.
  835.      * @access protected
  836.      * @link http://www.gnu.org/software/gnuzilla/
  837.      * @return boolean Returns true if the browser is GNU IceCat, false otherwise.
  838.      */
  839.     protected function checkBrowserIceCat()
  840.     {
  841.         return $this->checkSimpleBrowserUA('IceCat'$this->_agentself::BROWSER_ICECAT);
  842.     }
  843.  
  844.     /**
  845.      * Determine if the browser is GNU IceWeasel (now know as GNU IceCat) or not.
  846.      * @access protected
  847.      * @see checkBrowserIceCat()
  848.      * @return boolean Returns true if the browser is GNU IceWeasel, false otherwise.
  849.      */
  850.     protected function checkBrowserIceWeasel()
  851.     {
  852.         return $this->checkSimpleBrowserUA('Iceweasel'$this->_agentself::BROWSER_ICEWEASEL);
  853.     }
  854.  
  855.     /**
  856.      * Determine if the browser is Internet Explorer or not.
  857.      * @access protected
  858.      * @link http://www.microsoft.com/ie/
  859.      * @link http://en.wikipedia.org/wiki/Internet_Explorer_Mobile
  860.      * @return boolean Returns true if the browser is Internet Explorer, false otherwise.
  861.      */
  862.     protected function checkBrowserInternetExplorer()
  863.     {
  864.         //Test for Internet Explorer Mobile (formerly Pocket Internet Explorer)
  865.         if ($this->checkSimpleBrowserUA(array('IEMobile''MSPIE')$this->_agentself::BROWSER_IE_MOBILEtrue)) {
  866.             return true;
  867.         }
  868.  
  869.         //Several browsers uses IE compatibility UAs filter these browsers out (but after testing for IE Mobile)
  870.         if ($this->containString($this->_agent'Opera'|| $this->containString($this->_agentarray('BlackBerry''Nokia')truefalse)) {
  871.             return false;
  872.         }
  873.  
  874.         //Test for Internet Explorer 1
  875.         if ($this->checkSimpleBrowserUA('Microsoft Internet Explorer'$this->_agentself::BROWSER_IE)) {
  876.             if ($this->getVersion(== self::VERSION_UNKNOWN{
  877.                 if (preg_match('/308|425|426|474|0b1/i'$this->_agent)) {
  878.                     $this->setVersion('1.5');
  879.                 else {
  880.                     $this->setVersion('1.0');
  881.                 }
  882.             }
  883.             return true;
  884.         }
  885.  
  886.         //Test for Internet Explorer 2+
  887.         if ($this->containString($this->_agentarray('MSIE''Trident'))) {
  888.             $version '';
  889.  
  890.             if ($this->containString($this->_agent'Trident')) {
  891.                 //Test for Internet Explorer 11+ (check the rv: string)
  892.                 if ($this->containString($this->_agent'rv:'truefalse)) {
  893.                     if ($this->checkSimpleBrowserUA('Trident'$this->_agentself::BROWSER_IEfalsefalse'rv:')) {
  894.                         return true;
  895.                     }
  896.                 else {
  897.                     //Test for Internet Explorer 8, 9 & 10 (check the Trident string)
  898.                     if (preg_match('/Trident\/([\d]+)/i'$this->_agent$foundVersion)) {
  899.                         //Trident started with version 4.0 on IE 8
  900.                         $verFromTrident $this->parseInt($foundVersion[1]4;
  901.                         if ($verFromTrident >= 8{
  902.                             $version $verFromTrident '.0';
  903.                         }
  904.                     }
  905.                 }
  906.  
  907.                 //If we have the IE version from Trident, we can check for the compatibility view mode
  908.                 if ($version != ''{
  909.                     $emulatedVer '';
  910.                     preg_match_all('/MSIE\s*([^\s;$]+)/i'$this->_agent$foundVersions);
  911.                     foreach ($foundVersions[1as $currVer{
  912.                         //Keep the lowest MSIE version for the emulated version (in compatibility view mode)
  913.                         if ($emulatedVer == '' || $this->compareVersions($emulatedVer$currVer== 1{
  914.                             $emulatedVer $currVer;
  915.                         }
  916.                     }
  917.                     //Set the compatibility view mode if $version != $emulatedVer
  918.                     if ($this->compareVersions($version$emulatedVer!= 0{
  919.                         $this->_compatibilityViewName = self::BROWSER_IE;
  920.                         $this->_compatibilityViewVer = $this->cleanVersion($emulatedVer);
  921.                     }
  922.                 }
  923.             }
  924.  
  925.             //Test for Internet Explorer 2-7 versions if needed
  926.             if ($version == ''{
  927.                 preg_match_all('/MSIE\s+([^\s;$]+)/i'$this->_agent$foundVersions);
  928.                 foreach ($foundVersions[1as $currVer{
  929.                     //Keep the highest MSIE version
  930.                     if ($version == '' || $this->compareVersions($version$currVer== -1{
  931.                         $version $currVer;
  932.                     }
  933.                 }
  934.             }
  935.  
  936.             $this->setBrowser(self::BROWSER_IE);
  937.             $this->setVersion($version);
  938.             $this->setMobile(false);
  939.             $this->setRobot(false);
  940.  
  941.             return true;
  942.         }
  943.  
  944.         return false;
  945.     }
  946.  
  947.     /**
  948.      * Determine if the browser is Konqueror or not.
  949.      * @access protected
  950.      * @link http://www.konqueror.org/
  951.      * @return boolean Returns true if the browser is Konqueror, false otherwise.
  952.      */
  953.     protected function checkBrowserKonqueror()
  954.     {
  955.         return $this->checkSimpleBrowserUA('Konqueror'$this->_agentself::BROWSER_KONQUEROR);
  956.     }
  957.  
  958.     /**
  959.      * Determine if the browser is Lynx or not. It is the oldest web browser currently in general use and development.
  960.      * It is a text-based only Web browser.
  961.      * @access protected
  962.      * @link http://en.wikipedia.org/wiki/Lynx
  963.      * @return boolean Returns true if the browser is Lynx, false otherwise.
  964.      */
  965.     protected function checkBrowserLynx()
  966.     {
  967.         return $this->checkSimpleBrowserUA('Lynx'$this->_agentself::BROWSER_LYNX);
  968.     }
  969.  
  970.     /**
  971.      * Determine if the browser is Mozilla or not.
  972.      * @access protected
  973.      * @return boolean Returns true if the browser is Mozilla, false otherwise.
  974.      */
  975.     protected function checkBrowserMozilla()
  976.     {
  977.         return $this->checkSimpleBrowserUA('Mozilla'$this->_agentself::BROWSER_MOZILLAfalsefalse'rv:');
  978.     }
  979.  
  980.     /**
  981.      * Determine if the browser is the MSNBot crawler or not. In October 2010 it was replaced by the Bingbot robot.
  982.      * @access protected
  983.      * @see checkBrowserBingbot()
  984.      * @return boolean Returns true if the browser is MSNBot, false otherwise.
  985.      */
  986.     protected function checkBrowserMsnBot()
  987.     {
  988.         return $this->checkSimpleBrowserUA('msnbot'$this->_agentself::BROWSER_MSNBOTfalsetrue);
  989.     }
  990.  
  991.     /**
  992.      * Determine if the browser is MSN TV (formerly WebTV) or not.
  993.      * @access protected
  994.      * @link http://en.wikipedia.org/wiki/MSN_TV
  995.      * @return boolean Returns true if the browser is WebTv, false otherwise.
  996.      */
  997.     protected function checkBrowserMsnTv()
  998.     {
  999.         return $this->checkSimpleBrowserUA('webtv'$this->_agentself::BROWSER_MSNTV);
  1000.     }
  1001.  
  1002.     /**
  1003.      * Determine if the browser is Netscape or not. Official support for this browser ended on March 1st, 2008.
  1004.      * @access protected
  1005.      * @link http://en.wikipedia.org/wiki/Netscape
  1006.      * @return boolean Returns true if the browser is Netscape, false otherwise.
  1007.      */
  1008.     protected function checkBrowserNetscape()
  1009.     {
  1010.         //BlackBerry & Nokia UAs can conflict with Netscape UAs
  1011.         if ($this->containString($this->_agentarray('BlackBerry''Nokia')truefalse)) {
  1012.             return false;
  1013.         }
  1014.  
  1015.         //Netscape v6 to v9 check
  1016.         if ($this->checkSimpleBrowserUA(array('Netscape''Navigator''Netscape6')$this->_agentself::BROWSER_NETSCAPE)) {
  1017.             return true;
  1018.         }
  1019.  
  1020.         //Netscape v1-4 (v5 don't exists)
  1021.         $found false;
  1022.         if ($this->containString($this->_agent'Mozilla'&& !$this->containString($this->_agent'rv:'truefalse)) {
  1023.             $version '';
  1024.             $verParts explode('/'stristr($this->_agent'Mozilla'));
  1025.             if (count($verParts1{
  1026.                 $verParts explode(' '$verParts[1]);
  1027.                 $verParts explode('.'$verParts[0]);
  1028.  
  1029.                 $majorVer $this->parseInt($verParts[0]);
  1030.                 if ($majorVer && $majorVer 5{
  1031.                     $version implode('.'$verParts);
  1032.                     $found true;
  1033.  
  1034.                     if (strtolower(substr($version-4)) == '-sgi'{
  1035.                         $version substr($version0-4);
  1036.                     else {
  1037.                         if (strtolower(substr($version-4)) == 'gold'{
  1038.                             $version substr($version0-4' Gold'//Doubles spaces (if any) will be normalized by setVersion()
  1039.                         }
  1040.                     }
  1041.                 }
  1042.             }
  1043.         }
  1044.  
  1045.         if ($found{
  1046.             $this->setBrowser(self::BROWSER_NETSCAPE);
  1047.             $this->setVersion($version);
  1048.             $this->setMobile(false);
  1049.             $this->setRobot(false);
  1050.         }
  1051.  
  1052.         return $found;
  1053.     }
  1054.  
  1055.     /**
  1056.      * Determine if the browser is a Nokia browser or not.
  1057.      * @access protected
  1058.      * @link http://www.developer.nokia.com/Community/Wiki/User-Agent_headers_for_Nokia_devices
  1059.      * @return boolean Returns true if the browser is a Nokia browser, false otherwise.
  1060.      */
  1061.     protected function checkBrowserNokia()
  1062.     {
  1063.         if ($this->containString($this->_agentarray('Nokia5800''Nokia5530''Nokia5230')truefalse)) {
  1064.             $this->setBrowser(self::BROWSER_NOKIA);
  1065.             $this->setVersion('7.0');
  1066.             $this->setMobile(true);
  1067.             $this->setRobot(false);
  1068.  
  1069.             return true;
  1070.         }
  1071.  
  1072.         if ($this->checkSimpleBrowserUA(array('NokiaBrowser''BrowserNG''Series60''S60''S40OviBrowser')$this->_agentself::BROWSER_NOKIAtrue)) {
  1073.             return true;
  1074.         }
  1075.  
  1076.         return false;
  1077.     }
  1078.  
  1079.     /**
  1080.      * Determine if the browser is Opera or not.
  1081.      * @access protected
  1082.      * @link http://www.opera.com/
  1083.      * @link http://www.opera.com/mini/
  1084.      * @link http://www.opera.com/mobile/
  1085.      * @link http://my.opera.com/community/openweb/idopera/
  1086.      * @return boolean Returns true if the browser is Opera, false otherwise.
  1087.      */
  1088.     protected function checkBrowserOpera()
  1089.     {
  1090.         if ($this->checkBrowserUAWithVersion('Opera Mobi'$this->_agentself::BROWSER_OPERA_MOBILEtrue)) {
  1091.             return true;
  1092.         }
  1093.  
  1094.         if ($this->checkSimpleBrowserUA('Opera Mini'$this->_agentself::BROWSER_OPERA_MINItrue)) {
  1095.             return true;
  1096.         }
  1097.  
  1098.         $version '';
  1099.         $found $this->checkBrowserUAWithVersion('Opera'$this->_agentself::BROWSER_OPERA);
  1100.         if ($found && $this->getVersion(!= self::VERSION_UNKNOWN{
  1101.             $version $this->getVersion();
  1102.         }
  1103.  
  1104.         if (!$found || $version == ''{
  1105.             if ($this->checkSimpleBrowserUA('Opera'$this->_agentself::BROWSER_OPERA)) {
  1106.                 return true;
  1107.             }
  1108.         }
  1109.  
  1110.         if (!$found && $this->checkSimpleBrowserUA('Chrome'$this->_agentself::BROWSER_CHROME) ) {
  1111.             if ($this->checkSimpleBrowserUA('OPR/'$this->_agentself::BROWSER_OPERA)) {
  1112.                 return true;
  1113.             }
  1114.         }
  1115.  
  1116.         return $found;
  1117.     }
  1118.  
  1119.     /**
  1120.      * Determine if the browser is Phoenix or not. Phoenix was the name of Firefox from version 0.1 to 0.5.
  1121.      * @access protected
  1122.      * @return boolean Returns true if the browser is Phoenix, false otherwise.
  1123.      */
  1124.     protected function checkBrowserPhoenix()
  1125.     {
  1126.         return $this->checkSimpleBrowserUA('Phoenix'$this->_agentself::BROWSER_PHOENIX);
  1127.     }
  1128.  
  1129.     /**
  1130.      * Determine what is the browser used by the user.
  1131.      * @access protected
  1132.      * @return boolean Returns true if the browser has been identified, false otherwise.
  1133.      */
  1134.     protected function checkBrowsers()
  1135.     {
  1136.         //Changing the check order can break the class detection results!
  1137.         return
  1138.                /* Major browsers and browsers that need to be detected in a special order */
  1139.                $this->checkBrowserCustom(||           /* Customs rules are always checked first */
  1140.                $this->checkBrowserMsnTv(||            /* MSN TV is based on IE so we must check for MSN TV before IE */
  1141.                $this->checkBrowserInternetExplorer(||
  1142.                $this->checkBrowserOpera(||            /* Opera must be checked before Firefox, Netscape and Chrome to avoid conflicts */
  1143.                $this->checkBrowserEdge(||             /* Edge must be checked before Firefox, Safari and Chrome to avoid conflicts */
  1144.                $this->checkBrowserSamsung(||          /* Samsung Internet browser must be checked before Chrome and Safari to avoid conflicts */
  1145.                $this->checkBrowserUC(||               /* UC Browser must be checked before Chrome and Safari to avoid conflicts */
  1146.                $this->checkBrowserChrome(||           /* Chrome must be checked before Netscaoe and Mozilla to avoid conflicts */
  1147.                $this->checkBrowserIcab(||             /* Check iCab before Netscape since iCab have Mozilla UAs */
  1148.                $this->checkBrowserNetscape(||         /* Must be checked before Firefox since Netscape 8-9 are based on Firefox */
  1149.                $this->checkBrowserIceCat(||           /* Check IceCat and IceWeasel before Firefox since they are GNU builds of Firefox */
  1150.                $this->checkBrowserIceWeasel(||
  1151.                $this->checkBrowserFirefox(||
  1152.                /* Current browsers that don't need to be detected in any special order */
  1153.                $this->checkBrowserKonqueror(||
  1154.                $this->checkBrowserLynx(||
  1155.                /* Mobile */
  1156.                $this->checkBrowserAndroid(||
  1157.                $this->checkBrowserBlackBerry(||
  1158.                $this->checkBrowserNokia(||
  1159.                /* Bots */
  1160.                $this->checkBrowserGooglebot(||
  1161.                $this->checkBrowserBingbot(||
  1162.                $this->checkBrowserMsnBot(||
  1163.                $this->checkBrowserSlurp(||
  1164.                $this->checkBrowserYahooMultimedia(||
  1165.                $this->checkBrowserW3CValidator(||
  1166.                /* WebKit base check (after most other checks) */
  1167.                $this->checkBrowserSafari(||
  1168.                /* Deprecated browsers that don't need to be detected in any special order */
  1169.                $this->checkBrowserFirebird(||
  1170.                $this->checkBrowserPhoenix(||
  1171.                /* Mozilla is such an open standard that it must be checked last */
  1172.                $this->checkBrowserMozilla();
  1173.     }
  1174.  
  1175.     /**
  1176.      * Determine if the browser is Safari or not.
  1177.      * @access protected
  1178.      * @link http://www.apple.com/safari/
  1179.      * @link http://web.archive.org/web/20080514173941/http://developer.apple.com/internet/safari/uamatrix.html
  1180.      * @link http://en.wikipedia.org/wiki/Safari_version_history#Release_history
  1181.      * @return boolean Returns true if the browser is Safari, false otherwise.
  1182.      */
  1183.     protected function checkBrowserSafari()
  1184.     {
  1185.         $version '';
  1186.  
  1187.         //Check for current versions of Safari
  1188.         $found $this->checkBrowserUAWithVersion(array('Safari''AppleWebKit')$this->_agentself::BROWSER_SAFARI);
  1189.         if ($found && $this->getVersion(!= self::VERSION_UNKNOWN{
  1190.             $version $this->getVersion();
  1191.         }
  1192.  
  1193.         //Safari 1-2 didn't had a "Version" string in the UA, only a WebKit build and/or Safari build, extract version from these...
  1194.         if (!$found || $version == ''{
  1195.             if (preg_match('/.*Safari[ (\/]*([a-z0-9.-]*)/i'$this->_agent$matches)) {
  1196.                 $version $this->safariBuildToSafariVer($matches[1]);
  1197.                 $found true;
  1198.             }
  1199.         }
  1200.         if (!$found || $version == ''{
  1201.             if (preg_match('/.*AppleWebKit[ (\/]*([a-z0-9.-]*)/i'$this->_agent$matches)) {
  1202.                 $version $this->webKitBuildToSafariVer($matches[1]);
  1203.                 $found true;
  1204.             }
  1205.         }
  1206.  
  1207.         if ($found{
  1208.             $this->setBrowser(self::BROWSER_SAFARI);
  1209.             $this->setVersion($version);
  1210.             $this->setMobile(false);
  1211.             $this->setRobot(false);
  1212.         }
  1213.  
  1214.         return $found;
  1215.     }
  1216.  
  1217.     /**
  1218.      * Determine if the browser is the Samsung Internet browser or not.
  1219.      * @access protected
  1220.      * @return boolean Returns true if the browser is the the Samsung Internet browser, false otherwise.
  1221.      */
  1222.     protected function checkBrowserSamsung()
  1223.     {
  1224.         return $this->checkSimpleBrowserUA('SamsungBrowser'$this->_agentself::BROWSER_SAMSUNGtrue);
  1225.     }
  1226.  
  1227.     /**
  1228.      * Determine if the browser is the Yahoo! Slurp crawler or not.
  1229.      * @access protected
  1230.      * @return boolean Returns true if the browser is Yahoo! Slurp, false otherwise.
  1231.      */
  1232.     protected function checkBrowserSlurp()
  1233.     {
  1234.         return $this->checkSimpleBrowserUA('Yahoo! Slurp'$this->_agentself::BROWSER_SLURPfalsetrue);
  1235.     }
  1236.  
  1237.     /**
  1238.      * Test the user agent for a specific browser that use a "Version" string (like Safari and Opera). The user agent
  1239.      * should look like: "Version/1.0 Browser name/123.456" or "Browser name/123.456 Version/1.0".
  1240.      * @access protected
  1241.      * @param mixed $uaNameToLookFor The string (or array of strings) representing the browser name to find in the user
  1242.      *  agent.
  1243.      * @param string $userAgent The user agent string to work with.
  1244.      * @param string $browserName The literal browser name. Always use a class constant!
  1245.      * @param boolean $isMobile (optional) Determines if the browser is from a mobile device.
  1246.      * @param boolean $isRobot (optional) Determines if the browser is a robot or not.
  1247.      * @param boolean $findWords (optional) Determines if the needle should match a word to be found. For example "Bar"
  1248.      *  would not be found in "FooBar" when true but would be found in "Foo Bar". When set to false, the needle can be
  1249.      *  found anywhere in the haystack.
  1250.      * @return boolean Returns true if we found the browser we were looking for, false otherwise.
  1251.      */
  1252.     protected function checkBrowserUAWithVersion($uaNameToLookFor$userAgent$browserName$isMobile false$isRobot false$findWords true)
  1253.     {
  1254.         if (!is_array($uaNameToLookFor)) {
  1255.             $uaNameToLookFor array($uaNameToLookFor);
  1256.         }
  1257.  
  1258.         foreach ($uaNameToLookFor as $currUANameToLookFor{
  1259.             if ($this->containString($userAgent$currUANameToLookFortrue$findWords)) {
  1260.                 $version '';
  1261.                 $verParts explode('/'stristr($this->_agent'Version'));
  1262.                 if (count($verParts1{
  1263.                     $verParts explode(' '$verParts[1]);
  1264.                     $version $verParts[0];
  1265.                 }
  1266.  
  1267.                 $this->setBrowser($browserName);
  1268.                 $this->setVersion($version);
  1269.  
  1270.                 $this->setMobile($isMobile);
  1271.                 $this->setRobot($isRobot);
  1272.  
  1273.                 return true;
  1274.             }
  1275.         }
  1276.  
  1277.         return false;
  1278.     }
  1279.  
  1280.     /**
  1281.      * Determine if the browser is UC Browser or not.
  1282.      * @access protected
  1283.      * @return boolean Returns true if the browser is UC Browser, false otherwise.
  1284.      */
  1285.     protected function checkBrowserUC()
  1286.     {
  1287.         return $this->checkSimpleBrowserUA('UCBrowser'$this->_agentself::BROWSER_UCtruefalse);
  1288.     }
  1289.  
  1290.     /**
  1291.      * Determine if the browser is the W3C Validator or not.
  1292.      * @access protected
  1293.      * @link http://validator.w3.org/
  1294.      * @return boolean Returns true if the browser is the W3C Validator, false otherwise.
  1295.      */
  1296.     protected function checkBrowserW3CValidator()
  1297.     {
  1298.         //Since the W3C validates pages with different robots we will prefix our versions with the part validated on the page...
  1299.  
  1300.         //W3C Link Checker (prefixed with "Link-")
  1301.         if ($this->checkSimpleBrowserUA('W3C-checklink'$this->_agentself::BROWSER_W3CVALIDATORfalsetrue)) {
  1302.             if ($this->getVersion(!= self::VERSION_UNKNOWN{
  1303.                 $this->setVersion('Link-' $this->getVersion());
  1304.             }
  1305.             return true;
  1306.         }
  1307.  
  1308.         //W3C CSS Validation Service (prefixed with "CSS-")
  1309.         if ($this->checkSimpleBrowserUA('Jigsaw'$this->_agentself::BROWSER_W3CVALIDATORfalsetrue)) {
  1310.             if ($this->getVersion(!= self::VERSION_UNKNOWN{
  1311.                 $this->setVersion('CSS-' $this->getVersion());
  1312.             }
  1313.             return true;
  1314.         }
  1315.  
  1316.         //W3C mobileOK Checker (prefixed with "mobileOK-")
  1317.         if ($this->checkSimpleBrowserUA('W3C-mobileOK'$this->_agentself::BROWSER_W3CVALIDATORfalsetrue)) {
  1318.             if ($this->getVersion(!= self::VERSION_UNKNOWN{
  1319.                 $this->setVersion('mobileOK-' $this->getVersion());
  1320.             }
  1321.             return true;
  1322.         }
  1323.  
  1324.         //W3C Markup Validation Service (no prefix)
  1325.         return $this->checkSimpleBrowserUA('W3C_Validator'$this->_agentself::BROWSER_W3CVALIDATORfalsetrue);
  1326.     }
  1327.  
  1328.     /**
  1329.      * Determine if the browser is the Yahoo! multimedia crawler or not.
  1330.      * @access protected
  1331.      * @return boolean Returns true if the browser is the Yahoo! multimedia crawler, false otherwise.
  1332.      */
  1333.     protected function checkBrowserYahooMultimedia()
  1334.     {
  1335.         return $this->checkSimpleBrowserUA('Yahoo-MMCrawler'$this->_agentself::BROWSER_YAHOO_MMfalsetrue);
  1336.     }
  1337.  
  1338.     /**
  1339.      * Determine the user's platform.
  1340.      * @access protected
  1341.      */
  1342.     protected function checkPlatform()
  1343.     {
  1344.         if (!$this->checkPlatformCustom()) /* Customs rules are always checked first */
  1345.             /* Mobile platforms */
  1346.             if ($this->containString($this->_agentarray('Windows Phone''IEMobile'))) /* Check Windows Phone (formerly Windows Mobile) before Windows */
  1347.                 $this->setPlatform(self::PLATFORM_WINDOWS_PHONE);
  1348.                 $this->setMobile(true);
  1349.             else if ($this->containString($this->_agent'Windows CE')) /* Check Windows CE before Windows */
  1350.                 $this->setPlatform(self::PLATFORM_WINDOWS_CE);
  1351.                 $this->setMobile(true);
  1352.             else if ($this->containString($this->_agentarray('CPU OS''CPU iPhone OS''iPhone''iPad''iPod'))) /* Check iOS (iPad/iPod/iPhone) before Macintosh */
  1353.                 $this->setPlatform(self::PLATFORM_IOS);
  1354.                 $this->setMobile(true);
  1355.             else if ($this->containString($this->_agent'Android')) {
  1356.                 $this->setPlatform(self::PLATFORM_ANDROID);
  1357.                 $this->setMobile(true);
  1358.             else if ($this->containString($this->_agent'BlackBerry'truefalse|| $this->containString($this->_agentarray('BB10''RIM Tablet OS'))) {
  1359.                 $this->setPlatform(self::PLATFORM_BLACKBERRY);
  1360.                 $this->setMobile(true);
  1361.             else if ($this->containString($this->_agent'Nokia'truefalse)) {
  1362.                 $this->setPlatform(self::PLATFORM_NOKIA);
  1363.                 $this->setMobile(true);
  1364.  
  1365.             /* Desktop platforms */
  1366.             else if ($this->containString($this->_agent'Windows')) {
  1367.                 $this->setPlatform(self::PLATFORM_WINDOWS);
  1368.             else if ($this->containString($this->_agent'Macintosh')) {
  1369.                 $this->setPlatform(self::PLATFORM_MACINTOSH);
  1370.             else if ($this->containString($this->_agent'Linux')) {
  1371.                 $this->setPlatform(self::PLATFORM_LINUX);
  1372.             else if ($this->containString($this->_agent'FreeBSD')) {
  1373.                 $this->setPlatform(self::PLATFORM_FREEBSD);
  1374.             else if ($this->containString($this->_agent'OpenBSD')) {
  1375.                 $this->setPlatform(self::PLATFORM_OPENBSD);
  1376.             else if ($this->containString($this->_agent'NetBSD')) {
  1377.                 $this->setPlatform(self::PLATFORM_NETBSD);
  1378.  
  1379.             /* Discontinued */
  1380.             else if ($this->containString($this->_agentarray('Symbian''SymbianOS'))) {
  1381.                 $this->setPlatform(self::PLATFORM_SYMBIAN);
  1382.                 $this->setMobile(true);
  1383.             else if ($this->containString($this->_agent'OpenSolaris')) {
  1384.                 $this->setPlatform(self::PLATFORM_OPENSOLARIS);
  1385.  
  1386.             /* Generic */
  1387.             else if ($this->containString($this->_agent'Win'truefalse)) {
  1388.                 $this->setPlatform(self::PLATFORM_WINDOWS);
  1389.             else if ($this->containString($this->_agent'Mac'truefalse)) {
  1390.                 $this->setPlatform(self::PLATFORM_MACINTOSH);
  1391.             }
  1392.         }
  1393.  
  1394.         //Check if it's a 64-bit platform
  1395.         if ($this->containString($this->_agentarray('WOW64''Win64''AMD64''x86_64''x86-64''ia64''IRIX64',
  1396.                 'ppc64''sparc64''x64;''x64_64'))) {
  1397.             $this->set64bit(true);
  1398.         }
  1399.  
  1400.         $this->checkPlatformVersion();
  1401.     }
  1402.  
  1403.     /**
  1404.      * Determine if the platform is among the custom platform rules or not. Rules are checked in the order they were
  1405.      * added.
  1406.      * @access protected
  1407.      * @return boolean Returns true if we found the platform we were looking for in the custom rules, false otherwise.
  1408.      */
  1409.     protected function checkPlatformCustom()
  1410.     {
  1411.         foreach ($this->_customPlatformDetection as $platformName => $customPlatform{
  1412.             $platformNameToLookFor $customPlatform['platformNameToLookFor'];
  1413.             $isMobile $customPlatform['isMobile'];
  1414.             if ($this->containString($this->_agent$platformNameToLookFor)) {
  1415.                 $this->setPlatform($platformName);
  1416.                 if ($isMobile{
  1417.                     $this->setMobile(true);
  1418.                 }
  1419.                 return true;
  1420.             }
  1421.         }
  1422.  
  1423.         return false;
  1424.     }
  1425.  
  1426.     /**
  1427.      * Determine the user's platform version.
  1428.      * @access protected
  1429.      */
  1430.     protected function checkPlatformVersion()
  1431.     {
  1432.         $result '';
  1433.         switch ($this->getPlatform()) {
  1434.             case self::PLATFORM_WINDOWS:
  1435.                 if (preg_match('/Windows NT\s*(\d+(?:\.\d+)*)/i'$this->_agent$foundVersion)) {
  1436.                     $result 'NT ' $foundVersion[1];
  1437.                 else {
  1438.                     //https://support.microsoft.com/en-us/kb/158238
  1439.  
  1440.                     if ($this->containString($this->_agentarray('Windows XP''WinXP''Win XP'))) {
  1441.                         $result '5.1';
  1442.                     else if ($this->containString($this->_agent'Windows 2000''Win 2000''Win2000')) {
  1443.                         $result '5.0';
  1444.                     else if ($this->containString($this->_agentarray('Win 9x 4.90''Windows ME''WinME''Win ME'))) {
  1445.                         $result '4.90.3000'//Windows Me version range from 4.90.3000 to 4.90.3000A
  1446.                     else if ($this->containString($this->_agentarray('Windows 98''Win98''Win 98'))) {
  1447.                         $result '4.10'//Windows 98 version range from 4.10.1998 to 4.10.2222B
  1448.                     else if ($this->containString($this->_agentarray('Windows 95''Win95''Win 95'))) {
  1449.                         $result '4.00'//Windows 95 version range from 4.00.950 to 4.03.1214
  1450.                     else if (($foundAt stripos($this->_agent'Windows 3')) !== false{
  1451.                         $result '3';
  1452.                         if (preg_match('/\d+(?:\.\d+)*/'substr($this->_agent$foundAt strlen('Windows 3'))$foundVersion)) {
  1453.                             $result .= '.' $foundVersion[0];
  1454.                         }
  1455.                     else if ($this->containString($this->_agent'Win16')) {
  1456.                         $result '3.1';
  1457.                     }
  1458.                 }
  1459.                 break;
  1460.  
  1461.             case self::PLATFORM_MACINTOSH:
  1462.                 if (preg_match('/Mac OS X\s*(\d+(?:_\d+)+)/i'$this->_agent$foundVersion)) {
  1463.                     $result str_replace('_''.'$this->cleanVersion($foundVersion[1]));
  1464.                 else if ($this->containString($this->_agent'Mac OS X')) {
  1465.                     $result '10';
  1466.                 }
  1467.                 break;
  1468.  
  1469.             case self::PLATFORM_ANDROID:
  1470.                 if (preg_match('/Android\s+([^\s;$]+)/i'$this->_agent$foundVersion)) {
  1471.                     $result $this->cleanVersion($foundVersion[1]);
  1472.                 }
  1473.                 break;
  1474.  
  1475.             case self::PLATFORM_IOS:
  1476.                 if (preg_match('/(?:CPU OS|iPhone OS|iOS)[\s_]*([\d_]+)/i'$this->_agent$foundVersion)) {
  1477.                     $result str_replace('_''.'$this->cleanVersion($foundVersion[1]));
  1478.                 }
  1479.                 break;
  1480.         }
  1481.  
  1482.         if (trim($result== ''{
  1483.             $result self::PLATFORM_VERSION_UNKNOWN;
  1484.         }
  1485.         $this->setPlatformVersion($result);
  1486.     }
  1487.  
  1488.     /**
  1489.      * Test the user agent for a specific browser where the browser name is immediately followed by the version number.
  1490.      * The user agent should look like: "Browser name/1.0" or "Browser 1.0;".
  1491.      * @access protected
  1492.      * @param mixed $uaNameToLookFor The string (or array of strings) representing the browser name to find in the user
  1493.      *  agent.
  1494.      * @param string $userAgent The user agent string to work with.
  1495.      * @param string $browserName The literal browser name. Always use a class constant!
  1496.      * @param boolean $isMobile (optional) Determines if the browser is from a mobile device.
  1497.      * @param boolean $isRobot (optional) Determines if the browser is a robot or not.
  1498.      * @param string $separator (optional) The separator string used to split the browser name and the version number in
  1499.      *  the user agent.
  1500.      * @param boolean $uaNameFindWords (optional) Determines if the browser name to find should match a word instead of
  1501.      *  a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
  1502.      *  When set to false, the browser name can be found anywhere in the user agent string.
  1503.      * @return boolean Returns true if we found the browser we were looking for, false otherwise.
  1504.      */
  1505.     protected function checkSimpleBrowserUA($uaNameToLookFor$userAgent$browserName$isMobile false$isRobot false$separator '/'$uaNameFindWords true)
  1506.     {
  1507.         if (!is_array($uaNameToLookFor)) {
  1508.             $uaNameToLookFor array($uaNameToLookFor);
  1509.         }
  1510.  
  1511.         foreach ($uaNameToLookFor as $currUANameToLookFor{
  1512.  
  1513.             if ($this->containString($userAgent$currUANameToLookFortrue$uaNameFindWords)) {
  1514.                 //Many browsers don't use the standard "Browser/1.0" format, they uses "Browser 1.0;" instead
  1515.                 if (stripos($userAgent$currUANameToLookFor $separator=== false{
  1516.                     $userAgent str_ireplace($currUANameToLookFor ' '$currUANameToLookFor $separator$this->_agent);
  1517.                 }
  1518.  
  1519.                 $version '';
  1520.                 $verParts explode($separatorstristr($userAgent$currUANameToLookFor));
  1521.                 if (count($verParts1{
  1522.                     $verParts explode(' '$verParts[1]);
  1523.                     $version $verParts[0];
  1524.                 }
  1525.  
  1526.                 $this->setBrowser($browserName);
  1527.                 $this->setVersion($version);
  1528.  
  1529.                 $this->setMobile($isMobile);
  1530.                 $this->setRobot($isRobot);
  1531.  
  1532.                 return true;
  1533.             }
  1534.         }
  1535.  
  1536.         return false;
  1537.     }
  1538.  
  1539.     /**
  1540.      * Find if one or more substring is contained in a string.
  1541.      * @access protected
  1542.      * @param string $haystack The string to search in.
  1543.      * @param mixed $needle The string to search for. Can be a string or an array of strings if multiples values are to
  1544.      *  be searched.
  1545.      * @param boolean $insensitive (optional) Determines if we do a case-sensitive search (false) or a case-insensitive
  1546.      *  one (true).
  1547.      * @param boolean $findWords (optional) Determines if the needle should match a word to be found. For example "Bar"
  1548.      *  would not be found in "FooBar" when true but would be found in "Foo Bar". When set to false, the needle can be
  1549.      *  found anywhere in the haystack.
  1550.      * @return boolean Returns true if the needle (or one of the needles) has been found in the haystack, false
  1551.      *  otherwise.
  1552.      */
  1553.     protected function containString($haystack$needle$insensitive true$findWords true)
  1554.     {
  1555.         if (!is_array($needle)) {
  1556.             $needle array($needle);
  1557.         }
  1558.  
  1559.         foreach ($needle as $currNeedle{
  1560.             if ($findWords{
  1561.                  $found $this->wordPos($haystack$currNeedle$insensitive!== false;
  1562.             else {
  1563.                 if ($insensitive{
  1564.                     $found stripos($haystack$currNeedle!== false;
  1565.                 else {
  1566.                     $found strpos($haystack$currNeedle!== false;
  1567.                 }
  1568.             }
  1569.  
  1570.             if ($found{
  1571.                 return true;
  1572.             }
  1573.         }
  1574.  
  1575.         return false;
  1576.     }
  1577.  
  1578.     /**
  1579.      * Detect the user environment from the details in the user agent string.
  1580.      * @access protected
  1581.      */
  1582.     protected function detect()
  1583.     {
  1584.         $this->checkBrowsers();
  1585.         $this->checkPlatform()//Check the platform after the browser since some platforms can change the mobile value
  1586.     }
  1587.  
  1588.     /**
  1589.      * Clean a version string from unwanted characters.
  1590.      * @access protected
  1591.      * @param string $version The version string to clean.
  1592.      * @return string Returns the cleaned version number string.
  1593.      */
  1594.     protected function cleanVersion($version)
  1595.     {
  1596.         //Clear anything that is in parentheses (and the parentheses themselves) - will clear started but unclosed ones too
  1597.         $cleanVer preg_replace('/\([^)]+\)?/'''$version);
  1598.         //Replace with a space any character which is NOT an alphanumeric, dot (.), hyphen (-), underscore (_) or space
  1599.         $cleanVer preg_replace('/[^0-9.a-zA-Z_ -]/'' '$cleanVer);
  1600.  
  1601.         //Remove trailing and leading spaces
  1602.         $cleanVer trim($cleanVer);
  1603.  
  1604.         //Remove trailing dot (.), hyphen (-), underscore (_)
  1605.         while (in_array(substr($cleanVer-1)array('.''-''_'))) {
  1606.             $cleanVer substr($cleanVer0-1);
  1607.         }
  1608.         //Remove leading dot (.), hyphen (-), underscore (_) and character v
  1609.         while (in_array(substr($cleanVer01)array('.''-''_''v''V'))) {
  1610.             $cleanVer substr($cleanVer1);
  1611.         }
  1612.  
  1613.         //Remove double spaces if any
  1614.         while (strpos($cleanVer'  '!== false{
  1615.             $cleanVer str_replace('  '' '$cleanVer);
  1616.         }
  1617.  
  1618.         return trim($cleanVer);
  1619.     }
  1620.  
  1621.     /**
  1622.      * Convert the iOS version numbers to the operating system name. For instance '2.0' returns 'iPhone OS 2.0'.
  1623.      * @access protected
  1624.      * @param string $iOSVer The iOS version numbers as a string.
  1625.      * @return string The operating system name.
  1626.      */
  1627.     protected function iOSVerToStr($iOSVer)
  1628.     {
  1629.         if ($this->compareVersions($iOSVer'3.0'<= 0{
  1630.             return 'iPhone OS ' $iOSVer;
  1631.         else {
  1632.             return 'iOS ' $iOSVer;
  1633.         }
  1634.     }
  1635.  
  1636.     /**
  1637.      * Convert the macOS version numbers to the operating system name. For instance '10.7' returns 'Mac OS X Lion'.
  1638.      * @access protected
  1639.      * @param string $macVer The macOS version numbers as a string.
  1640.      * @return string The operating system name or the constant PLATFORM_VERSION_UNKNOWN if nothing match the version
  1641.      *  numbers.
  1642.      */
  1643.     protected function macVerToStr($macVer)
  1644.     {
  1645.         //https://en.wikipedia.org/wiki/OS_X#Release_history
  1646.  
  1647.         if ($this->_platformVersion === '10'{
  1648.             return 'Mac OS X'//Unspecified Mac OS X version
  1649.         else if ($this->compareVersions($macVer'10.13'>= && $this->compareVersions($macVer'10.14'0{
  1650.             return 'macOS High Sierra';
  1651.         else if ($this->compareVersions($macVer'10.12'>= && $this->compareVersions($macVer'10.13'0{
  1652.             return 'macOS Sierra';
  1653.         else if ($this->compareVersions($macVer'10.11'>= && $this->compareVersions($macVer'10.12'0{
  1654.             return 'OS X El Capitan';
  1655.         else if ($this->compareVersions($macVer'10.10'>= && $this->compareVersions($macVer'10.11'0{
  1656.             return 'OS X Yosemite';
  1657.         else if ($this->compareVersions($macVer'10.9'>= && $this->compareVersions($macVer'10.10'0{
  1658.             return 'OS X Mavericks';
  1659.         else if ($this->compareVersions($macVer'10.8'>= && $this->compareVersions($macVer'10.9'0{
  1660.             return 'OS X Mountain Lion';
  1661.         else if ($this->compareVersions($macVer'10.7'>= && $this->compareVersions($macVer'10.8'0{
  1662.             return 'Mac OS X Lion';
  1663.         else if ($this->compareVersions($macVer'10.6'>= && $this->compareVersions($macVer'10.7'0{
  1664.             return 'Mac OS X Snow Leopard';
  1665.         else if ($this->compareVersions($macVer'10.5'>= && $this->compareVersions($macVer'10.6'0{
  1666.             return 'Mac OS X Leopard';
  1667.         else if ($this->compareVersions($macVer'10.4'>= && $this->compareVersions($macVer'10.5'0{
  1668.             return 'Mac OS X Tiger';
  1669.         else if ($this->compareVersions($macVer'10.3'>= && $this->compareVersions($macVer'10.4'0{
  1670.             return 'Mac OS X Panther';
  1671.         else if ($this->compareVersions($macVer'10.2'>= && $this->compareVersions($macVer'10.3'0{
  1672.             return 'Mac OS X Jaguar';
  1673.         else if ($this->compareVersions($macVer'10.1'>= && $this->compareVersions($macVer'10.2'0{
  1674.             return 'Mac OS X Puma';
  1675.         else if ($this->compareVersions($macVer'10.0'>= && $this->compareVersions($macVer'10.1'0{
  1676.             return 'Mac OS X Cheetah';
  1677.         else {
  1678.             return self::PLATFORM_VERSION_UNKNOWN//Unknown/unnamed Mac OS version
  1679.         }
  1680.     }
  1681.  
  1682.     /**
  1683.      * Get the integer value of a string variable.
  1684.      * @access protected
  1685.      * @param string $intStr The scalar value being converted to an integer.
  1686.      * @return int The integer value of $intStr on success, or 0 on failure.
  1687.      */
  1688.     protected function parseInt($intStr)
  1689.     {
  1690.         return intval($intStr10);
  1691.     }
  1692.  
  1693.     /**
  1694.      * Reset all the properties of the class.
  1695.      * @access protected
  1696.      */
  1697.     protected function reset()
  1698.     {
  1699.         $this->_agent = '';
  1700.         $this->_browserName = self::BROWSER_UNKNOWN;
  1701.         $this->_compatibilityViewName = '';
  1702.         $this->_compatibilityViewVer = '';
  1703.         $this->_is64bit = false;
  1704.         $this->_isMobile = false;
  1705.         $this->_isRobot = false;
  1706.         $this->_platform = self::PLATFORM_UNKNOWN;
  1707.         $this->_platformVersion = self::PLATFORM_VERSION_UNKNOWN;
  1708.         $this->_version = self::VERSION_UNKNOWN;
  1709.     }
  1710.  
  1711.     /**
  1712.      * Convert a Safari build number to a Safari version number.
  1713.      * @access protected
  1714.      * @param string $version A string representing the version number.
  1715.      * @link http://web.archive.org/web/20080514173941/http://developer.apple.com/internet/safari/uamatrix.html
  1716.      * @return string Returns the Safari version string. If the version can't be determined, an empty string is
  1717.      *  returned.
  1718.      */
  1719.     protected function safariBuildToSafariVer($version)
  1720.     {
  1721.         $verParts explode('.'$version);
  1722.  
  1723.         //We need a 3 parts version (version 2 will becomes 2.0.0)
  1724.         while (count($verParts3{
  1725.             $verParts[0;
  1726.         }
  1727.         foreach ($verParts as $i => $currPart{
  1728.             $verParts[$i$this->parseInt($currPart);
  1729.         }
  1730.  
  1731.         switch ($verParts[0]{
  1732.             case 419$result '2.0.4';
  1733.                 break;
  1734.             case 417$result '2.0.3';
  1735.                 break;
  1736.             case 416$result '2.0.2';
  1737.                 break;
  1738.  
  1739.             case 412:
  1740.                 if ($verParts[1>= 5{
  1741.                     $result '2.0.1';
  1742.                 else {
  1743.                     $result '2.0';
  1744.                 }
  1745.                 break;
  1746.  
  1747.             case 312:
  1748.                 if ($verParts[1>= 5{
  1749.                     $result '1.3.2';
  1750.                 else {
  1751.                     if ($verParts[1>= 3{
  1752.                         $result '1.3.1';
  1753.                     else {
  1754.                         $result '1.3';
  1755.                     }
  1756.                 }
  1757.                 break;
  1758.  
  1759.             case 125:
  1760.                 if ($verParts[1>= 11{
  1761.                     $result '1.2.4';
  1762.                 else {
  1763.                     if ($verParts[1>= 9{
  1764.                         $result '1.2.3';
  1765.                     else {
  1766.                         if ($verParts[1>= 7{
  1767.                             $result '1.2.2';
  1768.                         else {
  1769.                             $result '1.2';
  1770.                         }
  1771.                     }
  1772.                 }
  1773.                 break;
  1774.  
  1775.             case 100:
  1776.                 if ($verParts[1>= 1{
  1777.                     $result '1.1.1';
  1778.                 else {
  1779.                     $result '1.1';
  1780.                 }
  1781.                 break;
  1782.  
  1783.             case 85:
  1784.                 if ($verParts[1>= 8{
  1785.                     $result '1.0.3';
  1786.                 else {
  1787.                     if ($verParts[1>= 7{
  1788.                         $result '1.0.2';
  1789.                     else {
  1790.                         $result '1.0';
  1791.                     }
  1792.                 }
  1793.                 break;
  1794.  
  1795.             case 73$result '0.9';
  1796.                 break;
  1797.             case 51$result '0.8.1';
  1798.                 break;
  1799.             case 48$result '0.8';
  1800.                 break;
  1801.  
  1802.             default$result '';
  1803.         }
  1804.  
  1805.         return $result;
  1806.     }
  1807.  
  1808.     /**
  1809.      * Set if the browser is executed from a 64-bit platform.
  1810.      * @access protected
  1811.      * @param boolean $is64bit Value that tells if the browser is executed from a 64-bit platform.
  1812.      */
  1813.     protected function set64bit($is64bit)
  1814.     {
  1815.         $this->_is64bit = $is64bit == true;
  1816.     }
  1817.  
  1818.     /**
  1819.      * Set the name of the browser.
  1820.      * @access protected
  1821.      * @param string $browserName The name of the browser.
  1822.      */
  1823.     protected function setBrowser($browserName)
  1824.     {
  1825.         $this->_browserName = $browserName;
  1826.     }
  1827.  
  1828.     /**
  1829.      * Set the browser to be from a mobile device or not.
  1830.      * @access protected
  1831.      * @param boolean $isMobile (optional) Value that tells if the browser is on a mobile device or not.
  1832.      */
  1833.     protected function setMobile($isMobile true)
  1834.     {
  1835.         $this->_isMobile = $isMobile == true;
  1836.     }
  1837.  
  1838.     /**
  1839.      * Set the platform on which the browser is on.
  1840.      * @access protected
  1841.      * @param string $platform The name of the platform.
  1842.      */
  1843.     protected function setPlatform($platform)
  1844.     {
  1845.         $this->_platform = $platform;
  1846.     }
  1847.  
  1848.     /**
  1849.      * Set the platform version on which the browser is on.
  1850.      * @access protected
  1851.      * @param string $platformVer The version numbers of the platform.
  1852.      */
  1853.     protected function setPlatformVersion($platformVer)
  1854.     {
  1855.         $this->_platformVersion = $platformVer;
  1856.     }
  1857.  
  1858.     /**
  1859.      * Set the browser to be a robot (crawler) or not.
  1860.      * @access protected
  1861.      * @param boolean $isRobot (optional) Value that tells if the browser is a robot or not.
  1862.      */
  1863.     protected function setRobot($isRobot true)
  1864.     {
  1865.         $this->_isRobot = $isRobot == true;
  1866.     }
  1867.  
  1868.     /**
  1869.      * Set the version of the browser.
  1870.      * @access protected
  1871.      * @param string $version The version of the browser.
  1872.      */
  1873.     protected function setVersion($version)
  1874.     {
  1875.         $cleanVer $this->cleanVersion($version);
  1876.  
  1877.         if ($cleanVer == ''{
  1878.             $this->_version = self::VERSION_UNKNOWN;
  1879.         else {
  1880.             $this->_version = $cleanVer;
  1881.         }
  1882.     }
  1883.  
  1884.     /**
  1885.      * Convert a WebKit build number to a Safari version number.
  1886.      * @access protected
  1887.      * @param string $version A string representing the version number.
  1888.      * @link http://web.archive.org/web/20080514173941/http://developer.apple.com/internet/safari/uamatrix.html
  1889.      * @return string Returns the Safari version string. If the version can't be determined, an empty string is
  1890.      *  returned.
  1891.      */
  1892.     protected function webKitBuildToSafariVer($version)
  1893.     {
  1894.         $verParts explode('.'$version);
  1895.  
  1896.         //We need a 3 parts version (version 2 will becomes 2.0.0)
  1897.         while (count($verParts3{
  1898.             $verParts[0;
  1899.         }
  1900.         foreach ($verParts as $i => $currPart{
  1901.             $verParts[$i$this->parseInt($currPart);
  1902.         }
  1903.  
  1904.         switch ($verParts[0]{
  1905.             case 419$result '2.0.4';
  1906.                 break;
  1907.  
  1908.             case 418:
  1909.                 if ($verParts[1>= 8{
  1910.                     $result '2.0.4';
  1911.                 else {
  1912.                     $result '2.0.3';
  1913.                 }
  1914.                 break;
  1915.  
  1916.             case 417$result '2.0.3';
  1917.                 break;
  1918.  
  1919.             case 416$result '2.0.2';
  1920.                 break;
  1921.  
  1922.             case 412:
  1923.                 if ($verParts[1>= 7{
  1924.                     $result '2.0.1';
  1925.                 else {
  1926.                     $result '2.0';
  1927.                 }
  1928.                 break;
  1929.  
  1930.             case 312:
  1931.                 if ($verParts[1>= 8{
  1932.                     $result '1.3.2';
  1933.                 else {
  1934.                     if ($verParts[1>= 5{
  1935.                         $result '1.3.1';
  1936.                     else {
  1937.                         $result '1.3';
  1938.                     }
  1939.                 }
  1940.                 break;
  1941.  
  1942.             case 125:
  1943.                 if ($this->compareVersions('5.4'$verParts[1'.' $verParts[2]== -1{
  1944.                     $result '1.2.4'//125.5.5+
  1945.                 else {
  1946.                     if ($verParts[1>= 4{
  1947.                         $result '1.2.3';
  1948.                     else {
  1949.                         if ($verParts[1>= 2{
  1950.                             $result '1.2.2';
  1951.                         else {
  1952.                             $result '1.2';
  1953.                         }
  1954.                     }
  1955.                 }
  1956.                 break;
  1957.  
  1958.             //WebKit 100 can be either Safari 1.1 (Safari build 100) or 1.1.1 (Safari build 100.1)
  1959.             //for this reason, check the Safari build before the WebKit build.
  1960.             case 100$result '1.1.1';
  1961.                 break;
  1962.  
  1963.             case 85:
  1964.                 if ($verParts[1>= 8{
  1965.                     $result '1.0.3';
  1966.                 else {
  1967.                     if ($verParts[1>= 7{
  1968.                         //WebKit 85.7 can be either Safari 1.0 (Safari build 85.5) or 1.0.2 (Safari build 85.7)
  1969.                         //for this reason, check the Safari build before the WebKit build.
  1970.                         $result '1.0.2';
  1971.                     else {
  1972.                         $result '1.0';
  1973.                     }
  1974.                 }
  1975.                 break;
  1976.  
  1977.             case 73$result '0.9';
  1978.                 break;
  1979.             case 51$result '0.8.1';
  1980.                 break;
  1981.             case 48$result '0.8';
  1982.                 break;
  1983.  
  1984.             default$result '';
  1985.         }
  1986.  
  1987.         return $result;
  1988.     }
  1989.  
  1990.     /**
  1991.      * Convert the Windows NT family version numbers to the operating system name. For instance '5.1' returns
  1992.      * 'Windows XP'.
  1993.      * @access protected
  1994.      * @param string $winVer The Windows NT family version numbers as a string.
  1995.      * @param boolean $returnServerFlavor (optional) Since some Windows NT versions have the same values, this flag
  1996.      *  determines if the Server flavor is returned or not. For instance Windows 8.1 and Windows Server 2012 R2 both use
  1997.      *  version 6.3.
  1998.      * @return string The operating system name or the constant PLATFORM_VERSION_UNKNOWN if nothing match the version
  1999.      *  numbers.
  2000.      */
  2001.     protected function windowsNTVerToStr($winVer$returnServerFlavor false)
  2002.     {
  2003.         //https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
  2004.  
  2005.         $cleanWinVer explode('.'$winVer);
  2006.         while (count($cleanWinVer2{
  2007.             array_pop($cleanWinVer);
  2008.         }
  2009.         $cleanWinVer implode('.'$cleanWinVer);
  2010.  
  2011.         if ($this->compareVersions($cleanWinVer'11'>= 0{
  2012.             //Future versions of Windows
  2013.             return self::PLATFORM_WINDOWS ' ' $winVer;
  2014.         else if ($this->compareVersions($cleanWinVer'10'>= 0{
  2015.             //Current version of Windows
  2016.             return $returnServerFlavor (self::PLATFORM_WINDOWS ' Server 2016'(self::PLATFORM_WINDOWS ' 10');
  2017.         else if ($this->compareVersions($cleanWinVer'7'0{
  2018.             if ($this->compareVersions($cleanWinVer'6.3'== 0{
  2019.                 return $returnServerFlavor (self::PLATFORM_WINDOWS ' Server 2012 R2'(self::PLATFORM_WINDOWS ' 8.1');
  2020.             else if ($this->compareVersions($cleanWinVer'6.2'== 0{
  2021.                 return $returnServerFlavor (self::PLATFORM_WINDOWS ' Server 2012'(self::PLATFORM_WINDOWS ' 8');
  2022.             else if ($this->compareVersions($cleanWinVer'6.1'== 0{
  2023.                 return $returnServerFlavor (self::PLATFORM_WINDOWS ' Server 2008 R2'(self::PLATFORM_WINDOWS ' 7');
  2024.             else if ($this->compareVersions($cleanWinVer'6'== 0{
  2025.                 return $returnServerFlavor (self::PLATFORM_WINDOWS ' Server 2008'(self::PLATFORM_WINDOWS ' Vista');
  2026.             else if ($this->compareVersions($cleanWinVer'5.2'== 0{
  2027.                 return $returnServerFlavor (self::PLATFORM_WINDOWS ' Server 2003 / ' self::PLATFORM_WINDOWS ' Server 2003 R2'(self::PLATFORM_WINDOWS ' XP x64 Edition');
  2028.             else if ($this->compareVersions($cleanWinVer'5.1'== 0{
  2029.                 return self::PLATFORM_WINDOWS ' XP';
  2030.             else if ($this->compareVersions($cleanWinVer'5'== 0{
  2031.                 return self::PLATFORM_WINDOWS ' 2000';
  2032.             else if ($this->compareVersions($cleanWinVer'5'&& $this->compareVersions($cleanWinVer'3'>= 0{
  2033.                 return self::PLATFORM_WINDOWS ' NT ' $winVer;
  2034.             }
  2035.         }
  2036.  
  2037.         return self::PLATFORM_VERSION_UNKNOWN//Invalid Windows NT version
  2038.     }
  2039.  
  2040.     /**
  2041.      * Convert the Windows 3.x & 9x family version numbers to the operating system name. For instance '4.10.1998'
  2042.      * returns 'Windows 98'.
  2043.      * @access protected
  2044.      * @param string $winVer The Windows 3.x or 9x family version numbers as a string.
  2045.      * @return string The operating system name or the constant PLATFORM_VERSION_UNKNOWN if nothing match the version
  2046.      *  numbers.
  2047.      */
  2048.     protected function windowsVerToStr($winVer)
  2049.     {
  2050.         //https://support.microsoft.com/en-us/kb/158238
  2051.  
  2052.         if ($this->compareVersions($winVer'4.90'>= && $this->compareVersions($winVer'4.91'0{
  2053.             return self::PLATFORM_WINDOWS ' Me'//Normally range from 4.90.3000 to 4.90.3000A
  2054.         else if ($this->compareVersions($winVer'4.10'>= && $this->compareVersions($winVer'4.11'0{
  2055.             return self::PLATFORM_WINDOWS ' 98'//Normally range from 4.10.1998 to 4.10.2222B
  2056.         else if ($this->compareVersions($winVer'4'>= && $this->compareVersions($winVer'4.04'0{
  2057.             return self::PLATFORM_WINDOWS ' 95'//Normally range from 4.00.950 to 4.03.1214
  2058.         else if ($this->compareVersions($winVer'3.1'== || $this->compareVersions($winVer'3.11'== 0{
  2059.             return self::PLATFORM_WINDOWS ' ' $winVer;
  2060.         else if ($this->compareVersions($winVer'3.10'== 0{
  2061.             return self::PLATFORM_WINDOWS ' 3.1';
  2062.         else {
  2063.             return self::PLATFORM_VERSION_UNKNOWN//Invalid Windows version
  2064.         }
  2065.     }
  2066.  
  2067.     /**
  2068.      * Find the position of the first occurrence of a word in a string.
  2069.      * @access protected
  2070.      * @param string $haystack The string to search in.
  2071.      * @param string $needle The string to search for.
  2072.      * @param boolean $insensitive (optional) Determines if we do a case-sensitive search (false) or a case-insensitive
  2073.      *  one (true).
  2074.      * @param int $offset If specified, search will start this number of characters counted from the beginning of the
  2075.      *  string. If the offset is negative, the search will start this number of characters counted from the end of the
  2076.      *  string.
  2077.      * @param string $foundString String buffer that will contain the exact matching needle found. Set to NULL when
  2078.      *  return value of the function is false.
  2079.      * @return mixed Returns the position of the needle (int) if found, false otherwise. Warning this function may
  2080.      *  return Boolean false, but may also return a non-Boolean value which evaluates to false.
  2081.      */
  2082.     protected function wordPos($haystack$needle$insensitive true$offset 0&$foundString NULL)
  2083.     {
  2084.         if ($offset != 0{
  2085.             $haystack substr($haystack$offset);
  2086.         }
  2087.  
  2088.         $parts explode(' '$needle);
  2089.         foreach ($parts as $i => $currPart{
  2090.             $parts[$ipreg_quote($currPart'/');
  2091.         }
  2092.  
  2093.         $regex '/(?<=\A|[\s\/\\.,;:_()-])' implode('[\s\/\\.,;:_()-]'$parts'(?=[\s\/\\.,;:_()-]|$)/';
  2094.         if ($insensitive{
  2095.              $regex .= 'i';
  2096.         }
  2097.  
  2098.         if (preg_match($regex$haystack$matchesPREG_OFFSET_CAPTURE)) {
  2099.             $foundString $matches[0][0];
  2100.             return (int)$matches[0][1];
  2101.         }
  2102.  
  2103.         return false;
  2104.     }
  2105. }

Documentation generated on Sun, 15 Jul 2018 10:02:37 -0400 by phpDocumentor 1.4.3