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

Source for file Detect.php

Documentation is available at Detect.php

  1. <?php
  2. // {{{ license
  3.  
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4.2                                                      |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2003 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Dan Allen <dan@mojavelinux.com>                             |
  18. // |          Jason Rust <jrust@php.net>                                  |
  19. // +----------------------------------------------------------------------+
  20.  
  21. // $Id: Detect.php,v 1.19 2005/04/18 18:32:50 jrust Exp $
  22.  
  23. // }}}
  24. // {{{ constants
  25.  
  26. define('NET_USERAGENT_DETECT_BROWSER',  'browser');
  27. define('NET_USERAGENT_DETECT_OS',       'os');
  28. define('NET_USERAGENT_DETECT_FEATURES''features');
  29. define('NET_USERAGENT_DETECT_QUIRKS',   'quirks');
  30. define('NET_USERAGENT_DETECT_ACCEPT',   'accept');
  31. define('NET_USERAGENT_DETECT_ALL',      'all');
  32.  
  33. // }}}
  34. // {{{ class Net_UserAgent_Detect
  35.  
  36. /**
  37.  * The Net_UserAgent_Detect object does a number of tests on an HTTP user
  38.  * agent string.  The results of these tests are available via methods of
  39.  * the object.  Note that all methods in this class can be called
  40.  * statically.  The constructor and singleton methods are only retained
  41.  * for BC.
  42.  *
  43.  * This module is based upon the JavaScript browser detection code
  44.  * available at http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html.
  45.  * This module had many influences from the lib/Browser.php code in
  46.  * version 1.3 of Horde.
  47.  *
  48.  * @author   Jason Rust <jrust@php.net>
  49.  * @author   Dan Allen <dan@mojavelinux.com>
  50.  * @author   Chuck Hagenbuch <chuck@horde.org>
  51.  * @author   Jon Parise <jon@horde.org>
  52.  * @package  Net_UserAgent
  53.  */
  54.  
  55. // }}}
  56.     // {{{ constructor
  57.  
  58.     function Net_UserAgent_Detect($in_userAgent = null$in_detect = null)
  59.     {
  60.         $this->detect($in_userAgent$in_detect);
  61.     }
  62.  
  63.     // }}}
  64.     // {{{ singleton
  65.  
  66.     /**
  67.      * To be used in place of the contructor to return only open instance.
  68.      *
  69.      * @access public
  70.      * @return object Net_UserAgent_Detect instance
  71.      */
  72.     function &singleton($in_userAgent = null$in_detect = null
  73.     
  74.         static $instance;
  75.        
  76.         if (!isset($instance)) 
  77.             $instance = new Net_UserAgent_Detect($in_userAgent$in_detect)
  78.         }
  79.         
  80.         return $instance
  81.     }
  82.  
  83.     // }}}
  84.     // {{{ detect()
  85.  
  86.     /**
  87.      * Detect the user agent and prepare flags, features and quirks
  88.      * based on what is found
  89.      *
  90.      * This is the core of the Net_UserAgent_Detect class.  It moves its
  91.      * way through the user agent string setting up the flags based on
  92.      * the vendors and versions of the browsers, determining the OS and
  93.      * setting up the features and quirks owned by each of the relevant
  94.      * clients.  Note that if you are going to be calling methods of
  95.      * this class statically then set all the parameters using th
  96.      * setOption()
  97.      *
  98.      * @param  string $in_userAgent (optional) User agent override.
  99.      * @param  mixed $in_detect (optional) The level of checking to do.
  100.      *
  101.      * @access public
  102.      * @return void 
  103.      */
  104.     function detect($in_userAgent = null$in_detect = null)
  105.     {
  106.         static $hasRun;
  107.         $options &Net_UserAgent_Detect::_getStaticProperty('options');
  108.         if (!empty($hasRun&& empty($options['re-evaluate'])) {
  109.             return;
  110.         }
  111.  
  112.         $hasRun = true;
  113.         // {{{ set up static properties
  114.  
  115.         $in_userAgent = isset($options['userAgent']&& is_null($in_userAgent$options['userAgent': null;
  116.         $in_detect = isset($options['detectOptions']&& is_null($in_detect$options['detectOptions': null;
  117.  
  118.         // User agent string that is being analyzed
  119.         $userAgent &Net_UserAgent_Detect::_getStaticProperty('userAgent');
  120.  
  121.         // Array that stores all of the flags for the vendor and version
  122.         // of the different browsers
  123.         $browser &Net_UserAgent_Detect::_getStaticProperty('browser');
  124.         $browser array_flip(array('ns''ns2''ns3''ns4''ns4up''nav''ns6''ns6up''firefox''firefox0.x''firefox1.x''gecko''ie''ie3''ie4''ie4up''ie5''ie5_5''ie5up''ie6''ie6up''opera''opera2''opera3''opera4''opera5''opera6''opera7''opera5up''opera6up''opera7up''aol''aol3''aol4''aol5''aol6''aol7''aol8''webtv''aoltv''tvnavigator''hotjava''hotjava3''hotjava3up''konq''safari''netgem''webdav'));
  125.         
  126.         // Array that stores all of the flags for the operating systems,
  127.         // and in some cases the versions of those operating systems (windows)
  128.         $os &Net_UserAgent_Detect::_getStaticProperty('os');
  129.         $os array_flip(array('win''win95''win16''win31''win9x''win98''winme''win2k''winxp''winnt''win2003''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'));
  130.  
  131.         // Array which stores known issues with the given client that can
  132.         // be used for on the fly tweaking so that the client may recieve
  133.         // the proper handling of this quirk.
  134.         $quirks &Net_UserAgent_Detect::_getStaticProperty('quirks');
  135.         $quirks = array(
  136.                 'must_cache_forms'         => false,
  137.                 'popups_disabled'          => false,
  138.                 'empty_file_input_value'   => false,
  139.                 'cache_ssl_downloads'      => false,
  140.                 'scrollbar_in_way'         => false,
  141.                 'break_disposition_header' => false,
  142.                 'nested_table_render_bug'  => false);
  143.  
  144.         // Array that stores credentials for each of the browser/os
  145.         // combinations.  These allow quick access to determine if the
  146.         // current client has a feature that is going to be implemented
  147.         // in the script.
  148.         $features &Net_UserAgent_Detect::_getStaticProperty('features');
  149.         $features = array(
  150.                 'javascript'   => false,
  151.                 'dhtml'        => false,
  152.                 'dom'          => false,
  153.                 'sidebar'      => false,
  154.                 'gecko'        => false);
  155.  
  156.         // The leading identifier is the very first term in the user
  157.         // agent string, which is used to identify clients which are not
  158.         // Mosaic-based browsers.
  159.         $leadingIdentifier &Net_UserAgent_Detect::_getStaticProperty('leadingIdentifier');
  160.  
  161.         // The full version of the client as supplied by the very first
  162.         // numbers in the user agent
  163.         $version &Net_UserAgent_Detect::_getStaticProperty('version');
  164.         $version = 0;
  165.  
  166.         // The major part of the client version, which is the integer
  167.         // value of the version.
  168.         $majorVersion &Net_UserAgent_Detect::_getStaticProperty('majorVersion');
  169.         $majorVersion = 0;
  170.  
  171.         // The minor part of the client version, which is the decimal
  172.         // parts of the version
  173.         $subVersion &Net_UserAgent_Detect::_getStaticProperty('subVersion');
  174.         $subVersion = 0;
  175.  
  176.         // }}}
  177.         // detemine what user agent we are using
  178.         if (is_null($in_userAgent)) {
  179.             if (isset($_SERVER['HTTP_USER_AGENT'])) {
  180.                 $userAgent $_SERVER['HTTP_USER_AGENT'];
  181.             }
  182.             elseif (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'])) {
  183.                 $userAgent $GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'];
  184.             }
  185.             else {
  186.                 $userAgent '';
  187.             }
  188.         }
  189.         else {
  190.             $userAgent $in_userAgent;
  191.         }
  192.  
  193.         // get the lowercase version for case-insensitive searching
  194.         $agt strtolower($userAgent);
  195.  
  196.         // figure out what we need to look for
  197.         $detectOptions = array(NET_USERAGENT_DETECT_BROWSER,
  198.                 NET_USERAGENT_DETECT_OSNET_USERAGENT_DETECT_FEATURES,
  199.                 NET_USERAGENT_DETECT_QUIRKSNET_USERAGENT_DETECT_ACCEPT
  200.                 NET_USERAGENT_DETECT_ALL);
  201.         $detect is_null($in_detectNET_USERAGENT_DETECT_ALL : $in_detect;
  202.         settype($detect'array');
  203.         foreach($detectOptions as $option{
  204.             if (in_array($option$detect)) {
  205.                 $detectFlags[$option= true; 
  206.             }
  207.             else {
  208.                 $detectFlags[$option= false;
  209.             }
  210.         }
  211.  
  212.         // initialize the arrays of browsers and operating systems
  213.  
  214.         // Get the type and version of the client
  215.         if (preg_match(";^([[:alnum:]]+)[ /\(]*[[:alpha:]]*([\d]*)(\.[\d\.]*);"$agt$matches)) {
  216.             list($leadingIdentifier$majorVersion$subVersion$matches;
  217.         }
  218.  
  219.         if (empty($leadingIdentifier)) {
  220.             $leadingIdentifier 'Unknown';
  221.         }
  222.  
  223.         $version $majorVersion $subVersion;
  224.     
  225.         // Browser type
  226.         if ($detectFlags[NET_USERAGENT_DETECT_ALL|| $detectFlags[NET_USERAGENT_DETECT_BROWSER]{
  227.             $browser['webdav']  ($agt == 'microsoft data access internet publishing provider dav' || $agt == 'microsoft data access internet publishing provider protocol discovery');
  228.             $browser['konq']    $browser['safari'(strpos($agt'konqueror'!== false || strpos($agt'safari'!== false);
  229.             $browser['text']    strpos($agt'links'!== false || strpos($agt'lynx'!== false || strpos($agt'w3m'!== false;
  230.             $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;
  231.             $browser['netgem']  strpos($agt'netgem'!== false;
  232.             $browser['ns2']     $browser['ns'&& $majorVersion == 2;
  233.             $browser['ns3']     $browser['ns'&& $majorVersion == 3;
  234.             $browser['ns4']     $browser['ns'&& $majorVersion == 4;
  235.             $browser['ns4up']   $browser['ns'&& $majorVersion >= 4;
  236.             // determine if this is a Netscape Navigator
  237.             $browser['nav']     $browser['ns'&& $majorVersion < 5;
  238.             $browser['ns6']     !$browser['konq'&& $browser['ns'&& $majorVersion == 5;
  239.             $browser['ns6up']   $browser['ns6'&& $majorVersion >= 5;
  240.             $browser['gecko']   strpos($agt'gecko'!== false && !$browser['konq'];
  241.             $browser['firefox'$browser['gecko'&& strpos($agt'firefox'!== false;
  242.             $browser['firefox0.x'$browser['firefox'&& strpos($agt'firefox/0.'!== false;
  243.             $browser['firefox1.x'$browser['firefox'&& strpos($agt'firefox/1.'!== false;
  244.             $browser['ie']      strpos($agt'msie'!== false && !(strpos($agt'opera'!== false);
  245.             $browser['ie3']     $browser['ie'&& $majorVersion < 4;
  246.             $browser['ie4']     $browser['ie'&& $majorVersion == 4 && (strpos($agt'msie 4'!== false);
  247.             $browser['ie4up']   $browser['ie'&& !$browser['ie3'];
  248.             $browser['ie5']     $browser['ie4up'&& (strpos($agt'msie 5.0'!== false);
  249.             $browser['ie5_5']   $browser['ie4up'&& (strpos($agt'msie 5.5'!== false);
  250.             $browser['ie5up']   $browser['ie4up'&& !$browser['ie3'&& !$browser['ie4'];
  251.             $browser['ie5_5up'$browser['ie5up'&& !$browser['ie5'];
  252.             $browser['ie6']     strpos($agt'msie 6'!== false;
  253.             $browser['ie6up']   $browser['ie5up'&& !$browser['ie5'&& !$browser['ie5_5'];
  254.             $browser['opera']   strpos($agt'opera'!== false;
  255.             $browser['opera2']  strpos($agt'opera 2'!== false || strpos($agt'opera/2'!== false;
  256.             $browser['opera3']  strpos($agt'opera 3'!== false || strpos($agt'opera/3'!== false;
  257.             $browser['opera4']  strpos($agt'opera 4'!== false || strpos($agt'opera/4'!== false;
  258.             $browser['opera5']  strpos($agt'opera 5'!== false || strpos($agt'opera/5'!== false;
  259.             $browser['opera6']  strpos($agt'opera 6'!== false || strpos($agt'opera/6'!== false;
  260.             $browser['opera7']  strpos($agt'opera 7'!== false || strpos($agt'opera/7'!== false;
  261.             $browser['opera5up'$browser['opera'&& !$browser['opera2'&& !$browser['opera3'&& !$browser['opera4'];
  262.             $browser['opera6up'$browser['opera'&& !$browser['opera2'&& !$browser['opera3'&& !$browser['opera4'&& !$browser['opera5'];
  263.             $browser['opera7up'$browser['opera'&& !$browser['opera2'&& !$browser['opera3'&& !$browser['opera4'&& !$browser['opera5'&& !$browser['opera6'];
  264.  
  265.             $browser['aol']   strpos($agt'aol'!== false;
  266.             $browser['aol3']  $browser['aol'&& $browser['ie3'];
  267.             $browser['aol4']  $browser['aol'&& $browser['ie4'];
  268.             $browser['aol5']  strpos($agt'aol 5'!== false;
  269.             $browser['aol6']  strpos($agt'aol 6'!== false;
  270.             $browser['aol7']  strpos($agt'aol 7'!== false || strpos($agt'aol7'!== false;
  271.             $browser['aol8']  strpos($agt'aol 8'!== false || strpos($agt'aol8'!== false;
  272.             $browser['webtv'strpos($agt'webtv'!== false; 
  273.             $browser['aoltv'$browser['tvnavigator'strpos($agt'navio'!== false || strpos($agt'navio_aoltv'!== false; 
  274.             $browser['hotjava'strpos($agt'hotjava'!== false;
  275.             $browser['hotjava3'$browser['hotjava'&& $majorVersion == 3;
  276.             $browser['hotjava3up'$browser['hotjava'&& $majorVersion >= 3;
  277.         }
  278.  
  279.         if ($detectFlags[NET_USERAGENT_DETECT_ALL|| 
  280.             ($detectFlags[NET_USERAGENT_DETECT_BROWSER&& $detectFlags[NET_USERAGENT_DETECT_FEATURES])) {
  281.             // Javascript Check
  282.             if ($browser['ns2'|| $browser['ie3']{
  283.                 Net_UserAgent_Detect::setFeature('javascript'1.0);
  284.             }
  285.             elseif ($browser['opera5up']{
  286.                 Net_UserAgent_Detect::setFeature('javascript'1.3);
  287.             }
  288.             elseif ($browser['opera'|| $browser['ns3']{
  289.                 Net_UserAgent_Detect::setFeature('javascript'1.1);
  290.             }
  291.             elseif (($browser['ns4'&& ($version <= 4.05)) || $browser['ie4']{
  292.                 Net_UserAgent_Detect::setFeature('javascript'1.2);
  293.             }
  294.             elseif (($browser['ie5up'&& strpos($agt'mac'!== false|| $browser['konq']{
  295.                 Net_UserAgent_Detect::setFeature('javascript'1.4);
  296.             }
  297.             // I can't believe IE6 still has javascript 1.3, what a shitty browser
  298.             elseif (($browser['ns4'&& ($version > 4.05)) || $browser['ie5up'|| $browser['hotjava3up']{
  299.                 Net_UserAgent_Detect::setFeature('javascript'1.3);
  300.             }
  301.             elseif ($browser['ns6up'|| $browser['gecko'|| $browser['netgem']{
  302.                 Net_UserAgent_Detect::setFeature('javascript'1.5);
  303.             }
  304.         }
  305.         
  306.         /** OS Check **/
  307.         if ($detectFlags[NET_USERAGENT_DETECT_ALL|| $detectFlags[NET_USERAGENT_DETECT_OS]{
  308.             $os['win']   strpos($agt'win'!== false || strpos($agt'16bit'!== false;
  309.             $os['win95'strpos($agt'win95'!== false || strpos($agt'windows 95'!== false;
  310.             $os['win16'strpos($agt'win16'!== false || strpos($agt'16bit'!== false || strpos($agt'windows 3.1'!== false || strpos($agt'windows 16-bit'!== false;  
  311.             $os['win31'strpos($agt'windows 3.1'!== false || strpos($agt'win16'!== false || strpos($agt'windows 16-bit'!== false;
  312.             $os['winme'strpos($agt'win 9x 4.90'!== false;
  313.             $os['win2k'strpos($agt'windows nt 5.0'!== false;
  314.             $os['winxp'strpos($agt'windows nt 5.1'!== false;
  315.             $os['win2003'strpos($agt'windows nt 5.2'!== false;
  316.             $os['win98'strpos($agt'win98'!== false || strpos($agt'windows 98'!== false;
  317.             $os['win9x'$os['win95'|| $os['win98'];
  318.             $os['winnt'(strpos($agt'winnt'!== false || strpos($agt'windows nt'!== false&& strpos($agt'windows nt 5'=== false;
  319.             $os['win32'$os['win95'|| $os['winnt'|| $os['win98'|| $majorVersion >= 4 && strpos($agt'win32'!== false || strpos($agt'32bit'!== false;
  320.             $os['os2']   strpos($agt'os/2'!== false || strpos($agt'ibm-webexplorer'!== false;
  321.             $os['mac']   strpos($agt'mac'!== false;
  322.             $os['mac68k']   $os['mac'&& (strpos($agt'68k'!== false || strpos($agt'68000'!== false);
  323.             $os['macppc']   $os['mac'&& (strpos($agt'ppc'!== false || strpos($agt'powerpc'!== false);
  324.             $os['sun']      strpos($agt'sunos'!== false;
  325.             $os['sun4']     strpos($agt'sunos 4'!== false;
  326.             $os['sun5']     strpos($agt'sunos 5'!== false;
  327.             $os['suni86']   $os['sun'&& strpos($agt'i86'!== false;
  328.             $os['irix']     strpos($agt'irix'!== false;
  329.             $os['irix5']    strpos($agt'irix 5'!== false;
  330.             $os['irix6']    strpos($agt'irix 6'!== false || strpos($agt'irix6'!== false;
  331.             $os['hpux']     strpos($agt'hp-ux'!== false;
  332.             $os['hpux9']    $os['hpux'&& strpos($agt'09.'!== false;
  333.             $os['hpux10']   $os['hpux'&& strpos($agt'10.'!== false;
  334.             $os['aix']      strpos($agt'aix'!== false;
  335.             $os['aix1']     strpos($agt'aix 1'!== false;
  336.             $os['aix2']     strpos($agt'aix 2'!== false;
  337.             $os['aix3']     strpos($agt'aix 3'!== false;
  338.             $os['aix4']     strpos($agt'aix 4'!== false;
  339.             $os['linux']    strpos($agt'inux'!== false;
  340.             $os['sco']      strpos($agt'sco'!== false || strpos($agt'unix_sv'!== false;
  341.             $os['unixware'strpos($agt'unix_system_v'!== false; 
  342.             $os['mpras']    strpos($agt'ncr'!== false; 
  343.             $os['reliant']  strpos($agt'reliant'!== false;
  344.             $os['dec']      strpos($agt'dec'!== false || strpos($agt'osf1'!== false || strpos($agt'dec_alpha'!== false || strpos($agt'alphaserver'!== false || strpos($agt'ultrix'!== false || strpos($agt'alphastation'!== false;
  345.             $os['sinix']    strpos($agt'sinix'!== false;
  346.             $os['freebsd']  strpos($agt'freebsd'!== false;
  347.             $os['bsd']      strpos($agt'bsd'!== false;
  348.             $os['unix']     strpos($agt'x11'!== false || strpos($agt'unix'!== false || $os['sun'|| $os['irix'|| $os['hpux'|| $os['sco'|| $os['unixware'|| $os['mpras'|| $os['reliant'|| $os['dec'|| $os['sinix'|| $os['aix'|| $os['linux'|| $os['bsd'|| $os['freebsd'];
  349.             $os['vms']      strpos($agt'vax'!== false || strpos($agt'openvms'!== false;
  350.         }
  351.  
  352.         // Setup the quirks
  353.         if ($detectFlags[NET_USERAGENT_DETECT_ALL|| 
  354.             ($detectFlags[NET_USERAGENT_DETECT_BROWSER&& $detectFlags[NET_USERAGENT_DETECT_QUIRKS])) {
  355.             if ($browser['konq']{
  356.                 Net_UserAgent_Detect::setQuirk('empty_file_input_value');
  357.             }
  358.  
  359.             if ($browser['ie']{
  360.                 Net_UserAgent_Detect::setQuirk('cache_ssl_downloads');
  361.             }
  362.  
  363.             if ($browser['ie6']{
  364.                 Net_UserAgent_Detect::setQuirk('scrollbar_in_way');
  365.             }
  366.  
  367.             if ($browser['ie5']{
  368.                 Net_UserAgent_Detect::setQuirk('break_disposition_header');
  369.             }
  370.  
  371.             if ($browser['ns6']{
  372.                 Net_UserAgent_Detect::setQuirk('popups_disabled');
  373.                 Net_UserAgent_Detect::setQuirk('must_cache_forms');
  374.             }
  375.             
  376.             if ($browser['nav'&& $subVersion < .79{
  377.                 Net_UserAgent_Detect::setQuirk('nested_table_render_bug');
  378.             }
  379.         }
  380.             
  381.         // Set features
  382.         if ($detectFlags[NET_USERAGENT_DETECT_ALL|| 
  383.             ($detectFlags[NET_USERAGENT_DETECT_BROWSER&& $detectFlags[NET_USERAGENT_DETECT_FEATURES])) {
  384.             if ($browser['gecko']{
  385.                 preg_match(';gecko/([\d]+)\b;i'$agt$matches);
  386.                 Net_UserAgent_Detect::setFeature('gecko'$matches[1]);
  387.             }
  388.  
  389.             if ($browser['ns6up'|| $browser['opera5up'|| $browser['konq'|| $browser['netgem']{
  390.                 Net_UserAgent_Detect::setFeature('dom');
  391.             }
  392.  
  393.             if ($browser['ie4up'|| $browser['ns4up'|| $browser['opera5up'|| $browser['konq'|| $browser['netgem']{
  394.                 Net_UserAgent_Detect::setFeature('dhtml');
  395.             }
  396.         }
  397.  
  398.         if ($detectFlags[NET_USERAGENT_DETECT_ALL|| $detectFlags[NET_USERAGENT_DETECT_ACCEPT]{
  399.             $mimetypes preg_split(';[\s,]+;'substr(getenv('HTTP_ACCEPT')0strpos(getenv('HTTP_ACCEPT'';'';'))-1PREG_SPLIT_NO_EMPTY);
  400.             Net_UserAgent_Detect::setAcceptType((array) $mimetypes'mimetype');
  401.  
  402.             $languages preg_split(';[\s,]+;'substr(getenv('HTTP_ACCEPT_LANGUAGE')0strpos(getenv('HTTP_ACCEPT_LANGUAGE'';'';'))-1PREG_SPLIT_NO_EMPTY);
  403.             if (empty($languages)) {
  404.                 $languages 'en';
  405.             }
  406.  
  407.             Net_UserAgent_Detect::setAcceptType((array) $languages'language');
  408.  
  409.             $encodings preg_split(';[\s,]+;'substr(getenv('HTTP_ACCEPT_ENCODING')0strpos(getenv('HTTP_ACCEPT_ENCODING'';'';'))-1PREG_SPLIT_NO_EMPTY);
  410.             Net_UserAgent_Detect::setAcceptType((array) $encodings'encoding');
  411.             
  412.             $charsets preg_split(';[\s,]+;'substr(getenv('HTTP_ACCEPT_CHARSET')0strpos(getenv('HTTP_ACCEPT_CHARSET'';'';'))-1PREG_SPLIT_NO_EMPTY);
  413.             Net_UserAgent_Detect::setAcceptType((array) $charsets'charset');
  414.         }
  415.     }
  416.     
  417.     // }}}
  418.     // {{{ setOption()
  419.  
  420.     /**
  421.      * Sets a class option.  The available settings are:
  422.      * o 'userAgent' => The user agent string to detect (useful for
  423.      * checking a string manually).
  424.      * o 'detectOptions' => The level of checking to do.  A single level
  425.      * or an array of options.  Default is NET_USERAGENT_DETECT_ALL.
  426.      *
  427.      * @param string $in_field The option field (userAgent or detectOptions)
  428.      * @param mixed $in_value The value for the field
  429.      */
  430.     function setOption($in_field$in_value)
  431.     {
  432.         $options &Net_UserAgent_Detect::_getStaticProperty('options');
  433.         $options[$in_field$in_value;
  434.     }
  435.  
  436.     // }}}
  437.     // {{{ isBrowser()
  438.  
  439.     /**
  440.      * Look up the provide browser flag and return a boolean value
  441.      *
  442.      * Given one of the flags listed in the properties, this function will return
  443.      * the value associated with that flag.
  444.      *
  445.      * @param  string $in_match flag to lookup
  446.      *
  447.      * @access public
  448.      * @return boolean whether or not the browser satisfies this flag
  449.      */
  450.     function isBrowser($in_match)
  451.     {
  452.         Net_UserAgent_Detect::detect();
  453.         $browser &Net_UserAgent_Detect::_getStaticProperty('browser');
  454.         return isset($browser[strtolower($in_match)]$browser[strtolower($in_match): false;
  455.     }
  456.  
  457.     // }}}
  458.     // {{{ getBrowser()
  459.  
  460.     /**
  461.      * Since simply returning the "browser" is somewhat ambiguous since there
  462.      * are different ways to classify the browser, this function works by taking
  463.      * an expect list and returning the string of the first match, so put the important
  464.      * ones first in the array.
  465.      *
  466.      * @param  array $in_expectList the browser flags to search for
  467.      *
  468.      * @access public
  469.      * @return string first flag that matches
  470.      */
  471.     function getBrowser($in_expectList)
  472.     {
  473.         Net_UserAgent_Detect::detect();
  474.         $browser &Net_UserAgent_Detect::_getStaticProperty('browser');
  475.         foreach((array) $in_expectList as $brwsr{
  476.             if (!empty($browser[strtolower($brwsr)])) {
  477.                 return $brwsr;
  478.             }
  479.         }
  480.     }
  481.  
  482.     // }}}
  483.     // {{{ getBrowserString()
  484.  
  485.     /**
  486.      * This function returns the vendor string corresponding to the flag.
  487.      *
  488.      * Either use the default matches or pass in an associative array of
  489.      * flags and corresponding vendor strings.  This function will find
  490.      * the highest version flag and return the vendor string corresponding
  491.      * to the appropriate flag.  Be sure to pass in the flags in ascending order
  492.      * if you want a basic matches first, followed by more detailed matches.
  493.      *
  494.      * @param  array $in_vendorStrings (optional) array of flags matched with vendor strings
  495.      *
  496.      * @access public
  497.      * @return string vendor string matches appropriate flag
  498.      */
  499.     function getBrowserString($in_vendorStrings = array (
  500.         'ie'       => 'Microsoft Internet Explorer',
  501.         'ie4up'    => 'Microsoft Internet Explorer 4.x',
  502.         'ie5up'    => 'Microsoft Internet Explorer 5.x',
  503.         'ie6up'    => 'Microsoft Internet Explorer 6.x',
  504.         'opera4'   => 'Opera 4.x',
  505.         'opera5up' => 'Opera 5.x',
  506.         'nav'      => 'Netscape Navigator',
  507.         'ns4'      => 'Netscape 4.x',
  508.         'ns6up'    => 'Mozilla/Netscape 6.x',
  509.         'firefox0.x' => 'Firefox 0.x',
  510.         'firefox1.x' => 'Firefox 1.x',
  511.         'konq'     => 'Konqueror/Safari',
  512.         'netgem'   => 'Netgem/iPlayer',
  513.     ))
  514.     {
  515.         Net_UserAgent_Detect::detect();
  516.         $browser &Net_UserAgent_Detect::_getStaticProperty('browser');
  517.         foreach((array) $in_vendorStrings as $flag => $string{
  518.             if (!empty($browser[$flag])) {
  519.                 $vendorString $string;
  520.             }
  521.         }
  522.  
  523.         // if there are no matches just use the user agent leading idendifier (usually Mozilla)
  524.         if (!isset($vendorString)) {
  525.             $leadingIdentifier &Net_UserAgent_Detect::_getStaticProperty('leadingIdentifier');
  526.             $vendorString $leadingIdentifier;
  527.         }
  528.         
  529.         return $vendorString;
  530.     }
  531.  
  532.     // }}}
  533.     // {{{ isIE()
  534.  
  535.     /**
  536.      * Determine if the browser is an Internet Explorer browser
  537.      *
  538.      * @access public
  539.      * @return bool whether or not this browser is an ie browser
  540.      */
  541.     function isIE()
  542.     {
  543.         Net_UserAgent_Detect::detect();
  544.         $browser &Net_UserAgent_Detect::_getStaticProperty('browser');
  545.         return !empty($browser['ie']);
  546.     }
  547.  
  548.     // }}}
  549.     // {{{ isNavigator()
  550.  
  551.     /**
  552.      * Determine if the browser is a Netscape Navigator browser
  553.      *
  554.      * @access public
  555.      * @return bool whether or not this browser is a Netscape Navigator browser
  556.      */
  557.     function isNavigator()
  558.     {
  559.         Net_UserAgent_Detect::detect();
  560.         $browser &Net_UserAgent_Detect::_getStaticProperty('browser');
  561.         return !empty($browser['nav']);
  562.     }
  563.  
  564.     // }}}
  565.     // {{{ isNetscape()
  566.  
  567.     /**
  568.      * Determine if the browser is a Netscape or Mozilla browser
  569.      *
  570.      * Note that this function is not the same as isNavigator, since the
  571.      * new Mozilla browsers are still sponsered by Netscape, and hence are
  572.      * Netscape products, but not the original Navigators
  573.      *
  574.      * @access public
  575.      * @return bool whether or not this browser is a Netscape product
  576.      */
  577.     function isNetscape()
  578.     {
  579.         Net_UserAgent_Detect::detect();
  580.         $browser &Net_UserAgent_Detect::_getStaticProperty('browser');
  581.         return !empty($browser['ns4up']);
  582.     }
  583.     
  584.     // }}}
  585.     // {{{ isOS()
  586.  
  587.     /**
  588.      * Look up the provide OS flag and return a boolean value
  589.      *
  590.      * Given one of the flags listed in the properties, this function will return
  591.      * the value associated with that flag for the operating system.
  592.      *
  593.      * @param  string $in_match flag to lookup
  594.      *
  595.      * @access public
  596.      * @return boolean whether or not the OS satisfies this flag
  597.      */
  598.     function isOS($in_match)
  599.     {
  600.         Net_UserAgent_Detect::detect();
  601.         $os &Net_UserAgent_Detect::_getStaticProperty('os');
  602.         return isset($os[strtolower($in_match)]$os[strtolower($in_match): false;
  603.     }
  604.  
  605.     // }}}
  606.     // {{{ getOS()
  607.  
  608.     /**
  609.      * Since simply returning the "os" is somewhat ambiguous since there
  610.      * are different ways to classify the browser, this function works by taking
  611.      * an expect list and returning the string of the first match, so put the important
  612.      * ones first in the array.
  613.      *
  614.      * @access public
  615.      * @return string first flag that matches
  616.      */
  617.     function getOS($in_expectList)
  618.     {
  619.         Net_UserAgent_Detect::detect();
  620.         $os &Net_UserAgent_Detect::_getStaticProperty('os');
  621.         foreach((array) $in_expectList as $expectOs{
  622.             if (!empty($os[strtolower($expectOs)])) {
  623.                 return $expectOs;
  624.             }
  625.         }
  626.     }
  627.  
  628.     // }}}
  629.     // {{{ getOSString()
  630.  
  631.     /**
  632.      * This function returns the os string corresponding to the flag.
  633.      *
  634.      * Either use the default matches or pass in an associative array of
  635.      * flags and corresponding os strings.  This function will find
  636.      * the highest version flag and return the os string corresponding
  637.      * to the appropriate flag.  Be sure to pass in the flags in ascending order
  638.      * if you want a basic matches first, followed by more detailed matches.
  639.      *
  640.      * @param  array $in_osStrings (optional) array of flags matched with os strings
  641.      *
  642.      * @access public
  643.      * @return string os string matches appropriate flag
  644.      */
  645.     function getOSString($in_osStrings = array(
  646.        'win'   => 'Microsoft Windows',
  647.        'win9x' => 'Microsoft Windows 9x',
  648.        'winme' => 'Microsoft Windows Millenium',
  649.        'win2k' => 'Microsoft Windows 2000',
  650.        'winnt' => 'Microsoft Windows NT',
  651.        'winxp' => 'Microsoft Windows XP',
  652.        'win2003' => 'Microsoft Windows 2003',
  653.        'mac'   => 'Macintosh',
  654.        'unix'  => 'Linux/Unix',
  655.     ))
  656.     {
  657.         Net_UserAgent_Detect::detect();
  658.         $osString 'Unknown';
  659.  
  660.         $os &Net_UserAgent_Detect::_getStaticProperty('os');
  661.         foreach((array) $in_osStrings as $flag => $string{
  662.             if (!empty($os[$flag])) {
  663.                 $osString $string;
  664.             }
  665.         }
  666.  
  667.         return $osString;
  668.     }
  669.  
  670.     // }}}
  671.     // {{{ setQuirk()
  672.  
  673.     /**
  674.      * Set a unique behavior for the current browser.
  675.      *
  676.      * Many client browsers do some really funky things, and this
  677.      * mechanism allows the coder to determine if an excepetion must
  678.      * be made with the current client.
  679.      *
  680.      * @param string $in_quirk The quirk to set
  681.      * @param string $in_hasQuirk (optional) Does the browser have the quirk?
  682.      *
  683.      * @access public
  684.      * @return void 
  685.      */
  686.     function setQuirk($in_quirk$in_hasQuirk = true)
  687.     {
  688.         $quirks &Net_UserAgent_Detect::_getStaticProperty('quirks');
  689.         $hasQuirk !empty($in_hasQuirk)
  690.         $quirks[strtolower($in_quirk)$hasQuirk;
  691.     }
  692.  
  693.     // }}}
  694.     // {{{ hasQuirk()
  695.  
  696.     /**
  697.      * Check a unique behavior for the current browser.
  698.      *
  699.      * Many client browsers do some really funky things, and this
  700.      * mechanism allows the coder to determine if an excepetion must
  701.      * be made with the current client.
  702.      *
  703.      * @param string $in_quirk The quirk to detect
  704.      *
  705.      * @access public
  706.      * @return bool whether or not browser has this quirk
  707.      */
  708.     function hasQuirk($in_quirk)
  709.     {
  710.         return (bool) Net_UserAgent_Detect::getQuirk($in_quirk);
  711.     }
  712.     
  713.     // }}}
  714.     // {{{ getQuirk()
  715.  
  716.     /**
  717.      * Get the unique behavior for the current browser.
  718.      *
  719.      * Many client browsers do some really funky things, and this
  720.      * mechanism allows the coder to determine if an excepetion must
  721.      * be made with the current client.
  722.      *
  723.      * @param string $in_quirk The quirk to detect
  724.      *
  725.      * @access public
  726.      * @return string value of the quirk, in this case usually a boolean
  727.      */
  728.     function getQuirk($in_quirk)
  729.     {
  730.         Net_UserAgent_Detect::detect();
  731.         $quirks &Net_UserAgent_Detect::_getStaticProperty('quirks');
  732.         return isset($quirks[strtolower($in_quirk)]$quirks[strtolower($in_quirk): null; 
  733.     }
  734.  
  735.     // }}}
  736.     // {{{ setFeature()
  737.  
  738.     /**
  739.      * Set capabilities for the current browser.
  740.      *
  741.      * Since the capabilities of client browsers vary widly, this interface
  742.      * helps keep track of the core features of a client, such as if the client
  743.      * supports dhtml, dom, javascript, etc.
  744.      *
  745.      * @param string $in_feature The feature to set
  746.      * @param string $in_hasFeature (optional) Does the browser have the feature?
  747.      *
  748.      * @access public
  749.      * @return void 
  750.      */
  751.     function setFeature($in_feature$in_hasFeature = true)
  752.     {
  753.         $features &Net_UserAgent_Detect::_getStaticProperty('features');
  754.         $features[strtolower($in_feature)$in_hasFeature;
  755.     }
  756.  
  757.     // }}}
  758.     // {{{ hasFeature()
  759.  
  760.     /**
  761.      * Check the capabilities for the current browser.
  762.      *
  763.      * Since the capabilities of client browsers vary widly, this interface
  764.      * helps keep track of the core features of a client, such as if the client
  765.      * supports dhtml, dom, javascript, etc.
  766.      *
  767.      * @param string $in_feature The feature to detect
  768.      *
  769.      * @access public
  770.      * @return bool whether or not the current client has this feature
  771.      */
  772.     function hasFeature($in_feature)
  773.     {
  774.         return (bool) Net_UserAgent_Detect::getFeature($in_feature);
  775.     }
  776.     
  777.     // }}}
  778.     // {{{ getFeature()
  779.  
  780.     /**
  781.      * Get the capabilities for the current browser.
  782.      *
  783.      * Since the capabilities of client browsers vary widly, this interface
  784.      * helps keep track of the core features of a client, such as if the client
  785.      * supports dhtml, dom, javascript, etc.
  786.      *
  787.      * @param string $in_feature The feature to detect
  788.      *
  789.      * @access public
  790.      * @return string value of the feature requested
  791.      */
  792.     function getFeature($in_feature)
  793.     {
  794.         Net_UserAgent_Detect::detect();
  795.         $features &Net_UserAgent_Detect::_getStaticProperty('features');
  796.         return isset($features[strtolower($in_feature)]$features[strtolower($in_feature): null; 
  797.     }
  798.  
  799.     // }}}
  800.     // {{{ getAcceptType()
  801.  
  802.     /**
  803.      * Retrive the accept type for the current browser.
  804.      *
  805.      * To keep track of the mime-types, languages, charsets and encodings
  806.      * that each browser accepts we use associative arrays for each type.
  807.      * This function works like getBrowser() as it takes an expect list
  808.      * and returns the first match.  For instance, to find the language
  809.      * you would pass in your allowed languages and see if any of the
  810.      * languages set in the browser match.
  811.      *
  812.      * @param  string $in_expectList values to check
  813.      * @param  string $in_type type of accept
  814.      *
  815.      * @access public
  816.      * @return string the first matched value
  817.      */
  818.     function getAcceptType($in_expectList$in_type)
  819.     {
  820.         Net_UserAgent_Detect::detect();
  821.         $type strtolower($in_type);
  822.  
  823.         if ($type == 'mimetype' || $type == 'language' || $type == 'charset' || $type == 'encoding'{
  824.             $typeArray &Net_UserAgent_Detect::_getStaticProperty($type);
  825.             foreach((array) $in_expectList as $match{
  826.                 if (!empty($typeArray[$match])) {
  827.                     return $match;
  828.                 }
  829.             }
  830.         }
  831.  
  832.         return null;
  833.     }
  834.  
  835.     // }}}
  836.     // {{{ setAcceptType()
  837.  
  838.     /**
  839.      * Set the accept types for the current browser.
  840.      *
  841.      * To keep track of the mime-types, languages, charsets and encodings
  842.      * that each browser accepts we use associative arrays for each type.
  843.      * This function takes and array of accepted values for the type and
  844.      * records them for retrieval.
  845.      *
  846.      * @param  array $in_values values of the accept type
  847.      * @param  string $in_type type of accept
  848.      *
  849.      * @access public
  850.      * @return void 
  851.      */
  852.     function setAcceptType($in_values$in_type)
  853.     {
  854.         $type strtolower($in_type);
  855.  
  856.         if ($type == 'mimetype' || $type == 'language' || $type == 'charset' || $type == 'encoding'{
  857.             $typeArray &Net_UserAgent_Detect::_getStaticProperty($type);
  858.             foreach((array) $in_values as $value{
  859.                 $typeArray[$value= true;
  860.             }
  861.         }
  862.     }
  863.  
  864.     // }}}
  865.     // {{{ hasAcceptType()
  866.  
  867.     /**
  868.      * Check the accept types for the current browser.
  869.      *
  870.      * To keep track of the mime-types, languages, charsets and encodings
  871.      * that each browser accepts we use associative arrays for each type.
  872.      * This function checks the array for the given type and determines if
  873.      * the browser accepts it.
  874.      *
  875.      * @param  string $in_value values to check
  876.      * @param  string $in_type type of accept
  877.      *
  878.      * @access public
  879.      * @return bool whether or not the value is accept for this type
  880.      */
  881.     function hasAcceptType($in_value$in_type)
  882.     {
  883.         return (bool) Net_UserAgent_Detect::getAcceptType((array) $in_value$in_type);
  884.     }
  885.  
  886.     // }}}
  887.     // {{{ getUserAgent()
  888.  
  889.     /**
  890.      * Return the user agent string that is being worked on
  891.      *
  892.      * @access public
  893.      * @return string user agent
  894.      */
  895.     function getUserAgent()
  896.     {
  897.         Net_UserAgent_Detect::detect();
  898.         $userAgent &Net_UserAgent_Detect::_getStaticProperty('userAgent');
  899.         return $userAgent;
  900.     }
  901.  
  902.     // }}}
  903.     // {{{ _getStaticProperty()
  904.  
  905.     /**
  906.      * Copy of getStaticProperty() from PEAR.php to avoid having to
  907.      * include PEAR.php
  908.      *
  909.      * @access private
  910.      * @param  string $var    The variable to retrieve.
  911.      * @return mixed   A reference to the variable. If not set it will be
  912.      *                  auto initialised to NULL.
  913.      */
  914.     function &_getStaticProperty($var)
  915.     {
  916.         static $properties;
  917.         return $properties[$var];
  918.     }
  919.  
  920.     // }}}
  921. }
  922. ?>

Documentation generated on Mon, 11 Mar 2019 14:21:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.