Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/filestorage/stored_file.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 
00028 defined('MOODLE_INTERNAL') || die();
00029 
00030 require_once("$CFG->libdir/filestorage/stored_file.php");
00031 
00042 class stored_file {
00044     private $fs;
00046     private $file_record;
00048     private $filedir;
00049 
00057     public function __construct(file_storage $fs, stdClass $file_record, $filedir) {
00058         $this->fs          = $fs;
00059         $this->file_record = clone($file_record); // prevent modifications
00060         $this->filedir     = $filedir; // keep secret, do not expose!
00061     }
00062 
00072     public function is_directory() {
00073         return ($this->file_record->filename === '.');
00074     }
00075 
00084     public function delete() {
00085         global $DB;
00086         $DB->delete_records('files', array('id'=>$this->file_record->id));
00087         // moves pool file to trash if content not needed any more
00088         $this->fs->deleted_file_cleanup($this->file_record->contenthash);
00089         return true; // BC only
00090     }
00091 
00099     protected function get_content_file_location() {
00100         $contenthash = $this->file_record->contenthash;
00101         $l1 = $contenthash[0].$contenthash[1];
00102         $l2 = $contenthash[2].$contenthash[3];
00103         return "$this->filedir/$l1/$l2/$contenthash";
00104     }
00105 
00113     public function add_to_curl_request(&$curlrequest, $key) {
00114         $curlrequest->_tmp_file_post_params[$key] = '@' . $this->get_content_file_location();
00115     }
00116 
00124     public function get_content_file_handle() {
00125         $path = $this->get_content_file_location();
00126         if (!is_readable($path)) {
00127             if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
00128                 throw new file_exception('storedfilecannotread', '', $path);
00129             }
00130         }
00131         return fopen($path, 'rb'); //binary reading only!!
00132     }
00133 
00139     public function readfile() {
00140         $path = $this->get_content_file_location();
00141         if (!is_readable($path)) {
00142             if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
00143                 throw new file_exception('storedfilecannotread', '', $path);
00144             }
00145         }
00146         readfile($path);
00147     }
00148 
00154     public function get_content() {
00155         $path = $this->get_content_file_location();
00156         if (!is_readable($path)) {
00157             if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
00158                 throw new file_exception('storedfilecannotread', '', $path);
00159             }
00160         }
00161         return file_get_contents($this->get_content_file_location());
00162     }
00163 
00170     public function copy_content_to($pathname) {
00171         $path = $this->get_content_file_location();
00172         if (!is_readable($path)) {
00173             if (!$this->fs->try_content_recovery($this) or !is_readable($path)) {
00174                 throw new file_exception('storedfilecannotread', '', $path);
00175             }
00176         }
00177         return copy($path, $pathname);
00178     }
00179 
00186     public function list_files(file_packer $packer) {
00187         $archivefile = $this->get_content_file_location();
00188         return $packer->list_files($archivefile);
00189     }
00190 
00198     public function extract_to_pathname(file_packer $packer, $pathname) {
00199         $archivefile = $this->get_content_file_location();
00200         return $packer->extract_to_pathname($archivefile, $pathname);
00201     }
00202 
00215     public function extract_to_storage(file_packer $packer, $contextid, $component, $filearea, $itemid, $pathbase, $userid = NULL) {
00216         $archivefile = $this->get_content_file_location();
00217         return $packer->extract_to_storage($archivefile, $contextid, $component, $filearea, $itemid, $pathbase);
00218     }
00219 
00227     public function archive_file(file_archive $filearch, $archivepath) {
00228         if ($this->is_directory()) {
00229             return $filearch->add_directory($archivepath);
00230         } else {
00231             $path = $this->get_content_file_location();
00232             if (!is_readable($path)) {
00233                 return false;
00234             }
00235             return $filearch->add_file_from_pathname($archivepath, $path);
00236         }
00237     }
00238 
00244     public function get_imageinfo() {
00245         if (!$imageinfo = getimagesize($this->get_content_file_location())) {
00246             return false;
00247         }
00248         $image = array('width'=>$imageinfo[0], 'height'=>$imageinfo[1], 'mimetype'=>image_type_to_mime_type($imageinfo[2]));
00249         if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) {
00250             // gd can not parse it, sorry
00251             return false;
00252         }
00253         return $image;
00254     }
00255 
00263     public function is_valid_image() {
00264         $mimetype = $this->get_mimetype();
00265         if ($mimetype !== 'image/gif' and $mimetype !== 'image/jpeg' and $mimetype !== 'image/png') {
00266             return false;
00267         }
00268         if (!$info = $this->get_imageinfo()) {
00269             return false;
00270         }
00271         if ($info['mimetype'] !== $mimetype) {
00272             return false;
00273         }
00274         // ok, GD likes this image
00275         return true;
00276     }
00277 
00283     public function get_parent_directory() {
00284         if ($this->file_record->filepath === '/' and $this->file_record->filename === '.') {
00285             //root dir does not have parent
00286             return null;
00287         }
00288 
00289         if ($this->file_record->filename !== '.') {
00290             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);
00291         }
00292 
00293         $filepath = $this->file_record->filepath;
00294         $filepath = trim($filepath, '/');
00295         $dirs = explode('/', $filepath);
00296         array_pop($dirs);
00297         $filepath = implode('/', $dirs);
00298         $filepath = ($filepath === '') ? '/' : "/$filepath/";
00299 
00300         return $this->fs->create_directory($this->file_record->contextid, $this->file_record->component, $this->file_record->filearea, $this->file_record->itemid, $filepath);
00301     }
00302 
00308     public function get_contextid() {
00309         return $this->file_record->contextid;
00310     }
00311 
00318     public function get_component() {
00319         return $this->file_record->component;
00320     }
00321 
00328     public function get_filearea() {
00329         return $this->file_record->filearea;
00330     }
00331 
00337     public function get_itemid() {
00338         return $this->file_record->itemid;
00339     }
00340 
00346     public function get_filepath() {
00347         return $this->file_record->filepath;
00348     }
00349 
00355     public function get_filename() {
00356         return $this->file_record->filename;
00357     }
00358 
00364     public function get_userid() {
00365         return $this->file_record->userid;
00366     }
00367 
00373     public function get_filesize() {
00374         return $this->file_record->filesize;
00375     }
00376 
00382     public function get_mimetype() {
00383         return $this->file_record->mimetype;
00384     }
00385 
00391     public function get_timecreated() {
00392         return $this->file_record->timecreated;
00393     }
00394 
00400     public function get_timemodified() {
00401         return $this->file_record->timemodified;
00402     }
00403 
00409     public function get_status() {
00410         return $this->file_record->status;
00411     }
00412 
00418     public function get_id() {
00419         return $this->file_record->id;
00420     }
00421 
00427     public function get_contenthash() {
00428         return $this->file_record->contenthash;
00429     }
00430 
00436     public function get_pathnamehash() {
00437         return $this->file_record->pathnamehash;
00438     }
00439 
00445     public function get_license() {
00446         return $this->file_record->license;
00447     }
00448 
00454     public function get_author() {
00455         return $this->file_record->author;
00456     }
00457 
00463     public function get_source() {
00464         return $this->file_record->source;
00465     }
00466 
00472     public function get_sortorder() {
00473         return $this->file_record->sortorder;
00474     }
00475 }
 All Data Structures Namespaces Files Functions Variables Enumerations