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 /**
0b2bfbd1 525 * Synchronize file if it is a reference and needs synchronizing
67233725 526 *
0b2bfbd1 527 * Updates contenthash and filesize
67233725
DC
528 */
529 public function sync_external_file() {
0b2bfbd1
MG
530 global $CFG;
531 if (!empty($this->file_record->referencefileid)) {
67233725 532 require_once($CFG->dirroot.'/repository/lib.php');
0b2bfbd1 533 repository::sync_external_file($this);
67233725 534 }
67233725
DC
535 }
536
537 /**
538 * Returns context id of the file
8496fdac 539 *
16a95e8f 540 * @return int context id
541 */
172dd12c 542 public function get_contextid() {
543 return $this->file_record->contextid;
544 }
545
16a95e8f 546 /**
64f93798
PS
547 * Returns component name - this is the owner of the areas,
548 * nothing else is allowed to read or modify the files directly!!
549 *
550 * @return string
551 */
552 public function get_component() {
553 return $this->file_record->component;
554 }
555
556 /**
557 * Returns file area name, this divides files of one component into groups with different access control.
558 * All files in one area have the same access control.
8496fdac 559 *
16a95e8f 560 * @return string
561 */
172dd12c 562 public function get_filearea() {
563 return $this->file_record->filearea;
564 }
565
16a95e8f 566 /**
8496fdac
PS
567 * Returns returns item id of file.
568 *
16a95e8f 569 * @return int
570 */
172dd12c 571 public function get_itemid() {
572 return $this->file_record->itemid;
573 }
574
16a95e8f 575 /**
576 * Returns file path - starts and ends with /, \ are not allowed.
8496fdac 577 *
16a95e8f 578 * @return string
579 */
172dd12c 580 public function get_filepath() {
581 return $this->file_record->filepath;
582 }
583
16a95e8f 584 /**
585 * Returns file name or '.' in case of directories.
8496fdac 586 *
16a95e8f 587 * @return string
588 */
172dd12c 589 public function get_filename() {
590 return $this->file_record->filename;
591 }
592
16a95e8f 593 /**
594 * Returns id of user who created the file.
8496fdac 595 *
16a95e8f 596 * @return int
597 */
172dd12c 598 public function get_userid() {
599 return $this->file_record->userid;
600 }
601
16a95e8f 602 /**
603 * Returns the size of file in bytes.
8496fdac 604 *
16a95e8f 605 * @return int bytes
606 */
172dd12c 607 public function get_filesize() {
67233725 608 $this->sync_external_file();
172dd12c 609 return $this->file_record->filesize;
610 }
611
61506a0a
DC
612 /**
613 * Returns the size of file in bytes.
614 *
615 * @param int $filesize bytes
616 */
617 public function set_filesize($filesize) {
618 $filerecord = new stdClass;
619 $filerecord->filesize = $filesize;
620 $this->update($filerecord);
621 }
622
16a95e8f 623 /**
8496fdac
PS
624 * Returns mime type of file.
625 *
16a95e8f 626 * @return string
627 */
172dd12c 628 public function get_mimetype() {
629 return $this->file_record->mimetype;
630 }
631
16a95e8f 632 /**
8496fdac
PS
633 * Returns unix timestamp of file creation date.
634 *
16a95e8f 635 * @return int
636 */
172dd12c 637 public function get_timecreated() {
638 return $this->file_record->timecreated;
639 }
640
16a95e8f 641 /**
8496fdac
PS
642 * Returns unix timestamp of last file modification.
643 *
16a95e8f 644 * @return int
645 */
172dd12c 646 public function get_timemodified() {
67233725 647 $this->sync_external_file();
172dd12c 648 return $this->file_record->timemodified;
649 }
6c0e2d08 650
67233725
DC
651 /**
652 * set timemodified
653 *
654 * @param int $timemodified
655 */
656 public function set_timemodified($timemodified) {
657 $filerecord = new stdClass;
658 $filerecord->timemodified = $timemodified;
659 $this->update($filerecord);
660 }
661
16a95e8f 662 /**
8496fdac
PS
663 * Returns file status flag.
664 *
16a95e8f 665 * @return int 0 means file OK, anything else is a problem and file can not be used
666 */
6c0e2d08 667 public function get_status() {
668 return $this->file_record->status;
669 }
ee03a651 670
16a95e8f 671 /**
8496fdac
PS
672 * Returns file id.
673 *
16a95e8f 674 * @return int
675 */
ee03a651 676 public function get_id() {
677 return $this->file_record->id;
678 }
4284e1cc 679
16a95e8f 680 /**
8496fdac
PS
681 * Returns sha1 hash of file content.
682 *
16a95e8f 683 * @return string
684 */
4284e1cc 685 public function get_contenthash() {
67233725 686 $this->sync_external_file();
4284e1cc 687 return $this->file_record->contenthash;
688 }
6ed19c74 689
67233725
DC
690 /**
691 * Set contenthash
692 *
693 * @param string $contenthash
694 */
695 protected function set_contenthash($contenthash) {
696 // make sure the content exists in moodle file pool
697 if ($this->fs->content_exists($contenthash)) {
698 $filerecord = new stdClass;
699 $filerecord->contenthash = $contenthash;
700 $this->update($filerecord);
701 } else {
702 throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
703 }
704 }
705
16a95e8f 706 /**
64f93798 707 * Returns sha1 hash of all file path components sha1("contextid/component/filearea/itemid/dir/dir/filename.ext").
8496fdac 708 *
16a95e8f 709 * @return string
710 */
6ed19c74 711 public function get_pathnamehash() {
712 return $this->file_record->pathnamehash;
713 }
1dce6261
DC
714
715 /**
8496fdac
PS
716 * Returns the license type of the file, it is a short name referred from license table.
717 *
1dce6261
DC
718 * @return string
719 */
720 public function get_license() {
721 return $this->file_record->license;
722 }
723
67233725
DC
724 /**
725 * Set license
726 *
727 * @param string $license license
728 */
729 public function set_license($license) {
730 $filerecord = new stdClass;
731 $filerecord->license = $license;
732 $this->update($filerecord);
733 }
734
1dce6261 735 /**
8496fdac
PS
736 * Returns the author name of the file.
737 *
1dce6261
DC
738 * @return string
739 */
740 public function get_author() {
31cd5fe8 741 return $this->file_record->author;
1dce6261
DC
742 }
743
67233725
DC
744 /**
745 * Set author
746 *
747 * @param string $author
748 */
749 public function set_author($author) {
750 $filerecord = new stdClass;
751 $filerecord->author = $author;
752 $this->update($filerecord);
753 }
754
1dce6261 755 /**
8496fdac
PS
756 * Returns the source of the file, usually it is a url.
757 *
1dce6261
DC
758 * @return string
759 */
760 public function get_source() {
761 return $this->file_record->source;
762 }
8496fdac 763
67233725
DC
764 /**
765 * Set license
766 *
767 * @param string $license license
768 */
769 public function set_source($source) {
770 $filerecord = new stdClass;
771 $filerecord->source = $source;
772 $this->update($filerecord);
773 }
774
775
f79321f1
DC
776 /**
777 * Returns the sort order of file
778 *
779 * @return int
780 */
781 public function get_sortorder() {
782 return $this->file_record->sortorder;
783 }
67233725
DC
784
785 /**
786 * Set file sort order
787 *
788 * @param int $sortorder
789 * @return int
790 */
791 public function set_sortorder($sortorder) {
792 $filerecord = new stdClass;
793 $filerecord->sortorder = $sortorder;
794 $this->update($filerecord);
795 }
796
797 /**
798 * Returns repository id
799 *
800 * @return int|null
801 */
802 public function get_repository_id() {
803 if (!empty($this->repository)) {
804 return $this->repository->id;
805 } else {
806 return null;
807 }
808 }
809
810 /**
811 * get reference file id
812 * @return int
813 */
814 public function get_referencefileid() {
815 return $this->file_record->referencefileid;
816 }
817
818 /**
819 * Get reference last sync time
820 * @return int
821 */
822 public function get_referencelastsync() {
823 return $this->file_record->referencelastsync;
824 }
825
826 /**
827 * Get reference last sync time
828 * @return int
829 */
830 public function get_referencelifetime() {
831 return $this->file_record->referencelifetime;
832 }
833 /**
834 * Returns file reference
835 *
836 * @return string
837 */
838 public function get_reference() {
839 return $this->file_record->reference;
840 }
841
842 /**
843 * Get human readable file reference information
844 *
845 * @return string
846 */
847 public function get_reference_details() {
0b2bfbd1
MG
848 return $this->repository->get_reference_details($this->get_reference(), $this->get_status());
849 }
850
851 /**
852 * Called after reference-file has been synchronized with the repository
853 *
854 * We update contenthash, filesize and status in files table if changed
855 * and we always update lastsync in files_reference table
856 *
857 * @param type $contenthash
858 * @param type $filesize
859 */
860 public function set_synchronized($contenthash, $filesize, $status = 0) {
861 global $DB;
862 if (!$this->is_external_file()) {
863 return;
864 }
865 $now = time();
866 $filerecord = new stdClass();
867 if ($this->get_contenthash() !== $contenthash) {
868 $filerecord->contenthash = $contenthash;
869 }
870 if ($this->get_filesize() != $filesize) {
871 $filerecord->filesize = $filesize;
872 }
873 if ($this->get_status() != $status) {
874 $filerecord->status = $status;
875 }
876 $filerecord->referencelastsync = $now; // TODO MDL-33416 remove this
877 if (!empty($filerecord)) {
878 $this->update($filerecord);
879 }
880
881 $DB->set_field('files_reference', 'lastsync', $now, array('id'=>$this->get_referencefileid()));
882 // $this->file_record->lastsync = $now; // TODO MDL-33416 uncomment or remove
883 }
884
885 public function set_missingsource() {
886 $this->set_synchronized($this->get_contenthash(), 0, 666);
67233725
DC
887 }
888
889 /**
890 * Send file references
891 *
892 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
893 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
894 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
895 * @param array $options additional options affecting the file serving
896 */
897 public function send_file($lifetime, $filter, $forcedownload, $options) {
898 $this->repository->send_file($this, $lifetime, $filter, $forcedownload, $options);
899 }
f79321f1 900}