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

Source for file libdetectuseragent.php

Documentation is available at libdetectuseragent.php

  1. <?php
  2. /**
  3.  * User Agent detection class
  4.  * @author Christopher Troup <mini@chatetheory.com>
  5.  * @package phpUserTrack
  6.  * @subclass Libraries
  7.  */
  8. class detectUserAgent {
  9.  
  10.     /**
  11.      * User Agent identification string
  12.      * @var string
  13.      */
  14.     var $useragent;
  15.     
  16.     /**
  17.      * @todo What does this do?
  18.      */
  19.     var $browscapIni//Cache
  20.     
  21.     /**
  22.      * @todo What does this do?
  23.      */
  24.     var $browscapPath//Cached database
  25.     
  26.     /**
  27.      * All the information about the browser that could be detected
  28.      * @var array
  29.      */
  30.     var $dataArray;
  31.  
  32.     /**
  33.      * Class constructor
  34.      * @param string $useragent User Agent indentification string
  35.      */
  36.     function detectUserAgent($useragent null{
  37.         define('PHPUSERTRACK_BROWSCAP'dirname(__FILE__'/../include/browscap.ini');
  38.         if ($useragent == null{
  39.             $useragent $_SERVER['HTTP_USER_AGENT'];
  40.         }
  41.         $this->useragent = $useragent;
  42.         $this->browscapIni = null;
  43.         $this->browscapPath = '';
  44.         //if (!$array = $this->get_browser_php()) { // try native php function first
  45.             //$array = $this->get_browser_local(PHPUSERTRACK_BROWSCAP);
  46.             $array $this->get_browser_php();
  47.         //}
  48.         $this->dataArray = $array;
  49.     }
  50.     
  51.     /**
  52.      * Return the detected information
  53.      * @return array
  54.      */
  55.     function getBrowserArray({
  56.         return $this->dataArray;
  57.     }
  58.     
  59.     /**
  60.      * Attempt to use PHPs built in get_browser() function
  61.      * @return array detected information
  62.      */
  63.     function get_browser_php({
  64.         return get_browser(nulltrue);
  65.     }
  66.     
  67.     /**
  68.      * Drop-in replacement for get_browser()
  69.      * @param string $db
  70.      * @param boolean $cache
  71.      * @return array detected information
  72.      */
  73.     function get_browser_local($db$cache false {
  74.          if ((!isset($this->browscapIni))||(!$cache)||($this->browscapPath!==$db)) {
  75.               $this->browscapIni=parse_ini_file($db,true);
  76.             $this->browscapPath=$db;
  77.          }
  78.          $cap=null;
  79.          foreach ($this->browscapIni as $key=>$value{
  80.               if (($key!='*')&&(!array_key_exists('parent',$value))) continue;
  81.               $keyEreg='^'.str_replace(
  82.                array('\\','.','?','*','^','$','[',']','|','(',')','+','{','}','%'),
  83.                array('\\\\','\\.','.','.*','\\^','\\$','\\[','\\]','\\|','\\(','\\)','\\+','\\{','\\}','\\%')$key).'$';
  84.               if (preg_match('%'.$keyEreg.'%i',$this->useragent)) {
  85.                    $cap=array('browser_name_regex'=>strtolower($keyEreg),'browser_name_pattern'=>$key)+$value;
  86.                    $maxDeep=8;
  87.                    while (array_key_exists('parent',$value)&&(--$maxDeep>0))
  88.                 $cap+=($value=$this->browscapIni[$value['parent']]);
  89.                    break;
  90.               }
  91.          }
  92.          if (!$cache$this->browscapIni=null;
  93.          return $cap;
  94.     }
  95. }
  96.  
  97. ?>

Documentation generated on Tue, 06 Nov 2007 09:22:01 -0800 by phpDocumentor 1.4.0a2