Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Rick Ellis | e4a7db4 | 2008-09-12 23:35:09 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 26 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 27 | * @link http://codeigniter.com/user_guide/libraries/user_agent.html
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 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 | function CI_User_agent()
|
| 60 | {
|
| 61 | if (isset($_SERVER['HTTP_USER_AGENT']))
|
| 62 | {
|
| 63 | $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
|
| 64 | }
|
| 65 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 66 | if ( ! is_null($this->agent))
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 67 | {
|
| 68 | if ($this->_load_agent_file())
|
| 69 | {
|
| 70 | $this->_compile_data();
|
| 71 | }
|
| 72 | }
|
| 73 |
|
Derek Allard | c39a41f | 2007-07-11 17:26:21 +0000 | [diff] [blame] | 74 | log_message('debug', "User Agent Class Initialized");
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 75 | }
|
| 76 |
|
| 77 | // --------------------------------------------------------------------
|
| 78 |
|
| 79 | /**
|
| 80 | * Compile the User Agent Data
|
| 81 | *
|
| 82 | * @access private
|
| 83 | * @return bool
|
| 84 | */
|
| 85 | function _load_agent_file()
|
| 86 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 87 | if ( ! @include(APPPATH.'config/user_agents'.EXT))
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 88 | {
|
| 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 | {
|
| 110 | $this->mobiles = $mobiles;
|
| 111 | unset($mobiles);
|
| 112 | $return = TRUE;
|
| 113 | }
|
| 114 |
|
| 115 | if (isset($robots))
|
| 116 | {
|
| 117 | $this->robots = $robots;
|
| 118 | unset($robots);
|
| 119 | $return = TRUE;
|
| 120 | }
|
| 121 |
|
| 122 | return $return;
|
| 123 | }
|
| 124 |
|
| 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 | {
|
| 156 | if (is_array($this->platforms) AND count($this->platforms) > 0)
|
| 157 | {
|
| 158 | foreach ($this->platforms as $key => $val)
|
| 159 | {
|
| 160 | if (preg_match("|".preg_quote($key)."|i", $this->agent))
|
| 161 | {
|
| 162 | $this->platform = $val;
|
| 163 | return TRUE;
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 | $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 | {
|
| 180 | 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 | $this->_set_mobile();
|
| 190 | return TRUE;
|
| 191 | }
|
| 192 | }
|
| 193 | }
|
| 194 | return FALSE;
|
| 195 | }
|
| 196 |
|
| 197 | // --------------------------------------------------------------------
|
| 198 |
|
| 199 | /**
|
| 200 | * Set the Robot
|
| 201 | *
|
| 202 | * @access private
|
| 203 | * @return bool
|
| 204 | */
|
| 205 | function _set_robot()
|
| 206 | {
|
| 207 | if (is_array($this->robots) AND count($this->robots) > 0)
|
| 208 | {
|
| 209 | foreach ($this->robots as $key => $val)
|
| 210 | {
|
| 211 | if (preg_match("|".preg_quote($key)."|i", $this->agent))
|
| 212 | {
|
| 213 | $this->is_robot = TRUE;
|
| 214 | $this->robot = $val;
|
| 215 | return TRUE;
|
| 216 | }
|
| 217 | }
|
| 218 | }
|
| 219 | return FALSE;
|
| 220 | }
|
| 221 |
|
| 222 | // --------------------------------------------------------------------
|
| 223 |
|
| 224 | /**
|
| 225 | * Set the Mobile Device
|
| 226 | *
|
| 227 | * @access private
|
| 228 | * @return bool
|
| 229 | */
|
| 230 | function _set_mobile()
|
| 231 | {
|
| 232 | if (is_array($this->mobiles) AND count($this->mobiles) > 0)
|
| 233 | {
|
| 234 | foreach ($this->mobiles as $key => $val)
|
| 235 | {
|
| 236 | if (FALSE !== (strpos(strtolower($this->agent), $key)))
|
| 237 | {
|
| 238 | $this->is_mobile = TRUE;
|
| 239 | $this->mobile = $val;
|
| 240 | return TRUE;
|
| 241 | }
|
| 242 | }
|
| 243 | }
|
| 244 | return FALSE;
|
| 245 | }
|
| 246 |
|
| 247 | // --------------------------------------------------------------------
|
| 248 |
|
| 249 | /**
|
| 250 | * Set the accepted languages
|
| 251 | *
|
| 252 | * @access private
|
| 253 | * @return void
|
| 254 | */
|
| 255 | function _set_languages()
|
| 256 | {
|
| 257 | if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
|
| 258 | {
|
Derek Allard | 86abae9 | 2008-01-22 07:35:40 +0000 | [diff] [blame] | 259 | $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 260 |
|
| 261 | $this->languages = explode(',', $languages);
|
| 262 | }
|
| 263 |
|
| 264 | if (count($this->languages) == 0)
|
| 265 | {
|
| 266 | $this->languages = array('Undefined');
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | // --------------------------------------------------------------------
|
| 271 |
|
| 272 | /**
|
| 273 | * Set the accepted character sets
|
| 274 | *
|
| 275 | * @access private
|
| 276 | * @return void
|
| 277 | */
|
| 278 | function _set_charsets()
|
| 279 | {
|
| 280 | if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
|
| 281 | {
|
Derek Allard | a070db9 | 2008-01-05 20:30:22 +0000 | [diff] [blame] | 282 | $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 283 |
|
| 284 | $this->charsets = explode(',', $charsets);
|
| 285 | }
|
| 286 |
|
| 287 | if (count($this->charsets) == 0)
|
| 288 | {
|
| 289 | $this->charsets = array('Undefined');
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 | // --------------------------------------------------------------------
|
| 294 |
|
| 295 | /**
|
| 296 | * Is Browser
|
| 297 | *
|
| 298 | * @access public
|
| 299 | * @return bool
|
| 300 | */
|
| 301 | function is_browser()
|
| 302 | {
|
| 303 | return $this->is_browser;
|
| 304 | }
|
| 305 |
|
| 306 | // --------------------------------------------------------------------
|
| 307 |
|
| 308 | /**
|
| 309 | * Is Robot
|
| 310 | *
|
| 311 | * @access public
|
| 312 | * @return bool
|
| 313 | */
|
| 314 | function is_robot()
|
| 315 | {
|
| 316 | return $this->is_robot;
|
| 317 | }
|
| 318 |
|
| 319 | // --------------------------------------------------------------------
|
| 320 |
|
| 321 | /**
|
| 322 | * Is Mobile
|
| 323 | *
|
| 324 | * @access public
|
| 325 | * @return bool
|
| 326 | */
|
| 327 | function is_mobile()
|
| 328 | {
|
| 329 | return $this->is_mobile;
|
| 330 | }
|
| 331 |
|
| 332 | // --------------------------------------------------------------------
|
| 333 |
|
| 334 | /**
|
| 335 | * Is this a referral from another site?
|
| 336 | *
|
| 337 | * @access public
|
| 338 | * @return bool
|
| 339 | */
|
| 340 | function is_referral()
|
| 341 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 342 | return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 343 | }
|
| 344 |
|
| 345 | // --------------------------------------------------------------------
|
| 346 |
|
| 347 | /**
|
| 348 | * Agent String
|
| 349 | *
|
| 350 | * @access public
|
| 351 | * @return string
|
| 352 | */
|
| 353 | function agent_string()
|
| 354 | {
|
| 355 | return $this->agent;
|
| 356 | }
|
| 357 |
|
| 358 | // --------------------------------------------------------------------
|
| 359 |
|
| 360 | /**
|
| 361 | * Get Platform
|
| 362 | *
|
| 363 | * @access public
|
| 364 | * @return string
|
| 365 | */
|
| 366 | function platform()
|
| 367 | {
|
| 368 | return $this->platform;
|
| 369 | }
|
| 370 |
|
| 371 | // --------------------------------------------------------------------
|
| 372 |
|
| 373 | /**
|
| 374 | * Get Browser Name
|
| 375 | *
|
| 376 | * @access public
|
| 377 | * @return string
|
| 378 | */
|
| 379 | function browser()
|
| 380 | {
|
| 381 | return $this->browser;
|
| 382 | }
|
| 383 |
|
| 384 | // --------------------------------------------------------------------
|
| 385 |
|
| 386 | /**
|
| 387 | * Get the Browser Version
|
| 388 | *
|
| 389 | * @access public
|
| 390 | * @return string
|
| 391 | */
|
| 392 | function version()
|
| 393 | {
|
| 394 | return $this->version;
|
| 395 | }
|
| 396 |
|
| 397 | // --------------------------------------------------------------------
|
| 398 |
|
| 399 | /**
|
| 400 | * Get The Robot Name
|
| 401 | *
|
| 402 | * @access public
|
| 403 | * @return string
|
| 404 | */
|
| 405 | function robot()
|
| 406 | {
|
| 407 | return $this->robot;
|
| 408 | }
|
| 409 | // --------------------------------------------------------------------
|
| 410 |
|
| 411 | /**
|
| 412 | * Get the Mobile Device
|
| 413 | *
|
| 414 | * @access public
|
| 415 | * @return string
|
| 416 | */
|
| 417 | function mobile()
|
| 418 | {
|
| 419 | return $this->mobile;
|
| 420 | }
|
| 421 |
|
| 422 | // --------------------------------------------------------------------
|
| 423 |
|
| 424 | /**
|
| 425 | * Get the referrer
|
| 426 | *
|
| 427 | * @access public
|
| 428 | * @return bool
|
| 429 | */
|
| 430 | function referrer()
|
| 431 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 432 | return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
|
Derek Allard | c250b14 | 2007-01-27 17:56:40 +0000 | [diff] [blame] | 433 | }
|
| 434 |
|
| 435 | // --------------------------------------------------------------------
|
| 436 |
|
| 437 | /**
|
| 438 | * Get the accepted languages
|
| 439 | *
|
| 440 | * @access public
|
| 441 | * @return array
|
| 442 | */
|
| 443 | function languages()
|
| 444 | {
|
| 445 | if (count($this->languages) == 0)
|
| 446 | {
|
| 447 | $this->_set_languages();
|
| 448 | }
|
| 449 |
|
| 450 | return $this->languages;
|
| 451 | }
|
| 452 |
|
| 453 | // --------------------------------------------------------------------
|
| 454 |
|
| 455 | /**
|
| 456 | * Get the accepted Character Sets
|
| 457 | *
|
| 458 | * @access public
|
| 459 | * @return array
|
| 460 | */
|
| 461 | function charsets()
|
| 462 | {
|
| 463 | if (count($this->charsets) == 0)
|
| 464 | {
|
| 465 | $this->_set_charsets();
|
| 466 | }
|
| 467 |
|
| 468 | return $this->charsets;
|
| 469 | }
|
| 470 |
|
| 471 | // --------------------------------------------------------------------
|
| 472 |
|
| 473 | /**
|
| 474 | * Test for a particular language
|
| 475 | *
|
| 476 | * @access public
|
| 477 | * @return bool
|
| 478 | */
|
| 479 | function accept_lang($lang = 'en')
|
| 480 | {
|
| 481 | return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
|
| 482 | }
|
| 483 |
|
| 484 | // --------------------------------------------------------------------
|
| 485 |
|
| 486 | /**
|
| 487 | * Test for a particular character set
|
| 488 | *
|
| 489 | * @access public
|
| 490 | * @return bool
|
| 491 | */
|
| 492 | function accept_charset($charset = 'utf-8')
|
| 493 | {
|
| 494 | return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
|
| 495 | }
|
| 496 |
|
| 497 |
|
| 498 | }
|
| 499 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 500 |
|
| 501 | /* End of file User_agent.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 502 | /* Location: ./system/libraries/User_agent.php */ |