Conditions | 25 |
Paths | 2176 |
Total Lines | 118 |
Lines | 33 |
Ratio | 27.97 % |
Changes | 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 |
||
230 | public function decrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $recursive = false) |
||
231 | { |
||
232 | View Code Duplication | if(is_object($this->logger)) { |
|
233 | $this->logger->info(sprintf('Decrypting file %s at position %d with IV %s', $source, $start, base64_encode($iv))); |
||
234 | } |
||
235 | |||
236 | //$key = substr(sha1($key, true), 0, 16); |
||
237 | if(!$key){ |
||
238 | $key = self::get_backup_encryption_key(); |
||
239 | } |
||
240 | |||
241 | $key_digest = openssl_digest ($key, "md5", true); |
||
242 | |||
243 | $keep_local = 1; |
||
244 | if(!$dest) { |
||
245 | $dest = $this->get_decrypted_target_backup_file_name($source); |
||
246 | $keep_local = 0; |
||
247 | } |
||
248 | |||
249 | if( !$start ) { |
||
250 | View Code Duplication | if($this->verification){ |
|
251 | $fpOut = fopen("php://stdout", 'w'); |
||
252 | }else { |
||
253 | $fpOut = fopen($this->get_xcloner_path() . $dest, 'w'); |
||
254 | } |
||
255 | View Code Duplication | }else{ |
|
256 | if($this->verification){ |
||
257 | $fpOut = fopen("php://stdout", 'a'); |
||
258 | }else { |
||
259 | $fpOut = fopen($this->get_xcloner_path() . $dest, 'a'); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | if ( $fpOut ) { |
||
264 | if ( file_exists($this->get_xcloner_path() .$source) && |
||
265 | $fpIn = fopen($this->get_xcloner_path() .$source, 'rb')) { |
||
266 | |||
267 | $encryption_length = (int)fread($fpIn, 16); |
||
268 | if(!$encryption_length) { |
||
269 | $encryption_length = self::FILE_ENCRYPTION_BLOCKS; |
||
270 | } |
||
271 | |||
272 | fseek($fpIn, (int)$start); |
||
273 | |||
274 | // Get the initialzation vector from the beginning of the file |
||
275 | if(!$iv) { |
||
276 | $iv = fread($fpIn, 16); |
||
277 | } |
||
278 | |||
279 | if (!feof($fpIn)) { |
||
280 | |||
281 | // we have to read one block more for decrypting than for encrypting |
||
282 | $ciphertext = fread($fpIn, 16 * ($encryption_length + 1)); |
||
283 | $plaintext = openssl_decrypt($ciphertext, 'AES-128-CBC', $key_digest, OPENSSL_RAW_DATA, $iv); |
||
284 | |||
285 | if(!$plaintext){ |
||
286 | unlink($this->get_xcloner_path() . $dest); |
||
287 | if(is_object($this->logger)) { |
||
288 | $this->logger->error('Backup decryption failed, please check your provided Encryption Key.'); |
||
289 | } |
||
290 | throw new \Exception("Backup decryption failed, please check your provided Encryption Key."); |
||
291 | } |
||
292 | |||
293 | // Use the first 16 bytes of the ciphertext as the next initialization vector |
||
294 | $iv = substr($ciphertext, 0, 16); |
||
295 | |||
296 | if( !$this->verification) { |
||
297 | fwrite($fpOut, $plaintext); |
||
298 | } |
||
299 | |||
300 | $start = ftell($fpIn); |
||
301 | |||
302 | fclose($fpOut ); |
||
303 | |||
304 | if(!feof($fpIn)) { |
||
305 | fclose($fpIn); |
||
306 | if($this->verification || $recursive) { |
||
307 | $ciphertext = ""; |
||
308 | $plaintext = ""; |
||
309 | $this->decrypt_file($source, $dest, $key, $start, $iv, $recursive); |
||
310 | View Code Duplication | }else { |
|
311 | if(($iv) != base64_decode(base64_encode($iv))) |
||
312 | { |
||
313 | throw new \Exception('Could not encode IV for transport'); |
||
314 | } |
||
315 | |||
316 | return array( |
||
317 | "start" => $start, |
||
318 | "encryption_length" => $encryption_length, |
||
319 | "iv" => base64_encode($iv), |
||
320 | "target_file" => $dest, |
||
321 | "finished" => 0 |
||
322 | ); |
||
323 | } |
||
324 | } |
||
325 | |||
326 | } |
||
327 | } else { |
||
328 | if(is_object($this->logger)) { |
||
329 | $this->logger->error('Unable to read source file for decryption'); |
||
330 | } |
||
331 | throw new \Exception("Unable to read source file for decryption"); |
||
332 | } |
||
333 | } else { |
||
334 | if(is_object($this->logger)) { |
||
335 | $this->logger->error('Unable to write destination file for decryption'); |
||
336 | } |
||
337 | throw new \Exception("Unable to write destination file for decryption"); |
||
338 | } |
||
339 | |||
340 | //we replace the original backup with the encrypted one |
||
341 | View Code Duplication | if( !$keep_local && !$this->verification && copy($this->get_xcloner_path() .$dest, |
|
342 | $this->get_xcloner_path() .$source) ) { |
||
343 | unlink($this->get_xcloner_path() .$dest); |
||
344 | } |
||
345 | |||
346 | return array("target_file" => $dest, "finished" => 1); |
||
347 | } |
||
348 | |||
380 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: