@@ -656,7 +656,7 @@ discard block |
||
656 | 656 | * Update Wordpress url in wp-config.php method |
657 | 657 | * @param $wp_path |
658 | 658 | * @param $url |
659 | - * @param $mysqli |
|
659 | + * @param mysqli $mysqli |
|
660 | 660 | * @return bool |
661 | 661 | * @throws Exception |
662 | 662 | */ |
@@ -786,7 +786,7 @@ discard block |
||
786 | 786 | * Get backup hash method |
787 | 787 | * |
788 | 788 | * @param $backup_file |
789 | - * @return bool |
|
789 | + * @return false|string |
|
790 | 790 | */ |
791 | 791 | private function get_hash_from_backup($backup_file) |
792 | 792 | { |
@@ -1009,7 +1009,7 @@ discard block |
||
1009 | 1009 | /** |
1010 | 1010 | * Return bytes from human readable value |
1011 | 1011 | * |
1012 | - * @param $val |
|
1012 | + * @param string $val |
|
1013 | 1013 | * @return int |
1014 | 1014 | * |
1015 | 1015 | */ |
@@ -1093,7 +1093,7 @@ discard block |
||
1093 | 1093 | * Sort_by method |
1094 | 1094 | * |
1095 | 1095 | * @param $array |
1096 | - * @param $field |
|
1096 | + * @param string $field |
|
1097 | 1097 | * @param string $direction |
1098 | 1098 | * @return bool |
1099 | 1099 | */ |
@@ -1102,6 +1102,10 @@ discard block |
||
1102 | 1102 | $direction = strtolower($direction); |
1103 | 1103 | |
1104 | 1104 | usort($array, |
1105 | + |
|
1106 | + /** |
|
1107 | + * @param string $b |
|
1108 | + */ |
|
1105 | 1109 | function($a, $b) use($field, $direction){ |
1106 | 1110 | |
1107 | 1111 | $a = $a[$field]; |
@@ -1167,7 +1171,7 @@ discard block |
||
1167 | 1171 | * Serialize fix methods below for mysql query lines |
1168 | 1172 | * |
1169 | 1173 | * @param $query |
1170 | - * @return string|string[]|null |
|
1174 | + * @return string |
|
1171 | 1175 | */ |
1172 | 1176 | |
1173 | 1177 | function do_serialized_fix($query) |
@@ -169,12 +169,12 @@ discard block |
||
169 | 169 | * @return mixed|string |
170 | 170 | */ |
171 | 171 | private function friendly_error_type($type) { |
172 | - static $levels=null; |
|
173 | - if ($levels===null) { |
|
174 | - $levels=[]; |
|
172 | + static $levels = null; |
|
173 | + if ($levels === null) { |
|
174 | + $levels = []; |
|
175 | 175 | foreach (get_defined_constants() as $key=>$value) { |
176 | - if (strpos($key,'E_')!==0) {continue;} |
|
177 | - $levels[$value]= $key; //substr($key,2); |
|
176 | + if (strpos($key, 'E_') !== 0) {continue; } |
|
177 | + $levels[$value] = $key; //substr($key,2); |
|
178 | 178 | } |
179 | 179 | } |
180 | 180 | return (isset($levels[$type]) ? $levels[$type] : "Error #{$type}"); |
@@ -224,43 +224,43 @@ discard block |
||
224 | 224 | */ |
225 | 225 | public function write_file_action() |
226 | 226 | { |
227 | - if(isset($_POST['file'])) |
|
227 | + if (isset($_POST['file'])) |
|
228 | 228 | { |
229 | 229 | $target_file = filter_input(INPUT_POST, 'file', FILTER_SANITIZE_STRING); |
230 | 230 | |
231 | - if(!$_POST['start']) |
|
231 | + if (!$_POST['start']) |
|
232 | 232 | $fp = fopen($target_file, "wb+"); |
233 | 233 | else |
234 | 234 | $fp = fopen($target_file, "ab+"); |
235 | 235 | |
236 | - if(!$fp) |
|
236 | + if (!$fp) |
|
237 | 237 | throw new Exception("Unable to open $target_file file for writing"); |
238 | 238 | |
239 | 239 | fseek($fp, $_POST['start']); |
240 | 240 | |
241 | - if(isset($_FILES['blob'])) |
|
241 | + if (isset($_FILES['blob'])) |
|
242 | 242 | { |
243 | 243 | $this->logger->debug(sprintf('Writing %s bytes to file %s starting position %s using FILES blob', filesize($_FILES['blob']['tmp_name']), $target_file, $_POST['start'])); |
244 | 244 | |
245 | 245 | $blob = file_get_contents($_FILES['blob']['tmp_name']); |
246 | 246 | |
247 | - if(!$bytes_written = fwrite($fp, $blob)) |
|
247 | + if (!$bytes_written = fwrite($fp, $blob)) |
|
248 | 248 | throw new Exception("Unable to write data to file $target_file"); |
249 | 249 | |
250 | 250 | try { |
251 | 251 | unlink($_FILES['blob']['tmp_name']); |
252 | - }catch(Exception $e){ |
|
252 | + }catch (Exception $e) { |
|
253 | 253 | |
254 | 254 | } |
255 | 255 | |
256 | - }elseif(isset($_POST['blob'])){ |
|
256 | + }elseif (isset($_POST['blob'])) { |
|
257 | 257 | $this->logger->debug(sprintf('Writing %s bytes to file %s starting position %s using POST blob', strlen($_POST['blob']), $target_file, $_POST['start'])); |
258 | 258 | |
259 | 259 | $blob = $_POST['blob']; |
260 | 260 | |
261 | - if(!$bytes_written = fwrite($fp, $blob)) |
|
261 | + if (!$bytes_written = fwrite($fp, $blob)) |
|
262 | 262 | throw new Exception("Unable to write data to file $target_file"); |
263 | - }else{ |
|
263 | + } else { |
|
264 | 264 | throw new Exception("Upload failed, did not receive any binary data"); |
265 | 265 | } |
266 | 266 | |
@@ -281,20 +281,20 @@ discard block |
||
281 | 281 | * @return mysqli |
282 | 282 | * @throws Exception |
283 | 283 | */ |
284 | - public function mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db ) |
|
284 | + public function mysql_connect($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db) |
|
285 | 285 | { |
286 | 286 | $this->logger->info(sprintf('Connecting to mysql database %s with %s@%s', $remote_mysql_db, $remote_mysql_user, $remote_mysql_host)); |
287 | 287 | |
288 | 288 | $mysqli = new mysqli($remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db); |
289 | 289 | |
290 | 290 | if ($mysqli->connect_error) { |
291 | - throw new Exception('Connect Error (' . $mysqli->connect_errno . ') ' |
|
291 | + throw new Exception('Connect Error ('.$mysqli->connect_errno.') ' |
|
292 | 292 | . $mysqli->connect_error); |
293 | 293 | } |
294 | 294 | |
295 | 295 | $mysqli->query("SET sql_mode='';"); |
296 | 296 | $mysqli->query("SET foreign_key_checks = 0;"); |
297 | - if(isset($_REQUEST['charset_of_file']) and $_REQUEST['charset_of_file']) |
|
297 | + if (isset($_REQUEST['charset_of_file']) and $_REQUEST['charset_of_file']) |
|
298 | 298 | $mysqli->query("SET NAMES ".$_REQUEST['charset_of_file'].""); |
299 | 299 | else |
300 | 300 | $mysqli->query("SET NAMES utf8;"); |
@@ -1015,8 +1015,8 @@ discard block |
||
1015 | 1015 | */ |
1016 | 1016 | private function return_bytes($val) { |
1017 | 1017 | $numeric_val = (int)trim($val); |
1018 | - $last = strtolower($val[strlen($val)-1]); |
|
1019 | - switch($last) { |
|
1018 | + $last = strtolower($val[strlen($val) - 1]); |
|
1019 | + switch ($last) { |
|
1020 | 1020 | // The 'G' modifier is available since PHP 5.1.0 |
1021 | 1021 | case 'g': |
1022 | 1022 | $numeric_val *= 1024; |
@@ -1097,7 +1097,7 @@ discard block |
||
1097 | 1097 | * @param string $direction |
1098 | 1098 | * @return bool |
1099 | 1099 | */ |
1100 | - private function sort_by( &$array, $field, $direction = 'asc') |
|
1100 | + private function sort_by(&$array, $field, $direction = 'asc') |
|
1101 | 1101 | { |
1102 | 1102 | $direction = strtolower($direction); |
1103 | 1103 | |
@@ -1112,18 +1112,18 @@ discard block |
||
1112 | 1112 | return 0; |
1113 | 1113 | } |
1114 | 1114 | |
1115 | - if($direction == 'desc') { |
|
1116 | - if($a > $b) { |
|
1115 | + if ($direction == 'desc') { |
|
1116 | + if ($a > $b) { |
|
1117 | 1117 | return -1; |
1118 | 1118 | } |
1119 | - else{ |
|
1119 | + else { |
|
1120 | 1120 | return 1; |
1121 | 1121 | } |
1122 | - }else { |
|
1123 | - if($a < $b) { |
|
1122 | + } else { |
|
1123 | + if ($a < $b) { |
|
1124 | 1124 | return -1; |
1125 | 1125 | } |
1126 | - else{ |
|
1126 | + else { |
|
1127 | 1127 | return 1; |
1128 | 1128 | } |
1129 | 1129 | } |
@@ -1217,14 +1217,14 @@ discard block |
||
1217 | 1217 | */ |
1218 | 1218 | private function has_serialized($s) |
1219 | 1219 | { |
1220 | - if( |
|
1221 | - stristr($s, '{' ) !== false && |
|
1222 | - stristr($s, '}' ) !== false && |
|
1223 | - stristr($s, ';' ) !== false && |
|
1224 | - stristr($s, ':' ) !== false |
|
1225 | - ){ |
|
1220 | + if ( |
|
1221 | + stristr($s, '{') !== false && |
|
1222 | + stristr($s, '}') !== false && |
|
1223 | + stristr($s, ';') !== false && |
|
1224 | + stristr($s, ':') !== false |
|
1225 | + ) { |
|
1226 | 1226 | return true; |
1227 | - }else{ |
|
1227 | + } else { |
|
1228 | 1228 | return false; |
1229 | 1229 | } |
1230 | 1230 |
@@ -45,8 +45,7 @@ discard block |
||
45 | 45 | { |
46 | 46 | |
47 | 47 | require_once($file); |
48 | -} |
|
49 | -elseif (file_exists("vendor.phar") and extension_loaded('phar')) |
|
48 | +} elseif (file_exists("vendor.phar") and extension_loaded('phar')) |
|
50 | 49 | { |
51 | 50 | require_once(__DIR__.DS."vendor.phar"); |
52 | 51 | } else { |
@@ -87,7 +86,7 @@ discard block |
||
87 | 86 | try { |
88 | 87 | $return = $xcloner_restore->init(); |
89 | 88 | $xcloner_restore->send_response(200, $return); |
90 | -}catch (Exception $e) { |
|
89 | +} catch (Exception $e) { |
|
91 | 90 | $xcloner_restore->send_response(417, $e->getMessage()); |
92 | 91 | } |
93 | 92 | |
@@ -228,13 +227,15 @@ discard block |
||
228 | 227 | { |
229 | 228 | $target_file = filter_input(INPUT_POST, 'file', FILTER_SANITIZE_STRING); |
230 | 229 | |
231 | - if(!$_POST['start']) |
|
232 | - $fp = fopen($target_file, "wb+"); |
|
233 | - else |
|
234 | - $fp = fopen($target_file, "ab+"); |
|
230 | + if(!$_POST['start']) { |
|
231 | + $fp = fopen($target_file, "wb+"); |
|
232 | + } else { |
|
233 | + $fp = fopen($target_file, "ab+"); |
|
234 | + } |
|
235 | 235 | |
236 | - if(!$fp) |
|
237 | - throw new Exception("Unable to open $target_file file for writing"); |
|
236 | + if(!$fp) { |
|
237 | + throw new Exception("Unable to open $target_file file for writing"); |
|
238 | + } |
|
238 | 239 | |
239 | 240 | fseek($fp, $_POST['start']); |
240 | 241 | |
@@ -244,23 +245,25 @@ discard block |
||
244 | 245 | |
245 | 246 | $blob = file_get_contents($_FILES['blob']['tmp_name']); |
246 | 247 | |
247 | - if(!$bytes_written = fwrite($fp, $blob)) |
|
248 | - throw new Exception("Unable to write data to file $target_file"); |
|
248 | + if(!$bytes_written = fwrite($fp, $blob)) { |
|
249 | + throw new Exception("Unable to write data to file $target_file"); |
|
250 | + } |
|
249 | 251 | |
250 | 252 | try { |
251 | 253 | unlink($_FILES['blob']['tmp_name']); |
252 | - }catch(Exception $e){ |
|
254 | + } catch(Exception $e){ |
|
253 | 255 | |
254 | 256 | } |
255 | 257 | |
256 | - }elseif(isset($_POST['blob'])){ |
|
258 | + } elseif(isset($_POST['blob'])){ |
|
257 | 259 | $this->logger->debug(sprintf('Writing %s bytes to file %s starting position %s using POST blob', strlen($_POST['blob']), $target_file, $_POST['start'])); |
258 | 260 | |
259 | 261 | $blob = $_POST['blob']; |
260 | 262 | |
261 | - if(!$bytes_written = fwrite($fp, $blob)) |
|
262 | - throw new Exception("Unable to write data to file $target_file"); |
|
263 | - }else{ |
|
263 | + if(!$bytes_written = fwrite($fp, $blob)) { |
|
264 | + throw new Exception("Unable to write data to file $target_file"); |
|
265 | + } |
|
266 | + } else{ |
|
264 | 267 | throw new Exception("Upload failed, did not receive any binary data"); |
265 | 268 | } |
266 | 269 | |
@@ -294,10 +297,11 @@ discard block |
||
294 | 297 | |
295 | 298 | $mysqli->query("SET sql_mode='';"); |
296 | 299 | $mysqli->query("SET foreign_key_checks = 0;"); |
297 | - if(isset($_REQUEST['charset_of_file']) and $_REQUEST['charset_of_file']) |
|
298 | - $mysqli->query("SET NAMES ".$_REQUEST['charset_of_file'].""); |
|
299 | - else |
|
300 | - $mysqli->query("SET NAMES utf8;"); |
|
300 | + if(isset($_REQUEST['charset_of_file']) and $_REQUEST['charset_of_file']) { |
|
301 | + $mysqli->query("SET NAMES ".$_REQUEST['charset_of_file'].""); |
|
302 | + } else { |
|
303 | + $mysqli->query("SET NAMES utf8;"); |
|
304 | + } |
|
301 | 305 | |
302 | 306 | return $mysqli; |
303 | 307 | } |
@@ -327,8 +331,9 @@ discard block |
||
327 | 331 | |
328 | 332 | $mysql_backup_file = $remote_path.DS.$mysqldump_file; |
329 | 333 | |
330 | - if (!file_exists($mysql_backup_file)) |
|
331 | - throw new Exception(sprintf("Mysql backup file %s does not exists", $mysql_backup_file)); |
|
334 | + if (!file_exists($mysql_backup_file)) { |
|
335 | + throw new Exception(sprintf("Mysql backup file %s does not exists", $mysql_backup_file)); |
|
336 | + } |
|
332 | 337 | |
333 | 338 | |
334 | 339 | /*if(defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS) |
@@ -360,16 +365,18 @@ discard block |
||
360 | 365 | // process the line read. |
361 | 366 | |
362 | 367 | //check if line is comment |
363 | - if (substr($line, 0, 1) == "#") |
|
364 | - continue; |
|
368 | + if (substr($line, 0, 1) == "#") { |
|
369 | + continue; |
|
370 | + } |
|
365 | 371 | |
366 | 372 | //check if line is empty |
367 | - if ($line == "\n" or trim($line) == "") |
|
368 | - continue; |
|
373 | + if ($line == "\n" or trim($line) == "") { |
|
374 | + continue; |
|
375 | + } |
|
369 | 376 | |
370 | - if (substr($line, strlen($line) - 2, strlen($line)) == ";\n") |
|
371 | - $query .= $line; |
|
372 | - else { |
|
377 | + if (substr($line, strlen($line) - 2, strlen($line)) == ";\n") { |
|
378 | + $query .= $line; |
|
379 | + } else { |
|
373 | 380 | $query .= $line; |
374 | 381 | continue; |
375 | 382 | } |
@@ -493,7 +500,7 @@ discard block |
||
493 | 500 | $tar->open($this->backup_storage_dir.DS.$backup_file, $start); |
494 | 501 | |
495 | 502 | $data = $tar->contents($this->process_files_limit_list); |
496 | - }catch (Exception $e) |
|
503 | + } catch (Exception $e) |
|
497 | 504 | { |
498 | 505 | $return['error'] = true; |
499 | 506 | $return['message'] = $e->getMessage(); |
@@ -528,8 +535,9 @@ discard block |
||
528 | 535 | |
529 | 536 | ++$return['part']; |
530 | 537 | |
531 | - if ($return['part'] < sizeof($backup_parts)) |
|
532 | - $return['finished'] = 0; |
|
538 | + if ($return['part'] < sizeof($backup_parts)) { |
|
539 | + $return['finished'] = 0; |
|
540 | + } |
|
533 | 541 | |
534 | 542 | } |
535 | 543 | } |
@@ -670,19 +678,22 @@ discard block |
||
670 | 678 | { |
671 | 679 | $config = file_get_contents($wp_config); |
672 | 680 | preg_match("/.*table_prefix.*=.*'(.*)'/i", $config, $matches); |
673 | - if (isset($matches[1])) |
|
674 | - $table_prefix = $matches[1]; |
|
675 | - else |
|
676 | - throw new Exception("Could not load wordpress table prefix from wp-config.php file."); |
|
681 | + if (isset($matches[1])) { |
|
682 | + $table_prefix = $matches[1]; |
|
683 | + } else { |
|
684 | + throw new Exception("Could not load wordpress table prefix from wp-config.php file."); |
|
685 | + } |
|
686 | + } else { |
|
687 | + throw new Exception("Could not update the SITEURL and HOME, wp-config.php file not found"); |
|
677 | 688 | } |
678 | - else |
|
679 | - throw new Exception("Could not update the SITEURL and HOME, wp-config.php file not found"); |
|
680 | 689 | |
681 | - if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='home'")) |
|
682 | - throw new Exception(sprintf("Could not update the HOME option, error: %s\n", $mysqli->error)); |
|
690 | + if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='home'")) { |
|
691 | + throw new Exception(sprintf("Could not update the HOME option, error: %s\n", $mysqli->error)); |
|
692 | + } |
|
683 | 693 | |
684 | - if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='siteurl'")) |
|
685 | - throw new Exception(sprintf("Could not update the SITEURL option, error: %s\n", $mysqli->error)); |
|
694 | + if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='siteurl'")) { |
|
695 | + throw new Exception(sprintf("Could not update the SITEURL option, error: %s\n", $mysqli->error)); |
|
696 | + } |
|
686 | 697 | |
687 | 698 | return true; |
688 | 699 | } |
@@ -720,8 +731,9 @@ discard block |
||
720 | 731 | |
721 | 732 | $this->logger->info(sprintf('Updating wp-config.php file with the new mysql details')); |
722 | 733 | |
723 | - if (!file_put_contents($wp_config, $content)) |
|
724 | - throw new Exception("Could not write updated config data to ".$wp_config); |
|
734 | + if (!file_put_contents($wp_config, $content)) { |
|
735 | + throw new Exception("Could not write updated config data to ".$wp_config); |
|
736 | + } |
|
725 | 737 | |
726 | 738 | chmod($wp_config, $file_perms); |
727 | 739 | |
@@ -766,10 +778,11 @@ discard block |
||
766 | 778 | $mysqldump_list[$file['path']]['size'] = $file['size']; |
767 | 779 | $mysqldump_list[$file['path']]['timestamp'] = date("d M,Y H:i", $file['timestamp']); |
768 | 780 | |
769 | - if ($hash and $hash == $matches[1]) |
|
770 | - $mysqldump_list[$file['path']]['selected'] = "selected"; |
|
771 | - else |
|
772 | - $mysqldump_list[$file['path']]['selected'] = ""; |
|
781 | + if ($hash and $hash == $matches[1]) { |
|
782 | + $mysqldump_list[$file['path']]['selected'] = "selected"; |
|
783 | + } else { |
|
784 | + $mysqldump_list[$file['path']]['selected'] = ""; |
|
785 | + } |
|
773 | 786 | } |
774 | 787 | } |
775 | 788 | } |
@@ -790,13 +803,15 @@ discard block |
||
790 | 803 | */ |
791 | 804 | private function get_hash_from_backup($backup_file) |
792 | 805 | { |
793 | - if (!$backup_file) |
|
794 | - return false; |
|
806 | + if (!$backup_file) { |
|
807 | + return false; |
|
808 | + } |
|
795 | 809 | |
796 | 810 | $result = preg_match("/-(\w*)./", substr($backup_file, strlen($backup_file) - 10, strlen($backup_file)), $matches); |
797 | 811 | |
798 | - if ($result and isset($matches[1])) |
|
799 | - return ($matches[1]); |
|
812 | + if ($result and isset($matches[1])) { |
|
813 | + return ($matches[1]); |
|
814 | + } |
|
800 | 815 | |
801 | 816 | return false; |
802 | 817 | } |
@@ -821,10 +836,11 @@ discard block |
||
821 | 836 | if (isset($file_info['extension']) and $file_info['extension'] == "csv") |
822 | 837 | { |
823 | 838 | $lines = explode(PHP_EOL, $this->filesystem->read($file_info['path'])); |
824 | - foreach ($lines as $line) |
|
825 | - if ($line) |
|
839 | + foreach ($lines as $line) { |
|
840 | + if ($line) |
|
826 | 841 | { |
827 | 842 | $data = str_getcsv($line); |
843 | + } |
|
828 | 844 | if (is_array($data)) { |
829 | 845 | $parents[$data[0]] = $file_info['path']; |
830 | 846 | $file_info['childs'][] = $data; |
@@ -834,20 +850,22 @@ discard block |
||
834 | 850 | |
835 | 851 | } |
836 | 852 | |
837 | - if ($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'], $this->backup_archive_extensions)) |
|
838 | - $backup_files[$file_info['path']] = $file_info; |
|
853 | + if ($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'], $this->backup_archive_extensions)) { |
|
854 | + $backup_files[$file_info['path']] = $file_info; |
|
855 | + } |
|
839 | 856 | } |
840 | 857 | |
841 | 858 | $new_list = array(); |
842 | 859 | |
843 | 860 | foreach ($backup_files as $key=>$file_info) |
844 | 861 | { |
845 | - if (isset($parents[$file_info['path']])) |
|
846 | - $backup_files[$key]['parent'] = $parents[$file_info['path']]; |
|
847 | - else { |
|
862 | + if (isset($parents[$file_info['path']])) { |
|
863 | + $backup_files[$key]['parent'] = $parents[$file_info['path']]; |
|
864 | + } else { |
|
848 | 865 | |
849 | - if ($local_backup_file and ($file_info['basename'] == $local_backup_file)) |
|
850 | - $file_info['selected'] = 'selected'; |
|
866 | + if ($local_backup_file and ($file_info['basename'] == $local_backup_file)) { |
|
867 | + $file_info['selected'] = 'selected'; |
|
868 | + } |
|
851 | 869 | |
852 | 870 | $this->logger->info(sprintf('Found %s backup file', $file_info['path'])); |
853 | 871 | |
@@ -894,8 +912,9 @@ discard block |
||
894 | 912 | $backup_archive = new Tar(); |
895 | 913 | if ($this->is_multipart($backup_file)) |
896 | 914 | { |
897 | - if (!$return['part']) |
|
898 | - $return['processed'] += $this->filesystem->getSize($backup_file); |
|
915 | + if (!$return['part']) { |
|
916 | + $return['processed'] += $this->filesystem->getSize($backup_file); |
|
917 | + } |
|
899 | 918 | |
900 | 919 | $backup_parts = $this->get_multipart_files($backup_file); |
901 | 920 | $backup_file = $backup_parts[$return['part']]; |
@@ -915,10 +934,11 @@ discard block |
||
915 | 934 | } |
916 | 935 | } |
917 | 936 | |
918 | - if (isset($data['start'])) |
|
919 | - //if(isset($data['start']) and $data['start'] <= $this->filesystem->getSize($backup_file)) |
|
937 | + if (isset($data['start'])) { |
|
938 | + //if(isset($data['start']) and $data['start'] <= $this->filesystem->getSize($backup_file)) |
|
920 | 939 | { |
921 | 940 | $return['finished'] = 0; |
941 | + } |
|
922 | 942 | $return['start'] = $data['start']; |
923 | 943 | } else { |
924 | 944 | |
@@ -930,14 +950,16 @@ discard block |
||
930 | 950 | |
931 | 951 | ++$return['part']; |
932 | 952 | |
933 | - if ($return['part'] < sizeof($backup_parts)) |
|
934 | - $return['finished'] = 0; |
|
953 | + if ($return['part'] < sizeof($backup_parts)) { |
|
954 | + $return['finished'] = 0; |
|
955 | + } |
|
935 | 956 | |
936 | 957 | } |
937 | 958 | } |
938 | 959 | |
939 | - if ($return['finished']) |
|
940 | - $this->logger->info(sprintf('Done extracting %s', $source_backup_file)); |
|
960 | + if ($return['finished']) { |
|
961 | + $this->logger->info(sprintf('Done extracting %s', $source_backup_file)); |
|
962 | + } |
|
941 | 963 | |
942 | 964 | $return['backup_file'] = $backup_file; |
943 | 965 | |
@@ -969,8 +991,7 @@ discard block |
||
969 | 991 | $return['remote_mysql_user'] = $wpdb->dbuser; |
970 | 992 | $return['remote_mysql_pass'] = $wpdb->dbpassword; |
971 | 993 | $return['remote_mysql_db'] = $wpdb->dbname; |
972 | - } |
|
973 | - else { |
|
994 | + } else { |
|
974 | 995 | $return['dir'] = ($pathinfo['dirname']).DS.$suffix; |
975 | 996 | $return['restore_script_url'] = str_replace($pathinfo['basename'], "", $restore_script_url).$suffix; |
976 | 997 | } |
@@ -989,11 +1010,13 @@ discard block |
||
989 | 1010 | { |
990 | 1011 | //check if i can write |
991 | 1012 | $tmp_file = md5(time()); |
992 | - if (!file_put_contents($tmp_file, "++")) |
|
993 | - throw new Exception("Could not write to new host"); |
|
1013 | + if (!file_put_contents($tmp_file, "++")) { |
|
1014 | + throw new Exception("Could not write to new host"); |
|
1015 | + } |
|
994 | 1016 | |
995 | - if (!unlink($tmp_file)) |
|
996 | - throw new Exception("Could not delete temporary file from new host"); |
|
1017 | + if (!unlink($tmp_file)) { |
|
1018 | + throw new Exception("Could not delete temporary file from new host"); |
|
1019 | + } |
|
997 | 1020 | |
998 | 1021 | $max_upload = $this->return_bytes((ini_get('upload_max_filesize'))); |
999 | 1022 | $max_post = $this->return_bytes((ini_get('post_max_size'))); |
@@ -1037,8 +1060,9 @@ discard block |
||
1037 | 1060 | */ |
1038 | 1061 | public function is_multipart($backup_name) |
1039 | 1062 | { |
1040 | - if (stristr($backup_name, "-multipart")) |
|
1041 | - return true; |
|
1063 | + if (stristr($backup_name, "-multipart")) { |
|
1064 | + return true; |
|
1065 | + } |
|
1042 | 1066 | |
1043 | 1067 | return false; |
1044 | 1068 | } |
@@ -1056,8 +1080,9 @@ discard block |
||
1056 | 1080 | if ($this->is_multipart($backup_name)) |
1057 | 1081 | { |
1058 | 1082 | $backup_parts = $this->get_multipart_files($backup_name); |
1059 | - foreach ($backup_parts as $part_file) |
|
1060 | - $backup_size += $this->filesystem->getSize($part_file); |
|
1083 | + foreach ($backup_parts as $part_file) { |
|
1084 | + $backup_size += $this->filesystem->getSize($part_file); |
|
1085 | + } |
|
1061 | 1086 | } |
1062 | 1087 | |
1063 | 1088 | return $backup_size; |
@@ -1115,15 +1140,13 @@ discard block |
||
1115 | 1140 | if($direction == 'desc') { |
1116 | 1141 | if($a > $b) { |
1117 | 1142 | return -1; |
1118 | - } |
|
1119 | - else{ |
|
1143 | + } else{ |
|
1120 | 1144 | return 1; |
1121 | 1145 | } |
1122 | - }else { |
|
1146 | + } else { |
|
1123 | 1147 | if($a < $b) { |
1124 | 1148 | return -1; |
1125 | - } |
|
1126 | - else{ |
|
1149 | + } else{ |
|
1127 | 1150 | return 1; |
1128 | 1151 | } |
1129 | 1152 | } |
@@ -1153,7 +1176,7 @@ discard block |
||
1153 | 1176 | { |
1154 | 1177 | $return['statusText'] = $response['message']; |
1155 | 1178 | $return['error'] = true; |
1156 | - }elseif ($status != 200 and $status != 418) |
|
1179 | + } elseif ($status != 200 and $status != 418) |
|
1157 | 1180 | { |
1158 | 1181 | $return['error'] = true; |
1159 | 1182 | $return['message'] = $response; |
@@ -1177,8 +1200,9 @@ discard block |
||
1177 | 1200 | return preg_replace_callback('!s:(\d+):([\\\\]?"[\\\\]?"|[\\\\]?"((.*?)[^\\\\])[\\\\]?");!', function($m) { |
1178 | 1201 | $data = ""; |
1179 | 1202 | |
1180 | - if (!isset($m[3])) |
|
1181 | - $m[3] = ""; |
|
1203 | + if (!isset($m[3])) { |
|
1204 | + $m[3] = ""; |
|
1205 | + } |
|
1182 | 1206 | |
1183 | 1207 | $data = 's:'.strlen(($m[3])).':\"'.($m[3]).'\";'; |
1184 | 1208 | //return $this->unescape_quotes($data); |
@@ -1224,7 +1248,7 @@ discard block |
||
1224 | 1248 | stristr($s, ':' ) !== false |
1225 | 1249 | ){ |
1226 | 1250 | return true; |
1227 | - }else{ |
|
1251 | + } else{ |
|
1228 | 1252 | return false; |
1229 | 1253 | } |
1230 | 1254 |
@@ -10,373 +10,373 @@ |
||
10 | 10 | |
11 | 11 | class Xcloner_Encryption |
12 | 12 | { |
13 | - const FILE_ENCRYPTION_BLOCKS = 1024 * 1024; |
|
14 | - const FILE_ENCRYPTION_SUFFIX = ".encrypted"; |
|
15 | - const FILE_DECRYPTION_SUFFIX = ".decrypted"; |
|
16 | - |
|
17 | - private $xcloner_settings; |
|
18 | - private $logger; |
|
19 | - private $verification = false; |
|
20 | - |
|
21 | - public function __construct(Xcloner $xcloner_container) |
|
22 | - { |
|
23 | - $this->xcloner_container = $xcloner_container; |
|
24 | - if (method_exists($xcloner_container, 'get_xcloner_settings')) { |
|
25 | - $this->xcloner_settings = $xcloner_container->get_xcloner_settings(); |
|
26 | - } else { |
|
27 | - $this->xcloner_settings = ""; |
|
28 | - } |
|
29 | - |
|
30 | - if (method_exists($xcloner_container, 'get_xcloner_logger')) { |
|
31 | - $this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_encryption"); |
|
32 | - } else { |
|
33 | - $this->logger = ""; |
|
34 | - } |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Returns the backup encryption key |
|
39 | - * |
|
40 | - * @return SECURE_AUTH_SALT|null |
|
41 | - */ |
|
42 | - public function get_backup_encryption_key() |
|
43 | - { |
|
44 | - if (is_object($this->xcloner_settings)) { |
|
45 | - return $this->xcloner_settings->get_xcloner_encryption_key(); |
|
46 | - } |
|
47 | - |
|
48 | - return ""; |
|
49 | - |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Check if provided filename has encrypted suffix |
|
54 | - * |
|
55 | - * @param $filename |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - public function is_encrypted_file($filename) { |
|
59 | - $fp = fopen($this->get_xcloner_path().$filename, 'r'); |
|
60 | - $encryption_length = fread($fp, 16); |
|
61 | - fclose($fp); |
|
62 | - if (is_numeric($encryption_length)) { |
|
63 | - return true; |
|
64 | - } |
|
65 | - |
|
66 | - return false; |
|
67 | - |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Returns the filename with encrypted suffix |
|
72 | - * |
|
73 | - * @param string $filename |
|
74 | - * @return string |
|
75 | - */ |
|
76 | - public function get_encrypted_target_backup_file_name($filename) { |
|
77 | - |
|
78 | - return str_replace(self::FILE_DECRYPTION_SUFFIX, "", $filename).self::FILE_ENCRYPTION_SUFFIX; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Returns the filename without encrypted suffix |
|
83 | - * |
|
84 | - * @param string $filename |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - public function get_decrypted_target_backup_file_name($filename) { |
|
88 | - |
|
89 | - return str_replace(self::FILE_ENCRYPTION_SUFFIX, "", $filename).self::FILE_DECRYPTION_SUFFIX; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Encrypt the passed file and saves the result in a new file with ".enc" as suffix. |
|
94 | - * |
|
95 | - * @param string $source Path to file that should be encrypted |
|
96 | - * @param string $dest File name where the encryped file should be written to. |
|
97 | - * @param string $key The key used for the encryption |
|
98 | - * @param int $start Start position for reading when doing incremental mode. |
|
99 | - * @param integer $iv The IV key to use. |
|
100 | - * @param bool $verification Weather we should we try to verify the decryption. |
|
101 | - * @return string|false Returns the file name that has been created or FALSE if an error occured |
|
102 | - */ |
|
103 | - public function encrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $verification = true, $recursive = false) |
|
104 | - { |
|
105 | - if (is_object($this->logger)) { |
|
106 | - $this->logger->info(sprintf('Encrypting file %s at position %d IV %s', $source, $start, base64_encode($iv))); |
|
107 | - } |
|
108 | - |
|
109 | - //$key = substr(sha1($key, true), 0, 16); |
|
110 | - if (!$key) { |
|
111 | - $key = self::get_backup_encryption_key(); |
|
112 | - } |
|
113 | - $key_digest = openssl_digest($key, "md5", true); |
|
114 | - |
|
115 | - $keep_local = 1; |
|
116 | - if (!$dest) { |
|
117 | - $dest = $this->get_encrypted_target_backup_file_name($source); |
|
118 | - $keep_local = 0; |
|
119 | - } |
|
120 | - |
|
121 | - if (!$iv || !$start) { |
|
122 | - //$iv = openssl_random_pseudo_bytes(16); |
|
123 | - $iv = str_pad(self::FILE_ENCRYPTION_BLOCKS, 16, "0000000000000000", STR_PAD_LEFT); |
|
124 | - } |
|
125 | - |
|
126 | - if (!$start) { |
|
127 | - $fpOut = fopen($this->get_xcloner_path().$dest, 'w'); |
|
128 | - } else { |
|
129 | - $fpOut = fopen($this->get_xcloner_path().$dest, 'a'); |
|
130 | - } |
|
131 | - |
|
132 | - if ($fpOut) { |
|
133 | - |
|
134 | - // Put the initialization vector to the beginning of the file |
|
135 | - if (!$start) { |
|
136 | - fwrite($fpOut, $iv); |
|
137 | - } |
|
138 | - |
|
139 | - if (file_exists($this->get_xcloner_path().$source) && |
|
140 | - $fpIn = fopen($this->get_xcloner_path().$source, 'rb')) { |
|
141 | - |
|
142 | - fseek($fpIn, (int)$start); |
|
143 | - |
|
144 | - if (!feof($fpIn)) { |
|
145 | - |
|
146 | - $plaintext = fread($fpIn, 16 * self::FILE_ENCRYPTION_BLOCKS); |
|
147 | - $ciphertext = openssl_encrypt($plaintext, 'AES-128-CBC', $key_digest, OPENSSL_RAW_DATA, $iv); |
|
148 | - |
|
149 | - // Use the first 16 bytes of the ciphertext as the next initialization vector |
|
150 | - $iv = substr($ciphertext, 0, 16); |
|
151 | - //$iv = openssl_random_pseudo_bytes(16); |
|
152 | - |
|
153 | - fwrite($fpOut, $ciphertext); |
|
154 | - |
|
155 | - $start = ftell($fpIn); |
|
156 | - |
|
157 | - fclose($fpOut); |
|
158 | - |
|
159 | - if (!feof($fpIn)) { |
|
160 | - fclose($fpIn); |
|
161 | - //echo "\n NEW:".$key.md5($iv); |
|
162 | - //self::encryptFile($source, $dest, $key, $start, $iv); |
|
163 | - if ($recursive) { |
|
164 | - $this->encrypt_file($source, $dest, $key, $start, ($iv), $verification, $recursive); |
|
165 | - } else { |
|
166 | - |
|
167 | - if (($iv) != base64_decode(base64_encode($iv))) |
|
168 | - { |
|
169 | - throw new \Exception('Could not encode IV for transport'); |
|
170 | - } |
|
171 | - |
|
172 | - return array( |
|
173 | - "start" => $start, |
|
174 | - "iv" => base64_encode($iv), |
|
175 | - "target_file" => $dest, |
|
176 | - "finished" => 0 |
|
177 | - ); |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - } |
|
182 | - } else { |
|
183 | - if (is_object($this->logger)) { |
|
184 | - $this->logger->error('Unable to read source file for encryption.'); |
|
185 | - } |
|
186 | - throw new \Exception("Unable to read source file for encryption."); |
|
187 | - } |
|
188 | - } else { |
|
189 | - if (is_object($this->logger)) { |
|
190 | - $this->logger->error('Unable to write destination file for encryption.'); |
|
191 | - } |
|
192 | - throw new \Exception("Unable to write destination file for encryption."); |
|
193 | - } |
|
194 | - |
|
195 | - if ($verification) { |
|
196 | - $this->verify_encrypted_file($dest); |
|
197 | - } |
|
198 | - |
|
199 | - //we replace the original backup with the encrypted one |
|
200 | - if (!$keep_local && copy($this->get_xcloner_path().$dest, |
|
201 | - $this->get_xcloner_path().$source)) { |
|
202 | - unlink($this->get_xcloner_path().$dest); |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - return array("target_file" => $dest, "finished" => 1); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * @param string $file |
|
211 | - */ |
|
212 | - public function verify_encrypted_file($file) { |
|
213 | - if (is_object($this->logger)) { |
|
214 | - $this->logger->info(sprintf('Verifying encrypted file %s', $file)); |
|
215 | - } |
|
216 | - |
|
217 | - $this->verification = true; |
|
218 | - $this->decrypt_file($file); |
|
219 | - $this->verification = false; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Dencrypt the passed file and saves the result in a new file, removing the |
|
224 | - * last 4 characters from file name. |
|
225 | - * |
|
226 | - * @param string $source Path to file that should be decrypted |
|
227 | - * @param string $dest File name where the decryped file should be written to. |
|
228 | - * @param string $key The key used for the decryption (must be the same as for encryption) |
|
229 | - * @param int $start Start position for reading when doing incremental mode. |
|
230 | - * @param integer $iv The IV key to use. |
|
231 | - * @return string|false Returns the file name that has been created or FALSE if an error occured |
|
232 | - */ |
|
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 | - } |
|
351 | - |
|
352 | - public function get_xcloner_path() { |
|
353 | - if (is_object($this->xcloner_settings)) { |
|
354 | - return $this->xcloner_settings->get_xcloner_store_path().DS; |
|
355 | - } |
|
356 | - |
|
357 | - return null; |
|
358 | - } |
|
13 | + const FILE_ENCRYPTION_BLOCKS = 1024 * 1024; |
|
14 | + const FILE_ENCRYPTION_SUFFIX = ".encrypted"; |
|
15 | + const FILE_DECRYPTION_SUFFIX = ".decrypted"; |
|
16 | + |
|
17 | + private $xcloner_settings; |
|
18 | + private $logger; |
|
19 | + private $verification = false; |
|
20 | + |
|
21 | + public function __construct(Xcloner $xcloner_container) |
|
22 | + { |
|
23 | + $this->xcloner_container = $xcloner_container; |
|
24 | + if (method_exists($xcloner_container, 'get_xcloner_settings')) { |
|
25 | + $this->xcloner_settings = $xcloner_container->get_xcloner_settings(); |
|
26 | + } else { |
|
27 | + $this->xcloner_settings = ""; |
|
28 | + } |
|
29 | + |
|
30 | + if (method_exists($xcloner_container, 'get_xcloner_logger')) { |
|
31 | + $this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_encryption"); |
|
32 | + } else { |
|
33 | + $this->logger = ""; |
|
34 | + } |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Returns the backup encryption key |
|
39 | + * |
|
40 | + * @return SECURE_AUTH_SALT|null |
|
41 | + */ |
|
42 | + public function get_backup_encryption_key() |
|
43 | + { |
|
44 | + if (is_object($this->xcloner_settings)) { |
|
45 | + return $this->xcloner_settings->get_xcloner_encryption_key(); |
|
46 | + } |
|
47 | + |
|
48 | + return ""; |
|
49 | + |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Check if provided filename has encrypted suffix |
|
54 | + * |
|
55 | + * @param $filename |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + public function is_encrypted_file($filename) { |
|
59 | + $fp = fopen($this->get_xcloner_path().$filename, 'r'); |
|
60 | + $encryption_length = fread($fp, 16); |
|
61 | + fclose($fp); |
|
62 | + if (is_numeric($encryption_length)) { |
|
63 | + return true; |
|
64 | + } |
|
65 | + |
|
66 | + return false; |
|
67 | + |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Returns the filename with encrypted suffix |
|
72 | + * |
|
73 | + * @param string $filename |
|
74 | + * @return string |
|
75 | + */ |
|
76 | + public function get_encrypted_target_backup_file_name($filename) { |
|
77 | + |
|
78 | + return str_replace(self::FILE_DECRYPTION_SUFFIX, "", $filename).self::FILE_ENCRYPTION_SUFFIX; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Returns the filename without encrypted suffix |
|
83 | + * |
|
84 | + * @param string $filename |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + public function get_decrypted_target_backup_file_name($filename) { |
|
88 | + |
|
89 | + return str_replace(self::FILE_ENCRYPTION_SUFFIX, "", $filename).self::FILE_DECRYPTION_SUFFIX; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Encrypt the passed file and saves the result in a new file with ".enc" as suffix. |
|
94 | + * |
|
95 | + * @param string $source Path to file that should be encrypted |
|
96 | + * @param string $dest File name where the encryped file should be written to. |
|
97 | + * @param string $key The key used for the encryption |
|
98 | + * @param int $start Start position for reading when doing incremental mode. |
|
99 | + * @param integer $iv The IV key to use. |
|
100 | + * @param bool $verification Weather we should we try to verify the decryption. |
|
101 | + * @return string|false Returns the file name that has been created or FALSE if an error occured |
|
102 | + */ |
|
103 | + public function encrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $verification = true, $recursive = false) |
|
104 | + { |
|
105 | + if (is_object($this->logger)) { |
|
106 | + $this->logger->info(sprintf('Encrypting file %s at position %d IV %s', $source, $start, base64_encode($iv))); |
|
107 | + } |
|
108 | + |
|
109 | + //$key = substr(sha1($key, true), 0, 16); |
|
110 | + if (!$key) { |
|
111 | + $key = self::get_backup_encryption_key(); |
|
112 | + } |
|
113 | + $key_digest = openssl_digest($key, "md5", true); |
|
114 | + |
|
115 | + $keep_local = 1; |
|
116 | + if (!$dest) { |
|
117 | + $dest = $this->get_encrypted_target_backup_file_name($source); |
|
118 | + $keep_local = 0; |
|
119 | + } |
|
120 | + |
|
121 | + if (!$iv || !$start) { |
|
122 | + //$iv = openssl_random_pseudo_bytes(16); |
|
123 | + $iv = str_pad(self::FILE_ENCRYPTION_BLOCKS, 16, "0000000000000000", STR_PAD_LEFT); |
|
124 | + } |
|
125 | + |
|
126 | + if (!$start) { |
|
127 | + $fpOut = fopen($this->get_xcloner_path().$dest, 'w'); |
|
128 | + } else { |
|
129 | + $fpOut = fopen($this->get_xcloner_path().$dest, 'a'); |
|
130 | + } |
|
131 | + |
|
132 | + if ($fpOut) { |
|
133 | + |
|
134 | + // Put the initialization vector to the beginning of the file |
|
135 | + if (!$start) { |
|
136 | + fwrite($fpOut, $iv); |
|
137 | + } |
|
138 | + |
|
139 | + if (file_exists($this->get_xcloner_path().$source) && |
|
140 | + $fpIn = fopen($this->get_xcloner_path().$source, 'rb')) { |
|
141 | + |
|
142 | + fseek($fpIn, (int)$start); |
|
143 | + |
|
144 | + if (!feof($fpIn)) { |
|
145 | + |
|
146 | + $plaintext = fread($fpIn, 16 * self::FILE_ENCRYPTION_BLOCKS); |
|
147 | + $ciphertext = openssl_encrypt($plaintext, 'AES-128-CBC', $key_digest, OPENSSL_RAW_DATA, $iv); |
|
148 | + |
|
149 | + // Use the first 16 bytes of the ciphertext as the next initialization vector |
|
150 | + $iv = substr($ciphertext, 0, 16); |
|
151 | + //$iv = openssl_random_pseudo_bytes(16); |
|
152 | + |
|
153 | + fwrite($fpOut, $ciphertext); |
|
154 | + |
|
155 | + $start = ftell($fpIn); |
|
156 | + |
|
157 | + fclose($fpOut); |
|
158 | + |
|
159 | + if (!feof($fpIn)) { |
|
160 | + fclose($fpIn); |
|
161 | + //echo "\n NEW:".$key.md5($iv); |
|
162 | + //self::encryptFile($source, $dest, $key, $start, $iv); |
|
163 | + if ($recursive) { |
|
164 | + $this->encrypt_file($source, $dest, $key, $start, ($iv), $verification, $recursive); |
|
165 | + } else { |
|
166 | + |
|
167 | + if (($iv) != base64_decode(base64_encode($iv))) |
|
168 | + { |
|
169 | + throw new \Exception('Could not encode IV for transport'); |
|
170 | + } |
|
171 | + |
|
172 | + return array( |
|
173 | + "start" => $start, |
|
174 | + "iv" => base64_encode($iv), |
|
175 | + "target_file" => $dest, |
|
176 | + "finished" => 0 |
|
177 | + ); |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + } |
|
182 | + } else { |
|
183 | + if (is_object($this->logger)) { |
|
184 | + $this->logger->error('Unable to read source file for encryption.'); |
|
185 | + } |
|
186 | + throw new \Exception("Unable to read source file for encryption."); |
|
187 | + } |
|
188 | + } else { |
|
189 | + if (is_object($this->logger)) { |
|
190 | + $this->logger->error('Unable to write destination file for encryption.'); |
|
191 | + } |
|
192 | + throw new \Exception("Unable to write destination file for encryption."); |
|
193 | + } |
|
194 | + |
|
195 | + if ($verification) { |
|
196 | + $this->verify_encrypted_file($dest); |
|
197 | + } |
|
198 | + |
|
199 | + //we replace the original backup with the encrypted one |
|
200 | + if (!$keep_local && copy($this->get_xcloner_path().$dest, |
|
201 | + $this->get_xcloner_path().$source)) { |
|
202 | + unlink($this->get_xcloner_path().$dest); |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + return array("target_file" => $dest, "finished" => 1); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * @param string $file |
|
211 | + */ |
|
212 | + public function verify_encrypted_file($file) { |
|
213 | + if (is_object($this->logger)) { |
|
214 | + $this->logger->info(sprintf('Verifying encrypted file %s', $file)); |
|
215 | + } |
|
216 | + |
|
217 | + $this->verification = true; |
|
218 | + $this->decrypt_file($file); |
|
219 | + $this->verification = false; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Dencrypt the passed file and saves the result in a new file, removing the |
|
224 | + * last 4 characters from file name. |
|
225 | + * |
|
226 | + * @param string $source Path to file that should be decrypted |
|
227 | + * @param string $dest File name where the decryped file should be written to. |
|
228 | + * @param string $key The key used for the decryption (must be the same as for encryption) |
|
229 | + * @param int $start Start position for reading when doing incremental mode. |
|
230 | + * @param integer $iv The IV key to use. |
|
231 | + * @return string|false Returns the file name that has been created or FALSE if an error occured |
|
232 | + */ |
|
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 | + } |
|
351 | + |
|
352 | + public function get_xcloner_path() { |
|
353 | + if (is_object($this->xcloner_settings)) { |
|
354 | + return $this->xcloner_settings->get_xcloner_store_path().DS; |
|
355 | + } |
|
356 | + |
|
357 | + return null; |
|
358 | + } |
|
359 | 359 | |
360 | 360 | } |
361 | 361 | |
362 | 362 | |
363 | 363 | try { |
364 | 364 | |
365 | - if (isset($argv[1])) { |
|
366 | - |
|
367 | - class Xcloner { |
|
368 | - public function __construct() |
|
369 | - { |
|
370 | - } |
|
371 | - } |
|
372 | - $xcloner_encryption = new Xcloner_Encryption(new Xcloner()); |
|
373 | - |
|
374 | - if ($argv[1] == "-e") { |
|
375 | - $xcloner_encryption->encrypt_file($argv[2], $argv[2].".enc", $argv[4], '', '', '', true); |
|
376 | - } elseif ($argv[1] == "-d") { |
|
377 | - $xcloner_encryption->decrypt_file($argv[2], $argv[2].".dec", $argv[4], '', '', true); |
|
378 | - } |
|
379 | - } |
|
365 | + if (isset($argv[1])) { |
|
366 | + |
|
367 | + class Xcloner { |
|
368 | + public function __construct() |
|
369 | + { |
|
370 | + } |
|
371 | + } |
|
372 | + $xcloner_encryption = new Xcloner_Encryption(new Xcloner()); |
|
373 | + |
|
374 | + if ($argv[1] == "-e") { |
|
375 | + $xcloner_encryption->encrypt_file($argv[2], $argv[2].".enc", $argv[4], '', '', '', true); |
|
376 | + } elseif ($argv[1] == "-d") { |
|
377 | + $xcloner_encryption->decrypt_file($argv[2], $argv[2].".dec", $argv[4], '', '', true); |
|
378 | + } |
|
379 | + } |
|
380 | 380 | }catch (\Exception $e) { |
381 | - echo "CAUGHT: ".$e->getMessage(); |
|
381 | + echo "CAUGHT: ".$e->getMessage(); |
|
382 | 382 | } |
@@ -377,6 +377,6 @@ |
||
377 | 377 | $xcloner_encryption->decrypt_file($argv[2], $argv[2].".dec", $argv[4], '', '', true); |
378 | 378 | } |
379 | 379 | } |
380 | -}catch (\Exception $e) { |
|
380 | +} catch (\Exception $e) { |
|
381 | 381 | echo "CAUGHT: ".$e->getMessage(); |
382 | 382 | } |