Revert "MDL-39832 Files: Revert MDL-39688 commit, 6ce0c9ce42fa7849e67d7611a628ece51c3...
[moodle.git] / lib / filelib.php
CommitLineData
64a19b38 1<?php
64a19b38 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
edc0c493 16
17/**
18 * Functions for file handling.
19 *
d2b7803e
DC
20 * @package core_files
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
edc0c493 23 */
24
64f93798
PS
25defined('MOODLE_INTERNAL') || die();
26
d2b7803e
DC
27/**
28 * BYTESERVING_BOUNDARY - string unique string constant.
29 */
edc0c493 30define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7');
4c8c65ec 31
64f93798
PS
32require_once("$CFG->libdir/filestorage/file_exceptions.php");
33require_once("$CFG->libdir/filestorage/file_storage.php");
34require_once("$CFG->libdir/filestorage/zip_packer.php");
35require_once("$CFG->libdir/filebrowser/file_browser.php");
172dd12c 36
4eef1399 37/**
38 * Encodes file serving url
ba21c9d4 39 *
f28ee49e
PS
40 * @deprecated use moodle_url factory methods instead
41 *
d2b7803e
DC
42 * @todo MDL-31071 deprecate this function
43 * @global stdClass $CFG
4eef1399 44 * @param string $urlbase
45 * @param string $path /filearea/itemid/dir/dir/file.exe
46 * @param bool $forcedownload
47 * @param bool $https https url required
48 * @return string encoded file url
49 */
50function file_encode_url($urlbase, $path, $forcedownload=false, $https=false) {
51 global $CFG;
52
f28ee49e
PS
53//TODO: deprecate this
54
4eef1399 55 if ($CFG->slasharguments) {
56 $parts = explode('/', $path);
57 $parts = array_map('rawurlencode', $parts);
58 $path = implode('/', $parts);
59 $return = $urlbase.$path;
60 if ($forcedownload) {
61 $return .= '?forcedownload=1';
62 }
63 } else {
64 $path = rawurlencode($path);
65 $return = $urlbase.'?file='.$path;
66 if ($forcedownload) {
67 $return .= '&amp;forcedownload=1';
68 }
69 }
70
71 if ($https) {
72 $return = str_replace('http://', 'https://', $return);
73 }
74
75 return $return;
76}
77
c94882ff 78/**
db2cc99b 79 * Prepares 'editor' formslib element from data in database
ba21c9d4 80 *
db2cc99b 81 * The passed $data record must contain field foobar, foobarformat and optionally foobartrust. This
34ff25a6
PS
82 * function then copies the embedded files into draft area (assigning itemids automatically),
83 * creates the form element foobar_editor and rewrites the URLs so the embedded images can be
db2cc99b 84 * displayed.
85 * In your mform definition, you must have an 'editor' element called foobar_editor. Then you call
86 * your mform's set_data() supplying the object returned by this function.
87 *
d2b7803e
DC
88 * @category files
89 * @param stdClass $data database field that holds the html text with embedded media
34ff25a6 90 * @param string $field the name of the database field that holds the html text with embedded media
db2cc99b 91 * @param array $options editor options (like maxifiles, maxbytes etc.)
d2b7803e 92 * @param stdClass $context context of the editor
64f93798 93 * @param string $component
c94882ff 94 * @param string $filearea file area name
95 * @param int $itemid item id, required if item exists
d2b7803e 96 * @return stdClass modified data object
c94882ff 97 */
64f93798 98function file_prepare_standard_editor($data, $field, array $options, $context=null, $component=null, $filearea=null, $itemid=null) {
c94882ff 99 $options = (array)$options;
100 if (!isset($options['trusttext'])) {
101 $options['trusttext'] = false;
102 }
103 if (!isset($options['forcehttps'])) {
104 $options['forcehttps'] = false;
105 }
106 if (!isset($options['subdirs'])) {
107 $options['subdirs'] = false;
108 }
989c16ee 109 if (!isset($options['maxfiles'])) {
110 $options['maxfiles'] = 0; // no files by default
111 }
b96ddb7d 112 if (!isset($options['noclean'])) {
113 $options['noclean'] = false;
114 }
c94882ff 115
f9157eb7
RT
116 //sanity check for passed context. This function doesn't expect $option['context'] to be set
117 //But this function is called before creating editor hence, this is one of the best places to check
118 //if context is used properly. This check notify developer that they missed passing context to editor.
119 if (isset($context) && !isset($options['context'])) {
120 //if $context is not null then make sure $option['context'] is also set.
121 debugging('Context for editor is not set in editoroptions. Hence editor will not respect editor filters', DEBUG_DEVELOPER);
122 } else if (isset($options['context']) && isset($context)) {
123 //If both are passed then they should be equal.
124 if ($options['context']->id != $context->id) {
125 $exceptionmsg = 'Editor context ['.$options['context']->id.'] is not equal to passed context ['.$context->id.']';
126 throw new coding_exception($exceptionmsg);
127 }
128 }
32dc8ecd 129
d5934b35 130 if (is_null($itemid) or is_null($context)) {
c94882ff 131 $contextid = null;
d5934b35 132 $itemid = null;
b85b25eb
PS
133 if (!isset($data)) {
134 $data = new stdClass();
135 }
c94882ff 136 if (!isset($data->{$field})) {
137 $data->{$field} = '';
138 }
139 if (!isset($data->{$field.'format'})) {
20e5da7d 140 $data->{$field.'format'} = editors_get_preferred_format();
b96ddb7d 141 }
142 if (!$options['noclean']) {
143 $data->{$field} = clean_text($data->{$field}, $data->{$field.'format'});
c94882ff 144 }
c94882ff 145
146 } else {
147 if ($options['trusttext']) {
b96ddb7d 148 // noclean ignored if trusttext enabled
c94882ff 149 if (!isset($data->{$field.'trust'})) {
150 $data->{$field.'trust'} = 0;
151 }
152 $data = trusttext_pre_edit($data, $field, $context);
153 } else {
b96ddb7d 154 if (!$options['noclean']) {
155 $data->{$field} = clean_text($data->{$field}, $data->{$field.'format'});
156 }
c94882ff 157 }
158 $contextid = $context->id;
159 }
160
989c16ee 161 if ($options['maxfiles'] != 0) {
162 $draftid_editor = file_get_submitted_draft_itemid($field);
64f93798 163 $currenttext = file_prepare_draft_area($draftid_editor, $contextid, $component, $filearea, $itemid, $options, $data->{$field});
989c16ee 164 $data->{$field.'_editor'} = array('text'=>$currenttext, 'format'=>$data->{$field.'format'}, 'itemid'=>$draftid_editor);
165 } else {
8302aae1 166 $data->{$field.'_editor'} = array('text'=>$data->{$field}, 'format'=>$data->{$field.'format'}, 'itemid'=>0);
989c16ee 167 }
c94882ff 168
169 return $data;
170}
171
172/**
34ff25a6 173 * Prepares the content of the 'editor' form element with embedded media files to be saved in database
ba21c9d4 174 *
19fbc617 175 * This function moves files from draft area to the destination area and
176 * encodes URLs to the draft files so they can be safely saved into DB. The
177 * form has to contain the 'editor' element named foobar_editor, where 'foobar'
178 * is the name of the database field to hold the wysiwyg editor content. The
179 * editor data comes as an array with text, format and itemid properties. This
180 * function automatically adds $data properties foobar, foobarformat and
34ff25a6 181 * foobartrust, where foobar has URL to embedded files encoded.
19fbc617 182 *
d2b7803e
DC
183 * @category files
184 * @param stdClass $data raw data submitted by the form
34ff25a6 185 * @param string $field name of the database field containing the html with embedded media files
19fbc617 186 * @param array $options editor options (trusttext, subdirs, maxfiles, maxbytes etc.)
d2b7803e
DC
187 * @param stdClass $context context, required for existing data
188 * @param string $component file component
c94882ff 189 * @param string $filearea file area name
190 * @param int $itemid item id, required if item exists
d2b7803e 191 * @return stdClass modified data object
c94882ff 192 */
64f93798 193function file_postupdate_standard_editor($data, $field, array $options, $context, $component=null, $filearea=null, $itemid=null) {
c94882ff 194 $options = (array)$options;
195 if (!isset($options['trusttext'])) {
196 $options['trusttext'] = false;
197 }
198 if (!isset($options['forcehttps'])) {
199 $options['forcehttps'] = false;
200 }
201 if (!isset($options['subdirs'])) {
202 $options['subdirs'] = false;
203 }
204 if (!isset($options['maxfiles'])) {
85db95e7 205 $options['maxfiles'] = 0; // no files by default
c94882ff 206 }
207 if (!isset($options['maxbytes'])) {
208 $options['maxbytes'] = 0; // unlimited
209 }
210
211 if ($options['trusttext']) {
19fbc617 212 $data->{$field.'trust'} = trusttext_trusted($context);
c94882ff 213 } else {
19fbc617 214 $data->{$field.'trust'} = 0;
c94882ff 215 }
216
85db95e7 217 $editor = $data->{$field.'_editor'};
c94882ff 218
9057a848 219 if ($options['maxfiles'] == 0 or is_null($filearea) or is_null($itemid) or empty($editor['itemid'])) {
989c16ee 220 $data->{$field} = $editor['text'];
7f0fedc0 221 } else {
64f93798 222 $data->{$field} = file_save_draft_area_files($editor['itemid'], $context->id, $component, $filearea, $itemid, $options, $editor['text'], $options['forcehttps']);
989c16ee 223 }
c94882ff 224 $data->{$field.'format'} = $editor['format'];
225
226 return $data;
227}
228
229/**
230 * Saves text and files modified by Editor formslib element
ba21c9d4 231 *
d2b7803e
DC
232 * @category files
233 * @param stdClass $data $database entry field
c94882ff 234 * @param string $field name of data field
235 * @param array $options various options
d2b7803e 236 * @param stdClass $context context - must already exist
64f93798 237 * @param string $component
c94882ff 238 * @param string $filearea file area name
239 * @param int $itemid must already exist, usually means data is in db
d2b7803e 240 * @return stdClass modified data obejct
c94882ff 241 */
64f93798 242function file_prepare_standard_filemanager($data, $field, array $options, $context=null, $component=null, $filearea=null, $itemid=null) {
c94882ff 243 $options = (array)$options;
244 if (!isset($options['subdirs'])) {
245 $options['subdirs'] = false;
246 }
d5934b35 247 if (is_null($itemid) or is_null($context)) {
248 $itemid = null;
c94882ff 249 $contextid = null;
250 } else {
251 $contextid = $context->id;
252 }
253
85db95e7 254 $draftid_editor = file_get_submitted_draft_itemid($field.'_filemanager');
64f93798 255 file_prepare_draft_area($draftid_editor, $contextid, $component, $filearea, $itemid, $options);
85db95e7 256 $data->{$field.'_filemanager'} = $draftid_editor;
c94882ff 257
258 return $data;
259}
260
261/**
262 * Saves files modified by File manager formslib element
ba21c9d4 263 *
d2b7803e
DC
264 * @todo MDL-31073 review this function
265 * @category files
266 * @param stdClass $data $database entry field
c94882ff 267 * @param string $field name of data field
268 * @param array $options various options
d2b7803e 269 * @param stdClass $context context - must already exist
64f93798 270 * @param string $component
c94882ff 271 * @param string $filearea file area name
272 * @param int $itemid must already exist, usually means data is in db
d2b7803e 273 * @return stdClass modified data obejct
c94882ff 274 */
64f93798 275function file_postupdate_standard_filemanager($data, $field, array $options, $context, $component, $filearea, $itemid) {
c94882ff 276 $options = (array)$options;
277 if (!isset($options['subdirs'])) {
278 $options['subdirs'] = false;
279 }
280 if (!isset($options['maxfiles'])) {
281 $options['maxfiles'] = -1; // unlimited
282 }
283 if (!isset($options['maxbytes'])) {
284 $options['maxbytes'] = 0; // unlimited
285 }
286
85db95e7 287 if (empty($data->{$field.'_filemanager'})) {
c94882ff 288 $data->$field = '';
289
290 } else {
64f93798 291 file_save_draft_area_files($data->{$field.'_filemanager'}, $context->id, $component, $filearea, $itemid, $options);
c94882ff 292 $fs = get_file_storage();
293
64f93798
PS
294 if ($fs->get_area_files($context->id, $component, $filearea, $itemid)) {
295 $data->$field = '1'; // TODO: this is an ugly hack (skodak)
c94882ff 296 } else {
297 $data->$field = '';
298 }
299 }
300
301 return $data;
302}
303
8546def3 304/**
d2b7803e 305 * Generate a draft itemid
ba21c9d4 306 *
d2b7803e
DC
307 * @category files
308 * @global moodle_database $DB
309 * @global stdClass $USER
509f67e3 310 * @return int a random but available draft itemid that can be used to create a new draft
edc0c493 311 * file area.
8546def3 312 */
edc0c493 313function file_get_unused_draft_itemid() {
8546def3 314 global $DB, $USER;
315
316 if (isguestuser() or !isloggedin()) {
85db95e7 317 // guests and not-logged-in users can not be allowed to upload anything!!!!!!
8546def3 318 print_error('noguest');
319 }
320
321 $contextid = get_context_instance(CONTEXT_USER, $USER->id)->id;
8546def3 322
323 $fs = get_file_storage();
324 $draftitemid = rand(1, 999999999);
64f93798 325 while ($files = $fs->get_area_files($contextid, 'user', 'draft', $draftitemid)) {
8546def3 326 $draftitemid = rand(1, 999999999);
327 }
328
b933a139 329 return $draftitemid;
8546def3 330}
331
7983d682 332/**
edc0c493 333 * Initialise a draft file area from a real one by copying the files. A draft
334 * area will be created if one does not already exist. Normally you should
335 * get $draftitemid by calling file_get_submitted_draft_itemid('elementname');
ba21c9d4 336 *
d2b7803e
DC
337 * @category files
338 * @global stdClass $CFG
339 * @global stdClass $USER
340 * @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.
341 * @param int $contextid This parameter and the next two identify the file area to copy files from.
64f93798 342 * @param string $component
edc0c493 343 * @param string $filearea helps indentify the file area.
d2b7803e 344 * @param int $itemid helps identify the file area. Can be null if there are no files yet.
33374f76 345 * @param array $options text and file options ('subdirs'=>false, 'forcehttps'=>false)
edc0c493 346 * @param string $text some html content that needs to have embedded links rewritten to point to the draft area.
d2b7803e 347 * @return string|null returns string if $text was passed in, the rewritten $text is returned. Otherwise NULL.
7983d682 348 */
64f93798 349function file_prepare_draft_area(&$draftitemid, $contextid, $component, $filearea, $itemid, array $options=null, $text=null) {
893fe4b6 350 global $CFG, $USER, $CFG;
7983d682 351
a19a06d0 352 $options = (array)$options;
353 if (!isset($options['subdirs'])) {
354 $options['subdirs'] = false;
355 }
33374f76 356 if (!isset($options['forcehttps'])) {
a19a06d0 357 $options['forcehttps'] = false;
358 }
359
8546def3 360 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
b933a139 361 $fs = get_file_storage();
362
363 if (empty($draftitemid)) {
364 // create a new area and copy existing files into
edc0c493 365 $draftitemid = file_get_unused_draft_itemid();
64f93798
PS
366 $file_record = array('contextid'=>$usercontext->id, 'component'=>'user', 'filearea'=>'draft', 'itemid'=>$draftitemid);
367 if (!is_null($itemid) and $files = $fs->get_area_files($contextid, $component, $filearea, $itemid)) {
b933a139 368 foreach ($files as $file) {
64f93798
PS
369 if ($file->is_directory() and $file->get_filepath() === '/') {
370 // we need a way to mark the age of each draft area,
371 // by not copying the root dir we force it to be created automatically with current timestamp
372 continue;
373 }
a19a06d0 374 if (!$options['subdirs'] and ($file->is_directory() or $file->get_filepath() !== '/')) {
b5b188ce 375 continue;
376 }
67233725 377 $draftfile = $fs->create_file_from_storedfile($file_record, $file);
61506a0a 378 // XXX: This is a hack for file manager (MDL-28666)
67233725
DC
379 // File manager needs to know the original file information before copying
380 // to draft area, so we append these information in mdl_files.source field
381 // {@link file_storage::search_references()}
382 // {@link file_storage::search_references_count()}
383 $sourcefield = $file->get_source();
384 $newsourcefield = new stdClass;
385 $newsourcefield->source = $sourcefield;
386 $original = new stdClass;
387 $original->contextid = $contextid;
388 $original->component = $component;
389 $original->filearea = $filearea;
390 $original->itemid = $itemid;
391 $original->filename = $file->get_filename();
392 $original->filepath = $file->get_filepath();
393 $newsourcefield->original = file_storage::pack_reference($original);
394 $draftfile->set_source(serialize($newsourcefield));
395 // End of file manager hack
b933a139 396 }
397 }
893fe4b6
PS
398 if (!is_null($text)) {
399 // at this point there should not be any draftfile links yet,
400 // because this is a new text from database that should still contain the @@pluginfile@@ links
401 // this happens when developers forget to post process the text
402 $text = str_replace("\"$CFG->httpswwwroot/draftfile.php", "\"$CFG->httpswwwroot/brokenfile.php#", $text);
403 }
b933a139 404 } else {
405 // nothing to do
406 }
407
408 if (is_null($text)) {
409 return null;
410 }
411
edc0c493 412 // relink embedded files - editor can not handle @@PLUGINFILE@@ !
64f93798 413 return file_rewrite_pluginfile_urls($text, 'draftfile.php', $usercontext->id, 'user', 'draft', $draftitemid, $options);
644d506a 414}
415
416/**
edc0c493 417 * Convert encoded URLs in $text from the @@PLUGINFILE@@/... form to an actual URL.
ba21c9d4 418 *
d2b7803e
DC
419 * @category files
420 * @global stdClass $CFG
edc0c493 421 * @param string $text The content that may contain ULRs in need of rewriting.
422 * @param string $file The script that should be used to serve these files. pluginfile.php, draftfile.php, etc.
d2b7803e 423 * @param int $contextid This parameter and the next two identify the file area to use.
64f93798 424 * @param string $component
34ff25a6 425 * @param string $filearea helps identify the file area.
d2b7803e 426 * @param int $itemid helps identify the file area.
33374f76 427 * @param array $options text and file options ('forcehttps'=>false)
edc0c493 428 * @return string the processed text.
644d506a 429 */
64f93798 430function file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $filearea, $itemid, array $options=null) {
644d506a 431 global $CFG;
b933a139 432
33374f76
MH
433 $options = (array)$options;
434 if (!isset($options['forcehttps'])) {
435 $options['forcehttps'] = false;
436 }
437
edc0c493 438 if (!$CFG->slasharguments) {
439 $file = $file . '?file=';
b933a139 440 }
8546def3 441
64f93798 442 $baseurl = "$CFG->wwwroot/$file/$contextid/$component/$filearea/";
7d2948bd 443
444 if ($itemid !== null) {
445 $baseurl .= "$itemid/";
446 }
edc0c493 447
33374f76 448 if ($options['forcehttps']) {
edc0c493 449 $baseurl = str_replace('http://', 'https://', $baseurl);
b933a139 450 }
451
edc0c493 452 return str_replace('@@PLUGINFILE@@/', $baseurl, $text);
b933a139 453}
454
12fab708 455/**
edc0c493 456 * Returns information about files in a draft area.
ba21c9d4 457 *
d2b7803e
DC
458 * @global stdClass $CFG
459 * @global stdClass $USER
460 * @param int $draftitemid the draft area item id.
edc0c493 461 * @return array with the following entries:
462 * 'filecount' => number of files in the draft area.
463 * (more information will be added as needed).
12fab708 464 */
edc0c493 465function file_get_draft_area_info($draftitemid) {
12fab708 466 global $CFG, $USER;
467
468 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
469 $fs = get_file_storage();
470
edc0c493 471 $results = array();
472
473 // The number of files
64f93798 474 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
b6accf69 475 $results['filecount'] = count($draftfiles);
529377b5
DC
476 $results['filesize'] = 0;
477 foreach ($draftfiles as $file) {
478 $results['filesize'] += $file->get_filesize();
479 }
b6accf69
DC
480
481 return $results;
482}
483
ea1780ad
DC
484/**
485 * Get used space of files
d2b7803e
DC
486 * @global moodle_database $DB
487 * @global stdClass $USER
34ff25a6 488 * @return int total bytes
ea1780ad
DC
489 */
490function file_get_user_used_space() {
e35194be 491 global $DB, $USER;
ea1780ad
DC
492
493 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
e35194be
DC
494 $sql = "SELECT SUM(files1.filesize) AS totalbytes FROM {files} files1
495 JOIN (SELECT contenthash, filename, MAX(id) AS id
496 FROM {files}
497 WHERE contextid = ? AND component = ? AND filearea != ?
498 GROUP BY contenthash, filename) files2 ON files1.id = files2.id";
499 $params = array('contextid'=>$usercontext->id, 'component'=>'user', 'filearea'=>'draft');
500 $record = $DB->get_record_sql($sql, $params);
501 return (int)$record->totalbytes;
ea1780ad 502}
f0e5f031 503
504/**
505 * Convert any string to a valid filepath
d2b7803e 506 * @todo review this function
f0e5f031 507 * @param string $str
508 * @return string path
509 */
64f93798 510function file_correct_filepath($str) { //TODO: what is this? (skodak)
f36cb951
DC
511 if ($str == '/' or empty($str)) {
512 return '/';
513 } else {
514 return '/'.trim($str, './@#$ ').'/';
515 }
f0e5f031 516}
517
518/**
34ff25a6 519 * Generate a folder tree of draft area of current USER recursively
d2b7803e
DC
520 *
521 * @todo MDL-31073 use normal return value instead, this does not fit the rest of api here (skodak)
522 * @param int $draftitemid
f0e5f031 523 * @param string $filepath
d2b7803e 524 * @param mixed $data
f0e5f031 525 */
64f93798 526function file_get_drafarea_folders($draftitemid, $filepath, &$data) {
f0e5f031 527 global $USER, $OUTPUT, $CFG;
528 $data->children = array();
529 $context = get_context_instance(CONTEXT_USER, $USER->id);
530 $fs = get_file_storage();
64f93798 531 if ($files = $fs->get_directory_files($context->id, 'user', 'draft', $draftitemid, $filepath, false)) {
f0e5f031 532 foreach ($files as $file) {
533 if ($file->is_directory()) {
6bdfef5d 534 $item = new stdClass();
f79321f1 535 $item->sortorder = $file->get_sortorder();
f0e5f031 536 $item->filepath = $file->get_filepath();
537
538 $foldername = explode('/', trim($item->filepath, '/'));
539 $item->fullname = trim(array_pop($foldername), '/');
540
541 $item->id = uniqid();
64f93798 542 file_get_drafarea_folders($draftitemid, $item->filepath, $item);
f0e5f031 543 $data->children[] = $item;
544 } else {
545 continue;
546 }
547 }
548 }
549}
550
551/**
552 * Listing all files (including folders) in current path (draft area)
553 * used by file manager
554 * @param int $draftitemid
555 * @param string $filepath
d2b7803e 556 * @return stdClass
f0e5f031 557 */
64f93798 558function file_get_drafarea_files($draftitemid, $filepath = '/') {
f0e5f031 559 global $USER, $OUTPUT, $CFG;
560
561 $context = get_context_instance(CONTEXT_USER, $USER->id);
562 $fs = get_file_storage();
563
6bdfef5d 564 $data = new stdClass();
f0e5f031 565 $data->path = array();
566 $data->path[] = array('name'=>get_string('files'), 'path'=>'/');
567
568 // will be used to build breadcrumb
e709ddd2 569 $trail = '/';
f0e5f031 570 if ($filepath !== '/') {
571 $filepath = file_correct_filepath($filepath);
572 $parts = explode('/', $filepath);
573 foreach ($parts as $part) {
574 if ($part != '' && $part != null) {
e709ddd2 575 $trail .= ($part.'/');
f0e5f031 576 $data->path[] = array('name'=>$part, 'path'=>$trail);
577 }
578 }
579 }
580
581 $list = array();
f79321f1 582 $maxlength = 12;
64f93798 583 if ($files = $fs->get_directory_files($context->id, 'user', 'draft', $draftitemid, $filepath, false)) {
f0e5f031 584 foreach ($files as $file) {
6bdfef5d 585 $item = new stdClass();
f0e5f031 586 $item->filename = $file->get_filename();
587 $item->filepath = $file->get_filepath();
588 $item->fullname = trim($item->filename, '/');
589 $filesize = $file->get_filesize();
e709ddd2 590 $item->size = $filesize ? $filesize : null;
f0e5f031 591 $item->filesize = $filesize ? display_size($filesize) : '';
592
f79321f1 593 $item->sortorder = $file->get_sortorder();
e709ddd2
MG
594 $item->author = $file->get_author();
595 $item->license = $file->get_license();
596 $item->datemodified = $file->get_timemodified();
597 $item->datecreated = $file->get_timecreated();
9a62f779 598 $item->isref = $file->is_external_file();
0b2bfbd1
MG
599 if ($item->isref && $file->get_status() == 666) {
600 $item->originalmissing = true;
601 }
6dd299be
MG
602 // find the file this draft file was created from and count all references in local
603 // system pointing to that file
83d2700e 604 $source = @unserialize($file->get_source());
6dd299be
MG
605 if (isset($source->original)) {
606 $item->refcount = $fs->search_references_count($source->original);
607 }
f0e5f031 608
f0e5f031 609 if ($file->is_directory()) {
610 $item->filesize = 0;
559276b1 611 $item->icon = $OUTPUT->pix_url(file_folder_icon(24))->out(false);
f0e5f031 612 $item->type = 'folder';
613 $foldername = explode('/', trim($item->filepath, '/'));
614 $item->fullname = trim(array_pop($foldername), '/');
559276b1 615 $item->thumbnail = $OUTPUT->pix_url(file_folder_icon(90))->out(false);
f0e5f031 616 } else {
64f93798 617 // do NOT use file browser here!
559276b1 618 $item->mimetype = get_mimetype_description($file);
ff32444a 619 if (file_extension_in_typegroup($file->get_filename(), 'archive')) {
559276b1
MG
620 $item->type = 'zip';
621 } else {
622 $item->type = 'file';
623 }
dafe1296
DM
624 $itemurl = moodle_url::make_draftfile_url($draftitemid, $item->filepath, $item->filename);
625 $item->url = $itemurl->out();
559276b1
MG
626 $item->icon = $OUTPUT->pix_url(file_file_icon($file, 24))->out(false);
627 $item->thumbnail = $OUTPUT->pix_url(file_file_icon($file, 90))->out(false);
dafe1296 628 if ($imageinfo = $file->get_imageinfo()) {
3333e7e2
DM
629 $item->realthumbnail = $itemurl->out(false, array('preview' => 'thumb', 'oid' => $file->get_timemodified()));
630 $item->realicon = $itemurl->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
dafe1296
DM
631 $item->image_width = $imageinfo['width'];
632 $item->image_height = $imageinfo['height'];
633 }
f0e5f031 634 }
635 $list[] = $item;
636 }
637 }
638 $data->itemid = $draftitemid;
639 $data->list = $list;
640 return $data;
641}
642
3156b8ca 643/**
edc0c493 644 * Returns draft area itemid for a given element.
ba21c9d4 645 *
d2b7803e 646 * @category files
edc0c493 647 * @param string $elname name of formlib editor element, or a hidden form field that stores the draft area item id, etc.
d2b7803e 648 * @return int the itemid, or 0 if there is not one yet.
3156b8ca 649 */
edc0c493 650function file_get_submitted_draft_itemid($elname) {
18bd7573
PS
651 // this is a nasty hack, ideally all new elements should use arrays here or there should be a new parameter
652 if (!isset($_REQUEST[$elname])) {
653 return 0;
edc0c493 654 }
18bd7573
PS
655 if (is_array($_REQUEST[$elname])) {
656 $param = optional_param_array($elname, 0, PARAM_INT);
56a7bf68 657 if (!empty($param['itemid'])) {
658 $param = $param['itemid'];
659 } else {
660 debugging('Missing itemid, maybe caused by unset maxfiles option', DEBUG_DEVELOPER);
661 return false;
662 }
18bd7573
PS
663
664 } else {
665 $param = optional_param($elname, 0, PARAM_INT);
3156b8ca 666 }
18bd7573
PS
667
668 if ($param) {
669 require_sesskey();
670 }
671
edc0c493 672 return $param;
3156b8ca 673}
674
67233725
DC
675/**
676 * Restore the original source field from draft files
677 *
239eb7e2
MG
678 * Do not use this function because it makes field files.source inconsistent
679 * for draft area files. This function will be deprecated in 2.6
680 *
67233725
DC
681 * @param stored_file $storedfile This only works with draft files
682 * @return stored_file
683 */
684function file_restore_source_field_from_draft_file($storedfile) {
83d2700e 685 $source = @unserialize($storedfile->get_source());
61506a0a
DC
686 if (!empty($source)) {
687 if (is_object($source)) {
688 $restoredsource = $source->source;
689 $storedfile->set_source($restoredsource);
690 } else {
691 throw new moodle_exception('invalidsourcefield', 'error');
692 }
67233725
DC
693 }
694 return $storedfile;
695}
b933a139 696/**
edc0c493 697 * Saves files from a draft file area to a real one (merging the list of files).
698 * Can rewrite URLs in some content at the same time if desired.
ba21c9d4 699 *
d2b7803e
DC
700 * @category files
701 * @global stdClass $USER
702 * @param int $draftitemid the id of the draft area to use. Normally obtained
edc0c493 703 * from file_get_submitted_draft_itemid('elementname') or similar.
d2b7803e 704 * @param int $contextid This parameter and the next two identify the file area to save to.
64f93798 705 * @param string $component
a08171c5 706 * @param string $filearea indentifies the file area.
d2b7803e 707 * @param int $itemid helps identifies the file area.
ba21c9d4 708 * @param array $options area options (subdirs=>false, maxfiles=-1, maxbytes=0)
edc0c493 709 * @param string $text some html content that needs to have embedded links rewritten
710 * to the @@PLUGINFILE@@ form for saving in the database.
d2b7803e
DC
711 * @param bool $forcehttps force https urls.
712 * @return string|null if $text was passed in, the rewritten $text is returned. Otherwise NULL.
b933a139 713 */
64f93798 714function file_save_draft_area_files($draftitemid, $contextid, $component, $filearea, $itemid, array $options=null, $text=null, $forcehttps=false) {
c08643da 715 global $USER;
b933a139 716
717 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
8546def3 718 $fs = get_file_storage();
b933a139 719
a08171c5 720 $options = (array)$options;
721 if (!isset($options['subdirs'])) {
722 $options['subdirs'] = false;
723 }
724 if (!isset($options['maxfiles'])) {
725 $options['maxfiles'] = -1; // unlimited
726 }
ec3fe6e1 727 if (!isset($options['maxbytes']) || $options['maxbytes'] == USER_CAN_IGNORE_FILE_SIZE_LIMITS) {
a08171c5 728 $options['maxbytes'] = 0; // unlimited
729 }
ec3fe6e1
MG
730 $allowreferences = true;
731 if (isset($options['return_types']) && !($options['return_types'] & FILE_REFERENCE)) {
732 // we assume that if $options['return_types'] is NOT specified, we DO allow references.
733 // this is not exactly right. BUT there are many places in code where filemanager options
734 // are not passed to file_save_draft_area_files()
735 $allowreferences = false;
736 }
a08171c5 737
64f93798
PS
738 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id');
739 $oldfiles = $fs->get_area_files($contextid, $component, $filearea, $itemid, 'id');
b933a139 740
7627b999
MG
741 // One file in filearea means it is empty (it has only top-level directory '.').
742 if (count($draftfiles) > 1 || count($oldfiles) > 1) {
b933a139 743 // we have to merge old and new files - we want to keep file ids for files that were not changed
656e1184 744 // we change time modified for all new and changed files, we keep time created as is
a08171c5 745
746 $newhashes = array();
02890b99 747 $filecount = 0;
b933a139 748 foreach ($draftfiles as $file) {
02890b99
MG
749 if (!$options['subdirs'] && ($file->get_filepath() !== '/' or $file->is_directory())) {
750 continue;
751 }
752 if (!$allowreferences && $file->is_external_file()) {
753 continue;
754 }
755 if (!$file->is_directory()) {
756 if ($options['maxbytes'] and $options['maxbytes'] < $file->get_filesize()) {
757 // oversized file - should not get here at all
758 continue;
759 }
760 if ($options['maxfiles'] != -1 and $options['maxfiles'] <= $filecount) {
761 // more files - should not get here at all
762 continue;
763 }
764 $filecount++;
765 }
64f93798 766 $newhash = $fs->get_pathname_hash($contextid, $component, $filearea, $itemid, $file->get_filepath(), $file->get_filename());
a08171c5 767 $newhashes[$newhash] = $file;
768 }
02890b99 769
7627b999
MG
770 // Loop through oldfiles and decide which we need to delete and which to update.
771 // After this cycle the array $newhashes will only contain the files that need to be added.
656e1184
PS
772 foreach ($oldfiles as $oldfile) {
773 $oldhash = $oldfile->get_pathnamehash();
774 if (!isset($newhashes[$oldhash])) {
64f93798 775 // delete files not needed any more - deleted by user
656e1184
PS
776 $oldfile->delete();
777 continue;
778 }
67233725 779
656e1184 780 $newfile = $newhashes[$oldhash];
77947f22
MG
781 // Now we know that we have $oldfile and $newfile for the same path.
782 // Let's check if we can update this file or we need to delete and create.
783 if ($newfile->is_directory()) {
784 // Directories are always ok to just update.
785 } else if (($source = @unserialize($newfile->get_source())) && isset($source->original)) {
786 // File has the 'original' - we need to update the file (it may even have not been changed at all).
787 $original = file_storage::unpack_reference($source->original);
788 if ($original['filename'] !== $oldfile->get_filename() || $original['filepath'] !== $oldfile->get_filepath()) {
789 // Very odd, original points to another file. Delete and create file.
790 $oldfile->delete();
791 continue;
792 }
793 } else {
794 // The same file name but absence of 'original' means that file was deteled and uploaded again.
795 // By deleting and creating new file we properly manage all existing references.
796 $oldfile->delete();
797 continue;
798 }
799
67233725
DC
800 // status changed, we delete old file, and create a new one
801 if ($oldfile->get_status() != $newfile->get_status()) {
656e1184
PS
802 // file was changed, use updated with new timemodified data
803 $oldfile->delete();
67233725 804 // This file will be added later
656e1184
PS
805 continue;
806 }
67233725 807
67233725
DC
808 // Updated author
809 if ($oldfile->get_author() != $newfile->get_author()) {
810 $oldfile->set_author($newfile->get_author());
811 }
812 // Updated license
813 if ($oldfile->get_license() != $newfile->get_license()) {
814 $oldfile->set_license($newfile->get_license());
815 }
816
817 // Updated file source
239eb7e2
MG
818 // Field files.source for draftarea files contains serialised object with source and original information.
819 // We only store the source part of it for non-draft file area.
77947f22
MG
820 $newsource = $newfile->get_source();
821 if ($source = @unserialize($newfile->get_source())) {
822 $newsource = $source->source;
823 }
824 if ($oldfile->get_source() !== $newsource) {
825 $oldfile->set_source($newsource);
67233725
DC
826 }
827
828 // Updated sort order
829 if ($oldfile->get_sortorder() != $newfile->get_sortorder()) {
830 $oldfile->set_sortorder($newfile->get_sortorder());
831 }
832
f653c9d8
DM
833 // Update file timemodified
834 if ($oldfile->get_timemodified() != $newfile->get_timemodified()) {
835 $oldfile->set_timemodified($newfile->get_timemodified());
836 }
837
26aaa7e8 838 // Replaced file content
77947f22
MG
839 if (!$oldfile->is_directory() &&
840 ($oldfile->get_contenthash() != $newfile->get_contenthash() ||
73c6550d 841 $oldfile->get_filesize() != $newfile->get_filesize() ||
6d5bcdeb
MG
842 $oldfile->get_referencefileid() != $newfile->get_referencefileid() ||
843 $oldfile->get_userid() != $newfile->get_userid())) {
73c6550d 844 $oldfile->replace_file_with($newfile);
26aaa7e8 845 // push changes to all local files that are referencing this file
102d0bd0 846 $fs->update_references_to_storedfile($oldfile);
26aaa7e8
MG
847 }
848
656e1184
PS
849 // unchanged file or directory - we keep it as is
850 unset($newhashes[$oldhash]);
b933a139 851 }
a08171c5 852
67233725 853 // Add fresh file or the file which has changed status
656e1184 854 // the size and subdirectory tests are extra safety only, the UI should prevent it
a08171c5 855 foreach ($newhashes as $file) {
65fead53 856 $file_record = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'timemodified'=>time());
77947f22
MG
857 if ($source = @unserialize($file->get_source())) {
858 // Field files.source for draftarea files contains serialised object with source and original information.
239eb7e2 859 // We only store the source part of it for non-draft file area.
77947f22
MG
860 $file_record['source'] = $source->source;
861 }
67233725
DC
862
863 if ($file->is_external_file()) {
864 $repoid = $file->get_repository_id();
865 if (!empty($repoid)) {
866 $file_record['repositoryid'] = $repoid;
867 $file_record['reference'] = $file->get_reference();
868 }
869 }
870
a08171c5 871 $fs->create_file_from_storedfile($file_record, $file);
8546def3 872 }
873 }
874
64f93798 875 // note: do not purge the draft area - we clean up areas later in cron,
42776c94
PS
876 // the reason is that user might press submit twice and they would loose the files,
877 // also sometimes we might want to use hacks that save files into two different areas
b933a139 878
8546def3 879 if (is_null($text)) {
880 return null;
c08643da
TH
881 } else {
882 return file_rewrite_urls_to_pluginfile($text, $draftitemid, $forcehttps);
8546def3 883 }
c08643da
TH
884}
885
886/**
887 * Convert the draft file area URLs in some content to @@PLUGINFILE@@ tokens
888 * ready to be saved in the database. Normally, this is done automatically by
889 * {@link file_save_draft_area_files()}.
d2b7803e
DC
890 *
891 * @category files
c08643da
TH
892 * @param string $text the content to process.
893 * @param int $draftitemid the draft file area the content was using.
894 * @param bool $forcehttps whether the content contains https URLs. Default false.
895 * @return string the processed content.
896 */
897function file_rewrite_urls_to_pluginfile($text, $draftitemid, $forcehttps = false) {
898 global $CFG, $USER;
899
900 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
8546def3 901
9337cf32
PS
902 $wwwroot = $CFG->wwwroot;
903 if ($forcehttps) {
904 $wwwroot = str_replace('http://', 'https://', $wwwroot);
7983d682 905 }
906
9337cf32
PS
907 // relink embedded files if text submitted - no absolute links allowed in database!
908 $text = str_ireplace("$wwwroot/draftfile.php/$usercontext->id/user/draft/$draftitemid/", '@@PLUGINFILE@@/', $text);
909
910 if (strpos($text, 'draftfile.php?file=') !== false) {
911 $matches = array();
912 preg_match_all("!$wwwroot/draftfile.php\?file=%2F{$usercontext->id}%2Fuser%2Fdraft%2F{$draftitemid}%2F[^'\",&<>|`\s:\\\\]+!iu", $text, $matches);
913 if ($matches) {
914 foreach ($matches[0] as $match) {
915 $replace = str_ireplace('%2F', '/', $match);
916 $text = str_replace($match, $replace, $text);
917 }
918 }
919 $text = str_ireplace("$wwwroot/draftfile.php?file=/$usercontext->id/user/draft/$draftitemid/", '@@PLUGINFILE@@/', $text);
7983d682 920 }
921
7983d682 922 return $text;
923}
924
f79321f1
DC
925/**
926 * Set file sort order
d2b7803e
DC
927 *
928 * @global moodle_database $DB
929 * @param int $contextid the context id
930 * @param string $component file component
f79321f1 931 * @param string $filearea file area.
d2b7803e 932 * @param int $itemid itemid.
f79321f1
DC
933 * @param string $filepath file path.
934 * @param string $filename file name.
d2b7803e
DC
935 * @param int $sortorder the sort order of file.
936 * @return bool
f79321f1 937 */
64f93798 938function file_set_sortorder($contextid, $component, $filearea, $itemid, $filepath, $filename, $sortorder) {
f79321f1 939 global $DB;
64f93798 940 $conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename);
f79321f1
DC
941 if ($file_record = $DB->get_record('files', $conditions)) {
942 $sortorder = (int)$sortorder;
943 $file_record->sortorder = $sortorder;
944 $DB->update_record('files', $file_record);
945 return true;
946 }
947 return false;
948}
949
950/**
951 * reset file sort order number to 0
d2b7803e
DC
952 * @global moodle_database $DB
953 * @param int $contextid the context id
64f93798 954 * @param string $component
f79321f1 955 * @param string $filearea file area.
d2b7803e
DC
956 * @param int|bool $itemid itemid.
957 * @return bool
f79321f1 958 */
64f93798 959function file_reset_sortorder($contextid, $component, $filearea, $itemid=false) {
f79321f1
DC
960 global $DB;
961
64f93798 962 $conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea);
f79321f1
DC
963 if ($itemid !== false) {
964 $conditions['itemid'] = $itemid;
965 }
966
967 $file_records = $DB->get_records('files', $conditions);
968 foreach ($file_records as $file_record) {
969 $file_record->sortorder = 0;
970 $DB->update_record('files', $file_record);
971 }
972 return true;
973}
974
a83ad946 975/**
976 * Returns description of upload error
ba21c9d4 977 *
a83ad946 978 * @param int $errorcode found in $_FILES['filename.ext']['error']
ba21c9d4 979 * @return string error description string, '' if ok
a83ad946 980 */
981function file_get_upload_error($errorcode) {
a08171c5 982
a83ad946 983 switch ($errorcode) {
984 case 0: // UPLOAD_ERR_OK - no error
985 $errmessage = '';
986 break;
a08171c5 987
a83ad946 988 case 1: // UPLOAD_ERR_INI_SIZE
989 $errmessage = get_string('uploadserverlimit');
990 break;
a08171c5 991
a83ad946 992 case 2: // UPLOAD_ERR_FORM_SIZE
993 $errmessage = get_string('uploadformlimit');
994 break;
a08171c5 995
a83ad946 996 case 3: // UPLOAD_ERR_PARTIAL
997 $errmessage = get_string('uploadpartialfile');
998 break;
a08171c5 999
a83ad946 1000 case 4: // UPLOAD_ERR_NO_FILE
1001 $errmessage = get_string('uploadnofilefound');
1002 break;
a08171c5 1003
a83ad946 1004 // Note: there is no error with a value of 5
1005
1006 case 6: // UPLOAD_ERR_NO_TMP_DIR
1007 $errmessage = get_string('uploadnotempdir');
1008 break;
1009
1010 case 7: // UPLOAD_ERR_CANT_WRITE
1011 $errmessage = get_string('uploadcantwrite');
1012 break;
1013
1014 case 8: // UPLOAD_ERR_EXTENSION
1015 $errmessage = get_string('uploadextension');
1016 break;
1017
1018 default:
1019 $errmessage = get_string('uploadproblem');
1020 }
1021
1022 return $errmessage;
1023}
1024
5db608f4 1025/**
1026 * Recursive function formating an array in POST parameter
1027 * @param array $arraydata - the array that we are going to format and add into &$data array
1028 * @param string $currentdata - a row of the final postdata array at instant T
1029 * when finish, it's assign to $data under this format: name[keyname][][]...[]='value'
1030 * @param array $data - the final data array containing all POST parameters : 1 row = 1 parameter
1031 */
1032function format_array_postdata_for_curlcall($arraydata, $currentdata, &$data) {
1033 foreach ($arraydata as $k=>$v) {
277c7a40 1034 $newcurrentdata = $currentdata;
5db608f4 1035 if (is_array($v)) { //the value is an array, call the function recursively
277c7a40 1036 $newcurrentdata = $newcurrentdata.'['.urlencode($k).']';
1037 format_array_postdata_for_curlcall($v, $newcurrentdata, $data);
5db608f4 1038 } else { //add the POST parameter to the $data array
277c7a40 1039 $data[] = $newcurrentdata.'['.urlencode($k).']='.urlencode($v);
5db608f4 1040 }
1041 }
1042}
1043
1044/**
1045 * Transform a PHP array into POST parameter
1046 * (see the recursive function format_array_postdata_for_curlcall)
1047 * @param array $postdata
1048 * @return array containing all POST parameters (1 row = 1 POST parameter)
1049 */
1050function format_postdata_for_curlcall($postdata) {
1051 $data = array();
1052 foreach ($postdata as $k=>$v) {
1053 if (is_array($v)) {
1054 $currentdata = urlencode($k);
1055 format_array_postdata_for_curlcall($v, $currentdata, $data);
1056 } else {
ea1780ad 1057 $data[] = urlencode($k).'='.urlencode($v);
5db608f4 1058 }
1059 }
1060 $convertedpostdata = implode('&', $data);
1061 return $convertedpostdata;
1062}
1063
8ee88311 1064/**
5f8bdc17 1065 * Fetches content of file from Internet (using proxy if defined). Uses cURL extension if present.
599f06cf 1066 * Due to security concerns only downloads from http(s) sources are supported.
1067 *
d2b7803e
DC
1068 * @todo MDL-31073 add version test for '7.10.5'
1069 * @category files
599f06cf 1070 * @param string $url file url starting with http(s)://
5ef082df 1071 * @param array $headers http headers, null if none. If set, should be an
1072 * associative array of header name => value pairs.
6bf55889 1073 * @param array $postdata array means use POST request with given parameters
1074 * @param bool $fullresponse return headers, responses, etc in a similar way snoopy does
5ef082df 1075 * (if false, just returns content)
1076 * @param int $timeout timeout for complete download process including all file transfer
44e02d79 1077 * (default 5 minutes)
1078 * @param int $connecttimeout timeout for connection to server; this is the timeout that
1079 * usually happens if the remote server is completely down (default 20 seconds);
1080 * may not work when using proxy
98eaf27e 1081 * @param bool $skipcertverify If true, the peer's SSL certificate will not be checked.
5f1c825d
SH
1082 * Only use this when already in a trusted location.
1083 * @param string $tofile store the downloaded content to file instead of returning it.
98eaf27e 1084 * @param bool $calctimeout false by default, true enables an extra head request to try and determine
5f1c825d
SH
1085 * filesize and appropriately larger timeout based on $CFG->curltimeoutkbitrate
1086 * @return mixed false if request failed or content of the file as string if ok. True if file downloaded into $tofile successfully.
8ee88311 1087 */
60b5a2fe 1088function download_file_content($url, $headers=null, $postdata=null, $fullresponse=false, $timeout=300, $connecttimeout=20, $skipcertverify=false, $tofile=NULL, $calctimeout=false) {
e27f0765 1089 global $CFG;
1090
599f06cf 1091 // some extra security
1092 $newlines = array("\r", "\n");
1093 if (is_array($headers) ) {
1094 foreach ($headers as $key => $value) {
1095 $headers[$key] = str_replace($newlines, '', $value);
1096 }
1097 }
1098 $url = str_replace($newlines, '', $url);
1099 if (!preg_match('|^https?://|i', $url)) {
1100 if ($fullresponse) {
365a5941 1101 $response = new stdClass();
599f06cf 1102 $response->status = 0;
1103 $response->headers = array();
1104 $response->response_code = 'Invalid protocol specified in url';
1105 $response->results = '';
1106 $response->error = 'Invalid protocol specified in url';
1107 return $response;
1108 } else {
1109 return false;
1110 }
1111 }
1112
bb2c046d 1113 // check if proxy (if used) should be bypassed for this url
aa944588 1114 $proxybypass = is_proxybypass($url);
1115
7fb90df0 1116 if (!$ch = curl_init($url)) {
1117 debugging('Can not init curl.');
1118 return false;
1119 }
599f06cf 1120
599f06cf 1121 // set extra headers
6bf55889 1122 if (is_array($headers) ) {
1123 $headers2 = array();
1124 foreach ($headers as $key => $value) {
6bf55889 1125 $headers2[] = "$key: $value";
1126 }
1127 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2);
1128 }
1129
83947a36 1130 if ($skipcertverify) {
1131 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1132 }
bb2c046d 1133
6bf55889 1134 // use POST if requested
1135 if (is_array($postdata)) {
5db608f4 1136 $postdata = format_postdata_for_curlcall($postdata);
6bf55889 1137 curl_setopt($ch, CURLOPT_POST, true);
1138 curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
5f8bdc17 1139 }
ea1780ad 1140
8ee88311 1141 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
3a1055a5 1142 curl_setopt($ch, CURLOPT_HEADER, false);
44e02d79 1143 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
60b5a2fe 1144
6bf55889 1145 if (!ini_get('open_basedir') and !ini_get('safe_mode')) {
599f06cf 1146 // TODO: add version test for '7.10.5'
6bf55889 1147 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1148 curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
1149 }
1150
15c31560 1151 if (!empty($CFG->proxyhost) and !$proxybypass) {
5f8bdc17 1152 // SOCKS supported in PHP5 only
1153 if (!empty($CFG->proxytype) and ($CFG->proxytype == 'SOCKS5')) {
1154 if (defined('CURLPROXY_SOCKS5')) {
1155 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
1156 } else {
5f8bdc17 1157 curl_close($ch);
599f06cf 1158 if ($fullresponse) {
365a5941 1159 $response = new stdClass();
599f06cf 1160 $response->status = '0';
1161 $response->headers = array();
1162 $response->response_code = 'SOCKS5 proxy is not supported in PHP4';
1163 $response->results = '';
1164 $response->error = 'SOCKS5 proxy is not supported in PHP4';
1165 return $response;
1166 } else {
1167 debugging("SOCKS5 proxy is not supported in PHP4.", DEBUG_ALL);
1168 return false;
1169 }
5f8bdc17 1170 }
1171 }
1172
08ec989f 1173 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
1174
8ee88311 1175 if (empty($CFG->proxyport)) {
e27f0765 1176 curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost);
8ee88311 1177 } else {
e27f0765 1178 curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport);
8ee88311 1179 }
5f8bdc17 1180
1181 if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
8ee88311 1182 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
5f8bdc17 1183 if (defined('CURLOPT_PROXYAUTH')) {
1184 // any proxy authentication if PHP 5.1
1185 curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
1186 }
8ee88311 1187 }
1188 }
6bf55889 1189
34ff25a6 1190 // set up header and content handlers
365a5941 1191 $received = new stdClass();
3a1055a5
PS
1192 $received->headers = array(); // received headers array
1193 $received->tofile = $tofile;
1194 $received->fh = null;
1195 curl_setopt($ch, CURLOPT_HEADERFUNCTION, partial('download_file_content_header_handler', $received));
1196 if ($tofile) {
1197 curl_setopt($ch, CURLOPT_WRITEFUNCTION, partial('download_file_content_write_handler', $received));
1198 }
1199
60b5a2fe
AB
1200 if (!isset($CFG->curltimeoutkbitrate)) {
1201 //use very slow rate of 56kbps as a timeout speed when not set
1202 $bitrate = 56;
1203 } else {
1204 $bitrate = $CFG->curltimeoutkbitrate;
1205 }
1206
5f1c825d
SH
1207 // try to calculate the proper amount for timeout from remote file size.
1208 // if disabled or zero, we won't do any checks nor head requests.
1209 if ($calctimeout && $bitrate > 0) {
60b5a2fe 1210 //setup header request only options
5f1c825d 1211 curl_setopt_array ($ch, array(
60b5a2fe 1212 CURLOPT_RETURNTRANSFER => false,
5f1c825d
SH
1213 CURLOPT_NOBODY => true)
1214 );
60b5a2fe
AB
1215
1216 curl_exec($ch);
1217 $info = curl_getinfo($ch);
1218 $err = curl_error($ch);
1219
1220 if ($err === '' && $info['download_content_length'] > 0) { //no curl errors
5f1c825d 1221 $timeout = max($timeout, ceil($info['download_content_length'] * 8 / ($bitrate * 1024))); //adjust for large files only - take max timeout.
60b5a2fe
AB
1222 }
1223 //reinstate affected curl options
5f1c825d 1224 curl_setopt_array ($ch, array(
60b5a2fe 1225 CURLOPT_RETURNTRANSFER => true,
d567c29d
JF
1226 CURLOPT_NOBODY => false,
1227 CURLOPT_HTTPGET => true)
5f1c825d 1228 );
60b5a2fe
AB
1229 }
1230
1231 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
3a1055a5 1232 $result = curl_exec($ch);
08ec989f 1233
599f06cf 1234 // try to detect encoding problems
6bf55889 1235 if ((curl_errno($ch) == 23 or curl_errno($ch) == 61) and defined('CURLOPT_ENCODING')) {
1236 curl_setopt($ch, CURLOPT_ENCODING, 'none');
3a1055a5
PS
1237 $result = curl_exec($ch);
1238 }
1239
1240 if ($received->fh) {
1241 fclose($received->fh);
6bf55889 1242 }
1243
08ec989f 1244 if (curl_errno($ch)) {
6bf55889 1245 $error = curl_error($ch);
1246 $error_no = curl_errno($ch);
1247 curl_close($ch);
1248
1249 if ($fullresponse) {
365a5941 1250 $response = new stdClass();
6bf55889 1251 if ($error_no == 28) {
1252 $response->status = '-100'; // mimic snoopy
1253 } else {
1254 $response->status = '0';
1255 }
1256 $response->headers = array();
1257 $response->response_code = $error;
3a1055a5 1258 $response->results = false;
6bf55889 1259 $response->error = $error;
1260 return $response;
1261 } else {
599f06cf 1262 debugging("cURL request for \"$url\" failed with: $error ($error_no)", DEBUG_ALL);
6bf55889 1263 return false;
1264 }
5f8bdc17 1265
1266 } else {
6bf55889 1267 $info = curl_getinfo($ch);
1268 curl_close($ch);
599f06cf 1269
1270 if (empty($info['http_code'])) {
1271 // for security reasons we support only true http connections (Location: file:// exploit prevention)
365a5941 1272 $response = new stdClass();
599f06cf 1273 $response->status = '0';
1274 $response->headers = array();
1275 $response->response_code = 'Unknown cURL error';
3a1055a5 1276 $response->results = false; // do NOT change this, we really want to ignore the result!
599f06cf 1277 $response->error = 'Unknown cURL error';
1278
1279 } else {
365a5941 1280 $response = new stdClass();;
599f06cf 1281 $response->status = (string)$info['http_code'];
3a1055a5
PS
1282 $response->headers = $received->headers;
1283 $response->response_code = $received->headers[0];
1284 $response->results = $result;
599f06cf 1285 $response->error = '';
1286 }
6bf55889 1287
1288 if ($fullresponse) {
1289 return $response;
1290 } else if ($info['http_code'] != 200) {
599f06cf 1291 debugging("cURL request for \"$url\" failed, HTTP response code: ".$response->response_code, DEBUG_ALL);
6bf55889 1292 return false;
1293 } else {
1294 return $response->results;
5f8bdc17 1295 }
08ec989f 1296 }
8ee88311 1297}
1298
3a1055a5 1299/**
f36cb951 1300 * internal implementation
d2b7803e
DC
1301 * @param stdClass $received
1302 * @param resource $ch
1303 * @param mixed $header
1304 * @return int header length
3a1055a5
PS
1305 */
1306function download_file_content_header_handler($received, $ch, $header) {
1307 $received->headers[] = $header;
1308 return strlen($header);
1309}
1310
1311/**
f36cb951 1312 * internal implementation
d2b7803e
DC
1313 * @param stdClass $received
1314 * @param resource $ch
1315 * @param mixed $data
3a1055a5
PS
1316 */
1317function download_file_content_write_handler($received, $ch, $data) {
1318 if (!$received->fh) {
1319 $received->fh = fopen($received->tofile, 'w');
1320 if ($received->fh === false) {
1321 // bad luck, file creation or overriding failed
1322 return 0;
1323 }
1324 }
1325 if (fwrite($received->fh, $data) === false) {
1326 // bad luck, write failed, let's abort completely
1327 return 0;
1328 }
1329 return strlen($data);
1330}
1331
3ce73b14 1332/**
559276b1
MG
1333 * Returns a list of information about file types based on extensions.
1334 *
1335 * The following elements expected in value array for each extension:
1336 * 'type' - mimetype
1337 * 'icon' - location of the icon file. If value is FILENAME, then either pix/f/FILENAME.gif
1338 * or pix/f/FILENAME.png must be present in moodle and contain 16x16 filetype icon;
1339 * also files with bigger sizes under names
1340 * FILENAME-24, FILENAME-32, FILENAME-64, FILENAME-128, FILENAME-256 are recommended.
1341 * 'groups' (optional) - array of filetype groups this filetype extension is part of;
1342 * commonly used in moodle the following groups:
1343 * - web_image - image that can be included as <img> in HTML
1344 * - image - image that we can parse using GD to find it's dimensions, also used for portfolio format
1345 * - video - file that can be imported as video in text editor
1346 * - audio - file that can be imported as audio in text editor
1347 * - archive - we can extract files from this archive
1348 * - spreadsheet - used for portfolio format
1349 * - document - used for portfolio format
1350 * - presentation - used for portfolio format
1351 * 'string' (optional) - the name of the string from lang/en/mimetypes.php that displays
1352 * human-readable description for this filetype;
1353 * Function {@link get_mimetype_description()} first looks at the presence of string for
1354 * particular mimetype (value of 'type'), if not found looks for string specified in 'string'
1355 * attribute, if not found returns the value of 'type';
1356 * 'defaulticon' (boolean, optional) - used by function {@link file_mimetype_icon()} to find
1357 * an icon for mimetype. If an entry with 'defaulticon' is not found for a particular mimetype,
1358 * this function will return first found icon; Especially usefull for types such as 'text/plain'
d2b7803e
DC
1359 *
1360 * @category files
ba21c9d4 1361 * @return array List of information about file types based on extensions.
3ce73b14 1362 * Associative array of extension (lower-case) to associative array
1363 * from 'element name' to data. Current element names are 'type' and 'icon'.
76ca1ff1 1364 * Unknown types should use the 'xxx' entry which includes defaults.
3ce73b14 1365 */
559276b1 1366function &get_mimetypes_array() {
172dd12c 1367 static $mimearray = array (
ede72522 1368 'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown'),
5a322c8c
BR
1369 '3gp' => array ('type'=>'video/quicktime', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
1370 'aac' => array ('type'=>'audio/aac', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
d36249d6 1371 'accdb' => array ('type'=>'application/msaccess', 'icon'=>'base'),
0f1bdd0d 1372 'ai' => array ('type'=>'application/postscript', 'icon'=>'eps', 'groups'=>array('image'), 'string'=>'image'),
559276b1
MG
1373 'aif' => array ('type'=>'audio/x-aiff', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
1374 'aiff' => array ('type'=>'audio/x-aiff', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
1375 'aifc' => array ('type'=>'audio/x-aiff', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
ede72522 1376 'applescript' => array ('type'=>'text/plain', 'icon'=>'text'),
5a322c8c
BR
1377 'asc' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
1378 'asm' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
559276b1 1379 'au' => array ('type'=>'audio/au', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
ae7f35b9 1380 'avi' => array ('type'=>'video/x-ms-wm', 'icon'=>'avi', 'groups'=>array('video','web_video'), 'string'=>'video'),
0f1bdd0d 1381 'bmp' => array ('type'=>'image/bmp', 'icon'=>'bmp', 'groups'=>array('image'), 'string'=>'image'),
5a322c8c 1382 'c' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
ede72522 1383 'cct' => array ('type'=>'shockwave/director', 'icon'=>'flash'),
5a322c8c
BR
1384 'cpp' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
1385 'cs' => array ('type'=>'application/x-csh', 'icon'=>'sourcecode'),
559276b1 1386 'css' => array ('type'=>'text/css', 'icon'=>'text', 'groups'=>array('web_file')),
5a322c8c
BR
1387 'csv' => array ('type'=>'text/csv', 'icon'=>'spreadsheet', 'groups'=>array('spreadsheet')),
1388 'dv' => array ('type'=>'video/x-dv', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
1389 'dmg' => array ('type'=>'application/octet-stream', 'icon'=>'unknown'),
ede72522 1390
5a322c8c
BR
1391 'doc' => array ('type'=>'application/msword', 'icon'=>'document', 'groups'=>array('document')),
1392 'docx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'icon'=>'document', 'groups'=>array('document')),
1393 'docm' => array ('type'=>'application/vnd.ms-word.document.macroEnabled.12', 'icon'=>'document'),
1394 'dotx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'icon'=>'document'),
1395 'dotm' => array ('type'=>'application/vnd.ms-word.template.macroEnabled.12', 'icon'=>'document'),
ede72522
PS
1396
1397 'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
5a322c8c 1398 'dif' => array ('type'=>'video/x-dv', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
ede72522
PS
1399 'dir' => array ('type'=>'application/x-director', 'icon'=>'flash'),
1400 'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
0f1bdd0d 1401 'eps' => array ('type'=>'application/postscript', 'icon'=>'eps'),
d6d63941 1402 'epub' => array ('type'=>'application/epub+zip', 'icon'=>'epub', 'groups'=>array('document')),
ede72522 1403 'fdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
ae7f35b9
MG
1404 'flv' => array ('type'=>'video/x-flv', 'icon'=>'flash', 'groups'=>array('video','web_video'), 'string'=>'video'),
1405 'f4v' => array ('type'=>'video/mp4', 'icon'=>'flash', 'groups'=>array('video','web_video'), 'string'=>'video'),
e4594f73
FM
1406
1407 'gallery' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
3c509fdc 1408 'galleryitem' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
e4594f73 1409 'gallerycollection' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
0f1bdd0d 1410 'gif' => array ('type'=>'image/gif', 'icon'=>'gif', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
5a322c8c
BR
1411 'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
1412 'tgz' => array ('type'=>'application/g-zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
1413 'gz' => array ('type'=>'application/g-zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
1414 'gzip' => array ('type'=>'application/g-zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
1415 'h' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
1416 'hpp' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
1417 'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
1418 'htc' => array ('type'=>'text/x-component', 'icon'=>'markup'),
386a862f 1419 'html' => array ('type'=>'text/html', 'icon'=>'html', 'groups'=>array('web_file')),
2425229c 1420 'xhtml'=> array ('type'=>'application/xhtml+xml', 'icon'=>'html', 'groups'=>array('web_file')),
386a862f 1421 'htm' => array ('type'=>'text/html', 'icon'=>'html', 'groups'=>array('web_file')),
559276b1 1422 'ico' => array ('type'=>'image/vnd.microsoft.icon', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
ede72522
PS
1423 'ics' => array ('type'=>'text/calendar', 'icon'=>'text'),
1424 'isf' => array ('type'=>'application/inspiration', 'icon'=>'isf'),
1425 'ist' => array ('type'=>'application/inspiration.template', 'icon'=>'isf'),
5a322c8c 1426 'java' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
52f37ef4 1427 'jar' => array ('type'=>'application/java-archive', 'icon' => 'archive'),
5a322c8c
BR
1428 'jcb' => array ('type'=>'text/xml', 'icon'=>'markup'),
1429 'jcl' => array ('type'=>'text/xml', 'icon'=>'markup'),
1430 'jcw' => array ('type'=>'text/xml', 'icon'=>'markup'),
1431 'jmt' => array ('type'=>'text/xml', 'icon'=>'markup'),
1432 'jmx' => array ('type'=>'text/xml', 'icon'=>'markup'),
a8d6dda4
MG
1433 'jpe' => array ('type'=>'image/jpeg', 'icon'=>'jpeg', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
1434 'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'jpeg', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
1435 'jpg' => array ('type'=>'image/jpeg', 'icon'=>'jpeg', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
5a322c8c 1436 'jqz' => array ('type'=>'text/xml', 'icon'=>'markup'),
559276b1 1437 'js' => array ('type'=>'application/x-javascript', 'icon'=>'text', 'groups'=>array('web_file')),
ede72522 1438 'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text'),
5a322c8c 1439 'm' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
b3ea9cb2 1440 'mbz' => array ('type'=>'application/vnd.moodle.backup', 'icon'=>'moodle'),
d36249d6 1441 'mdb' => array ('type'=>'application/x-msaccess', 'icon'=>'base'),
2ec0ba91
DW
1442 'mht' => array ('type'=>'message/rfc822', 'icon'=>'archive'),
1443 'mhtml'=> array ('type'=>'message/rfc822', 'icon'=>'archive'),
5a322c8c
BR
1444 'mov' => array ('type'=>'video/quicktime', 'icon'=>'quicktime', 'groups'=>array('video','web_video'), 'string'=>'video'),
1445 'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'quicktime', 'groups'=>array('video'), 'string'=>'video'),
a8d6dda4 1446 'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'mp3', 'groups'=>array('audio'), 'string'=>'audio'),
ae7f35b9
MG
1447 'mp3' => array ('type'=>'audio/mp3', 'icon'=>'mp3', 'groups'=>array('audio','web_audio'), 'string'=>'audio'),
1448 'mp4' => array ('type'=>'video/mp4', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
1449 'm4v' => array ('type'=>'video/mp4', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
a8d6dda4 1450 'm4a' => array ('type'=>'audio/mp4', 'icon'=>'mp3', 'groups'=>array('audio'), 'string'=>'audio'),
ae7f35b9
MG
1451 'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
1452 'mpe' => array ('type'=>'video/mpeg', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
1453 'mpg' => array ('type'=>'video/mpeg', 'icon'=>'mpeg', 'groups'=>array('video','web_video'), 'string'=>'video'),
0f1bdd0d 1454
e4594f73
FM
1455 'nbk' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
1456 'notebook' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
1457
5a322c8c
BR
1458 'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'writer', 'groups'=>array('document')),
1459 'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'writer', 'groups'=>array('document')),
ca9875db 1460 'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'oth', 'groups'=>array('document')),
5a322c8c
BR
1461 'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'icon'=>'writer'),
1462 'odg' => array ('type'=>'application/vnd.oasis.opendocument.graphics', 'icon'=>'draw'),
1463 'otg' => array ('type'=>'application/vnd.oasis.opendocument.graphics-template', 'icon'=>'draw'),
1464 'odp' => array ('type'=>'application/vnd.oasis.opendocument.presentation', 'icon'=>'impress'),
1465 'otp' => array ('type'=>'application/vnd.oasis.opendocument.presentation-template', 'icon'=>'impress'),
1466 'ods' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet', 'icon'=>'calc', 'groups'=>array('spreadsheet')),
1467 'ots' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet-template', 'icon'=>'calc', 'groups'=>array('spreadsheet')),
1468 'odc' => array ('type'=>'application/vnd.oasis.opendocument.chart', 'icon'=>'chart'),
1469 'odf' => array ('type'=>'application/vnd.oasis.opendocument.formula', 'icon'=>'math'),
1470 'odb' => array ('type'=>'application/vnd.oasis.opendocument.database', 'icon'=>'base'),
1471 'odi' => array ('type'=>'application/vnd.oasis.opendocument.image', 'icon'=>'draw'),
1472 'oga' => array ('type'=>'audio/ogg', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
1473 'ogg' => array ('type'=>'audio/ogg', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
1474 'ogv' => array ('type'=>'video/ogg', 'icon'=>'video', 'groups'=>array('video'), 'string'=>'video'),
ede72522 1475
559276b1 1476 'pct' => array ('type'=>'image/pict', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
ede72522 1477 'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
5a322c8c 1478 'php' => array ('type'=>'text/plain', 'icon'=>'sourcecode'),
559276b1
MG
1479 'pic' => array ('type'=>'image/pict', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
1480 'pict' => array ('type'=>'image/pict', 'icon'=>'image', 'groups'=>array('image'), 'string'=>'image'),
0f1bdd0d 1481 'png' => array ('type'=>'image/png', 'icon'=>'png', 'groups'=>array('image', 'web_image'), 'string'=>'image'),
ede72522 1482
5a322c8c
BR
1483 'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint', 'groups'=>array('presentation')),
1484 'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint', 'groups'=>array('presentation')),
1485 'pptx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'icon'=>'powerpoint'),
1486 'pptm' => array ('type'=>'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'icon'=>'powerpoint'),
1487 'potx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.template', 'icon'=>'powerpoint'),
1488 'potm' => array ('type'=>'application/vnd.ms-powerpoint.template.macroEnabled.12', 'icon'=>'powerpoint'),
1489 'ppam' => array ('type'=>'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'icon'=>'powerpoint'),
1490 'ppsx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'icon'=>'powerpoint'),
1491 'ppsm' => array ('type'=>'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'icon'=>'powerpoint'),
ede72522
PS
1492
1493 'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf'),
5a322c8c 1494 'qt' => array ('type'=>'video/quicktime', 'icon'=>'quicktime', 'groups'=>array('video','web_video'), 'string'=>'video'),
ae7f35b9 1495 'ra' => array ('type'=>'audio/x-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('audio','web_audio'), 'string'=>'audio'),
559276b1 1496 'ram' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
5a322c8c 1497 'rhb' => array ('type'=>'text/xml', 'icon'=>'markup'),
559276b1
MG
1498 'rm' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('audio'), 'string'=>'audio'),
1499 'rmvb' => array ('type'=>'application/vnd.rn-realmedia-vbr', 'icon'=>'video', 'groups'=>array('video'), 'string'=>'video'),
1500 'rtf' => array ('type'=>'text/rtf', 'icon'=>'text', 'groups'=>array('document')),
ede72522 1501 'rtx' => array ('type'=>'text/richtext', 'icon'=>'text'),
0f1bdd0d 1502 'rv' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio', 'groups'=>array('video'), 'string'=>'video'),
5a322c8c
BR
1503 'sh' => array ('type'=>'application/x-sh', 'icon'=>'sourcecode'),
1504 'sit' => array ('type'=>'application/x-stuffit', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
ede72522
PS
1505 'smi' => array ('type'=>'application/smil', 'icon'=>'text'),
1506 'smil' => array ('type'=>'application/smil', 'icon'=>'text'),
5a322c8c 1507 'sqt' => array ('type'=>'text/xml', 'icon'=>'markup'),
ae7f35b9
MG
1508 'svg' => array ('type'=>'image/svg+xml', 'icon'=>'image', 'groups'=>array('image','web_image'), 'string'=>'image'),
1509 'svgz' => array ('type'=>'image/svg+xml', 'icon'=>'image', 'groups'=>array('image','web_image'), 'string'=>'image'),
ede72522 1510 'swa' => array ('type'=>'application/x-director', 'icon'=>'flash'),
ae7f35b9
MG
1511 'swf' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash', 'groups'=>array('video','web_video')),
1512 'swfl' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash', 'groups'=>array('video','web_video')),
ede72522 1513
5a322c8c
BR
1514 'sxw' => array ('type'=>'application/vnd.sun.xml.writer', 'icon'=>'writer'),
1515 'stw' => array ('type'=>'application/vnd.sun.xml.writer.template', 'icon'=>'writer'),
1516 'sxc' => array ('type'=>'application/vnd.sun.xml.calc', 'icon'=>'calc'),
1517 'stc' => array ('type'=>'application/vnd.sun.xml.calc.template', 'icon'=>'calc'),
1518 'sxd' => array ('type'=>'application/vnd.sun.xml.draw', 'icon'=>'draw'),
1519 'std' => array ('type'=>'application/vnd.sun.xml.draw.template', 'icon'=>'draw'),
1520 'sxi' => array ('type'=>'application/vnd.sun.xml.impress', 'icon'=>'impress'),
1521 'sti' => array ('type'=>'application/vnd.sun.xml.impress.template', 'icon'=>'impress'),
1522 'sxg' => array ('type'=>'application/vnd.sun.xml.writer.global', 'icon'=>'writer'),
1523 'sxm' => array ('type'=>'application/vnd.sun.xml.math', 'icon'=>'math'),
1524
1525 'tar' => array ('type'=>'application/x-tar', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive'),
0f1bdd0d
BR
1526 'tif' => array ('type'=>'image/tiff', 'icon'=>'tiff', 'groups'=>array('image'), 'string'=>'image'),
1527 'tiff' => array ('type'=>'image/tiff', 'icon'=>'tiff', 'groups'=>array('image'), 'string'=>'image'),
ede72522
PS
1528 'tex' => array ('type'=>'application/x-tex', 'icon'=>'text'),
1529 'texi' => array ('type'=>'application/x-texinfo', 'icon'=>'text'),
1530 'texinfo' => array ('type'=>'application/x-texinfo', 'icon'=>'text'),
1531 'tsv' => array ('type'=>'text/tab-separated-values', 'icon'=>'text'),
559276b1 1532 'txt' => array ('type'=>'text/plain', 'icon'=>'text', 'defaulticon'=>true),
0f1bdd0d 1533 'wav' => array ('type'=>'audio/wav', 'icon'=>'wav', 'groups'=>array('audio'), 'string'=>'audio'),
5a322c8c 1534 'webm' => array ('type'=>'video/webm', 'icon'=>'video', 'groups'=>array('video'), 'string'=>'video'),
0f1bdd0d
BR
1535 'wmv' => array ('type'=>'video/x-ms-wmv', 'icon'=>'wmv', 'groups'=>array('video'), 'string'=>'video'),
1536 'asf' => array ('type'=>'video/x-ms-asf', 'icon'=>'wmv', 'groups'=>array('video'), 'string'=>'video'),
e4594f73
FM
1537
1538 'xbk' => array ('type'=>'application/x-smarttech-notebook', 'icon'=>'archive'),
ede72522
PS
1539 'xdp' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
1540 'xfd' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
1541 'xfdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
1542
5a322c8c
BR
1543 'xls' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'spreadsheet', 'groups'=>array('spreadsheet')),
1544 'xlsx' => array ('type'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'icon'=>'spreadsheet'),
1545 'xlsm' => array ('type'=>'application/vnd.ms-excel.sheet.macroEnabled.12', 'icon'=>'spreadsheet', 'groups'=>array('spreadsheet')),
1546 'xltx' => array ('type'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'icon'=>'spreadsheet'),
1547 'xltm' => array ('type'=>'application/vnd.ms-excel.template.macroEnabled.12', 'icon'=>'spreadsheet'),
1548 'xlsb' => array ('type'=>'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'icon'=>'spreadsheet'),
1549 'xlam' => array ('type'=>'application/vnd.ms-excel.addin.macroEnabled.12', 'icon'=>'spreadsheet'),
1550
1551 'xml' => array ('type'=>'application/xml', 'icon'=>'markup'),
1552 'xsl' => array ('type'=>'text/xml', 'icon'=>'markup'),
e4594f73 1553
5a322c8c 1554 'zip' => array ('type'=>'application/zip', 'icon'=>'archive', 'groups'=>array('archive'), 'string'=>'archive')
f1e0649c 1555 );
172dd12c 1556 return $mimearray;
3ce73b14 1557}
1558
76ca1ff1 1559/**
3ce73b14 1560 * Obtains information about a filetype based on its extension. Will
1561 * use a default if no information is present about that particular
1562 * extension.
ba21c9d4 1563 *
d2b7803e 1564 * @category files
76ca1ff1 1565 * @param string $element Desired information (usually 'icon'
559276b1
MG
1566 * for icon filename or 'type' for MIME type. Can also be
1567 * 'icon24', ...32, 48, 64, 72, 80, 96, 128, 256)
76ca1ff1 1568 * @param string $filename Filename we're looking up
3ce73b14 1569 * @return string Requested piece of information from array
1570 */
1571function mimeinfo($element, $filename) {
0ef98843 1572 global $CFG;
559276b1
MG
1573 $mimeinfo = & get_mimetypes_array();
1574 static $iconpostfixes = array(256=>'-256', 128=>'-128', 96=>'-96', 80=>'-80', 72=>'-72', 64=>'-64', 48=>'-48', 32=>'-32', 24=>'-24', 16=>'');
1575
1576 $filetype = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
1577 if (empty($filetype)) {
1578 $filetype = 'xxx'; // file without extension
1579 }
1580 if (preg_match('/^icon(\d*)$/', $element, $iconsizematch)) {
1581 $iconsize = max(array(16, (int)$iconsizematch[1]));
1582 $filenames = array($mimeinfo['xxx']['icon']);
1583 if ($filetype != 'xxx' && isset($mimeinfo[$filetype]['icon'])) {
1584 array_unshift($filenames, $mimeinfo[$filetype]['icon']);
1585 }
1586 // find the file with the closest size, first search for specific icon then for default icon
1587 foreach ($filenames as $filename) {
1588 foreach ($iconpostfixes as $size => $postfix) {
1589 $fullname = $CFG->dirroot.'/pix/f/'.$filename.$postfix;
1590 if ($iconsize >= $size && (file_exists($fullname.'.png') || file_exists($fullname.'.gif'))) {
1591 return $filename.$postfix;
72aa74ce 1592 }
0ef98843 1593 }
f1e0649c 1594 }
559276b1
MG
1595 } else if (isset($mimeinfo[$filetype][$element])) {
1596 return $mimeinfo[$filetype][$element];
1597 } else if (isset($mimeinfo['xxx'][$element])) {
a370c895 1598 return $mimeinfo['xxx'][$element]; // By default
559276b1
MG
1599 } else {
1600 return null;
f1e0649c 1601 }
1602}
1603
76ca1ff1 1604/**
3ce73b14 1605 * Obtains information about a filetype based on the MIME type rather than
1606 * the other way around.
ba21c9d4 1607 *
d2b7803e 1608 * @category files
559276b1 1609 * @param string $element Desired information ('extension', 'icon', 'icon-24', etc.)
76ca1ff1 1610 * @param string $mimetype MIME type we're looking up
3ce73b14 1611 * @return string Requested piece of information from array
1612 */
1613function mimeinfo_from_type($element, $mimetype) {
559276b1
MG
1614 /* array of cached mimetype->extension associations */
1615 static $cached = array();
1616 $mimeinfo = & get_mimetypes_array();
1617
1618 if (!array_key_exists($mimetype, $cached)) {
1082e7da 1619 $cached[$mimetype] = null;
559276b1
MG
1620 foreach($mimeinfo as $filetype => $values) {
1621 if ($values['type'] == $mimetype) {
1622 if ($cached[$mimetype] === null) {
e5635827 1623 $cached[$mimetype] = '.'.$filetype;
559276b1
MG
1624 }
1625 if (!empty($values['defaulticon'])) {
e5635827 1626 $cached[$mimetype] = '.'.$filetype;
559276b1
MG
1627 break;
1628 }
3ce73b14 1629 }
3ce73b14 1630 }
a8d6dda4 1631 if (empty($cached[$mimetype])) {
e5635827 1632 $cached[$mimetype] = '.xxx';
559276b1 1633 }
a8d6dda4
MG
1634 }
1635 if ($element === 'extension') {
1636 return $cached[$mimetype];
559276b1 1637 } else {
e5635827 1638 return mimeinfo($element, $cached[$mimetype]);
559276b1 1639 }
3ce73b14 1640}
b9709b76 1641
42ead7d7 1642/**
559276b1 1643 * Return the relative icon path for a given file
ba21c9d4 1644 *
559276b1
MG
1645 * Usage:
1646 * <code>
1647 * // $file - instance of stored_file or file_info
1648 * $icon = $OUTPUT->pix_url(file_file_icon($file))->out();
1649 * echo html_writer::empty_tag('img', array('src' => $icon, 'alt' => get_mimetype_description($file)));
1650 * </code>
1651 * or
1652 * <code>
1653 * echo $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file));
1654 * </code>
1655 *
1656 * @param stored_file|file_info|stdClass|array $file (in case of object attributes $file->filename
1657 * and $file->mimetype are expected)
1658 * @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
1659 * @return string
42ead7d7 1660 */
559276b1
MG
1661function file_file_icon($file, $size = null) {
1662 if (!is_object($file)) {
1663 $file = (object)$file;
1664 }
1665 if (isset($file->filename)) {
1666 $filename = $file->filename;
1667 } else if (method_exists($file, 'get_filename')) {
1668 $filename = $file->get_filename();
1669 } else if (method_exists($file, 'get_visible_name')) {
1670 $filename = $file->get_visible_name();
1671 } else {
1672 $filename = '';
637a60dc 1673 }
559276b1
MG
1674 if (isset($file->mimetype)) {
1675 $mimetype = $file->mimetype;
1676 } else if (method_exists($file, 'get_mimetype')) {
1677 $mimetype = $file->get_mimetype();
1678 } else {
1679 $mimetype = '';
42ead7d7 1680 }
ae7f35b9
MG
1681 $mimetypes = &get_mimetypes_array();
1682 if ($filename) {
1683 $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
1684 if ($extension && !empty($mimetypes[$extension])) {
1685 // if file name has known extension, return icon for this extension
1686 return file_extension_icon($filename, $size);
1687 }
0b46f19e 1688 }
ae7f35b9 1689 return file_mimetype_icon($mimetype, $size);
559276b1 1690}
637a60dc 1691
559276b1
MG
1692/**
1693 * Return the relative icon path for a folder image
1694 *
1695 * Usage:
1696 * <code>
1697 * $icon = $OUTPUT->pix_url(file_folder_icon())->out();
1698 * echo html_writer::empty_tag('img', array('src' => $icon));
1699 * </code>
1700 * or
1701 * <code>
1702 * echo $OUTPUT->pix_icon(file_folder_icon(32));
1703 * </code>
1704 *
1705 * @param int $iconsize The size of the icon. Defaults to 16 can also be 24, 32, 48, 64, 72, 80, 96, 128, 256
1706 * @return string
1707 */
1708function file_folder_icon($iconsize = null) {
1709 global $CFG;
1710 static $iconpostfixes = array(256=>'-256', 128=>'-128', 96=>'-96', 80=>'-80', 72=>'-72', 64=>'-64', 48=>'-48', 32=>'-32', 24=>'-24', 16=>'');
ae7f35b9 1711 static $cached = array();
559276b1 1712 $iconsize = max(array(16, (int)$iconsize));
ae7f35b9
MG
1713 if (!array_key_exists($iconsize, $cached)) {
1714 foreach ($iconpostfixes as $size => $postfix) {
1715 $fullname = $CFG->dirroot.'/pix/f/folder'.$postfix;
1716 if ($iconsize >= $size && (file_exists($fullname.'.png') || file_exists($fullname.'.gif'))) {
1717 $cached[$iconsize] = 'f/folder'.$postfix;
1718 break;
1719 }
559276b1 1720 }
637a60dc 1721 }
ae7f35b9 1722 return $cached[$iconsize];
42ead7d7 1723}
1724
9dffa7af 1725/**
1726 * Returns the relative icon path for a given mime type
1727 *
34ff25a6 1728 * This function should be used in conjunction with $OUTPUT->pix_url to produce
9dffa7af 1729 * a return the full path to an icon.
1730 *
1731 * <code>
1732 * $mimetype = 'image/jpg';
559276b1
MG
1733 * $icon = $OUTPUT->pix_url(file_mimetype_icon($mimetype))->out();
1734 * echo html_writer::empty_tag('img', array('src' => $icon, 'alt' => get_mimetype_description($mimetype)));
9dffa7af 1735 * </code>
1736 *
d2b7803e
DC
1737 * @category files
1738 * @todo MDL-31074 When an $OUTPUT->icon method is available this function should be altered
9dffa7af 1739 * to conform with that.
9dffa7af 1740 * @param string $mimetype The mimetype to fetch an icon for
559276b1 1741 * @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
9dffa7af 1742 * @return string The relative path to the icon
1743 */
ede72522 1744function file_mimetype_icon($mimetype, $size = NULL) {
559276b1 1745 return 'f/'.mimeinfo_from_type('icon'.$size, $mimetype);
9dffa7af 1746}
1747
1748/**
2dcb7d0b 1749 * Returns the relative icon path for a given file name
9dffa7af 1750 *
34ff25a6 1751 * This function should be used in conjunction with $OUTPUT->pix_url to produce
9dffa7af 1752 * a return the full path to an icon.
1753 *
9dffa7af 1754 * <code>
559276b1
MG
1755 * $filename = '.jpg';
1756 * $icon = $OUTPUT->pix_url(file_extension_icon($filename))->out();
1757 * echo html_writer::empty_tag('img', array('src' => $icon, 'alt' => '...'));
9dffa7af 1758 * </code>
1759 *
d2b7803e 1760 * @todo MDL-31074 When an $OUTPUT->icon method is available this function should be altered
9dffa7af 1761 * to conform with that.
d2b7803e
DC
1762 * @todo MDL-31074 Implement $size
1763 * @category files
1764 * @param string $filename The filename to get the icon for
559276b1 1765 * @param int $size The size of the icon. Defaults to 16 can also be 24, 32, 64, 128, 256
9dffa7af 1766 * @return string
1767 */
ede72522 1768function file_extension_icon($filename, $size = NULL) {
559276b1 1769 return 'f/'.mimeinfo('icon'.$size, $filename);
9dffa7af 1770}
1771
c0381e22 1772/**
76ca1ff1 1773 * Obtains descriptions for file types (e.g. 'Microsoft Word document') from the
1774 * mimetypes.php language file.
ba21c9d4 1775 *
559276b1
MG
1776 * @param mixed $obj - instance of stored_file or file_info or array/stdClass with field
1777 * 'filename' and 'mimetype', or just a string with mimetype (though it is recommended to
1778 * have filename); In case of array/stdClass the field 'mimetype' is optional.
c0381e22 1779 * @param bool $capitalise If true, capitalises first character of result
76ca1ff1 1780 * @return string Text description
c0381e22 1781 */
559276b1 1782function get_mimetype_description($obj, $capitalise=false) {
ae7f35b9 1783 $filename = $mimetype = '';
559276b1
MG
1784 if (is_object($obj) && method_exists($obj, 'get_filename') && method_exists($obj, 'get_mimetype')) {
1785 // this is an instance of stored_file
1786 $mimetype = $obj->get_mimetype();
1787 $filename = $obj->get_filename();
1788 } else if (is_object($obj) && method_exists($obj, 'get_visible_name') && method_exists($obj, 'get_mimetype')) {
1789 // this is an instance of file_info
1790 $mimetype = $obj->get_mimetype();
1791 $filename = $obj->get_visible_name();
1792 } else if (is_array($obj) || is_object ($obj)) {
1793 $obj = (array)$obj;
1794 if (!empty($obj['filename'])) {
1795 $filename = $obj['filename'];
559276b1
MG
1796 }
1797 if (!empty($obj['mimetype'])) {
1798 $mimetype = $obj['mimetype'];
559276b1
MG
1799 }
1800 } else {
1801 $mimetype = $obj;
ae7f35b9
MG
1802 }
1803 $mimetypefromext = mimeinfo('type', $filename);
1804 if (empty($mimetype) || $mimetypefromext !== 'document/unknown') {
1805 // if file has a known extension, overwrite the specified mimetype
1806 $mimetype = $mimetypefromext;
559276b1
MG
1807 }
1808 $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
1809 if (empty($extension)) {
1810 $mimetypestr = mimeinfo_from_type('string', $mimetype);
ae7f35b9 1811 $extension = str_replace('.', '', mimeinfo_from_type('extension', $mimetype));
559276b1
MG
1812 } else {
1813 $mimetypestr = mimeinfo('string', $filename);
1814 }
1815 $chunks = explode('/', $mimetype, 2);
1816 $chunks[] = '';
1817 $attr = array(
1818 'mimetype' => $mimetype,
1819 'ext' => $extension,
1820 'mimetype1' => $chunks[0],
1821 'mimetype2' => $chunks[1],
1822 );
1823 $a = array();
1824 foreach ($attr as $key => $value) {
1825 $a[$key] = $value;
1826 $a[strtoupper($key)] = strtoupper($value);
1827 $a[ucfirst($key)] = ucfirst($value);
1828 }
d6d63941 1829
1830 // MIME types may include + symbol but this is not permitted in string ids.
1831 $safemimetype = str_replace('+', '_', $mimetype);
1832 $safemimetypestr = str_replace('+', '_', $mimetypestr);
1833 if (get_string_manager()->string_exists($safemimetype, 'mimetypes')) {
1834 $result = get_string($safemimetype, 'mimetypes', (object)$a);
1835 } else if (get_string_manager()->string_exists($safemimetypestr, 'mimetypes')) {
1836 $result = get_string($safemimetypestr, 'mimetypes', (object)$a);
559276b1
MG
1837 } else if (get_string_manager()->string_exists('default', 'mimetypes')) {
1838 $result = get_string('default', 'mimetypes', (object)$a);
1b80c913 1839 } else {
559276b1 1840 $result = $mimetype;
76ca1ff1 1841 }
ede72522 1842 if ($capitalise) {
c0381e22 1843 $result=ucfirst($result);
1844 }
1845 return $result;
1846}
1847
559276b1
MG
1848/**
1849 * Returns array of elements of type $element in type group(s)
1850 *
1851 * @param string $element name of the element we are interested in, usually 'type' or 'extension'
1852 * @param string|array $groups one group or array of groups/extensions/mimetypes
1853 * @return array
1854 */
1855function file_get_typegroup($element, $groups) {
1856 static $cached = array();
1857 if (!is_array($groups)) {
1858 $groups = array($groups);
1859 }
1860 if (!array_key_exists($element, $cached)) {
1861 $cached[$element] = array();
1862 }
1863 $result = array();
1864 foreach ($groups as $group) {
1865 if (!array_key_exists($group, $cached[$element])) {
1866 // retrieive and cache all elements of type $element for group $group
1867 $mimeinfo = & get_mimetypes_array();
1868 $cached[$element][$group] = array();
1869 foreach ($mimeinfo as $extension => $value) {
e5635827 1870 $value['extension'] = '.'.$extension;
559276b1
MG
1871 if (empty($value[$element])) {
1872 continue;
1873 }
ae7f35b9 1874 if (($group === '.'.$extension || $group === $value['type'] ||
559276b1
MG
1875 (!empty($value['groups']) && in_array($group, $value['groups']))) &&
1876 !in_array($value[$element], $cached[$element][$group])) {
1877 $cached[$element][$group][] = $value[$element];
1878 }
1879 }
1880 }
1881 $result = array_merge($result, $cached[$element][$group]);
1882 }
1883 return array_unique($result);
1884}
1885
1886/**
1887 * Checks if file with name $filename has one of the extensions in groups $groups
1888 *
1889 * @see get_mimetypes_array()
1890 * @param string $filename name of the file to check
1891 * @param string|array $groups one group or array of groups to check
1892 * @param bool $checktype if true and extension check fails, find the mimetype and check if
1893 * file mimetype is in mimetypes in groups $groups
1894 * @return bool
1895 */
1896function file_extension_in_typegroup($filename, $groups, $checktype = false) {
1897 $extension = pathinfo($filename, PATHINFO_EXTENSION);
e5635827 1898 if (!empty($extension) && in_array('.'.strtolower($extension), file_get_typegroup('extension', $groups))) {
559276b1
MG
1899 return true;
1900 }
1901 return $checktype && file_mimetype_in_typegroup(mimeinfo('type', $filename), $groups);
1902}
1903
1904/**
1905 * Checks if mimetype $mimetype belongs to one of the groups $groups
1906 *
1907 * @see get_mimetypes_array()
1908 * @param string $mimetype
1909 * @param string|array $groups one group or array of groups to check
1910 * @return bool
1911 */
1912function file_mimetype_in_typegroup($mimetype, $groups) {
1913 return !empty($mimetype) && in_array($mimetype, file_get_typegroup('type', $groups));
1914}
1915
9e5fa330 1916/**
d2b7803e 1917 * Requested file is not found or not accessible, does not return, terminates script
ba21c9d4 1918 *
d2b7803e
DC
1919 * @global stdClass $CFG
1920 * @global stdClass $COURSE
9e5fa330 1921 */
1922function send_file_not_found() {
1923 global $CFG, $COURSE;
dbdc7355 1924 send_header_404();
9e5fa330 1925 print_error('filenotfound', 'error', $CFG->wwwroot.'/course/view.php?id='.$COURSE->id); //this is not displayed on IIS??
1926}
dbdc7355
DM
1927/**
1928 * Helper function to send correct 404 for server.
1929 */
1930function send_header_404() {
1931 if (substr(php_sapi_name(), 0, 3) == 'cgi') {
1932 header("Status: 404 Not Found");
1933 } else {
1934 header('HTTP/1.0 404 not found');
1935 }
1936}
9e5fa330 1937
cbad562e 1938/**
d5dd0540
PS
1939 * Enhanced readfile() with optional acceleration.
1940 * @param string|stored_file $file
1941 * @param string $mimetype
1942 * @param bool $accelerate
1943 * @return void
cbad562e 1944 */
d5dd0540
PS
1945function readfile_accel($file, $mimetype, $accelerate) {
1946 global $CFG;
1947
1948 if ($mimetype === 'text/plain') {
1949 // there is no encoding specified in text files, we need something consistent
1950 header('Content-Type: text/plain; charset=utf-8');
1951 } else {
1952 header('Content-Type: '.$mimetype);
596c7115
PS
1953 }
1954
d5dd0540
PS
1955 $lastmodified = is_object($file) ? $file->get_timemodified() : filemtime($file);
1956 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
116ee3e4 1957
d5dd0540 1958 if (is_object($file)) {
c5338156
DP
1959 if (empty($_SERVER['HTTP_RANGE'])) {
1960 // Use Etag only when not byteserving,
1961 // is it tag of this range or whole file?
1962 header('Etag: ' . $file->get_contenthash());
1963 }
d5dd0540
PS
1964 if (isset($_SERVER['HTTP_IF_NONE_MATCH']) and $_SERVER['HTTP_IF_NONE_MATCH'] === $file->get_contenthash()) {
1965 header('HTTP/1.1 304 Not Modified');
1966 return;
1967 }
1968 }
1969
1970 // if etag present for stored file rely on it exclusively
1971 if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) and (empty($_SERVER['HTTP_IF_NONE_MATCH']) or !is_object($file))) {
1972 // get unixtime of request header; clip extra junk off first
1973 $since = strtotime(preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]));
1974 if ($since && $since >= $lastmodified) {
1975 header('HTTP/1.1 304 Not Modified');
1976 return;
1977 }
1978 }
1979
1980 if ($accelerate and !empty($CFG->xsendfile)) {
1981 if (empty($CFG->disablebyteserving) and $mimetype !== 'text/plain') {
1982 header('Accept-Ranges: bytes');
1983 } else {
1984 header('Accept-Ranges: none');
1985 }
1986
1987 if (is_object($file)) {
1988 $fs = get_file_storage();
1989 if ($fs->xsendfile($file->get_contenthash())) {
1990 return;
1991 }
1992
1993 } else {
1994 require_once("$CFG->libdir/xsendfilelib.php");
1995 if (xsendfile($file)) {
1996 return;
1997 }
1998 }
1999 }
2000
2001 $filesize = is_object($file) ? $file->get_filesize() : filesize($file);
2002
2003 header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
2004
2005 if ($accelerate and empty($CFG->disablebyteserving) and $mimetype !== 'text/plain') {
2006 header('Accept-Ranges: bytes');
2007
2008 if (!empty($_SERVER['HTTP_RANGE']) and strpos($_SERVER['HTTP_RANGE'],'bytes=') !== FALSE) {
2009 // byteserving stuff - for acrobat reader and download accelerators
2010 // see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
2011 // inspired by: http://www.coneural.org/florian/papers/04_byteserving.php
2012 $ranges = false;
2013 if (preg_match_all('/(\d*)-(\d*)/', $_SERVER['HTTP_RANGE'], $ranges, PREG_SET_ORDER)) {
2014 foreach ($ranges as $key=>$value) {
2015 if ($ranges[$key][1] == '') {
2016 //suffix case
2017 $ranges[$key][1] = $filesize - $ranges[$key][2];
2018 $ranges[$key][2] = $filesize - 1;
2019 } else if ($ranges[$key][2] == '' || $ranges[$key][2] > $filesize - 1) {
2020 //fix range length
2021 $ranges[$key][2] = $filesize - 1;
2022 }
2023 if ($ranges[$key][2] != '' && $ranges[$key][2] < $ranges[$key][1]) {
2024 //invalid byte-range ==> ignore header
2025 $ranges = false;
2026 break;
2027 }
2028 //prepare multipart header
2029 $ranges[$key][0] = "\r\n--".BYTESERVING_BOUNDARY."\r\nContent-Type: $mimetype\r\n";
2030 $ranges[$key][0] .= "Content-Range: bytes {$ranges[$key][1]}-{$ranges[$key][2]}/$filesize\r\n\r\n";
2031 }
2032 } else {
2033 $ranges = false;
2034 }
2035 if ($ranges) {
2036 if (is_object($file)) {
2037 $handle = $file->get_content_file_handle();
2038 } else {
2039 $handle = fopen($file, 'rb');
2040 }
2041 byteserving_send_file($handle, $mimetype, $ranges, $filesize);
2042 }
2043 }
2044 } else {
2045 // Do not byteserve
2046 header('Accept-Ranges: none');
2047 }
2048
2049 header('Content-Length: '.$filesize);
2050
2051 if ($filesize > 10000000) {
2052 // for large files try to flush and close all buffers to conserve memory
2053 while(@ob_get_level()) {
2054 if (!@ob_end_flush()) {
2055 break;
2056 }
cbad562e
PS
2057 }
2058 }
2059
d5dd0540
PS
2060 // send the whole file content
2061 if (is_object($file)) {
2062 $file->readfile();
2063 } else {
2064 readfile($file);
2065 }
2066}
2067
2068/**
2069 * Similar to readfile_accel() but designed for strings.
2070 * @param string $string
2071 * @param string $mimetype
2072 * @param bool $accelerate
2073 * @return void
2074 */
2075function readstring_accel($string, $mimetype, $accelerate) {
2076 global $CFG;
2077
2078 if ($mimetype === 'text/plain') {
2079 // there is no encoding specified in text files, we need something consistent
2080 header('Content-Type: text/plain; charset=utf-8');
2081 } else {
2082 header('Content-Type: '.$mimetype);
2083 }
2084 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
2085 header('Accept-Ranges: none');
2086
2087 if ($accelerate and !empty($CFG->xsendfile)) {
2088 $fs = get_file_storage();
2089 if ($fs->xsendfile(sha1($string))) {
2090 return;
2091 }
2092 }
cbad562e 2093
d5dd0540
PS
2094 header('Content-Length: '.strlen($string));
2095 echo $string;
cbad562e
PS
2096}
2097
c87c428e 2098/**
2099 * Handles the sending of temporary file to user, download is forced.
d2b7803e 2100 * File is deleted after abort or successful sending, does not return, script terminated
ba21c9d4 2101 *
c87c428e 2102 * @param string $path path to file, preferably from moodledata/temp/something; or content of file itself
2103 * @param string $filename proposed file name when saving file
d2b7803e 2104 * @param bool $pathisstring If the path is string
c87c428e 2105 */
45c0d224 2106function send_temp_file($path, $filename, $pathisstring=false) {
c87c428e 2107 global $CFG;
2108
d5dd0540
PS
2109 if (check_browser_version('Firefox', '1.5')) {
2110 // only FF is known to correctly save to disk before opening...
2111 $mimetype = mimeinfo('type', $filename);
2112 } else {
2113 $mimetype = 'application/x-forcedownload';
2114 }
2115
c87c428e 2116 // close session - not needed anymore
d5dd0540 2117 session_get_instance()->write_close();
c87c428e 2118
2119 if (!$pathisstring) {
2120 if (!file_exists($path)) {
dbdc7355 2121 send_header_404();
45c0d224 2122 print_error('filenotfound', 'error', $CFG->wwwroot.'/');
c87c428e 2123 }
2124 // executed after normal finish or abort
2125 @register_shutdown_function('send_temp_file_finished', $path);
2126 }
2127
c87c428e 2128 // if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
2129 if (check_browser_version('MSIE')) {
2130 $filename = urlencode($filename);
2131 }
2132
66969d50 2133 header('Content-Disposition: attachment; filename="'.$filename.'"');
c87c428e 2134 if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
cbad562e
PS
2135 header('Cache-Control: max-age=10');
2136 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
2137 header('Pragma: ');
c87c428e 2138 } else { //normal http - prevent caching at all cost
cbad562e
PS
2139 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
2140 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
2141 header('Pragma: no-cache');
c87c428e 2142 }
c87c428e 2143
d5dd0540 2144 // send the contents - we can not accelerate this because the file will be deleted asap
c87c428e 2145 if ($pathisstring) {
d5dd0540 2146 readstring_accel($path, $mimetype, false);
c87c428e 2147 } else {
d5dd0540
PS
2148 readfile_accel($path, $mimetype, false);
2149 @unlink($path);
c87c428e 2150 }
2151
2152 die; //no more chars to output
2153}
2154
2155/**
34ff25a6 2156 * Internal callback function used by send_temp_file()
d2b7803e
DC
2157 *
2158 * @param string $path
c87c428e 2159 */
2160function send_temp_file_finished($path) {
2161 if (file_exists($path)) {
2162 @unlink($path);
2163 }
2164}
2165
76ca1ff1 2166/**
2167 * Handles the sending of file data to the user's browser, including support for
2168 * byteranges etc.
ba21c9d4 2169 *
d2b7803e 2170 * @category files
ba75ad94 2171 * @param string $path Path of file on disk (including real filename), or actual content of file as string
2172 * @param string $filename Filename to send
2173 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
2174 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
2175 * @param bool $pathisstring If true (default false), $path is the content to send and not the pathname
2176 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
2177 * @param string $mimetype Include to specify the MIME type; leave blank to have it guess the type from $filename
b379f7d9 2178 * @param bool $dontdie - return control to caller afterwards. this is not recommended and only used for cleanup tasks.
2179 * if this is passed as true, ignore_user_abort is called. if you don't want your processing to continue on cancel,
29e3d7e2 2180 * you must detect this case when control is returned using connection_aborted. Please not that session is closed
2181 * and should not be reopened.
d2b7803e 2182 * @return null script execution stopped unless $dontdie is true
b9709b76 2183 */
b379f7d9 2184function send_file($path, $filename, $lifetime = 'default' , $filter=0, $pathisstring=false, $forcedownload=false, $mimetype='', $dontdie=false) {
d5dd0540 2185 global $CFG, $COURSE;
50f95991 2186
b379f7d9 2187 if ($dontdie) {
15325f55 2188 ignore_user_abort(true);
b379f7d9 2189 }
2190
c8a5c6a4 2191 // MDL-11789, apply $CFG->filelifetime here
2192 if ($lifetime === 'default') {
2193 if (!empty($CFG->filelifetime)) {
0cde3ede 2194 $lifetime = $CFG->filelifetime;
c8a5c6a4 2195 } else {
0cde3ede 2196 $lifetime = 86400;
c8a5c6a4 2197 }
2198 }
2199
56949c17 2200 session_get_instance()->write_close(); // unlock session during fileserving
172dd12c 2201
ba4e0b05 2202 // Use given MIME type if specified, otherwise guess it using mimeinfo.
2203 // IE, Konqueror and Opera open html file directly in browser from web even when directed to save it to disk :-O
2204 // only Firefox saves all files locally before opening when content-disposition: attachment stated
2205 $isFF = check_browser_version('Firefox', '1.5'); // only FF > 1.5 properly tested
76ca1ff1 2206 $mimetype = ($forcedownload and !$isFF) ? 'application/x-forcedownload' :
ba4e0b05 2207 ($mimetype ? $mimetype : mimeinfo('type', $filename));
b5bbeaf0 2208
4f047de2 2209 // if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
2210 if (check_browser_version('MSIE')) {
4638009b 2211 $filename = rawurlencode($filename);
4f047de2 2212 }
2213
4c8c65ec 2214 if ($forcedownload) {
cbad562e 2215 header('Content-Disposition: attachment; filename="'.$filename.'"');
4c8c65ec 2216 } else {
cbad562e 2217 header('Content-Disposition: inline; filename="'.$filename.'"');
4c8c65ec 2218 }
2219
f1e0649c 2220 if ($lifetime > 0) {
d5dd0540 2221 $nobyteserving = false;
cbad562e
PS
2222 header('Cache-Control: max-age='.$lifetime);
2223 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
2224 header('Pragma: ');
4c8c65ec 2225
4c8c65ec 2226 } else { // Do not cache files in proxies and browsers
d5dd0540 2227 $nobyteserving = true;
85e00626 2228 if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
cbad562e
PS
2229 header('Cache-Control: max-age=10');
2230 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
2231 header('Pragma: ');
85e00626 2232 } else { //normal http - prevent caching at all cost
cbad562e
PS
2233 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
2234 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
2235 header('Pragma: no-cache');
85e00626 2236 }
69faecce 2237 }
f1e0649c 2238
b9709b76 2239 if (empty($filter)) {
cbad562e 2240 // send the contents
f1e0649c 2241 if ($pathisstring) {
d5dd0540 2242 readstring_accel($path, $mimetype, !$dontdie);
4c8c65ec 2243 } else {
d5dd0540 2244 readfile_accel($path, $mimetype, !$dontdie);
f1e0649c 2245 }
cbad562e 2246
d5dd0540
PS
2247 } else {
2248 // Try to put the file through filters
f1e0649c 2249 if ($mimetype == 'text/html') {
365a5941 2250 $options = new stdClass();
f1e0649c 2251 $options->noclean = true;
a17c57b5 2252 $options->nocache = true; // temporary workaround for MDL-5136
f1e0649c 2253 $text = $pathisstring ? $path : implode('', file($path));
76ca1ff1 2254
3ace5ee4 2255 $text = file_modify_html_header($text);
60f9e36e 2256 $output = format_text($text, FORMAT_HTML, $options, $COURSE->id);
f1e0649c 2257
d5dd0540 2258 readstring_accel($output, $mimetype, false);
c4cef1fe 2259
b9709b76 2260 } else if (($mimetype == 'text/plain') and ($filter == 1)) {
d5dd0540 2261 // only filter text if filter all files is selected
365a5941 2262 $options = new stdClass();
f1e0649c 2263 $options->newlines = false;
2264 $options->noclean = true;
f6ca5cf3 2265 $text = htmlentities($pathisstring ? $path : implode('', file($path)), ENT_QUOTES, 'UTF-8');
60f9e36e 2266 $output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $COURSE->id) .'</pre>';
f1e0649c 2267
d5dd0540 2268 readstring_accel($output, $mimetype, false);
c4cef1fe 2269
d5dd0540 2270 } else {
cbad562e 2271 // send the contents
f1e0649c 2272 if ($pathisstring) {
d5dd0540
PS
2273 readstring_accel($path, $mimetype, !$dontdie);
2274 } else {
2275 readfile_accel($path, $mimetype, !$dontdie);
f1e0649c 2276 }
2277 }
2278 }
b379f7d9 2279 if ($dontdie) {
2280 return;
2281 }
f1e0649c 2282 die; //no more chars to output!!!
2283}
2284
172dd12c 2285/**
2286 * Handles the sending of file data to the user's browser, including support for
2287 * byteranges etc.
ba21c9d4 2288 *
796495fe
DM
2289 * The $options parameter supports the following keys:
2290 * (string|null) preview - send the preview of the file (e.g. "thumb" for a thumbnail)
2291 * (string|null) filename - overrides the implicit filename
2292 * (bool) dontdie - return control to caller afterwards. this is not recommended and only used for cleanup tasks.
2293 * if this is passed as true, ignore_user_abort is called. if you don't want your processing to continue on cancel,
2294 * you must detect this case when control is returned using connection_aborted. Please not that session is closed
2295 * and should not be reopened.
2296 *
d2b7803e 2297 * @category files
d2b7803e 2298 * @param stored_file $stored_file local file object
172dd12c 2299 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
2300 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
2301 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
796495fe
DM
2302 * @param array $options additional options affecting the file serving
2303 * @return null script execution stopped unless $options['dontdie'] is true
172dd12c 2304 */
796495fe 2305function send_stored_file($stored_file, $lifetime=86400 , $filter=0, $forcedownload=false, array $options=array()) {
d5dd0540 2306 global $CFG, $COURSE;
172dd12c 2307
796495fe
DM
2308 if (empty($options['filename'])) {
2309 $filename = null;
2310 } else {
2311 $filename = $options['filename'];
2312 }
2313
2314 if (empty($options['dontdie'])) {
2315 $dontdie = false;
2316 } else {
2317 $dontdie = true;
2318 }
2319
82c224ee
DM
2320 if (!empty($options['preview'])) {
2321 // replace the file with its preview
2322 $fs = get_file_storage();
77a43933
DM
2323 $preview_file = $fs->get_file_preview($stored_file, $options['preview']);
2324 if (!$preview_file) {
2325 // unable to create a preview of the file, send its default mime icon instead
2326 if ($options['preview'] === 'tinyicon') {
2327 $size = 24;
2328 } else if ($options['preview'] === 'thumb') {
2329 $size = 90;
2330 } else {
2331 $size = 256;
2332 }
2333 $fileicon = file_file_icon($stored_file, $size);
2334 send_file($CFG->dirroot.'/pix/'.$fileicon.'.png', basename($fileicon).'.png');
82c224ee
DM
2335 } else {
2336 // preview images have fixed cache lifetime and they ignore forced download
2337 // (they are generated by GD and therefore they are considered reasonably safe).
77a43933 2338 $stored_file = $preview_file;
82c224ee
DM
2339 $lifetime = DAYSECS;
2340 $filter = 0;
2341 $forcedownload = false;
2342 }
2343 }
2344
67233725 2345 // handle external resource
9001c724 2346 if ($stored_file && $stored_file->is_external_file() && !isset($options['sendcachedexternalfile'])) {
67233725
DC
2347 $stored_file->send_file($lifetime, $filter, $forcedownload, $options);
2348 die;
2349 }
2350
2f657978
PS
2351 if (!$stored_file or $stored_file->is_directory()) {
2352 // nothing to serve
2353 if ($dontdie) {
2354 return;
2355 }
2356 die;
2357 }
2358
b379f7d9 2359 if ($dontdie) {
15325f55 2360 ignore_user_abort(true);
b379f7d9 2361 }
2362
56949c17 2363 session_get_instance()->write_close(); // unlock session during fileserving
172dd12c 2364
2365 // Use given MIME type if specified, otherwise guess it using mimeinfo.
2366 // IE, Konqueror and Opera open html file directly in browser from web even when directed to save it to disk :-O
2367 // only Firefox saves all files locally before opening when content-disposition: attachment stated
2368 $filename = is_null($filename) ? $stored_file->get_filename() : $filename;
2369 $isFF = check_browser_version('Firefox', '1.5'); // only FF > 1.5 properly tested
2370 $mimetype = ($forcedownload and !$isFF) ? 'application/x-forcedownload' :
2371 ($stored_file->get_mimetype() ? $stored_file->get_mimetype() : mimeinfo('type', $filename));
b5bbeaf0 2372
172dd12c 2373 // if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup
2374 if (check_browser_version('MSIE')) {
2375 $filename = rawurlencode($filename);
2376 }
2377
2378 if ($forcedownload) {
cbad562e 2379 header('Content-Disposition: attachment; filename="'.$filename.'"');
172dd12c 2380 } else {
cbad562e 2381 header('Content-Disposition: inline; filename="'.$filename.'"');
172dd12c 2382 }
2383
2384 if ($lifetime > 0) {
cbad562e
PS
2385 header('Cache-Control: max-age='.$lifetime);
2386 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
2387 header('Pragma: ');
172dd12c 2388
172dd12c 2389 } else { // Do not cache files in proxies and browsers
2390 if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
cbad562e
PS
2391 header('Cache-Control: max-age=10');
2392 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
2393 header('Pragma: ');
172dd12c 2394 } else { //normal http - prevent caching at all cost
cbad562e
PS
2395 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
2396 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
2397 header('Pragma: no-cache');
172dd12c 2398 }
172dd12c 2399 }
2400
2401 if (empty($filter)) {
cbad562e 2402 // send the contents
d5dd0540 2403 readfile_accel($stored_file, $mimetype, !$dontdie);
172dd12c 2404
2405 } else { // Try to put the file through filters
2406 if ($mimetype == 'text/html') {
365a5941 2407 $options = new stdClass();
172dd12c 2408 $options->noclean = true;
2409 $options->nocache = true; // temporary workaround for MDL-5136
2410 $text = $stored_file->get_content();
2411 $text = file_modify_html_header($text);
2412 $output = format_text($text, FORMAT_HTML, $options, $COURSE->id);
172dd12c 2413
d5dd0540 2414 readstring_accel($output, $mimetype, false);
c4cef1fe 2415
172dd12c 2416 } else if (($mimetype == 'text/plain') and ($filter == 1)) {
c4cef1fe 2417 // only filter text if filter all files is selected
365a5941 2418 $options = new stdClass();
172dd12c 2419 $options->newlines = false;
2420 $options->noclean = true;
2421 $text = $stored_file->get_content();
2422 $output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $COURSE->id) .'</pre>';
172dd12c 2423
d5dd0540 2424 readstring_accel($output, $mimetype, false);
c4cef1fe 2425
172dd12c 2426 } else { // Just send it out raw
d5dd0540 2427 readfile_accel($stored_file, $mimetype, !$dontdie);
172dd12c 2428 }
2429 }
b379f7d9 2430 if ($dontdie) {
2431 return;
2432 }
172dd12c 2433 die; //no more chars to output!!!
2434}
2435
ba21c9d4 2436/**
34ff25a6 2437 * Retrieves an array of records from a CSV file and places
ba21c9d4 2438 * them into a given table structure
2439 *
d2b7803e
DC
2440 * @global stdClass $CFG
2441 * @global moodle_database $DB
ba21c9d4 2442 * @param string $file The path to a CSV file
2443 * @param string $table The table to retrieve columns from
2444 * @return bool|array Returns an array of CSV records or false
2445 */
a43b5308 2446function get_records_csv($file, $table) {
f33e1ed4 2447 global $CFG, $DB;
599f38f9 2448
f33e1ed4 2449 if (!$metacolumns = $DB->get_columns($table)) {
599f38f9 2450 return false;
2451 }
2452
a77b98eb 2453 if(!($handle = @fopen($file, 'r'))) {
5a2a5331 2454 print_error('get_records_csv failed to open '.$file);
599f38f9 2455 }
2456
2457 $fieldnames = fgetcsv($handle, 4096);
2458 if(empty($fieldnames)) {
2459 fclose($handle);
2460 return false;
2461 }
2462
2463 $columns = array();
2464
2465 foreach($metacolumns as $metacolumn) {
2466 $ord = array_search($metacolumn->name, $fieldnames);
2467 if(is_int($ord)) {
2468 $columns[$metacolumn->name] = $ord;
2469 }
2470 }
2471
2472 $rows = array();
2473
2474 while (($data = fgetcsv($handle, 4096)) !== false) {
2475 $item = new stdClass;
2476 foreach($columns as $name => $ord) {
2477 $item->$name = $data[$ord];
2478 }
2479 $rows[] = $item;
2480 }
2481
2482 fclose($handle);
2483 return $rows;
2484}
2485
ba21c9d4 2486/**
d2b7803e 2487 * Create a file with CSV contents
117bd748 2488 *
d2b7803e
DC
2489 * @global stdClass $CFG
2490 * @global moodle_database $DB
ba21c9d4 2491 * @param string $file The file to put the CSV content into
2492 * @param array $records An array of records to write to a CSV file
2493 * @param string $table The table to get columns from
2494 * @return bool success
2495 */
a77b98eb 2496function put_records_csv($file, $records, $table = NULL) {
f33e1ed4 2497 global $CFG, $DB;
a77b98eb 2498
a1e93da2 2499 if (empty($records)) {
a77b98eb 2500 return true;
2501 }
2502
2503 $metacolumns = NULL;
f33e1ed4 2504 if ($table !== NULL && !$metacolumns = $DB->get_columns($table)) {
a77b98eb 2505 return false;
2506 }
2507
a1e93da2 2508 echo "x";
2509
7aa06e6d 2510 if(!($fp = @fopen($CFG->tempdir.'/'.$file, 'w'))) {
5a2a5331 2511 print_error('put_records_csv failed to open '.$file);
a77b98eb 2512 }
2513
a43b5308 2514 $proto = reset($records);
2515 if(is_object($proto)) {
2516 $fields_records = array_keys(get_object_vars($proto));
2517 }
2518 else if(is_array($proto)) {
2519 $fields_records = array_keys($proto);
2520 }
2521 else {
2522 return false;
2523 }
a1e93da2 2524 echo "x";
a77b98eb 2525
2526 if(!empty($metacolumns)) {
2527 $fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
2528 $fields = array_intersect($fields_records, $fields_table);
2529 }
2530 else {
2531 $fields = $fields_records;
2532 }
2533
2534 fwrite($fp, implode(',', $fields));
2535 fwrite($fp, "\r\n");
2536
2537 foreach($records as $record) {
a43b5308 2538 $array = (array)$record;
a77b98eb 2539 $values = array();
2540 foreach($fields as $field) {
a43b5308 2541 if(strpos($array[$field], ',')) {
2542 $values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
a77b98eb 2543 }
2544 else {
a43b5308 2545 $values[] = $array[$field];
a77b98eb 2546 }
2547 }
2548 fwrite($fp, implode(',', $values)."\r\n");
2549 }
2550
2551 fclose($fp);
2552 return true;
2553}
2554
f401cc97 2555
34763a79 2556/**
76ca1ff1 2557 * Recursively delete the file or folder with path $location. That is,
34763a79 2558 * if it is a file delete it. If it is a folder, delete all its content
db3a0b34 2559 * then delete it. If $location does not exist to start, that is not
2560 * considered an error.
76ca1ff1 2561 *
eab8ed9f 2562 * @param string $location the path to remove.
ba21c9d4 2563 * @return bool
34763a79 2564 */
4c8c65ec 2565function fulldelete($location) {
1e06adb4
PS
2566 if (empty($location)) {
2567 // extra safety against wrong param
2568 return false;
2569 }
f401cc97 2570 if (is_dir($location)) {
80c27aab
CF
2571 if (!$currdir = opendir($location)) {
2572 return false;
2573 }
f401cc97 2574 while (false !== ($file = readdir($currdir))) {
2575 if ($file <> ".." && $file <> ".") {
2576 $fullfile = $location."/".$file;
4c8c65ec 2577 if (is_dir($fullfile)) {
f401cc97 2578 if (!fulldelete($fullfile)) {
2579 return false;
2580 }
2581 } else {
2582 if (!unlink($fullfile)) {
2583 return false;
2584 }
4c8c65ec 2585 }
f401cc97 2586 }
4c8c65ec 2587 }
f401cc97 2588 closedir($currdir);
2589 if (! rmdir($location)) {
2590 return false;
2591 }
2592
34763a79 2593 } else if (file_exists($location)) {
f401cc97 2594 if (!unlink($location)) {
2595 return false;
2596 }
2597 }
2598 return true;
2599}
2600
4c8c65ec 2601/**
2602 * Send requested byterange of file.
ba21c9d4 2603 *
d2b7803e 2604 * @param resource $handle A file handle
ba21c9d4 2605 * @param string $mimetype The mimetype for the output
2606 * @param array $ranges An array of ranges to send
2607 * @param string $filesize The size of the content if only one range is used
4c8c65ec 2608 */
172dd12c 2609function byteserving_send_file($handle, $mimetype, $ranges, $filesize) {
d5dd0540
PS
2610 // better turn off any kind of compression and buffering
2611 @ini_set('zlib.output_compression', 'Off');
2612
c5338156
DP
2613 // Remove Etag because is is not strictly defined for byteserving,
2614 // is it tag of this range or whole file?
2615 header_remove('Etag');
2616
4c8c65ec 2617 $chunksize = 1*(1024*1024); // 1MB chunks - must be less than 2MB!
4c8c65ec 2618 if ($handle === false) {
2619 die;
2620 }
2621 if (count($ranges) == 1) { //only one range requested
2622 $length = $ranges[0][2] - $ranges[0][1] + 1;
cbad562e
PS
2623 header('HTTP/1.1 206 Partial content');
2624 header('Content-Length: '.$length);
2625 header('Content-Range: bytes '.$ranges[0][1].'-'.$ranges[0][2].'/'.$filesize);
2626 header('Content-Type: '.$mimetype);
c4cef1fe 2627
d5dd0540
PS
2628 while(@ob_get_level()) {
2629 if (!@ob_end_flush()) {
2630 break;
2631 }
2632 }
c4cef1fe 2633
4c8c65ec 2634 fseek($handle, $ranges[0][1]);
2635 while (!feof($handle) && $length > 0) {
68913aec 2636 @set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk
4c8c65ec 2637 $buffer = fread($handle, ($chunksize < $length ? $chunksize : $length));
2638 echo $buffer;
2639 flush();
2640 $length -= strlen($buffer);
2641 }
2642 fclose($handle);
2643 die;
2644 } else { // multiple ranges requested - not tested much
2645 $totallength = 0;
2646 foreach($ranges as $range) {
aba588a7 2647 $totallength += strlen($range[0]) + $range[2] - $range[1] + 1;
4c8c65ec 2648 }
aba588a7 2649 $totallength += strlen("\r\n--".BYTESERVING_BOUNDARY."--\r\n");
cbad562e
PS
2650 header('HTTP/1.1 206 Partial content');
2651 header('Content-Length: '.$totallength);
2652 header('Content-Type: multipart/byteranges; boundary='.BYTESERVING_BOUNDARY);
c4cef1fe 2653
d5dd0540
PS
2654 while(@ob_get_level()) {
2655 if (!@ob_end_flush()) {
2656 break;
2657 }
2658 }
c4cef1fe 2659
4c8c65ec 2660 foreach($ranges as $range) {
2661 $length = $range[2] - $range[1] + 1;
2662 echo $range[0];
4c8c65ec 2663 fseek($handle, $range[1]);
2664 while (!feof($handle) && $length > 0) {
68913aec 2665 @set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk
4c8c65ec 2666 $buffer = fread($handle, ($chunksize < $length ? $chunksize : $length));
2667 echo $buffer;
2668 flush();
2669 $length -= strlen($buffer);
2670 }
2671 }
2672 echo "\r\n--".BYTESERVING_BOUNDARY."--\r\n";
2673 fclose($handle);
2674 die;
2675 }
2676}
f401cc97 2677
3ace5ee4 2678/**
2679 * add includes (js and css) into uploaded files
2680 * before returning them, useful for themes and utf.js includes
ba21c9d4 2681 *
d2b7803e 2682 * @global stdClass $CFG
ba21c9d4 2683 * @param string $text text to search and replace
2684 * @return string text with added head includes
d2b7803e 2685 * @todo MDL-21120
3ace5ee4 2686 */
2687function file_modify_html_header($text) {
2688 // first look for <head> tag
2689 global $CFG;
76ca1ff1 2690
3ace5ee4 2691 $stylesheetshtml = '';
78946b9b
PS
2692/* foreach ($CFG->stylesheets as $stylesheet) {
2693 //TODO: MDL-21120
3ace5ee4 2694 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
78946b9b 2695 }*/
76ca1ff1 2696
5b8fa09b 2697 $ufo = '';
2698 if (filter_is_enabled('filter/mediaplugin')) {
76ca1ff1 2699 // this script is needed by most media filter plugins.
f2ab0d06 2700 $attributes = array('type'=>'text/javascript', 'src'=>$CFG->httpswwwroot . '/lib/ufo.js');
26acc814 2701 $ufo = html_writer::tag('script', '', $attributes) . "\n";
3ace5ee4 2702 }
76ca1ff1 2703
3ace5ee4 2704 preg_match('/\<head\>|\<HEAD\>/', $text, $matches);
2705 if ($matches) {
2706 $replacement = '<head>'.$ufo.$stylesheetshtml;
2707 $text = preg_replace('/\<head\>|\<HEAD\>/', $replacement, $text, 1);
76ca1ff1 2708 return $text;
3ace5ee4 2709 }
76ca1ff1 2710
3ace5ee4 2711 // if not, look for <html> tag, and stick <head> right after
2712 preg_match('/\<html\>|\<HTML\>/', $text, $matches);
2713 if ($matches) {
2714 // replace <html> tag with <html><head>includes</head>
13534ef7 2715 $replacement = '<html>'."\n".'<head>'.$ufo.$stylesheetshtml.'</head>';
3ace5ee4 2716 $text = preg_replace('/\<html\>|\<HTML\>/', $replacement, $text, 1);
76ca1ff1 2717 return $text;
3ace5ee4 2718 }
76ca1ff1 2719
3ace5ee4 2720 // if not, look for <body> tag, and stick <head> before body
2721 preg_match('/\<body\>|\<BODY\>/', $text, $matches);
2722 if ($matches) {
13534ef7 2723 $replacement = '<head>'.$ufo.$stylesheetshtml.'</head>'."\n".'<body>';
3ace5ee4 2724 $text = preg_replace('/\<body\>|\<BODY\>/', $replacement, $text, 1);
76ca1ff1 2725 return $text;
2726 }
2727
3ace5ee4 2728 // if not, just stick a <head> tag at the beginning
2729 $text = '<head>'.$ufo.$stylesheetshtml.'</head>'."\n".$text;
2730 return $text;
2731}
2732
bb2c046d 2733/**
2734 * RESTful cURL class
2735 *
2736 * This is a wrapper class for curl, it is quite easy to use:
ba21c9d4 2737 * <code>
bb2c046d 2738 * $c = new curl;
2739 * // enable cache
2740 * $c = new curl(array('cache'=>true));
2741 * // enable cookie
2742 * $c = new curl(array('cookie'=>true));
2743 * // enable proxy
2744 * $c = new curl(array('proxy'=>true));
2745 *
2746 * // HTTP GET Method
2747 * $html = $c->get('http://example.com');
2748 * // HTTP POST Method
2749 * $html = $c->post('http://example.com/', array('q'=>'words', 'name'=>'moodle'));
2750 * // HTTP PUT Method
2751 * $html = $c->put('http://example.com/', array('file'=>'/var/www/test.txt');
ba21c9d4 2752 * </code>
bb2c046d 2753 *
d2b7803e
DC
2754 * @package core_files
2755 * @category files
2756 * @copyright Dongsheng Cai <dongsheng@moodle.com>
2757 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
bb2c046d 2758 */
bb2c046d 2759class curl {
d2b7803e 2760 /** @var bool Caches http request contents */
bb2c046d 2761 public $cache = false;
d2b7803e 2762 /** @var bool Uses proxy */
bb2c046d 2763 public $proxy = false;
d2b7803e 2764 /** @var string library version */
bb2c046d 2765 public $version = '0.4 dev';
d2b7803e 2766 /** @var array http's response */
bb2c046d 2767 public $response = array();
d2b7803e 2768 /** @var array http header */
bb2c046d 2769 public $header = array();
d2b7803e 2770 /** @var string cURL information */
bb2c046d 2771 public $info;
d2b7803e 2772 /** @var string error */
bb2c046d 2773 public $error;
2e0b5fef
MG
2774 /** @var int error code */
2775 public $errno;
117bd748 2776
d2b7803e 2777 /** @var array cURL options */
bb2c046d 2778 private $options;
d2b7803e 2779 /** @var string Proxy host */
bb2c046d 2780 private $proxy_host = '';
d2b7803e 2781 /** @var string Proxy auth */
bb2c046d 2782 private $proxy_auth = '';
d2b7803e 2783 /** @var string Proxy type */
bb2c046d 2784 private $proxy_type = '';
d2b7803e 2785 /** @var bool Debug mode on */
bb2c046d 2786 private $debug = false;
d2b7803e 2787 /** @var bool|string Path to cookie file */
bb2c046d 2788 private $cookie = false;
2789
ba21c9d4 2790 /**
d2b7803e
DC
2791 * Constructor
2792 *
2793 * @global stdClass $CFG
ba21c9d4 2794 * @param array $options
2795 */
bb2c046d 2796 public function __construct($options = array()){
2797 global $CFG;
2798 if (!function_exists('curl_init')) {
2799 $this->error = 'cURL module must be enabled!';
2800 trigger_error($this->error, E_USER_ERROR);
2801 return false;
2802 }
2803 // the options of curl should be init here.
2804 $this->resetopt();
2805 if (!empty($options['debug'])) {
2806 $this->debug = true;
2807 }
2808 if(!empty($options['cookie'])) {
2809 if($options['cookie'] === true) {
2810 $this->cookie = $CFG->dataroot.'/curl_cookie.txt';
2811 } else {
2812 $this->cookie = $options['cookie'];
2813 }
2814 }
2815 if (!empty($options['cache'])) {
2816 if (class_exists('curl_cache')) {
5430f05b 2817 if (!empty($options['module_cache'])) {
2818 $this->cache = new curl_cache($options['module_cache']);
2819 } else {
2820 $this->cache = new curl_cache('misc');
2821 }
bb2c046d 2822 }
2823 }
d04ce87f 2824 if (!empty($CFG->proxyhost)) {
2825 if (empty($CFG->proxyport)) {
2826 $this->proxy_host = $CFG->proxyhost;
2827 } else {
2828 $this->proxy_host = $CFG->proxyhost.':'.$CFG->proxyport;
2829 }
2830 if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
2831 $this->proxy_auth = $CFG->proxyuser.':'.$CFG->proxypassword;
2832 $this->setopt(array(
2833 'proxyauth'=> CURLAUTH_BASIC | CURLAUTH_NTLM,
2834 'proxyuserpwd'=>$this->proxy_auth));
2835 }
2836 if (!empty($CFG->proxytype)) {
2837 if ($CFG->proxytype == 'SOCKS5') {
2838 $this->proxy_type = CURLPROXY_SOCKS5;
bb2c046d 2839 } else {
d04ce87f 2840 $this->proxy_type = CURLPROXY_HTTP;
f6d3e2c4 2841 $this->setopt(array('httpproxytunnel'=>false));
bb2c046d 2842 }
d04ce87f 2843 $this->setopt(array('proxytype'=>$this->proxy_type));
bb2c046d 2844 }
d04ce87f 2845 }
2846 if (!empty($this->proxy_host)) {
2847 $this->proxy = array('proxy'=>$this->proxy_host);
bb2c046d 2848 }
2849 }
ba21c9d4 2850 /**
2851 * Resets the CURL options that have already been set
2852 */
bb2c046d 2853 public function resetopt(){
2854 $this->options = array();
2855 $this->options['CURLOPT_USERAGENT'] = 'MoodleBot/1.0';
2856 // True to include the header in the output
2857 $this->options['CURLOPT_HEADER'] = 0;
2858 // True to Exclude the body from the output
2859 $this->options['CURLOPT_NOBODY'] = 0;
2860 // TRUE to follow any "Location: " header that the server
2861 // sends as part of the HTTP header (note this is recursive,
2862 // PHP will follow as many "Location: " headers that it is sent,
2863 // unless CURLOPT_MAXREDIRS is set).
99eaca9d 2864 //$this->options['CURLOPT_FOLLOWLOCATION'] = 1;
bb2c046d 2865 $this->options['CURLOPT_MAXREDIRS'] = 10;
2866 $this->options['CURLOPT_ENCODING'] = '';
2867 // TRUE to return the transfer as a string of the return
2868 // value of curl_exec() instead of outputting it out directly.
2869 $this->options['CURLOPT_RETURNTRANSFER'] = 1;
2870 $this->options['CURLOPT_BINARYTRANSFER'] = 0;
2871 $this->options['CURLOPT_SSL_VERIFYPEER'] = 0;
2872 $this->options['CURLOPT_SSL_VERIFYHOST'] = 2;
6135bd45 2873 $this->options['CURLOPT_CONNECTTIMEOUT'] = 30;
bb2c046d 2874 }
2875
2876 /**
2877 * Reset Cookie
bb2c046d 2878 */
2879 public function resetcookie() {
2880 if (!empty($this->cookie)) {
2881 if (is_file($this->cookie)) {
2882 $fp = fopen($this->cookie, 'w');
2883 if (!empty($fp)) {
2884 fwrite($fp, '');
2885 fclose($fp);
2886 }
2887 }
2888 }
2889 }
2890
2891 /**
2892 * Set curl options
2893 *
2894 * @param array $options If array is null, this function will
2895 * reset the options to default value.
bb2c046d 2896 */
2897 public function setopt($options = array()) {
2898 if (is_array($options)) {
2899 foreach($options as $name => $val){
2900 if (stripos($name, 'CURLOPT_') === false) {
2901 $name = strtoupper('CURLOPT_'.$name);
2902 }
2903 $this->options[$name] = $val;
2904 }
2905 }
2906 }
d2b7803e 2907
bb2c046d 2908 /**
2909 * Reset http method
bb2c046d 2910 */
2911 public function cleanopt(){
2912 unset($this->options['CURLOPT_HTTPGET']);
2913 unset($this->options['CURLOPT_POST']);
2914 unset($this->options['CURLOPT_POSTFIELDS']);
2915 unset($this->options['CURLOPT_PUT']);
2916 unset($this->options['CURLOPT_INFILE']);
2917 unset($this->options['CURLOPT_INFILESIZE']);
2918 unset($this->options['CURLOPT_CUSTOMREQUEST']);
2e0b5fef 2919 unset($this->options['CURLOPT_FILE']);
bb2c046d 2920 }
2921
a6b0ac53
MG
2922 /**
2923 * Resets the HTTP Request headers (to prepare for the new request)
2924 */
2925 public function resetHeader() {
2926 $this->header = array();
2927 }
2928
bb2c046d 2929 /**
2930 * Set HTTP Request Header
2931 *
d2b7803e 2932 * @param array $header
bb2c046d 2933 */
2934 public function setHeader($header) {
2935 if (is_array($header)){
2936 foreach ($header as $v) {
2937 $this->setHeader($v);
2938 }
2939 } else {
2940 $this->header[] = $header;
2941 }
2942 }
d2b7803e 2943
bb2c046d 2944 /**
2945 * Set HTTP Response Header
2946 *
2947 */
2948 public function getResponse(){
2949 return $this->response;
2950 }
d2b7803e 2951
bb2c046d 2952 /**
2953 * private callback function
2954 * Formatting HTTP Response Header
2955 *
d2b7803e 2956 * @param resource $ch Apparently not used
ba21c9d4 2957 * @param string $header
2958 * @return int The strlen of the header
bb2c046d 2959 */
2960 private function formatHeader($ch, $header)
2961 {
2962 $this->count++;
2963 if (strlen($header) > 2) {
2964 list($key, $value) = explode(" ", rtrim($header, "\r\n"), 2);
2965 $key = rtrim($key, ':');
2966 if (!empty($this->response[$key])) {
2967 if (is_array($this->response[$key])){
2968 $this->response[$key][] = $value;
2969 } else {
2970 $tmp = $this->response[$key];
2971 $this->response[$key] = array();
2972 $this->response[$key][] = $tmp;
2973 $this->response[$key][] = $value;
2974
2975 }
2976 } else {
2977 $this->response[$key] = $value;
2978 }
2979 }
2980 return strlen($header);
2981 }
2982
2983 /**
2984 * Set options for individual curl instance
ba21c9d4 2985 *
d2b7803e 2986 * @param resource $curl A curl handle
ba21c9d4 2987 * @param array $options
d2b7803e 2988 * @return resource The curl handle
bb2c046d 2989 */
2990 private function apply_opt($curl, $options) {
2991 // Clean up
2992 $this->cleanopt();
2993 // set cookie
2994 if (!empty($this->cookie) || !empty($options['cookie'])) {
2995 $this->setopt(array('cookiejar'=>$this->cookie,
2996 'cookiefile'=>$this->cookie
2997 ));
2998 }
2999
3000 // set proxy
3001 if (!empty($this->proxy) || !empty($options['proxy'])) {
3002 $this->setopt($this->proxy);
3003 }
3004 $this->setopt($options);
3005 // reset before set options
3006 curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader'));
3007 // set headers
3008 if (empty($this->header)){
3009 $this->setHeader(array(
3010 'User-Agent: MoodleBot/1.0',
3011 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
3012 'Connection: keep-alive'
3013 ));
3014 }
3015 curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
3016
29c73325 3017 // Bypass proxy (for this request only) if required.
3018 if (!empty($this->options['CURLOPT_URL']) &&
3019 is_proxybypass($this->options['CURLOPT_URL'])) {
3020 unset($this->options['CURLOPT_PROXY']);
3021 }
3022
bb2c046d 3023 if ($this->debug){
3024 echo '<h1>Options</h1>';
3025 var_dump($this->options);
3026 echo '<h1>Header</h1>';
3027 var_dump($this->header);
3028 }
3029
3030 // set options
3031 foreach($this->options as $name => $val) {
3032 if (is_string($name)) {
3033 $name = constant(strtoupper($name));
3034 }
3035 curl_setopt($curl, $name, $val);
3036 }
3037 return $curl;
3038 }
d2b7803e 3039
ba21c9d4 3040 /**
bb2c046d 3041 * Download multiple files in parallel
ba21c9d4 3042 *
3043 * Calls {@link multi()} with specific download headers
3044 *
3045 * <code>
3bcca355
JH
3046 * $c = new curl();
3047 * $file1 = fopen('a', 'wb');
3048 * $file2 = fopen('b', 'wb');
bb2c046d 3049 * $c->download(array(
3bcca355
JH
3050 * array('url'=>'http://localhost/', 'file'=>$file1),
3051 * array('url'=>'http://localhost/20/', 'file'=>$file2)
3052 * ));
3053 * fclose($file1);
3054 * fclose($file2);
3055 * </code>
3056 *
3057 * or
3058 *
3059 * <code>
3060 * $c = new curl();
3061 * $c->download(array(
3062 * array('url'=>'http://localhost/', 'filepath'=>'/tmp/file1.tmp'),
3063 * array('url'=>'http://localhost/20/', 'filepath'=>'/tmp/file2.tmp')
bb2c046d 3064 * ));
ba21c9d4 3065 * </code>
3066 *
3bcca355
JH
3067 * @param array $requests An array of files to request {
3068 * url => url to download the file [required]
3069 * file => file handler, or
3070 * filepath => file path
3071 * }
3072 * If 'file' and 'filepath' parameters are both specified in one request, the
3073 * open file handle in the 'file' parameter will take precedence and 'filepath'
3074 * will be ignored.
3075 *
ba21c9d4 3076 * @param array $options An array of options to set
3077 * @return array An array of results
bb2c046d 3078 */
3079 public function download($requests, $options = array()) {
3080 $options['CURLOPT_BINARYTRANSFER'] = 1;
3081 $options['RETURNTRANSFER'] = false;
3082 return $this->multi($requests, $options);
3083 }
d2b7803e
DC
3084
3085 /**
bb2c046d 3086 * Mulit HTTP Requests
3087 * This function could run multi-requests in parallel.
ba21c9d4 3088 *
3089 * @param array $requests An array of files to request
3090 * @param array $options An array of options to set
3091 * @return array An array of results
bb2c046d 3092 */
3093 protected function multi($requests, $options = array()) {
3094 $count = count($requests);
3095 $handles = array();
3096 $results = array();
3097 $main = curl_multi_init();
3098 for ($i = 0; $i < $count; $i++) {
3bcca355
JH
3099 if (!empty($requests[$i]['filepath']) and empty($requests[$i]['file'])) {
3100 // open file
3101 $requests[$i]['file'] = fopen($requests[$i]['filepath'], 'w');
3102 $requests[$i]['auto-handle'] = true;
3103 }
3104 foreach($requests[$i] as $n=>$v){
3105 $options[$n] = $v;
bb2c046d 3106 }
3bcca355 3107 $handles[$i] = curl_init($requests[$i]['url']);
bb2c046d 3108 $this->apply_opt($handles[$i], $options);
3109 curl_multi_add_handle($main, $handles[$i]);
3110 }
3111 $running = 0;
3112 do {
3113 curl_multi_exec($main, $running);
3114 } while($running > 0);
3115 for ($i = 0; $i < $count; $i++) {
ba4082f5 3116 if (!empty($options['CURLOPT_RETURNTRANSFER'])) {
bb2c046d 3117 $results[] = true;
3118 } else {
3119 $results[] = curl_multi_getcontent($handles[$i]);
3120 }
3121 curl_multi_remove_handle($main, $handles[$i]);
3122 }
3123 curl_multi_close($main);
3bcca355
JH
3124
3125 for ($i = 0; $i < $count; $i++) {
3126 if (!empty($requests[$i]['filepath']) and !empty($requests[$i]['auto-handle'])) {
3127 // close file handler if file is opened in this function
3128 fclose($requests[$i]['file']);
3129 }
3130 }
bb2c046d 3131 return $results;
3132 }
d2b7803e 3133
bb2c046d 3134 /**
3135 * Single HTTP Request
ba21c9d4 3136 *
3137 * @param string $url The URL to request
3138 * @param array $options
3139 * @return bool
bb2c046d 3140 */
3141 protected function request($url, $options = array()){
3142 // create curl instance
3143 $curl = curl_init($url);
3144 $options['url'] = $url;
3145 $this->apply_opt($curl, $options);
3146 if ($this->cache && $ret = $this->cache->get($this->options)) {
3147 return $ret;
3148 } else {
6135bd45 3149 $ret = curl_exec($curl);
bb2c046d 3150 if ($this->cache) {
3151 $this->cache->set($this->options, $ret);
3152 }
3153 }
3154
3155 $this->info = curl_getinfo($curl);
3156 $this->error = curl_error($curl);
2e0b5fef 3157 $this->errno = curl_errno($curl);
bb2c046d 3158
3159 if ($this->debug){
3160 echo '<h1>Return Data</h1>';
3161 var_dump($ret);
3162 echo '<h1>Info</h1>';
3163 var_dump($this->info);
3164 echo '<h1>Error</h1>';
3165 var_dump($this->error);
3166 }
3167
3168 curl_close($curl);
3169
6135bd45 3170 if (empty($this->error)){
bb2c046d 3171 return $ret;
3172 } else {
4dbb2b05 3173 return $this->error;
3174 // exception is not ajax friendly
3175 //throw new moodle_exception($this->error, 'curl');
bb2c046d 3176 }
3177 }
3178
3179 /**
3180 * HTTP HEAD method
ba21c9d4 3181 *
117bd748 3182 * @see request()
ba21c9d4 3183 *
3184 * @param string $url
3185 * @param array $options
3186 * @return bool
bb2c046d 3187 */
3188 public function head($url, $options = array()){
3189 $options['CURLOPT_HTTPGET'] = 0;
3190 $options['CURLOPT_HEADER'] = 1;
3191 $options['CURLOPT_NOBODY'] = 1;
3192 return $this->request($url, $options);
3193 }
3194
3195 /**
3196 * HTTP POST method
ba21c9d4 3197 *
3198 * @param string $url
117bd748 3199 * @param array|string $params
ba21c9d4 3200 * @param array $options
3201 * @return bool
bb2c046d 3202 */
28c58294 3203 public function post($url, $params = '', $options = array()){
bb2c046d 3204 $options['CURLOPT_POST'] = 1;
28c58294 3205 if (is_array($params)) {
3206 $this->_tmp_file_post_params = array();
3207 foreach ($params as $key => $value) {
3208 if ($value instanceof stored_file) {
3209 $value->add_to_curl_request($this, $key);
3210 } else {
3211 $this->_tmp_file_post_params[$key] = $value;
3212 }
5035a8b4 3213 }
28c58294 3214 $options['CURLOPT_POSTFIELDS'] = $this->_tmp_file_post_params;
3215 unset($this->_tmp_file_post_params);
3216 } else {
3217 // $params is the raw post data
3218 $options['CURLOPT_POSTFIELDS'] = $params;
5035a8b4 3219 }
bb2c046d 3220 return $this->request($url, $options);
3221 }
3222
3223 /**
3224 * HTTP GET method
ba21c9d4 3225 *
3226 * @param string $url
117bd748 3227 * @param array $params
ba21c9d4 3228 * @param array $options
3229 * @return bool
bb2c046d 3230 */
3231 public function get($url, $params = array(), $options = array()){
3232 $options['CURLOPT_HTTPGET'] = 1;
3233
3234 if (!empty($params)){
3235 $url .= (stripos($url, '?') !== false) ? '&' : '?';
3236 $url .= http_build_query($params, '', '&');
3237 }
3238 return $this->request($url, $options);
3239