blob: e920578d99c7053bea5e08bec367bca4118707a1 [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300161 if ( ! is_null($this->agent) && $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 Andreevd3bc53d2012-04-03 16:37:19 +0300178 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600179 {
180 include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php');
181 }
182 elseif (is_file(APPPATH.'config/user_agents.php'))
183 {
184 include(APPPATH.'config/user_agents.php');
185 }
186 else
bubbafoley0ea04142011-03-17 14:55:41 -0500187 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 return FALSE;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 $return = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 if (isset($platforms))
194 {
195 $this->platforms = $platforms;
196 unset($platforms);
197 $return = TRUE;
198 }
199
200 if (isset($browsers))
201 {
202 $this->browsers = $browsers;
203 unset($browsers);
204 $return = TRUE;
205 }
206
207 if (isset($mobiles))
208 {
209 $this->mobiles = $mobiles;
210 unset($mobiles);
211 $return = TRUE;
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 if (isset($robots))
215 {
216 $this->robots = $robots;
217 unset($robots);
218 $return = TRUE;
219 }
220
221 return $return;
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 /**
227 * Compile the User Agent Data
228 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200230 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200231 protected function _compile_data()
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
233 $this->_set_platform();
Barry Mienydd671972010-10-04 16:33:58 +0200234
Phil Sturgeon2c547df2011-08-10 08:36:02 -0600235 foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 if ($this->$function() === TRUE)
238 {
239 break;
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 /**
247 * Set the Platform
248 *
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300249 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200250 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200251 protected function _set_platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300253 if (is_array($this->platforms) && count($this->platforms) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 foreach ($this->platforms as $key => $val)
256 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300257 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 $this->platform = $val;
260 return TRUE;
261 }
262 }
263 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $this->platform = 'Unknown Platform';
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300266 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
268
269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 /**
272 * Set the Browser
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200275 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200276 protected function _set_browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300278 if (is_array($this->browsers) && count($this->browsers) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 foreach ($this->browsers as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200281 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300282 if (preg_match('|'.preg_quote($key).'.*?([0-9\.]+)|i', $this->agent, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
284 $this->is_browser = TRUE;
285 $this->version = $match[1];
286 $this->browser = $val;
287 $this->_set_mobile();
288 return TRUE;
289 }
290 }
291 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 return FALSE;
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 /**
299 * Set the Robot
300 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200302 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200303 protected function _set_robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300305 if (is_array($this->robots) && count($this->robots) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200306 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 foreach ($this->robots as $key => $val)
308 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300309 if (preg_match('|'.preg_quote($key).'|i', $this->agent))
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
311 $this->is_robot = TRUE;
312 $this->robot = $val;
313 return TRUE;
314 }
315 }
316 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 return FALSE;
319 }
320
321 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 /**
324 * Set the Mobile Device
325 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200327 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200328 protected function _set_mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300330 if (is_array($this->mobiles) && count($this->mobiles) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200331 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 foreach ($this->mobiles as $key => $val)
333 {
Andrey Andreevca7d8222012-05-11 10:59:09 +0300334 if (FALSE !== (stripos($this->agent, $key)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $this->is_mobile = TRUE;
337 $this->mobile = $val;
338 return TRUE;
339 }
340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341 }
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 return FALSE;
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 /**
349 * Set the accepted languages
350 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200352 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200353 protected function _set_languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300355 if ((count($this->languages) === 0) && ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200357 $this->languages = explode(',', preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE']))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200360 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 $this->languages = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200363 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 /**
369 * Set the accepted character sets
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200372 */
Andrey Andreev75c5efb2011-12-26 16:28:40 +0200373 protected function _set_charsets()
Barry Mienydd671972010-10-04 16:33:58 +0200374 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300375 if ((count($this->charsets) === 0) && ! empty($_SERVER['HTTP_ACCEPT_CHARSET']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200377 $this->charsets = explode(',', preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET']))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200380 if (count($this->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 $this->charsets = array('Undefined');
Barry Mienydd671972010-10-04 16:33:58 +0200383 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
385
386 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 /**
389 * Is Browser
390 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400391 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200393 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000394 public function is_browser($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000396 if ( ! $this->is_browser)
397 {
398 return FALSE;
399 }
400
401 // No need to be specific, it's a browser
402 if ($key === NULL)
403 {
404 return TRUE;
405 }
406
407 // Check for a specific browser
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300408 return (isset($this->browsers[$key]) && $this->browser === $this->browsers[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
410
411 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 /**
414 * Is Robot
415 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400416 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200418 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000419 public function is_robot($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000421 if ( ! $this->is_robot)
422 {
423 return FALSE;
424 }
425
426 // No need to be specific, it's a robot
427 if ($key === NULL)
428 {
429 return TRUE;
430 }
431
432 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300433 return (isset($this->robots[$key]) && $this->robot === $this->robots[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
435
436 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 /**
439 * Is Mobile
440 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400441 * @param string $key
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200443 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000444 public function is_mobile($key = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
Phil Sturgeondac1b462011-01-06 17:40:10 +0000446 if ( ! $this->is_mobile)
447 {
448 return FALSE;
449 }
450
451 // No need to be specific, it's a mobile
452 if ($key === NULL)
453 {
454 return TRUE;
455 }
456
457 // Check for a specific robot
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300458 return (isset($this->mobiles[$key]) && $this->mobile === $this->mobiles[$key]);
Barry Mienydd671972010-10-04 16:33:58 +0200459 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000460
461 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 /**
464 * Is this a referral from another site?
465 *
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_referral()
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Andrey Andreeva0836b92012-10-24 22:03:42 +0300470 if (empty($_SERVER['HTTP_REFERER']))
471 {
472 return FALSE;
473 }
474
475 $referer = parse_url($_SERVER['HTTP_REFERER']);
476 return ! (empty($referer['host']) && strpos(config_item('base_url'), $referer['host']) !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
478
479 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 /**
482 * Agent String
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200485 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000486 public function agent_string()
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
488 return $this->agent;
489 }
490
491 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 /**
494 * Get Platform
495 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200497 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000498 public function platform()
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 return $this->platform;
501 }
502
503 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 /**
506 * Get Browser Name
507 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200509 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000510 public function browser()
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 return $this->browser;
513 }
514
515 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 /**
518 * Get the Browser Version
519 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200521 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000522 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 return $this->version;
525 }
526
527 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 /**
530 * Get The Robot Name
531 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200533 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000534 public function robot()
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
536 return $this->robot;
537 }
538 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 /**
541 * Get the Mobile Device
542 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200544 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000545 public function mobile()
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
547 return $this->mobile;
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 /**
553 * Get the referrer
554 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200556 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000557 public function referrer()
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300559 return empty($_SERVER['HTTP_REFERER']) ? '' : trim($_SERVER['HTTP_REFERER']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
561
562 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 /**
565 * Get the accepted languages
566 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200568 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000569 public function languages()
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200571 if (count($this->languages) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
573 $this->_set_languages();
574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 return $this->languages;
577 }
578
579 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 /**
582 * Get the accepted Character Sets
583 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200585 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000586 public function charsets()
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
Andrey Andreeve9ccf742011-12-25 17:30:10 +0200588 if (count($this->charsets) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
590 $this->_set_charsets();
591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 return $this->charsets;
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 /**
599 * Test for a particular language
600 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400601 * @param string $lang
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200603 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000604 public function accept_lang($lang = 'en')
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300606 return in_array(strtolower($lang), $this->languages(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 /**
612 * Test for a particular character set
613 *
Andrey Andreevca7d8222012-05-11 10:59:09 +0300614 * @param string $charset
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200616 */
Phil Sturgeondac1b462011-01-06 17:40:10 +0000617 public function accept_charset($charset = 'utf-8')
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300619 return in_array(strtolower($charset), $this->charsets(), TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622}
623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624/* End of file User_agent.php */
Andrey Andreevd3bc53d2012-04-03 16:37:19 +0300625/* Location: ./system/libraries/User_agent.php */