Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 402 and 403]

Environment class to aid with the detection and establishment of the working environment.

Copyright: 2013 Sam Hemelryk
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 1220 lines (46 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: core_useragent  - X-Ref

The user agent class.

It's important to note that we do not like browser sniffing and its use in core code is highly discouraged.
No new uses of this API will be integrated unless there is absolutely no alternative.

This API supports the few browser checks we do have in core, all of which one day will hopefully be removed.
The API will remain to support any third party use out there, however at some point like all code it will be deprecated.

Use sparingly and only with good cause!

instance($reload = false, $forceuseragent = null)   X-Ref
Get an instance of the user agent object.

param: bool $reload If set to true the user agent will be reset and all ascertations remade.
param: string $forceuseragent The string to force as the user agent, don't use unless absolutely unavoidable.
return: core_useragent

__construct($forceuseragent = null)   X-Ref
Constructs a new user agent object. Publically you must use the instance method above.

param: string|null $forceuseragent Optional a user agent to force.

get_moodlebot_useragent()   X-Ref
Get the MoodleBot UserAgent for this site.

return: string UserAgent

get_user_agent_string()   X-Ref
Returns the user agent string.

return: bool|string The user agent string or false if one isn't available.

get_device_type()   X-Ref
Returns the device type we believe is being used.

return: string

guess_device_type()   X-Ref
Guesses the device type the user agent is running on.

return: string

is_useragent_mobile()   X-Ref
Returns true if the user appears to be on a mobile device.

return: bool

is_useragent_tablet()   X-Ref
Returns true if the user appears to be on a tablet.

return: int

is_useragent_web_crawler()   X-Ref
Whether the user agent relates to a web crawler.
This includes all types of web crawler.

return: bool

get_device_type_list($includecustomtypes = true)   X-Ref
Gets a list of known device types.

param: bool $includecustomtypes If set to true we'll include types that have been added by the admin.
return: array

get_device_type_theme($devicetype = null)   X-Ref
Returns the theme to use for the given device type.

This used to be get_selected_theme_for_device_type.
param: null|string $devicetype The device type to find out for. Defaults to the device the user is using,
return: bool

get_device_type_cfg_var_name($devicetype = null)   X-Ref
Returns the CFG var used to find the theme to use for the given device.

Used to be get_device_cfg_var_name.

param: null|string $devicetype The device type to find out for. Defaults to the device the user is using,
return: string

get_user_device_type()   X-Ref
Gets the device type the user is currently using.

return: string

set_user_device_type($newdevice)   X-Ref
Switches the device type we think the user is using to what ever was given.

param: string $newdevice
return: bool

check_browser_version($brand, $version = null)   X-Ref
Returns true if the user agent matches the given brand and the version is equal to or greater than that specified.

param: string $brand The branch to check for.
param: scalar $version The version if we need to find out if it is equal to or greater than that specified.
return: bool

check_camino_version($version = null)   X-Ref
Checks the user agent is camino based and that the version is equal to or greater than that specified.

Camino browser is at the end of its life, its no longer being developed or supported, just don't worry about it.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_firefox()   X-Ref
Checks the user agent is Firefox (of any version).

return: bool true if firefox

check_firefox_version($version = null)   X-Ref
Checks the user agent is Firefox based and that the version is equal to or greater than that specified.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_gecko()   X-Ref
Checks the user agent is Gecko based (of any version).

return: bool true if Gecko based.

check_gecko_version($version = null)   X-Ref
Checks the user agent is Gecko based and that the version is equal to or greater than that specified.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_edge()   X-Ref
Checks the user agent is Edge (of any version).

return: bool true if Edge

check_edge_version($version = null)   X-Ref
Check the User Agent for the version of Edge.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_ie()   X-Ref
Checks the user agent is IE (of any version).

return: bool true if internet exporeer

check_ie_properties()   X-Ref
Checks the user agent is IE and returns its main properties:
- browser version;
- whether running in compatibility view.

return: bool|array False if not IE, otherwise an associative array of properties.

check_ie_version($version = null)   X-Ref
Checks the user agent is IE and that the version is equal to or greater than that specified.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

check_ie_compatibility_view()   X-Ref
Checks the user agent is IE and that IE is running under Compatibility View setting.

return: bool true if internet explorer runs in Compatibility View mode.

is_opera()   X-Ref
Checks the user agent is Opera (of any version).

return: bool true if opera

check_opera_version($version = null)   X-Ref
Checks the user agent is Opera and that the version is equal to or greater than that specified.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_webkit()   X-Ref
Checks the user agent is webkit based

return: bool true if webkit

check_webkit_version($version = null)   X-Ref
Checks the user agent is Webkit based and that the version is equal to or greater than that specified.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_safari()   X-Ref
Checks the user agent is Safari

return: bool true if safari

check_safari_version($version = null)   X-Ref
Checks the user agent is Safari based and that the version is equal to or greater than that specified.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_chrome()   X-Ref
Checks the user agent is Chrome

return: bool true if chrome

check_chrome_version($version = null)   X-Ref
Checks the user agent is Chrome based and that the version is equal to or greater than that specified.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_webkit_android()   X-Ref
Checks the user agent is webkit android based.

return: bool true if webkit based and on Android

check_webkit_android_version($version = null)   X-Ref
Checks the user agent is Webkit based and on Android and that the version is equal to or greater than that specified.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_safari_ios()   X-Ref
Checks the user agent is Safari on iOS

return: bool true if Safari on iOS

check_safari_ios_version($version = null)   X-Ref
Checks the user agent is Safari on iOS and that the version is equal to or greater than that specified.

param: string|int $version A version to check for, returns true if its equal to or greater than that specified.
return: bool

is_msword()   X-Ref
Checks if the user agent is MS Word.
Not perfect, as older versions of Word use standard IE6/7 user agents without any identifying traits.

return: bool true if user agent could be identified as MS Word.

check_browser_operating_system($brand)   X-Ref
Check if the user agent matches a given brand.

Known brand: 'Windows','Linux','Macintosh','SGI','SunOS','HP-UX'

param: string $brand
return: bool

get_browser_version_classes()   X-Ref
Gets an array of CSS classes to represent the user agent.

return: array

supports_svg()   X-Ref
Returns true if the user agent supports the display of SVG images.

return: bool

supports_json_contenttype()   X-Ref
Returns true if the user agent supports the MIME media type for JSON text, as defined in RFC 4627.

return: bool

is_web_crawler()   X-Ref
Returns true if the client appears to be some kind of web crawler.
This may include other types of crawler.

return: bool

is_ios($version = null)   X-Ref
Returns true if the client appears to be a device using iOS (iPhone, iPad, iPod).

param: scalar $version The version if we need to find out if it is equal to or greater than that specified.
return: bool true if the client is using iOS

is_moodle_app()   X-Ref
Returns true if the client appears to be the Moodle app (or an app based on the Moodle app code).

return: bool true if the client is the Moodle app

supports_html5($extension)   X-Ref
Checks if current browser supports files with give extension as <video> or <audio> source

Note, the check here is not 100% accurate!

First, we do not know which codec is used in .mp4 or .webm files. Not all browsers support
all codecs.

Also we assume that users of Firefox/Chrome/Safari do not use the ancient versions of browsers.

We check the exact version for IE/Edge though. We know that there are still users of very old
versions that are afraid to upgrade or have slow IT department.

Resources:
https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats
https://en.wikipedia.org/wiki/HTML5_video
https://en.wikipedia.org/wiki/HTML5_Audio

param: string $extension extension without leading .
return: bool

supports_media_source_extensions(string $extension)   X-Ref
Checks if current browser supports the HLS and MPEG-DASH media
streaming formats. Most browsers get this from Media Source Extensions.
Safari on iOS, doesn't support MPEG-DASH at all.

Note, the check here is not 100% accurate!

Also we assume that users of Firefox/Chrome/Safari do not use the ancient versions of browsers.
We check the exact version for IE/Edge though. We know that there are still users of very old
versions that are afraid to upgrade or have slow IT department.

Resources:
https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API
https://caniuse.com/#search=mpeg-dash
https://caniuse.com/#search=hls

param: string $extension
return: bool