1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
21 * Identifies the platform, browser, robot, or mobile devise of the browsing agent
\r
23 * @package CodeIgniter
\r
24 * @subpackage Libraries
\r
25 * @category User Agent
\r
26 * @author ExpressionEngine Dev Team
\r
27 * @link http://codeigniter.com/user_guide/libraries/user_agent.html
\r
29 class CI_User_agent {
\r
33 var $is_browser = FALSE;
\r
34 var $is_robot = FALSE;
\r
35 var $is_mobile = FALSE;
\r
37 var $languages = array();
\r
38 var $charsets = array();
\r
40 var $platforms = array();
\r
41 var $browsers = array();
\r
42 var $mobiles = array();
\r
43 var $robots = array();
\r
54 * Sets the User Agent and runs the compilation routine
\r
59 function CI_User_agent()
\r
61 if (isset($_SERVER['HTTP_USER_AGENT']))
\r
63 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
\r
66 if ( ! is_null($this->agent))
\r
68 if ($this->_load_agent_file())
\r
70 $this->_compile_data();
\r
74 log_message('debug', "User Agent Class Initialized");
\r
77 // --------------------------------------------------------------------
\r
80 * Compile the User Agent Data
\r
85 function _load_agent_file()
\r
87 if ( ! @include(APPPATH.'config/user_agents'.EXT))
\r
94 if (isset($platforms))
\r
96 $this->platforms = $platforms;
\r
101 if (isset($browsers))
\r
103 $this->browsers = $browsers;
\r
108 if (isset($mobiles))
\r
110 $this->mobiles = $mobiles;
\r
115 if (isset($robots))
\r
117 $this->robots = $robots;
\r
125 // --------------------------------------------------------------------
\r
128 * Compile the User Agent Data
\r
133 function _compile_data()
\r
135 $this->_set_platform();
\r
137 foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function)
\r
139 if ($this->$function() === TRUE)
\r
146 // --------------------------------------------------------------------
\r
154 function _set_platform()
\r
156 if (is_array($this->platforms) AND count($this->platforms) > 0)
\r
158 foreach ($this->platforms as $key => $val)
\r
160 if (preg_match("|".preg_quote($key)."|i", $this->agent))
\r
162 $this->platform = $val;
\r
167 $this->platform = 'Unknown Platform';
\r
170 // --------------------------------------------------------------------
\r
178 function _set_browser()
\r
180 if (is_array($this->browsers) AND count($this->browsers) > 0)
\r
182 foreach ($this->browsers as $key => $val)
\r
184 if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
\r
186 $this->is_browser = TRUE;
\r
187 $this->version = $match[1];
\r
188 $this->browser = $val;
\r
189 $this->_set_mobile();
\r
197 // --------------------------------------------------------------------
\r
205 function _set_robot()
\r
207 if (is_array($this->robots) AND count($this->robots) > 0)
\r
209 foreach ($this->robots as $key => $val)
\r
211 if (preg_match("|".preg_quote($key)."|i", $this->agent))
\r
213 $this->is_robot = TRUE;
\r
214 $this->robot = $val;
\r
222 // --------------------------------------------------------------------
\r
225 * Set the Mobile Device
\r
230 function _set_mobile()
\r
232 if (is_array($this->mobiles) AND count($this->mobiles) > 0)
\r
234 foreach ($this->mobiles as $key => $val)
\r
236 if (FALSE !== (strpos(strtolower($this->agent), $key)))
\r
238 $this->is_mobile = TRUE;
\r
239 $this->mobile = $val;
\r
247 // --------------------------------------------------------------------
\r
250 * Set the accepted languages
\r
255 function _set_languages()
\r
257 if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
\r
259 $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
\r
261 $this->languages = explode(',', $languages);
\r
264 if (count($this->languages) == 0)
\r
266 $this->languages = array('Undefined');
\r
270 // --------------------------------------------------------------------
\r
273 * Set the accepted character sets
\r
278 function _set_charsets()
\r
280 if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
\r
282 $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
\r
284 $this->charsets = explode(',', $charsets);
\r
287 if (count($this->charsets) == 0)
\r
289 $this->charsets = array('Undefined');
\r
293 // --------------------------------------------------------------------
\r
301 function is_browser()
\r
303 return $this->is_browser;
\r
306 // --------------------------------------------------------------------
\r
314 function is_robot()
\r
316 return $this->is_robot;
\r
319 // --------------------------------------------------------------------
\r
327 function is_mobile()
\r
329 return $this->is_mobile;
\r
332 // --------------------------------------------------------------------
\r
335 * Is this a referral from another site?
\r
340 function is_referral()
\r
342 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
\r
345 // --------------------------------------------------------------------
\r
353 function agent_string()
\r
355 return $this->agent;
\r
358 // --------------------------------------------------------------------
\r
366 function platform()
\r
368 return $this->platform;
\r
371 // --------------------------------------------------------------------
\r
381 return $this->browser;
\r
384 // --------------------------------------------------------------------
\r
387 * Get the Browser Version
\r
394 return $this->version;
\r
397 // --------------------------------------------------------------------
\r
400 * Get The Robot Name
\r
407 return $this->robot;
\r
409 // --------------------------------------------------------------------
\r
412 * Get the Mobile Device
\r
419 return $this->mobile;
\r
422 // --------------------------------------------------------------------
\r
430 function referrer()
\r
432 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
\r
435 // --------------------------------------------------------------------
\r
438 * Get the accepted languages
\r
443 function languages()
\r
445 if (count($this->languages) == 0)
\r
447 $this->_set_languages();
\r
450 return $this->languages;
\r
453 // --------------------------------------------------------------------
\r
456 * Get the accepted Character Sets
\r
461 function charsets()
\r
463 if (count($this->charsets) == 0)
\r
465 $this->_set_charsets();
\r
468 return $this->charsets;
\r
471 // --------------------------------------------------------------------
\r
474 * Test for a particular language
\r
479 function accept_lang($lang = 'en')
\r
481 return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
\r
484 // --------------------------------------------------------------------
\r
487 * Test for a particular character set
\r
492 function accept_charset($charset = 'utf-8')
\r
494 return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
\r
501 /* End of file User_agent.php */
\r
502 /* Location: ./system/libraries/User_agent.php */