@@ -106,11 +106,11 @@ discard block |
||
| 106 | 106 | private $backup_storage_dir; |
| 107 | 107 | private $parent_api; |
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Xcloner_Restore constructor. |
|
| 111 | - * @param string $parent_api |
|
| 112 | - * @throws Exception |
|
| 113 | - */ |
|
| 109 | + /** |
|
| 110 | + * Xcloner_Restore constructor. |
|
| 111 | + * @param string $parent_api |
|
| 112 | + * @throws Exception |
|
| 113 | + */ |
|
| 114 | 114 | public function __construct($parent_api = "") |
| 115 | 115 | { |
| 116 | 116 | register_shutdown_function(array($this, 'exception_handler')); |
@@ -150,9 +150,9 @@ discard block |
||
| 150 | 150 | |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Exception handler method |
|
| 155 | - */ |
|
| 153 | + /** |
|
| 154 | + * Exception handler method |
|
| 155 | + */ |
|
| 156 | 156 | public function exception_handler() { |
| 157 | 157 | |
| 158 | 158 | $error = error_get_last(); |
@@ -164,20 +164,20 @@ discard block |
||
| 164 | 164 | |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @param $type |
|
| 169 | - * @return mixed|string |
|
| 170 | - */ |
|
| 167 | + /** |
|
| 168 | + * @param $type |
|
| 169 | + * @return mixed|string |
|
| 170 | + */ |
|
| 171 | 171 | private function friendly_error_type($type) { |
| 172 | - static $levels=null; |
|
| 173 | - if ($levels===null) { |
|
| 174 | - $levels=[]; |
|
| 175 | - foreach (get_defined_constants() as $key=>$value) { |
|
| 176 | - if (strpos($key,'E_')!==0) {continue;} |
|
| 172 | + static $levels=null; |
|
| 173 | + if ($levels===null) { |
|
| 174 | + $levels=[]; |
|
| 175 | + foreach (get_defined_constants() as $key=>$value) { |
|
| 176 | + if (strpos($key,'E_')!==0) {continue;} |
|
| 177 | 177 | $levels[$value]= $key; //substr($key,2); |
| 178 | - } |
|
| 179 | - } |
|
| 180 | - return (isset($levels[$type]) ? $levels[$type] : "Error #{$type}"); |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + return (isset($levels[$type]) ? $levels[$type] : "Error #{$type}"); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | public function get_logger_filename() |
@@ -187,12 +187,12 @@ discard block |
||
| 187 | 187 | return $filename; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * Init method |
|
| 192 | - * |
|
| 193 | - * @return mixed|void |
|
| 194 | - * @throws Exception |
|
| 195 | - */ |
|
| 190 | + /** |
|
| 191 | + * Init method |
|
| 192 | + * |
|
| 193 | + * @return mixed|void |
|
| 194 | + * @throws Exception |
|
| 195 | + */ |
|
| 196 | 196 | public function init() |
| 197 | 197 | { |
| 198 | 198 | if(isset($_POST['xcloner_action']) and $_POST['xcloner_action']) |
@@ -216,12 +216,12 @@ discard block |
||
| 216 | 216 | return $this->check_system(); |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | - /** |
|
| 220 | - * Write file method |
|
| 221 | - * |
|
| 222 | - * @return bool|int |
|
| 223 | - * @throws Exception |
|
| 224 | - */ |
|
| 219 | + /** |
|
| 220 | + * Write file method |
|
| 221 | + * |
|
| 222 | + * @return bool|int |
|
| 223 | + * @throws Exception |
|
| 224 | + */ |
|
| 225 | 225 | public function write_file_action() |
| 226 | 226 | { |
| 227 | 227 | if(isset($_POST['file'])) |
@@ -248,10 +248,10 @@ discard block |
||
| 248 | 248 | throw new Exception("Unable to write data to file $target_file"); |
| 249 | 249 | |
| 250 | 250 | try { |
| 251 | - unlink($_FILES['blob']['tmp_name']); |
|
| 252 | - }catch(Exception $e){ |
|
| 251 | + unlink($_FILES['blob']['tmp_name']); |
|
| 252 | + }catch(Exception $e){ |
|
| 253 | 253 | |
| 254 | - } |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | 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'])); |
@@ -271,16 +271,16 @@ discard block |
||
| 271 | 271 | |
| 272 | 272 | } |
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Connect to mysql server method |
|
| 276 | - * |
|
| 277 | - * @param $remote_mysql_host |
|
| 278 | - * @param $remote_mysql_user |
|
| 279 | - * @param $remote_mysql_pass |
|
| 280 | - * @param $remote_mysql_db |
|
| 281 | - * @return mysqli |
|
| 282 | - * @throws Exception |
|
| 283 | - */ |
|
| 274 | + /** |
|
| 275 | + * Connect to mysql server method |
|
| 276 | + * |
|
| 277 | + * @param $remote_mysql_host |
|
| 278 | + * @param $remote_mysql_user |
|
| 279 | + * @param $remote_mysql_pass |
|
| 280 | + * @param $remote_mysql_db |
|
| 281 | + * @return mysqli |
|
| 282 | + * @throws Exception |
|
| 283 | + */ |
|
| 284 | 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)); |
@@ -302,11 +302,11 @@ discard block |
||
| 302 | 302 | return $mysqli; |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - /** |
|
| 306 | - * Restore mysql backup file |
|
| 307 | - * |
|
| 308 | - * @throws Exception |
|
| 309 | - */ |
|
| 305 | + /** |
|
| 306 | + * Restore mysql backup file |
|
| 307 | + * |
|
| 308 | + * @throws Exception |
|
| 309 | + */ |
|
| 310 | 310 | public function restore_mysql_backup_action() |
| 311 | 311 | { |
| 312 | 312 | $mysqldump_file = filter_input(INPUT_POST, 'mysqldump_file', FILTER_SANITIZE_STRING); |
@@ -437,14 +437,14 @@ discard block |
||
| 437 | 437 | $this->send_response(200, $return); |
| 438 | 438 | } |
| 439 | 439 | |
| 440 | - /** |
|
| 441 | - * Url replace method inside database backup file |
|
| 442 | - * |
|
| 443 | - * @param $search |
|
| 444 | - * @param $replace |
|
| 445 | - * @param $query |
|
| 446 | - * @return mixed|string|string[]|null |
|
| 447 | - */ |
|
| 440 | + /** |
|
| 441 | + * Url replace method inside database backup file |
|
| 442 | + * |
|
| 443 | + * @param $search |
|
| 444 | + * @param $replace |
|
| 445 | + * @param $query |
|
| 446 | + * @return mixed|string|string[]|null |
|
| 447 | + */ |
|
| 448 | 448 | private function url_replace($search, $replace, $query) |
| 449 | 449 | { |
| 450 | 450 | $this->logger->info(sprintf("Doing url replace on query with length %s", strlen($query)), array("QUERY_REPLACE")); |
@@ -467,11 +467,11 @@ discard block |
||
| 467 | 467 | return $query; |
| 468 | 468 | } |
| 469 | 469 | |
| 470 | - /** |
|
| 471 | - * List backup files method |
|
| 472 | - * |
|
| 473 | - * @throws \League\Flysystem\FileNotFoundException |
|
| 474 | - */ |
|
| 470 | + /** |
|
| 471 | + * List backup files method |
|
| 472 | + * |
|
| 473 | + * @throws \League\Flysystem\FileNotFoundException |
|
| 474 | + */ |
|
| 475 | 475 | public function list_backup_files_action() |
| 476 | 476 | { |
| 477 | 477 | $backup_parts = array(); |
@@ -537,11 +537,11 @@ discard block |
||
| 537 | 537 | $this->send_response(200, $return); |
| 538 | 538 | } |
| 539 | 539 | |
| 540 | - /** |
|
| 541 | - * Finish backup restore method |
|
| 542 | - * |
|
| 543 | - * @throws \League\Flysystem\FileNotFoundException |
|
| 544 | - */ |
|
| 540 | + /** |
|
| 541 | + * Finish backup restore method |
|
| 542 | + * |
|
| 543 | + * @throws \League\Flysystem\FileNotFoundException |
|
| 544 | + */ |
|
| 545 | 545 | public function restore_finish_action() |
| 546 | 546 | { |
| 547 | 547 | $remote_path = filter_input(INPUT_POST, 'remote_path', FILTER_SANITIZE_STRING); |
@@ -582,12 +582,12 @@ discard block |
||
| 582 | 582 | $this->send_response(200, $return); |
| 583 | 583 | } |
| 584 | 584 | |
| 585 | - /** |
|
| 586 | - * Delete backup temporary folder |
|
| 587 | - * |
|
| 588 | - * @param $remote_path |
|
| 589 | - * @return bool |
|
| 590 | - */ |
|
| 585 | + /** |
|
| 586 | + * Delete backup temporary folder |
|
| 587 | + * |
|
| 588 | + * @param $remote_path |
|
| 589 | + * @return bool |
|
| 590 | + */ |
|
| 591 | 591 | private function delete_backup_temporary_folder($remote_path) |
| 592 | 592 | { |
| 593 | 593 | $this->target_adapter = new Local($remote_path ,LOCK_EX, 'SKIP_LINKS'); |
@@ -615,11 +615,11 @@ discard block |
||
| 615 | 615 | |
| 616 | 616 | } |
| 617 | 617 | |
| 618 | - /** |
|
| 619 | - * Delete restore script method |
|
| 620 | - * |
|
| 621 | - * @throws \League\Flysystem\FileNotFoundException |
|
| 622 | - */ |
|
| 618 | + /** |
|
| 619 | + * Delete restore script method |
|
| 620 | + * |
|
| 621 | + * @throws \League\Flysystem\FileNotFoundException |
|
| 622 | + */ |
|
| 623 | 623 | private function delete_self() |
| 624 | 624 | { |
| 625 | 625 | if($this->filesystem->has("vendor.phar")) |
@@ -652,14 +652,14 @@ discard block |
||
| 652 | 652 | |
| 653 | 653 | } |
| 654 | 654 | |
| 655 | - /** |
|
| 656 | - * Update Wordpress url in wp-config.php method |
|
| 657 | - * @param $wp_path |
|
| 658 | - * @param $url |
|
| 659 | - * @param $mysqli |
|
| 660 | - * @return bool |
|
| 661 | - * @throws Exception |
|
| 662 | - */ |
|
| 655 | + /** |
|
| 656 | + * Update Wordpress url in wp-config.php method |
|
| 657 | + * @param $wp_path |
|
| 658 | + * @param $url |
|
| 659 | + * @param $mysqli |
|
| 660 | + * @return bool |
|
| 661 | + * @throws Exception |
|
| 662 | + */ |
|
| 663 | 663 | private function update_wp_url($wp_path, $url, $mysqli) |
| 664 | 664 | { |
| 665 | 665 | $wp_config = $wp_path.DS."wp-config.php"; |
@@ -687,17 +687,17 @@ discard block |
||
| 687 | 687 | return true; |
| 688 | 688 | } |
| 689 | 689 | |
| 690 | - /** |
|
| 691 | - * Update local wp-config.php file method |
|
| 692 | - * |
|
| 693 | - * @param $remote_path |
|
| 694 | - * @param $remote_mysql_host |
|
| 695 | - * @param $remote_mysql_user |
|
| 696 | - * @param $remote_mysql_pass |
|
| 697 | - * @param $remote_mysql_db |
|
| 698 | - * @return string |
|
| 699 | - * @throws Exception |
|
| 700 | - */ |
|
| 690 | + /** |
|
| 691 | + * Update local wp-config.php file method |
|
| 692 | + * |
|
| 693 | + * @param $remote_path |
|
| 694 | + * @param $remote_mysql_host |
|
| 695 | + * @param $remote_mysql_user |
|
| 696 | + * @param $remote_mysql_pass |
|
| 697 | + * @param $remote_mysql_db |
|
| 698 | + * @return string |
|
| 699 | + * @throws Exception |
|
| 700 | + */ |
|
| 701 | 701 | private function update_wp_config($remote_path, $remote_mysql_host, $remote_mysql_user, $remote_mysql_pass, $remote_mysql_db) |
| 702 | 702 | { |
| 703 | 703 | $wp_config = $remote_path.DS."wp-config.php"; |
@@ -729,10 +729,10 @@ discard block |
||
| 729 | 729 | |
| 730 | 730 | } |
| 731 | 731 | |
| 732 | - /** |
|
| 733 | - * List mysqldump database backup files |
|
| 734 | - * |
|
| 735 | - */ |
|
| 732 | + /** |
|
| 733 | + * List mysqldump database backup files |
|
| 734 | + * |
|
| 735 | + */ |
|
| 736 | 736 | public function list_mysqldump_backups_action() |
| 737 | 737 | { |
| 738 | 738 | $source_backup_file = filter_input(INPUT_POST, 'backup_file', FILTER_SANITIZE_STRING); |
@@ -782,12 +782,12 @@ discard block |
||
| 782 | 782 | $this->send_response(200, $return); |
| 783 | 783 | } |
| 784 | 784 | |
| 785 | - /** |
|
| 786 | - * Get backup hash method |
|
| 787 | - * |
|
| 788 | - * @param $backup_file |
|
| 789 | - * @return bool |
|
| 790 | - */ |
|
| 785 | + /** |
|
| 786 | + * Get backup hash method |
|
| 787 | + * |
|
| 788 | + * @param $backup_file |
|
| 789 | + * @return bool |
|
| 790 | + */ |
|
| 791 | 791 | private function get_hash_from_backup($backup_file) |
| 792 | 792 | { |
| 793 | 793 | if(!$backup_file) |
@@ -801,11 +801,11 @@ discard block |
||
| 801 | 801 | return false; |
| 802 | 802 | } |
| 803 | 803 | |
| 804 | - /** |
|
| 805 | - * List backup archives found on local system |
|
| 806 | - * |
|
| 807 | - * @throws \League\Flysystem\FileNotFoundException |
|
| 808 | - */ |
|
| 804 | + /** |
|
| 805 | + * List backup archives found on local system |
|
| 806 | + * |
|
| 807 | + * @throws \League\Flysystem\FileNotFoundException |
|
| 808 | + */ |
|
| 809 | 809 | public function list_backup_archives_action() |
| 810 | 810 | { |
| 811 | 811 | $local_backup_file = filter_input(INPUT_POST, 'local_backup_file', FILTER_SANITIZE_STRING); |
@@ -864,12 +864,12 @@ discard block |
||
| 864 | 864 | |
| 865 | 865 | } |
| 866 | 866 | |
| 867 | - /** |
|
| 868 | - * Restore backup archive to local path |
|
| 869 | - * |
|
| 870 | - * @throws \League\Flysystem\FileNotFoundException |
|
| 871 | - * @throws \splitbrain\PHPArchive\ArchiveIOException |
|
| 872 | - */ |
|
| 867 | + /** |
|
| 868 | + * Restore backup archive to local path |
|
| 869 | + * |
|
| 870 | + * @throws \League\Flysystem\FileNotFoundException |
|
| 871 | + * @throws \splitbrain\PHPArchive\ArchiveIOException |
|
| 872 | + */ |
|
| 873 | 873 | public function restore_backup_to_path_action() |
| 874 | 874 | { |
| 875 | 875 | $source_backup_file = filter_input(INPUT_POST, 'backup_file', FILTER_SANITIZE_STRING); |
@@ -944,9 +944,9 @@ discard block |
||
| 944 | 944 | $this->send_response(200, $return); |
| 945 | 945 | } |
| 946 | 946 | |
| 947 | - /** |
|
| 948 | - * Get current directory method |
|
| 949 | - */ |
|
| 947 | + /** |
|
| 948 | + * Get current directory method |
|
| 949 | + */ |
|
| 950 | 950 | public function get_current_directory_action() |
| 951 | 951 | { |
| 952 | 952 | global $wpdb; |
@@ -980,11 +980,11 @@ discard block |
||
| 980 | 980 | $this->send_response(200, $return); |
| 981 | 981 | } |
| 982 | 982 | |
| 983 | - /** |
|
| 984 | - * Check current filesystem |
|
| 985 | - * |
|
| 986 | - * @throws Exception |
|
| 987 | - */ |
|
| 983 | + /** |
|
| 984 | + * Check current filesystem |
|
| 985 | + * |
|
| 986 | + * @throws Exception |
|
| 987 | + */ |
|
| 988 | 988 | public function check_system() |
| 989 | 989 | { |
| 990 | 990 | //check if i can write |
@@ -1006,35 +1006,35 @@ discard block |
||
| 1006 | 1006 | $this->send_response(200, $return); |
| 1007 | 1007 | } |
| 1008 | 1008 | |
| 1009 | - /** |
|
| 1010 | - * Return bytes from human readable value |
|
| 1011 | - * |
|
| 1012 | - * @param $val |
|
| 1013 | - * @return int |
|
| 1014 | - * |
|
| 1015 | - */ |
|
| 1016 | - private function return_bytes($val) { |
|
| 1017 | - $numeric_val = (int)trim($val); |
|
| 1018 | - $last = strtolower($val[strlen($val)-1]); |
|
| 1019 | - switch($last) { |
|
| 1020 | - // The 'G' modifier is available since PHP 5.1.0 |
|
| 1021 | - case 'g': |
|
| 1022 | - $numeric_val *= 1024; |
|
| 1023 | - case 'm': |
|
| 1024 | - $numeric_val *= 1024; |
|
| 1025 | - case 'k': |
|
| 1026 | - $numeric_val *= 1024; |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - return $numeric_val; |
|
| 1030 | - } |
|
| 1031 | - |
|
| 1032 | - /** |
|
| 1033 | - * Check if backup archive os multipart |
|
| 1034 | - * |
|
| 1035 | - * @param $backup_name |
|
| 1036 | - * @return bool |
|
| 1037 | - */ |
|
| 1009 | + /** |
|
| 1010 | + * Return bytes from human readable value |
|
| 1011 | + * |
|
| 1012 | + * @param $val |
|
| 1013 | + * @return int |
|
| 1014 | + * |
|
| 1015 | + */ |
|
| 1016 | + private function return_bytes($val) { |
|
| 1017 | + $numeric_val = (int)trim($val); |
|
| 1018 | + $last = strtolower($val[strlen($val)-1]); |
|
| 1019 | + switch($last) { |
|
| 1020 | + // The 'G' modifier is available since PHP 5.1.0 |
|
| 1021 | + case 'g': |
|
| 1022 | + $numeric_val *= 1024; |
|
| 1023 | + case 'm': |
|
| 1024 | + $numeric_val *= 1024; |
|
| 1025 | + case 'k': |
|
| 1026 | + $numeric_val *= 1024; |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + return $numeric_val; |
|
| 1030 | + } |
|
| 1031 | + |
|
| 1032 | + /** |
|
| 1033 | + * Check if backup archive os multipart |
|
| 1034 | + * |
|
| 1035 | + * @param $backup_name |
|
| 1036 | + * @return bool |
|
| 1037 | + */ |
|
| 1038 | 1038 | public function is_multipart($backup_name) |
| 1039 | 1039 | { |
| 1040 | 1040 | if(stristr($backup_name, "-multipart")) |
@@ -1043,13 +1043,13 @@ discard block |
||
| 1043 | 1043 | return false; |
| 1044 | 1044 | } |
| 1045 | 1045 | |
| 1046 | - /** |
|
| 1047 | - * Get backup archive size |
|
| 1048 | - * |
|
| 1049 | - * @param $backup_name |
|
| 1050 | - * @return bool|false|int |
|
| 1051 | - * @throws \League\Flysystem\FileNotFoundException |
|
| 1052 | - */ |
|
| 1046 | + /** |
|
| 1047 | + * Get backup archive size |
|
| 1048 | + * |
|
| 1049 | + * @param $backup_name |
|
| 1050 | + * @return bool|false|int |
|
| 1051 | + * @throws \League\Flysystem\FileNotFoundException |
|
| 1052 | + */ |
|
| 1053 | 1053 | public function get_backup_size($backup_name) |
| 1054 | 1054 | { |
| 1055 | 1055 | $backup_size = $this->filesystem->getSize($backup_name); |
@@ -1063,12 +1063,12 @@ discard block |
||
| 1063 | 1063 | return $backup_size; |
| 1064 | 1064 | } |
| 1065 | 1065 | |
| 1066 | - /** |
|
| 1067 | - * Get multipart backup files list |
|
| 1068 | - * @param $backup_name |
|
| 1069 | - * @return array |
|
| 1070 | - * @throws \League\Flysystem\FileNotFoundException |
|
| 1071 | - */ |
|
| 1066 | + /** |
|
| 1067 | + * Get multipart backup files list |
|
| 1068 | + * @param $backup_name |
|
| 1069 | + * @return array |
|
| 1070 | + * @throws \League\Flysystem\FileNotFoundException |
|
| 1071 | + */ |
|
| 1072 | 1072 | public function get_multipart_files($backup_name) |
| 1073 | 1073 | { |
| 1074 | 1074 | $files = array(); |
@@ -1089,58 +1089,58 @@ discard block |
||
| 1089 | 1089 | return $files; |
| 1090 | 1090 | } |
| 1091 | 1091 | |
| 1092 | - /** |
|
| 1093 | - * Sort_by method |
|
| 1094 | - * |
|
| 1095 | - * @param $array |
|
| 1096 | - * @param $field |
|
| 1097 | - * @param string $direction |
|
| 1098 | - * @return bool |
|
| 1099 | - */ |
|
| 1100 | - private function sort_by( &$array, $field, $direction = 'asc') |
|
| 1101 | - { |
|
| 1102 | - $direction = strtolower($direction); |
|
| 1103 | - |
|
| 1104 | - usort($array, |
|
| 1105 | - function($a, $b) use($field, $direction){ |
|
| 1106 | - |
|
| 1107 | - $a = $a[$field]; |
|
| 1108 | - $b = $b[$field]; |
|
| 1109 | - |
|
| 1110 | - if ($a == $b) |
|
| 1111 | - { |
|
| 1112 | - return 0; |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - if($direction == 'desc') { |
|
| 1116 | - if($a > $b) { |
|
| 1117 | - return -1; |
|
| 1118 | - } |
|
| 1119 | - else{ |
|
| 1120 | - return 1; |
|
| 1121 | - } |
|
| 1122 | - }else { |
|
| 1123 | - if($a < $b) { |
|
| 1124 | - return -1; |
|
| 1125 | - } |
|
| 1126 | - else{ |
|
| 1127 | - return 1; |
|
| 1128 | - } |
|
| 1129 | - } |
|
| 1130 | - |
|
| 1131 | - //return ($a.($direction == 'desc' ? '>' : '<').$b) ? -1 : 1; |
|
| 1132 | - } |
|
| 1133 | - ); |
|
| 1134 | - |
|
| 1135 | - return true; |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - /** |
|
| 1139 | - * Send response method |
|
| 1140 | - * |
|
| 1141 | - * @param int $status |
|
| 1142 | - * @param $response |
|
| 1143 | - */ |
|
| 1092 | + /** |
|
| 1093 | + * Sort_by method |
|
| 1094 | + * |
|
| 1095 | + * @param $array |
|
| 1096 | + * @param $field |
|
| 1097 | + * @param string $direction |
|
| 1098 | + * @return bool |
|
| 1099 | + */ |
|
| 1100 | + private function sort_by( &$array, $field, $direction = 'asc') |
|
| 1101 | + { |
|
| 1102 | + $direction = strtolower($direction); |
|
| 1103 | + |
|
| 1104 | + usort($array, |
|
| 1105 | + function($a, $b) use($field, $direction){ |
|
| 1106 | + |
|
| 1107 | + $a = $a[$field]; |
|
| 1108 | + $b = $b[$field]; |
|
| 1109 | + |
|
| 1110 | + if ($a == $b) |
|
| 1111 | + { |
|
| 1112 | + return 0; |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + if($direction == 'desc') { |
|
| 1116 | + if($a > $b) { |
|
| 1117 | + return -1; |
|
| 1118 | + } |
|
| 1119 | + else{ |
|
| 1120 | + return 1; |
|
| 1121 | + } |
|
| 1122 | + }else { |
|
| 1123 | + if($a < $b) { |
|
| 1124 | + return -1; |
|
| 1125 | + } |
|
| 1126 | + else{ |
|
| 1127 | + return 1; |
|
| 1128 | + } |
|
| 1129 | + } |
|
| 1130 | + |
|
| 1131 | + //return ($a.($direction == 'desc' ? '>' : '<').$b) ? -1 : 1; |
|
| 1132 | + } |
|
| 1133 | + ); |
|
| 1134 | + |
|
| 1135 | + return true; |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + /** |
|
| 1139 | + * Send response method |
|
| 1140 | + * |
|
| 1141 | + * @param int $status |
|
| 1142 | + * @param $response |
|
| 1143 | + */ |
|
| 1144 | 1144 | public static function send_response($status = 200, $response) |
| 1145 | 1145 | { |
| 1146 | 1146 | header("Access-Control-Allow-Origin: *"); |
@@ -1163,12 +1163,12 @@ discard block |
||
| 1163 | 1163 | exit; |
| 1164 | 1164 | } |
| 1165 | 1165 | |
| 1166 | - /** |
|
| 1167 | - * Serialize fix methods below for mysql query lines |
|
| 1168 | - * |
|
| 1169 | - * @param $query |
|
| 1170 | - * @return string|string[]|null |
|
| 1171 | - */ |
|
| 1166 | + /** |
|
| 1167 | + * Serialize fix methods below for mysql query lines |
|
| 1168 | + * |
|
| 1169 | + * @param $query |
|
| 1170 | + * @return string|string[]|null |
|
| 1171 | + */ |
|
| 1172 | 1172 | |
| 1173 | 1173 | function do_serialized_fix($query) |
| 1174 | 1174 | { |
@@ -1181,51 +1181,51 @@ discard block |
||
| 1181 | 1181 | $m[3] = ""; |
| 1182 | 1182 | |
| 1183 | 1183 | $data = 's:'.strlen(($m[3])).':\"'.($m[3]).'\";'; |
| 1184 | - //return $this->unescape_quotes($data); |
|
| 1184 | + //return $this->unescape_quotes($data); |
|
| 1185 | 1185 | |
| 1186 | - return $data; |
|
| 1187 | - }, $query); |
|
| 1186 | + return $data; |
|
| 1187 | + }, $query); |
|
| 1188 | 1188 | } |
| 1189 | 1189 | |
| 1190 | - /** |
|
| 1191 | - * Unescape quotes method |
|
| 1192 | - * |
|
| 1193 | - * @param $value |
|
| 1194 | - * @return mixed |
|
| 1195 | - */ |
|
| 1190 | + /** |
|
| 1191 | + * Unescape quotes method |
|
| 1192 | + * |
|
| 1193 | + * @param $value |
|
| 1194 | + * @return mixed |
|
| 1195 | + */ |
|
| 1196 | 1196 | private function unescape_quotes($value) { |
| 1197 | 1197 | return str_replace('\"', '"', $value); |
| 1198 | 1198 | } |
| 1199 | 1199 | |
| 1200 | - /** |
|
| 1201 | - * Unescape mysql method |
|
| 1202 | - * |
|
| 1203 | - * @param $value |
|
| 1204 | - * @return mixed |
|
| 1205 | - */ |
|
| 1200 | + /** |
|
| 1201 | + * Unescape mysql method |
|
| 1202 | + * |
|
| 1203 | + * @param $value |
|
| 1204 | + * @return mixed |
|
| 1205 | + */ |
|
| 1206 | 1206 | private function unescape_mysql($value) { |
| 1207 | 1207 | return str_replace(array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), |
| 1208 | 1208 | array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), |
| 1209 | 1209 | $value); |
| 1210 | 1210 | } |
| 1211 | 1211 | |
| 1212 | - /** |
|
| 1213 | - * Check if string is in serialized format |
|
| 1214 | - * |
|
| 1215 | - * @param $s |
|
| 1216 | - * @return bool |
|
| 1217 | - */ |
|
| 1212 | + /** |
|
| 1213 | + * Check if string is in serialized format |
|
| 1214 | + * |
|
| 1215 | + * @param $s |
|
| 1216 | + * @return bool |
|
| 1217 | + */ |
|
| 1218 | 1218 | private function has_serialized($s) |
| 1219 | 1219 | { |
| 1220 | 1220 | if( |
| 1221 | - stristr($s, '{' ) !== false && |
|
| 1222 | - stristr($s, '}' ) !== false && |
|
| 1223 | - stristr($s, ';' ) !== false && |
|
| 1224 | - stristr($s, ':' ) !== false |
|
| 1225 | - ){ |
|
| 1226 | - return true; |
|
| 1221 | + stristr($s, '{' ) !== false && |
|
| 1222 | + stristr($s, '}' ) !== false && |
|
| 1223 | + stristr($s, ';' ) !== false && |
|
| 1224 | + stristr($s, ':' ) !== false |
|
| 1225 | + ){ |
|
| 1226 | + return true; |
|
| 1227 | 1227 | }else{ |
| 1228 | - return false; |
|
| 1228 | + return false; |
|
| 1229 | 1229 | } |
| 1230 | 1230 | |
| 1231 | 1231 | } |
@@ -10,370 +10,370 @@ |
||
| 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 $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 $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 string $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 | - public function verify_encrypted_file($file) { |
|
| 210 | - if(is_object($this->logger)) { |
|
| 211 | - $this->logger->info(sprintf('Verifying encrypted file %s', $file)); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $this->verification = true; |
|
| 215 | - $this->decrypt_file($file); |
|
| 216 | - $this->verification = false; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Dencrypt the passed file and saves the result in a new file, removing the |
|
| 221 | - * last 4 characters from file name. |
|
| 222 | - * |
|
| 223 | - * @param string $source Path to file that should be decrypted |
|
| 224 | - * @param string $dest File name where the decryped file should be written to. |
|
| 225 | - * @param string $key The key used for the decryption (must be the same as for encryption) |
|
| 226 | - * @param int $start Start position for reading when doing incremental mode. |
|
| 227 | - * @param string $iv The IV key to use. |
|
| 228 | - * @return string|false Returns the file name that has been created or FALSE if an error occured |
|
| 229 | - */ |
|
| 230 | - public function decrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $recursive = false) |
|
| 231 | - { |
|
| 232 | - 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 | - if($this->verification){ |
|
| 251 | - $fpOut = fopen("php://stdout", 'w'); |
|
| 252 | - }else { |
|
| 253 | - $fpOut = fopen($this->get_xcloner_path() . $dest, 'w'); |
|
| 254 | - } |
|
| 255 | - }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 | - }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 | - 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 | - |
|
| 349 | - public function get_xcloner_path(){ |
|
| 350 | - if(is_object($this->xcloner_settings)) { |
|
| 351 | - return $this->xcloner_settings->get_xcloner_store_path() . DS; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - return null; |
|
| 355 | - } |
|
| 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 $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 $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 string $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 | + public function verify_encrypted_file($file) { |
|
| 210 | + if(is_object($this->logger)) { |
|
| 211 | + $this->logger->info(sprintf('Verifying encrypted file %s', $file)); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $this->verification = true; |
|
| 215 | + $this->decrypt_file($file); |
|
| 216 | + $this->verification = false; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Dencrypt the passed file and saves the result in a new file, removing the |
|
| 221 | + * last 4 characters from file name. |
|
| 222 | + * |
|
| 223 | + * @param string $source Path to file that should be decrypted |
|
| 224 | + * @param string $dest File name where the decryped file should be written to. |
|
| 225 | + * @param string $key The key used for the decryption (must be the same as for encryption) |
|
| 226 | + * @param int $start Start position for reading when doing incremental mode. |
|
| 227 | + * @param string $iv The IV key to use. |
|
| 228 | + * @return string|false Returns the file name that has been created or FALSE if an error occured |
|
| 229 | + */ |
|
| 230 | + public function decrypt_file($source, $dest = "", $key = "", $start = 0, $iv = 0, $recursive = false) |
|
| 231 | + { |
|
| 232 | + 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 | + if($this->verification){ |
|
| 251 | + $fpOut = fopen("php://stdout", 'w'); |
|
| 252 | + }else { |
|
| 253 | + $fpOut = fopen($this->get_xcloner_path() . $dest, 'w'); |
|
| 254 | + } |
|
| 255 | + }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 | + }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 | + 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 | + |
|
| 349 | + public function get_xcloner_path(){ |
|
| 350 | + if(is_object($this->xcloner_settings)) { |
|
| 351 | + return $this->xcloner_settings->get_xcloner_store_path() . DS; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + return null; |
|
| 355 | + } |
|
| 356 | 356 | |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | |
| 360 | 360 | try { |
| 361 | 361 | |
| 362 | - if(isset($argv[1])) { |
|
| 363 | - |
|
| 364 | - class Xcloner { |
|
| 365 | - public function __construct() |
|
| 366 | - { |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - $xcloner_encryption = new Xcloner_Encryption( new Xcloner() ); |
|
| 370 | - |
|
| 371 | - if ($argv[1] == "-e") { |
|
| 372 | - $xcloner_encryption->encrypt_file($argv[2], $argv[2] . ".enc", $argv[4], '', '', '', true); |
|
| 373 | - } elseif ($argv[1] == "-d") { |
|
| 374 | - $xcloner_encryption->decrypt_file($argv[2], $argv[2] . ".dec", $argv[4], '', '', true); |
|
| 375 | - } |
|
| 376 | - } |
|
| 362 | + if(isset($argv[1])) { |
|
| 363 | + |
|
| 364 | + class Xcloner { |
|
| 365 | + public function __construct() |
|
| 366 | + { |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + $xcloner_encryption = new Xcloner_Encryption( new Xcloner() ); |
|
| 370 | + |
|
| 371 | + if ($argv[1] == "-e") { |
|
| 372 | + $xcloner_encryption->encrypt_file($argv[2], $argv[2] . ".enc", $argv[4], '', '', '', true); |
|
| 373 | + } elseif ($argv[1] == "-d") { |
|
| 374 | + $xcloner_encryption->decrypt_file($argv[2], $argv[2] . ".dec", $argv[4], '', '', true); |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | 377 | }catch(\Exception $e) { |
| 378 | - echo "CAUGHT: " . $e->getMessage(); |
|
| 378 | + echo "CAUGHT: " . $e->getMessage(); |
|
| 379 | 379 | } |