blob: 53cd7b2fd4b81f0e6e8ad8f96a773ef1481a1ec1 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
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 */
Greg Akera9263282010-11-10 15:26:43 -060059 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000060 {
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 */
Phil Sturgeondac1b462011-01-06 17:40:10 +000085 private function _load_agent_file()
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
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 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000133 private function _compile_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
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 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000154 private function _set_platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
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 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000178 private function _set_browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
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 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000205 private function _set_robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
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 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000230 private function _set_mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
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 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000255 private function _set_languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
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 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000278 private 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 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000301 public function is_browser($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000303 if ( ! $this->is_browser)
304 {
305 return FALSE;
306 }
307
308 // No need to be specific, it's a browser
309 if ($key === NULL)
310 {
311 return TRUE;
312 }
313
314 // Check for a specific browser
315 return array_key_exists($key, $this->browsers) AND $this->browser === $this->browsers[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317
318 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 /**
321 * Is Robot
322 *
323 * @access public
324 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200325 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000326 public function is_robot($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000328 if ( ! $this->is_robot)
329 {
330 return FALSE;
331 }
332
333 // No need to be specific, it's a robot
334 if ($key === NULL)
335 {
336 return TRUE;
337 }
338
339 // Check for a specific robot
340 return array_key_exists($key, $this->robots) AND $this->robot === $this->robots[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 }
342
343 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 /**
346 * Is Mobile
347 *
348 * @access public
349 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200350 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000351 public function is_mobile($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000353 if ( ! $this->is_mobile)
354 {
355 return FALSE;
356 }
357
358 // No need to be specific, it's a mobile
359 if ($key === NULL)
360 {
361 return TRUE;
362 }
363
364 // Check for a specific robot
365 return array_key_exists($key, $this->mobiles) AND $this->mobile === $this->mobiles[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200366 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000367
368 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 /**
371 * Is this a referral from another site?
372 *
373 * @access public
374 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200375 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000376 public function is_referral()
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000378 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
380
381 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 /**
384 * Agent String
385 *
386 * @access public
387 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200388 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000389 public function agent_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 return $this->agent;
392 }
393
394 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 /**
397 * Get Platform
398 *
399 * @access public
400 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200401 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000402 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 return $this->platform;
405 }
406
407 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 /**
410 * Get Browser Name
411 *
412 * @access public
413 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200414 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000415 public function browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 return $this->browser;
418 }
419
420 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 /**
423 * Get the Browser Version
424 *
425 * @access public
426 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200427 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000428 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
430 return $this->version;
431 }
432
433 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 /**
436 * Get The Robot Name
437 *
438 * @access public
439 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200440 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000441 public function robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 return $this->robot;
444 }
445 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 /**
448 * Get the Mobile Device
449 *
450 * @access public
451 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200452 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000453 public function mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
455 return $this->mobile;
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 /**
461 * Get the referrer
462 *
463 * @access public
464 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200465 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000466 public function referrer()
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
468 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
469 }
470
471 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 /**
474 * Get the accepted languages
475 *
476 * @access public
477 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200478 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000479 public function languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
481 if (count($this->languages) == 0)
482 {
483 $this->_set_languages();
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 return $this->languages;
487 }
488
489 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 /**
492 * Get the accepted Character Sets
493 *
494 * @access public
495 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200496 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000497 public function charsets()
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
499 if (count($this->charsets) == 0)
500 {
501 $this->_set_charsets();
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 return $this->charsets;
505 }
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 /**
510 * Test for a particular language
511 *
512 * @access public
513 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200514 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000515 public function accept_lang($lang = 'en')
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000517 return (in_array(strtolower($lang), $this->languages(), TRUE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 /**
523 * Test for a particular character set
524 *
525 * @access public
526 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200527 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000528 public function accept_charset($charset = 'utf-8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000530 return (in_array(strtolower($charset), $this->charsets(), TRUE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533}
534
535
536/* End of file User_agent.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000537/* Location: ./system/libraries/User_agent.php */