blob: 9bab8666edb5b99bc0f49455da9a624cd563f3ce [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreeve9ccf742011-12-25 17:30:10 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreeve9ccf742011-12-25 17:30:10 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * User Agent Class
31 *
cenk115e9982011-09-27 10:07:53 +030032 * Identifies the platform, browser, robot, or mobile device of the browsing agent
Derek Allard2067d1a2008-11-13 22:59:24 +000033 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category User Agent
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/user_agent.html
39 */
40class CI_User_agent {
41
Timothy Warren0688ac92012-04-20 10:25:04 -040042 /**
43 * Current user-agent
44 *
45 * @var string
46 */
47 public $agent = NULL;
Barry Mienydd671972010-10-04 16:33:58 +020048
Timothy Warren0688ac92012-04-20 10:25:04 -040049 /**
50 * Flag for if the user-agent belongs to a browser
51 *
52 * @var bool
53 */
54 public $is_browser = FALSE;
Andrey Andreevca7d8222012-05-11 10:59:09 +030055
Timothy Warren0688ac92012-04-20 10:25:04 -040056 /**
57 * Flag for if the user-agent is a robot
58 *
59 * @var bool
60 */
61 public $is_robot = FALSE;
Andrey Andreevca7d8222012-05-11 10:59:09 +030062
Timothy Warren0688ac92012-04-20 10:25:04 -040063 /**
64 * Flag for if the user-agent is a mobile browser
65 *
66 * @var bool
67 */
68 public $is_mobile = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000069
Timothy Warren0688ac92012-04-20 10:25:04 -040070 /**
71 * Languages accepted by the current user agent
72 *
73 * @var array
74 */
75 public $languages = array();
Andrey Andreevca7d8222012-05-11 10:59:09 +030076
Timothy Warren0688ac92012-04-20 10:25:04 -040077 /**
78 * Character sets accepted by the current user agent
79 *
80 * @var array
81 */
82 public $charsets = array();
Barry Mienydd671972010-10-04 16:33:58 +020083
Timothy Warren0688ac92012-04-20 10:25:04 -040084 /**
85 * List of platforms to compare against current user agent
86 *
87 * @var array
88 */
89 public $platforms = array();
Andrey Andreevca7d8222012-05-11 10:59:09 +030090
Timothy Warren0688ac92012-04-20 10:25:04 -040091 /**
92 * List of browsers to compare against current user agent
93 *
94 * @var array
95 */
96 public $browsers = array();
Andrey Andreevca7d8222012-05-11 10:59:09 +030097
Timothy Warren0688ac92012-04-20 10:25:04 -040098 /**
99 * List of mobile browsers to compare against current user agent
100 *
101 * @var array
102 */
103 public $mobiles = array();
Andrey Andreevca7d8222012-05-11 10:59:09 +0300104
Timothy Warren0688ac92012-04-20 10:25:04 -0400105 /**
106 * List of robots to compare against current user agent
107 *
108 * @var array
109 */
110 public $robots = array();
Barry Mienydd671972010-10-04 16:33:58 +0200111
Timothy Warren0688ac92012-04-20 10:25:04 -0400112 /**
113 * Current user-agent platform
114 *
115 * @var string
116 */
117 public $platform = '';
Andrey Andreevca7d8222012-05-11 10:59:09 +0300118
Timothy Warren0688ac92012-04-20 10:25:04 -0400119 /**
120 * Current user-agent browser
121 *
122 * @var string
123 */
124 public $browser = '';
Andrey Andreevca7d8222012-05-11 10:59:09 +0300125
Timothy Warren0688ac92012-04-20 10:25:04 -0400126 /**
127 * Current user-agent version
128 *
129 * @var string
130 */
131 public $version = '';
Andrey Andreevca7d8222012-05-11 10:59:09 +0300132
Timothy Warren0688ac92012-04-20 10:25:04 -0400133 /**
134 * Current user-agent mobile name
135 *
136 * @var string
137 */
138 public $mobile = '';
Andrey Andreevca7d8222012-05-11 10:59:09 +0300139
Timothy Warren0688ac92012-04-20 10:25:04 -0400140 /**
141 * Current user-agent robot name
142 *
143 * @var string
144 */
145 public $robot = '';
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 /**
Andrey Andreeva9938a02014-01-17 14:55:56 +0200148 * HTTP Referer
149 *
150 * @var mixed
151 */
152 public $referer;
153
154 // --------------------------------------------------------------------
155
156 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * Constructor
158 *
159 * Sets the User Agent and runs the compilation routine
160 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200162 */
Greg Akera9263282010-11-10 15:26:43 -0600163 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 if (isset($_SERVER['HTTP_USER_AGENT']))
166 {
167 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
vlakoff1228fe22013-01-14 01:30:09 +0100170 if ($this->agent !== NULL && $this->_load_agent_file())
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300172 $this->_compile_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300175 log_message('debug', 'User Agent Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 /**
181 * Compile the User Agent Data
182 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200184 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200185 protected function _load_agent_file()
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreev06879112013-01-29 15:05:02 +0200187 if (($found = file_exists(APPPATH.'config/user_agents.php')))
Greg Akerd96f8822011-12-27 16:23:47 -0600188 {
189 include(APPPATH.'config/user_agents.php');
190 }
Andrey Andreev06879112013-01-29 15:05:02 +0200191
192 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'))
193 {
194 include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php');
195 $found = TRUE;
196 }
197
198 if ($found !== TRUE)
bubbafoley0ea04142011-03-17 14:55:41 -0500199 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 return FALSE;
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 $return = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 if (isset($platforms))
206 {
207 $this->platforms = $platforms;
208 unset($platforms);
209 $return = TRUE;
210 }
211
212 if (isset($browsers))
213 {
214 $this->browsers = $browsers;
215 unset($browsers);
216 $return = TRUE;
217 }
218
219 if (isset($mobiles))
220 {
221 $this->mobiles = $mobiles;
222 unset($mobiles);
223 $return = TRUE;
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 if (isset($robots))
227 {
228 $this->robots = $robots;
229 unset($robots);
230 $return = TRUE;
231 }
232
233 return $return;
234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 /**
239 * Compile the User Agent Data
240 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200242 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200243 protected function _compile_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
245 $this->_set_platform();
Barry Mienydd671972010-10-04 16:33:58 +0200246
Phil Sturgeon2c547df2011-08-10 08:36:02 -0600247 foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 if ($this->$function() === TRUE)
250 {
251 break;
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 /**
259 * Set the Platform
260 *
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300261 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200262 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200263 protected function _set_platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300265 if (is_array($this->platforms) && count($this->platforms) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
267 foreach ($this->platforms as $key => $val)
268 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300269 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
271 $this->platform = $val;
272 return TRUE;
273 }
274 }
275 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 $this->platform = 'Unknown Platform';
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300278 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280
281 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 /**
284 * Set the Browser
285 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200287 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200288 protected function _set_browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300290 if (is_array($this->browsers) && count($this->browsers) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 foreach ($this->browsers as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200293 {
Andrey Andreev10925d22014-01-09 00:16:46 +0200294 if (preg_match('|'.$key.'.*?([0-9\.]+)|i', $this->agent, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
296 $this->is_browser = TRUE;
297 $this->version = $match[1];
298 $this->browser = $val;
299 $this->_set_mobile();
300 return TRUE;
301 }
302 }
303 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 return FALSE;
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 /**
311 * Set the Robot
312 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200314 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200315 protected function _set_robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300317 if (is_array($this->robots) && count($this->robots) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200318 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 foreach ($this->robots as $key => $val)
320 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300321 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 $this->is_robot = TRUE;
324 $this->robot = $val;
Eric Roberts001a7642012-04-14 16:41:13 -0500325 $this->_set_mobile();
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 return TRUE;
327 }
328 }
329 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 return FALSE;
332 }
333
334 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 /**
337 * Set the Mobile Device
338 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200340 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200341 protected function _set_mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300343 if (is_array($this->mobiles) && count($this->mobiles) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200344 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 foreach ($this->mobiles as $key => $val)
346 {
Andrey Andreevca7d8222012-05-11 10:59:09 +0300347 if (FALSE !== (stripos($this->agent, $key)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 $this->is_mobile = TRUE;
350 $this->mobile = $val;
351 return TRUE;
352 }
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 return FALSE;
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 /**
362 * Set the accepted languages
363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200365 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200366 protected function _set_languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300368 if ((count($this->languages) === 0) && ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200370 $this->languages = explode(',', preg_replace('/(;\s?q=[0-9\.]+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE']))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200373 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
375 $this->languages = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200376 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 /**
382 * Set the accepted character sets
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200385 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200386 protected function _set_charsets()
Barry Mienydd671972010-10-04 16:33:58 +0200387 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300388 if ((count($this->charsets) === 0) && ! empty($_SERVER['HTTP_ACCEPT_CHARSET']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200390 $this->charsets = explode(',', preg_replace('/(;\s?q=.+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET']))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200393 if (count($this->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
395 $this->charsets = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200396 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
398
399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 /**
402 * Is Browser
403 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400404 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200406 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000407 public function is_browser($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000409 if ( ! $this->is_browser)
410 {
411 return FALSE;
412 }
413
414 // No need to be specific, it's a browser
415 if ($key === NULL)
416 {
417 return TRUE;
418 }
419
420 // Check for a specific browser
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300421 return (isset($this->browsers[$key]) && $this->browser === $this->browsers[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423
424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
427 * Is Robot
428 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400429 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200431 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000432 public function is_robot($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000434 if ( ! $this->is_robot)
435 {
436 return FALSE;
437 }
438
439 // No need to be specific, it's a robot
440 if ($key === NULL)
441 {
442 return TRUE;
443 }
444
445 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300446 return (isset($this->robots[$key]) && $this->robot === $this->robots[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
448
449 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 /**
452 * Is Mobile
453 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400454 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200456 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000457 public function is_mobile($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000459 if ( ! $this->is_mobile)
460 {
461 return FALSE;
462 }
463
464 // No need to be specific, it's a mobile
465 if ($key === NULL)
466 {
467 return TRUE;
468 }
469
470 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300471 return (isset($this->mobiles[$key]) && $this->mobile === $this->mobiles[$key]);
Barry Mienydd671972010-10-04 16:33:58 +0200472 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000473
474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 /**
477 * Is this a referral from another site?
478 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200480 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000481 public function is_referral()
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200483 if ( ! isset($this->referer))
Andrey Andreeva0836b92012-10-24 22:03:42 +0300484 {
vlakoffc05ab732013-07-20 20:27:57 +0200485 if (empty($_SERVER['HTTP_REFERER']))
486 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200487 $this->referer = FALSE;
vlakoffc05ab732013-07-20 20:27:57 +0200488 }
489 else
490 {
491 $referer_host = @parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
492 $own_host = parse_url(config_item('base_url'), PHP_URL_HOST);
493
Andrey Andreeva9938a02014-01-17 14:55:56 +0200494 $this->referer = ($referer_host && $referer_host !== $own_host);
vlakoffc05ab732013-07-20 20:27:57 +0200495 }
Andrey Andreeva0836b92012-10-24 22:03:42 +0300496 }
497
Andrey Andreeva9938a02014-01-17 14:55:56 +0200498 return $this->referer;
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500
501 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 /**
504 * Agent String
505 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200507 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000508 public function agent_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
510 return $this->agent;
511 }
512
513 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 /**
516 * Get Platform
517 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200519 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000520 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
522 return $this->platform;
523 }
524
525 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 /**
528 * Get Browser Name
529 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200531 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000532 public function browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 return $this->browser;
535 }
536
537 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 /**
540 * Get the Browser Version
541 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200543 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000544 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
546 return $this->version;
547 }
548
549 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 /**
552 * Get The Robot Name
553 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200555 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000556 public function robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
558 return $this->robot;
559 }
560 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 /**
563 * Get the Mobile Device
564 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200566 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000567 public function mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 return $this->mobile;
570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 /**
575 * Get the referrer
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200578 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000579 public function referrer()
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300581 return empty($_SERVER['HTTP_REFERER']) ? '' : trim($_SERVER['HTTP_REFERER']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
583
584 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 /**
587 * Get the accepted languages
588 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200590 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000591 public function languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200593 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 $this->_set_languages();
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 return $this->languages;
599 }
600
601 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 /**
604 * Get the accepted Character Sets
605 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200607 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000608 public function charsets()
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200610 if (count($this->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 $this->_set_charsets();
613 }
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 return $this->charsets;
616 }
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 /**
621 * Test for a particular language
622 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400623 * @param string $lang
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200625 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000626 public function accept_lang($lang = 'en')
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300628 return in_array(strtolower($lang), $this->languages(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 /**
634 * Test for a particular character set
635 *
Andrey Andreevca7d8222012-05-11 10:59:09 +0300636 * @param string $charset
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200638 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000639 public function accept_charset($charset = 'utf-8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300641 return in_array(strtolower($charset), $this->charsets(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 }
Barry Mienydd671972010-10-04 16:33:58 +0200643
Andrey Andreev27e91a02014-01-09 01:00:48 +0200644 // --------------------------------------------------------------------
645
646 /**
647 * Parse a custom user-agent string
648 *
649 * @param string $string
650 * @return void
651 */
652 public function parse($string)
653 {
654 // Reset values
655 $this->is_browser = FALSE;
656 $this->is_robot = FALSE;
657 $this->is_mobile = FALSE;
658 $this->browser = '';
659 $this->version = '';
660 $this->mobile = '';
661 $this->robot = '';
662
663 // Set the new user-agent string and parse it, unless empty
664 $this->agent = $string;
665
666 if ( ! empty($string))
667 {
668 $this->_compile_data();
669 }
670 }
671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672}
673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674/* End of file User_agent.php */
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300675/* Location: ./system/libraries/User_agent.php */