1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * Identifies the platform, browser, robot, or mobile devise of the browsing agent
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category User Agent
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/user_agent.html
33 var $is_browser = FALSE;
34 var $is_robot = FALSE;
35 var $is_mobile = FALSE;
37 var $languages = array();
38 var $charsets = array();
40 var $platforms = array();
41 var $browsers = array();
42 var $mobiles = array();
43 var $robots = array();
54 * Sets the User Agent and runs the compilation routine
59 public function __construct()
61 if (isset($_SERVER['HTTP_USER_AGENT']))
63 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
66 if ( ! is_null($this->agent))
68 if ($this->_load_agent_file())
70 $this->_compile_data();
74 log_message('debug', "User Agent Class Initialized");
77 // --------------------------------------------------------------------
80 * Compile the User Agent Data
85 private function _load_agent_file()
87 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/user_agents'.EXT))
89 include(APPPATH.'config/'.ENVIRONMENT.'/user_agents'.EXT);
91 elseif (is_file(APPPATH.'config/user_agents'.EXT))
93 include(APPPATH.'config/user_agents'.EXT);
102 if (isset($platforms))
104 $this->platforms = $platforms;
109 if (isset($browsers))
111 $this->browsers = $browsers;
118 $this->mobiles = $mobiles;
125 $this->robots = $robots;
133 // --------------------------------------------------------------------
136 * Compile the User Agent Data
141 private function _compile_data()
143 $this->_set_platform();
145 foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function)
147 if ($this->$function() === TRUE)
154 // --------------------------------------------------------------------
162 private function _set_platform()
164 if (is_array($this->platforms) AND count($this->platforms) > 0)
166 foreach ($this->platforms as $key => $val)
168 if (preg_match("|".preg_quote($key)."|i", $this->agent))
170 $this->platform = $val;
175 $this->platform = 'Unknown Platform';
178 // --------------------------------------------------------------------
186 private function _set_browser()
188 if (is_array($this->browsers) AND count($this->browsers) > 0)
190 foreach ($this->browsers as $key => $val)
192 if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
194 $this->is_browser = TRUE;
195 $this->version = $match[1];
196 $this->browser = $val;
197 $this->_set_mobile();
205 // --------------------------------------------------------------------
213 private function _set_robot()
215 if (is_array($this->robots) AND count($this->robots) > 0)
217 foreach ($this->robots as $key => $val)
219 if (preg_match("|".preg_quote($key)."|i", $this->agent))
221 $this->is_robot = TRUE;
230 // --------------------------------------------------------------------
233 * Set the Mobile Device
238 private function _set_mobile()
240 if (is_array($this->mobiles) AND count($this->mobiles) > 0)
242 foreach ($this->mobiles as $key => $val)
244 if (FALSE !== (strpos(strtolower($this->agent), $key)))
246 $this->is_mobile = TRUE;
247 $this->mobile = $val;
255 // --------------------------------------------------------------------
258 * Set the accepted languages
263 private function _set_languages()
265 if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
267 $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
269 $this->languages = explode(',', $languages);
272 if (count($this->languages) == 0)
274 $this->languages = array('Undefined');
278 // --------------------------------------------------------------------
281 * Set the accepted character sets
286 private function _set_charsets()
288 if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
290 $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
292 $this->charsets = explode(',', $charsets);
295 if (count($this->charsets) == 0)
297 $this->charsets = array('Undefined');
301 // --------------------------------------------------------------------
309 public function is_browser($key = NULL)
311 if ( ! $this->is_browser)
316 // No need to be specific, it's a browser
322 // Check for a specific browser
323 return array_key_exists($key, $this->browsers) AND $this->browser === $this->browsers[$key];
326 // --------------------------------------------------------------------
334 public function is_robot($key = NULL)
336 if ( ! $this->is_robot)
341 // No need to be specific, it's a robot
347 // Check for a specific robot
348 return array_key_exists($key, $this->robots) AND $this->robot === $this->robots[$key];
351 // --------------------------------------------------------------------
359 public function is_mobile($key = NULL)
361 if ( ! $this->is_mobile)
366 // No need to be specific, it's a mobile
372 // Check for a specific robot
373 return array_key_exists($key, $this->mobiles) AND $this->mobile === $this->mobiles[$key];
376 // --------------------------------------------------------------------
379 * Is this a referral from another site?
384 public function is_referral()
386 if ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '')
393 // --------------------------------------------------------------------
401 public function agent_string()
406 // --------------------------------------------------------------------
414 public function platform()
416 return $this->platform;
419 // --------------------------------------------------------------------
427 public function browser()
429 return $this->browser;
432 // --------------------------------------------------------------------
435 * Get the Browser Version
440 public function version()
442 return $this->version;
445 // --------------------------------------------------------------------
453 public function robot()
457 // --------------------------------------------------------------------
460 * Get the Mobile Device
465 public function mobile()
467 return $this->mobile;
470 // --------------------------------------------------------------------
478 public function referrer()
480 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
483 // --------------------------------------------------------------------
486 * Get the accepted languages
491 public function languages()
493 if (count($this->languages) == 0)
495 $this->_set_languages();
498 return $this->languages;
501 // --------------------------------------------------------------------
504 * Get the accepted Character Sets
509 public function charsets()
511 if (count($this->charsets) == 0)
513 $this->_set_charsets();
516 return $this->charsets;
519 // --------------------------------------------------------------------
522 * Test for a particular language
527 public function accept_lang($lang = 'en')
529 return (in_array(strtolower($lang), $this->languages(), TRUE));
532 // --------------------------------------------------------------------
535 * Test for a particular character set
540 public function accept_charset($charset = 'utf-8')
542 return (in_array(strtolower($charset), $this->charsets(), TRUE));
548 /* End of file User_agent.php */
549 /* Location: ./system/libraries/User_agent.php */