blob: 11c2baa68f9690f4fa9a7b4de1e3dcdd126962cd [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
adminb1fddc02006-10-09 21:29:07 +000040 var $platforms = array();
41 var $browsers = array();
42 var $mobiles = array();
43 var $robots = array();
44
admindf752b22006-10-08 07:17:13 +000045 var $platform = '';
46 var $browser = '';
47 var $version = '';
48 var $moble = '';
49 var $robot = '';
admindf752b22006-10-08 07:17:13 +000050
51 /**
52 * Constructor
53 *
54 * Sets the User Agent and runs the compilation routine
55 *
56 * @access public
57 * @return void
58 */
59 function CI_User_agent()
60 {
61 if (isset($_SERVER['HTTP_USER_AGENT']))
62 {
63 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
64 }
65
66 if ( ! is_null($this->agent))
67 {
admin38221942006-10-09 17:34:19 +000068 if ($this->_load_agent_file())
69 {
70 $this->_compile_data();
71 }
admindf752b22006-10-08 07:17:13 +000072 }
adminb1fddc02006-10-09 21:29:07 +000073
74 log_message('debug', "Table Class Initialized");
admindf752b22006-10-08 07:17:13 +000075 }
admin38221942006-10-09 17:34:19 +000076
77 // --------------------------------------------------------------------
78
79 /**
80 * Compile the User Agent Data
81 *
82 * @access private
83 * @return bool
84 */
85 function _load_agent_file()
86 {
adminb1fddc02006-10-09 21:29:07 +000087 if ( ! @include(APPPATH.'config/user_agents'.EXT))
admin38221942006-10-09 17:34:19 +000088 {
89 return FALSE;
90 }
91
92 $return = FALSE;
93
94 if (isset($platforms))
95 {
96 $this->platforms = $platforms;
97 unset($platforms);
98 $return = TRUE;
99 }
100
101 if (isset($browsers))
102 {
103 $this->browsers = $browsers;
104 unset($browsers);
105 $return = TRUE;
106 }
107
108 if (isset($mobiles))
109 {
adminb1fddc02006-10-09 21:29:07 +0000110 $this->mobiles = $mobiles;
admin38221942006-10-09 17:34:19 +0000111 unset($mobiles);
112 $return = TRUE;
113 }
114
115 if (isset($robots))
116 {
117 $this->robots = $robots;
118 unset($robots);
119 $return = TRUE;
120 }
admin2f0bac82006-10-09 17:36:45 +0000121
admin38221942006-10-09 17:34:19 +0000122 return $return;
123 }
admindf752b22006-10-08 07:17:13 +0000124
125 // --------------------------------------------------------------------
126
127 /**
128 * Compile the User Agent Data
129 *
130 * @access private
131 * @return bool
132 */
133 function _compile_data()
134 {
135 $this->_set_platform();
136
137 foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function)
138 {
139 if ($this->$function() === TRUE)
140 {
141 break;
142 }
143 }
144 }
145
146 // --------------------------------------------------------------------
147
148 /**
149 * Set the Platform
150 *
151 * @access private
152 * @return mixed
153 */
154 function _set_platform()
155 {
admin38221942006-10-09 17:34:19 +0000156 if (is_array($this->platforms) AND count($this->platforms) > 0)
admindf752b22006-10-08 07:17:13 +0000157 {
admin38221942006-10-09 17:34:19 +0000158 foreach ($this->platforms as $key => $val)
admindf752b22006-10-08 07:17:13 +0000159 {
admin38221942006-10-09 17:34:19 +0000160 if (preg_match("|".preg_quote($key)."|i", $this->agent))
161 {
162 $this->platform = $val;
163 return TRUE;
164 }
admindf752b22006-10-08 07:17:13 +0000165 }
166 }
admindf752b22006-10-08 07:17:13 +0000167 $this->platform = 'Unknown Platform';
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Set the Browser
174 *
175 * @access private
176 * @return bool
177 */
178 function _set_browser()
179 {
admin38221942006-10-09 17:34:19 +0000180 if (is_array($this->browsers) AND count($this->browsers) > 0)
181 {
182 foreach ($this->browsers as $key => $val)
183 {
184 if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
185 {
186 $this->is_browser = TRUE;
187 $this->version = $match[1];
188 $this->browser = $val;
189 return TRUE;
190 }
admindf752b22006-10-08 07:17:13 +0000191 }
192 }
admindf752b22006-10-08 07:17:13 +0000193 return FALSE;
194 }
admin6c9f7362006-10-09 03:33:48 +0000195
admindf752b22006-10-08 07:17:13 +0000196 // --------------------------------------------------------------------
197
198 /**
199 * Set the Robot
200 *
201 * @access private
202 * @return bool
203 */
204 function _set_robot()
205 {
admin38221942006-10-09 17:34:19 +0000206 if (is_array($this->robots) AND count($this->robots) > 0)
207 {
208 foreach ($this->robots as $key => $val)
admindf752b22006-10-08 07:17:13 +0000209 {
admin38221942006-10-09 17:34:19 +0000210 if (preg_match("|".preg_quote($key)."|i", $this->agent))
211 {
212 $this->is_robot = TRUE;
213 $this->robot = $val;
214 return TRUE;
215 }
admindf752b22006-10-08 07:17:13 +0000216 }
217 }
admindf752b22006-10-08 07:17:13 +0000218 return FALSE;
219 }
220
221 // --------------------------------------------------------------------
222
223 /**
admin38221942006-10-09 17:34:19 +0000224 * Set the Mobile Device
admindf752b22006-10-08 07:17:13 +0000225 *
226 * @access private
227 * @return bool
228 */
229 function _set_mobile()
230 {
admin38221942006-10-09 17:34:19 +0000231 if (is_array($this->mobiles) AND count($this->mobiles) > 0)
232 {
233 foreach ($this->mobiles as $key => $val)
admindf752b22006-10-08 07:17:13 +0000234 {
admin38221942006-10-09 17:34:19 +0000235 if (FALSE !== (strpos(strtolower($this->agent), $key)))
236 {
237 $this->is_mobile = TRUE;
238 $this->mobile = $val;
239 return TRUE;
240 }
admindf752b22006-10-08 07:17:13 +0000241 }
admin38221942006-10-09 17:34:19 +0000242 }
admindf752b22006-10-08 07:17:13 +0000243 return FALSE;
244 }
245
admin6c9f7362006-10-09 03:33:48 +0000246 // --------------------------------------------------------------------
247
248 /**
249 * Set the accepted languages
250 *
251 * @access private
252 * @return void
253 */
254 function _set_languages()
255 {
256 if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
257 {
admin38221942006-10-09 17:34:19 +0000258 $languages = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_LANGUAGE']));
admin6c9f7362006-10-09 03:33:48 +0000259
260 $this->languages = explode(',', $languages);
261 }
262
263 if (count($this->languages) == 0)
264 {
265 $this->languages = array('Undefined');
266 }
267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Set the accepted character sets
273 *
274 * @access private
275 * @return void
276 */
277 function _set_charsets()
278 {
279 if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
280 {
admin38221942006-10-09 17:34:19 +0000281 $charsets = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_CHARSET']));
admin6c9f7362006-10-09 03:33:48 +0000282
283 $this->charsets = explode(',', $charsets);
284 }
285
286 if (count($this->charsets) == 0)
287 {
288 $this->charsets = array('Undefined');
289 }
290 }
admindf752b22006-10-08 07:17:13 +0000291
292 // --------------------------------------------------------------------
293
294 /**
295 * Is Browser
296 *
297 * @access public
298 * @return bool
299 */
300 function is_browser()
301 {
302 return $this->is_browser;
303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * Is Robot
309 *
310 * @access public
311 * @return bool
312 */
313 function is_robot()
314 {
315 return $this->is_robot;
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Is Mobile
322 *
323 * @access public
324 * @return bool
325 */
326 function is_mobile()
327 {
328 return $this->is_mobile;
329 }
330
331 // --------------------------------------------------------------------
332
333 /**
admin6c9f7362006-10-09 03:33:48 +0000334 * Is this a referral from another site?
335 *
336 * @access public
337 * @return bool
338 */
339 function is_referral()
340 {
341 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
342 }
343
344 // --------------------------------------------------------------------
345
346 /**
347 * Agent String
348 *
349 * @access public
350 * @return string
351 */
adminb1fddc02006-10-09 21:29:07 +0000352 function agent_string()
admin6c9f7362006-10-09 03:33:48 +0000353 {
354 return $this->agent;
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
admindf752b22006-10-08 07:17:13 +0000360 * Get Platform
361 *
362 * @access public
363 * @return string
364 */
admin6c9f7362006-10-09 03:33:48 +0000365 function platform()
admindf752b22006-10-08 07:17:13 +0000366 {
367 return $this->platform;
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Get Browser Name
374 *
375 * @access public
376 * @return string
377 */
admin6c9f7362006-10-09 03:33:48 +0000378 function browser()
admindf752b22006-10-08 07:17:13 +0000379 {
380 return $this->browser;
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Get the Browser Version
387 *
388 * @access public
389 * @return string
390 */
admin6c9f7362006-10-09 03:33:48 +0000391 function version()
admindf752b22006-10-08 07:17:13 +0000392 {
393 return $this->version;
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Get The Robot Name
400 *
401 * @access public
402 * @return string
403 */
admin6c9f7362006-10-09 03:33:48 +0000404 function robot()
admindf752b22006-10-08 07:17:13 +0000405 {
406 return $this->robot;
407 }
408 // --------------------------------------------------------------------
409
410 /**
admin38221942006-10-09 17:34:19 +0000411 * Get the Mobile Device
admindf752b22006-10-08 07:17:13 +0000412 *
413 * @access public
414 * @return string
415 */
admin6c9f7362006-10-09 03:33:48 +0000416 function mobile()
admindf752b22006-10-08 07:17:13 +0000417 {
418 return $this->mobile;
419 }
420
admin6c9f7362006-10-09 03:33:48 +0000421 // --------------------------------------------------------------------
422
423 /**
424 * Get the referrer
425 *
426 * @access public
427 * @return bool
428 */
429 function referrer()
430 {
admin38221942006-10-09 17:34:19 +0000431 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
admin6c9f7362006-10-09 03:33:48 +0000432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * Get the accepted languages
438 *
439 * @access public
440 * @return array
441 */
442 function languages()
443 {
444 if (count($this->languages) == 0)
445 {
446 $this->_set_languages();
447 }
448
449 return $this->languages;
450 }
451
452 // --------------------------------------------------------------------
453
454 /**
455 * Get the accepted Character Sets
456 *
457 * @access public
458 * @return array
459 */
460 function charsets()
461 {
462 if (count($this->charsets) == 0)
463 {
464 $this->_set_charsets();
465 }
466
467 return $this->charsets;
468 }
469
470 // --------------------------------------------------------------------
471
472 /**
473 * Test for a particular language
474 *
475 * @access public
476 * @return bool
477 */
478 function accept_lang($lang = 'en')
479 {
480 return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
481 }
482
483 // --------------------------------------------------------------------
484
485 /**
486 * Test for a particular character set
487 *
488 * @access public
489 * @return bool
490 */
491 function accept_charset($charset = 'utf-8')
492 {
493 return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
494 }
495
admindf752b22006-10-08 07:17:13 +0000496
497}
498
499?>