View source
<?php
namespace Drupal\backup_migrate\Core\Service;
use Drupal\backup_migrate\Core\Exception\BackupMigrateException;
use Drupal\backup_migrate\Core\File\BackupFileReadableInterface;
class TarArchiveReader implements ArchiveReaderInterface {
protected $archive;
public function getFileExt() {
return 'tar';
}
public function setArchive(BackupFileReadableInterface $out) {
$this->archive = $out;
}
public function extractTo($directory) {
$this->archive
->openForRead(TRUE);
$result = $this
->extractAllToDirectory($directory);
$this->archive
->close();
return $result;
}
private function extractAllToDirectory($directory) {
clearstatcache();
while (strlen($block = $this->archive
->readBytes(512)) != 0) {
$header = $this
->readHeader($block);
if (!$header) {
return FALSE;
}
if ($header['filename'] == '') {
continue;
}
if ($this
->maliciousFilename($header['filename'])) {
throw new BackupMigrateException('Malicious .tar detected, file %filename. Will not install in desired directory tree', [
'%filename' => $header['filename'],
]);
}
if ($header['typeflag'] == 'x' || $header['typeflag'] == 'g') {
$this->archive
->seekBytes(ceil($header['size'] / 512));
continue;
}
if (substr($header['filename'], 0, 1) == '/') {
$header['filename'] = $directory . $header['filename'];
}
else {
$header['filename'] = $directory . '/' . $header['filename'];
}
if (file_exists($header['filename'])) {
if (@is_dir($header['filename']) && $header['typeflag'] == '') {
throw new BackupMigrateException('File %filename already exists as a directory', [
'%filename' => $header['filename'],
]);
}
if (@is_file($header['filename']) && !@is_link($header['filename']) && $header['typeflag'] == "5") {
throw new BackupMigrateException('Directory %filename already exists as file', [
'%filename' => $header['filename'],
]);
}
if (!is_writable($header['filename'])) {
throw new BackupMigrateException('File %filename already exists and is write protected', [
'%filename' => $header['filename'],
]);
}
}
if ($header['typeflag'] == "5") {
if (!$this
->createDir($header['filename'])) {
throw new BackupMigrateException('Unable to create directory %filename', [
'%filename' => $header['filename'],
]);
}
}
else {
if (!$this
->createDir(dirname($header['filename']))) {
throw new BackupMigrateException('Unable to create directory for %filename', [
'%filename' => $header['filename'],
]);
}
if ($header['typeflag'] == "2") {
if (@file_exists($header['filename'])) {
@unlink($header['filename']);
}
if (!@symlink($header['link'], $header['filename'])) {
throw new BackupMigrateException('Unable to extract symbolic link: %filename', [
'%filename' => $header['filename'],
]);
}
}
else {
if (($dest_file = @fopen($header['filename'], "wb")) == 0) {
throw new BackupMigrateException('Error while opening %filename in write binary mode', [
'%filename' => $header['filename'],
]);
}
$n = floor($header['size'] / 512);
for ($i = 0; $i < $n; $i++) {
$content = $this->archive
->readBytes(512);
fwrite($dest_file, $content, 512);
}
if ($header['size'] % 512 != 0) {
$content = $this->archive
->readBytes(512);
fwrite($dest_file, $content, $header['size'] % 512);
}
@fclose($dest_file);
@touch($header['filename'], $header['mtime']);
if ($header['mode'] & 0111) {
$mode = fileperms($header['filename']) | ~umask() & 0111;
@chmod($header['filename'], $mode);
}
clearstatcache();
if (!is_file($header['filename'])) {
throw new BackupMigrateException('Extracted file %filename does not exist. Archive may be corrupted.', [
'%filename' => $header['filename'],
]);
}
$file_size = filesize($header['filename']);
if ($file_size != $header['size']) {
throw new BackupMigrateException('Extracted file %filename does not have the correct file size. File is %actual bytes (%expected bytes expected). Archive may be corrupted', [
'%filename' => $header['filename'],
'%expected' => (int) $header['size'],
(int) '%actual' => $file_size,
]);
}
}
}
}
return TRUE;
}
private function createDir($directory) {
if (@is_dir($directory) || $directory == '') {
return TRUE;
}
$parent = dirname($directory);
if ($parent != $directory && $parent != '' && !$this
->createDir($parent)) {
return FALSE;
}
if (@(!mkdir($directory, 0777))) {
return FALSE;
}
return TRUE;
}
private function readHeader($block, array $header = []) {
if (strlen($block) == 0) {
$header['filename'] = '';
return TRUE;
}
if (strlen($block) != 512) {
$header['filename'] = '';
throw new BackupMigrateException('Invalid block size: %size bytes', [
'%size' => strlen($block),
]);
}
if (!is_array($header)) {
$header = [];
}
$checksum = 0;
for ($i = 0; $i < 148; $i++) {
$checksum += ord(substr($block, $i, 1));
}
for ($i = 148; $i < 156; $i++) {
$checksum += ord(' ');
}
for ($i = 156; $i < 512; $i++) {
$checksum += ord(substr($block, $i, 1));
}
if (version_compare(PHP_VERSION, "5.5.0-dev") < 0) {
$fmt = "a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" . "a8checksum/a1typeflag/a100link/a6magic/a2version/" . "a32uname/a32gname/a8devmajor/a8devminor/a131prefix";
}
else {
$fmt = "Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/" . "Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/" . "Z32uname/Z32gname/Z8devmajor/Z8devminor/Z131prefix";
}
$data = unpack($fmt, $block);
if (strlen($data["prefix"]) > 0) {
$data["filename"] = "{$data['prefix']}/{$data['filename']}";
}
$header['checksum'] = octdec(trim($data['checksum']));
if ($header['checksum'] != $checksum) {
$header['filename'] = '';
if ($checksum == 256 && $header['checksum'] == 0) {
return $header;
}
throw new BackupMigrateException('Invalid checksum for file %filename', [
'%filename' => $data['filename'],
]);
}
$header['filename'] = rtrim($data['filename'], "\0");
$header['mode'] = octdec(trim($data['mode']));
$header['uid'] = octdec(trim($data['uid']));
$header['gid'] = octdec(trim($data['gid']));
$header['size'] = octdec(trim($data['size']));
$header['mtime'] = octdec(trim($data['mtime']));
if (($header['typeflag'] = $data['typeflag']) == "5") {
$header['size'] = 0;
}
$header['link'] = trim($data['link']);
if ($header['typeflag'] == 'L') {
$header = $this
->readLongHeader($header);
}
return $header;
}
private function readLongHeader(array $header) {
$filename = '';
$filesize = $header['size'];
$n = floor($header['size'] / 512);
for ($i = 0; $i < $n; $i++) {
$content = $this->archive
->readBytes(512);
$filename .= $content;
}
if ($header['size'] % 512 != 0) {
$content = $this->archive
->readBytes(512);
$filename .= $content;
}
$filename = rtrim(substr($filename, 0, $filesize), "\0");
$data = $this->archive
->readBytes(512);
$header = $this
->readHeader($data, $header);
$header['filename'] = $filename;
return $header;
}
private function maliciousFilename($file) {
if (strpos($file, '/../') !== FALSE) {
return TRUE;
}
if (strpos($file, '../') === 0) {
return TRUE;
}
return FALSE;
}
public function closeArchive() {
if ($this->archive) {
$this->archive
->close();
}
}
}