View source
<?php
namespace TYPO3\PharStreamWrapper\Phar;
class Reader {
private $fileName;
private $fileType;
public function __construct($fileName) {
if (strpos($fileName, '://') !== false) {
throw new ReaderException('File name must not contain stream prefix', 1539623708);
}
$this->fileName = $fileName;
$this->fileType = $this
->determineFileType();
}
public function resolveContainer() {
$data = $this
->extractData($this
->resolveStream() . $this->fileName);
if ($data['stubContent'] === null) {
throw new ReaderException('Cannot resolve stub', 1547807881);
}
if ($data['manifestContent'] === null || $data['manifestLength'] === null) {
throw new ReaderException('Cannot resolve manifest', 1547807882);
}
if (strlen($data['manifestContent']) < $data['manifestLength']) {
throw new ReaderException(sprintf('Exected manifest length %d, got %d', strlen($data['manifestContent']), $data['manifestLength']), 1547807883);
}
return new Container(Stub::fromContent($data['stubContent']), Manifest::fromContent($data['manifestContent']));
}
private function extractData($fileName) {
$stubContent = null;
$manifestContent = null;
$manifestLength = null;
$resource = fopen($fileName, 'r');
if (!is_resource($resource)) {
throw new ReaderException(sprintf('Resource %s could not be opened', $fileName), 1547902055);
}
while (!feof($resource)) {
$line = fgets($resource);
if ($manifestLength !== null && $manifestContent !== null && strlen($manifestContent) >= $manifestLength) {
break;
}
$manifestPosition = strpos($line, '__HALT_COMPILER();');
if ($stubContent === null && $manifestContent === null && $manifestPosition !== false) {
$stubContent = substr($line, 0, $manifestPosition - 1);
$manifestContent = preg_replace('#^.*__HALT_COMPILER\\(\\);(?>[ \\n]\\?>(?>\\r\\n|\\n)?)?#', '', $line);
$manifestLength = $this
->resolveManifestLength($manifestContent);
}
elseif ($stubContent === null) {
$stubContent = $line;
}
elseif ($manifestContent === null && $manifestPosition !== false) {
$manifestContent = preg_replace('#^.*__HALT_COMPILER\\(\\);(?>[ \\n]\\?>(?>\\r\\n|\\n)?)?#', '', $line);
$manifestLength = $this
->resolveManifestLength($manifestContent);
}
elseif ($manifestContent !== null) {
$manifestContent .= $line;
$manifestLength = $this
->resolveManifestLength($manifestContent);
}
elseif ($stubContent !== null) {
$stubContent .= $line;
}
}
fclose($resource);
return array(
'stubContent' => $stubContent,
'manifestContent' => $manifestContent,
'manifestLength' => $manifestLength,
);
}
private function resolveStream() {
if ($this->fileType === 'application/x-gzip' || $this->fileType === 'application/gzip') {
return 'compress.zlib://';
}
elseif ($this->fileType === 'application/x-bzip2') {
return 'compress.bzip2://';
}
return '';
}
private function determineFileType() {
if (class_exists('\\finfo')) {
$fileInfo = new \finfo();
return $fileInfo
->file($this->fileName, FILEINFO_MIME_TYPE);
}
return $this
->determineFileTypeByHeader();
}
private function determineFileTypeByHeader() {
$resource = fopen($this->fileName, 'r');
if (!is_resource($resource)) {
throw new ReaderException(sprintf('Resource %s could not be opened', $this->fileName), 1557753055);
}
$header = fgets($resource, 4);
fclose($resource);
$mimeType = '';
if (strpos($header, "BZh") === 0) {
$mimeType = 'application/x-bzip2';
}
elseif (strpos($header, "") === 0) {
$mimeType = 'application/x-gzip';
}
return $mimeType;
}
private function resolveManifestLength($content) {
if (strlen($content) < 4) {
return null;
}
return static::resolveFourByteLittleEndian($content, 0);
}
public static function resolveFourByteLittleEndian($content, $start) {
$payload = substr($content, $start, 4);
if (!is_string($payload)) {
throw new ReaderException(sprintf('Cannot resolve value at offset %d', $start), 1539614260);
}
$value = unpack('V', $payload);
if (!isset($value[1])) {
throw new ReaderException(sprintf('Cannot resolve value at offset %d', $start), 1539614261);
}
return $value[1];
}
public static function resolveTwoByteBigEndian($content, $start) {
$payload = substr($content, $start, 2);
if (!is_string($payload)) {
throw new ReaderException(sprintf('Cannot resolve value at offset %d', $start), 1539614263);
}
$value = unpack('n', $payload);
if (!isset($value[1])) {
throw new ReaderException(sprintf('Cannot resolve value at offset %d', $start), 1539614264);
}
return $value[1];
}
}