2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Environment class to aid with the detection and establishment of the working environment.
21 * @copyright 2013 Sam Hemelryk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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!
37 * @copyright 2013 Sam Hemelryk
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class core_useragent
{
43 * The default for devices, think of as a computer.
45 const DEVICETYPE_DEFAULT
= 'default';
47 * Legacy devices, or at least legacy browsers. These are older devices/browsers
48 * that don't support standards.
50 const DEVICETYPE_LEGACY
= 'legacy';
52 * Mobile devices like your cell phone or hand held gaming device.
54 const DEVICETYPE_MOBILE
= 'mobile';
56 * Tables, larger than hand held, but still easily portable and smaller than a laptop.
58 const DEVICETYPE_TABLET
= 'tablet';
61 * An instance of this class.
64 protected static $instance = null;
67 * The device types we track.
70 public static $devicetypes = array(
71 self
::DEVICETYPE_DEFAULT
,
72 self
::DEVICETYPE_LEGACY
,
73 self
::DEVICETYPE_MOBILE
,
74 self
::DEVICETYPE_TABLET
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;
84 * The users device type, one of self::DEVICETYPE_*.
85 * @var string null until initialised
87 protected $devicetype = null;
90 * Custom device types entered into the admin interface.
93 protected $devicetypecustoms = array();
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) {
122 if (!empty($CFG->devicedetectregex
)) {
123 $this->devicetypecustoms
= json_decode($CFG->devicedetectregex
);
125 if ($forceuseragent !== null) {
126 $this->useragent
= $forceuseragent;
127 } else if (!empty($_SERVER['HTTP_USER_AGENT'])) {
128 $this->useragent
= $_SERVER['HTTP_USER_AGENT'];
130 $this->useragent
= false;
131 $this->devicetype
= self
::DEVICETYPE_DEFAULT
;
136 * Returns the user agent string.
137 * @return bool|string The user agent string or false if one isn't available.
139 public static function get_user_agent_string() {
140 $instance = self
::instance();
141 return $instance->useragent
;
145 * Returns the device type we believe is being used.
148 public static function get_device_type() {
149 $instance = self
::instance();
150 if ($instance->devicetype
=== null) {
151 return $instance->guess_device_type();
153 return $instance->devicetype
;
157 * Guesses the device type the user agent is running on.
161 protected function guess_device_type() {
163 if (empty($CFG->enabledevicedetection
)) {
164 $this->devicetype
= self
::DEVICETYPE_DEFAULT
;
165 return $this->devicetype
;
167 foreach ($this->devicetypecustoms
as $value => $regex) {
168 if (preg_match($regex, $this->useragent
)) {
169 $this->devicetype
= $value;
170 return $this->devicetype
;
173 if ($this->is_useragent_mobile()) {
174 $this->devicetype
= 'mobile';
175 } else if ($this->is_useragent_tablet()) {
176 $this->devicetype
= 'tablet';
177 } else if (substr($this->useragent
, 0, 34) === 'Mozilla/4.0 (compatible; MSIE 6.0;') {
178 // Safe way to check for IE6 and not get false positives for some IE 7/8 users.
179 $this->devicetype
= 'legacy';
181 $this->devicetype
= self
::DEVICETYPE_DEFAULT
;
183 return $this->devicetype
;
187 * Returns true if the user appears to be on a mobile device.
190 protected function is_useragent_mobile() {
191 // Mobile detection PHP direct copy from open source detectmobilebrowser.com.
192 $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';
193 $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';
194 return (preg_match($phonesregex, $this->useragent
) ||
preg_match($modelsregex, substr($this->useragent
, 0, 4)));
198 * Returns true if the user appears to be on a tablet.
201 protected function is_useragent_tablet() {
202 $tabletregex = '/Tablet browser|android|iPad|iProd|GT-P1000|GT-I9000|SHW-M180S|SGH-T849|SCH-I800|Build\/ERE27|sholest/i';
203 return (preg_match($tabletregex, $this->useragent
));
207 * Gets a list of known device types.
209 * @param bool $includecustomtypes If set to true we'll include types that have been added by the admin.
212 public static function get_device_type_list($includecustomtypes = true) {
213 $types = self
::$devicetypes;
214 if ($includecustomtypes) {
215 $instance = self
::instance();
216 $types = array_merge($types, array_keys($instance->devicetypecustoms
));
222 * Returns the theme to use for the given device type.
224 * This used to be get_selected_theme_for_device_type.
225 * @param null|string $devicetype The device type to find out for. Defaults to the device the user is using,
228 public static function get_device_type_theme($devicetype = null) {
230 if ($devicetype === null) {
231 $devicetype = self
::get_device_type();
233 $themevarname = self
::get_device_type_cfg_var_name($devicetype);
234 if (empty($CFG->$themevarname)) {
237 return $CFG->$themevarname;
241 * Returns the CFG var used to find the theme to use for the given device.
243 * Used to be get_device_cfg_var_name.
245 * @param null|string $devicetype The device type to find out for. Defaults to the device the user is using,
248 public static function get_device_type_cfg_var_name($devicetype = null) {
249 if ($devicetype == self
::DEVICETYPE_DEFAULT ||
empty($devicetype)) {
252 return 'theme' . $devicetype;
256 * Gets the device type the user is currently using.
259 public static function get_user_device_type() {
260 $device = self
::get_device_type();
261 $switched = get_user_preferences('switchdevice'.$device, false);
262 if ($switched != false) {
269 * Switches the device type we think the user is using to what ever was given.
270 * @param string $newdevice
272 * @throws coding_exception
274 public static function set_user_device_type($newdevice) {
275 $devicetype = self
::get_device_type();
276 if ($newdevice == $devicetype) {
277 unset_user_preference('switchdevice'.$devicetype);
280 $devicetypes = self
::get_device_type_list();
281 if (in_array($newdevice, $devicetypes)) {
282 set_user_preference('switchdevice'.$devicetype, $newdevice);
286 throw new coding_exception('Invalid device type provided to set_user_device_type');
290 * Returns true if the user agent matches the given brand and the version is equal to or greater than that specified.
292 * @param string $brand The branch to check for.
293 * @param scalar $version The version if we need to find out if it is equal to or greater than that specified.
296 public static function check_browser_version($brand, $version = null) {
300 // Internet Explorer.
301 return self
::check_ie_version($version);
304 // Mozilla Firefox browsers.
305 return self
::check_firefox_version($version);
308 return self
::check_chrome_version($version);
312 return self
::check_opera_version($version);
315 // Desktop version of Apple Safari browser - no mobile or touch devices.
316 return self
::check_safari_version($version);
319 // Safari on iPhone, iPad and iPod touch.
320 return self
::check_safari_ios_version($version);
323 // WebKit based browser - everything derived from it (Safari, Chrome, iOS, Android and other mobiles).
324 return self
::check_webkit_version($version);
327 // Gecko based browsers.
328 return self
::check_gecko_version($version);
330 case 'WebKit Android':
331 // WebKit browser on Android.
332 return self
::check_webkit_android_version($version);
335 // OSX browser using Gecke engine.
336 return self
::check_camino_version($version);
338 // Who knows?! doesn't pass anyway.
343 * Checks the user agent is camino based and that the version is equal to or greater than that specified.
345 * Camino browser is at the end of its life, its no longer being developed or supported, just don't worry about it.
347 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
350 protected static function check_camino_version($version = null) {
351 // OSX browser using Gecko engine.
352 $useragent = self
::get_user_agent_string();
353 if ($useragent === false) {
356 if (strpos($useragent, 'Camino') === false) {
359 if (empty($version)) {
360 return true; // No version specified.
362 if (preg_match("/Camino\/([0-9\.]+)/i", $useragent, $match)) {
363 if (version_compare($match[1], $version) >= 0) {
371 * Checks the user agent is Firefox (of any version).
373 * @return bool true if firefox
375 public static function is_firefox() {
376 return self
::check_firefox_version();
380 * Checks the user agent is Firefox based and that the version is equal to or greater than that specified.
382 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
385 public static function check_firefox_version($version = null) {
386 // Mozilla Firefox browsers.
387 $useragent = self
::get_user_agent_string();
388 if ($useragent === false) {
391 if (strpos($useragent, 'Firefox') === false && strpos($useragent, 'Iceweasel') === false) {
394 if (empty($version)) {
395 return true; // No version specified..
397 if (preg_match("/(Iceweasel|Firefox)\/([0-9\.]+)/i", $useragent, $match)) {
398 if (version_compare($match[2], $version) >= 0) {
406 * Checks the user agent is Gecko based (of any version).
408 * @return bool true if Gecko based.
410 public static function is_gecko() {
411 return self
::check_gecko_version();
415 * Checks the user agent is Gecko based and that the version is equal to or greater than that specified.
417 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
420 public static function check_gecko_version($version = null) {
421 // Gecko based browsers.
422 // Do not look for dates any more, we expect real Firefox version here.
423 $useragent = self
::get_user_agent_string();
424 if ($useragent === false) {
427 if (empty($version)) {
429 } else if ($version > 20000000) {
430 // This is just a guess, it is not supposed to be 100% accurate!
431 if (preg_match('/^201/', $version)) {
433 } else if (preg_match('/^200[7-9]/', $version)) {
435 } else if (preg_match('/^2006/', $version)) {
441 if (preg_match("/(Iceweasel|Firefox)\/([0-9\.]+)/i", $useragent, $match)) {
442 // Use real Firefox version if specified in user agent string.
443 if (version_compare($match[2], $version) >= 0) {
446 } else if (preg_match("/Gecko\/([0-9\.]+)/i", $useragent, $match)) {
447 // Gecko might contain date or Firefox revision, let's just guess the Firefox version from the date.
448 $browserver = $match[1];
449 if ($browserver > 20000000) {
450 // This is just a guess, it is not supposed to be 100% accurate!
451 if (preg_match('/^201/', $browserver)) {
453 } else if (preg_match('/^200[7-9]/', $browserver)) {
455 } else if (preg_match('/^2006/', $version)) {
461 if (version_compare($browserver, $version) >= 0) {
469 * Checks the user agent is IE (of any version).
471 * @return bool true if internet exporeer
473 public static function is_ie() {
474 return self
::check_ie_version();
478 * Checks the user agent is IE and returns its main properties:
480 * - whether running in compatibility view.
482 * @return bool|array False if not IE, otherwise an associative array of properties.
484 public static function check_ie_properties() {
485 // Internet Explorer.
486 $useragent = self
::get_user_agent_string();
487 if ($useragent === false) {
490 if (strpos($useragent, 'Opera') !== false) {
494 // See: http://www.useragentstring.com/pages/Internet%20Explorer/.
495 if (preg_match("/MSIE ([0-9\.]+)/", $useragent, $match)) {
496 $browser = $match[1];
497 // See: http://msdn.microsoft.com/en-us/library/ie/bg182625%28v=vs.85%29.aspx for IE11+ useragent details.
498 } else if (preg_match("/Trident\/[0-9\.]+/", $useragent) && preg_match("/rv:([0-9\.]+)/", $useragent, $match)) {
499 $browser = $match[1];
503 $compat_view = false;
504 // IE8 and later versions may pretend to be IE7 for intranet sites, use Trident version instead,
505 // the Trident should always describe the capabilities of IE in any emulation mode.
506 if ($browser === '7.0' and preg_match("/Trident\/([0-9\.]+)/", $useragent, $match)) {
508 $browser = $match[1] +
4; // NOTE: Hopefully this will work also for future IE versions.
510 $browser = round($browser, 1);
512 'version' => $browser,
513 'compatview' => $compat_view
518 * Checks the user agent is IE and that the version is equal to or greater than that specified.
520 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
523 public static function check_ie_version($version = null) {
524 // Internet Explorer.
525 $properties = self
::check_ie_properties();
526 if (!is_array($properties)) {
529 // In case of IE we have to deal with BC of the version parameter.
530 if (is_null($version)) {
531 $version = 5.5; // Anything older is not considered a browser at all!
533 // IE uses simple versions, let's cast it to float to simplify the logic here.
534 $version = round($version, 1);
535 return ($properties['version'] >= $version);
539 * Checks the user agent is IE and that IE is running under Compatibility View setting.
541 * @return bool true if internet explorer runs in Compatibility View mode.
543 public static function check_ie_compatibility_view() {
544 // IE User Agent string when in Compatibility View:
545 // - IE 8: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; ...)".
546 // - IE 9: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; ...)".
547 // - IE 10: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/6.0; ...)".
548 // - IE 11: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; ...)".
550 // - http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx.
551 // - http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx.
552 // - http://blogs.msdn.com/b/ie/archive/2011/04/15/the-ie10-user-agent-string.aspx.
553 // - http://msdn.microsoft.com/en-us/library/ie/hh869301%28v=vs.85%29.aspx.
554 $properties = self
::check_ie_properties();
555 if (!is_array($properties)) {
558 return $properties['compatview'];
562 * Checks the user agent is Opera (of any version).
564 * @return bool true if opera
566 public static function is_opera() {
567 return self
::check_opera_version();
571 * Checks the user agent is Opera and that the version is equal to or greater than that specified.
573 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
576 public static function check_opera_version($version = null) {
578 $useragent = self
::get_user_agent_string();
579 if ($useragent === false) {
582 if (strpos($useragent, 'Opera') === false) {
585 if (empty($version)) {
586 return true; // No version specified.
588 // Recent Opera useragents have Version/ with the actual version, e.g.:
589 // Opera/9.80 (Windows NT 6.1; WOW64; U; en) Presto/2.10.289 Version/12.01
590 // That's Opera 12.01, not 9.8.
591 if (preg_match("/Version\/([0-9\.]+)/i", $useragent, $match)) {
592 if (version_compare($match[1], $version) >= 0) {
595 } else if (preg_match("/Opera\/([0-9\.]+)/i", $useragent, $match)) {
596 if (version_compare($match[1], $version) >= 0) {
604 * Checks the user agent is webkit based
606 * @return bool true if webkit
608 public static function is_webkit() {
609 return self
::check_webkit_version();
613 * Checks the user agent is Webkit based and that the version is equal to or greater than that specified.
615 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
618 public static function check_webkit_version($version = null) {
619 // WebKit based browser - everything derived from it (Safari, Chrome, iOS, Android and other mobiles).
620 $useragent = self
::get_user_agent_string();
621 if ($useragent === false) {
624 if (strpos($useragent, 'AppleWebKit') === false) {
627 if (empty($version)) {
628 return true; // No version specified.
630 if (preg_match("/AppleWebKit\/([0-9.]+)/i", $useragent, $match)) {
631 if (version_compare($match[1], $version) >= 0) {
639 * Checks the user agent is Safari
641 * @return bool true if safari
643 public static function is_safari() {
644 return self
::check_safari_version();
648 * Checks the user agent is Safari based and that the version is equal to or greater than that specified.
650 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
653 public static function check_safari_version($version = null) {
654 // Desktop version of Apple Safari browser - no mobile or touch devices.
655 $useragent = self
::get_user_agent_string();
656 if ($useragent === false) {
659 if (strpos($useragent, 'AppleWebKit') === false) {
662 // Look for AppleWebKit, excluding strings with OmniWeb, Shiira and SymbianOS and any other mobile devices.
663 if (strpos($useragent, 'OmniWeb')) {
667 if (strpos($useragent, 'Shiira')) {
671 if (strpos($useragent, 'SymbianOS')) {
675 if (strpos($useragent, 'Android')) {
676 // Reject Androids too.
679 if (strpos($useragent, 'iPhone') or strpos($useragent, 'iPad') or strpos($useragent, 'iPod')) {
680 // No Apple mobile devices here - editor does not work, course ajax is not touch compatible, etc.
683 if (strpos($useragent, 'Chrome')) { // Reject chrome browsers - it needs to be tested explicitly.
687 if (empty($version)) {
688 return true; // No version specified.
690 if (preg_match("/AppleWebKit\/([0-9.]+)/i", $useragent, $match)) {
691 if (version_compare($match[1], $version) >= 0) {
699 * Checks the user agent is Chrome
701 * @return bool true if chrome
703 public static function is_chrome() {
704 return self
::check_chrome_version();
708 * Checks the user agent is Chrome based and that the version is equal to or greater than that specified.
710 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
713 public static function check_chrome_version($version = null) {
715 $useragent = self
::get_user_agent_string();
716 if ($useragent === false) {
719 if (strpos($useragent, 'Chrome') === false) {
722 if (empty($version)) {
723 return true; // No version specified.
725 if (preg_match("/Chrome\/(.*)[ ]+/i", $useragent, $match)) {
726 if (version_compare($match[1], $version) >= 0) {
734 * Checks the user agent is webkit android based.
736 * @return bool true if webkit based and on Android
738 public static function is_webkit_android() {
739 return self
::check_webkit_android_version();
743 * Checks the user agent is Webkit based and on Android and that the version is equal to or greater than that specified.
745 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
748 public static function check_webkit_android_version($version = null) {
749 // WebKit browser on Android.
750 $useragent = self
::get_user_agent_string();
751 if ($useragent === false) {
754 if (strpos($useragent, 'Linux; U; Android') === false) {
757 if (empty($version)) {
758 return true; // No version specified.
760 if (preg_match("/AppleWebKit\/([0-9]+)/i", $useragent, $match)) {
761 if (version_compare($match[1], $version) >= 0) {
769 * Checks the user agent is Safari on iOS
771 * @return bool true if Safari on iOS
773 public static function is_safari_ios() {
774 return self
::check_safari_ios_version();
778 * Checks the user agent is Safari on iOS and that the version is equal to or greater than that specified.
780 * @param string|int $version A version to check for, returns true if its equal to or greater than that specified.
783 public static function check_safari_ios_version($version = null) {
784 // Safari on iPhone, iPad and iPod touch.
785 $useragent = self
::get_user_agent_string();
786 if ($useragent === false) {
789 if (strpos($useragent, 'AppleWebKit') === false or strpos($useragent, 'Safari') === false) {
792 if (!strpos($useragent, 'iPhone') and !strpos($useragent, 'iPad') and !strpos($useragent, 'iPod')) {
795 if (empty($version)) {
796 return true; // No version specified.
798 if (preg_match("/AppleWebKit\/([0-9]+)/i", $useragent, $match)) {
799 if (version_compare($match[1], $version) >= 0) {
807 * Check if the user agent matches a given brand.
809 * Known brand: 'Windows','Linux','Macintosh','SGI','SunOS','HP-UX'
811 * @param string $brand
814 public static function check_browser_operating_system($brand) {
815 $useragent = self
::get_user_agent_string();
816 return ($useragent !== false && preg_match("/$brand/i", $useragent));
820 * Gets an array of CSS classes to represent the user agent.
823 public static function get_browser_version_classes() {
827 for ($i = 12; $i >= 6; $i--) {
828 if (self
::check_ie_version($i)) {
829 $classes[] = 'ie'.$i;
833 } else if (self
::is_firefox() || self
::is_gecko() || self
::check_camino_version()) {
834 $classes[] = 'gecko';
835 if (preg_match('/rv\:([1-2])\.([0-9])/', self
::get_user_agent_string(), $matches)) {
836 $classes[] = "gecko{$matches[1]}{$matches[2]}";
838 } else if (self
::is_webkit()) {
839 $classes[] = 'safari';
840 if (self
::is_safari_ios()) {
842 } else if (self
::is_webkit_android()) {
843 $classes[] = 'android';
845 } else if (self
::is_opera()) {
846 $classes[] = 'opera';
852 * Returns true if the user agent supports the display of SVG images.
856 public static function supports_svg() {
857 // IE 5 - 8 don't support SVG at all.
858 $instance = self
::instance();
859 if ($instance->supportssvg
=== null) {
860 if ($instance->useragent
=== false) {
861 // Can't be sure, just say no.
862 $instance->supportssvg
= false;
863 } else if (self
::is_ie() and !self
::check_ie_version('9')) {
864 // IE < 9 doesn't support SVG. Say no.
865 $instance->supportssvg
= false;
866 } else if (preg_match('#Android +[0-2]\.#', $instance->useragent
)) {
867 // Android < 3 doesn't support SVG. Say no.
868 $instance->supportssvg
= false;
869 } else if (self
::is_opera()) {
870 // Opera 12 still does not support SVG well enough. Say no.
871 $instance->supportssvg
= false;
874 $instance->supportssvg
= true;
877 return $instance->supportssvg
;
881 * Returns true if the user agent supports the MIME media type for JSON text, as defined in RFC 4627.
885 public static function supports_json_contenttype() {
886 // Modern browsers other than IE correctly supports 'application/json' media type.
887 if (!self
::is_ie()) {
891 // IE8+ supports 'application/json' media type, when NOT in Compatibility View mode.
893 // - http://blogs.msdn.com/b/ie/archive/2008/09/10/native-json-in-ie8.aspx;
894 // - MDL-39810: issues when using 'text/plain' in Compatibility View for the body of an HTTP POST response.
895 if (self
::check_ie_version(8) && !self
::check_ie_compatibility_view()) {
899 // This browser does not support json.