PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/User_agent.php

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