blob: 2f6f81909b6362bff620878a6fb5ebbe69b98637 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 /**
148 * Constructor
149 *
150 * Sets the User Agent and runs the compilation routine
151 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200153 */
Greg Akera9263282010-11-10 15:26:43 -0600154 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 if (isset($_SERVER['HTTP_USER_AGENT']))
157 {
158 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
vlakoff1228fe22013-01-14 01:30:09 +0100161 if ($this->agent !== NULL && $this->_load_agent_file())
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300163 $this->_compile_data();
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300166 log_message('debug', 'User Agent Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 /**
172 * Compile the User Agent Data
173 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200175 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200176 protected function _load_agent_file()
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Andrey Andreev06879112013-01-29 15:05:02 +0200178 if (($found = file_exists(APPPATH.'config/user_agents.php')))
Greg Akerd96f8822011-12-27 16:23:47 -0600179 {
180 include(APPPATH.'config/user_agents.php');
181 }
Andrey Andreev06879112013-01-29 15:05:02 +0200182
183 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'))
184 {
185 include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php');
186 $found = TRUE;
187 }
188
189 if ($found !== TRUE)
bubbafoley0ea04142011-03-17 14:55:41 -0500190 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 return FALSE;
192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 $return = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 if (isset($platforms))
197 {
198 $this->platforms = $platforms;
199 unset($platforms);
200 $return = TRUE;
201 }
202
203 if (isset($browsers))
204 {
205 $this->browsers = $browsers;
206 unset($browsers);
207 $return = TRUE;
208 }
209
210 if (isset($mobiles))
211 {
212 $this->mobiles = $mobiles;
213 unset($mobiles);
214 $return = TRUE;
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 if (isset($robots))
218 {
219 $this->robots = $robots;
220 unset($robots);
221 $return = TRUE;
222 }
223
224 return $return;
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 /**
230 * Compile the User Agent Data
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200234 protected function _compile_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
236 $this->_set_platform();
Barry Mienydd671972010-10-04 16:33:58 +0200237
Phil Sturgeon2c547df2011-08-10 08:36:02 -0600238 foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
240 if ($this->$function() === TRUE)
241 {
242 break;
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 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 /**
250 * Set the Platform
251 *
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300252 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200253 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200254 protected function _set_platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300256 if (is_array($this->platforms) && count($this->platforms) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 foreach ($this->platforms as $key => $val)
259 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300260 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
262 $this->platform = $val;
263 return TRUE;
264 }
265 }
266 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 $this->platform = 'Unknown Platform';
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300269 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
271
272 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 /**
275 * Set the Browser
276 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200278 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200279 protected function _set_browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300281 if (is_array($this->browsers) && count($this->browsers) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 foreach ($this->browsers as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200284 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300285 if (preg_match('|'.preg_quote($key).'.*?([0-9\.]+)|i', $this->agent, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
287 $this->is_browser = TRUE;
288 $this->version = $match[1];
289 $this->browser = $val;
290 $this->_set_mobile();
291 return TRUE;
292 }
293 }
294 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 return FALSE;
297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 /**
302 * Set the Robot
303 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200305 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200306 protected function _set_robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300308 if (is_array($this->robots) && count($this->robots) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200309 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 foreach ($this->robots as $key => $val)
311 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300312 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
314 $this->is_robot = TRUE;
315 $this->robot = $val;
Eric Roberts001a7642012-04-14 16:41:13 -0500316 $this->_set_mobile();
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 return TRUE;
318 }
319 }
320 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 return FALSE;
323 }
324
325 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 /**
328 * Set the Mobile Device
329 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200331 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200332 protected function _set_mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300334 if (is_array($this->mobiles) && count($this->mobiles) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200335 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 foreach ($this->mobiles as $key => $val)
337 {
Andrey Andreevca7d8222012-05-11 10:59:09 +0300338 if (FALSE !== (stripos($this->agent, $key)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
340 $this->is_mobile = TRUE;
341 $this->mobile = $val;
342 return TRUE;
343 }
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 return FALSE;
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 /**
353 * Set the accepted languages
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200357 protected function _set_languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300359 if ((count($this->languages) === 0) && ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200361 $this->languages = explode(',', preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE']))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200364 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
366 $this->languages = array('Undefined');
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 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 /**
373 * Set the accepted character sets
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_charsets()
Barry Mienydd671972010-10-04 16:33:58 +0200378 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300379 if ((count($this->charsets) === 0) && ! empty($_SERVER['HTTP_ACCEPT_CHARSET']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200381 $this->charsets = explode(',', preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET']))));
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->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 $this->charsets = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200387 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
389
390 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 /**
393 * Is Browser
394 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400395 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200397 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000398 public function is_browser($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000400 if ( ! $this->is_browser)
401 {
402 return FALSE;
403 }
404
405 // No need to be specific, it's a browser
406 if ($key === NULL)
407 {
408 return TRUE;
409 }
410
411 // Check for a specific browser
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300412 return (isset($this->browsers[$key]) && $this->browser === $this->browsers[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414
415 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 /**
418 * Is Robot
419 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400420 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200422 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000423 public function is_robot($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000425 if ( ! $this->is_robot)
426 {
427 return FALSE;
428 }
429
430 // No need to be specific, it's a robot
431 if ($key === NULL)
432 {
433 return TRUE;
434 }
435
436 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300437 return (isset($this->robots[$key]) && $this->robot === $this->robots[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
440 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 /**
443 * Is Mobile
444 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400445 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200447 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000448 public function is_mobile($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000450 if ( ! $this->is_mobile)
451 {
452 return FALSE;
453 }
454
455 // No need to be specific, it's a mobile
456 if ($key === NULL)
457 {
458 return TRUE;
459 }
460
461 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300462 return (isset($this->mobiles[$key]) && $this->mobile === $this->mobiles[$key]);
Barry Mienydd671972010-10-04 16:33:58 +0200463 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000464
465 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 /**
468 * Is this a referral from another site?
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200471 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000472 public function is_referral()
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreeva0836b92012-10-24 22:03:42 +0300474 if (empty($_SERVER['HTTP_REFERER']))
475 {
476 return FALSE;
477 }
478
479 $referer = parse_url($_SERVER['HTTP_REFERER']);
480 return ! (empty($referer['host']) && strpos(config_item('base_url'), $referer['host']) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482
483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 /**
486 * Agent String
487 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200489 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000490 public function agent_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 return $this->agent;
493 }
494
495 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 /**
498 * Get Platform
499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200501 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000502 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
504 return $this->platform;
505 }
506
507 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 /**
510 * Get Browser Name
511 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200513 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000514 public function browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 return $this->browser;
517 }
518
519 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 /**
522 * Get the Browser Version
523 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200525 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000526 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
528 return $this->version;
529 }
530
531 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 /**
534 * Get The Robot Name
535 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200537 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000538 public function robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
540 return $this->robot;
541 }
542 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 /**
545 * Get the Mobile Device
546 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200548 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000549 public function mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
551 return $this->mobile;
552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 /**
557 * Get the referrer
558 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200560 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000561 public function referrer()
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300563 return empty($_SERVER['HTTP_REFERER']) ? '' : trim($_SERVER['HTTP_REFERER']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
565
566 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 /**
569 * Get the accepted languages
570 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200572 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000573 public function languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200575 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
577 $this->_set_languages();
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 return $this->languages;
581 }
582
583 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 /**
586 * Get the accepted Character Sets
587 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200589 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000590 public function charsets()
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200592 if (count($this->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 $this->_set_charsets();
595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 return $this->charsets;
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 /**
603 * Test for a particular language
604 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400605 * @param string $lang
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200607 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000608 public function accept_lang($lang = 'en')
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300610 return in_array(strtolower($lang), $this->languages(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 /**
616 * Test for a particular character set
617 *
Andrey Andreevca7d8222012-05-11 10:59:09 +0300618 * @param string $charset
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200620 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000621 public function accept_charset($charset = 'utf-8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300623 return in_array(strtolower($charset), $this->charsets(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626}
627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628/* End of file User_agent.php */
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300629/* Location: ./system/libraries/User_agent.php */