[ Index ]
 

Code source de PHP PEAR 1.4.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Net/UserAgent/ -> Detect.php (source)

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


Généré le : Sun Feb 25 14:08:00 2007 par Balluche grâce à PHPXref 0.7