PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/convert/system/libraries/User_agent.php

https://github.com/usagi-project/mynets1
PHP | 500 lines | 235 code | 71 blank | 194 comment | 34 complexity | eec51ddb5cf05ee85eb421f49efe70be 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 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2006, 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. function CI_User_agent()
  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. 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. 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. 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. 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. 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. 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. 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. 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. function is_browser()
  263. {
  264. return $this->is_browser;
  265. }
  266. // --------------------------------------------------------------------
  267. /**
  268. * Is Robot
  269. *
  270. * @access public
  271. * @return bool
  272. */
  273. function is_robot()
  274. {
  275. return $this->is_robot;
  276. }
  277. // --------------------------------------------------------------------
  278. /**
  279. * Is Mobile
  280. *
  281. * @access public
  282. * @return bool
  283. */
  284. function is_mobile()
  285. {
  286. return $this->is_mobile;
  287. }
  288. // --------------------------------------------------------------------
  289. /**
  290. * Is this a referral from another site?
  291. *
  292. * @access public
  293. * @return bool
  294. */
  295. function is_referral()
  296. {
  297. return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
  298. }
  299. // --------------------------------------------------------------------
  300. /**
  301. * Agent String
  302. *
  303. * @access public
  304. * @return string
  305. */
  306. function agent_string()
  307. {
  308. return $this->agent;
  309. }
  310. // --------------------------------------------------------------------
  311. /**
  312. * Get Platform
  313. *
  314. * @access public
  315. * @return string
  316. */
  317. function platform()
  318. {
  319. return $this->platform;
  320. }
  321. // --------------------------------------------------------------------
  322. /**
  323. * Get Browser Name
  324. *
  325. * @access public
  326. * @return string
  327. */
  328. function browser()
  329. {
  330. return $this->browser;
  331. }
  332. // --------------------------------------------------------------------
  333. /**
  334. * Get the Browser Version
  335. *
  336. * @access public
  337. * @return string
  338. */
  339. function version()
  340. {
  341. return $this->version;
  342. }
  343. // --------------------------------------------------------------------
  344. /**
  345. * Get The Robot Name
  346. *
  347. * @access public
  348. * @return string
  349. */
  350. function robot()
  351. {
  352. return $this->robot;
  353. }
  354. // --------------------------------------------------------------------
  355. /**
  356. * Get the Mobile Device
  357. *
  358. * @access public
  359. * @return string
  360. */
  361. function mobile()
  362. {
  363. return $this->mobile;
  364. }
  365. // --------------------------------------------------------------------
  366. /**
  367. * Get the referrer
  368. *
  369. * @access public
  370. * @return bool
  371. */
  372. function referrer()
  373. {
  374. return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
  375. }
  376. // --------------------------------------------------------------------
  377. /**
  378. * Get the accepted languages
  379. *
  380. * @access public
  381. * @return array
  382. */
  383. function languages()
  384. {
  385. if (count($this->languages) == 0)
  386. {
  387. $this->_set_languages();
  388. }
  389. return $this->languages;
  390. }
  391. // --------------------------------------------------------------------
  392. /**
  393. * Get the accepted Character Sets
  394. *
  395. * @access public
  396. * @return array
  397. */
  398. function charsets()
  399. {
  400. if (count($this->charsets) == 0)
  401. {
  402. $this->_set_charsets();
  403. }
  404. return $this->charsets;
  405. }
  406. // --------------------------------------------------------------------
  407. /**
  408. * Test for a particular language
  409. *
  410. * @access public
  411. * @return bool
  412. */
  413. function accept_lang($lang = 'en')
  414. {
  415. return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
  416. }
  417. // --------------------------------------------------------------------
  418. /**
  419. * Test for a particular character set
  420. *
  421. * @access public
  422. * @return bool
  423. */
  424. function accept_charset($charset = 'utf-8')
  425. {
  426. return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
  427. }
  428. }
  429. ?>