Skip to content

Commit 824f330

Browse files
authored
Restructure to be purely ZipArchive-based (#17)
* Restructure to be purely ZipArchive-based * Remove non-required RDONLY-mode in testing * Update scrutinizer.yml * Update readme * Another scrutinizer-update * Fix spacing-issue
1 parent 8e71542 commit 824f330

File tree

10 files changed

+99
-153
lines changed

10 files changed

+99
-153
lines changed

.scrutinizer.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
filter:
22
excluded_paths: [tests/*]
33

4+
build:
5+
environment:
6+
php:
7+
version: 7.4
8+
9+
# see https://pecl.php.net/
10+
pecl_extensions:
11+
- zip
12+
413
checks:
514
php:
615
remove_extra_empty_lines: true

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ script:
1313

1414
jobs:
1515
include:
16-
- php: 7.2.24
17-
env:
18-
- ILLUMINATE_VERSION=7.0.*
19-
- php: 7.2.24
20-
env:
21-
- ILLUMINATE_VERSION=^6.0
2216
- php: 7.3
2317
env:
2418
- ILLUMINATE_VERSION=^6.0
@@ -34,3 +28,9 @@ jobs:
3428
- php: 7.4
3529
env:
3630
- ILLUMINATE_VERSION=^8.0
31+
- php: 8.0
32+
env:
33+
- ILLUMINATE_VERSION=^7.0
34+
- php: 8.0
35+
env:
36+
- ILLUMINATE_VERSION=^8.0

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ Backup Shield simply listens for when the .zip-file generated by Laravel-backup
1515

1616
*Using older versions of Laravel? Check out the [v1 branch](https://github.com/olssonm/laravel-backup-shield/tree/v1) (for Laravel 5.2) and the [v2 branch](https://github.com/olssonm/laravel-backup-shield/tree/v2).*
1717

18+
## Requirements
19+
20+
`php: 7.3|^8.0`
21+
`ext-zip: ^1.14`
22+
`laravel: ^6|^7|^8`
23+
24+
An appropriate zip-extension should be come with your PHP-install since PHP 7.2. If you for some reason don't have it installed – and don't want to install/upgrade it – look a versions prior to v3.4 of this package.
25+
1826
## Installation
1927

2028
```bash
2129
composer require olssonm/laravel-backup-shield
2230
```
2331

24-
Please note that `spatie/laravel-backup: "^6"` and `laravel/framework: "^6.0|^7.0"` requires PHP 7.2.
25-
2632
## Configuration
2733

2834
Publish your configuration using `php artisan vendor:publish` and select `BackupShieldServiceProvider`. Or directly via ```php artisan vendor:publish --provider="Olssonm\BackupShield\BackupShieldServiceProvider"```.
@@ -47,22 +53,14 @@ Set to `NULL` if you want to keep your backup without a password.
4753

4854
Set your type of encryption. Available options are:
4955

50-
`\Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT` (PHP < 7.2: PKWARE/ZipCrypto, PHP >= 7.2: AES 128)
56+
`\Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT` (AES 128)
5157
`\Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_128` (AES 128)
5258
`\Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_192` (AES 192)
5359
`\Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_256` (AES 256)
5460

55-
**Important information regarding encryption**
56-
57-
Using the `ENCRYPTION_DEFAULT` (PKWARE/ZipCrypto) crypto gives you the best portability as most operating systems can natively unzip the file – however, ZipCrypto might be weak. The Winzip AES-methods on the other hand might require a separate app and/or licence to be able to unzip depending on your OS; suggestions for macOS are [Keka](http://www.kekaosx.com/en/) and [Stuffit Expander](https://itunes.apple.com/us/app/stuffit-expander-16/id919269455).
58-
59-
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.
60-
61-
#### Differences when using PHP < 7.2 and PHP >= 7.2
62-
63-
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.
61+
#### Regarding the layered archive
6462

65-
The former might be less memory-intensive.
63+
This package adds the backup-zip created by spatie/laravel-backup inside a new password protected archive. This is to disable its contents to be able to be viewed without a password – instead only backup.zip will be displayed. Becouse, even without a password, a zip's contents (i.e. the file- and folder names) can be extracted.
6664

6765
## Testing
6866

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
}
1919
],
2020
"require": {
21-
"php": "^7.2",
21+
"php": "^7.3|^8.0",
22+
"ext-zip": "^1.14.0",
2223
"illuminate/support": "^6.0|^7.0|^8.0",
23-
"nelexa/zip": "3.3",
2424
"spatie/laravel-backup": "~6.0"
2525
},
2626
"require-dev": {

src/BackupShieldServiceProvider.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
66
use Illuminate\Support\Facades\Event;
77

8-
use Olssonm\BackupShield\Factories\Password;
9-
use Olssonm\BackupShield\Encryption;
10-
118
use Spatie\Backup\Events\BackupZipWasCreated;
129
use Olssonm\BackupShield\Listeners\PasswordProtectZip;
1310

@@ -61,10 +58,6 @@ public function boot() : void
6158
*/
6259
public function register() : void
6360
{
64-
$this->app->singleton('Olssonm\BackupShield\Encryption', function ($app) {
65-
return new \Olssonm\BackupShield\Encryption;
66-
});
67-
6861
$this->mergeConfigFrom(
6962
$this->config, 'backup-shield'
7063
);

src/Encryption.php

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Olssonm\BackupShield;
44

5-
use PhpZip\ZipFile;
6-
7-
use \ZipArchive;
5+
use ZipArchive;
86

97
class Encryption
108
{
@@ -13,69 +11,26 @@ class Encryption
1311
*
1412
* @var string
1513
*/
16-
const ENCRYPTION_DEFAULT = 'default';
14+
const ENCRYPTION_DEFAULT = ZipArchive::EM_AES_128;
1715

1816
/**
1917
* AES-128 encryption contants
2018
*
2119
* @var string
2220
*/
23-
const ENCRYPTION_WINZIP_AES_128 = 'aes_128';
21+
const ENCRYPTION_WINZIP_AES_128 = ZipArchive::EM_AES_128;
2422

2523
/**
2624
* AES-192 encryption contants
2725
*
2826
* @var string
2927
*/
30-
const ENCRYPTION_WINZIP_AES_192 = 'aes_192';
28+
const ENCRYPTION_WINZIP_AES_192 = ZipArchive::EM_AES_192;
3129

3230
/**
3331
* AES-256 encryption contants
3432
*
3533
* @var string
3634
*/
37-
const ENCRYPTION_WINZIP_AES_256 = 'aes_256';
38-
39-
/**
40-
* ZipArchive encryption constants; stores as simple string for PHP < 7.2
41-
* backwards compatability
42-
*
43-
* @var array
44-
*/
45-
private $zipArchiveOptions = [
46-
self::ENCRYPTION_DEFAULT => '257',
47-
self::ENCRYPTION_WINZIP_AES_128 => '257',
48-
self::ENCRYPTION_WINZIP_AES_192 => '258',
49-
self::ENCRYPTION_WINZIP_AES_256 => '259',
50-
];
51-
52-
/**
53-
* ZipFile encryption constants
54-
*
55-
* @var array
56-
*/
57-
private $zipFileOptions = [
58-
self::ENCRYPTION_DEFAULT => \PhpZip\Constants\ZipEncryptionMethod::PKWARE,
59-
self::ENCRYPTION_WINZIP_AES_128 => \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_128,
60-
self::ENCRYPTION_WINZIP_AES_192 => \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_192,
61-
self::ENCRYPTION_WINZIP_AES_256 => \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_256,
62-
];
63-
64-
/**
65-
* Retrive appropriate encryption constant
66-
*
67-
* @param string $type
68-
* @param string $engine
69-
* @return mixed
70-
*/
71-
public function getEncryptionConstant($type, $engine)
72-
{
73-
if ($engine == 'ZipArchive' && isset($this->zipArchiveOptions[$type])) {
74-
return $this->zipArchiveOptions[$type];
75-
} elseif ($engine == 'ZipFile' && isset($this->zipFileOptions[$type])) {
76-
return $this->zipFileOptions[$type];
77-
} else {
78-
throw new \Exception("Encryption key not set or invalid value", 1);
79-
}
80-
}
35+
const ENCRYPTION_WINZIP_AES_256 = ZipArchive::EM_AES_256;
8136
}

src/Factories/Password.php

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
use Illuminate\Support\Collection;
66
use Olssonm\BackupShield\Encryption;
7-
use PhpZip\ZipFile;
8-
use PhpZip\Constants\ZipCompressionMethod;
7+
98
use \ZipArchive;
109

1110
class Password
@@ -27,77 +26,43 @@ class Password
2726
/**
2827
* Read the .zip, apply password and encryption, then rewrite the file
2928
*
30-
* @param Encryption $encryption
3129
* @param string $path
3230
*/
33-
function __construct(Encryption $encryption, string $path)
31+
function __construct(string $path)
3432
{
3533
$this->password = config('backup-shield.password');
3634

37-
if (!$this->password) {
35+
// If no password is set, just return the backup-path
36+
if (!$this->password) {
3837
return $this->path = $path;
3938
}
4039

41-
// If ZipArchive is enabled
42-
if (class_exists('ZipArchive') && in_array('setEncryptionIndex', get_class_methods('ZipArchive'))) {
43-
consoleOutput()->info('Applying password and encryption to zip using ZipArchive...');
44-
$this->makeZipArchive($encryption, $path);
45-
}
46-
47-
// Fall back on PHP-driven ZipFile
48-
else {
49-
consoleOutput()->info('Applying password and encryption to zip using ZipFile...');
50-
$this->makeZipFile($encryption, $path);
51-
}
40+
consoleOutput()->info('Applying password and encryption to zip using ZipArchive...');
41+
42+
$this->makeZip($path);
5243

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

5647
/**
5748
* Use native PHP ZipArchive
5849
*
59-
* @param Encryption $encryption
6050
* @return void
6151
*/
62-
protected function makeZipArchive(Encryption $encryption, string $path) : void
52+
protected function makeZip(string $path): void
6353
{
64-
$encryptionConstant = $encryption->getEncryptionConstant(
65-
config('backup-shield.encryption'),
66-
'ZipArchive'
67-
);
54+
$encryption = config('backup-shield.encryption');
6855

6956
$zipArchive = new ZipArchive;
7057

7158
$zipArchive->open($path, ZipArchive::OVERWRITE);
7259
$zipArchive->addFile($path, 'backup.zip');
7360
$zipArchive->setPassword($this->password);
74-
Collection::times($zipArchive->numFiles, function ($i) use ($zipArchive, $encryptionConstant) {
75-
$zipArchive->setEncryptionIndex($i - 1, $encryptionConstant);
61+
Collection::times($zipArchive->numFiles, function($i) use ($zipArchive, $encryption) {
62+
$zipArchive->setEncryptionIndex($i - 1, $encryption);
7663
});
7764
$zipArchive->close();
7865

7966
$this->path = $path;
8067
}
81-
82-
/**
83-
* Use PhpZip\ZipFile-package to create the zip
84-
*
85-
* @param Encryption $encryption
86-
* @return void
87-
*/
88-
protected function makeZipFile(Encryption $encryption, string $path) : void
89-
{
90-
$encryptionConstant = $encryption->getEncryptionConstant(
91-
config('backup-shield.encryption'),
92-
'ZipFile'
93-
);
94-
95-
$zipFile = new ZipFile();
96-
$zipFile->addFile($path, 'backup.zip', ZipCompressionMethod::DEFLATED);
97-
$zipFile->setPassword($this->password, $encryptionConstant);
98-
$zipFile->saveAsFile($path);
99-
$zipFile->close();
100-
101-
$this->path = $path;
102-
}
10368
}

src/Listeners/PasswordProtectZip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public function __construct()
2626
*/
2727
public function handle(BackupZipWasCreated $event) : string
2828
{
29-
return (new Password(new \Olssonm\BackupShield\Encryption, $event->pathToZip))->path;
29+
return (new Password($event->pathToZip))->path;
3030
}
3131
}

src/config/backup-shield.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'encryption' => \Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT
66

77
// Available encryption methods:
8-
// \Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT (PHP < 7.2: PKWARE/ZipCrypto, PHP >= 7.2: AES 128)
8+
// \Olssonm\BackupShield\Encryption::ENCRYPTION_DEFAULT (AES 128)
99
// \Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_128 (AES 128)
1010
// \Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_192 (AES 192)
1111
// \Olssonm\BackupShield\Encryption::ENCRYPTION_WINZIP_AES_256 (AES 256)

0 commit comments

Comments
 (0)