admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 1 | <?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 | */ |
| 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; |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 36 | |
| 37 | var $languages = array(); |
| 38 | var $charsets = array(); |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 39 | |
| 40 | var $platform = ''; |
| 41 | var $browser = ''; |
| 42 | var $version = ''; |
| 43 | var $moble = ''; |
| 44 | var $robot = ''; |
| 45 | |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 46 | |
| 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 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 64 | if ($this->_load_agent_file()) |
| 65 | { |
| 66 | $this->_compile_data(); |
| 67 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 70 | |
| 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 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 120 | |
| 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 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 152 | if (is_array($this->platforms) AND count($this->platforms) > 0) |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 153 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 154 | foreach ($this->platforms as $key => $val) |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 155 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 156 | if (preg_match("|".preg_quote($key)."|i", $this->agent)) |
| 157 | { |
| 158 | $this->platform = $val; |
| 159 | return TRUE; |
| 160 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 163 | $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 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 176 | 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 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 189 | return FALSE; |
| 190 | } |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 191 | |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 192 | // -------------------------------------------------------------------- |
| 193 | |
| 194 | /** |
| 195 | * Set the Robot |
| 196 | * |
| 197 | * @access private |
| 198 | * @return bool |
| 199 | */ |
| 200 | function _set_robot() |
| 201 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 202 | if (is_array($this->robots) AND count($this->robots) > 0) |
| 203 | { |
| 204 | foreach ($this->robots as $key => $val) |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 205 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 206 | if (preg_match("|".preg_quote($key)."|i", $this->agent)) |
| 207 | { |
| 208 | $this->is_robot = TRUE; |
| 209 | $this->robot = $val; |
| 210 | return TRUE; |
| 211 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 214 | return FALSE; |
| 215 | } |
| 216 | |
| 217 | // -------------------------------------------------------------------- |
| 218 | |
| 219 | /** |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 220 | * Set the Mobile Device |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 221 | * |
| 222 | * @access private |
| 223 | * @return bool |
| 224 | */ |
| 225 | function _set_mobile() |
| 226 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 227 | if (is_array($this->mobiles) AND count($this->mobiles) > 0) |
| 228 | { |
| 229 | foreach ($this->mobiles as $key => $val) |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 230 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 231 | if (FALSE !== (strpos(strtolower($this->agent), $key))) |
| 232 | { |
| 233 | $this->is_mobile = TRUE; |
| 234 | $this->mobile = $val; |
| 235 | return TRUE; |
| 236 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 237 | } |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 238 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 239 | return FALSE; |
| 240 | } |
| 241 | |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 242 | // -------------------------------------------------------------------- |
| 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 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 254 | $languages = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])); |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 255 | |
| 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 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 277 | $charsets = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_CHARSET'])); |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 278 | |
| 279 | $this->charsets = explode(',', $charsets); |
| 280 | } |
| 281 | |
| 282 | if (count($this->charsets) == 0) |
| 283 | { |
| 284 | $this->charsets = array('Undefined'); |
| 285 | } |
| 286 | } |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 287 | |
| 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 | /** |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 330 | * 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 | /** |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 356 | * Get Platform |
| 357 | * |
| 358 | * @access public |
| 359 | * @return string |
| 360 | */ |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 361 | function platform() |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 362 | { |
| 363 | return $this->platform; |
| 364 | } |
| 365 | |
| 366 | // -------------------------------------------------------------------- |
| 367 | |
| 368 | /** |
| 369 | * Get Browser Name |
| 370 | * |
| 371 | * @access public |
| 372 | * @return string |
| 373 | */ |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 374 | function browser() |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 375 | { |
| 376 | return $this->browser; |
| 377 | } |
| 378 | |
| 379 | // -------------------------------------------------------------------- |
| 380 | |
| 381 | /** |
| 382 | * Get the Browser Version |
| 383 | * |
| 384 | * @access public |
| 385 | * @return string |
| 386 | */ |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 387 | function version() |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 388 | { |
| 389 | return $this->version; |
| 390 | } |
| 391 | |
| 392 | // -------------------------------------------------------------------- |
| 393 | |
| 394 | /** |
| 395 | * Get The Robot Name |
| 396 | * |
| 397 | * @access public |
| 398 | * @return string |
| 399 | */ |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 400 | function robot() |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 401 | { |
| 402 | return $this->robot; |
| 403 | } |
| 404 | // -------------------------------------------------------------------- |
| 405 | |
| 406 | /** |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 407 | * Get the Mobile Device |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 408 | * |
| 409 | * @access public |
| 410 | * @return string |
| 411 | */ |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 412 | function mobile() |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 413 | { |
| 414 | return $this->mobile; |
| 415 | } |
| 416 | |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 417 | // -------------------------------------------------------------------- |
| 418 | |
| 419 | /** |
| 420 | * Get the referrer |
| 421 | * |
| 422 | * @access public |
| 423 | * @return bool |
| 424 | */ |
| 425 | function referrer() |
| 426 | { |
admin | 3822194 | 2006-10-09 17:34:19 +0000 | [diff] [blame^] | 427 | return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']); |
admin | 6c9f736 | 2006-10-09 03:33:48 +0000 | [diff] [blame] | 428 | } |
| 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 | |
admin | df752b2 | 2006-10-08 07:17:13 +0000 | [diff] [blame] | 492 | |
| 493 | } |
| 494 | |
| 495 | ?> |