User_agent.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class CI_User_agent {
00030
00031 var $agent = NULL;
00032
00033 var $is_browser = FALSE;
00034 var $is_robot = FALSE;
00035 var $is_mobile = FALSE;
00036
00037 var $languages = array();
00038 var $charsets = array();
00039
00040 var $platforms = array();
00041 var $browsers = array();
00042 var $mobiles = array();
00043 var $robots = array();
00044
00045 var $platform = '';
00046 var $browser = '';
00047 var $version = '';
00048 var $mobile = '';
00049 var $robot = '';
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 function CI_User_agent()
00060 {
00061 if (isset($_SERVER['HTTP_USER_AGENT']))
00062 {
00063 $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
00064 }
00065
00066 if ( ! is_null($this->agent))
00067 {
00068 if ($this->_load_agent_file())
00069 {
00070 $this->_compile_data();
00071 }
00072 }
00073
00074 log_message('debug', "User Agent Class Initialized");
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 function _load_agent_file()
00086 {
00087 if ( ! @include(APPPATH.'config/user_agents'.EXT))
00088 {
00089 return FALSE;
00090 }
00091
00092 $return = FALSE;
00093
00094 if (isset($platforms))
00095 {
00096 $this->platforms = $platforms;
00097 unset($platforms);
00098 $return = TRUE;
00099 }
00100
00101 if (isset($browsers))
00102 {
00103 $this->browsers = $browsers;
00104 unset($browsers);
00105 $return = TRUE;
00106 }
00107
00108 if (isset($mobiles))
00109 {
00110 $this->mobiles = $mobiles;
00111 unset($mobiles);
00112 $return = TRUE;
00113 }
00114
00115 if (isset($robots))
00116 {
00117 $this->robots = $robots;
00118 unset($robots);
00119 $return = TRUE;
00120 }
00121
00122 return $return;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 function _compile_data()
00134 {
00135 $this->_set_platform();
00136
00137 foreach (array('_set_browser', '_set_robot', '_set_mobile') as $function)
00138 {
00139 if ($this->$function() === TRUE)
00140 {
00141 break;
00142 }
00143 }
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 function _set_platform()
00155 {
00156 if (is_array($this->platforms) AND count($this->platforms) > 0)
00157 {
00158 foreach ($this->platforms as $key => $val)
00159 {
00160 if (preg_match("|".preg_quote($key)."|i", $this->agent))
00161 {
00162 $this->platform = $val;
00163 return TRUE;
00164 }
00165 }
00166 }
00167 $this->platform = 'Unknown Platform';
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 function _set_browser()
00179 {
00180 if (is_array($this->browsers) AND count($this->browsers) > 0)
00181 {
00182 foreach ($this->browsers as $key => $val)
00183 {
00184 if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
00185 {
00186 $this->is_browser = TRUE;
00187 $this->version = $match[1];
00188 $this->browser = $val;
00189 $this->_set_mobile();
00190 return TRUE;
00191 }
00192 }
00193 }
00194 return FALSE;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 function _set_robot()
00206 {
00207 if (is_array($this->robots) AND count($this->robots) > 0)
00208 {
00209 foreach ($this->robots as $key => $val)
00210 {
00211 if (preg_match("|".preg_quote($key)."|i", $this->agent))
00212 {
00213 $this->is_robot = TRUE;
00214 $this->robot = $val;
00215 return TRUE;
00216 }
00217 }
00218 }
00219 return FALSE;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 function _set_mobile()
00231 {
00232 if (is_array($this->mobiles) AND count($this->mobiles) > 0)
00233 {
00234 foreach ($this->mobiles as $key => $val)
00235 {
00236 if (FALSE !== (strpos(strtolower($this->agent), $key)))
00237 {
00238 $this->is_mobile = TRUE;
00239 $this->mobile = $val;
00240 return TRUE;
00241 }
00242 }
00243 }
00244 return FALSE;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 function _set_languages()
00256 {
00257 if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
00258 {
00259 $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
00260
00261 $this->languages = explode(',', $languages);
00262 }
00263
00264 if (count($this->languages) == 0)
00265 {
00266 $this->languages = array('Undefined');
00267 }
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 function _set_charsets()
00279 {
00280 if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
00281 {
00282 $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
00283
00284 $this->charsets = explode(',', $charsets);
00285 }
00286
00287 if (count($this->charsets) == 0)
00288 {
00289 $this->charsets = array('Undefined');
00290 }
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301 function is_browser()
00302 {
00303 return $this->is_browser;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 function is_robot()
00315 {
00316 return $this->is_robot;
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 function is_mobile()
00328 {
00329 return $this->is_mobile;
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 function is_referral()
00341 {
00342 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 function agent_string()
00354 {
00355 return $this->agent;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 function platform()
00367 {
00368 return $this->platform;
00369 }
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 function browser()
00380 {
00381 return $this->browser;
00382 }
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 function version()
00393 {
00394 return $this->version;
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 function robot()
00406 {
00407 return $this->robot;
00408 }
00409
00410
00411
00412
00413
00414
00415
00416
00417 function mobile()
00418 {
00419 return $this->mobile;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 function referrer()
00431 {
00432 return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 function languages()
00444 {
00445 if (count($this->languages) == 0)
00446 {
00447 $this->_set_languages();
00448 }
00449
00450 return $this->languages;
00451 }
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461 function charsets()
00462 {
00463 if (count($this->charsets) == 0)
00464 {
00465 $this->_set_charsets();
00466 }
00467
00468 return $this->charsets;
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 function accept_lang($lang = 'en')
00480 {
00481 return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492 function accept_charset($charset = 'utf-8')
00493 {
00494 return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
00495 }
00496
00497
00498 }
00499
00500
00501
00502