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

/phppi/includes/classes/browser.php

http://phppi.googlecode.com/
PHP | 1051 lines | 631 code | 50 blank | 370 comment | 100 complexity | ce958421bb9addcf197beb02dced3513 MD5 | raw file
  1. <?php
  2. /**
  3. * File: Browser.php
  4. * Author: Chris Schuld (http://chrisschuld.com/)
  5. * Last Modified: August 20th, 2010
  6. * @version 1.9
  7. * @package PegasusPHP
  8. *
  9. * Copyright (C) 2008-2010 Chris Schuld (chris@chrisschuld.com)
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details at:
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. *
  23. * Typical Usage:
  24. *
  25. * $browser = new Browser();
  26. * if( $browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >= 2 ) {
  27. * echo 'You have FireFox version 2 or greater';
  28. * }
  29. *
  30. * User Agents Sampled from: http://www.useragentstring.com/
  31. *
  32. * This implementation is based on the original work from Gary White
  33. * http://apptools.com/phptools/browser/
  34. *
  35. * UPDATES:
  36. *
  37. * 2010-08-20 (v1.9):
  38. * + Added MSN Explorer Browser (legacy)
  39. * + Added Bing/MSN Robot (Thanks Rob MacDonald)
  40. * + Added the Android Platform (PLATFORM_ANDROID)
  41. * + Fixed issue with Android 1.6/2.2 (Thanks Tom Hirashima)
  42. *
  43. * 2010-04-27 (v1.8):
  44. * + Added iPad Support
  45. *
  46. * 2010-03-07 (v1.7):
  47. * + *MAJOR* Rebuild (preg_match and other "slow" routine removal(s))
  48. * + Almost allof Gary's original code has been replaced
  49. * + Large PHPUNIT testing environment created to validate new releases and additions
  50. * + Added FreeBSD Platform
  51. * + Added OpenBSD Platform
  52. * + Added NetBSD Platform
  53. * + Added SunOS Platform
  54. * + Added OpenSolaris Platform
  55. * + Added support of the Iceweazel Browser
  56. * + Added isChromeFrame() call to check if chromeframe is in use
  57. * + Moved the Opera check in front of the Firefox check due to legacy Opera User Agents
  58. * + Added the __toString() method (Thanks Deano)
  59. *
  60. * 2009-11-15:
  61. * + Updated the checkes for Firefox
  62. * + Added the NOKIA platform
  63. * + Added Checks for the NOKIA brower(s)
  64. *
  65. * 2009-11-08:
  66. * + PHP 5.3 Support
  67. * + Added support for BlackBerry OS and BlackBerry browser
  68. * + Added support for the Opera Mini browser
  69. * + Added additional documenation
  70. * + Added support for isRobot() and isMobile()
  71. * + Added support for Opera version 10
  72. * + Added support for deprecated Netscape Navigator version 9
  73. * + Added support for IceCat
  74. * + Added support for Shiretoko
  75. *
  76. * 2010-04-27 (v1.8):
  77. * + Added iPad Support
  78. *
  79. * 2009-08-18:
  80. * + Updated to support PHP 5.3 - removed all deprecated function calls
  81. * + Updated to remove all double quotes (") -- converted to single quotes (')
  82. *
  83. * 2009-04-27:
  84. * + Updated the IE check to remove a typo and bug (thanks John)
  85. *
  86. * 2009-04-22:
  87. * + Added detection for GoogleBot
  88. * + Added detection for the W3C Validator.
  89. * + Added detection for Yahoo! Slurp
  90. *
  91. * 2009-03-14:
  92. * + Added detection for iPods.
  93. * + Added Platform detection for iPhones
  94. * + Added Platform detection for iPods
  95. *
  96. * 2009-02-16: (Rick Hale)
  97. * + Added version detection for Android phones.
  98. *
  99. * 2008-12-09:
  100. * + Removed unused constant
  101. *
  102. * 2008-11-07:
  103. * + Added Google's Chrome to the detection list
  104. * + Added isBrowser(string) to the list of functions special thanks to
  105. * Daniel 'mavrick' Lang for the function concept (http://mavrick.id.au)
  106. *
  107. *
  108. * Gary White noted: "Since browser detection is so unreliable, I am
  109. * no longer maintaining this script. You are free to use and or
  110. * modify/update it as you want, however the author assumes no
  111. * responsibility for the accuracy of the detected values."
  112. *
  113. * Anyone experienced with Gary's script might be interested in these notes:
  114. *
  115. * Added class constants
  116. * Added detection and version detection for Google's Chrome
  117. * Updated the version detection for Amaya
  118. * Updated the version detection for Firefox
  119. * Updated the version detection for Lynx
  120. * Updated the version detection for WebTV
  121. * Updated the version detection for NetPositive
  122. * Updated the version detection for IE
  123. * Updated the version detection for OmniWeb
  124. * Updated the version detection for iCab
  125. * Updated the version detection for Safari
  126. * Updated Safari to remove mobile devices (iPhone)
  127. * Added detection for iPhone
  128. * Added detection for robots
  129. * Added detection for mobile devices
  130. * Added detection for BlackBerry
  131. * Removed Netscape checks (matches heavily with firefox & mozilla)
  132. *
  133. */
  134. class Browser {
  135. private $_agent = '';
  136. private $_browser_name = '';
  137. private $_version = '';
  138. private $_platform = '';
  139. private $_os = '';
  140. private $_is_aol = false;
  141. private $_is_mobile = false;
  142. private $_is_robot = false;
  143. private $_aol_version = '';
  144. const BROWSER_UNKNOWN = 'unknown';
  145. const VERSION_UNKNOWN = 'unknown';
  146. const BROWSER_OPERA = 'Opera'; // http://www.opera.com/
  147. const BROWSER_OPERA_MINI = 'Opera Mini'; // http://www.opera.com/mini/
  148. const BROWSER_WEBTV = 'WebTV'; // http://www.webtv.net/pc/
  149. const BROWSER_IE = 'Internet Explorer'; // http://www.microsoft.com/ie/
  150. const BROWSER_POCKET_IE = 'Pocket Internet Explorer'; // http://en.wikipedia.org/wiki/Internet_Explorer_Mobile
  151. const BROWSER_KONQUEROR = 'Konqueror'; // http://www.konqueror.org/
  152. const BROWSER_ICAB = 'iCab'; // http://www.icab.de/
  153. const BROWSER_OMNIWEB = 'OmniWeb'; // http://www.omnigroup.com/applications/omniweb/
  154. const BROWSER_FIREBIRD = 'Firebird'; // http://www.ibphoenix.com/
  155. const BROWSER_FIREFOX = 'Firefox'; // http://www.mozilla.com/en-US/firefox/firefox.html
  156. const BROWSER_ICEWEASEL = 'Iceweasel'; // http://www.geticeweasel.org/
  157. const BROWSER_SHIRETOKO = 'Shiretoko'; // http://wiki.mozilla.org/Projects/shiretoko
  158. const BROWSER_MOZILLA = 'Mozilla'; // http://www.mozilla.com/en-US/
  159. const BROWSER_AMAYA = 'Amaya'; // http://www.w3.org/Amaya/
  160. const BROWSER_LYNX = 'Lynx'; // http://en.wikipedia.org/wiki/Lynx
  161. const BROWSER_SAFARI = 'Safari'; // http://apple.com
  162. const BROWSER_IPHONE = 'iPhone'; // http://apple.com
  163. const BROWSER_IPOD = 'iPod'; // http://apple.com
  164. const BROWSER_IPAD = 'iPad'; // http://apple.com
  165. const BROWSER_CHROME = 'Chrome'; // http://www.google.com/chrome
  166. const BROWSER_ANDROID = 'Android'; // http://www.android.com/
  167. const BROWSER_GOOGLEBOT = 'GoogleBot'; // http://en.wikipedia.org/wiki/Googlebot
  168. const BROWSER_SLURP = 'Yahoo! Slurp'; // http://en.wikipedia.org/wiki/Yahoo!_Slurp
  169. const BROWSER_W3CVALIDATOR = 'W3C Validator'; // http://validator.w3.org/
  170. const BROWSER_BLACKBERRY = 'BlackBerry'; // http://www.blackberry.com/
  171. const BROWSER_ICECAT = 'IceCat'; // http://en.wikipedia.org/wiki/GNU_IceCat
  172. const BROWSER_NOKIA_S60 = 'Nokia S60 OSS Browser'; // http://en.wikipedia.org/wiki/Web_Browser_for_S60
  173. const BROWSER_NOKIA = 'Nokia Browser'; // * all other WAP-based browsers on the Nokia Platform
  174. const BROWSER_MSN = 'MSN Browser'; // http://explorer.msn.com/
  175. const BROWSER_MSNBOT = 'MSN Bot'; // http://search.msn.com/msnbot.htm
  176. // http://en.wikipedia.org/wiki/Msnbot (used for Bing as well)
  177. const BROWSER_NETSCAPE_NAVIGATOR = 'Netscape Navigator'; // http://browser.netscape.com/ (DEPRECATED)
  178. const BROWSER_GALEON = 'Galeon'; // http://galeon.sourceforge.net/ (DEPRECATED)
  179. const BROWSER_NETPOSITIVE = 'NetPositive'; // http://en.wikipedia.org/wiki/NetPositive (DEPRECATED)
  180. const BROWSER_PHOENIX = 'Phoenix'; // http://en.wikipedia.org/wiki/History_of_Mozilla_Firefox (DEPRECATED)
  181. const PLATFORM_UNKNOWN = 'unknown';
  182. const PLATFORM_WINDOWS = 'Windows';
  183. const PLATFORM_WINDOWS_CE = 'Windows CE';
  184. const PLATFORM_APPLE = 'Apple';
  185. const PLATFORM_LINUX = 'Linux';
  186. const PLATFORM_OS2 = 'OS/2';
  187. const PLATFORM_BEOS = 'BeOS';
  188. const PLATFORM_IPHONE = 'iPhone';
  189. const PLATFORM_IPOD = 'iPod';
  190. const PLATFORM_IPAD = 'iPad';
  191. const PLATFORM_BLACKBERRY = 'BlackBerry';
  192. const PLATFORM_NOKIA = 'Nokia';
  193. const PLATFORM_FREEBSD = 'FreeBSD';
  194. const PLATFORM_OPENBSD = 'OpenBSD';
  195. const PLATFORM_NETBSD = 'NetBSD';
  196. const PLATFORM_SUNOS = 'SunOS';
  197. const PLATFORM_OPENSOLARIS = 'OpenSolaris';
  198. const PLATFORM_ANDROID = 'Android';
  199. const OPERATING_SYSTEM_UNKNOWN = 'unknown';
  200. public function Browser($useragent="") {
  201. $this->reset();
  202. if( $useragent != "" ) {
  203. $this->setUserAgent($useragent);
  204. }
  205. else {
  206. $this->determine();
  207. }
  208. }
  209. /**
  210. * Reset all properties
  211. */
  212. public function reset() {
  213. $this->_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
  214. $this->_browser_name = self::BROWSER_UNKNOWN;
  215. $this->_version = self::VERSION_UNKNOWN;
  216. $this->_platform = self::PLATFORM_UNKNOWN;
  217. $this->_os = self::OPERATING_SYSTEM_UNKNOWN;
  218. $this->_is_aol = false;
  219. $this->_is_mobile = false;
  220. $this->_is_robot = false;
  221. $this->_aol_version = self::VERSION_UNKNOWN;
  222. }
  223. /**
  224. * Check to see if the specific browser is valid
  225. * @param string $browserName
  226. * @return True if the browser is the specified browser
  227. */
  228. function isBrowser($browserName) { return( 0 == strcasecmp($this->_browser_name, trim($browserName))); }
  229. /**
  230. * The name of the browser. All return types are from the class contants
  231. * @return string Name of the browser
  232. */
  233. public function getBrowser() { return $this->_browser_name; }
  234. /**
  235. * Set the name of the browser
  236. * @param $browser The name of the Browser
  237. */
  238. public function setBrowser($browser) { return $this->_browser_name = $browser; }
  239. /**
  240. * The name of the platform. All return types are from the class contants
  241. * @return string Name of the browser
  242. */
  243. public function getPlatform() { return $this->_platform; }
  244. /**
  245. * Set the name of the platform
  246. * @param $platform The name of the Platform
  247. */
  248. public function setPlatform($platform) { return $this->_platform = $platform; }
  249. /**
  250. * The version of the browser.
  251. * @return string Version of the browser (will only contain alpha-numeric characters and a period)
  252. */
  253. public function getVersion() { return $this->_version; }
  254. /**
  255. * Set the version of the browser
  256. * @param $version The version of the Browser
  257. */
  258. public function setVersion($version) { $this->_version = preg_replace('/[^0-9,.,a-z,A-Z-]/','',$version); }
  259. /**
  260. * The version of AOL.
  261. * @return string Version of AOL (will only contain alpha-numeric characters and a period)
  262. */
  263. public function getAolVersion() { return $this->_aol_version; }
  264. /**
  265. * Set the version of AOL
  266. * @param $version The version of AOL
  267. */
  268. public function setAolVersion($version) { $this->_aol_version = preg_replace('/[^0-9,.,a-z,A-Z]/','',$version); }
  269. /**
  270. * Is the browser from AOL?
  271. * @return boolean True if the browser is from AOL otherwise false
  272. */
  273. public function isAol() { return $this->_is_aol; }
  274. /**
  275. * Is the browser from a mobile device?
  276. * @return boolean True if the browser is from a mobile device otherwise false
  277. */
  278. public function isMobile() { return $this->_is_mobile; }
  279. /**
  280. * Is the browser from a robot (ex Slurp,GoogleBot)?
  281. * @return boolean True if the browser is from a robot otherwise false
  282. */
  283. public function isRobot() { return $this->_is_robot; }
  284. /**
  285. * Set the browser to be from AOL
  286. * @param $isAol
  287. */
  288. public function setAol($isAol) { $this->_is_aol = $isAol; }
  289. /**
  290. * Set the Browser to be mobile
  291. * @param boolean $value is the browser a mobile brower or not
  292. */
  293. protected function setMobile($value=true) { $this->_is_mobile = $value; }
  294. /**
  295. * Set the Browser to be a robot
  296. * @param boolean $value is the browser a robot or not
  297. */
  298. protected function setRobot($value=true) { $this->_is_robot = $value; }
  299. /**
  300. * Get the user agent value in use to determine the browser
  301. * @return string The user agent from the HTTP header
  302. */
  303. public function getUserAgent() { return $this->_agent; }
  304. /**
  305. * Set the user agent value (the construction will use the HTTP header value - this will overwrite it)
  306. * @param $agent_string The value for the User Agent
  307. */
  308. public function setUserAgent($agent_string) {
  309. $this->reset();
  310. $this->_agent = $agent_string;
  311. $this->determine();
  312. }
  313. /**
  314. * Used to determine if the browser is actually "chromeframe"
  315. * @since 1.7
  316. * @return boolean True if the browser is using chromeframe
  317. */
  318. public function isChromeFrame() {
  319. return( strpos($this->_agent,"chromeframe") !== false );
  320. }
  321. /**
  322. * Returns a formatted string with a summary of the details of the browser.
  323. * @return string formatted string with a summary of the browser
  324. */
  325. public function __toString() {
  326. return "<strong>Browser Name:</strong>{$this->getBrowser()}<br/>\n" .
  327. "<strong>Browser Version:</strong>{$this->getVersion()}<br/>\n" .
  328. "<strong>Browser User Agent String:</strong>{$this->getUserAgent()}<br/>\n" .
  329. "<strong>Platform:</strong>{$this->getPlatform()}<br/>";
  330. }
  331. /**
  332. * Protected routine to calculate and determine what the browser is in use (including platform)
  333. */
  334. protected function determine() {
  335. $this->checkPlatform();
  336. $this->checkBrowsers();
  337. $this->checkForAol();
  338. }
  339. /**
  340. * Protected routine to determine the browser type
  341. * @return boolean True if the browser was detected otherwise false
  342. */
  343. protected function checkBrowsers() {
  344. return (
  345. // well-known, well-used
  346. // Special Notes:
  347. // (1) Opera must be checked before FireFox due to the odd
  348. // user agents used in some older versions of Opera
  349. // (2) WebTV is strapped onto Internet Explorer so we must
  350. // check for WebTV before IE
  351. // (3) (deprecated) Galeon is based on Firefox and needs to be
  352. // tested before Firefox is tested
  353. // (4) OmniWeb is based on Safari so OmniWeb check must occur
  354. // before Safari
  355. // (5) Netscape 9+ is based on Firefox so Netscape checks
  356. // before FireFox are necessary
  357. $this->checkBrowserWebTv() ||
  358. $this->checkBrowserInternetExplorer() ||
  359. $this->checkBrowserOpera() ||
  360. $this->checkBrowserGaleon() ||
  361. $this->checkBrowserNetscapeNavigator9Plus() ||
  362. $this->checkBrowserFirefox() ||
  363. $this->checkBrowserChrome() ||
  364. $this->checkBrowserOmniWeb() ||
  365. // common mobile
  366. $this->checkBrowserAndroid() ||
  367. $this->checkBrowseriPad() ||
  368. $this->checkBrowseriPod() ||
  369. $this->checkBrowseriPhone() ||
  370. $this->checkBrowserBlackBerry() ||
  371. $this->checkBrowserNokia() ||
  372. // common bots
  373. $this->checkBrowserGoogleBot() ||
  374. $this->checkBrowserMSNBot() ||
  375. $this->checkBrowserSlurp() ||
  376. // WebKit base check (post mobile and others)
  377. $this->checkBrowserSafari() ||
  378. // everyone else
  379. $this->checkBrowserNetPositive() ||
  380. $this->checkBrowserFirebird() ||
  381. $this->checkBrowserKonqueror() ||
  382. $this->checkBrowserIcab() ||
  383. $this->checkBrowserPhoenix() ||
  384. $this->checkBrowserAmaya() ||
  385. $this->checkBrowserLynx() ||
  386. $this->checkBrowserShiretoko() ||
  387. $this->checkBrowserIceCat() ||
  388. $this->checkBrowserW3CValidator() ||
  389. $this->checkBrowserMozilla() /* Mozilla is such an open standard that you must check it last */
  390. );
  391. }
  392. /**
  393. * Determine if the user is using a BlackBerry (last updated 1.7)
  394. * @return boolean True if the browser is the BlackBerry browser otherwise false
  395. */
  396. protected function checkBrowserBlackBerry() {
  397. if( stripos($this->_agent,'blackberry') !== false ) {
  398. $aresult = explode("/",stristr($this->_agent,"BlackBerry"));
  399. $aversion = explode(' ',$aresult[1]);
  400. $this->setVersion($aversion[0]);
  401. $this->_browser_name = self::BROWSER_BLACKBERRY;
  402. $this->setMobile(true);
  403. return true;
  404. }
  405. return false;
  406. }
  407. /**
  408. * Determine if the user is using an AOL User Agent (last updated 1.7)
  409. * @return boolean True if the browser is from AOL otherwise false
  410. */
  411. protected function checkForAol() {
  412. $this->setAol(false);
  413. $this->setAolVersion(self::VERSION_UNKNOWN);
  414. if( stripos($this->_agent,'aol') !== false ) {
  415. $aversion = explode(' ',stristr($this->_agent, 'AOL'));
  416. $this->setAol(true);
  417. $this->setAolVersion(preg_replace('/[^0-9\.a-z]/i', '', $aversion[1]));
  418. return true;
  419. }
  420. return false;
  421. }
  422. /**
  423. * Determine if the browser is the GoogleBot or not (last updated 1.7)
  424. * @return boolean True if the browser is the GoogletBot otherwise false
  425. */
  426. protected function checkBrowserGoogleBot() {
  427. if( stripos($this->_agent,'googlebot') !== false ) {
  428. $aresult = explode('/',stristr($this->_agent,'googlebot'));
  429. $aversion = explode(' ',$aresult[1]);
  430. $this->setVersion(str_replace(';','',$aversion[0]));
  431. $this->_browser_name = self::BROWSER_GOOGLEBOT;
  432. $this->setRobot(true);
  433. return true;
  434. }
  435. return false;
  436. }
  437. /**
  438. * Determine if the browser is the MSNBot or not (last updated 1.9)
  439. * @return boolean True if the browser is the MSNBot otherwise false
  440. */
  441. protected function checkBrowserMSNBot() {
  442. if( stripos($this->_agent,"msnbot") !== false ) {
  443. $aresult = explode("/",stristr($this->_agent,"msnbot"));
  444. $aversion = explode(" ",$aresult[1]);
  445. $this->setVersion(str_replace(";","",$aversion[0]));
  446. $this->_browser_name = self::BROWSER_MSNBOT;
  447. $this->setRobot(true);
  448. return true;
  449. }
  450. return false;
  451. }
  452. /**
  453. * Determine if the browser is the W3C Validator or not (last updated 1.7)
  454. * @return boolean True if the browser is the W3C Validator otherwise false
  455. */
  456. protected function checkBrowserW3CValidator() {
  457. if( stripos($this->_agent,'W3C-checklink') !== false ) {
  458. $aresult = explode('/',stristr($this->_agent,'W3C-checklink'));
  459. $aversion = explode(' ',$aresult[1]);
  460. $this->setVersion($aversion[0]);
  461. $this->_browser_name = self::BROWSER_W3CVALIDATOR;
  462. return true;
  463. }
  464. else if( stripos($this->_agent,'W3C_Validator') !== false ) {
  465. // Some of the Validator versions do not delineate w/ a slash - add it back in
  466. $ua = str_replace("W3C_Validator ", "W3C_Validator/", $this->_agent);
  467. $aresult = explode('/',stristr($ua,'W3C_Validator'));
  468. $aversion = explode(' ',$aresult[1]);
  469. $this->setVersion($aversion[0]);
  470. $this->_browser_name = self::BROWSER_W3CVALIDATOR;
  471. return true;
  472. }
  473. return false;
  474. }
  475. /**
  476. * Determine if the browser is the Yahoo! Slurp Robot or not (last updated 1.7)
  477. * @return boolean True if the browser is the Yahoo! Slurp Robot otherwise false
  478. */
  479. protected function checkBrowserSlurp() {
  480. if( stripos($this->_agent,'slurp') !== false ) {
  481. $aresult = explode('/',stristr($this->_agent,'Slurp'));
  482. $aversion = explode(' ',$aresult[1]);
  483. $this->setVersion($aversion[0]);
  484. $this->_browser_name = self::BROWSER_SLURP;
  485. $this->setRobot(true);
  486. $this->setMobile(false);
  487. return true;
  488. }
  489. return false;
  490. }
  491. /**
  492. * Determine if the browser is Internet Explorer or not (last updated 1.7)
  493. * @return boolean True if the browser is Internet Explorer otherwise false
  494. */
  495. protected function checkBrowserInternetExplorer() {
  496. // Test for v1 - v1.5 IE
  497. if( stripos($this->_agent,'microsoft internet explorer') !== false ) {
  498. $this->setBrowser(self::BROWSER_IE);
  499. $this->setVersion('1.0');
  500. $aresult = stristr($this->_agent, '/');
  501. if( preg_match('/308|425|426|474|0b1/i', $aresult) ) {
  502. $this->setVersion('1.5');
  503. }
  504. return true;
  505. }
  506. // Test for versions > 1.5
  507. else if( stripos($this->_agent,'msie') !== false && stripos($this->_agent,'opera') === false ) {
  508. // See if the browser is the odd MSN Explorer
  509. if( stripos($this->_agent,'msnb') !== false ) {
  510. $aresult = explode(' ',stristr(str_replace(';','; ',$this->_agent),'MSN'));
  511. $this->setBrowser( self::BROWSER_MSN );
  512. $this->setVersion(str_replace(array('(',')',';'),'',$aresult[1]));
  513. return true;
  514. }
  515. $aresult = explode(' ',stristr(str_replace(';','; ',$this->_agent),'msie'));
  516. $this->setBrowser( self::BROWSER_IE );
  517. $this->setVersion(str_replace(array('(',')',';'),'',$aresult[1]));
  518. return true;
  519. }
  520. // Test for Pocket IE
  521. else if( stripos($this->_agent,'mspie') !== false || stripos($this->_agent,'pocket') !== false ) {
  522. $aresult = explode(' ',stristr($this->_agent,'mspie'));
  523. $this->setPlatform( self::PLATFORM_WINDOWS_CE );
  524. $this->setBrowser( self::BROWSER_POCKET_IE );
  525. $this->setMobile(true);
  526. if( stripos($this->_agent,'mspie') !== false ) {
  527. $this->setVersion($aresult[1]);
  528. }
  529. else {
  530. $aversion = explode('/',$this->_agent);
  531. $this->setVersion($aversion[1]);
  532. }
  533. return true;
  534. }
  535. return false;
  536. }
  537. /**
  538. * Determine if the browser is Opera or not (last updated 1.7)
  539. * @return boolean True if the browser is Opera otherwise false
  540. */
  541. protected function checkBrowserOpera() {
  542. if( stripos($this->_agent,'opera mini') !== false ) {
  543. $resultant = stristr($this->_agent, 'opera mini');
  544. if( preg_match('/\//',$resultant) ) {
  545. $aresult = explode('/',$resultant);
  546. $aversion = explode(' ',$aresult[1]);
  547. $this->setVersion($aversion[0]);
  548. }
  549. else {
  550. $aversion = explode(' ',stristr($resultant,'opera mini'));
  551. $this->setVersion($aversion[1]);
  552. }
  553. $this->_browser_name = self::BROWSER_OPERA_MINI;
  554. $this->setMobile(true);
  555. return true;
  556. }
  557. else if( stripos($this->_agent,'opera') !== false ) {
  558. $resultant = stristr($this->_agent, 'opera');
  559. if( preg_match('/Version\/(10.*)$/',$resultant,$matches) ) {
  560. $this->setVersion($matches[1]);
  561. }
  562. else if( preg_match('/\//',$resultant) ) {
  563. $aresult = explode('/',str_replace("("," ",$resultant));
  564. $aversion = explode(' ',$aresult[1]);
  565. $this->setVersion($aversion[0]);
  566. }
  567. else {
  568. $aversion = explode(' ',stristr($resultant,'opera'));
  569. $this->setVersion(isset($aversion[1])?$aversion[1]:"");
  570. }
  571. $this->_browser_name = self::BROWSER_OPERA;
  572. return true;
  573. }
  574. return false;
  575. }
  576. /**
  577. * Determine if the browser is Chrome or not (last updated 1.7)
  578. * @return boolean True if the browser is Chrome otherwise false
  579. */
  580. protected function checkBrowserChrome() {
  581. if( stripos($this->_agent,'Chrome') !== false ) {
  582. $aresult = explode('/',stristr($this->_agent,'Chrome'));
  583. $aversion = explode(' ',$aresult[1]);
  584. $this->setVersion($aversion[0]);
  585. $this->setBrowser(self::BROWSER_CHROME);
  586. return true;
  587. }
  588. return false;
  589. }
  590. /**
  591. * Determine if the browser is WebTv or not (last updated 1.7)
  592. * @return boolean True if the browser is WebTv otherwise false
  593. */
  594. protected function checkBrowserWebTv() {
  595. if( stripos($this->_agent,'webtv') !== false ) {
  596. $aresult = explode('/',stristr($this->_agent,'webtv'));
  597. $aversion = explode(' ',$aresult[1]);
  598. $this->setVersion($aversion[0]);
  599. $this->setBrowser(self::BROWSER_WEBTV);
  600. return true;
  601. }
  602. return false;
  603. }
  604. /**
  605. * Determine if the browser is NetPositive or not (last updated 1.7)
  606. * @return boolean True if the browser is NetPositive otherwise false
  607. */
  608. protected function checkBrowserNetPositive() {
  609. if( stripos($this->_agent,'NetPositive') !== false ) {
  610. $aresult = explode('/',stristr($this->_agent,'NetPositive'));
  611. $aversion = explode(' ',$aresult[1]);
  612. $this->setVersion(str_replace(array('(',')',';'),'',$aversion[0]));
  613. $this->setBrowser(self::BROWSER_NETPOSITIVE);
  614. return true;
  615. }
  616. return false;
  617. }
  618. /**
  619. * Determine if the browser is Galeon or not (last updated 1.7)
  620. * @return boolean True if the browser is Galeon otherwise false
  621. */
  622. protected function checkBrowserGaleon() {
  623. if( stripos($this->_agent,'galeon') !== false ) {
  624. $aresult = explode(' ',stristr($this->_agent,'galeon'));
  625. $aversion = explode('/',$aresult[0]);
  626. $this->setVersion($aversion[1]);
  627. $this->setBrowser(self::BROWSER_GALEON);
  628. return true;
  629. }
  630. return false;
  631. }
  632. /**
  633. * Determine if the browser is Konqueror or not (last updated 1.7)
  634. * @return boolean True if the browser is Konqueror otherwise false
  635. */
  636. protected function checkBrowserKonqueror() {
  637. if( stripos($this->_agent,'Konqueror') !== false ) {
  638. $aresult = explode(' ',stristr($this->_agent,'Konqueror'));
  639. $aversion = explode('/',$aresult[0]);
  640. $this->setVersion($aversion[1]);
  641. $this->setBrowser(self::BROWSER_KONQUEROR);
  642. return true;
  643. }
  644. return false;
  645. }
  646. /**
  647. * Determine if the browser is iCab or not (last updated 1.7)
  648. * @return boolean True if the browser is iCab otherwise false
  649. */
  650. protected function checkBrowserIcab() {
  651. if( stripos($this->_agent,'icab') !== false ) {
  652. $aversion = explode(' ',stristr(str_replace('/',' ',$this->_agent),'icab'));
  653. $this->setVersion($aversion[1]);
  654. $this->setBrowser(self::BROWSER_ICAB);
  655. return true;
  656. }
  657. return false;
  658. }
  659. /**
  660. * Determine if the browser is OmniWeb or not (last updated 1.7)
  661. * @return boolean True if the browser is OmniWeb otherwise false
  662. */
  663. protected function checkBrowserOmniWeb() {
  664. if( stripos($this->_agent,'omniweb') !== false ) {
  665. $aresult = explode('/',stristr($this->_agent,'omniweb'));
  666. $aversion = explode(' ',isset($aresult[1])?$aresult[1]:"");
  667. $this->setVersion($aversion[0]);
  668. $this->setBrowser(self::BROWSER_OMNIWEB);
  669. return true;
  670. }
  671. return false;
  672. }
  673. /**
  674. * Determine if the browser is Phoenix or not (last updated 1.7)
  675. * @return boolean True if the browser is Phoenix otherwise false
  676. */
  677. protected function checkBrowserPhoenix() {
  678. if( stripos($this->_agent,'Phoenix') !== false ) {
  679. $aversion = explode('/',stristr($this->_agent,'Phoenix'));
  680. $this->setVersion($aversion[1]);
  681. $this->setBrowser(self::BROWSER_PHOENIX);
  682. return true;
  683. }
  684. return false;
  685. }
  686. /**
  687. * Determine if the browser is Firebird or not (last updated 1.7)
  688. * @return boolean True if the browser is Firebird otherwise false
  689. */
  690. protected function checkBrowserFirebird() {
  691. if( stripos($this->_agent,'Firebird') !== false ) {
  692. $aversion = explode('/',stristr($this->_agent,'Firebird'));
  693. $this->setVersion($aversion[1]);
  694. $this->setBrowser(self::BROWSER_FIREBIRD);
  695. return true;
  696. }
  697. return false;
  698. }
  699. /**
  700. * Determine if the browser is Netscape Navigator 9+ or not (last updated 1.7)
  701. * NOTE: (http://browser.netscape.com/ - Official support ended on March 1st, 2008)
  702. * @return boolean True if the browser is Netscape Navigator 9+ otherwise false
  703. */
  704. protected function checkBrowserNetscapeNavigator9Plus() {
  705. if( stripos($this->_agent,'Firefox') !== false && preg_match('/Navigator\/([^ ]*)/i',$this->_agent,$matches) ) {
  706. $this->setVersion($matches[1]);
  707. $this->setBrowser(self::BROWSER_NETSCAPE_NAVIGATOR);
  708. return true;
  709. }
  710. else if( stripos($this->_agent,'Firefox') === false && preg_match('/Netscape6?\/([^ ]*)/i',$this->_agent,$matches) ) {
  711. $this->setVersion($matches[1]);
  712. $this->setBrowser(self::BROWSER_NETSCAPE_NAVIGATOR);
  713. return true;
  714. }
  715. return false;
  716. }
  717. /**
  718. * Determine if the browser is Shiretoko or not (https://wiki.mozilla.org/Projects/shiretoko) (last updated 1.7)
  719. * @return boolean True if the browser is Shiretoko otherwise false
  720. */
  721. protected function checkBrowserShiretoko() {
  722. if( stripos($this->_agent,'Mozilla') !== false && preg_match('/Shiretoko\/([^ ]*)/i',$this->_agent,$matches) ) {
  723. $this->setVersion($matches[1]);
  724. $this->setBrowser(self::BROWSER_SHIRETOKO);
  725. return true;
  726. }
  727. return false;
  728. }
  729. /**
  730. * Determine if the browser is Ice Cat or not (http://en.wikipedia.org/wiki/GNU_IceCat) (last updated 1.7)
  731. * @return boolean True if the browser is Ice Cat otherwise false
  732. */
  733. protected function checkBrowserIceCat() {
  734. if( stripos($this->_agent,'Mozilla') !== false && preg_match('/IceCat\/([^ ]*)/i',$this->_agent,$matches) ) {
  735. $this->setVersion($matches[1]);
  736. $this->setBrowser(self::BROWSER_ICECAT);
  737. return true;
  738. }
  739. return false;
  740. }
  741. /**
  742. * Determine if the browser is Nokia or not (last updated 1.7)
  743. * @return boolean True if the browser is Nokia otherwise false
  744. */
  745. protected function checkBrowserNokia() {
  746. if( preg_match("/Nokia([^\/]+)\/([^ SP]+)/i",$this->_agent,$matches) ) {
  747. $this->setVersion($matches[2]);
  748. if( stripos($this->_agent,'Series60') !== false || strpos($this->_agent,'S60') !== false ) {
  749. $this->setBrowser(self::BROWSER_NOKIA_S60);
  750. }
  751. else {
  752. $this->setBrowser( self::BROWSER_NOKIA );
  753. }
  754. $this->setMobile(true);
  755. return true;
  756. }
  757. return false;
  758. }
  759. /**
  760. * Determine if the browser is Firefox or not (last updated 1.7)
  761. * @return boolean True if the browser is Firefox otherwise false
  762. */
  763. protected function checkBrowserFirefox() {
  764. if( stripos($this->_agent,'safari') === false ) {
  765. if( preg_match("/Firefox[\/ \(]([^ ;\)]+)/i",$this->_agent,$matches) ) {
  766. $this->setVersion($matches[1]);
  767. $this->setBrowser(self::BROWSER_FIREFOX);
  768. return true;
  769. }
  770. else if( preg_match("/Firefox$/i",$this->_agent,$matches) ) {
  771. $this->setVersion("");
  772. $this->setBrowser(self::BROWSER_FIREFOX);
  773. return true;
  774. }
  775. }
  776. return false;
  777. }
  778. /**
  779. * Determine if the browser is Firefox or not (last updated 1.7)
  780. * @return boolean True if the browser is Firefox otherwise false
  781. */
  782. protected function checkBrowserIceweasel() {
  783. if( stripos($this->_agent,'Iceweasel') !== false ) {
  784. $aresult = explode('/',stristr($this->_agent,'Iceweasel'));
  785. $aversion = explode(' ',$aresult[1]);
  786. $this->setVersion($aversion[0]);
  787. $this->setBrowser(self::BROWSER_ICEWEASEL);
  788. return true;
  789. }
  790. return false;
  791. }
  792. /**
  793. * Determine if the browser is Mozilla or not (last updated 1.7)
  794. * @return boolean True if the browser is Mozilla otherwise false
  795. */
  796. protected function checkBrowserMozilla() {
  797. if( stripos($this->_agent,'mozilla') !== false && preg_match('/rv:[0-9].[0-9][a-b]?/i',$this->_agent) && stripos($this->_agent,'netscape') === false) {
  798. $aversion = explode(' ',stristr($this->_agent,'rv:'));
  799. preg_match('/rv:[0-9].[0-9][a-b]?/i',$this->_agent,$aversion);
  800. $this->setVersion(str_replace('rv:','',$aversion[0]));
  801. $this->setBrowser(self::BROWSER_MOZILLA);
  802. return true;
  803. }
  804. else if( stripos($this->_agent,'mozilla') !== false && preg_match('/rv:[0-9]\.[0-9]/i',$this->_agent) && stripos($this->_agent,'netscape') === false ) {
  805. $aversion = explode('',stristr($this->_agent,'rv:'));
  806. $this->setVersion(str_replace('rv:','',$aversion[0]));
  807. $this->setBrowser(self::BROWSER_MOZILLA);
  808. return true;
  809. }
  810. else if( stripos($this->_agent,'mozilla') !== false && preg_match('/mozilla\/([^ ]*)/i',$this->_agent,$matches) && stripos($this->_agent,'netscape') === false ) {
  811. $this->setVersion($matches[1]);
  812. $this->setBrowser(self::BROWSER_MOZILLA);
  813. return true;
  814. }
  815. return false;
  816. }
  817. /**
  818. * Determine if the browser is Lynx or not (last updated 1.7)
  819. * @return boolean True if the browser is Lynx otherwise false
  820. */
  821. protected function checkBrowserLynx() {
  822. if( stripos($this->_agent,'lynx') !== false ) {
  823. $aresult = explode('/',stristr($this->_agent,'Lynx'));
  824. $aversion = explode(' ',(isset($aresult[1])?$aresult[1]:""));
  825. $this->setVersion($aversion[0]);
  826. $this->setBrowser(self::BROWSER_LYNX);
  827. return true;
  828. }
  829. return false;
  830. }
  831. /**
  832. * Determine if the browser is Amaya or not (last updated 1.7)
  833. * @return boolean True if the browser is Amaya otherwise false
  834. */
  835. protected function checkBrowserAmaya() {
  836. if( stripos($this->_agent,'amaya') !== false ) {
  837. $aresult = explode('/',stristr($this->_agent,'Amaya'));
  838. $aversion = explode(' ',$aresult[1]);
  839. $this->setVersion($aversion[0]);
  840. $this->setBrowser(self::BROWSER_AMAYA);
  841. return true;
  842. }
  843. return false;
  844. }
  845. /**
  846. * Determine if the browser is Safari or not (last updated 1.7)
  847. * @return boolean True if the browser is Safari otherwise false
  848. */
  849. protected function checkBrowserSafari() {
  850. if( stripos($this->_agent,'Safari') !== false && stripos($this->_agent,'iPhone') === false && stripos($this->_agent,'iPod') === false ) {
  851. $aresult = explode('/',stristr($this->_agent,'Version'));
  852. if( isset($aresult[1]) ) {
  853. $aversion = explode(' ',$aresult[1]);
  854. $this->setVersion($aversion[0]);
  855. }
  856. else {
  857. $this->setVersion(self::VERSION_UNKNOWN);
  858. }
  859. $this->setBrowser(self::BROWSER_SAFARI);
  860. return true;
  861. }
  862. return false;
  863. }
  864. /**
  865. * Determine if the browser is iPhone or not (last updated 1.7)
  866. * @return boolean True if the browser is iPhone otherwise false
  867. */
  868. protected function checkBrowseriPhone() {
  869. if( stripos($this->_agent,'iPhone') !== false ) {
  870. $aresult = explode('/',stristr($this->_agent,'Version'));
  871. if( isset($aresult[1]) ) {
  872. $aversion = explode(' ',$aresult[1]);
  873. $this->setVersion($aversion[0]);
  874. }
  875. else {
  876. $this->setVersion(self::VERSION_UNKNOWN);
  877. }
  878. $this->setMobile(true);
  879. $this->setBrowser(self::BROWSER_IPHONE);
  880. return true;
  881. }
  882. return false;
  883. }
  884. /**
  885. * Determine if the browser is iPod or not (last updated 1.7)
  886. * @return boolean True if the browser is iPod otherwise false
  887. */
  888. protected function checkBrowseriPad() {
  889. if( stripos($this->_agent,'iPad') !== false ) {
  890. $aresult = explode('/',stristr($this->_agent,'Version'));
  891. if( isset($aresult[1]) ) {
  892. $aversion = explode(' ',$aresult[1]);
  893. $this->setVersion($aversion[0]);
  894. }
  895. else {
  896. $this->setVersion(self::VERSION_UNKNOWN);
  897. }
  898. $this->setMobile(true);
  899. $this->setBrowser(self::BROWSER_IPAD);
  900. return true;
  901. }
  902. return false;
  903. }
  904. /**
  905. * Determine if the browser is iPod or not (last updated 1.7)
  906. * @return boolean True if the browser is iPod otherwise false
  907. */
  908. protected function checkBrowseriPod() {
  909. if( stripos($this->_agent,'iPod') !== false ) {
  910. $aresult = explode('/',stristr($this->_agent,'Version'));
  911. if( isset($aresult[1]) ) {
  912. $aversion = explode(' ',$aresult[1]);
  913. $this->setVersion($aversion[0]);
  914. }
  915. else {
  916. $this->setVersion(self::VERSION_UNKNOWN);
  917. }
  918. $this->setMobile(true);
  919. $this->setBrowser(self::BROWSER_IPOD);
  920. return true;
  921. }
  922. return false;
  923. }
  924. /**
  925. * Determine if the browser is Android or not (last updated 1.7)
  926. * @return boolean True if the browser is Android otherwise false
  927. */
  928. protected function checkBrowserAndroid() {
  929. if( stripos($this->_agent,'Android') !== false ) {
  930. $aresult = explode(' ',stristr($this->_agent,'Android'));
  931. if( isset($aresult[1]) ) {
  932. $aversion = explode(' ',$aresult[1]);
  933. $this->setVersion($aversion[0]);
  934. }
  935. else {
  936. $this->setVersion(self::VERSION_UNKNOWN);
  937. }
  938. $this->setMobile(true);
  939. $this->setBrowser(self::BROWSER_ANDROID);
  940. return true;
  941. }
  942. return false;
  943. }
  944. /**
  945. * Determine the user's platform (last updated 1.7)
  946. */
  947. protected function checkPlatform() {
  948. if( stripos($this->_agent, 'windows') !== false ) {
  949. $this->_platform = self::PLATFORM_WINDOWS;
  950. }
  951. else if( stripos($this->_agent, 'iPad') !== false ) {
  952. $this->_platform = self::PLATFORM_IPAD;
  953. }
  954. else if( stripos($this->_agent, 'iPod') !== false ) {
  955. $this->_platform = self::PLATFORM_IPOD;
  956. }
  957. else if( stripos($this->_agent, 'iPhone') !== false ) {
  958. $this->_platform = self::PLATFORM_IPHONE;
  959. }
  960. elseif( stripos($this->_agent, 'mac') !== false ) {
  961. $this->_platform = self::PLATFORM_APPLE;
  962. }
  963. elseif( stripos($this->_agent, 'android') !== false ) {
  964. $this->_platform = self::PLATFORM_ANDROID;
  965. }
  966. elseif( stripos($this->_agent, 'linux') !== false ) {
  967. $this->_platform = self::PLATFORM_LINUX;
  968. }
  969. else if( stripos($this->_agent, 'Nokia') !== false ) {
  970. $this->_platform = self::PLATFORM_NOKIA;
  971. }
  972. else if( stripos($this->_agent, 'BlackBerry') !== false ) {
  973. $this->_platform = self::PLATFORM_BLACKBERRY;
  974. }
  975. elseif( stripos($this->_agent,'FreeBSD') !== false ) {
  976. $this->_platform = self::PLATFORM_FREEBSD;
  977. }
  978. elseif( stripos($this->_agent,'OpenBSD') !== false ) {
  979. $this->_platform = self::PLATFORM_OPENBSD;
  980. }
  981. elseif( stripos($this->_agent,'NetBSD') !== false ) {
  982. $this->_platform = self::PLATFORM_NETBSD;
  983. }
  984. elseif( stripos($this->_agent, 'OpenSolaris') !== false ) {
  985. $this->_platform = self::PLATFORM_OPENSOLARIS;
  986. }
  987. elseif( stripos($this->_agent, 'SunOS') !== false ) {
  988. $this->_platform = self::PLATFORM_SUNOS;
  989. }
  990. elseif( stripos($this->_agent, 'OS\/2') !== false ) {
  991. $this->_platform = self::PLATFORM_OS2;
  992. }
  993. elseif( stripos($this->_agent, 'BeOS') !== false ) {
  994. $this->_platform = self::PLATFORM_BEOS;
  995. }
  996. elseif( stripos($this->_agent, 'win') !== false ) {
  997. $this->_platform = self::PLATFORM_WINDOWS;
  998. }
  999. }
  1000. }
  1001. ?>