MDL-14589 course cat areas not writable
[moodle.git] / lib / file / stored_file.php
CommitLineData
172dd12c 1<?php //$Id$
2
3/**
4 * Class representing local files stored in sha1 file pool
5 */
6class stored_file {
7 private $fs;
8 private $file_record;
9
10 /**
11 * Constructor
12 * @param object $fs file storage instance
13 * @param object $file_record description of file
14 */
15 public function __construct($fs, $file_record) {
16 $this->fs = $fs;
17 $this->file_record = clone($file_record);
18 }
19
20 /**
21 * Is this a directory?
22 * @return bool
23 */
24 public function is_directory() {
25 return $this->file_record->filename === '.';
26 }
27
28 /**
29 * Delete file
30 * @return success
31 */
32 public function delete() {
33 global $DB;
34 $this->fs->mark_delete_candidate($this->file_record->contenthash);
35 return $DB->delete_records('files', array('id'=>$this->file_record->id));
36 }
37
38 /**
6c0e2d08 39 * Protected - developers must not gain direct access to this function
17d9269f 40 * NOTE: do not make this public, we must not modify or delete the pool files directly! ;-)
41 * @return ful path to pool file with file content
172dd12c 42 **/
43 protected function get_content_file_location() {
17d9269f 44 global $CFG;
45 if (isset($CFG->filedir)) {
46 $filedir = $CFG->filedir;
47 } else {
48 $filedir = $CFG->dataroot.'/filedir';
49 }
50 $contenthash = $this->file_record->contenthash;
51 $l1 = $contenthash[0].$contenthash[1];
52 $l2 = $contenthash[2].$contenthash[3];
53 $l3 = $contenthash[4].$contenthash[5];
54 return "$filedir/$l1/$l2/$l3/$contenthash";
172dd12c 55 }
56
57 /**
58 * Returns file handle - read only mode, no writing allowed into pool files!
59 * @return file handle
60 */
61 public function get_content_file_handle() {
62 $path = $this->get_content_file_location();
63 if (!is_readable($path)) {
64 throw new file_exception('localfilecannotread');
65 }
66 return fopen($path, 'rb'); //binary reading only!!
67 }
68
69 /**
70 * Dumps file content to page
71 * @return file handle
72 */
73 public function readfile() {
74 $path = $this->get_content_file_location();
75 if (!is_readable($path)) {
76 throw new file_exception('localfilecannotread');
77 }
78 readfile($path);
79 }
80
81 /**
82 * Returns file content as string
83 * @return string content
84 */
85 public function get_content() {
86 $path = $this->get_content_file_location();
87 if (!is_readable($path)) {
88 throw new file_exception('localfilecannotread');
89 }
90 return file_get_contents($this->get_content_file_location());
91 }
92
6c0e2d08 93 /**
94 * Copy content of file to give npathname
17d9269f 95 * @param string $pathnema rela path to new file
6c0e2d08 96 * @return bool success
97 */
98 public function copy_content_to($pathname) {
99 $path = $this->get_content_file_location();
100 if (!is_readable($path)) {
101 throw new file_exception('localfilecannotread');
102 }
103 return copy($path, $pathname);
104 }
105
17d9269f 106 /**
107 * Unzip file to given file path (real OS filesystem), existing files are overwrited
108 * @param string $path target directory
109 * @return mixed list of processed files; false if error
110 */
111 public function unzip_files_to_pathname($path) {
112 $packer = get_file_packer();
113 $zipfile = $this->get_content_file_location();
114 return $packer->unzip_files_to_pathname($path, $path);
115 }
116
117 /**
118 * Unzip file to given file path (real OS filesystem), existing files are overwrited
119 * @param int $contextid
120 * @param string $filearea
121 * @param int $itemid
122 * @param string $pathbase
123 * @param int $userid
124 * @return mixed list of processed files; false if error
125 */
126 public function unzip_files_to_storage($contextid, $filearea, $itemid, $pathbase, $userid=null) {
127 $packer = get_file_packer();
128 $zipfile = $this->get_content_file_location();
129 return $packer->unzip_files_to_storage($zipfile, $contextid, $filearea, $itemid, $pathbase);
130 }
131
172dd12c 132 public function get_contextid() {
133 return $this->file_record->contextid;
134 }
135
136 public function get_filearea() {
137 return $this->file_record->filearea;
138 }
139
140 public function get_itemid() {
141 return $this->file_record->itemid;
142 }
143
144 public function get_filepath() {
145 return $this->file_record->filepath;
146 }
147
148 public function get_filename() {
149 return $this->file_record->filename;
150 }
151
152 public function get_userid() {
153 return $this->file_record->userid;
154 }
155
156 public function get_filesize() {
157 return $this->file_record->filesize;
158 }
159
160 public function get_mimetype() {
161 return $this->file_record->mimetype;
162 }
163
164 public function get_timecreated() {
165 return $this->file_record->timecreated;
166 }
167
168 public function get_timemodified() {
169 return $this->file_record->timemodified;
170 }
6c0e2d08 171
172 public function get_status() {
173 return $this->file_record->status;
174 }
172dd12c 175}