Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

Provides the {@link core_form\filetypes_util} class.

Copyright: 2017 David Mudrák <david@moodle.com>
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 525 lines (18 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: filetypes_util  - X-Ref

Utility class for handling with file types in the forms.

This class is supposed to serve as a helper class for {@link MoodleQuickForm_filetypes}
and {@link admin_setting_filetypes} classes.

The file types can be specified in a syntax compatible with what filepicker
and filemanager support via the "accepted_types" option: a list of extensions
(e.g. ".doc"), mimetypes ("image/png") or groups ("audio").

normalize_file_types($types)   X-Ref
Converts the argument into an array (list) of file types.

The list can be separated by whitespace, end of lines, commas, colons and semicolons.
Empty values are not returned. Values are converted to lowercase.
Duplicates are removed. Glob evaluation is not supported.

The return value can be used as the accepted_types option for the filepicker.

param: string|array $types List of file extensions, groups or mimetypes
return: array of strings

looks_like_mimetype($type)   X-Ref
Does the given file type looks like a valid MIME type?

This does not check of the MIME type is actually registered here/known.

param: string $type
return: bool

is_filetype_group($type)   X-Ref
Is the given string a known filetype group?

param: string $type
return: bool|object false or the group info

get_groups_info()   X-Ref
Provides a list of all known file type groups and their properties.

return: array

get_group_description($group)   X-Ref
Return a human readable name of the filetype group.

param: string $group
return: string

describe_file_types($types)   X-Ref
Describe the list of file types for human user.

Given the list of file types, return a list of human readable
descriptive names of relevant groups, types or file formats.

param: string|array $types
return: object

data_for_browser($onlytypes=null, $allowall=true, $current=null)   X-Ref
Prepares data for the filetypes-browser.mustache

param: string|array $onlytypes Allow selection from these file types only; for example 'web_image'.
param: bool $allowall Allow to select 'All file types'. Does not apply with onlytypes are set.
param: string|array $current Current values that should be selected.
return: object

expand($types, $keepgroups=false, $keepmimetypes=false)   X-Ref
Expands the file types into the list of file extensions.

The groups and mimetypes are expanded into the list of their associated file
extensions. Depending on the $keepgroups and $keepmimetypes, the groups
and mimetypes themselves are either kept in the list or removed.

param: string|array $types
param: bool $keepgroups Keep the group item in the list after expansion
param: bool $keepmimetypes Keep the mimetype item in the list after expansion
return: array list of extensions and eventually groups and types

is_whitelisted($types, $whitelist)   X-Ref
Should the given file type be considered as a part of the given whitelist.

If multiple types are provided, all of them must be part of the
whitelist. Empty type is part of any whitelist. Any type is part of an
empty whitelist.

param: string|array $types File types to be checked
param: string|array $whitelist An array or string of whitelisted types
return: boolean

get_not_whitelisted($types, $whitelist)   X-Ref
Returns all types that are not part of the give whitelist.

This is similar check to the {@link self::is_whitelisted()} but this one
actually returns the extra types.

param: string|array $types File types to be checked
param: string|array $whitelist An array or string of whitelisted types
return: array Types not present in the whitelist

is_allowed_file_type($filename, $whitelist)   X-Ref
Is the given filename of an allowed file type?

Empty whitelist is interpretted as "any file type is allowed" rather
than "no file can be uploaded".

param: string $filename the file name
param: string|array $whitelist list of allowed file extensions
return: boolean True if the file type is allowed, false if not

get_unknown_file_types($types)   X-Ref
Returns file types from the list that are not recognized

param: string|array $types list of user-defined file types
return: array A list of unknown file types.