blob: 39e12327f97943da9be7ad00d7fdf99376ad0c32 [file] [log] [blame]
admindf752b22006-10-08 07:17:13 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * User Agent Class
20 *
21 * Identifies the platform, browser, robot, or mobile devise of the browsing agent
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category User Agent
26 * @author Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/libraries/uri.html
28 */
29class CI_User_agent {
30
31 var $agent = NULL;
32
33 var $is_browser = FALSE;
34 var $is_robot = FALSE;
35 var $is_mobile = FALSE;
admin6c9f7362006-10-09 03:33:48 +000036
37 var $languages = array();
38 var $charsets = array();
admindf752b22006-10-08 07:17:13 +000039
40 var $platform = '';
41 var $browser = '';
42 var $version = '';
43 var $moble = '';
44 var $robot = '';
45
admindf752b22006-10-08 07:17:13 +000046
47 /**
48 * Constructor
49 *
50 * Sets the User Agent and runs the compilation routine
51 *
52 * @access public
53 * @return void
54 */
55 function CI_User_agent()
56 {
57 if (isset($_SERVER['HTTP_USER_AGENT']))
58 {
59 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
60 }
61
62 if ( ! is_null($this->agent))
63 {
admin38221942006-10-09 17:34:19 +000064 if ($this->_load_agent_file())
65 {
66 $this->_compile_data();
67 }
admindf752b22006-10-08 07:17:13 +000068 }
69 }
admin38221942006-10-09 17:34:19 +000070
71
72 // --------------------------------------------------------------------
73
74 /**
75 * Compile the User Agent Data
76 *
77 * @access private
78 * @return bool
79 */
80 function _load_agent_file()
81 {
82 if ( ! @include(APPPATH.'config/user_agent'.EXT))
83 {
84 return FALSE;
85 }
86
87 $return = FALSE;
88
89 if (isset($platforms))
90 {
91 $this->platforms = $platforms;
92 unset($platforms);
93 $return = TRUE;
94 }
95
96 if (isset($browsers))
97 {
98 $this->browsers = $browsers;
99 unset($browsers);
100 $return = TRUE;
101 }
102
103 if (isset($mobiles))
104 {
105 $this->browsers = $mobiles;
106 unset($mobiles);
107 $return = TRUE;
108 }
109
110 if (isset($robots))
111 {
112 $this->robots = $robots;
113 unset($robots);
114 $return = TRUE;
115 }
admin2f0bac82006-10-09 17:36:45 +0000116
admin38221942006-10-09 17:34:19 +0000117 return $return;
118 }
admindf752b22006-10-08 07:17:13 +0000119
120 // --------------------------------------------------------------------
121
122 /**
123 * Compile the User Agent Data
124 *
125 * @access private
126 * @return bool
127 */
128 function _compile_data()
129 {
130 $this->_set_platform();
131
132 foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function)
133 {
134 if ($this->$function() === TRUE)
135 {
136 break;
137 }
138 }
139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Set the Platform
145 *
146 * @access private
147 * @return mixed
148 */
149 function _set_platform()
150 {
admin38221942006-10-09 17:34:19 +0000151 if (is_array($this->platforms) AND count($this->platforms) > 0)
admindf752b22006-10-08 07:17:13 +0000152 {
admin38221942006-10-09 17:34:19 +0000153 foreach ($this->platforms as $key => $val)
admindf752b22006-10-08 07:17:13 +0000154 {
admin38221942006-10-09 17:34:19 +0000155 if (preg_match("|".preg_quote($key)."|i", $this->agent))
156 {
157 $this->platform = $val;
158 return TRUE;
159 }
admindf752b22006-10-08 07:17:13 +0000160 }
161 }
admindf752b22006-10-08 07:17:13 +0000162 $this->platform = 'Unknown Platform';
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Set the Browser
169 *
170 * @access private
171 * @return bool
172 */
173 function _set_browser()
174 {
admin38221942006-10-09 17:34:19 +0000175 if (is_array($this->browsers) AND count($this->browsers) > 0)
176 {
177 foreach ($this->browsers as $key => $val)
178 {
179 if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
180 {
181 $this->is_browser = TRUE;
182 $this->version = $match[1];
183 $this->browser = $val;
184 return TRUE;
185 }
admindf752b22006-10-08 07:17:13 +0000186 }
187 }
admindf752b22006-10-08 07:17:13 +0000188 return FALSE;
189 }
admin6c9f7362006-10-09 03:33:48 +0000190
admindf752b22006-10-08 07:17:13 +0000191 // --------------------------------------------------------------------
192
193 /**
194 * Set the Robot
195 *
196 * @access private
197 * @return bool
198 */
199 function _set_robot()
200 {
admin38221942006-10-09 17:34:19 +0000201 if (is_array($this->robots) AND count($this->robots) > 0)
202 {
203 foreach ($this->robots as $key => $val)
admindf752b22006-10-08 07:17:13 +0000204 {
admin38221942006-10-09 17:34:19 +0000205 if (preg_match("|".preg_quote($key)."|i", $this->agent))
206 {
207 $this->is_robot = TRUE;
208 $this->robot = $val;
209 return TRUE;
210 }
admindf752b22006-10-08 07:17:13 +0000211 }
212 }
admindf752b22006-10-08 07:17:13 +0000213 return FALSE;
214 }
215
216 // --------------------------------------------------------------------
217
218 /**
admin38221942006-10-09 17:34:19 +0000219 * Set the Mobile Device
admindf752b22006-10-08 07:17:13 +0000220 *
221 * @access private
222 * @return bool
223 */
224 function _set_mobile()
225 {
admin38221942006-10-09 17:34:19 +0000226 if (is_array($this->mobiles) AND count($this->mobiles) > 0)
227 {
228 foreach ($this->mobiles as $key => $val)
admindf752b22006-10-08 07:17:13 +0000229 {
admin38221942006-10-09 17:34:19 +0000230 if (FALSE !== (strpos(strtolower($this->agent), $key)))
231 {
232 $this->is_mobile = TRUE;
233 $this->mobile = $val;
234 return TRUE;
235 }
admindf752b22006-10-08 07:17:13 +0000236 }
admin38221942006-10-09 17:34:19 +0000237 }
admindf752b22006-10-08 07:17:13 +0000238 return FALSE;
239 }
240
admin6c9f7362006-10-09 03:33:48 +0000241 // --------------------------------------------------------------------
242
243 /**
244 * Set the accepted languages
245 *
246 * @access private
247 * @return void
248 */
249 function _set_languages()
250 {
251 if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
252 {
admin38221942006-10-09 17:34:19 +0000253 $languages = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_LANGUAGE']));
admin6c9f7362006-10-09 03:33:48 +0000254
255 $this->languages = explode(',', $languages);
256 }
257
258 if (count($this->languages) == 0)
259 {
260 $this->languages = array('Undefined');
261 }
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Set the accepted character sets
268 *
269 * @access private
270 * @return void
271 */
272 function _set_charsets()
273 {
274 if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
275 {
admin38221942006-10-09 17:34:19 +0000276 $charsets = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_CHARSET']));
admin6c9f7362006-10-09 03:33:48 +0000277
278 $this->charsets = explode(',', $charsets);
279 }
280
281 if (count($this->charsets) == 0)
282 {
283 $this->charsets = array('Undefined');
284 }
285 }
admindf752b22006-10-08 07:17:13 +0000286
287 // --------------------------------------------------------------------
288
289 /**
290 * Is Browser
291 *
292 * @access public
293 * @return bool
294 */
295 function is_browser()
296 {
297 return $this->is_browser;
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Is Robot
304 *
305 * @access public
306 * @return bool
307 */
308 function is_robot()
309 {
310 return $this->is_robot;
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Is Mobile
317 *
318 * @access public
319 * @return bool
320 */
321 function is_mobile()
322 {
323 return $this->is_mobile;
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
admin6c9f7362006-10-09 03:33:48 +0000329 * Is this a referral from another site?
330 *
331 * @access public
332 * @return bool
333 */
334 function is_referral()
335 {
336 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Agent String
343 *
344 * @access public
345 * @return string
346 */
347 function agent()
348 {
349 return $this->agent;
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
admindf752b22006-10-08 07:17:13 +0000355 * Get Platform
356 *
357 * @access public
358 * @return string
359 */
admin6c9f7362006-10-09 03:33:48 +0000360 function platform()
admindf752b22006-10-08 07:17:13 +0000361 {
362 return $this->platform;
363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * Get Browser Name
369 *
370 * @access public
371 * @return string
372 */
admin6c9f7362006-10-09 03:33:48 +0000373 function browser()
admindf752b22006-10-08 07:17:13 +0000374 {
375 return $this->browser;
376 }
377
378 // --------------------------------------------------------------------
379
380 /**
381 * Get the Browser Version
382 *
383 * @access public
384 * @return string
385 */
admin6c9f7362006-10-09 03:33:48 +0000386 function version()
admindf752b22006-10-08 07:17:13 +0000387 {
388 return $this->version;
389 }
390
391 // --------------------------------------------------------------------
392
393 /**
394 * Get The Robot Name
395 *
396 * @access public
397 * @return string
398 */
admin6c9f7362006-10-09 03:33:48 +0000399 function robot()
admindf752b22006-10-08 07:17:13 +0000400 {
401 return $this->robot;
402 }
403 // --------------------------------------------------------------------
404
405 /**
admin38221942006-10-09 17:34:19 +0000406 * Get the Mobile Device
admindf752b22006-10-08 07:17:13 +0000407 *
408 * @access public
409 * @return string
410 */
admin6c9f7362006-10-09 03:33:48 +0000411 function mobile()
admindf752b22006-10-08 07:17:13 +0000412 {
413 return $this->mobile;
414 }
415
admin6c9f7362006-10-09 03:33:48 +0000416 // --------------------------------------------------------------------
417
418 /**
419 * Get the referrer
420 *
421 * @access public
422 * @return bool
423 */
424 function referrer()
425 {
admin38221942006-10-09 17:34:19 +0000426 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
admin6c9f7362006-10-09 03:33:48 +0000427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Get the accepted languages
433 *
434 * @access public
435 * @return array
436 */
437 function languages()
438 {
439 if (count($this->languages) == 0)
440 {
441 $this->_set_languages();
442 }
443
444 return $this->languages;
445 }
446
447 // --------------------------------------------------------------------
448
449 /**
450 * Get the accepted Character Sets
451 *
452 * @access public
453 * @return array
454 */
455 function charsets()
456 {
457 if (count($this->charsets) == 0)
458 {
459 $this->_set_charsets();
460 }
461
462 return $this->charsets;
463 }
464
465 // --------------------------------------------------------------------
466
467 /**
468 * Test for a particular language
469 *
470 * @access public
471 * @return bool
472 */
473 function accept_lang($lang = 'en')
474 {
475 return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Test for a particular character set
482 *
483 * @access public
484 * @return bool
485 */
486 function accept_charset($charset = 'utf-8')
487 {
488 return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
489 }
490
admindf752b22006-10-08 07:17:13 +0000491
492}
493
494?>