diff options
author | Phil Sturgeon <email@philsturgeon.co.uk> | 2011-01-06 18:40:10 +0100 |
---|---|---|
committer | Phil Sturgeon <email@philsturgeon.co.uk> | 2011-01-06 18:40:10 +0100 |
commit | dac1b468d5432ff9166fe221ac520b5050a65f0e (patch) | |
tree | 6e3d80bf9d37347ede0244b480f08f8a330a1957 | |
parent | c2e9225eec976326df36b666d6baf6db29eee831 (diff) |
Altered User agent library so that is_browser(), is_mobile() and is_robot() can optionally check for a specific browser or mobile device.
-rw-r--r-- | application/config/user_agents.php | 1 | ||||
-rw-r--r-- | system/libraries/User_agent.php | 95 | ||||
-rw-r--r-- | user_guide/changelog.html | 1 | ||||
-rw-r--r-- | user_guide/libraries/user_agent.html | 25 |
4 files changed, 92 insertions, 30 deletions
diff --git a/application/config/user_agents.php b/application/config/user_agents.php index 985deed24..e2d3c3af0 100644 --- a/application/config/user_agents.php +++ b/application/config/user_agents.php @@ -94,6 +94,7 @@ $mobiles = array( 'nokia' => "Nokia", 'palm' => "Palm", 'iphone' => "Apple iPhone", + 'ipad' => "iPad", 'ipod' => "Apple iPod Touch", 'sony' => "Sony Ericsson", 'ericsson' => "Sony Ericsson", diff --git a/system/libraries/User_agent.php b/system/libraries/User_agent.php index c1fb0231a..53cd7b2fd 100644 --- a/system/libraries/User_agent.php +++ b/system/libraries/User_agent.php @@ -82,7 +82,7 @@ class CI_User_agent { * @access private * @return bool */ - function _load_agent_file() + private function _load_agent_file() { if ( ! @include(APPPATH.'config/user_agents'.EXT)) { @@ -130,7 +130,7 @@ class CI_User_agent { * @access private * @return bool */ - function _compile_data() + private function _compile_data() { $this->_set_platform(); @@ -151,7 +151,7 @@ class CI_User_agent { * @access private * @return mixed */ - function _set_platform() + private function _set_platform() { if (is_array($this->platforms) AND count($this->platforms) > 0) { @@ -175,7 +175,7 @@ class CI_User_agent { * @access private * @return bool */ - function _set_browser() + private function _set_browser() { if (is_array($this->browsers) AND count($this->browsers) > 0) { @@ -202,7 +202,7 @@ class CI_User_agent { * @access private * @return bool */ - function _set_robot() + private function _set_robot() { if (is_array($this->robots) AND count($this->robots) > 0) { @@ -227,7 +227,7 @@ class CI_User_agent { * @access private * @return bool */ - function _set_mobile() + private function _set_mobile() { if (is_array($this->mobiles) AND count($this->mobiles) > 0) { @@ -252,7 +252,7 @@ class CI_User_agent { * @access private * @return void */ - function _set_languages() + private function _set_languages() { if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '') { @@ -275,7 +275,7 @@ class CI_User_agent { * @access private * @return void */ - function _set_charsets() + private function _set_charsets() { if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '') { @@ -298,9 +298,21 @@ class CI_User_agent { * @access public * @return bool */ - function is_browser() + public function is_browser($key = NULL) { - return $this->is_browser; + 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]; } // -------------------------------------------------------------------- @@ -311,9 +323,21 @@ class CI_User_agent { * @access public * @return bool */ - function is_robot() + public function is_robot($key = NULL) { - return $this->is_robot; + 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]; } // -------------------------------------------------------------------- @@ -324,9 +348,21 @@ class CI_User_agent { * @access public * @return bool */ - function is_mobile() + public function is_mobile($key = NULL) { - return $this->is_mobile; + 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]; } // -------------------------------------------------------------------- @@ -337,9 +373,9 @@ class CI_User_agent { * @access public * @return bool */ - function is_referral() + public function is_referral() { - return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE; + return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == ''); } // -------------------------------------------------------------------- @@ -350,7 +386,7 @@ class CI_User_agent { * @access public * @return string */ - function agent_string() + public function agent_string() { return $this->agent; } @@ -363,7 +399,7 @@ class CI_User_agent { * @access public * @return string */ - function platform() + public function platform() { return $this->platform; } @@ -376,7 +412,7 @@ class CI_User_agent { * @access public * @return string */ - function browser() + public function browser() { return $this->browser; } @@ -389,7 +425,7 @@ class CI_User_agent { * @access public * @return string */ - function version() + public function version() { return $this->version; } @@ -402,7 +438,7 @@ class CI_User_agent { * @access public * @return string */ - function robot() + public function robot() { return $this->robot; } @@ -414,7 +450,7 @@ class CI_User_agent { * @access public * @return string */ - function mobile() + public function mobile() { return $this->mobile; } @@ -427,7 +463,7 @@ class CI_User_agent { * @access public * @return bool */ - function referrer() + public function referrer() { return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']); } @@ -440,7 +476,7 @@ class CI_User_agent { * @access public * @return array */ - function languages() + public function languages() { if (count($this->languages) == 0) { @@ -458,7 +494,7 @@ class CI_User_agent { * @access public * @return array */ - function charsets() + public function charsets() { if (count($this->charsets) == 0) { @@ -476,9 +512,9 @@ class CI_User_agent { * @access public * @return bool */ - function accept_lang($lang = 'en') + public function accept_lang($lang = 'en') { - return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE; + return (in_array(strtolower($lang), $this->languages(), TRUE)); } // -------------------------------------------------------------------- @@ -489,12 +525,11 @@ class CI_User_agent { * @access public * @return bool */ - function accept_charset($charset = 'utf-8') + public function accept_charset($charset = 'utf-8') { - return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE; + return (in_array(strtolower($charset), $this->charsets(), TRUE)); } - } diff --git a/user_guide/changelog.html b/user_guide/changelog.html index 09efc5434..a6332d64d 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -113,6 +113,7 @@ Hg Tag: </p> <li>Altered Form_Validation library to allow for method chaining on <kbd>set_rules()</kbd>, <kbd>set_message()</kbd> and <kbd>set_error_delimiters()</kbd> functions.</li> <li>Altered Email Library to allow for method chaining.</li> <li>Added <kbd>request_headers()</kbd>, <kbd>get_request_header()</kbd> and <kbd>is_ajax_request()</kbd> to the input class.</li> + <li>Altered <a href="libraries/user_agent.html">User agent library</a> so that <kbd>is_browser()</kbd>, <kbd>is_mobile()</kbd> and <kbd>is_robot()</kbd> can optionally check for a specific browser or mobile device.</li> </ul> </li> <li>Database diff --git a/user_guide/libraries/user_agent.html b/user_guide/libraries/user_agent.html index 944f1ed86..d2f79d2ec 100644 --- a/user_guide/libraries/user_agent.html +++ b/user_guide/libraries/user_agent.html @@ -111,9 +111,34 @@ echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.) <h2>$this->agent->is_browser()</h2> <p>Returns TRUE/FALSE (boolean) if the user agent is a known web browser.</p> +<code> if ($this->agent->is_browser('Safari'))<br /> +{<br /> + echo 'You are using Safari.';<br /> +}<br /> +else if ($this->agent->is_browser())<br /> +{<br /> + echo 'You are using a browser.';<br /> +}</code> + +<p class="important"><strong>Note:</strong> The string "Safari" in this example is an array key in the list of browser definitions. +You can find this list in <dfn>application/config/user_agents.php</dfn> if you want to add new browsers or change the stings.</p> + <h2>$this->agent->is_mobile()</h2> <p>Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.</p> +<code> if ($this->agent->is_mobile('iphone'))<br /> +{<br /> + $this->load->view('iphone/home');<br /> +}<br /> +else if ($this->agent->is_mobile())<br /> +{<br /> + $this->load->view('mobile/home');<br /> +}<br/> +else<br /> +{<br /> + $this->load->view('web/home');<br /> +}</code> + <h2>$this->agent->is_robot()</h2> <p>Returns TRUE/FALSE (boolean) if the user agent is a known robot.</p> |