Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

4367 lines (3891 sloc) 171.95 kb
<
<?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/>.
/**
* Functions for file handling.
*
* @package core_files
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* BYTESERVING_BOUNDARY - string unique string constant.
*/
define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7');
require_once("$CFG->libdir/filestorage/file_exceptions.php");
require_once("$CFG->libdir/filestorage/file_storage.php");
require_once("$CFG->libdir/filestorage/zip_packer.php");
require_once("$CFG->libdir/filebrowser/file_browser.php");
/**
* Encodes file serving url
*
* @deprecated use moodle_url factory methods instead
*
* @todo MDL-31071 deprecate this function
* @global stdClass $CFG
* @param string $urlbase
* @param string $path /filearea/itemid/dir/dir/file.exe
* @param bool $forcedownload
* @param bool $https https url required
* @return string encoded file url
*/
function file_encode_url($urlbase, $path, $forcedownload=false, $https=false) {
global $CFG;
//TODO: deprecate this
if ($CFG->slasharguments) {
$parts = explode('/', $path);
$parts = array_map('rawurlencode', $parts);
$path = implode('/', $parts);
$return = $urlbase.$path;
if ($forcedownload) {
$return .= '?forcedownload=1';
}
} else {
$path = rawurlencode($path);
$return = $urlbase.'?file='.$path;
if ($forcedownload) {
$return .= '&amp;forcedownload=1';
}
}
if ($https) {
$return = str_replace('http://', 'https://', $return);
}
return $return;
}
/**
* Prepares 'editor' formslib element from data in database
*
* The passed $data record must contain field foobar, foobarformat and optionally foobartrust. This
* function then copies the embedded files into draft area (assigning itemids automatically),
* creates the form element foobar_editor and rewrites the URLs so the embedded images can be
* displayed.
* In your mform definition, you must have an 'editor' element called foobar_editor. Then you call
* your mform's set_data() supplying the object returned by this function.
*
* @category files
* @param stdClass $data database field that holds the html text with embedded media
* @param string $field the name of the database field that holds the html text with embedded media
* @param array $options editor options (like maxifiles, maxbytes etc.)
* @param stdClass $context context of the editor
* @param string $component
* @param string $filearea file area name
* @param int $itemid item id, required if item exists
* @return stdClass modified data object
*/
function file_prepare_standard_editor($data, $field, array $options, $context=null, $component=null, $filearea=null, $itemid=null) {
$options = (array)$options;
if (!isset($options['trusttext'])) {
$options['trusttext'] = false;
}
if (!isset($options['forcehttps'])) {
$options['forcehttps'] = false;
}
if (!isset($options['subdirs'])) {
$options['subdirs'] = false;
}
if (!isset($options['maxfiles'])) {
$options['maxfiles'] = 0; // no files by default
}
if (!isset($options['noclean'])) {
$options['noclean'] = false;
}
//sanity check for passed context. This function doesn't expect $option['context'] to be set
//But this function is called before creating editor hence, this is one of the best places to check
//if context is used properly. This check notify developer that they missed passing context to editor.
if (isset($context) && !isset($options['context'])) {
//if $context is not null then make sure $option['context'] is also set.
debugging('Context for editor is not set in editoroptions. Hence editor will not respect editor filters', DEBUG_DEVELOPER);
} else if (isset($options['context']) && isset($context)) {
//If both are passed then they should be equal.
if ($options['context']->id != $context->id) {
$exceptionmsg = 'Editor context ['.$options['context']->id.'] is not equal to passed context ['.$context->id.']';
throw new coding_exception($exceptionmsg);
}
}
if (is_null($itemid) or is_null($context)) {
$contextid = null;
$itemid = null;
if (!isset($data)) {
$data = new stdClass();
}
if (!isset($data->{$field})) {
$data->{$field} = '';
}
if (!isset($data->{$field.'format'})) {
$data->{$field.'format'} = editors_get_preferred_format();
}
if (!$options['noclean']) {
$data->{$field} = clean_text($data->{$field}, $data->{$field.'format'});
}
} else {
if ($options['trusttext']) {
// noclean ignored if trusttext enabled
if (!isset($data->{$field.'trust'})) {
$data->{$field.'trust'} = 0;
}
$data = trusttext_pre_edit($data, $field, $context);
} else {
if (!$options['noclean']) {
$data->{$field} = clean_text($data->{$field}, $data->{$field.'format'});
}
}
$contextid = $context->id;
}
if ($options['maxfiles'] != 0) {
$draftid_editor = file_get_submitted_draft_itemid($field);
$currenttext = file_prepare_draft_area($draftid_editor, $contextid, $component, $filearea, $itemid, $options, $data->{$field});
$data->{$field.'_editor'} = array('text'=>$currenttext, 'format'=>$data->{$field.'format'}, 'itemid'=>$draftid_editor);
} else {
$data->{$field.'_editor'} = array('text'=>$data->{$field}, 'format'=>$data->{$field.'format'}, 'itemid'=>0);
}
return $data;
}
/**
* Prepares the content of the 'editor' form element with embedded media files to be saved in database
*
* This function moves files from draft area to the destination area and
* encodes URLs to the draft files so they can be safely saved into DB. The
* form has to contain the 'editor' element named foobar_editor, where 'foobar'
* is the name of the database field to hold the wysiwyg editor content. The
* editor data comes as an array with text, format and itemid properties. This
* function automatically adds $data properties foobar, foobarformat and
* foobartrust, where foobar has URL to embedded files encoded.
*
* @category files
* @param stdClass $data raw data submitted by the form
* @param string $field name of the database field containing the html with embedded media files
* @param array $options editor options (trusttext, subdirs, maxfiles, maxbytes etc.)
* @param stdClass $context context, required for existing data
* @param string $component file component
* @param string $filearea file area name
* @param int $itemid item id, required if item exists
* @return stdClass modified data object
*/
function file_postupdate_standard_editor($data, $field, array $options, $context, $component=null, $filearea=null, $itemid=null) {
$options = (array)$options;
if (!isset($options['trusttext'])) {
$options['trusttext'] = false;
}
if (!isset($options['forcehttps'])) {
$options['forcehttps'] = false;
}
if (!isset($options['subdirs'])) {
$options['subdirs'] = false;
}
if (!isset($options['maxfiles'])) {
$options['maxfiles'] = 0; // no files by default
}
if (!isset($options['maxbytes'])) {
$options['maxbytes'] = 0; // unlimited
}
if ($options['trusttext']) {
$data->{$field.'trust'} = trusttext_trusted($context);
} else {
$data->{$field.'trust'} = 0;
}
$editor = $data->{$field.'_editor'};
if ($options['maxfiles'] == 0 or is_null($filearea) or is_null($itemid) or empty($editor['itemid'])) {
$data->{$field} = $editor['text'];
} else {
$data->{$field} = file_save_draft_area_files($editor['itemid'], $context->id, $component, $filearea, $itemid, $options, $editor['text'], $options['forcehttps']);
}
$data->{$field.'format'} = $editor['format'];
return $data;
}
/**
* Saves text and files modified by Editor formslib element
*
* @category files
* @param stdClass $data $database entry field
* @param string $field name of data field
* @param array $options various options
* @param stdClass $context context - must already exist
* @param string $component
* @param string $filearea file area name
* @param int $itemid must already exist, usually means data is in db
* @return stdClass modified data obejct
*/
function file_prepare_standard_filemanager($data, $field, array $options, $context=null, $component=null, $filearea=null, $itemid=null) {
$options = (array)$options;
if (!isset($options['subdirs'])) {
$options['subdirs'] = false;
}
if (is_null($itemid) or is_null($context)) {
$itemid = null;
$contextid = null;
} else {
$contextid = $context->id;
}
$draftid_editor = file_get_submitted_draft_itemid($field.'_filemanager');
file_prepare_draft_area($draftid_editor, $contextid, $component, $filearea, $itemid, $options);
$data->{$field.'_filemanager'} = $draftid_editor;
return $data;
}
/**
* Saves files modified by File manager formslib element
*
* @todo MDL-31073 review this function
* @category files
* @param stdClass $data $database entry field
* @param string $field name of data field
* @param array $options various options
* @param stdClass $context context - must already exist
* @param string $component
* @param string $filearea file area name
* @param int $itemid must already exist, usually means data is in db
* @return stdClass modified data obejct
*/
function file_postupdate_standard_filemanager($data, $field, array $options, $context, $component, $filearea, $itemid) {
$options = (array)$options;
if (!isset($options['subdirs'])) {
$options['subdirs'] = false;
}
if (!isset($options['maxfiles'])) {
$options['maxfiles'] = -1; // unlimited
}
if (!isset($options['maxbytes'])) {
$options['maxbytes'] = 0; // unlimited
}
if (empty($data->{$field.'_filemanager'})) {
$data->$field = '';
} else {
file_save_draft_area_files($data->{$field.'_filemanager'}, $context->id, $component, $filearea, $itemid, $options);
$fs = get_file_storage();
if ($fs->get_area_files($context->id, $component, $filearea, $itemid)) {
$data->$field = '1'; // TODO: this is an ugly hack (skodak)
} else {
$data->$field = '';
}
}
return $data;
}
/**
* Generate a draft itemid
*
* @category files
* @global moodle_database $DB
* @global stdClass $USER
* @return int a random but available draft itemid that can be used to create a new draft
* file area.
*/
function file_get_unused_draft_itemid() {
global $DB, $USER;
if (isguestuser() or !isloggedin()) {
// guests and not-logged-in users can not be allowed to upload anything!!!!!!
print_error('noguest');
}
$contextid = get_context_instance(CONTEXT_USER, $USER->id)->id;
$fs = get_file_storage();
$draftitemid = rand(1, 999999999);
while ($files = $fs->get_area_files($contextid, 'user', 'draft', $draftitemid)) {
$draftitemid = rand(1, 999999999);
}
return $draftitemid;
}
/**
* Initialise a draft file area from a real one by copying the files. A draft
* area will be created if one does not already exist. Normally you should
* get $draftitemid by calling file_get_submitted_draft_itemid('elementname');
*
* @category files
* @global stdClass $CFG
* @global stdClass $USER
* @param int $draftitemid the id of the draft area to use, or 0 to create a new one, in which case this parameter is updated.
* @param int $contextid This parameter and the next two identify the file area to copy files from.
* @param string $component
* @param string $filearea helps indentify the file area.
* @param int $itemid helps identify the file area. Can be null if there are no files yet.
* @param array $options text and file options ('subdirs'=>false, 'forcehttps'=>false)
* @param string $text some html content that needs to have embedded links rewritten to point to the draft area.
* @return string|null returns string if $text was passed in, the rewritten $text is returned. Otherwise NULL.
*/
function file_prepare_draft_area(&$draftitemid, $contextid, $component, $filearea, $itemid, array $options=null, $text=null) {
global $CFG, $USER, $CFG;
$options = (array)$options;
if (!isset($options['subdirs'])) {
$options['subdirs'] = false;
}
if (!isset($options['forcehttps'])) {
$options['forcehttps'] = false;
}
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();
if (empty($draftitemid)) {
// create a new area and copy existing files into
$draftitemid = file_get_unused_draft_itemid();
$file_record = array('contextid'=>$usercontext->id, 'component'=>'user', 'filearea'=>'draft', 'itemid'=>$draftitemid);
if (!is_null($itemid) and $files = $fs->get_area_files($contextid, $component, $filearea, $itemid)) {
foreach ($files as $file) {
if ($file->is_directory() and $file->get_filepath() === '/') {
// we need a way to mark the age of each draft area,
// by not copying the root dir we force it to be created automatically with current timestamp
continue;
}
if (!$options['subdirs'] and ($file->is_directory() or $file->get_filepath() !== '/')) {
continue;
}
$draftfile = $fs->create_file_from_storedfile($file_record, $file);
// XXX: This is a hack for file manager (MDL-28666)
// File manager needs to know the original file information before copying
// to draft area, so we append these information in mdl_files.source field
// {@link file_storage::search_references()}
// {@link file_storage::search_references_count()}
$sourcefield = $file->get_source();
$newsourcefield = new stdClass;
$newsourcefield->source = $sourcefield;
$original = new stdClass;
$original->contextid = $contextid;
$original->component = $component;
$original->filearea = $filearea;
$original->itemid = $itemid;
$original->filename = $file->get_filename();
$original->filepath = $file->get_filepath();
$newsourcefield->original = file_storage::pack_reference($original);
$draftfile->set_source(serialize($newsourcefield));
// End of file manager hack
}
}
if (!is_null($text)) {
// at this point there should not be any draftfile links yet,
// because this is a new text from database that should still contain the @@pluginfile@@ links
// this happens when developers forget to post process the text
$text = str_replace("\"$CFG->httpswwwroot/draftfile.php", "\"$CFG->httpswwwroot/brokenfile.php#", $text);
}
} else {
// nothing to do
}
if (is_null($text)) {
return null;
}
// relink embedded files - editor can not handle @@PLUGINFILE@@ !
return file_rewrite_pluginfile_urls($text, 'draftfile.php', $usercontext->id, 'user', 'draft', $draftitemid, $options);
}
/**
* Convert encoded URLs in $text from the @@PLUGINFILE@@/... form to an actual URL.
*
* @category files
* @global stdClass $CFG
* @param string $text The content that may contain ULRs in need of rewriting.
* @param string $file The script that should be used to serve these files. pluginfile.php, draftfile.php, etc.
* @param int $contextid This parameter and the next two identify the file area to use.
* @param string $component
* @param string $filearea helps identify the file area.
* @param int $itemid helps identify the file area.
* @param array $options text and file options ('forcehttps'=>false)
* @return string the processed text.
*/
function file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $filearea, $itemid, array $options=null) {
global $CFG;
$options = (array)$options;
if (!isset($options['forcehttps'])) {
$options['forcehttps'] = false;
}
if (!$CFG->slasharguments) {
$file = $file . '?file=';
}
$baseurl = "$CFG->wwwroot/$file/$contextid/$component/$filearea/";
if ($itemid !== null) {
$baseurl .= "$itemid/";
}
if ($options['forcehttps']) {
$baseurl = str_replace('http://', 'https://', $baseurl);
}
return str_replace('@@PLUGINFILE@@/', $baseurl, $text);
}
/**
* Returns information about files in a draft area.
*
* @global stdClass $CFG
* @global stdClass $USER
* @param int $draftitemid the draft area item id.
* @return array with the following entries:
* 'filecount' => number of files in the draft area.
* (more information will be added as needed).
*/
function file_get_draft_area_info($draftitemid) {
global $CFG, $USER;
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();
$results = array();
// The number of files
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
$results['filecount'] = count($draftfiles);
$results['filesize'] = 0;
foreach ($draftfiles as $file) {
$results['filesize'] += $file->get_filesize();
}
return $results;
}
/**
* Get used space of files
* @global moodle_database $DB
* @global stdClass $USER
* @return int total bytes
*/
function file_get_user_used_space() {
global $DB, $USER;
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$sql = "SELECT SUM(files1.filesize) AS totalbytes FROM {files} files1
JOIN (SELECT contenthash, filename, MAX(id) AS id
FROM {files}
WHERE contextid = ? AND component = ? AND filearea != ?
GROUP BY contenthash, filename) files2 ON files1.id = files2.id";
$params = array('contextid'=>$usercontext->id, 'component'=>'user', 'filearea'=>'draft');
$record = $DB->get_record_sql($sql, $params);
return (int)$record->totalbytes;
}
/**
* Convert any string to a valid filepath
* @todo review this function
* @param string $str
* @return string path
*/
function file_correct_filepath($str) { //TODO: what is this? (skodak)
if ($str == '/' or empty($str)) {
return '/';
} else {
return '/'.trim($str, './@#$ ').'/';
}
}
/**
* Generate a folder tree of draft area of current USER recursively
*
* @todo MDL-31073 use normal return value instead, this does not fit the rest of api here (skodak)
* @param int $draftitemid
* @param string $filepath
* @param mixed $data
*/
function file_get_drafarea_folders($draftitemid, $filepath, &$data) {
global $USER, $OUTPUT, $CFG;
$data->children = array();
$context = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();
if ($files = $fs->get_directory_files($context->id, 'user', 'draft', $draftitemid, $filepath, false)) {
foreach ($files as $file) {
if ($file->is_directory()) {
$item = new stdClass();
$item->sortorder = $file->get_sortorder();
$item->filepath = $file->get_filepath();
$foldername = explode('/', trim($item->filepath, '/'));
$item->fullname = trim(array_pop($foldername), '/');
$item->id = uniqid();
file_get_drafarea_folders($draftitemid, $item->filepath, $item);
$data->children[] = $item;
} else {
continue;
}
}
}
}
/**
* Listing all files (including folders) in current path (draft area)
* used by file manager
* @param int $draftitemid
* @param string $filepath
* @return stdClass
*/
function file_get_drafarea_files($draftitemid, $filepath = '/') {
global $USER, $OUTPUT, $CFG;
$context = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();
$data = new stdClass();
$data->path = array();
$data->path[] = array('name'=>get_string('files'), 'path'=>'/');
// will be used to build breadcrumb
$trail = '/';
if ($filepath !== '/') {
$filepath = file_correct_filepath($filepath);
$parts = explode('/', $filepath);
foreach ($parts as $part) {
if ($part != '' && $part != null) {
$trail .= ($part.'/');
$data->path[] = array('name'=>$part, 'path'=>$trail);
}
}
}
$list = array();
$maxlength = 12;
if ($files = $fs->get_directory_files($context->id, 'user', 'draft', $draftitemid, $filepath, false)) {
foreach ($files as $file) {
$item = new stdClass();
$item->filename = $file->get_filename();
$item->filepath = $file->get_filepath();
$item->fullname = trim($item->filename, '/');
$filesize = $file->get_filesize();
$item->size = $filesize ? $filesize : null;
$item->filesize = $filesize ? display_size($filesize) : '';
$item->sortorder = $file->get_sortorder();
$item->author = $file->get_author();
$item->license = $file->get_license();
$item->datemodified = $file->get_timemodified();
$item->datecreated = $file->get_timecreated();
$item->isref = $file->is_external_file();
if ($item->isref && $file->get_status() == 666) {
$item->originalmissing = true;
}
// find the file this draft file was created from and count all references in local
// system pointing to that file
$source = @unserialize($file->get_source());
if (isset($source->original)) {
$item->refcount = $fs->search_references_count($source->original);
}
if ($file->is_directory()) {
$item->filesize = 0;
$item->icon = $OUTPUT->pix_url(file_folder_icon(24))->out(false);
$item->type = 'folder';
$foldername = explode('/', trim($item->filepath, '/'));
$item->fullname = trim(array_pop($foldername), '/');
$item->thumbnail = $OUTPUT->pix_url(file_folder_icon(90))->out(false);
} else {
// do NOT use file browser here!
$item->mimetype = get_mimetype_description($file);
if (file_extension_in_typegroup($file->get_filename(), 'archive')) {
$item->type = 'zip';
} else {
$item->type = 'file';
}
$itemurl = moodle_url::make_draftfile_url($draftitemid, $item->filepath, $item->filename);
$item->url = $itemurl->out();
$item->icon = $OUTPUT->pix_url(file_file_icon($file, 24))->out(false);
$item->thumbnail = $OUTPUT->pix_url(file_file_icon($file, 90))->out(false);
if ($imageinfo = $file->get_imageinfo()) {
$item->realthumbnail = $itemurl->out(false, array('preview' => 'thumb', 'oid' => $file->get_timemodified()));
$item->realicon = $itemurl->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
$item->image_width = $imageinfo['width'];
$item->image_height = $imageinfo['height'];
}
}
$list[] = $item;
}
}
$data->itemid = $draftitemid;
$data->list = $list;
return $data;
}
/**
* Returns draft area itemid for a given element.
*
* @category files
* @param string $elname name of formlib editor element, or a hidden form field that stores the draft area item id, etc.
* @return int the itemid, or 0 if there is not one yet.
*/
function file_get_submitted_draft_itemid($elname) {
// this is a nasty hack, ideally all new elements should use arrays here or there should be a new parameter
if (!isset($_REQUEST[$elname])) {
return 0;
}
if (is_array($_REQUEST[$elname])) {
$param = optional_param_array($elname, 0, PARAM_INT);
if (!empty($param['itemid'])) {
$param = $param['itemid'];
} else {
debugging('Missing itemid, maybe caused by unset maxfiles option', DEBUG_DEVELOPER);
return false;
}
} else {
$param = optional_param($elname, 0, PARAM_INT);
}
if ($param) {
require_sesskey();
}
return $param;
}
/**
* Restore the original source field from draft files
*
* @param stored_file $storedfile This only works with draft files
* @return stored_file
*/
function file_restore_source_field_from_draft_file($storedfile) {
$source = @unserialize($storedfile->get_source());
if (!empty($source)) {
if (is_object($source)) {
$restoredsource = $source->source;
$storedfile->set_source($restoredsource);
} else {
throw new moodle_exception('invalidsourcefield', 'error');
}
}
return $storedfile;
}
/**
* Saves files from a draft file area to a real one (merging the list of files).
* Can rewrite URLs in some content at the same time if desired.
*
* @category files
* @global stdClass $USER
* @param int $draftitemid the id of the draft area to use. Normally obtained
* from file_get_submitted_draft_itemid('elementname') or similar.
* @param int $contextid This parameter and the next two identify the file area to save to.
* @param string $component
* @param string $filearea indentifies the file area.
* @param int $itemid helps identifies the file area.
* @param array $options area options (subdirs=>false, maxfiles=-1, maxbytes=0)
* @param string $text some html content that needs to have embedded links rewritten
* to the @@PLUGINFILE@@ form for saving in the database.
* @param bool $forcehttps force https urls.
* @return string|null if $text was passed in, the rewritten $text is returned. Otherwise NULL.
*/
function file_save_draft_area_files($draftitemid, $contextid, $component, $filearea, $itemid, array $options=null, $text=null, $forcehttps=false) {
global $USER;
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();
$options = (array)$options;
if (!isset($options['subdirs'])) {
$options['subdirs'] = false;
}
if (!isset($options['maxfiles'])) {
$options['maxfiles'] = -1; // unlimited
}
if (!isset($options['maxbytes']) || $options['maxbytes'] == USER_CAN_IGNORE_FILE_SIZE_LIMITS) {
$options['maxbytes'] = 0; // unlimited
}
$allowreferences = true;
if (isset($options['return_types']) && !($options['return_types'] & FILE_REFERENCE)) {
// we assume that if $options['return_types'] is NOT specified, we DO allow references.
// this is not exactly right. BUT there are many places in code where filemanager options
// are not passed to file_save_draft_area_files()
$allowreferences = false;
}
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id');
$oldfiles = $fs->get_area_files($contextid, $component, $filearea, $itemid, 'id');
if (count($draftfiles) < 2) {
// means there are no files - one file means root dir only ;-)
$fs->delete_area_files($contextid, $component, $filearea, $itemid);
} else if (count($oldfiles) < 2) {
$filecount = 0;
// there were no files before - one file means root dir only ;-)
foreach ($draftfiles as $file) {
$file_record = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid);
if (!$options['subdirs']) {
if ($file->get_filepath() !== '/' or $file->is_directory()) {
continue;
}
}
if ($options['maxbytes'] and $options['maxbytes'] < $file->get_filesize()) {
// oversized file - should not get here at all
continue;
}
if ($options['maxfiles'] != -1 and $options['maxfiles'] <= $filecount) {
// more files - should not get here at all
break;
}
if (!$file->is_directory()) {
$filecount++;
}
if ($file->is_external_file()) {
if (!$allowreferences) {
continue;
}
$repoid = $file->get_repository_id();
if (!empty($repoid)) {
$file_record['repositoryid'] = $repoid;
$file_record['reference'] = $file->get_reference();
}
}
file_restore_source_field_from_draft_file($file);
$fs->create_file_from_storedfile($file_record, $file);
}
} else {
// we have to merge old and new files - we want to keep file ids for files that were not changed
// we change time modified for all new and changed files, we keep time created as is
$newhashes = array();
foreach ($draftfiles as $file) {
$newhash = $fs->get_pathname_hash($contextid, $component, $filearea, $itemid, $file->get_filepath(), $file->get_filename());
file_restore_source_field_from_draft_file($file);
$newhashes[$newhash] = $file;
}
$filecount = 0;
foreach ($oldfiles as $oldfile) {
$oldhash = $oldfile->get_pathnamehash();
if (!isset($newhashes[$oldhash])) {
// delete files not needed any more - deleted by user
$oldfile->delete();
continue;
}
$newfile = $newhashes[$oldhash];
// status changed, we delete old file, and create a new one
if ($oldfile->get_status() != $newfile->get_status()) {
// file was changed, use updated with new timemodified data
$oldfile->delete();
// This file will be added later
continue;
}
// Replaced file content
if ($oldfile->get_contenthash() != $newfile->get_contenthash()) {
$oldfile->replace_content_with($newfile);
}
// Updated author
if ($oldfile->get_author() != $newfile->get_author()) {
$oldfile->set_author($newfile->get_author());
}
// Updated license
if ($oldfile->get_license() != $newfile->get_license()) {
$oldfile->set_license($newfile->get_license());
}
// Updated file source
if ($oldfile->get_source() != $newfile->get_source()) {
$oldfile->set_source($newfile->get_source());
}
// Updated sort order
if ($oldfile->get_sortorder() != $newfile->get_sortorder()) {
$oldfile->set_sortorder($newfile->get_sortorder());
}
// Update file size
if ($oldfile->get_filesize() != $newfile->get_filesize()) {
$oldfile->set_filesize($newfile->get_filesize());
}
// Update file timemodified
if ($oldfile->get_timemodified() != $newfile->get_timemodified()) {
$oldfile->set_timemodified($newfile->get_timemodified());
}
// unchanged file or directory - we keep it as is
unset($newhashes[$oldhash]);
if (!$oldfile->is_directory()) {
$filecount++;
}
}
// Add fresh file or the file which has changed status
// the size and subdirectory tests are extra safety only, the UI should prevent it
foreach ($newhashes as $file) {
$file_record = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'timemodified'=>time());
if (!$options['subdirs']) {
if ($file->get_filepath() !== '/' or $file->is_directory()) {
continue;
}
}
if ($options['maxbytes'] and $options['maxbytes'] < $file->get_filesize()) {
// oversized file - should not get here at all
continue;
}
if ($options['maxfiles'] != -1 and $options['maxfiles'] <= $filecount) {
// more files - should not get here at all
break;
}
if (!$file->is_directory()) {
$filecount++;
}
if ($file->is_external_file()) {
if (!$allowreferences) {
continue;
}
$repoid = $file->get_repository_id();
if (!empty($repoid)) {
$file_record['repositoryid'] = $repoid;
$file_record['reference'] = $file->get_reference();
}
}
$fs->create_file_from_storedfile($file_record, $file);
}
}
// note: do not purge the draft area - we clean up areas later in cron,
// the reason is that user might press submit twice and they would loose the files,
// also sometimes we might want to use hacks that save files into two different areas
if (is_null($text)) {
return null;
} else {
return file_rewrite_urls_to_pluginfile($text, $draftitemid, $forcehttps);
}
}
/**
* Convert the draft file area URLs in some content to @@PLUGINFILE@@ tokens
* ready to be saved in the database. Normally, this is done automatically by
* {@link file_save_draft_area_files()}.
*
* @category files
* @param string $text the content to process.
* @param int $draftitemid the draft file area the content was using.
* @param bool $forcehttps whether the content contains https URLs. Default false.
* @return string the processed content.
*/
function file_rewrite_urls_to_pluginfile($text, $draftitemid, $forcehttps = false) {
global $CFG, $USER;
$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$wwwroot = $CFG->wwwroot;
if ($forcehttps) {
$wwwroot = str_replace('http://', 'https://', $wwwroot);
}
// relink embedded files if text submitted - no absolute links allowed in database!
$text = str_ireplace("$wwwroot/draftfile.php/$usercontext->id/user/draft/$draftitemid/", '@@PLUGINFILE@@/', $text);
if (strpos($text, 'draftfile.php?file=') !== false) {
$matches = array();
preg_match_all("!$wwwroot/draftfile.php\?file=%2F{$usercontext->id}%2Fuser%2Fdraft%2F{$draftitemid}%2F[^'\",&<>|`\s:\\\\]+!iu", $text, $matches);
if ($matches) {
foreach ($matches[0] as $match) {
$replace = str_ireplace('%2F', '/', $match);
$text = str_replace($match, $replace, $text);
}
}
$text = str_ireplace("$wwwroot/draftfile.php?file=/$usercontext->id/user/draft/$draftitemid/", '@@PLUGINFILE@@/', $text);
}
return $text;
}
/**
* Set file sort order
*
* @global moodle_database $DB
* @param int $contextid the context id
* @param string $component file component
* @param string $filearea file area.
* @param int $itemid itemid.
* @param string $filepath file path.
* @param string $filename file name.
* @param int $sortorder the sort order of file.
* @return bool
*/
function file_set_sortorder($contextid, $component, $filearea, $itemid, $filepath, $filename, $sortorder) {
global $DB;
$conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename);
if ($file_record = $DB->get_record('files', $conditions)) {
$sortorder = (int)$sortorder;
$file_record->sortorder = $sortorder;
$DB->update_record('files', $file_record);
return true;
}
return false;
}
/**
* reset file sort order number to 0
* @global moodle_database $DB
* @param int $contextid the context id
* @param string $component
* @param string $filearea file area.
* @param int|bool $itemid itemid.
* @return bool
*/
function file_reset_sortorder($contextid, $component, $filearea, $itemid=false) {
global $DB;
$conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea);
if ($itemid !== false) {
$conditions['itemid'] = $itemid;
}
$file_records = $DB->get_records('files', $conditions);
foreach ($file_records as $file_record) {
$file_record->sortorder = 0;
$DB->update_record('files', $file_record);
}
return true;
}
/**
* Returns description of upload error
*
* @param int $errorcode found in $_FILES['filename.ext']['error']
* @return string error description string, '' if ok
*/
function file_get_upload_error($errorcode) {
switch ($errorcode) {
case 0: // UPLOAD_ERR_OK - no error
$errmessage = '';
break;
case 1: // UPLOAD_ERR_INI_SIZE
$errmessage = get_string('uploadserverlimit');
break;
case 2: // UPLOAD_ERR_FORM_SIZE
$errmessage = get_string('uploadformlimit');
break;
case 3: // UPLOAD_ERR_PARTIAL
$errmessage = get_string('uploadpartialfile');
break;
case 4: // UPLOAD_ERR_NO_FILE
$errmessage = get_string('uploadnofilefound');
break;
// Note: there is no error with a value of 5
case 6: // UPLOAD_ERR_NO_TMP_DIR
$errmessage = get_string('uploadnotempdir');
break;
case 7: // UPLOAD_ERR_CANT_WRITE
$errmessage = get_string('uploadcantwrite');
break;
case 8: // UPLOAD_ERR_EXTENSION
$errmessage = get_string('uploadextension');
break;
default:
$errmessage = get_string('uploadproblem');
}
return $errmessage;
}
/**
* Recursive function formating an array in POST parameter
* @param array $arraydata - the array that we are going to format and add into &$data array
* @param string $currentdata - a row of the final postdata array at instant T
* when finish, it's assign to $data under this format: name[keyname][][]...[]='value'
* @param array $data - the final data array containing all POST parameters : 1 row = 1 parameter
*/
function format_array_postdata_for_curlcall($arraydata, $currentdata, &$data) {
foreach ($arraydata as $k=>$v) {
$newcurrentdata = $currentdata;
if (is_array($v)) { //the value is an array, call the function recursively
$newcurrentdata = $newcurrentdata.'['.urlencode($k).']';
format_array_postdata_for_curlcall($v, $newcurrentdata, $data);
} else { //add the POST parameter to the $data array
$data[] = $newcurrentdata.'['.urlencode($k).']='.urlencode($v);
}
}
}
/**
* Transform a PHP array into POST parameter
* (see the recursive function format_array_postdata_for_curlcall)
* @param array $postdata
* @return array containing all POST parameters (1 row = 1 POST parameter)
*/
function format_postdata_for_curlcall($postdata) {
$data = array();
foreach ($postdata as $k=>$v) {
if (is_array($v)) {
$currentdata = urlencode($k);
format_array_postdata_for_curlcall($v, $currentdata, $data);
} else {
$data[] = urlencode($k).'='.urlencode($v);
}
}
$convertedpostdata = implode('&', $data);
return $convertedpostdata;
}
/**
* Fetches content of file from Internet (using proxy if defined). Uses cURL extension if present.
* Due to security concerns only downloads from http(s) sources are supported.
*
* @todo MDL-31073 add version test for '7.10.5'
* @category files
* @param string $url file url starting with http(s)://
* @param array $headers http headers, null if none. If set, should be an
* associative array of header name => value pairs.
* @param array $postdata array means use POST request with given parameters
* @param bool $fullresponse return headers, responses, etc in a similar way snoopy does
* (if false, just returns content)
* @param int $timeout timeout for complete download process including all file transfer
* (default 5 minutes)
* @param int $connecttimeout timeout for connection to server; this is the timeout that
* usually happens if the remote server is completely down (default 20 seconds);
* may not work when using proxy
* @param bool $skipcertverify If true, the peer's SSL certificate will not be checked.
* Only use this when already in a trusted location.
* @param string $tofile store the downloaded content to file instead of returning it.
* @param bool $calctimeout false by default, true enables an extra head request to try and determine
* filesize and appropriately larger timeout based on $CFG->curltimeoutkbitrate
* @return mixed false if request failed or content of the file as string if ok. True if file downloaded into $tofile successfully.
*/
function download_file_content($url, $headers=null, $postdata=null, $fullresponse=false, $timeout=300, $connecttimeout=20, $skipcertverify=false, $tofile=NULL, $calctimeout=false) {
global $CFG;
// some extra security
$newlines = array("\r", "\n");
if (is_array($headers) ) {
foreach ($headers as $key => $value) {
$headers[$key] = str_replace($newlines, '', $value);
}
}
$url = str_replace($newlines, '', $url);
if (!preg_match('|^https?://|i', $url)) {
if ($fullresponse) {
$response = new stdClass();
$response->status = 0;
$response->headers = array();
$response->response_code = 'Invalid protocol specified in url';
$response->results = '';
$response->error = 'Invalid protocol specified in url';
return $response;
} else {
return false;
}
}
// check if proxy (if used) should be bypassed for this url
$proxybypass = is_proxybypass($url);
if (!$ch = curl_init($url)) {
debugging('Can not init curl.');
return false;
}
// set extra headers
if (is_array($headers) ) {
$headers2 = array();
foreach ($headers as $key => $value) {
$headers2[] = "$key: $value";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2);
}
if ($skipcertverify) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
// use POST if requested
if (is_array($postdata)) {
$postdata = format_postdata_for_curlcall($postdata);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
if (!ini_get('open_basedir') and !ini_get('safe_mode')) {
// TODO: add version test for '7.10.5'
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
}
if (!empty($CFG->proxyhost) and !$proxybypass) {
// SOCKS supported in PHP5 only
if (!empty($CFG->proxytype) and ($CFG->proxytype == 'SOCKS5')) {
if (defined('CURLPROXY_SOCKS5')) {
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
} else {
curl_close($ch);
if ($fullresponse) {
$response = new stdClass();
$response->status = '0';
$response->headers = array();
$response->response_code = 'SOCKS5 proxy is not supported in PHP4';
$response->results = '';
$response->error = 'SOCKS5 proxy is not supported in PHP4';
return $response;
} else {
debugging("SOCKS5 proxy is not supported in PHP4.", DEBUG_ALL);
return false;
}
}
}
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
if (empty($CFG->proxyport)) {
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost);
} else {
curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport);
}
if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
if (defined('CURLOPT_PROXYAUTH')) {
// any proxy authentication if PHP 5.1
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
}
}
}
// set up header and content handlers
$received = new stdClass();
$received->headers = array(); // received headers array
$received->tofile = $tofile;
$received->fh = null;
curl_setopt($ch, CURLOPT_HEADERFUNCTION, partial('download_file_content_header_handler', $received));
if ($tofile) {
curl_setopt($ch, CURLOPT_WRITEFUNCTION, partial('download_file_content_write_handler', $received));
}
if (!isset($CFG->curltimeoutkbitrate)) {
//use very slow rate of 56kbps as a timeout speed when not set
$bitrate = 56;
} else {
$bitrate = $CFG->curltimeoutkbitrate;
}
// try to calculate the proper amount for timeout from remote file size.
// if disabled or zero, we won't do any checks nor head requests.
if ($calctimeout && $bitrate > 0) {
//setup header request only options
curl_setopt_array ($ch, array(
CURLOPT_RETURNTRANSFER => false,
CURLOPT_NOBODY => true)
);
curl_exec($ch);
$info = curl_getinfo($ch);
$err = curl_error($ch);
if ($err === '' && $info['download_content_length'] > 0) { //no curl errors
$timeout = max($timeout, ceil($info['download_content_length'] * 8 / ($bitrate * 1024))); //adjust for large files only - take max timeout.
}
//reinstate affected curl options
curl_setopt_array ($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_NOBODY => false)
);
}
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$result = curl_exec($ch);
// try to detect encoding problems
if ((curl_errno($ch) == 23 or curl_errno($ch) == 61) and defined('CURLOPT_ENCODING')) {
curl_setopt($ch, CURLOPT_ENCODING, 'none');
$result = curl_exec($ch);
}
if ($received->fh) {
fclose($received->fh);
}
if (curl_errno($ch)) {
$error = curl_error($ch);
$error_no = curl_errno($ch);
curl_close($ch);
if ($fullresponse) {
$response = new stdClass();
if ($error_no == 28) {
$response->status = '-100'; // mimic snoopy
} else {
$response->status = '0';
}
$response->headers = array();
$response->response_code = $error;
$response->results = false;
$response->error = $error;
return $response;
} else {
debugging("cURL request for \"$url\" failed with: $error ($error_no)", DEBUG_ALL);
return false;
}
} else {
$info = curl_getinfo($ch);
curl_close($ch);
if (empty($info['http_code'])) {
// for security reasons we support only true http connections (Location: file:// exploit prevention)
$response = new stdClass();
$response->status = '0';
$response->headers = array();
$response->response_code = 'Unknown cURL error';
$response->results = false; // do NOT change this, we really want to ignore the result!
$response->error = 'Unknown cURL error';
} else {
$response = new stdClass();;
$response->status = (string)$info['http_code'];
$response->headers = $received->headers;
$response->response_code = $received->headers[0];
$response->results = $result;
$response->error = '';
}
if ($fullresponse) {
return $response;
} else if ($info['http_code'] != 200) {
debugging("cURL request for \"$url\" failed, HTTP response code: ".$response->response_code, DEBUG_ALL);
return false;
} else {
return $response->results;
}
}
}
/**
* internal implementation
* @param stdClass $received
* @param resource $ch
* @param mixed $header
* @return int header length
*/
function download_file_content_header_handler($received, $ch, $header) {
$received->headers[] = $header;
return strlen($header);
}
/**
* internal implementation
* @param stdClass $received
* @param resource $ch
* @param mixed $data
*/
function download_file_content_write_handler($received, $ch, $data) {
if (!$received->fh) {
$received->fh = fopen($received->tofile, 'w');
if ($received->fh === false) {
// bad luck, file creation or overriding failed
return 0;
}
}
if (fwrite($received->fh, $data) === false) {
// bad luck, write failed, let's abort completely
return 0;
}
return strlen($data);
}
/**
* Returns a list of information about file types based on extensions.
*
* The following elements expected in value array for each extension:
* 'type' - mimetype
* 'icon' - location of the icon file. If value is FILENAME, then either pix/f/FILENAME.gif
* or pix/f/FILENAME.png must be present in moodle and contain 16x16 filetype icon;
* also files with bigger sizes under names
* FILENAME-24, FILENAME-32, FILENAME-64, FILENAME-128, FILENAME-256 are recommended.
* 'groups' (optional) - array of filetype groups this filetype extension is part of;
* commonly used in moodle the following groups:
* - web_image - image that can be included as <img> in HTML
* - image - image that we can parse using GD to find it's dimensions, also used for portfolio format
* - video - file that can be imported as video in text editor
* - audio - file that can be imported as audio in text editor
* - archive - we can extract files from this archive
* - spreadsheet - used for portfolio format
* - document - used for portfolio format
* - presentation - used for portfolio format
* 'string' (optional) - the name of the string from lang/en/mimetypes.php that displays
* human-readable description for this filetype;
* Function {@link get_mimetype_description()} first looks at the presence of string for
* particular mimetype (value of 'type'), if not found looks for string specified in 'string'
* attribute, if not found returns the value of 'type';
* 'defaulticon' (boolean, optional) - used by function {@link file_mimetype_icon()} to find
* an icon for mimetype. If an entry with 'defaulticon' is not found for a particular mimetype,
* this function will return first found icon; Especially usefull for types such as 'text/plain'
*
* @category files
* @return array List of information about file types based on extensions.
* Associative array of extension (lower-case) to associative array
* from 'element name' to data. Current element names are 'type' and 'icon'.
* 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'),
'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')),
'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'),
'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'),
'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'),
'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'),
'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'),
'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'),
'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'),
'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'),
'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'),
'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'),
'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;
}
/**
* Obtains information about a filetype based on its extension. Will
* use a default if no information is present about that particular
* extension.
*
* @category files
* @param string $element Desired information (usually 'icon'
* for icon filename or 'type' for MIME type. Can also be
* 'icon24', ...32, 48, 64, 72, 80, 96, 128, 256)
* @param string $filename Filename we're looking up
* @return string Requested piece of information from array
*/
function mimeinfo($element, $filename) {
global $CFG;
$mimeinfo = & get_mimetypes_array();
static $iconpostfixes = array(256=>'-256', 128=>'-128', 96=>'-96', 80=>'-80', 72=>'-72', 64=>'-64', 48=>'-48', 32=>'-32', 24=>'-24', 16=>'');
$filetype = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (empty($filetype)) {
$filetype = 'xxx'; // file without extension
}
if (preg_match('/^icon(\d*)$/', $element, $iconsizematch)) {
$iconsize = max(array(16, (int)$iconsizematch[1]));
$filenames = array($mimeinfo['xxx']['icon']);
if ($filetype != 'xxx' && isset($mimeinfo[$filetype]['icon'])) {
array_unshift($filenames, $mimeinfo[$filetype]['icon']);
}
// find the file with the closest size, first search for specific icon then for default icon
foreach ($filenames as $filename) {
foreach ($iconpostfixes as $size => $postfix) {
$fullname = $CFG->dirroot.'/pix/f/'.$filename.$postfix;
if ($iconsize >= $size && (file_exists($fullname.'.png') || file_exists($fullname.'.gif'))) {
return $filename.$postfix;
}
}
}
} else if (isset($mimeinfo[$filetype][$element])) {
return $mimeinfo[$filetype][$element];
} else if (isset($mimeinfo['xxx'][$element])) {
return $mimeinfo['xxx'][$element]; // By default
} else {
return null;
}
}
/**
* Obtains information about a filetype based on the MIME type rather than
* the other way around.
*
* @category files
* @param string $element Desired information ('extension', 'icon', 'icon-24', etc.)
* @param string $mimetype MIME type we're looking up
* @return string Requested piece of information from array
*/
function mimeinfo_from_type($element, $mimetype) {
/* array of cached mimetype->extension associations */
static $cached = array();
$mimeinfo = & get_mimetypes_array();
if (!array_key_exists($mimetype, $cached)) {
$cached[$mimetype] = null;
foreach($mimeinfo as $filetype => $values) {
if ($values['type'] == $mimetype) {
if ($cached[$mimetype] === null) {
$cached[$mimetype] = '.'.$filetype;
}
if (!empty($values['defaulticon'])) {
$cached[$mimetype] = '.'.$filetype;
break;
}
}
}
if (empty($cached[$mimetype])) {
$cached[$mimetype] = '.xxx';
}
}
if ($element === 'extension') {
return $cached[$mimetype];
} else {
return mimeinfo($element, $cached[$mimetype]);
}
}
/**
* Return the relative icon path for a given file
*
* Usage:
* <code>
* // $file - instance of stored_file or file_info
* $icon = $OUTPUT->pix_url(file_file_icon($file))->out();
* echo html_writer::empty_tag('img', array('src' => $icon, 'alt' => get_mimetype_description($file)));
* </code>
* or
* <code>
* echo $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file));
* </code>
*
* @param stored_file|file_info|stdClass|array $file (in case of object attributes $file->filename
* and $file->mimetype are expected)
* @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
* @return string
*/
function file_file_icon($file, $size = null) {
if (!is_object($file)) {
$file = (object)$file;
}
if (isset($file->filename)) {
$filename = $file->filename;
} else if (method_exists($file, 'get_filename')) {
$filename = $file->get_filename();
} else if (method_exists($file, 'get_visible_name')) {
$filename = $file->get_visible_name();
} else {
$filename = '';
}
if (isset($file->mimetype)) {
$mimetype = $file->mimetype;
} else if (method_exists($file, 'get_mimetype')) {
$mimetype = $file->get_mimetype();
} else {
$mimetype = '';
}
$mimetypes = &get_mimetypes_array();
if ($filename) {
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if ($extension && !empty($mimetypes[$extension])) {
// if file name has known extension, return icon for this extension
return file_extension_icon($filename, $size);
}
}
return file_mimetype_icon($mimetype, $size);
}
/**
* Return the relative icon path for a folder image
*
* Usage:
* <code>
* $icon = $OUTPUT->pix_url(file_folder_icon())->out();
* echo html_writer::empty_tag('img', array('src' => $icon));
* </code>
* or
* <code>
* echo $OUTPUT->pix_icon(file_folder_icon(32));
* </code>
*
* @param int $iconsize The size of the icon. Defaults to 16 can also be 24, 32, 48, 64, 72, 80, 96, 128, 256
* @return string
*/
function file_folder_icon($iconsize = null) {
global $CFG;
static $iconpostfixes = array(256=>'-256', 128=>'-128', 96=>'-96', 80=>'-80', 72=>'-72', 64=>'-64', 48=>'-48', 32=>'-32', 24=>'-24', 16=>'');
static $cached = array();
$iconsize = max(array(16, (int)$iconsize));
if (!array_key_exists($iconsize, $cached)) {
foreach ($iconpostfixes as $size => $postfix) {
$fullname = $CFG->dirroot.'/pix/f/folder'.$postfix;
if ($iconsize >= $size && (file_exists($fullname.'.png') || file_exists($fullname.'.gif'))) {
$cached[$iconsize] = 'f/folder'.$postfix;
break;
}
}
}
return $cached[$iconsize];
}
/**
* Returns the relative icon path for a given mime type
*
* This function should be used in conjunction with $OUTPUT->pix_url to produce
* a return the full path to an icon.
*
* <code>
* $mimetype = 'image/jpg';
* $icon = $OUTPUT->pix_url(file_mimetype_icon($mimetype))->out();
* echo html_writer::empty_tag('img', array('src' => $icon, 'alt' => get_mimetype_description($mimetype)));
* </code>
*
* @category files
* @todo MDL-31074 When an $OUTPUT->icon method is available this function should be altered
* to conform with that.
* @param string $mimetype The mimetype to fetch an icon for
* @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
* @return string The relative path to the icon
*/
function file_mimetype_icon($mimetype, $size = NULL) {
return 'f/'.mimeinfo_from_type('icon'.$size, $mimetype);
}
/**
* Returns the relative icon path for a given file name
*
* This function should be used in conjunction with $OUTPUT->pix_url to produce
* a return the full path to an icon.
*
* <code>
* $filename = '.jpg';
* $icon = $OUTPUT->pix_url(file_extension_icon($filename))->out();
* echo html_writer::empty_tag('img', array('src' => $icon, 'alt' => '...'));
* </code>
*
* @todo MDL-31074 When an $OUTPUT->icon method is available this function should be altered
* to conform with that.
* @todo MDL-31074 Implement $size
* @category files
* @param string $filename The filename to get the icon for
* @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
* @return string
*/
function file_extension_icon($filename, $size = NULL) {
return 'f/'.mimeinfo('icon'.$size, $filename);
}
/**
* Obtains descriptions for file types (e.g. 'Microsoft Word document') from the
* mimetypes.php language file.
*
* @param mixed $obj - instance of stored_file or file_info or array/stdClass with field
* 'filename' and 'mimetype', or just a string with mimetype (though it is recommended to
* have filename); In case of array/stdClass the field 'mimetype' is optional.
* @param bool $capitalise If true, capitalises first character of result
* @return string Text description
*/
function get_mimetype_description($obj, $capitalise=false) {
$filename = $mimetype = '';
if (is_object($obj) && method_exists($obj, 'get_filename') && method_exists($obj, 'get_mimetype')) {
// this is an instance of stored_file
$mimetype = $obj->get_mimetype();
$filename = $obj->get_filename();
} else if (is_object($obj) && method_exists($obj, 'get_visible_name') && method_exists($obj, 'get_mimetype')) {
// this is an instance of file_info
$mimetype = $obj->get_mimetype();
$filename = $obj->get_visible_name();
} else if (is_array($obj) || is_object ($obj)) {
$obj = (array)$obj;
if (!empty($obj['filename'])) {
$filename = $obj['filename'];
}
if (!empty($obj['mimetype'])) {
$mimetype = $obj['mimetype'];
}
} else {
$mimetype = $obj;
}
$mimetypefromext = mimeinfo('type', $filename);
if (empty($mimetype) || $mimetypefromext !== 'document/unknown') {
// if file has a known extension, overwrite the specified mimetype
$mimetype = $mimetypefromext;
}
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (empty($extension)) {
$mimetypestr = mimeinfo_from_type('string', $mimetype);
$extension = str_replace('.', '', mimeinfo_from_type('extension', $mimetype));
} else {
$mimetypestr = mimeinfo('string', $filename);
}
$chunks = explode('/', $mimetype, 2);
$chunks[] = '';
$attr = array(
'mimetype' => $mimetype,
'ext' => $extension,
'mimetype1' => $chunks[0],
'mimetype2' => $chunks[1],
);
$a = array();
foreach ($attr as $key => $value) {
$a[$key] = $value;
$a[strtoupper($key)] = strtoupper($value);
$a[ucfirst($key)] = ucfirst($value);
}
if (get_string_manager()->string_exists($mimetype, 'mimetypes')) {
$result = get_string($mimetype, 'mimetypes', (object)$a);
} else if (get_string_manager()->string_exists($mimetypestr, 'mimetypes')) {
$result = get_string($mimetypestr, 'mimetypes', (object)$a);
} else if (get_string_manager()->string_exists('default', 'mimetypes')) {
$result = get_string('default', 'mimetypes', (object)$a);
} else {
$result = $mimetype;
}
if ($capitalise) {
$result=ucfirst($result);
}
return $result;
}
/**
* Returns array of elements of type $element in type group(s)
*
* @param string $element name of the element we are interested in, usually 'type' or 'extension'
* @param string|array $groups one group or array of groups/extensions/mimetypes
* @return array
*/
function file_get_typegroup($element, $groups) {
static $cached = array();
if (!is_array($groups)) {
$groups = array($groups);
}
if (!array_key_exists($element, $cached)) {
$cached[$element] = array();
}
$result = array();
foreach ($groups as $group) {
if (!array_key_exists($group, $cached[$element])) {
// retrieive and cache all elements of type $element for group $group
$mimeinfo = & get_mimetypes_array();
$cached[$element][$group] = array();
foreach ($mimeinfo as $extension => $value) {
$value['extension'] = '.'.$extension;
if (empty($value[$element])) {
continue;
}
if (($group === '.'.$extension || $group === $value['type'] ||
(!empty($value['groups']) && in_array($group, $value['groups']))) &&
!in_array($value[$element], $cached[$element][$group])) {
$cached[$element][$group][] = $value[$element];
}
}
}
$result = array_merge($result, $cached[$element][$group]);
}
return array_unique($result);
}
/**
* Checks if file with name $filename has one of the extensions in groups $groups
*
* @see get_mimetypes_array()
* @param string $filename name of the file to check
* @param string|array $groups one group or array of groups to check
* @param bool $checktype if true and extension check fails, find the mimetype and check if
* file mimetype is in mimetypes in groups $groups
* @return bool
*/
function file_extension_in_typegroup($filename, $groups, $checktype = false) {
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if (!empty($extension) && in_array('.'.strtolower($extension), file_get_typegroup('extension', $groups))) {
return true;
}
return $checktype && file_mimetype_in_typegroup(mimeinfo('type', $filename), $groups);
}
/**
* Checks if mimetype $mimetype belongs to one of the groups $groups
*
* @see get_mimetypes_array()
* @param string $mimetype
* @param string|array $groups one group or array of groups to check
* @return bool
*/
function file_mimetype_in_typegroup($mimetype, $groups) {
return !empty($mimetype) && in_array($mimetype, file_get_typegroup('type', $groups));
}
/**
* Requested file is not found or not accessible, does not return, terminates script
*
* @global stdClass $CFG
* @global stdClass $COURSE
*/
function send_file_not_found() {
global $CFG, $COURSE;
send_header_404();
print_error('filenotfound', 'error', $CFG->wwwroot.'/course/view.php?id='.$COURSE->id); //this is not displayed on IIS??
}
/**
* Helper function to send correct 404 for server.
*/
function send_header_404() {
if (substr(php_sapi_name(), 0, 3) == 'cgi') {
header("Status: 404 Not Found");
} else {
header('HTTP/1.0 404 not found');
}
}
/**
* Enhanced readfile() with optional acceleration.
* @param string|stored_file $file
* @param string $mimetype
* @param bool $accelerate
* @return void
*/
function readfile_accel($file, $mimetype, $accelerate) {
global $CFG;
if ($mimetype === 'text/plain') {
// there is no encoding specified in text files, we need something consistent
header('Content-Type: text/plain; charset=utf-8');
} else {
header('Content-Type: '.$mimetype);
}
$lastmodified = is_object($file) ? $file->get_timemodified() : filemtime($file);
header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
if (is_object($file)) {
header('ETag: ' . $file->get_contenthash());
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) and $_SERVER['HTTP_IF_NONE_MATCH'] === $file->get_contenthash()) {
header('HTTP/1.1 304 Not Modified');
return;
}
}
// if etag present for stored file rely on it exclusively
if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) and (empty($_SERVER['HTTP_IF_NONE_MATCH']) or !is_object($file))) {
// get unixtime of request header; clip extra junk off first
$since = strtotime(preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]));
if ($since && $since >= $lastmodified) {
header('HTTP/1.1 304 Not Modified');
return;
}
}
if ($accelerate and !empty($CFG->xsendfile)) {
if (empty($CFG->disablebyteserving) and $mimetype !== 'text/plain') {
header('Accept-Ranges: bytes');
} else {
header('Accept-Ranges: none');
}
if (is_object($file)) {
$fs = get_file_storage();
if ($fs->xsendfile($file->get_contenthash())) {
return;
}
} else {
require_once("$CFG->libdir/xsendfilelib.php");
if (xsendfile($file)) {
return;
}
}
}
$filesize = is_object($file) ? $file->get_filesize() : filesize($file);
header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
if ($accelerate and empty($CFG->disablebyteserving) and $mimetype !== 'text/plain') {
header('Accept-Ranges: bytes');
if (!empty($_SERVER['HTTP_RANGE']) and strpos($_SERVER['HTTP_RANGE'],'bytes=') !== FALSE) {
// byteserving stuff - for acrobat reader and download accelerators
// see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
// inspired by: http://www.coneural.org/florian/papers/04_byteserving.php
$ranges = false;
if (preg_match_all('/(\d*)-(\d*)/', $_SERVER['HTTP_RANGE'], $ranges, PREG_SET_ORDER)) {
foreach ($ranges as $key=>$value) {
if ($ranges[$key][1] == '') {
//suffix case
$ranges[$key][1] = $filesize - $ranges[$key][2];
$ranges[$key][2] = $filesize - 1;
} else if ($ranges[$key][2] == '' || $ranges[$key][2] > $filesize - 1) {
//fix range length
$ranges[$key][2] = $filesize - 1;
}
if ($ranges[$key][2] != '' && $ranges[$key][2] < $ranges[$key][1]) {
//invalid byte-range ==> ignore header
$ranges = false;
break;
}
//prepare multipart header
$ranges[$key][0] = "\r\n--".BYTESERVING_BOUNDARY."\r\nContent-Type: $mimetype\r\n";
$ranges[$key][0] .= "Content-Range: bytes {$ranges[$key][1]}-{$ranges[$key][2]}/$filesize\r\n\r\n";
}
} else {
$ranges = false;
}
if ($ranges) {
if (is_object($file)) {
$handle = $file->get_content_file_handle();
} else {
$handle = fopen($file, 'rb');
}
byteserving_send_file($handle, $mimetype, $ranges, $filesize);
}
}
} else {
// Do not byteserve
header('Accept-Ranges: none');
}
header('Content-Length: '.$filesize);
if ($filesize > 10000000) {
// for large files try to flush and close all buffers to conserve memory
while(@ob_get_level()) {
if (!@ob_end_flush()) {
break;
}
}
}
// send the whole file content
if (is_object($file)) {
$file->readfile();
} else {
readfile($file);
}
}
/**
* Similar to readfile_accel() but designed for strings.
* @param string $string
* @param string $mimetype
* @param bool $accelerate
* @return void
*/
function readstring_accel($string, $mimetype, $accelerate) {
global $CFG;
if ($mimetype === 'text/plain') {
// there is no encoding specified in text files, we need something consistent
header('Content-Type: text/plain; charset=utf-8');
} else {
header('Content-Type: '.$mimetype);
}
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
header('Accept-Ranges: none');
if ($accelerate and !empty($CFG->xsendfile)) {
$fs = get_file_storage();
if ($fs->xsendfile(sha1($string))) {
return;
}
}
header('Content-Length: '.strlen($string));
echo $string;
}
/**
* Handles the sending of temporary file to user, download is forced.
* File is deleted after abort or successful sending, does not return, script terminated
*
* @param string $path path to file, preferably from moodledata/temp/something; or content of file itself
* @param string $filename proposed file name when saving file
* @param bool $pathisstring If the path is string
*/
function send_temp_file($path, $filename, $pathisstring=false) {
global $CFG;
if (check_browser_version('Firefox', '1.5')) {
// only FF is known to correctly save to disk before opening...
$mimetype = mimeinfo('type', $filename);
} else {
$mimetype = 'application/x-forcedownload';
}
// close session - not needed anymore
session_get_instance()->write_close();
if (!$pathisstring) {
if (!file_exists($path)) {
send_header_404();
print_error('filenotfound', 'error', $CFG->wwwroot.'/');
}
// executed after normal finish or abort
@register_shutdown_function('send_temp_file_finished', $path);
}
// if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
if (check_browser_version('MSIE')) {
$filename = urlencode($filename);
}
header('Content-Disposition: attachment; filename="'.$filename.'"');
if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
header('Cache-Control: max-age=10');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: ');
} else { //normal http - prevent caching at all cost
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: no-cache');
}
// send the contents - we can not accelerate this because the file will be deleted asap
if ($pathisstring) {
readstring_accel($path, $mimetype, false);
} else {
readfile_accel($path, $mimetype, false);
@unlink($path);
}
die; //no more chars to output
}
/**
* Internal callback function used by send_temp_file()
*
* @param string $path
*/
function send_temp_file_finished($path) {
if (file_exists($path)) {
@unlink($path);
}
}
/**
* Handles the sending of file data to the user's browser, including support for
* byteranges etc.
*
* @category files
* @param string $path Path of file on disk (including real filename), or actual content of file as string
* @param string $filename Filename to send
* @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
* @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
* @param bool $pathisstring If true (default false), $path is the content to send and not the pathname
* @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
* @param string $mimetype Include to specify the MIME type; leave blank to have it guess the type from $filename
* @param bool $dontdie - return control to caller afterwards. this is not recommended and only used for cleanup tasks.
* if this is passed as true, ignore_user_abort is called. if you don't want your processing to continue on cancel,
* you must detect this case when control is returned using connection_aborted. Please not that session is closed
* and should not be reopened.
* @return null script execution stopped unless $dontdie is true
*/
function send_file($path, $filename, $lifetime = 'default' , $filter=0, $pathisstring=false, $forcedownload=false, $mimetype='', $dontdie=false) {
global $CFG, $COURSE;
if ($dontdie) {
ignore_user_abort(true);
}
// MDL-11789, apply $CFG->filelifetime here
if ($lifetime === 'default') {
if (!empty($CFG->filelifetime)) {
$lifetime = $CFG->filelifetime;
} else {
$lifetime = 86400;
}
}
session_get_instance()->write_close(); // unlock session during fileserving
// Use given MIME type if specified, otherwise guess it using mimeinfo.
// IE, Konqueror and Opera open html file directly in browser from web even when directed to save it to disk :-O
// only Firefox saves all files locally before opening when content-disposition: attachment stated
$isFF = check_browser_version('Firefox', '1.5'); // only FF > 1.5 properly tested
$mimetype = ($forcedownload and !$isFF) ? 'application/x-forcedownload' :
($mimetype ? $mimetype : mimeinfo('type', $filename));
// if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
if (check_browser_version('MSIE')) {
$filename = rawurlencode($filename);
}
if ($forcedownload) {
header('Content-Disposition: attachment; filename="'.$filename.'"');
} else {
header('Content-Disposition: inline; filename="'.$filename.'"');
}
if ($lifetime > 0) {
$nobyteserving = false;
header('Cache-Control: max-age='.$lifetime);
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Pragma: ');
} else { // Do not cache files in proxies and browsers
$nobyteserving = true;
if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
header('Cache-Control: max-age=10');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: ');
} else { //normal http - prevent caching at all cost
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: no-cache');
}
}
if (empty($filter)) {
// send the contents
if ($pathisstring) {
readstring_accel($path, $mimetype, !$dontdie);
} else {
readfile_accel($path, $mimetype, !$dontdie);
}
} else {
// Try to put the file through filters
if ($mimetype == 'text/html') {
$options = new stdClass();
$options->noclean = true;
$options->nocache = true; // temporary workaround for MDL-5136
$text = $pathisstring ? $path : implode('', file($path));
$text = file_modify_html_header($text);
$output = format_text($text, FORMAT_HTML, $options, $COURSE->id);
readstring_accel($output, $mimetype, false);
} else if (($mimetype == 'text/plain') and ($filter == 1)) {
// only filter text if filter all files is selected
$options = new stdClass();
$options->newlines = false;
$options->noclean = true;
$text = htmlentities($pathisstring ? $path : implode('', file($path)));
$output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $COURSE->id) .'</pre>';
readstring_accel($output, $mimetype, false);
} else {
// send the contents
if ($pathisstring) {
readstring_accel($path, $mimetype, !$dontdie);
} else {
readfile_accel($path, $mimetype, !$dontdie);
}
}
}
if ($dontdie) {
return;
}
die; //no more chars to output!!!
}
/**
* Handles the sending of file data to the user's browser, including support for
* byteranges etc.
*
* The $options parameter supports the following keys:
* (string|null) preview - send the preview of the file (e.g. "thumb" for a thumbnail)
* (string|null) filename - overrides the implicit filename
* (bool) dontdie - return control to caller afterwards. this is not recommended and only used for cleanup tasks.
* if this is passed as true, ignore_user_abort is called. if you don't want your processing to continue on cancel,
* you must detect this case when control is returned using connection_aborted. Please not that session is closed
* and should not be reopened.
*
* @category files
* @param stored_file $stored_file local file object
* @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
* @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
* @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
* @param array $options additional options affecting the file serving
* @return null script execution stopped unless $options['dontdie'] is true
*/
function send_stored_file($stored_file, $lifetime=86400 , $filter=0, $forcedownload=false, array $options=array()) {
global $CFG, $COURSE;
if (empty($options['filename'])) {
$filename = null;
} else {
$filename = $options['filename'];
}
if (empty($options['dontdie'])) {
$dontdie = false;
} else {
$dontdie = true;
}
if (!empty($options['preview'])) {
// replace the file with its preview
$fs = get_file_storage();
$preview_file = $fs->get_file_preview($stored_file, $options['preview']);
if (!$preview_file) {
// unable to create a preview of the file, send its default mime icon instead
if ($options['preview'] === 'tinyicon') {
$size = 24;
} else if ($options['preview'] === 'thumb') {
$size = 90;
} else {
$size = 256;
}
$fileicon = file_file_icon($stored_file, $size);
send_file($CFG->dirroot.'/pix/'.$fileicon.'.png', basename($fileicon).'.png');
} else {
// preview images have fixed cache lifetime and they ignore forced download
// (they are generated by GD and therefore they are considered reasonably safe).
$stored_file = $preview_file;
$lifetime = DAYSECS;
$filter = 0;
$forcedownload = false;
}
}
// handle external resource
if ($stored_file && $stored_file->is_external_file()) {
$stored_file->send_file($lifetime, $filter, $forcedownload, $options);
die;
}
if (!$stored_file or $stored_file->is_directory()) {
// nothing to serve
if ($dontdie) {
return;
}
die;
}
if ($dontdie) {
ignore_user_abort(true);
}
session_get_instance()->write_close(); // unlock session during fileserving
// Use given MIME type if specified, otherwise guess it using mimeinfo.
// IE, Konqueror and Opera open html file directly in browser from web even when directed to save it to disk :-O
// only Firefox saves all files locally before opening when content-disposition: attachment stated
$filename = is_null($filename) ? $stored_file->get_filename() : $filename;
$isFF = check_browser_version('Firefox', '1.5'); // only FF > 1.5 properly tested
$mimetype = ($forcedownload and !$isFF) ? 'application/x-forcedownload' :
($stored_file->get_mimetype() ? $stored_file->get_mimetype() : mimeinfo('type', $filename));
// if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
if (check_browser_version('MSIE')) {
$filename = rawurlencode($filename);
}
if ($forcedownload) {
header('Content-Disposition: attachment; filename="'.$filename.'"');
} else {
header('Content-Disposition: inline; filename="'.$filename.'"');
}
if ($lifetime > 0) {
header('Cache-Control: max-age='.$lifetime);
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Pragma: ');
} else { // Do not cache files in proxies and browsers
if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
header('Cache-Control: max-age=10');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: ');
} else { //normal http - prevent caching at all cost
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: no-cache');
}
}
if (empty($filter)) {
// send the contents
readfile_accel($stored_file, $mimetype, !$dontdie);
} else { // Try to put the file through filters
if ($mimetype == 'text/html') {
$options = new stdClass();
$options->noclean = true;
$options->nocache = true; // temporary workaround for MDL-5136
$text = $stored_file->get_content();
$text = file_modify_html_header($text);
$output = format_text($text, FORMAT_HTML, $options, $COURSE->id);
readstring_accel($output, $mimetype, false);
} else if (($mimetype == 'text/plain') and ($filter == 1)) {
// only filter text if filter all files is selected
$options = new stdClass();
$options->newlines = false;
$options->noclean = true;
$text = $stored_file->get_content();
$output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $COURSE->id) .'</pre>';
readstring_accel($output, $mimetype, false);
} else { // Just send it out raw
readfile_accel($stored_file, $mimetype, !$dontdie);
}
}
if ($dontdie) {
return;
}
die; //no more chars to output!!!
}
/**
* Retrieves an array of records from a CSV file and places
* them into a given table structure
*
* @global stdClass $CFG
* @global moodle_database $DB
* @param string $file The path to a CSV file
* @param string $table The table to retrieve columns from
* @return bool|array Returns an array of CSV records or false
*/
function get_records_csv($file, $table) {
global $CFG, $DB;
if (!$metacolumns = $DB->get_columns($table)) {
return false;
}
if(!($handle = @fopen($file, 'r'))) {
print_error('get_records_csv failed to open '.$file);
}
$fieldnames = fgetcsv($handle, 4096);
if(empty($fieldnames)) {
fclose($handle);
return false;
}
$columns = array();
foreach($metacolumns as $metacolumn) {
$ord = array_search($metacolumn->name, $fieldnames);
if(is_int($ord)) {
$columns[$metacolumn->name] = $ord;
}
}
$rows = array();
while (($data = fgetcsv($handle, 4096)) !== false) {
$item = new stdClass;
foreach($columns as $name => $ord) {
$item->$name = $data[$ord];
}
$rows[] = $item;
}
fclose($handle);
return $rows;
}
/**
* Create a file with CSV contents
*
* @global stdClass $CFG
* @global moodle_database $DB
* @param string $file The file to put the CSV content into
* @param array $records An array of records to write to a CSV file
* @param string $table The table to get columns from
* @return bool success
*/
function put_records_csv($file, $records, $table = NULL) {
global $CFG, $DB;
if (empty($records)) {
return true;
}
$metacolumns = NULL;
if ($table !== NULL && !$metacolumns = $DB->get_columns($table)) {
return false;
}
echo "x";
if(!($fp = @fopen($CFG->tempdir.'/'.$file, 'w'))) {
print_error('put_records_csv failed to open '.$file);
}
$proto = reset($records);
if(is_object($proto)) {
$fields_records = array_keys(get_object_vars($proto));
}
else if(is_array($proto)) {
$fields_records = array_keys($proto);
}
else {
return false;
}
echo "x";
if(!empty($metacolumns)) {
$fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
$fields = array_intersect($fields_records, $fields_table);
}
else {
$fields = $fields_records;
}
fwrite($fp, implode(',', $fields));
fwrite($fp, "\r\n");
foreach($records as $record) {
$array = (array)$record;
$values = array();
foreach($fields as $field) {
if(strpos($array[$field], ',')) {
$values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
}
else {
$values[] = $array[$field];
}
}
fwrite($fp, implode(',', $values)."\r\n");
}
fclose($fp);
return true;
}