Total Complexity | 59 |
Total Lines | 359 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 9 | Features | 0 |
Complex classes like Xcloner_Encryption often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Xcloner_Encryption, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Xcloner_Encryption |
||
12 | { |
||
13 | const FILE_ENCRYPTION_BLOCKS = 1024 * 1024; |
||
14 | const FILE_ENCRYPTION_SUFFIX = ".encrypted"; |
||
15 | const FILE_DECRYPTION_SUFFIX = ".decrypted"; |
||
16 | |||
17 | private $xcloner_settings; |
||
18 | private $logger; |
||
19 | private $xcloner_container; |
||
20 | private $verification = false; |
||
21 | |||
22 | /** |
||
23 | * Xcloner_Encryption constructor. |
||
24 | * @param Xcloner $xcloner_container |
||
25 | */ |
||
26 | public function __construct(Xcloner $xcloner_container) |
||
27 | { |
||
28 | $this->xcloner_container = $xcloner_container; |
||
29 | if (property_exists($xcloner_container, 'xcloner_settings')) { |
||
30 | $this->xcloner_settings = $xcloner_container->get_xcloner_settings(); |
||
31 | } else { |
||
32 | $this->xcloner_settings = ""; |
||
33 | } |
||
34 | |||
35 | if (property_exists($xcloner_container, 'xcloner_logger')) { |
||
36 | $this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_encryption"); |
||
37 | } else { |
||
38 | $this->logger = ""; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Returns the backup encryption key |
||
44 | * |
||
45 | * @return string|null |
||
46 | */ |
||
47 | public function get_backup_encryption_key() |
||
48 | { |
||
49 | if (is_object($this->xcloner_settings)) { |
||
50 | return $this->xcloner_settings->get_xcloner_encryption_key(); |
||
51 | } |
||
52 | |||
53 | return null; |
||
54 | |||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Check if provided filename has encrypted suffix |
||
59 | * |
||
60 | * @param $filename |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function is_encrypted_file($filename) { |
||
64 | $fp = fopen($this->get_xcloner_path().$filename, 'r'); |
||
65 | if (is_resource($fp)) { |
||
66 | $encryption_length = fread($fp, 16); |
||
67 | fclose($fp); |
||
68 | if (is_numeric($encryption_length)) { |
||
69 | return true; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | return false; |
||
74 | |||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Returns the filename with encrypted suffix |
||
79 | * |
||
80 | * @param string $filename |
||
81 | * @return string |
||
82 | */ |
||
83 | public function get_encrypted_target_backup_file_name($filename) { |
||
84 | |||
85 | return str_replace(self::FILE_DECRYPTION_SUFFIX, "", $filename).self::FILE_ENCRYPTION_SUFFIX; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Returns the filename without encrypted suffix |
||
90 | * |
||
91 | * @param string $filename |
||
92 | * @return string |
||
93 | */ |
||
94 | public function get_decrypted_target_backup_file_name($filename) { |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Encrypt the passed file and saves the result in a new file with ".enc" as suffix. |
||
101 | * |
||
102 | * @param string $source Path to file that should be encrypted |
||
103 | * @param string $dest File name where the encryped file should be written to. |
||
104 | * @param string $key The key used for the encryption |
||
105 | * @param int $start Start position for reading when doing incremental mode. |
||
106 | * @param string $iv The IV key to use. |
||
107 | * @param bool $verification Weather we should we try to verify the decryption. |
||
108 | * @return array|false Returns array or FALSE if an error occured |
||
109 | * @throws Exception |
||
110 | */ |
||
111 | public function encrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $verification = true, $recursive = false) |
||
112 | { |
||
113 | if (is_object($this->logger)) { |
||
114 | $this->logger->info(sprintf('Encrypting file %s at position %d IV %s', $source, $start, base64_encode($iv))); |
||
115 | } |
||
116 | |||
117 | //$key = substr(sha1($key, true), 0, 16); |
||
118 | if (!$key) { |
||
119 | $key = $this->get_backup_encryption_key(); |
||
120 | } |
||
121 | $key_digest = openssl_digest($key, "md5", true); |
||
122 | |||
123 | $keep_local = 1; |
||
124 | if (!$dest) { |
||
125 | $dest = $this->get_encrypted_target_backup_file_name($source); |
||
126 | $keep_local = 0; |
||
127 | } |
||
128 | |||
129 | if (!$iv || !$start) { |
||
130 | //$iv = openssl_random_pseudo_bytes(16); |
||
131 | $iv = str_pad(self::FILE_ENCRYPTION_BLOCKS, 16, "0000000000000000", STR_PAD_LEFT); |
||
132 | } |
||
133 | |||
134 | if (!$start) { |
||
135 | $fpOut = fopen($this->get_xcloner_path().$dest, 'w'); |
||
136 | } else { |
||
137 | $fpOut = fopen($this->get_xcloner_path().$dest, 'a'); |
||
138 | } |
||
139 | |||
140 | if (is_resource($fpOut)) { |
||
141 | |||
142 | // Put the initialization vector to the beginning of the file |
||
143 | if (!$start) { |
||
144 | fwrite($fpOut, $iv); |
||
145 | } |
||
146 | |||
147 | if (file_exists($this->get_xcloner_path().$source) && |
||
148 | $fpIn = fopen($this->get_xcloner_path().$source, 'rb')) { |
||
149 | |||
150 | fseek($fpIn, (int)$start); |
||
151 | |||
152 | if (!feof($fpIn)) { |
||
153 | |||
154 | $plaintext = fread($fpIn, 16 * self::FILE_ENCRYPTION_BLOCKS); |
||
155 | $ciphertext = openssl_encrypt($plaintext, 'AES-128-CBC', $key_digest, OPENSSL_RAW_DATA, $iv); |
||
156 | |||
157 | // Use the first 16 bytes of the ciphertext as the next initialization vector |
||
158 | $iv = substr($ciphertext, 0, 16); |
||
159 | //$iv = openssl_random_pseudo_bytes(16); |
||
160 | |||
161 | fwrite($fpOut, $ciphertext); |
||
162 | |||
163 | $start = ftell($fpIn); |
||
164 | |||
165 | fclose($fpOut); |
||
166 | |||
167 | unset($ciphertext); |
||
168 | unset($plaintext); |
||
169 | |||
170 | if (!feof($fpIn)) { |
||
171 | fclose($fpIn); |
||
172 | //echo "\n NEW:".$key.md5($iv); |
||
173 | //self::encryptFile($source, $dest, $key, $start, $iv); |
||
174 | if ($recursive) { |
||
175 | $this->encrypt_file($source, $dest, $key, $start, ($iv), $verification, $recursive); |
||
176 | } else { |
||
177 | |||
178 | if (($iv) != base64_decode(base64_encode($iv))) |
||
179 | { |
||
180 | throw new \Exception('Could not encode IV for transport'); |
||
181 | } |
||
182 | |||
183 | return array( |
||
184 | "start" => $start, |
||
185 | "iv" => base64_encode($iv), |
||
186 | "target_file" => $dest, |
||
187 | "finished" => 0 |
||
188 | ); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | } |
||
193 | } else { |
||
194 | if (is_object($this->logger)) { |
||
195 | $this->logger->error('Unable to read source file for encryption.'); |
||
196 | } |
||
197 | throw new \Exception("Unable to read source file for encryption."); |
||
198 | } |
||
199 | } else { |
||
200 | if (is_object($this->logger)) { |
||
201 | $this->logger->error('Unable to write destination file for encryption.'); |
||
202 | } |
||
203 | throw new \Exception("Unable to write destination file for encryption."); |
||
204 | } |
||
205 | |||
206 | if ($verification) { |
||
207 | $this->verify_encrypted_file($dest); |
||
208 | } |
||
209 | |||
210 | //we replace the original backup with the encrypted one |
||
211 | if (!$keep_local && copy($this->get_xcloner_path().$dest, |
||
212 | $this->get_xcloner_path().$source)) { |
||
213 | unlink($this->get_xcloner_path().$dest); |
||
214 | } |
||
215 | |||
216 | |||
217 | return array("target_file" => $dest, "finished" => 1); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param string $file |
||
222 | */ |
||
223 | public function verify_encrypted_file($file) { |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Dencrypt the passed file and saves the result in a new file, removing the |
||
235 | * last 4 characters from file name. |
||
236 | * |
||
237 | * @param string $source Path to file that should be decrypted |
||
238 | * @param string $dest File name where the decryped file should be written to. |
||
239 | * @param string $key The key used for the decryption (must be the same as for encryption) |
||
240 | * @param int $start Start position for reading when doing incremental mode. |
||
241 | * @param string $iv The IV key to use. |
||
242 | * @return array|false Returns array or FALSE if an error occured |
||
243 | * @throws Exception |
||
244 | */ |
||
245 | public function decrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $recursive = false) |
||
362 | } |
||
363 | |||
364 | public function get_xcloner_path() { |
||
370 | } |
||
371 | |||
398 |