Class-BrowserDetect.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * This class is an experiment for the job of correctly detecting browsers and settings
  4. * needed for them.
  5. * - Detects the following browsers
  6. * - Opera, Webkit, Firefox, Web_tv, Konqueror, IE, Gecko
  7. * - Webkit variants: Chrome, iphone, blackberry, android, safari, ipad, ipod
  8. * - Opera Versions: 6, 7, 8 ... 10 ... and mobile mini and mobi
  9. * - Firefox Versions: 1, 2, 3 .... 11 ...
  10. * - Chrome Versions: 1 ... 18 ...
  11. * - IE Versions: 4, 5, 5.5, 6, 7, 8, 9, 10 ... mobile and Mac
  12. * - Nokia
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. class browser_detector
  17. {
  18. /**
  19. * Holds all browsers information. Its contents will be placed into $context['browser'].
  20. *
  21. * @var array
  22. */
  23. private $_browsers = null;
  24. private $_is_mobile = null;
  25. /**
  26. * The main method of this class, you know the one that does the job: detect the thing.
  27. * - determines the user agent (browser) as best it can.
  28. */
  29. function detectBrowser()
  30. {
  31. global $context, $user_info;
  32. // Init
  33. $this->_browsers = array();
  34. $this->_is_mobile = false;
  35. // Initialize some values we'll set differently if necessary...
  36. $this->_browsers['needs_size_fix'] = false;
  37. // One at a time, one at a time, and in this order too
  38. if ($this->isOpera())
  39. $this->setupOpera();
  40. // Them webkits need to be set up too
  41. elseif ($this->isWebkit())
  42. $this->setupWebkit();
  43. // We may have work to do on Firefox...
  44. elseif ($this->isFirefox())
  45. $this->setupFirefox();
  46. // Old friend, old frenemy
  47. elseif ($this->isIe())
  48. $this->setupIe();
  49. // Just a few mobile checks
  50. $this->isOperaMini();
  51. $this->isOperaMobi();
  52. // Be you robot or human?
  53. if ($user_info['possibly_robot'])
  54. {
  55. // This isn't meant to be reliable, it's just meant to catch most bots to prevent PHPSESSID from showing up.
  56. $this->_browsers['possibly_robot'] = !empty($user_info['possibly_robot']);
  57. // Robots shouldn't be logging in or registering. So, they aren't a bot. Better to be wrong than sorry (or people won't be able to log in!), anyway.
  58. if ((isset($_REQUEST['action']) && in_array($_REQUEST['action'], array('login', 'login2', 'register'))) || !$user_info['is_guest'])
  59. $this->_browsers['possibly_robot'] = false;
  60. }
  61. else
  62. $this->_browsers['possibly_robot'] = false;
  63. // Fill out the historical array as needed to support old mods that don't use isBrowser
  64. $this->fillInformation();
  65. // Last step ...
  66. $this->setupBrowserPriority();
  67. // Now see what you've done!
  68. $context['browser'] = $this->_browsers;
  69. }
  70. /**
  71. * Determine if the browser is Opera or not
  72. * @return boolean true if the browser is Opera otherwise false
  73. */
  74. function isOpera()
  75. {
  76. if (!isset($this->_browsers['is_opera']))
  77. $this->_browsers['is_opera'] = strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false;
  78. return $this->_browsers['is_opera'];
  79. }
  80. /**
  81. * Determine if the browser is IE or not
  82. * @return boolean true if the browser is IE otherwise false
  83. */
  84. function isIe()
  85. {
  86. // I'm IE, Yes I'm the real IE; All you other IEs are just imitating.
  87. if (!isset($this->_browsers['is_ie']))
  88. $this->_browsers['is_ie'] = !$this->isOpera() && !$this->isGecko() && !$this->isWebTv() && preg_match('~MSIE \d+~', $_SERVER['HTTP_USER_AGENT']) === 1;
  89. return $this->_browsers['is_ie'];
  90. }
  91. /**
  92. * Determine if the browser is a Webkit based one or not
  93. * @return boolean true if the browser is Webkit based otherwise false
  94. */
  95. function isWebkit()
  96. {
  97. if (!isset($this->_browsers['is_webkit']))
  98. $this->_browsers['is_webkit'] = strpos($_SERVER['HTTP_USER_AGENT'], 'AppleWebKit') !== false;
  99. return $this->_browsers['is_webkit'];
  100. }
  101. /**
  102. * Determine if the browser is Firefox or one of its variants
  103. * @return boolean true if the browser is Firefox otherwise false
  104. */
  105. function isFirefox()
  106. {
  107. if (!isset($this->_browsers['is_firefox']))
  108. $this->_browsers['is_firefox'] = preg_match('~(?:Firefox|Ice[wW]easel|IceCat|Shiretoko|Minefield)/~', $_SERVER['HTTP_USER_AGENT']) === 1 && $this->isGecko();
  109. return $this->_browsers['is_firefox'];
  110. }
  111. /**
  112. * Determine if the browser is WebTv or not
  113. * @return boolean true if the browser is WebTv otherwise false
  114. */
  115. function isWebTv()
  116. {
  117. if (!isset($this->_browsers['is_web_tv']))
  118. $this->_browsers['is_web_tv'] = strpos($_SERVER['HTTP_USER_AGENT'], 'WebTV') !== false;
  119. return $this->_browsers['is_web_tv'];
  120. }
  121. /**
  122. * Determine if the browser is konqueror or not
  123. * @return boolean true if the browser is konqueror otherwise false
  124. */
  125. function isKonqueror()
  126. {
  127. if (!isset($this->_browsers['is_konqueror']))
  128. $this->_browsers['is_konqueror'] = strpos($_SERVER['HTTP_USER_AGENT'], 'Konqueror') !== false;
  129. return $this->_browsers['is_konqueror'];
  130. }
  131. /**
  132. * Determine if the browser is Gecko or not
  133. * @return boolean true if the browser is Gecko otherwise false
  134. */
  135. function isGecko()
  136. {
  137. if (!isset($this->_browsers['is_gecko']))
  138. $this->_browsers['is_gecko'] = strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false && !$this->isWebkit() && !$this->isKonqueror();
  139. return $this->_browsers['is_gecko'];
  140. }
  141. /**
  142. * Determine if the browser is OperaMini or not
  143. * @return boolean true if the browser is OperaMini otherwise false
  144. */
  145. function isOperaMini()
  146. {
  147. if (!isset($this->_browsers['is_opera_mini']))
  148. $this->_browsers['is_opera_mini'] = (isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) || stripos($_SERVER['HTTP_USER_AGENT'], 'opera mini') !== false);
  149. if ($this->_browsers['is_opera_mini'])
  150. $this->_is_mobile = true;
  151. return $this->_browsers['is_opera_mini'];
  152. }
  153. /**
  154. * Determine if the browser is OperaMobi or not
  155. * @return boolean true if the browser is OperaMobi otherwise false
  156. */
  157. function isOperaMobi()
  158. {
  159. if (!isset($this->_browsers['is_opera_mobi']))
  160. $this->_browsers['is_opera_mobi'] = stripos($_SERVER['HTTP_USER_AGENT'], 'opera mobi') !== false;
  161. if ($this->_browsers['is_opera_mobi'])
  162. $this->_is_mobile = true;
  163. return $this->_browsers['is_opera_mini'];
  164. }
  165. /**
  166. * Detect Safari / Chrome / iP[ao]d / iPhone / Android / Blackberry from webkit.
  167. * - set the browser version for Safari and Chrome
  168. * - set the mobile flag for mobile based useragents
  169. */
  170. private function setupWebkit()
  171. {
  172. $this->_browsers += array(
  173. 'is_chrome' => strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false,
  174. 'is_iphone' => (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') !== false) && strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') === false,
  175. 'is_blackberry' => stripos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'PlayBook') !== false,
  176. 'is_android' => strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false,
  177. 'is_nokia' => strpos($_SERVER['HTTP_USER_AGENT'], 'SymbianOS') !== false,
  178. );
  179. // blackberry, playbook, iphone, nokia, android and ipods set a mobile flag
  180. if ($this->_browsers['is_iphone'] || $this->_browsers['is_blackberry'] || $this->_browsers['is_android'] || $this->_browsers['is_nokia'])
  181. $this->_is_mobile = true;
  182. // @todo what to do with the blaPad? ... for now leave it detected as Safari ...
  183. $this->_browsers['is_safari'] = strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false && !$this->_browsers['is_chrome'] && !$this->_browsers['is_iphone'];
  184. $this->_browsers['is_ipad'] = strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false;
  185. // if Chrome, get the major version
  186. if ($this->_browsers['is_chrome'])
  187. {
  188. if (preg_match('~chrome[/]([0-9][0-9]?[.])~i', $_SERVER['HTTP_USER_AGENT'], $match) === 1)
  189. $this->_browsers['is_chrome' . (int) $match[1]] = true;
  190. }
  191. // or if Safari get its major version
  192. if ($this->_browsers['is_safari'])
  193. {
  194. if (preg_match('~version/?(.*)safari.*~i', $_SERVER['HTTP_USER_AGENT'], $match) === 1)
  195. $this->_browsers['is_safari' . (int) trim($match[1])] = true;
  196. }
  197. }
  198. /**
  199. * Additional IE checks and settings.
  200. * - determines the version of the IE browser in use
  201. * - detects ie4 onward
  202. * - attempts to distinguish between IE and IE in compatabilty view
  203. * - checks for old IE on macs as well, since we can
  204. */
  205. private function setupIe()
  206. {
  207. $this->_browsers['is_ie_compat_view'] = false;
  208. // get the version of the browser from the msie tag
  209. if (preg_match('~MSIE\s?([0-9][0-9]?.[0-9])~i', $_SERVER['HTTP_USER_AGENT'], $msie_match) === 1)
  210. {
  211. $msie_match[1] = trim($msie_match[1]);
  212. $msie_match[1] = (($msie_match[1] - (int) $msie_match[1]) == 0) ? (int) $msie_match[1] : $msie_match[1];
  213. $this->_browsers['is_ie' . $msie_match[1]] = true;
  214. }
  215. // "modern" ie uses trident 4=ie8, 5=ie9, 6=ie10, even in compatability view
  216. if (preg_match('~Trident/([0-9.])~i', $_SERVER['HTTP_USER_AGENT'], $trident_match) === 1)
  217. {
  218. $this->_browsers['is_ie' . ((int) $trident_match[1] + 4)] = true;
  219. // If trident is set, see the (if any) msie tag in the user agent matches ... if not its in some compatablity view
  220. if (isset($msie_match[1]) && ($msie_match[1] < $trident_match[1] + 4))
  221. $this->_browsers['is_ie_compat_view'] = true;
  222. }
  223. // Detect true IE6 and IE7 and not IE in compat mode.
  224. $this->_browsers['is_ie7'] = !empty($this->_browsers['is_ie7']) && ($this->_browsers['is_ie_compat_view'] === false);
  225. $this->_browsers['is_ie6'] = !empty($this->_browsers['is_ie6']) && ($this->_browsers['is_ie_compat_view'] === false);
  226. // IE mobile 7 or 9, ... shucks why not
  227. if ((!empty($this->_browsers['is_ie7']) && strpos($_SERVER['HTTP_USER_AGENT'], 'IEMobile/7') !== false) || (!empty($this->_browsers['is_ie9']) && strpos($_SERVER['HTTP_USER_AGENT'], 'IEMobile/9') !== false))
  228. {
  229. $this->_browsers['is_ie_mobi'] = true;
  230. $this->_is_mobile = true;
  231. }
  232. // And some throwbacks to a bygone era, deposited here like cholesterol in your arteries
  233. $this->_browsers += array(
  234. 'is_ie4' => !empty($this->_browsers['is_ie4']) && !$this->_browsers['is_web_tv'],
  235. 'is_mac_ie' => strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5.') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false
  236. );
  237. // Before IE8 we need to fix IE... lots!
  238. $this->_browsers['ie_standards_fix'] = (($this->_browsers['is_ie6'] === true) || ($this->_browsers['is_ie7'] === true)) ? true : false;
  239. // We may even need a size fix...
  240. $this->_browsers['needs_size_fix'] = (!empty($this->_browsers['is_ie5']) || !empty($this->_browsers['is_ie5.5']) || !empty($this->_browsers['is_ie4'])) && !$this->_browsers['is_mac_ie'];
  241. }
  242. /**
  243. * Additional firefox checks.
  244. * - Gets the version of the FF browser in use
  245. * - Considers all FF variants as FF including IceWeasel, IceCat, Shiretoko and Minefiled
  246. */
  247. private function setupFirefox()
  248. {
  249. if (preg_match('~(?:Firefox|Ice[wW]easel|IceCat|Shiretoko|Minefield)[\/ \(]([^ ;\)]+)~', $_SERVER['HTTP_USER_AGENT'], $match) === 1)
  250. $this->_browsers['is_firefox' . (int) $match[1]] = true;
  251. }
  252. /**
  253. * More Opera checks if we are opera.
  254. * - checks for the version of Opera in use
  255. * - uses checks for 10 first and falls through to <9
  256. */
  257. private function setupOpera()
  258. {
  259. // Opera 10+ uses the version tag at the end of the string
  260. if (preg_match('~\sVersion/([0-9]+)\.[0-9]+(?:\s*|$)~', $_SERVER['HTTP_USER_AGENT'], $match))
  261. $this->_browsers['is_opera' . (int) $match[1]] = true;
  262. // Opera pre 10 is supposed to uses the Opera tag alone, as do some spoofers
  263. elseif (preg_match('~Opera[ /]([0-9]+)(?!\\.[89])~', $_SERVER['HTTP_USER_AGENT'], $match))
  264. $this->_browsers['is_opera' . (int) $match[1]] = true;
  265. // Needs size fix?
  266. $this->_browsers['needs_size_fix'] = !empty($this->_browsers['is_opera6']);
  267. }
  268. /**
  269. * Get the browser name that we will use in the <body id="this_browser">
  270. * - The order of each browser in $browser_priority is important
  271. * - if you want to have id='ie6' and not id='ie' then it must appear first in the list of ie browsers
  272. * - only sets browsers that may need some help via css for compatablity
  273. */
  274. private function setupBrowserPriority()
  275. {
  276. global $context;
  277. if ($this->_is_mobile)
  278. $context['browser_body_id'] = 'mobile';
  279. else
  280. {
  281. // add in any specific detection conversions here if you want a special body id e.g. 'is_opera9' => 'opera9'
  282. $browser_priority = array(
  283. 'is_ie6' => 'ie6',
  284. 'is_ie7' => 'ie7',
  285. 'is_ie8' => 'ie8',
  286. 'is_ie9' => 'ie9',
  287. 'is_ie10' => 'ie10',
  288. 'is_ie' => 'ie',
  289. 'is_firefox' => 'firefox',
  290. 'is_chrome' => 'chrome',
  291. 'is_safari' => 'safari',
  292. 'is_opera10' => 'opera10',
  293. 'is_opera11' => 'opera11',
  294. 'is_opera12' => 'opera12',
  295. 'is_opera' => 'opera',
  296. 'is_konqueror' => 'konqueror',
  297. );
  298. $context['browser_body_id'] = 'smf';
  299. $active = array_reverse(array_keys($this->_browsers, true));
  300. foreach ($active as $key => $browser)
  301. {
  302. if (array_key_exists($browser, $browser_priority))
  303. {
  304. $context['browser_body_id'] = $browser_priority[$browser];
  305. break;
  306. }
  307. }
  308. }
  309. }
  310. /**
  311. * Fill out the historical array
  312. * - needed to support old mods that don't use isBrowser
  313. */
  314. function fillInformation()
  315. {
  316. $this->_browsers += array(
  317. 'is_webkit' => false,
  318. 'is_opera6' => false,
  319. 'is_opera7' => false,
  320. 'is_opera8' => false,
  321. 'is_opera9' => false,
  322. 'is_opera10' => false,
  323. 'is_ie4' => false,
  324. 'is_mac_ie' => false,
  325. 'is_firefox1' => false,
  326. 'is_firefox2' => false,
  327. 'is_firefox3' => false,
  328. 'is_iphone' => false,
  329. 'is_android' => false,
  330. 'is_chrome' => false,
  331. 'is_safari' => false,
  332. 'is_ie8' => false,
  333. 'is_ie7' => false,
  334. 'is_ie6' => false,
  335. 'is_ie5.5' => false,
  336. 'is_ie5' => false,
  337. 'ie_standards_fix' => false,
  338. );
  339. }
  340. }