Commits

Phil Sturgeon  committed 5c36ff3

Altered User agent library so that is_browser(), is_mobile() and is_robot() can optionally check for a specific browser or mobile device.

  • Participants
  • Parent commits a9ac234

Comments (0)

Files changed (4)

File application/config/user_agents.php

 					'nokia'				=> "Nokia",
 					'palm'				=> "Palm",
 					'iphone'			=> "Apple iPhone",
+					'ipad'				=> "iPad",
 					'ipod'				=> "Apple iPod Touch",
 					'sony'				=> "Sony Ericsson",
 					'ericsson'			=> "Sony Ericsson",

File system/libraries/User_agent.php

 	 * @access	private
 	 * @return	bool
 	 */
-	function _load_agent_file()
+	private function _load_agent_file()
 	{
 		if ( ! @include(APPPATH.'config/user_agents'.EXT))
 		{
 	 * @access	private
 	 * @return	bool
 	 */
-	function _compile_data()
+	private function _compile_data()
 	{
 		$this->_set_platform();
 
 	 * @access	private
 	 * @return	mixed
 	 */
-	function _set_platform()
+	private function _set_platform()
 	{
 		if (is_array($this->platforms) AND count($this->platforms) > 0)
 		{
 	 * @access	private
 	 * @return	bool
 	 */
-	function _set_browser()
+	private function _set_browser()
 	{
 		if (is_array($this->browsers) AND count($this->browsers) > 0)
 		{
 	 * @access	private
 	 * @return	bool
 	 */
-	function _set_robot()
+	private function _set_robot()
 	{
 		if (is_array($this->robots) AND count($this->robots) > 0)
 		{
 	 * @access	private
 	 * @return	bool
 	 */
-	function _set_mobile()
+	private function _set_mobile()
 	{
 		if (is_array($this->mobiles) AND count($this->mobiles) > 0)
 		{
 	 * @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'] != '')
 		{
 	 * @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'] != '')
 		{
 	 * @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];
 	}
 
 	// --------------------------------------------------------------------
 	 * @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];
 	}
 
 	// --------------------------------------------------------------------
 	 * @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];
 	}
 
 	// --------------------------------------------------------------------
 	 * @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'] == '');
 	}
 
 	// --------------------------------------------------------------------
 	 * @access	public
 	 * @return	string
 	 */
-	function agent_string()
+	public function agent_string()
 	{
 		return $this->agent;
 	}
 	 * @access	public
 	 * @return	string
 	 */
-	function platform()
+	public function platform()
 	{
 		return $this->platform;
 	}
 	 * @access	public
 	 * @return	string
 	 */
-	function browser()
+	public function browser()
 	{
 		return $this->browser;
 	}
 	 * @access	public
 	 * @return	string
 	 */
-	function version()
+	public function version()
 	{
 		return $this->version;
 	}
 	 * @access	public
 	 * @return	string
 	 */
-	function robot()
+	public function robot()
 	{
 		return $this->robot;
 	}
 	 * @access	public
 	 * @return	string
 	 */
-	function mobile()
+	public function mobile()
 	{
 		return $this->mobile;
 	}
 	 * @access	public
 	 * @return	bool
 	 */
-	function referrer()
+	public function referrer()
 	{
 		return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
 	}
 	 * @access	public
 	 * @return	array
 	 */
-	function languages()
+	public function languages()
 	{
 		if (count($this->languages) == 0)
 		{
 	 * @access	public
 	 * @return	array
 	 */
-	function charsets()
+	public function charsets()
 	{
 		if (count($this->charsets) == 0)
 		{
 	 * @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));
 	}
 
 	// --------------------------------------------------------------------
 	 * @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));
 	}
 
-
 }
 
 

File user_guide/changelog.html

 			<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

File user_guide/libraries/user_agent.html

 <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 />
+&nbsp;&nbsp;&nbsp;&nbsp;echo 'You are using Safari.';<br />
+}<br />
+else if ($this->agent->is_browser())<br />
+{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;echo 'You are using a browser.';<br />
+}</code>
+
+<p class="important"><strong>Note:</strong>&nbsp; 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 />
+&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('iphone/home');<br />
+}<br />
+else if ($this->agent->is_mobile())<br />
+{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('mobile/home');<br />
+}<br/>
+else<br />
+{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;$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>