Class-BrowserDetect.php 14 KB

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