DrupalDirectoryDestination.php in Backup and Migrate 8.4
File
src/Destination/DrupalDirectoryDestination.php
View source
<?php
namespace BackupMigrate\Drupal\Destination;
use BackupMigrate\Core\Destination\DirectoryDestination;
use BackupMigrate\Core\Exception\BackupMigrateException;
use BackupMigrate\Core\File\BackupFileReadableInterface;
use Drupal\Core\File\FileSystem;
use Drupal\Core\StreamWrapper\PrivateStream;
use BackupMigrate\Core\File\ReadableStreamBackupFile;
class DrupalDirectoryDestination extends DirectoryDestination {
function _saveFile(BackupFileReadableInterface $file) {
$this
->checkDirectory();
file_unmanaged_move($file
->realpath(), $this
->_idToPath($file
->getFullName()), FILE_EXISTS_REPLACE);
}
protected function checkDirectory() {
$dir = $this
->confGet('directory');
$is_private = strpos($dir, 'private://') === 0;
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 (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY && FILE_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 {
$real = \Drupal::service('file_system')
->realpath($dir);
if ($real) {
$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,
]);
}
}
}
parent::checkDirectory();
}
public function queryFiles($filters = [], $sort = 'datestamp', $sort_direction = SORT_DESC, $count = 100, $start = 0) {
$out = $this
->listFiles($count + $start);
foreach ($out as $key => $file) {
$out[$key] = $this
->loadFileMetadata($file);
}
if ($filters) {
$out = array_filter($out, function ($file) use ($filters) {
foreach ($filters as $key => $value) {
if ($file
->getMeta($key) !== $value) {
return FALSE;
}
}
return TRUE;
});
}
if ($sort && $sort_direction) {
uasort($out, function ($a, $b) use ($sort, $sort_direction) {
if ($sort_direction == SORT_DESC) {
if ($sort == 'name') {
return $a
->getFullName() <=> $b
->getFullName();
}
return $a
->getMeta($sort) <=> $b
->getMeta($sort);
}
else {
if ($sort == 'name') {
return $a
->getFullName() <=> $b
->getFullName();
}
return $a
->getMeta($sort) <=> $b
->getMeta($sort);
}
});
}
if ($count || $start) {
$out = array_slice($out, $start, $count);
}
return $out;
}
}