Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Core file system class definition. 19 * 20 * @package core_files 21 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 /** 28 * File system class used for low level access to real files in filedir. 29 * 30 * @package core_files 31 * @category files 32 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 abstract class file_system { 36 37 /** 38 * Private clone method to prevent cloning of the instance. 39 */ 40 final protected function __clone() { 41 return; 42 } 43 44 /** 45 * Private wakeup method to prevent unserialising of the instance. 46 */ 47 final protected function __wakeup() { 48 return; 49 } 50 51 /** 52 * Output the content of the specified stored file. 53 * 54 * Note, this is different to get_content() as it uses the built-in php 55 * readfile function which is more efficient. 56 * 57 * @param stored_file $file The file to serve. 58 * @return void 59 */ 60 public function readfile(stored_file $file) { 61 if ($this->is_file_readable_locally_by_storedfile($file, false)) { 62 $path = $this->get_local_path_from_storedfile($file, false); 63 } else { 64 $path = $this->get_remote_path_from_storedfile($file); 65 } 66 if (readfile_allow_large($path, $file->get_filesize()) === false) { 67 throw new file_exception('storedfilecannotreadfile', $file->get_filename()); 68 } 69 } 70 71 /** 72 * Get the full path on disk for the specified stored file. 73 * 74 * Note: This must return a consistent path for the file's contenthash 75 * and the path _will_ be in a standard local format. 76 * Streamable paths will not work. 77 * A local copy of the file _will_ be fetched if $fetchifnotfound is tree. 78 * 79 * The $fetchifnotfound allows you to determine the expected path of the file. 80 * 81 * @param stored_file $file The file to serve. 82 * @param bool $fetchifnotfound Whether to attempt to fetch from the remote path if not found. 83 * @return string full path to pool file with file content 84 */ 85 public function get_local_path_from_storedfile(stored_file $file, $fetchifnotfound = false) { 86 return $this->get_local_path_from_hash($file->get_contenthash(), $fetchifnotfound); 87 } 88 89 /** 90 * Get a remote filepath for the specified stored file. 91 * 92 * This is typically either the same as the local filepath, or it is a streamable resource. 93 * 94 * See https://secure.php.net/manual/en/wrappers.php for further information on valid wrappers. 95 * 96 * @param stored_file $file The file to serve. 97 * @return string full path to pool file with file content 98 */ 99 public function get_remote_path_from_storedfile(stored_file $file) { 100 return $this->get_remote_path_from_hash($file->get_contenthash(), false); 101 } 102 103 /** 104 * Get the full path for the specified hash, including the path to the filedir. 105 * 106 * Note: This must return a consistent path for the file's contenthash 107 * and the path _will_ be in a standard local format. 108 * Streamable paths will not work. 109 * A local copy of the file _will_ be fetched if $fetchifnotfound is tree. 110 * 111 * The $fetchifnotfound allows you to determine the expected path of the file. 112 * 113 * @param string $contenthash The content hash 114 * @param bool $fetchifnotfound Whether to attempt to fetch from the remote path if not found. 115 * @return string The full path to the content file 116 */ 117 abstract protected function get_local_path_from_hash($contenthash, $fetchifnotfound = false); 118 119 /** 120 * Get the full path for the specified hash, including the path to the filedir. 121 * 122 * This is typically either the same as the local filepath, or it is a streamable resource. 123 * 124 * See https://secure.php.net/manual/en/wrappers.php for further information on valid wrappers. 125 * 126 * @param string $contenthash The content hash 127 * @return string The full path to the content file 128 */ 129 abstract protected function get_remote_path_from_hash($contenthash); 130 131 /** 132 * Determine whether the file is present on the file system somewhere. 133 * A local copy of the file _will_ be fetched if $fetchifnotfound is tree. 134 * 135 * The $fetchifnotfound allows you to determine the expected path of the file. 136 * 137 * @param stored_file $file The file to ensure is available. 138 * @param bool $fetchifnotfound Whether to attempt to fetch from the remote path if not found. 139 * @return bool 140 */ 141 public function is_file_readable_locally_by_storedfile(stored_file $file, $fetchifnotfound = false) { 142 if (!$file->get_filesize()) { 143 // Files with empty size are either directories or empty. 144 // We handle these virtually. 145 return true; 146 } 147 148 // Check to see if the file is currently readable. 149 $path = $this->get_local_path_from_storedfile($file, $fetchifnotfound); 150 if (is_readable($path)) { 151 return true; 152 } 153 154 return false; 155 } 156 157 /** 158 * Determine whether the file is present on the local file system somewhere. 159 * 160 * @param stored_file $file The file to ensure is available. 161 * @return bool 162 */ 163 public function is_file_readable_remotely_by_storedfile(stored_file $file) { 164 if (!$file->get_filesize()) { 165 // Files with empty size are either directories or empty. 166 // We handle these virtually. 167 return true; 168 } 169 170 $path = $this->get_remote_path_from_storedfile($file, false); 171 if (is_readable($path)) { 172 return true; 173 } 174 175 return false; 176 } 177 178 /** 179 * Determine whether the file is present on the file system somewhere given 180 * the contenthash. 181 * 182 * @param string $contenthash The contenthash of the file to check. 183 * @param bool $fetchifnotfound Whether to attempt to fetch from the remote path if not found. 184 * @return bool 185 */ 186 public function is_file_readable_locally_by_hash($contenthash, $fetchifnotfound = false) { 187 if ($contenthash === file_storage::hash_from_string('')) { 188 // Files with empty size are either directories or empty. 189 // We handle these virtually. 190 return true; 191 } 192 193 // This is called by file_storage::content_exists(), and in turn by the repository system. 194 $path = $this->get_local_path_from_hash($contenthash, $fetchifnotfound); 195 196 // Note - it is not possible to perform a content recovery safely from a hash alone. 197 return is_readable($path); 198 } 199 200 /** 201 * Determine whether the file is present locally on the file system somewhere given 202 * the contenthash. 203 * 204 * @param string $contenthash The contenthash of the file to check. 205 * @return bool 206 */ 207 public function is_file_readable_remotely_by_hash($contenthash) { 208 if ($contenthash === file_storage::hash_from_string('')) { 209 // Files with empty size are either directories or empty. 210 // We handle these virtually. 211 return true; 212 } 213 214 $path = $this->get_remote_path_from_hash($contenthash, false); 215 216 // Note - it is not possible to perform a content recovery safely from a hash alone. 217 return is_readable($path); 218 } 219 220 /** 221 * Copy content of file to given pathname. 222 * 223 * @param stored_file $file The file to be copied 224 * @param string $target real path to the new file 225 * @return bool success 226 */ 227 abstract public function copy_content_from_storedfile(stored_file $file, $target); 228 229 /** 230 * Remove the file with the specified contenthash. 231 * 232 * Note, if overriding this function, you _must_ check that the file is 233 * no longer in use - see {check_file_usage}. 234 * 235 * DO NOT call directly - reserved for core!! 236 * 237 * @param string $contenthash 238 */ 239 abstract public function remove_file($contenthash); 240 241 /** 242 * Check whether a file is removable. 243 * 244 * This must be called prior to file removal. 245 * 246 * @param string $contenthash 247 * @return bool 248 */ 249 protected static function is_file_removable($contenthash) { 250 global $DB; 251 252 if ($contenthash === file_storage::hash_from_string('')) { 253 // No need to delete files without content. 254 return false; 255 } 256 257 // Note: This section is critical - in theory file could be reused at the same time, if this 258 // happens we can still recover the file from trash. 259 // Technically this is the responsibility of the file_storage API, but as this method is public, we go belt-and-braces. 260 if ($DB->record_exists('files', array('contenthash' => $contenthash))) { 261 // File content is still used. 262 return false; 263 } 264 265 return true; 266 } 267 268 /** 269 * Get the content of the specified stored file. 270 * 271 * Generally you will probably want to use readfile() to serve content, 272 * and where possible you should see if you can use 273 * get_content_file_handle and work with the file stream instead. 274 * 275 * @param stored_file $file The file to retrieve 276 * @return string The full file content 277 */ 278 public function get_content(stored_file $file) { 279 if (!$file->get_filesize()) { 280 // Directories are empty. Empty files are not worth fetching. 281 return ''; 282 } 283 284 $source = $this->get_remote_path_from_storedfile($file); 285 return file_get_contents($source); 286 } 287 288 /** 289 * List contents of archive. 290 * 291 * @param stored_file $file The archive to inspect 292 * @param file_packer $packer file packer instance 293 * @return array of file infos 294 */ 295 public function list_files($file, file_packer $packer) { 296 $archivefile = $this->get_local_path_from_storedfile($file, true); 297 return $packer->list_files($archivefile); 298 } 299 300 /** 301 * Extract file to given file path (real OS filesystem), existing files are overwritten. 302 * 303 * @param stored_file $file The archive to inspect 304 * @param file_packer $packer File packer instance 305 * @param string $pathname Target directory 306 * @param file_progress $progress progress indicator callback or null if not required 307 * @return array|bool List of processed files; false if error 308 */ 309 public function extract_to_pathname(stored_file $file, file_packer $packer, $pathname, file_progress $progress = null) { 310 $archivefile = $this->get_local_path_from_storedfile($file, true); 311 return $packer->extract_to_pathname($archivefile, $pathname, null, $progress); 312 } 313 314 /** 315 * Extract file to given file path (real OS filesystem), existing files are overwritten. 316 * 317 * @param stored_file $file The archive to inspect 318 * @param file_packer $packer file packer instance 319 * @param int $contextid context ID 320 * @param string $component component 321 * @param string $filearea file area 322 * @param int $itemid item ID 323 * @param string $pathbase path base 324 * @param int $userid user ID 325 * @param file_progress $progress Progress indicator callback or null if not required 326 * @return array|bool list of processed files; false if error 327 */ 328 public function extract_to_storage(stored_file $file, file_packer $packer, $contextid, 329 $component, $filearea, $itemid, $pathbase, $userid = null, file_progress $progress = null) { 330 331 // Since we do not know which extractor we have, and whether it supports remote paths, use a local path here. 332 $archivefile = $this->get_local_path_from_storedfile($file, true); 333 return $packer->extract_to_storage($archivefile, $contextid, 334 $component, $filearea, $itemid, $pathbase, $userid, $progress); 335 } 336 337 /** 338 * Add file/directory into archive. 339 * 340 * @param stored_file $file The file to archive 341 * @param file_archive $filearch file archive instance 342 * @param string $archivepath pathname in archive 343 * @return bool success 344 */ 345 public function add_storedfile_to_archive(stored_file $file, file_archive $filearch, $archivepath) { 346 if ($file->is_directory()) { 347 return $filearch->add_directory($archivepath); 348 } else { 349 // Since we do not know which extractor we have, and whether it supports remote paths, use a local path here. 350 return $filearch->add_file_from_pathname($archivepath, $this->get_local_path_from_storedfile($file, true)); 351 } 352 } 353 354 /** 355 * Adds this file path to a curl request (POST only). 356 * 357 * @param stored_file $file The file to add to the curl request 358 * @param curl $curlrequest The curl request object 359 * @param string $key What key to use in the POST request 360 * @return void 361 * This needs the fullpath for the storedfile :/ 362 * Can this be achieved in some other fashion? 363 */ 364 public function add_to_curl_request(stored_file $file, &$curlrequest, $key) { 365 // Note: curl_file_create does not work with remote paths. 366 $path = $this->get_local_path_from_storedfile($file, true); 367 $curlrequest->_tmp_file_post_params[$key] = curl_file_create($path, null, $file->get_filename()); 368 } 369 370 /** 371 * Returns information about image. 372 * Information is determined from the file content 373 * 374 * @param stored_file $file The file to inspect 375 * @return mixed array with width, height and mimetype; false if not an image 376 */ 377 public function get_imageinfo(stored_file $file) { 378 if (!$this->is_image_from_storedfile($file)) { 379 return false; 380 } 381 382 // Whilst get_imageinfo_from_path can use remote paths, it must download the entire file first. 383 // It is more efficient to use a local file when possible. 384 return $this->get_imageinfo_from_path($this->get_local_path_from_storedfile($file, true)); 385 } 386 387 /** 388 * Attempt to determine whether the specified file is likely to be an 389 * image. 390 * Since this relies upon the mimetype stored in the files table, there 391 * may be times when this information is not 100% accurate. 392 * 393 * @param stored_file $file The file to check 394 * @return bool 395 */ 396 public function is_image_from_storedfile(stored_file $file) { 397 if (!$file->get_filesize()) { 398 // An empty file cannot be an image. 399 return false; 400 } 401 402 $mimetype = $file->get_mimetype(); 403 if (!preg_match('|^image/|', $mimetype)) { 404 // The mimetype does not include image. 405 return false; 406 } 407 408 // If it looks like an image, and it smells like an image, perhaps it's an image! 409 return true; 410 } 411 412 /** 413 * Returns image information relating to the specified path or URL. 414 * 415 * @param string $path The full path of the image file. 416 * @return array|bool array that containing width, height, and mimetype or false if cannot get the image info. 417 */ 418 protected function get_imageinfo_from_path($path) { 419 $imagemimetype = file_storage::mimetype_from_file($path); 420 $issvgimage = file_is_svg_image_from_mimetype($imagemimetype); 421 422 if (!$issvgimage) { 423 $imageinfo = getimagesize($path); 424 if (!is_array($imageinfo)) { 425 return false; // Nothing to process, the file was not recognised as image by GD. 426 } 427 $image = [ 428 'width' => $imageinfo[0], 429 'height' => $imageinfo[1], 430 'mimetype' => image_type_to_mime_type($imageinfo[2]), 431 ]; 432 } else { 433 // Since SVG file is actually an XML file, GD cannot handle. 434 $svgcontent = @simplexml_load_file($path); 435 if (!$svgcontent) { 436 // Cannot parse the file. 437 return false; 438 } 439 $svgattrs = $svgcontent->attributes(); 440 441 if (!empty($svgattrs->viewBox)) { 442 // We have viewBox. 443 $viewboxval = explode(' ', $svgattrs->viewBox); 444 $width = intval($viewboxval[2]); 445 $height = intval($viewboxval[3]); 446 } else { 447 // Get the width. 448 if (!empty($svgattrs->width) && intval($svgattrs->width) > 0) { 449 $width = intval($svgattrs->width); 450 } else { 451 // Default width. 452 $width = 800; 453 } 454 // Get the height. 455 if (!empty($svgattrs->height) && intval($svgattrs->height) > 0) { 456 $height = intval($svgattrs->height); 457 } else { 458 // Default width. 459 $height = 600; 460 } 461 } 462 463 $image = [ 464 'width' => $width, 465 'height' => $height, 466 'mimetype' => $imagemimetype, 467 ]; 468 } 469 470 if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) { 471 // GD can not parse it, sorry. 472 return false; 473 } 474 return $image; 475 } 476 477 /** 478 * Serve file content using X-Sendfile header. 479 * Please make sure that all headers are already sent and the all 480 * access control checks passed. 481 * 482 * This alternate method to xsendfile() allows an alternate file system 483 * to use the full file metadata and avoid extra lookups. 484 * 485 * @param stored_file $file The file to send 486 * @return bool success 487 */ 488 public function xsendfile_file(stored_file $file): bool { 489 return $this->xsendfile($file->get_contenthash()); 490 } 491 492 /** 493 * Serve file content using X-Sendfile header. 494 * Please make sure that all headers are already sent and the all 495 * access control checks passed. 496 * 497 * @param string $contenthash The content hash of the file to be served 498 * @return bool success 499 */ 500 public function xsendfile($contenthash) { 501 global $CFG; 502 require_once($CFG->libdir . "/xsendfilelib.php"); 503 504 return xsendfile($this->get_remote_path_from_hash($contenthash)); 505 } 506 507 /** 508 * Returns true if filesystem is configured to support xsendfile. 509 * 510 * @return bool 511 */ 512 public function supports_xsendfile() { 513 global $CFG; 514 return !empty($CFG->xsendfile); 515 } 516 517 /** 518 * Validate that the content hash matches the content hash of the file on disk. 519 * 520 * @param string $contenthash The current content hash to validate 521 * @param string $pathname The path to the file on disk 522 * @return array The content hash (it might change) and file size 523 */ 524 protected function validate_hash_and_file_size($contenthash, $pathname) { 525 global $CFG; 526 527 if (!is_readable($pathname)) { 528 throw new file_exception('storedfilecannotread', '', $pathname); 529 } 530 531 $filesize = filesize($pathname); 532 if ($filesize === false) { 533 throw new file_exception('storedfilecannotread', '', $pathname); 534 } 535 536 if (is_null($contenthash)) { 537 $contenthash = file_storage::hash_from_path($pathname); 538 } else if ($CFG->debugdeveloper) { 539 $filehash = file_storage::hash_from_path($pathname); 540 if ($filehash === false) { 541 throw new file_exception('storedfilecannotread', '', $pathname); 542 } 543 if ($filehash !== $contenthash) { 544 // Hopefully this never happens, if yes we need to fix calling code. 545 debugging("Invalid contenthash submitted for file $pathname", DEBUG_DEVELOPER); 546 $contenthash = $filehash; 547 } 548 } 549 if ($contenthash === false) { 550 throw new file_exception('storedfilecannotread', '', $pathname); 551 } 552 553 if ($filesize > 0 and $contenthash === file_storage::hash_from_string('')) { 554 // Did the file change or is file_storage::hash_from_path() borked for this file? 555 clearstatcache(); 556 $contenthash = file_storage::hash_from_path($pathname); 557 $filesize = filesize($pathname); 558 559 if ($contenthash === false or $filesize === false) { 560 throw new file_exception('storedfilecannotread', '', $pathname); 561 } 562 if ($filesize > 0 and $contenthash === file_storage::hash_from_string('')) { 563 // This is very weird... 564 throw new file_exception('storedfilecannotread', '', $pathname); 565 } 566 } 567 568 return [$contenthash, $filesize]; 569 } 570 571 /** 572 * Add the supplied file to the file system. 573 * 574 * Note: If overriding this function, it is advisable to store the file 575 * in the path returned by get_local_path_from_hash as there may be 576 * subsequent uses of the file in the same request. 577 * 578 * @param string $pathname Path to file currently on disk 579 * @param string $contenthash SHA1 hash of content if known (performance only) 580 * @return array (contenthash, filesize, newfile) 581 */ 582 abstract public function add_file_from_path($pathname, $contenthash = null); 583 584 /** 585 * Add a file with the supplied content to the file system. 586 * 587 * Note: If overriding this function, it is advisable to store the file 588 * in the path returned by get_local_path_from_hash as there may be 589 * subsequent uses of the file in the same request. 590 * 591 * @param string $content file content - binary string 592 * @return array (contenthash, filesize, newfile) 593 */ 594 abstract public function add_file_from_string($content); 595 596 /** 597 * Returns file handle - read only mode, no writing allowed into pool files! 598 * 599 * When you want to modify a file, create a new file and delete the old one. 600 * 601 * @param stored_file $file The file to retrieve a handle for 602 * @param int $type Type of file handle (FILE_HANDLE_xx constant) 603 * @return resource file handle 604 */ 605 public function get_content_file_handle(stored_file $file, $type = stored_file::FILE_HANDLE_FOPEN) { 606 if ($type === stored_file::FILE_HANDLE_GZOPEN) { 607 // Local file required for gzopen. 608 $path = $this->get_local_path_from_storedfile($file, true); 609 } else { 610 $path = $this->get_remote_path_from_storedfile($file); 611 } 612 613 return self::get_file_handle_for_path($path, $type); 614 } 615 616 /** 617 * Return a file handle for the specified path. 618 * 619 * This abstraction should be used when overriding get_content_file_handle in a new file system. 620 * 621 * @param string $path The path to the file. This shoudl be any type of path that fopen and gzopen accept. 622 * @param int $type Type of file handle (FILE_HANDLE_xx constant) 623 * @return resource 624 * @throws coding_exception When an unexpected type of file handle is requested 625 */ 626 protected static function get_file_handle_for_path($path, $type = stored_file::FILE_HANDLE_FOPEN) { 627 switch ($type) { 628 case stored_file::FILE_HANDLE_FOPEN: 629 // Binary reading. 630 return fopen($path, 'rb'); 631 case stored_file::FILE_HANDLE_GZOPEN: 632 // Binary reading of file in gz format. 633 return gzopen($path, 'rb'); 634 default: 635 throw new coding_exception('Unexpected file handle type'); 636 } 637 } 638 639 /** 640 * Retrieve the mime information for the specified stored file. 641 * 642 * @param string $contenthash 643 * @param string $filename 644 * @return string The MIME type. 645 */ 646 public function mimetype_from_hash($contenthash, $filename) { 647 $pathname = $this->get_local_path_from_hash($contenthash); 648 $mimetype = file_storage::mimetype($pathname, $filename); 649 650 if ($mimetype === 'document/unknown' && !$this->is_file_readable_locally_by_hash($contenthash)) { 651 // The type is unknown, but the full checks weren't completed because the file isn't locally available. 652 // Ensure we have a local copy and try again. 653 $pathname = $this->get_local_path_from_hash($contenthash, true); 654 $mimetype = file_storage::mimetype_from_file($pathname); 655 } 656 657 return $mimetype; 658 } 659 660 /** 661 * Retrieve the mime information for the specified stored file. 662 * 663 * @param stored_file $file The stored file to retrieve mime information for 664 * @return string The MIME type. 665 */ 666 public function mimetype_from_storedfile($file) { 667 if (!$file->get_filesize()) { 668 // Files with an empty filesize are treated as directories and have no mimetype. 669 return null; 670 } 671 return $this->mimetype_from_hash($file->get_contenthash(), $file->get_filename()); 672 } 673 674 /** 675 * Run any periodic tasks which must be performed. 676 */ 677 public function cron() { 678 } 679 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body