/fuel/codeigniter/libraries/User_agent.php

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