PageRenderTime 45ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Browser.php/lib/Browser.php

https://bitbucket.org/englishextra/shimansky.biz
PHP | 1177 lines | 790 code | 83 blank | 304 comment | 105 complexity | 85be3264032ed38e6a46609706c65f4b MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-SA-3.0, GPL-2.0, LGPL-3.0, GPL-3.0, CC-BY-3.0
  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. */
  36. class Browser
  37. {
  38. private $_agent = '';
  39. private $_browser_name = '';
  40. private $_version = '';
  41. private $_platform = '';
  42. private $_os = '';
  43. private $_is_aol = false;
  44. private $_is_mobile = false;
  45. private $_is_tablet = false;
  46. private $_is_robot = false;
  47. private $_is_facebook = false;
  48. private $_aol_version = '';
  49. const BROWSER_UNKNOWN = 'unknown';
  50. const VERSION_UNKNOWN = 'unknown';
  51. const BROWSER_OPERA = 'Opera'; // http://www.opera.com/
  52. const BROWSER_OPERA_MINI = 'Opera Mini'; // http://www.opera.com/mini/
  53. const BROWSER_WEBTV = 'WebTV'; // http://www.webtv.net/pc/
  54. const BROWSER_IE = 'Internet Explorer'; // http://www.microsoft.com/ie/
  55. const BROWSER_POCKET_IE = 'Pocket Internet Explorer'; // http://en.wikipedia.org/wiki/Internet_Explorer_Mobile
  56. const BROWSER_KONQUEROR = 'Konqueror'; // http://www.konqueror.org/
  57. const BROWSER_ICAB = 'iCab'; // http://www.icab.de/
  58. const BROWSER_OMNIWEB = 'OmniWeb'; // http://www.omnigroup.com/applications/omniweb/
  59. const BROWSER_FIREBIRD = 'Firebird'; // http://www.ibphoenix.com/
  60. const BROWSER_FIREFOX = 'Firefox'; // http://www.mozilla.com/en-US/firefox/firefox.html
  61. const BROWSER_ICEWEASEL = 'Iceweasel'; // http://www.geticeweasel.org/
  62. const BROWSER_SHIRETOKO = 'Shiretoko'; // http://wiki.mozilla.org/Projects/shiretoko
  63. const BROWSER_MOZILLA = 'Mozilla'; // http://www.mozilla.com/en-US/
  64. const BROWSER_AMAYA = 'Amaya'; // http://www.w3.org/Amaya/
  65. const BROWSER_LYNX = 'Lynx'; // http://en.wikipedia.org/wiki/Lynx
  66. const BROWSER_SAFARI = 'Safari'; // http://apple.com
  67. const BROWSER_IPHONE = 'iPhone'; // http://apple.com
  68. const BROWSER_IPOD = 'iPod'; // http://apple.com
  69. const BROWSER_IPAD = 'iPad'; // http://apple.com
  70. const BROWSER_CHROME = 'Chrome'; // http://www.google.com/chrome
  71. const BROWSER_ANDROID = 'Android'; // http://www.android.com/
  72. const BROWSER_GOOGLEBOT = 'GoogleBot'; // http://en.wikipedia.org/wiki/Googlebot
  73. const BROWSER_SLURP = 'Yahoo! Slurp'; // http://en.wikipedia.org/wiki/Yahoo!_Slurp
  74. const BROWSER_W3CVALIDATOR = 'W3C Validator'; // http://validator.w3.org/
  75. const BROWSER_BLACKBERRY = 'BlackBerry'; // http://www.blackberry.com/
  76. const BROWSER_ICECAT = 'IceCat'; // http://en.wikipedia.org/wiki/GNU_IceCat
  77. const BROWSER_NOKIA_S60 = 'Nokia S60 OSS Browser'; // http://en.wikipedia.org/wiki/Web_Browser_for_S60
  78. const BROWSER_NOKIA = 'Nokia Browser'; // * all other WAP-based browsers on the Nokia Platform
  79. const BROWSER_MSN = 'MSN Browser'; // http://explorer.msn.com/
  80. const BROWSER_MSNBOT = 'MSN Bot'; // http://search.msn.com/msnbot.htm
  81. // http://en.wikipedia.org/wiki/Msnbot (used for Bing as well)
  82. const BROWSER_NETSCAPE_NAVIGATOR = 'Netscape Navigator'; // http://browser.netscape.com/ (DEPRECATED)
  83. const BROWSER_GALEON = 'Galeon'; // http://galeon.sourceforge.net/ (DEPRECATED)
  84. const BROWSER_NETPOSITIVE = 'NetPositive'; // http://en.wikipedia.org/wiki/NetPositive (DEPRECATED)
  85. const BROWSER_PHOENIX = 'Phoenix'; // http://en.wikipedia.org/wiki/History_of_Mozilla_Firefox (DEPRECATED)
  86. const PLATFORM_UNKNOWN = 'unknown';
  87. const PLATFORM_WINDOWS = 'Windows';
  88. const PLATFORM_WINDOWS_CE = 'Windows CE';
  89. const PLATFORM_APPLE = 'Apple';
  90. const PLATFORM_LINUX = 'Linux';
  91. const PLATFORM_OS2 = 'OS/2';
  92. const PLATFORM_BEOS = 'BeOS';
  93. const PLATFORM_IPHONE = 'iPhone';
  94. const PLATFORM_IPOD = 'iPod';
  95. const PLATFORM_IPAD = 'iPad';
  96. const PLATFORM_BLACKBERRY = 'BlackBerry';
  97. const PLATFORM_NOKIA = 'Nokia';
  98. const PLATFORM_FREEBSD = 'FreeBSD';
  99. const PLATFORM_OPENBSD = 'OpenBSD';
  100. const PLATFORM_NETBSD = 'NetBSD';
  101. const PLATFORM_SUNOS = 'SunOS';
  102. const PLATFORM_OPENSOLARIS = 'OpenSolaris';
  103. const PLATFORM_ANDROID = 'Android';
  104. const OPERATING_SYSTEM_UNKNOWN = 'unknown';
  105. public function Browser($userAgent = "")
  106. {
  107. $this->reset();
  108. if ($userAgent != "") {
  109. $this->setUserAgent($userAgent);
  110. } else {
  111. $this->determine();
  112. }
  113. }
  114. /**
  115. * Reset all properties
  116. */
  117. public function reset()
  118. {
  119. $this->_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
  120. $this->_browser_name = self::BROWSER_UNKNOWN;
  121. $this->_version = self::VERSION_UNKNOWN;
  122. $this->_platform = self::PLATFORM_UNKNOWN;
  123. $this->_os = self::OPERATING_SYSTEM_UNKNOWN;
  124. $this->_is_aol = false;
  125. $this->_is_mobile = false;
  126. $this->_is_tablet = false;
  127. $this->_is_robot = false;
  128. $this->_is_facebook = false;
  129. $this->_aol_version = self::VERSION_UNKNOWN;
  130. }
  131. /**
  132. * Check to see if the specific browser is valid
  133. * @param string $browserName
  134. * @return bool True if the browser is the specified browser
  135. */
  136. function isBrowser($browserName)
  137. {
  138. return (0 == strcasecmp($this->_browser_name, trim($browserName)));
  139. }
  140. /**
  141. * The name of the browser. All return types are from the class contants
  142. * @return string Name of the browser
  143. */
  144. public function getBrowser()
  145. {
  146. return $this->_browser_name;
  147. }
  148. /**
  149. * Set the name of the browser
  150. * @param $browser string The name of the Browser
  151. */
  152. public function setBrowser($browser)
  153. {
  154. $this->_browser_name = $browser;
  155. }
  156. /**
  157. * The name of the platform. All return types are from the class contants
  158. * @return string Name of the browser
  159. */
  160. public function getPlatform()
  161. {
  162. return $this->_platform;
  163. }
  164. /**
  165. * Set the name of the platform
  166. * @param string $platform The name of the Platform
  167. */
  168. public function setPlatform($platform)
  169. {
  170. $this->_platform = $platform;
  171. }
  172. /**
  173. * The version of the browser.
  174. * @return string Version of the browser (will only contain alpha-numeric characters and a period)
  175. */
  176. public function getVersion()
  177. {
  178. return $this->_version;
  179. }
  180. /**
  181. * Set the version of the browser
  182. * @param string $version The version of the Browser
  183. */
  184. public function setVersion($version)
  185. {
  186. $this->_version = preg_replace('/[^0-9,.,a-z,A-Z-]/', '', $version);
  187. }
  188. /**
  189. * The version of AOL.
  190. * @return string Version of AOL (will only contain alpha-numeric characters and a period)
  191. */
  192. public function getAolVersion()
  193. {
  194. return $this->_aol_version;
  195. }
  196. /**
  197. * Set the version of AOL
  198. * @param string $version The version of AOL
  199. */
  200. public function setAolVersion($version)
  201. {
  202. $this->_aol_version = preg_replace('/[^0-9,.,a-z,A-Z]/', '', $version);
  203. }
  204. /**
  205. * Is the browser from AOL?
  206. * @return boolean True if the browser is from AOL otherwise false
  207. */
  208. public function isAol()
  209. {
  210. return $this->_is_aol;
  211. }
  212. /**
  213. * Is the browser from a mobile device?
  214. * @return boolean True if the browser is from a mobile device otherwise false
  215. */
  216. public function isMobile()
  217. {
  218. return $this->_is_mobile;
  219. }
  220. /**
  221. * Is the browser from a tablet device?
  222. * @return boolean True if the browser is from a tablet device otherwise false
  223. */
  224. public function isTablet()
  225. {
  226. return $this->_is_tablet;
  227. }
  228. /**
  229. * Is the browser from a robot (ex Slurp,GoogleBot)?
  230. * @return boolean True if the browser is from a robot otherwise false
  231. */
  232. public function isRobot()
  233. {
  234. return $this->_is_robot;
  235. }
  236. /**
  237. * Is the browser from facebook?
  238. * @return boolean True if the browser is from facebook otherwise false
  239. */
  240. public function isFacebook()
  241. {
  242. return $this->_is_facebook;
  243. }
  244. /**
  245. * Set the browser to be from AOL
  246. * @param $isAol
  247. */
  248. public function setAol($isAol)
  249. {
  250. $this->_is_aol = $isAol;
  251. }
  252. /**
  253. * Set the Browser to be mobile
  254. * @param boolean $value is the browser a mobile browser or not
  255. */
  256. protected function setMobile($value = true)
  257. {
  258. $this->_is_mobile = $value;
  259. }
  260. /**
  261. * Set the Browser to be tablet
  262. * @param boolean $value is the browser a tablet browser or not
  263. */
  264. protected function setTablet($value = true)
  265. {
  266. $this->_is_tablet = $value;
  267. }
  268. /**
  269. * Set the Browser to be a robot
  270. * @param boolean $value is the browser a robot or not
  271. */
  272. protected function setRobot($value = true)
  273. {
  274. $this->_is_robot = $value;
  275. }
  276. /**
  277. * Set the Browser to be a Facebook request
  278. * @param boolean $value is the browser a robot or not
  279. */
  280. protected function setFacebook($value = true)
  281. {
  282. $this->_is_facebook = $value;
  283. }
  284. /**
  285. * Get the user agent value in use to determine the browser
  286. * @return string The user agent from the HTTP header
  287. */
  288. public function getUserAgent()
  289. {
  290. return $this->_agent;
  291. }
  292. /**
  293. * Set the user agent value (the construction will use the HTTP header value - this will overwrite it)
  294. * @param string $agent_string The value for the User Agent
  295. */
  296. public function setUserAgent($agent_string)
  297. {
  298. $this->reset();
  299. $this->_agent = $agent_string;
  300. $this->determine();
  301. }
  302. /**
  303. * Used to determine if the browser is actually "chromeframe"
  304. * @since 1.7
  305. * @return boolean True if the browser is using chromeframe
  306. */
  307. public function isChromeFrame()
  308. {
  309. return (strpos($this->_agent, "chromeframe") !== false);
  310. }
  311. /**
  312. * Returns a formatted string with a summary of the details of the browser.
  313. * @return string formatted string with a summary of the browser
  314. */
  315. public function __toString()
  316. {
  317. return "<strong>Browser Name:</strong> {$this->getBrowser()}<br/>\n" .
  318. "<strong>Browser Version:</strong> {$this->getVersion()}<br/>\n" .
  319. "<strong>Browser User Agent String:</strong> {$this->getUserAgent()}<br/>\n" .
  320. "<strong>Platform:</strong> {$this->getPlatform()}<br/>";
  321. }
  322. /**
  323. * Protected routine to calculate and determine what the browser is in use (including platform)
  324. */
  325. protected function determine()
  326. {
  327. $this->checkPlatform();
  328. $this->checkBrowsers();
  329. $this->checkForAol();
  330. }
  331. /**
  332. * Protected routine to determine the browser type
  333. * @return boolean True if the browser was detected otherwise false
  334. */
  335. protected function checkBrowsers()
  336. {
  337. return (
  338. // well-known, well-used
  339. // Special Notes:
  340. // (1) Opera must be checked before FireFox due to the odd
  341. // user agents used in some older versions of Opera
  342. // (2) WebTV is strapped onto Internet Explorer so we must
  343. // check for WebTV before IE
  344. // (3) (deprecated) Galeon is based on Firefox and needs to be
  345. // tested before Firefox is tested
  346. // (4) OmniWeb is based on Safari so OmniWeb check must occur
  347. // before Safari
  348. // (5) Netscape 9+ is based on Firefox so Netscape checks
  349. // before FireFox are necessary
  350. $this->checkBrowserWebTv() ||
  351. $this->checkBrowserInternetExplorer() ||
  352. $this->checkBrowserOpera() ||
  353. $this->checkBrowserGaleon() ||
  354. $this->checkBrowserNetscapeNavigator9Plus() ||
  355. $this->checkBrowserFirefox() ||
  356. $this->checkBrowserChrome() ||
  357. $this->checkBrowserOmniWeb() ||
  358. // common mobile
  359. $this->checkBrowserAndroid() ||
  360. $this->checkBrowseriPad() ||
  361. $this->checkBrowseriPod() ||
  362. $this->checkBrowseriPhone() ||
  363. $this->checkBrowserBlackBerry() ||
  364. $this->checkBrowserNokia() ||
  365. // common bots
  366. $this->checkBrowserGoogleBot() ||
  367. $this->checkBrowserMSNBot() ||
  368. $this->checkBrowserSlurp() ||
  369. // check for facebook external hit when loading URL
  370. $this->checkFacebookExternalHit() ||
  371. // WebKit base check (post mobile and others)
  372. $this->checkBrowserSafari() ||
  373. // everyone else
  374. $this->checkBrowserNetPositive() ||
  375. $this->checkBrowserFirebird() ||
  376. $this->checkBrowserKonqueror() ||
  377. $this->checkBrowserIcab() ||
  378. $this->checkBrowserPhoenix() ||
  379. $this->checkBrowserAmaya() ||
  380. $this->checkBrowserLynx() ||
  381. $this->checkBrowserShiretoko() ||
  382. $this->checkBrowserIceCat() ||
  383. $this->checkBrowserIceweasel() ||
  384. $this->checkBrowserW3CValidator() ||
  385. $this->checkBrowserMozilla() /* Mozilla is such an open standard that you must check it last */
  386. );
  387. }
  388. /**
  389. * Determine if the user is using a BlackBerry (last updated 1.7)
  390. * @return boolean True if the browser is the BlackBerry browser otherwise false
  391. */
  392. protected function checkBrowserBlackBerry()
  393. {
  394. if (stripos($this->_agent, 'blackberry') !== false) {
  395. $aresult = explode("/", stristr($this->_agent, "BlackBerry"));
  396. $aversion = explode(' ', $aresult[1]);
  397. $this->setVersion($aversion[0]);
  398. $this->_browser_name = self::BROWSER_BLACKBERRY;
  399. $this->setMobile(true);
  400. return true;
  401. }
  402. return false;
  403. }
  404. /**
  405. * Determine if the user is using an AOL User Agent (last updated 1.7)
  406. * @return boolean True if the browser is from AOL otherwise false
  407. */
  408. protected function checkForAol()
  409. {
  410. $this->setAol(false);
  411. $this->setAolVersion(self::VERSION_UNKNOWN);
  412. if (stripos($this->_agent, 'aol') !== false) {
  413. $aversion = explode(' ', stristr($this->_agent, 'AOL'));
  414. $this->setAol(true);
  415. $this->setAolVersion(preg_replace('/[^0-9\.a-z]/i', '', $aversion[1]));
  416. return true;
  417. }
  418. return false;
  419. }
  420. /**
  421. * Determine if the browser is the GoogleBot or not (last updated 1.7)
  422. * @return boolean True if the browser is the GoogletBot otherwise false
  423. */
  424. protected function checkBrowserGoogleBot()
  425. {
  426. if (stripos($this->_agent, 'googlebot') !== false) {
  427. $aresult = explode('/', stristr($this->_agent, 'googlebot'));
  428. $aversion = explode(' ', $aresult[1]);
  429. $this->setVersion(str_replace(';', '', $aversion[0]));
  430. $this->_browser_name = self::BROWSER_GOOGLEBOT;
  431. $this->setRobot(true);
  432. return true;
  433. }
  434. return false;
  435. }
  436. /**
  437. * Determine if the browser is the MSNBot or not (last updated 1.9)
  438. * @return boolean True if the browser is the MSNBot otherwise false
  439. */
  440. protected function checkBrowserMSNBot()
  441. {
  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. {
  458. if (stripos($this->_agent, 'W3C-checklink') !== false) {
  459. $aresult = explode('/', stristr($this->_agent, 'W3C-checklink'));
  460. $aversion = explode(' ', $aresult[1]);
  461. $this->setVersion($aversion[0]);
  462. $this->_browser_name = self::BROWSER_W3CVALIDATOR;
  463. return true;
  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. {
  481. if (stripos($this->_agent, 'slurp') !== false) {
  482. $aresult = explode('/', stristr($this->_agent, 'Slurp'));
  483. $aversion = explode(' ', $aresult[1]);
  484. $this->setVersion($aversion[0]);
  485. $this->_browser_name = self::BROWSER_SLURP;
  486. $this->setRobot(true);
  487. $this->setMobile(false);
  488. return true;
  489. }
  490. return false;
  491. }
  492. /**
  493. * Determine if the browser is Internet Explorer or not (last updated 1.7)
  494. * @return boolean True if the browser is Internet Explorer otherwise false
  495. */
  496. protected function checkBrowserInternetExplorer()
  497. {
  498. // Test for v1 - v1.5 IE
  499. if (stripos($this->_agent, 'microsoft internet explorer') !== false) {
  500. $this->setBrowser(self::BROWSER_IE);
  501. $this->setVersion('1.0');
  502. $aresult = stristr($this->_agent, '/');
  503. if (preg_match('/308|425|426|474|0b1/i', $aresult)) {
  504. $this->setVersion('1.5');
  505. }
  506. return true;
  507. } // Test for versions > 1.5
  508. else if (stripos($this->_agent, 'msie') !== false && stripos($this->_agent, 'opera') === false) {
  509. // See if the browser is the odd MSN Explorer
  510. if (stripos($this->_agent, 'msnb') !== false) {
  511. $aresult = explode(' ', stristr(str_replace(';', '; ', $this->_agent), 'MSN'));
  512. $this->setBrowser(self::BROWSER_MSN);
  513. $this->setVersion(str_replace(array('(', ')', ';'), '', $aresult[1]));
  514. return true;
  515. }
  516. $aresult = explode(' ', stristr(str_replace(';', '; ', $this->_agent), 'msie'));
  517. $this->setBrowser(self::BROWSER_IE);
  518. $this->setVersion(str_replace(array('(', ')', ';'), '', $aresult[1]));
  519. return true;
  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. } else {
  529. $aversion = explode('/', $this->_agent);
  530. $this->setVersion($aversion[1]);
  531. }
  532. return true;
  533. }
  534. return false;
  535. }
  536. /**
  537. * Determine if the browser is Opera or not (last updated 1.7)
  538. * @return boolean True if the browser is Opera otherwise false
  539. */
  540. protected function checkBrowserOpera()
  541. {
  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. } else {
  549. $aversion = explode(' ', stristr($resultant, 'opera mini'));
  550. $this->setVersion($aversion[1]);
  551. }
  552. $this->_browser_name = self::BROWSER_OPERA_MINI;
  553. $this->setMobile(true);
  554. return true;
  555. } else if (stripos($this->_agent, 'opera') !== false) {
  556. $resultant = stristr($this->_agent, 'opera');
  557. if (preg_match('/Version\/(1*.*)$/', $resultant, $matches)) {
  558. $this->setVersion($matches[1]);
  559. } else if (preg_match('/\//', $resultant)) {
  560. $aresult = explode('/', str_replace("(", " ", $resultant));
  561. $aversion = explode(' ', $aresult[1]);
  562. $this->setVersion($aversion[0]);
  563. } else {
  564. $aversion = explode(' ', stristr($resultant, 'opera'));
  565. $this->setVersion(isset($aversion[1]) ? $aversion[1] : "");
  566. }
  567. if (stripos($this->_agent, 'Opera Mobi') !== false) {
  568. $this->setMobile(true);
  569. }
  570. $this->_browser_name = self::BROWSER_OPERA;
  571. return true;
  572. } else if (stripos($this->_agent, 'OPR') !== false) {
  573. $resultant = stristr($this->_agent, 'OPR');
  574. if (preg_match('/\//', $resultant)) {
  575. $aresult = explode('/', str_replace("(", " ", $resultant));
  576. $aversion = explode(' ', $aresult[1]);
  577. $this->setVersion($aversion[0]);
  578. }
  579. if (stripos($this->_agent, 'Mobile') !== false) {
  580. $this->setMobile(true);
  581. }
  582. $this->_browser_name = self::BROWSER_OPERA;
  583. return true;
  584. }
  585. return false;
  586. }
  587. /**
  588. * Determine if the browser is Chrome or not (last updated 1.7)
  589. * @return boolean True if the browser is Chrome otherwise false
  590. */
  591. protected function checkBrowserChrome()
  592. {
  593. if (stripos($this->_agent, 'Chrome') !== false) {
  594. $aresult = explode('/', stristr($this->_agent, 'Chrome'));
  595. $aversion = explode(' ', $aresult[1]);
  596. $this->setVersion($aversion[0]);
  597. $this->setBrowser(self::BROWSER_CHROME);
  598. //Chrome on Android
  599. if (stripos($this->_agent, 'Android') !== false) {
  600. $this->setMobile(true);
  601. }
  602. return true;
  603. }
  604. return false;
  605. }
  606. /**
  607. * Determine if the browser is WebTv or not (last updated 1.7)
  608. * @return boolean True if the browser is WebTv otherwise false
  609. */
  610. protected function checkBrowserWebTv()
  611. {
  612. if (stripos($this->_agent, 'webtv') !== false) {
  613. $aresult = explode('/', stristr($this->_agent, 'webtv'));
  614. $aversion = explode(' ', $aresult[1]);
  615. $this->setVersion($aversion[0]);
  616. $this->setBrowser(self::BROWSER_WEBTV);
  617. return true;
  618. }
  619. return false;
  620. }
  621. /**
  622. * Determine if the browser is NetPositive or not (last updated 1.7)
  623. * @return boolean True if the browser is NetPositive otherwise false
  624. */
  625. protected function checkBrowserNetPositive()
  626. {
  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. {
  642. if (stripos($this->_agent, 'galeon') !== false) {
  643. $aresult = explode(' ', stristr($this->_agent, 'galeon'));
  644. $aversion = explode('/', $aresult[0]);
  645. $this->setVersion($aversion[1]);
  646. $this->setBrowser(self::BROWSER_GALEON);
  647. return true;
  648. }
  649. return false;
  650. }
  651. /**
  652. * Determine if the browser is Konqueror or not (last updated 1.7)
  653. * @return boolean True if the browser is Konqueror otherwise false
  654. */
  655. protected function checkBrowserKonqueror()
  656. {
  657. if (stripos($this->_agent, 'Konqueror') !== false) {
  658. $aresult = explode(' ', stristr($this->_agent, 'Konqueror'));
  659. $aversion = explode('/', $aresult[0]);
  660. $this->setVersion($aversion[1]);
  661. $this->setBrowser(self::BROWSER_KONQUEROR);
  662. return true;
  663. }
  664. return false;
  665. }
  666. /**
  667. * Determine if the browser is iCab or not (last updated 1.7)
  668. * @return boolean True if the browser is iCab otherwise false
  669. */
  670. protected function checkBrowserIcab()
  671. {
  672. if (stripos($this->_agent, 'icab') !== false) {
  673. $aversion = explode(' ', stristr(str_replace('/', ' ', $this->_agent), 'icab'));
  674. $this->setVersion($aversion[1]);
  675. $this->setBrowser(self::BROWSER_ICAB);
  676. return true;
  677. }
  678. return false;
  679. }
  680. /**
  681. * Determine if the browser is OmniWeb or not (last updated 1.7)
  682. * @return boolean True if the browser is OmniWeb otherwise false
  683. */
  684. protected function checkBrowserOmniWeb()
  685. {
  686. if (stripos($this->_agent, 'omniweb') !== false) {
  687. $aresult = explode('/', stristr($this->_agent, 'omniweb'));
  688. $aversion = explode(' ', isset($aresult[1]) ? $aresult[1] : "");
  689. $this->setVersion($aversion[0]);
  690. $this->setBrowser(self::BROWSER_OMNIWEB);
  691. return true;
  692. }
  693. return false;
  694. }
  695. /**
  696. * Determine if the browser is Phoenix or not (last updated 1.7)
  697. * @return boolean True if the browser is Phoenix otherwise false
  698. */
  699. protected function checkBrowserPhoenix()
  700. {
  701. if (stripos($this->_agent, 'Phoenix') !== false) {
  702. $aversion = explode('/', stristr($this->_agent, 'Phoenix'));
  703. $this->setVersion($aversion[1]);
  704. $this->setBrowser(self::BROWSER_PHOENIX);
  705. return true;
  706. }
  707. return false;
  708. }
  709. /**
  710. * Determine if the browser is Firebird or not (last updated 1.7)
  711. * @return boolean True if the browser is Firebird otherwise false
  712. */
  713. protected function checkBrowserFirebird()
  714. {
  715. if (stripos($this->_agent, 'Firebird') !== false) {
  716. $aversion = explode('/', stristr($this->_agent, 'Firebird'));
  717. $this->setVersion($aversion[1]);
  718. $this->setBrowser(self::BROWSER_FIREBIRD);
  719. return true;
  720. }
  721. return false;
  722. }
  723. /**
  724. * Determine if the browser is Netscape Navigator 9+ or not (last updated 1.7)
  725. * NOTE: (http://browser.netscape.com/ - Official support ended on March 1st, 2008)
  726. * @return boolean True if the browser is Netscape Navigator 9+ otherwise false
  727. */
  728. protected function checkBrowserNetscapeNavigator9Plus()
  729. {
  730. if (stripos($this->_agent, 'Firefox') !== false && preg_match('/Navigator\/([^ ]*)/i', $this->_agent, $matches)) {
  731. $this->setVersion($matches[1]);
  732. $this->setBrowser(self::BROWSER_NETSCAPE_NAVIGATOR);
  733. return true;
  734. } else if (stripos($this->_agent, 'Firefox') === false && preg_match('/Netscape6?\/([^ ]*)/i', $this->_agent, $matches)) {
  735. $this->setVersion($matches[1]);
  736. $this->setBrowser(self::BROWSER_NETSCAPE_NAVIGATOR);
  737. return true;
  738. }
  739. return false;
  740. }
  741. /**
  742. * Determine if the browser is Shiretoko or not (https://wiki.mozilla.org/Projects/shiretoko) (last updated 1.7)
  743. * @return boolean True if the browser is Shiretoko otherwise false
  744. */
  745. protected function checkBrowserShiretoko()
  746. {
  747. if (stripos($this->_agent, 'Mozilla') !== false && preg_match('/Shiretoko\/([^ ]*)/i', $this->_agent, $matches)) {
  748. $this->setVersion($matches[1]);
  749. $this->setBrowser(self::BROWSER_SHIRETOKO);
  750. return true;
  751. }
  752. return false;
  753. }
  754. /**
  755. * Determine if the browser is Ice Cat or not (http://en.wikipedia.org/wiki/GNU_IceCat) (last updated 1.7)
  756. * @return boolean True if the browser is Ice Cat otherwise false
  757. */
  758. protected function checkBrowserIceCat()
  759. {
  760. if (stripos($this->_agent, 'Mozilla') !== false && preg_match('/IceCat\/([^ ]*)/i', $this->_agent, $matches)) {
  761. $this->setVersion($matches[1]);
  762. $this->setBrowser(self::BROWSER_ICECAT);
  763. return true;
  764. }
  765. return false;
  766. }
  767. /**
  768. * Determine if the browser is Nokia or not (last updated 1.7)
  769. * @return boolean True if the browser is Nokia otherwise false
  770. */
  771. protected function checkBrowserNokia()
  772. {
  773. if (preg_match("/Nokia([^\/]+)\/([^ SP]+)/i", $this->_agent, $matches)) {
  774. $this->setVersion($matches[2]);
  775. if (stripos($this->_agent, 'Series60') !== false || strpos($this->_agent, 'S60') !== false) {
  776. $this->setBrowser(self::BROWSER_NOKIA_S60);
  777. } else {
  778. $this->setBrowser(self::BROWSER_NOKIA);
  779. }
  780. $this->setMobile(true);
  781. return true;
  782. }
  783. return false;
  784. }
  785. /**
  786. * Determine if the browser is Firefox or not (last updated 1.7)
  787. * @return boolean True if the browser is Firefox otherwise false
  788. */
  789. protected function checkBrowserFirefox()
  790. {
  791. if (stripos($this->_agent, 'safari') === false) {
  792. if (preg_match("/Firefox[\/ \(]([^ ;\)]+)/i", $this->_agent, $matches)) {
  793. $this->setVersion($matches[1]);
  794. $this->setBrowser(self::BROWSER_FIREFOX);
  795. return true;
  796. } else if (preg_match("/Firefox$/i", $this->_agent, $matches)) {
  797. $this->setVersion("");
  798. $this->setBrowser(self::BROWSER_FIREFOX);
  799. return true;
  800. }
  801. }
  802. return false;
  803. }
  804. /**
  805. * Determine if the browser is Firefox or not (last updated 1.7)
  806. * @return boolean True if the browser is Firefox otherwise false
  807. */
  808. protected function checkBrowserIceweasel()
  809. {
  810. if (stripos($this->_agent, 'Iceweasel') !== false) {
  811. $aresult = explode('/', stristr($this->_agent, 'Iceweasel'));
  812. $aversion = explode(' ', $aresult[1]);
  813. $this->setVersion($aversion[0]);
  814. $this->setBrowser(self::BROWSER_ICEWEASEL);
  815. return true;
  816. }
  817. return false;
  818. }
  819. /**
  820. * Determine if the browser is Mozilla or not (last updated 1.7)
  821. * @return boolean True if the browser is Mozilla otherwise false
  822. */
  823. protected function checkBrowserMozilla()
  824. {
  825. if (stripos($this->_agent, 'mozilla') !== false && preg_match('/rv:[0-9].[0-9][a-b]?/i', $this->_agent) && stripos($this->_agent, 'netscape') === false) {
  826. $aversion = explode(' ', stristr($this->_agent, 'rv:'));
  827. preg_match('/rv:[0-9].[0-9][a-b]?/i', $this->_agent, $aversion);
  828. $this->setVersion(str_replace('rv:', '', $aversion[0]));
  829. $this->setBrowser(self::BROWSER_MOZILLA);
  830. return true;
  831. } else if (stripos($this->_agent, 'mozilla') !== false && preg_match('/rv:[0-9]\.[0-9]/i', $this->_agent) && stripos($this->_agent, 'netscape') === false) {
  832. $aversion = explode('', stristr($this->_agent, 'rv:'));
  833. $this->setVersion(str_replace('rv:', '', $aversion[0]));
  834. $this->setBrowser(self::BROWSER_MOZILLA);
  835. return true;
  836. } else if (stripos($this->_agent, 'mozilla') !== false && preg_match('/mozilla\/([^ ]*)/i', $this->_agent, $matches) && stripos($this->_agent, 'netscape') === false) {
  837. $this->setVersion($matches[1]);
  838. $this->setBrowser(self::BROWSER_MOZILLA);
  839. return true;
  840. }
  841. return false;
  842. }
  843. /**
  844. * Determine if the browser is Lynx or not (last updated 1.7)
  845. * @return boolean True if the browser is Lynx otherwise false
  846. */
  847. protected function checkBrowserLynx()
  848. {
  849. if (stripos($this->_agent, 'lynx') !== false) {
  850. $aresult = explode('/', stristr($this->_agent, 'Lynx'));
  851. $aversion = explode(' ', (isset($aresult[1]) ? $aresult[1] : ""));
  852. $this->setVersion($aversion[0]);
  853. $this->setBrowser(self::BROWSER_LYNX);
  854. return true;
  855. }
  856. return false;
  857. }
  858. /**
  859. * Determine if the browser is Amaya or not (last updated 1.7)
  860. * @return boolean True if the browser is Amaya otherwise false
  861. */
  862. protected function checkBrowserAmaya()
  863. {
  864. if (stripos($this->_agent, 'amaya') !== false) {
  865. $aresult = explode('/', stristr($this->_agent, 'Amaya'));
  866. $aversion = explode(' ', $aresult[1]);
  867. $this->setVersion($aversion[0]);
  868. $this->setBrowser(self::BROWSER_AMAYA);
  869. return true;
  870. }
  871. return false;
  872. }
  873. /**
  874. * Determine if the browser is Safari or not (last updated 1.7)
  875. * @return boolean True if the browser is Safari otherwise false
  876. */
  877. protected function checkBrowserSafari()
  878. {
  879. if (stripos($this->_agent, 'Safari') !== false
  880. && stripos($this->_agent, 'iPhone') === false
  881. && stripos($this->_agent, 'iPod') === false) {
  882. $aresult = explode('/', stristr($this->_agent, 'Version'));
  883. if (isset($aresult[1])) {
  884. $aversion = explode(' ', $aresult[1]);
  885. $this->setVersion($aversion[0]);
  886. } else {
  887. $this->setVersion(self::VERSION_UNKNOWN);
  888. }
  889. $this->setBrowser(self::BROWSER_SAFARI);
  890. return true;
  891. }
  892. return false;
  893. }
  894. /**
  895. * Detect if URL is loaded from FacebookExternalHit
  896. * @return boolean True if it detects FacebookExternalHit otherwise false
  897. */
  898. protected function checkFacebookExternalHit()
  899. {
  900. if(stristr($this->_agent,'FacebookExternalHit'))
  901. {
  902. $this->setRobot(true);
  903. $this->setFacebook(true);
  904. return true;
  905. }
  906. return false;
  907. }
  908. /**
  909. * Detect if URL is being loaded from internal Facebook browser
  910. * @return boolean True if it detects internal Facebook browser otherwise false
  911. */
  912. protected function checkForFacebookIos()
  913. {
  914. if(stristr($this->_agent,'FBIOS'))
  915. {
  916. $this->setFacebook(true);
  917. return true;
  918. }
  919. return false;
  920. }
  921. /**
  922. * Detect Version for the Safari browser on iOS devices
  923. * @return boolean True if it detects the version correctly otherwise false
  924. */
  925. protected function getSafariVersionOnIos()
  926. {
  927. $aresult = explode('/',stristr($this->_agent,'Version'));
  928. if( isset($aresult[1]) )
  929. {
  930. $aversion = explode(' ',$aresult[1]);
  931. $this->setVersion($aversion[0]);
  932. return true;
  933. }
  934. return false;
  935. }
  936. /**
  937. * Detect Version for the Chrome browser on iOS devices
  938. * @return boolean True if it detects the version correctly otherwise false
  939. */
  940. protected function getChromeVersionOnIos()
  941. {
  942. $aresult = explode('/',stristr($this->_agent,'CriOS'));
  943. if( isset($aresult[1]) )
  944. {
  945. $aversion = explode(' ',$aresult[1]);
  946. $this->setVersion($aversion[0]);
  947. return true;
  948. }
  949. return false;
  950. }
  951. /**
  952. * Determine if the browser is iPhone or not (last updated 1.7)
  953. * @return boolean True if the browser is iPhone otherwise false
  954. */
  955. protected function checkBrowseriPhone() {
  956. if( stripos($this->_agent,'iPhone') !== false ) {
  957. $this->setVersion(self::VERSION_UNKNOWN);
  958. $this->getSafariVersionOnIos();
  959. $this->getChromeVersionOnIos();
  960. $this->checkForFacebookIos();
  961. $this->setMobile(true);
  962. $this->setBrowser(self::BROWSER_IPHONE);
  963. return true;
  964. }
  965. return false;
  966. }
  967. /**
  968. * Determine if the browser is iPad or not (last updated 1.7)
  969. * @return boolean True if the browser is iPad otherwise false
  970. */
  971. protected function checkBrowseriPad() {
  972. if( stripos($this->_agent,'iPad') !== false ) {
  973. $this->setVersion(self::VERSION_UNKNOWN);
  974. $this->getSafariVersionOnIos();
  975. $this->getChromeVersionOnIos();
  976. $this->checkForFacebookIos();
  977. $this->setTablet(true);
  978. $this->setBrowser(self::BROWSER_IPAD);
  979. return true;
  980. }
  981. return false;
  982. }
  983. /**
  984. * Determine if the browser is iPod or not (last updated 1.7)
  985. * @return boolean True if the browser is iPod otherwise false
  986. */
  987. protected function checkBrowseriPod() {
  988. if( stripos($this->_agent,'iPod') !== false ) {
  989. $this->setVersion(self::VERSION_UNKNOWN);
  990. $this->getSafariVersionOnIos();
  991. $this->getChromeVersionOnIos();
  992. $this->checkForFacebookIos();
  993. $this->setMobile(true);
  994. $this->setBrowser(self::BROWSER_IPOD);
  995. return true;
  996. }
  997. return false;
  998. }
  999. /**
  1000. * Determine if the browser is Android or not (last updated 1.7)
  1001. * @return boolean True if the browser is Android otherwise false
  1002. */
  1003. protected function checkBrowserAndroid()
  1004. {
  1005. if (stripos($this->_agent, 'Android') !== false) {
  1006. $aresult = explode(' ', stristr($this->_agent, 'Android'));
  1007. if (isset($aresult[1])) {
  1008. $aversion = explode(' ', $aresult[1]);
  1009. $this->setVersion($aversion[0]);
  1010. } else {
  1011. $this->setVersion(self::VERSION_UNKNOWN);
  1012. }
  1013. $this->setMobile(true);
  1014. $this->setBrowser(self::BROWSER_ANDROID);
  1015. return true;
  1016. }
  1017. return false;
  1018. }
  1019. /**
  1020. * Determine the user's platform (last updated 1.7)
  1021. */
  1022. protected function checkPlatform()
  1023. {
  1024. if (stripos($this->_agent, 'windows') !== false)
  1025. {
  1026. $this->_platform = self::PLATFORM_WINDOWS;
  1027. }
  1028. else if (stripos($this->_agent, 'iPad') !== false)
  1029. {
  1030. $this->_platform = self::PLATFORM_IPAD;
  1031. }
  1032. else if (stripos($this->_agent, 'iPod') !== false)
  1033. {
  1034. $this->_platform = self::PLATFORM_IPOD;
  1035. }
  1036. else if (stripos($this->_agent, 'iPhone') !== false)
  1037. {
  1038. $this->_platform = self::PLATFORM_IPHONE;
  1039. }
  1040. elseif (stripos($this->_agent, 'mac') !== false)
  1041. {
  1042. $this->_platform = self::PLATFORM_APPLE;
  1043. }
  1044. elseif (stripos($this->_agent, 'android') !== false)
  1045. {
  1046. $this->_platform = self::PLATFORM_ANDROID;
  1047. }
  1048. elseif (stripos($this->_agent, 'linux') !== false)
  1049. {
  1050. $this->_platform = self::PLATFORM_LINUX;
  1051. }
  1052. else if (stripos($this->_agent, 'Nokia') !== false)
  1053. {
  1054. $this->_platform = self::PLATFORM_NOKIA;
  1055. }
  1056. else if (stripos($this->_agent, 'BlackBerry') !== false)
  1057. {
  1058. $this->_platform = self::PLATFORM_BLACKBERRY;
  1059. }
  1060. elseif (stripos($this->_agent, 'FreeBSD') !== false)
  1061. {
  1062. $this->_platform = self::PLATFORM_FREEBSD;
  1063. }
  1064. elseif (stripos($this->_agent, 'OpenBSD') !== false)
  1065. {
  1066. $this->_platform = self::PLATFORM_OPENBSD;
  1067. }
  1068. elseif (stripos($this->_agent, 'NetBSD') !== false)
  1069. {
  1070. $this->_platform = self::PLATFORM_NETBSD;
  1071. }
  1072. elseif (stripos($this->_agent, 'OpenSolaris') !== false)
  1073. {
  1074. $this->_platform = self::PLATFORM_OPENSOLARIS;
  1075. }
  1076. elseif (stripos($this->_agent, 'SunOS') !== false)
  1077. {
  1078. $this->_platform = self::PLATFORM_SUNOS;
  1079. }
  1080. elseif (stripos($this->_agent, 'OS\/2') !== false)
  1081. {
  1082. $this->_platform = self::PLATFORM_OS2;
  1083. }
  1084. elseif (stripos($this->_agent, 'BeOS') !== false)
  1085. {
  1086. $this->_platform = self::PLATFORM_BEOS;
  1087. }
  1088. elseif (stripos($this->_agent, 'win') !== false)
  1089. {
  1090. $this->_platform = self::PLATFORM_WINDOWS;
  1091. }
  1092. }
  1093. }
  1094. ?>