blob: f28fda80c6c2c0172951b0df2165219d0856b506 [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 *
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/libraries/user_agent.html
50 */
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 {
176 if (isset($_SERVER['HTTP_USER_AGENT']))
177 {
178 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
vlakoff1228fe22013-01-14 01:30:09 +0100181 if ($this->agent !== NULL && $this->_load_agent_file())
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300183 $this->_compile_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300186 log_message('debug', 'User Agent Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 /**
192 * Compile the User Agent Data
193 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200195 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200196 protected function _load_agent_file()
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Andrey Andreev06879112013-01-29 15:05:02 +0200198 if (($found = file_exists(APPPATH.'config/user_agents.php')))
Greg Akerd96f8822011-12-27 16:23:47 -0600199 {
200 include(APPPATH.'config/user_agents.php');
201 }
Andrey Andreev06879112013-01-29 15:05:02 +0200202
203 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'))
204 {
205 include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php');
206 $found = TRUE;
207 }
208
209 if ($found !== TRUE)
bubbafoley0ea04142011-03-17 14:55:41 -0500210 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 return FALSE;
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 $return = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 if (isset($platforms))
217 {
218 $this->platforms = $platforms;
219 unset($platforms);
220 $return = TRUE;
221 }
222
223 if (isset($browsers))
224 {
225 $this->browsers = $browsers;
226 unset($browsers);
227 $return = TRUE;
228 }
229
230 if (isset($mobiles))
231 {
232 $this->mobiles = $mobiles;
233 unset($mobiles);
234 $return = TRUE;
235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 if (isset($robots))
238 {
239 $this->robots = $robots;
240 unset($robots);
241 $return = TRUE;
242 }
243
244 return $return;
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 * Compile the User Agent Data
251 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200253 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200254 protected function _compile_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
256 $this->_set_platform();
Barry Mienydd671972010-10-04 16:33:58 +0200257
Phil Sturgeon2c547df2011-08-10 08:36:02 -0600258 foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 if ($this->$function() === TRUE)
261 {
262 break;
263 }
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 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 /**
270 * Set the Platform
271 *
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300272 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200273 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200274 protected function _set_platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300276 if (is_array($this->platforms) && count($this->platforms) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 foreach ($this->platforms as $key => $val)
279 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300280 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 $this->platform = $val;
283 return TRUE;
284 }
285 }
286 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 $this->platform = 'Unknown Platform';
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300289 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
291
292 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 /**
295 * Set the Browser
296 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200298 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200299 protected function _set_browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300301 if (is_array($this->browsers) && count($this->browsers) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
303 foreach ($this->browsers as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200304 {
Andrey Andreev10925d22014-01-09 00:16:46 +0200305 if (preg_match('|'.$key.'.*?([0-9\.]+)|i', $this->agent, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
307 $this->is_browser = TRUE;
308 $this->version = $match[1];
309 $this->browser = $val;
310 $this->_set_mobile();
311 return TRUE;
312 }
313 }
314 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 return FALSE;
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 /**
322 * Set the Robot
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200325 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200326 protected function _set_robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300328 if (is_array($this->robots) && count($this->robots) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200329 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 foreach ($this->robots as $key => $val)
331 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300332 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $this->is_robot = TRUE;
335 $this->robot = $val;
Eric Roberts001a7642012-04-14 16:41:13 -0500336 $this->_set_mobile();
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 return TRUE;
338 }
339 }
340 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 return FALSE;
343 }
344
345 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 /**
348 * Set the Mobile Device
349 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200351 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200352 protected function _set_mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300354 if (is_array($this->mobiles) && count($this->mobiles) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200355 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 foreach ($this->mobiles as $key => $val)
357 {
Andrey Andreevca7d8222012-05-11 10:59:09 +0300358 if (FALSE !== (stripos($this->agent, $key)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 $this->is_mobile = TRUE;
361 $this->mobile = $val;
362 return TRUE;
363 }
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 return FALSE;
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 /**
373 * Set the accepted languages
374 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200376 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200377 protected function _set_languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300379 if ((count($this->languages) === 0) && ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200381 $this->languages = explode(',', preg_replace('/(;\s?q=[0-9\.]+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE']))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200384 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 $this->languages = array('Undefined');
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 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 /**
393 * Set the accepted character sets
394 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200396 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200397 protected function _set_charsets()
Barry Mienydd671972010-10-04 16:33:58 +0200398 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300399 if ((count($this->charsets) === 0) && ! empty($_SERVER['HTTP_ACCEPT_CHARSET']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200401 $this->charsets = explode(',', preg_replace('/(;\s?q=.+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET']))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200404 if (count($this->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 $this->charsets = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200407 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409
410 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 /**
413 * Is Browser
414 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400415 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200417 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000418 public function is_browser($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000420 if ( ! $this->is_browser)
421 {
422 return FALSE;
423 }
424
425 // No need to be specific, it's a browser
426 if ($key === NULL)
427 {
428 return TRUE;
429 }
430
431 // Check for a specific browser
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300432 return (isset($this->browsers[$key]) && $this->browser === $this->browsers[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434
435 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 /**
438 * Is Robot
439 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400440 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200442 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000443 public function is_robot($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000445 if ( ! $this->is_robot)
446 {
447 return FALSE;
448 }
449
450 // No need to be specific, it's a robot
451 if ($key === NULL)
452 {
453 return TRUE;
454 }
455
456 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300457 return (isset($this->robots[$key]) && $this->robot === $this->robots[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
459
460 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 /**
463 * Is Mobile
464 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400465 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200467 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000468 public function is_mobile($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000470 if ( ! $this->is_mobile)
471 {
472 return FALSE;
473 }
474
475 // No need to be specific, it's a mobile
476 if ($key === NULL)
477 {
478 return TRUE;
479 }
480
481 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300482 return (isset($this->mobiles[$key]) && $this->mobile === $this->mobiles[$key]);
Barry Mienydd671972010-10-04 16:33:58 +0200483 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000484
485 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 /**
488 * Is this a referral from another site?
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200491 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000492 public function is_referral()
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200494 if ( ! isset($this->referer))
Andrey Andreeva0836b92012-10-24 22:03:42 +0300495 {
vlakoffc05ab732013-07-20 20:27:57 +0200496 if (empty($_SERVER['HTTP_REFERER']))
497 {
Andrey Andreeva9938a02014-01-17 14:55:56 +0200498 $this->referer = FALSE;
vlakoffc05ab732013-07-20 20:27:57 +0200499 }
500 else
501 {
502 $referer_host = @parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
503 $own_host = parse_url(config_item('base_url'), PHP_URL_HOST);
504
Andrey Andreeva9938a02014-01-17 14:55:56 +0200505 $this->referer = ($referer_host && $referer_host !== $own_host);
vlakoffc05ab732013-07-20 20:27:57 +0200506 }
Andrey Andreeva0836b92012-10-24 22:03:42 +0300507 }
508
Andrey Andreeva9938a02014-01-17 14:55:56 +0200509 return $this->referer;
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
512 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 /**
515 * Agent String
516 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200518 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000519 public function agent_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
521 return $this->agent;
522 }
523
524 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 /**
527 * Get Platform
528 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200530 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000531 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
533 return $this->platform;
534 }
535
536 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 /**
539 * Get Browser Name
540 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200542 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000543 public function browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
545 return $this->browser;
546 }
547
548 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 /**
551 * Get the Browser Version
552 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200554 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000555 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
557 return $this->version;
558 }
559
560 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 /**
563 * Get The Robot Name
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 robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 return $this->robot;
570 }
571 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 /**
574 * Get the Mobile Device
575 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200577 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000578 public function mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
580 return $this->mobile;
581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 /**
586 * Get the referrer
587 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200589 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000590 public function referrer()
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300592 return empty($_SERVER['HTTP_REFERER']) ? '' : trim($_SERVER['HTTP_REFERER']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
594
595 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 /**
598 * Get the accepted languages
599 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200601 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000602 public function languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200604 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
606 $this->_set_languages();
607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 return $this->languages;
610 }
611
612 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 /**
615 * Get the accepted Character Sets
616 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200618 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000619 public function charsets()
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200621 if (count($this->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 $this->_set_charsets();
624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 return $this->charsets;
627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 /**
632 * Test for a particular language
633 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400634 * @param string $lang
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200636 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000637 public function accept_lang($lang = 'en')
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300639 return in_array(strtolower($lang), $this->languages(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 }
Barry Mienydd671972010-10-04 16:33:58 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 /**
645 * Test for a particular character set
646 *
Andrey Andreevca7d8222012-05-11 10:59:09 +0300647 * @param string $charset
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200649 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000650 public function accept_charset($charset = 'utf-8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300652 return in_array(strtolower($charset), $this->charsets(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
Barry Mienydd671972010-10-04 16:33:58 +0200654
Andrey Andreev27e91a02014-01-09 01:00:48 +0200655 // --------------------------------------------------------------------
656
657 /**
658 * Parse a custom user-agent string
659 *
660 * @param string $string
661 * @return void
662 */
663 public function parse($string)
664 {
665 // Reset values
666 $this->is_browser = FALSE;
667 $this->is_robot = FALSE;
668 $this->is_mobile = FALSE;
669 $this->browser = '';
670 $this->version = '';
671 $this->mobile = '';
672 $this->robot = '';
673
674 // Set the new user-agent string and parse it, unless empty
675 $this->agent = $string;
676
677 if ( ! empty($string))
678 {
679 $this->_compile_data();
680 }
681 }
682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683}
684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685/* End of file User_agent.php */
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300686/* Location: ./system/libraries/User_agent.php */