Back to Browser class

Method match

public void
match
(mixed $userAgent = null, mixed $accept = null)
Parses the user agent string and inititializes the object with all the known features and quirks for the given browser.
Parameters
  • string $userAgent The browser string to parse.
  • string $accept The HTTP_ACCEPT settings to use.
Returns
  • void
Since
  • 1.7.0
Class: Browser
Project: Joomla

Method match - Source code

/**
 * Parses the user agent string and inititializes the object with
 * all the known features and quirks for the given browser.
 *
 * @param   string  $userAgent  The browser string to parse.
 * @param   string  $accept     The HTTP_ACCEPT settings to use.
 *
 * @return  void
 *
 * @since   1.7.0
 */
public function match($userAgent = null, $accept = null)
{
    // Set our agent string.
    if (\is_null($userAgent)) {
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
        }
    } else {
        $this->agent = $userAgent;
    }
    $this->lowerAgent = strtolower($this->agent);
    // Set our accept string.
    if (\is_null($accept)) {
        if (isset($_SERVER['HTTP_ACCEPT'])) {
            $this->accept = strtolower(trim($_SERVER['HTTP_ACCEPT']));
        }
    } else {
        $this->accept = strtolower($accept);
    }
    if (!empty($this->agent)) {
        $this->_setPlatform();
        /*
         * Determine if mobile. Note: Some Handhelds have their screen resolution in the
         * user agent string, which we can use to look for mobile agents.
         */
        if (strpos($this->agent, 'MOT-') !== false || strpos($this->lowerAgent, 'j-') !== false || preg_match('/(mobileexplorer|openwave|opera mini|opera mobi|operamini|avantgo|wap|elaine)/i', $this->agent) || preg_match('/(iPhone|iPod|iPad|Android|Mobile|Phone|BlackBerry|Xiino|Palmscape|palmsource)/i', $this->agent) || preg_match('/(Nokia|Ericsson|docomo|digital paths|portalmmm|CriOS[\\/ ]([0-9.]+))/i', $this->agent) || preg_match('/(UP|UP.B|UP.L)/', $this->agent) || preg_match('/; (120x160|240x280|240x320|320x320)\\)/', $this->agent)) {
            $this->mobile = true;
        }
        /*
         * We have to check for Edge as the first browser, because Edge has something like:
         * Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393
         */
        if (preg_match('|Edge\\/([0-9.]+)|', $this->agent, $version)) {
            $this->setBrowser('edge');
            if (strpos($version[1], '.') !== false) {
                list($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
            } else {
                $this->majorVersion = $version[1];
                $this->minorVersion = 0;
            }
        } elseif (preg_match('|Edg\\/([0-9.]+)|', $this->agent, $version)) {
            $this->setBrowser('edg');
            list($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
        } elseif (preg_match('|Opera[\\/ ]([0-9.]+)|', $this->agent, $version)) {
            $this->setBrowser('opera');
            list($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
            /*
             * Due to changes in Opera UA, we need to check Version/xx.yy,
             * but only if version is > 9.80. See: http://dev.opera.com/articles/view/opera-ua-string-changes/
             */
            if ($this->majorVersion == 9 && $this->minorVersion >= 80) {
                $this->identifyBrowserVersion();
            }
        } elseif (preg_match('/OPR[\\/ ]([0-9.]+)/', $this->agent, $version)) {
            $this->setBrowser('opera');
            list($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
        } elseif (preg_match('/Chrome[\\/ ]([0-9.]+)/i', $this->agent, $version) || preg_match('/CrMo[\\/ ]([0-9.]+)/i', $this->agent, $version) || preg_match('/CriOS[\\/ ]([0-9.]+)/i', $this->agent, $version)) {
            $this->setBrowser('chrome');
            list($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
        } elseif (strpos($this->lowerAgent, 'elaine/') !== false || strpos($this->lowerAgent, 'palmsource') !== false || strpos($this->lowerAgent, 'digital paths') !== false) {
            $this->setBrowser('palm');
        } elseif (preg_match('/MSIE ([0-9.]+)/i', $this->agent, $version) || preg_match('/IE ([0-9.]+)/i', $this->agent, $version) || preg_match('/Internet Explorer[\\/ ]([0-9.]+)/i', $this->agent, $version) || preg_match('/Trident\\/.*rv:([0-9.]+)/i', $this->agent, $version)) {
            $this->setBrowser('msie');
            // Special case for IE 11+
            if (strpos($version[0], 'Trident') !== false && strpos($version[0], 'rv:') !== false) {
                preg_match('|rv:([0-9.]+)|', $this->agent, $version);
            }
            if (strpos($version[1], '.') !== false) {
                list($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
            } else {
                $this->majorVersion = $version[1];
                $this->minorVersion = 0;
            }
        } elseif (preg_match('|amaya\\/([0-9.]+)|', $this->agent, $version)) {
            $this->setBrowser('amaya');
            $this->majorVersion = $version[1];
            if (isset($version[2])) {
                $this->minorVersion = $version[2];
            }
        } elseif (preg_match('|ANTFresco\\/([0-9]+)|', $this->agent, $version)) {
            $this->setBrowser('fresco');
        } elseif (strpos($this->lowerAgent, 'avantgo') !== false) {
            $this->setBrowser('avantgo');
        } elseif (preg_match('|[Kk]onqueror\\/([0-9]+)|', $this->agent, $version) || preg_match('|Safari/([0-9]+)\\.?([0-9]+)?|', $this->agent, $version)) {
            // Konqueror and Apple's Safari both use the KHTML rendering engine.
            $this->setBrowser('konqueror');
            $this->majorVersion = $version[1];
            if (isset($version[2])) {
                $this->minorVersion = $version[2];
            }
            if (strpos($this->agent, 'Safari') !== false && $this->majorVersion >= 60) {
                // Safari.
                $this->setBrowser('safari');
                $this->identifyBrowserVersion();
            }
        } elseif (preg_match('|Firefox\\/([0-9.]+)|', $this->agent, $version)) {
            $this->setBrowser('firefox');
            list($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
        } elseif (preg_match('|Lynx\\/([0-9]+)|', $this->agent, $version)) {
            $this->setBrowser('lynx');
        } elseif (preg_match('|Links \\(([0-9]+)|', $this->agent, $version)) {
            $this->setBrowser('links');
        } elseif (preg_match('|HotJava\\/([0-9]+)|', $this->agent, $version)) {
            $this->setBrowser('hotjava');
        } elseif (strpos($this->agent, 'UP/') !== false || strpos($this->agent, 'UP.B') !== false || strpos($this->agent, 'UP.L') !== false) {
            $this->setBrowser('up');
        } elseif (strpos($this->agent, 'Xiino/') !== false) {
            $this->setBrowser('xiino');
        } elseif (strpos($this->agent, 'Palmscape/') !== false) {
            $this->setBrowser('palmscape');
        } elseif (strpos($this->agent, 'Nokia') !== false) {
            $this->setBrowser('nokia');
        } elseif (strpos($this->agent, 'Ericsson') !== false) {
            $this->setBrowser('ericsson');
        } elseif (strpos($this->lowerAgent, 'wap') !== false) {
            $this->setBrowser('wap');
        } elseif (strpos($this->lowerAgent, 'docomo') !== false || strpos($this->lowerAgent, 'portalmmm') !== false) {
            $this->setBrowser('imode');
        } elseif (strpos($this->agent, 'BlackBerry') !== false) {
            $this->setBrowser('blackberry');
        } elseif (strpos($this->agent, 'MOT-') !== false) {
            $this->setBrowser('motorola');
        } elseif (strpos($this->lowerAgent, 'j-') !== false) {
            $this->setBrowser('mml');
        } elseif (preg_match('|Mozilla\\/([0-9.]+)|', $this->agent, $version)) {
            $this->setBrowser('mozilla');
            list($this->majorVersion, $this->minorVersion) = explode('.', $version[1]);
        }
    }
}