Skip to content

Commit

Permalink
feat: ZipFile to unpackPackage() (#2492)
Browse files Browse the repository at this point in the history
  • Loading branch information
modelrailroader authored Jul 15, 2023
1 parent 6135ad0 commit ea92806
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 18 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"guzzlehttp/guzzle": "^7.5",
"monolog/monolog": "^3.3",
"myclabs/deep-copy": "~1.0",
"nelexa/zip": "^4.0",
"phpseclib/phpseclib": "~3.0",
"robthree/twofactorauth": "^2.0.0",
"symfony/html-sanitizer": "^6.2",
Expand Down
139 changes: 138 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 50 additions & 17 deletions phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace phpMyFAQ\Setup;

use JsonException;
use Monolog\Level;
use phpMyFAQ\Configuration;
use phpMyFAQ\Setup;
Expand All @@ -26,7 +25,8 @@
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use ZipArchive;
use PhpZip\ZipFile;
use PhpZip\Exception\ZipException;

class Upgrade extends Setup
{
Expand Down Expand Up @@ -129,8 +129,8 @@ public function downloadPackage(string $version): string|bool
*
* @return bool
* @throws TransportExceptionInterface|ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|JsonException
* @var string $path | Path to zip file
* @var string $version | Version to verify
* @param string $path | Path to zip file
* @param string $version | Version to verify
*/
public function verifyPackage(string $path, string $version): bool
{
Expand Down Expand Up @@ -163,34 +163,67 @@ public function verifyPackage(string $path, string $version): bool
* Method to unpack the downloaded phpMyFAQ package
*
* @return bool
* @var string $path Path of the package
* @param string $path | Path of the package
* @throws ZipException
*/
public function unpackPackage(string $path): bool
{
$zip = new ZipArchive();
if (!$zip->open($path)) {
$this->configuration->getLogger()->log(Level::Error, $zip->getStatusString());
$zip = new ZipFile();
try {
if (!is_file($path)) {
return false;
} else {
$zip->openFile($path);
$zip->extractTo(PMF_CONTENT_DIR . '/upgrades/');
$zip->close();
return true;
}
} catch (ZipException $e) {
$this->configuration->getLogger()->log(Level::Error, $e->getMessage());

return false;
} else {
if (!$zip->extractTo(PMF_CONTENT_DIR . '/upgrades/')) {
$this->configuration->getLogger()->log(Level::Error, $zip->getStatusString());
}
}

/**
* Method to create a temporary backup of the current files
*
* @param string $backupName | Name of the created backup
* @return bool
* @throws ZipException
*/
public function createTemporaryBackup(string $backupName): bool
{
try {
$zip = new ZipFile();
if (!is_file(PMF_CONTENT_DIR . '/upgrades/' . $backupName)) {
$zip->addDirRecursive(PMF_ROOT_DIR);
$zip->saveAsFile(PMF_CONTENT_DIR . '/upgrades/' . $backupName);
$zip->close();
return true;
} else {
return false;
}
$zip->close();
} catch (ZipException $e) {
$this->configuration->getLogger()->log(Level::Error, $e->getMessage());

return true;
return false;
}
}

/**
* Method to create a temporary backup of the current files
/**
* Method to delete the temporary created backup.
*
* @return void
* @param string $backupName | Name of the created backup
* @return bool
*/
public function createTemporaryBackup()
public function deleteTemporaryBackup(string $backupName): bool
{
if (is_file(PMF_CONTENT_DIR . '/upgrades/' . $backupName)) {
return unlink(PMF_CONTENT_DIR . '/upgrades/' . $backupName);
} else {
return false;
}
}

/**
Expand Down

0 comments on commit ea92806

Please sign in to comment.