MDL-15906 removing obsoleted code, now in filelib.php
[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() {
744b64ff 44 $filedir = $this->fs->get_filedir();
17d9269f 45 $contenthash = $this->file_record->contenthash;
46 $l1 = $contenthash[0].$contenthash[1];
47 $l2 = $contenthash[2].$contenthash[3];
48 $l3 = $contenthash[4].$contenthash[5];
49 return "$filedir/$l1/$l2/$l3/$contenthash";
172dd12c 50 }
51
5035a8b4 52 /**
53 * adds this file path to a curl request (POST only)
54 *
55 * @param curl $curlrequest the curl request object
56 * @param string $key what key to use in the POST request
57 */
58 public function add_to_curl_request(&$curlrequest, $key) {
59 $curlrequest->_tmp_file_post_params[$key] = '@' . $this->get_content_file_location();
60 }
61
172dd12c 62 /**
63 * Returns file handle - read only mode, no writing allowed into pool files!
64 * @return file handle
65 */
66 public function get_content_file_handle() {
67 $path = $this->get_content_file_location();
68 if (!is_readable($path)) {
145a0a31 69 throw new file_exception('storedfilecannotread');
172dd12c 70 }
71 return fopen($path, 'rb'); //binary reading only!!
72 }
73
74 /**
75 * Dumps file content to page
76 * @return file handle
77 */
78 public function readfile() {
79 $path = $this->get_content_file_location();
80 if (!is_readable($path)) {
145a0a31 81 throw new file_exception('storedfilecannotread');
172dd12c 82 }
83 readfile($path);
84 }
85
86 /**
87 * Returns file content as string
88 * @return string content
89 */
90 public function get_content() {
91 $path = $this->get_content_file_location();
92 if (!is_readable($path)) {
145a0a31 93 throw new file_exception('storedfilecannotread');
172dd12c 94 }
95 return file_get_contents($this->get_content_file_location());
96 }
97
6c0e2d08 98 /**
99 * Copy content of file to give npathname
17d9269f 100 * @param string $pathnema rela path to new file
6c0e2d08 101 * @return bool success
102 */
103 public function copy_content_to($pathname) {
104 $path = $this->get_content_file_location();
105 if (!is_readable($path)) {
145a0a31 106 throw new file_exception('storedfilecannotread');
6c0e2d08 107 }
108 return copy($path, $pathname);
109 }
110
17d9269f 111 /**
c78a0558 112 * List contents of archive
113 * @param object $file_packer
114 * @return array of file infos
115 */
116 public function list_files(file_packer $packer) {
117 $archivefile = $this->get_content_file_location();
118 return $packer->list_files($archivefile);
119 }
120
121 /**
122 * Extract file to given file path (real OS filesystem), existing files are overwrited
0b0bfa93 123 * @param object $file_packer
124 * @param string $pathname target directory
17d9269f 125 * @return mixed list of processed files; false if error
126 */
0b0bfa93 127 public function extract_to_pathname(file_packer $packer, $pathname) {
128 $archivefile = $this->get_content_file_location();
129 return $packer->extract_to_pathname($archivefile, $pathname);
17d9269f 130 }
131
132 /**
c78a0558 133 * Extract file to given file path (real OS filesystem), existing files are overwrited
0b0bfa93 134 * @param object $file_packer
17d9269f 135 * @param int $contextid
136 * @param string $filearea
137 * @param int $itemid
138 * @param string $pathbase
139 * @param int $userid
140 * @return mixed list of processed files; false if error
141 */
0b0bfa93 142 public function extract_to_storage(file_packer $packer, $contextid, $filearea, $itemid, $pathbase, $userid=null) {
143 $archivefile = $this->get_content_file_location();
144 return $packer->extract_to_storage($archivefile, $contextid, $filearea, $itemid, $pathbase);
17d9269f 145 }
146
b1897a6d 147 /**
c78a0558 148 * Add file/directory into archive
149 * @param object $filearch
150 * @param string $archivepath pathname in archive
b1897a6d 151 * @return bool success
152 */
0b0bfa93 153 public function archive_file(file_archive $filearch, $archivepath) {
b1897a6d 154 if ($this->is_directory()) {
0b0bfa93 155 return $filearch->add_directory($archivepath);
b1897a6d 156 } else {
157 $path = $this->get_content_file_location();
158 if (!is_readable($path)) {
159 return false;
160 }
0b0bfa93 161 return $filearch->add_file_from_pathname($archivepath, $path);
b1897a6d 162 }
163 }
164
797f19e8 165 /**
166 * Returns information about image,
167 * information is determined from the file content
168 * @return mixed array with width, height and mimetype; false if not an image
169 */
170 public function get_imageinfo() {
171 if (!$imageinfo = getimagesize($this->get_content_file_location())) {
172 return false;
173 }
174 $image = array('width'=>$imageinfo[0], 'height'=>$imageinfo[1], 'mimetype'=>image_type_to_mime_type($imageinfo[2]));
175 if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) {
176 // gd can not parse it, sorry
177 return false;
178 }
179 return $image;
180 }
181
182 /**
183 * Verifies the file is a valid web image - gif, png and jpeg only.
184 * It should be ok to serve this image from server without any other security workarounds.
185 * @return bool true if file ok
186 */
187 public function is_valid_image() {
188 $mimetype = $this->get_mimetype();
189 if ($mimetype !== 'image/gif' and $mimetype !== 'image/jpeg' and $mimetype !== 'image/png') {
190 return false;
191 }
192 if (!$info = $this->get_imageinfo()) {
193 return false;
194 }
195 if ($info['mimetype'] !== $mimetype) {
196 return false;
197 }
198 // ok, GD likes this image
199 return true;
200 }
201
4b6b5ce7 202 /**
203 * Returns parent directory, creates missing parents if needed
204 * @return object stored_file
205 */
206 public function get_parent_directory() {
207 if ($this->file_record->filepath === '/' and $this->file_record->filename === '.') {
208 //root dir does not have parent
209 return null;
210 }
211
212 if ($this->file_record->filename !== '.') {
213 return $this->fs->create_directory($this->file_record->contextid, $this->file_record->filearea, $this->file_record->itemid, $this->file_record->filepath);
214 }
215
216 $filepath = $this->file_record->filepath;
217 $filepath = trim($filepath, '/');
218 $dirs = explode('/', $filepath);
219 array_pop($dirs);
220 $filepath = implode('/', $dirs);
221 $filepath = ($filepath === '') ? '/' : "/$filepath/";
222
223 return $this->fs->create_directory($this->file_record->contextid, $this->file_record->filearea, $this->file_record->itemid, $filepath);
224 }
225
172dd12c 226 public function get_contextid() {
227 return $this->file_record->contextid;
228 }
229
230 public function get_filearea() {
231 return $this->file_record->filearea;
232 }
233
234 public function get_itemid() {
235 return $this->file_record->itemid;
236 }
237
238 public function get_filepath() {
239 return $this->file_record->filepath;
240 }
241
242 public function get_filename() {
243 return $this->file_record->filename;
244 }
245
246 public function get_userid() {
247 return $this->file_record->userid;
248 }
249
250 public function get_filesize() {
251 return $this->file_record->filesize;
252 }
253
254 public function get_mimetype() {
255 return $this->file_record->mimetype;
256 }
257
258 public function get_timecreated() {
259 return $this->file_record->timecreated;
260 }
261
262 public function get_timemodified() {
263 return $this->file_record->timemodified;
264 }
6c0e2d08 265
266 public function get_status() {
267 return $this->file_record->status;
268 }
ee03a651 269
270 public function get_id() {
271 return $this->file_record->id;
272 }
4284e1cc 273
274 public function get_contenthash() {
275 return $this->file_record->contenthash;
276 }
6ed19c74 277
278 public function get_pathnamehash() {
279 return $this->file_record->pathnamehash;
280 }
172dd12c 281}