|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00039 class file_info_stored extends file_info { 00040 protected $lf; 00041 protected $urlbase; 00042 protected $topvisiblename; 00043 protected $itemidused; 00044 protected $readaccess; 00045 protected $writeaccess; 00046 protected $areaonly; 00047 00061 public function __construct(file_browser $browser, $context, $storedfile, $urlbase, $topvisiblename, $itemidused, $readaccess, $writeaccess, $areaonly) { 00062 parent::__construct($browser, $context); 00063 00064 $this->lf = $storedfile; 00065 $this->urlbase = $urlbase; 00066 $this->topvisiblename = $topvisiblename; 00067 $this->itemidused = $itemidused; 00068 $this->readaccess = $readaccess; 00069 $this->writeaccess = $writeaccess; 00070 $this->areaonly = $areaonly; 00071 } 00072 00079 public function get_params() { 00080 return array('contextid'=>$this->context->id, 00081 'component'=>$this->lf->get_component(), 00082 'filearea' =>$this->lf->get_filearea(), 00083 'itemid' =>$this->lf->get_itemid(), 00084 'filepath' =>$this->lf->get_filepath(), 00085 'filename' =>$this->lf->get_filename()); 00086 } 00087 00092 public function get_visible_name() { 00093 $filename = $this->lf->get_filename(); 00094 $filepath = $this->lf->get_filepath(); 00095 00096 if ($filename !== '.') { 00097 return $filename; 00098 00099 } else { 00100 $dir = trim($filepath, '/'); 00101 $dir = explode('/', $dir); 00102 $dir = array_pop($dir); 00103 if ($dir === '') { 00104 return $this->topvisiblename; 00105 } else { 00106 return $dir; 00107 } 00108 } 00109 } 00110 00117 public function get_url($forcedownload=false, $https=false) { 00118 if (!$this->is_readable()) { 00119 return null; 00120 } 00121 00122 if ($this->is_directory()) { 00123 return null; 00124 } 00125 00126 $this->urlbase; 00127 $contextid = $this->lf->get_contextid(); 00128 $component = $this->lf->get_component(); 00129 $filearea = $this->lf->get_filearea(); 00130 $itemid = $this->lf->get_itemid(); 00131 $filepath = $this->lf->get_filepath(); 00132 $filename = $this->lf->get_filename(); 00133 00134 if ($this->itemidused) { 00135 $path = '/'.$contextid.'/'.$component.'/'.$filearea.'/'.$itemid.$filepath.$filename; 00136 } else { 00137 $path = '/'.$contextid.'/'.$component.'/'.$filearea.$filepath.$filename; 00138 } 00139 return file_encode_url($this->urlbase, $path, $forcedownload, $https); 00140 } 00141 00146 public function is_readable() { 00147 return $this->readaccess; 00148 } 00149 00154 public function is_writable() { 00155 return $this->writeaccess; 00156 } 00157 00163 public function is_empty_area() { 00164 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') { 00165 // test the emptiness only in the top most level, it does not make sense at lower levels 00166 $fs = get_file_storage(); 00167 return $fs->is_area_empty($this->lf->get_contextid(), $this->lf->get_component(), $this->lf->get_filearea(), $this->lf->get_itemid()); 00168 } else { 00169 return false; 00170 } 00171 } 00172 00177 public function get_filesize() { 00178 return $this->lf->get_filesize(); 00179 } 00180 00185 public function get_mimetype() { 00186 return $this->lf->get_mimetype(); 00187 } 00188 00193 public function get_timecreated() { 00194 return $this->lf->get_timecreated(); 00195 } 00196 00201 public function get_timemodified() { 00202 return $this->lf->get_timemodified(); 00203 } 00204 00209 public function is_directory() { 00210 return $this->lf->is_directory(); 00211 } 00212 00217 public function get_license() { 00218 return $this->lf->get_license(); 00219 } 00220 00225 public function get_author() { 00226 return $this->lf->get_author(); 00227 } 00228 00233 public function get_source() { 00234 return $this->lf->get_source(); 00235 } 00236 00241 public function get_sortorder() { 00242 return $this->lf->get_sortorder(); 00243 } 00244 00249 public function get_children() { 00250 if (!$this->lf->is_directory()) { 00251 return array(); 00252 } 00253 00254 $result = array(); 00255 $fs = get_file_storage(); 00256 00257 $storedfiles = $fs->get_directory_files($this->context->id, $this->lf->get_component(), $this->lf->get_filearea(), $this->lf->get_itemid(), 00258 $this->lf->get_filepath(), false, true, "filepath, filename"); 00259 foreach ($storedfiles as $file) { 00260 $result[] = new file_info_stored($this->browser, $this->context, $file, $this->urlbase, $this->topvisiblename, 00261 $this->itemidused, $this->readaccess, $this->writeaccess, false); 00262 } 00263 00264 return $result; 00265 } 00266 00271 public function get_parent() { 00272 if ($this->lf->get_filepath() === '/' and $this->lf->is_directory()) { 00273 if ($this->areaonly) { 00274 return null; 00275 } else if ($this->itemidused) { 00276 return $this->browser->get_file_info($this->context, $this->lf->get_component(), $this->lf->get_filearea()); 00277 } else { 00278 return $this->browser->get_file_info($this->context); 00279 } 00280 } 00281 00282 if (!$this->lf->is_directory()) { 00283 return $this->browser->get_file_info($this->context, $this->lf->get_component(), $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(), '.'); 00284 } 00285 00286 $filepath = $this->lf->get_filepath(); 00287 $filepath = trim($filepath, '/'); 00288 $dirs = explode('/', $filepath); 00289 array_pop($dirs); 00290 $filepath = implode('/', $dirs); 00291 $filepath = ($filepath === '') ? '/' : "/$filepath/"; 00292 00293 return $this->browser->get_file_info($this->context, $this->lf->get_component(), $this->lf->get_filearea(), $this->lf->get_itemid(), $filepath, '.'); 00294 } 00295 00303 public function create_directory($newdirname, $userid = NULL) { 00304 if (!$this->is_writable() or !$this->lf->is_directory()) { 00305 return null; 00306 } 00307 00308 $newdirname = clean_param($newdirname, PARAM_FILE); 00309 if ($newdirname === '') { 00310 return null; 00311 } 00312 00313 $filepath = $this->lf->get_filepath().'/'.$newdirname.'/'; 00314 00315 $fs = get_file_storage(); 00316 00317 if ($file = $fs->create_directory($this->lf->get_contextid(), $this->lf->get_component(), $this->lf->get_filearea(), $this->lf->get_itemid(), $filepath, $userid)) { 00318 return $this->browser->get_file_info($this->context, $this->lf->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); 00319 } 00320 return null; 00321 } 00322 00323 00332 public function create_file_from_string($newfilename, $content, $userid = NULL) { 00333 if (!$this->is_writable() or !$this->lf->is_directory()) { 00334 return null; 00335 } 00336 00337 $newfilename = clean_param($newfilename, PARAM_FILE); 00338 if ($newfilename === '') { 00339 return null; 00340 } 00341 00342 $fs = get_file_storage(); 00343 00344 $now = time(); 00345 00346 $newrecord = new stdClass(); 00347 $newrecord->contextid = $this->lf->get_contextid(); 00348 $newrecord->component = $this->lf->get_component(); 00349 $newrecord->filearea = $this->lf->get_filearea(); 00350 $newrecord->itemid = $this->lf->get_itemid(); 00351 $newrecord->filepath = $this->lf->get_filepath(); 00352 $newrecord->filename = $newfilename; 00353 00354 if ($fs->file_exists($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) { 00355 // file already exists, sorry 00356 return null; 00357 } 00358 00359 $newrecord->timecreated = $now; 00360 $newrecord->timemodified = $now; 00361 $newrecord->mimetype = mimeinfo('type', $newfilename); 00362 $newrecord->userid = $userid; 00363 00364 if ($file = $fs->create_file_from_string($newrecord, $content)) { 00365 return $this->browser->get_file_info($this->context, $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); 00366 } 00367 return null; 00368 } 00369 00378 public function create_file_from_pathname($newfilename, $pathname, $userid = NULL) { 00379 if (!$this->is_writable() or !$this->lf->is_directory()) { 00380 return null; 00381 } 00382 00383 $newfilename = clean_param($newfilename, PARAM_FILE); 00384 if ($newfilename === '') { 00385 return null; 00386 } 00387 00388 $fs = get_file_storage(); 00389 00390 $now = time(); 00391 00392 $newrecord = new stdClass(); 00393 $newrecord->contextid = $this->lf->get_contextid(); 00394 $newrecord->component = $this->lf->get_component(); 00395 $newrecord->filearea = $this->lf->get_filearea(); 00396 $newrecord->itemid = $this->lf->get_itemid(); 00397 $newrecord->filepath = $this->lf->get_filepath(); 00398 $newrecord->filename = $newfilename; 00399 00400 if ($fs->file_exists($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) { 00401 // file already exists, sorry 00402 return null; 00403 } 00404 00405 $newrecord->timecreated = $now; 00406 $newrecord->timemodified = $now; 00407 $newrecord->mimetype = mimeinfo('type', $newfilename); 00408 $newrecord->userid = $userid; 00409 00410 if ($file = $fs->create_file_from_pathname($newrecord, $pathname)) { 00411 return $this->browser->get_file_info($this->context, $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); 00412 } 00413 return null; 00414 } 00415 00424 public function create_file_from_storedfile($newfilename, $fid, $userid = NULL) { 00425 if (!$this->is_writable() or $this->lf->get_filename() !== '.') { 00426 return null; 00427 } 00428 00429 $newfilename = clean_param($newfilename, PARAM_FILE); 00430 if ($newfilename === '') { 00431 return null; 00432 } 00433 00434 $fs = get_file_storage(); 00435 00436 $now = time(); 00437 00438 $newrecord = new stdClass(); 00439 $newrecord->contextid = $this->lf->get_contextid(); 00440 $newrecord->component = $this->lf->get_component(); 00441 $newrecord->filearea = $this->lf->get_filearea(); 00442 $newrecord->itemid = $this->lf->get_itemid(); 00443 $newrecord->filepath = $this->lf->get_filepath(); 00444 $newrecord->filename = $newfilename; 00445 00446 if ($fs->file_exists($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename)) { 00447 // file already exists, sorry 00448 return null; 00449 } 00450 00451 $newrecord->timecreated = $now; 00452 $newrecord->timemodified = $now; 00453 $newrecord->mimetype = mimeinfo('type', $newfilename); 00454 $newrecord->userid = $userid; 00455 00456 if ($file = $fs->create_file_from_storedfile($newrecord, $fid)) { 00457 return $this->browser->get_file_info($this->context, $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); 00458 } 00459 return null; 00460 } 00461 00466 public function delete() { 00467 if (!$this->is_writable()) { 00468 return false; 00469 } 00470 00471 if ($this->is_directory()) { 00472 $filepath = $this->lf->get_filepath(); 00473 $fs = get_file_storage(); 00474 $storedfiles = $fs->get_area_files($this->context->id, $this->get_component(), $this->lf->get_filearea(), $this->lf->get_itemid(), ""); 00475 foreach ($storedfiles as $file) { 00476 if (strpos($file->get_filepath(), $filepath) === 0) { 00477 $file->delete(); 00478 } 00479 } 00480 } 00481 00482 return $this->lf->delete(); 00483 } 00484 00494 public function copy_to_storage($contextid, $component, $filearea, $itemid, $filepath, $filename) { 00495 if (!$this->is_readable() or $this->is_directory()) { 00496 return false; 00497 } 00498 00499 $fs = get_file_storage(); 00500 if ($existing = $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename)) { 00501 $existing->delete(); 00502 } 00503 $file_record = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename); 00504 $fs->create_file_from_storedfile($file_record, $this->lf); 00505 00506 return true; 00507 } 00508 00514 public function copy_to_pathname($pathname) { 00515 if (!$this->is_readable() or $this->is_directory()) { 00516 return false; 00517 } 00518 00519 if (file_exists($pathname)) { 00520 if (!unlink($pathname)) { 00521 return false; 00522 } 00523 } 00524 00525 $this->lf->copy_content_to($pathname); 00526 00527 return true; 00528 } 00529 }