diff options
Diffstat (limited to 'system/libraries/User_agent.php')
-rw-r--r-- | system/libraries/User_agent.php | 261 |
1 files changed, 165 insertions, 96 deletions
diff --git a/system/libraries/User_agent.php b/system/libraries/User_agent.php index 0b77a7d42..ff596f04b 100644 --- a/system/libraries/User_agent.php +++ b/system/libraries/User_agent.php @@ -1,59 +1,153 @@ -<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); +<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * CodeIgniter * - * An open source application development framework for PHP 5.1.6 or newer + * An open source application development framework for PHP 5.2.4 or newer + * + * NOTICE OF LICENSE + * + * Licensed under the Open Software License version 3.0 + * + * This source file is subject to the Open Software License (OSL 3.0) that is + * bundled with this package in the files license.txt / license.rst. It is + * also available through the world wide web at this URL: + * http://opensource.org/licenses/OSL-3.0 + * If you did not receive a copy of the license and are unable to obtain it + * through the world wide web, please send an email to + * licensing@ellislab.com so we can send you a copy immediately. * * @package CodeIgniter - * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. - * @license http://codeigniter.com/user_guide/license.html + * @author EllisLab Dev Team + * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) + * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 1.0 * @filesource */ -// ------------------------------------------------------------------------ - /** * User Agent Class * - * Identifies the platform, browser, robot, or mobile devise of the browsing agent + * Identifies the platform, browser, robot, or mobile device of the browsing agent * * @package CodeIgniter * @subpackage Libraries * @category User Agent - * @author ExpressionEngine Dev Team + * @author EllisLab Dev Team * @link http://codeigniter.com/user_guide/libraries/user_agent.html */ class CI_User_agent { - var $agent = NULL; + /** + * Current user-agent + * + * @var string + */ + public $agent = NULL; - var $is_browser = FALSE; - var $is_robot = FALSE; - var $is_mobile = FALSE; + /** + * Flag for if the user-agent belongs to a browser + * + * @var bool + */ + public $is_browser = FALSE; - var $languages = array(); - var $charsets = array(); + /** + * Flag for if the user-agent is a robot + * + * @var bool + */ + public $is_robot = FALSE; - var $platforms = array(); - var $browsers = array(); - var $mobiles = array(); - var $robots = array(); + /** + * Flag for if the user-agent is a mobile browser + * + * @var bool + */ + public $is_mobile = FALSE; - var $platform = ''; - var $browser = ''; - var $version = ''; - var $mobile = ''; - var $robot = ''; + /** + * Languages accepted by the current user agent + * + * @var array + */ + public $languages = array(); + + /** + * Character sets accepted by the current user agent + * + * @var array + */ + public $charsets = array(); + + /** + * List of platforms to compare against current user agent + * + * @var array + */ + public $platforms = array(); + + /** + * List of browsers to compare against current user agent + * + * @var array + */ + public $browsers = array(); + + /** + * List of mobile browsers to compare against current user agent + * + * @var array + */ + public $mobiles = array(); + + /** + * List of robots to compare against current user agent + * + * @var array + */ + public $robots = array(); + + /** + * Current user-agent platform + * + * @var string + */ + public $platform = ''; + + /** + * Current user-agent browser + * + * @var string + */ + public $browser = ''; + + /** + * Current user-agent version + * + * @var string + */ + public $version = ''; + + /** + * Current user-agent mobile name + * + * @var string + */ + public $mobile = ''; + + /** + * Current user-agent robot name + * + * @var string + */ + public $robot = ''; /** * Constructor * * Sets the User Agent and runs the compilation routine * - * @access public * @return void */ public function __construct() @@ -63,15 +157,12 @@ class CI_User_agent { $this->agent = trim($_SERVER['HTTP_USER_AGENT']); } - if ( ! is_null($this->agent)) + if ( ! is_null($this->agent) && $this->_load_agent_file()) { - if ($this->_load_agent_file()) - { - $this->_compile_data(); - } + $this->_compile_data(); } - log_message('debug', "User Agent Class Initialized"); + log_message('debug', 'User Agent Class Initialized'); } // -------------------------------------------------------------------- @@ -79,12 +170,11 @@ class CI_User_agent { /** * Compile the User Agent Data * - * @access private * @return bool */ - private function _load_agent_file() + protected function _load_agent_file() { - if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php')) + if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php')) { include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'); } @@ -135,10 +225,9 @@ class CI_User_agent { /** * Compile the User Agent Data * - * @access private * @return bool */ - private function _compile_data() + protected function _compile_data() { $this->_set_platform(); @@ -156,23 +245,24 @@ class CI_User_agent { /** * Set the Platform * - * @access private - * @return mixed + * @return bool */ - private function _set_platform() + protected function _set_platform() { - if (is_array($this->platforms) AND count($this->platforms) > 0) + if (is_array($this->platforms) && count($this->platforms) > 0) { foreach ($this->platforms as $key => $val) { - if (preg_match("|".preg_quote($key)."|i", $this->agent)) + if (preg_match('|'.preg_quote($key).'|i', $this->agent)) { $this->platform = $val; return TRUE; } } } + $this->platform = 'Unknown Platform'; + return FALSE; } // -------------------------------------------------------------------- @@ -180,16 +270,15 @@ class CI_User_agent { /** * Set the Browser * - * @access private * @return bool */ - private function _set_browser() + protected function _set_browser() { - if (is_array($this->browsers) AND count($this->browsers) > 0) + if (is_array($this->browsers) && count($this->browsers) > 0) { foreach ($this->browsers as $key => $val) { - if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match)) + if (preg_match('|'.preg_quote($key).'.*?([0-9\.]+)|i', $this->agent, $match)) { $this->is_browser = TRUE; $this->version = $match[1]; @@ -199,6 +288,7 @@ class CI_User_agent { } } } + return FALSE; } @@ -207,16 +297,15 @@ class CI_User_agent { /** * Set the Robot * - * @access private * @return bool */ - private function _set_robot() + protected function _set_robot() { - if (is_array($this->robots) AND count($this->robots) > 0) + if (is_array($this->robots) && count($this->robots) > 0) { foreach ($this->robots as $key => $val) { - if (preg_match("|".preg_quote($key)."|i", $this->agent)) + if (preg_match('|'.preg_quote($key).'|i', $this->agent)) { $this->is_robot = TRUE; $this->robot = $val; @@ -224,6 +313,7 @@ class CI_User_agent { } } } + return FALSE; } @@ -232,16 +322,15 @@ class CI_User_agent { /** * Set the Mobile Device * - * @access private * @return bool */ - private function _set_mobile() + protected function _set_mobile() { - if (is_array($this->mobiles) AND count($this->mobiles) > 0) + if (is_array($this->mobiles) && count($this->mobiles) > 0) { foreach ($this->mobiles as $key => $val) { - if (FALSE !== (strpos(strtolower($this->agent), $key))) + if (FALSE !== (stripos($this->agent, $key))) { $this->is_mobile = TRUE; $this->mobile = $val; @@ -249,6 +338,7 @@ class CI_User_agent { } } } + return FALSE; } @@ -257,19 +347,16 @@ class CI_User_agent { /** * Set the accepted languages * - * @access private * @return void */ - private function _set_languages() + protected function _set_languages() { - if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '') + if ((count($this->languages) === 0) && ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE']))); - - $this->languages = explode(',', $languages); + $this->languages = explode(',', preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])))); } - if (count($this->languages) == 0) + if (count($this->languages) === 0) { $this->languages = array('Undefined'); } @@ -280,19 +367,16 @@ class CI_User_agent { /** * Set the accepted character sets * - * @access private * @return void */ - private function _set_charsets() + protected function _set_charsets() { - if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '') + if ((count($this->charsets) === 0) && ! empty($_SERVER['HTTP_ACCEPT_CHARSET'])) { - $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET']))); - - $this->charsets = explode(',', $charsets); + $this->charsets = explode(',', preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])))); } - if (count($this->charsets) == 0) + if (count($this->charsets) === 0) { $this->charsets = array('Undefined'); } @@ -303,7 +387,7 @@ class CI_User_agent { /** * Is Browser * - * @access public + * @param string $key * @return bool */ public function is_browser($key = NULL) @@ -320,7 +404,7 @@ class CI_User_agent { } // Check for a specific browser - return array_key_exists($key, $this->browsers) AND $this->browser === $this->browsers[$key]; + return (isset($this->browsers[$key]) && $this->browser === $this->browsers[$key]); } // -------------------------------------------------------------------- @@ -328,7 +412,7 @@ class CI_User_agent { /** * Is Robot * - * @access public + * @param string $key * @return bool */ public function is_robot($key = NULL) @@ -345,7 +429,7 @@ class CI_User_agent { } // Check for a specific robot - return array_key_exists($key, $this->robots) AND $this->robot === $this->robots[$key]; + return (isset($this->robots[$key]) && $this->robot === $this->robots[$key]); } // -------------------------------------------------------------------- @@ -353,7 +437,7 @@ class CI_User_agent { /** * Is Mobile * - * @access public + * @param string $key * @return bool */ public function is_mobile($key = NULL) @@ -370,7 +454,7 @@ class CI_User_agent { } // Check for a specific robot - return array_key_exists($key, $this->mobiles) AND $this->mobile === $this->mobiles[$key]; + return (isset($this->mobiles[$key]) && $this->mobile === $this->mobiles[$key]); } // -------------------------------------------------------------------- @@ -378,16 +462,11 @@ class CI_User_agent { /** * 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; + return ! empty($_SERVER['HTTP_REFERER']); } // -------------------------------------------------------------------- @@ -395,7 +474,6 @@ class CI_User_agent { /** * Agent String * - * @access public * @return string */ public function agent_string() @@ -408,7 +486,6 @@ class CI_User_agent { /** * Get Platform * - * @access public * @return string */ public function platform() @@ -421,7 +498,6 @@ class CI_User_agent { /** * Get Browser Name * - * @access public * @return string */ public function browser() @@ -434,7 +510,6 @@ class CI_User_agent { /** * Get the Browser Version * - * @access public * @return string */ public function version() @@ -447,7 +522,6 @@ class CI_User_agent { /** * Get The Robot Name * - * @access public * @return string */ public function robot() @@ -459,7 +533,6 @@ class CI_User_agent { /** * Get the Mobile Device * - * @access public * @return string */ public function mobile() @@ -472,12 +545,11 @@ class CI_User_agent { /** * Get the referrer * - * @access public * @return bool */ public function referrer() { - return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']); + return empty($_SERVER['HTTP_REFERER']) ? '' : trim($_SERVER['HTTP_REFERER']); } // -------------------------------------------------------------------- @@ -485,12 +557,11 @@ class CI_User_agent { /** * Get the accepted languages * - * @access public * @return array */ public function languages() { - if (count($this->languages) == 0) + if (count($this->languages) === 0) { $this->_set_languages(); } @@ -503,12 +574,11 @@ class CI_User_agent { /** * Get the accepted Character Sets * - * @access public * @return array */ public function charsets() { - if (count($this->charsets) == 0) + if (count($this->charsets) === 0) { $this->_set_charsets(); } @@ -521,12 +591,12 @@ class CI_User_agent { /** * Test for a particular language * - * @access public + * @param string $lang * @return bool */ public function accept_lang($lang = 'en') { - return (in_array(strtolower($lang), $this->languages(), TRUE)); + return in_array(strtolower($lang), $this->languages(), TRUE); } // -------------------------------------------------------------------- @@ -534,16 +604,15 @@ class CI_User_agent { /** * Test for a particular character set * - * @access public + * @param string $charset * @return bool */ public function accept_charset($charset = 'utf-8') { - return (in_array(strtolower($charset), $this->charsets(), TRUE)); + return in_array(strtolower($charset), $this->charsets(), TRUE); } } - /* End of file User_agent.php */ /* Location: ./system/libraries/User_agent.php */
\ No newline at end of file |