blob: 21162388a08e63ec41d06f4404cbf0c0f6a3c3fa [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 }
116
117
118 return $return;
119 }
admindf752b22006-10-08 07:17:13 +0000120
121 // --------------------------------------------------------------------
122
123 /**
124 * Compile the User Agent Data
125 *
126 * @access private
127 * @return bool
128 */
129 function _compile_data()
130 {
131 $this->_set_platform();
132
133 foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function)
134 {
135 if ($this->$function() === TRUE)
136 {
137 break;
138 }
139 }
140 }
141
142 // --------------------------------------------------------------------
143
144 /**
145 * Set the Platform
146 *
147 * @access private
148 * @return mixed
149 */
150 function _set_platform()
151 {
admin38221942006-10-09 17:34:19 +0000152 if (is_array($this->platforms) AND count($this->platforms) > 0)
admindf752b22006-10-08 07:17:13 +0000153 {
admin38221942006-10-09 17:34:19 +0000154 foreach ($this->platforms as $key => $val)
admindf752b22006-10-08 07:17:13 +0000155 {
admin38221942006-10-09 17:34:19 +0000156 if (preg_match("|".preg_quote($key)."|i", $this->agent))
157 {
158 $this->platform = $val;
159 return TRUE;
160 }
admindf752b22006-10-08 07:17:13 +0000161 }
162 }
admindf752b22006-10-08 07:17:13 +0000163 $this->platform = 'Unknown Platform';
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Set the Browser
170 *
171 * @access private
172 * @return bool
173 */
174 function _set_browser()
175 {
admin38221942006-10-09 17:34:19 +0000176 if (is_array($this->browsers) AND count($this->browsers) > 0)
177 {
178 foreach ($this->browsers as $key => $val)
179 {
180 if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
181 {
182 $this->is_browser = TRUE;
183 $this->version = $match[1];
184 $this->browser = $val;
185 return TRUE;
186 }
admindf752b22006-10-08 07:17:13 +0000187 }
188 }
admindf752b22006-10-08 07:17:13 +0000189 return FALSE;
190 }
admin6c9f7362006-10-09 03:33:48 +0000191
admindf752b22006-10-08 07:17:13 +0000192 // --------------------------------------------------------------------
193
194 /**
195 * Set the Robot
196 *
197 * @access private
198 * @return bool
199 */
200 function _set_robot()
201 {
admin38221942006-10-09 17:34:19 +0000202 if (is_array($this->robots) AND count($this->robots) > 0)
203 {
204 foreach ($this->robots as $key => $val)
admindf752b22006-10-08 07:17:13 +0000205 {
admin38221942006-10-09 17:34:19 +0000206 if (preg_match("|".preg_quote($key)."|i", $this->agent))
207 {
208 $this->is_robot = TRUE;
209 $this->robot = $val;
210 return TRUE;
211 }
admindf752b22006-10-08 07:17:13 +0000212 }
213 }
admindf752b22006-10-08 07:17:13 +0000214 return FALSE;
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
admin38221942006-10-09 17:34:19 +0000220 * Set the Mobile Device
admindf752b22006-10-08 07:17:13 +0000221 *
222 * @access private
223 * @return bool
224 */
225 function _set_mobile()
226 {
admin38221942006-10-09 17:34:19 +0000227 if (is_array($this->mobiles) AND count($this->mobiles) > 0)
228 {
229 foreach ($this->mobiles as $key => $val)
admindf752b22006-10-08 07:17:13 +0000230 {
admin38221942006-10-09 17:34:19 +0000231 if (FALSE !== (strpos(strtolower($this->agent), $key)))
232 {
233 $this->is_mobile = TRUE;
234 $this->mobile = $val;
235 return TRUE;
236 }
admindf752b22006-10-08 07:17:13 +0000237 }
admin38221942006-10-09 17:34:19 +0000238 }
admindf752b22006-10-08 07:17:13 +0000239 return FALSE;
240 }
241
admin6c9f7362006-10-09 03:33:48 +0000242 // --------------------------------------------------------------------
243
244 /**
245 * Set the accepted languages
246 *
247 * @access private
248 * @return void
249 */
250 function _set_languages()
251 {
252 if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
253 {
admin38221942006-10-09 17:34:19 +0000254 $languages = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_LANGUAGE']));
admin6c9f7362006-10-09 03:33:48 +0000255
256 $this->languages = explode(',', $languages);
257 }
258
259 if (count($this->languages) == 0)
260 {
261 $this->languages = array('Undefined');
262 }
263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * Set the accepted character sets
269 *
270 * @access private
271 * @return void
272 */
273 function _set_charsets()
274 {
275 if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
276 {
admin38221942006-10-09 17:34:19 +0000277 $charsets = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_CHARSET']));
admin6c9f7362006-10-09 03:33:48 +0000278
279 $this->charsets = explode(',', $charsets);
280 }
281
282 if (count($this->charsets) == 0)
283 {
284 $this->charsets = array('Undefined');
285 }
286 }
admindf752b22006-10-08 07:17:13 +0000287
288 // --------------------------------------------------------------------
289
290 /**
291 * Is Browser
292 *
293 * @access public
294 * @return bool
295 */
296 function is_browser()
297 {
298 return $this->is_browser;
299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Is Robot
305 *
306 * @access public
307 * @return bool
308 */
309 function is_robot()
310 {
311 return $this->is_robot;
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Is Mobile
318 *
319 * @access public
320 * @return bool
321 */
322 function is_mobile()
323 {
324 return $this->is_mobile;
325 }
326
327 // --------------------------------------------------------------------
328
329 /**
admin6c9f7362006-10-09 03:33:48 +0000330 * Is this a referral from another site?
331 *
332 * @access public
333 * @return bool
334 */
335 function is_referral()
336 {
337 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Agent String
344 *
345 * @access public
346 * @return string
347 */
348 function agent()
349 {
350 return $this->agent;
351 }
352
353 // --------------------------------------------------------------------
354
355 /**
admindf752b22006-10-08 07:17:13 +0000356 * Get Platform
357 *
358 * @access public
359 * @return string
360 */
admin6c9f7362006-10-09 03:33:48 +0000361 function platform()
admindf752b22006-10-08 07:17:13 +0000362 {
363 return $this->platform;
364 }
365
366 // --------------------------------------------------------------------
367
368 /**
369 * Get Browser Name
370 *
371 * @access public
372 * @return string
373 */
admin6c9f7362006-10-09 03:33:48 +0000374 function browser()
admindf752b22006-10-08 07:17:13 +0000375 {
376 return $this->browser;
377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * Get the Browser Version
383 *
384 * @access public
385 * @return string
386 */
admin6c9f7362006-10-09 03:33:48 +0000387 function version()
admindf752b22006-10-08 07:17:13 +0000388 {
389 return $this->version;
390 }
391
392 // --------------------------------------------------------------------
393
394 /**
395 * Get The Robot Name
396 *
397 * @access public
398 * @return string
399 */
admin6c9f7362006-10-09 03:33:48 +0000400 function robot()
admindf752b22006-10-08 07:17:13 +0000401 {
402 return $this->robot;
403 }
404 // --------------------------------------------------------------------
405
406 /**
admin38221942006-10-09 17:34:19 +0000407 * Get the Mobile Device
admindf752b22006-10-08 07:17:13 +0000408 *
409 * @access public
410 * @return string
411 */
admin6c9f7362006-10-09 03:33:48 +0000412 function mobile()
admindf752b22006-10-08 07:17:13 +0000413 {
414 return $this->mobile;
415 }
416
admin6c9f7362006-10-09 03:33:48 +0000417 // --------------------------------------------------------------------
418
419 /**
420 * Get the referrer
421 *
422 * @access public
423 * @return bool
424 */
425 function referrer()
426 {
admin38221942006-10-09 17:34:19 +0000427 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
admin6c9f7362006-10-09 03:33:48 +0000428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Get the accepted languages
434 *
435 * @access public
436 * @return array
437 */
438 function languages()
439 {
440 if (count($this->languages) == 0)
441 {
442 $this->_set_languages();
443 }
444
445 return $this->languages;
446 }
447
448 // --------------------------------------------------------------------
449
450 /**
451 * Get the accepted Character Sets
452 *
453 * @access public
454 * @return array
455 */
456 function charsets()
457 {
458 if (count($this->charsets) == 0)
459 {
460 $this->_set_charsets();
461 }
462
463 return $this->charsets;
464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * Test for a particular language
470 *
471 * @access public
472 * @return bool
473 */
474 function accept_lang($lang = 'en')
475 {
476 return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
482 * Test for a particular character set
483 *
484 * @access public
485 * @return bool
486 */
487 function accept_charset($charset = 'utf-8')
488 {
489 return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
490 }
491
admindf752b22006-10-08 07:17:13 +0000492
493}
494
495?>