1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
---|
2 | /** |
---|
3 | * CodeIgniter |
---|
4 | * |
---|
5 | * An open source application development framework for PHP 5.1.6 or newer |
---|
6 | * |
---|
7 | * @package CodeIgniter |
---|
8 | * @author ExpressionEngine Dev Team |
---|
9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
---|
10 | * @license http://codeigniter.com/user_guide/license.html |
---|
11 | * @link http://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 ExpressionEngine Dev Team |
---|
27 | * @link http://codeigniter.com/user_guide/libraries/user_agent.html |
---|
28 | */ |
---|
29 | class CI_User_agent { |
---|
30 | |
---|
31 | var $agent = NULL; |
---|
32 | |
---|
33 | var $is_browser = FALSE; |
---|
34 | var $is_robot = FALSE; |
---|
35 | var $is_mobile = FALSE; |
---|
36 | |
---|
37 | var $languages = array(); |
---|
38 | var $charsets = array(); |
---|
39 | |
---|
40 | var $platforms = array(); |
---|
41 | var $browsers = array(); |
---|
42 | var $mobiles = array(); |
---|
43 | var $robots = array(); |
---|
44 | |
---|
45 | var $platform = ''; |
---|
46 | var $browser = ''; |
---|
47 | var $version = ''; |
---|
48 | var $mobile = ''; |
---|
49 | var $robot = ''; |
---|
50 | |
---|
51 | /** |
---|
52 | * Constructor |
---|
53 | * |
---|
54 | * Sets the User Agent and runs the compilation routine |
---|
55 | * |
---|
56 | * @access public |
---|
57 | * @return void |
---|
58 | */ |
---|
59 | public function __construct() |
---|
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 | { |
---|
68 | if ($this->_load_agent_file()) |
---|
69 | { |
---|
70 | $this->_compile_data(); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | log_message('debug', "User Agent Class Initialized"); |
---|
75 | } |
---|
76 | |
---|
77 | // -------------------------------------------------------------------- |
---|
78 | |
---|
79 | /** |
---|
80 | * Compile the User Agent Data |
---|
81 | * |
---|
82 | * @access private |
---|
83 | * @return bool |
---|
84 | */ |
---|
85 | private function _load_agent_file() |
---|
86 | { |
---|
87 | if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php')) |
---|
88 | { |
---|
89 | include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'); |
---|
90 | } |
---|
91 | elseif (is_file(APPPATH.'config/user_agents.php')) |
---|
92 | { |
---|
93 | include(APPPATH.'config/user_agents.php'); |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | return FALSE; |
---|
98 | } |
---|
99 | |
---|
100 | $return = FALSE; |
---|
101 | |
---|
102 | if (isset($platforms)) |
---|
103 | { |
---|
104 | $this->platforms = $platforms; |
---|
105 | unset($platforms); |
---|
106 | $return = TRUE; |
---|
107 | } |
---|
108 | |
---|
109 | if (isset($browsers)) |
---|
110 | { |
---|
111 | $this->browsers = $browsers; |
---|
112 | unset($browsers); |
---|
113 | $return = TRUE; |
---|
114 | } |
---|
115 | |
---|
116 | if (isset($mobiles)) |
---|
117 | { |
---|
118 | $this->mobiles = $mobiles; |
---|
119 | unset($mobiles); |
---|
120 | $return = TRUE; |
---|
121 | } |
---|
122 | |
---|
123 | if (isset($robots)) |
---|
124 | { |
---|
125 | $this->robots = $robots; |
---|
126 | unset($robots); |
---|
127 | $return = TRUE; |
---|
128 | } |
---|
129 | |
---|
130 | return $return; |
---|
131 | } |
---|
132 | |
---|
133 | // -------------------------------------------------------------------- |
---|
134 | |
---|
135 | /** |
---|
136 | * Compile the User Agent Data |
---|
137 | * |
---|
138 | * @access private |
---|
139 | * @return bool |
---|
140 | */ |
---|
141 | private function _compile_data() |
---|
142 | { |
---|
143 | $this->_set_platform(); |
---|
144 | |
---|
145 | foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function) |
---|
146 | { |
---|
147 | if ($this->$function() === TRUE) |
---|
148 | { |
---|
149 | break; |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | // -------------------------------------------------------------------- |
---|
155 | |
---|
156 | /** |
---|
157 | * Set the Platform |
---|
158 | * |
---|
159 | * @access private |
---|
160 | * @return mixed |
---|
161 | */ |
---|
162 | private function _set_platform() |
---|
163 | { |
---|
164 | if (is_array($this->platforms) AND count($this->platforms) > 0) |
---|
165 | { |
---|
166 | foreach ($this->platforms as $key => $val) |
---|
167 | { |
---|
168 | if (preg_match("|".preg_quote($key)."|i", $this->agent)) |
---|
169 | { |
---|
170 | $this->platform = $val; |
---|
171 | return TRUE; |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | $this->platform = 'Unknown Platform'; |
---|
176 | } |
---|
177 | |
---|
178 | // -------------------------------------------------------------------- |
---|
179 | |
---|
180 | /** |
---|
181 | * Set the Browser |
---|
182 | * |
---|
183 | * @access private |
---|
184 | * @return bool |
---|
185 | */ |
---|
186 | private function _set_browser() |
---|
187 | { |
---|
188 | if (is_array($this->browsers) AND count($this->browsers) > 0) |
---|
189 | { |
---|
190 | foreach ($this->browsers as $key => $val) |
---|
191 | { |
---|
192 | if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match)) |
---|
193 | { |
---|
194 | $this->is_browser = TRUE; |
---|
195 | $this->version = $match[1]; |
---|
196 | $this->browser = $val; |
---|
197 | $this->_set_mobile(); |
---|
198 | return TRUE; |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | return FALSE; |
---|
203 | } |
---|
204 | |
---|
205 | // -------------------------------------------------------------------- |
---|
206 | |
---|
207 | /** |
---|
208 | * Set the Robot |
---|
209 | * |
---|
210 | * @access private |
---|
211 | * @return bool |
---|
212 | */ |
---|
213 | private function _set_robot() |
---|
214 | { |
---|
215 | if (is_array($this->robots) AND count($this->robots) > 0) |
---|
216 | { |
---|
217 | foreach ($this->robots as $key => $val) |
---|
218 | { |
---|
219 | if (preg_match("|".preg_quote($key)."|i", $this->agent)) |
---|
220 | { |
---|
221 | $this->is_robot = TRUE; |
---|
222 | $this->robot = $val; |
---|
223 | return TRUE; |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | return FALSE; |
---|
228 | } |
---|
229 | |
---|
230 | // -------------------------------------------------------------------- |
---|
231 | |
---|
232 | /** |
---|
233 | * Set the Mobile Device |
---|
234 | * |
---|
235 | * @access private |
---|
236 | * @return bool |
---|
237 | */ |
---|
238 | private function _set_mobile() |
---|
239 | { |
---|
240 | if (is_array($this->mobiles) AND count($this->mobiles) > 0) |
---|
241 | { |
---|
242 | foreach ($this->mobiles as $key => $val) |
---|
243 | { |
---|
244 | if (FALSE !== (strpos(strtolower($this->agent), $key))) |
---|
245 | { |
---|
246 | $this->is_mobile = TRUE; |
---|
247 | $this->mobile = $val; |
---|
248 | return TRUE; |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | return FALSE; |
---|
253 | } |
---|
254 | |
---|
255 | // -------------------------------------------------------------------- |
---|
256 | |
---|
257 | /** |
---|
258 | * Set the accepted languages |
---|
259 | * |
---|
260 | * @access private |
---|
261 | * @return void |
---|
262 | */ |
---|
263 | private function _set_languages() |
---|
264 | { |
---|
265 | if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '') |
---|
266 | { |
---|
267 | $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE']))); |
---|
268 | |
---|
269 | $this->languages = explode(',', $languages); |
---|
270 | } |
---|
271 | |
---|
272 | if (count($this->languages) == 0) |
---|
273 | { |
---|
274 | $this->languages = array('Undefined'); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | // -------------------------------------------------------------------- |
---|
279 | |
---|
280 | /** |
---|
281 | * Set the accepted character sets |
---|
282 | * |
---|
283 | * @access private |
---|
284 | * @return void |
---|
285 | */ |
---|
286 | private function _set_charsets() |
---|
287 | { |
---|
288 | if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '') |
---|
289 | { |
---|
290 | $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET']))); |
---|
291 | |
---|
292 | $this->charsets = explode(',', $charsets); |
---|
293 | } |
---|
294 | |
---|
295 | if (count($this->charsets) == 0) |
---|
296 | { |
---|
297 | $this->charsets = array('Undefined'); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | // -------------------------------------------------------------------- |
---|
302 | |
---|
303 | /** |
---|
304 | * Is Browser |
---|
305 | * |
---|
306 | * @access public |
---|
307 | * @return bool |
---|
308 | */ |
---|
309 | public function is_browser($key = NULL) |
---|
310 | { |
---|
311 | if ( ! $this->is_browser) |
---|
312 | { |
---|
313 | return FALSE; |
---|
314 | } |
---|
315 | |
---|
316 | // No need to be specific, it's a browser |
---|
317 | if ($key === NULL) |
---|
318 | { |
---|
319 | return TRUE; |
---|
320 | } |
---|
321 | |
---|
322 | // Check for a specific browser |
---|
323 | return array_key_exists($key, $this->browsers) AND $this->browser === $this->browsers[$key]; |
---|
324 | } |
---|
325 | |
---|
326 | // -------------------------------------------------------------------- |
---|
327 | |
---|
328 | /** |
---|
329 | * Is Robot |
---|
330 | * |
---|
331 | * @access public |
---|
332 | * @return bool |
---|
333 | */ |
---|
334 | public function is_robot($key = NULL) |
---|
335 | { |
---|
336 | if ( ! $this->is_robot) |
---|
337 | { |
---|
338 | return FALSE; |
---|
339 | } |
---|
340 | |
---|
341 | // No need to be specific, it's a robot |
---|
342 | if ($key === NULL) |
---|
343 | { |
---|
344 | return TRUE; |
---|
345 | } |
---|
346 | |
---|
347 | // Check for a specific robot |
---|
348 | return array_key_exists($key, $this->robots) AND $this->robot === $this->robots[$key]; |
---|
349 | } |
---|
350 | |
---|
351 | // -------------------------------------------------------------------- |
---|
352 | |
---|
353 | /** |
---|
354 | * Is Mobile |
---|
355 | * |
---|
356 | * @access public |
---|
357 | * @return bool |
---|
358 | */ |
---|
359 | public function is_mobile($key = NULL) |
---|
360 | { |
---|
361 | if ( ! $this->is_mobile) |
---|
362 | { |
---|
363 | return FALSE; |
---|
364 | } |
---|
365 | |
---|
366 | // No need to be specific, it's a mobile |
---|
367 | if ($key === NULL) |
---|
368 | { |
---|
369 | return TRUE; |
---|
370 | } |
---|
371 | |
---|
372 | // Check for a specific robot |
---|
373 | return array_key_exists($key, $this->mobiles) AND $this->mobile === $this->mobiles[$key]; |
---|
374 | } |
---|
375 | |
---|
376 | // -------------------------------------------------------------------- |
---|
377 | |
---|
378 | /** |
---|
379 | * Is this a referral from another site? |
---|
380 | * |
---|
381 | * @access public |
---|
382 | * @return bool |
---|
383 | */ |
---|
384 | public function is_referral() |
---|
385 | { |
---|
386 | if ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') |
---|
387 | { |
---|
388 | return FALSE; |
---|
389 | } |
---|
390 | return TRUE; |
---|
391 | } |
---|
392 | |
---|
393 | // -------------------------------------------------------------------- |
---|
394 | |
---|
395 | /** |
---|
396 | * Agent String |
---|
397 | * |
---|
398 | * @access public |
---|
399 | * @return string |
---|
400 | */ |
---|
401 | public function agent_string() |
---|
402 | { |
---|
403 | return $this->agent; |
---|
404 | } |
---|
405 | |
---|
406 | // -------------------------------------------------------------------- |
---|
407 | |
---|
408 | /** |
---|
409 | * Get Platform |
---|
410 | * |
---|
411 | * @access public |
---|
412 | * @return string |
---|
413 | */ |
---|
414 | public function platform() |
---|
415 | { |
---|
416 | return $this->platform; |
---|
417 | } |
---|
418 | |
---|
419 | // -------------------------------------------------------------------- |
---|
420 | |
---|
421 | /** |
---|
422 | * Get Browser Name |
---|
423 | * |
---|
424 | * @access public |
---|
425 | * @return string |
---|
426 | */ |
---|
427 | public function browser() |
---|
428 | { |
---|
429 | return $this->browser; |
---|
430 | } |
---|
431 | |
---|
432 | // -------------------------------------------------------------------- |
---|
433 | |
---|
434 | /** |
---|
435 | * Get the Browser Version |
---|
436 | * |
---|
437 | * @access public |
---|
438 | * @return string |
---|
439 | */ |
---|
440 | public function version() |
---|
441 | { |
---|
442 | return $this->version; |
---|
443 | } |
---|
444 | |
---|
445 | // -------------------------------------------------------------------- |
---|
446 | |
---|
447 | /** |
---|
448 | * Get The Robot Name |
---|
449 | * |
---|
450 | * @access public |
---|
451 | * @return string |
---|
452 | */ |
---|
453 | public function robot() |
---|
454 | { |
---|
455 | return $this->robot; |
---|
456 | } |
---|
457 | // -------------------------------------------------------------------- |
---|
458 | |
---|
459 | /** |
---|
460 | * Get the Mobile Device |
---|
461 | * |
---|
462 | * @access public |
---|
463 | * @return string |
---|
464 | */ |
---|
465 | public function mobile() |
---|
466 | { |
---|
467 | return $this->mobile; |
---|
468 | } |
---|
469 | |
---|
470 | // -------------------------------------------------------------------- |
---|
471 | |
---|
472 | /** |
---|
473 | * Get the referrer |
---|
474 | * |
---|
475 | * @access public |
---|
476 | * @return bool |
---|
477 | */ |
---|
478 | public function referrer() |
---|
479 | { |
---|
480 | return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']); |
---|
481 | } |
---|
482 | |
---|
483 | // -------------------------------------------------------------------- |
---|
484 | |
---|
485 | /** |
---|
486 | * Get the accepted languages |
---|
487 | * |
---|
488 | * @access public |
---|
489 | * @return array |
---|
490 | */ |
---|
491 | public function languages() |
---|
492 | { |
---|
493 | if (count($this->languages) == 0) |
---|
494 | { |
---|
495 | $this->_set_languages(); |
---|
496 | } |
---|
497 | |
---|
498 | return $this->languages; |
---|
499 | } |
---|
500 | |
---|
501 | // -------------------------------------------------------------------- |
---|
502 | |
---|
503 | /** |
---|
504 | * Get the accepted Character Sets |
---|
505 | * |
---|
506 | * @access public |
---|
507 | * @return array |
---|
508 | */ |
---|
509 | public function charsets() |
---|
510 | { |
---|
511 | if (count($this->charsets) == 0) |
---|
512 | { |
---|
513 | $this->_set_charsets(); |
---|
514 | } |
---|
515 | |
---|
516 | return $this->charsets; |
---|
517 | } |
---|
518 | |
---|
519 | // -------------------------------------------------------------------- |
---|
520 | |
---|
521 | /** |
---|
522 | * Test for a particular language |
---|
523 | * |
---|
524 | * @access public |
---|
525 | * @return bool |
---|
526 | */ |
---|
527 | public function accept_lang($lang = 'en') |
---|
528 | { |
---|
529 | return (in_array(strtolower($lang), $this->languages(), TRUE)); |
---|
530 | } |
---|
531 | |
---|
532 | // -------------------------------------------------------------------- |
---|
533 | |
---|
534 | /** |
---|
535 | * Test for a particular character set |
---|
536 | * |
---|
537 | * @access public |
---|
538 | * @return bool |
---|
539 | */ |
---|
540 | public function accept_charset($charset = 'utf-8') |
---|
541 | { |
---|
542 | return (in_array(strtolower($charset), $this->charsets(), TRUE)); |
---|
543 | } |
---|
544 | |
---|
545 | } |
---|
546 | |
---|
547 | |
---|
548 | /* End of file User_agent.php */ |
---|
549 | /* Location: ./system/libraries/User_agent.php */ |
---|