first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+149
View File
@@ -0,0 +1,149 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_form;
use context;
use core_external\external_api;
use moodle_url;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/formslib.php');
/**
* Class modal
*
* Extend this class to create a form that can be used in a modal dialogue.
*
* @package core_form
* @copyright 2020 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class dynamic_form extends \moodleform {
/**
* Constructor for modal forms can not be overridden, however the same form can be used both in AJAX and normally
*
* @param string $action
* @param array $customdata
* @param string $method
* @param string $target
* @param array $attributes
* @param bool $editable
* @param array $ajaxformdata Forms submitted via ajax, must pass their data here, instead of relying on _GET and _POST.
* @param bool $isajaxsubmission whether the form is called from WS and it needs to validate user access and set up context
*/
final public function __construct(
?string $action = null,
?array $customdata = null,
string $method = 'post',
string $target = '',
?array $attributes = [],
bool $editable = true,
?array $ajaxformdata = null,
bool $isajaxsubmission = false
) {
global $PAGE, $CFG;
$this->_ajaxformdata = $ajaxformdata;
if ($isajaxsubmission) {
// This form was created from the WS that needs to validate user access to it and set page context.
// It has to be done before calling parent constructor because elements definitions may need to use
// format_string functions and other methods that expect the page to be set up.
external_api::validate_context($this->get_context_for_dynamic_submission());
$PAGE->set_url($this->get_page_url_for_dynamic_submission());
$this->check_access_for_dynamic_submission();
}
$attributes = ['data-random-ids' => 1] + ($attributes ?: []);
parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
}
/**
* Returns context where this form is used
*
* This context is validated in {@see external_api::validate_context()}
*
* If context depends on the form data, it is available in $this->_ajaxformdata or
* by calling $this->optional_param()
*
* Example:
* $cmid = $this->optional_param('cmid', 0, PARAM_INT);
* return context_module::instance($cmid);
*
* @return context
*/
abstract protected function get_context_for_dynamic_submission(): context;
/**
* Checks if current user has access to this form, otherwise throws exception
*
* Sometimes permission check may depend on the action and/or id of the entity.
* If necessary, form data is available in $this->_ajaxformdata or
* by calling $this->optional_param()
*
* Example:
* require_capability('dosomething', $this->get_context_for_dynamic_submission());
*/
abstract protected function check_access_for_dynamic_submission(): void;
/**
* Process the form submission, used if form was submitted via AJAX
*
* This method can return scalar values or arrays that can be json-encoded, they will be passed to the caller JS.
*
* Submission data can be accessed as: $this->get_data()
*
* Example:
* $data = $this->get_data();
* file_postupdate_standard_filemanager($data, ....);
* api::save_entity($data); // Save into the DB, trigger event, etc.
*
* @return mixed
*/
abstract public function process_dynamic_submission();
/**
* Load in existing data as form defaults
*
* Can be overridden to retrieve existing values from db by entity id and also
* to preprocess editor and filemanager elements
*
* Example:
* $id = $this->optional_param('id', 0, PARAM_INT);
* $data = api::get_entity($id); // For example, retrieve a row from the DB.
* file_prepare_standard_filemanager($data, ...);
* $this->set_data($data);
*/
abstract public function set_data_for_dynamic_submission(): void;
/**
* Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
*
* This is used in the form elements sensitive to the page url, such as Atto autosave in 'editor'
*
* If the form has arguments (such as 'id' of the element being edited), the URL should
* also have respective argument.
*
* Example:
* $id = $this->optional_param('id', 0, PARAM_INT);
* return new moodle_url('/my/page/where/form/is/used.php', ['id' => $id]);
*
* @return moodle_url
*/
abstract protected function get_page_url_for_dynamic_submission(): moodle_url;
}
+104
View File
@@ -0,0 +1,104 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Provides the {@link core_form\external} class.
*
* @package core_form
* @category external
* @copyright 2017 David Mudrák <david@moodle.com>
* @copyright 2016 Jonathon Fowler <fowlerj@usq.edu.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_form;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
/**
* Implements the external functions provided by the core_form subsystem.
*
* @copyright 2017 David Mudrak <david@moodle.com>
* @copyright 2016 Jonathon Fowler <fowlerj@usq.edu.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external extends external_api {
/**
* Describes the input paramaters of the get_filetypes_browser_data external function.
*
* @return \core_external\external_description
*/
public static function get_filetypes_browser_data_parameters() {
return new external_function_parameters([
'onlytypes' => new external_value(PARAM_RAW, 'Limit the browser to the given groups and extensions', VALUE_DEFAULT, ''),
'allowall' => new external_value(PARAM_BOOL, 'Allows to select All file types, does not apply with onlytypes are set.',
VALUE_DEFAULT, true),
'current' => new external_value(PARAM_RAW, 'Current types that should be selected.', VALUE_DEFAULT, ''),
]);
}
/**
* Implements the get_filetypes_browser_data external function.
*
* @param string $onlytypes Allow selection from these file types only; for example 'web_image'.
* @param bool $allowall Allow to select 'All file types'. Does not apply if onlytypes is set.
* @param string $current Current values that should be selected.
* @return object
*/
public static function get_filetypes_browser_data($onlytypes, $allowall, $current) {
$params = self::validate_parameters(self::get_filetypes_browser_data_parameters(),
compact('onlytypes', 'allowall', 'current'));
$util = new filetypes_util();
return ['groups' => $util->data_for_browser($params['onlytypes'], $params['allowall'], $params['current'])];
}
/**
* Describes the output of the get_filetypes_browser_data external function.
*
* @return \core_external\external_description
*/
public static function get_filetypes_browser_data_returns() {
$type = new external_single_structure([
'key' => new external_value(PARAM_RAW, 'The file type identifier'),
'name' => new external_value(PARAM_RAW, 'The file type name'),
'selected' => new external_value(PARAM_BOOL, 'Should it be marked as selected'),
'ext' => new external_value(PARAM_RAW, 'The file extension associated with the file type'),
]);
$group = new external_single_structure([
'key' => new external_value(PARAM_RAW, 'The file type group identifier'),
'name' => new external_value(PARAM_RAW, 'The file type group name'),
'selectable' => new external_value(PARAM_BOOL, 'Can it be marked as selected'),
'selected' => new external_value(PARAM_BOOL, 'Should it be marked as selected'),
'ext' => new external_value(PARAM_RAW, 'The list of file extensions associated with the group'),
'expanded' => new external_value(PARAM_BOOL, 'Should the group start as expanded or collapsed'),
'types' => new external_multiple_structure($type, 'List of file types in the group'),
]);
return new external_single_structure([
'groups' => new external_multiple_structure($group, 'List of file type groups'),
]);
}
}
+127
View File
@@ -0,0 +1,127 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_form\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
/**
* Implements the external functions provided by the core_form subsystem.
*
* @copyright 2020 Marina Glancy
* @package core_form
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dynamic_form extends external_api {
/**
* Parameters for modal form
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'form' => new external_value(PARAM_RAW_TRIMMED, 'Form class', VALUE_REQUIRED),
'formdata' => new external_value(PARAM_RAW, 'url-encoded form data', VALUE_REQUIRED),
]);
}
/**
* Submit a form from a modal dialogue.
*
* @param string $formclass
* @param string $formdatastr
* @return array
* @throws \moodle_exception
*/
public static function execute(string $formclass, string $formdatastr): array {
global $PAGE, $OUTPUT;
$params = self::validate_parameters(self::execute_parameters(), [
'form' => $formclass,
'formdata' => $formdatastr,
]);
$formclass = $params['form'];
parse_str($params['formdata'], $formdata);
self::autoload_block_edit_form($formclass);
if (!class_exists($formclass) || !is_subclass_of($formclass, \core_form\dynamic_form::class)) {
// For security reason we don't throw exception "class does not exist" but rather an access exception.
throw new \moodle_exception('nopermissionform', 'core_form');
}
/** @var \core_form\dynamic_form $form */
$form = new $formclass(null, null, 'post', '', [], true, $formdata, true);
$form->set_data_for_dynamic_submission();
if (!$form->is_cancelled() && $form->is_submitted() && $form->is_validated()) {
// Form was properly submitted, process and return results of processing. No need to render it again.
return ['submitted' => true, 'data' => json_encode($form->process_dynamic_submission())];
}
// Render actual form.
if ($form->no_submit_button_pressed()) {
// If form has not been submitted, we have to recreate the form for being able to properly handle non-submit action
// like "repeat elements" to include additional JS.
/** @var \core_form\dynamic_form $form */
$form = new $formclass(null, null, 'post', '', [], true, $formdata, true);
$form->set_data_for_dynamic_submission();
}
// Hack alert: Forcing bootstrap_renderer to initiate moodle page.
$OUTPUT->header();
$PAGE->start_collecting_javascript_requirements();
$data = $form->render();
$jsfooter = $PAGE->requires->get_end_code();
$output = ['submitted' => false, 'html' => $data, 'javascript' => $jsfooter];
return $output;
}
/**
* Special autoloading for block forms.
*
* @param string $formclass
* @return void
*/
protected static function autoload_block_edit_form(string $formclass): void {
global $CFG;
if (preg_match('/^block_([\w_]+)_edit_form$/', $formclass, $matches)) {
\block_manager::get_block_edit_form_class($matches[1]);
}
if ($formclass === 'block_edit_form') {
require_once($CFG->dirroot . '/blocks/edit_form.php');
}
}
/**
* Return for modal
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure(
array(
'submitted' => new external_value(PARAM_BOOL, 'If form was submitted and validated'),
'data' => new external_value(PARAM_RAW, 'JSON-encoded return data from form processing method', VALUE_OPTIONAL),
'html' => new external_value(PARAM_RAW, 'HTML fragment of the form', VALUE_OPTIONAL),
'javascript' => new external_value(PARAM_RAW, 'JavaScript fragment of the form', VALUE_OPTIONAL)
)
);
}
}
+537
View File
@@ -0,0 +1,537 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Provides the {@link core_form\filetypes_util} class.
*
* @package core_form
* @copyright 2017 David Mudrák <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_form;
use core_collator;
use core_filetypes;
use core_text;
defined('MOODLE_INTERNAL') || die();
/**
* 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").
*
* @copyright 2017 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filetypes_util {
/** @var array Cache of all file type groups for the {@link self::get_groups_info()}. */
protected $cachegroups = null;
/**
* 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
*/
public function normalize_file_types($types) {
if ($types === '' || $types === null) {
return [];
}
// Turn string into a list.
if (!is_array($types)) {
$types = preg_split('/[\s,;:"\']+/', $types, -1, PREG_SPLIT_NO_EMPTY);
}
// Fix whitespace and normalize the syntax a bit.
foreach ($types as $i => $type) {
$type = str_replace('*.', '.', $type);
$type = core_text::strtolower($type);
$type = trim($type);
if ($type === '*') {
return ['*'];
}
$types[$i] = $type;
}
// Do not make the user think that globs (like ".doc?") would work.
foreach ($types as $i => $type) {
if (strpos($type, '*') !== false or strpos($type, '?') !== false) {
unset($types[$i]);
}
}
foreach ($types as $i => $type) {
if (substr($type, 0, 1) === '.') {
// It looks like an extension.
$type = '.'.ltrim($type, '.');
$types[$i] = clean_param($type, PARAM_FILE);
} else if ($this->looks_like_mimetype($type)) {
// All good, it looks like a mimetype.
continue;
} else if ($this->is_filetype_group($type)) {
// All good, it is a known type group.
continue;
} else {
// We assume the user typed something like "png" so we consider
// it an extension.
$types[$i] = '.'.$type;
}
}
$types = array_filter($types, 'strlen');
$types = array_keys(array_flip($types));
return $types;
}
/**
* 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
*/
public function looks_like_mimetype($type) {
return (bool)preg_match('~^[-\.a-z0-9]+/[a-z0-9]+([-\.\+][a-z0-9]+)*$~', $type);
}
/**
* Is the given string a known filetype group?
*
* @param string $type
* @return bool|object false or the group info
*/
public function is_filetype_group($type) {
$info = $this->get_groups_info();
if (isset($info[$type])) {
return $info[$type];
} else {
return false;
}
}
/**
* Provides a list of all known file type groups and their properties.
*
* @return array
*/
public function get_groups_info() {
if ($this->cachegroups !== null) {
return $this->cachegroups;
}
$groups = [];
foreach (core_filetypes::get_types() as $ext => $info) {
if (isset($info['groups']) && is_array($info['groups'])) {
foreach ($info['groups'] as $group) {
if (!isset($groups[$group])) {
$groups[$group] = (object) [
'extensions' => [],
'mimetypes' => [],
];
}
$groups[$group]->extensions['.'.$ext] = true;
if (isset($info['type'])) {
$groups[$group]->mimetypes[$info['type']] = true;
}
}
}
}
foreach ($groups as $group => $info) {
$info->extensions = array_keys($info->extensions);
$info->mimetypes = array_keys($info->mimetypes);
}
$this->cachegroups = $groups;
return $this->cachegroups;
}
/**
* Return a human readable name of the filetype group.
*
* @param string $group
* @return string
*/
public function get_group_description($group) {
if (get_string_manager()->string_exists('group:'.$group, 'core_mimetypes')) {
return get_string('group:'.$group, 'core_mimetypes');
} else {
return s($group);
}
}
/**
* 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
*/
public function describe_file_types($types) {
$descriptions = [];
$types = $this->normalize_file_types($types);
foreach ($types as $type) {
if ($type === '*') {
$desc = get_string('filetypesany', 'core_form');
$descriptions[$desc] = [];
} else if ($group = $this->is_filetype_group($type)) {
$desc = $this->get_group_description($type);
$descriptions[$desc] = $group->extensions;
} else if ($this->looks_like_mimetype($type)) {
$desc = get_mimetype_description($type);
$descriptions[$desc] = file_get_typegroup('extension', [$type]);
} else {
$desc = get_mimetype_description(['filename' => 'fakefile'.$type]);
if (isset($descriptions[$desc])) {
$descriptions[$desc][] = $type;
} else {
$descriptions[$desc] = [$type];
}
}
}
$data = [];
foreach ($descriptions as $desc => $exts) {
sort($exts);
$data[] = (object)[
'description' => $desc,
'extensions' => join(' ', $exts),
];
}
core_collator::asort_objects_by_property($data, 'description', core_collator::SORT_NATURAL);
return (object)[
'hasdescriptions' => !empty($data),
'descriptions' => array_values($data),
];
}
/**
* 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
*/
public function data_for_browser($onlytypes=null, $allowall=true, $current=null) {
$groups = [];
$current = $this->normalize_file_types($current);
// Firstly populate the tree of extensions categorized into groups.
foreach ($this->get_groups_info() as $groupkey => $groupinfo) {
if (empty($groupinfo->extensions)) {
continue;
}
$group = (object) [
'key' => $groupkey,
'name' => $this->get_group_description($groupkey),
'selectable' => true,
'selected' => in_array($groupkey, $current),
'ext' => implode(' ', $groupinfo->extensions),
'expanded' => false,
];
$types = [];
foreach ($groupinfo->extensions as $extension) {
if ($onlytypes && !$this->is_listed($extension, $onlytypes)) {
$group->selectable = false;
$group->expanded = true;
$group->ext = '';
continue;
}
$desc = get_mimetype_description(['filename' => 'fakefile'.$extension]);
if ($selected = in_array($extension, $current)) {
$group->expanded = true;
}
$types[] = (object) [
'key' => $extension,
'name' => get_mimetype_description(['filename' => 'fakefile'.$extension]),
'selected' => $selected,
'ext' => $extension,
];
}
if (empty($types)) {
continue;
}
core_collator::asort_objects_by_property($types, 'name', core_collator::SORT_NATURAL);
$group->types = array_values($types);
$groups[] = $group;
}
core_collator::asort_objects_by_property($groups, 'name', core_collator::SORT_NATURAL);
// Append all other uncategorized extensions.
$others = [];
foreach (core_filetypes::get_types() as $extension => $info) {
// Reserved for unknown file types. Not available here.
if ($extension === 'xxx') {
continue;
}
$extension = '.'.$extension;
if ($onlytypes && !$this->is_listed($extension, $onlytypes)) {
continue;
}
if (!isset($info['groups']) || empty($info['groups'])) {
$others[] = (object) [
'key' => $extension,
'name' => get_mimetype_description(['filename' => 'fakefile'.$extension]),
'selected' => in_array($extension, $current),
'ext' => $extension,
];
}
}
core_collator::asort_objects_by_property($others, 'name', core_collator::SORT_NATURAL);
if (!empty($others)) {
$groups[] = (object) [
'key' => '',
'name' => get_string('filetypesothers', 'core_form'),
'selectable' => false,
'selected' => false,
'ext' => '',
'types' => array_values($others),
'expanded' => true,
];
}
if (empty($onlytypes) and $allowall) {
array_unshift($groups, (object) [
'key' => '*',
'name' => get_string('filetypesany', 'core_form'),
'selectable' => true,
'selected' => in_array('*', $current),
'ext' => null,
'types' => [],
'expanded' => false,
]);
}
$groups = array_values($groups);
return $groups;
}
/**
* 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
*/
public function expand($types, $keepgroups=false, $keepmimetypes=false) {
$expanded = [];
foreach ($this->normalize_file_types($types) as $type) {
if ($group = $this->is_filetype_group($type)) {
foreach ($group->extensions as $ext) {
$expanded[$ext] = true;
}
if ($keepgroups) {
$expanded[$type] = true;
}
} else if ($this->looks_like_mimetype($type)) {
// A mime type expands to the associated extensions.
foreach (file_get_typegroup('extension', [$type]) as $ext) {
$expanded[$ext] = true;
}
if ($keepmimetypes) {
$expanded[$type] = true;
}
} else {
// Single extension expands to itself.
$expanded[$type] = true;
}
}
return array_keys($expanded);
}
/**
* 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
*/
public function is_listed($types, $list) {
return empty($this->get_not_listed($types, $list));
}
/**
* @deprecated since Moodle 3.10 MDL-69050 - please use {@see is_listed} instead.
*/
public function is_whitelisted() {
throw new \coding_exception('\core_form\filetypes_util::is_whitelisted() has been removed.');
}
/**
* 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.
*/
public function get_not_listed($types, $list) {
$listedtypes = $this->expand($list, true, true);
if (empty($listedtypes) || $listedtypes == ['*']) {
return [];
}
$giventypes = $this->normalize_file_types($types);
if (empty($giventypes)) {
return [];
}
return array_diff($giventypes, $listedtypes);
}
/**
* @deprecated since Moodle 3.10 MDL-69050 - please use {@see get_not_listed} instead.
*/
public function get_not_whitelisted() {
throw new \coding_exception('\core_form\filetypes_util::get_not_whitelisted() has been removed.');
}
/**
* 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
*/
public function is_allowed_file_type($filename, $allowlist) {
$allowedextensions = $this->expand($allowlist);
if (empty($allowedextensions) || $allowedextensions == ['*']) {
return true;
}
$haystack = strrev(trim(core_text::strtolower($filename)));
foreach ($allowedextensions as $extension) {
if (strpos($haystack, strrev($extension)) === 0) {
// The file name ends with the extension.
return true;
}
}
return false;
}
/**
* 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.
*/
public function get_unknown_file_types($types) {
$unknown = [];
foreach ($this->normalize_file_types($types) as $type) {
if ($type === '*') {
// Any file is considered as a known type.
continue;
} else if ($type === '.xxx') {
$unknown[$type] = true;
} else if ($this->is_filetype_group($type)) {
// The type is a group that exists.
continue;
} else if ($this->looks_like_mimetype($type)) {
// If there's no extension associated with that mimetype, we consider it unknown.
if (empty(file_get_typegroup('extension', [$type]))) {
$unknown[$type] = true;
}
} else {
$coretypes = core_filetypes::get_types();
$typecleaned = str_replace(".", "", $type);
if (empty($coretypes[$typecleaned])) {
// If there's no extension, it doesn't exist.
$unknown[$type] = true;
}
}
}
return array_keys($unknown);
}
}
+86
View File
@@ -0,0 +1,86 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Provides {@link \core_form\privacy\provider} class.
*
* @package core_form
* @category privacy
* @copyright 2018 David Mudrák <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_form\privacy;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\writer;
defined('MOODLE_INTERNAL') || die();
/**
* Implements the privacy API for the core_form subsystem.
*
* @package core_files
* @copyright 2018 David Mudrák <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
// The forms subsystem does not store any data itself, it has no database tables.
\core_privacy\local\metadata\provider,
// The forms subsystem has user preferences.
\core_privacy\local\request\user_preference_provider {
/**
* Returns meta data about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
$collection->add_user_preference('filemanager_recentviewmode', 'privacy:metadata:preference:filemanager_recentviewmode');
return $collection;
}
/**
* Export all user preferences for the subsystem.
*
* @param int $userid The ID of the user whose data is to be exported.
*/
public static function export_user_preferences(int $userid) {
$preference = get_user_preferences('filemanager_recentviewmode', null, $userid);
if ($preference !== null) {
switch ($preference) {
case 1:
$value = get_string('displayasicons', 'core_repository');
break;
case 2:
$value = get_string('displayastree', 'core_repository');
break;
case 3:
$value = get_string('displaydetails', 'core_repository');
break;
default:
$value = $preference;
}
$desc = get_string('privacy:preference:filemanager_recentviewmode', 'core_form', $value);
writer::export_user_preference('core_form', 'filemanager_recentviewmode', $preference, $desc);
}
}
}
+64
View File
@@ -0,0 +1,64 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Provides the {@link core_form\util} class.
*
* @package core_form
* @copyright 2019 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_form;
defined('MOODLE_INTERNAL') || die();
/**
* General utility class for form-related methods.
*
* @copyright 2019 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class util {
/**
* This function should be called if a form submit results in a file download (i.e. with the
* Content-Disposition: attachment header) instead of navigating to a new page, before the
* file download is sent. It will set a cookie which is used to inform page javascript in
* submit.js.
*
* You may call this function in scripts which might not necessarily be called from forms; it
* will only set the cookie if there is a POST request from a form.
*
* This is automatically called from various points in Moodle such as send_file_xx functions
* in filelib.php.
*/
public static function form_download_complete() {
// If this doesn't look like a Moodle QuickForms request, ignore.
$quickform = false;
foreach ($_POST as $name => $value) {
if (preg_match('~^_qf__~', $name)) {
$quickform = true;
break;
}
}
if (!$quickform) {
return;
}
// Set a session cookie.
setcookie('moodledownload_' . sesskey(), time());
}
}