/** * {@inheritdoc} */ function saveFile(BackupFileReadableInterface $file) { // Set some default download headers. $headers = array(array('key' => 'Content-Disposition', 'value' => 'attachment; filename="' . $file->getFullName() . '"'), array('key' => 'Cache-Control', 'value' => 'no-cache')); // Set a mime-type header. if ($mime = $file->getMeta('mimetype')) { $headers[] = array('key' => 'Content-Type', 'value' => $mime); } else { // Get the mime type for this file if possible $mime = 'application/octet-stream'; $mime = $this->plugins()->call('alterMime', $mime, array('ext' => $file->getExtLast())); $headers[] = array('key' => 'Content-Type', 'value' => $mime); } // In some circumstances, web-servers will double compress gzipped files. // This may help aleviate that issue by disabling mod-deflate. if ($file->getMeta('mimetype') == 'application/x-gzip') { if (function_exists('apache_setenv')) { apache_setenv('no-gzip', '1'); } $headers[] = array('key' => 'Content-Encoding', 'value' => 'gzip'); } if ($size = $file->getMeta('filesize')) { $headers[] = array('key' => 'Content-Length', 'value' => $size); } // Suppress the warning you get when the buffer is empty. @ob_end_clean(); if ($file->openForRead()) { foreach ($headers as $header) { // To prevent HTTP header injection, we delete new lines that are // not followed by a space or a tab. // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 $header['value'] = preg_replace('/\\r?\\n(?!\\t| )/', '', $header['value']); header($header['key'] . ': ' . $header['value']); } // Transfer file in 1024 byte chunks to save memory usage. while ($data = $file->readBytes(1024 * 512)) { print $data; } $file->close(); } // @TODO Throw exception. }
/** * {@inheritdoc} */ public function importFromFile(BackupFileReadableInterface $file) { if ($directory = $this->confGet('directory')) { // Make sure the directory ends in exactly 1 slash: if (substr($directory, -1) !== '/') { $directory = $directory . '/'; } if (!file_exists($directory)) { throw new BackupMigrateException('The directory %dir does not exist to restore to.', array('%dir' => $directory)); } if (!is_writable($directory)) { throw new BackupMigrateException('The directory %dir cannot be written to because of the operating system file permissions.', array('%dir' => $directory)); } if (!($reader = $this->getArchiveReader())) { throw new BackupMigrateException('A file directory source requires an archive reader object.'); } // Check that the file endings match. if ($reader->getFileExt() !== $file->getExtLast()) { throw new BackupMigrateException('This source expects a .%ext file.', array('%ext' => $archiver->getFileExt())); } $reader->setArchive($file); $reader->extractTo($directory); $reader->closeArchive(); return TRUE; } return FALSE; }
/** * Run on a restore * * @param \BackupMigrate\Core\File\BackupFileReadableInterface $file * @return \BackupMigrate\Core\File\BackupFileReadableInterface */ public function beforeRestore(BackupFileReadableInterface $file) { // If the file is not a supported compression type then simply return the same input file. $out = $file; $type = $file->getExtLast(); switch (strtolower($type)) { case "gz": case "gzip": $out = $this->getTempFileManager()->popExt($file); $this->_gzipDecode($file, $out); break; case "bz": case "bz2": case "bzip": case "bzip2": $out = $this->getTempFileManager()->popExt($file); $this->_bzipDecode($file, $out); break; case "zip": $out = $this->getTempFileManager()->popExt($file); $this->_ZipDecode($file, $out); break; } return $out; }