Class-BrowserDetect.php 14 KB

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