Skip to content

Commit fd3dd8b

Browse files
author
Sebastian Feldmann
committed
Merge branch 'master' into 2.1
2 parents 5365932 + 1b90792 commit fd3dd8b

File tree

13 files changed

+901
-19
lines changed

13 files changed

+901
-19
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ before_script:
1313
- composer require "aws/aws-sdk-php=2.7.*"
1414
- composer require "barracuda/copy=1.1.*"
1515
- composer require "dropbox/dropbox-sdk=1.1.*"
16-
- composer require "phpseclib/phpseclib=2.0.*@dev"
16+
- composer require "phpseclib/phpseclib=2.0.*"
1717
- composer require "softlayer/objectstorage=dev-master"
1818

1919
script:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"aws/aws-sdk-php": "Require '2.7.*' to sync to Amazon S3",
3737
"barracuda/copy": "Require '1.1.*' to sync to copy.com",
3838
"dropbox/dropbox-sdk": "Require '1.1.*' to sync to Dropbox",
39-
"phpseclib/phpseclib": "Require '2.0.*@dev' to use SFTP sync",
39+
"phpseclib/phpseclib": "Require '2.0.*' to use SFTP sync",
4040
"softlayer/objectstorage": "Require 'dev-master' to sync to Softlayer"
4141
},
4242
"bin": [

doc/config/crypt/openssl.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "openssl",
3+
"options": {
4+
"certFile": "myCertificate.pem",
5+
"algorithm": "aes256",
6+
"keepUncryptedFile": "false",
7+
"showStdError": "false"
8+
}
9+
}

doc/config/crypt/openssl.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<crypt type="openssl">
3+
<!-- use only one of the following -->
4+
<option name="password" value="top-secret" />
5+
<option name="certFile" value="cert.pem" />
6+
7+
<!-- mandatory -->
8+
<!-- be careful certFile and password modes used different algorithms -->
9+
<!-- use `openssl enc -h` and `openssl smime -h` for an exact list -->
10+
<option name="algorithm" value="aes-256-cbc" />
11+
12+
<!-- optional, default=false -->
13+
<option name="keepUncryptedFile" value="false" />
14+
15+
<!-- optional, default=false -->
16+
<option name="showStdError" value="false" />
17+
</crypt>

src/Backup/Crypter/Key.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace phpbu\App\Backup\Crypter;
3+
4+
use phpbu\App\Backup\Cli;
5+
use phpbu\App\Backup\Crypter;
6+
use phpbu\App\Cli\Executable;
7+
use phpbu\App\Result;
8+
use phpbu\App\Util;
9+
10+
/**
11+
* Abstract key crypter class.
12+
*
13+
* @package phpbu
14+
* @subpackage Backup
15+
* @author Sebastian Feldmann <sebastian@phpbu.de>
16+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
17+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
18+
* @link http://phpbu.de/
19+
* @since Class available since Release 2.1.6
20+
*/
21+
abstract class Key extends Cli
22+
{
23+
/**
24+
* Return an absolute path relative to the used file.
25+
*
26+
* @param string $path
27+
* @param string $default
28+
* @return string
29+
*/
30+
protected function toAbsolutePath($path, $default = null)
31+
{
32+
return !empty($path) ? Util\Cli::toAbsolutePath($path, Util\Cli::getBase('configuration')) : $default;
33+
}
34+
}

src/Backup/Crypter/Mcrypt.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace phpbu\App\Backup\Crypter;
33

4-
use phpbu\App\Backup\Cli;
54
use phpbu\App\Backup\Crypter;
65
use phpbu\App\Backup\Target;
76
use phpbu\App\Cli\Executable;
@@ -19,7 +18,7 @@
1918
* @link http://phpbu.de/
2019
* @since Class available since Release 1.3.0
2120
*/
22-
class Mcrypt extends Cli implements Crypter
21+
class Mcrypt extends Key implements Crypter
2322
{
2423
/**
2524
* Path to mcrypt command.
@@ -154,16 +153,4 @@ public function getExecutable(Target $target)
154153

155154
return $this->executable;
156155
}
157-
158-
/**
159-
* Return an absolute path relative to the used configuration.
160-
*
161-
* @param string $path
162-
* @param string $default
163-
* @return string
164-
*/
165-
protected function toAbsolutePath($path, $default = null)
166-
{
167-
return !empty($path) ? Util\Cli::toAbsolutePath($path, Util\Cli::getBase('configuration')) : $default;
168-
}
169156
}

src/Backup/Crypter/OpenSSL.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
namespace phpbu\App\Backup\Crypter;
3+
4+
use phpbu\App\Backup\Crypter;
5+
use phpbu\App\Backup\Target;
6+
use phpbu\App\Cli\Executable;
7+
use phpbu\App\Result;
8+
use phpbu\App\Util;
9+
10+
/**
11+
* OpenSSL crypter class.
12+
*
13+
* @package phpbu
14+
* @subpackage Backup
15+
* @author Sebastian Feldmann <sebastian@phpbu.de>
16+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
17+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
18+
* @link http://phpbu.de/
19+
* @since Class available since Release 2.1.6
20+
*/
21+
class OpenSSL extends Key implements Crypter
22+
{
23+
/**
24+
* Path to mcrypt command.
25+
*
26+
* @var string
27+
*/
28+
private $pathToOpenSSL;
29+
30+
/**
31+
* @var boolean
32+
*/
33+
private $showStdErr;
34+
35+
/**
36+
* Key file
37+
*
38+
* @var string
39+
*/
40+
private $certFile;
41+
42+
/**
43+
* Algorithm to use
44+
*
45+
* @var string
46+
*/
47+
private $algorithm;
48+
49+
/**
50+
* Password to use
51+
*
52+
* @var string
53+
*/
54+
private $password;
55+
56+
/**
57+
* Keep the not encrypted file
58+
*
59+
* @var boolean
60+
*/
61+
private $keepUncrypted;
62+
63+
/**
64+
* Setup.
65+
*
66+
* @see \phpbu\App\Backup\Crypter
67+
* @param array $options
68+
* @throws Exception
69+
*/
70+
public function setup(array $options = array())
71+
{
72+
if (!Util\Arr::isSetAndNotEmptyString($options, 'algorithm')) {
73+
throw new Exception('openssl expects \'algorithm\'');
74+
}
75+
if (!Util\Arr::isSetAndNotEmptyString($options, 'password')
76+
&& !Util\Arr::isSetAndNotEmptyString($options, 'certFile')) {
77+
throw new Exception('openssl expects \'key\' or \'password\'');
78+
}
79+
80+
$this->pathToOpenSSL = Util\Arr::getValue($options, 'pathToOpenSSL');
81+
$this->showStdErr = Util\Str::toBoolean(Util\Arr::getValue($options, 'showStdErr', ''), false);
82+
$this->keepUncrypted = Util\Str::toBoolean(Util\Arr::getValue($options, 'keepUncrypted', ''), false);
83+
$this->certFile = $this->toAbsolutePath(Util\Arr::getValue($options, 'certFile'));
84+
$this->algorithm = Util\Arr::getValue($options, 'algorithm');
85+
$this->password = Util\Arr::getValue($options, 'password');
86+
}
87+
88+
/**
89+
* (non-PHPDoc)
90+
*
91+
* @see \phpbu\App\Backup\Crypter
92+
* @param \phpbu\App\Backup\Target $target
93+
* @param \phpbu\App\Result $result
94+
* @throws Exception
95+
*/
96+
public function crypt(Target $target, Result $result)
97+
{
98+
$openssl = $this->execute($target);
99+
100+
$result->debug('openssl:' . $openssl->getCmd());
101+
102+
if (!$openssl->wasSuccessful()) {
103+
throw new Exception('openssl failed:' . PHP_EOL . $openssl->getOutputAsString());
104+
}
105+
}
106+
107+
/**
108+
* (non-PHPDoc)
109+
*
110+
* @see \phpbu\App\Backup\Crypter
111+
* @return string
112+
*/
113+
public function getSuffix()
114+
{
115+
return 'enc';
116+
}
117+
118+
/**
119+
* Create the Exec to run the 'mcrypt' command.
120+
*
121+
* @param \phpbu\App\Backup\Target $target
122+
* @return \phpbu\App\Cli\Executable
123+
*/
124+
public function getExecutable(Target $target)
125+
{
126+
if (null == $this->executable) {
127+
$this->executable = new Executable\OpenSSL($this->pathToOpenSSL);
128+
$this->executable->encryptFile($target->getPathname());
129+
130+
// use key or password to encrypt
131+
if (!empty($this->certFile)) {
132+
$this->executable->useSSLCert($this->certFile);
133+
} else {
134+
$this->executable->usePassword($this->password)
135+
->encodeBase64(true);
136+
}
137+
$this->executable->useAlgorithm($this->algorithm)
138+
->deleteUncrypted(!$this->keepUncrypted)
139+
->showStdErr($this->showStdErr);
140+
}
141+
142+
return $this->executable;
143+
}
144+
}

0 commit comments

Comments
 (0)