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();
28 require_once($CFG->dirroot . '/lib/filestorage/file_progress.php');
31 * Class representing local files stored in a sha1 file pool.
33 * Since Moodle 2.0 file contents are stored in sha1 pool and
34 * all other file information is stored in new "files" database table.
38 * @copyright 2008 Petr Skoda {@link http://skodak.org}
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 /** @var file_storage file storage pool instance */
45 /** @var stdClass record from the files table left join files_reference table */
47 /** @var string location of content files */
49 /** @var repository repository plugin instance */
53 * Constructor, this constructor should be called ONLY from the file_storage class!
55 * @param file_storage $fs file storage instance
56 * @param stdClass $file_record description of file
57 * @param string $filedir location of file directory with sh1 named content files
59 public function __construct(file_storage $fs, stdClass $file_record, $filedir) {
62 $this->file_record = clone($file_record); // prevent modifications
63 $this->filedir = $filedir; // keep secret, do not expose!
65 if (!empty($file_record->repositoryid)) {
66 require_once("$CFG->dirroot/repository/lib.php");
67 $this->repository = repository::get_repository_by_id($file_record->repositoryid, SYSCONTEXTID);
68 if ($this->repository->supported_returntypes() & FILE_REFERENCE != FILE_REFERENCE) {
69 // Repository cannot do file reference.
70 throw new moodle_exception('error');
73 $this->repository = null;
75 // make sure all reference fields exist in file_record even when it is not a reference
76 foreach (array('referencelastsync', 'referencelifetime', 'referencefileid', 'reference', 'repositoryid') as $key) {
77 if (empty($this->file_record->$key)) {
78 $this->file_record->$key = null;
84 * Whether or not this is a external resource
88 public function is_external_file() {
89 return !empty($this->repository);
93 * Update some file record fields
94 * NOTE: Must remain protected
96 * @param stdClass $dataobject
98 protected function update($dataobject) {
100 $keys = array_keys((array)$this->file_record);
101 foreach ($dataobject as $field => $value) {
102 if (in_array($field, $keys)) {
103 if ($field == 'contextid' and (!is_number($value) or $value < 1)) {
104 throw new file_exception('storedfileproblem', 'Invalid contextid');
107 if ($field == 'component') {
108 $value = clean_param($value, PARAM_COMPONENT);
110 throw new file_exception('storedfileproblem', 'Invalid component');
114 if ($field == 'filearea') {
115 $value = clean_param($value, PARAM_AREA);
117 throw new file_exception('storedfileproblem', 'Invalid filearea');
121 if ($field == 'itemid' and (!is_number($value) or $value < 0)) {
122 throw new file_exception('storedfileproblem', 'Invalid itemid');
126 if ($field == 'filepath') {
127 $value = clean_param($value, PARAM_PATH);
128 if (strpos($value, '/') !== 0 or strrpos($value, '/') !== strlen($value)-1) {
129 // path must start and end with '/'
130 throw new file_exception('storedfileproblem', 'Invalid file path');
134 if ($field == 'filename') {
135 // folder has filename == '.', so we pass this
137 $value = clean_param($value, PARAM_FILE);
140 throw new file_exception('storedfileproblem', 'Invalid file name');
144 if ($field === 'timecreated' or $field === 'timemodified') {
145 if (!is_number($value)) {
146 throw new file_exception('storedfileproblem', 'Invalid timestamp');
153 if ($field === 'referencefileid') {
154 if (!is_null($value) and !is_number($value)) {
155 throw new file_exception('storedfileproblem', 'Invalid reference info');
159 if ($field === 'referencelastsync' or $field === 'referencelifetime') {
160 // do not update those fields
161 // TODO MDL-33416 [2.4] fields referencelastsync and referencelifetime to be removed from {files} table completely
166 $this->file_record->$field = $value;
168 throw new coding_exception("Invalid field name, $field doesn't exist in file record");
171 // Validate mimetype field
172 // we don't use {@link stored_file::get_content_file_location()} here becaues it will try to update file_record
173 $pathname = $this->get_pathname_by_contenthash();
174 // try to recover the content from trash
175 if (!is_readable($pathname)) {
176 if (!$this->fs->try_content_recovery($this) or !is_readable($pathname)) {
177 throw new file_exception('storedfilecannotread', '', $pathname);
180 $mimetype = $this->fs->mimetype($pathname, $this->file_record->filename);
181 $this->file_record->mimetype = $mimetype;
183 $DB->update_record('files', $this->file_record);
189 * @param string $filepath file path
190 * @param string $filename file name
192 public function rename($filepath, $filename) {
193 if ($this->fs->file_exists($this->get_contextid(), $this->get_component(), $this->get_filearea(), $this->get_itemid(), $filepath, $filename)) {
194 throw new file_exception('storedfilenotcreated', '', 'file exists, cannot rename');
196 $filerecord = new stdClass;
197 $filerecord->filepath = $filepath;
198 $filerecord->filename = $filename;
199 // populate the pathname hash
200 $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);
201 $this->update($filerecord);
205 * Replace the content by providing another stored_file instance
207 * @param stored_file $storedfile
209 public function replace_content_with(stored_file $storedfile) {
210 $contenthash = $storedfile->get_contenthash();
211 $this->set_contenthash($contenthash);
212 $this->set_filesize($storedfile->get_filesize());
216 * Replaces the fields that might have changed when file was overriden in filepicker:
217 * reference, contenthash, filesize, userid
219 * Note that field 'source' must be updated separately because
220 * it has different format for draft and non-draft areas and
221 * this function will usually be used to replace non-draft area
222 * file with draft area file.
224 * @param stored_file $newfile
225 * @throws coding_exception
227 public function replace_file_with(stored_file $newfile) {
228 if ($newfile->get_referencefileid() &&
229 $this->fs->get_references_count_by_storedfile($this)) {
230 // The new file is a reference.
231 // The current file has other local files referencing to it.
232 // Double reference is not allowed.
233 throw new moodle_exception('errordoublereference', 'repository');
236 $filerecord = new stdClass;
237 $contenthash = $newfile->get_contenthash();
238 if ($this->fs->content_exists($contenthash)) {
239 $filerecord->contenthash = $contenthash;
241 throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
243 $filerecord->filesize = $newfile->get_filesize();
244 $filerecord->referencefileid = $newfile->get_referencefileid();
245 $filerecord->userid = $newfile->get_userid();
246 $this->update($filerecord);
250 * Unlink the stored file from the referenced file
252 * This methods destroys the link to the record in files_reference table. This effectively
253 * turns the stored file from being an alias to a plain copy. However, the caller has
254 * to make sure that the actual file's content has beed synced prior to calling this method.
256 public function delete_reference() {
259 if (!$this->is_external_file()) {
260 throw new coding_exception('An attempt to unlink a non-reference file.');
263 $transaction = $DB->start_delegated_transaction();
265 // Are we the only one referring to the original file? If so, delete the
266 // referenced file record. Note we do not use file_storage::search_references_count()
267 // here because we want to count draft files too and we are at a bit lower access level here.
268 $countlinks = $DB->count_records('files',
269 array('referencefileid' => $this->file_record->referencefileid));
270 if ($countlinks == 1) {
271 $DB->delete_records('files_reference', array('id' => $this->file_record->referencefileid));
274 // Update the underlying record in the database.
275 $update = new stdClass();
276 $update->referencefileid = null;
277 $this->update($update);
279 $transaction->allow_commit();
281 // Update our properties and the record in the memory.
282 $this->repository = null;
283 $this->file_record->repositoryid = null;
284 $this->file_record->reference = null;
285 $this->file_record->referencefileid = null;
286 $this->file_record->referencelastsync = null;
287 $this->file_record->referencelifetime = null;
291 * Is this a directory?
293 * Directories are only emulated, internally they are stored as empty
294 * files with a "." instead of name - this means empty directory contains
295 * exactly one empty file with name dot.
297 * @return bool true means directory, false means file
299 public function is_directory() {
300 return ($this->file_record->filename === '.');
304 * Delete file from files table.
306 * The content of files stored in sha1 pool is reclaimed
307 * later - the occupied disk space is reclaimed much later.
309 * @return bool always true or exception if error occurred
311 public function delete() {
314 if ($this->is_directory()) {
315 // Directories can not be referenced, just delete the record.
316 $DB->delete_records('files', array('id'=>$this->file_record->id));
319 $transaction = $DB->start_delegated_transaction();
321 // If there are other files referring to this file, convert them to copies.
322 if ($files = $this->fs->get_references_by_storedfile($this)) {
323 foreach ($files as $file) {
324 $this->fs->import_external_file($file);
328 // If this file is a reference (alias) to another file, unlink it first.
329 if ($this->is_external_file()) {
330 $this->delete_reference();
333 // Now delete the file record.
334 $DB->delete_records('files', array('id'=>$this->file_record->id));
336 $transaction->allow_commit();
339 // Move pool file to trash if content not needed any more.
340 $this->fs->deleted_file_cleanup($this->file_record->contenthash);
341 return true; // BC only
345 * Get file pathname by contenthash
347 * NOTE, this function is not calling sync_external_file, it assume the contenthash is current
348 * Protected - developers must not gain direct access to this function.
350 * @return string full path to pool file with file content
352 protected function get_pathname_by_contenthash() {
353 // Detect is local file or not.
354 $contenthash = $this->file_record->contenthash;
355 $l1 = $contenthash[0].$contenthash[1];
356 $l2 = $contenthash[2].$contenthash[3];
357 return "$this->filedir/$l1/$l2/$contenthash";
361 * Get file pathname by given contenthash, this method will try to sync files
363 * Protected - developers must not gain direct access to this function.
365 * NOTE: do not make this public, we must not modify or delete the pool files directly! ;-)
367 * @return string full path to pool file with file content
369 protected function get_content_file_location() {
370 $this->sync_external_file();
371 return $this->get_pathname_by_contenthash();
375 * adds this file path to a curl request (POST only)
377 * @param curl $curlrequest the curl request object
378 * @param string $key what key to use in the POST request
381 public function add_to_curl_request(&$curlrequest, $key) {
382 $curlrequest->_tmp_file_post_params[$key] = '@' . $this->get_content_file_location();
386 * Returns file handle - read only mode, no writing allowed into pool files!
388 * When you want to modify a file, create a new file and delete the old one.
390 * @return resource file handle
392 public function get_content_file_handle() {
393 $path = $this->get_content_file_location();
394 if (!is_readable($path)) {
395 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
396 throw new file_exception('storedfilecannotread', '', $path);
399 return fopen($path, 'rb'); // Binary reading only!!
403 * Dumps file content to page.
405 public function readfile() {
406 $path = $this->get_content_file_location();
407 if (!is_readable($path)) {
408 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
409 throw new file_exception('storedfilecannotread', '', $path);
412 readfile_allow_large($path, $this->get_filesize());
416 * Returns file content as string.
418 * @return string content
420 public function get_content() {
421 $path = $this->get_content_file_location();
422 if (!is_readable($path)) {
423 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
424 throw new file_exception('storedfilecannotread', '', $path);
427 return file_get_contents($this->get_content_file_location());
431 * Copy content of file to given pathname.
433 * @param string $pathname real path to the new file
434 * @return bool success
436 public function copy_content_to($pathname) {
437 $path = $this->get_content_file_location();
438 if (!is_readable($path)) {
439 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
440 throw new file_exception('storedfilecannotread', '', $path);
443 return copy($path, $pathname);
447 * Copy content of file to temporary folder and returns file path
449 * @param string $dir name of the temporary directory
450 * @param string $fileprefix prefix of temporary file.
451 * @return string|bool path of temporary file or false.
453 public function copy_content_to_temp($dir = 'files', $fileprefix = 'tempup_') {
455 if (!$dir = make_temp_directory($dir)) {
458 if (!$tempfile = tempnam($dir, $fileprefix)) {
461 if (!$this->copy_content_to($tempfile)) {
462 // something went wrong
470 * List contents of archive.
472 * @param file_packer $packer file packer instance
473 * @return array of file infos
475 public function list_files(file_packer $packer) {
476 $archivefile = $this->get_content_file_location();
477 return $packer->list_files($archivefile);
481 * Extract file to given file path (real OS filesystem), existing files are overwritten.
483 * @param file_packer $packer file packer instance
484 * @param string $pathname target directory
485 * @param file_progress $progress Progress indicator callback or null if not required
486 * @return array|bool list of processed files; false if error
488 public function extract_to_pathname(file_packer $packer, $pathname,
489 file_progress $progress = null) {
490 $archivefile = $this->get_content_file_location();
491 return $packer->extract_to_pathname($archivefile, $pathname, null, $progress);
495 * Extract file to given file path (real OS filesystem), existing files are overwritten.
497 * @param file_packer $packer file packer instance
498 * @param int $contextid context ID
499 * @param string $component component
500 * @param string $filearea file area
501 * @param int $itemid item ID
502 * @param string $pathbase path base
503 * @param int $userid user ID
504 * @param file_progress $progress Progress indicator callback or null if not required
505 * @return array|bool list of processed files; false if error
507 public function extract_to_storage(file_packer $packer, $contextid,
508 $component, $filearea, $itemid, $pathbase, $userid = null, file_progress $progress = null) {
509 $archivefile = $this->get_content_file_location();
510 return $packer->extract_to_storage($archivefile, $contextid,
511 $component, $filearea, $itemid, $pathbase, $userid, $progress);
515 * Add file/directory into archive.
517 * @param file_archive $filearch file archive instance
518 * @param string $archivepath pathname in archive
519 * @return bool success
521 public function archive_file(file_archive $filearch, $archivepath) {
522 if ($this->is_directory()) {
523 return $filearch->add_directory($archivepath);
525 $path = $this->get_content_file_location();
526 if (!is_readable($path)) {
529 return $filearch->add_file_from_pathname($archivepath, $path);
534 * Returns information about image,
535 * information is determined from the file content
537 * @return mixed array with width, height and mimetype; false if not an image
539 public function get_imageinfo() {
540 $path = $this->get_content_file_location();
541 if (!is_readable($path)) {
542 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
543 throw new file_exception('storedfilecannotread', '', $path);
546 $mimetype = $this->get_mimetype();
547 if (!preg_match('|^image/|', $mimetype) || !filesize($path) || !($imageinfo = getimagesize($path))) {
550 $image = array('width'=>$imageinfo[0], 'height'=>$imageinfo[1], 'mimetype'=>image_type_to_mime_type($imageinfo[2]));
551 if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) {
552 // gd can not parse it, sorry
559 * Verifies the file is a valid web image - gif, png and jpeg only.
561 * It should be ok to serve this image from server without any other security workarounds.
563 * @return bool true if file ok
565 public function is_valid_image() {
566 $mimetype = $this->get_mimetype();
567 if (!file_mimetype_in_typegroup($mimetype, 'web_image')) {
570 if (!$info = $this->get_imageinfo()) {
573 if ($info['mimetype'] !== $mimetype) {
576 // ok, GD likes this image
581 * Returns parent directory, creates missing parents if needed.
583 * @return stored_file
585 public function get_parent_directory() {
586 if ($this->file_record->filepath === '/' and $this->file_record->filename === '.') {
587 //root dir does not have parent
591 if ($this->file_record->filename !== '.') {
592 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);
595 $filepath = $this->file_record->filepath;
596 $filepath = trim($filepath, '/');
597 $dirs = explode('/', $filepath);
599 $filepath = implode('/', $dirs);
600 $filepath = ($filepath === '') ? '/' : "/$filepath/";
602 return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath);
606 * Synchronize file if it is a reference and needs synchronizing
608 * Updates contenthash and filesize
610 public function sync_external_file() {
612 if (!empty($this->file_record->referencefileid)) {
613 require_once($CFG->dirroot.'/repository/lib.php');
614 repository::sync_external_file($this);
619 * Returns context id of the file
621 * @return int context id
623 public function get_contextid() {
624 return $this->file_record->contextid;
628 * Returns component name - this is the owner of the areas,
629 * nothing else is allowed to read or modify the files directly!!
633 public function get_component() {
634 return $this->file_record->component;
638 * Returns file area name, this divides files of one component into groups with different access control.
639 * All files in one area have the same access control.
643 public function get_filearea() {
644 return $this->file_record->filearea;
648 * Returns returns item id of file.
652 public function get_itemid() {
653 return $this->file_record->itemid;
657 * Returns file path - starts and ends with /, \ are not allowed.
661 public function get_filepath() {
662 return $this->file_record->filepath;
666 * Returns file name or '.' in case of directories.
670 public function get_filename() {
671 return $this->file_record->filename;
675 * Returns id of user who created the file.
679 public function get_userid() {
680 return $this->file_record->userid;
684 * Returns the size of file in bytes.
688 public function get_filesize() {
689 $this->sync_external_file();
690 return $this->file_record->filesize;
694 * Returns the size of file in bytes.
696 * @param int $filesize bytes
698 public function set_filesize($filesize) {
699 $filerecord = new stdClass;
700 $filerecord->filesize = $filesize;
701 $this->update($filerecord);
705 * Returns mime type of file.
709 public function get_mimetype() {
710 return $this->file_record->mimetype;
714 * Returns unix timestamp of file creation date.
718 public function get_timecreated() {
719 return $this->file_record->timecreated;
723 * Returns unix timestamp of last file modification.
727 public function get_timemodified() {
728 $this->sync_external_file();
729 return $this->file_record->timemodified;
735 * @param int $timemodified
737 public function set_timemodified($timemodified) {
738 $filerecord = new stdClass;
739 $filerecord->timemodified = $timemodified;
740 $this->update($filerecord);
744 * Returns file status flag.
746 * @return int 0 means file OK, anything else is a problem and file can not be used
748 public function get_status() {
749 return $this->file_record->status;
757 public function get_id() {
758 return $this->file_record->id;
762 * Returns sha1 hash of file content.
766 public function get_contenthash() {
767 $this->sync_external_file();
768 return $this->file_record->contenthash;
774 * @param string $contenthash
776 protected function set_contenthash($contenthash) {
777 // make sure the content exists in moodle file pool
778 if ($this->fs->content_exists($contenthash)) {
779 $filerecord = new stdClass;
780 $filerecord->contenthash = $contenthash;
781 $this->update($filerecord);
783 throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
788 * Returns sha1 hash of all file path components sha1("contextid/component/filearea/itemid/dir/dir/filename.ext").
792 public function get_pathnamehash() {
793 return $this->file_record->pathnamehash;
797 * Returns the license type of the file, it is a short name referred from license table.
801 public function get_license() {
802 return $this->file_record->license;
808 * @param string $license license
810 public function set_license($license) {
811 $filerecord = new stdClass;
812 $filerecord->license = $license;
813 $this->update($filerecord);
817 * Returns the author name of the file.
821 public function get_author() {
822 return $this->file_record->author;
828 * @param string $author
830 public function set_author($author) {
831 $filerecord = new stdClass;
832 $filerecord->author = $author;
833 $this->update($filerecord);
837 * Returns the source of the file, usually it is a url.
841 public function get_source() {
842 return $this->file_record->source;
848 * @param string $license license
850 public function set_source($source) {
851 $filerecord = new stdClass;
852 $filerecord->source = $source;
853 $this->update($filerecord);
858 * Returns the sort order of file
862 public function get_sortorder() {
863 return $this->file_record->sortorder;
867 * Set file sort order
869 * @param int $sortorder
872 public function set_sortorder($sortorder) {
873 $filerecord = new stdClass;
874 $filerecord->sortorder = $sortorder;
875 $this->update($filerecord);
879 * Returns repository id
883 public function get_repository_id() {
884 if (!empty($this->repository)) {
885 return $this->repository->id;
892 * get reference file id
895 public function get_referencefileid() {
896 return $this->file_record->referencefileid;
900 * Get reference last sync time
903 public function get_referencelastsync() {
904 return $this->file_record->referencelastsync;
908 * Get reference last sync time
911 public function get_referencelifetime() {
912 return $this->file_record->referencelifetime;
915 * Returns file reference
919 public function get_reference() {
920 return $this->file_record->reference;
924 * Get human readable file reference information
928 public function get_reference_details() {
929 return $this->repository->get_reference_details($this->get_reference(), $this->get_status());
933 * Called after reference-file has been synchronized with the repository
935 * We update contenthash, filesize and status in files table if changed
936 * and we always update lastsync in files_reference table
938 * @param string $contenthash
939 * @param int $filesize
941 * @param int $lifetime the life time of this synchronisation results
943 public function set_synchronized($contenthash, $filesize, $status = 0, $lifetime = null) {
945 if (!$this->is_external_file()) {
949 if ($contenthash != $this->file_record->contenthash) {
950 $oldcontenthash = $this->file_record->contenthash;
952 if ($lifetime === null) {
953 $lifetime = $this->file_record->referencelifetime;
955 // this will update all entries in {files} that have the same filereference id
956 $this->fs->update_references($this->file_record->referencefileid, $now, $lifetime, $contenthash, $filesize, $status);
957 // we don't need to call update() for this object, just set the values of changed fields
958 $this->file_record->contenthash = $contenthash;
959 $this->file_record->filesize = $filesize;
960 $this->file_record->status = $status;
961 $this->file_record->referencelastsync = $now;
962 $this->file_record->referencelifetime = $lifetime;
963 if (isset($oldcontenthash)) {
964 $this->fs->deleted_file_cleanup($oldcontenthash);
969 * Sets the error status for a file that could not be synchronised
971 * @param int $lifetime the life time of this synchronisation results
973 public function set_missingsource($lifetime = null) {
974 $this->set_synchronized($this->get_contenthash(), $this->get_filesize(), 666, $lifetime);
978 * Send file references
980 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
981 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
982 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
983 * @param array $options additional options affecting the file serving
985 public function send_file($lifetime, $filter, $forcedownload, $options) {
986 $this->repository->send_file($this, $lifetime, $filter, $forcedownload, $options);
990 * Imports the contents of an external file into moodle filepool.
992 * @throws moodle_exception if file could not be downloaded or is too big
993 * @param int $maxbytes throw an exception if file size is bigger than $maxbytes (0 means no limit)
995 public function import_external_file_contents($maxbytes = 0) {
996 if ($this->repository) {
997 $this->repository->import_external_file_contents($this, $maxbytes);