MDL-59960 core_files: improve mimetype detection
[moodle.git] / lib / filestorage / stored_file.php
CommitLineData
16a95e8f 1<?php
16a95e8f 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/>.
16
17
18/**
8496fdac 19 * Definition of a class stored_file.
16a95e8f 20 *
d2b7803e
DC
21 * @package core_files
22 * @copyright 2008 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
16a95e8f 24 */
25
64f93798
PS
26defined('MOODLE_INTERNAL') || die();
27
71ab7e51 28require_once($CFG->dirroot . '/lib/filestorage/file_progress.php');
16a34ae1 29require_once($CFG->dirroot . '/lib/filestorage/file_system.php');
71ab7e51 30
172dd12c 31/**
8496fdac
PS
32 * Class representing local files stored in a sha1 file pool.
33 *
34 * Since Moodle 2.0 file contents are stored in sha1 pool and
35 * all other file information is stored in new "files" database table.
36 *
d2b7803e
DC
37 * @package core_files
38 * @category files
8496fdac
PS
39 * @copyright 2008 Petr Skoda {@link http://skodak.org}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 * @since Moodle 2.0
172dd12c 42 */
43class stored_file {
8496fdac 44 /** @var file_storage file storage pool instance */
172dd12c 45 private $fs;
04e3b007 46 /** @var stdClass record from the files table left join files_reference table */
172dd12c 47 private $file_record;
67233725 48 /** @var repository repository plugin instance */
291a3d1f 49 private $repository;
16a34ae1
AN
50 /** @var file_system filesystem instance */
51 private $filesystem;
172dd12c 52
c858655d 53 /**
54 * @var int Indicates a file handle of the type returned by fopen.
55 */
56 const FILE_HANDLE_FOPEN = 0;
57
58 /**
59 * @var int Indicates a file handle of the type returned by gzopen.
60 */
61 const FILE_HANDLE_GZOPEN = 1;
62
63
172dd12c 64 /**
8496fdac
PS
65 * Constructor, this constructor should be called ONLY from the file_storage class!
66 *
67 * @param file_storage $fs file storage instance
d2b7803e 68 * @param stdClass $file_record description of file
16a34ae1 69 * @param string $deprecated
172dd12c 70 */
16a34ae1 71 public function __construct(file_storage $fs, stdClass $file_record, $deprecated = null) {
67233725 72 global $DB, $CFG;
8496fdac
PS
73 $this->fs = $fs;
74 $this->file_record = clone($file_record); // prevent modifications
67233725
DC
75
76 if (!empty($file_record->repositoryid)) {
b99065be 77 require_once("$CFG->dirroot/repository/lib.php");
67233725
DC
78 $this->repository = repository::get_repository_by_id($file_record->repositoryid, SYSCONTEXTID);
79 if ($this->repository->supported_returntypes() & FILE_REFERENCE != FILE_REFERENCE) {
80 // Repository cannot do file reference.
81 throw new moodle_exception('error');
82 }
83 } else {
84 $this->repository = null;
85 }
42aa6e15 86 // make sure all reference fields exist in file_record even when it is not a reference
87355560 87 foreach (array('referencelastsync', 'referencefileid', 'reference', 'repositoryid') as $key) {
42aa6e15
MG
88 if (empty($this->file_record->$key)) {
89 $this->file_record->$key = null;
90 }
91 }
16a34ae1
AN
92
93 $this->filesystem = $fs->get_file_system();
67233725
DC
94 }
95
e927581a
JD
96 /**
97 * Magic method, called during serialization.
98 *
99 * @return array
100 */
101 public function __sleep() {
102 // We only ever want the file_record saved, not the file_storage object.
103 return ['file_record'];
104 }
105
106 /**
107 * Magic method, called during unserialization.
108 */
109 public function __wakeup() {
110 // Recreate our stored_file based on the file_record, and using file storage retrieved the correct way.
111 $this->__construct(get_file_storage(), $this->file_record);
112 }
113
67233725
DC
114 /**
115 * Whether or not this is a external resource
116 *
117 * @return bool
118 */
119 public function is_external_file() {
120 return !empty($this->repository);
121 }
122
151b0f94
DW
123 /**
124 * Whether or not this is a controlled link. Note that repositories cannot support FILE_REFERENCE and FILE_CONTROLLED_LINK.
125 *
126 * @return bool
127 */
128 public function is_controlled_link() {
129 return $this->is_external_file() && $this->repository->supported_returntypes() & FILE_CONTROLLED_LINK;
130 }
131
67233725
DC
132 /**
133 * Update some file record fields
134 * NOTE: Must remain protected
135 *
136 * @param stdClass $dataobject
137 */
138 protected function update($dataobject) {
139 global $DB;
2a68ee0e 140 $updatereferencesneeded = false;
67233725 141 $keys = array_keys((array)$this->file_record);
023037dc 142 $filepreupdate = clone($this->file_record);
67233725
DC
143 foreach ($dataobject as $field => $value) {
144 if (in_array($field, $keys)) {
145 if ($field == 'contextid' and (!is_number($value) or $value < 1)) {
146 throw new file_exception('storedfileproblem', 'Invalid contextid');
147 }
148
149 if ($field == 'component') {
150 $value = clean_param($value, PARAM_COMPONENT);
151 if (empty($value)) {
152 throw new file_exception('storedfileproblem', 'Invalid component');
153 }
154 }
155
156 if ($field == 'filearea') {
157 $value = clean_param($value, PARAM_AREA);
158 if (empty($value)) {
159 throw new file_exception('storedfileproblem', 'Invalid filearea');
160 }
161 }
162
163 if ($field == 'itemid' and (!is_number($value) or $value < 0)) {
164 throw new file_exception('storedfileproblem', 'Invalid itemid');
165 }
166
167
168 if ($field == 'filepath') {
169 $value = clean_param($value, PARAM_PATH);
170 if (strpos($value, '/') !== 0 or strrpos($value, '/') !== strlen($value)-1) {
171 // path must start and end with '/'
172 throw new file_exception('storedfileproblem', 'Invalid file path');
173 }
174 }
175
176 if ($field == 'filename') {
fc4e8034
DC
177 // folder has filename == '.', so we pass this
178 if ($value != '.') {
179 $value = clean_param($value, PARAM_FILE);
180 }
67233725
DC
181 if ($value === '') {
182 throw new file_exception('storedfileproblem', 'Invalid file name');
183 }
184 }
185
186 if ($field === 'timecreated' or $field === 'timemodified') {
187 if (!is_number($value)) {
188 throw new file_exception('storedfileproblem', 'Invalid timestamp');
189 }
190 if ($value < 0) {
191 $value = 0;
192 }
193 }
194
42aa6e15 195 if ($field === 'referencefileid') {
1c0da849
DM
196 if (!is_null($value) and !is_number($value)) {
197 throw new file_exception('storedfileproblem', 'Invalid reference info');
198 }
67233725
DC
199 }
200
2a68ee0e
MG
201 if (($field == 'contenthash' || $field == 'filesize') && $this->file_record->$field != $value) {
202 $updatereferencesneeded = true;
203 }
204
67233725
DC
205 // adding the field
206 $this->file_record->$field = $value;
207 } else {
208 throw new coding_exception("Invalid field name, $field doesn't exist in file record");
209 }
210 }
8177b7b9 211 // Validate mimetype field
16a34ae1 212 $mimetype = $this->filesystem->mimetype_from_storedfile($this);
8177b7b9
DC
213 $this->file_record->mimetype = $mimetype;
214
67233725 215 $DB->update_record('files', $this->file_record);
2a68ee0e
MG
216 if ($updatereferencesneeded) {
217 // Either filesize or contenthash of this file have changed. Update all files that reference to it.
218 $this->fs->update_references_to_storedfile($this);
219 }
023037dc 220
221 // Callback for file update.
222 if (!$this->is_directory()) {
223 if ($pluginsfunction = get_plugins_with_function('after_file_updated')) {
224 foreach ($pluginsfunction as $plugintype => $plugins) {
225 foreach ($plugins as $pluginfunction) {
226 $pluginfunction($this->file_record, $filepreupdate);
227 }
228 }
229 }
230 }
67233725
DC
231 }
232
233 /**
234 * Rename filename
235 *
236 * @param string $filepath file path
237 * @param string $filename file name
238 */
239 public function rename($filepath, $filename) {
7051415c 240 if ($this->fs->file_exists($this->get_contextid(), $this->get_component(), $this->get_filearea(), $this->get_itemid(), $filepath, $filename)) {
8436e859
MS
241 $a = new stdClass();
242 $a->contextid = $this->get_contextid();
243 $a->component = $this->get_component();
244 $a->filearea = $this->get_filearea();
245 $a->itemid = $this->get_itemid();
246 $a->filepath = $filepath;
247 $a->filename = $filename;
248 throw new file_exception('storedfilenotcreated', $a, 'file exists, cannot rename');
7051415c 249 }
67233725
DC
250 $filerecord = new stdClass;
251 $filerecord->filepath = $filepath;
252 $filerecord->filename = $filename;
253 // populate the pathname hash
254 $filerecord->pathnamehash = $this->fs->get_pathname_hash($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath, $filename);
255 $this->update($filerecord);
256 }
257
258 /**
e6904efc 259 * Function stored_file::replace_content_with() is deprecated. Please use stored_file::replace_file_with()
67233725 260 *
e6904efc 261 * @deprecated since Moodle 2.6 MDL-42016 - please do not use this function any more.
2a68ee0e 262 * @see stored_file::replace_file_with()
67233725
DC
263 */
264 public function replace_content_with(stored_file $storedfile) {
e6904efc
JO
265 throw new coding_exception('Function stored_file::replace_content_with() can not be used any more . ' .
266 'Please use stored_file::replace_file_with()');
67233725
DC
267 }
268
e9e32b1d
MG
269 /**
270 * Replaces the fields that might have changed when file was overriden in filepicker:
6dd92c02 271 * reference, contenthash, filesize, userid
e9e32b1d 272 *
935429af
MG
273 * Note that field 'source' must be updated separately because
274 * it has different format for draft and non-draft areas and
275 * this function will usually be used to replace non-draft area
276 * file with draft area file.
e9e32b1d
MG
277 *
278 * @param stored_file $newfile
279 * @throws coding_exception
280 */
281 public function replace_file_with(stored_file $newfile) {
282 if ($newfile->get_referencefileid() &&
283 $this->fs->get_references_count_by_storedfile($this)) {
284 // The new file is a reference.
285 // The current file has other local files referencing to it.
286 // Double reference is not allowed.
287 throw new moodle_exception('errordoublereference', 'repository');
288 }
289
290 $filerecord = new stdClass;
16a34ae1
AN
291 if ($this->filesystem->is_file_readable_remotely_by_storedfile($newfile)) {
292 $contenthash = $newfile->get_contenthash();
e9e32b1d
MG
293 $filerecord->contenthash = $contenthash;
294 } else {
295 throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
296 }
297 $filerecord->filesize = $newfile->get_filesize();
298 $filerecord->referencefileid = $newfile->get_referencefileid();
6dd92c02 299 $filerecord->userid = $newfile->get_userid();
e9e32b1d
MG
300 $this->update($filerecord);
301 }
302
67233725 303 /**
2fa8aea2 304 * Unlink the stored file from the referenced file
67233725 305 *
2fa8aea2
DM
306 * This methods destroys the link to the record in files_reference table. This effectively
307 * turns the stored file from being an alias to a plain copy. However, the caller has
308 * to make sure that the actual file's content has beed synced prior to calling this method.
67233725
DC
309 */
310 public function delete_reference() {
311 global $DB;
61506a0a 312
2fa8aea2
DM
313 if (!$this->is_external_file()) {
314 throw new coding_exception('An attempt to unlink a non-reference file.');
315 }
316
e3c02118
DC
317 $transaction = $DB->start_delegated_transaction();
318
1c0da849
DM
319 // Are we the only one referring to the original file? If so, delete the
320 // referenced file record. Note we do not use file_storage::search_references_count()
321 // here because we want to count draft files too and we are at a bit lower access level here.
322 $countlinks = $DB->count_records('files',
323 array('referencefileid' => $this->file_record->referencefileid));
324 if ($countlinks == 1) {
325 $DB->delete_records('files_reference', array('id' => $this->file_record->referencefileid));
326 }
61506a0a 327
1c0da849
DM
328 // Update the underlying record in the database.
329 $update = new stdClass();
330 $update->referencefileid = null;
1c0da849 331 $this->update($update);
61506a0a 332
e3c02118
DC
333 $transaction->allow_commit();
334
1c0da849
DM
335 // Update our properties and the record in the memory.
336 $this->repository = null;
337 $this->file_record->repositoryid = null;
338 $this->file_record->reference = null;
339 $this->file_record->referencefileid = null;
340 $this->file_record->referencelastsync = null;
172dd12c 341 }
342
343 /**
344 * Is this a directory?
8496fdac
PS
345 *
346 * Directories are only emulated, internally they are stored as empty
347 * files with a "." instead of name - this means empty directory contains
348 * exactly one empty file with name dot.
349 *
350 * @return bool true means directory, false means file
172dd12c 351 */
352 public function is_directory() {
8496fdac 353 return ($this->file_record->filename === '.');
172dd12c 354 }
355
356 /**
8496fdac
PS
357 * Delete file from files table.
358 *
359 * The content of files stored in sha1 pool is reclaimed
360 * later - the occupied disk space is reclaimed much later.
361 *
362 * @return bool always true or exception if error occurred
172dd12c 363 */
364 public function delete() {
365 global $DB;
e3c02118 366
e029dff4
PS
367 if ($this->is_directory()) {
368 // Directories can not be referenced, just delete the record.
369 $DB->delete_records('files', array('id'=>$this->file_record->id));
370
371 } else {
372 $transaction = $DB->start_delegated_transaction();
e3c02118 373
e029dff4
PS
374 // If there are other files referring to this file, convert them to copies.
375 if ($files = $this->fs->get_references_by_storedfile($this)) {
376 foreach ($files as $file) {
377 $this->fs->import_external_file($file);
378 }
67233725 379 }
3b2465fe 380
e029dff4
PS
381 // If this file is a reference (alias) to another file, unlink it first.
382 if ($this->is_external_file()) {
383 $this->delete_reference();
384 }
3b2465fe 385
e029dff4
PS
386 // Now delete the file record.
387 $DB->delete_records('files', array('id'=>$this->file_record->id));
e3c02118 388
e029dff4 389 $transaction->allow_commit();
023037dc 390
391 if (!$this->is_directory()) {
392 // Callback for file deletion.
393 if ($pluginsfunction = get_plugins_with_function('after_file_deleted')) {
394 foreach ($pluginsfunction as $plugintype => $plugins) {
395 foreach ($plugins as $pluginfunction) {
396 $pluginfunction($this->file_record);
397 }
398 }
399 }
400 }
e029dff4 401 }
e3c02118 402
e029dff4 403 // Move pool file to trash if content not needed any more.
16a34ae1 404 $this->filesystem->remove_file($this->file_record->contenthash);
8496fdac 405 return true; // BC only
172dd12c 406 }
407
5035a8b4 408 /**
409 * adds this file path to a curl request (POST only)
410 *
411 * @param curl $curlrequest the curl request object
412 * @param string $key what key to use in the POST request
8496fdac 413 * @return void
5035a8b4 414 */
415 public function add_to_curl_request(&$curlrequest, $key) {
16a34ae1 416 return $this->filesystem->add_to_curl_request($this, $curlrequest, $key);
5035a8b4 417 }
418
172dd12c 419 /**
420 * Returns file handle - read only mode, no writing allowed into pool files!
8496fdac
PS
421 *
422 * When you want to modify a file, create a new file and delete the old one.
423 *
c858655d 424 * @param int $type Type of file handle (FILE_HANDLE_xx constant)
8496fdac 425 * @return resource file handle
172dd12c 426 */
c858655d 427 public function get_content_file_handle($type = self::FILE_HANDLE_FOPEN) {
16a34ae1 428 return $this->filesystem->get_content_file_handle($this, $type);
172dd12c 429 }
430
431 /**
8496fdac 432 * Dumps file content to page.
172dd12c 433 */
434 public function readfile() {
16a34ae1 435 return $this->filesystem->readfile($this);
172dd12c 436 }
437
438 /**
8496fdac
PS
439 * Returns file content as string.
440 *
172dd12c 441 * @return string content
442 */
443 public function get_content() {
16a34ae1 444 return $this->filesystem->get_content($this);
172dd12c 445 }
446
6c0e2d08 447 /**
8496fdac
PS
448 * Copy content of file to given pathname.
449 *
450 * @param string $pathname real path to the new file
6c0e2d08 451 * @return bool success
452 */
453 public function copy_content_to($pathname) {
16a34ae1 454 return $this->filesystem->copy_content_from_storedfile($this, $pathname);
6c0e2d08 455 }
456
4e782b32
RT
457 /**
458 * Copy content of file to temporary folder and returns file path
459 *
460 * @param string $dir name of the temporary directory
461 * @param string $fileprefix prefix of temporary file.
462 * @return string|bool path of temporary file or false.
463 */
464 public function copy_content_to_temp($dir = 'files', $fileprefix = 'tempup_') {
465 $tempfile = false;
466 if (!$dir = make_temp_directory($dir)) {
467 return false;
468 }
469 if (!$tempfile = tempnam($dir, $fileprefix)) {
470 return false;
471 }
472 if (!$this->copy_content_to($tempfile)) {
473 // something went wrong
474 @unlink($tempfile);
475 return false;
476 }
477 return $tempfile;
478 }
479
17d9269f 480 /**
8496fdac
PS
481 * List contents of archive.
482 *
d2b7803e 483 * @param file_packer $packer file packer instance
c78a0558 484 * @return array of file infos
485 */
486 public function list_files(file_packer $packer) {
16a34ae1 487 return $this->filesystem->list_files($this, $packer);
c78a0558 488 }
489
490 /**
8496fdac
PS
491 * Extract file to given file path (real OS filesystem), existing files are overwritten.
492 *
d2b7803e 493 * @param file_packer $packer file packer instance
0b0bfa93 494 * @param string $pathname target directory
71ab7e51 495 * @param file_progress $progress Progress indicator callback or null if not required
8496fdac 496 * @return array|bool list of processed files; false if error
17d9269f 497 */
71ab7e51 498 public function extract_to_pathname(file_packer $packer, $pathname,
499 file_progress $progress = null) {
16a34ae1 500 return $this->filesystem->extract_to_pathname($this, $packer, $pathname, $progress);
17d9269f 501 }
502
503 /**
8496fdac
PS
504 * Extract file to given file path (real OS filesystem), existing files are overwritten.
505 *
d2b7803e
DC
506 * @param file_packer $packer file packer instance
507 * @param int $contextid context ID
508 * @param string $component component
509 * @param string $filearea file area
510 * @param int $itemid item ID
511 * @param string $pathbase path base
512 * @param int $userid user ID
71ab7e51 513 * @param file_progress $progress Progress indicator callback or null if not required
8496fdac 514 * @return array|bool list of processed files; false if error
17d9269f 515 */
71ab7e51 516 public function extract_to_storage(file_packer $packer, $contextid,
517 $component, $filearea, $itemid, $pathbase, $userid = null, file_progress $progress = null) {
16a34ae1
AN
518
519 return $this->filesystem->extract_to_storage($this, $packer, $contextid, $component, $filearea,
520 $itemid, $pathbase, $userid, $progress);
17d9269f 521 }
522
b1897a6d 523 /**
8496fdac
PS
524 * Add file/directory into archive.
525 *
d2b7803e 526 * @param file_archive $filearch file archive instance
c78a0558 527 * @param string $archivepath pathname in archive
b1897a6d 528 * @return bool success
529 */
0b0bfa93 530 public function archive_file(file_archive $filearch, $archivepath) {
16a34ae1 531 return $this->filesystem->add_storedfile_to_archive($this, $filearch, $archivepath);
b1897a6d 532 }
533
797f19e8 534 /**
535 * Returns information about image,
536 * information is determined from the file content
d2b7803e 537 *
797f19e8 538 * @return mixed array with width, height and mimetype; false if not an image
539 */
540 public function get_imageinfo() {
16a34ae1 541 return $this->filesystem->get_imageinfo($this);
797f19e8 542 }
543
544 /**
545 * Verifies the file is a valid web image - gif, png and jpeg only.
8496fdac 546 *
797f19e8 547 * It should be ok to serve this image from server without any other security workarounds.
8496fdac 548 *
797f19e8 549 * @return bool true if file ok
550 */
551 public function is_valid_image() {
552 $mimetype = $this->get_mimetype();
559276b1 553 if (!file_mimetype_in_typegroup($mimetype, 'web_image')) {
797f19e8 554 return false;
555 }
556 if (!$info = $this->get_imageinfo()) {
557 return false;
558 }
559 if ($info['mimetype'] !== $mimetype) {
560 return false;
561 }
562 // ok, GD likes this image
563 return true;
564 }
565
4b6b5ce7 566 /**
8496fdac
PS
567 * Returns parent directory, creates missing parents if needed.
568 *
569 * @return stored_file
4b6b5ce7 570 */
571 public function get_parent_directory() {
572 if ($this->file_record->filepath === '/' and $this->file_record->filename === '.') {
573 //root dir does not have parent
574 return null;
575 }
576
577 if ($this->file_record->filename !== '.') {
64f93798 578 return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $this->file_record->filepath);
4b6b5ce7 579 }
580
581 $filepath = $this->file_record->filepath;
582 $filepath = trim($filepath, '/');
583 $dirs = explode('/', $filepath);
584 array_pop($dirs);
585 $filepath = implode('/', $dirs);
586 $filepath = ($filepath === '') ? '/' : "/$filepath/";
587
64f93798 588 return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath);
4b6b5ce7 589 }
590
03756f0b
CB
591 /**
592 * Set synchronised content from file.
593 *
594 * @param string $path Path to the file.
595 */
596 public function set_synchronised_content_from_file($path) {
597 $this->fs->synchronise_stored_file_from_file($this, $path, $this->file_record);
598 }
599
600 /**
601 * Set synchronised content from content.
602 *
603 * @param string $content File content.
604 */
605 public function set_synchronised_content_from_string($content) {
606 $this->fs->synchronise_stored_file_from_string($this, $content, $this->file_record);
607 }
608
16a95e8f 609 /**
0b2bfbd1 610 * Synchronize file if it is a reference and needs synchronizing
67233725 611 *
0b2bfbd1 612 * Updates contenthash and filesize
67233725
DC
613 */
614 public function sync_external_file() {
87355560
MG
615 if (!empty($this->repository)) {
616 $this->repository->sync_reference($this);
67233725 617 }
67233725
DC
618 }
619
620 /**
621 * Returns context id of the file
8496fdac 622 *
16a95e8f 623 * @return int context id
624 */
172dd12c 625 public function get_contextid() {
626 return $this->file_record->contextid;
627 }
628
16a95e8f 629 /**
64f93798
PS
630 * Returns component name - this is the owner of the areas,
631 * nothing else is allowed to read or modify the files directly!!
632 *
633 * @return string
634 */
635 public function get_component() {
636 return $this->file_record->component;
637 }
638
639 /**
640 * Returns file area name, this divides files of one component into groups with different access control.
641 * All files in one area have the same access control.
8496fdac 642 *
16a95e8f 643 * @return string
644 */
172dd12c 645 public function get_filearea() {
646 return $this->file_record->filearea;
647 }
648
16a95e8f 649 /**
8496fdac
PS
650 * Returns returns item id of file.
651 *
16a95e8f 652 * @return int
653 */
172dd12c 654 public function get_itemid() {
655 return $this->file_record->itemid;
656 }
657
16a95e8f 658 /**
659 * Returns file path - starts and ends with /, \ are not allowed.
8496fdac 660 *
16a95e8f 661 * @return string
662 */
172dd12c 663 public function get_filepath() {
664 return $this->file_record->filepath;
665 }
666
16a95e8f 667 /**
668 * Returns file name or '.' in case of directories.
8496fdac 669 *
16a95e8f 670 * @return string
671 */
172dd12c 672 public function get_filename() {
673 return $this->file_record->filename;
674 }
675
16a95e8f 676 /**
677 * Returns id of user who created the file.
8496fdac 678 *
16a95e8f 679 * @return int
680 */
172dd12c 681 public function get_userid() {
682 return $this->file_record->userid;
683 }
684
16a95e8f 685 /**
686 * Returns the size of file in bytes.
8496fdac 687 *
16a95e8f 688 * @return int bytes
689 */
172dd12c 690 public function get_filesize() {
67233725 691 $this->sync_external_file();
172dd12c 692 return $this->file_record->filesize;
693 }
694
2a68ee0e 695 /**
e6904efc 696 * Function stored_file::set_filesize() is deprecated. Please use stored_file::replace_file_with
61506a0a 697 *
e6904efc
JO
698 * @deprecated since Moodle 2.6 MDL-42016 - please do not use this function any more.
699 * @see stored_file::replace_file_with()
61506a0a
DC
700 */
701 public function set_filesize($filesize) {
e6904efc
JO
702 throw new coding_exception('Function stored_file::set_filesize() can not be used any more. ' .
703 'Please use stored_file::replace_file_with()');
61506a0a
DC
704 }
705
16a95e8f 706 /**
8496fdac
PS
707 * Returns mime type of file.
708 *
16a95e8f 709 * @return string
710 */
172dd12c 711 public function get_mimetype() {
712 return $this->file_record->mimetype;
713 }
714
16a95e8f 715 /**
8496fdac
PS
716 * Returns unix timestamp of file creation date.
717 *
16a95e8f 718 * @return int
719 */
172dd12c 720 public function get_timecreated() {
721 return $this->file_record->timecreated;
722 }
723
16a95e8f 724 /**
8496fdac
PS
725 * Returns unix timestamp of last file modification.
726 *
16a95e8f 727 * @return int
728 */
172dd12c 729 public function get_timemodified() {
67233725 730 $this->sync_external_file();
172dd12c 731 return $this->file_record->timemodified;
732 }
6c0e2d08 733
67233725
DC
734 /**
735 * set timemodified
736 *
737 * @param int $timemodified
738 */
739 public function set_timemodified($timemodified) {
740 $filerecord = new stdClass;
741 $filerecord->timemodified = $timemodified;
742 $this->update($filerecord);
743 }
744
16a95e8f 745 /**
8496fdac
PS
746 * Returns file status flag.
747 *
16a95e8f 748 * @return int 0 means file OK, anything else is a problem and file can not be used
749 */
6c0e2d08 750 public function get_status() {
751 return $this->file_record->status;
752 }
ee03a651 753
16a95e8f 754 /**
8496fdac
PS
755 * Returns file id.
756 *
16a95e8f 757 * @return int
758 */
ee03a651 759 public function get_id() {
760 return $this->file_record->id;
761 }
4284e1cc 762
16a95e8f 763 /**
8496fdac
PS
764 * Returns sha1 hash of file content.
765 *
16a95e8f 766 * @return string
767 */
4284e1cc 768 public function get_contenthash() {
67233725 769 $this->sync_external_file();
4284e1cc 770 return $this->file_record->contenthash;
771 }
6ed19c74 772
16a95e8f 773 /**
64f93798 774 * Returns sha1 hash of all file path components sha1("contextid/component/filearea/itemid/dir/dir/filename.ext").
8496fdac 775 *
16a95e8f 776 * @return string
777 */
6ed19c74 778 public function get_pathnamehash() {
779 return $this->file_record->pathnamehash;
780 }
1dce6261
DC
781
782 /**
8496fdac
PS
783 * Returns the license type of the file, it is a short name referred from license table.
784 *
1dce6261
DC
785 * @return string
786 */
787 public function get_license() {
788 return $this->file_record->license;
789 }
790
67233725
DC
791 /**
792 * Set license
793 *
794 * @param string $license license
795 */
796 public function set_license($license) {
797 $filerecord = new stdClass;
798 $filerecord->license = $license;
799 $this->update($filerecord);
800 }
801
1dce6261 802 /**
8496fdac
PS
803 * Returns the author name of the file.
804 *
1dce6261
DC
805 * @return string
806 */
807 public function get_author() {
31cd5fe8 808 return $this->file_record->author;
1dce6261
DC
809 }
810
67233725
DC
811 /**
812 * Set author
813 *
814 * @param string $author
815 */
816 public function set_author($author) {
817 $filerecord = new stdClass;
818 $filerecord->author = $author;
819 $this->update($filerecord);
820 }
821
1dce6261 822 /**
8496fdac
PS
823 * Returns the source of the file, usually it is a url.
824 *
1dce6261
DC
825 * @return string
826 */
827 public function get_source() {
828 return $this->file_record->source;
829 }
8496fdac 830
67233725
DC
831 /**
832 * Set license
833 *
834 * @param string $license license
835 */
836 public function set_source($source) {
837 $filerecord = new stdClass;
838 $filerecord->source = $source;
839 $this->update($filerecord);
840 }
841
842
f79321f1
DC
843 /**
844 * Returns the sort order of file
845 *
846 * @return int
847 */
848 public function get_sortorder() {
849 return $this->file_record->sortorder;
850 }
67233725
DC
851
852 /**
853 * Set file sort order
854 *
855 * @param int $sortorder
856 * @return int
857 */
858 public function set_sortorder($sortorder) {
023037dc 859 $oldorder = $this->file_record->sortorder;
67233725
DC
860 $filerecord = new stdClass;
861 $filerecord->sortorder = $sortorder;
862 $this->update($filerecord);
023037dc 863 if (!$this->is_directory()) {
864 // Callback for file sort order change.
865 if ($pluginsfunction = get_plugins_with_function('after_file_sorted')) {
866 foreach ($pluginsfunction as $plugintype => $plugins) {
867 foreach ($plugins as $pluginfunction) {
868 $pluginfunction($this->file_record, $oldorder, $sortorder);
869 }
870 }
871 }
872 }
67233725
DC
873 }
874
875 /**
876 * Returns repository id
877 *
878 * @return int|null
879 */
880 public function get_repository_id() {
881 if (!empty($this->repository)) {
882 return $this->repository->id;
883 } else {
884 return null;
885 }
886 }
887
1104a9fa
JL
888 /**
889 * Returns repository type.
890 *
891 * @return mixed str|null the repository type or null if is not an external file
892 * @since Moodle 3.3
893 */
894 public function get_repository_type() {
895
896 if (!empty($this->repository)) {
897 return $this->repository->get_typename();
898 } else {
899 return null;
900 }
901 }
902
903
67233725
DC
904 /**
905 * get reference file id
906 * @return int
907 */
908 public function get_referencefileid() {
909 return $this->file_record->referencefileid;
910 }
911
912 /**
913 * Get reference last sync time
914 * @return int
915 */
916 public function get_referencelastsync() {
917 return $this->file_record->referencelastsync;
918 }
919
920 /**
e6904efc
JO
921 * Function stored_file::get_referencelifetime() is deprecated as reference
922 * life time is no longer stored in DB or returned by repository. Each
87355560
MG
923 * repository should decide by itself when to synchronise the references.
924 *
e6904efc 925 * @deprecated since Moodle 2.6 MDL-42016 - please do not use this function any more.
87355560 926 * @see repository::sync_reference()
67233725
DC
927 */
928 public function get_referencelifetime() {
e6904efc
JO
929 throw new coding_exception('Function stored_file::get_referencelifetime() can not be used any more. ' .
930 'See repository::sync_reference().');
67233725
DC
931 }
932 /**
933 * Returns file reference
934 *
935 * @return string
936 */
937 public function get_reference() {
938 return $this->file_record->reference;
939 }
940
941 /**
942 * Get human readable file reference information
943 *
944 * @return string
945 */
946 public function get_reference_details() {
0b2bfbd1
MG
947 return $this->repository->get_reference_details($this->get_reference(), $this->get_status());
948 }
949
950 /**
951 * Called after reference-file has been synchronized with the repository
952 *
953 * We update contenthash, filesize and status in files table if changed
954 * and we always update lastsync in files_reference table
955 *
87355560
MG
956 * @param null|string $contenthash if set to null contenthash is not changed
957 * @param int $filesize new size of the file
958 * @param int $status new status of the file (0 means OK, 666 - source missing)
58e5ab1b 959 * @param int $timemodified last time modified of the source, if known
0b2bfbd1 960 */
58e5ab1b 961 public function set_synchronized($contenthash, $filesize, $status = 0, $timemodified = null) {
0b2bfbd1
MG
962 if (!$this->is_external_file()) {
963 return;
964 }
965 $now = time();
87355560
MG
966 if ($contenthash === null) {
967 $contenthash = $this->file_record->contenthash;
968 }
14b7e500
MG
969 if ($contenthash != $this->file_record->contenthash) {
970 $oldcontenthash = $this->file_record->contenthash;
0b2bfbd1 971 }
14b7e500 972 // this will update all entries in {files} that have the same filereference id
58e5ab1b 973 $this->fs->update_references($this->file_record->referencefileid, $now, null, $contenthash, $filesize, $status, $timemodified);
14b7e500
MG
974 // we don't need to call update() for this object, just set the values of changed fields
975 $this->file_record->contenthash = $contenthash;
976 $this->file_record->filesize = $filesize;
977 $this->file_record->status = $status;
978 $this->file_record->referencelastsync = $now;
58e5ab1b
MG
979 if ($timemodified) {
980 $this->file_record->timemodified = $timemodified;
981 }
14b7e500 982 if (isset($oldcontenthash)) {
16a34ae1 983 $this->filesystem->remove_file($oldcontenthash);
0b2bfbd1 984 }
0b2bfbd1
MG
985 }
986
14b7e500
MG
987 /**
988 * Sets the error status for a file that could not be synchronised
14b7e500 989 */
87355560
MG
990 public function set_missingsource() {
991 $this->set_synchronized($this->file_record->contenthash, $this->file_record->filesize, 666);
67233725
DC
992 }
993
994 /**
995 * Send file references
996 *
997 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
998 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
999 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
1000 * @param array $options additional options affecting the file serving
1001 */
1002 public function send_file($lifetime, $filter, $forcedownload, $options) {
1003 $this->repository->send_file($this, $lifetime, $filter, $forcedownload, $options);
1004 }
bc6f241c
MG
1005
1006 /**
1007 * Imports the contents of an external file into moodle filepool.
1008 *
1009 * @throws moodle_exception if file could not be downloaded or is too big
1010 * @param int $maxbytes throw an exception if file size is bigger than $maxbytes (0 means no limit)
1011 */
1012 public function import_external_file_contents($maxbytes = 0) {
1013 if ($this->repository) {
1014 $this->repository->import_external_file_contents($this, $maxbytes);
1015 }
1016 }
361a47d4
DM
1017
1018 /**
1019 * Gets a file relative to this file in the repository and sends it to the browser.
1020 * Checks the function repository::supports_relative_file() to make sure it can be used.
1021 *
1022 * @param string $relativepath the relative path to the file we are trying to access
1023 */
1024 public function send_relative_file($relativepath) {
1025 if ($this->repository && $this->repository->supports_relative_file()) {
1026 $relativepath = clean_param($relativepath, PARAM_PATH);
1027 $this->repository->send_relative_file($this, $relativepath);
1028 } else {
1029 send_file_not_found();
1030 }
1031 }
b7067f06
AN
1032
1033 /**
1034 * Generates a thumbnail for this stored_file.
1035 *
1036 * If the GD library has at least version 2 and PNG support is available, the returned data
1037 * is the content of a transparent PNG file containing the thumbnail. Otherwise, the function
1038 * returns contents of a JPEG file with black background containing the thumbnail.
1039 *
1040 * @param int $width the width of the requested thumbnail
1041 * @param int $height the height of the requested thumbnail
1042 * @return string|bool false if a problem occurs, the thumbnail image data otherwise
1043 */
1044 public function generate_image_thumbnail($width, $height) {
1045 if (empty($width) or empty($height)) {
1046 return false;
1047 }
1048
16a34ae1
AN
1049 $content = $this->get_content();
1050
b7067f06 1051 // Fetch the image information for this image.
16a34ae1 1052 $imageinfo = @getimagesizefromstring($content);
b7067f06
AN
1053 if (empty($imageinfo)) {
1054 return false;
1055 }
1056
1057 // Create a new image from the file.
16a34ae1 1058 $original = @imagecreatefromstring($content);
b7067f06
AN
1059
1060 // Generate the thumbnail.
1061 return generate_image_thumbnail_from_image($original, $imageinfo, $width, $height);
1062 }
92b9ef99
FM
1063
1064 /**
1065 * Generate a resized image for this stored_file.
1066 *
1067 * @param int|null $width The desired width, or null to only use the height.
1068 * @param int|null $height The desired height, or null to only use the width.
1069 * @return string|false False when a problem occurs, else the image data.
1070 */
1071 public function resize_image($width, $height) {
1072 global $CFG;
1073 require_once($CFG->libdir . '/gdlib.php');
1074
16a34ae1
AN
1075 $content = $this->get_content();
1076
92b9ef99 1077 // Fetch the image information for this image.
16a34ae1 1078 $imageinfo = @getimagesizefromstring($content);
92b9ef99
FM
1079 if (empty($imageinfo)) {
1080 return false;
1081 }
1082
1083 // Create a new image from the file.
16a34ae1 1084 $original = @imagecreatefromstring($content);
92b9ef99
FM
1085
1086 // Generate the resized image.
1087 return resize_image_from_image($original, $imageinfo, $width, $height);
1088 }
a30a04fa
AN
1089
1090 /**
1091 * Check whether the supplied file is the same as this file.
1092 *
1093 * @param string $path The path to the file on disk
1094 * @return boolean
1095 */
1096 public function compare_to_path($path) {
1097 return $this->get_contenthash() === file_storage::hash_from_path($path);
1098 }
1099
1100 /**
1101 * Check whether the supplied content is the same as this file.
1102 *
1103 * @param string $content The file content
1104 * @return boolean
1105 */
1106 public function compare_to_string($content) {
1107 return $this->get_contenthash() === file_storage::hash_from_string($content);
1108 }
f79321f1 1109}