mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
Merge branch 'MDL-42616-master' of https://github.com/sammarshallou/moodle
This commit is contained in:
commit
c67c00a6fe
122
admin/tool/filetypes/classes/utils.php
Normal file
122
admin/tool/filetypes/classes/utils.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Class with static back-end methods used by the file type tool.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace tool_filetypes;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Class with static back-end methods used by the file type tool.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class utils {
|
||||
/**
|
||||
* Checks if the given file type extension is invalid.
|
||||
* The added file type extension must be unique and must not begin with a dot.
|
||||
*
|
||||
* @param string $extension Extension of the file type to add
|
||||
* @param string $oldextension Extension prior to update (empty string if adding new type)
|
||||
* @return bool True if it the file type trying to add already exists
|
||||
*/
|
||||
public static function is_extension_invalid($extension, $oldextension = '') {
|
||||
$extension = trim($extension);
|
||||
if ($extension === '' || $extension[0] === '.') {
|
||||
return true;
|
||||
}
|
||||
|
||||
$mimeinfo = get_mimetypes_array();
|
||||
if ($oldextension !== '') {
|
||||
unset($mimeinfo[$oldextension]);
|
||||
}
|
||||
|
||||
return array_key_exists($extension, $mimeinfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if we are allowed to turn on the 'default icon' option. You can
|
||||
* only have one of these for a given MIME type.
|
||||
*
|
||||
* @param string $mimetype MIME type
|
||||
* @param string $oldextension File extension name (before any change)
|
||||
*/
|
||||
public static function is_defaulticon_allowed($mimetype, $oldextension = '') {
|
||||
$mimeinfo = get_mimetypes_array();
|
||||
if ($oldextension !== '') {
|
||||
unset($mimeinfo[$oldextension]);
|
||||
}
|
||||
foreach ($mimeinfo as $extension => $values) {
|
||||
if ($values['type'] !== $mimetype) {
|
||||
continue;
|
||||
}
|
||||
if (!empty($values['defaulticon'])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all unique file type icons from a specific path, not including
|
||||
* sub-directories.
|
||||
*
|
||||
* Icon files such as pdf.png, pdf-24.png and pdf-36.png etc. are counted as
|
||||
* the same icon type.
|
||||
*
|
||||
* The resultant array has both key and value set to the icon name prefix,
|
||||
* such as 'pdf' => 'pdf'.
|
||||
*
|
||||
* @param string $path The path of the icon path
|
||||
* @return array An array of unique file icons within the given path
|
||||
*/
|
||||
public static function get_icons_from_path($path) {
|
||||
$icons = array();
|
||||
if ($handle = @opendir($path)) {
|
||||
while (($file = readdir($handle)) !== false) {
|
||||
$matches = array();
|
||||
if (preg_match('~(.+?)(?:-24|-32|-48|-64|-72|-80|-96|-128|-256)?\.(?:gif|png)$~',
|
||||
$file, $matches)) {
|
||||
$key = $matches[1];
|
||||
$icons[$key] = $key;
|
||||
}
|
||||
}
|
||||
closedir($handle);
|
||||
}
|
||||
ksort($icons);
|
||||
return $icons;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets unique file type icons from pix/f folder.
|
||||
*
|
||||
* @return array An array of unique file type icons e.g. 'pdf' => 'pdf'
|
||||
*/
|
||||
public static function get_file_icons() {
|
||||
global $CFG;
|
||||
$path = $CFG->dirroot . '/pix/f';
|
||||
return self::get_icons_from_path($path);
|
||||
}
|
||||
}
|
60
admin/tool/filetypes/delete.php
Normal file
60
admin/tool/filetypes/delete.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Delete a file type with a confirmation box.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require(__DIR__ . '/../../../config.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
admin_externalpage_setup('tool_filetypes');
|
||||
|
||||
$extension = required_param('extension', PARAM_ALPHANUMEXT);
|
||||
$redirecturl = new \moodle_url('/admin/tool/filetypes/index.php');
|
||||
|
||||
if (optional_param('delete', 0, PARAM_INT)) {
|
||||
require_sesskey();
|
||||
|
||||
// Delete the file type from the config.
|
||||
core_filetypes::delete_type($extension);
|
||||
redirect($redirecturl);
|
||||
}
|
||||
|
||||
// Page settings.
|
||||
$title = get_string('deletefiletypes', 'tool_filetypes');
|
||||
|
||||
$context = context_system::instance();
|
||||
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/delete.php', array('extension' => $extension)));
|
||||
$PAGE->navbar->add($title);
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->set_pagelayout('admin');
|
||||
$PAGE->set_title($SITE->fullname. ': ' . $title);
|
||||
|
||||
// Display the page.
|
||||
echo $OUTPUT->header();
|
||||
|
||||
$message = get_string('delete_confirmation', 'tool_filetypes', $extension);
|
||||
$deleteurl = new \moodle_url('delete.php', array('extension' => $extension, 'delete' => 1));
|
||||
$yesbutton = new single_button($deleteurl, get_string('yes'));
|
||||
$nobutton = new single_button($redirecturl, get_string('no'), 'get');
|
||||
echo $OUTPUT->confirm($message, $yesbutton, $nobutton);
|
||||
|
||||
echo $OUTPUT->footer();
|
111
admin/tool/filetypes/edit.php
Normal file
111
admin/tool/filetypes/edit.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Display the file type updating page.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
require(__DIR__ . '/../../../config.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
require_once('edit_form.php');
|
||||
|
||||
admin_externalpage_setup('tool_filetypes');
|
||||
|
||||
$oldextension = optional_param('oldextension', '', PARAM_ALPHANUMEXT);
|
||||
$mform = new tool_filetypes_form('edit.php', array('oldextension' => $oldextension));
|
||||
$title = get_string('addfiletypes', 'tool_filetypes');
|
||||
|
||||
if ($oldextension) {
|
||||
// This is editing an existing filetype, load data to the form.
|
||||
$mimetypes = get_mimetypes_array();
|
||||
if (!array_key_exists($oldextension, $mimetypes)) {
|
||||
throw new moodle_exception('error_notfound', 'tool_filetypes');
|
||||
}
|
||||
$typeinfo = $mimetypes[$oldextension];
|
||||
$formdata = array(
|
||||
'extension' => $oldextension,
|
||||
'mimetype' => $typeinfo['type'],
|
||||
'icon' => $typeinfo['icon'],
|
||||
'oldextension' => $oldextension,
|
||||
'description' => '',
|
||||
'groups' => '',
|
||||
'corestring' => '',
|
||||
'defaulticon' => 0
|
||||
);
|
||||
if (!empty($typeinfo['customdescription'])) {
|
||||
$formdata['description'] = $typeinfo['customdescription'];
|
||||
}
|
||||
if (!empty($typeinfo['groups'])) {
|
||||
$formdata['groups'] = implode(', ', $typeinfo['groups']);
|
||||
}
|
||||
if (!empty($typeinfo['string'])) {
|
||||
$formdata['corestring'] = $typeinfo['string'];
|
||||
}
|
||||
if (!empty($typeinfo['defaulticon'])) {
|
||||
$formdata['defaulticon'] = 1;
|
||||
}
|
||||
|
||||
$mform->set_data($formdata);
|
||||
$title = get_string('editfiletypes', 'tool_filetypes');
|
||||
}
|
||||
|
||||
$backurl = new \moodle_url('/admin/tool/filetypes/index.php');
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($backurl);
|
||||
} else if ($data = $mform->get_data()) {
|
||||
// Convert the groups value back into an array.
|
||||
$data->groups = trim($data->groups);
|
||||
if ($data->groups) {
|
||||
$data->groups = preg_split('~,\s*~', $data->groups);
|
||||
} else {
|
||||
$data->groups = array();
|
||||
}
|
||||
if (empty($data->defaulticon)) {
|
||||
$data->defaulticon = 0;
|
||||
}
|
||||
if (empty($data->corestring)) {
|
||||
$data->corestring = '';
|
||||
}
|
||||
if (empty($data->description)) {
|
||||
$data->description = '';
|
||||
}
|
||||
if ($data->oldextension) {
|
||||
// Update an existing file type.
|
||||
core_filetypes::update_type($data->oldextension, $data->extension, $data->mimetype, $data->icon,
|
||||
$data->groups, $data->corestring, $data->description, (bool)$data->defaulticon);
|
||||
} else {
|
||||
// Add a new file type entry.
|
||||
core_filetypes::add_type($data->extension, $data->mimetype, $data->icon,
|
||||
$data->groups, $data->corestring, $data->description, (bool)$data->defaulticon);
|
||||
}
|
||||
redirect($backurl);
|
||||
}
|
||||
|
||||
// Page settings.
|
||||
$context = context_system::instance();
|
||||
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/edit.php', array('oldextension' => $oldextension)));
|
||||
$PAGE->navbar->add($oldextension ? s($oldextension) : $title);
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->set_pagelayout('admin');
|
||||
$PAGE->set_title($SITE->fullname. ': ' . $title);
|
||||
|
||||
// Display the page.
|
||||
echo $OUTPUT->header();
|
||||
$mform->display();
|
||||
echo $OUTPUT->footer();
|
115
admin/tool/filetypes/edit_form.php
Normal file
115
admin/tool/filetypes/edit_form.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Customised file types editing form.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require_once($CFG->dirroot . '/lib/formslib.php');
|
||||
|
||||
/**
|
||||
* Form for adding a new custom file type or updating an existing custom file type.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class tool_filetypes_form extends moodleform {
|
||||
|
||||
public function definition() {
|
||||
global $CFG;
|
||||
$mform = $this->_form;
|
||||
$oldextension = $this->_customdata['oldextension'];
|
||||
|
||||
$mform->addElement('text', 'extension', get_string('extension', 'tool_filetypes'));
|
||||
$mform->setType('extension', PARAM_ALPHANUMEXT);
|
||||
$mform->addRule('extension', null, 'required', null, 'client');
|
||||
$mform->addHelpButton('extension', 'extension', 'tool_filetypes');
|
||||
|
||||
$mform->addElement('text', 'mimetype', get_string('mimetype', 'tool_filetypes'));
|
||||
$mform->setType('mimetype', PARAM_RAW);
|
||||
$mform->addRule('mimetype', null, 'required', null, 'client');
|
||||
$mform->addHelpButton('mimetype', 'mimetype', 'tool_filetypes');
|
||||
|
||||
$fileicons = \tool_filetypes\utils::get_file_icons();
|
||||
$mform->addElement('select', 'icon',
|
||||
get_string('icon', 'tool_filetypes'), $fileicons);
|
||||
$mform->addHelpButton('icon', 'icon', 'tool_filetypes');
|
||||
|
||||
$mform->addElement('text', 'groups', get_string('groups', 'tool_filetypes'));
|
||||
$mform->setType('groups', PARAM_RAW);
|
||||
$mform->addHelpButton('groups', 'groups', 'tool_filetypes');
|
||||
|
||||
$mform->addElement('select', 'descriptiontype', get_string('descriptiontype', 'tool_filetypes'),
|
||||
array('' => get_string('descriptiontype_default', 'tool_filetypes'),
|
||||
'custom' => get_string('descriptiontype_custom', 'tool_filetypes'),
|
||||
'lang' => get_string('descriptiontype_lang', 'tool_filetypes')));
|
||||
|
||||
$mform->addElement('text', 'description', get_string('description', 'tool_filetypes'));
|
||||
$mform->setType('description', PARAM_TEXT);
|
||||
$mform->addHelpButton('description', 'description', 'tool_filetypes');
|
||||
$mform->disabledIf('description', 'descriptiontype', 'ne', 'custom');
|
||||
|
||||
$mform->addElement('text', 'corestring', get_string('corestring', 'tool_filetypes'));
|
||||
$mform->setType('corestring', PARAM_ALPHANUMEXT);
|
||||
$mform->addHelpButton('corestring', 'corestring', 'tool_filetypes');
|
||||
$mform->disabledIf('corestring', 'descriptiontype', 'ne', 'lang');
|
||||
|
||||
$mform->addElement('checkbox', 'defaulticon', get_string('defaulticon', 'tool_filetypes'));
|
||||
$mform->addHelpButton('defaulticon', 'defaulticon', 'tool_filetypes');
|
||||
|
||||
$mform->addElement('hidden', 'oldextension', $oldextension);
|
||||
$mform->setType('oldextension', PARAM_RAW);
|
||||
$this->add_action_buttons(true, get_string('savechanges'));
|
||||
}
|
||||
|
||||
public function set_data($data) {
|
||||
// Set up the description type.
|
||||
if (!empty($data['corestring'])) {
|
||||
$data['descriptiontype'] = 'lang';
|
||||
} else if (!empty($data['description'])) {
|
||||
$data['descriptiontype'] = 'custom';
|
||||
} else {
|
||||
$data['descriptiontype'] = '';
|
||||
}
|
||||
|
||||
// Call parent.
|
||||
parent::set_data($data);
|
||||
}
|
||||
|
||||
public function validation($data, $files) {
|
||||
$errors = parent::validation($data, $files);
|
||||
|
||||
// Check the extension isn't already in use.
|
||||
$oldextension = $data['oldextension'];
|
||||
$extension = trim($data['extension']);
|
||||
if (\tool_filetypes\utils::is_extension_invalid($extension, $oldextension)) {
|
||||
$errors['extension'] = get_string('error_extension', 'tool_filetypes', $extension);
|
||||
}
|
||||
|
||||
// Check the 'default icon' setting doesn't conflict with an existing one.
|
||||
if (!empty($data['defaulticon']) && !\tool_filetypes\utils::is_defaulticon_allowed(
|
||||
$data['mimetype'], $oldextension)) {
|
||||
$errors['defaulticon'] = get_string('error_defaulticon', 'tool_filetypes', $extension);
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
}
|
48
admin/tool/filetypes/index.php
Normal file
48
admin/tool/filetypes/index.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Display the custom file type settings page.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require(__DIR__ . '/../../../config.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
admin_externalpage_setup('tool_filetypes');
|
||||
|
||||
// Page settings.
|
||||
$title = get_string('pluginname', 'tool_filetypes');
|
||||
|
||||
$context = context_system::instance();
|
||||
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/index.php'));
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->set_pagelayout('admin');
|
||||
$PAGE->set_title($SITE->fullname. ': ' . $title);
|
||||
|
||||
$renderer = $PAGE->get_renderer('tool_filetypes');
|
||||
|
||||
// Is it restricted because set in config.php?
|
||||
$restricted = array_key_exists('customfiletypes', $CFG->config_php_settings);
|
||||
|
||||
// Display the page.
|
||||
echo $renderer->header();
|
||||
echo $renderer->edit_table(get_mimetypes_array(), core_filetypes::get_deleted_types(),
|
||||
$restricted);
|
||||
echo $renderer->footer();
|
69
admin/tool/filetypes/lang/en/tool_filetypes.php
Normal file
69
admin/tool/filetypes/lang/en/tool_filetypes.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Strings for custom file types.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
$string['addfiletypes'] = 'Add a new file type';
|
||||
$string['corestring'] = 'Alternative language string';
|
||||
$string['corestring_help'] = 'This setting can be used to select a different language string from the core mimetypes.php language file. Generally it should be left blank. For custom types, use the description field.';
|
||||
$string['defaulticon'] = 'Default icon for MIME type';
|
||||
$string['defaulticon_help'] = 'If there are multiple file extensions with the same MIME type, select this option for one of the extensions so that its icon will be used when determining an icon from the MIME type.';
|
||||
$string['delete_confirmation'] = 'Are you absolutely sure you want to remove <strong>.{$a}</strong>?';
|
||||
$string['deletea'] = 'Delete {$a}';
|
||||
$string['deletefiletypes'] = 'Delete a file type';
|
||||
$string['description'] = 'Custom description';
|
||||
$string['description_help'] = 'Simple file type description, e.g. ‘Kindle ebook’. If your site supports multiple languages and uses the multi-language filter, you can enter multi-language tags in this field to supply a description in different languages.';
|
||||
$string['descriptiontype'] = 'Description type';
|
||||
$string['descriptiontype_help'] = 'There are three possible ways to specify a description.
|
||||
|
||||
* Default behaviour uses the MIME type. If there is a language string in mimetypes.php corresponding to that MIME type, it will be used; otherwise the MIME type itself will be displayed to users.
|
||||
* You can specify a custom description on this form.
|
||||
* You can specify the name of a languge string in mimetypes.php to use instead of the MIME type.';
|
||||
$string['descriptiontype_default'] = 'Default (MIME type or corresponding language string if available)';
|
||||
$string['descriptiontype_custom'] = 'Custom description specified in this form';
|
||||
$string['descriptiontype_lang'] = 'Alternative language string (from mimetypes.php)';
|
||||
$string['displaydescription'] = 'Description';
|
||||
$string['editfiletypes'] = 'Edit an existing file type';
|
||||
$string['emptylist'] = 'There are no file types defined.';
|
||||
$string['error_addentry'] = 'The file type extension, description, MIME type, and icon must not contain line feed and semicolon characters.';
|
||||
$string['error_defaulticon'] = 'Another file extension with the same MIME type is already marked as the default icon.';
|
||||
$string['error_extension'] = 'The file type extension <strong>{$a}</strong> already exists or is invalid. File extensions must be unique and must not contain special characters.';
|
||||
$string['error_notfound'] = 'The file type with extension {$a} cannot be found.';
|
||||
$string['extension'] = 'Extension';
|
||||
$string['extension_help'] = 'File name extension without the dot, e.g. ‘mobi’';
|
||||
$string['groups'] = 'Type groups';
|
||||
$string['groups_help'] = 'Optional list of file type groups that this type belongs to. These are generic categories such as ‘document’ and ‘image’.';
|
||||
$string['icon'] = 'File icon';
|
||||
$string['icon_help'] = 'Icon filename.
|
||||
|
||||
The list of icons is taken from the /pix/f directory inside your Moodle installation. You can add custom icons to this folder if required.';
|
||||
$string['mimetype'] = 'MIME type';
|
||||
$string['mimetype_help'] = 'MIME type associated with this file type, e.g. ‘application/x-mobipocket-ebook’';
|
||||
$string['pluginname'] = 'File types';
|
||||
$string['revert'] = 'Restore {$a} to Moodle defaults';
|
||||
$string['revert_confirmation'] = 'Are you sure you want to restore <strong>.{$a}</strong> to Moodle defaults, discarding your changes?';
|
||||
$string['revertfiletype'] = 'Restore a file type';
|
||||
$string['source'] = 'Type';
|
||||
$string['source_custom'] = 'Custom';
|
||||
$string['source_deleted'] = 'Deleted';
|
||||
$string['source_modified'] = 'Modified';
|
||||
$string['source_standard'] = 'Standard';
|
179
admin/tool/filetypes/renderer.php
Normal file
179
admin/tool/filetypes/renderer.php
Normal file
@ -0,0 +1,179 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Renderer.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Class containing the renderer functions for displaying file types.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class tool_filetypes_renderer extends plugin_renderer_base {
|
||||
|
||||
/**
|
||||
* Renderer for displaying the file type edit table.
|
||||
*
|
||||
* @param array $filetypes An array of file type objects (from get_mimetypes_array)
|
||||
* @param array $deleted An array of deleted file types
|
||||
* @param bool $restricted If true, cannot be edited because set in config.php.
|
||||
* @return string HTML code
|
||||
*/
|
||||
public function edit_table(array $filetypes, array $deleted, $restricted) {
|
||||
// Get combined array of all types, with deleted marker.
|
||||
$combined = array_merge($filetypes, $deleted);
|
||||
foreach ($deleted as $ext => $value) {
|
||||
$combined[$ext]['deleted'] = true;
|
||||
}
|
||||
ksort($combined);
|
||||
|
||||
$out = $this->heading(get_string('pluginname', 'tool_filetypes'));
|
||||
if ($restricted) {
|
||||
$out .= html_writer::div(
|
||||
html_writer::div(get_string('configoverride', 'admin'), 'form-overridden'),
|
||||
'', array('id' => 'adminsettings'));
|
||||
}
|
||||
if (count($combined) > 1) {
|
||||
// Display the file type table if any file types exist (other than 'xxx').
|
||||
$table = new html_table();
|
||||
$headings = new html_table_row();
|
||||
$headings->cells = array();
|
||||
$headings->cells[] = new html_table_cell(get_string('extension', 'tool_filetypes'));
|
||||
if (!$restricted) {
|
||||
$headings->cells[] =
|
||||
new html_table_cell(html_writer::span(get_string('edit'), 'accesshide'));
|
||||
}
|
||||
$headings->cells[] = new html_table_cell(get_string('source', 'tool_filetypes'));
|
||||
$headings->cells[] = new html_table_cell(get_string('mimetype', 'tool_filetypes'));
|
||||
$headings->cells[] = new html_table_cell(get_string('groups', 'tool_filetypes'));
|
||||
$headings->cells[] = new html_table_cell(get_string('displaydescription', 'tool_filetypes'));
|
||||
foreach ($headings->cells as $cell) {
|
||||
$cell->header = true;
|
||||
}
|
||||
$table->data = array($headings);
|
||||
foreach ($combined as $extension => $filetype) {
|
||||
if ($extension === 'xxx') {
|
||||
continue;
|
||||
}
|
||||
$row = new html_table_row();
|
||||
$row->cells = array();
|
||||
|
||||
// First cell has icon and extension.
|
||||
$icon = $this->pix_icon('f/' . $filetype['icon'], '');
|
||||
$row->cells[] = new html_table_cell($icon . ' ' . html_writer::span(s($extension)));
|
||||
|
||||
// Reset URL and button if needed.
|
||||
$reverturl = new \moodle_url('/admin/tool/filetypes/revert.php',
|
||||
array('extension' => $extension));
|
||||
$revertbutton = html_writer::link($reverturl, $this->pix_icon('t/restore',
|
||||
get_string('revert', 'tool_filetypes', s($extension))));
|
||||
if ($restricted) {
|
||||
$revertbutton = '';
|
||||
}
|
||||
|
||||
// Rest is different for deleted items.
|
||||
if (!empty($filetype['deleted'])) {
|
||||
// Show deleted standard types differently.
|
||||
if (!$restricted) {
|
||||
$row->cells[] = new html_table_cell('');
|
||||
}
|
||||
$source = new html_table_cell(get_string('source_deleted', 'tool_filetypes') .
|
||||
' ' . $revertbutton);
|
||||
$source->attributes = array('class' => 'nonstandard');
|
||||
$row->cells[] = $source;
|
||||
|
||||
// Other cells are blank.
|
||||
$row->cells[] = new html_table_cell('');
|
||||
$row->cells[] = new html_table_cell('');
|
||||
$row->cells[] = new html_table_cell('');
|
||||
$row->attributes = array('class' => 'deleted');
|
||||
} else {
|
||||
if (!$restricted) {
|
||||
// Edit icons. For accessibility, the name of these links should
|
||||
// be different for each row, so we have to include the extension.
|
||||
$editurl = new \moodle_url('/admin/tool/filetypes/edit.php',
|
||||
array('oldextension' => $extension));
|
||||
$editbutton = html_writer::link($editurl, $this->pix_icon('t/edit',
|
||||
get_string('edita', '', s($extension))));
|
||||
$deleteurl = new \moodle_url('/admin/tool/filetypes/delete.php',
|
||||
array('extension' => $extension));
|
||||
$deletebutton = html_writer::link($deleteurl, $this->pix_icon('t/delete',
|
||||
get_string('deletea', 'tool_filetypes', s($extension))));
|
||||
$row->cells[] = new html_table_cell($editbutton . ' ' . $deletebutton);
|
||||
}
|
||||
|
||||
// Source.
|
||||
$sourcestring = 'source_';
|
||||
if (!empty($filetype['custom'])) {
|
||||
$sourcestring .= 'custom';
|
||||
} else if (!empty($filetype['modified'])) {
|
||||
$sourcestring .= 'modified';
|
||||
} else {
|
||||
$sourcestring .= 'standard';
|
||||
}
|
||||
$source = new html_table_cell(get_string($sourcestring, 'tool_filetypes') .
|
||||
($sourcestring === 'source_modified' ? ' ' . $revertbutton : ''));
|
||||
if ($sourcestring !== 'source_standard') {
|
||||
$source->attributes = array('class' => 'nonstandard');
|
||||
}
|
||||
$row->cells[] = $source;
|
||||
|
||||
// MIME type.
|
||||
$mimetype = html_writer::div(s($filetype['type']), 'mimetype');
|
||||
if (!empty($filetype['defaulticon'])) {
|
||||
// Include the 'default for MIME type' info in the MIME type cell.
|
||||
$mimetype .= html_writer::div(html_writer::tag('i',
|
||||
get_string('defaulticon', 'tool_filetypes')));
|
||||
}
|
||||
$row->cells[] = new html_table_cell($mimetype);
|
||||
|
||||
// Groups.
|
||||
$groups = !empty($filetype['groups']) ? implode(', ', $filetype['groups']) : '';
|
||||
$row->cells[] = new html_table_cell(s($groups));
|
||||
|
||||
// Description.
|
||||
$description = get_mimetype_description(array('filename' => 'a.' . $extension));
|
||||
// Don't show the description if it's just a copy of the MIME type,
|
||||
// it makes the table ugly with the long duplicate text; leave blank instead.
|
||||
if ($description === $filetype['type']) {
|
||||
$description = '';
|
||||
}
|
||||
$row->cells[] = new html_table_cell($description);
|
||||
}
|
||||
|
||||
$table->data[] = $row;
|
||||
}
|
||||
$out .= html_writer::table($table);
|
||||
} else {
|
||||
$out .= html_writer::tag('div', get_string('emptylist', 'tool_filetypes'));
|
||||
}
|
||||
// Displaying the 'Add' button.
|
||||
if (!$restricted) {
|
||||
$out .= $this->single_button(new moodle_url('/admin/tool/filetypes/edit.php',
|
||||
array('name' => 'add')), get_string('addfiletypes', 'tool_filetypes'), 'get');
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
60
admin/tool/filetypes/revert.php
Normal file
60
admin/tool/filetypes/revert.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Resets a file type to the default Moodle values.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require(__DIR__ . '/../../../config.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
admin_externalpage_setup('tool_filetypes');
|
||||
|
||||
$extension = required_param('extension', PARAM_RAW);
|
||||
$redirecturl = new \moodle_url('/admin/tool/filetypes/index.php');
|
||||
|
||||
if (optional_param('revert', 0, PARAM_INT)) {
|
||||
require_sesskey();
|
||||
|
||||
// Reset the file type in config.
|
||||
core_filetypes::revert_type_to_default($extension);
|
||||
redirect($redirecturl);
|
||||
}
|
||||
|
||||
// Page settings.
|
||||
$title = get_string('revertfiletype', 'tool_filetypes');
|
||||
|
||||
$context = context_system::instance();
|
||||
$PAGE->set_url(new \moodle_url('/admin/tool/filetypes/revert.php', array('extension' => $extension)));
|
||||
$PAGE->navbar->add($title);
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->set_pagelayout('admin');
|
||||
$PAGE->set_title($SITE->fullname. ': ' . $title);
|
||||
|
||||
// Display the page.
|
||||
echo $OUTPUT->header();
|
||||
|
||||
$message = get_string('revert_confirmation', 'tool_filetypes', $extension);
|
||||
$reverturl = new \moodle_url('revert.php', array('extension' => $extension, 'revert' => 1));
|
||||
$yesbutton = new single_button($reverturl, get_string('yes'));
|
||||
$nobutton = new single_button($redirecturl, get_string('no'), 'get');
|
||||
echo $OUTPUT->confirm($message, $yesbutton, $nobutton);
|
||||
|
||||
echo $OUTPUT->footer();
|
31
admin/tool/filetypes/settings.php
Normal file
31
admin/tool/filetypes/settings.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Configure the setting page of the custom file type as an external page.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
if ($hassiteconfig) {
|
||||
$ADMIN->add('server', new admin_externalpage('tool_filetypes',
|
||||
new lang_string('pluginname', 'tool_filetypes'),
|
||||
$CFG->wwwroot . '/admin/tool/filetypes/index.php'));
|
||||
}
|
32
admin/tool/filetypes/styles.css
Normal file
32
admin/tool/filetypes/styles.css
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Styles for admin tool page.
|
||||
*/
|
||||
|
||||
/* Fix bad default table wrap (caused by the long mimetype field). */
|
||||
.path-admin-tool-filetypes .generaltable .c0,
|
||||
.path-admin-tool-filetypes .generaltable .c1,
|
||||
.path-admin-tool-filetypes .generaltable .c2,
|
||||
.path-admin-tool-filetypes .generaltable th {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
/* Formatting for deleted lines. */
|
||||
.path-admin-tool-filetypes .generaltable .deleted .c0 img {
|
||||
opacity: 0.2;
|
||||
}
|
||||
|
||||
.path-admin-tool-filetypes .generaltable .deleted .c0 span {
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
||||
/* Custom types show bold */
|
||||
.path-admin-tool-filetypes .generaltable .nonstandard {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
/* Spacing around the 'Defined in config.php' stripe */
|
||||
.path-admin-tool-filetypes .form-overridden {
|
||||
display: inline-block;
|
||||
margin-bottom: 1em;
|
||||
padding: 4px 6px;
|
||||
}
|
107
admin/tool/filetypes/tests/behat/add_filetypes.feature
Normal file
107
admin/tool/filetypes/tests/behat/add_filetypes.feature
Normal file
@ -0,0 +1,107 @@
|
||||
@tool @tool_filetypes
|
||||
Feature: Add customised file types
|
||||
In order to support a file mime type which doesn't exist in Moodle
|
||||
As an administrator
|
||||
I need to add a new customised file type
|
||||
|
||||
Scenario: Add a new file type
|
||||
Given I log in as "admin"
|
||||
And I navigate to "File types" node in "Site administration > Server"
|
||||
And I press "Add"
|
||||
# Try setting all the form fields, not just the optional ones.
|
||||
And I set the following fields to these values:
|
||||
| Extension | frog |
|
||||
| MIME type | application/x-frog |
|
||||
| File icon | archive |
|
||||
| Type groups | document |
|
||||
| Description type | Custom description specified in this form |
|
||||
| Custom description | Froggy file |
|
||||
| Default icon for MIME type | 1 |
|
||||
When I press "Save changes"
|
||||
Then I should see "Froggy file" in the "application/x-frog" "table_row"
|
||||
And I should see "document" in the "application/x-frog" "table_row"
|
||||
And I should see "frog" in the "application/x-frog" "table_row"
|
||||
And "//img[contains(@src, 'archive')]" "xpath_element" should exist in the "application/x-frog" "table_row"
|
||||
|
||||
Scenario: Update an existing file type
|
||||
Given I log in as "admin"
|
||||
And I navigate to "File types" node in "Site administration > Server"
|
||||
When I click on "Edit 7z" "link"
|
||||
And I set the following fields to these values:
|
||||
| Extension | doc |
|
||||
And I press "Save changes"
|
||||
Then I should see "File extensions must be unique"
|
||||
And I set the following fields to these values:
|
||||
| Extension | frog |
|
||||
And I press "Save changes"
|
||||
And I should see "frog" in the "application/x-7z-compressed" "table_row"
|
||||
|
||||
Scenario: Delete an existing file type
|
||||
Given I log in as "admin"
|
||||
And I navigate to "File types" node in "Site administration > Server"
|
||||
When I click on "Delete 7z" "link"
|
||||
Then I should see "Are you absolutely sure you want to remove .7z?"
|
||||
And I press "Yes"
|
||||
And I should see "Deleted" in the "7z" "table_row"
|
||||
|
||||
Scenario: Delete a custom file type
|
||||
Given I log in as "admin"
|
||||
And I navigate to "File types" node in "Site administration > Server"
|
||||
And I press "Add"
|
||||
And I set the following fields to these values:
|
||||
| Extension | frog |
|
||||
| MIME type | application/x-frog |
|
||||
And I press "Save changes"
|
||||
When I click on "Delete frog" "link"
|
||||
And I press "Yes"
|
||||
Then I should not see "frog"
|
||||
|
||||
Scenario: Revert changes to deleted file type
|
||||
Given I log in as "admin"
|
||||
And I navigate to "File types" node in "Site administration > Server"
|
||||
When I click on "Delete 7z" "link"
|
||||
And I press "Yes"
|
||||
And I follow "Restore 7z to Moodle defaults"
|
||||
And I press "Yes"
|
||||
Then I should not see "Deleted" in the "7z" "table_row"
|
||||
|
||||
Scenario: Revert changes to updated file type
|
||||
Given I log in as "admin"
|
||||
And I navigate to "File types" node in "Site administration > Server"
|
||||
And I click on "Edit 7z" "link"
|
||||
And I set the following fields to these values:
|
||||
| File icon | document |
|
||||
And I press "Save changes"
|
||||
When I follow "Restore 7z to Moodle defaults"
|
||||
And I press "Yes"
|
||||
Then "//img[contains(@src, 'archive')]" "xpath_element" should exist in the "7z" "table_row"
|
||||
|
||||
@javascript
|
||||
Scenario: Create a resource activity which contains a customised file type
|
||||
Given the following "courses" exist:
|
||||
| fullname | shortname |
|
||||
| Course 1 | C1 |
|
||||
And I log in as "admin"
|
||||
And I navigate to "File types" node in "Site administration > Server"
|
||||
And I press "Add"
|
||||
And I set the following fields to these values:
|
||||
| Extension | frog |
|
||||
| MIME type | application/x-frog |
|
||||
| File icon | archive |
|
||||
| Description type | Custom description specified in this form |
|
||||
| Custom description | Froggy file |
|
||||
And I press "Save changes"
|
||||
# Create a resource activity and add it to a course
|
||||
And I am on homepage
|
||||
And I follow "Course 1"
|
||||
And I turn editing mode on
|
||||
When I add a "File" to section "1"
|
||||
And I set the following fields to these values:
|
||||
| Name | An example of customised file type |
|
||||
| Description | File description |
|
||||
And I upload "admin/tool/filetypes/tests/fixtures/test.frog" file to "Select files" filemanager
|
||||
And I expand all fieldsets
|
||||
And I set the field "Show type" to "1"
|
||||
And I press "Save and return to course"
|
||||
Then I should see "Froggy file"
|
||||
And the "src" attribute of ".modtype_resource a img" "css_element" should contain "archive"
|
BIN
admin/tool/filetypes/tests/fixtures/frog-24.png
vendored
Normal file
BIN
admin/tool/filetypes/tests/fixtures/frog-24.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 67 B |
BIN
admin/tool/filetypes/tests/fixtures/frog-48.png
vendored
Normal file
BIN
admin/tool/filetypes/tests/fixtures/frog-48.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 67 B |
1
admin/tool/filetypes/tests/fixtures/test.frog
vendored
Normal file
1
admin/tool/filetypes/tests/fixtures/test.frog
vendored
Normal file
@ -0,0 +1 @@
|
||||
This is not a real file format.
|
BIN
admin/tool/filetypes/tests/fixtures/zombie.gif
vendored
Normal file
BIN
admin/tool/filetypes/tests/fixtures/zombie.gif
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 B |
86
admin/tool/filetypes/tests/tool_filetypes_test.php
Normal file
86
admin/tool/filetypes/tests/tool_filetypes_test.php
Normal 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/>.
|
||||
|
||||
/**
|
||||
* Unit tests for the custom file types.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use tool_filetypes\utils;
|
||||
|
||||
/**
|
||||
* Unit tests for the custom file types.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class tool_filetypes_test extends advanced_testcase {
|
||||
/**
|
||||
* Tests is_extension_invalid() function.
|
||||
*/
|
||||
public function test_is_extension_invalid() {
|
||||
// The pdf file extension already exists in default moodle minetypes.
|
||||
$this->assertTrue(utils::is_extension_invalid('pdf'));
|
||||
|
||||
// The frog extension does not.
|
||||
$this->assertFalse(utils::is_extension_invalid('frog'));
|
||||
|
||||
// However you could use the pdf extension when editing the pdf extension.
|
||||
$this->assertFalse(utils::is_extension_invalid('pdf', 'pdf'));
|
||||
|
||||
// Blank extension is invalid.
|
||||
$this->assertTrue(utils::is_extension_invalid(''));
|
||||
|
||||
// Extensions with dot are invalid.
|
||||
$this->assertTrue(utils::is_extension_invalid('.frog'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests is_defaulticon_allowed() function.
|
||||
*/
|
||||
public function test_is_defaulticon_allowed() {
|
||||
// You ARE allowed to set a default icon for a MIME type that hasn't
|
||||
// been used yet.
|
||||
$this->assertTrue(utils::is_defaulticon_allowed('application/x-frog'));
|
||||
|
||||
// You AREN'T allowed to set default icon for text/plain as there is
|
||||
// already a type that has that set.
|
||||
$this->assertFalse(utils::is_defaulticon_allowed('text/plain'));
|
||||
|
||||
// But you ARE still allowed to set it when actually editing txt, which
|
||||
// is the default.
|
||||
$this->assertTrue(utils::is_defaulticon_allowed('text/plain', 'txt'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_icons_from_path() function.
|
||||
*/
|
||||
public function test_get_icons_from_path() {
|
||||
// Get icons from the fixtures folder.
|
||||
$icons = utils::get_icons_from_path(__DIR__ . '/fixtures');
|
||||
|
||||
// The icons are returned alphabetically and with keys === values.
|
||||
// For the icon with numbers after the name, only the base name is
|
||||
// returned and only one of it.
|
||||
$this->assertEquals(array('frog' => 'frog', 'zombie' => 'zombie'), $icons);
|
||||
}
|
||||
}
|
29
admin/tool/filetypes/version.php
Normal file
29
admin/tool/filetypes/version.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Version details.
|
||||
*
|
||||
* @package tool_filetypes
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2014112700;
|
||||
$plugin->requires = 2014112700;
|
||||
$plugin->component = 'tool_filetypes';
|
@ -517,6 +517,23 @@ $CFG->admin = 'admin';
|
||||
// on a shared file system that supports locking.
|
||||
// $CFG->lock_file_root = $CFG->dataroot . '/lock';
|
||||
//
|
||||
// Moodle 2.9 allows administrators to customise the list of supported file types.
|
||||
// To add a new filetype or override the definition of an existing one, set the
|
||||
// customfiletypes variable like this:
|
||||
//
|
||||
// $CFG->customfiletypes = array(
|
||||
// (object)array(
|
||||
// 'extension' => 'frog',
|
||||
// 'icon' => 'archive',
|
||||
// 'type' => 'application/frog',
|
||||
// 'customdescription' => 'Amphibian-related file archive'
|
||||
// )
|
||||
// );
|
||||
//
|
||||
// The extension, icon, and type fields are required. The icon field can refer to
|
||||
// any icon inside the pix/f folder. You can also set the customdescription field
|
||||
// (shown above) and (for advanced use) the groups, string, and defaulticon fields.
|
||||
//
|
||||
//=========================================================================
|
||||
// 7. SETTINGS FOR DEVELOPMENT SERVERS - not intended for production use!!!
|
||||
//=========================================================================
|
||||
|
682
lib/classes/filetypes.php
Normal file
682
lib/classes/filetypes.php
Normal file
@ -0,0 +1,682 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Class to manage the custom filetypes list that is stored in a config variable.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir . '/filelib.php');
|
||||
|
||||
/**
|
||||
* Class to manage the custom filetypes list that is stored in a config variable.
|
||||
*
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class core_filetypes {
|
||||
/** @var array Cached MIME types for current request */
|
||||
protected static $cachedtypes;
|
||||
|
||||
/**
|
||||
* Gets default MIME types that are included as standard.
|
||||
*
|
||||
* Note: Use the function get_mimetypes_array to access this data including
|
||||
* any customisations the user might have made.
|
||||
*
|
||||
* @return array Default (pre-installed) MIME type information
|
||||
*/
|
||||
protected static function get_default_types() {
|
||||
return array(
|
||||
'xxx' => array('type' => 'document/unknown', 'icon' => 'unknown'),
|
||||
'3gp' => array('type' => 'video/quicktime', 'icon' => 'quicktime', 'groups' => array('video'), 'string' => 'video'),
|
||||
'7z' => array('type' => 'application/x-7z-compressed', 'icon' => 'archive',
|
||||
'groups' => array('archive'), 'string' => 'archive'),
|
||||
'aac' => array('type' => 'audio/aac', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'accdb' => array('type' => 'application/msaccess', 'icon' => 'base'),
|
||||
'ai' => array('type' => 'application/postscript', 'icon' => 'eps', 'groups' => array('image'), 'string' => 'image'),
|
||||
'aif' => array('type' => 'audio/x-aiff', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'aiff' => array('type' => 'audio/x-aiff', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'aifc' => array('type' => 'audio/x-aiff', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'applescript' => array('type' => 'text/plain', 'icon' => 'text'),
|
||||
'asc' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
|
||||
'asm' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
|
||||
'au' => array('type' => 'audio/au', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'avi' => array('type' => 'video/x-ms-wm', 'icon' => 'avi',
|
||||
'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
'bmp' => array('type' => 'image/bmp', 'icon' => 'bmp', 'groups' => array('image'), 'string' => 'image'),
|
||||
'c' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
|
||||
'cct' => array('type' => 'shockwave/director', 'icon' => 'flash'),
|
||||
'cpp' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
|
||||
'cs' => array('type' => 'application/x-csh', 'icon' => 'sourcecode'),
|
||||
'css' => array('type' => 'text/css', 'icon' => 'text', 'groups' => array('web_file')),
|
||||
'csv' => array('type' => 'text/csv', 'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
|
||||
'dv' => array('type' => 'video/x-dv', 'icon' => 'quicktime', 'groups' => array('video'), 'string' => 'video'),
|
||||
'dmg' => array('type' => 'application/octet-stream', 'icon' => 'unknown'),
|
||||
|
||||
'doc' => array('type' => 'application/msword', 'icon' => 'document', 'groups' => array('document')),
|
||||
'bdoc' => array('type' => 'application/x-digidoc', 'icon' => 'document', 'groups' => array('archive')),
|
||||
'cdoc' => array('type' => 'application/x-digidoc', 'icon' => 'document', 'groups' => array('archive')),
|
||||
'ddoc' => array('type' => 'application/x-digidoc', 'icon' => 'document', 'groups' => array('archive')),
|
||||
'docx' => array('type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'icon' => 'document', 'groups' => array('document')),
|
||||
'docm' => array('type' => 'application/vnd.ms-word.document.macroEnabled.12', 'icon' => 'document'),
|
||||
'dotx' => array('type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
|
||||
'icon' => 'document'),
|
||||
'dotm' => array('type' => 'application/vnd.ms-word.template.macroEnabled.12', 'icon' => 'document'),
|
||||
|
||||
'dcr' => array('type' => 'application/x-director', 'icon' => 'flash'),
|
||||
'dif' => array('type' => 'video/x-dv', 'icon' => 'quicktime', 'groups' => array('video'), 'string' => 'video'),
|
||||
'dir' => array('type' => 'application/x-director', 'icon' => 'flash'),
|
||||
'dxr' => array('type' => 'application/x-director', 'icon' => 'flash'),
|
||||
'eps' => array('type' => 'application/postscript', 'icon' => 'eps'),
|
||||
'epub' => array('type' => 'application/epub+zip', 'icon' => 'epub', 'groups' => array('document')),
|
||||
'fdf' => array('type' => 'application/pdf', 'icon' => 'pdf'),
|
||||
'flv' => array('type' => 'video/x-flv', 'icon' => 'flash',
|
||||
'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
'f4v' => array('type' => 'video/mp4', 'icon' => 'flash', 'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
|
||||
'gallery' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
|
||||
'galleryitem' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
|
||||
'gallerycollection' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
|
||||
'gif' => array('type' => 'image/gif', 'icon' => 'gif', 'groups' => array('image', 'web_image'), 'string' => 'image'),
|
||||
'gtar' => array('type' => 'application/x-gtar', 'icon' => 'archive',
|
||||
'groups' => array('archive'), 'string' => 'archive'),
|
||||
'tgz' => array('type' => 'application/g-zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
|
||||
'gz' => array('type' => 'application/g-zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
|
||||
'gzip' => array('type' => 'application/g-zip', 'icon' => 'archive',
|
||||
'groups' => array('archive'), 'string' => 'archive'),
|
||||
'h' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
|
||||
'hpp' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
|
||||
'hqx' => array('type' => 'application/mac-binhex40', 'icon' => 'archive',
|
||||
'groups' => array('archive'), 'string' => 'archive'),
|
||||
'htc' => array('type' => 'text/x-component', 'icon' => 'markup'),
|
||||
'html' => array('type' => 'text/html', 'icon' => 'html', 'groups' => array('web_file')),
|
||||
'xhtml' => array('type' => 'application/xhtml+xml', 'icon' => 'html', 'groups' => array('web_file')),
|
||||
'htm' => array('type' => 'text/html', 'icon' => 'html', 'groups' => array('web_file')),
|
||||
'ico' => array('type' => 'image/vnd.microsoft.icon', 'icon' => 'image',
|
||||
'groups' => array('image'), 'string' => 'image'),
|
||||
'ics' => array('type' => 'text/calendar', 'icon' => 'text'),
|
||||
'isf' => array('type' => 'application/inspiration', 'icon' => 'isf'),
|
||||
'ist' => array('type' => 'application/inspiration.template', 'icon' => 'isf'),
|
||||
'java' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
|
||||
'jar' => array('type' => 'application/java-archive', 'icon' => 'archive'),
|
||||
'jcb' => array('type' => 'text/xml', 'icon' => 'markup'),
|
||||
'jcl' => array('type' => 'text/xml', 'icon' => 'markup'),
|
||||
'jcw' => array('type' => 'text/xml', 'icon' => 'markup'),
|
||||
'jmt' => array('type' => 'text/xml', 'icon' => 'markup'),
|
||||
'jmx' => array('type' => 'text/xml', 'icon' => 'markup'),
|
||||
'jnlp' => array('type' => 'application/x-java-jnlp-file', 'icon' => 'markup'),
|
||||
'jpe' => array('type' => 'image/jpeg', 'icon' => 'jpeg', 'groups' => array('image', 'web_image'), 'string' => 'image'),
|
||||
'jpeg' => array('type' => 'image/jpeg', 'icon' => 'jpeg', 'groups' => array('image', 'web_image'), 'string' => 'image'),
|
||||
'jpg' => array('type' => 'image/jpeg', 'icon' => 'jpeg', 'groups' => array('image', 'web_image'), 'string' => 'image'),
|
||||
'jqz' => array('type' => 'text/xml', 'icon' => 'markup'),
|
||||
'js' => array('type' => 'application/x-javascript', 'icon' => 'text', 'groups' => array('web_file')),
|
||||
'latex' => array('type' => 'application/x-latex', 'icon' => 'text'),
|
||||
'm' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
|
||||
'mbz' => array('type' => 'application/vnd.moodle.backup', 'icon' => 'moodle'),
|
||||
'mdb' => array('type' => 'application/x-msaccess', 'icon' => 'base'),
|
||||
'mht' => array('type' => 'message/rfc822', 'icon' => 'archive'),
|
||||
'mhtml' => array('type' => 'message/rfc822', 'icon' => 'archive'),
|
||||
'mov' => array('type' => 'video/quicktime', 'icon' => 'quicktime',
|
||||
'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
'movie' => array('type' => 'video/x-sgi-movie', 'icon' => 'quicktime', 'groups' => array('video'), 'string' => 'video'),
|
||||
'mw' => array('type' => 'application/maple', 'icon' => 'math'),
|
||||
'mws' => array('type' => 'application/maple', 'icon' => 'math'),
|
||||
'm3u' => array('type' => 'audio/x-mpegurl', 'icon' => 'mp3', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'mp3' => array('type' => 'audio/mp3', 'icon' => 'mp3', 'groups' => array('audio', 'web_audio'), 'string' => 'audio'),
|
||||
'mp4' => array('type' => 'video/mp4', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
'm4v' => array('type' => 'video/mp4', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
'm4a' => array('type' => 'audio/mp4', 'icon' => 'mp3', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'mpeg' => array('type' => 'video/mpeg', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
'mpe' => array('type' => 'video/mpeg', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
'mpg' => array('type' => 'video/mpeg', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
'mpr' => array('type' => 'application/vnd.moodle.profiling', 'icon' => 'moodle'),
|
||||
|
||||
'nbk' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
|
||||
'notebook' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
|
||||
|
||||
'odt' => array('type' => 'application/vnd.oasis.opendocument.text', 'icon' => 'writer', 'groups' => array('document')),
|
||||
'ott' => array('type' => 'application/vnd.oasis.opendocument.text-template',
|
||||
'icon' => 'writer', 'groups' => array('document')),
|
||||
'oth' => array('type' => 'application/vnd.oasis.opendocument.text-web', 'icon' => 'oth', 'groups' => array('document')),
|
||||
'odm' => array('type' => 'application/vnd.oasis.opendocument.text-master', 'icon' => 'writer'),
|
||||
'odg' => array('type' => 'application/vnd.oasis.opendocument.graphics', 'icon' => 'draw'),
|
||||
'otg' => array('type' => 'application/vnd.oasis.opendocument.graphics-template', 'icon' => 'draw'),
|
||||
'odp' => array('type' => 'application/vnd.oasis.opendocument.presentation', 'icon' => 'impress'),
|
||||
'otp' => array('type' => 'application/vnd.oasis.opendocument.presentation-template', 'icon' => 'impress'),
|
||||
'ods' => array('type' => 'application/vnd.oasis.opendocument.spreadsheet',
|
||||
'icon' => 'calc', 'groups' => array('spreadsheet')),
|
||||
'ots' => array('type' => 'application/vnd.oasis.opendocument.spreadsheet-template',
|
||||
'icon' => 'calc', 'groups' => array('spreadsheet')),
|
||||
'odc' => array('type' => 'application/vnd.oasis.opendocument.chart', 'icon' => 'chart'),
|
||||
'odf' => array('type' => 'application/vnd.oasis.opendocument.formula', 'icon' => 'math'),
|
||||
'odb' => array('type' => 'application/vnd.oasis.opendocument.database', 'icon' => 'base'),
|
||||
'odi' => array('type' => 'application/vnd.oasis.opendocument.image', 'icon' => 'draw'),
|
||||
'oga' => array('type' => 'audio/ogg', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'ogg' => array('type' => 'audio/ogg', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'ogv' => array('type' => 'video/ogg', 'icon' => 'video', 'groups' => array('video'), 'string' => 'video'),
|
||||
|
||||
'pct' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
|
||||
'pdf' => array('type' => 'application/pdf', 'icon' => 'pdf'),
|
||||
'php' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
|
||||
'pic' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
|
||||
'pict' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
|
||||
'png' => array('type' => 'image/png', 'icon' => 'png', 'groups' => array('image', 'web_image'), 'string' => 'image'),
|
||||
'pps' => array('type' => 'application/vnd.ms-powerpoint', 'icon' => 'powerpoint', 'groups' => array('presentation')),
|
||||
'ppt' => array('type' => 'application/vnd.ms-powerpoint', 'icon' => 'powerpoint', 'groups' => array('presentation')),
|
||||
'pptx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||
'icon' => 'powerpoint'),
|
||||
'pptm' => array('type' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'icon' => 'powerpoint'),
|
||||
'potx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
|
||||
'icon' => 'powerpoint'),
|
||||
'potm' => array('type' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', 'icon' => 'powerpoint'),
|
||||
'ppam' => array('type' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'icon' => 'powerpoint'),
|
||||
'ppsx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
|
||||
'icon' => 'powerpoint'),
|
||||
'ppsm' => array('type' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'icon' => 'powerpoint'),
|
||||
'ps' => array('type' => 'application/postscript', 'icon' => 'pdf'),
|
||||
'pub' => array('type' => 'application/x-mspublisher', 'icon' => 'publisher', 'groups' => array('presentation')),
|
||||
|
||||
'qt' => array('type' => 'video/quicktime', 'icon' => 'quicktime',
|
||||
'groups' => array('video', 'web_video'), 'string' => 'video'),
|
||||
'ra' => array('type' => 'audio/x-realaudio-plugin', 'icon' => 'audio',
|
||||
'groups' => array('audio', 'web_audio'), 'string' => 'audio'),
|
||||
'ram' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
|
||||
'groups' => array('audio'), 'string' => 'audio'),
|
||||
'rar' => array('type' => 'application/x-rar-compressed', 'icon' => 'archive',
|
||||
'groups' => array('archive'), 'string' => 'archive'),
|
||||
'rhb' => array('type' => 'text/xml', 'icon' => 'markup'),
|
||||
'rm' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
|
||||
'groups' => array('audio'), 'string' => 'audio'),
|
||||
'rmvb' => array('type' => 'application/vnd.rn-realmedia-vbr', 'icon' => 'video',
|
||||
'groups' => array('video'), 'string' => 'video'),
|
||||
'rtf' => array('type' => 'text/rtf', 'icon' => 'text', 'groups' => array('document')),
|
||||
'rtx' => array('type' => 'text/richtext', 'icon' => 'text'),
|
||||
'rv' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
|
||||
'groups' => array('video'), 'string' => 'video'),
|
||||
'sh' => array('type' => 'application/x-sh', 'icon' => 'sourcecode'),
|
||||
'sit' => array('type' => 'application/x-stuffit', 'icon' => 'archive',
|
||||
'groups' => array('archive'), 'string' => 'archive'),
|
||||
'smi' => array('type' => 'application/smil', 'icon' => 'text'),
|
||||
'smil' => array('type' => 'application/smil', 'icon' => 'text'),
|
||||
'sqt' => array('type' => 'text/xml', 'icon' => 'markup'),
|
||||
'svg' => array('type' => 'image/svg+xml', 'icon' => 'image',
|
||||
'groups' => array('image', 'web_image'), 'string' => 'image'),
|
||||
'svgz' => array('type' => 'image/svg+xml', 'icon' => 'image',
|
||||
'groups' => array('image', 'web_image'), 'string' => 'image'),
|
||||
'swa' => array('type' => 'application/x-director', 'icon' => 'flash'),
|
||||
'swf' => array('type' => 'application/x-shockwave-flash', 'icon' => 'flash', 'groups' => array('video', 'web_video')),
|
||||
'swfl' => array('type' => 'application/x-shockwave-flash', 'icon' => 'flash', 'groups' => array('video', 'web_video')),
|
||||
|
||||
'sxw' => array('type' => 'application/vnd.sun.xml.writer', 'icon' => 'writer'),
|
||||
'stw' => array('type' => 'application/vnd.sun.xml.writer.template', 'icon' => 'writer'),
|
||||
'sxc' => array('type' => 'application/vnd.sun.xml.calc', 'icon' => 'calc'),
|
||||
'stc' => array('type' => 'application/vnd.sun.xml.calc.template', 'icon' => 'calc'),
|
||||
'sxd' => array('type' => 'application/vnd.sun.xml.draw', 'icon' => 'draw'),
|
||||
'std' => array('type' => 'application/vnd.sun.xml.draw.template', 'icon' => 'draw'),
|
||||
'sxi' => array('type' => 'application/vnd.sun.xml.impress', 'icon' => 'impress'),
|
||||
'sti' => array('type' => 'application/vnd.sun.xml.impress.template', 'icon' => 'impress'),
|
||||
'sxg' => array('type' => 'application/vnd.sun.xml.writer.global', 'icon' => 'writer'),
|
||||
'sxm' => array('type' => 'application/vnd.sun.xml.math', 'icon' => 'math'),
|
||||
|
||||
'tar' => array('type' => 'application/x-tar', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
|
||||
'tif' => array('type' => 'image/tiff', 'icon' => 'tiff', 'groups' => array('image'), 'string' => 'image'),
|
||||
'tiff' => array('type' => 'image/tiff', 'icon' => 'tiff', 'groups' => array('image'), 'string' => 'image'),
|
||||
'tex' => array('type' => 'application/x-tex', 'icon' => 'text'),
|
||||
'texi' => array('type' => 'application/x-texinfo', 'icon' => 'text'),
|
||||
'texinfo' => array('type' => 'application/x-texinfo', 'icon' => 'text'),
|
||||
'tsv' => array('type' => 'text/tab-separated-values', 'icon' => 'text'),
|
||||
'txt' => array('type' => 'text/plain', 'icon' => 'text', 'defaulticon' => true),
|
||||
'wav' => array('type' => 'audio/wav', 'icon' => 'wav', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
'webm' => array('type' => 'video/webm', 'icon' => 'video', 'groups' => array('video'), 'string' => 'video'),
|
||||
'wmv' => array('type' => 'video/x-ms-wmv', 'icon' => 'wmv', 'groups' => array('video'), 'string' => 'video'),
|
||||
'asf' => array('type' => 'video/x-ms-asf', 'icon' => 'wmv', 'groups' => array('video'), 'string' => 'video'),
|
||||
'wma' => array('type' => 'audio/x-ms-wma', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
|
||||
|
||||
'xbk' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
|
||||
'xdp' => array('type' => 'application/pdf', 'icon' => 'pdf'),
|
||||
'xfd' => array('type' => 'application/pdf', 'icon' => 'pdf'),
|
||||
'xfdf' => array('type' => 'application/pdf', 'icon' => 'pdf'),
|
||||
|
||||
'xls' => array('type' => 'application/vnd.ms-excel', 'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
|
||||
'xlsx' => array('type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'icon' => 'spreadsheet'),
|
||||
'xlsm' => array('type' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
|
||||
'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
|
||||
'xltx' => array('type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
|
||||
'icon' => 'spreadsheet'),
|
||||
'xltm' => array('type' => 'application/vnd.ms-excel.template.macroEnabled.12', 'icon' => 'spreadsheet'),
|
||||
'xlsb' => array('type' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'icon' => 'spreadsheet'),
|
||||
'xlam' => array('type' => 'application/vnd.ms-excel.addin.macroEnabled.12', 'icon' => 'spreadsheet'),
|
||||
|
||||
'xml' => array('type' => 'application/xml', 'icon' => 'markup'),
|
||||
'xsl' => array('type' => 'text/xml', 'icon' => 'markup'),
|
||||
|
||||
'zip' => array('type' => 'application/zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the current types.
|
||||
*
|
||||
* @return array Associative array from extension to array of data about type
|
||||
*/
|
||||
public static function &get_types() {
|
||||
// If it was already done in this request, use cache.
|
||||
if (self::$cachedtypes) {
|
||||
return self::$cachedtypes;
|
||||
}
|
||||
|
||||
// Get defaults.
|
||||
$mimetypes = self::get_default_types();
|
||||
|
||||
// If there are no custom types, just return.
|
||||
$custom = self::get_custom_types();
|
||||
if (empty($custom)) {
|
||||
return $mimetypes;
|
||||
}
|
||||
|
||||
// Check value is an array.
|
||||
if (!is_array($custom)) {
|
||||
debugging('Invalid $CFG->customfiletypes (not array)', DEBUG_DEVELOPER);
|
||||
return $mimetypes;
|
||||
}
|
||||
|
||||
foreach ($custom as $customentry) {
|
||||
// Each entry is a stdClass object similar to the array values above.
|
||||
if (empty($customentry->extension)) {
|
||||
debugging('Invalid $CFG->customfiletypes entry (extension field required)',
|
||||
DEBUG_DEVELOPER);
|
||||
continue;
|
||||
}
|
||||
|
||||
// To delete a standard entry, set 'deleted' to true.
|
||||
if (!empty($customentry->deleted)) {
|
||||
unset($mimetypes[$customentry->extension]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check required fields.
|
||||
if (empty($customentry->type) || empty($customentry->icon)) {
|
||||
debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
|
||||
' (type and icon fields required)', DEBUG_DEVELOPER);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Build result array.
|
||||
$result = array('type' => $customentry->type, 'icon' => $customentry->icon);
|
||||
if (!empty($customentry->groups)) {
|
||||
if (!is_array($customentry->groups)) {
|
||||
debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
|
||||
' (groups field not array)', DEBUG_DEVELOPER);
|
||||
continue;
|
||||
}
|
||||
$result['groups'] = $customentry->groups;
|
||||
}
|
||||
if (!empty($customentry->string)) {
|
||||
if (!is_string($customentry->string)) {
|
||||
debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
|
||||
' (string field not string)', DEBUG_DEVELOPER);
|
||||
continue;
|
||||
}
|
||||
$result['string'] = $customentry->string;
|
||||
}
|
||||
if (!empty($customentry->defaulticon)) {
|
||||
if (!is_bool($customentry->defaulticon)) {
|
||||
debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
|
||||
' (defaulticon field not bool)', DEBUG_DEVELOPER);
|
||||
continue;
|
||||
}
|
||||
$result['defaulticon'] = $customentry->defaulticon;
|
||||
}
|
||||
if (!empty($customentry->customdescription)) {
|
||||
if (!is_string($customentry->customdescription)) {
|
||||
debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
|
||||
' (customdescription field not string)', DEBUG_DEVELOPER);
|
||||
continue;
|
||||
}
|
||||
// As the name suggests, this field is used only for custom entries.
|
||||
$result['customdescription'] = $customentry->customdescription;
|
||||
}
|
||||
|
||||
// Track whether it is a custom filetype or a modified existing
|
||||
// filetype.
|
||||
if (array_key_exists($customentry->extension, $mimetypes)) {
|
||||
$result['modified'] = true;
|
||||
} else {
|
||||
$result['custom'] = true;
|
||||
}
|
||||
|
||||
// Add result array to list.
|
||||
$mimetypes[$customentry->extension] = $result;
|
||||
}
|
||||
|
||||
self::$cachedtypes = $mimetypes;
|
||||
return self::$cachedtypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets custom types from config variable, after decoding the JSON if required.
|
||||
*
|
||||
* @return array Array of custom types (empty array if none)
|
||||
*/
|
||||
protected static function get_custom_types() {
|
||||
global $CFG;
|
||||
if (!empty($CFG->customfiletypes)) {
|
||||
if (is_array($CFG->customfiletypes)) {
|
||||
// You can define this as an array in config.php...
|
||||
return $CFG->customfiletypes;
|
||||
} else {
|
||||
// Or as a JSON string in the config table.
|
||||
return json_decode($CFG->customfiletypes);
|
||||
}
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the custom types into config variable, encoding into JSON.
|
||||
*
|
||||
* @param array $types Array of custom types
|
||||
* @throws coding_exception If the custom types are fixed in config.php.
|
||||
*/
|
||||
protected static function set_custom_types(array $types) {
|
||||
global $CFG;
|
||||
// Check the setting hasn't been forced.
|
||||
if (array_key_exists('customfiletypes', $CFG->config_php_settings)) {
|
||||
throw new coding_exception('Cannot set custom filetypes because they ' .
|
||||
'are defined in config.php');
|
||||
}
|
||||
if (empty($types)) {
|
||||
unset_config('customfiletypes');
|
||||
} else {
|
||||
set_config('customfiletypes', json_encode(array_values($types)));
|
||||
}
|
||||
|
||||
// Clear the cached type list.
|
||||
self::reset_caches();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the type cache. This is not needed in normal use as the
|
||||
* set_custom_types function automatically clears the cache. Intended for
|
||||
* use in unit tests.
|
||||
*/
|
||||
public static function reset_caches() {
|
||||
self::$cachedtypes = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default types that have been deleted. Returns an array containing
|
||||
* the defaults of all those types.
|
||||
*
|
||||
* @return array Array (same format as get_mimetypes_array)
|
||||
*/
|
||||
public static function get_deleted_types() {
|
||||
$defaults = self::get_default_types();
|
||||
$deleted = array();
|
||||
foreach (self::get_custom_types() as $customentry) {
|
||||
if (!empty($customentry->deleted)) {
|
||||
$deleted[$customentry->extension] = $defaults[$customentry->extension];
|
||||
}
|
||||
}
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new entry to the list of custom filetypes.
|
||||
*
|
||||
* @param string $extension File extension without dot, e.g. 'doc'
|
||||
* @param string $mimetype MIME type e.g. 'application/msword'
|
||||
* @param string $coreicon Core icon to use e.g. 'document'
|
||||
* @param array $groups Array of group strings that this type belongs to
|
||||
* @param string $corestring Custom lang string name in mimetypes.php
|
||||
* @param string $customdescription Custom description (plain text/multilang)
|
||||
* @param bool $defaulticon True if this should be the default icon for the type
|
||||
* @throws coding_exception If the extension already exists, or otherwise invalid
|
||||
*/
|
||||
public static function add_type($extension, $mimetype, $coreicon,
|
||||
array $groups = array(), $corestring = '', $customdescription = '',
|
||||
$defaulticon = false) {
|
||||
// Check for blank extensions or incorrectly including the dot.
|
||||
$extension = (string)$extension;
|
||||
if ($extension === '' || $extension[0] === '.') {
|
||||
throw new coding_exception('Invalid extension .' . $extension);
|
||||
}
|
||||
|
||||
// Check extension not already used.
|
||||
$mimetypes = get_mimetypes_array();
|
||||
if (array_key_exists($extension, $mimetypes)) {
|
||||
throw new coding_exception('Extension ' . $extension . ' already exists');
|
||||
}
|
||||
|
||||
// For default icon, check there isn't already something with default icon
|
||||
// set for that MIME type.
|
||||
if ($defaulticon) {
|
||||
foreach ($mimetypes as $type) {
|
||||
if ($type['type'] === $mimetype && !empty($type['defaulticon'])) {
|
||||
throw new coding_exception('MIME type ' . $mimetype .
|
||||
' already has a default icon set');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get existing custom filetype list.
|
||||
$customs = self::get_custom_types();
|
||||
|
||||
// Check if there's a 'deleted' entry for the extension, if so then get
|
||||
// rid of it.
|
||||
foreach ($customs as $key => $custom) {
|
||||
if ($custom->extension === $extension) {
|
||||
unset($customs[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Set up config record for new type.
|
||||
$newtype = self::create_config_record($extension, $mimetype, $coreicon, $groups,
|
||||
$corestring, $customdescription, $defaulticon);
|
||||
|
||||
// See if there's a default value with this extension.
|
||||
$needsadding = true;
|
||||
$defaults = self::get_default_types();
|
||||
if (array_key_exists($extension, $defaults)) {
|
||||
// If it has the same values, we don't need to add it.
|
||||
$defaultvalue = $defaults[$extension];
|
||||
$modified = (array)$newtype;
|
||||
unset($modified['extension']);
|
||||
ksort($defaultvalue);
|
||||
ksort($modified);
|
||||
if ($modified === $defaultvalue) {
|
||||
$needsadding = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Add to array and set in config.
|
||||
if ($needsadding) {
|
||||
$customs[] = $newtype;
|
||||
}
|
||||
self::set_custom_types($customs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an entry in the list of filetypes in config.
|
||||
*
|
||||
* @param string $extension File extension without dot, e.g. 'doc'
|
||||
* @param string $newextension New file extension (same if not changing)
|
||||
* @param string $mimetype MIME type e.g. 'application/msword'
|
||||
* @param string $coreicon Core icon to use e.g. 'document'
|
||||
* @param array $groups Array of group strings that this type belongs to
|
||||
* @param string $corestring Custom lang string name in mimetypes.php
|
||||
* @param string $customdescription Custom description (plain text/multilang)
|
||||
* @param bool $defaulticon True if this should be the default icon for the type
|
||||
* @throws coding_exception If the new extension already exists, or otherwise invalid
|
||||
*/
|
||||
public static function update_type($extension, $newextension, $mimetype, $coreicon,
|
||||
array $groups = array(), $corestring = '', $customdescription = '',
|
||||
$defaulticon = false) {
|
||||
|
||||
// Extension must exist.
|
||||
$extension = (string)$extension;
|
||||
$mimetypes = get_mimetypes_array();
|
||||
if (!array_key_exists($extension, $mimetypes)) {
|
||||
throw new coding_exception('Extension ' . $extension . ' not found');
|
||||
}
|
||||
|
||||
// If there's a new extension then this must not exist.
|
||||
$newextension = (string)$newextension;
|
||||
if ($newextension !== $extension) {
|
||||
if ($newextension === '' || $newextension[0] === '.') {
|
||||
throw new coding_exception('Invalid extension .' . $newextension);
|
||||
}
|
||||
if (array_key_exists($newextension, $mimetypes)) {
|
||||
throw new coding_exception('Extension ' . $newextension . ' already exists');
|
||||
}
|
||||
}
|
||||
|
||||
// For default icon, check there isn't already something with default icon
|
||||
// set for that MIME type (unless it's this).
|
||||
if ($defaulticon) {
|
||||
foreach ($mimetypes as $ext => $type) {
|
||||
if ($ext !== $extension && $type['type'] === $mimetype &&
|
||||
!empty($type['defaulticon'])) {
|
||||
throw new coding_exception('MIME type ' . $mimetype .
|
||||
' already has a default icon set');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the old extension and then add the new one (may be same). This
|
||||
// will correctly handle cases when a default type is involved.
|
||||
self::delete_type($extension);
|
||||
self::add_type($newextension, $mimetype, $coreicon, $groups, $corestring,
|
||||
$customdescription, $defaulticon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a file type from the config list (or, for a standard one, marks it
|
||||
* as deleted).
|
||||
*
|
||||
* @param string $extension File extension without dot, e.g. 'doc'
|
||||
* @throws coding_exception If the extension does not exist, or otherwise invalid
|
||||
*/
|
||||
public static function delete_type($extension) {
|
||||
// Extension must exist.
|
||||
$mimetypes = get_mimetypes_array();
|
||||
if (!array_key_exists($extension, $mimetypes)) {
|
||||
throw new coding_exception('Extension ' . $extension . ' not found');
|
||||
}
|
||||
|
||||
// Get existing custom filetype list.
|
||||
$customs = self::get_custom_types();
|
||||
|
||||
// Remove any entries for this extension.
|
||||
foreach ($customs as $key => $custom) {
|
||||
if ($custom->extension === $extension && empty($custom->deleted)) {
|
||||
unset($customs[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// If it was a standard entry (doesn't have 'custom' set) then add a
|
||||
// deleted marker.
|
||||
if (empty($mimetypes[$extension]['custom'])) {
|
||||
$customs[] = (object)array('extension' => $extension, 'deleted' => true);
|
||||
}
|
||||
|
||||
// Save and reset cache.
|
||||
self::set_custom_types($customs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverts a file type to the default. May only be called on types that have
|
||||
* default values. This will undelete the type if necessary or set its values.
|
||||
* If the type is already at default values, does nothing.
|
||||
*
|
||||
* @param string $extension File extension without dot, e.g. 'doc'
|
||||
* @return bool True if anything was changed, false if it was already default
|
||||
* @throws coding_exception If the extension is not a default type.
|
||||
*/
|
||||
public static function revert_type_to_default($extension) {
|
||||
$extension = (string)$extension;
|
||||
|
||||
// Check it actually is a default type.
|
||||
$defaults = self::get_default_types();
|
||||
if (!array_key_exists($extension, $defaults)) {
|
||||
throw new coding_exception('Extension ' . $extension . ' is not a default type');
|
||||
}
|
||||
|
||||
// Loop through all the custom settings.
|
||||
$changed = false;
|
||||
$customs = self::get_custom_types();
|
||||
foreach ($customs as $key => $customentry) {
|
||||
if ($customentry->extension === $extension) {
|
||||
unset($customs[$key]);
|
||||
$changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Save changes if any.
|
||||
if ($changed) {
|
||||
self::set_custom_types($customs);
|
||||
}
|
||||
return $changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts function parameters into a record for storing in the JSON value.
|
||||
*
|
||||
* @param string $extension File extension without dot, e.g. 'doc'
|
||||
* @param string $mimetype MIME type e.g. 'application/msword'
|
||||
* @param string $coreicon Core icon to use e.g. 'document'
|
||||
* @param array $groups Array of group strings that this type belongs to
|
||||
* @param string $corestring Custom lang string name in mimetypes.php
|
||||
* @param string $customdescription Custom description (plain text/multilang)
|
||||
* @param bool $defaulticon True if this should be the default icon for the type
|
||||
* @return stdClass Record matching the parameters
|
||||
*/
|
||||
protected static function create_config_record($extension, $mimetype,
|
||||
$coreicon, array $groups, $corestring, $customdescription, $defaulticon) {
|
||||
// Construct new entry.
|
||||
$newentry = (object)array('extension' => (string)$extension, 'type' => (string)$mimetype,
|
||||
'icon' => (string)$coreicon);
|
||||
if ($groups) {
|
||||
if (!is_array($groups)) {
|
||||
throw new coding_exception('Groups must be an array');
|
||||
}
|
||||
foreach ($groups as $group) {
|
||||
if (!is_string($group)) {
|
||||
throw new coding_exception('Groups must be an array of strings');
|
||||
}
|
||||
}
|
||||
$newentry->groups = $groups;
|
||||
}
|
||||
if ($corestring) {
|
||||
$newentry->string = (string)$corestring;
|
||||
}
|
||||
if ($customdescription) {
|
||||
$newentry->customdescription = (string)$customdescription;
|
||||
}
|
||||
if ($defaulticon) {
|
||||
$newentry->defaulticon = true;
|
||||
}
|
||||
return $newentry;
|
||||
}
|
||||
}
|
209
lib/filelib.php
209
lib/filelib.php
@ -1386,206 +1386,8 @@ function download_file_content($url, $headers=null, $postdata=null, $fullrespons
|
||||
* Unknown types should use the 'xxx' entry which includes defaults.
|
||||
*/
|
||||
function &get_mimetypes_array() {
|
||||
static $mimearray = array (
|
||||
'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown'),
|
||||
'3gp' => array ('type'=>'video/quicktime', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
|
||||
'7z' => array ('type'=>'application/x-7z-compressed', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
|
||||
'aac' => array ('type'=>'audio/aac', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'accdb' => array ('type'=>'application/msaccess', 'icon'=>'base'),
|
||||
'ai' => array ('type'=>'application/postscript', 'icon'=>'eps', 'groups'=>array('image'), 'string'=>'image'),
|
||||
'aif' => array ('type'=>'audio/x-aiff', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'aiff' => array ('type'=>'audio/x-aiff', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'aifc' => array ('type'=>'audio/x-aiff', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'applescript' => array ('type'=>'text/plain', 'icon'=>'text'),
|
||||
'asc' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
|
||||
'asm' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
|
||||
'au' => array ('type'=>'audio/au', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'avi' => array ('type'=>'video/x-ms-wm', 'icon'=>'avi', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
'bmp' => array ('type'=>'image/bmp', 'icon'=>'bmp', 'groups'=>array('image'), 'string'=>'image'),
|
||||
'c' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
|
||||
'cct' => array ('type'=>'shockwave/director', 'icon'=>'flash'),
|
||||
'cpp' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
|
||||
'cs' => array ('type'=>'application/x-csh', 'icon'=>'sourcecode'),
|
||||
'css' => array ('type'=>'text/css', 'icon'=>'text', 'groups'=>array('web_file')),
|
||||
'csv' => array ('type'=>'text/csv', 'icon'=>'spreadsheet', 'groups'=>array('spreadsheet')),
|
||||
'dv' => array ('type'=>'video/x-dv', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
|
||||
'dmg' => array ('type'=>'application/octet-stream', 'icon'=>'unknown'),
|
||||
|
||||
'doc' => array ('type'=>'application/msword', 'icon'=>'document', 'groups'=>array('document')),
|
||||
'bdoc' => array ('type'=>'application/x-digidoc', 'icon'=>'document', 'groups'=>array('archive')),
|
||||
'cdoc' => array ('type'=>'application/x-digidoc', 'icon'=>'document', 'groups'=>array('archive')),
|
||||
'ddoc' => array ('type'=>'application/x-digidoc', 'icon'=>'document', 'groups'=>array('archive')),
|
||||
'docx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'icon'=>'document', 'groups'=>array('document')),
|
||||
'docm' => array ('type'=>'application/vnd.ms-word.document.macroEnabled.12', 'icon'=>'document'),
|
||||
'dotx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'icon'=>'document'),
|
||||
'dotm' => array ('type'=>'application/vnd.ms-word.template.macroEnabled.12', 'icon'=>'document'),
|
||||
|
||||
'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
|
||||
'dif' => array ('type'=>'video/x-dv', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
|
||||
'dir' => array ('type'=>'application/x-director', 'icon'=>'flash'),
|
||||
'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
|
||||
'eps' => array ('type'=>'application/postscript', 'icon'=>'eps'),
|
||||
'epub' => array ('type'=>'application/epub+zip', 'icon'=>'epub', 'groups'=>array('document')),
|
||||
'fdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
|
||||
'flv' => array ('type'=>'video/x-flv', 'icon'=>'flash', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
'f4v' => array ('type'=>'video/mp4', 'icon'=>'flash', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
|
||||
'gallery' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
|
||||
'galleryitem' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
|
||||
'gallerycollection' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
|
||||
'gif' => array ('type'=>'image/gif', 'icon'=>'gif', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
|
||||
'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
|
||||
'tgz' => array ('type'=>'application/g-zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
|
||||
'gz' => array ('type'=>'application/g-zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
|
||||
'gzip' => array ('type'=>'application/g-zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
|
||||
'h' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
|
||||
'hpp' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
|
||||
'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
|
||||
'htc' => array ('type'=>'text/x-component', 'icon'=>'markup'),
|
||||
'html' => array ('type'=>'text/html', 'icon'=>'html', 'groups'=>array('web_file')),
|
||||
'xhtml'=> array ('type'=>'application/xhtml+xml', 'icon'=>'html', 'groups'=>array('web_file')),
|
||||
'htm' => array ('type'=>'text/html', 'icon'=>'html', 'groups'=>array('web_file')),
|
||||
'ico' => array ('type'=>'image/vnd.microsoft.icon', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
|
||||
'ics' => array ('type'=>'text/calendar', 'icon'=>'text'),
|
||||
'isf' => array ('type'=>'application/inspiration', 'icon'=>'isf'),
|
||||
'ist' => array ('type'=>'application/inspiration.template', 'icon'=>'isf'),
|
||||
'java' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
|
||||
'jar' => array ('type'=>'application/java-archive', 'icon' => 'archive'),
|
||||
'jcb' => array ('type'=>'text/xml', 'icon'=>'markup'),
|
||||
'jcl' => array ('type'=>'text/xml', 'icon'=>'markup'),
|
||||
'jcw' => array ('type'=>'text/xml', 'icon'=>'markup'),
|
||||
'jmt' => array ('type'=>'text/xml', 'icon'=>'markup'),
|
||||
'jmx' => array ('type'=>'text/xml', 'icon'=>'markup'),
|
||||
'jnlp' => array ('type'=>'application/x-java-jnlp-file', 'icon'=>'markup'),
|
||||
'jpe' => array ('type'=>'image/jpeg', 'icon'=>'jpeg', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
|
||||
'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'jpeg', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
|
||||
'jpg' => array ('type'=>'image/jpeg', 'icon'=>'jpeg', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
|
||||
'jqz' => array ('type'=>'text/xml', 'icon'=>'markup'),
|
||||
'js' => array ('type'=>'application/x-javascript', 'icon'=>'text', 'groups'=>array('web_file')),
|
||||
'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text'),
|
||||
'm' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
|
||||
'mbz' => array ('type'=>'application/vnd.moodle.backup', 'icon'=>'moodle'),
|
||||
'mdb' => array ('type'=>'application/x-msaccess', 'icon'=>'base'),
|
||||
'mht' => array ('type'=>'message/rfc822', 'icon'=>'archive'),
|
||||
'mhtml'=> array ('type'=>'message/rfc822', 'icon'=>'archive'),
|
||||
'mov' => array ('type'=>'video/quicktime', 'icon'=>'quicktime', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
|
||||
'mw' => array ('type'=>'application/maple', 'icon'=>'math'),
|
||||
'mws' => array ('type'=>'application/maple', 'icon'=>'math'),
|
||||
'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'mp3', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'mp3' => array ('type'=>'audio/mp3', 'icon'=>'mp3', 'groups'=>array('audio','web_audio'), 'string'=>'audio'),
|
||||
'mp4' => array ('type'=>'video/mp4', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
'm4v' => array ('type'=>'video/mp4', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
'm4a' => array ('type'=>'audio/mp4', 'icon'=>'mp3', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
'mpe' => array ('type'=>'video/mpeg', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
'mpg' => array ('type'=>'video/mpeg', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
'mpr' => array ('type'=>'application/vnd.moodle.profiling', 'icon'=>'moodle'),
|
||||
|
||||
'nbk' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
|
||||
'notebook' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
|
||||
|
||||
'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'writer', 'groups'=>array('document')),
|
||||
'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'writer', 'groups'=>array('document')),
|
||||
'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'oth', 'groups'=>array('document')),
|
||||
'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'icon'=>'writer'),
|
||||
'odg' => array ('type'=>'application/vnd.oasis.opendocument.graphics', 'icon'=>'draw'),
|
||||
'otg' => array ('type'=>'application/vnd.oasis.opendocument.graphics-template', 'icon'=>'draw'),
|
||||
'odp' => array ('type'=>'application/vnd.oasis.opendocument.presentation', 'icon'=>'impress'),
|
||||
'otp' => array ('type'=>'application/vnd.oasis.opendocument.presentation-template', 'icon'=>'impress'),
|
||||
'ods' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet', 'icon'=>'calc', 'groups'=>array('spreadsheet')),
|
||||
'ots' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet-template', 'icon'=>'calc', 'groups'=>array('spreadsheet')),
|
||||
'odc' => array ('type'=>'application/vnd.oasis.opendocument.chart', 'icon'=>'chart'),
|
||||
'odf' => array ('type'=>'application/vnd.oasis.opendocument.formula', 'icon'=>'math'),
|
||||
'odb' => array ('type'=>'application/vnd.oasis.opendocument.database', 'icon'=>'base'),
|
||||
'odi' => array ('type'=>'application/vnd.oasis.opendocument.image', 'icon'=>'draw'),
|
||||
'oga' => array ('type'=>'audio/ogg', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'ogg' => array ('type'=>'audio/ogg', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'ogv' => array ('type'=>'video/ogg', 'icon'=>'video', 'groups'=>array('video'), 'string'=>'video'),
|
||||
|
||||
'pct' => array ('type'=>'image/pict', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
|
||||
'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
|
||||
'php' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
|
||||
'pic' => array ('type'=>'image/pict', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
|
||||
'pict' => array ('type'=>'image/pict', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
|
||||
'png' => array ('type'=>'image/png', 'icon'=>'png', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
|
||||
'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint', 'groups'=>array('presentation')),
|
||||
'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint', 'groups'=>array('presentation')),
|
||||
'pptx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'icon'=>'powerpoint'),
|
||||
'pptm' => array ('type'=>'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'icon'=>'powerpoint'),
|
||||
'potx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.template', 'icon'=>'powerpoint'),
|
||||
'potm' => array ('type'=>'application/vnd.ms-powerpoint.template.macroEnabled.12', 'icon'=>'powerpoint'),
|
||||
'ppam' => array ('type'=>'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'icon'=>'powerpoint'),
|
||||
'ppsx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'icon'=>'powerpoint'),
|
||||
'ppsm' => array ('type'=>'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'icon'=>'powerpoint'),
|
||||
'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf'),
|
||||
'pub' => array ('type'=>'application/x-mspublisher', 'icon'=>'publisher', 'groups'=>array('presentation')),
|
||||
|
||||
'qt' => array ('type'=>'video/quicktime', 'icon'=>'quicktime', 'groups'=>array('video','web_video'), 'string'=>'video'),
|
||||
'ra' => array ('type'=>'audio/x-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('audio','web_audio'), 'string'=>'audio'),
|
||||
'ram' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'rar' => array ('type'=>'application/x-rar-compressed', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
|
||||
'rhb' => array ('type'=>'text/xml', 'icon'=>'markup'),
|
||||
'rm' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'rmvb' => array ('type'=>'application/vnd.rn-realmedia-vbr', 'icon'=>'video', 'groups'=>array('video'), 'string'=>'video'),
|
||||
'rtf' => array ('type'=>'text/rtf', 'icon'=>'text', 'groups'=>array('document')),
|
||||
'rtx' => array ('type'=>'text/richtext', 'icon'=>'text'),
|
||||
'rv' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('video'), 'string'=>'video'),
|
||||
'sh' => array ('type'=>'application/x-sh', 'icon'=>'sourcecode'),
|
||||
'sit' => array ('type'=>'application/x-stuffit', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
|
||||
'smi' => array ('type'=>'application/smil', 'icon'=>'text'),
|
||||
'smil' => array ('type'=>'application/smil', 'icon'=>'text'),
|
||||
'sqt' => array ('type'=>'text/xml', 'icon'=>'markup'),
|
||||
'svg' => array ('type'=>'image/svg+xml', 'icon'=>'image', 'groups'=>array('image','web_image'), 'string'=>'image'),
|
||||
'svgz' => array ('type'=>'image/svg+xml', 'icon'=>'image', 'groups'=>array('image','web_image'), 'string'=>'image'),
|
||||
'swa' => array ('type'=>'application/x-director', 'icon'=>'flash'),
|
||||
'swf' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash', 'groups'=>array('video','web_video')),
|
||||
'swfl' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash', 'groups'=>array('video','web_video')),
|
||||
|
||||
'sxw' => array ('type'=>'application/vnd.sun.xml.writer', 'icon'=>'writer'),
|
||||
'stw' => array ('type'=>'application/vnd.sun.xml.writer.template', 'icon'=>'writer'),
|
||||
'sxc' => array ('type'=>'application/vnd.sun.xml.calc', 'icon'=>'calc'),
|
||||
'stc' => array ('type'=>'application/vnd.sun.xml.calc.template', 'icon'=>'calc'),
|
||||
'sxd' => array ('type'=>'application/vnd.sun.xml.draw', 'icon'=>'draw'),
|
||||
'std' => array ('type'=>'application/vnd.sun.xml.draw.template', 'icon'=>'draw'),
|
||||
'sxi' => array ('type'=>'application/vnd.sun.xml.impress', 'icon'=>'impress'),
|
||||
'sti' => array ('type'=>'application/vnd.sun.xml.impress.template', 'icon'=>'impress'),
|
||||
'sxg' => array ('type'=>'application/vnd.sun.xml.writer.global', 'icon'=>'writer'),
|
||||
'sxm' => array ('type'=>'application/vnd.sun.xml.math', 'icon'=>'math'),
|
||||
|
||||
'tar' => array ('type'=>'application/x-tar', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
|
||||
'tif' => array ('type'=>'image/tiff', 'icon'=>'tiff', 'groups'=>array('image'), 'string'=>'image'),
|
||||
'tiff' => array ('type'=>'image/tiff', 'icon'=>'tiff', 'groups'=>array('image'), 'string'=>'image'),
|
||||
'tex' => array ('type'=>'application/x-tex', 'icon'=>'text'),
|
||||
'texi' => array ('type'=>'application/x-texinfo', 'icon'=>'text'),
|
||||
'texinfo' => array ('type'=>'application/x-texinfo', 'icon'=>'text'),
|
||||
'tsv' => array ('type'=>'text/tab-separated-values', 'icon'=>'text'),
|
||||
'txt' => array ('type'=>'text/plain', 'icon'=>'text', 'defaulticon'=>true),
|
||||
'wav' => array ('type'=>'audio/wav', 'icon'=>'wav', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
'webm' => array ('type'=>'video/webm', 'icon'=>'video', 'groups'=>array('video'), 'string'=>'video'),
|
||||
'wmv' => array ('type'=>'video/x-ms-wmv', 'icon'=>'wmv', 'groups'=>array('video'), 'string'=>'video'),
|
||||
'asf' => array ('type'=>'video/x-ms-asf', 'icon'=>'wmv', 'groups'=>array('video'), 'string'=>'video'),
|
||||
'wma' => array ('type'=>'audio/x-ms-wma', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
|
||||
|
||||
'xbk' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
|
||||
'xdp' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
|
||||
'xfd' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
|
||||
'xfdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
|
||||
|
||||
'xls' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'spreadsheet', 'groups'=>array('spreadsheet')),
|
||||
'xlsx' => array ('type'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'icon'=>'spreadsheet'),
|
||||
'xlsm' => array ('type'=>'application/vnd.ms-excel.sheet.macroEnabled.12', 'icon'=>'spreadsheet', 'groups'=>array('spreadsheet')),
|
||||
'xltx' => array ('type'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'icon'=>'spreadsheet'),
|
||||
'xltm' => array ('type'=>'application/vnd.ms-excel.template.macroEnabled.12', 'icon'=>'spreadsheet'),
|
||||
'xlsb' => array ('type'=>'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'icon'=>'spreadsheet'),
|
||||
'xlam' => array ('type'=>'application/vnd.ms-excel.addin.macroEnabled.12', 'icon'=>'spreadsheet'),
|
||||
|
||||
'xml' => array ('type'=>'application/xml', 'icon'=>'markup'),
|
||||
'xsl' => array ('type'=>'text/xml', 'icon'=>'markup'),
|
||||
|
||||
'zip' => array ('type'=>'application/zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive')
|
||||
);
|
||||
return $mimearray;
|
||||
// Get types from the core_filetypes function, which includes caching.
|
||||
return core_filetypes::get_types();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1862,7 +1664,12 @@ function get_mimetype_description($obj, $capitalise=false) {
|
||||
// MIME types may include + symbol but this is not permitted in string ids.
|
||||
$safemimetype = str_replace('+', '_', $mimetype);
|
||||
$safemimetypestr = str_replace('+', '_', $mimetypestr);
|
||||
if (get_string_manager()->string_exists($safemimetype, 'mimetypes')) {
|
||||
$customdescription = mimeinfo('customdescription', $filename);
|
||||
if ($customdescription) {
|
||||
// Call format_string on the custom description so that multilang
|
||||
// filter can be used (if enabled).
|
||||
$result = format_string($customdescription);
|
||||
} else if (get_string_manager()->string_exists($safemimetype, 'mimetypes')) {
|
||||
$result = get_string($safemimetype, 'mimetypes', (object)$a);
|
||||
} else if (get_string_manager()->string_exists($safemimetypestr, 'mimetypes')) {
|
||||
$result = get_string($safemimetypestr, 'mimetypes', (object)$a);
|
||||
|
@ -205,6 +205,8 @@ class phpunit_util extends testing_util {
|
||||
core_text::reset_caches();
|
||||
get_message_processors(false, true);
|
||||
filter_manager::reset_caches();
|
||||
core_filetypes::reset_caches();
|
||||
|
||||
// Reset internal users.
|
||||
core_user::reset_internal_users();
|
||||
|
||||
|
@ -824,4 +824,58 @@ EOF;
|
||||
// Test it does nothing to the 'plain' data.
|
||||
$this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the get_mimetype_description function.
|
||||
*/
|
||||
public function test_get_mimetype_description() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Test example type (.doc).
|
||||
$this->assertEquals(get_string('application/msword', 'mimetypes'),
|
||||
get_mimetype_description(array('filename' => 'test.doc')));
|
||||
|
||||
// Test an unknown file type.
|
||||
$this->assertEquals(get_string('document/unknown', 'mimetypes'),
|
||||
get_mimetype_description(array('filename' => 'test.frog')));
|
||||
|
||||
// Test a custom filetype with no lang string specified.
|
||||
core_filetypes::add_type('frog', 'application/x-frog', 'document');
|
||||
$this->assertEquals('application/x-frog',
|
||||
get_mimetype_description(array('filename' => 'test.frog')));
|
||||
|
||||
// Test custom description.
|
||||
core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document',
|
||||
array(), '', 'Froggy file');
|
||||
$this->assertEquals('Froggy file',
|
||||
get_mimetype_description(array('filename' => 'test.frog')));
|
||||
|
||||
// Test custom description using multilang filter.
|
||||
filter_set_global_state('multilang', TEXTFILTER_ON);
|
||||
filter_set_applies_to_strings('multilang', true);
|
||||
core_filetypes::update_type('frog', 'frog', 'application/x-frog', 'document',
|
||||
array(), '', '<span lang="en" class="multilang">Green amphibian</span>' .
|
||||
'<span lang="fr" class="multilang">Amphibian vert</span>');
|
||||
$this->assertEquals('Green amphibian',
|
||||
get_mimetype_description(array('filename' => 'test.frog')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the get_mimetypes_array function.
|
||||
*/
|
||||
public function test_get_mimetypes_array() {
|
||||
$mimeinfo = get_mimetypes_array();
|
||||
|
||||
// Test example MIME type (doc).
|
||||
$this->assertEquals('application/msword', $mimeinfo['doc']['type']);
|
||||
$this->assertEquals('document', $mimeinfo['doc']['icon']);
|
||||
$this->assertEquals(array('document'), $mimeinfo['doc']['groups']);
|
||||
$this->assertFalse(isset($mimeinfo['doc']['string']));
|
||||
$this->assertFalse(isset($mimeinfo['doc']['defaulticon']));
|
||||
$this->assertFalse(isset($mimeinfo['doc']['customdescription']));
|
||||
|
||||
// Check the less common fields using other examples.
|
||||
$this->assertEquals('image', $mimeinfo['png']['string']);
|
||||
$this->assertEquals(true, $mimeinfo['txt']['defaulticon']);
|
||||
}
|
||||
}
|
||||
|
253
lib/tests/filetypes_test.php
Normal file
253
lib/tests/filetypes_test.php
Normal file
@ -0,0 +1,253 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Unit tests for /lib/classes/filetypes.php.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->libdir . '/filelib.php');
|
||||
|
||||
/**
|
||||
* Unit tests for /lib/classes/filetypes.php.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2014 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class core_filetypes_testcase extends advanced_testcase {
|
||||
|
||||
public function test_add_type() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Check the filetypes to be added do not exist yet (basically this
|
||||
// ensures we're testing the cache clear).
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertArrayNotHasKey('frog', $types);
|
||||
$this->assertArrayNotHasKey('zombie', $types);
|
||||
|
||||
// Add two filetypes (minimal, then all options).
|
||||
core_filetypes::add_type('frog', 'application/x-frog', 'document');
|
||||
core_filetypes::add_type('zombie', 'application/x-zombie', 'document',
|
||||
array('document', 'image'), 'image', 'A zombie', true);
|
||||
|
||||
// Check they now exist, and check data.
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertEquals('application/x-frog', $types['frog']['type']);
|
||||
$this->assertEquals('document', $types['frog']['icon']);
|
||||
$this->assertEquals(array('document', 'image'), $types['zombie']['groups']);
|
||||
$this->assertEquals('image', $types['zombie']['string']);
|
||||
$this->assertEquals(true, $types['zombie']['defaulticon']);
|
||||
$this->assertEquals('A zombie', $types['zombie']['customdescription']);
|
||||
|
||||
// Test adding again causes exception.
|
||||
try {
|
||||
core_filetypes::add_type('frog', 'application/x-frog', 'document');
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('already exists', $e->getMessage());
|
||||
$this->assertContains('frog', $e->getMessage());
|
||||
}
|
||||
|
||||
// Test bogus extension causes exception.
|
||||
try {
|
||||
core_filetypes::add_type('.frog', 'application/x-frog', 'document');
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('Invalid extension', $e->getMessage());
|
||||
$this->assertContains('..frog', $e->getMessage());
|
||||
}
|
||||
try {
|
||||
core_filetypes::add_type('', 'application/x-frog', 'document');
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('Invalid extension', $e->getMessage());
|
||||
}
|
||||
|
||||
// Test there is an exception if you add something with defaulticon when
|
||||
// there is already a type that has it.
|
||||
try {
|
||||
core_filetypes::add_type('gecko', 'text/plain', 'document',
|
||||
array(), '', '', true);
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('default icon set', $e->getMessage());
|
||||
$this->assertContains('text/plain', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function test_update_type() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Check previous value for the MIME type of Word documents.
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertEquals('application/msword', $types['doc']['type']);
|
||||
|
||||
// Change it.
|
||||
core_filetypes::update_type('doc', 'doc', 'application/x-frog', 'document');
|
||||
|
||||
// Check the MIME type is now set and also the other (not specified)
|
||||
// options, like groups, were removed.
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertEquals('application/x-frog', $types['doc']['type']);
|
||||
$this->assertArrayNotHasKey('groups', $types['doc']);
|
||||
|
||||
// This time change the extension.
|
||||
core_filetypes::update_type('doc', 'docccc', 'application/x-frog', 'document');
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertEquals('application/x-frog', $types['docccc']['type']);
|
||||
$this->assertArrayNotHasKey('doc', $types);
|
||||
|
||||
// Test unknown extension.
|
||||
try {
|
||||
core_filetypes::update_type('doc', 'doc', 'application/x-frog', 'document');
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('not found', $e->getMessage());
|
||||
$this->assertContains('doc', $e->getMessage());
|
||||
}
|
||||
|
||||
// Test bogus extension causes exception.
|
||||
try {
|
||||
core_filetypes::update_type('docccc', '.frog', 'application/x-frog', 'document');
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('Invalid extension', $e->getMessage());
|
||||
$this->assertContains('.frog', $e->getMessage());
|
||||
}
|
||||
try {
|
||||
core_filetypes::update_type('docccc', '', 'application/x-frog', 'document');
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('Invalid extension', $e->getMessage());
|
||||
}
|
||||
|
||||
// Test defaulticon changes.
|
||||
try {
|
||||
core_filetypes::update_type('docccc', 'docccc', 'text/plain', 'document',
|
||||
array(), '', '', true);
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('default icon set', $e->getMessage());
|
||||
$this->assertContains('text/plain', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function test_delete_type() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Filetype exists.
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertArrayHasKey('doc', $types);
|
||||
|
||||
// Remove it.
|
||||
core_filetypes::delete_type('doc');
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertArrayNotHasKey('doc', $types);
|
||||
|
||||
// Test removing one that doesn't exist causes exception.
|
||||
try {
|
||||
core_filetypes::delete_type('doc');
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('not found', $e->getMessage());
|
||||
$this->assertContains('doc', $e->getMessage());
|
||||
}
|
||||
|
||||
// Try a custom type (slightly different).
|
||||
core_filetypes::add_type('frog', 'application/x-frog', 'document');
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertArrayHasKey('frog', $types);
|
||||
core_filetypes::delete_type('frog');
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertArrayNotHasKey('frog', $types);
|
||||
}
|
||||
|
||||
public function test_revert_type_to_default() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Delete and then revert.
|
||||
core_filetypes::delete_type('doc');
|
||||
$this->assertArrayNotHasKey('doc', get_mimetypes_array());
|
||||
core_filetypes::revert_type_to_default('doc');
|
||||
$this->assertArrayHasKey('doc', get_mimetypes_array());
|
||||
|
||||
// Update and then revert.
|
||||
core_filetypes::update_type('asm', 'asm', 'text/plain', 'sourcecode', array(), '', 'An asm file');
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertEquals('An asm file', $types['asm']['customdescription']);
|
||||
core_filetypes::revert_type_to_default('asm');
|
||||
$types = get_mimetypes_array();
|
||||
$this->assertArrayNotHasKey('customdescription', $types['asm']);
|
||||
|
||||
// Test reverting a non-default type causes exception.
|
||||
try {
|
||||
core_filetypes::revert_type_to_default('frog');
|
||||
$this->fail();
|
||||
} catch (coding_exception $e) {
|
||||
$this->assertContains('not a default type', $e->getMessage());
|
||||
$this->assertContains('frog', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the logic cleans up the variable by deleting parts that are
|
||||
* no longer needed.
|
||||
*/
|
||||
public function test_cleanup() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest();
|
||||
|
||||
// The custom filetypes setting is empty to start with.
|
||||
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
|
||||
|
||||
// Add a custom filetype, then delete it.
|
||||
core_filetypes::add_type('frog', 'application/x-frog', 'document');
|
||||
$this->assertObjectHasAttribute('customfiletypes', $CFG);
|
||||
core_filetypes::delete_type('frog');
|
||||
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
|
||||
|
||||
// Change a standard filetype, then change it back.
|
||||
core_filetypes::update_type('asm', 'asm', 'text/plain', 'document');
|
||||
$this->assertObjectHasAttribute('customfiletypes', $CFG);
|
||||
core_filetypes::update_type('asm', 'asm', 'text/plain', 'sourcecode');
|
||||
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
|
||||
|
||||
// Delete a standard filetype, then add it back (the same).
|
||||
core_filetypes::delete_type('asm');
|
||||
$this->assertObjectHasAttribute('customfiletypes', $CFG);
|
||||
core_filetypes::add_type('asm', 'text/plain', 'sourcecode');
|
||||
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
|
||||
|
||||
// Revert a changed type.
|
||||
core_filetypes::update_type('asm', 'asm', 'text/plain', 'document');
|
||||
$this->assertObjectHasAttribute('customfiletypes', $CFG);
|
||||
core_filetypes::revert_type_to_default('asm');
|
||||
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
|
||||
|
||||
// Revert a deleted type.
|
||||
core_filetypes::delete_type('asm');
|
||||
$this->assertObjectHasAttribute('customfiletypes', $CFG);
|
||||
core_filetypes::revert_type_to_default('asm');
|
||||
$this->assertObjectNotHasAttribute('customfiletypes', $CFG);
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ information provided here is intended especially for developers.
|
||||
=== 2.9 ===
|
||||
|
||||
* \core\event\course_viewed 'other' argument renamed from coursesectionid to coursesectionnumber as it contains the section number.
|
||||
* New API core_filetypes::add_type (etc.) allows custom filetypes to be added and modified.
|
||||
* PHPUnit: PHPMailer Sink is now started for all tests and is setup within the phpunit wrapper for advanced tests.
|
||||
Catching debugging messages when sending mail will no longer work. Use $sink = $this->redirectEmails(); and then check
|
||||
the message in the sink instead.
|
||||
|
Loading…
x
Reference in New Issue
Block a user