Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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 path to pass to getimagesize.
 416       * @return array Containing width, height, and mimetype.
 417       */
 418      protected function get_imageinfo_from_path($path) {
 419          $imageinfo = getimagesize($path);
 420  
 421          if (!is_array($imageinfo)) {
 422              return false; // Nothing to process, the file was not recognised as image by GD.
 423          }
 424  
 425          $image = array(
 426                  'width'     => $imageinfo[0],
 427                  'height'    => $imageinfo[1],
 428                  'mimetype'  => image_type_to_mime_type($imageinfo[2]),
 429              );
 430  
 431          if (empty($image['width']) or empty($image['height']) or empty($image['mimetype'])) {
 432              // GD can not parse it, sorry.
 433              return false;
 434          }
 435          return $image;
 436      }
 437  
 438      /**
 439       * Serve file content using X-Sendfile header.
 440       * Please make sure that all headers are already sent and the all
 441       * access control checks passed.
 442       *
 443       * This alternate method to xsendfile() allows an alternate file system
 444       * to use the full file metadata and avoid extra lookups.
 445       *
 446       * @param stored_file $file The file to send
 447       * @return bool success
 448       */
 449      public function xsendfile_file(stored_file $file): bool {
 450          return $this->xsendfile($file->get_contenthash());
 451      }
 452  
 453      /**
 454       * Serve file content using X-Sendfile header.
 455       * Please make sure that all headers are already sent and the all
 456       * access control checks passed.
 457       *
 458       * @param string $contenthash The content hash of the file to be served
 459       * @return bool success
 460       */
 461      public function xsendfile($contenthash) {
 462          global $CFG;
 463          require_once($CFG->libdir . "/xsendfilelib.php");
 464  
 465          return xsendfile($this->get_remote_path_from_hash($contenthash));
 466      }
 467  
 468      /**
 469       * Returns true if filesystem is configured to support xsendfile.
 470       *
 471       * @return bool
 472       */
 473      public function supports_xsendfile() {
 474          global $CFG;
 475          return !empty($CFG->xsendfile);
 476      }
 477  
 478      /**
 479       * Validate that the content hash matches the content hash of the file on disk.
 480       *
 481       * @param string $contenthash The current content hash to validate
 482       * @param string $pathname The path to the file on disk
 483       * @return array The content hash (it might change) and file size
 484       */
 485      protected function validate_hash_and_file_size($contenthash, $pathname) {
 486          global $CFG;
 487  
 488          if (!is_readable($pathname)) {
 489              throw new file_exception('storedfilecannotread', '', $pathname);
 490          }
 491  
 492          $filesize = filesize($pathname);
 493          if ($filesize === false) {
 494              throw new file_exception('storedfilecannotread', '', $pathname);
 495          }
 496  
 497          if (is_null($contenthash)) {
 498              $contenthash = file_storage::hash_from_path($pathname);
 499          } else if ($CFG->debugdeveloper) {
 500              $filehash = file_storage::hash_from_path($pathname);
 501              if ($filehash === false) {
 502                  throw new file_exception('storedfilecannotread', '', $pathname);
 503              }
 504              if ($filehash !== $contenthash) {
 505                  // Hopefully this never happens, if yes we need to fix calling code.
 506                  debugging("Invalid contenthash submitted for file $pathname", DEBUG_DEVELOPER);
 507                  $contenthash = $filehash;
 508              }
 509          }
 510          if ($contenthash === false) {
 511              throw new file_exception('storedfilecannotread', '', $pathname);
 512          }
 513  
 514          if ($filesize > 0 and $contenthash === file_storage::hash_from_string('')) {
 515              // Did the file change or is file_storage::hash_from_path() borked for this file?
 516              clearstatcache();
 517              $contenthash = file_storage::hash_from_path($pathname);
 518              $filesize    = filesize($pathname);
 519  
 520              if ($contenthash === false or $filesize === false) {
 521                  throw new file_exception('storedfilecannotread', '', $pathname);
 522              }
 523              if ($filesize > 0 and $contenthash === file_storage::hash_from_string('')) {
 524                  // This is very weird...
 525                  throw new file_exception('storedfilecannotread', '', $pathname);
 526              }
 527          }
 528  
 529          return [$contenthash, $filesize];
 530      }
 531  
 532      /**
 533       * Add the supplied file to the file system.
 534       *
 535       * Note: If overriding this function, it is advisable to store the file
 536       * in the path returned by get_local_path_from_hash as there may be
 537       * subsequent uses of the file in the same request.
 538       *
 539       * @param string $pathname Path to file currently on disk
 540       * @param string $contenthash SHA1 hash of content if known (performance only)
 541       * @return array (contenthash, filesize, newfile)
 542       */
 543      abstract public function add_file_from_path($pathname, $contenthash = null);
 544  
 545      /**
 546       * Add a file with the supplied content to the file system.
 547       *
 548       * Note: If overriding this function, it is advisable to store the file
 549       * in the path returned by get_local_path_from_hash as there may be
 550       * subsequent uses of the file in the same request.
 551       *
 552       * @param string $content file content - binary string
 553       * @return array (contenthash, filesize, newfile)
 554       */
 555      abstract public function add_file_from_string($content);
 556  
 557      /**
 558       * Returns file handle - read only mode, no writing allowed into pool files!
 559       *
 560       * When you want to modify a file, create a new file and delete the old one.
 561       *
 562       * @param stored_file $file The file to retrieve a handle for
 563       * @param int $type Type of file handle (FILE_HANDLE_xx constant)
 564       * @return resource file handle
 565       */
 566      public function get_content_file_handle(stored_file $file, $type = stored_file::FILE_HANDLE_FOPEN) {
 567          if ($type === stored_file::FILE_HANDLE_GZOPEN) {
 568              // Local file required for gzopen.
 569              $path = $this->get_local_path_from_storedfile($file, true);
 570          } else {
 571              $path = $this->get_remote_path_from_storedfile($file);
 572          }
 573  
 574          return self::get_file_handle_for_path($path, $type);
 575      }
 576  
 577      /**
 578       * Return a file handle for the specified path.
 579       *
 580       * This abstraction should be used when overriding get_content_file_handle in a new file system.
 581       *
 582       * @param string $path The path to the file. This shoudl be any type of path that fopen and gzopen accept.
 583       * @param int $type Type of file handle (FILE_HANDLE_xx constant)
 584       * @return resource
 585       * @throws coding_exception When an unexpected type of file handle is requested
 586       */
 587      protected static function get_file_handle_for_path($path, $type = stored_file::FILE_HANDLE_FOPEN) {
 588          switch ($type) {
 589              case stored_file::FILE_HANDLE_FOPEN:
 590                  // Binary reading.
 591                  return fopen($path, 'rb');
 592              case stored_file::FILE_HANDLE_GZOPEN:
 593                  // Binary reading of file in gz format.
 594                  return gzopen($path, 'rb');
 595              default:
 596                  throw new coding_exception('Unexpected file handle type');
 597          }
 598      }
 599  
 600      /**
 601       * Retrieve the mime information for the specified stored file.
 602       *
 603       * @param string $contenthash
 604       * @param string $filename
 605       * @return string The MIME type.
 606       */
 607      public function mimetype_from_hash($contenthash, $filename) {
 608          $pathname = $this->get_local_path_from_hash($contenthash);
 609          $mimetype = file_storage::mimetype($pathname, $filename);
 610  
 611          if ($mimetype === 'document/unknown' && !$this->is_file_readable_locally_by_hash($contenthash)) {
 612              // The type is unknown, but the full checks weren't completed because the file isn't locally available.
 613              // Ensure we have a local copy and try again.
 614              $pathname = $this->get_local_path_from_hash($contenthash, true);
 615              $mimetype = file_storage::mimetype_from_file($pathname);
 616          }
 617  
 618          return $mimetype;
 619      }
 620  
 621      /**
 622       * Retrieve the mime information for the specified stored file.
 623       *
 624       * @param stored_file $file The stored file to retrieve mime information for
 625       * @return string The MIME type.
 626       */
 627      public function mimetype_from_storedfile($file) {
 628          if (!$file->get_filesize()) {
 629              // Files with an empty filesize are treated as directories and have no mimetype.
 630              return null;
 631          }
 632          return $this->mimetype_from_hash($file->get_contenthash(), $file->get_filename());
 633      }
 634  
 635      /**
 636       * Run any periodic tasks which must be performed.
 637       */
 638      public function cron() {
 639      }
 640  }