MDL-33587 Throw coding exception when trying to unlink a non-reference file
[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
145 if ($field == 'referencefileid' or $field == 'referencelastsync' or $field == 'referencelifetime') {
146 $value = clean_param($value, PARAM_INT);
147 }
148
149 // adding the field
150 $this->file_record->$field = $value;
151 } else {
152 throw new coding_exception("Invalid field name, $field doesn't exist in file record");
153 }
154 }
8177b7b9 155 // Validate mimetype field
70c1bc84
DC
156 // we don't use {@link stored_file::get_content_file_location()} here becaues it will try to update file_record
157 $pathname = $this->get_pathname_by_contenthash();
8177b7b9
DC
158 // try to recover the content from trash
159 if (!is_readable($pathname)) {
160 if (!$this->fs->try_content_recovery($this) or !is_readable($pathname)) {
161 throw new file_exception('storedfilecannotread', '', $pathname);
162 }
163 }
efa28f30 164 $mimetype = $this->fs->mimetype($pathname, $this->file_record->filename);
8177b7b9
DC
165 $this->file_record->mimetype = $mimetype;
166
67233725
DC
167 $DB->update_record('files', $this->file_record);
168 }
169
170 /**
171 * Rename filename
172 *
173 * @param string $filepath file path
174 * @param string $filename file name
175 */
176 public function rename($filepath, $filename) {
7051415c
DC
177 if ($this->fs->file_exists($this->get_contextid(), $this->get_component(), $this->get_filearea(), $this->get_itemid(), $filepath, $filename)) {
178 throw new file_exception('storedfilenotcreated', '', 'file exists, cannot rename');
179 }
67233725
DC
180 $filerecord = new stdClass;
181 $filerecord->filepath = $filepath;
182 $filerecord->filename = $filename;
183 // populate the pathname hash
184 $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);
185 $this->update($filerecord);
186 }
187
188 /**
189 * Replace the content by providing another stored_file instance
190 *
191 * @param stored_file $storedfile
192 */
193 public function replace_content_with(stored_file $storedfile) {
194 $contenthash = $storedfile->get_contenthash();
195 $this->set_contenthash($contenthash);
196 }
197
198 /**
2fa8aea2 199 * Unlink the stored file from the referenced file
67233725 200 *
2fa8aea2
DM
201 * This methods destroys the link to the record in files_reference table. This effectively
202 * turns the stored file from being an alias to a plain copy. However, the caller has
203 * to make sure that the actual file's content has beed synced prior to calling this method.
67233725
DC
204 */
205 public function delete_reference() {
206 global $DB;
61506a0a 207
2fa8aea2
DM
208 if (!$this->is_external_file()) {
209 throw new coding_exception('An attempt to unlink a non-reference file.');
210 }
211
67233725
DC
212 // Remove repository info.
213 $this->repository = null;
61506a0a 214
e3c02118
DC
215 $transaction = $DB->start_delegated_transaction();
216
61506a0a
DC
217 // Remove reference info from DB.
218 $DB->delete_records('files_reference', array('id'=>$this->file_record->referencefileid));
219
220 // Must refresh $this->file_record form DB
221 $filerecord = $DB->get_record('files', array('id'=>$this->get_id()));
222 // Update DB
223 $filerecord->referencelastsync = null;
224 $filerecord->referencelifetime = null;
225 $filerecord->referencefileid = null;
226 $this->update($filerecord);
227
e3c02118
DC
228 $transaction->allow_commit();
229
61506a0a 230 // unset object variable
67233725
DC
231 unset($this->file_record->repositoryid);
232 unset($this->file_record->reference);
233 unset($this->file_record->referencelastsync);
234 unset($this->file_record->referencelifetime);
61506a0a 235 unset($this->file_record->referencefileid);
172dd12c 236 }
237
238 /**
239 * Is this a directory?
8496fdac
PS
240 *
241 * Directories are only emulated, internally they are stored as empty
242 * files with a "." instead of name - this means empty directory contains
243 * exactly one empty file with name dot.
244 *
245 * @return bool true means directory, false means file
172dd12c 246 */
247 public function is_directory() {
8496fdac 248 return ($this->file_record->filename === '.');
172dd12c 249 }
250
251 /**
8496fdac
PS
252 * Delete file from files table.
253 *
254 * The content of files stored in sha1 pool is reclaimed
255 * later - the occupied disk space is reclaimed much later.
256 *
257 * @return bool always true or exception if error occurred
172dd12c 258 */
259 public function delete() {
260 global $DB;
e3c02118
DC
261
262 $transaction = $DB->start_delegated_transaction();
263
67233725
DC
264 // If other files referring to this file, we need convert them
265 if ($files = $this->fs->get_references_by_storedfile($this)) {
266 foreach ($files as $file) {
267 $this->fs->import_external_file($file);
268 }
269 }
270 // Now delete file records in DB
1aa01caf 271 $DB->delete_records('files', array('id'=>$this->file_record->id));
67233725 272 $DB->delete_records('files_reference', array('id'=>$this->file_record->referencefileid));
e3c02118
DC
273
274 $transaction->allow_commit();
275
1aa01caf 276 // moves pool file to trash if content not needed any more
277 $this->fs->deleted_file_cleanup($this->file_record->contenthash);
8496fdac 278 return true; // BC only
172dd12c 279 }
280
281 /**
70c1bc84 282 * Get file pathname by contenthash
8496fdac 283 *
70c1bc84
DC
284 * NOTE, this function is not calling sync_external_file, it assume the contenthash is current
285 * Protected - developers must not gain direct access to this function.
8496fdac
PS
286 *
287 * @return string full path to pool file with file content
70c1bc84
DC
288 */
289 protected function get_pathname_by_contenthash() {
67233725 290 // Detect is local file or not.
17d9269f 291 $contenthash = $this->file_record->contenthash;
292 $l1 = $contenthash[0].$contenthash[1];
293 $l2 = $contenthash[2].$contenthash[3];
693ef3a8 294 return "$this->filedir/$l1/$l2/$contenthash";
172dd12c 295 }
296
70c1bc84
DC
297 /**
298 * Get file pathname by given contenthash, this method will try to sync files
299 *
300 * Protected - developers must not gain direct access to this function.
301 *
302 * NOTE: do not make this public, we must not modify or delete the pool files directly! ;-)
303 *
304 * @return string full path to pool file with file content
305 **/
306 protected function get_content_file_location() {
307 $this->sync_external_file();
308 return $this->get_pathname_by_contenthash();
309 }
310
5035a8b4 311 /**
312 * adds this file path to a curl request (POST only)
313 *
314 * @param curl $curlrequest the curl request object
315 * @param string $key what key to use in the POST request
8496fdac 316 * @return void
5035a8b4 317 */
318 public function add_to_curl_request(&$curlrequest, $key) {
319 $curlrequest->_tmp_file_post_params[$key] = '@' . $this->get_content_file_location();
320 }
321
172dd12c 322 /**
323 * Returns file handle - read only mode, no writing allowed into pool files!
8496fdac
PS
324 *
325 * When you want to modify a file, create a new file and delete the old one.
326 *
327 * @return resource file handle
172dd12c 328 */
329 public function get_content_file_handle() {
330 $path = $this->get_content_file_location();
331 if (!is_readable($path)) {
1aa01caf 332 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
d610cb89 333 throw new file_exception('storedfilecannotread', '', $path);
1aa01caf 334 }
172dd12c 335 }
67233725 336 return fopen($path, 'rb'); // Binary reading only!!
172dd12c 337 }
338
339 /**
8496fdac 340 * Dumps file content to page.
172dd12c 341 */
342 public function readfile() {
343 $path = $this->get_content_file_location();
344 if (!is_readable($path)) {
1aa01caf 345 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
d610cb89 346 throw new file_exception('storedfilecannotread', '', $path);
1aa01caf 347 }
172dd12c 348 }
349 readfile($path);
350 }
351
352 /**
8496fdac
PS
353 * Returns file content as string.
354 *
172dd12c 355 * @return string content
356 */
357 public function get_content() {
358 $path = $this->get_content_file_location();
359 if (!is_readable($path)) {
1aa01caf 360 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
d610cb89 361 throw new file_exception('storedfilecannotread', '', $path);
1aa01caf 362 }
172dd12c 363 }
364 return file_get_contents($this->get_content_file_location());
365 }
366
6c0e2d08 367 /**
8496fdac
PS
368 * Copy content of file to given pathname.
369 *
370 * @param string $pathname real path to the new file
6c0e2d08 371 * @return bool success
372 */
373 public function copy_content_to($pathname) {
374 $path = $this->get_content_file_location();
375 if (!is_readable($path)) {
1aa01caf 376 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
d610cb89 377 throw new file_exception('storedfilecannotread', '', $path);
1aa01caf 378 }
6c0e2d08 379 }
380 return copy($path, $pathname);
381 }
382
17d9269f 383 /**
8496fdac
PS
384 * List contents of archive.
385 *
d2b7803e 386 * @param file_packer $packer file packer instance
c78a0558 387 * @return array of file infos
388 */
389 public function list_files(file_packer $packer) {
390 $archivefile = $this->get_content_file_location();
391 return $packer->list_files($archivefile);
392 }
393
394 /**
8496fdac
PS
395 * Extract file to given file path (real OS filesystem), existing files are overwritten.
396 *
d2b7803e 397 * @param file_packer $packer file packer instance
0b0bfa93 398 * @param string $pathname target directory
8496fdac 399 * @return array|bool list of processed files; false if error
17d9269f 400 */
0b0bfa93 401 public function extract_to_pathname(file_packer $packer, $pathname) {
402 $archivefile = $this->get_content_file_location();
403 return $packer->extract_to_pathname($archivefile, $pathname);
17d9269f 404 }
405
406 /**
8496fdac
PS
407 * Extract file to given file path (real OS filesystem), existing files are overwritten.
408 *
d2b7803e
DC
409 * @param file_packer $packer file packer instance
410 * @param int $contextid context ID
411 * @param string $component component
412 * @param string $filearea file area
413 * @param int $itemid item ID
414 * @param string $pathbase path base
415 * @param int $userid user ID
8496fdac 416 * @return array|bool list of processed files; false if error
17d9269f 417 */
64f93798 418 public function extract_to_storage(file_packer $packer, $contextid, $component, $filearea, $itemid, $pathbase, $userid = NULL) {
0b0bfa93 419 $archivefile = $this->get_content_file_location();
64f93798 420 return $packer->extract_to_storage($archivefile, $contextid, $component, $filearea, $itemid, $pathbase);
17d9269f 421 }
422
b1897a6d 423 /**
8496fdac
PS
424 * Add file/directory into archive.
425 *
d2b7803e 426 * @param file_archive $filearch file archive instance
c78a0558 427 * @param string $archivepath pathname in archive
b1897a6d 428 * @return bool success
429 */
0b0bfa93 430 public function archive_file(file_archive $filearch, $archivepath) {
b1897a6d 431 if ($this->is_directory()) {
0b0bfa93 432 return $filearch->add_directory($archivepath);
b1897a6d 433 } else {
434 $path = $this->get_content_file_location();
435 if (!is_readable($path)) {
436 return false;
437 }
0b0bfa93 438 return $filearch->add_file_from_pathname($archivepath, $path);
b1897a6d 439 }
440 }
441
797f19e8 442 /**
443 * Returns information about image,
444 * information is determined from the file content
d2b7803e 445 *
797f19e8 446 * @return mixed array with width, height and mimetype; false if not an image
447 */
448 public function get_imageinfo() {
b7725e30
MG
449 $path = $this->get_content_file_location();
450 if (!is_readable($path)) {
451 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
452 throw new file_exception('storedfilecannotread', '', $path);
453 }
454 }
455 $mimetype = $this->get_mimetype();
456 if (!preg_match('|^image/|', $mimetype) || !filesize($path) || !($imageinfo = getimagesize($path))) {
797f19e8 457 return false;
458 }
459 $image = array('width'=>$imageinfo[0], 'height'=>$imageinfo[1], 'mimetype'=>image_type_to_mime_type($imageinfo[2]));
460 if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) {
461 // gd can not parse it, sorry
462 return false;
463 }
464 return $image;
465 }
466
467 /**
468 * Verifies the file is a valid web image - gif, png and jpeg only.
8496fdac 469 *
797f19e8 470 * It should be ok to serve this image from server without any other security workarounds.
8496fdac 471 *
797f19e8 472 * @return bool true if file ok
473 */
474 public function is_valid_image() {
475 $mimetype = $this->get_mimetype();
559276b1 476 if (!file_mimetype_in_typegroup($mimetype, 'web_image')) {
797f19e8 477 return false;
478 }
479 if (!$info = $this->get_imageinfo()) {
480 return false;
481 }
482 if ($info['mimetype'] !== $mimetype) {
483 return false;
484 }
485 // ok, GD likes this image
486 return true;
487 }
488
4b6b5ce7 489 /**
8496fdac
PS
490 * Returns parent directory, creates missing parents if needed.
491 *
492 * @return stored_file
4b6b5ce7 493 */
494 public function get_parent_directory() {
495 if ($this->file_record->filepath === '/' and $this->file_record->filename === '.') {
496 //root dir does not have parent
497 return null;
498 }
499
500 if ($this->file_record->filename !== '.') {
64f93798 501 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 502 }
503
504 $filepath = $this->file_record->filepath;
505 $filepath = trim($filepath, '/');
506 $dirs = explode('/', $filepath);
507 array_pop($dirs);
508 $filepath = implode('/', $dirs);
509 $filepath = ($filepath === '') ? '/' : "/$filepath/";
510
64f93798 511 return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath);
4b6b5ce7 512 }
513
16a95e8f 514 /**
67233725
DC
515 * Sync external files
516 *
517 * @return bool true if file content changed, false if not
518 */
519 public function sync_external_file() {
520 global $CFG, $DB;
521 if (empty($this->file_record->referencefileid)) {
522 return false;
523 }
524 if (empty($this->file_record->referencelastsync) or ($this->file_record->referencelastsync + $this->file_record->referencelifetime < time())) {
525 require_once($CFG->dirroot.'/repository/lib.php');
526 if (repository::sync_external_file($this)) {
527 $prevcontent = $this->file_record->contenthash;
528 $sql = "SELECT f.*, r.repositoryid, r.reference
529 FROM {files} f
530 LEFT JOIN {files_reference} r
531 ON f.referencefileid = r.id
532 WHERE f.id = ?";
533 $this->file_record = $DB->get_record_sql($sql, array($this->file_record->id), MUST_EXIST);
534 return ($prevcontent !== $this->file_record->contenthash);
535 }
536 }
537 return false;
538 }
539
540 /**
541 * Returns context id of the file
8496fdac 542 *
16a95e8f 543 * @return int context id
544 */
172dd12c 545 public function get_contextid() {
546 return $this->file_record->contextid;
547 }
548
16a95e8f 549 /**
64f93798
PS
550 * Returns component name - this is the owner of the areas,
551 * nothing else is allowed to read or modify the files directly!!
552 *
553 * @return string
554 */
555 public function get_component() {
556 return $this->file_record->component;
557 }
558
559 /**
560 * Returns file area name, this divides files of one component into groups with different access control.
561 * All files in one area have the same access control.
8496fdac 562 *
16a95e8f 563 * @return string
564 */
172dd12c 565 public function get_filearea() {
566 return $this->file_record->filearea;
567 }
568
16a95e8f 569 /**
8496fdac
PS
570 * Returns returns item id of file.
571 *
16a95e8f 572 * @return int
573 */
172dd12c 574 public function get_itemid() {
575 return $this->file_record->itemid;
576 }
577
16a95e8f 578 /**
579 * Returns file path - starts and ends with /, \ are not allowed.
8496fdac 580 *
16a95e8f 581 * @return string
582 */
172dd12c 583 public function get_filepath() {
584 return $this->file_record->filepath;
585 }
586
16a95e8f 587 /**
588 * Returns file name or '.' in case of directories.
8496fdac 589 *
16a95e8f 590 * @return string
591 */
172dd12c 592 public function get_filename() {
593 return $this->file_record->filename;
594 }
595
16a95e8f 596 /**
597 * Returns id of user who created the file.
8496fdac 598 *
16a95e8f 599 * @return int
600 */
172dd12c 601 public function get_userid() {
602 return $this->file_record->userid;
603 }
604
16a95e8f 605 /**
606 * Returns the size of file in bytes.
8496fdac 607 *
16a95e8f 608 * @return int bytes
609 */
172dd12c 610 public function get_filesize() {
67233725 611 $this->sync_external_file();
172dd12c 612 return $this->file_record->filesize;
613 }
614
61506a0a
DC
615 /**
616 * Returns the size of file in bytes.
617 *
618 * @param int $filesize bytes
619 */
620 public function set_filesize($filesize) {
621 $filerecord = new stdClass;
622 $filerecord->filesize = $filesize;
623 $this->update($filerecord);
624 }
625
16a95e8f 626 /**
8496fdac
PS
627 * Returns mime type of file.
628 *
16a95e8f 629 * @return string
630 */
172dd12c 631 public function get_mimetype() {
632 return $this->file_record->mimetype;
633 }
634
16a95e8f 635 /**
8496fdac
PS
636 * Returns unix timestamp of file creation date.
637 *
16a95e8f 638 * @return int
639 */
172dd12c 640 public function get_timecreated() {
641 return $this->file_record->timecreated;
642 }
643
16a95e8f 644 /**
8496fdac
PS
645 * Returns unix timestamp of last file modification.
646 *
16a95e8f 647 * @return int
648 */
172dd12c 649 public function get_timemodified() {
67233725 650 $this->sync_external_file();
172dd12c 651 return $this->file_record->timemodified;
652 }
6c0e2d08 653
67233725
DC
654 /**
655 * set timemodified
656 *
657 * @param int $timemodified
658 */
659 public function set_timemodified($timemodified) {
660 $filerecord = new stdClass;
661 $filerecord->timemodified = $timemodified;
662 $this->update($filerecord);
663 }
664
16a95e8f 665 /**
8496fdac
PS
666 * Returns file status flag.
667 *
16a95e8f 668 * @return int 0 means file OK, anything else is a problem and file can not be used
669 */
6c0e2d08 670 public function get_status() {
671 return $this->file_record->status;
672 }
ee03a651 673
16a95e8f 674 /**
8496fdac
PS
675 * Returns file id.
676 *
16a95e8f 677 * @return int
678 */
ee03a651 679 public function get_id() {
680 return $this->file_record->id;
681 }
4284e1cc 682
16a95e8f 683 /**
8496fdac
PS
684 * Returns sha1 hash of file content.
685 *
16a95e8f 686 * @return string
687 */
4284e1cc 688 public function get_contenthash() {
67233725 689 $this->sync_external_file();
4284e1cc 690 return $this->file_record->contenthash;
691 }
6ed19c74 692
67233725
DC
693 /**
694 * Set contenthash
695 *
696 * @param string $contenthash
697 */
698 protected function set_contenthash($contenthash) {
699 // make sure the content exists in moodle file pool
700 if ($this->fs->content_exists($contenthash)) {
701 $filerecord = new stdClass;
702 $filerecord->contenthash = $contenthash;
703 $this->update($filerecord);
704 } else {
705 throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
706 }
707 }
708
16a95e8f 709 /**
64f93798 710 * Returns sha1 hash of all file path components sha1("contextid/component/filearea/itemid/dir/dir/filename.ext").
8496fdac 711 *
16a95e8f 712 * @return string
713 */
6ed19c74 714 public function get_pathnamehash() {
715 return $this->file_record->pathnamehash;
716 }
1dce6261
DC
717
718 /**
8496fdac
PS
719 * Returns the license type of the file, it is a short name referred from license table.
720 *
1dce6261
DC
721 * @return string
722 */
723 public function get_license() {
724 return $this->file_record->license;
725 }
726
67233725
DC
727 /**
728 * Set license
729 *
730 * @param string $license license
731 */
732 public function set_license($license) {
733 $filerecord = new stdClass;
734 $filerecord->license = $license;
735 $this->update($filerecord);
736 }
737
1dce6261 738 /**
8496fdac
PS
739 * Returns the author name of the file.
740 *
1dce6261
DC
741 * @return string
742 */
743 public function get_author() {
31cd5fe8 744 return $this->file_record->author;
1dce6261
DC
745 }
746
67233725
DC
747 /**
748 * Set author
749 *
750 * @param string $author
751 */
752 public function set_author($author) {
753 $filerecord = new stdClass;
754 $filerecord->author = $author;
755 $this->update($filerecord);
756 }
757
1dce6261 758 /**
8496fdac
PS
759 * Returns the source of the file, usually it is a url.
760 *
1dce6261
DC
761 * @return string
762 */
763 public function get_source() {
764 return $this->file_record->source;
765 }
8496fdac 766
67233725
DC
767 /**
768 * Set license
769 *
770 * @param string $license license
771 */
772 public function set_source($source) {
773 $filerecord = new stdClass;
774 $filerecord->source = $source;
775 $this->update($filerecord);
776 }
777
778
f79321f1
DC
779 /**
780 * Returns the sort order of file
781 *
782 * @return int
783 */
784 public function get_sortorder() {
785 return $this->file_record->sortorder;
786 }
67233725
DC
787
788 /**
789 * Set file sort order
790 *
791 * @param int $sortorder
792 * @return int
793 */
794 public function set_sortorder($sortorder) {
795 $filerecord = new stdClass;
796 $filerecord->sortorder = $sortorder;
797 $this->update($filerecord);
798 }
799
800 /**
801 * Returns repository id
802 *
803 * @return int|null
804 */
805 public function get_repository_id() {
806 if (!empty($this->repository)) {
807 return $this->repository->id;
808 } else {
809 return null;
810 }
811 }
812
813 /**
814 * get reference file id
815 * @return int
816 */
817 public function get_referencefileid() {
818 return $this->file_record->referencefileid;
819 }
820
821 /**
822 * Get reference last sync time
823 * @return int
824 */
825 public function get_referencelastsync() {
826 return $this->file_record->referencelastsync;
827 }
828
829 /**
830 * Get reference last sync time
831 * @return int
832 */
833 public function get_referencelifetime() {
834 return $this->file_record->referencelifetime;
835 }
836 /**
837 * Returns file reference
838 *
839 * @return string
840 */
841 public function get_reference() {
842 return $this->file_record->reference;
843 }
844
845 /**
846 * Get human readable file reference information
847 *
848 * @return string
849 */
850 public function get_reference_details() {
851 return $this->repository->get_reference_details($this->get_reference());
852 }
853
854 /**
855 * Send file references
856 *
857 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
858 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
859 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
860 * @param array $options additional options affecting the file serving
861 */
862 public function send_file($lifetime, $filter, $forcedownload, $options) {
863 $this->repository->send_file($this, $lifetime, $filter, $forcedownload, $options);
864 }
f79321f1 865}