Skip to content

Commit

Permalink
Merge pull request #1345 from HDInnovations/analysis-2Qb4Dr
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
HDVinnie authored May 19, 2020
2 parents 4748e9e + f90bfe8 commit 9c5427b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 44 deletions.
25 changes: 13 additions & 12 deletions app/Helpers/BackupEncryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,75 +16,76 @@
class BackupEncryption
{
/**
* Default encryption contants
* Default encryption contants.
*
* @var string
*/
const ENCRYPTION_DEFAULT = 'default';

/**
* AES-128 encryption contants
* AES-128 encryption contants.
*
* @var string
*/
const ENCRYPTION_WINZIP_AES_128 = 'aes_128';

/**
* AES-192 encryption contants
* AES-192 encryption contants.
*
* @var string
*/
const ENCRYPTION_WINZIP_AES_192 = 'aes_192';

/**
* AES-256 encryption contants
* AES-256 encryption contants.
*
* @var string
*/
const ENCRYPTION_WINZIP_AES_256 = 'aes_256';

/**
* ZipArchive encryption constants; stores as simple string for PHP < 7.2
* backwards compatability
* backwards compatability.
*
* @var array
*/
private $zipArchiveOptions = [
self::ENCRYPTION_DEFAULT => '257',
self::ENCRYPTION_DEFAULT => '257',
self::ENCRYPTION_WINZIP_AES_128 => '257',
self::ENCRYPTION_WINZIP_AES_192 => '258',
self::ENCRYPTION_WINZIP_AES_256 => '259',
];

/**
* ZipFile encryption constants
* ZipFile encryption constants.
*
* @var array
*/
private $zipFileOptions = [
self::ENCRYPTION_DEFAULT => \PhpZip\Constants\ZipEncryptionMethod::PKWARE,
self::ENCRYPTION_DEFAULT => \PhpZip\Constants\ZipEncryptionMethod::PKWARE,
self::ENCRYPTION_WINZIP_AES_128 => \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_128,
self::ENCRYPTION_WINZIP_AES_192 => \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_192,
self::ENCRYPTION_WINZIP_AES_256 => \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_256,
];

/**
* Retrive appropriate encryption constant
* Retrive appropriate encryption constant.
*
* @param string $type
* @param string $engine
*
* @return mixed
* @throws \Exception
*
* @return mixed
*/
public function getEncryptionConstant($type, $engine)
{
if ($engine == 'ZipArchive' && isset($this->zipArchiveOptions[$type])) {
return $this->zipArchiveOptions[$type];
} elseif ($engine == 'ZipFile' && isset($this->zipFileOptions[$type])) {
return $this->zipFileOptions[$type];
} else {
throw new \Exception("Encryption key not set or invalid value", 1);
}

throw new \Exception('Encryption key not set or invalid value', 1);
}
}
35 changes: 18 additions & 17 deletions app/Helpers/BackupPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@

use Illuminate\Support\Collection;
use PhpZip\ZipFile;
use \ZipArchive;
use ZipArchive;

class BackupPassword
{
/**
* Path to .zip-file
* Path to .zip-file.
*
* @var string
*/
public $path;

/**
* The chosen password
* The chosen password.
*
* @var string
*/
protected $password;

/**
* Read the .zip, apply password and encryption, then rewrite the file
* Read the .zip, apply password and encryption, then rewrite the file.
*
* @param \App\Helpers\BackupEncryption $encryption
* @param string $path the path to the .zip-file
* @param string $path the path to the .zip-file
*
* @throws \PhpZip\Exception\ZipException
*/
function __construct(BackupEncryption $encryption, string $path)
public function __construct(BackupEncryption $encryption, string $path)
{
$this->password = config('backup.security.password');

if (!$this->password) {
if (! $this->password) {
return $this->path = $path;
}

Expand All @@ -65,23 +65,23 @@ function __construct(BackupEncryption $encryption, string $path)
}

/**
* Use native PHP ZipArchive
* Use native PHP ZipArchive.
*
* @param \App\Helpers\BackupEncryption $encryption
* @param string $path
*
* @param string $path
*
* @return void
* @throws \Exception
*
* @return void
*/
protected function makeZipArchive(BackupEncryption $encryption, string $path) : void
protected function makeZipArchive(BackupEncryption $encryption, string $path): void
{
$encryptionConstant = $encryption->getEncryptionConstant(
config('backup.security.encryption'),
'ZipArchive'
);

$zipArchive = new ZipArchive;
$zipArchive = new ZipArchive();

$zipArchive->open($path, ZipArchive::OVERWRITE);
$zipArchive->addFile($path, 'backup.zip');
Expand All @@ -95,14 +95,15 @@ protected function makeZipArchive(BackupEncryption $encryption, string $path) :
}

/**
* Use PhpZip\ZipFile-package to create the zip
* Use PhpZip\ZipFile-package to create the zip.
*
* @param \App\Helpers\BackupEncryption $encryption
*
* @return void
* @throws \PhpZip\Exception\ZipException
*
* @return void
*/
protected function makeZipFile(BackupEncryption $encryption, string $path) : void
protected function makeZipFile(BackupEncryption $encryption, string $path): void
{
$encryptionConstant = $encryption->getEncryptionConstant(
config('backup.security.encryption'),
Expand All @@ -117,4 +118,4 @@ protected function makeZipFile(BackupEncryption $encryption, string $path) : voi

$this->path = $path;
}
}
}
20 changes: 10 additions & 10 deletions app/Http/Controllers/Staff/BackupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use Exception;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\Adapter\Local;

Expand Down Expand Up @@ -92,11 +92,11 @@ public function create(Request $request)
$output = Artisan::output();
if (strpos($output, 'Backup failed because')) {
preg_match('Backup failed because(.*?)$/ms', $output, $match);
$message = "BackupManager process failed because ";
$message .= isset($match[1]) ? $match[1] : '';
$message = 'BackupManager process failed because ';
$message .= $match[1] ?? '';
Log::error($message.PHP_EOL.$output);
} else {
Log::info("BackupManager process has started");
Log::info('BackupManager process has started');
}
} catch (Exception $e) {
Log::error($e);
Expand Down Expand Up @@ -128,11 +128,11 @@ public function files(Request $request)
$output = Artisan::output();
if (strpos($output, 'Backup failed because')) {
preg_match('Backup failed because(.*?)$/ms', $output, $match);
$message = "BackupManager process failed because ";
$message .= isset($match[1]) ? $match[1] : '';
$message = 'BackupManager process failed because ';
$message .= $match[1] ?? '';
Log::error($message.PHP_EOL.$output);
} else {
Log::info("BackupManager process has started");
Log::info('BackupManager process has started');
}
} catch (Exception $e) {
Log::error($e);
Expand Down Expand Up @@ -164,11 +164,11 @@ public function database(Request $request)
$output = Artisan::output();
if (strpos($output, 'Backup failed because')) {
preg_match('Backup failed because(.*?)$/ms', $output, $match);
$message = "BackupManager process failed because ";
$message .= isset($match[1]) ? $match[1] : '';
$message = 'BackupManager process failed because ';
$message .= $match[1] ?? '';
Log::error($message.PHP_EOL.$output);
} else {
Log::info("BackupManager process has started");
Log::info('BackupManager process has started');
}
} catch (Exception $e) {
Log::error($e);
Expand Down
7 changes: 4 additions & 3 deletions app/Listeners/PasswordProtectBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public function __construct()
*
* @param \Spatie\Backup\Events\BackupZipWasCreated $event
*
* @return string
* @throws \PhpZip\Exception\ZipException
*
* @return string
*/
public function handle(BackupZipWasCreated $event) : string
public function handle(BackupZipWasCreated $event): string
{
return (new BackupPassword(new \App\Helpers\BackupEncryption, $event->pathToZip))->path;
return (new BackupPassword(new \App\Helpers\BackupEncryption(), $event->pathToZip))->path;
}
}
4 changes: 2 additions & 2 deletions config/backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@
],

'security' => [
'password' => env('APP_KEY'),
'encryption' => \App\Helpers\BackupEncryption::ENCRYPTION_DEFAULT
'password' => env('APP_KEY'),
'encryption' => \App\Helpers\BackupEncryption::ENCRYPTION_DEFAULT,

// Available encryption methods:
// \App\Helpers\BackupEncryption::ENCRYPTION_DEFAULT (PHP < 7.2: PKWARE/ZipCrypto, PHP >= 7.2: AES 128)
Expand Down

0 comments on commit 9c5427b

Please sign in to comment.