📄 detect.php
字号:
<?php// {{{ license// +----------------------------------------------------------------------+// | PHP version 4.2 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2003 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 2.0 of the PHP license, |// | that is bundled with this package in the file LICENSE, and is |// | available at through the world-wide-web at |// | http://www.php.net/license/2_02.txt. |// | If you did not receive a copy of the PHP license and are unable to |// | obtain it through the world-wide-web, please send a note to |// | license@php.net so we can mail you a copy immediately. |// +----------------------------------------------------------------------+// | Authors: Dan Allen <dan@mojavelinux.com> |// | Jason Rust <jrust@php.net> |// +----------------------------------------------------------------------+// $Id: Detect.php,v 1.12 2004/03/10 20:22:34 jrust Exp $// }}}// {{{ constantsdefine('NET_USERAGENT_DETECT_BROWSER', 'browser');define('NET_USERAGENT_DETECT_OS', 'os');define('NET_USERAGENT_DETECT_FEATURES', 'features');define('NET_USERAGENT_DETECT_QUIRKS', 'quirks');define('NET_USERAGENT_DETECT_ACCEPT', 'accept');define('NET_USERAGENT_DETECT_ALL', 'all');// }}}// {{{ class Net_UserAgent_Detect/** * The Net_UserAgent_Detect object does a number of tests on an HTTP user * agent string. The results of these tests are available via methods of * the object. Note that all methods in this class can be called * statically. The constructor and singleton methods are only retained * for BC. * * This module is based upon the JavaScript browser detection code * available at http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html. * This module had many influences from the lib/Browser.php code in * version 1.3 of Horde. * * @author Jason Rust <jrust@php.net> * @author Dan Allen <dan@mojavelinux.com> * @author Chuck Hagenbuch <chuck@horde.org> * @author Jon Parise <jon@horde.org> * @package Net_UserAgent */// }}}class Net_UserAgent_Detect { // {{{ constructor function Net_UserAgent_Detect($in_userAgent = null, $in_detect = null) { $this->detect($in_userAgent, $in_detect); } // }}} // {{{ singleton /** * To be used in place of the contructor to return only open instance. * * @access public * @return object Net_UserAgent_Detect instance */ function &singleton($in_userAgent = null, $in_detect = null) { static $instance; if (!isset($instance)) { $instance = new Net_UserAgent_Detect($in_userAgent, $in_detect); } return $instance; } // }}} // {{{ detect() /** * Detect the user agent and prepare flags, features and quirks * based on what is found * * This is the core of the Net_UserAgent_Detect class. It moves its * way through the user agent string setting up the flags based on * the vendors and versions of the browsers, determining the OS and * setting up the features and quirks owned by each of the relevant * clients. Note that if you are going to be calling methods of * this class statically then set all the parameters using th * setOption() * * @param string $in_userAgent (optional) User agent override. * @param mixed $in_detect (optional) The level of checking to do. * * @access public * @return void */ function detect($in_userAgent = null, $in_detect = null) { static $hasRun; if (!empty($hasRun)) { return; } $hasRun = true; // {{{ set up static properties $options = &Net_UserAgent_Detect::_getStaticProperty('options'); $in_userAgent = isset($options['userAgent']) && is_null($in_userAgent) ? $options['userAgent'] : null; $in_detect = isset($options['detectOptions']) && is_null($in_detect) ? $options['detectOptions'] : null; // User agent string that is being analyzed $userAgent = &Net_UserAgent_Detect::_getStaticProperty('userAgent'); // Array that stores all of the flags for the vendor and version // of the different browsers $browser = &Net_UserAgent_Detect::_getStaticProperty('browser'); $browser = array_flip(array('ns', 'ns2', 'ns3', 'ns4', 'ns4up', 'nav', 'ns6', 'ns6up', 'gecko', 'ie', 'ie3', 'ie4', 'ie4up', 'ie5', 'ie5_5', 'ie5up', 'ie6', 'ie6up', 'opera', 'opera2', 'opera3', 'opera4', 'opera5', 'opera5up', 'aol', 'aol3', 'aol4', 'aol5', 'aol6', 'aol7', 'webtv', 'aoltv', 'tvnavigator', 'hotjava', 'hotjava3', 'hotjava3up', 'konq', 'safari')); // Array that stores all of the flags for the operating systems, // and in some cases the versions of those operating systems (windows) $os = &Net_UserAgent_Detect::_getStaticProperty('os'); $os = array_flip(array('win', 'win95', 'win16', 'win31', 'win9x', 'win98', 'winme', 'win2k', 'winxp', 'winnt', 'os2', 'mac', 'mac68k', 'macppc', 'linux', 'unix', 'vms', 'sun', 'sun4', 'sun5', 'suni86', 'irix', 'irix5', 'irix6', 'hpux', 'hpux9', 'hpux10', 'aix', 'aix1', 'aix2', 'aix3', 'aix4', 'sco', 'unixware', 'mpras', 'reliant', 'dec', 'sinix', 'freebsd', 'bsd')); // Array which stores known issues with the given client that can // be used for on the fly tweaking so that the client may recieve // the proper handling of this quirk. $quirks = &Net_UserAgent_Detect::_getStaticProperty('quirks'); $quirks = array( 'must_cache_forms' => false, 'popups_disabled' => false, 'empty_file_input_value' => false, 'cache_ssl_downloads' => false, 'scrollbar_in_way' => false, 'break_disposition_header' => false, 'nested_table_render_bug' => false); // Array that stores credentials for each of the browser/os // combinations. These allow quick access to determine if the // current client has a feature that is going to be implemented // in the script. $features = &Net_UserAgent_Detect::_getStaticProperty('features'); $features = array( 'javascript' => false, 'dhtml' => false, 'dom' => false, 'sidebar' => false, 'gecko' => false); // The leading identifier is the very first term in the user // agent string, which is used to identify clients which are not // Mosaic-based browsers. $leadingIdentifier = &Net_UserAgent_Detect::_getStaticProperty('leadingIdentifier'); // The full version of the client as supplied by the very first // numbers in the user agent $version = &Net_UserAgent_Detect::_getStaticProperty('version'); $version = 0; // The major part of the client version, which is the integer // value of the version. $majorVersion = &Net_UserAgent_Detect::_getStaticProperty('majorVersion'); $majorVersion = 0; // The minor part of the client version, which is the decimal // parts of the version $subVersion = &Net_UserAgent_Detect::_getStaticProperty('subVersion'); $subVersion = 0; // }}} // detemine what user agent we are using if (is_null($in_userAgent)) { if (isset($_SERVER['HTTP_USER_AGENT'])) { $userAgent = $_SERVER['HTTP_USER_AGENT']; } elseif (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'])) { $userAgent = $GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT']; } else { $userAgent = ''; } } else { $userAgent = $in_userAgent; } // get the lowercase version for case-insensitive searching $agt = strtolower($userAgent); // figure out what we need to look for $detectOptions = array(NET_USERAGENT_DETECT_BROWSER, NET_USERAGENT_DETECT_OS, NET_USERAGENT_DETECT_FEATURES, NET_USERAGENT_DETECT_QUIRKS, NET_USERAGENT_DETECT_ACCEPT, NET_USERAGENT_DETECT_ALL); $detect = is_null($in_detect) ? NET_USERAGENT_DETECT_ALL : $in_detect; settype($detect, 'array'); foreach($detectOptions as $option) { if (in_array($option, $detect)) { $detectFlags[$option] = true; } else { $detectFlags[$option] = false; } } // initialize the arrays of browsers and operating systems // Get the type and version of the client if (preg_match(";^([[:alnum:]]+)[ /\(]*[[:alpha:]]*([\d]*)(\.[\d\.]*);", $agt, $matches)) { list(, $leadingIdentifier, $majorVersion, $subVersion) = $matches; } if (empty($leadingIdentifier)) { $leadingIdentifier = 'Unknown'; } $version = $majorVersion . $subVersion; // Browser type if ($detectFlags[NET_USERAGENT_DETECT_ALL] || $detectFlags[NET_USERAGENT_DETECT_BROWSER]) { $browser['konq'] = $browser['safari'] = (strpos($agt, 'konqueror') !== false || strpos($agt, 'safari') !== false); $browser['text'] = (strpos($agt, 'links') !== false) || (strpos($agt, 'lynx') !== false) || (strpos($agt, 'w3m') !== false); $browser['ns'] = (strpos($agt, 'mozilla') !== false) && !(strpos($agt, 'spoofer') !== false) && !(strpos($agt, 'compatible') !== false) && !(strpos($agt, 'hotjava') !== false) && !(strpos($agt, 'opera') !== false) && !(strpos($agt, 'webtv') !== false) ? 1 : 0; $browser['ns2'] = $browser['ns'] && $majorVersion == 2; $browser['ns3'] = $browser['ns'] && $majorVersion == 3; $browser['ns4'] = $browser['ns'] && $majorVersion == 4; $browser['ns4up'] = $browser['ns'] && $majorVersion >= 4; // determine if this is a Netscape Navigator $browser['nav'] = $browser['ns'] && $majorVersion < 5; $browser['ns6'] = !$browser['konq'] && $browser['ns'] && $majorVersion == 5; $browser['ns6up'] = $browser['ns6'] && $majorVersion >= 5; $browser['gecko'] = (strpos($agt, 'gecko') !== false && !$browser['konq']); $browser['ie'] = (strpos($agt, 'msie') !== false) && !(strpos($agt, 'opera') !== false); $browser['ie3'] = $browser['ie'] && $majorVersion < 4; $browser['ie4'] = $browser['ie'] && $majorVersion == 4 && (strpos($agt, 'msie 4') !== false); $browser['ie4up'] = $browser['ie'] && !$browser['ie3']; $browser['ie5'] = $browser['ie4up'] && (strpos($agt, 'msie 5.0') !== false); $browser['ie5_5'] = $browser['ie4up'] && (strpos($agt, 'msie 5.5') !== false); $browser['ie5up'] = $browser['ie4up'] && !$browser['ie3'] && !$browser['ie4']; $browser['ie5_5up'] = $browser['ie5up'] && !$browser['ie5']; $browser['ie6'] = (strpos($agt, 'msie 6') !== false); $browser['ie6up'] = $browser['ie5up'] && !$browser['ie5'] && !$browser['ie5_5']; $browser['opera'] = (strpos($agt, 'opera') !== false); $browser['opera2'] = (strpos($agt, 'opera 2') !== false) || (strpos($agt, 'opera/2') !== false); $browser['opera3'] = (strpos($agt, 'opera 3') !== false) || (strpos($agt, 'opera/3') !== false); $browser['opera4'] = (strpos($agt, 'opera 4') !== false) || (strpos($agt, 'opera/4') !== false); $browser['opera5'] = (strpos($agt, 'opera 5') !== false) || (strpos($agt, 'opera/5') !== false); $browser['opera5up'] = $browser['opera'] && !$browser['opera2'] && !$browser['opera3'] && !$browser['opera4']; $browser['aol'] = (strpos($agt, 'aol') !== false); $browser['aol3'] = $browser['aol'] && $browser['ie3']; $browser['aol4'] = $browser['aol'] && $browser['ie4']; $browser['aol5'] = (strpos($agt, 'aol 5') !== false); $browser['aol6'] = (strpos($agt, 'aol 6') !== false); $browser['aol7'] = (strpos($agt, 'aol 7') !== false); $browser['webtv'] = (strpos($agt, 'webtv') !== false); $browser['aoltv'] = $browser['tvnavigator'] = (strpos($agt, 'navio') !== false) || (strpos($agt, 'navio_aoltv') !== false); $browser['hotjava'] = (strpos($agt, 'hotjava') !== false); $browser['hotjava3'] = $browser['hotjava'] && $majorVersion == 3; $browser['hotjava3up'] = $browser['hotjava'] && $majorVersion >= 3; } if ($detectFlags[NET_USERAGENT_DETECT_ALL] || ($detectFlags[NET_USERAGENT_DETECT_BROWSER] && $detectFlags[NET_USERAGENT_DETECT_FEATURES])) { // Javascript Check if ($browser['ns2'] || $browser['ie3']) { Net_UserAgent_Detect::setFeature('javascript', 1.0); } elseif ($browser['opera5up']) { Net_UserAgent_Detect::setFeature('javascript', 1.3); } elseif ($browser['opera'] || $browser['ns3']) { Net_UserAgent_Detect::setFeature('javascript', 1.1); } elseif (($browser['ns4'] && ($version <= 4.05)) || $browser['ie4']) { Net_UserAgent_Detect::setFeature('javascript', 1.2); } elseif (($browser['ie5up'] && strpos($agt, 'mac') !== false) || $browser['konq']) { Net_UserAgent_Detect::setFeature('javascript', 1.4); } // I can't believe IE6 still has javascript 1.3, what a shitty browser elseif (($browser['ns4'] && ($version > 4.05)) || $browser['ie5up'] || $browser['hotjava3up']) { Net_UserAgent_Detect::setFeature('javascript', 1.3); } elseif ($browser['ns6up'] || $browser['gecko']) { Net_UserAgent_Detect::setFeature('javascript', 1.5); } } /** OS Check **/ if ($detectFlags[NET_USERAGENT_DETECT_ALL] || $detectFlags[NET_USERAGENT_DETECT_OS]) { $os['win'] = (strpos($agt, 'win') !== false) || (strpos($agt, '16bit') !== false); $os['win95'] = (strpos($agt, 'win95') !== false) || (strpos($agt, 'windows 95') !== false); $os['win16'] = (strpos($agt, 'win16') !== false) || (strpos($agt, '16bit') !== false) || (strpos($agt, 'windows 3.1') !== false) || (strpos($agt, 'windows 16-bit') !== false); $os['win31'] = (strpos($agt, 'windows 3.1') !== false) || (strpos($agt, 'win16') !== false) || (strpos($agt, 'windows 16-bit') !== false); $os['winme'] = (strpos($agt, 'win 9x 4.90') !== false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -