Skip to content

Dev #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 13, 2019
Merged

Dev #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ matrix:
- php: 7.1
env:
- ILLUMINATE_VERSION=5.8.*
- php: 7.2
- php: 7.2.24
env:
- ILLUMINATE_VERSION=5.8.*
- php: 7.2
- php: 7.2.24
env:
- ILLUMINATE_VERSION=6.0.*
- php: 7.2.24
env:
- ILLUMINATE_VERSION=^6.0
- php: 7.3
env:
- ILLUMINATE_VERSION=^6.0
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Backup Shield simply listens for when the .zip-file generated by Laravel-backup
composer require olssonm/laravel-backup-shield
```

Requires `PHP: "^7.1"` and `laravel/framework: "^5.8"`. **Compatible with Laravel 6**
Requires `PHP: "^7.1"` and `laravel/framework: "^5.8"`. Compatible with Laravel 6.

Please note that `spatie/laravel-backup: "^6"` and `laravel/framework: "^6.0"` requires PHP 7.2.

Expand Down Expand Up @@ -49,7 +49,7 @@ Set to `NULL` if you want to keep your backup without a password.

Set your type of encryption. Available options are:

`\Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT` (PKWARE/ZipCrypto)
`\Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT` (PHP < 7.2: PKWARE/ZipCrypto, PHP >= 7.2: AES 128)
`\Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_128` (AES 128)
`\Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_192` (AES 192)
`\Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_256` (AES 256)
Expand All @@ -60,6 +60,12 @@ Using the `ENCRYPTION_DEFAULT` (PKWARE/ZipCrypto) crypto gives you the best port

Also to note is that when zipping very large files ZipCrypto might be very inefficient as the entire data-set will have to be loaded into memory to perform the encryption, if the zipped file's content is bigger than your available RAM you *will* run out of memory.

#### Differences when using PHP < 7.2 and PHP >= 7.2

Since PHP 7.2 (coupled with zip-extension >= 1.14.0) PHP can natively password-protect .zip-files via the ZipArchive-methods. If these conditions are met, ZipArchive is used. Else, the package will automatically use the [nelexa/zip](https://github.com/Ne-Lexa/php-zip)-package.

The former might be less memory-intensive.

## Testing

``` bash
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnFailure="true"
>
<testsuites>
<testsuite name="Package Test Suite">
Expand Down
15 changes: 12 additions & 3 deletions src/BackupShieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
namespace Olssonm\BackupShield;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Olssonm\BackupShield\Factories\Password;
use Olssonm\BackupShield\Encryption;

/**
* Laravel service provider for the Backup Shield-package
*/
class BackupShieldServiceProvider extends ServiceProvider
{
/**
Expand Down Expand Up @@ -38,7 +43,7 @@ public function __construct($app) {
*
* @return void
*/
public function boot()
public function boot() : void
{
// Publishing of configuration
$this->publishes([
Expand All @@ -50,11 +55,15 @@ public function boot()

/**
* Register any package services.
*
*
* @return void
*/
public function register()
public function register() : void
{
$this->app->singleton('Olssonm\BackupShield\Encryption', function ($app) {
return new \Olssonm\BackupShield\Encryption;
});

$this->mergeConfigFrom(
$this->config, 'backup-shield'
);
Expand Down
76 changes: 72 additions & 4 deletions src/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,78 @@

use PhpZip\ZipFile;

use \ZipArchive;

class Encryption
{
const ENCRYPTION_DEFAULT = ZipFile::ENCRYPTION_METHOD_TRADITIONAL;
const ENCRYPTION_WINZIP_AES_128 = ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128;
const ENCRYPTION_WINZIP_AES_192 = ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192;
const ENCRYPTION_WINZIP_AES_256 = ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256;
/**
* Default encryption contants
*
* @var string
*/
const ENCRYPTION_DEFAULT = 'default';

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

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

/**
* 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
*
* @var array
*/
private $zipArchiveOptions = [
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
*
* @var array
*/
private $zipFileOptions = [
self::ENCRYPTION_DEFAULT => ZipFile::ENCRYPTION_METHOD_TRADITIONAL,
self::ENCRYPTION_WINZIP_AES_128 => ZipFile::ENCRYPTION_METHOD_WINZIP_AES_128,
self::ENCRYPTION_WINZIP_AES_192 => ZipFile::ENCRYPTION_METHOD_WINZIP_AES_192,
self::ENCRYPTION_WINZIP_AES_256 => ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256,
];

/**
* Retrive appropriate encryption constant
*
* @param string $type
* @param string $engine
* @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);
}
}
}
81 changes: 74 additions & 7 deletions src/Factories/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,100 @@

namespace Olssonm\BackupShield\Factories;

use Illuminate\Support\Collection;

use Olssonm\BackupShield\Encryption;
use PhpZip\ZipFile;
use \ZipArchive;

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

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

/**
* Read the .zip, apply password and encryption, then rewrite the file
* @param string $path the path to the .zip-file
*/
function __construct(string $path)
function __construct(Encryption $encryption, string $path)
{
$this->password = config('backup-shield.password');

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

// If ZipArchive is enabled
if (class_exists('ZipArchive') && in_array('setEncryptionIndex', get_class_methods('ZipArchive'))) {
consoleOutput()->info('Applying password and encryption to zip using ZipArchive...');
$this->makeZipArchive($encryption, $path);
}

// Fall back on PHP-driven ZipFile
else {
consoleOutput()->info('Applying password and encryption to zip using ZipFile...');
$this->makeZipFile($encryption, $path);
}

consoleOutput()->info('Successfully applied password and encryption to zip.');
}

/**
* Use native PHP ZipArchive
*
* @param Encryption $encryption
* @return void
*/
protected function makeZipArchive(Encryption $encryption, string $path) : void
{
consoleOutput()->info('Applying password and encryption to zip...');
$encryptionConstant = $encryption->getEncryptionConstant(
config('backup-shield.encryption'),
'ZipArchive'
);

$zipArchive = new ZipArchive;

$zipArchive->open($path, ZipArchive::OVERWRITE);
$zipArchive->addFile($path, 'backup.zip');
$zipArchive->setPassword($this->password);
Collection::times($zipArchive->numFiles, function ($i) use ($zipArchive, $encryptionConstant) {
$zipArchive->setEncryptionIndex($i - 1, $encryptionConstant);
});
$zipArchive->close();

$this->path = $path;
}

/**
* Use PhpZip\ZipFile-package to create the zip
*
* @param Encryption $encryption
* @return void
*/
protected function makeZipFile(Encryption $encryption, string $path) : void
{
$encryptionConstant = $encryption->getEncryptionConstant(
config('backup-shield.encryption'),
'ZipFile'
);

// Create a new zip, add the zip from spatie/backup, encrypt and resave/overwrite
$zipFile = new ZipFile();
$zipFile->addFile($path, 'backup.zip', ZipFile::METHOD_DEFLATED);
$zipFile->setPassword(config('backup-shield.password'), config('backup-shield.encryption'));
$zipFile->setPassword($this->password, $encryptionConstant);
$zipFile->saveAsFile($path);
$zipFile->close();

consoleOutput()->info('Successfully applied password and encryption to zip.');

$this->path = $path;
}
}
4 changes: 2 additions & 2 deletions src/Listeners/PasswordProtectZip.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function __construct()
* @param \Spatie\Backup\Events\BackupZipWasCreated $event
* @return string
*/
public function handle(BackupZipWasCreated $event)
public function handle(BackupZipWasCreated $event) : string
{
return (new Password($event->pathToZip))->path;
return (new Password(new \Olssonm\BackupShield\Encryption, $event->pathToZip))->path;
}
}
2 changes: 1 addition & 1 deletion src/config/backup-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'encryption' => \Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT

// Available encryption methods:
// \Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT (PKWARE/ZipCrypto)
// \Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT (PHP < 7.2: PKWARE/ZipCrypto, PHP >= 7.2 AES 128)
// \Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_128 (AES 128)
// \Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_192 (AES 192)
// \Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_256 (AES 256)
Expand Down
4 changes: 2 additions & 2 deletions tests/BackupShieldTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function test_listener_return_data()
copy($path, $pathTest);

// Manually set config
config()->set('backup-shield.password', 'Z|n1eMyw3[9&%u=ga$h&');
config()->set('backup-shield.password', 'M79Y6aKARXa9yLrcZd3srz');
config()->set('backup-shield.encryption', \Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_256);

$data = event(new BackupZipWasCreated($pathTest));
Expand All @@ -79,7 +79,7 @@ public function test_encryption_protection()

$this->assertEquals(true, $zipInfo['backup.zip']->isEncrypted());
$this->assertEquals('backup.zip', $zipInfo['backup.zip']->getName());
$this->assertEquals(config('backup-shield.encryption'), $zipInfo['backup.zip']->getEncryptionMethod());
$this->assertEquals(0, $zipInfo['backup.zip']->getEncryptionMethod());
}

/** Teardown */
Expand Down
Binary file modified tests/resources/test.zip
Binary file not shown.