MDL-34290 repository_filesystem add original info function and reduce ref lifetime
[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
172dd12c 28/**
8496fdac
PS
29 * Class representing local files stored in a sha1 file pool.
30 *
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.
33 *
d2b7803e
DC
34 * @package core_files
35 * @category files
8496fdac
PS
36 * @copyright 2008 Petr Skoda {@link http://skodak.org}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 2.0
172dd12c 39 */
40class stored_file {
8496fdac 41 /** @var file_storage file storage pool instance */
172dd12c 42 private $fs;
04e3b007 43 /** @var stdClass record from the files table left join files_reference table */
172dd12c 44 private $file_record;
693ef3a8
PS
45 /** @var string location of content files */
46 private $filedir;
67233725 47 /** @var repository repository plugin instance */
291a3d1f 48 private $repository;
172dd12c 49
50 /**
8496fdac
PS
51 * Constructor, this constructor should be called ONLY from the file_storage class!
52 *
53 * @param file_storage $fs file storage instance
d2b7803e
DC
54 * @param stdClass $file_record description of file
55 * @param string $filedir location of file directory with sh1 named content files
172dd12c 56 */
693ef3a8 57 public function __construct(file_storage $fs, stdClass $file_record, $filedir) {
67233725 58 global $DB, $CFG;
8496fdac
PS
59 $this->fs = $fs;
60 $this->file_record = clone($file_record); // prevent modifications
693ef3a8 61 $this->filedir = $filedir; // keep secret, do not expose!
67233725
DC
62
63 if (!empty($file_record->repositoryid)) {
b99065be 64 require_once("$CFG->dirroot/repository/lib.php");
67233725
DC
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');
69 }
70 } else {
71 $this->repository = null;
72 }
73 }
74
75 /**
76 * Whether or not this is a external resource
77 *
78 * @return bool
79 */
80 public function is_external_file() {
81 return !empty($this->repository);
82 }
83
84 /**
85 * Update some file record fields
86 * NOTE: Must remain protected
87 *
88 * @param stdClass $dataobject
89 */
90 protected function update($dataobject) {
91 global $DB;
92 $keys = array_keys((array)$this->file_record);
93 foreach ($dataobject as $field => $value) {
94 if (in_array($field, $keys)) {
95 if ($field == 'contextid' and (!is_number($value) or $value < 1)) {
96 throw new file_exception('storedfileproblem', 'Invalid contextid');
97 }
98
99 if ($field == 'component') {
100 $value = clean_param($value, PARAM_COMPONENT);
101 if (empty($value)) {
102 throw new file_exception('storedfileproblem', 'Invalid component');
103 }
104 }
105
106 if ($field == 'filearea') {
107 $value = clean_param($value, PARAM_AREA);
108 if (empty($value)) {
109 throw new file_exception('storedfileproblem', 'Invalid filearea');
110 }
111 }
112
113 if ($field == 'itemid' and (!is_number($value) or $value < 0)) {
114 throw new file_exception('storedfileproblem', 'Invalid itemid');
115 }
116
117
118 if ($field == 'filepath') {
119 $value = clean_param($value, PARAM_PATH);
120 if (strpos($value, '/') !== 0 or strrpos($value, '/') !== strlen($value)-1) {
121 // path must start and end with '/'
122 throw new file_exception('storedfileproblem', 'Invalid file path');
123 }
124 }
125
126 if ($field == 'filename') {
fc4e8034
DC
127 // folder has filename == '.', so we pass this
128 if ($value != '.') {
129 $value = clean_param($value, PARAM_FILE);
130 }
67233725
DC
131 if ($value === '') {
132 throw new file_exception('storedfileproblem', 'Invalid file name');
133 }
134 }
135
136 if ($field === 'timecreated' or $field === 'timemodified') {
137 if (!is_number($value)) {
138 throw new file_exception('storedfileproblem', 'Invalid timestamp');
139 }
140 if ($value < 0) {
141 $value = 0;
142 }
143 }
144
1c0da849
DM
145 if ($field === 'referencefileid' or $field === 'referencelastsync' or $field === 'referencelifetime') {
146 if (!is_null($value) and !is_number($value)) {
147 throw new file_exception('storedfileproblem', 'Invalid reference info');
148 }
67233725
DC
149 }
150
151 // adding the field
152 $this->file_record->$field = $value;
153 } else {
154 throw new coding_exception("Invalid field name, $field doesn't exist in file record");
155 }
156 }
8177b7b9 157 // Validate mimetype field
70c1bc84
DC
158 // we don't use {@link stored_file::get_content_file_location()} here becaues it will try to update file_record
159 $pathname = $this->get_pathname_by_contenthash();
8177b7b9
DC
160 // try to recover the content from trash
161 if (!is_readable($pathname)) {
162 if (!$this->fs->try_content_recovery($this) or !is_readable($pathname)) {
163 throw new file_exception('storedfilecannotread', '', $pathname);
164 }
165 }
efa28f30 166 $mimetype = $this->fs->mimetype($pathname, $this->file_record->filename);
8177b7b9
DC
167 $this->file_record->mimetype = $mimetype;
168
67233725
DC
169 $DB->update_record('files', $this->file_record);
170 }
171
172 /**
173 * Rename filename
174 *
175 * @param string $filepath file path
176 * @param string $filename file name
177 */
178 public function rename($filepath, $filename) {
7051415c
DC
179 if ($this->fs->file_exists($this->get_contextid(), $this->get_component(), $this->get_filearea(), $this->get_itemid(), $filepath, $filename)) {
180 throw new file_exception('storedfilenotcreated', '', 'file exists, cannot rename');
181 }
67233725
DC
182 $filerecord = new stdClass;
183 $filerecord->filepath = $filepath;
184 $filerecord->filename = $filename;
185 // populate the pathname hash
186 $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);
187 $this->update($filerecord);
188 }
189
190 /**
191 * Replace the content by providing another stored_file instance
192 *
193 * @param stored_file $storedfile
194 */
195 public function replace_content_with(stored_file $storedfile) {
196 $contenthash = $storedfile->get_contenthash();
197 $this->set_contenthash($contenthash);
14b7e500 198 $this->set_filesize($storedfile->get_filesize());
67233725
DC
199 }
200
201 /**
2fa8aea2 202 * Unlink the stored file from the referenced file
67233725 203 *
2fa8aea2
DM
204 * This methods destroys the link to the record in files_reference table. This effectively
205 * turns the stored file from being an alias to a plain copy. However, the caller has
206 * to make sure that the actual file's content has beed synced prior to calling this method.
67233725
DC
207 */
208 public function delete_reference() {
209 global $DB;
61506a0a 210
2fa8aea2
DM
211 if (!$this->is_external_file()) {
212 throw new coding_exception('An attempt to unlink a non-reference file.');
213 }
214
e3c02118
DC
215 $transaction = $DB->start_delegated_transaction();
216
1c0da849
DM
217 // Are we the only one referring to the original file? If so, delete the
218 // referenced file record. Note we do not use file_storage::search_references_count()
219 // here because we want to count draft files too and we are at a bit lower access level here.
220 $countlinks = $DB->count_records('files',
221 array('referencefileid' => $this->file_record->referencefileid));
222 if ($countlinks == 1) {
223 $DB->delete_records('files_reference', array('id' => $this->file_record->referencefileid));
224 }
61506a0a 225
1c0da849
DM
226 // Update the underlying record in the database.
227 $update = new stdClass();
228 $update->referencefileid = null;
229 $update->referencelastsync = null;
230 $update->referencelifetime = null;
231 $this->update($update);
61506a0a 232
e3c02118
DC
233 $transaction->allow_commit();
234
1c0da849
DM
235 // Update our properties and the record in the memory.
236 $this->repository = null;
237 $this->file_record->repositoryid = null;
238 $this->file_record->reference = null;
239 $this->file_record->referencefileid = null;
240 $this->file_record->referencelastsync = null;
241 $this->file_record->referencelifetime = null;
172dd12c 242 }
243
244 /**
245 * Is this a directory?
8496fdac
PS
246 *
247 * Directories are only emulated, internally they are stored as empty
248 * files with a "." instead of name - this means empty directory contains
249 * exactly one empty file with name dot.
250 *
251 * @return bool true means directory, false means file
172dd12c 252 */
253 public function is_directory() {
8496fdac 254 return ($this->file_record->filename === '.');
172dd12c 255 }
256
257 /**
8496fdac
PS
258 * Delete file from files table.
259 *
260 * The content of files stored in sha1 pool is reclaimed
261 * later - the occupied disk space is reclaimed much later.
262 *
263 * @return bool always true or exception if error occurred
172dd12c 264 */
265 public function delete() {
266 global $DB;
e3c02118
DC
267
268 $transaction = $DB->start_delegated_transaction();
269
3b2465fe 270 // If there are other files referring to this file, convert them to copies.
67233725
DC
271 if ($files = $this->fs->get_references_by_storedfile($this)) {
272 foreach ($files as $file) {
273 $this->fs->import_external_file($file);
274 }
275 }
3b2465fe
DM
276
277 // If this file is a reference (alias) to another file, unlink it first.
278 if ($this->is_external_file()) {
279 $this->delete_reference();
280 }
281
282 // Now delete the file record.
1aa01caf 283 $DB->delete_records('files', array('id'=>$this->file_record->id));
e3c02118
DC
284
285 $transaction->allow_commit();
286
1aa01caf 287 // moves pool file to trash if content not needed any more
288 $this->fs->deleted_file_cleanup($this->file_record->contenthash);
8496fdac 289 return true; // BC only
172dd12c 290 }
291
292 /**
70c1bc84 293 * Get file pathname by contenthash
8496fdac 294 *
70c1bc84
DC
295 * NOTE, this function is not calling sync_external_file, it assume the contenthash is current
296 * Protected - developers must not gain direct access to this function.
8496fdac
PS
297 *
298 * @return string full path to pool file with file content
70c1bc84
DC
299 */
300 protected function get_pathname_by_contenthash() {
67233725 301 // Detect is local file or not.
17d9269f 302 $contenthash = $this->file_record->contenthash;
303 $l1 = $contenthash[0].$contenthash[1];
304 $l2 = $contenthash[2].$contenthash[3];
693ef3a8 305 return "$this->filedir/$l1/$l2/$contenthash";
172dd12c 306 }
307
70c1bc84
DC
308 /**
309 * Get file pathname by given contenthash, this method will try to sync files
310 *
311 * Protected - developers must not gain direct access to this function.
312 *
313 * NOTE: do not make this public, we must not modify or delete the pool files directly! ;-)
314 *
315 * @return string full path to pool file with file content
316 **/
317 protected function get_content_file_location() {
318 $this->sync_external_file();
319 return $this->get_pathname_by_contenthash();
320 }
321
5035a8b4 322 /**
323 * adds this file path to a curl request (POST only)
324 *
325 * @param curl $curlrequest the curl request object
326 * @param string $key what key to use in the POST request
8496fdac 327 * @return void
5035a8b4 328 */
329 public function add_to_curl_request(&$curlrequest, $key) {
330 $curlrequest->_tmp_file_post_params[$key] = '@' . $this->get_content_file_location();
331 }
332
172dd12c 333 /**
334 * Returns file handle - read only mode, no writing allowed into pool files!
8496fdac
PS
335 *
336 * When you want to modify a file, create a new file and delete the old one.
337 *
338 * @return resource file handle
172dd12c 339 */
340 public function get_content_file_handle() {
341 $path = $this->get_content_file_location();
342 if (!is_readable($path)) {
1aa01caf 343 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
d610cb89 344 throw new file_exception('storedfilecannotread', '', $path);
1aa01caf 345 }
172dd12c 346 }
67233725 347 return fopen($path, 'rb'); // Binary reading only!!
172dd12c 348 }
349
350 /**
8496fdac 351 * Dumps file content to page.
172dd12c 352 */
353 public function readfile() {
354 $path = $this->get_content_file_location();
355 if (!is_readable($path)) {
1aa01caf 356 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
d610cb89 357 throw new file_exception('storedfilecannotread', '', $path);
1aa01caf 358 }
172dd12c 359 }
360 readfile($path);
361 }
362
363 /**
8496fdac
PS
364 * Returns file content as string.
365 *
172dd12c 366 * @return string content
367 */
368 public function get_content() {
369 $path = $this->get_content_file_location();
370 if (!is_readable($path)) {
1aa01caf 371 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
d610cb89 372 throw new file_exception('storedfilecannotread', '', $path);
1aa01caf 373 }
172dd12c 374 }
375 return file_get_contents($this->get_content_file_location());
376 }
377
6c0e2d08 378 /**
8496fdac
PS
379 * Copy content of file to given pathname.
380 *
381 * @param string $pathname real path to the new file
6c0e2d08 382 * @return bool success
383 */
384 public function copy_content_to($pathname) {
385 $path = $this->get_content_file_location();
386 if (!is_readable($path)) {
1aa01caf 387 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
d610cb89 388 throw new file_exception('storedfilecannotread', '', $path);
1aa01caf 389 }
6c0e2d08 390 }
391 return copy($path, $pathname);
392 }
393
4e782b32
RT
394 /**
395 * Copy content of file to temporary folder and returns file path
396 *
397 * @param string $dir name of the temporary directory
398 * @param string $fileprefix prefix of temporary file.
399 * @return string|bool path of temporary file or false.
400 */
401 public function copy_content_to_temp($dir = 'files', $fileprefix = 'tempup_') {
402 $tempfile = false;
403 if (!$dir = make_temp_directory($dir)) {
404 return false;
405 }
406 if (!$tempfile = tempnam($dir, $fileprefix)) {
407 return false;
408 }
409 if (!$this->copy_content_to($tempfile)) {
410 // something went wrong
411 @unlink($tempfile);
412 return false;
413 }
414 return $tempfile;
415 }
416
17d9269f 417 /**
8496fdac
PS
418 * List contents of archive.
419 *
d2b7803e 420 * @param file_packer $packer file packer instance
c78a0558 421 * @return array of file infos
422 */
423 public function list_files(file_packer $packer) {
424 $archivefile = $this->get_content_file_location();
425 return $packer->list_files($archivefile);
426 }
427
428 /**
8496fdac
PS
429 * Extract file to given file path (real OS filesystem), existing files are overwritten.
430 *
d2b7803e 431 * @param file_packer $packer file packer instance
0b0bfa93 432 * @param string $pathname target directory
8496fdac 433 * @return array|bool list of processed files; false if error
17d9269f 434 */
0b0bfa93 435 public function extract_to_pathname(file_packer $packer, $pathname) {
436 $archivefile = $this->get_content_file_location();
437 return $packer->extract_to_pathname($archivefile, $pathname);
17d9269f 438 }
439
440 /**
8496fdac
PS
441 * Extract file to given file path (real OS filesystem), existing files are overwritten.
442 *
d2b7803e
DC
443 * @param file_packer $packer file packer instance
444 * @param int $contextid context ID
445 * @param string $component component
446 * @param string $filearea file area
447 * @param int $itemid item ID
448 * @param string $pathbase path base
449 * @param int $userid user ID
8496fdac 450 * @return array|bool list of processed files; false if error
17d9269f 451 */
64f93798 452 public function extract_to_storage(file_packer $packer, $contextid, $component, $filearea, $itemid, $pathbase, $userid = NULL) {
0b0bfa93 453 $archivefile = $this->get_content_file_location();
64f93798 454 return $packer->extract_to_storage($archivefile, $contextid, $component, $filearea, $itemid, $pathbase);
17d9269f 455 }
456
b1897a6d 457 /**
8496fdac
PS
458 * Add file/directory into archive.
459 *
d2b7803e 460 * @param file_archive $filearch file archive instance
c78a0558 461 * @param string $archivepath pathname in archive
b1897a6d 462 * @return bool success
463 */
0b0bfa93 464 public function archive_file(file_archive $filearch, $archivepath) {
b1897a6d 465 if ($this->is_directory()) {
0b0bfa93 466 return $filearch->add_directory($archivepath);
b1897a6d 467 } else {
468 $path = $this->get_content_file_location();
469 if (!is_readable($path)) {
470 return false;
471 }
0b0bfa93 472 return $filearch->add_file_from_pathname($archivepath, $path);
b1897a6d 473 }
474 }
475
797f19e8 476 /**
477 * Returns information about image,
478 * information is determined from the file content
d2b7803e 479 *
797f19e8 480 * @return mixed array with width, height and mimetype; false if not an image
481 */
482 public function get_imageinfo() {
b7725e30
MG
483 $path = $this->get_content_file_location();
484 if (!is_readable($path)) {
485 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
486 throw new file_exception('storedfilecannotread', '', $path);
487 }
488 }
489 $mimetype = $this->get_mimetype();
490 if (!preg_match('|^image/|', $mimetype) || !filesize($path) || !($imageinfo = getimagesize($path))) {
797f19e8 491 return false;
492 }
493 $image = array('width'=>$imageinfo[0], 'height'=>$imageinfo[1], 'mimetype'=>image_type_to_mime_type($imageinfo[2]));
494 if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) {
495 // gd can not parse it, sorry
496 return false;
497 }
498 return $image;
499 }
500
501 /**
502 * Verifies the file is a valid web image - gif, png and jpeg only.
8496fdac 503 *
797f19e8 504 * It should be ok to serve this image from server without any other security workarounds.
8496fdac 505 *
797f19e8 506 * @return bool true if file ok
507 */
508 public function is_valid_image() {
509 $mimetype = $this->get_mimetype();
559276b1 510 if (!file_mimetype_in_typegroup($mimetype, 'web_image')) {
797f19e8 511 return false;
512 }
513 if (!$info = $this->get_imageinfo()) {
514 return false;
515 }
516 if ($info['mimetype'] !== $mimetype) {
517 return false;
518 }
519 // ok, GD likes this image
520 return true;
521 }
522
4b6b5ce7 523 /**
8496fdac
PS
524 * Returns parent directory, creates missing parents if needed.
525 *
526 * @return stored_file
4b6b5ce7 527 */
528 public function get_parent_directory() {
529 if ($this->file_record->filepath === '/' and $this->file_record->filename === '.') {
530 //root dir does not have parent
531 return null;
532 }
533
534 if ($this->file_record->filename !== '.') {
64f93798 535 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 536 }
537
538 $filepath = $this->file_record->filepath;
539 $filepath = trim($filepath, '/');
540 $dirs = explode('/', $filepath);
541 array_pop($dirs);
542 $filepath = implode('/', $dirs);
543 $filepath = ($filepath === '') ? '/' : "/$filepath/";
544
64f93798 545 return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath);
4b6b5ce7 546 }
547
16a95e8f 548 /**
0b2bfbd1 549 * Synchronize file if it is a reference and needs synchronizing
67233725 550 *
0b2bfbd1 551 * Updates contenthash and filesize
67233725
DC
552 */
553 public function sync_external_file() {
0b2bfbd1
MG
554 global $CFG;
555 if (!empty($this->file_record->referencefileid)) {
67233725 556 require_once($CFG->dirroot.'/repository/lib.php');
0b2bfbd1 557 repository::sync_external_file($this);
67233725 558 }
67233725
DC
559 }
560
561 /**
562 * Returns context id of the file
8496fdac 563 *
16a95e8f 564 * @return int context id
565 */
172dd12c 566 public function get_contextid() {
567 return $this->file_record->contextid;
568 }
569
16a95e8f 570 /**
64f93798
PS
571 * Returns component name - this is the owner of the areas,
572 * nothing else is allowed to read or modify the files directly!!
573 *
574 * @return string
575 */
576 public function get_component() {
577 return $this->file_record->component;
578 }
579
580 /**
581 * Returns file area name, this divides files of one component into groups with different access control.
582 * All files in one area have the same access control.
8496fdac 583 *
16a95e8f 584 * @return string
585 */
172dd12c 586 public function get_filearea() {
587 return $this->file_record->filearea;
588 }
589
16a95e8f 590 /**
8496fdac
PS
591 * Returns returns item id of file.
592 *
16a95e8f 593 * @return int
594 */
172dd12c 595 public function get_itemid() {
596 return $this->file_record->itemid;
597 }
598
16a95e8f 599 /**
600 * Returns file path - starts and ends with /, \ are not allowed.
8496fdac 601 *
16a95e8f 602 * @return string
603 */
172dd12c 604 public function get_filepath() {
605 return $this->file_record->filepath;
606 }
607
16a95e8f 608 /**
609 * Returns file name or '.' in case of directories.
8496fdac 610 *
16a95e8f 611 * @return string
612 */
172dd12c 613 public function get_filename() {
614 return $this->file_record->filename;
615 }
616
16a95e8f 617 /**
618 * Returns id of user who created the file.
8496fdac 619 *
16a95e8f 620 * @return int
621 */
172dd12c 622 public function get_userid() {
623 return $this->file_record->userid;
624 }
625
16a95e8f 626 /**
627 * Returns the size of file in bytes.
8496fdac 628 *
16a95e8f 629 * @return int bytes
630 */
172dd12c 631 public function get_filesize() {
67233725 632 $this->sync_external_file();
172dd12c 633 return $this->file_record->filesize;
634 }
635
61506a0a
DC
636 /**
637 * Returns the size of file in bytes.
638 *
639 * @param int $filesize bytes
640 */
641 public function set_filesize($filesize) {
642 $filerecord = new stdClass;
643 $filerecord->filesize = $filesize;
644 $this->update($filerecord);
645 }
646
16a95e8f 647 /**
8496fdac
PS
648 * Returns mime type of file.
649 *
16a95e8f 650 * @return string
651 */
172dd12c 652 public function get_mimetype() {
653 return $this->file_record->mimetype;
654 }
655
16a95e8f 656 /**
8496fdac
PS
657 * Returns unix timestamp of file creation date.
658 *
16a95e8f 659 * @return int
660 */
172dd12c 661 public function get_timecreated() {
662 return $this->file_record->timecreated;
663 }
664
16a95e8f 665 /**
8496fdac
PS
666 * Returns unix timestamp of last file modification.
667 *
16a95e8f 668 * @return int
669 */
172dd12c 670 public function get_timemodified() {
67233725 671 $this->sync_external_file();
172dd12c 672 return $this->file_record->timemodified;
673 }
6c0e2d08 674
67233725
DC
675 /**
676 * set timemodified
677 *
678 * @param int $timemodified
679 */
680 public function set_timemodified($timemodified) {
681 $filerecord = new stdClass;
682 $filerecord->timemodified = $timemodified;
683 $this->update($filerecord);
684 }
685
16a95e8f 686 /**
8496fdac
PS
687 * Returns file status flag.
688 *
16a95e8f 689 * @return int 0 means file OK, anything else is a problem and file can not be used
690 */
6c0e2d08 691 public function get_status() {
692 return $this->file_record->status;
693 }
ee03a651 694
16a95e8f 695 /**
8496fdac
PS
696 * Returns file id.
697 *
16a95e8f 698 * @return int
699 */
ee03a651 700 public function get_id() {
701 return $this->file_record->id;
702 }
4284e1cc 703
16a95e8f 704 /**
8496fdac
PS
705 * Returns sha1 hash of file content.
706 *
16a95e8f 707 * @return string
708 */
4284e1cc 709 public function get_contenthash() {
67233725 710 $this->sync_external_file();
4284e1cc 711 return $this->file_record->contenthash;
712 }
6ed19c74 713
67233725
DC
714 /**
715 * Set contenthash
716 *
717 * @param string $contenthash
718 */
719 protected function set_contenthash($contenthash) {
720 // make sure the content exists in moodle file pool
721 if ($this->fs->content_exists($contenthash)) {
722 $filerecord = new stdClass;
723 $filerecord->contenthash = $contenthash;
724 $this->update($filerecord);
725 } else {
726 throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
727 }
728 }
729
16a95e8f 730 /**
64f93798 731 * Returns sha1 hash of all file path components sha1("contextid/component/filearea/itemid/dir/dir/filename.ext").
8496fdac 732 *
16a95e8f 733 * @return string
734 */
6ed19c74 735 public function get_pathnamehash() {
736 return $this->file_record->pathnamehash;
737 }
1dce6261
DC
738
739 /**
8496fdac
PS
740 * Returns the license type of the file, it is a short name referred from license table.
741 *
1dce6261
DC
742 * @return string
743 */
744 public function get_license() {
745 return $this->file_record->license;
746 }
747
67233725
DC
748 /**
749 * Set license
750 *
751 * @param string $license license
752 */
753 public function set_license($license) {
754 $filerecord = new stdClass;
755 $filerecord->license = $license;
756 $this->update($filerecord);
757 }
758
1dce6261 759 /**
8496fdac
PS
760 * Returns the author name of the file.
761 *
1dce6261
DC
762 * @return string
763 */
764 public function get_author() {
31cd5fe8 765 return $this->file_record->author;
1dce6261
DC
766 }
767
67233725
DC
768 /**
769 * Set author
770 *
771 * @param string $author
772 */
773 public function set_author($author) {
774 $filerecord = new stdClass;
775 $filerecord->author = $author;
776 $this->update($filerecord);
777 }
778
1dce6261 779 /**
8496fdac
PS
780 * Returns the source of the file, usually it is a url.
781 *
1dce6261
DC
782 * @return string
783 */
784 public function get_source() {
785 return $this->file_record->source;
786 }
8496fdac 787
67233725
DC
788 /**
789 * Set license
790 *
791 * @param string $license license
792 */
793 public function set_source($source) {
794 $filerecord = new stdClass;
795 $filerecord->source = $source;
796 $this->update($filerecord);
797 }
798
799
f79321f1
DC
800 /**
801 * Returns the sort order of file
802 *
803 * @return int
804 */
805 public function get_sortorder() {
806 return $this->file_record->sortorder;
807 }
67233725
DC
808
809 /**
810 * Set file sort order
811 *
812 * @param int $sortorder
813 * @return int
814 */
815 public function set_sortorder($sortorder) {
816 $filerecord = new stdClass;
817 $filerecord->sortorder = $sortorder;
818 $this->update($filerecord);
819 }
820
821 /**
822 * Returns repository id
823 *
824 * @return int|null
825 */
826 public function get_repository_id() {
827 if (!empty($this->repository)) {
828 return $this->repository->id;
829 } else {
830 return null;
831 }
832 }
833
834 /**
835 * get reference file id
836 * @return int
837 */
838 public function get_referencefileid() {
839 return $this->file_record->referencefileid;
840 }
841
842 /**
843 * Get reference last sync time
844 * @return int
845 */
846 public function get_referencelastsync() {
847 return $this->file_record->referencelastsync;
848 }
849
850 /**
851 * Get reference last sync time
852 * @return int
853 */
854 public function get_referencelifetime() {
855 return $this->file_record->referencelifetime;
856 }
857 /**
858 * Returns file reference
859 *
860 * @return string
861 */
862 public function get_reference() {
863 return $this->file_record->reference;
864 }
865
866 /**
867 * Get human readable file reference information
868 *
869 * @return string
870 */
871 public function get_reference_details() {
0b2bfbd1
MG
872 return $this->repository->get_reference_details($this->get_reference(), $this->get_status());
873 }
874
875 /**
876 * Called after reference-file has been synchronized with the repository
877 *
878 * We update contenthash, filesize and status in files table if changed
879 * and we always update lastsync in files_reference table
880 *
14b7e500
MG
881 * @param string $contenthash
882 * @param int $filesize
883 * @param int $status
884 * @param int $lifetime the life time of this synchronisation results
0b2bfbd1 885 */
14b7e500 886 public function set_synchronized($contenthash, $filesize, $status = 0, $lifetime = null) {
0b2bfbd1
MG
887 global $DB;
888 if (!$this->is_external_file()) {
889 return;
890 }
891 $now = time();
14b7e500
MG
892 if ($contenthash != $this->file_record->contenthash) {
893 $oldcontenthash = $this->file_record->contenthash;
0b2bfbd1 894 }
14b7e500
MG
895 if ($lifetime === null) {
896 $lifetime = $this->file_record->referencelifetime;
0b2bfbd1 897 }
14b7e500
MG
898 // this will update all entries in {files} that have the same filereference id
899 $this->fs->update_references($this->file_record->referencefileid, $now, $lifetime, $contenthash, $filesize, $status);
900 // we don't need to call update() for this object, just set the values of changed fields
901 $this->file_record->contenthash = $contenthash;
902 $this->file_record->filesize = $filesize;
903 $this->file_record->status = $status;
904 $this->file_record->referencelastsync = $now;
905 $this->file_record->referencelifetime = $lifetime;
906 if (isset($oldcontenthash)) {
907 $this->fs->deleted_file_cleanup($oldcontenthash);
0b2bfbd1 908 }
0b2bfbd1
MG
909 }
910
14b7e500
MG
911 /**
912 * Sets the error status for a file that could not be synchronised
913 *
914 * @param int $lifetime the life time of this synchronisation results
915 */
916 public function set_missingsource($lifetime = null) {
917 $this->set_synchronized($this->get_contenthash(), $this->get_filesize(), 666, $lifetime);
67233725
DC
918 }
919
920 /**
921 * Send file references
922 *
923 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
924 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
925 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
926 * @param array $options additional options affecting the file serving
927 */
928 public function send_file($lifetime, $filter, $forcedownload, $options) {
929 $this->repository->send_file($this, $lifetime, $filter, $forcedownload, $options);
930 }
f79321f1 931}