From bae0431dfb7a525d505edcea1ffc374bc913968a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Wed, 14 Feb 2018 01:57:26 -0200 Subject: [PATCH] Refactor PhpMyAdmin\ZipExtension class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace static methods with instance methods. Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/Export.php | 3 +- libraries/classes/File.php | 9 ++- libraries/classes/OpenDocument.php | 3 +- .../classes/Plugins/Import/ImportShp.php | 12 +++- libraries/classes/ZipExtension.php | 69 +++++++++++-------- test/classes/ZipExtensionTest.php | 37 ++++++---- 6 files changed, 82 insertions(+), 51 deletions(-) diff --git a/libraries/classes/Export.php b/libraries/classes/Export.php index a857045655..19ea342b5f 100644 --- a/libraries/classes/Export.php +++ b/libraries/classes/Export.php @@ -417,8 +417,9 @@ class Export public static function compress($dump_buffer, $compression, $filename) { if ($compression == 'zip' && @function_exists('gzcompress')) { + $zipExtension = new ZipExtension(); $filename = substr($filename, 0, -4); // remove extension (.zip) - $dump_buffer = ZipExtension::createFile($dump_buffer, $filename); + $dump_buffer = $zipExtension->createFile($dump_buffer, $filename); } elseif ($compression == 'gzip' && self::gzencodeNeeded()) { // without the optional parameter level because it bugs $dump_buffer = gzencode($dump_buffer); diff --git a/libraries/classes/File.php b/libraries/classes/File.php index e8157867a6..515f9758b1 100644 --- a/libraries/classes/File.php +++ b/libraries/classes/File.php @@ -77,6 +77,11 @@ class File */ var $_charset = null; + /** + * @var ZipExtension + */ + private $zipExtension; + /** * constructor * @@ -89,6 +94,8 @@ class File if ($name && is_string($name)) { $this->setName($name); } + + $this->zipExtension = new ZipExtension(); } /** @@ -662,7 +669,7 @@ class File */ public function openZip($specific_entry = null) { - $result = ZipExtension::getContents($this->getName(), $specific_entry); + $result = $this->zipExtension->getContents($this->getName(), $specific_entry); if (! empty($result['error'])) { $this->_error_message = Message::rawError($result['error']); return false; diff --git a/libraries/classes/OpenDocument.php b/libraries/classes/OpenDocument.php index d03f765f46..77104683db 100644 --- a/libraries/classes/OpenDocument.php +++ b/libraries/classes/OpenDocument.php @@ -171,6 +171,7 @@ EOT; 'META-INF/manifest.xml' ); - return ZipExtension::createFile($data, $name); + $zipExtension = new ZipExtension(); + return $zipExtension->createFile($data, $name); } } diff --git a/libraries/classes/Plugins/Import/ImportShp.php b/libraries/classes/Plugins/Import/ImportShp.php index 4d87dd8cad..2102509a30 100644 --- a/libraries/classes/Plugins/Import/ImportShp.php +++ b/libraries/classes/Plugins/Import/ImportShp.php @@ -28,12 +28,18 @@ use PhpMyAdmin\ZipExtension; */ class ImportShp extends ImportPlugin { + /** + * @var ZipExtension + */ + private $zipExtension; + /** * Constructor */ public function __construct() { $this->setProperties(); + $this->zipExtension = new ZipExtension(); } /** @@ -73,7 +79,7 @@ class ImportShp extends ImportPlugin // If the zip archive has more than one file, // get the correct content to the buffer from .shp file. if ($compression == 'application/zip' - && ZipExtension::getNumberOfFiles($import_file) > 1 + && $this->zipExtension->getNumberOfFiles($import_file) > 1 ) { if ($GLOBALS['import_handle']->openZip('/^.*\.shp$/i') === false) { $message = Message::error( @@ -92,14 +98,14 @@ class ImportShp extends ImportPlugin // If we can extract the zip archive to 'TempDir' // and use the files in it for import if ($compression == 'application/zip' && ! is_null($temp)) { - $dbf_file_name = ZipExtension::findFile( + $dbf_file_name = $this->zipExtension->findFile( $import_file, '/^.*\.dbf$/i' ); // If the corresponding .dbf file is in the zip archive if ($dbf_file_name) { // Extract the .dbf file and point to it. - $extracted = ZipExtension::extract( + $extracted = $this->zipExtension->extract( $import_file, $dbf_file_name ); diff --git a/libraries/classes/ZipExtension.php b/libraries/classes/ZipExtension.php index 8da7df46a4..8fc1159147 100644 --- a/libraries/classes/ZipExtension.php +++ b/libraries/classes/ZipExtension.php @@ -16,6 +16,19 @@ use ZipArchive; */ class ZipExtension { + /** + * @var ZipArchive + */ + private $zip; + + /** + * Constructor + */ + public function __construct() + { + $this->zip = new ZipArchive(); + } + /** * Gets zip file contents * @@ -25,7 +38,7 @@ class ZipExtension * @return array ($error_message, $file_data); $error_message * is empty if no error */ - public static function getContents($file, $specific_entry = null) + public function getContents($file, $specific_entry = null) { /** * This function is used to "import" a SQL file which has been exported earlier @@ -36,33 +49,32 @@ class ZipExtension $error_message = ''; $file_data = ''; - $zip = new ZipArchive; - $res = $zip->open($file); + $res = $this->zip->open($file); if ($res === true) { - if ($zip->numFiles === 0) { + if ($this->zip->numFiles === 0) { $error_message = __('No files found inside ZIP archive!'); - $zip->close(); + $this->zip->close(); return (array('error' => $error_message, 'data' => $file_data)); } /* Is the the zip really an ODS file? */ $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet'; - $first_zip_entry = $zip->getFromIndex(0); + $first_zip_entry = $this->zip->getFromIndex(0); if (!strcmp($ods_mime, $first_zip_entry)) { $specific_entry = '/^content\.xml$/'; } if (!isset($specific_entry)) { $file_data = $first_zip_entry; - $zip->close(); + $this->zip->close(); return (array('error' => $error_message, 'data' => $file_data)); } /* Return the correct contents, not just the first entry */ - for ($i = 0; $i < $zip->numFiles; $i++) { - if (@preg_match($specific_entry, $zip->getNameIndex($i))) { - $file_data = $zip->getFromIndex($i); + for ($i = 0; $i < $this->zip->numFiles; $i++) { + if (@preg_match($specific_entry, $this->zip->getNameIndex($i))) { + $file_data = $this->zip->getFromIndex($i); break; } } @@ -73,11 +85,11 @@ class ZipExtension . ' Could not find "' . $specific_entry . '"'; } - $zip->close(); + $this->zip->close(); return (array('error' => $error_message, 'data' => $file_data)); } else { - $error_message = __('Error in ZIP archive:') . ' ' . $zip->getStatusString(); - $zip->close(); + $error_message = __('Error in ZIP archive:') . ' ' . $this->zip->getStatusString(); + $this->zip->close(); return (array('error' => $error_message, 'data' => $file_data)); } } @@ -90,16 +102,15 @@ class ZipExtension * * @return string the file name of the first file that matches the given regular expression */ - public static function findFile($file, $regex) + public function findFile($file, $regex) { - $zip = new ZipArchive; - $res = $zip->open($file); + $res = $this->zip->open($file); if ($res === true) { - for ($i = 0; $i < $zip->numFiles; $i++) { - if (preg_match($regex, $zip->getNameIndex($i))) { - $filename = $zip->getNameIndex($i); - $zip->close(); + for ($i = 0; $i < $this->zip->numFiles; $i++) { + if (preg_match($regex, $this->zip->getNameIndex($i))) { + $filename = $this->zip->getNameIndex($i); + $this->zip->close(); return $filename; } } @@ -114,14 +125,13 @@ class ZipExtension * * @return int the number of files in the zip archive or 0, either if there wern't any files or an error occured. */ - public static function getNumberOfFiles($file) + public function getNumberOfFiles($file) { $num = 0; - $zip = new ZipArchive; - $res = $zip->open($file); + $res = $this->zip->open($file); if ($res === true) { - $num = $zip->numFiles; + $num = $this->zip->numFiles; } return $num; } @@ -134,12 +144,11 @@ class ZipExtension * * @return string|bool data on sucess, false otherwise */ - public static function extract($file, $entry) + public function extract($file, $entry) { - $zip = new ZipArchive; - if ($zip->open($file) === true) { - $result = $zip->getFromName($entry); - $zip->close(); + if ($this->zip->open($file) === true) { + $result = $this->zip->getFromName($entry); + $this->zip->close(); return $result; } return false; @@ -156,7 +165,7 @@ class ZipExtension * * @return string|bool the ZIP file contents, or false if there was an error. */ - public static function createFile($data, $name, $time = 0) + public function createFile($data, $name, $time = 0) { $datasec = array(); // Array to store compressed data $ctrl_dir = array(); // Central directory diff --git a/test/classes/ZipExtensionTest.php b/test/classes/ZipExtensionTest.php index 54b964f1db..f99fc6d285 100644 --- a/test/classes/ZipExtensionTest.php +++ b/test/classes/ZipExtensionTest.php @@ -17,8 +17,15 @@ use ZipArchive; */ class ZipExtensionTest extends PmaTestCase { + private $zipExtension; + + protected function setUp() + { + $this->zipExtension = new ZipExtension(); + } + /** - * Test for ZipExtension::getContents + * Test for getContents * * @param string $file path to zip file * @param string $specific_entry regular expression to match a file @@ -30,7 +37,7 @@ class ZipExtensionTest extends PmaTestCase public function testGetContents($file, $specific_entry, $output) { $this->assertEquals( - ZipExtension::getContents($file, $specific_entry), + $this->zipExtension->getContents($file, $specific_entry), $output ); } @@ -63,7 +70,7 @@ class ZipExtensionTest extends PmaTestCase } /** - * Test for ZipExtension::findFile + * Test for findFile * * @param string $file path to zip file * @param string $file_regexp regular expression for the file name to match @@ -75,7 +82,7 @@ class ZipExtensionTest extends PmaTestCase public function testFindFile($file, $file_regexp, $output) { $this->assertEquals( - ZipExtension::findFile($file, $file_regexp), + $this->zipExtension->findFile($file, $file_regexp), $output ); } @@ -97,20 +104,20 @@ class ZipExtensionTest extends PmaTestCase } /** - * Test for ZipExtension::getNumberOfFiles + * Test for getNumberOfFiles * * @return void */ public function testGetNumberOfFiles() { $this->assertEquals( - ZipExtension::getNumberOfFiles('./test/test_data/test.zip'), + $this->zipExtension->getNumberOfFiles('./test/test_data/test.zip'), 1 ); } /** - * Test for ZipExtension::extract + * Test for extract * * @return void */ @@ -118,13 +125,13 @@ class ZipExtensionTest extends PmaTestCase { $this->assertEquals( false, - ZipExtension::extract( + $this->zipExtension->extract( './test/test_data/test.zip', 'wrongName' ) ); $this->assertEquals( "TEST FILE\n", - ZipExtension::extract( + $this->zipExtension->extract( './test/test_data/test.zip', 'test.file' ) ); @@ -153,13 +160,13 @@ class ZipExtensionTest extends PmaTestCase } /** - * Test for ZipExtension::createFile + * Test for createFile * * @return void */ public function testCreateSingleFile() { - $file = ZipExtension::createFile("Test content", "test.txt"); + $file = $this->zipExtension->createFile("Test content", "test.txt"); $this->assertNotEmpty($file); $zip = $this->getZip($file); @@ -168,7 +175,7 @@ class ZipExtensionTest extends PmaTestCase } /** - * Test for ZipExtension::createFile + * Test for createFile * * @return void */ @@ -176,7 +183,7 @@ class ZipExtensionTest extends PmaTestCase { $this->assertEquals( false, - ZipExtension::createFile( + $this->zipExtension->createFile( "Content", array("name1.txt", "name2.txt") ) @@ -184,13 +191,13 @@ class ZipExtensionTest extends PmaTestCase } /** - * Test for ZipExtension::createFile + * Test for createFile * * @return void */ public function testCreateMultiFile() { - $file = ZipExtension::createFile( + $file = $this->zipExtension->createFile( array("Content", 'Content2'), array("name1.txt", "name2.txt") );