MDL-50891 useragent: Move web crawler checks to useragent class
[moodle.git] / lib / classes / useragent.php
blobfcc3c6d3601498b44e8a03e36e9d216fba6a5c3c
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Environment class to aid with the detection and establishment of the working environment.
20 * @package core
21 * @copyright 2013 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 /**
26 * The user agent class.
28 * It's important to note that we do not like browser sniffing and its use in core code is highly discouraged.
29 * No new uses of this API will be integrated unless there is absolutely no alternative.
31 * This API supports the few browser checks we do have in core, all of which one day will hopefully be removed.
32 * The API will remain to support any third party use out there, however at some point like all code it will be deprecated.
34 * Use sparingly and only with good cause!
36 * @package core
37 * @copyright 2013 Sam Hemelryk
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class core_useragent {
42 /**
43 * The default for devices, think of as a computer.
45 const DEVICETYPE_DEFAULT = 'default';
46 /**
47 * Legacy devices, or at least legacy browsers. These are older devices/browsers
48 * that don't support standards.
50 const DEVICETYPE_LEGACY = 'legacy';
51 /**
52 * Mobile devices like your cell phone or hand held gaming device.
54 const DEVICETYPE_MOBILE = 'mobile';
55 /**
56 * Tables, larger than hand held, but still easily portable and smaller than a laptop.
58 const DEVICETYPE_TABLET = 'tablet';
60 /**
61 * An instance of this class.
62 * @var core_useragent
64 protected static $instance = null;
66 /**
67 * The device types we track.
68 * @var array
70 public static $devicetypes = array(
71 self::DEVICETYPE_DEFAULT,
72 self::DEVICETYPE_LEGACY,
73 self::DEVICETYPE_MOBILE,
74 self::DEVICETYPE_TABLET,
77 /**
78 * The current requests user agent string if there was one.
79 * @var string|bool|null Null until initialised, false if none available, or string when available.
81 protected $useragent = null;
83 /**
84 * The users device type, one of self::DEVICETYPE_*.
85 * @var string null until initialised
87 protected $devicetype = null;
89 /**
90 * Custom device types entered into the admin interface.
91 * @var array
93 protected $devicetypecustoms = array();
95 /**
96 * True if the user agent supports the display of svg images. False if not.
97 * @var bool|null Null until initialised, then true or false.
99 protected $supportssvg = null;
102 * Get an instance of the user agent object.
104 * @param bool $reload If set to true the user agent will be reset and all ascertations remade.
105 * @param string $forceuseragent The string to force as the user agent, don't use unless absolutely unavoidable.
106 * @return core_useragent
108 public static function instance($reload = false, $forceuseragent = null) {
109 if (!self::$instance || $reload) {
110 self::$instance = new core_useragent($forceuseragent);
112 return self::$instance;
116 * Constructs a new user agent object. Publically you must use the instance method above.
118 * @param string|null $forceuseragent Optional a user agent to force.
120 protected function __construct($forceuseragent = null) {
121 global $CFG;
122 if (!empty($CFG->devicedetectregex)) {
123 $this->devicetypecustoms = json_decode($CFG->devicedetectregex, true);
125 if ($this->devicetypecustoms === null) {
126 // This shouldn't happen unless you're hardcoding the config value.
127 debugging('Config devicedetectregex is not valid JSON object');
128 $this->devicetypecustoms = array();
130 if ($forceuseragent !== null) {
131 $this->useragent = $forceuseragent;
132 } else if (!empty($_SERVER['HTTP_USER_AGENT'])) {
133 $this->useragent = $_SERVER['HTTP_USER_AGENT'];
134 } else {
135 $this->useragent = false;
136 $this->devicetype = self::DEVICETYPE_DEFAULT;
141 * Returns the user agent string.
142 * @return bool|string The user agent string or false if one isn't available.
144 public static function get_user_agent_string() {
145 $instance = self::instance();
146 return $instance->useragent;
150 * Returns the device type we believe is being used.
151 * @return string
153 public static function get_device_type() {
154 $instance = self::instance();
155 if ($instance->devicetype === null) {
156 return $instance->guess_device_type();
158 return $instance->devicetype;
162 * Guesses the device type the user agent is running on.
164 * @return string
166 protected function guess_device_type() {
167 global $CFG;
168 if (empty($CFG->enabledevicedetection)) {
169 $this->devicetype = self::DEVICETYPE_DEFAULT;
170 return $this->devicetype;
172 foreach ($this->devicetypecustoms as $value => $regex) {
173 if (preg_match($regex, $this->useragent)) {
174 $this->devicetype = $value;
175 return $this->devicetype;
178 if ($this->is_useragent_mobile()) {
179 $this->devicetype = self::DEVICETYPE_MOBILE;
180 } else if ($this->is_useragent_tablet()) {
181 $this->devicetype = self::DEVICETYPE_TABLET;
182 } else if (self::check_ie_version('0') && !self::check_ie_version('7.0')) {
183 // IE 6 and before are considered legacy.
184 $this->devicetype = self::DEVICETYPE_LEGACY;
185 } else {
186 $this->devicetype = self::DEVICETYPE_DEFAULT;
188 return $this->devicetype;
192 * Returns true if the user appears to be on a mobile device.
193 * @return bool
195 protected function is_useragent_mobile() {
196 // Mobile detection PHP direct copy from open source detectmobilebrowser.com.
197 $phonesregex = '/android .+ mobile|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i';
198 $modelsregex = '/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i';
199 return (preg_match($phonesregex, $this->useragent) || preg_match($modelsregex, substr($this->useragent, 0, 4)));
203 * Returns true if the user appears to be on a tablet.
205 * @return int
207 protected function is_useragent_tablet() {
208 $tabletregex = '/Tablet browser|android|iPad|iProd|GT-P1000|GT-I9000|SHW-M180S|SGH-T849|SCH-I800|Build\/ERE27|sholest/i';
209 return (preg_match($tabletregex, $this->useragent));
213 * Whether the user agent relates to a web crawler.
214 * This includes all types of web crawler.
215 * @return bool
217 protected function is_useragent_web_crawler() {
218 $regex = '/Googlebot|google\.com|Yahoo! Slurp|\[ZSEBOT\]|msnbot|bingbot|BingPreview|Yandex|AltaVista|Baiduspider|Teoma/';
219 return (preg_match($regex, $this->useragent));
223 * Gets a list of known device types.
225 * @param bool $includecustomtypes If set to true we'll include types that have been added by the admin.
226 * @return array
228 public static function get_device_type_list($includecustomtypes = true) {
229 $types = self::$devicetypes;
230 if ($includecustomtypes) {
231 $instance = self::instance();
232 $types = array_merge($types, array_keys($instance->devicetypecustoms));
234 return $types;
238 * Returns the theme to use for the given device type.
240 * This used to be get_selected_theme_for_device_type.
241 * @param null|string $devicetype The device type to find out for. Defaults to the device the user is using,
242 * @return bool
244 public static function get_device_type_theme($devicetype = null) {
245 global $CFG;
246 if ($devicetype === null) {
247 $devicetype = self::get_device_type();
249 $themevarname = self::get_device_type_cfg_var_name($devicetype);
250 if (empty($CFG->$themevarname)) {
251 return false;
253 return $CFG->$themevarname;
257 * Returns the CFG var used to find the theme to use for the given device.
259 * Used to be get_device_cfg_var_name.
261 * @param null|string $devicetype The device type to find out for. Defaults to the device the user is using,
262 * @return string
264 public static function get_device_type_cfg_var_name($devicetype = null) {
265 if ($devicetype == self::DEVICETYPE_DEFAULT || empty($devicetype)) {
266 return 'theme';
268 return 'theme' . $devicetype;
272 * Gets the device type the user is currently using.
273 * @return string
275 public static function get_user_device_type() {
276 $device = self::get_device_type();
277 $switched = get_user_preferences('switchdevice'.$device, false);
278 if ($switched != false) {
279 return $switched;
281 return $device;
285 * Switches the device type we think the user is using to what ever was given.
286 * @param string $newdevice
287 * @return bool
288 * @throws coding_exception
290 public static function set_user_device_type($newdevice) {
291 $devicetype = self::get_device_type();
292 if ($newdevice == $devicetype) {
293 unset_user_preference('switchdevice'.$devicetype);
294 return true;
295 } else {
296 $devicetypes = self::get_device_type_list();
297 if (in_array($newdevice, $devicetypes)) {
298 set_user_preference('switchdevice'.$devicetype, $newdevice);
299 return true;
302 throw new coding_exception('Invalid device type provided to set_user_device_type');
306 * Returns true if the user agent matches the given brand and the version is equal to or greater than that specified.
308 * @param string $brand The branch to check for.
309 * @param scalar $version The version if we need to find out if it is equal to or greater than that specified.
310 * @return bool
312 public static function check_browser_version($brand, $version = null) {
313 switch ($brand) {
315 case 'MSIE':
316 // Internet Explorer.
317 return self::check_ie_version($version);
319 case 'Firefox':
320 // Mozilla Firefox browsers.
321 return self::check_firefox_version($version);
323 case 'Chrome':
324 return self::check_chrome_version($version);
326 case 'Opera':
327 // Opera.
328 return self::check_opera_version($version);
330 case 'Safari':
331 // Desktop version of Apple Safari browser - no mobile or touch devices.
332 return self::check_safari_version($version);
334 case 'Safari iOS':
335 // Safari on iPhone, iPad and iPod touch.
336 return self::check_safari_ios_version($version);
338 case 'WebKit':
339 // WebKit based browser - everything derived from it (Safari, Chrome, iOS, Android and other mobiles).
340 return self::check_webkit_version($version);
342 case 'Gecko':
343 // Gecko based browsers.
344 return self::check_gecko_version($version);
346 case 'WebKit Android':
347 // WebKit browser on Android.
348 return self::check_webkit_android_version($version);
350 case 'Camino':
351 // OSX browser using Gecke engine.
352 return self::check_camino_version($version);
354 // Who knows?! doesn't pass anyway.
355 return false;
359 * Checks the user agent is camino based and that the version is equal to or greater than that specified.
361 * Camino browser is at the end of its life, its no longer being developed or supported, just don't worry about it.
363 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
364 * @return bool
366 protected static function check_camino_version($version = null) {
367 // OSX browser using Gecko engine.
368 $useragent = self::get_user_agent_string();
369 if ($useragent === false) {
370 return false;
372 if (strpos($useragent, 'Camino') === false) {
373 return false;
375 if (empty($version)) {
376 return true; // No version specified.
378 if (preg_match("/Camino\/([0-9\.]+)/i", $useragent, $match)) {
379 if (version_compare($match[1], $version) >= 0) {
380 return true;
383 return false;
387 * Checks the user agent is Firefox (of any version).
389 * @return bool true if firefox
391 public static function is_firefox() {
392 return self::check_firefox_version();
396 * Checks the user agent is Firefox based and that the version is equal to or greater than that specified.
398 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
399 * @return bool
401 public static function check_firefox_version($version = null) {
402 // Mozilla Firefox browsers.
403 $useragent = self::get_user_agent_string();
404 if ($useragent === false) {
405 return false;
407 if (strpos($useragent, 'Firefox') === false && strpos($useragent, 'Iceweasel') === false) {
408 return false;
410 if (empty($version)) {
411 return true; // No version specified..
413 if (preg_match("/(Iceweasel|Firefox)\/([0-9\.]+)/i", $useragent, $match)) {
414 if (version_compare($match[2], $version) >= 0) {
415 return true;
418 return false;
422 * Checks the user agent is Gecko based (of any version).
424 * @return bool true if Gecko based.
426 public static function is_gecko() {
427 return self::check_gecko_version();
431 * Checks the user agent is Gecko based and that the version is equal to or greater than that specified.
433 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
434 * @return bool
436 public static function check_gecko_version($version = null) {
437 // Gecko based browsers.
438 // Do not look for dates any more, we expect real Firefox version here.
439 $useragent = self::get_user_agent_string();
440 if ($useragent === false) {
441 return false;
443 if (empty($version)) {
444 $version = 1;
445 } else if ($version > 20000000) {
446 // This is just a guess, it is not supposed to be 100% accurate!
447 if (preg_match('/^201/', $version)) {
448 $version = 3.6;
449 } else if (preg_match('/^200[7-9]/', $version)) {
450 $version = 3;
451 } else if (preg_match('/^2006/', $version)) {
452 $version = 2;
453 } else {
454 $version = 1.5;
457 if (preg_match("/(Iceweasel|Firefox)\/([0-9\.]+)/i", $useragent, $match)) {
458 // Use real Firefox version if specified in user agent string.
459 if (version_compare($match[2], $version) >= 0) {
460 return true;
462 } else if (preg_match("/Gecko\/([0-9\.]+)/i", $useragent, $match)) {
463 // Gecko might contain date or Firefox revision, let's just guess the Firefox version from the date.
464 $browserver = $match[1];
465 if ($browserver > 20000000) {
466 // This is just a guess, it is not supposed to be 100% accurate!
467 if (preg_match('/^201/', $browserver)) {
468 $browserver = 3.6;
469 } else if (preg_match('/^200[7-9]/', $browserver)) {
470 $browserver = 3;
471 } else if (preg_match('/^2006/', $version)) {
472 $browserver = 2;
473 } else {
474 $browserver = 1.5;
477 if (version_compare($browserver, $version) >= 0) {
478 return true;
481 return false;
485 * Checks the user agent is IE (of any version).
487 * @return bool true if internet exporeer
489 public static function is_ie() {
490 return self::check_ie_version();
494 * Checks the user agent is IE and returns its main properties:
495 * - browser version;
496 * - whether running in compatibility view.
498 * @return bool|array False if not IE, otherwise an associative array of properties.
500 public static function check_ie_properties() {
501 // Internet Explorer.
502 $useragent = self::get_user_agent_string();
503 if ($useragent === false) {
504 return false;
506 if (strpos($useragent, 'Opera') !== false) {
507 // Reject Opera.
508 return false;
510 // See: http://www.useragentstring.com/pages/Internet%20Explorer/.
511 if (preg_match("/MSIE ([0-9\.]+)/", $useragent, $match)) {
512 $browser = $match[1];
513 // See: http://msdn.microsoft.com/en-us/library/ie/bg182625%28v=vs.85%29.aspx for IE11+ useragent details.
514 } else if (preg_match("/Trident\/[0-9\.]+/", $useragent) && preg_match("/rv:([0-9\.]+)/", $useragent, $match)) {
515 $browser = $match[1];
516 } else {
517 return false;
519 $compatview = false;
520 // IE8 and later versions may pretend to be IE7 for intranet sites, use Trident version instead,
521 // the Trident should always describe the capabilities of IE in any emulation mode.
522 if ($browser === '7.0' and preg_match("/Trident\/([0-9\.]+)/", $useragent, $match)) {
523 $compatview = true;
524 $browser = $match[1] + 4; // NOTE: Hopefully this will work also for future IE versions.
526 $browser = round($browser, 1);
527 return array(
528 'version' => $browser,
529 'compatview' => $compatview
534 * Checks the user agent is IE and that the version is equal to or greater than that specified.
536 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
537 * @return bool
539 public static function check_ie_version($version = null) {
540 // Internet Explorer.
541 $properties = self::check_ie_properties();
542 if (!is_array($properties)) {
543 return false;
545 // In case of IE we have to deal with BC of the version parameter.
546 if (is_null($version)) {
547 $version = 5.5; // Anything older is not considered a browser at all!
549 // IE uses simple versions, let's cast it to float to simplify the logic here.
550 $version = round($version, 1);
551 return ($properties['version'] >= $version);
555 * Checks the user agent is IE and that IE is running under Compatibility View setting.
557 * @return bool true if internet explorer runs in Compatibility View mode.
559 public static function check_ie_compatibility_view() {
560 // IE User Agent string when in Compatibility View:
561 // - IE 8: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; ...)".
562 // - IE 9: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; ...)".
563 // - IE 10: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/6.0; ...)".
564 // - IE 11: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; ...)".
565 // Refs:
566 // - http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx.
567 // - http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx.
568 // - http://blogs.msdn.com/b/ie/archive/2011/04/15/the-ie10-user-agent-string.aspx.
569 // - http://msdn.microsoft.com/en-us/library/ie/hh869301%28v=vs.85%29.aspx.
570 $properties = self::check_ie_properties();
571 if (!is_array($properties)) {
572 return false;
574 return $properties['compatview'];
578 * Checks the user agent is Opera (of any version).
580 * @return bool true if opera
582 public static function is_opera() {
583 return self::check_opera_version();
587 * Checks the user agent is Opera and that the version is equal to or greater than that specified.
589 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
590 * @return bool
592 public static function check_opera_version($version = null) {
593 // Opera.
594 $useragent = self::get_user_agent_string();
595 if ($useragent === false) {
596 return false;
598 if (strpos($useragent, 'Opera') === false) {
599 return false;
601 if (empty($version)) {
602 return true; // No version specified.
604 // Recent Opera useragents have Version/ with the actual version, e.g.:
605 // Opera/9.80 (Windows NT 6.1; WOW64; U; en) Presto/2.10.289 Version/12.01
606 // That's Opera 12.01, not 9.8.
607 if (preg_match("/Version\/([0-9\.]+)/i", $useragent, $match)) {
608 if (version_compare($match[1], $version) >= 0) {
609 return true;
611 } else if (preg_match("/Opera\/([0-9\.]+)/i", $useragent, $match)) {
612 if (version_compare($match[1], $version) >= 0) {
613 return true;
616 return false;
620 * Checks the user agent is webkit based
622 * @return bool true if webkit
624 public static function is_webkit() {
625 return self::check_webkit_version();
629 * Checks the user agent is Webkit based and that the version is equal to or greater than that specified.
631 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
632 * @return bool
634 public static function check_webkit_version($version = null) {
635 // WebKit based browser - everything derived from it (Safari, Chrome, iOS, Android and other mobiles).
636 $useragent = self::get_user_agent_string();
637 if ($useragent === false) {
638 return false;
640 if (strpos($useragent, 'AppleWebKit') === false) {
641 return false;
643 if (empty($version)) {
644 return true; // No version specified.
646 if (preg_match("/AppleWebKit\/([0-9.]+)/i", $useragent, $match)) {
647 if (version_compare($match[1], $version) >= 0) {
648 return true;
651 return false;
655 * Checks the user agent is Safari
657 * @return bool true if safari
659 public static function is_safari() {
660 return self::check_safari_version();
664 * Checks the user agent is Safari based and that the version is equal to or greater than that specified.
666 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
667 * @return bool
669 public static function check_safari_version($version = null) {
670 // Desktop version of Apple Safari browser - no mobile or touch devices.
671 $useragent = self::get_user_agent_string();
672 if ($useragent === false) {
673 return false;
675 if (strpos($useragent, 'AppleWebKit') === false) {
676 return false;
678 // Look for AppleWebKit, excluding strings with OmniWeb, Shiira and SymbianOS and any other mobile devices.
679 if (strpos($useragent, 'OmniWeb')) {
680 // Reject OmniWeb.
681 return false;
683 if (strpos($useragent, 'Shiira')) {
684 // Reject Shiira.
685 return false;
687 if (strpos($useragent, 'SymbianOS')) {
688 // Reject SymbianOS.
689 return false;
691 if (strpos($useragent, 'Android')) {
692 // Reject Androids too.
693 return false;
695 if (strpos($useragent, 'iPhone') or strpos($useragent, 'iPad') or strpos($useragent, 'iPod')) {
696 // No Apple mobile devices here - editor does not work, course ajax is not touch compatible, etc.
697 return false;
699 if (strpos($useragent, 'Chrome')) { // Reject chrome browsers - it needs to be tested explicitly.
700 return false;
703 if (empty($version)) {
704 return true; // No version specified.
706 if (preg_match("/AppleWebKit\/([0-9.]+)/i", $useragent, $match)) {
707 if (version_compare($match[1], $version) >= 0) {
708 return true;
711 return false;
715 * Checks the user agent is Chrome
717 * @return bool true if chrome
719 public static function is_chrome() {
720 return self::check_chrome_version();
724 * Checks the user agent is Chrome based and that the version is equal to or greater than that specified.
726 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
727 * @return bool
729 public static function check_chrome_version($version = null) {
730 // Chrome.
731 $useragent = self::get_user_agent_string();
732 if ($useragent === false) {
733 return false;
735 if (strpos($useragent, 'Chrome') === false) {
736 return false;
738 if (empty($version)) {
739 return true; // No version specified.
741 if (preg_match("/Chrome\/(.*)[ ]+/i", $useragent, $match)) {
742 if (version_compare($match[1], $version) >= 0) {
743 return true;
746 return false;
750 * Checks the user agent is webkit android based.
752 * @return bool true if webkit based and on Android
754 public static function is_webkit_android() {
755 return self::check_webkit_android_version();
759 * Checks the user agent is Webkit based and on Android and that the version is equal to or greater than that specified.
761 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
762 * @return bool
764 public static function check_webkit_android_version($version = null) {
765 // WebKit browser on Android.
766 $useragent = self::get_user_agent_string();
767 if ($useragent === false) {
768 return false;
770 if (strpos($useragent, 'Android') === false) {
771 return false;
773 if (empty($version)) {
774 return true; // No version specified.
776 if (preg_match("/AppleWebKit\/([0-9]+)/i", $useragent, $match)) {
777 if (version_compare($match[1], $version) >= 0) {
778 return true;
781 return false;
785 * Checks the user agent is Safari on iOS
787 * @return bool true if Safari on iOS
789 public static function is_safari_ios() {
790 return self::check_safari_ios_version();
794 * Checks the user agent is Safari on iOS and that the version is equal to or greater than that specified.
796 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
797 * @return bool
799 public static function check_safari_ios_version($version = null) {
800 // Safari on iPhone, iPad and iPod touch.
801 $useragent = self::get_user_agent_string();
802 if ($useragent === false) {
803 return false;
805 if (strpos($useragent, 'AppleWebKit') === false or strpos($useragent, 'Safari') === false) {
806 return false;
808 if (!strpos($useragent, 'iPhone') and !strpos($useragent, 'iPad') and !strpos($useragent, 'iPod')) {
809 return false;
811 if (empty($version)) {
812 return true; // No version specified.
814 if (preg_match("/AppleWebKit\/([0-9]+)/i", $useragent, $match)) {
815 if (version_compare($match[1], $version) >= 0) {
816 return true;
819 return false;
823 * Checks if the user agent is MS Word.
824 * Not perfect, as older versions of Word use standard IE6/7 user agents without any identifying traits.
826 * @return bool true if user agent could be identified as MS Word.
828 public static function is_msword() {
829 $useragent = self::get_user_agent_string();
830 if (!preg_match('/(\bWord\b|ms-office|MSOffice|Microsoft Office)/i', $useragent)) {
831 return false;
832 } else if (strpos($useragent, 'Outlook') !== false) {
833 return false;
834 } else if (strpos($useragent, 'Meridio') !== false) {
835 return false;
837 // It's Office, not Outlook and not Meridio - so it's probably Word, but we can't really be sure in most cases.
838 return true;
842 * Check if the user agent matches a given brand.
844 * Known brand: 'Windows','Linux','Macintosh','SGI','SunOS','HP-UX'
846 * @param string $brand
847 * @return bool
849 public static function check_browser_operating_system($brand) {
850 $useragent = self::get_user_agent_string();
851 return ($useragent !== false && preg_match("/$brand/i", $useragent));
855 * Gets an array of CSS classes to represent the user agent.
856 * @return array
858 public static function get_browser_version_classes() {
859 $classes = array();
860 if (self::is_ie()) {
861 $classes[] = 'ie';
862 for ($i = 12; $i >= 6; $i--) {
863 if (self::check_ie_version($i)) {
864 $classes[] = 'ie'.$i;
865 break;
868 } else if (self::is_firefox() || self::is_gecko() || self::check_camino_version()) {
869 $classes[] = 'gecko';
870 if (preg_match('/rv\:([1-2])\.([0-9])/', self::get_user_agent_string(), $matches)) {
871 $classes[] = "gecko{$matches[1]}{$matches[2]}";
873 } else if (self::is_webkit()) {
874 $classes[] = 'safari';
875 if (self::is_safari_ios()) {
876 $classes[] = 'ios';
877 } else if (self::is_webkit_android()) {
878 $classes[] = 'android';
880 } else if (self::is_opera()) {
881 $classes[] = 'opera';
883 return $classes;
887 * Returns true if the user agent supports the display of SVG images.
889 * @return bool
891 public static function supports_svg() {
892 // IE 5 - 8 don't support SVG at all.
893 $instance = self::instance();
894 if ($instance->supportssvg === null) {
895 if ($instance->useragent === false) {
896 // Can't be sure, just say no.
897 $instance->supportssvg = false;
898 } else if (self::check_ie_version('0') and !self::check_ie_version('9')) {
899 // IE < 9 doesn't support SVG. Say no.
900 $instance->supportssvg = false;
901 } else if (self::is_ie() and !self::check_ie_version('10') and self::check_ie_compatibility_view()) {
902 // IE 9 Compatibility View doesn't support SVG. Say no.
903 $instance->supportssvg = false;
904 } else if (preg_match('#Android +[0-2]\.#', $instance->useragent)) {
905 // Android < 3 doesn't support SVG. Say no.
906 $instance->supportssvg = false;
907 } else if (self::is_opera()) {
908 // Opera 12 still does not support SVG well enough. Say no.
909 $instance->supportssvg = false;
910 } else {
911 // Presumed fine.
912 $instance->supportssvg = true;
915 return $instance->supportssvg;
919 * Returns true if the user agent supports the MIME media type for JSON text, as defined in RFC 4627.
921 * @return bool
923 public static function supports_json_contenttype() {
924 // Modern browsers other than IE correctly supports 'application/json' media type.
925 if (!self::check_ie_version('0')) {
926 return true;
929 // IE8+ supports 'application/json' media type, when NOT in Compatibility View mode.
930 // Refs:
931 // - http://blogs.msdn.com/b/ie/archive/2008/09/10/native-json-in-ie8.aspx;
932 // - MDL-39810: issues when using 'text/plain' in Compatibility View for the body of an HTTP POST response.
933 if (self::check_ie_version(8) && !self::check_ie_compatibility_view()) {
934 return true;
937 // This browser does not support json.
938 return false;
942 * Returns true if the client appears to be some kind of web crawler.
943 * This may include other types of crawler.
945 * @return bool
947 public static function is_web_crawler() {
948 $instance = self::instance();
949 return (bool) $instance->is_useragent_web_crawler();