blob: 00cbf6967fde45870bdde3d787ff443e40466553 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * User Agent Class
20 *
21 * Identifies the platform, browser, robot, or mobile devise of the browsing agent
22 *
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
28 */
29class CI_User_agent {
30
31 var $agent = NULL;
Barry Mienydd671972010-10-04 16:33:58 +020032
Derek Allard2067d1a2008-11-13 22:59:24 +000033 var $is_browser = FALSE;
34 var $is_robot = FALSE;
35 var $is_mobile = FALSE;
36
37 var $languages = array();
38 var $charsets = array();
Barry Mienydd671972010-10-04 16:33:58 +020039
Derek Allard2067d1a2008-11-13 22:59:24 +000040 var $platforms = array();
41 var $browsers = array();
42 var $mobiles = array();
43 var $robots = array();
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 var $platform = '';
46 var $browser = '';
47 var $version = '';
48 var $mobile = '';
49 var $robot = '';
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 /**
52 * Constructor
53 *
54 * Sets the User Agent and runs the compilation routine
55 *
56 * @access public
57 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020058 */
Derek Allard2067d1a2008-11-13 22:59:24 +000059 function CI_User_agent()
60 {
61 if (isset($_SERVER['HTTP_USER_AGENT']))
62 {
63 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
64 }
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 if ( ! is_null($this->agent))
67 {
68 if ($this->_load_agent_file())
69 {
70 $this->_compile_data();
71 }
72 }
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Allard2067d1a2008-11-13 22:59:24 +000074 log_message('debug', "User Agent Class Initialized");
75 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Allard2067d1a2008-11-13 22:59:24 +000079 /**
80 * Compile the User Agent Data
81 *
82 * @access private
83 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020084 */
Derek Allard2067d1a2008-11-13 22:59:24 +000085 function _load_agent_file()
86 {
87 if ( ! @include(APPPATH.'config/user_agents'.EXT))
88 {
89 return FALSE;
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 $return = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 if (isset($platforms))
95 {
96 $this->platforms = $platforms;
97 unset($platforms);
98 $return = TRUE;
99 }
100
101 if (isset($browsers))
102 {
103 $this->browsers = $browsers;
104 unset($browsers);
105 $return = TRUE;
106 }
107
108 if (isset($mobiles))
109 {
110 $this->mobiles = $mobiles;
111 unset($mobiles);
112 $return = TRUE;
113 }
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 if (isset($robots))
116 {
117 $this->robots = $robots;
118 unset($robots);
119 $return = TRUE;
120 }
121
122 return $return;
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 /**
128 * Compile the User Agent Data
129 *
130 * @access private
131 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200132 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 function _compile_data()
134 {
135 $this->_set_platform();
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function)
138 {
139 if ($this->$function() === TRUE)
140 {
141 break;
142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 /**
149 * Set the Platform
150 *
151 * @access private
152 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200153 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 function _set_platform()
155 {
156 if (is_array($this->platforms) AND count($this->platforms) > 0)
157 {
158 foreach ($this->platforms as $key => $val)
159 {
160 if (preg_match("|".preg_quote($key)."|i", $this->agent))
161 {
162 $this->platform = $val;
163 return TRUE;
164 }
165 }
166 }
167 $this->platform = 'Unknown Platform';
168 }
169
170 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 /**
173 * Set the Browser
174 *
175 * @access private
176 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200177 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 function _set_browser()
179 {
180 if (is_array($this->browsers) AND count($this->browsers) > 0)
181 {
182 foreach ($this->browsers as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200183 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
185 {
186 $this->is_browser = TRUE;
187 $this->version = $match[1];
188 $this->browser = $val;
189 $this->_set_mobile();
190 return TRUE;
191 }
192 }
193 }
194 return FALSE;
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 /**
200 * Set the Robot
201 *
202 * @access private
203 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200204 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 function _set_robot()
206 {
207 if (is_array($this->robots) AND count($this->robots) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200208 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 foreach ($this->robots as $key => $val)
210 {
211 if (preg_match("|".preg_quote($key)."|i", $this->agent))
212 {
213 $this->is_robot = TRUE;
214 $this->robot = $val;
215 return TRUE;
216 }
217 }
218 }
219 return FALSE;
220 }
221
222 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 /**
225 * Set the Mobile Device
226 *
227 * @access private
228 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200229 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 function _set_mobile()
231 {
232 if (is_array($this->mobiles) AND count($this->mobiles) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200233 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 foreach ($this->mobiles as $key => $val)
235 {
236 if (FALSE !== (strpos(strtolower($this->agent), $key)))
237 {
238 $this->is_mobile = TRUE;
239 $this->mobile = $val;
240 return TRUE;
241 }
242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 return FALSE;
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 /**
250 * Set the accepted languages
251 *
252 * @access private
253 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200254 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 function _set_languages()
256 {
257 if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
258 {
259 $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 $this->languages = explode(',', $languages);
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 if (count($this->languages) == 0)
265 {
266 $this->languages = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200267 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 /**
273 * Set the accepted character sets
274 *
275 * @access private
276 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200277 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 function _set_charsets()
Barry Mienydd671972010-10-04 16:33:58 +0200279 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
281 {
282 $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 $this->charsets = explode(',', $charsets);
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if (count($this->charsets) == 0)
288 {
289 $this->charsets = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
293 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 /**
296 * Is Browser
297 *
298 * @access public
299 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200300 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 function is_browser()
302 {
303 return $this->is_browser;
304 }
305
306 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 /**
309 * Is Robot
310 *
311 * @access public
312 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200313 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 function is_robot()
315 {
316 return $this->is_robot;
317 }
318
319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 /**
322 * Is Mobile
323 *
324 * @access public
325 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200326 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 function is_mobile()
328 {
329 return $this->is_mobile;
Barry Mienydd671972010-10-04 16:33:58 +0200330 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000331
332 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 /**
335 * Is this a referral from another site?
336 *
337 * @access public
338 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200339 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 function is_referral()
341 {
342 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
343 }
344
345 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 /**
348 * Agent String
349 *
350 * @access public
351 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200352 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 function agent_string()
354 {
355 return $this->agent;
356 }
357
358 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 /**
361 * Get Platform
362 *
363 * @access public
364 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200365 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 function platform()
367 {
368 return $this->platform;
369 }
370
371 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 /**
374 * Get Browser Name
375 *
376 * @access public
377 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200378 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 function browser()
380 {
381 return $this->browser;
382 }
383
384 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 /**
387 * Get the Browser Version
388 *
389 * @access public
390 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200391 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 function version()
393 {
394 return $this->version;
395 }
396
397 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 /**
400 * Get The Robot Name
401 *
402 * @access public
403 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200404 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 function robot()
406 {
407 return $this->robot;
408 }
409 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 /**
412 * Get the Mobile Device
413 *
414 * @access public
415 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200416 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 function mobile()
418 {
419 return $this->mobile;
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 /**
425 * Get the referrer
426 *
427 * @access public
428 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200429 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 function referrer()
431 {
432 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
433 }
434
435 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 /**
438 * Get the accepted languages
439 *
440 * @access public
441 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200442 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 function languages()
444 {
445 if (count($this->languages) == 0)
446 {
447 $this->_set_languages();
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 return $this->languages;
451 }
452
453 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 /**
456 * Get the accepted Character Sets
457 *
458 * @access public
459 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200460 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 function charsets()
462 {
463 if (count($this->charsets) == 0)
464 {
465 $this->_set_charsets();
466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 return $this->charsets;
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 /**
474 * Test for a particular language
475 *
476 * @access public
477 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200478 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 function accept_lang($lang = 'en')
480 {
481 return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 /**
487 * Test for a particular character set
488 *
489 * @access public
490 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200491 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 function accept_charset($charset = 'utf-8')
493 {
494 return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498}
499
500
501/* End of file User_agent.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000502/* Location: ./system/libraries/User_agent.php */