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