blob: b1a011f19f51be3b22e120ee5d0cfae2cda32c5c [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreeve9ccf742011-12-25 17:30:10 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreeve9ccf742011-12-25 17:30:10 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * User Agent Class
42 *
cenk115e9982011-09-27 10:07:53 +030043 * Identifies the platform, browser, robot, or mobile device of the browsing agent
Derek Allard2067d1a2008-11-13 22:59:24 +000044 *
45 * @package CodeIgniter
46 * @subpackage Libraries
47 * @category User Agent
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/libraries/user_agent.html
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
51class CI_User_agent {
52
Timothy Warren0688ac92012-04-20 10:25:04 -040053 /**
54 * Current user-agent
55 *
56 * @var string
57 */
58 public $agent = NULL;
Barry Mienydd671972010-10-04 16:33:58 +020059
Timothy Warren0688ac92012-04-20 10:25:04 -040060 /**
61 * Flag for if the user-agent belongs to a browser
62 *
63 * @var bool
64 */
65 public $is_browser = FALSE;
Andrey Andreevca7d8222012-05-11 10:59:09 +030066
Timothy Warren0688ac92012-04-20 10:25:04 -040067 /**
68 * Flag for if the user-agent is a robot
69 *
70 * @var bool
71 */
72 public $is_robot = FALSE;
Andrey Andreevca7d8222012-05-11 10:59:09 +030073
Timothy Warren0688ac92012-04-20 10:25:04 -040074 /**
75 * Flag for if the user-agent is a mobile browser
76 *
77 * @var bool
78 */
79 public $is_mobile = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000080
Timothy Warren0688ac92012-04-20 10:25:04 -040081 /**
82 * Languages accepted by the current user agent
83 *
84 * @var array
85 */
86 public $languages = array();
Andrey Andreevca7d8222012-05-11 10:59:09 +030087
Timothy Warren0688ac92012-04-20 10:25:04 -040088 /**
89 * Character sets accepted by the current user agent
90 *
91 * @var array
92 */
93 public $charsets = array();
Barry Mienydd671972010-10-04 16:33:58 +020094
Timothy Warren0688ac92012-04-20 10:25:04 -040095 /**
96 * List of platforms to compare against current user agent
97 *
98 * @var array
99 */
100 public $platforms = array();
Andrey Andreevca7d8222012-05-11 10:59:09 +0300101
Timothy Warren0688ac92012-04-20 10:25:04 -0400102 /**
103 * List of browsers to compare against current user agent
104 *
105 * @var array
106 */
107 public $browsers = array();
Andrey Andreevca7d8222012-05-11 10:59:09 +0300108
Timothy Warren0688ac92012-04-20 10:25:04 -0400109 /**
110 * List of mobile browsers to compare against current user agent
111 *
112 * @var array
113 */
114 public $mobiles = array();
Andrey Andreevca7d8222012-05-11 10:59:09 +0300115
Timothy Warren0688ac92012-04-20 10:25:04 -0400116 /**
117 * List of robots to compare against current user agent
118 *
119 * @var array
120 */
121 public $robots = array();
Barry Mienydd671972010-10-04 16:33:58 +0200122
Timothy Warren0688ac92012-04-20 10:25:04 -0400123 /**
124 * Current user-agent platform
125 *
126 * @var string
127 */
128 public $platform = '';
Andrey Andreevca7d8222012-05-11 10:59:09 +0300129
Timothy Warren0688ac92012-04-20 10:25:04 -0400130 /**
131 * Current user-agent browser
132 *
133 * @var string
134 */
135 public $browser = '';
Andrey Andreevca7d8222012-05-11 10:59:09 +0300136
Timothy Warren0688ac92012-04-20 10:25:04 -0400137 /**
138 * Current user-agent version
139 *
140 * @var string
141 */
142 public $version = '';
Andrey Andreevca7d8222012-05-11 10:59:09 +0300143
Timothy Warren0688ac92012-04-20 10:25:04 -0400144 /**
145 * Current user-agent mobile name
146 *
147 * @var string
148 */
149 public $mobile = '';
Andrey Andreevca7d8222012-05-11 10:59:09 +0300150
Timothy Warren0688ac92012-04-20 10:25:04 -0400151 /**
152 * Current user-agent robot name
153 *
154 * @var string
155 */
156 public $robot = '';
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 /**
Andrey Andreeva9938a02014-01-17 14:55:56 +0200159 * HTTP Referer
160 *
161 * @var mixed
162 */
163 public $referer;
164
165 // --------------------------------------------------------------------
166
167 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * Constructor
169 *
170 * Sets the User Agent and runs the compilation routine
171 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200173 */
Greg Akera9263282010-11-10 15:26:43 -0600174 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Andrey Andreevb0104752016-07-19 14:36:20 +0300176 $this->_load_agent_file();
177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 if (isset($_SERVER['HTTP_USER_AGENT']))
179 {
180 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300181 $this->_compile_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Andrey Andreev90726b82015-01-20 12:39:22 +0200184 log_message('info', 'User Agent Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 /**
190 * Compile the User Agent Data
191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200193 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200194 protected function _load_agent_file()
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreev06879112013-01-29 15:05:02 +0200196 if (($found = file_exists(APPPATH.'config/user_agents.php')))
Greg Akerd96f8822011-12-27 16:23:47 -0600197 {
198 include(APPPATH.'config/user_agents.php');
199 }
Andrey Andreev06879112013-01-29 15:05:02 +0200200
201 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'))
202 {
203 include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php');
204 $found = TRUE;
205 }
206
207 if ($found !== TRUE)
bubbafoley0ea04142011-03-17 14:55:41 -0500208 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 return FALSE;
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 $return = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 if (isset($platforms))
215 {
216 $this->platforms = $platforms;
217 unset($platforms);
218 $return = TRUE;
219 }
220
221 if (isset($browsers))
222 {
223 $this->browsers = $browsers;
224 unset($browsers);
225 $return = TRUE;
226 }
227
228 if (isset($mobiles))
229 {
230 $this->mobiles = $mobiles;
231 unset($mobiles);
232 $return = TRUE;
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 if (isset($robots))
236 {
237 $this->robots = $robots;
238 unset($robots);
239 $return = TRUE;
240 }
241
242 return $return;
243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 /**
248 * Compile the User Agent Data
249 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200251 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200252 protected function _compile_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 $this->_set_platform();
Barry Mienydd671972010-10-04 16:33:58 +0200255
Phil Sturgeon2c547df2011-08-10 08:36:02 -0600256 foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 if ($this->$function() === TRUE)
259 {
260 break;
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 /**
268 * Set the Platform
269 *
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300270 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200271 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200272 protected function _set_platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300274 if (is_array($this->platforms) && count($this->platforms) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 foreach ($this->platforms as $key => $val)
277 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300278 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 $this->platform = $val;
281 return TRUE;
282 }
283 }
284 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 $this->platform = 'Unknown Platform';
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300287 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
289
290 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 /**
293 * Set the Browser
294 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200296 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200297 protected function _set_browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300299 if (is_array($this->browsers) && count($this->browsers) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 foreach ($this->browsers as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200302 {
Andrey Andreev10925d22014-01-09 00:16:46 +0200303 if (preg_match('|'.$key.'.*?([0-9\.]+)|i', $this->agent, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
305 $this->is_browser = TRUE;
306 $this->version = $match[1];
307 $this->browser = $val;
308 $this->_set_mobile();
309 return TRUE;
310 }
311 }
312 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 return FALSE;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 /**
320 * Set the Robot
321 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200323 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200324 protected function _set_robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300326 if (is_array($this->robots) && count($this->robots) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200327 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 foreach ($this->robots as $key => $val)
329 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300330 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $this->is_robot = TRUE;
333 $this->robot = $val;
Eric Roberts001a7642012-04-14 16:41:13 -0500334 $this->_set_mobile();
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 return TRUE;
336 }
337 }
338 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 return FALSE;
341 }
342
343 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 /**
346 * Set the Mobile Device
347 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200349 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200350 protected function _set_mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300352 if (is_array($this->mobiles) && count($this->mobiles) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200353 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 foreach ($this->mobiles as $key => $val)
355 {
Andrey Andreevca7d8222012-05-11 10:59:09 +0300356 if (FALSE !== (stripos($this->agent, $key)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
358 $this->is_mobile = TRUE;
359 $this->mobile = $val;
360 return TRUE;
361 }
362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 return FALSE;
366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 /**
371 * Set the accepted languages
372 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200374 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200375 protected function _set_languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300377 if ((count($this->languages) === 0) && ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200379 $this->languages = explode(',', preg_replace('/(;\s?q=[0-9\.]+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE']))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
Barry Mienydd671972010-10-04 16:33:58 +0200381
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200382 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 $this->languages = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200385 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 /**
391 * Set the accepted character sets
392 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200394 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200395 protected function _set_charsets()
Barry Mienydd671972010-10-04 16:33:58 +0200396 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300397 if ((count($this->charsets) === 0) && ! empty($_SERVER['HTTP_ACCEPT_CHARSET']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200399 $this->charsets = explode(',', preg_replace('/(;\s?q=.+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET']))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200402 if (count($this->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 $this->charsets = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200405 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407
408 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 /**
411 * Is Browser
412 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400413 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200415 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000416 public function is_browser($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000418 if ( ! $this->is_browser)
419 {
420 return FALSE;
421 }
422
423 // No need to be specific, it's a browser
424 if ($key === NULL)
425 {
426 return TRUE;
427 }
428
429 // Check for a specific browser
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300430 return (isset($this->browsers[$key]) && $this->browser === $this->browsers[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432
433 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 /**
436 * Is Robot
437 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400438 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200440 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000441 public function is_robot($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000443 if ( ! $this->is_robot)
444 {
445 return FALSE;
446 }
447
448 // No need to be specific, it's a robot
449 if ($key === NULL)
450 {
451 return TRUE;
452 }
453
454 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300455 return (isset($this->robots[$key]) && $this->robot === $this->robots[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
457
458 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 /**
461 * Is Mobile
462 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400463 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200465 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000466 public function is_mobile($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000468 if ( ! $this->is_mobile)
469 {
470 return FALSE;
471 }
472
473 // No need to be specific, it's a mobile
474 if ($key === NULL)
475 {
476 return TRUE;
477 }
478
479 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300480 return (isset($this->mobiles[$key]) && $this->mobile === $this->mobiles[$key]);
Barry Mienydd671972010-10-04 16:33:58 +0200481 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000482
483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 /**
486 * Is this a referral from another site?
487 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200489 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000490 public function is_referral()
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200492 if ( ! isset($this->referer))
Andrey Andreeva0836b92012-10-24 22:03:42 +0300493 {
vlakoffc05ab732013-07-20 20:27:57 +0200494 if (empty($_SERVER['HTTP_REFERER']))
495 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200496 $this->referer = FALSE;
vlakoffc05ab732013-07-20 20:27:57 +0200497 }
498 else
499 {
500 $referer_host = @parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
501 $own_host = parse_url(config_item('base_url'), PHP_URL_HOST);
502
Andrey Andreeva9938a02014-01-17 14:55:56 +0200503 $this->referer = ($referer_host && $referer_host !== $own_host);
vlakoffc05ab732013-07-20 20:27:57 +0200504 }
Andrey Andreeva0836b92012-10-24 22:03:42 +0300505 }
506
Andrey Andreeva9938a02014-01-17 14:55:56 +0200507 return $this->referer;
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
509
510 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 /**
513 * Agent String
514 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200516 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000517 public function agent_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 return $this->agent;
520 }
521
522 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 /**
525 * Get Platform
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200528 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000529 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
531 return $this->platform;
532 }
533
534 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 /**
537 * Get Browser Name
538 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200540 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000541 public function browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
543 return $this->browser;
544 }
545
546 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 /**
549 * Get the Browser Version
550 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200552 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000553 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
555 return $this->version;
556 }
557
558 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 /**
561 * Get The Robot Name
562 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200564 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000565 public function robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
567 return $this->robot;
568 }
569 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 /**
572 * Get the Mobile Device
573 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200575 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000576 public function mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 return $this->mobile;
579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 /**
584 * Get the referrer
585 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200587 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000588 public function referrer()
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300590 return empty($_SERVER['HTTP_REFERER']) ? '' : trim($_SERVER['HTTP_REFERER']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 }
592
593 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 /**
596 * Get the accepted languages
597 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200599 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000600 public function languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200602 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 $this->_set_languages();
605 }
Barry Mienydd671972010-10-04 16:33:58 +0200606
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 return $this->languages;
608 }
609
610 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 /**
613 * Get the accepted Character Sets
614 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200616 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000617 public function charsets()
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200619 if (count($this->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
621 $this->_set_charsets();
622 }
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 return $this->charsets;
625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 /**
630 * Test for a particular language
631 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400632 * @param string $lang
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200634 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000635 public function accept_lang($lang = 'en')
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300637 return in_array(strtolower($lang), $this->languages(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 /**
643 * Test for a particular character set
644 *
Andrey Andreevca7d8222012-05-11 10:59:09 +0300645 * @param string $charset
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200647 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000648 public function accept_charset($charset = 'utf-8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300650 return in_array(strtolower($charset), $this->charsets(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Andrey Andreev27e91a02014-01-09 01:00:48 +0200653 // --------------------------------------------------------------------
654
655 /**
656 * Parse a custom user-agent string
657 *
658 * @param string $string
659 * @return void
660 */
661 public function parse($string)
662 {
663 // Reset values
664 $this->is_browser = FALSE;
665 $this->is_robot = FALSE;
666 $this->is_mobile = FALSE;
667 $this->browser = '';
668 $this->version = '';
669 $this->mobile = '';
670 $this->robot = '';
671
672 // Set the new user-agent string and parse it, unless empty
673 $this->agent = $string;
674
675 if ( ! empty($string))
676 {
677 $this->_compile_data();
678 }
679 }
680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681}