2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Definition of a class stored_file.
22 * @copyright 2008 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Class representing local files stored in a sha1 file pool.
31 * Since Moodle 2.0 file contents are stored in sha1 pool and
32 * all other file information is stored in new "files" database table.
36 * @copyright 2008 Petr Skoda {@link http://skodak.org}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 /** @var file_storage file storage pool instance */
43 /** @var stdClass record from the files table left join files_reference table */
45 /** @var string location of content files */
47 /** @var repository repository plugin instance */
51 * Constructor, this constructor should be called ONLY from the file_storage class!
53 * @param file_storage $fs file storage instance
54 * @param stdClass $file_record description of file
55 * @param string $filedir location of file directory with sh1 named content files
57 public function __construct(file_storage
$fs, stdClass
$file_record, $filedir) {
60 $this->file_record
= clone($file_record); // prevent modifications
61 $this->filedir
= $filedir; // keep secret, do not expose!
63 if (!empty($file_record->repositoryid
)) {
64 require_once("$CFG->dirroot/repository/lib.php");
65 $this->repository
= repository
::get_repository_by_id($file_record->repositoryid
, SYSCONTEXTID
);
66 if ($this->repository
->supported_returntypes() & FILE_REFERENCE
!= FILE_REFERENCE
) {
67 // Repository cannot do file reference.
68 throw new moodle_exception('error');
71 $this->repository
= null;
73 // make sure all reference fields exist in file_record even when it is not a reference
74 foreach (array('referencelastsync', 'referencelifetime', 'referencefileid', 'reference', 'repositoryid') as $key) {
75 if (empty($this->file_record
->$key)) {
76 $this->file_record
->$key = null;
82 * Whether or not this is a external resource
86 public function is_external_file() {
87 return !empty($this->repository
);
91 * Update some file record fields
92 * NOTE: Must remain protected
94 * @param stdClass $dataobject
96 protected function update($dataobject) {
98 $keys = array_keys((array)$this->file_record
);
99 foreach ($dataobject as $field => $value) {
100 if (in_array($field, $keys)) {
101 if ($field == 'contextid' and (!is_number($value) or $value < 1)) {
102 throw new file_exception('storedfileproblem', 'Invalid contextid');
105 if ($field == 'component') {
106 $value = clean_param($value, PARAM_COMPONENT
);
108 throw new file_exception('storedfileproblem', 'Invalid component');
112 if ($field == 'filearea') {
113 $value = clean_param($value, PARAM_AREA
);
115 throw new file_exception('storedfileproblem', 'Invalid filearea');
119 if ($field == 'itemid' and (!is_number($value) or $value < 0)) {
120 throw new file_exception('storedfileproblem', 'Invalid itemid');
124 if ($field == 'filepath') {
125 $value = clean_param($value, PARAM_PATH
);
126 if (strpos($value, '/') !== 0 or strrpos($value, '/') !== strlen($value)-1) {
127 // path must start and end with '/'
128 throw new file_exception('storedfileproblem', 'Invalid file path');
132 if ($field == 'filename') {
133 // folder has filename == '.', so we pass this
135 $value = clean_param($value, PARAM_FILE
);
138 throw new file_exception('storedfileproblem', 'Invalid file name');
142 if ($field === 'timecreated' or $field === 'timemodified') {
143 if (!is_number($value)) {
144 throw new file_exception('storedfileproblem', 'Invalid timestamp');
151 if ($field === 'referencefileid') {
152 if (!is_null($value) and !is_number($value)) {
153 throw new file_exception('storedfileproblem', 'Invalid reference info');
157 if ($field === 'referencelastsync' or $field === 'referencelifetime') {
158 // do not update those fields
159 // TODO MDL-33416 [2.4] fields referencelastsync and referencelifetime to be removed from {files} table completely
164 $this->file_record
->$field = $value;
166 throw new coding_exception("Invalid field name, $field doesn't exist in file record");
169 // Validate mimetype field
170 // we don't use {@link stored_file::get_content_file_location()} here becaues it will try to update file_record
171 $pathname = $this->get_pathname_by_contenthash();
172 // try to recover the content from trash
173 if (!is_readable($pathname)) {
174 if (!$this->fs
->try_content_recovery($this) or !is_readable($pathname)) {
175 throw new file_exception('storedfilecannotread', '', $pathname);
178 $mimetype = $this->fs
->mimetype($pathname, $this->file_record
->filename
);
179 $this->file_record
->mimetype
= $mimetype;
181 $DB->update_record('files', $this->file_record
);
187 * @param string $filepath file path
188 * @param string $filename file name
190 public function rename($filepath, $filename) {
191 if ($this->fs
->file_exists($this->get_contextid(), $this->get_component(), $this->get_filearea(), $this->get_itemid(), $filepath, $filename)) {
192 throw new file_exception('storedfilenotcreated', '', 'file exists, cannot rename');
194 $filerecord = new stdClass
;
195 $filerecord->filepath
= $filepath;
196 $filerecord->filename
= $filename;
197 // populate the pathname hash
198 $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);
199 $this->update($filerecord);
203 * Replace the content by providing another stored_file instance
205 * @param stored_file $storedfile
207 public function replace_content_with(stored_file
$storedfile) {
208 $contenthash = $storedfile->get_contenthash();
209 $this->set_contenthash($contenthash);
210 $this->set_filesize($storedfile->get_filesize());
214 * Replaces the fields that might have changed when file was overriden in filepicker:
215 * reference, contenthash, filesize, userid
217 * Note that field 'source' must be updated separately because
218 * it has different format for draft and non-draft areas and
219 * this function will usually be used to replace non-draft area
220 * file with draft area file.
222 * @param stored_file $newfile
223 * @throws coding_exception
225 public function replace_file_with(stored_file
$newfile) {
226 if ($newfile->get_referencefileid() &&
227 $this->fs
->get_references_count_by_storedfile($this)) {
228 // The new file is a reference.
229 // The current file has other local files referencing to it.
230 // Double reference is not allowed.
231 throw new moodle_exception('errordoublereference', 'repository');
234 $filerecord = new stdClass
;
235 $contenthash = $newfile->get_contenthash();
236 if ($this->fs
->content_exists($contenthash)) {
237 $filerecord->contenthash
= $contenthash;
239 throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
241 $filerecord->filesize
= $newfile->get_filesize();
242 $filerecord->referencefileid
= $newfile->get_referencefileid();
243 $filerecord->userid
= $newfile->get_userid();
244 $this->update($filerecord);
248 * Unlink the stored file from the referenced file
250 * This methods destroys the link to the record in files_reference table. This effectively
251 * turns the stored file from being an alias to a plain copy. However, the caller has
252 * to make sure that the actual file's content has beed synced prior to calling this method.
254 public function delete_reference() {
257 if (!$this->is_external_file()) {
258 throw new coding_exception('An attempt to unlink a non-reference file.');
261 $transaction = $DB->start_delegated_transaction();
263 // Are we the only one referring to the original file? If so, delete the
264 // referenced file record. Note we do not use file_storage::search_references_count()
265 // here because we want to count draft files too and we are at a bit lower access level here.
266 $countlinks = $DB->count_records('files',
267 array('referencefileid' => $this->file_record
->referencefileid
));
268 if ($countlinks == 1) {
269 $DB->delete_records('files_reference', array('id' => $this->file_record
->referencefileid
));
272 // Update the underlying record in the database.
273 $update = new stdClass();
274 $update->referencefileid
= null;
275 $this->update($update);
277 $transaction->allow_commit();
279 // Update our properties and the record in the memory.
280 $this->repository
= null;
281 $this->file_record
->repositoryid
= null;
282 $this->file_record
->reference
= null;
283 $this->file_record
->referencefileid
= null;
284 $this->file_record
->referencelastsync
= null;
285 $this->file_record
->referencelifetime
= null;
289 * Is this a directory?
291 * Directories are only emulated, internally they are stored as empty
292 * files with a "." instead of name - this means empty directory contains
293 * exactly one empty file with name dot.
295 * @return bool true means directory, false means file
297 public function is_directory() {
298 return ($this->file_record
->filename
=== '.');
302 * Delete file from files table.
304 * The content of files stored in sha1 pool is reclaimed
305 * later - the occupied disk space is reclaimed much later.
307 * @return bool always true or exception if error occurred
309 public function delete() {
312 if ($this->is_directory()) {
313 // Directories can not be referenced, just delete the record.
314 $DB->delete_records('files', array('id'=>$this->file_record
->id
));
317 $transaction = $DB->start_delegated_transaction();
319 // If there are other files referring to this file, convert them to copies.
320 if ($files = $this->fs
->get_references_by_storedfile($this)) {
321 foreach ($files as $file) {
322 $this->fs
->import_external_file($file);
326 // If this file is a reference (alias) to another file, unlink it first.
327 if ($this->is_external_file()) {
328 $this->delete_reference();
331 // Now delete the file record.
332 $DB->delete_records('files', array('id'=>$this->file_record
->id
));
334 $transaction->allow_commit();
337 // Move pool file to trash if content not needed any more.
338 $this->fs
->deleted_file_cleanup($this->file_record
->contenthash
);
339 return true; // BC only
343 * Get file pathname by contenthash
345 * NOTE, this function is not calling sync_external_file, it assume the contenthash is current
346 * Protected - developers must not gain direct access to this function.
348 * @return string full path to pool file with file content
350 protected function get_pathname_by_contenthash() {
351 // Detect is local file or not.
352 $contenthash = $this->file_record
->contenthash
;
353 $l1 = $contenthash[0].$contenthash[1];
354 $l2 = $contenthash[2].$contenthash[3];
355 return "$this->filedir/$l1/$l2/$contenthash";
359 * Get file pathname by given contenthash, this method will try to sync files
361 * Protected - developers must not gain direct access to this function.
363 * NOTE: do not make this public, we must not modify or delete the pool files directly! ;-)
365 * @return string full path to pool file with file content
367 protected function get_content_file_location() {
368 $this->sync_external_file();
369 return $this->get_pathname_by_contenthash();
373 * adds this file path to a curl request (POST only)
375 * @param curl $curlrequest the curl request object
376 * @param string $key what key to use in the POST request
379 public function add_to_curl_request(&$curlrequest, $key) {
380 $curlrequest->_tmp_file_post_params
[$key] = '@' . $this->get_content_file_location();
384 * Returns file handle - read only mode, no writing allowed into pool files!
386 * When you want to modify a file, create a new file and delete the old one.
388 * @return resource file handle
390 public function get_content_file_handle() {
391 $path = $this->get_content_file_location();
392 if (!is_readable($path)) {
393 if (!$this->fs
->try_content_recovery($this) or !is_readable($path)) {
394 throw new file_exception('storedfilecannotread', '', $path);
397 return fopen($path, 'rb'); // Binary reading only!!
401 * Dumps file content to page.
403 public function readfile() {
404 $path = $this->get_content_file_location();
405 if (!is_readable($path)) {
406 if (!$this->fs
->try_content_recovery($this) or !is_readable($path)) {
407 throw new file_exception('storedfilecannotread', '', $path);
410 readfile_allow_large($path, $this->get_filesize());
414 * Returns file content as string.
416 * @return string content
418 public function get_content() {
419 $path = $this->get_content_file_location();
420 if (!is_readable($path)) {
421 if (!$this->fs
->try_content_recovery($this) or !is_readable($path)) {
422 throw new file_exception('storedfilecannotread', '', $path);
425 return file_get_contents($this->get_content_file_location());
429 * Copy content of file to given pathname.
431 * @param string $pathname real path to the new file
432 * @return bool success
434 public function copy_content_to($pathname) {
435 $path = $this->get_content_file_location();
436 if (!is_readable($path)) {
437 if (!$this->fs
->try_content_recovery($this) or !is_readable($path)) {
438 throw new file_exception('storedfilecannotread', '', $path);
441 return copy($path, $pathname);
445 * Copy content of file to temporary folder and returns file path
447 * @param string $dir name of the temporary directory
448 * @param string $fileprefix prefix of temporary file.
449 * @return string|bool path of temporary file or false.
451 public function copy_content_to_temp($dir = 'files', $fileprefix = 'tempup_') {
453 if (!$dir = make_temp_directory($dir)) {
456 if (!$tempfile = tempnam($dir, $fileprefix)) {
459 if (!$this->copy_content_to($tempfile)) {
460 // something went wrong
468 * List contents of archive.
470 * @param file_packer $packer file packer instance
471 * @return array of file infos
473 public function list_files(file_packer
$packer) {
474 $archivefile = $this->get_content_file_location();
475 return $packer->list_files($archivefile);
479 * Extract file to given file path (real OS filesystem), existing files are overwritten.
481 * @param file_packer $packer file packer instance
482 * @param string $pathname target directory
483 * @return array|bool list of processed files; false if error
485 public function extract_to_pathname(file_packer
$packer, $pathname) {
486 $archivefile = $this->get_content_file_location();
487 return $packer->extract_to_pathname($archivefile, $pathname);
491 * Extract file to given file path (real OS filesystem), existing files are overwritten.
493 * @param file_packer $packer file packer instance
494 * @param int $contextid context ID
495 * @param string $component component
496 * @param string $filearea file area
497 * @param int $itemid item ID
498 * @param string $pathbase path base
499 * @param int $userid user ID
500 * @return array|bool list of processed files; false if error
502 public function extract_to_storage(file_packer
$packer, $contextid, $component, $filearea, $itemid, $pathbase, $userid = NULL) {
503 $archivefile = $this->get_content_file_location();
504 return $packer->extract_to_storage($archivefile, $contextid, $component, $filearea, $itemid, $pathbase);
508 * Add file/directory into archive.
510 * @param file_archive $filearch file archive instance
511 * @param string $archivepath pathname in archive
512 * @return bool success
514 public function archive_file(file_archive
$filearch, $archivepath) {
515 if ($this->is_directory()) {
516 return $filearch->add_directory($archivepath);
518 $path = $this->get_content_file_location();
519 if (!is_readable($path)) {
522 return $filearch->add_file_from_pathname($archivepath, $path);
527 * Returns information about image,
528 * information is determined from the file content
530 * @return mixed array with width, height and mimetype; false if not an image
532 public function get_imageinfo() {
533 $path = $this->get_content_file_location();
534 if (!is_readable($path)) {
535 if (!$this->fs
->try_content_recovery($this) or !is_readable($path)) {
536 throw new file_exception('storedfilecannotread', '', $path);
539 $mimetype = $this->get_mimetype();
540 if (!preg_match('|^image/|', $mimetype) ||
!filesize($path) ||
!($imageinfo = getimagesize($path))) {
543 $image = array('width'=>$imageinfo[0], 'height'=>$imageinfo[1], 'mimetype'=>image_type_to_mime_type($imageinfo[2]));
544 if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) {
545 // gd can not parse it, sorry
552 * Verifies the file is a valid web image - gif, png and jpeg only.
554 * It should be ok to serve this image from server without any other security workarounds.
556 * @return bool true if file ok
558 public function is_valid_image() {
559 $mimetype = $this->get_mimetype();
560 if (!file_mimetype_in_typegroup($mimetype, 'web_image')) {
563 if (!$info = $this->get_imageinfo()) {
566 if ($info['mimetype'] !== $mimetype) {
569 // ok, GD likes this image
574 * Returns parent directory, creates missing parents if needed.
576 * @return stored_file
578 public function get_parent_directory() {
579 if ($this->file_record
->filepath
=== '/' and $this->file_record
->filename
=== '.') {
580 //root dir does not have parent
584 if ($this->file_record
->filename
!== '.') {
585 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
);
588 $filepath = $this->file_record
->filepath
;
589 $filepath = trim($filepath, '/');
590 $dirs = explode('/', $filepath);
592 $filepath = implode('/', $dirs);
593 $filepath = ($filepath === '') ?
'/' : "/$filepath/";
595 return $this->fs
->create_directory($this->file_record
->contextid
, $this->file_record
->component
, $this->file_record
->filearea
, $this->file_record
->itemid
, $filepath);
599 * Synchronize file if it is a reference and needs synchronizing
601 * Updates contenthash and filesize
603 public function sync_external_file() {
605 if (!empty($this->file_record
->referencefileid
)) {
606 require_once($CFG->dirroot
.'/repository/lib.php');
607 repository
::sync_external_file($this);
612 * Returns context id of the file
614 * @return int context id
616 public function get_contextid() {
617 return $this->file_record
->contextid
;
621 * Returns component name - this is the owner of the areas,
622 * nothing else is allowed to read or modify the files directly!!
626 public function get_component() {
627 return $this->file_record
->component
;
631 * Returns file area name, this divides files of one component into groups with different access control.
632 * All files in one area have the same access control.
636 public function get_filearea() {
637 return $this->file_record
->filearea
;
641 * Returns returns item id of file.
645 public function get_itemid() {
646 return $this->file_record
->itemid
;
650 * Returns file path - starts and ends with /, \ are not allowed.
654 public function get_filepath() {
655 return $this->file_record
->filepath
;
659 * Returns file name or '.' in case of directories.
663 public function get_filename() {
664 return $this->file_record
->filename
;
668 * Returns id of user who created the file.
672 public function get_userid() {
673 return $this->file_record
->userid
;
677 * Returns the size of file in bytes.
681 public function get_filesize() {
682 $this->sync_external_file();
683 return $this->file_record
->filesize
;
687 * Returns the size of file in bytes.
689 * @param int $filesize bytes
691 public function set_filesize($filesize) {
692 $filerecord = new stdClass
;
693 $filerecord->filesize
= $filesize;
694 $this->update($filerecord);
698 * Returns mime type of file.
702 public function get_mimetype() {
703 return $this->file_record
->mimetype
;
707 * Returns unix timestamp of file creation date.
711 public function get_timecreated() {
712 return $this->file_record
->timecreated
;
716 * Returns unix timestamp of last file modification.
720 public function get_timemodified() {
721 $this->sync_external_file();
722 return $this->file_record
->timemodified
;
728 * @param int $timemodified
730 public function set_timemodified($timemodified) {
731 $filerecord = new stdClass
;
732 $filerecord->timemodified
= $timemodified;
733 $this->update($filerecord);
737 * Returns file status flag.
739 * @return int 0 means file OK, anything else is a problem and file can not be used
741 public function get_status() {
742 return $this->file_record
->status
;
750 public function get_id() {
751 return $this->file_record
->id
;
755 * Returns sha1 hash of file content.
759 public function get_contenthash() {
760 $this->sync_external_file();
761 return $this->file_record
->contenthash
;
767 * @param string $contenthash
769 protected function set_contenthash($contenthash) {
770 // make sure the content exists in moodle file pool
771 if ($this->fs
->content_exists($contenthash)) {
772 $filerecord = new stdClass
;
773 $filerecord->contenthash
= $contenthash;
774 $this->update($filerecord);
776 throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
781 * Returns sha1 hash of all file path components sha1("contextid/component/filearea/itemid/dir/dir/filename.ext").
785 public function get_pathnamehash() {
786 return $this->file_record
->pathnamehash
;
790 * Returns the license type of the file, it is a short name referred from license table.
794 public function get_license() {
795 return $this->file_record
->license
;
801 * @param string $license license
803 public function set_license($license) {
804 $filerecord = new stdClass
;
805 $filerecord->license
= $license;
806 $this->update($filerecord);
810 * Returns the author name of the file.
814 public function get_author() {
815 return $this->file_record
->author
;
821 * @param string $author
823 public function set_author($author) {
824 $filerecord = new stdClass
;
825 $filerecord->author
= $author;
826 $this->update($filerecord);
830 * Returns the source of the file, usually it is a url.
834 public function get_source() {
835 return $this->file_record
->source
;
841 * @param string $license license
843 public function set_source($source) {
844 $filerecord = new stdClass
;
845 $filerecord->source
= $source;
846 $this->update($filerecord);
851 * Returns the sort order of file
855 public function get_sortorder() {
856 return $this->file_record
->sortorder
;
860 * Set file sort order
862 * @param int $sortorder
865 public function set_sortorder($sortorder) {
866 $filerecord = new stdClass
;
867 $filerecord->sortorder
= $sortorder;
868 $this->update($filerecord);
872 * Returns repository id
876 public function get_repository_id() {
877 if (!empty($this->repository
)) {
878 return $this->repository
->id
;
885 * get reference file id
888 public function get_referencefileid() {
889 return $this->file_record
->referencefileid
;
893 * Get reference last sync time
896 public function get_referencelastsync() {
897 return $this->file_record
->referencelastsync
;
901 * Get reference last sync time
904 public function get_referencelifetime() {
905 return $this->file_record
->referencelifetime
;
908 * Returns file reference
912 public function get_reference() {
913 return $this->file_record
->reference
;
917 * Get human readable file reference information
921 public function get_reference_details() {
922 return $this->repository
->get_reference_details($this->get_reference(), $this->get_status());
926 * Called after reference-file has been synchronized with the repository
928 * We update contenthash, filesize and status in files table if changed
929 * and we always update lastsync in files_reference table
931 * @param string $contenthash
932 * @param int $filesize
934 * @param int $lifetime the life time of this synchronisation results
936 public function set_synchronized($contenthash, $filesize, $status = 0, $lifetime = null) {
938 if (!$this->is_external_file()) {
942 if ($contenthash != $this->file_record
->contenthash
) {
943 $oldcontenthash = $this->file_record
->contenthash
;
945 if ($lifetime === null) {
946 $lifetime = $this->file_record
->referencelifetime
;
948 // this will update all entries in {files} that have the same filereference id
949 $this->fs
->update_references($this->file_record
->referencefileid
, $now, $lifetime, $contenthash, $filesize, $status);
950 // we don't need to call update() for this object, just set the values of changed fields
951 $this->file_record
->contenthash
= $contenthash;
952 $this->file_record
->filesize
= $filesize;
953 $this->file_record
->status
= $status;
954 $this->file_record
->referencelastsync
= $now;
955 $this->file_record
->referencelifetime
= $lifetime;
956 if (isset($oldcontenthash)) {
957 $this->fs
->deleted_file_cleanup($oldcontenthash);
962 * Sets the error status for a file that could not be synchronised
964 * @param int $lifetime the life time of this synchronisation results
966 public function set_missingsource($lifetime = null) {
967 $this->set_synchronized($this->get_contenthash(), $this->get_filesize(), 666, $lifetime);
971 * Send file references
973 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
974 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
975 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
976 * @param array $options additional options affecting the file serving
978 public function send_file($lifetime, $filter, $forcedownload, $options) {
979 $this->repository
->send_file($this, $lifetime, $filter, $forcedownload, $options);
983 * Imports the contents of an external file into moodle filepool.
985 * @throws moodle_exception if file could not be downloaded or is too big
986 * @param int $maxbytes throw an exception if file size is bigger than $maxbytes (0 means no limit)
988 public function import_external_file_contents($maxbytes = 0) {
989 if ($this->repository
) {
990 $this->repository
->import_external_file_contents($this, $maxbytes);