Class-BrowserDetect.php 14 KB

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