PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/war/php/libraries/browser.php

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