BrowserDownloadDestination.php in Backup and Migrate 5.0.x
File
src/Core/Destination/BrowserDownloadDestination.php
View source
<?php
namespace Drupal\backup_migrate\Core\Destination;
use Drupal\backup_migrate\Core\Exception\DestinationNotWritableException;
use Drupal\backup_migrate\Core\File\BackupFileReadableInterface;
use Drupal\backup_migrate\Core\Plugin\PluginCallerInterface;
use Drupal\backup_migrate\Core\Plugin\PluginCallerTrait;
class BrowserDownloadDestination extends StreamDestination implements WritableDestinationInterface, PluginCallerInterface {
use PluginCallerTrait;
public function saveFile(BackupFileReadableInterface $file) {
$headers = [
[
'key' => 'Content-Disposition',
'value' => 'attachment; filename="' . $file
->getFullName() . '"',
],
[
'key' => 'Cache-Control',
'value' => 'no-cache',
],
];
if ($mime = $file
->getMeta('mimetype')) {
$headers[] = [
'key' => 'Content-Type',
'value' => $mime,
];
}
else {
$mime = 'application/octet-stream';
$mime = $this
->plugins()
->call('alterMime', $mime, [
'ext' => $file
->getExtLast(),
]);
$headers[] = [
'key' => 'Content-Type',
'value' => $mime,
];
}
if ($file
->getMeta('mimetype') == 'application/x-gzip') {
if (function_exists('apache_setenv')) {
apache_setenv('no-gzip', '1');
}
$headers[] = [
'key' => 'Content-Encoding',
'value' => 'gzip',
];
}
if ($size = $file
->getMeta('filesize')) {
$headers[] = [
'key' => 'Content-Length',
'value' => $size,
];
}
@ob_end_clean();
if ($file
->openForRead()) {
foreach ($headers as $header) {
$header['value'] = preg_replace('/\\r?\\n(?!\\t| )/', '', $header['value']);
header($header['key'] . ': ' . $header['value']);
}
while ($data = $file
->readBytes(1024 * 512)) {
print $data;
}
$file
->close();
}
}
public function checkWritable() {
if (!isset($_SERVER['REQUEST_METHOD'])) {
throw new DestinationNotWritableException("The download destination only works when accessed through a http client.");
}
}
}