2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Utility class for browsing of stored files.
22 * @copyright 2008 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Represents an actual file or folder - a row in the file table in the tree navigated by {@link file_browser}.
32 * @copyright 2008 Petr Skoda (http://skodak.org)
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class file_info_stored
extends file_info
{
36 /** @var stored_file|virtual_root_file stored_file or virtual_root_file instance */
38 /** @var string the serving script */
40 /** @var string the human readable name of this area */
41 protected $topvisiblename;
42 /** @var int|bool it's false if itemid is 0 and not included in URL */
43 protected $itemidused;
44 /** @var bool allow file reading */
45 protected $readaccess;
46 /** @var bool allow file write, delee */
47 protected $writeaccess;
48 /** @var string do not show links to parent context/area */
54 * @param file_browser $browser file browser instance
55 * @param stdClass $context context object
56 * @param stored_file|virtual_root_file $storedfile stored_file instance
57 * @param string $urlbase the serving script - usually the $CFG->wwwroot/.'pluginfile.php'
58 * @param string $topvisiblename the human readable name of this area
59 * @param int|bool $itemidused false if itemid always 0 and not included in URL
60 * @param bool $readaccess allow file reading
61 * @param bool $writeaccess allow file write, delete
62 * @param string $areaonly do not show links to parent context/area
64 public function __construct(file_browser
$browser, $context, $storedfile, $urlbase, $topvisiblename, $itemidused, $readaccess, $writeaccess, $areaonly) {
65 parent
::__construct($browser, $context);
67 $this->lf
= $storedfile;
68 $this->urlbase
= $urlbase;
69 $this->topvisiblename
= $topvisiblename;
70 $this->itemidused
= $itemidused;
71 $this->readaccess
= $readaccess;
72 $this->writeaccess
= $writeaccess;
73 $this->areaonly
= $areaonly;
77 * Returns list of standard virtual file/directory identification.
78 * The difference from stored_file parameters is that null values
79 * are allowed in all fields
81 * @return array with keys contextid, component, filearea, itemid, filepath and filename
83 public function get_params() {
84 return array('contextid'=>$this->context
->id
,
85 'component'=>$this->lf
->get_component(),
86 'filearea' =>$this->lf
->get_filearea(),
87 'itemid' =>$this->lf
->get_itemid(),
88 'filepath' =>$this->lf
->get_filepath(),
89 'filename' =>$this->lf
->get_filename());
93 * Returns localised visible name.
97 public function get_visible_name() {
98 $filename = $this->lf
->get_filename();
99 $filepath = $this->lf
->get_filepath();
101 if ($filename !== '.') {
105 $dir = trim($filepath, '/');
106 $dir = explode('/', $dir);
107 $dir = array_pop($dir);
109 return $this->topvisiblename
;
117 * Returns file download url
119 * @param bool $forcedownload Whether or not force download
120 * @param bool $https whether or not force https
123 public function get_url($forcedownload=false, $https=false) {
124 if (!$this->is_readable()) {
128 if ($this->is_directory()) {
133 $contextid = $this->lf
->get_contextid();
134 $component = $this->lf
->get_component();
135 $filearea = $this->lf
->get_filearea();
136 $itemid = $this->lf
->get_itemid();
137 $filepath = $this->lf
->get_filepath();
138 $filename = $this->lf
->get_filename();
140 if ($this->itemidused
) {
141 $path = '/'.$contextid.'/'.$component.'/'.$filearea.'/'.$itemid.$filepath.$filename;
143 $path = '/'.$contextid.'/'.$component.'/'.$filearea.$filepath.$filename;
145 return file_encode_url($this->urlbase
, $path, $forcedownload, $https);
149 * Whether or not I can read content of this file or enter directory
153 public function is_readable() {
154 return $this->readaccess
;
158 * Whether or not new files or directories can be added
162 public function is_writable() {
163 return $this->writeaccess
;
167 * Whether or not this is an empty area
171 public function is_empty_area() {
172 if ($this->lf
->get_filepath() === '/' and $this->lf
->get_filename() === '.') {
173 // test the emptiness only in the top most level, it does not make sense at lower levels
174 $fs = get_file_storage();
175 return $fs->is_area_empty($this->lf
->get_contextid(), $this->lf
->get_component(), $this->lf
->get_filearea(), $this->lf
->get_itemid());
182 * Returns file size in bytes, null for directories
184 * @return int bytes or null if not known
186 public function get_filesize() {
187 return $this->lf
->get_filesize();
193 * @return string mimetype or null if not known
195 public function get_mimetype() {
196 return $this->lf
->get_mimetype();
200 * Returns time created unix timestamp if known
202 * @return int timestamp or null
204 public function get_timecreated() {
205 return $this->lf
->get_timecreated();
209 * Returns time modified unix timestamp if known
211 * @return int timestamp or null
213 public function get_timemodified() {
214 return $this->lf
->get_timemodified();
218 * Whether or not this is a directory
222 public function is_directory() {
223 return $this->lf
->is_directory();
227 * Returns the license type of the file
229 * @return string license short name or null
231 public function get_license() {
232 return $this->lf
->get_license();
236 * Returns the author name of the file
238 * @return string author name or null
240 public function get_author() {
241 return $this->lf
->get_author();
245 * Returns the source of the file
247 * @return string a source url or null
249 public function get_source() {
250 return $this->lf
->get_source();
254 * Returns the sort order of the file
258 public function get_sortorder() {
259 return $this->lf
->get_sortorder();
263 * Returns list of children.
265 * @return array of file_info instances
267 public function get_children() {
268 if (!$this->lf
->is_directory()) {
273 $fs = get_file_storage();
275 $storedfiles = $fs->get_directory_files($this->context
->id
, $this->lf
->get_component(), $this->lf
->get_filearea(), $this->lf
->get_itemid(),
276 $this->lf
->get_filepath(), false, true, "filepath, filename");
277 foreach ($storedfiles as $file) {
278 $result[] = new file_info_stored($this->browser
, $this->context
, $file, $this->urlbase
, $this->topvisiblename
,
279 $this->itemidused
, $this->readaccess
, $this->writeaccess
, false);
286 * Returns parent file_info instance
288 * @return file_info|null file_info instance or null for root
290 public function get_parent() {
291 if ($this->lf
->get_filepath() === '/' and $this->lf
->is_directory()) {
292 if ($this->areaonly
) {
294 } else if ($this->itemidused
) {
295 return $this->browser
->get_file_info($this->context
, $this->lf
->get_component(), $this->lf
->get_filearea());
297 return $this->browser
->get_file_info($this->context
);
301 if (!$this->lf
->is_directory()) {
302 return $this->browser
->get_file_info($this->context
, $this->lf
->get_component(), $this->lf
->get_filearea(), $this->lf
->get_itemid(), $this->lf
->get_filepath(), '.');
305 $filepath = $this->lf
->get_filepath();
306 $filepath = trim($filepath, '/');
307 $dirs = explode('/', $filepath);
309 $filepath = implode('/', $dirs);
310 $filepath = ($filepath === '') ?
'/' : "/$filepath/";
312 return $this->browser
->get_file_info($this->context
, $this->lf
->get_component(), $this->lf
->get_filearea(), $this->lf
->get_itemid(), $filepath, '.');
316 * Create new directory, may throw exception - make sure
319 * @param string $newdirname name of new directory
320 * @param int $userid id of author, default $USER->id
321 * @return file_info|null new directory's file_info instance or null if failed
323 public function create_directory($newdirname, $userid = NULL) {
324 if (!$this->is_writable() or !$this->lf
->is_directory()) {
328 $newdirname = clean_param($newdirname, PARAM_FILE
);
329 if ($newdirname === '') {
333 $filepath = $this->lf
->get_filepath().'/'.$newdirname.'/';
335 $fs = get_file_storage();
337 if ($file = $fs->create_directory($this->lf
->get_contextid(), $this->lf
->get_component(), $this->lf
->get_filearea(), $this->lf
->get_itemid(), $filepath, $userid)) {
338 return $this->browser
->get_file_info($this->context
, $this->lf
->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
345 * Create new file from string - make sure
348 * @param string $newfilename name of new file
349 * @param string $content of file
350 * @param int $userid id of author, default $USER->id
351 * @return file_info|null new file's file_info instance or null if failed
353 public function create_file_from_string($newfilename, $content, $userid = NULL) {
354 if (!$this->is_writable() or !$this->lf
->is_directory()) {
358 $newfilename = clean_param($newfilename, PARAM_FILE
);
359 if ($newfilename === '') {
363 $fs = get_file_storage();
367 $newrecord = new stdClass();
368 $newrecord->contextid
= $this->lf
->get_contextid();
369 $newrecord->component
= $this->lf
->get_component();
370 $newrecord->filearea
= $this->lf
->get_filearea();
371 $newrecord->itemid
= $this->lf
->get_itemid();
372 $newrecord->filepath
= $this->lf
->get_filepath();
373 $newrecord->filename
= $newfilename;
375 if ($fs->file_exists($newrecord->contextid
, $newrecord->component
, $newrecord->filearea
, $newrecord->itemid
, $newrecord->filepath
, $newrecord->filename
)) {
376 // file already exists, sorry
380 $newrecord->timecreated
= $now;
381 $newrecord->timemodified
= $now;
382 $newrecord->mimetype
= mimeinfo('type', $newfilename);
383 $newrecord->userid
= $userid;
385 if ($file = $fs->create_file_from_string($newrecord, $content)) {
386 return $this->browser
->get_file_info($this->context
, $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
392 * Create new file from pathname - make sure
395 * @param string $newfilename name of new file
396 * @param string $pathname location of file
397 * @param int $userid id of author, default $USER->id
398 * @return file_info|null new file's file_info instance or null if failed
400 public function create_file_from_pathname($newfilename, $pathname, $userid = NULL) {
401 if (!$this->is_writable() or !$this->lf
->is_directory()) {
405 $newfilename = clean_param($newfilename, PARAM_FILE
);
406 if ($newfilename === '') {
410 $fs = get_file_storage();
414 $newrecord = new stdClass();
415 $newrecord->contextid
= $this->lf
->get_contextid();
416 $newrecord->component
= $this->lf
->get_component();
417 $newrecord->filearea
= $this->lf
->get_filearea();
418 $newrecord->itemid
= $this->lf
->get_itemid();
419 $newrecord->filepath
= $this->lf
->get_filepath();
420 $newrecord->filename
= $newfilename;
422 if ($fs->file_exists($newrecord->contextid
, $newrecord->component
, $newrecord->filearea
, $newrecord->itemid
, $newrecord->filepath
, $newrecord->filename
)) {
423 // file already exists, sorry
427 $newrecord->timecreated
= $now;
428 $newrecord->timemodified
= $now;
429 $newrecord->mimetype
= mimeinfo('type', $newfilename);
430 $newrecord->userid
= $userid;
432 if ($file = $fs->create_file_from_pathname($newrecord, $pathname)) {
433 return $this->browser
->get_file_info($this->context
, $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
439 * Create new file from stored file - make sure
442 * @param string $newfilename name of new file
443 * @param int|stored_file $fid file id or stored_file of file
444 * @param int $userid id of author, default $USER->id
445 * @return file_info|null new file's file_info instance or null if failed
447 public function create_file_from_storedfile($newfilename, $fid, $userid = NULL) {
448 if (!$this->is_writable() or $this->lf
->get_filename() !== '.') {
452 $newfilename = clean_param($newfilename, PARAM_FILE
);
453 if ($newfilename === '') {
457 $fs = get_file_storage();
461 $newrecord = new stdClass();
462 $newrecord->contextid
= $this->lf
->get_contextid();
463 $newrecord->component
= $this->lf
->get_component();
464 $newrecord->filearea
= $this->lf
->get_filearea();
465 $newrecord->itemid
= $this->lf
->get_itemid();
466 $newrecord->filepath
= $this->lf
->get_filepath();
467 $newrecord->filename
= $newfilename;
469 if ($fs->file_exists($newrecord->contextid
, $newrecord->component
, $newrecord->filearea
, $newrecord->itemid
, $newrecord->filepath
, $newrecord->filename
)) {
470 // file already exists, sorry
474 $newrecord->timecreated
= $now;
475 $newrecord->timemodified
= $now;
476 $newrecord->mimetype
= mimeinfo('type', $newfilename);
477 $newrecord->userid
= $userid;
479 if ($file = $fs->create_file_from_storedfile($newrecord, $fid)) {
480 return $this->browser
->get_file_info($this->context
, $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
486 * Delete file, make sure file is deletable first.
488 * @return bool success
490 public function delete() {
491 if (!$this->is_writable()) {
495 if ($this->is_directory()) {
496 $filepath = $this->lf
->get_filepath();
497 $fs = get_file_storage();
498 $storedfiles = $fs->get_area_files($this->context
->id
, $this->get_component(), $this->lf
->get_filearea(), $this->lf
->get_itemid(), "");
499 foreach ($storedfiles as $file) {
500 if (strpos($file->get_filepath(), $filepath) === 0) {
506 return $this->lf
->delete();
510 * Copy content of this file to local storage, overriding current file if needed.
512 * @param int $contextid context ID
513 * @param string $component file component
514 * @param string $filearea file area
515 * @param int $itemid item ID
516 * @param string $filepath file path
517 * @param string $filename file name
518 * @return bool success
520 public function copy_to_storage($contextid, $component, $filearea, $itemid, $filepath, $filename) {
521 if (!$this->is_readable() or $this->is_directory()) {
525 $fs = get_file_storage();
526 if ($existing = $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename)) {
529 $file_record = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename);
530 $fs->create_file_from_storedfile($file_record, $this->lf
);
536 * Copy content of this file to local storage, overriding current file if needed.
538 * @param string $pathname real local full file name
539 * @return bool success
541 public function copy_to_pathname($pathname) {
542 if (!$this->is_readable() or $this->is_directory()) {
546 if (file_exists($pathname)) {
547 if (!unlink($pathname)) {
552 $this->lf
->copy_content_to($pathname);