MDL-51360 core_grades: Improve documentation of grade_get_grades().
[moodle.git] / lib / classes / useragent.php
blob1e8cc91ec1452b29a00f5ac1f5416321aa2fe3c0
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 * True if the user agent supports the display of svg images. False if not.
91 * @var bool|null Null until initialised, then true or false.
93 protected $supportssvg = null;
95 /**
96 * Get an instance of the user agent object.
98 * @param bool $reload If set to true the user agent will be reset and all ascertations remade.
99 * @param string $forceuseragent The string to force as the user agent, don't use unless absolutely unavoidable.
100 * @return core_useragent
102 public static function instance($reload = false, $forceuseragent = null) {
103 if (!self::$instance || $reload) {
104 self::$instance = new core_useragent($forceuseragent);
106 return self::$instance;
110 * Constructs a new user agent object. Publically you must use the instance method above.
112 * @param string|null $forceuseragent Optional a user agent to force.
114 protected function __construct($forceuseragent = null) {
115 if ($forceuseragent !== null) {
116 $this->useragent = $forceuseragent;
117 } else if (!empty($_SERVER['HTTP_USER_AGENT'])) {
118 $this->useragent = $_SERVER['HTTP_USER_AGENT'];
119 } else {
120 $this->useragent = false;
121 $this->devicetype = self::DEVICETYPE_DEFAULT;
126 * Get the MoodleBot UserAgent for this site.
128 * @return string UserAgent
130 public static function get_moodlebot_useragent() {
131 global $CFG;
133 $version = moodle_major_version(); // Only major version for security.
134 return "MoodleBot/$version (+{$CFG->wwwroot})";
138 * Returns the user agent string.
139 * @return bool|string The user agent string or false if one isn't available.
141 public static function get_user_agent_string() {
142 $instance = self::instance();
143 return $instance->useragent;
147 * Returns the device type we believe is being used.
148 * @return string
150 public static function get_device_type() {
151 $instance = self::instance();
152 if ($instance->devicetype === null) {
153 return $instance->guess_device_type();
155 return $instance->devicetype;
159 * Guesses the device type the user agent is running on.
161 * @return string
163 protected function guess_device_type() {
164 if ($this->is_useragent_mobile()) {
165 $this->devicetype = self::DEVICETYPE_MOBILE;
166 } else if ($this->is_useragent_tablet()) {
167 $this->devicetype = self::DEVICETYPE_TABLET;
168 } else if (self::check_ie_version('0') && !self::check_ie_version('7.0')) {
169 // IE 6 and before are considered legacy.
170 $this->devicetype = self::DEVICETYPE_LEGACY;
171 } else {
172 $this->devicetype = self::DEVICETYPE_DEFAULT;
174 return $this->devicetype;
178 * Returns true if the user appears to be on a mobile device.
179 * @return bool
181 protected function is_useragent_mobile() {
182 // Mobile detection PHP direct copy from open source detectmobilebrowser.com.
183 $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';
184 $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';
185 return (preg_match($phonesregex, $this->useragent) || preg_match($modelsregex, substr($this->useragent, 0, 4)));
189 * Returns true if the user appears to be on a tablet.
191 * @return int
193 protected function is_useragent_tablet() {
194 $tabletregex = '/Tablet browser|android|iPad|iProd|GT-P1000|GT-I9000|SHW-M180S|SGH-T849|SCH-I800|Build\/ERE27|sholest/i';
195 return (preg_match($tabletregex, $this->useragent));
199 * Whether the user agent relates to a web crawler.
200 * This includes all types of web crawler.
201 * @return bool
203 protected function is_useragent_web_crawler() {
204 $regex = '/MoodleBot|Googlebot|google\.com|Yahoo! Slurp|\[ZSEBOT\]|msnbot|bingbot|BingPreview|Yandex|AltaVista'
205 .'|Baiduspider|Teoma/i';
206 return (preg_match($regex, $this->useragent));
210 * Gets a list of known device types.
212 * @deprecated Moodle 4.3 MDL-78468 - No longer used. Please use core_useragent::devicetypes instead.
213 * @todo Final deprecation on Moodle 4.7 MDL-79052
214 * @param bool $includecustomtypes If set to true we'll include types that have been added by the admin.
215 * @return array
217 public static function get_device_type_list($includecustomtypes = true) {
218 debugging(
219 __FUNCTION__ . '() is deprecated.' .
220 'All functions associated with devicedetectregex theme setting are being removed.
221 Please use core_useragent::devicetypes instead',
222 DEBUG_DEVELOPER
224 $types = self::$devicetypes;
225 if ($includecustomtypes) {
226 $instance = self::instance();
227 $types = array_merge($types, array_keys($instance->devicetypecustoms));
229 return $types;
233 * Returns the theme to use for the given device type.
235 * This used to be get_selected_theme_for_device_type.
236 * @param null|string $devicetype The device type to find out for. Defaults to the device the user is using,
237 * @deprecated since 4.3.
238 * @return bool
240 public static function get_device_type_theme($devicetype = null) {
241 debugging(
242 __FUNCTION__ . '() is deprecated.' .
243 'All functions associated with device specific themes are being removed.',
244 DEBUG_DEVELOPER
246 global $CFG;
247 if ($devicetype === null) {
248 $devicetype = self::get_device_type();
250 $themevarname = self::get_device_type_cfg_var_name($devicetype);
251 if (empty($CFG->$themevarname)) {
252 return false;
254 return $CFG->$themevarname;
258 * Returns the CFG var used to find the theme to use for the given device.
260 * Used to be get_device_cfg_var_name.
262 * @param null|string $devicetype The device type to find out for. Defaults to the device the user is using,
263 * @deprecated since 4.3.
264 * @return string
266 public static function get_device_type_cfg_var_name($devicetype = null) {
267 debugging(
268 __FUNCTION__ . '() is deprecated.' .
269 'All functions associated with device specific themes are being removed.',
270 DEBUG_DEVELOPER
272 if ($devicetype == self::DEVICETYPE_DEFAULT || empty($devicetype)) {
273 return 'theme';
275 return 'theme' . $devicetype;
279 * Gets the device type the user is currently using.
280 * @return string
282 public static function get_user_device_type() {
283 $device = self::get_device_type();
284 $switched = get_user_preferences('switchdevice'.$device, false);
285 if ($switched != false) {
286 return $switched;
288 return $device;
292 * Switches the device type we think the user is using to what ever was given.
293 * @param string $newdevice
294 * @return bool
295 * @throws coding_exception
297 public static function set_user_device_type($newdevice) {
298 $devicetype = self::get_device_type();
299 if ($newdevice == $devicetype) {
300 unset_user_preference('switchdevice'.$devicetype);
301 return true;
302 } else {
303 $devicetypes = self::$devicetypes;
304 if (in_array($newdevice, $devicetypes)) {
305 set_user_preference('switchdevice'.$devicetype, $newdevice);
306 return true;
309 throw new coding_exception('Invalid device type provided to set_user_device_type');
313 * Returns true if the user agent matches the given brand and the version is equal to or greater than that specified.
315 * @param string $brand The branch to check for.
316 * @param scalar $version The version if we need to find out if it is equal to or greater than that specified.
317 * @return bool
319 public static function check_browser_version($brand, $version = null) {
320 switch ($brand) {
322 case 'MSIE':
323 // Internet Explorer.
324 return self::check_ie_version($version);
326 case 'Edge':
327 // Microsoft Edge.
328 return self::check_edge_version($version);
330 case 'Firefox':
331 // Mozilla Firefox browsers.
332 return self::check_firefox_version($version);
334 case 'Chrome':
335 return self::check_chrome_version($version);
337 case 'Opera':
338 // Opera.
339 return self::check_opera_version($version);
341 case 'Safari':
342 // Desktop version of Apple Safari browser - no mobile or touch devices.
343 return self::check_safari_version($version);
345 case 'Safari iOS':
346 // Safari on iPhone, iPad and iPod touch.
347 return self::check_safari_ios_version($version);
349 case 'WebKit':
350 // WebKit based browser - everything derived from it (Safari, Chrome, iOS, Android and other mobiles).
351 return self::check_webkit_version($version);
353 case 'Gecko':
354 // Gecko based browsers.
355 return self::check_gecko_version($version);
357 case 'WebKit Android':
358 // WebKit browser on Android.
359 return self::check_webkit_android_version($version);
361 case 'Camino':
362 // OSX browser using Gecke engine.
363 return self::check_camino_version($version);
365 // Who knows?! doesn't pass anyway.
366 return false;
370 * Checks the user agent is camino based and that the version is equal to or greater than that specified.
372 * Camino browser is at the end of its life, its no longer being developed or supported, just don't worry about it.
374 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
375 * @return bool
377 protected static function check_camino_version($version = null) {
378 // OSX browser using Gecko engine.
379 $useragent = self::get_user_agent_string();
380 if ($useragent === false) {
381 return false;
383 if (strpos($useragent, 'Camino') === false) {
384 return false;
386 if (empty($version)) {
387 return true; // No version specified.
389 if (preg_match("/Camino\/([0-9\.]+)/i", $useragent, $match)) {
390 if (version_compare($match[1], $version) >= 0) {
391 return true;
394 return false;
398 * Checks the user agent is Firefox (of any version).
400 * @return bool true if firefox
402 public static function is_firefox() {
403 return self::check_firefox_version();
407 * Checks the user agent is Firefox based and that the version is equal to or greater than that specified.
409 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
410 * @return bool
412 public static function check_firefox_version($version = null) {
413 // Mozilla Firefox browsers.
414 $useragent = self::get_user_agent_string();
415 if ($useragent === false) {
416 return false;
418 if (strpos($useragent, 'Firefox') === false && strpos($useragent, 'Iceweasel') === false) {
419 return false;
421 if (empty($version)) {
422 return true; // No version specified..
424 if (preg_match("/(Iceweasel|Firefox)\/([0-9\.]+)/i", $useragent, $match)) {
425 if (version_compare($match[2], $version) >= 0) {
426 return true;
429 return false;
433 * Checks the user agent is Gecko based (of any version).
435 * @return bool true if Gecko based.
437 public static function is_gecko() {
438 return self::check_gecko_version();
442 * Checks the user agent is Gecko based and that the version is equal to or greater than that specified.
444 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
445 * @return bool
447 public static function check_gecko_version($version = null) {
448 // Gecko based browsers.
449 // Do not look for dates any more, we expect real Firefox version here.
450 $useragent = self::get_user_agent_string();
451 if ($useragent === false) {
452 return false;
454 if (empty($version)) {
455 $version = 1;
456 } else if ($version > 20000000) {
457 // This is just a guess, it is not supposed to be 100% accurate!
458 if (preg_match('/^201/', $version)) {
459 $version = 3.6;
460 } else if (preg_match('/^200[7-9]/', $version)) {
461 $version = 3;
462 } else if (preg_match('/^2006/', $version)) {
463 $version = 2;
464 } else {
465 $version = 1.5;
468 if (preg_match("/(Iceweasel|Firefox)\/([0-9\.]+)/i", $useragent, $match)) {
469 // Use real Firefox version if specified in user agent string.
470 if (version_compare($match[2], $version) >= 0) {
471 return true;
473 } else if (preg_match("/Gecko\/([0-9\.]+)/i", $useragent, $match)) {
474 // Gecko might contain date or Firefox revision, let's just guess the Firefox version from the date.
475 $browserver = $match[1];
476 if ($browserver > 20000000) {
477 // This is just a guess, it is not supposed to be 100% accurate!
478 if (preg_match('/^201/', $browserver)) {
479 $browserver = 3.6;
480 } else if (preg_match('/^200[7-9]/', $browserver)) {
481 $browserver = 3;
482 } else if (preg_match('/^2006/', $version)) {
483 $browserver = 2;
484 } else {
485 $browserver = 1.5;
488 if (version_compare($browserver, $version) >= 0) {
489 return true;
492 return false;
496 * Checks the user agent is Edge (of any version).
498 * @return bool true if Edge
500 public static function is_edge() {
501 return self::check_edge_version();
505 * Check the User Agent for the version of Edge.
507 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
508 * @return bool
510 public static function check_edge_version($version = null) {
511 $useragent = self::get_user_agent_string();
513 if ($useragent === false) {
514 // No User Agent found.
515 return false;
518 if (strpos($useragent, 'Edge/') === false) {
519 // Edge was not found in the UA - this is not Edge.
520 return false;
523 if (empty($version)) {
524 // No version to check.
525 return true;
528 // Find the version.
529 // Edge versions are always in the format:
530 // Edge/<version>.<OS build number>
531 preg_match('%Edge/([\d]+)\.(.*)$%', $useragent, $matches);
533 // Just to be safe, round the version being tested.
534 // Edge only uses integer versions - the second component is the OS build number.
535 $version = round($version);
537 // Check whether the version specified is >= the version found.
538 return version_compare($matches[1], $version, '>=');
542 * Checks the user agent is IE (of any version).
544 * @return bool true if internet exporeer
546 public static function is_ie() {
547 return self::check_ie_version();
551 * Checks the user agent is IE and returns its main properties:
552 * - browser version;
553 * - whether running in compatibility view.
555 * @return bool|array False if not IE, otherwise an associative array of properties.
557 public static function check_ie_properties() {
558 // Internet Explorer.
559 $useragent = self::get_user_agent_string();
560 if ($useragent === false) {
561 return false;
563 if (strpos($useragent, 'Opera') !== false) {
564 // Reject Opera.
565 return false;
567 // See: http://www.useragentstring.com/pages/Internet%20Explorer/.
568 if (preg_match("/MSIE ([0-9\.]+)/", $useragent, $match)) {
569 $browser = $match[1];
570 // See: http://msdn.microsoft.com/en-us/library/ie/bg182625%28v=vs.85%29.aspx for IE11+ useragent details.
571 } else if (preg_match("/Trident\/[0-9\.]+/", $useragent) && preg_match("/rv:([0-9\.]+)/", $useragent, $match)) {
572 $browser = $match[1];
573 } else {
574 return false;
577 $compatview = false;
578 // IE8 and later versions may pretend to be IE7 for intranet sites, use Trident version instead,
579 // the Trident should always describe the capabilities of IE in any emulation mode.
580 if ($browser === '7.0' and preg_match("/Trident\/([0-9\.]+)/", $useragent, $match)) {
581 $compatview = true;
582 $browser = $match[1] + 4; // NOTE: Hopefully this will work also for future IE versions.
584 $browser = round($browser, 1);
585 return array(
586 'version' => $browser,
587 'compatview' => $compatview
592 * Checks the user agent is IE and that the version is equal to or greater than that specified.
594 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
595 * @return bool
597 public static function check_ie_version($version = null) {
598 // Internet Explorer.
599 $properties = self::check_ie_properties();
600 if (!is_array($properties)) {
601 return false;
603 // In case of IE we have to deal with BC of the version parameter.
604 if (is_null($version)) {
605 $version = 5.5; // Anything older is not considered a browser at all!
607 // IE uses simple versions, let's cast it to float to simplify the logic here.
608 $version = round($version, 1);
609 return ($properties['version'] >= $version);
613 * Checks the user agent is IE and that IE is running under Compatibility View setting.
615 * @return bool true if internet explorer runs in Compatibility View mode.
617 public static function check_ie_compatibility_view() {
618 // IE User Agent string when in Compatibility View:
619 // - IE 8: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; ...)".
620 // - IE 9: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; ...)".
621 // - IE 10: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/6.0; ...)".
622 // - IE 11: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; ...)".
623 // Refs:
624 // - http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx.
625 // - http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx.
626 // - http://blogs.msdn.com/b/ie/archive/2011/04/15/the-ie10-user-agent-string.aspx.
627 // - http://msdn.microsoft.com/en-us/library/ie/hh869301%28v=vs.85%29.aspx.
628 $properties = self::check_ie_properties();
629 if (!is_array($properties)) {
630 return false;
632 return $properties['compatview'];
636 * Checks the user agent is Opera (of any version).
638 * @return bool true if opera
640 public static function is_opera() {
641 return self::check_opera_version();
645 * Checks the user agent is Opera and that the version is equal to or greater than that specified.
647 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
648 * @return bool
650 public static function check_opera_version($version = null) {
651 // Opera.
652 $useragent = self::get_user_agent_string();
653 if ($useragent === false) {
654 return false;
656 if (strpos($useragent, 'Opera') === false) {
657 return false;
659 if (empty($version)) {
660 return true; // No version specified.
662 // Recent Opera useragents have Version/ with the actual version, e.g.:
663 // Opera/9.80 (Windows NT 6.1; WOW64; U; en) Presto/2.10.289 Version/12.01
664 // That's Opera 12.01, not 9.8.
665 if (preg_match("/Version\/([0-9\.]+)/i", $useragent, $match)) {
666 if (version_compare($match[1], $version) >= 0) {
667 return true;
669 } else if (preg_match("/Opera\/([0-9\.]+)/i", $useragent, $match)) {
670 if (version_compare($match[1], $version) >= 0) {
671 return true;
674 return false;
678 * Checks the user agent is webkit based
680 * @return bool true if webkit
682 public static function is_webkit() {
683 return self::check_webkit_version();
687 * Checks the user agent is Webkit based and that the version is equal to or greater than that specified.
689 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
690 * @return bool
692 public static function check_webkit_version($version = null) {
693 // WebKit based browser - everything derived from it (Safari, Chrome, iOS, Android and other mobiles).
694 $useragent = self::get_user_agent_string();
695 if ($useragent === false) {
696 return false;
698 if (strpos($useragent, 'AppleWebKit') === false) {
699 return false;
701 if (empty($version)) {
702 return true; // No version specified.
704 if (preg_match("/AppleWebKit\/([0-9.]+)/i", $useragent, $match)) {
705 if (version_compare($match[1], $version) >= 0) {
706 return true;
709 return false;
713 * Checks the user agent is Safari
715 * @return bool true if safari
717 public static function is_safari() {
718 return self::check_safari_version();
722 * Checks the user agent is Safari based and that the version is equal to or greater than that specified.
724 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
725 * @return bool
727 public static function check_safari_version($version = null) {
728 // Desktop version of Apple Safari browser - no mobile or touch devices.
729 $useragent = self::get_user_agent_string();
730 if ($useragent === false) {
731 return false;
733 if (strpos($useragent, 'AppleWebKit') === false) {
734 return false;
736 // Look for AppleWebKit, excluding strings with OmniWeb, Shiira and SymbianOS and any other mobile devices.
737 if (strpos($useragent, 'OmniWeb')) {
738 // Reject OmniWeb.
739 return false;
741 if (strpos($useragent, 'Shiira')) {
742 // Reject Shiira.
743 return false;
745 if (strpos($useragent, 'SymbianOS')) {
746 // Reject SymbianOS.
747 return false;
749 if (strpos($useragent, 'Android')) {
750 // Reject Androids too.
751 return false;
753 if (strpos($useragent, 'iPhone') or strpos($useragent, 'iPad') or strpos($useragent, 'iPod')) {
754 // No Apple mobile devices here - editor does not work, course ajax is not touch compatible, etc.
755 return false;
757 if (strpos($useragent, 'Chrome')) {
758 // Reject chrome browsers - it needs to be tested explicitly.
759 // This will also reject Edge, which pretends to be both Chrome, and Safari.
760 return false;
763 if (empty($version)) {
764 return true; // No version specified.
766 if (preg_match("/AppleWebKit\/([0-9.]+)/i", $useragent, $match)) {
767 if (version_compare($match[1], $version) >= 0) {
768 return true;
771 return false;
775 * Checks the user agent is Chrome
777 * @return bool true if chrome
779 public static function is_chrome() {
780 return self::check_chrome_version();
784 * Checks the user agent is Chrome based and that the version is equal to or greater than that specified.
786 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
787 * @return bool
789 public static function check_chrome_version($version = null) {
790 // Chrome.
791 $useragent = self::get_user_agent_string();
792 if ($useragent === false) {
793 return false;
795 if (strpos($useragent, 'Chrome') === false) {
796 return false;
798 if (empty($version)) {
799 return true; // No version specified.
801 if (preg_match("/Chrome\/(.*)[ ]+/i", $useragent, $match)) {
802 if (version_compare($match[1], $version) >= 0) {
803 return true;
806 return false;
810 * Checks the user agent is webkit android based.
812 * @return bool true if webkit based and on Android
814 public static function is_webkit_android() {
815 return self::check_webkit_android_version();
819 * Checks the user agent is Webkit based and on Android and that the version is equal to or greater than that specified.
821 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
822 * @return bool
824 public static function check_webkit_android_version($version = null) {
825 // WebKit browser on Android.
826 $useragent = self::get_user_agent_string();
827 if ($useragent === false) {
828 return false;
830 if (strpos($useragent, 'Android') === false) {
831 return false;
833 if (empty($version)) {
834 return true; // No version specified.
836 if (preg_match("/AppleWebKit\/([0-9]+)/i", $useragent, $match)) {
837 if (version_compare($match[1], $version) >= 0) {
838 return true;
841 return false;
845 * Checks the user agent is Safari on iOS
847 * @return bool true if Safari on iOS
849 public static function is_safari_ios() {
850 return self::check_safari_ios_version();
854 * Checks the user agent is Safari on iOS and that the version is equal to or greater than that specified.
856 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
857 * @return bool
859 public static function check_safari_ios_version($version = null) {
860 // Safari on iPhone, iPad and iPod touch.
861 $useragent = self::get_user_agent_string();
862 if ($useragent === false) {
863 return false;
865 if (strpos($useragent, 'AppleWebKit') === false or strpos($useragent, 'Safari') === false) {
866 return false;
868 if (!strpos($useragent, 'iPhone') and !strpos($useragent, 'iPad') and !strpos($useragent, 'iPod')) {
869 return false;
871 if (empty($version)) {
872 return true; // No version specified.
874 if (preg_match("/AppleWebKit\/([0-9]+)/i", $useragent, $match)) {
875 if (version_compare($match[1], $version) >= 0) {
876 return true;
879 return false;
883 * Checks if the user agent is MS Word.
884 * Not perfect, as older versions of Word use standard IE6/7 user agents without any identifying traits.
886 * @return bool true if user agent could be identified as MS Word.
888 public static function is_msword() {
889 $useragent = self::get_user_agent_string();
890 if (!preg_match('/(\bWord\b|ms-office|MSOffice|Microsoft Office)/i', $useragent)) {
891 return false;
892 } else if (strpos($useragent, 'Outlook') !== false) {
893 return false;
894 } else if (strpos($useragent, 'Meridio') !== false) {
895 return false;
897 // It's Office, not Outlook and not Meridio - so it's probably Word, but we can't really be sure in most cases.
898 return true;
902 * Check if the user agent matches a given brand.
904 * Known brand: 'Windows','Linux','Macintosh','SGI','SunOS','HP-UX'
906 * @param string $brand
907 * @return bool
909 public static function check_browser_operating_system($brand) {
910 $useragent = self::get_user_agent_string();
911 return ($useragent !== false && preg_match("/$brand/i", $useragent));
915 * Gets an array of CSS classes to represent the user agent.
916 * @return array
918 public static function get_browser_version_classes() {
919 $classes = array();
920 if (self::is_edge()) {
921 $classes[] = 'edge';
922 } else if (self::is_ie()) {
923 $classes[] = 'ie';
924 for ($i = 12; $i >= 6; $i--) {
925 if (self::check_ie_version($i)) {
926 $classes[] = 'ie'.$i;
927 break;
930 } else if (self::is_firefox() || self::is_gecko() || self::check_camino_version()) {
931 $classes[] = 'gecko';
932 if (preg_match('/rv\:([1-2])\.([0-9])/', self::get_user_agent_string(), $matches)) {
933 $classes[] = "gecko{$matches[1]}{$matches[2]}";
935 } else if (self::is_chrome()) {
936 $classes[] = 'chrome';
937 if (self::is_webkit_android()) {
938 $classes[] = 'android';
940 } else if (self::is_webkit()) {
941 if (self::is_safari()) {
942 $classes[] = 'safari';
944 if (self::is_safari_ios()) {
945 $classes[] = 'ios';
946 } else if (self::is_webkit_android()) {
947 $classes[] = 'android'; // Old pre-Chrome android browsers.
949 } else if (self::is_opera()) {
950 $classes[] = 'opera';
952 return $classes;
956 * Returns true if the user agent supports the display of SVG images.
958 * @return bool
960 public static function supports_svg() {
961 $instance = self::instance();
962 if ($instance->supportssvg === null) {
963 if (preg_match('#Android +[0-2]\.#', $instance->useragent)) {
964 // Android < 3 doesn't support SVG.
965 $instance->supportssvg = false;
966 } else {
967 // With widespread SVG support in modern browsers, default to returning true (even when useragent is false).
968 $instance->supportssvg = true;
971 return $instance->supportssvg;
975 * Returns true if the user agent supports the MIME media type for JSON text, as defined in RFC 4627.
977 * @return bool
979 public static function supports_json_contenttype() {
980 // Modern browsers other than IE correctly supports 'application/json' media type.
981 if (!self::check_ie_version('0')) {
982 return true;
985 // IE8+ supports 'application/json' media type, when NOT in Compatibility View mode.
986 // Refs:
987 // - http://blogs.msdn.com/b/ie/archive/2008/09/10/native-json-in-ie8.aspx;
988 // - MDL-39810: issues when using 'text/plain' in Compatibility View for the body of an HTTP POST response.
989 if (self::check_ie_version(8) && !self::check_ie_compatibility_view()) {
990 return true;
993 // This browser does not support json.
994 return false;
998 * Returns true if the client appears to be some kind of web crawler.
999 * This may include other types of crawler.
1001 * @return bool
1003 public static function is_web_crawler() {
1004 $instance = self::instance();
1005 return (bool) $instance->is_useragent_web_crawler();
1009 * Returns true if the client appears to be a device using iOS (iPhone, iPad, iPod).
1011 * @param scalar $version The version if we need to find out if it is equal to or greater than that specified.
1012 * @return bool true if the client is using iOS
1013 * @since Moodle 3.2
1015 public static function is_ios($version = null) {
1016 $useragent = self::get_user_agent_string();
1017 if ($useragent === false) {
1018 return false;
1020 if (strpos($useragent, 'AppleWebKit') === false) {
1021 return false;
1023 if (strpos($useragent, 'Windows')) {
1024 // Reject Windows Safari.
1025 return false;
1027 if (strpos($useragent, 'Macintosh')) {
1028 // Reject MacOS Safari.
1029 return false;
1031 // Look for AppleWebKit, excluding strings with OmniWeb, Shiira and SymbianOS and any other mobile devices.
1032 if (strpos($useragent, 'OmniWeb')) {
1033 // Reject OmniWeb.
1034 return false;
1036 if (strpos($useragent, 'Shiira')) {
1037 // Reject Shiira.
1038 return false;
1040 if (strpos($useragent, 'SymbianOS')) {
1041 // Reject SymbianOS.
1042 return false;
1044 if (strpos($useragent, 'Android')) {
1045 // Reject Androids too.
1046 return false;
1048 if (strpos($useragent, 'Chrome')) {
1049 // Reject chrome browsers - it needs to be tested explicitly.
1050 // This will also reject Edge, which pretends to be both Chrome, and Safari.
1051 return false;
1054 if (empty($version)) {
1055 return true; // No version specified.
1057 if (preg_match("/AppleWebKit\/([0-9.]+)/i", $useragent, $match)) {
1058 if (version_compare($match[1], $version) >= 0) {
1059 return true;
1062 return false;
1066 * Returns true if the client appears to be the Moodle app (or an app based on the Moodle app code).
1068 * @return bool true if the client is the Moodle app
1069 * @since Moodle 3.7
1071 public static function is_moodle_app() {
1072 $useragent = self::get_user_agent_string();
1074 // Make it case insensitive, things can change in the app or desktop app depending on the platform frameworks.
1075 if (stripos($useragent, 'MoodleMobile') !== false) {
1076 return true;
1079 return false;
1083 * Checks if current browser supports files with give extension as <video> or <audio> source
1085 * Note, the check here is not 100% accurate!
1087 * First, we do not know which codec is used in .mp4 or .webm files. Not all browsers support
1088 * all codecs.
1090 * Also we assume that users of Firefox/Chrome/Safari do not use the ancient versions of browsers.
1092 * We check the exact version for IE/Edge though. We know that there are still users of very old
1093 * versions that are afraid to upgrade or have slow IT department.
1095 * Resources:
1096 * https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats
1097 * https://en.wikipedia.org/wiki/HTML5_video
1098 * https://en.wikipedia.org/wiki/HTML5_Audio
1100 * @param string $extension extension without leading .
1101 * @return bool
1103 public static function supports_html5($extension) {
1104 $extension = strtolower($extension);
1106 $supportedvideo = array('m4v', 'webm', 'ogv', 'mp4', 'mov', 'fmp4');
1107 $supportedaudio = array('ogg', 'oga', 'aac', 'm4a', 'mp3', 'wav', 'flac');
1109 // Basic extension support.
1110 if (!in_array($extension, $supportedvideo) && !in_array($extension, $supportedaudio)) {
1111 return false;
1114 // MS IE support - version 9.0 or later.
1115 if (self::is_ie() && !self::check_ie_version('9.0')) {
1116 return false;
1119 // MS Edge support - version 12.0 for desktop and 13.0 for mobile.
1120 if (self::is_edge()) {
1121 if (!self::check_edge_version('12.0')) {
1122 return false;
1124 if (self::instance()->is_useragent_mobile() && !self::check_edge_version('13.0')) {
1125 return false;
1129 // Different exceptions.
1131 // Webm is not supported in IE, Edge and in Safari.
1132 if ($extension === 'webm' &&
1133 (self::is_ie() || self::is_edge() || self::is_safari() || self::is_safari_ios())) {
1134 return false;
1136 // Ogg is not supported in IE, Edge and Safari.
1137 $isogg = in_array($extension, ['ogg', 'oga', 'ogv']);
1138 if ($isogg && (self::is_ie() || self::is_edge() || self::is_safari() || self::is_safari_ios())) {
1139 return false;
1141 // FLAC is not supported in IE and Edge (below 16.0).
1142 if ($extension === 'flac' &&
1143 (self::is_ie() || (self::is_edge() && !self::check_edge_version('16.0')))) {
1144 return false;
1146 // Wave is not supported in IE.
1147 if ($extension === 'wav' && self::is_ie()) {
1148 return false;
1150 // Aac is not supported in IE below 11.0.
1151 if ($extension === 'aac' && (self::is_ie() && !self::check_ie_version('11.0'))) {
1152 return false;
1154 // Mpeg is not supported in IE below 10.0.
1155 $ismpeg = in_array($extension, ['m4a', 'mp3', 'm4v', 'mp4', 'fmp4']);
1156 if ($ismpeg && (self::is_ie() && !self::check_ie_version('10.0'))) {
1157 return false;
1159 // Mov is not supported in IE.
1160 if ($extension === 'mov' && self::is_ie()) {
1161 return false;
1164 return true;
1168 * Checks if current browser supports the HLS and MPEG-DASH media
1169 * streaming formats. Most browsers get this from Media Source Extensions.
1170 * Safari on iOS, doesn't support MPEG-DASH at all.
1172 * Note, the check here is not 100% accurate!
1174 * Also we assume that users of Firefox/Chrome/Safari do not use the ancient versions of browsers.
1175 * We check the exact version for IE/Edge though. We know that there are still users of very old
1176 * versions that are afraid to upgrade or have slow IT department.
1178 * Resources:
1179 * https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API
1180 * https://caniuse.com/#search=mpeg-dash
1181 * https://caniuse.com/#search=hls
1183 * @param string $extension
1184 * @return bool
1186 public static function supports_media_source_extensions(string $extension): bool {
1187 // Not supported in IE below 11.0.
1188 if (self::is_ie() && !self::check_ie_version('11.0')) {
1189 return false;
1192 if ($extension == '.mpd') {
1193 // Not supported in Safari on iOS.
1194 if (self::is_safari_ios()) {
1195 return false;
1199 return true;