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