User_agent.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * User Agent Class
00020  *
00021  * Identifies the platform, browser, robot, or mobile devise of the browsing agent
00022  *
00023  * @package             CodeIgniter
00024  * @subpackage  Libraries
00025  * @category    User Agent
00026  * @author              ExpressionEngine Dev Team
00027  * @link                http://codeigniter.com/user_guide/libraries/user_agent.html
00028  */
00029 class CI_User_agent {
00030 
00031         var $agent              = NULL;
00032         
00033         var $is_browser = FALSE;
00034         var $is_robot   = FALSE;
00035         var $is_mobile  = FALSE;
00036 
00037         var $languages  = array();
00038         var $charsets   = array();
00039         
00040         var $platforms  = array();
00041         var $browsers   = array();
00042         var $mobiles    = array();
00043         var $robots             = array();
00044         
00045         var $platform   = '';
00046         var $browser    = '';
00047         var $version    = '';
00048         var $mobile             = '';
00049         var $robot              = '';
00050         
00051         /**
00052          * Constructor
00053          *
00054          * Sets the User Agent and runs the compilation routine
00055          *
00056          * @access      public
00057          * @return      void
00058          */             
00059         function CI_User_agent()
00060         {
00061                 if (isset($_SERVER['HTTP_USER_AGENT']))
00062                 {
00063                         $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
00064                 }
00065                 
00066                 if ( ! is_null($this->agent))
00067                 {
00068                         if ($this->_load_agent_file())
00069                         {
00070                                 $this->_compile_data();
00071                         }
00072                 }
00073                 
00074                 log_message('debug', "User Agent Class Initialized");
00075         }
00076         
00077         // --------------------------------------------------------------------
00078         
00079         /**
00080          * Compile the User Agent Data
00081          *
00082          * @access      private
00083          * @return      bool
00084          */             
00085         function _load_agent_file()
00086         {
00087                 if ( ! @include(APPPATH.'config/user_agents'.EXT))
00088                 {
00089                         return FALSE;
00090                 }
00091                 
00092                 $return = FALSE;
00093                 
00094                 if (isset($platforms))
00095                 {
00096                         $this->platforms = $platforms;
00097                         unset($platforms);
00098                         $return = TRUE;
00099                 }
00100 
00101                 if (isset($browsers))
00102                 {
00103                         $this->browsers = $browsers;
00104                         unset($browsers);
00105                         $return = TRUE;
00106                 }
00107 
00108                 if (isset($mobiles))
00109                 {
00110                         $this->mobiles = $mobiles;
00111                         unset($mobiles);
00112                         $return = TRUE;
00113                 }
00114                 
00115                 if (isset($robots))
00116                 {
00117                         $this->robots = $robots;
00118                         unset($robots);
00119                         $return = TRUE;
00120                 }
00121 
00122                 return $return;
00123         }
00124         
00125         // --------------------------------------------------------------------
00126         
00127         /**
00128          * Compile the User Agent Data
00129          *
00130          * @access      private
00131          * @return      bool
00132          */             
00133         function _compile_data()
00134         {
00135                 $this->_set_platform();
00136         
00137                 foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function)
00138                 {
00139                         if ($this->$function() === TRUE)
00140                         {
00141                                 break;
00142                         }
00143                 }       
00144         }
00145         
00146         // --------------------------------------------------------------------
00147         
00148         /**
00149          * Set the Platform
00150          *
00151          * @access      private
00152          * @return      mixed
00153          */             
00154         function _set_platform()
00155         {
00156                 if (is_array($this->platforms) AND count($this->platforms) > 0)
00157                 {
00158                         foreach ($this->platforms as $key => $val)
00159                         {
00160                                 if (preg_match("|".preg_quote($key)."|i", $this->agent))
00161                                 {
00162                                         $this->platform = $val;
00163                                         return TRUE;
00164                                 }
00165                         }
00166                 }
00167                 $this->platform = 'Unknown Platform';
00168         }
00169 
00170         // --------------------------------------------------------------------
00171         
00172         /**
00173          * Set the Browser
00174          *
00175          * @access      private
00176          * @return      bool
00177          */             
00178         function _set_browser()
00179         {
00180                 if (is_array($this->browsers) AND count($this->browsers) > 0)
00181                 {
00182                         foreach ($this->browsers as $key => $val)
00183                         {               
00184                                 if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
00185                                 {
00186                                         $this->is_browser = TRUE;
00187                                         $this->version = $match[1];
00188                                         $this->browser = $val;
00189                                         $this->_set_mobile();
00190                                         return TRUE;
00191                                 }
00192                         }
00193                 }
00194                 return FALSE;
00195         }
00196                         
00197         // --------------------------------------------------------------------
00198         
00199         /**
00200          * Set the Robot
00201          *
00202          * @access      private
00203          * @return      bool
00204          */             
00205         function _set_robot()
00206         {
00207                 if (is_array($this->robots) AND count($this->robots) > 0)
00208                 {               
00209                         foreach ($this->robots as $key => $val)
00210                         {
00211                                 if (preg_match("|".preg_quote($key)."|i", $this->agent))
00212                                 {
00213                                         $this->is_robot = TRUE;
00214                                         $this->robot = $val;
00215                                         return TRUE;
00216                                 }
00217                         }
00218                 }
00219                 return FALSE;
00220         }
00221 
00222         // --------------------------------------------------------------------
00223         
00224         /**
00225          * Set the Mobile Device
00226          *
00227          * @access      private
00228          * @return      bool
00229          */             
00230         function _set_mobile()
00231         {
00232                 if (is_array($this->mobiles) AND count($this->mobiles) > 0)
00233                 {               
00234                         foreach ($this->mobiles as $key => $val)
00235                         {
00236                                 if (FALSE !== (strpos(strtolower($this->agent), $key)))
00237                                 {
00238                                         $this->is_mobile = TRUE;
00239                                         $this->mobile = $val;
00240                                         return TRUE;
00241                                 }
00242                         }
00243                 }       
00244                 return FALSE;
00245         }
00246         
00247         // --------------------------------------------------------------------
00248         
00249         /**
00250          * Set the accepted languages
00251          *
00252          * @access      private
00253          * @return      void
00254          */                     
00255         function _set_languages()
00256         {
00257                 if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
00258                 {
00259                         $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
00260                         
00261                         $this->languages = explode(',', $languages);
00262                 }
00263                 
00264                 if (count($this->languages) == 0)
00265                 {
00266                         $this->languages = array('Undefined');
00267                 }       
00268         }
00269         
00270         // --------------------------------------------------------------------
00271         
00272         /**
00273          * Set the accepted character sets
00274          *
00275          * @access      private
00276          * @return      void
00277          */                     
00278         function _set_charsets()
00279         {       
00280                 if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
00281                 {
00282                         $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
00283                         
00284                         $this->charsets = explode(',', $charsets);
00285                 }
00286                 
00287                 if (count($this->charsets) == 0)
00288                 {
00289                         $this->charsets = array('Undefined');
00290                 }       
00291         }
00292 
00293         // --------------------------------------------------------------------
00294         
00295         /**
00296          * Is Browser
00297          *
00298          * @access      public
00299          * @return      bool
00300          */             
00301         function is_browser()
00302         {
00303                 return $this->is_browser;
00304         }
00305 
00306         // --------------------------------------------------------------------
00307         
00308         /**
00309          * Is Robot
00310          *
00311          * @access      public
00312          * @return      bool
00313          */             
00314         function is_robot()
00315         {
00316                 return $this->is_robot;
00317         }
00318 
00319         // --------------------------------------------------------------------
00320         
00321         /**
00322          * Is Mobile
00323          *
00324          * @access      public
00325          * @return      bool
00326          */             
00327         function is_mobile()
00328         {
00329                 return $this->is_mobile;
00330         }       
00331 
00332         // --------------------------------------------------------------------
00333         
00334         /**
00335          * Is this a referral from another site?
00336          *
00337          * @access      public
00338          * @return      bool
00339          */                     
00340         function is_referral()
00341         {
00342                 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
00343         }
00344 
00345         // --------------------------------------------------------------------
00346         
00347         /**
00348          * Agent String
00349          *
00350          * @access      public
00351          * @return      string
00352          */                     
00353         function agent_string()
00354         {
00355                 return $this->agent;
00356         }
00357 
00358         // --------------------------------------------------------------------
00359         
00360         /**
00361          * Get Platform
00362          *
00363          * @access      public
00364          * @return      string
00365          */                     
00366         function platform()
00367         {
00368                 return $this->platform;
00369         }
00370 
00371         // --------------------------------------------------------------------
00372         
00373         /**
00374          * Get Browser Name
00375          *
00376          * @access      public
00377          * @return      string
00378          */                     
00379         function browser()
00380         {
00381                 return $this->browser;
00382         }
00383 
00384         // --------------------------------------------------------------------
00385         
00386         /**
00387          * Get the Browser Version
00388          *
00389          * @access      public
00390          * @return      string
00391          */                     
00392         function version()
00393         {
00394                 return $this->version;
00395         }
00396 
00397         // --------------------------------------------------------------------
00398         
00399         /**
00400          * Get The Robot Name
00401          *
00402          * @access      public
00403          * @return      string
00404          */                             
00405         function robot()
00406         {
00407                 return $this->robot;
00408         }
00409         // --------------------------------------------------------------------
00410         
00411         /**
00412          * Get the Mobile Device
00413          *
00414          * @access      public
00415          * @return      string
00416          */                     
00417         function mobile()
00418         {
00419                 return $this->mobile;
00420         }
00421         
00422         // --------------------------------------------------------------------
00423         
00424         /**
00425          * Get the referrer
00426          *
00427          * @access      public
00428          * @return      bool
00429          */                     
00430         function referrer()
00431         {
00432                 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
00433         }
00434 
00435         // --------------------------------------------------------------------
00436         
00437         /**
00438          * Get the accepted languages
00439          *
00440          * @access      public
00441          * @return      array
00442          */                     
00443         function languages()
00444         {
00445                 if (count($this->languages) == 0)
00446                 {
00447                         $this->_set_languages();
00448                 }
00449         
00450                 return $this->languages;
00451         }
00452 
00453         // --------------------------------------------------------------------
00454         
00455         /**
00456          * Get the accepted Character Sets
00457          *
00458          * @access      public
00459          * @return      array
00460          */                     
00461         function charsets()
00462         {
00463                 if (count($this->charsets) == 0)
00464                 {
00465                         $this->_set_charsets();
00466                 }
00467         
00468                 return $this->charsets;
00469         }
00470         
00471         // --------------------------------------------------------------------
00472         
00473         /**
00474          * Test for a particular language
00475          *
00476          * @access      public
00477          * @return      bool
00478          */                     
00479         function accept_lang($lang = 'en')
00480         {
00481                 return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
00482         }
00483         
00484         // --------------------------------------------------------------------
00485         
00486         /**
00487          * Test for a particular character set
00488          *
00489          * @access      public
00490          * @return      bool
00491          */                     
00492         function accept_charset($charset = 'utf-8')
00493         {
00494                 return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
00495         }
00496         
00497         
00498 }
00499 
00500 
00501 /* End of file User_agent.php */
00502 /* Location: ./system/libraries/User_agent.php */