PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/extlib/browser.php

https://github.com/abdallahchamas/haiti_tracker
PHP | 375 lines | 230 code | 42 blank | 103 comment | 38 complexity | 2102d48b4c68b73fe2c3ada575bd1d8b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*****************************************************************
  3. * File name: browser.php
  4. * Author: Gary White & Damien Raude-Morvan
  5. * Last modified: December 10, 2006
  6. *
  7. **************************************************************
  8. * Copyright (C) 2003 Gary White
  9. * Copyright (C) 2006 Damien Raude-Morvan <drazzib@drazzib.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
  13. as published by the Free Software Foundation; either version 2
  14. of 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. **************************************************************
  24. *
  25. Browser class
  26. *
  27. Identifies the user's Operating system, browser and version
  28. by parsing the HTTP_USER_AGENT string sent to the server
  29. *
  30. Typical Usage:
  31. *
  32. require_once($_SERVER['DOCUMENT_ROOT'].'/include/browser.php');
  33. $br = new Browser;
  34. echo "$br->Platform, $br->Name version $br->Version";
  35. *
  36. For operating systems, it will correctly identify:
  37. Microsoft Windows
  38. MacIntosh
  39. Linux
  40. *
  41. Anything not determined to be one of the above is considered to by Unix
  42. because most Unix based browsers seem to not report the operating system.
  43. The only known problem here is that, if a HTTP_USER_AGENT string does not
  44. contain the operating system, it will be identified as Unix. For unknown
  45. browsers, this may not be correct.
  46. *
  47. For browsers, it should correctly identify all versions of:
  48. Amaya
  49. Galeon
  50. iCab
  51. Internet Explorer
  52. For AOL versions it will identify as Internet Explorer (AOL) and the version
  53. will be the AOL version instead of the IE version.
  54. Konqueror
  55. Lynx
  56. Mozilla
  57. Netscape Navigator/Communicator
  58. OmniWeb
  59. Opera
  60. Pocket Internet Explorer for handhelds
  61. Safari
  62. WebTV
  63. *****************************************************************/
  64. class browser {
  65. var $Name = "Unknown";
  66. var $Version = "Unknown";
  67. var $Platform = "Unknown";
  68. var $UserAgent = "Not reported";
  69. var $AOL = false;
  70. var $isMoz = false;
  71. var $isOpera = false;
  72. var $isSafari = false;
  73. var $isKonqueror = false;
  74. var $isIE = false;
  75. function browser() {
  76. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  77. $agent = $_SERVER['HTTP_USER_AGENT'];
  78. } else {
  79. $agent = null;
  80. }
  81. // initialize properties
  82. $bd['platform'] = "Unknown";
  83. $bd['browser'] = "Unknown";
  84. $bd['version'] = "Unknown";
  85. $this->UserAgent = $agent;
  86. // find operating system
  87. if (eregi("win", $agent))
  88. $bd['platform'] = "Windows";
  89. elseif (eregi("mac", $agent)) $bd['platform'] = "MacIntosh";
  90. elseif (eregi("linux", $agent)) $bd['platform'] = "Linux";
  91. elseif (eregi("OS/2", $agent)) $bd['platform'] = "OS/2";
  92. elseif (eregi("BeOS", $agent)) $bd['platform'] = "BeOS";
  93. // test for Opera
  94. if (eregi("opera", $agent)) {
  95. $val = stristr($agent, "opera");
  96. if (eregi("/", $val)) {
  97. $val = explode("/", $val);
  98. $bd['browser'] = $val[0];
  99. $val = explode(" ", $val[1]);
  100. $bd['version'] = $val[0];
  101. } else {
  102. $val = explode(" ", stristr($val, "opera"));
  103. $bd['browser'] = $val[0];
  104. $bd['version'] = $val[1];
  105. }
  106. $this->isOpera = true;
  107. // test for WebTV
  108. }
  109. elseif (eregi("webtv", $agent)) {
  110. $val = explode("/", stristr($agent, "webtv"));
  111. $bd['browser'] = $val[0];
  112. $bd['version'] = $val[1];
  113. // test for MS Internet Explorer version 1
  114. }
  115. elseif (eregi("microsoft internet explorer", $agent)) {
  116. $bd['browser'] = "MSIE";
  117. $bd['version'] = "1.0";
  118. $var = stristr($agent, "/");
  119. if (ereg("308|425|426|474|0b1", $var)) {
  120. $bd['version'] = "1.5";
  121. }
  122. $this -> isIE = true;
  123. // test for NetPositive
  124. }
  125. elseif (eregi("NetPositive", $agent)) {
  126. $val = explode("/", stristr($agent, "NetPositive"));
  127. $bd['platform'] = "BeOS";
  128. $bd['browser'] = $val[0];
  129. $bd['version'] = $val[1];
  130. // test for MS Internet Explorer
  131. }
  132. elseif (eregi("msie", $agent) && !eregi("opera", $agent)) {
  133. $val = explode(" ", stristr($agent, "msie"));
  134. $bd['browser'] = $val[0];
  135. $bd['version'] = $val[1];
  136. $this -> isIE = true;
  137. // test for MS Pocket Internet Explorer
  138. }
  139. elseif (eregi("mspie", $agent) || eregi('pocket', $agent)) {
  140. $val = explode(" ", stristr($agent, "mspie"));
  141. $bd['browser'] = "MSPIE";
  142. $bd['platform'] = "WindowsCE";
  143. if (eregi("mspie", $agent))
  144. $bd['version'] = $val[1];
  145. else {
  146. $val = explode("/", $agent);
  147. $bd['version'] = $val[1];
  148. }
  149. // test for Galeon
  150. }
  151. elseif (eregi("galeon", $agent)) {
  152. $val = explode(" ", stristr($agent, "galeon"));
  153. $val = explode("/", $val[0]);
  154. $bd['browser'] = $val[0];
  155. $bd['version'] = $val[1];
  156. $this->isMoz = true;
  157. // test for Konqueror
  158. }
  159. elseif (eregi("Konqueror", $agent)) {
  160. $val = explode(" ", stristr($agent, "Konqueror"));
  161. $val = explode("/", $val[0]);
  162. $bd['browser'] = $val[0];
  163. $bd['version'] = $val[1];
  164. $this->isKonqueror = true;
  165. // test for iCab
  166. }
  167. elseif (eregi("icab", $agent)) {
  168. $val = explode(" ", stristr($agent, "icab"));
  169. $bd['browser'] = $val[0];
  170. $bd['version'] = $val[1];
  171. // test for OmniWeb
  172. }
  173. elseif (eregi("omniweb", $agent)) {
  174. $val = explode("/", stristr($agent, "omniweb"));
  175. $bd['browser'] = $val[0];
  176. $bd['version'] = $val[1];
  177. // test for Phoenix
  178. }
  179. elseif (eregi("Phoenix", $agent)) {
  180. $bd['browser'] = "Phoenix";
  181. $val = explode("/", stristr($agent, "Phoenix/"));
  182. $bd['version'] = $val[1];
  183. $this->isMoz = true;
  184. // test for Firebird
  185. }
  186. elseif (eregi("firebird", $agent)) {
  187. $bd['browser'] = "Firebird";
  188. $val = stristr($agent, "Firebird");
  189. $val = explode("/", $val);
  190. $bd['version'] = $val[1];
  191. $this->isMoz = true;
  192. // test for Firefox
  193. }
  194. elseif (eregi("Firefox", $agent)) {
  195. $bd['browser'] = "Firefox";
  196. $val = stristr($agent, "Firefox");
  197. $val = explode("/", $val);
  198. $bd['version'] = $val[1];
  199. $this->isMoz = true;
  200. // test for Mozilla Alpha/Beta Versions
  201. }
  202. elseif (eregi("mozilla", $agent) && eregi("rv:[0-9].[0-9][a-b]", $agent) && !eregi("netscape", $agent)) {
  203. $bd['browser'] = "Mozilla";
  204. $val = explode(" ", stristr($agent, "rv:"));
  205. eregi("rv:[0-9].[0-9][a-b]", $agent, $val);
  206. $bd['version'] = str_replace("rv:", "", $val[0]);
  207. $this->isMoz = true;
  208. // test for Mozilla Stable Versions
  209. }
  210. elseif (eregi("mozilla", $agent) && eregi("rv:[0-9]\.[0-9]", $agent) && !eregi("netscape", $agent)) {
  211. $bd['browser'] = "Mozilla";
  212. $val = explode(" ", stristr($agent, "rv:"));
  213. eregi("rv:[0-9]\.[0-9]\.[0-9]", $agent, $val);
  214. $bd['version'] = str_replace("rv:", "", $val[0]);
  215. $this->isMoz = true;
  216. // test for Lynx & Amaya
  217. }
  218. elseif (eregi("libwww", $agent)) {
  219. if (eregi("amaya", $agent)) {
  220. $val = explode("/", stristr($agent, "amaya"));
  221. $bd['browser'] = "Amaya";
  222. $val = explode(" ", $val[1]);
  223. $bd['version'] = $val[0];
  224. } else {
  225. $val = explode("/", $agent);
  226. $bd['browser'] = "Lynx";
  227. $bd['version'] = $val[1];
  228. }
  229. // test for Safari
  230. }
  231. elseif (eregi("AppleWebKit", $agent)) {
  232. $bd['browser'] = "Safari";
  233. $val = explode("/", $agent);
  234. $bd['version'] = $val[3];
  235. $this -> isSafari = true;
  236. // remaining two tests are for Netscape
  237. }
  238. elseif (eregi("netscape", $agent)) {
  239. $val = explode(" ", stristr($agent, "netscape"));
  240. $val = explode("/", $val[0]);
  241. $bd['browser'] = $val[0];
  242. $bd['version'] = $val[1];
  243. if ($bd['version'] > 6) {
  244. $this->isMoz = true;
  245. }
  246. }
  247. elseif (eregi("mozilla", $agent) && !eregi("rv:[0-9]\.[0-9]\.[0-9]", $agent)) {
  248. $val = explode(" ", stristr($agent, "mozilla"));
  249. $val = explode("/", $val[0]);
  250. $bd['browser'] = "Netscape";
  251. $bd['version'] = $val[1];
  252. if ($bd['version'] > 6) {
  253. $this->isMoz = true;
  254. }
  255. }
  256. // clean up extraneous garbage that may be in the name
  257. $bd['browser'] = ereg_replace("[^a-z,A-Z]", "", $bd['browser']);
  258. // clean up extraneous garbage that may be in the version
  259. $bd['version'] = ereg_replace("[^0-9,.,a-z,A-Z]", "", $bd['version']);
  260. // check for AOL
  261. if (eregi("AOL", $agent)) {
  262. $var = stristr($agent, "AOL");
  263. $var = explode(" ", $var);
  264. $bd['aol'] = ereg_replace("[^0-9,.,a-z,A-Z]", "", $var[1]);
  265. } else {
  266. $bd['aol'] = false;
  267. }
  268. // finally assign our properties
  269. $this->Name = $bd['browser'];
  270. $this->Version = $bd['version'];
  271. $this->Platform = $bd['platform'];
  272. $this->AOL = $bd['aol'];
  273. }
  274. function isGecko() {
  275. return $this->isMoz;
  276. }
  277. function isOpera() {
  278. return $this->isOpera;
  279. }
  280. /** Support of multipart/x-mixed-replace complete and stable :
  281. * o in Safari 2.0.2 (http://webkit.org/blog/?p=32)
  282. * (http://developer.apple.com/internet/safari/uamatrix.html)
  283. * o in Konqueror at least 3.4.3
  284. * (http://websvn.kde.org/trunk/KDE/kdelibs/khtml/kmultipart/kmultipart.cpp/)
  285. */
  286. function supportsServerPush() {
  287. return ($this->isMoz
  288. || $this->isOpera
  289. || ($this->isSafari && $this->compareBrowserVersion("416", $this->Version))
  290. || ($this->isKonqueror && $this->compareBrowserVersion("3.4.3", $this->Version))
  291. );
  292. }
  293. /** Support of XMLHTTPRequest complete and stable :
  294. * o in Konqueror at least 3.4 (since 2004 -
  295. * http://websvn.kde.org/trunk/KDE/kdelibs/khtml/ecma/xmlhttprequest.cpp/)
  296. */
  297. function supportsAJAX() {
  298. return ($this->isMoz
  299. || $this->isOpera
  300. || $this->isSafari
  301. || ($this->isIE && $this->compareBrowserVersion("5", $this->Version))
  302. || ($this->isKonqueror && $this->compareBrowserVersion("3.4", $this->Version))
  303. );
  304. }
  305. /**
  306. * Compare two version strings of browser.
  307. * @param $requiredVersion minimal version number required for feature
  308. * @param $browserVersion current detected browser version
  309. * @return true if $browserVersion is greater than $requiredVersion
  310. */
  311. function compareBrowserVersion($requiredVersion, $browserVersion) {
  312. // Standardise versions
  313. $requiredVersion = preg_replace('/([^0-9\.]+)/', '.$1.', $requiredVersion);
  314. $requiredVersion = trim($requiredVersion);
  315. $v1 = explode('.', $requiredVersion);
  316. $browserVersion = preg_replace('/([^0-9\.]+)/', '.$1.', $browserVersion);
  317. $browserVersion = trim($browserVersion);
  318. $v2 = explode('.', $browserVersion);
  319. $compare = 0;
  320. for ($i = 0, $x = min(count($v1), count($v2)); $i < $x; $i++) {
  321. if ($v1[$i] == $v2[$i]) {
  322. continue;
  323. }
  324. $i1 = $v1[$i];
  325. $i2 = $v2[$i];
  326. if (is_numeric($i1) && is_numeric($i2)) {
  327. $compare = ($i1 < $i2) ? -1 : 1;
  328. }
  329. }
  330. return (bool) ($compare <= 0);
  331. }
  332. }
  333. ?>