Moodle APIs  3.8
Moodle 3.8.6 (Build: 20201109)
Public Member Functions | Protected Attributes | List of all members
core_form\filetypes_util Class Reference

Utility class for handling with file types in the forms. More...

Public Member Functions

 data_for_browser ($onlytypes=null, $allowall=true, $current=null)
 Prepares data for the filetypes-browser.mustache. More...
 
 describe_file_types ($types)
 Describe the list of file types for human user. More...
 
 expand ($types, $keepgroups=false, $keepmimetypes=false)
 Expands the file types into the list of file extensions. More...
 
 get_group_description ($group)
 Return a human readable name of the filetype group. More...
 
 get_groups_info ()
 Provides a list of all known file type groups and their properties. More...
 
 get_not_whitelisted ($types, $whitelist)
 Returns all types that are not part of the give whitelist. More...
 
 get_unknown_file_types ($types)
 Returns file types from the list that are not recognized. More...
 
 is_allowed_file_type ($filename, $whitelist)
 Is the given filename of an allowed file type? More...
 
 is_filetype_group ($type)
 Is the given string a known filetype group? More...
 
 is_whitelisted ($types, $whitelist)
 Should the given file type be considered as a part of the given whitelist. More...
 
 looks_like_mimetype ($type)
 Does the given file type looks like a valid MIME type? More...
 
 normalize_file_types ($types)
 Converts the argument into an array (list) of file types. More...
 

Protected Attributes

array $cachegroups = null
 Cache of all file type groups for the self::get_groups_info().
 

Detailed Description

Utility class for handling with file types in the forms.

This class is supposed to serve as a helper class for } * and 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").

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

Member Function Documentation

◆ data_for_browser()

core_form\filetypes_util::data_for_browser (   $onlytypes = null,
  $allowall = true,
  $current = null 
)

Prepares data for the filetypes-browser.mustache.

Parameters
string | array$onlytypesAllow selection from these file types only; for example 'web_image'.
bool$allowallAllow to select 'All file types'. Does not apply with onlytypes are set.
string | array$currentCurrent values that should be selected.
Return values
object

◆ describe_file_types()

core_form\filetypes_util::describe_file_types (   $types)

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.

Parameters
string | array$types
Return values
object

◆ expand()

core_form\filetypes_util::expand (   $types,
  $keepgroups = false,
  $keepmimetypes = false 
)

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.

Parameters
string | array$types
bool$keepgroupsKeep the group item in the list after expansion
bool$keepmimetypesKeep the mimetype item in the list after expansion
Return values
arraylist of extensions and eventually groups and types

◆ get_group_description()

core_form\filetypes_util::get_group_description (   $group)

Return a human readable name of the filetype group.

Parameters
string$group
Return values
string

◆ get_groups_info()

core_form\filetypes_util::get_groups_info ( )

Provides a list of all known file type groups and their properties.

Return values
array

◆ get_not_whitelisted()

core_form\filetypes_util::get_not_whitelisted (   $types,
  $whitelist 
)

Returns all types that are not part of the give whitelist.

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

Parameters
string | array$typesFile types to be checked
string | array$whitelistAn array or string of whitelisted types
Return values
arrayTypes not present in the whitelist

◆ get_unknown_file_types()

core_form\filetypes_util::get_unknown_file_types (   $types)

Returns file types from the list that are not recognized.

Parameters
string | array$typeslist of user-defined file types
Return values
arrayA list of unknown file types.

◆ is_allowed_file_type()

core_form\filetypes_util::is_allowed_file_type (   $filename,
  $whitelist 
)

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".

Parameters
string$filenamethe file name
string | array$whitelistlist of allowed file extensions
Return values
booleanTrue if the file type is allowed, false if not

◆ is_filetype_group()

core_form\filetypes_util::is_filetype_group (   $type)

Is the given string a known filetype group?

Parameters
string$type
Return values
bool|objectfalse or the group info

◆ is_whitelisted()

core_form\filetypes_util::is_whitelisted (   $types,
  $whitelist 
)

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.

Parameters
string | array$typesFile types to be checked
string | array$whitelistAn array or string of whitelisted types
Return values
boolean

◆ looks_like_mimetype()

core_form\filetypes_util::looks_like_mimetype (   $type)

Does the given file type looks like a valid MIME type?

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

Parameters
string$type
Return values
bool

◆ normalize_file_types()

core_form\filetypes_util::normalize_file_types (   $types)

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.

Parameters
string | array$typesList of file extensions, groups or mimetypes
Return values
arrayof strings

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