PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/User_agent.php

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