Conditions | 25 |
Paths | 2176 |
Total Lines | 117 |
Code Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
233 | public function decrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $recursive = false) |
||
234 | { |
||
235 | if (is_object($this->logger)) { |
||
236 | $this->logger->info(sprintf('Decrypting file %s at position %d with IV %s', $source, $start, base64_encode($iv))); |
||
237 | } |
||
238 | |||
239 | //$key = substr(sha1($key, true), 0, 16); |
||
240 | if (!$key) { |
||
241 | $key = self::get_backup_encryption_key(); |
||
242 | } |
||
243 | |||
244 | $key_digest = openssl_digest($key, "md5", true); |
||
245 | |||
246 | $keep_local = 1; |
||
247 | if (!$dest) { |
||
248 | $dest = $this->get_decrypted_target_backup_file_name($source); |
||
249 | $keep_local = 0; |
||
250 | } |
||
251 | |||
252 | if (!$start) { |
||
253 | if ($this->verification) { |
||
254 | $fpOut = fopen("php://stdout", 'w'); |
||
255 | } else { |
||
256 | $fpOut = fopen($this->get_xcloner_path().$dest, 'w'); |
||
257 | } |
||
258 | } else { |
||
259 | if ($this->verification) { |
||
260 | $fpOut = fopen("php://stdout", 'a'); |
||
261 | } else { |
||
262 | $fpOut = fopen($this->get_xcloner_path().$dest, 'a'); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | if ($fpOut) { |
||
267 | if (file_exists($this->get_xcloner_path().$source) && |
||
268 | $fpIn = fopen($this->get_xcloner_path().$source, 'rb')) { |
||
269 | |||
270 | $encryption_length = (int)fread($fpIn, 16); |
||
271 | if (!$encryption_length) { |
||
272 | $encryption_length = self::FILE_ENCRYPTION_BLOCKS; |
||
273 | } |
||
274 | |||
275 | fseek($fpIn, (int)$start); |
||
276 | |||
277 | // Get the initialzation vector from the beginning of the file |
||
278 | if (!$iv) { |
||
279 | $iv = fread($fpIn, 16); |
||
280 | } |
||
281 | |||
282 | if (!feof($fpIn)) { |
||
283 | |||
284 | // we have to read one block more for decrypting than for encrypting |
||
285 | $ciphertext = fread($fpIn, 16 * ($encryption_length + 1)); |
||
286 | $plaintext = openssl_decrypt($ciphertext, 'AES-128-CBC', $key_digest, OPENSSL_RAW_DATA, $iv); |
||
287 | |||
288 | if (!$plaintext) { |
||
289 | unlink($this->get_xcloner_path().$dest); |
||
290 | if (is_object($this->logger)) { |
||
291 | $this->logger->error('Backup decryption failed, please check your provided Encryption Key.'); |
||
292 | } |
||
293 | throw new \Exception("Backup decryption failed, please check your provided Encryption Key."); |
||
294 | } |
||
295 | |||
296 | // Use the first 16 bytes of the ciphertext as the next initialization vector |
||
297 | $iv = substr($ciphertext, 0, 16); |
||
298 | |||
299 | if (!$this->verification) { |
||
300 | fwrite($fpOut, $plaintext); |
||
301 | } |
||
302 | |||
303 | $start = ftell($fpIn); |
||
304 | |||
305 | fclose($fpOut); |
||
306 | |||
307 | if (!feof($fpIn)) { |
||
308 | fclose($fpIn); |
||
309 | if ($this->verification || $recursive) { |
||
310 | $ciphertext = ""; |
||
311 | $plaintext = ""; |
||
312 | $this->decrypt_file($source, $dest, $key, $start, $iv, $recursive); |
||
313 | } else { |
||
314 | if (($iv) != base64_decode(base64_encode($iv))) |
||
315 | { |
||
316 | throw new \Exception('Could not encode IV for transport'); |
||
317 | } |
||
318 | |||
319 | return array( |
||
320 | "start" => $start, |
||
321 | "encryption_length" => $encryption_length, |
||
322 | "iv" => base64_encode($iv), |
||
323 | "target_file" => $dest, |
||
324 | "finished" => 0 |
||
325 | ); |
||
326 | } |
||
327 | } |
||
328 | |||
329 | } |
||
330 | } else { |
||
331 | if (is_object($this->logger)) { |
||
332 | $this->logger->error('Unable to read source file for decryption'); |
||
333 | } |
||
334 | throw new \Exception("Unable to read source file for decryption"); |
||
335 | } |
||
336 | } else { |
||
337 | if (is_object($this->logger)) { |
||
338 | $this->logger->error('Unable to write destination file for decryption'); |
||
339 | } |
||
340 | throw new \Exception("Unable to write destination file for decryption"); |
||
341 | } |
||
342 | |||
343 | //we replace the original backup with the encrypted one |
||
344 | if (!$keep_local && !$this->verification && copy($this->get_xcloner_path().$dest, |
||
345 | $this->get_xcloner_path().$source)) { |
||
346 | unlink($this->get_xcloner_path().$dest); |
||
347 | } |
||
348 | |||
349 | return array("target_file" => $dest, "finished" => 1); |
||
350 | } |
||
383 |