Skip to content

Commit ea92806

Browse files
feat: ZipFile to unpackPackage() (#2492)
1 parent 6135ad0 commit ea92806

File tree

3 files changed

+189
-18
lines changed

3 files changed

+189
-18
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"guzzlehttp/guzzle": "^7.5",
3434
"monolog/monolog": "^3.3",
3535
"myclabs/deep-copy": "~1.0",
36+
"nelexa/zip": "^4.0",
3637
"phpseclib/phpseclib": "~3.0",
3738
"robthree/twofactorauth": "^2.0.0",
3839
"symfony/html-sanitizer": "^6.2",

composer.lock

Lines changed: 138 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
namespace phpMyFAQ\Setup;
1818

19-
use JsonException;
2019
use Monolog\Level;
2120
use phpMyFAQ\Configuration;
2221
use phpMyFAQ\Setup;
@@ -26,7 +25,8 @@
2625
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
2726
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
2827
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
29-
use ZipArchive;
28+
use PhpZip\ZipFile;
29+
use PhpZip\Exception\ZipException;
3030

3131
class Upgrade extends Setup
3232
{
@@ -129,8 +129,8 @@ public function downloadPackage(string $version): string|bool
129129
*
130130
* @return bool
131131
* @throws TransportExceptionInterface|ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|JsonException
132-
* @var string $path | Path to zip file
133-
* @var string $version | Version to verify
132+
* @param string $path | Path to zip file
133+
* @param string $version | Version to verify
134134
*/
135135
public function verifyPackage(string $path, string $version): bool
136136
{
@@ -163,34 +163,67 @@ public function verifyPackage(string $path, string $version): bool
163163
* Method to unpack the downloaded phpMyFAQ package
164164
*
165165
* @return bool
166-
* @var string $path Path of the package
166+
* @param string $path | Path of the package
167+
* @throws ZipException
167168
*/
168169
public function unpackPackage(string $path): bool
169170
{
170-
$zip = new ZipArchive();
171-
if (!$zip->open($path)) {
172-
$this->configuration->getLogger()->log(Level::Error, $zip->getStatusString());
171+
$zip = new ZipFile();
172+
try {
173+
if (!is_file($path)) {
174+
return false;
175+
} else {
176+
$zip->openFile($path);
177+
$zip->extractTo(PMF_CONTENT_DIR . '/upgrades/');
178+
$zip->close();
179+
return true;
180+
}
181+
} catch (ZipException $e) {
182+
$this->configuration->getLogger()->log(Level::Error, $e->getMessage());
173183

174184
return false;
175-
} else {
176-
if (!$zip->extractTo(PMF_CONTENT_DIR . '/upgrades/')) {
177-
$this->configuration->getLogger()->log(Level::Error, $zip->getStatusString());
185+
}
186+
}
178187

188+
/**
189+
* Method to create a temporary backup of the current files
190+
*
191+
* @param string $backupName | Name of the created backup
192+
* @return bool
193+
* @throws ZipException
194+
*/
195+
public function createTemporaryBackup(string $backupName): bool
196+
{
197+
try {
198+
$zip = new ZipFile();
199+
if (!is_file(PMF_CONTENT_DIR . '/upgrades/' . $backupName)) {
200+
$zip->addDirRecursive(PMF_ROOT_DIR);
201+
$zip->saveAsFile(PMF_CONTENT_DIR . '/upgrades/' . $backupName);
202+
$zip->close();
203+
return true;
204+
} else {
179205
return false;
180206
}
181-
$zip->close();
207+
} catch (ZipException $e) {
208+
$this->configuration->getLogger()->log(Level::Error, $e->getMessage());
182209

183-
return true;
210+
return false;
184211
}
185212
}
186213

187-
/**
188-
* Method to create a temporary backup of the current files
214+
/**
215+
* Method to delete the temporary created backup.
189216
*
190-
* @return void
217+
* @param string $backupName | Name of the created backup
218+
* @return bool
191219
*/
192-
public function createTemporaryBackup()
220+
public function deleteTemporaryBackup(string $backupName): bool
193221
{
222+
if (is_file(PMF_CONTENT_DIR . '/upgrades/' . $backupName)) {
223+
return unlink(PMF_CONTENT_DIR . '/upgrades/' . $backupName);
224+
} else {
225+
return false;
226+
}
194227
}
195228

196229
/**

0 commit comments

Comments
 (0)