123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * CodeIgniter
- *
- * An open source application development framework for PHP 5.1.6 or newer
- *
- * @package CodeIgniter
- * @author EllisLab Dev Team
- * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
- * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
- * @license http://codeigniter.com/user_guide/license.html
- * @link http://codeigniter.com
- * @since Version 1.0
- * @filesource
- */
- // ------------------------------------------------------------------------
- /**
- * User Agent Class
- *
- * Identifies the platform, browser, robot, or mobile devise of the browsing agent
- *
- * @package CodeIgniter
- * @subpackage Libraries
- * @category User Agent
- * @author EllisLab Dev Team
- * @link http://codeigniter.com/user_guide/libraries/user_agent.html
- */
- class CI_User_agent {
- var $agent = NULL;
- var $is_browser = FALSE;
- var $is_robot = FALSE;
- var $is_mobile = FALSE;
- var $languages = array();
- var $charsets = array();
- var $platforms = array();
- var $browsers = array();
- var $mobiles = array();
- var $robots = array();
- var $platform = '';
- var $browser = '';
- var $version = '';
- var $mobile = '';
- var $robot = '';
- /**
- * Constructor
- *
- * Sets the User Agent and runs the compilation routine
- *
- * @access public
- * @return void
- */
- public function __construct()
- {
- if (isset($_SERVER['HTTP_USER_AGENT']))
- {
- $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
- }
- if ( ! is_null($this->agent))
- {
- if ($this->_load_agent_file())
- {
- $this->_compile_data();
- }
- }
- log_message('debug', "User Agent Class Initialized");
- }
- // --------------------------------------------------------------------
- /**
- * Compile the User Agent Data
- *
- * @access private
- * @return bool
- */
- private function _load_agent_file()
- {
- if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'))
- {
- include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php');
- }
- elseif (is_file(APPPATH.'config/user_agents.php'))
- {
- include(APPPATH.'config/user_agents.php');
- }
- else
- {
- return FALSE;
- }
- $return = FALSE;
- if (isset($platforms))
- {
- $this->platforms = $platforms;
- unset($platforms);
- $return = TRUE;
- }
- if (isset($browsers))
- {
- $this->browsers = $browsers;
- unset($browsers);
- $return = TRUE;
- }
- if (isset($mobiles))
- {
- $this->mobiles = $mobiles;
- unset($mobiles);
- $return = TRUE;
- }
- if (isset($robots))
- {
- $this->robots = $robots;
- unset($robots);
- $return = TRUE;
- }
- return $return;
- }
- // --------------------------------------------------------------------
- /**
- * Compile the User Agent Data
- *
- * @access private
- * @return bool
- */
- private function _compile_data()
- {
- $this->_set_platform();
- foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function)
- {
- if ($this->$function() === TRUE)
- {
- break;
- }
- }
- }
- // --------------------------------------------------------------------
- /**
- * Set the Platform
- *
- * @access private
- * @return mixed
- */
- private function _set_platform()
- {
- if (is_array($this->platforms) AND count($this->platforms) > 0)
- {
- foreach ($this->platforms as $key => $val)
- {
- if (preg_match("|".preg_quote($key)."|i", $this->agent))
- {
- $this->platform = $val;
- return TRUE;
- }
- }
- }
- $this->platform = 'Unknown Platform';
- }
- // --------------------------------------------------------------------
- /**
- * Set the Browser
- *
- * @access private
- * @return bool
- */
- private function _set_browser()
- {
- if (is_array($this->browsers) AND count($this->browsers) > 0)
- {
- foreach ($this->browsers as $key => $val)
- {
- if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
- {
- $this->is_browser = TRUE;
- $this->version = $match[1];
- $this->browser = $val;
- $this->_set_mobile();
- return TRUE;
- }
- }
- }
- return FALSE;
- }
- // --------------------------------------------------------------------
- /**
- * Set the Robot
- *
- * @access private
- * @return bool
- */
- private function _set_robot()
- {
- if (is_array($this->robots) AND count($this->robots) > 0)
- {
- foreach ($this->robots as $key => $val)
- {
- if (preg_match("|".preg_quote($key)."|i", $this->agent))
- {
- $this->is_robot = TRUE;
- $this->robot = $val;
- return TRUE;
- }
- }
- }
- return FALSE;
- }
- // --------------------------------------------------------------------
- /**
- * Set the Mobile Device
- *
- * @access private
- * @return bool
- */
- private function _set_mobile()
- {
- if (is_array($this->mobiles) AND count($this->mobiles) > 0)
- {
- foreach ($this->mobiles as $key => $val)
- {
- if (FALSE !== (strpos(strtolower($this->agent), $key)))
- {
- $this->is_mobile = TRUE;
- $this->mobile = $val;
- return TRUE;
- }
- }
- }
- return FALSE;
- }
- // --------------------------------------------------------------------
- /**
- * Set the accepted languages
- *
- * @access private
- * @return void
- */
- private function _set_languages()
- {
- if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
- {
- $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
- $this->languages = explode(',', $languages);
- }
- if (count($this->languages) == 0)
- {
- $this->languages = array('Undefined');
- }
- }
- // --------------------------------------------------------------------
- /**
- * Set the accepted character sets
- *
- * @access private
- * @return void
- */
- private function _set_charsets()
- {
- if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
- {
- $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
- $this->charsets = explode(',', $charsets);
- }
- if (count($this->charsets) == 0)
- {
- $this->charsets = array('Undefined');
- }
- }
- // --------------------------------------------------------------------
- /**
- * Is Browser
- *
- * @access public
- * @return bool
- */
- public function is_browser($key = NULL)
- {
- if ( ! $this->is_browser)
- {
- return FALSE;
- }
- // No need to be specific, it's a browser
- if ($key === NULL)
- {
- return TRUE;
- }
- // Check for a specific browser
- return array_key_exists($key, $this->browsers) AND $this->browser === $this->browsers[$key];
- }
- // --------------------------------------------------------------------
- /**
- * Is Robot
- *
- * @access public
- * @return bool
- */
- public function is_robot($key = NULL)
- {
- if ( ! $this->is_robot)
- {
- return FALSE;
- }
- // No need to be specific, it's a robot
- if ($key === NULL)
- {
- return TRUE;
- }
- // Check for a specific robot
- return array_key_exists($key, $this->robots) AND $this->robot === $this->robots[$key];
- }
- // --------------------------------------------------------------------
- /**
- * Is Mobile
- *
- * @access public
- * @return bool
- */
- public function is_mobile($key = NULL)
- {
- if ( ! $this->is_mobile)
- {
- return FALSE;
- }
- // No need to be specific, it's a mobile
- if ($key === NULL)
- {
- return TRUE;
- }
- // Check for a specific robot
- return array_key_exists($key, $this->mobiles) AND $this->mobile === $this->mobiles[$key];
- }
- // --------------------------------------------------------------------
- /**
- * Is this a referral from another site?
- *
- * @access public
- * @return bool
- */
- public function is_referral()
- {
- if ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '')
- {
- return FALSE;
- }
- return TRUE;
- }
- // --------------------------------------------------------------------
- /**
- * Agent String
- *
- * @access public
- * @return string
- */
- public function agent_string()
- {
- return $this->agent;
- }
- // --------------------------------------------------------------------
- /**
- * Get Platform
- *
- * @access public
- * @return string
- */
- public function platform()
- {
- return $this->platform;
- }
- // --------------------------------------------------------------------
- /**
- * Get Browser Name
- *
- * @access public
- * @return string
- */
- public function browser()
- {
- return $this->browser;
- }
- // --------------------------------------------------------------------
- /**
- * Get the Browser Version
- *
- * @access public
- * @return string
- */
- public function version()
- {
- return $this->version;
- }
- // --------------------------------------------------------------------
- /**
- * Get The Robot Name
- *
- * @access public
- * @return string
- */
- public function robot()
- {
- return $this->robot;
- }
- // --------------------------------------------------------------------
- /**
- * Get the Mobile Device
- *
- * @access public
- * @return string
- */
- public function mobile()
- {
- return $this->mobile;
- }
- // --------------------------------------------------------------------
- /**
- * Get the referrer
- *
- * @access public
- * @return bool
- */
- public function referrer()
- {
- return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
- }
- // --------------------------------------------------------------------
- /**
- * Get the accepted languages
- *
- * @access public
- * @return array
- */
- public function languages()
- {
- if (count($this->languages) == 0)
- {
- $this->_set_languages();
- }
- return $this->languages;
- }
- // --------------------------------------------------------------------
- /**
- * Get the accepted Character Sets
- *
- * @access public
- * @return array
- */
- public function charsets()
- {
- if (count($this->charsets) == 0)
- {
- $this->_set_charsets();
- }
- return $this->charsets;
- }
- // --------------------------------------------------------------------
- /**
- * Test for a particular language
- *
- * @access public
- * @return bool
- */
- public function accept_lang($lang = 'en')
- {
- return (in_array(strtolower($lang), $this->languages(), TRUE));
- }
- // --------------------------------------------------------------------
- /**
- * Test for a particular character set
- *
- * @access public
- * @return bool
- */
- public function accept_charset($charset = 'utf-8')
- {
- return (in_array(strtolower($charset), $this->charsets(), TRUE));
- }
- }
- /* End of file User_agent.php */
- /* Location: ./system/libraries/User_agent.php */
|