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