Conditions | 25 |
Paths | 2176 |
Total Lines | 117 |
Code Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 4 | 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 |
||
245 | public function decrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $recursive = false) |
||
246 | { |
||
247 | if (is_object($this->logger)) { |
||
248 | $this->logger->info(sprintf('Decrypting file %s at position %d with IV %s', $source, $start, base64_encode($iv))); |
||
249 | } |
||
250 | |||
251 | //$key = substr(sha1($key, true), 0, 16); |
||
252 | if (!$key) { |
||
253 | $key = $this->get_backup_encryption_key(); |
||
254 | } |
||
255 | |||
256 | $key_digest = openssl_digest($key, "md5", true); |
||
257 | |||
258 | $keep_local = 1; |
||
259 | if (!$dest) { |
||
260 | $dest = $this->get_decrypted_target_backup_file_name($source); |
||
261 | $keep_local = 0; |
||
262 | } |
||
263 | |||
264 | if (!$start) { |
||
265 | if ($this->verification) { |
||
266 | $fpOut = fopen("php://stdout", 'w'); |
||
267 | } else { |
||
268 | $fpOut = fopen($this->get_xcloner_path().$dest, 'w'); |
||
269 | } |
||
270 | } else { |
||
271 | if ($this->verification) { |
||
272 | $fpOut = fopen("php://stdout", 'a'); |
||
273 | } else { |
||
274 | $fpOut = fopen($this->get_xcloner_path().$dest, 'a'); |
||
275 | } |
||
276 | } |
||
277 | |||
278 | if (is_resource($fpOut)) { |
||
279 | if (file_exists($this->get_xcloner_path().$source) && |
||
280 | $fpIn = fopen($this->get_xcloner_path().$source, 'rb')) { |
||
281 | |||
282 | $encryption_length = (int)fread($fpIn, 16); |
||
283 | if (!$encryption_length) { |
||
284 | $encryption_length = self::FILE_ENCRYPTION_BLOCKS; |
||
285 | } |
||
286 | |||
287 | fseek($fpIn, (int)$start); |
||
288 | |||
289 | // Get the initialzation vector from the beginning of the file |
||
290 | if (!$iv) { |
||
291 | $iv = fread($fpIn, 16); |
||
292 | } |
||
293 | |||
294 | if (!feof($fpIn)) { |
||
295 | |||
296 | // we have to read one block more for decrypting than for encrypting |
||
297 | $ciphertext = fread($fpIn, 16 * ($encryption_length + 1)); |
||
298 | $plaintext = openssl_decrypt($ciphertext, 'AES-128-CBC', $key_digest, OPENSSL_RAW_DATA, $iv); |
||
299 | |||
300 | if (!$plaintext) { |
||
301 | unlink($this->get_xcloner_path().$dest); |
||
302 | if (is_object($this->logger)) { |
||
303 | $this->logger->error('Backup decryption failed, please check your provided Encryption Key.'); |
||
304 | } |
||
305 | throw new \Exception("Backup decryption failed, please check your provided Encryption Key."); |
||
306 | } |
||
307 | |||
308 | // Use the first 16 bytes of the ciphertext as the next initialization vector |
||
309 | $iv = substr($ciphertext, 0, 16); |
||
310 | |||
311 | if (!$this->verification) { |
||
312 | fwrite($fpOut, $plaintext); |
||
313 | } |
||
314 | |||
315 | $start = ftell($fpIn); |
||
316 | |||
317 | fclose($fpOut); |
||
318 | |||
319 | if (!feof($fpIn)) { |
||
320 | fclose($fpIn); |
||
321 | if ($this->verification || $recursive) { |
||
322 | unset($ciphertext); |
||
323 | unset($plaintext); |
||
324 | $this->decrypt_file($source, $dest, $key, $start, $iv, $recursive); |
||
325 | } else { |
||
326 | if (($iv) != base64_decode(base64_encode($iv))) |
||
327 | { |
||
328 | throw new \Exception('Could not encode IV for transport'); |
||
329 | } |
||
330 | |||
331 | return array( |
||
332 | "start" => $start, |
||
333 | "encryption_length" => $encryption_length, |
||
334 | "iv" => base64_encode($iv), |
||
335 | "target_file" => $dest, |
||
336 | "finished" => 0 |
||
337 | ); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | } |
||
342 | } else { |
||
343 | if (is_object($this->logger)) { |
||
344 | $this->logger->error('Unable to read source file for decryption'); |
||
345 | } |
||
346 | throw new \Exception("Unable to read source file for decryption"); |
||
347 | } |
||
348 | } else { |
||
349 | if (is_object($this->logger)) { |
||
350 | $this->logger->error('Unable to write destination file for decryption'); |
||
351 | } |
||
352 | throw new \Exception("Unable to write destination file for decryption"); |
||
353 | } |
||
354 | |||
355 | //we replace the original backup with the encrypted one |
||
356 | if (!$keep_local && !$this->verification && copy($this->get_xcloner_path().$dest, |
||
357 | $this->get_xcloner_path().$source)) { |
||
358 | unlink($this->get_xcloner_path().$dest); |
||
359 | } |
||
360 | |||
361 | return array("target_file" => $dest, "finished" => 1); |
||
362 | } |
||
398 |