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 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

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: 537 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: array

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_listed($types, $list)   X-Ref
Should the file type be considered as a part of the given list.

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

param: string|array $types File type or list of types to be checked.
param: string|array $list An array or string listing the types to check against.
return: boolean

is_whitelisted()   X-Ref


get_not_listed($types, $list)   X-Ref
Returns all types that are not part of the given list.

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

param: string|array $types File type or list of types to be checked.
param: string|array $list An array or string listing the types to check against.
return: array Types not present in the list.

get_not_whitelisted()   X-Ref


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

Empty allowlist is interpreted as "any file type is allowed" rather
than "no file can be uploaded".

param: string $filename the file name
param: string|array $allowlist 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.