Updated the HEAD build version to 20080913
[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
172dd12c 165 public function get_contextid() {
166 return $this->file_record->contextid;
167 }
168
169 public function get_filearea() {
170 return $this->file_record->filearea;
171 }
172
173 public function get_itemid() {
174 return $this->file_record->itemid;
175 }
176
177 public function get_filepath() {
178 return $this->file_record->filepath;
179 }
180
181 public function get_filename() {
182 return $this->file_record->filename;
183 }
184
185 public function get_userid() {
186 return $this->file_record->userid;
187 }
188
189 public function get_filesize() {
190 return $this->file_record->filesize;
191 }
192
193 public function get_mimetype() {
194 return $this->file_record->mimetype;
195 }
196
197 public function get_timecreated() {
198 return $this->file_record->timecreated;
199 }
200
201 public function get_timemodified() {
202 return $this->file_record->timemodified;
203 }
6c0e2d08 204
205 public function get_status() {
206 return $this->file_record->status;
207 }
ee03a651 208
209 public function get_id() {
210 return $this->file_record->id;
211 }
4284e1cc 212
213 public function get_contenthash() {
214 return $this->file_record->contenthash;
215 }
172dd12c 216}