|
| 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