1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, 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 function CI_User_agent()
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 function _load_agent_file()
87 if ( ! @include(APPPATH.'config/user_agents'.EXT))
94 if (isset($platforms))
96 $this->platforms = $platforms;
101 if (isset($browsers))
103 $this->browsers = $browsers;
110 $this->mobiles = $mobiles;
117 $this->robots = $robots;
125 // --------------------------------------------------------------------
128 * Compile the User Agent Data
133 function _compile_data()
135 $this->_set_platform();
137 foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function)
139 if ($this->$function() === TRUE)
146 // --------------------------------------------------------------------
154 function _set_platform()
156 if (is_array($this->platforms) AND count($this->platforms) > 0)
158 foreach ($this->platforms as $key => $val)
160 if (preg_match("|".preg_quote($key)."|i", $this->agent))
162 $this->platform = $val;
167 $this->platform = 'Unknown Platform';
170 // --------------------------------------------------------------------
178 function _set_browser()
180 if (is_array($this->browsers) AND count($this->browsers) > 0)
182 foreach ($this->browsers as $key => $val)
184 if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
186 $this->is_browser = TRUE;
187 $this->version = $match[1];
188 $this->browser = $val;
189 $this->_set_mobile();
197 // --------------------------------------------------------------------
205 function _set_robot()
207 if (is_array($this->robots) AND count($this->robots) > 0)
209 foreach ($this->robots as $key => $val)
211 if (preg_match("|".preg_quote($key)."|i", $this->agent))
213 $this->is_robot = TRUE;
222 // --------------------------------------------------------------------
225 * Set the Mobile Device
230 function _set_mobile()
232 if (is_array($this->mobiles) AND count($this->mobiles) > 0)
234 foreach ($this->mobiles as $key => $val)
236 if (FALSE !== (strpos(strtolower($this->agent), $key)))
238 $this->is_mobile = TRUE;
239 $this->mobile = $val;
247 // --------------------------------------------------------------------
250 * Set the accepted languages
255 function _set_languages()
257 if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
259 $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
261 $this->languages = explode(',', $languages);
264 if (count($this->languages) == 0)
266 $this->languages = array('Undefined');
270 // --------------------------------------------------------------------
273 * Set the accepted character sets
278 function _set_charsets()
280 if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
282 $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
284 $this->charsets = explode(',', $charsets);
287 if (count($this->charsets) == 0)
289 $this->charsets = array('Undefined');
293 // --------------------------------------------------------------------
301 function is_browser()
303 return $this->is_browser;
306 // --------------------------------------------------------------------
316 return $this->is_robot;
319 // --------------------------------------------------------------------
329 return $this->is_mobile;
332 // --------------------------------------------------------------------
335 * Is this a referral from another site?
340 function is_referral()
342 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
345 // --------------------------------------------------------------------
353 function agent_string()
358 // --------------------------------------------------------------------
368 return $this->platform;
371 // --------------------------------------------------------------------
381 return $this->browser;
384 // --------------------------------------------------------------------
387 * Get the Browser Version
394 return $this->version;
397 // --------------------------------------------------------------------
409 // --------------------------------------------------------------------
412 * Get the Mobile Device
419 return $this->mobile;
422 // --------------------------------------------------------------------
432 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
435 // --------------------------------------------------------------------
438 * Get the accepted languages
445 if (count($this->languages) == 0)
447 $this->_set_languages();
450 return $this->languages;
453 // --------------------------------------------------------------------
456 * Get the accepted Character Sets
463 if (count($this->charsets) == 0)
465 $this->_set_charsets();
468 return $this->charsets;
471 // --------------------------------------------------------------------
474 * Test for a particular language
479 function accept_lang($lang = 'en')
481 return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
484 // --------------------------------------------------------------------
487 * Test for a particular character set
492 function accept_charset($charset = 'utf-8')
494 return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
501 /* End of file User_agent.php */
502 /* Location: ./system/libraries/User_agent.php */