MDL-34290 curl class: add functions to return error code and to download one 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
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
4e782b32
RT
393 /**
394 * Copy content of file to temporary folder and returns file path
395 *
396 * @param string $dir name of the temporary directory
397 * @param string $fileprefix prefix of temporary file.
398 * @return string|bool path of temporary file or false.
399 */
400 public function copy_content_to_temp($dir = 'files', $fileprefix = 'tempup_') {
401 $tempfile = false;
402 if (!$dir = make_temp_directory($dir)) {
403 return false;
404 }
405 if (!$tempfile = tempnam($dir, $fileprefix)) {
406 return false;
407 }
408 if (!$this->copy_content_to($tempfile)) {
409 // something went wrong
410 @unlink($tempfile);
411 return false;
412 }
413 return $tempfile;
414 }
415
17d9269f 416 /**
8496fdac
PS
417 * List contents of archive.
418 *
d2b7803e 419 * @param file_packer $packer file packer instance
c78a0558 420 * @return array of file infos
421 */
422 public function list_files(file_packer $packer) {
423 $archivefile = $this->get_content_file_location();
424 return $packer->list_files($archivefile);
425 }
426
427 /**
8496fdac
PS
428 * Extract file to given file path (real OS filesystem), existing files are overwritten.
429 *
d2b7803e 430 * @param file_packer $packer file packer instance
0b0bfa93 431 * @param string $pathname target directory
8496fdac 432 * @return array|bool list of processed files; false if error
17d9269f 433 */
0b0bfa93 434 public function extract_to_pathname(file_packer $packer, $pathname) {
435 $archivefile = $this->get_content_file_location();
436 return $packer->extract_to_pathname($archivefile, $pathname);
17d9269f 437 }
438
439 /**
8496fdac
PS
440 * Extract file to given file path (real OS filesystem), existing files are overwritten.
441 *
d2b7803e
DC
442 * @param file_packer $packer file packer instance
443 * @param int $contextid context ID
444 * @param string $component component
445 * @param string $filearea file area
446 * @param int $itemid item ID
447 * @param string $pathbase path base
448 * @param int $userid user ID
8496fdac 449 * @return array|bool list of processed files; false if error
17d9269f 450 */
64f93798 451 public function extract_to_storage(file_packer $packer, $contextid, $component, $filearea, $itemid, $pathbase, $userid = NULL) {
0b0bfa93 452 $archivefile = $this->get_content_file_location();
64f93798 453 return $packer->extract_to_storage($archivefile, $contextid, $component, $filearea, $itemid, $pathbase);
17d9269f 454 }
455
b1897a6d 456 /**
8496fdac
PS
457 * Add file/directory into archive.
458 *
d2b7803e 459 * @param file_archive $filearch file archive instance
c78a0558 460 * @param string $archivepath pathname in archive
b1897a6d 461 * @return bool success
462 */
0b0bfa93 463 public function archive_file(file_archive $filearch, $archivepath) {
b1897a6d 464 if ($this->is_directory()) {
0b0bfa93 465 return $filearch->add_directory($archivepath);
b1897a6d 466 } else {
467 $path = $this->get_content_file_location();
468 if (!is_readable($path)) {
469 return false;
470 }
0b0bfa93 471 return $filearch->add_file_from_pathname($archivepath, $path);
b1897a6d 472 }
473 }
474
797f19e8 475 /**
476 * Returns information about image,
477 * information is determined from the file content
d2b7803e 478 *
797f19e8 479 * @return mixed array with width, height and mimetype; false if not an image
480 */
481 public function get_imageinfo() {
b7725e30
MG
482 $path = $this->get_content_file_location();
483 if (!is_readable($path)) {
484 if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
485 throw new file_exception('storedfilecannotread', '', $path);
486 }
487 }
488 $mimetype = $this->get_mimetype();
489 if (!preg_match('|^image/|', $mimetype) || !filesize($path) || !($imageinfo = getimagesize($path))) {
797f19e8 490 return false;
491 }
492 $image = array('width'=>$imageinfo[0], 'height'=>$imageinfo[1], 'mimetype'=>image_type_to_mime_type($imageinfo[2]));
493 if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) {
494 // gd can not parse it, sorry
495 return false;
496 }
497 return $image;
498 }
499
500 /**
501 * Verifies the file is a valid web image - gif, png and jpeg only.
8496fdac 502 *
797f19e8 503 * It should be ok to serve this image from server without any other security workarounds.
8496fdac 504 *
797f19e8 505 * @return bool true if file ok
506 */
507 public function is_valid_image() {
508 $mimetype = $this->get_mimetype();
559276b1 509 if (!file_mimetype_in_typegroup($mimetype, 'web_image')) {
797f19e8 510 return false;
511 }
512 if (!$info = $this->get_imageinfo()) {
513 return false;
514 }
515 if ($info['mimetype'] !== $mimetype) {
516 return false;
517 }
518 // ok, GD likes this image
519 return true;
520 }
521
4b6b5ce7 522 /**
8496fdac
PS
523 * Returns parent directory, creates missing parents if needed.
524 *
525 * @return stored_file
4b6b5ce7 526 */
527 public function get_parent_directory() {
528 if ($this->file_record->filepath === '/' and $this->file_record->filename === '.') {
529 //root dir does not have parent
530 return null;
531 }
532
533 if ($this->file_record->filename !== '.') {
64f93798 534 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 535 }
536
537 $filepath = $this->file_record->filepath;
538 $filepath = trim($filepath, '/');
539 $dirs = explode('/', $filepath);
540 array_pop($dirs);
541 $filepath = implode('/', $dirs);
542 $filepath = ($filepath === '') ? '/' : "/$filepath/";
543
64f93798 544 return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath);
4b6b5ce7 545 }
546
16a95e8f 547 /**
0b2bfbd1 548 * Synchronize file if it is a reference and needs synchronizing
67233725 549 *
0b2bfbd1 550 * Updates contenthash and filesize
67233725
DC
551 */
552 public function sync_external_file() {
0b2bfbd1
MG
553 global $CFG;
554 if (!empty($this->file_record->referencefileid)) {
67233725 555 require_once($CFG->dirroot.'/repository/lib.php');
0b2bfbd1 556 repository::sync_external_file($this);
67233725 557 }
67233725
DC
558 }
559
560 /**
561 * Returns context id of the file
8496fdac 562 *
16a95e8f 563 * @return int context id
564 */
172dd12c 565 public function get_contextid() {
566 return $this->file_record->contextid;
567 }
568
16a95e8f 569 /**
64f93798
PS
570 * Returns component name - this is the owner of the areas,
571 * nothing else is allowed to read or modify the files directly!!
572 *
573 * @return string
574 */
575 public function get_component() {
576 return $this->file_record->component;
577 }
578
579 /**
580 * Returns file area name, this divides files of one component into groups with different access control.
581 * All files in one area have the same access control.
8496fdac 582 *
16a95e8f 583 * @return string
584 */
172dd12c 585 public function get_filearea() {
586 return $this->file_record->filearea;
587 }
588
16a95e8f 589 /**
8496fdac
PS
590 * Returns returns item id of file.
591 *
16a95e8f 592 * @return int
593 */
172dd12c 594 public function get_itemid() {
595 return $this->file_record->itemid;
596 }
597
16a95e8f 598 /**
599 * Returns file path - starts and ends with /, \ are not allowed.
8496fdac 600 *
16a95e8f 601 * @return string
602 */
172dd12c 603 public function get_filepath() {
604 return $this->file_record->filepath;
605 }
606
16a95e8f 607 /**
608 * Returns file name or '.' in case of directories.
8496fdac 609 *
16a95e8f 610 * @return string
611 */
172dd12c 612 public function get_filename() {
613 return $this->file_record->filename;
614 }
615
16a95e8f 616 /**
617 * Returns id of user who created the file.
8496fdac 618 *
16a95e8f 619 * @return int
620 */
172dd12c 621 public function get_userid() {
622 return $this->file_record->userid;
623 }
624
16a95e8f 625 /**
626 * Returns the size of file in bytes.
8496fdac 627 *
16a95e8f 628 * @return int bytes
629 */
172dd12c 630 public function get_filesize() {
67233725 631 $this->sync_external_file();
172dd12c 632 return $this->file_record->filesize;
633 }
634
61506a0a
DC
635 /**
636 * Returns the size of file in bytes.
637 *
638 * @param int $filesize bytes
639 */
640 public function set_filesize($filesize) {
641 $filerecord = new stdClass;
642 $filerecord->filesize = $filesize;
643 $this->update($filerecord);
644 }
645
16a95e8f 646 /**
8496fdac
PS
647 * Returns mime type of file.
648 *
16a95e8f 649 * @return string
650 */
172dd12c 651 public function get_mimetype() {
652 return $this->file_record->mimetype;
653 }
654
16a95e8f 655 /**
8496fdac
PS
656 * Returns unix timestamp of file creation date.
657 *
16a95e8f 658 * @return int
659 */
172dd12c 660 public function get_timecreated() {
661 return $this->file_record->timecreated;
662 }
663
16a95e8f 664 /**
8496fdac
PS
665 * Returns unix timestamp of last file modification.
666 *
16a95e8f 667 * @return int
668 */
172dd12c 669 public function get_timemodified() {
67233725 670 $this->sync_external_file();
172dd12c 671 return $this->file_record->timemodified;
672 }
6c0e2d08 673
67233725
DC
674 /**
675 * set timemodified
676 *
677 * @param int $timemodified
678 */
679 public function set_timemodified($timemodified) {
680 $filerecord = new stdClass;
681 $filerecord->timemodified = $timemodified;
682 $this->update($filerecord);
683 }
684
16a95e8f 685 /**
8496fdac
PS
686 * Returns file status flag.
687 *
16a95e8f 688 * @return int 0 means file OK, anything else is a problem and file can not be used
689 */
6c0e2d08 690 public function get_status() {
691 return $this->file_record->status;
692 }
ee03a651 693
16a95e8f 694 /**
8496fdac
PS
695 * Returns file id.
696 *
16a95e8f 697 * @return int
698 */
ee03a651 699 public function get_id() {
700 return $this->file_record->id;
701 }
4284e1cc 702
16a95e8f 703 /**
8496fdac
PS
704 * Returns sha1 hash of file content.
705 *
16a95e8f 706 * @return string
707 */
4284e1cc 708 public function get_contenthash() {
67233725 709 $this->sync_external_file();
4284e1cc 710 return $this->file_record->contenthash;
711 }
6ed19c74 712
67233725
DC
713 /**
714 * Set contenthash
715 *
716 * @param string $contenthash
717 */
718 protected function set_contenthash($contenthash) {
719 // make sure the content exists in moodle file pool
720 if ($this->fs->content_exists($contenthash)) {
721 $filerecord = new stdClass;
722 $filerecord->contenthash = $contenthash;
723 $this->update($filerecord);
724 } else {
725 throw new file_exception('storedfileproblem', 'Invalid contenthash, content must be already in filepool', $contenthash);
726 }
727 }
728
16a95e8f 729 /**
64f93798 730 * Returns sha1 hash of all file path components sha1("contextid/component/filearea/itemid/dir/dir/filename.ext").
8496fdac 731 *
16a95e8f 732 * @return string
733 */
6ed19c74 734 public function get_pathnamehash() {
735 return $this->file_record->pathnamehash;
736 }
1dce6261
DC
737
738 /**
8496fdac
PS
739 * Returns the license type of the file, it is a short name referred from license table.
740 *
1dce6261
DC
741 * @return string
742 */
743 public function get_license() {
744 return $this->file_record->license;
745 }
746
67233725
DC
747 /**
748 * Set license
749 *
750 * @param string $license license
751 */
752 public function set_license($license) {
753 $filerecord = new stdClass;
754 $filerecord->license = $license;
755 $this->update($filerecord);
756 }
757
1dce6261 758 /**
8496fdac
PS
759 * Returns the author name of the file.
760 *
1dce6261
DC
761 * @return string
762 */
763 public function get_author() {
31cd5fe8 764 return $this->file_record->author;
1dce6261
DC
765 }
766
67233725
DC
767 /**
768 * Set author
769 *
770 * @param string $author
771 */
772 public function set_author($author) {
773 $filerecord = new stdClass;
774 $filerecord->author = $author;
775 $this->update($filerecord);
776 }
777
1dce6261 778 /**
8496fdac
PS
779 * Returns the source of the file, usually it is a url.
780 *
1dce6261
DC
781 * @return string
782 */
783 public function get_source() {
784 return $this->file_record->source;
785 }
8496fdac 786
67233725
DC
787 /**
788 * Set license
789 *
790 * @param string $license license
791 */
792 public function set_source($source) {
793 $filerecord = new stdClass;
794 $filerecord->source = $source;
795 $this->update($filerecord);
796 }
797
798
f79321f1
DC
799 /**
800 * Returns the sort order of file
801 *
802 * @return int
803 */
804 public function get_sortorder() {
805 return $this->file_record->sortorder;
806 }
67233725
DC
807
808 /**
809 * Set file sort order
810 *
811 * @param int $sortorder
812 * @return int
813 */
814 public function set_sortorder($sortorder) {
815 $filerecord = new stdClass;
816 $filerecord->sortorder = $sortorder;
817 $this->update($filerecord);
818 }
819
820 /**
821 * Returns repository id
822 *
823 * @return int|null
824 */
825 public function get_repository_id() {
826 if (!empty($this->repository)) {
827 return $this->repository->id;
828 } else {
829 return null;
830 }
831 }
832
833 /**
834 * get reference file id
835 * @return int
836 */
837 public function get_referencefileid() {
838 return $this->file_record->referencefileid;
839 }
840
841 /**
842 * Get reference last sync time
843 * @return int
844 */
845 public function get_referencelastsync() {
846 return $this->file_record->referencelastsync;
847 }
848
849 /**
850 * Get reference last sync time
851 * @return int
852 */
853 public function get_referencelifetime() {
854 return $this->file_record->referencelifetime;
855 }
856 /**
857 * Returns file reference
858 *
859 * @return string
860 */
861 public function get_reference() {
862 return $this->file_record->reference;
863 }
864
865 /**
866 * Get human readable file reference information
867 *
868 * @return string
869 */
870 public function get_reference_details() {
0b2bfbd1
MG
871 return $this->repository->get_reference_details($this->get_reference(), $this->get_status());
872 }
873
874 /**
875 * Called after reference-file has been synchronized with the repository
876 *
877 * We update contenthash, filesize and status in files table if changed
878 * and we always update lastsync in files_reference table
879 *
880 * @param type $contenthash
881 * @param type $filesize
882 */
883 public function set_synchronized($contenthash, $filesize, $status = 0) {
884 global $DB;
885 if (!$this->is_external_file()) {
886 return;
887 }
888 $now = time();
889 $filerecord = new stdClass();
890 if ($this->get_contenthash() !== $contenthash) {
891 $filerecord->contenthash = $contenthash;
892 }
893 if ($this->get_filesize() != $filesize) {
894 $filerecord->filesize = $filesize;
895 }
896 if ($this->get_status() != $status) {
897 $filerecord->status = $status;
898 }
899 $filerecord->referencelastsync = $now; // TODO MDL-33416 remove this
900 if (!empty($filerecord)) {
901 $this->update($filerecord);
902 }
903
904 $DB->set_field('files_reference', 'lastsync', $now, array('id'=>$this->get_referencefileid()));
905 // $this->file_record->lastsync = $now; // TODO MDL-33416 uncomment or remove
906 }
907
908 public function set_missingsource() {
909 $this->set_synchronized($this->get_contenthash(), 0, 666);
67233725
DC
910 }
911
912 /**
913 * Send file references
914 *
915 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
916 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
917 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
918 * @param array $options additional options affecting the file serving
919 */
920 public function send_file($lifetime, $filter, $forcedownload, $options) {
921 $this->repository->send_file($this, $lifetime, $filter, $forcedownload, $options);
922 }
f79321f1 923}