diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-10-03 00:57:43 +0400 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-10-03 00:57:43 +0400 |
commit | f29c9380249d95fe004d6f2652dfdb318aea0784 (patch) | |
tree | f2af227dac62daa885f173077984917a79cc36ef /core | |
parent | 699f9cd8bd84ee53c8b4e65e948052dbd8858d10 (diff) |
Extracted the decompressing classes into a standalone Decompress component
Diffstat (limited to 'core')
-rw-r--r-- | core/Unzip.php | 14 | ||||
-rwxr-xr-x | core/Unzip/Gzip.php | 82 | ||||
-rw-r--r-- | core/Unzip/PclZip.php | 88 | ||||
-rwxr-xr-x | core/Unzip/Tar.php | 84 | ||||
-rw-r--r-- | core/Unzip/UncompressInterface.php | 39 | ||||
-rw-r--r-- | core/Unzip/ZipArchive.php | 138 |
6 files changed, 6 insertions, 439 deletions
diff --git a/core/Unzip.php b/core/Unzip.php index 5b5495c4e4..547a1a84a5 100644 --- a/core/Unzip.php +++ b/core/Unzip.php @@ -4,19 +4,17 @@ * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * */ namespace Piwik; -use Piwik\Unzip\Gzip; -use Piwik\Unzip\PclZip; -use Piwik\Unzip\Tar; -use Piwik\Unzip\ZipArchive; +use Piwik\Decompress\Gzip; +use Piwik\Decompress\PclZip; +use Piwik\Decompress\Tar; +use Piwik\Decompress\ZipArchive; /** - * Unzip wrapper around ZipArchive and PclZip - * + * Factory for Decompress adapters. */ class Unzip { @@ -25,7 +23,7 @@ class Unzip * * @param string $name Name of unarchiver * @param string $filename Name of .zip archive - * @return \Piwik\Unzip\UncompressInterface + * @return \Piwik\Decompress\DecompressInterface */ public static function factory($name, $filename) { diff --git a/core/Unzip/Gzip.php b/core/Unzip/Gzip.php deleted file mode 100755 index 7e572e35ea..0000000000 --- a/core/Unzip/Gzip.php +++ /dev/null @@ -1,82 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - */ - -namespace Piwik\Unzip; - -/** - * Unzip implementation for .gz files. - * - */ -class Gzip implements UncompressInterface -{ - /** - * Name of .gz file. - * - * @var string - */ - private $filename = null; - - /** - * Error string. - * - * @var string - */ - private $error = null; - - /** - * Constructor. - * - * @param string $filename Name of .gz file. - */ - public function __construct($filename) - { - $this->filename = $filename; - } - - /** - * Extracts the contents of the .gz file to $pathExtracted. - * - * @param string $pathExtracted Must be file, not directory. - * @return bool true if successful, false if otherwise. - */ - public function extract($pathExtracted) - { - $file = gzopen($this->filename, 'r'); - - if ($file === false) { - $this->error = "gzopen failed"; - return false; - } - - $output = fopen($pathExtracted, 'w'); - while (!feof($file)) { - fwrite($output, fread($file, 1024 * 1024)); - } - fclose($output); - - $success = gzclose($file); - if (false === $success) { - $this->error = "gzclose failed"; - return false; - } - - return true; - } - - /** - * Get error status string for the latest error. - * - * @return string - */ - public function errorInfo() - { - return $this->error; - } -} - diff --git a/core/Unzip/PclZip.php b/core/Unzip/PclZip.php deleted file mode 100644 index ff03aac3d9..0000000000 --- a/core/Unzip/PclZip.php +++ /dev/null @@ -1,88 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - */ -namespace Piwik\Unzip; - -/** - * @see libs/PclZip - */ -require_once PIWIK_INCLUDE_PATH . '/libs/PclZip/pclzip.lib.php'; - -/** - * Unzip wrapper around PclZip - * - */ -class PclZip implements UncompressInterface -{ - /** - * @var PclZip - */ - private $pclzip; - /** - * @var string - */ - public $filename; - - /** - * Constructor - * - * @param string $filename Name of the .zip archive - */ - public function __construct($filename) - { - $this->pclzip = new \PclZip($filename); - $this->filename = $filename; - } - - /** - * Extract files from archive to target directory - * - * @param string $pathExtracted Absolute path of target directory - * @return mixed Array of filenames if successful; or 0 if an error occurred - */ - public function extract($pathExtracted) - { - $pathExtracted = str_replace('\\', '/', $pathExtracted); - $list = $this->pclzip->listContent(); - if (empty($list)) { - return 0; - } - - foreach ($list as $entry) { - $filename = str_replace('\\', '/', $entry['stored_filename']); - $parts = explode('/', $filename); - - if (!strncmp($filename, '/', 1) || - array_search('..', $parts) !== false || - strpos($filename, ':') !== false - ) { - return 0; - } - } - - // PCLZIP_CB_PRE_EXTRACT callback returns 0 to skip, 1 to resume, or 2 to abort - return $this->pclzip->extract( - PCLZIP_OPT_PATH, $pathExtracted, - PCLZIP_OPT_STOP_ON_ERROR, - PCLZIP_OPT_REPLACE_NEWER, - PCLZIP_CB_PRE_EXTRACT, function ($p_event, &$p_header) use ($pathExtracted) { - return strncmp($p_header['filename'], $pathExtracted, strlen($pathExtracted)) ? 0 : 1; - } - ); - } - - /** - * Get error status string for the latest error - * - * @return string - */ - public function errorInfo() - { - return $this->pclzip->errorInfo(true); - } -} diff --git a/core/Unzip/Tar.php b/core/Unzip/Tar.php deleted file mode 100755 index 2c87882611..0000000000 --- a/core/Unzip/Tar.php +++ /dev/null @@ -1,84 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - */ -namespace Piwik\Unzip; - -use Archive_Tar; - -/** - * @see libs/Archive_Tar - */ -require_once PIWIK_INCLUDE_PATH . '/libs/Archive_Tar/Tar.php'; - -/** - * Unzip implementation for Archive_Tar PEAR lib. - * - */ -class Tar implements UncompressInterface -{ - /** - * Archive_Tar instance. - * - * @var Archive_Tar - */ - private $tarArchive = null; - - /** - * Constructor. - * - * @param string $filename Path to tar file. - * @param string|null $compression Either 'gz', 'bz2' or null for no compression. - */ - public function __construct($filename, $compression = null) - { - $this->tarArchive = new Archive_Tar($filename, $compression); - } - - /** - * Extracts the contents of the tar file to $pathExtracted. - * - * @param string $pathExtracted Directory to extract into. - * @return bool true if successful, false if otherwise. - */ - public function extract($pathExtracted) - { - return $this->tarArchive->extract($pathExtracted); - } - - /** - * Extracts one file held in a tar archive and returns the deflated file - * as a string. - * - * @param string $inArchivePath Path to file in the tar archive. - * @return bool true if successful, false if otherwise. - */ - public function extractInString($inArchivePath) - { - return $this->tarArchive->extractInString($inArchivePath); - } - - /** - * Lists the files held in the tar archive. - * - * @return array List of paths describing everything held in the tar archive. - */ - public function listContent() - { - return $this->tarArchive->listContent(); - } - - /** - * Get error status string for the latest error. - * - * @return string - */ - public function errorInfo() - { - return $this->tarArchive->error_object->getMessage(); - } -} diff --git a/core/Unzip/UncompressInterface.php b/core/Unzip/UncompressInterface.php deleted file mode 100644 index 072f476bb4..0000000000 --- a/core/Unzip/UncompressInterface.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - */ - -namespace Piwik\Unzip; - -/** - * Unzip interface - * - */ -interface UncompressInterface -{ - /** - * Constructor - * - * @param string $filename Name of the .zip archive - */ - public function __construct($filename); - - /** - * Extract files from archive to target directory - * - * @param string $pathExtracted Absolute path of target directory - * @return mixed Array of filenames if successful; or 0 if an error occurred - */ - public function extract($pathExtracted); - - /** - * Get error status string for the latest error - * - * @return string - */ - public function errorInfo(); -} diff --git a/core/Unzip/ZipArchive.php b/core/Unzip/ZipArchive.php deleted file mode 100644 index e68e168c0c..0000000000 --- a/core/Unzip/ZipArchive.php +++ /dev/null @@ -1,138 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - */ -namespace Piwik\Unzip; - -use Exception; - -/** - * Unzip wrapper around ZipArchive - * - */ -class ZipArchive implements UncompressInterface -{ - /** - * @var \ZipArchive - */ - private $ziparchive; - /** - * @var string - */ - public $filename; - - /** - * Constructor - * - * @param string $filename Name of the .zip archive - * @throws Exception - */ - public function __construct($filename) - { - $this->filename = $filename; - $this->ziparchive = new \ZipArchive; - if ($this->ziparchive->open($filename) !== true) { - throw new Exception('Error opening ' . $filename); - } - } - - /** - * Extract files from archive to target directory - * - * @param string $pathExtracted Absolute path of target directory - * @return mixed Array of filenames if successful; or 0 if an error occurred - */ - public function extract($pathExtracted) - { - if (substr($pathExtracted, -1) !== '/') { - $pathExtracted .= '/'; - } - - $fileselector = array(); - $list = array(); - $count = $this->ziparchive->numFiles; - - if ($count === 0) { - return 0; - } - - for ($i = 0; $i < $count; $i++) { - $entry = $this->ziparchive->statIndex($i); - - $filename = str_replace('\\', '/', $entry['name']); - $parts = explode('/', $filename); - - if (!strncmp($filename, '/', 1) || - array_search('..', $parts) !== false || - strpos($filename, ':') !== false - ) { - return 0; - } - - $fileselector[] = $entry['name']; - $list[] = array( - 'filename' => $pathExtracted . $entry['name'], - 'stored_filename' => $entry['name'], - 'size' => $entry['size'], - 'compressed_size' => $entry['comp_size'], - 'mtime' => $entry['mtime'], - 'index' => $i, - 'crc' => $entry['crc'], - ); - } - - $res = $this->ziparchive->extractTo($pathExtracted, $fileselector); - if ($res === false) { - return 0; - } - - return $list; - } - - /** - * Get error status string for the latest error - * - * @return string - */ - public function errorInfo() - { - static $statusStrings = array( - \ZipArchive::ER_OK => 'No error', - \ZipArchive::ER_MULTIDISK => 'Multi-disk zip archives not supported', - \ZipArchive::ER_RENAME => 'Renaming temporary file failed', - \ZipArchive::ER_CLOSE => 'Closing zip archive failed', - \ZipArchive::ER_SEEK => 'Seek error', - \ZipArchive::ER_READ => 'Read error', - \ZipArchive::ER_WRITE => 'Write error', - \ZipArchive::ER_CRC => 'CRC error', - \ZipArchive::ER_ZIPCLOSED => 'Containing zip archive was closed', - \ZipArchive::ER_NOENT => 'No such file', - \ZipArchive::ER_EXISTS => 'File already exists', - \ZipArchive::ER_OPEN => 'Can\'t open file', - \ZipArchive::ER_TMPOPEN => 'Failure to create temporary file', - \ZipArchive::ER_ZLIB => 'Zlib error', - \ZipArchive::ER_MEMORY => 'Malloc failure', - \ZipArchive::ER_CHANGED => 'Entry has been changed', - \ZipArchive::ER_COMPNOTSUPP => 'Compression method not supported', - \ZipArchive::ER_EOF => 'Premature EOF', - \ZipArchive::ER_INVAL => 'Invalid argument', - \ZipArchive::ER_NOZIP => 'Not a zip archive', - \ZipArchive::ER_INTERNAL => 'Internal error', - \ZipArchive::ER_INCONS => 'Zip archive inconsistent', - \ZipArchive::ER_REMOVE => 'Can\'t remove file', - \ZipArchive::ER_DELETED => 'Entry has been deleted', - ); - - if (isset($statusStrings[$this->ziparchive->status])) { - $statusString = $statusStrings[$this->ziparchive->status]; - } else { - $statusString = 'Unknown status'; - } - - return $statusString . '(' . $this->ziparchive->status . ')'; - } -} |