Class-BrowserDetect.php 14 KB

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