PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendors/php_speedy/libs/php/user_agent.php

http://scp-soft.googlecode.com/
PHP | 588 lines | 318 code | 75 blank | 195 comment | 32 complexity | c82f74583e91b24b4bb7c00a9c1eba70 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * User Agent Class
  4. *
  5. * Identifies the platform, browser, robot, or mobile devise of the browsing agent
  6. *
  7. * @package Kohana
  8. * @subpackage Libraries
  9. * @category User Agent
  10. * @author Rick Ellis
  11. * @link http://kohanaphp.com/user_guide/libraries/user_agent.html
  12. */
  13. class _speedy_User_agent {
  14. var $agent = NULL;
  15. var $is_browser = FALSE;
  16. var $is_robot = FALSE;
  17. var $is_mobile = FALSE;
  18. var $languages = array();
  19. var $charsets = array();
  20. var $platforms = array();
  21. var $browsers = array();
  22. var $mobiles = array();
  23. var $robots = array();
  24. var $platform = '';
  25. var $browser = '';
  26. var $version = '';
  27. var $mobile = '';
  28. var $robot = '';
  29. /**
  30. * Constructor
  31. *
  32. * Sets the User Agent and runs the compilation routine
  33. *
  34. * @access public
  35. * @return void
  36. */
  37. function _speedy_User_agent()
  38. {
  39. if (isset($_SERVER['HTTP_USER_AGENT']))
  40. {
  41. $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
  42. }
  43. if ( ! is_null($this->agent))
  44. {
  45. if ($this->_load_agent_file())
  46. {
  47. $this->_compile_data();
  48. }
  49. }
  50. //log_message('debug', "Table Class Initialized");
  51. }
  52. // --------------------------------------------------------------------
  53. /**
  54. * Compile the User Agent Data
  55. *
  56. * @access private
  57. * @return bool
  58. */
  59. function _load_agent_file()
  60. {
  61. /*
  62. | -------------------------------------------------------------------
  63. | USER AGENT TYPES
  64. | -------------------------------------------------------------------
  65. | This file contains four arrays of user agent data. It is used by the
  66. | User Agent Class to help identify browser, platform, robot, and
  67. | mobile device data. The array keys are used to identify the device
  68. | and the array values are used to set the actual name of the item.
  69. |
  70. */
  71. $platforms = array(
  72. 'windows nt 6.0' => 'Windows Vista',
  73. 'windows nt 5.2' => 'Windows 2003',
  74. 'windows nt 5.0' => 'Windows 2000',
  75. 'windows nt 5.1' => 'Windows XP',
  76. 'windows nt 4.0' => 'Windows NT',
  77. 'winnt4.0' => 'Windows NT',
  78. 'winnt 4.0' => 'Windows NT',
  79. 'winnt' => 'Windows NT',
  80. 'windows 98' => 'Windows 98',
  81. 'win98' => 'Windows 98',
  82. 'windows 95' => 'Windows 95',
  83. 'win95' => 'Windows 95',
  84. 'windows' => 'Unknown Windows OS',
  85. 'os x' => 'Mac OS X',
  86. 'intel mac' => 'Intel Mac',
  87. 'ppc mac' => 'PowerPC Mac',
  88. 'powerpc' => 'PowerPC',
  89. 'ppc' => 'PowerPC',
  90. 'cygwin' => 'Cygwin',
  91. 'linux' => 'Linux',
  92. 'debian' => 'Debian',
  93. 'openvms' => 'OpenVMS',
  94. 'sunos' => 'Sun Solaris',
  95. 'amiga' => 'Amiga',
  96. 'beos' => 'BeOS',
  97. 'apachebench' => 'ApacheBench',
  98. 'freebsd' => 'FreeBSD',
  99. 'netbsd' => 'NetBSD',
  100. 'bsdi' => 'BSDi',
  101. 'openbsd' => 'OpenBSD',
  102. 'os/2' => 'OS/2',
  103. 'warp' => 'OS/2',
  104. 'aix' => 'AIX',
  105. 'irix' => 'Irix',
  106. 'osf' => 'DEC OSF',
  107. 'hp-ux' => 'HP-UX',
  108. 'hurd' => 'GNU/Hurd',
  109. 'unix' => 'Unknown Unix OS',
  110. );
  111. // The order of this array should NOT be changed. Many browsers return
  112. // multiple browser types so we want to identify the sub-type first.
  113. $browsers = array(
  114. 'Opera' => 'Opera',
  115. 'MSIE' => 'Internet Explorer',
  116. 'Internet Explorer' => 'Internet Explorer',
  117. 'Shiira' => 'Shiira',
  118. 'Firefox' => 'Firefox',
  119. 'Chrome' => 'Google Chrome',
  120. 'Chimera' => 'Chimera',
  121. 'Phoenix' => 'Phoenix',
  122. 'Firebird' => 'Firebird',
  123. 'Camino' => 'Camino',
  124. 'Netscape' => 'Netscape',
  125. 'OmniWeb' => 'OmniWeb',
  126. 'Mozilla' => 'Mozilla',
  127. 'Safari' => 'Safari',
  128. 'Konqueror' => 'Konqueror',
  129. 'icab' => 'iCab',
  130. 'Lynx' => 'Lynx',
  131. 'Links' => 'Links',
  132. 'hotjava' => 'HotJava',
  133. 'amaya' => 'Amaya',
  134. 'IBrowse' => 'IBrowse'
  135. );
  136. $mobiles = array(
  137. 'mobileexplorer' => 'Mobile Explorer',
  138. 'openwave' => 'Open Wave',
  139. 'opera mini' => 'Opera Mini',
  140. 'operamini' => 'Opera Mini',
  141. 'elaine' => 'Palm',
  142. 'palmsource' => 'Palm',
  143. 'digital paths' => 'Palm',
  144. 'avantgo' => 'Avantgo',
  145. 'xiino' => 'Xiino',
  146. 'palmscape' => 'Palmscape',
  147. 'nokia' => 'Nokia',
  148. 'ericsson' => 'Ericsson',
  149. 'blackBerry' => 'BlackBerry',
  150. 'motorola' => 'Motorola'
  151. );
  152. // There are hundreds of bots but these are the most common.
  153. $robots = array(
  154. 'googlebot' => 'Googlebot',
  155. 'msnbot' => 'MSNBot',
  156. 'slurp' => 'Inktomi Slurp',
  157. 'yahoo' => 'Yahoo',
  158. 'askjeeves' => 'AskJeeves',
  159. 'fastcrawler' => 'FastCrawler',
  160. 'infoseek' => 'InfoSeek Robot 1.0',
  161. 'lycos' => 'Lycos'
  162. );
  163. $return = FALSE;
  164. if (isset($platforms))
  165. {
  166. $this->platforms = $platforms;
  167. unset($platforms);
  168. $return = TRUE;
  169. }
  170. if (isset($browsers))
  171. {
  172. $this->browsers = $browsers;
  173. unset($browsers);
  174. $return = TRUE;
  175. }
  176. if (isset($mobiles))
  177. {
  178. $this->mobiles = $mobiles;
  179. unset($mobiles);
  180. $return = TRUE;
  181. }
  182. if (isset($robots))
  183. {
  184. $this->robots = $robots;
  185. unset($robots);
  186. $return = TRUE;
  187. }
  188. return $return;
  189. }
  190. // --------------------------------------------------------------------
  191. /**
  192. * Compile the User Agent Data
  193. *
  194. * @access private
  195. * @return bool
  196. */
  197. function _compile_data()
  198. {
  199. $this->_set_platform();
  200. foreach (array('_set_browser', '_set_robot', '_set_mobile', '_set_languages', '_set_charsets') as $function)
  201. {
  202. if ($this->$function() === TRUE)
  203. {
  204. //break;
  205. }
  206. }
  207. }
  208. // --------------------------------------------------------------------
  209. /**
  210. * Set the Platform
  211. *
  212. * @access private
  213. * @return mixed
  214. */
  215. function _set_platform()
  216. {
  217. if (is_array($this->platforms) AND count($this->platforms) > 0)
  218. {
  219. foreach ($this->platforms as $key => $val)
  220. {
  221. if (preg_match("|".preg_quote($key)."|i", $this->agent))
  222. {
  223. $this->platform = $val;
  224. return TRUE;
  225. }
  226. }
  227. }
  228. $this->platform = 'Unknown Platform';
  229. }
  230. // --------------------------------------------------------------------
  231. /**
  232. * Set the Browser
  233. *
  234. * @access private
  235. * @return bool
  236. */
  237. function _set_browser()
  238. {
  239. if (is_array($this->browsers) AND count($this->browsers) > 0)
  240. {
  241. foreach ($this->browsers as $key => $val)
  242. {
  243. if (preg_match("|".preg_quote($key).".*?([0-9\.]+)|i", $this->agent, $match))
  244. {
  245. $this->is_browser = TRUE;
  246. $this->version = $match[1];
  247. $this->browser = $val;
  248. $this->_set_mobile();
  249. return TRUE;
  250. }
  251. }
  252. }
  253. return FALSE;
  254. }
  255. // --------------------------------------------------------------------
  256. /**
  257. * Set the Robot
  258. *
  259. * @access private
  260. * @return bool
  261. */
  262. function _set_robot()
  263. {
  264. if (is_array($this->robots) AND count($this->robots) > 0)
  265. {
  266. foreach ($this->robots as $key => $val)
  267. {
  268. if (preg_match("|".preg_quote($key)."|i", $this->agent))
  269. {
  270. $this->is_robot = TRUE;
  271. $this->robot = $val;
  272. return TRUE;
  273. }
  274. }
  275. }
  276. return FALSE;
  277. }
  278. // --------------------------------------------------------------------
  279. /**
  280. * Set the Mobile Device
  281. *
  282. * @access private
  283. * @return bool
  284. */
  285. function _set_mobile()
  286. {
  287. if (is_array($this->mobiles) AND count($this->mobiles) > 0)
  288. {
  289. foreach ($this->mobiles as $key => $val)
  290. {
  291. if ((strpos(strtolower($this->agent), $key)) !== FALSE)
  292. {
  293. $this->is_mobile = TRUE;
  294. $this->mobile = $val;
  295. return TRUE;
  296. }
  297. }
  298. }
  299. return FALSE;
  300. }
  301. // --------------------------------------------------------------------
  302. /**
  303. * Set the accepted languages
  304. *
  305. * @access private
  306. * @return void
  307. */
  308. function _set_languages()
  309. {
  310. if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
  311. {
  312. $languages = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_LANGUAGE']));
  313. $this->languages = explode(',', $languages);
  314. }
  315. if (count($this->languages) == 0)
  316. {
  317. $this->languages = array('Undefined');
  318. }
  319. }
  320. // --------------------------------------------------------------------
  321. /**
  322. * Set the accepted character sets
  323. *
  324. * @access private
  325. * @return void
  326. */
  327. function _set_charsets()
  328. {
  329. if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '')
  330. {
  331. $charsets = preg_replace('/(;q=.+)/i', '', trim($_SERVER['HTTP_ACCEPT_CHARSET']));
  332. $this->charsets = explode(',', $charsets);
  333. }
  334. if (count($this->charsets) == 0)
  335. {
  336. $this->charsets = array('Undefined');
  337. }
  338. }
  339. // --------------------------------------------------------------------
  340. /**
  341. * Is Browser
  342. *
  343. * @access public
  344. * @return bool
  345. */
  346. function is_browser()
  347. {
  348. return $this->is_browser;
  349. }
  350. // --------------------------------------------------------------------
  351. /**
  352. * Is Robot
  353. *
  354. * @access public
  355. * @return bool
  356. */
  357. function is_robot()
  358. {
  359. return $this->is_robot;
  360. }
  361. // --------------------------------------------------------------------
  362. /**
  363. * Is Mobile
  364. *
  365. * @access public
  366. * @return bool
  367. */
  368. function is_mobile()
  369. {
  370. return $this->is_mobile;
  371. }
  372. // --------------------------------------------------------------------
  373. /**
  374. * Is this a referral from another site?
  375. *
  376. * @access public
  377. * @return bool
  378. */
  379. function is_referral()
  380. {
  381. return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? FALSE : TRUE;
  382. }
  383. // --------------------------------------------------------------------
  384. /**
  385. * Agent String
  386. *
  387. * @access public
  388. * @return string
  389. */
  390. function agent_string()
  391. {
  392. return $this->agent;
  393. }
  394. // --------------------------------------------------------------------
  395. /**
  396. * Get Platform
  397. *
  398. * @access public
  399. * @return string
  400. */
  401. function platform()
  402. {
  403. return $this->platform;
  404. }
  405. // --------------------------------------------------------------------
  406. /**
  407. * Get Browser Name
  408. *
  409. * @access public
  410. * @return string
  411. */
  412. function browser()
  413. {
  414. return $this->browser;
  415. }
  416. // --------------------------------------------------------------------
  417. /**
  418. * Get the Browser Version
  419. *
  420. * @access public
  421. * @return string
  422. */
  423. function version()
  424. {
  425. return $this->version;
  426. }
  427. // --------------------------------------------------------------------
  428. /**
  429. * Get The Robot Name
  430. *
  431. * @access public
  432. * @return string
  433. */
  434. function robot()
  435. {
  436. return $this->robot;
  437. }
  438. // --------------------------------------------------------------------
  439. /**
  440. * Get the Mobile Device
  441. *
  442. * @access public
  443. * @return string
  444. */
  445. function mobile()
  446. {
  447. return $this->mobile;
  448. }
  449. // --------------------------------------------------------------------
  450. /**
  451. * Get the referrer
  452. *
  453. * @access public
  454. * @return bool
  455. */
  456. function referrer()
  457. {
  458. return ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
  459. }
  460. // --------------------------------------------------------------------
  461. /**
  462. * Get the accepted languages
  463. *
  464. * @access public
  465. * @return array
  466. */
  467. function languages()
  468. {
  469. if (count($this->languages) == 0)
  470. {
  471. $this->_set_languages();
  472. }
  473. return $this->languages;
  474. }
  475. // --------------------------------------------------------------------
  476. /**
  477. * Get the accepted Character Sets
  478. *
  479. * @access public
  480. * @return array
  481. */
  482. function charsets()
  483. {
  484. if (count($this->charsets) == 0)
  485. {
  486. $this->_set_charsets();
  487. }
  488. return $this->charsets;
  489. }
  490. // --------------------------------------------------------------------
  491. /**
  492. * Test for a particular language
  493. *
  494. * @access public
  495. * @return bool
  496. */
  497. function accept_lang($lang = 'en')
  498. {
  499. return (in_array(strtolower($lang), $this->languages(), TRUE)) ? TRUE : FALSE;
  500. }
  501. // --------------------------------------------------------------------
  502. /**
  503. * Test for a particular character set
  504. *
  505. * @access public
  506. * @return bool
  507. */
  508. function accept_charset($charset = 'utf-8')
  509. {
  510. return (in_array(strtolower($charset), $this->charsets(), TRUE)) ? TRUE : FALSE;
  511. }
  512. }
  513. ?>