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