You are here

protected function DrupalDirectoryDestination::checkDirectory in Backup and Migrate 5.0.x

Check that the directory can be used for backup.

Throws

\Drupal\backup_migrate\Core\Exception\BackupMigrateException

Overrides DirectoryDestination::checkDirectory

1 call to DrupalDirectoryDestination::checkDirectory()
DrupalDirectoryDestination::saveTheFile in src/Drupal/Destination/DrupalDirectoryDestination.php
Do the actual file save.

File

src/Drupal/Destination/DrupalDirectoryDestination.php, line 46

Class

DrupalDirectoryDestination
@package Drupal\backup_migrate\Drupal\Destination

Namespace

Drupal\backup_migrate\Drupal\Destination

Code

protected function checkDirectory() {

  // @todo Figure out if the file is or might be accessible via the web.
  $dir = $this
    ->confGet('directory');
  $is_private = strpos($dir, 'private://') === 0;

  // Attempt to create/prepare the directory if it is in the private
  // directory.
  if ($is_private) {
    if (!PrivateStream::basePath()) {
      throw new BackupMigrateException("The backup file could not be saved to '%dir' because your private files system path has not been set.", [
        '%dir' => $dir,
      ]);
    }
    if (!\Drupal::service('file_system')
      ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY && FileSystemInterface::MODIFY_PERMISSIONS)) {
      throw new BackupMigrateException("The backup file could not be saved to '%dir' because the directory could not be created or cannot be written to. Please make sure your private files directory is writable by the web server.", [
        '%dir' => $dir,
      ]);
    }
  }
  else {

    // If the file is local to the server.
    $real = \Drupal::service('file_system')
      ->realpath($dir);
    if ($real) {

      // If the file is within the docroot.
      $in_root = strpos($real, DRUPAL_ROOT) === 0;
      if ($in_root) {
        throw new BackupMigrateException("The backup file could not be saved to '%dir' because that directory may be publicly accessible via the web. Please save your backups to the private file directory or a directory outside of the web root.", [
          '%dir' => $dir,
        ]);
      }
    }
  }

  // Do the regular exists/writable checks.
  parent::checkDirectory();

  // @todo Warn if the realpath cannot be resolved (because we cannot
  // determine if the file is publicly accessible).
}