Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
core_useragent Class Reference

The user agent class. More...

Static Public Member Functions

static check_browser_operating_system ($brand)
 Check if the user agent matches a given brand.
 
static check_browser_version ($brand, $version=null)
 Returns true if the user agent matches the given brand and the version is equal to or greater than that specified.
 
static check_chrome_version ($version=null)
 Checks the user agent is Chrome based and that the version is equal to or greater than that specified.
 
static check_edge_version ($version=null)
 Check the User Agent for the version of Edge.
 
static check_firefox_version ($version=null)
 Checks the user agent is Firefox based and that the version is equal to or greater than that specified.
 
static check_gecko_version ($version=null)
 Checks the user agent is Gecko based and that the version is equal to or greater than that specified.
 
static check_ie_compatibility_view ()
 Checks the user agent is IE and that IE is running under Compatibility View setting.
 
static check_ie_properties ()
 Checks the user agent is IE and returns its main properties:
 
static check_ie_version ($version=null)
 Checks the user agent is IE and that the version is equal to or greater than that specified.
 
static check_opera_version ($version=null)
 Checks the user agent is Opera and that the version is equal to or greater than that specified.
 
static check_safari_ios_version ($version=null)
 Checks the user agent is Safari on iOS and that the version is equal to or greater than that specified.
 
static check_safari_version ($version=null)
 Checks the user agent is Safari based and that the version is equal to or greater than that specified.
 
static check_webkit_android_version ($version=null)
 Checks the user agent is Webkit based and on Android and that the version is equal to or greater than that specified.
 
static check_webkit_version ($version=null)
 Checks the user agent is Webkit based and that the version is equal to or greater than that specified.
 
static get_browser_version_classes ()
 Gets an array of CSS classes to represent the user agent.
 
static get_device_type ()
 Returns the device type we believe is being used.
 
static get_device_type_cfg_var_name ($devicetype=null)
 Returns the CFG var used to find the theme to use for the given device.
 
static get_device_type_list ($includecustomtypes=true)
 Gets a list of known device types.
 
static get_device_type_theme ($devicetype=null)
 Returns the theme to use for the given device type.
 
static get_moodlebot_useragent ()
 Get the MoodleBot UserAgent for this site.
 
static get_user_agent_string ()
 Returns the user agent string.
 
static get_user_device_type ()
 Gets the device type the user is currently using.
 
static instance ($reload=false, $forceuseragent=null)
 Get an instance of the user agent object.
 
static is_chrome ()
 Checks the user agent is Chrome.
 
static is_edge ()
 Checks the user agent is Edge (of any version).
 
static is_firefox ()
 Checks the user agent is Firefox (of any version).
 
static is_gecko ()
 Checks the user agent is Gecko based (of any version).
 
static is_ie ()
 Checks the user agent is IE (of any version).
 
static is_ios ($version=null)
 Returns true if the client appears to be a device using iOS (iPhone, iPad, iPod).
 
static is_moodle_app ()
 Returns true if the client appears to be the Moodle app (or an app based on the Moodle app code).
 
static is_msword ()
 Checks if the user agent is MS Word.
 
static is_opera ()
 Checks the user agent is Opera (of any version).
 
static is_safari ()
 Checks the user agent is Safari.
 
static is_safari_ios ()
 Checks the user agent is Safari on iOS.
 
static is_web_crawler ()
 Returns true if the client appears to be some kind of web crawler.
 
static is_webkit ()
 Checks the user agent is webkit based.
 
static is_webkit_android ()
 Checks the user agent is webkit android based.
 
static set_user_device_type ($newdevice)
 Switches the device type we think the user is using to what ever was given.
 
static supports_html5 ($extension)
 Checks if current browser supports files with give extension as <video> or <audio> source.
 
static supports_json_contenttype ()
 Returns true if the user agent supports the MIME media type for JSON text, as defined in RFC 4627.
 
static supports_media_source_extensions (string $extension)
 Checks if current browser supports the HLS and MPEG-DASH media streaming formats.
 
static supports_svg ()
 Returns true if the user agent supports the display of SVG images.
 

Public Attributes

const DEVICETYPE_DEFAULT = 'default'
 The default for devices, think of as a computer.
 
const DEVICETYPE_LEGACY = 'legacy'
 Legacy devices, or at least legacy browsers.
 
const DEVICETYPE_MOBILE = 'mobile'
 Mobile devices like your cell phone or hand held gaming device.
 
const DEVICETYPE_TABLET = 'tablet'
 Tables, larger than hand held, but still easily portable and smaller than a laptop.
 

Static Public Attributes

static array $devicetypes
 The device types we track.
 

Protected Member Functions

 __construct ($forceuseragent=null)
 Constructs a new user agent object.
 
 guess_device_type ()
 Guesses the device type the user agent is running on.
 
 is_useragent_mobile ()
 Returns true if the user appears to be on a mobile device.
 
 is_useragent_tablet ()
 Returns true if the user appears to be on a tablet.
 
 is_useragent_web_crawler ()
 Whether the user agent relates to a web crawler.
 

Static Protected Member Functions

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

Protected Attributes

string $devicetype = null
 The users device type, one of self\DEVICETYPE_*.
 
array $devicetypecustoms = array()
 Custom device types entered into the admin interface.
 
bool null $supportssvg = null
 True if the user agent supports the display of svg images.
 
string bool null $useragent = null
 The current requests user agent string if there was one.
 

Static Protected Attributes

static core_useragent $instance = null
 An instance of this class.
 

Detailed Description

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!

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Constructor & Destructor Documentation

◆ __construct()

core_useragent::__construct ( $forceuseragent = null)
protected

Constructs a new user agent object.

Publically you must use the instance method above.

Parameters
string | null$forceuseragentOptional a user agent to force.

Member Function Documentation

◆ check_browser_operating_system()

static core_useragent::check_browser_operating_system ( $brand)
static

Check if the user agent matches a given brand.

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

Parameters
string$brand
Return values
bool

◆ check_browser_version()

static core_useragent::check_browser_version ( $brand,
$version = null )
static

Returns true if the user agent matches the given brand and the version is equal to or greater than that specified.

Parameters
string$brandThe branch to check for.
scalar$versionThe version if we need to find out if it is equal to or greater than that specified.
Return values
bool

◆ check_camino_version()

static core_useragent::check_camino_version ( $version = null)
staticprotected

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.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_chrome_version()

static core_useragent::check_chrome_version ( $version = null)
static

Checks the user agent is Chrome based and that the version is equal to or greater than that specified.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_edge_version()

static core_useragent::check_edge_version ( $version = null)
static

Check the User Agent for the version of Edge.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_firefox_version()

static core_useragent::check_firefox_version ( $version = null)
static

Checks the user agent is Firefox based and that the version is equal to or greater than that specified.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_gecko_version()

static core_useragent::check_gecko_version ( $version = null)
static

Checks the user agent is Gecko based and that the version is equal to or greater than that specified.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_ie_compatibility_view()

static core_useragent::check_ie_compatibility_view ( )
static

Checks the user agent is IE and that IE is running under Compatibility View setting.

Return values
booltrue if internet explorer runs in Compatibility View mode.

◆ check_ie_properties()

static core_useragent::check_ie_properties ( )
static

Checks the user agent is IE and returns its main properties:

  • browser version;
  • whether running in compatibility view.
Return values
bool|arrayFalse if not IE, otherwise an associative array of properties.

◆ check_ie_version()

static core_useragent::check_ie_version ( $version = null)
static

Checks the user agent is IE and that the version is equal to or greater than that specified.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_opera_version()

static core_useragent::check_opera_version ( $version = null)
static

Checks the user agent is Opera and that the version is equal to or greater than that specified.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_safari_ios_version()

static core_useragent::check_safari_ios_version ( $version = null)
static

Checks the user agent is Safari on iOS and that the version is equal to or greater than that specified.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_safari_version()

static core_useragent::check_safari_version ( $version = null)
static

Checks the user agent is Safari based and that the version is equal to or greater than that specified.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_webkit_android_version()

static core_useragent::check_webkit_android_version ( $version = null)
static

Checks the user agent is Webkit based and on Android and that the version is equal to or greater than that specified.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ check_webkit_version()

static core_useragent::check_webkit_version ( $version = null)
static

Checks the user agent is Webkit based and that the version is equal to or greater than that specified.

Parameters
string | int$versionA version to check for, returns true if its equal to or greater than that specified.
Return values
bool

◆ get_browser_version_classes()

static core_useragent::get_browser_version_classes ( )
static

Gets an array of CSS classes to represent the user agent.

Return values
array

◆ get_device_type()

static core_useragent::get_device_type ( )
static

Returns the device type we believe is being used.

Return values
string

◆ get_device_type_cfg_var_name()

static core_useragent::get_device_type_cfg_var_name ( $devicetype = null)
static

Returns the CFG var used to find the theme to use for the given device.

Used to be get_device_cfg_var_name.

Parameters
null | string$devicetypeThe device type to find out for. Defaults to the device the user is using,
Return values
string

◆ get_device_type_list()

static core_useragent::get_device_type_list ( $includecustomtypes = true)
static

Gets a list of known device types.

Parameters
bool$includecustomtypesIf set to true we'll include types that have been added by the admin.
Return values
array

◆ get_device_type_theme()

static core_useragent::get_device_type_theme ( $devicetype = null)
static

Returns the theme to use for the given device type.

This used to be get_selected_theme_for_device_type.

Parameters
null | string$devicetypeThe device type to find out for. Defaults to the device the user is using,
Return values
bool

◆ get_moodlebot_useragent()

static core_useragent::get_moodlebot_useragent ( )
static

Get the MoodleBot UserAgent for this site.

Return values
stringUserAgent

◆ get_user_agent_string()

static core_useragent::get_user_agent_string ( )
static

Returns the user agent string.

Return values
bool|stringThe user agent string or false if one isn't available.

◆ get_user_device_type()

static core_useragent::get_user_device_type ( )
static

Gets the device type the user is currently using.

Return values
string

◆ guess_device_type()

core_useragent::guess_device_type ( )
protected

Guesses the device type the user agent is running on.

Return values
string

◆ instance()

static core_useragent::instance ( $reload = false,
$forceuseragent = null )
static

Get an instance of the user agent object.

Parameters
bool$reloadIf set to true the user agent will be reset and all ascertations remade.
string$forceuseragentThe string to force as the user agent, don't use unless absolutely unavoidable.
Return values
core_useragent

◆ is_chrome()

static core_useragent::is_chrome ( )
static

Checks the user agent is Chrome.

Return values
booltrue if chrome

◆ is_edge()

static core_useragent::is_edge ( )
static

Checks the user agent is Edge (of any version).

Return values
booltrue if Edge

◆ is_firefox()

static core_useragent::is_firefox ( )
static

Checks the user agent is Firefox (of any version).

Return values
booltrue if firefox

◆ is_gecko()

static core_useragent::is_gecko ( )
static

Checks the user agent is Gecko based (of any version).

Return values
booltrue if Gecko based.

◆ is_ie()

static core_useragent::is_ie ( )
static

Checks the user agent is IE (of any version).

Return values
booltrue if internet exporeer

◆ is_ios()

static core_useragent::is_ios ( $version = null)
static

Returns true if the client appears to be a device using iOS (iPhone, iPad, iPod).

Parameters
scalar$versionThe version if we need to find out if it is equal to or greater than that specified.
Return values
booltrue if the client is using iOS
Since
Moodle 3.2

◆ is_moodle_app()

static core_useragent::is_moodle_app ( )
static

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

Return values
booltrue if the client is the Moodle app
Since
Moodle 3.7

◆ is_msword()

static core_useragent::is_msword ( )
static

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 values
booltrue if user agent could be identified as MS Word.

◆ is_opera()

static core_useragent::is_opera ( )
static

Checks the user agent is Opera (of any version).

Return values
booltrue if opera

◆ is_safari()

static core_useragent::is_safari ( )
static

Checks the user agent is Safari.

Return values
booltrue if safari

◆ is_safari_ios()

static core_useragent::is_safari_ios ( )
static

Checks the user agent is Safari on iOS.

Return values
booltrue if Safari on iOS

◆ is_useragent_mobile()

core_useragent::is_useragent_mobile ( )
protected

Returns true if the user appears to be on a mobile device.

Return values
bool

◆ is_useragent_tablet()

core_useragent::is_useragent_tablet ( )
protected

Returns true if the user appears to be on a tablet.

Return values
int

◆ is_useragent_web_crawler()

core_useragent::is_useragent_web_crawler ( )
protected

Whether the user agent relates to a web crawler.

This includes all types of web crawler.

Return values
bool

◆ is_web_crawler()

static core_useragent::is_web_crawler ( )
static

Returns true if the client appears to be some kind of web crawler.

This may include other types of crawler.

Return values
bool

◆ is_webkit()

static core_useragent::is_webkit ( )
static

Checks the user agent is webkit based.

Return values
booltrue if webkit

◆ is_webkit_android()

static core_useragent::is_webkit_android ( )
static

Checks the user agent is webkit android based.

Return values
booltrue if webkit based and on Android

◆ set_user_device_type()

static core_useragent::set_user_device_type ( $newdevice)
static

Switches the device type we think the user is using to what ever was given.

Parameters
string$newdevice
Return values
bool
Exceptions
coding_exception

◆ supports_html5()

static core_useragent::supports_html5 ( $extension)
static

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

Parameters
string$extensionextension without leading .
Return values
bool

◆ supports_json_contenttype()

static core_useragent::supports_json_contenttype ( )
static

Returns true if the user agent supports the MIME media type for JSON text, as defined in RFC 4627.

Return values
bool

◆ supports_media_source_extensions()

static core_useragent::supports_media_source_extensions ( string $extension)
static

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

Parameters
string$extension
Return values
bool

◆ supports_svg()

static core_useragent::supports_svg ( )
static

Returns true if the user agent supports the display of SVG images.

Return values
bool

Member Data Documentation

◆ $devicetype

string core_useragent::$devicetype = null
protected

The users device type, one of self\DEVICETYPE_*.

null until initialised

◆ $devicetypes

array core_useragent::$devicetypes
static
Initial value:
= array(
self::DEVICETYPE_DEFAULT,
self::DEVICETYPE_LEGACY,
self::DEVICETYPE_MOBILE,
self::DEVICETYPE_TABLET,
)

The device types we track.

◆ $supportssvg

bool null core_useragent::$supportssvg = null
protected

True if the user agent supports the display of svg images.

False if not. Null until initialised, then true or false.

◆ $useragent

string bool null core_useragent::$useragent = null
protected

The current requests user agent string if there was one.

Null until initialised, false if none available, or string when available.

◆ DEVICETYPE_LEGACY

const core_useragent::DEVICETYPE_LEGACY = 'legacy'

Legacy devices, or at least legacy browsers.

These are older devices/browsers that don't support standards.


The documentation for this class was generated from the following file: