@@ -1023,13 +1023,13 @@ |
||
1023 | 1023 | switch ($last) { |
1024 | 1024 | // The 'G' modifier is available since PHP 5.1.0 |
1025 | 1025 | case 'g': |
1026 | - //gigabytes |
|
1026 | + //gigabytes |
|
1027 | 1027 | $numeric_val *= 1024; |
1028 | 1028 | case 'm': |
1029 | - //megabytes |
|
1029 | + //megabytes |
|
1030 | 1030 | $numeric_val *= 1024; |
1031 | 1031 | case 'k': |
1032 | - //kilobytes |
|
1032 | + //kilobytes |
|
1033 | 1033 | $numeric_val *= 1024; |
1034 | 1034 | } |
1035 | 1035 |
@@ -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 | |
@@ -231,13 +230,15 @@ discard block |
||
231 | 230 | { |
232 | 231 | $target_file = filter_input(INPUT_POST, 'file', FILTER_SANITIZE_STRING); |
233 | 232 | |
234 | - if (!$_POST['start']) |
|
235 | - $fp = fopen($target_file, "wb+"); |
|
236 | - else |
|
237 | - $fp = fopen($target_file, "ab+"); |
|
233 | + if (!$_POST['start']) { |
|
234 | + $fp = fopen($target_file, "wb+"); |
|
235 | + } else { |
|
236 | + $fp = fopen($target_file, "ab+"); |
|
237 | + } |
|
238 | 238 | |
239 | - if (!$fp) |
|
240 | - throw new Exception("Unable to open $target_file file for writing"); |
|
239 | + if (!$fp) { |
|
240 | + throw new Exception("Unable to open $target_file file for writing"); |
|
241 | + } |
|
241 | 242 | |
242 | 243 | fseek($fp, $_POST['start']); |
243 | 244 | |
@@ -247,22 +248,24 @@ discard block |
||
247 | 248 | |
248 | 249 | $blob = file_get_contents($_FILES['blob']['tmp_name']); |
249 | 250 | |
250 | - if (!$bytes_written = fwrite($fp, $blob)) |
|
251 | - throw new Exception("Unable to write data to file $target_file"); |
|
251 | + if (!$bytes_written = fwrite($fp, $blob)) { |
|
252 | + throw new Exception("Unable to write data to file $target_file"); |
|
253 | + } |
|
252 | 254 | |
253 | 255 | try { |
254 | 256 | unlink($_FILES['blob']['tmp_name']); |
255 | - }catch (Exception $e) { |
|
257 | + } catch (Exception $e) { |
|
256 | 258 | //silent message |
257 | 259 | } |
258 | 260 | |
259 | - }elseif (isset($_POST['blob'])) { |
|
261 | + } elseif (isset($_POST['blob'])) { |
|
260 | 262 | $this->logger->debug(sprintf('Writing %s bytes to file %s starting position %s using POST blob', strlen($_POST['blob']), $target_file, $_POST['start'])); |
261 | 263 | |
262 | 264 | $blob = $_POST['blob']; |
263 | 265 | |
264 | - if (!$bytes_written = fwrite($fp, $blob)) |
|
265 | - throw new Exception("Unable to write data to file $target_file"); |
|
266 | + if (!$bytes_written = fwrite($fp, $blob)) { |
|
267 | + throw new Exception("Unable to write data to file $target_file"); |
|
268 | + } |
|
266 | 269 | } else { |
267 | 270 | throw new Exception("Upload failed, did not receive any binary data"); |
268 | 271 | } |
@@ -297,10 +300,11 @@ discard block |
||
297 | 300 | |
298 | 301 | $mysqli->query("SET sql_mode='';"); |
299 | 302 | $mysqli->query("SET foreign_key_checks = 0;"); |
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;"); |
|
303 | + if (isset($_REQUEST['charset_of_file']) and $_REQUEST['charset_of_file']) { |
|
304 | + $mysqli->query("SET NAMES ".$_REQUEST['charset_of_file'].""); |
|
305 | + } else { |
|
306 | + $mysqli->query("SET NAMES utf8;"); |
|
307 | + } |
|
304 | 308 | |
305 | 309 | return $mysqli; |
306 | 310 | } |
@@ -330,8 +334,9 @@ discard block |
||
330 | 334 | |
331 | 335 | $mysql_backup_file = $remote_path.DS.$mysqldump_file; |
332 | 336 | |
333 | - if (!file_exists($mysql_backup_file)) |
|
334 | - throw new Exception(sprintf("Mysql backup file %s does not exists", $mysql_backup_file)); |
|
337 | + if (!file_exists($mysql_backup_file)) { |
|
338 | + throw new Exception(sprintf("Mysql backup file %s does not exists", $mysql_backup_file)); |
|
339 | + } |
|
335 | 340 | |
336 | 341 | |
337 | 342 | /*if(defined('XCLONER_PLUGIN_ACCESS') && XCLONER_PLUGIN_ACCESS) |
@@ -364,16 +369,18 @@ discard block |
||
364 | 369 | // process the line read. |
365 | 370 | |
366 | 371 | //check if line is comment |
367 | - if (substr($line, 0, 1) == "#") |
|
368 | - continue; |
|
372 | + if (substr($line, 0, 1) == "#") { |
|
373 | + continue; |
|
374 | + } |
|
369 | 375 | |
370 | 376 | //check if line is empty |
371 | - if ($line == "\n" or trim($line) == "") |
|
372 | - continue; |
|
377 | + if ($line == "\n" or trim($line) == "") { |
|
378 | + continue; |
|
379 | + } |
|
373 | 380 | |
374 | - if (substr($line, strlen($line) - 2, strlen($line)) == ";\n") |
|
375 | - $query .= $line; |
|
376 | - else { |
|
381 | + if (substr($line, strlen($line) - 2, strlen($line)) == ";\n") { |
|
382 | + $query .= $line; |
|
383 | + } else { |
|
377 | 384 | $query .= $line; |
378 | 385 | continue; |
379 | 386 | } |
@@ -497,7 +504,7 @@ discard block |
||
497 | 504 | $tar->open($this->backup_storage_dir.DS.$backup_file, $start); |
498 | 505 | |
499 | 506 | $data = $tar->contents($this->process_files_limit_list); |
500 | - }catch (Exception $e) |
|
507 | + } catch (Exception $e) |
|
501 | 508 | { |
502 | 509 | $return['error'] = true; |
503 | 510 | $return['message'] = $e->getMessage(); |
@@ -532,8 +539,9 @@ discard block |
||
532 | 539 | |
533 | 540 | ++$return['part']; |
534 | 541 | |
535 | - if ($return['part'] < sizeof($backup_parts)) |
|
536 | - $return['finished'] = 0; |
|
542 | + if ($return['part'] < sizeof($backup_parts)) { |
|
543 | + $return['finished'] = 0; |
|
544 | + } |
|
537 | 545 | |
538 | 546 | } |
539 | 547 | } |
@@ -674,19 +682,22 @@ discard block |
||
674 | 682 | { |
675 | 683 | $config = file_get_contents($wp_config); |
676 | 684 | preg_match("/.*table_prefix.*=.*'(.*)'/i", $config, $matches); |
677 | - if (isset($matches[1])) |
|
678 | - $table_prefix = $matches[1]; |
|
679 | - else |
|
680 | - throw new Exception("Could not load wordpress table prefix from wp-config.php file."); |
|
685 | + if (isset($matches[1])) { |
|
686 | + $table_prefix = $matches[1]; |
|
687 | + } else { |
|
688 | + throw new Exception("Could not load wordpress table prefix from wp-config.php file."); |
|
689 | + } |
|
690 | + } else { |
|
691 | + throw new Exception("Could not update the SITEURL and HOME, wp-config.php file not found"); |
|
681 | 692 | } |
682 | - else |
|
683 | - throw new Exception("Could not update the SITEURL and HOME, wp-config.php file not found"); |
|
684 | 693 | |
685 | - if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='home'")) |
|
686 | - throw new Exception(sprintf("Could not update the HOME option, error: %s\n", $mysqli->error)); |
|
694 | + if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='home'")) { |
|
695 | + throw new Exception(sprintf("Could not update the HOME option, error: %s\n", $mysqli->error)); |
|
696 | + } |
|
687 | 697 | |
688 | - if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='siteurl'")) |
|
689 | - throw new Exception(sprintf("Could not update the SITEURL option, error: %s\n", $mysqli->error)); |
|
698 | + if (!$mysqli->query("update ".$table_prefix."options set option_value='".($url)."' where option_name='siteurl'")) { |
|
699 | + throw new Exception(sprintf("Could not update the SITEURL option, error: %s\n", $mysqli->error)); |
|
700 | + } |
|
690 | 701 | |
691 | 702 | return true; |
692 | 703 | } |
@@ -724,8 +735,9 @@ discard block |
||
724 | 735 | |
725 | 736 | $this->logger->info(sprintf('Updating wp-config.php file with the new mysql details')); |
726 | 737 | |
727 | - if (!file_put_contents($wp_config, $content)) |
|
728 | - throw new Exception("Could not write updated config data to ".$wp_config); |
|
738 | + if (!file_put_contents($wp_config, $content)) { |
|
739 | + throw new Exception("Could not write updated config data to ".$wp_config); |
|
740 | + } |
|
729 | 741 | |
730 | 742 | chmod($wp_config, $file_perms); |
731 | 743 | |
@@ -770,10 +782,11 @@ discard block |
||
770 | 782 | $mysqldump_list[$file['path']]['size'] = $file['size']; |
771 | 783 | $mysqldump_list[$file['path']]['timestamp'] = date("d M,Y H:i", $file['timestamp']); |
772 | 784 | |
773 | - if ($hash and $hash == $matches[1]) |
|
774 | - $mysqldump_list[$file['path']]['selected'] = "selected"; |
|
775 | - else |
|
776 | - $mysqldump_list[$file['path']]['selected'] = ""; |
|
785 | + if ($hash and $hash == $matches[1]) { |
|
786 | + $mysqldump_list[$file['path']]['selected'] = "selected"; |
|
787 | + } else { |
|
788 | + $mysqldump_list[$file['path']]['selected'] = ""; |
|
789 | + } |
|
777 | 790 | } |
778 | 791 | } |
779 | 792 | } |
@@ -794,13 +807,15 @@ discard block |
||
794 | 807 | */ |
795 | 808 | private function get_hash_from_backup($backup_file) |
796 | 809 | { |
797 | - if (!$backup_file) |
|
798 | - return false; |
|
810 | + if (!$backup_file) { |
|
811 | + return false; |
|
812 | + } |
|
799 | 813 | |
800 | 814 | $result = preg_match("/-(\w*)./", substr($backup_file, strlen($backup_file) - 10, strlen($backup_file)), $matches); |
801 | 815 | |
802 | - if ($result and isset($matches[1])) |
|
803 | - return ($matches[1]); |
|
816 | + if ($result and isset($matches[1])) { |
|
817 | + return ($matches[1]); |
|
818 | + } |
|
804 | 819 | |
805 | 820 | return false; |
806 | 821 | } |
@@ -825,10 +840,11 @@ discard block |
||
825 | 840 | if (isset($file_info['extension']) and $file_info['extension'] == "csv") |
826 | 841 | { |
827 | 842 | $lines = explode(PHP_EOL, $this->filesystem->read($file_info['path'])); |
828 | - foreach ($lines as $line) |
|
829 | - if ($line) |
|
843 | + foreach ($lines as $line) { |
|
844 | + if ($line) |
|
830 | 845 | { |
831 | 846 | $data = str_getcsv($line); |
847 | + } |
|
832 | 848 | if (is_array($data)) { |
833 | 849 | $parents[$data[0]] = $file_info['path']; |
834 | 850 | $file_info['childs'][] = $data; |
@@ -838,20 +854,22 @@ discard block |
||
838 | 854 | |
839 | 855 | } |
840 | 856 | |
841 | - if ($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'], $this->backup_archive_extensions)) |
|
842 | - $backup_files[$file_info['path']] = $file_info; |
|
857 | + if ($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'], $this->backup_archive_extensions)) { |
|
858 | + $backup_files[$file_info['path']] = $file_info; |
|
859 | + } |
|
843 | 860 | } |
844 | 861 | |
845 | 862 | $new_list = array(); |
846 | 863 | |
847 | 864 | foreach ($backup_files as $key=>$file_info) |
848 | 865 | { |
849 | - if (isset($parents[$file_info['path']])) |
|
850 | - $backup_files[$key]['parent'] = $parents[$file_info['path']]; |
|
851 | - else { |
|
866 | + if (isset($parents[$file_info['path']])) { |
|
867 | + $backup_files[$key]['parent'] = $parents[$file_info['path']]; |
|
868 | + } else { |
|
852 | 869 | |
853 | - if ($local_backup_file and ($file_info['basename'] == $local_backup_file)) |
|
854 | - $file_info['selected'] = 'selected'; |
|
870 | + if ($local_backup_file and ($file_info['basename'] == $local_backup_file)) { |
|
871 | + $file_info['selected'] = 'selected'; |
|
872 | + } |
|
855 | 873 | |
856 | 874 | $this->logger->info(sprintf('Found %s backup file', $file_info['path'])); |
857 | 875 | |
@@ -898,8 +916,9 @@ discard block |
||
898 | 916 | $backup_archive = new Tar(); |
899 | 917 | if ($this->is_multipart($backup_file)) |
900 | 918 | { |
901 | - if (!$return['part']) |
|
902 | - $return['processed'] += $this->filesystem->getSize($backup_file); |
|
919 | + if (!$return['part']) { |
|
920 | + $return['processed'] += $this->filesystem->getSize($backup_file); |
|
921 | + } |
|
903 | 922 | |
904 | 923 | $backup_parts = $this->get_multipart_files($backup_file); |
905 | 924 | $backup_file = $backup_parts[$return['part']]; |
@@ -919,10 +938,11 @@ discard block |
||
919 | 938 | } |
920 | 939 | } |
921 | 940 | |
922 | - if (isset($data['start'])) |
|
923 | - //if(isset($data['start']) and $data['start'] <= $this->filesystem->getSize($backup_file)) |
|
941 | + if (isset($data['start'])) { |
|
942 | + //if(isset($data['start']) and $data['start'] <= $this->filesystem->getSize($backup_file)) |
|
924 | 943 | { |
925 | 944 | $return['finished'] = 0; |
945 | + } |
|
926 | 946 | $return['start'] = $data['start']; |
927 | 947 | } else { |
928 | 948 | |
@@ -934,14 +954,16 @@ discard block |
||
934 | 954 | |
935 | 955 | ++$return['part']; |
936 | 956 | |
937 | - if ($return['part'] < sizeof($backup_parts)) |
|
938 | - $return['finished'] = 0; |
|
957 | + if ($return['part'] < sizeof($backup_parts)) { |
|
958 | + $return['finished'] = 0; |
|
959 | + } |
|
939 | 960 | |
940 | 961 | } |
941 | 962 | } |
942 | 963 | |
943 | - if ($return['finished']) |
|
944 | - $this->logger->info(sprintf('Done extracting %s', $source_backup_file)); |
|
964 | + if ($return['finished']) { |
|
965 | + $this->logger->info(sprintf('Done extracting %s', $source_backup_file)); |
|
966 | + } |
|
945 | 967 | |
946 | 968 | $return['backup_file'] = $backup_file; |
947 | 969 | |
@@ -973,8 +995,7 @@ discard block |
||
973 | 995 | $return['remote_mysql_user'] = $wpdb->dbuser; |
974 | 996 | $return['remote_mysql_pass'] = $wpdb->dbpassword; |
975 | 997 | $return['remote_mysql_db'] = $wpdb->dbname; |
976 | - } |
|
977 | - else { |
|
998 | + } else { |
|
978 | 999 | $return['dir'] = ($pathinfo['dirname']).DS.$suffix; |
979 | 1000 | $return['restore_script_url'] = str_replace($pathinfo['basename'], "", $restore_script_url).$suffix; |
980 | 1001 | } |
@@ -993,11 +1014,13 @@ discard block |
||
993 | 1014 | { |
994 | 1015 | //check if i can write |
995 | 1016 | $tmp_file = md5(time()); |
996 | - if (!file_put_contents($tmp_file, "++")) |
|
997 | - throw new Exception("Could not write to new host"); |
|
1017 | + if (!file_put_contents($tmp_file, "++")) { |
|
1018 | + throw new Exception("Could not write to new host"); |
|
1019 | + } |
|
998 | 1020 | |
999 | - if (!unlink($tmp_file)) |
|
1000 | - throw new Exception("Could not delete temporary file from new host"); |
|
1021 | + if (!unlink($tmp_file)) { |
|
1022 | + throw new Exception("Could not delete temporary file from new host"); |
|
1023 | + } |
|
1001 | 1024 | |
1002 | 1025 | $max_upload = $this->return_bytes((ini_get('upload_max_filesize'))); |
1003 | 1026 | $max_post = $this->return_bytes((ini_get('post_max_size'))); |
@@ -1044,8 +1067,9 @@ discard block |
||
1044 | 1067 | */ |
1045 | 1068 | public function is_multipart($backup_name) |
1046 | 1069 | { |
1047 | - if (stristr($backup_name, "-multipart")) |
|
1048 | - return true; |
|
1070 | + if (stristr($backup_name, "-multipart")) { |
|
1071 | + return true; |
|
1072 | + } |
|
1049 | 1073 | |
1050 | 1074 | return false; |
1051 | 1075 | } |
@@ -1063,8 +1087,9 @@ discard block |
||
1063 | 1087 | if ($this->is_multipart($backup_name)) |
1064 | 1088 | { |
1065 | 1089 | $backup_parts = $this->get_multipart_files($backup_name); |
1066 | - foreach ($backup_parts as $part_file) |
|
1067 | - $backup_size += $this->filesystem->getSize($part_file); |
|
1090 | + foreach ($backup_parts as $part_file) { |
|
1091 | + $backup_size += $this->filesystem->getSize($part_file); |
|
1092 | + } |
|
1068 | 1093 | } |
1069 | 1094 | |
1070 | 1095 | return $backup_size; |
@@ -1126,15 +1151,13 @@ discard block |
||
1126 | 1151 | if ($direction == 'desc') { |
1127 | 1152 | if ($a > $b) { |
1128 | 1153 | return -1; |
1129 | - } |
|
1130 | - else { |
|
1154 | + } else { |
|
1131 | 1155 | return 1; |
1132 | 1156 | } |
1133 | 1157 | } else { |
1134 | 1158 | if ($a < $b) { |
1135 | 1159 | return -1; |
1136 | - } |
|
1137 | - else { |
|
1160 | + } else { |
|
1138 | 1161 | return 1; |
1139 | 1162 | } |
1140 | 1163 | } |
@@ -1164,7 +1187,7 @@ discard block |
||
1164 | 1187 | { |
1165 | 1188 | $return['statusText'] = $response['message']; |
1166 | 1189 | $return['error'] = true; |
1167 | - }elseif ($status != 200 and $status != 418) |
|
1190 | + } elseif ($status != 200 and $status != 418) |
|
1168 | 1191 | { |
1169 | 1192 | $return['error'] = true; |
1170 | 1193 | $return['message'] = $response; |
@@ -1188,8 +1211,9 @@ discard block |
||
1188 | 1211 | return preg_replace_callback('!s:(\d+):([\\\\]?"[\\\\]?"|[\\\\]?"((.*?)[^\\\\])[\\\\]?");!', function($m) { |
1189 | 1212 | $data = ""; |
1190 | 1213 | |
1191 | - if (!isset($m[3])) |
|
1192 | - $m[3] = ""; |
|
1214 | + if (!isset($m[3])) { |
|
1215 | + $m[3] = ""; |
|
1216 | + } |
|
1193 | 1217 | |
1194 | 1218 | $data = 's:'.strlen(($m[3])).':\"'.($m[3]).'\";'; |
1195 | 1219 | //return $this->unescape_quotes($data); |
@@ -37,861 +37,861 @@ |
||
37 | 37 | class Xcloner_File_System |
38 | 38 | { |
39 | 39 | |
40 | - private $excluded_files = ""; |
|
41 | - private $additional_regex_patterns = array(); |
|
42 | - private $excluded_files_by_default = array("administrator/backups", "wp-content/backups"); |
|
43 | - private $included_files_handler = "backup_files.csv"; |
|
44 | - private $temp_dir_handler = ".dir"; |
|
45 | - public $filesystem; |
|
46 | - public $tmp_filesystem; |
|
47 | - public $storage_filesystem; |
|
48 | - private $xcloner_container; |
|
49 | - private $diff_timestamp_start = ""; |
|
50 | - |
|
51 | - private $logger; |
|
52 | - private $start_adapter; |
|
53 | - private $tmp_adapter; |
|
54 | - private $storage_adapter; |
|
55 | - private $xcloner_settings; |
|
56 | - private $start_filesystem; |
|
57 | - private $tmp_filesystem_append; |
|
58 | - private $storage_filesystem_append; |
|
59 | - |
|
60 | - private $files_counter; |
|
61 | - private $files_size; |
|
62 | - private $last_logged_file; |
|
63 | - private $folders_to_process_per_session = 25; |
|
64 | - private $backup_archive_extensions = array("tar", "tgz", "tar.gz", "gz", "csv", "encrypted", "decrypted"); |
|
65 | - private $backup_name_tags = array('[time]', '[hostname]', '[domain]'); |
|
66 | - |
|
67 | - /** |
|
68 | - * Xcloner_File_System constructor. |
|
69 | - * @param Xcloner $xcloner_container |
|
70 | - * @param string $hash |
|
71 | - */ |
|
72 | - public function __construct(Xcloner $xcloner_container, $hash = "") |
|
73 | - { |
|
74 | - $this->xcloner_container = $xcloner_container; |
|
75 | - |
|
76 | - $this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_file_system"); |
|
77 | - $this->xcloner_settings = $xcloner_container->get_xcloner_settings(); |
|
78 | - |
|
79 | - try { |
|
80 | - |
|
81 | - $this->start_adapter = new Local($this->xcloner_settings->get_xcloner_start_path(), LOCK_EX, 'SKIP_LINKS'); |
|
82 | - $this->start_filesystem = new Filesystem($this->start_adapter, new Config([ |
|
83 | - 'disable_asserts' => true, |
|
84 | - ])); |
|
85 | - |
|
86 | - $this->tmp_adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(), LOCK_EX, 'SKIP_LINKS'); |
|
87 | - $this->tmp_filesystem = new Filesystem($this->tmp_adapter, new Config([ |
|
88 | - 'disable_asserts' => true, |
|
89 | - ])); |
|
90 | - $adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(), LOCK_EX | FILE_APPEND, 'SKIP_LINKS'); |
|
91 | - $this->tmp_filesystem_append = new Filesystem($adapter, new Config([ |
|
92 | - 'disable_asserts' => true, |
|
93 | - ])); |
|
94 | - |
|
95 | - $adapter = new Local($this->xcloner_settings->get_xcloner_store_path(), LOCK_EX, 'SKIP_LINKS'); |
|
96 | - $this->storage_filesystem = new Filesystem($adapter, new Config([ |
|
97 | - 'disable_asserts' => true, |
|
98 | - ])); |
|
99 | - |
|
100 | - $this->storage_adapter = new Local($this->xcloner_settings->get_xcloner_store_path(), FILE_APPEND, |
|
101 | - 'SKIP_LINKS'); |
|
102 | - $this->storage_filesystem_append = new Filesystem($this->storage_adapter, new Config([ |
|
103 | - 'disable_asserts' => true, |
|
104 | - ])); |
|
105 | - }catch (Exception $e) { |
|
106 | - $this->logger->error("Filesystem Initialization Error: ".$e->getMessage()); |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - if ($value = get_option('xcloner_directories_to_scan_per_request')) { |
|
111 | - $this->folders_to_process_per_session = $value; |
|
112 | - } |
|
113 | - |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Set differential timestamp date |
|
118 | - * @param string $timestamp |
|
119 | - */ |
|
120 | - public function set_diff_timestamp_start($timestamp = "") |
|
121 | - { |
|
122 | - if ($timestamp) { |
|
123 | - $this->logger->info(sprintf("Setting Differential Timestamp To %s", date("Y-m-d", $timestamp)), array( |
|
124 | - "FILESYSTEM", |
|
125 | - "DIFF" |
|
126 | - )); |
|
127 | - $this->diff_timestamp_start = $timestamp; |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Gets the differential timestamp date |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - public function get_diff_timestamp_start() |
|
136 | - { |
|
137 | - return $this->diff_timestamp_start; |
|
138 | - } |
|
139 | - |
|
140 | - private function get_xcloner_container() |
|
141 | - { |
|
142 | - return $this->xcloner_container; |
|
143 | - } |
|
144 | - |
|
145 | - public function set_hash($hash) |
|
146 | - { |
|
147 | - $this->xcloner_settings->set_hash($hash); |
|
148 | - } |
|
149 | - |
|
150 | - public function get_hash($hash) |
|
151 | - { |
|
152 | - $this->xcloner_settings->get_hash(); |
|
153 | - } |
|
154 | - |
|
155 | - public function get_tmp_filesystem() |
|
156 | - { |
|
157 | - return $this->tmp_filesystem; |
|
158 | - } |
|
159 | - |
|
160 | - public function get_storage_filesystem($remote_storage_selection = "") |
|
161 | - { |
|
162 | - if ($remote_storage_selection != "") { |
|
163 | - $remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage(); |
|
164 | - $method = "get_".$remote_storage_selection."_filesystem"; |
|
165 | - |
|
166 | - if (!method_exists($remote_storage, $method)) { |
|
167 | - return false; |
|
168 | - } |
|
169 | - |
|
170 | - list($adapter, $filesystem) = $remote_storage->$method(); |
|
171 | - |
|
172 | - return $filesystem; |
|
173 | - } |
|
174 | - |
|
175 | - return $this->storage_filesystem; |
|
176 | - } |
|
177 | - |
|
178 | - public function get_tmp_filesystem_adapter() |
|
179 | - { |
|
180 | - return $this->tmp_adapter; |
|
181 | - } |
|
182 | - |
|
183 | - public function get_tmp_filesystem_append() |
|
184 | - { |
|
185 | - return $this->tmp_filesystem_append; |
|
186 | - } |
|
187 | - |
|
188 | - public function get_start_adapter() |
|
189 | - { |
|
190 | - return $this->start_adapter; |
|
191 | - } |
|
192 | - |
|
193 | - public function get_start_filesystem() |
|
194 | - { |
|
195 | - return $this->start_filesystem; |
|
196 | - } |
|
197 | - |
|
198 | - public function get_logger() |
|
199 | - { |
|
200 | - return $this->logger; |
|
201 | - } |
|
202 | - |
|
203 | - public function get_start_path_file_info($file) |
|
204 | - { |
|
205 | - $info = $this->getMetadataFull('start_adapter', $file); |
|
206 | - |
|
207 | - return $this->start_filesystem->normalizeFileInfo($info); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * @param string $file |
|
212 | - */ |
|
213 | - public function get_storage_path_file_info($file) |
|
214 | - { |
|
215 | - return $this->getMetadataFull('storage_adapter', $file); |
|
216 | - } |
|
217 | - |
|
218 | - public function get_included_files_handler($metadata = 0) |
|
219 | - { |
|
220 | - $path = $this->included_files_handler; |
|
221 | - if (!$metadata) { |
|
222 | - return $path; |
|
223 | - } |
|
224 | - |
|
225 | - $spl_info = $this->getMetadataFull('tmp_adapter', $path); |
|
226 | - |
|
227 | - return $spl_info; |
|
228 | - |
|
229 | - } |
|
230 | - |
|
231 | - public function get_temp_dir_handler() |
|
232 | - { |
|
233 | - return $this->temp_dir_handler; |
|
234 | - } |
|
235 | - |
|
236 | - public function get_latest_backup() |
|
237 | - { |
|
238 | - $files = $this->get_backup_archives_list(); |
|
239 | - |
|
240 | - if (is_array($files)) { |
|
241 | - $this->sort_by($files, "timestamp", "desc"); |
|
242 | - } |
|
243 | - |
|
244 | - $new_list = array(); |
|
245 | - |
|
246 | - foreach ($files as $key => $file) { |
|
247 | - if (!isset($file['parent'])) { |
|
248 | - $new_list[] = ($files[$key]); |
|
249 | - } |
|
250 | - } |
|
251 | - |
|
252 | - if (isset($new_list[0])) { |
|
253 | - return $new_list[0]; |
|
254 | - } |
|
255 | - } |
|
256 | - |
|
257 | - public function get_latest_backups() |
|
258 | - { |
|
259 | - $files = $this->get_backup_archives_list(); |
|
260 | - |
|
261 | - if (is_array($files)) { |
|
262 | - $this->sort_by($files, "timestamp", "desc"); |
|
263 | - } |
|
264 | - |
|
265 | - $new_list = array(); |
|
266 | - |
|
267 | - foreach ($files as $key => $file) { |
|
268 | - if (!isset($file['parent'])) { |
|
269 | - $new_list[] = ($files[$key]); |
|
270 | - } |
|
271 | - } |
|
272 | - |
|
273 | - return $new_list; |
|
274 | - } |
|
275 | - |
|
276 | - public function get_storage_usage() |
|
277 | - { |
|
278 | - $files = $this->get_backup_archives_list(); |
|
279 | - $total = 0; |
|
280 | - |
|
281 | - if (is_array($files)) { |
|
282 | - foreach ($files as $file) { |
|
283 | - $total += $file['size']; |
|
284 | - } |
|
285 | - } |
|
286 | - |
|
287 | - return $total; |
|
288 | - } |
|
289 | - |
|
290 | - public function is_part($backup_name) |
|
291 | - { |
|
292 | - if (stristr($backup_name, "-part")) { |
|
293 | - return true; |
|
294 | - } |
|
295 | - |
|
296 | - return false; |
|
297 | - } |
|
298 | - |
|
299 | - public function is_multipart($backup_name) |
|
300 | - { |
|
301 | - if (stristr($backup_name, "-multipart")) { |
|
302 | - return true; |
|
303 | - } |
|
304 | - |
|
305 | - return false; |
|
306 | - } |
|
307 | - |
|
308 | - public function get_backup_size($backup_name) |
|
309 | - { |
|
310 | - $backup_size = $this->get_storage_filesystem()->getSize($backup_name); |
|
311 | - if ($this->is_multipart($backup_name)) { |
|
312 | - $backup_parts = $this->get_multipart_files($backup_name); |
|
313 | - foreach ($backup_parts as $part_file) { |
|
314 | - $backup_size += $this->get_storage_filesystem()->getSize($part_file); |
|
315 | - } |
|
316 | - } |
|
317 | - |
|
318 | - return $backup_size; |
|
319 | - } |
|
320 | - |
|
321 | - public function get_multipart_files($backup_name, $storage_selection = "") |
|
322 | - { |
|
323 | - $files = array(); |
|
324 | - |
|
325 | - if ($this->is_multipart($backup_name)) { |
|
326 | - $lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($backup_name)); |
|
327 | - foreach ($lines as $line) { |
|
328 | - if ($line) { |
|
329 | - $data = str_getcsv($line); |
|
330 | - $files[] = $data[0]; |
|
331 | - } |
|
332 | - } |
|
333 | - } |
|
334 | - |
|
335 | - return $files; |
|
336 | - } |
|
337 | - |
|
338 | - public function delete_backup_by_name($backup_name, $storage_selection = "") |
|
339 | - { |
|
340 | - if ($this->is_multipart($backup_name)) { |
|
341 | - $lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($backup_name)); |
|
342 | - foreach ($lines as $line) { |
|
343 | - if ($line) { |
|
344 | - $data = str_getcsv($line); |
|
345 | - $this->get_storage_filesystem($storage_selection)->delete($data[0]); |
|
346 | - } |
|
347 | - } |
|
348 | - } |
|
349 | - |
|
350 | - if ($this->get_storage_filesystem($storage_selection)->delete($backup_name)) { |
|
351 | - $return = true; |
|
352 | - } else { |
|
353 | - $return = false; |
|
354 | - } |
|
355 | - |
|
356 | - return $return; |
|
357 | - } |
|
358 | - |
|
359 | - public function getMetadataFull($adapter = "storage_adapter", $path) |
|
360 | - { |
|
361 | - $location = $this->$adapter->applyPathPrefix($path); |
|
362 | - $spl_info = new SplFileInfo($location); |
|
363 | - |
|
364 | - return ($spl_info); |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - public function get_backup_archives_list($storage_selection = "") |
|
369 | - { |
|
370 | - $list = array(); |
|
371 | - |
|
372 | - |
|
373 | - if (method_exists($this->get_storage_filesystem($storage_selection), "listContents")) { |
|
374 | - $list = $this->get_storage_filesystem($storage_selection)->listContents(); |
|
375 | - } |
|
376 | - |
|
377 | - $backup_files = array(); |
|
378 | - $parents = array(); |
|
379 | - |
|
380 | - foreach ($list as $file_info) { |
|
381 | - if (isset($file_info['extension']) and $file_info['extension'] == "csv") { |
|
382 | - $data = array(); |
|
383 | - |
|
384 | - $lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($file_info['path'])); |
|
385 | - foreach ($lines as $line) { |
|
386 | - if ($line) { |
|
387 | - $data = str_getcsv($line); |
|
388 | - if (is_array($data)) { |
|
389 | - $parents[$data[0]] = $file_info['basename']; |
|
390 | - $file_info['childs'][] = $data; |
|
391 | - $file_info['size'] += $data[2]; |
|
392 | - } |
|
393 | - } |
|
394 | - } |
|
395 | - |
|
396 | - } |
|
397 | - |
|
398 | - if ($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'], |
|
399 | - $this->backup_archive_extensions)) { |
|
400 | - $backup_files[$file_info['path']] = $file_info; |
|
401 | - } |
|
402 | - } |
|
403 | - |
|
404 | - foreach ($backup_files as $key => $file_info) { |
|
405 | - if (!isset($backup_files[$key]['timestamp'])) { |
|
406 | - //$backup_files[$key]['timestamp'] = $this->get_storage_filesystem($storage_selection)->getTimestamp($file_info['path']); |
|
407 | - } |
|
408 | - |
|
409 | - if (isset($parents[$file_info['basename']])) { |
|
410 | - $backup_files[$key]['parent'] = $parents[$file_info['basename']]; |
|
411 | - } |
|
412 | - } |
|
413 | - |
|
414 | - return $backup_files; |
|
415 | - } |
|
416 | - |
|
417 | - public function start_file_recursion($init = 0) |
|
418 | - { |
|
419 | - if ($init) { |
|
420 | - $this->logger->info(sprintf(__("Starting the filesystem scanner on root folder %s"), |
|
421 | - $this->xcloner_settings->get_xcloner_start_path())); |
|
422 | - $this->do_system_init(); |
|
423 | - } |
|
424 | - |
|
425 | - if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) { |
|
426 | - //.dir exists, we presume we have files to iterate |
|
427 | - $content = $this->tmp_filesystem->read($this->get_temp_dir_handler()); |
|
428 | - $files = array_filter(explode("\n", $content)); |
|
429 | - $this->tmp_filesystem->delete($this->get_temp_dir_handler()); |
|
430 | - |
|
431 | - $counter = 0; |
|
432 | - foreach ($files as $file) { |
|
433 | - if ($counter < $this->folders_to_process_per_session) { |
|
434 | - $this->build_files_list($file); |
|
435 | - $counter++; |
|
436 | - } else { |
|
437 | - $this->tmp_filesystem_append->write($this->get_temp_dir_handler(), $file."\n"); |
|
438 | - } |
|
439 | - } |
|
440 | - } else { |
|
441 | - $this->build_files_list(); |
|
442 | - } |
|
443 | - |
|
444 | - if ($this->scan_finished()) { |
|
445 | - $metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata("index.html"); |
|
446 | - $this->store_file($metadata_dumpfile, 'tmp_filesystem'); |
|
447 | - $this->files_counter++; |
|
448 | - |
|
449 | - //adding included dump file to the included files list |
|
450 | - if ($this->get_tmp_filesystem()->has($this->get_included_files_handler())) { |
|
451 | - $metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->get_included_files_handler()); |
|
452 | - $this->store_file($metadata_dumpfile, 'tmp_filesystem'); |
|
453 | - $this->files_counter++; |
|
454 | - } |
|
455 | - |
|
456 | - //adding a default index.html to the temp xcloner folder |
|
457 | - if (!$this->get_tmp_filesystem()->has("index.html")) { |
|
458 | - $this->get_tmp_filesystem()->write("index.html", ""); |
|
459 | - } |
|
460 | - |
|
461 | - //adding the default log file |
|
462 | - if ($this->get_tmp_filesystem()->has($this->xcloner_settings->get_logger_filename(1))) { |
|
463 | - $metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->xcloner_settings->get_logger_filename(1)); |
|
464 | - $this->store_file($metadata_dumpfile, 'tmp_filesystem'); |
|
465 | - $this->files_counter++; |
|
466 | - } |
|
467 | - |
|
468 | - return false; |
|
469 | - } |
|
470 | - |
|
471 | - return true; |
|
472 | - } |
|
473 | - |
|
474 | - public function get_backup_attachments() |
|
475 | - { |
|
476 | - $return = array(); |
|
477 | - |
|
478 | - $files_list_file = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->get_included_files_handler(); |
|
479 | - if (file_exists($files_list_file)) { |
|
480 | - $return[] = $files_list_file; |
|
481 | - } |
|
482 | - |
|
483 | - if ($this->xcloner_settings->get_xcloner_option('xcloner_enable_log')) { |
|
484 | - $log_file = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->xcloner_settings->get_logger_filename(1); |
|
485 | - if (!file_exists($log_file)) { |
|
486 | - $log_file = $this->xcloner_settings->get_xcloner_store_path().DS.$this->xcloner_settings->get_logger_filename(); |
|
487 | - } |
|
488 | - |
|
489 | - if (file_exists($log_file)) { |
|
490 | - $return[] = $log_file; |
|
491 | - } |
|
492 | - } |
|
493 | - |
|
494 | - return $return; |
|
495 | - } |
|
496 | - |
|
497 | - public function remove_tmp_filesystem() |
|
498 | - { |
|
499 | - //delete the temporary folder |
|
500 | - $this->logger->debug(sprintf("Deleting the temporary storage folder %s", |
|
501 | - $this->xcloner_settings->get_xcloner_tmp_path())); |
|
502 | - |
|
503 | - $contents = $this->get_tmp_filesystem()->listContents(); |
|
504 | - |
|
505 | - if (is_array($contents)) { |
|
506 | - foreach ($contents as $file_info) { |
|
507 | - $this->get_tmp_filesystem()->delete($file_info['path']); |
|
508 | - } |
|
509 | - } |
|
510 | - |
|
511 | - try{ |
|
512 | - rmdir($this->xcloner_settings->get_xcloner_tmp_path()); |
|
513 | - }catch(Exception $e){ |
|
514 | - //silent continue |
|
515 | - } |
|
516 | - |
|
517 | - return; |
|
518 | - } |
|
519 | - |
|
520 | - public function cleanup_tmp_directories() |
|
521 | - { |
|
522 | - $adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(false), LOCK_EX | FILE_APPEND, 'SKIP_LINKS'); |
|
523 | - $tmp_filesystem = new Filesystem($adapter, new Config([ |
|
524 | - 'disable_asserts' => true, |
|
525 | - ])); |
|
526 | - |
|
527 | - $contents = $tmp_filesystem->listContents(); |
|
528 | - |
|
529 | - foreach ($contents as $file) { |
|
530 | - |
|
531 | - if (preg_match("/.xcloner-(.*)/", $file['path'])) { |
|
532 | - if ($file['timestamp'] < strtotime("-1days")) { |
|
533 | - $tmp_filesystem->deleteDir($file['path']); |
|
534 | - $this->logger->debug(sprintf("Delete temporary directory %s", $file['path'])); |
|
535 | - } |
|
536 | - } |
|
537 | - } |
|
538 | - |
|
539 | - return true; |
|
540 | - } |
|
541 | - |
|
542 | - private function do_system_init() |
|
543 | - { |
|
544 | - $this->files_counter = 0; |
|
545 | - |
|
546 | - if (!$this->storage_filesystem->has("index.html")) { |
|
547 | - $this->storage_filesystem->write("index.html", ""); |
|
548 | - } |
|
549 | - |
|
550 | - if (!$this->tmp_filesystem->has("index.html")) { |
|
551 | - $this->tmp_filesystem->write("index.html", ""); |
|
552 | - } |
|
553 | - |
|
554 | - if ($this->tmp_filesystem->has($this->get_included_files_handler())) { |
|
555 | - $this->tmp_filesystem->delete($this->get_included_files_handler()); |
|
556 | - } |
|
557 | - |
|
558 | - if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) { |
|
559 | - $this->tmp_filesystem->delete($this->get_temp_dir_handler()); |
|
560 | - } |
|
561 | - } |
|
562 | - |
|
563 | - public function get_scanned_files_num() |
|
564 | - { |
|
565 | - return $this->files_counter; |
|
566 | - } |
|
567 | - |
|
568 | - public function get_scanned_files_total_size() |
|
569 | - { |
|
570 | - return $this->files_size; |
|
571 | - } |
|
572 | - |
|
573 | - public function last_logged_file() |
|
574 | - { |
|
575 | - return $this->last_logged_file; |
|
576 | - } |
|
577 | - |
|
578 | - public static function is_regex($regex) |
|
579 | - { |
|
580 | - return preg_match("/^\^(.*)\$$/i", $regex); |
|
581 | - } |
|
40 | + private $excluded_files = ""; |
|
41 | + private $additional_regex_patterns = array(); |
|
42 | + private $excluded_files_by_default = array("administrator/backups", "wp-content/backups"); |
|
43 | + private $included_files_handler = "backup_files.csv"; |
|
44 | + private $temp_dir_handler = ".dir"; |
|
45 | + public $filesystem; |
|
46 | + public $tmp_filesystem; |
|
47 | + public $storage_filesystem; |
|
48 | + private $xcloner_container; |
|
49 | + private $diff_timestamp_start = ""; |
|
50 | + |
|
51 | + private $logger; |
|
52 | + private $start_adapter; |
|
53 | + private $tmp_adapter; |
|
54 | + private $storage_adapter; |
|
55 | + private $xcloner_settings; |
|
56 | + private $start_filesystem; |
|
57 | + private $tmp_filesystem_append; |
|
58 | + private $storage_filesystem_append; |
|
59 | + |
|
60 | + private $files_counter; |
|
61 | + private $files_size; |
|
62 | + private $last_logged_file; |
|
63 | + private $folders_to_process_per_session = 25; |
|
64 | + private $backup_archive_extensions = array("tar", "tgz", "tar.gz", "gz", "csv", "encrypted", "decrypted"); |
|
65 | + private $backup_name_tags = array('[time]', '[hostname]', '[domain]'); |
|
582 | 66 | |
583 | - public function set_excluded_files($excluded_files = array()) |
|
584 | - { |
|
585 | - if (!is_array($excluded_files)) { |
|
586 | - $excluded_files = array(); |
|
587 | - } |
|
67 | + /** |
|
68 | + * Xcloner_File_System constructor. |
|
69 | + * @param Xcloner $xcloner_container |
|
70 | + * @param string $hash |
|
71 | + */ |
|
72 | + public function __construct(Xcloner $xcloner_container, $hash = "") |
|
73 | + { |
|
74 | + $this->xcloner_container = $xcloner_container; |
|
588 | 75 | |
589 | - foreach ($excluded_files as $excl) { |
|
76 | + $this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_file_system"); |
|
77 | + $this->xcloner_settings = $xcloner_container->get_xcloner_settings(); |
|
590 | 78 | |
591 | - if ($this->is_regex($excl)) { |
|
592 | - $this->additional_regex_patterns[] = $excl; |
|
593 | - } |
|
594 | - } |
|
79 | + try { |
|
595 | 80 | |
596 | - $this->excluded_files = array_merge($excluded_files, $this->excluded_files_by_default); |
|
81 | + $this->start_adapter = new Local($this->xcloner_settings->get_xcloner_start_path(), LOCK_EX, 'SKIP_LINKS'); |
|
82 | + $this->start_filesystem = new Filesystem($this->start_adapter, new Config([ |
|
83 | + 'disable_asserts' => true, |
|
84 | + ])); |
|
85 | + |
|
86 | + $this->tmp_adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(), LOCK_EX, 'SKIP_LINKS'); |
|
87 | + $this->tmp_filesystem = new Filesystem($this->tmp_adapter, new Config([ |
|
88 | + 'disable_asserts' => true, |
|
89 | + ])); |
|
90 | + $adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(), LOCK_EX | FILE_APPEND, 'SKIP_LINKS'); |
|
91 | + $this->tmp_filesystem_append = new Filesystem($adapter, new Config([ |
|
92 | + 'disable_asserts' => true, |
|
93 | + ])); |
|
94 | + |
|
95 | + $adapter = new Local($this->xcloner_settings->get_xcloner_store_path(), LOCK_EX, 'SKIP_LINKS'); |
|
96 | + $this->storage_filesystem = new Filesystem($adapter, new Config([ |
|
97 | + 'disable_asserts' => true, |
|
98 | + ])); |
|
99 | + |
|
100 | + $this->storage_adapter = new Local($this->xcloner_settings->get_xcloner_store_path(), FILE_APPEND, |
|
101 | + 'SKIP_LINKS'); |
|
102 | + $this->storage_filesystem_append = new Filesystem($this->storage_adapter, new Config([ |
|
103 | + 'disable_asserts' => true, |
|
104 | + ])); |
|
105 | + }catch (Exception $e) { |
|
106 | + $this->logger->error("Filesystem Initialization Error: ".$e->getMessage()); |
|
107 | + } |
|
597 | 108 | |
598 | - return $this->excluded_files; |
|
599 | - } |
|
600 | 109 | |
601 | - public function get_excluded_files() |
|
602 | - { |
|
603 | - return $this->excluded_files_by_default; |
|
604 | - } |
|
110 | + if ($value = get_option('xcloner_directories_to_scan_per_request')) { |
|
111 | + $this->folders_to_process_per_session = $value; |
|
112 | + } |
|
605 | 113 | |
606 | - public function list_directory($path) |
|
607 | - { |
|
608 | - return $this->start_filesystem->listContents($path); |
|
609 | - } |
|
114 | + } |
|
610 | 115 | |
611 | - public function build_files_list($folder = "") |
|
612 | - { |
|
613 | - $this->logger->debug(sprintf(("Building the files system list"))); |
|
116 | + /** |
|
117 | + * Set differential timestamp date |
|
118 | + * @param string $timestamp |
|
119 | + */ |
|
120 | + public function set_diff_timestamp_start($timestamp = "") |
|
121 | + { |
|
122 | + if ($timestamp) { |
|
123 | + $this->logger->info(sprintf("Setting Differential Timestamp To %s", date("Y-m-d", $timestamp)), array( |
|
124 | + "FILESYSTEM", |
|
125 | + "DIFF" |
|
126 | + )); |
|
127 | + $this->diff_timestamp_start = $timestamp; |
|
128 | + } |
|
129 | + } |
|
614 | 130 | |
615 | - //if we start with the root folder(empty value), we initializa the file system |
|
616 | - if (!$folder) { |
|
131 | + /** |
|
132 | + * Gets the differential timestamp date |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + public function get_diff_timestamp_start() |
|
136 | + { |
|
137 | + return $this->diff_timestamp_start; |
|
138 | + } |
|
617 | 139 | |
618 | - } |
|
140 | + private function get_xcloner_container() |
|
141 | + { |
|
142 | + return $this->xcloner_container; |
|
143 | + } |
|
619 | 144 | |
620 | - try { |
|
145 | + public function set_hash($hash) |
|
146 | + { |
|
147 | + $this->xcloner_settings->set_hash($hash); |
|
148 | + } |
|
621 | 149 | |
622 | - $files = $this->start_filesystem->listContents($folder); |
|
623 | - foreach ($files as $file) { |
|
624 | - if (!is_readable($this->xcloner_settings->get_xcloner_start_path().DS.$file['path'])) { |
|
625 | - $this->logger->info(sprintf(__("Excluding %s from the filesystem list, file not readable"), |
|
626 | - $file['path']), array( |
|
627 | - "FILESYSTEM SCAN", |
|
628 | - "NOT READABLE" |
|
629 | - )); |
|
630 | - } elseif (!$matching_pattern = $this->is_excluded($file)) { |
|
631 | - $this->logger->info(sprintf(__("Adding %s to the filesystem list"), $file['path']), array( |
|
632 | - "FILESYSTEM SCAN", |
|
633 | - "INCLUDE" |
|
634 | - )); |
|
635 | - $file['visibility'] = $this->start_filesystem->getVisibility($file['path']); |
|
636 | - if ($this->store_file($file)) { |
|
637 | - $this->files_counter++; |
|
638 | - } |
|
639 | - if (isset($file['size'])) { |
|
640 | - $this->files_size += $file['size']; |
|
641 | - } |
|
642 | - |
|
643 | - } else { |
|
644 | - $this->logger->info(sprintf(__("Excluding %s from the filesystem list, matching pattern %s"), |
|
645 | - $file['path'], $matching_pattern), array( |
|
646 | - "FILESYSTEM SCAN", |
|
647 | - "EXCLUDE" |
|
648 | - )); |
|
649 | - } |
|
650 | - } |
|
150 | + public function get_hash($hash) |
|
151 | + { |
|
152 | + $this->xcloner_settings->get_hash(); |
|
153 | + } |
|
651 | 154 | |
652 | - }catch (Exception $e) { |
|
155 | + public function get_tmp_filesystem() |
|
156 | + { |
|
157 | + return $this->tmp_filesystem; |
|
158 | + } |
|
653 | 159 | |
654 | - $this->logger->error($e->getMessage()); |
|
655 | - |
|
656 | - } |
|
160 | + public function get_storage_filesystem($remote_storage_selection = "") |
|
161 | + { |
|
162 | + if ($remote_storage_selection != "") { |
|
163 | + $remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage(); |
|
164 | + $method = "get_".$remote_storage_selection."_filesystem"; |
|
657 | 165 | |
658 | - } |
|
166 | + if (!method_exists($remote_storage, $method)) { |
|
167 | + return false; |
|
168 | + } |
|
659 | 169 | |
660 | - public function estimate_read_write_time() |
|
661 | - { |
|
662 | - $tmp_file = ".xcloner".substr(md5(time()), 0, 5); |
|
170 | + list($adapter, $filesystem) = $remote_storage->$method(); |
|
663 | 171 | |
664 | - $start_time = microtime(true); |
|
172 | + return $filesystem; |
|
173 | + } |
|
665 | 174 | |
666 | - $data = str_repeat(rand(0, 9), 1024 * 1024); //write 1MB data |
|
175 | + return $this->storage_filesystem; |
|
176 | + } |
|
667 | 177 | |
668 | - try { |
|
669 | - $this->tmp_filesystem->write($tmp_file, $data); |
|
178 | + public function get_tmp_filesystem_adapter() |
|
179 | + { |
|
180 | + return $this->tmp_adapter; |
|
181 | + } |
|
670 | 182 | |
671 | - $end_time = microtime(true) - $start_time; |
|
183 | + public function get_tmp_filesystem_append() |
|
184 | + { |
|
185 | + return $this->tmp_filesystem_append; |
|
186 | + } |
|
672 | 187 | |
673 | - $return['writing_time'] = $end_time; |
|
188 | + public function get_start_adapter() |
|
189 | + { |
|
190 | + return $this->start_adapter; |
|
191 | + } |
|
674 | 192 | |
675 | - $return['reading_time'] = $this->estimate_reading_time($tmp_file); |
|
193 | + public function get_start_filesystem() |
|
194 | + { |
|
195 | + return $this->start_filesystem; |
|
196 | + } |
|
676 | 197 | |
677 | - $this->tmp_filesystem->delete($tmp_file); |
|
198 | + public function get_logger() |
|
199 | + { |
|
200 | + return $this->logger; |
|
201 | + } |
|
678 | 202 | |
679 | - }catch (Exception $e) { |
|
203 | + public function get_start_path_file_info($file) |
|
204 | + { |
|
205 | + $info = $this->getMetadataFull('start_adapter', $file); |
|
680 | 206 | |
681 | - $this->logger->error($e->getMessage()); |
|
207 | + return $this->start_filesystem->normalizeFileInfo($info); |
|
208 | + } |
|
682 | 209 | |
683 | - } |
|
210 | + /** |
|
211 | + * @param string $file |
|
212 | + */ |
|
213 | + public function get_storage_path_file_info($file) |
|
214 | + { |
|
215 | + return $this->getMetadataFull('storage_adapter', $file); |
|
216 | + } |
|
684 | 217 | |
685 | - return $return; |
|
686 | - } |
|
218 | + public function get_included_files_handler($metadata = 0) |
|
219 | + { |
|
220 | + $path = $this->included_files_handler; |
|
221 | + if (!$metadata) { |
|
222 | + return $path; |
|
223 | + } |
|
224 | + |
|
225 | + $spl_info = $this->getMetadataFull('tmp_adapter', $path); |
|
226 | + |
|
227 | + return $spl_info; |
|
228 | + |
|
229 | + } |
|
230 | + |
|
231 | + public function get_temp_dir_handler() |
|
232 | + { |
|
233 | + return $this->temp_dir_handler; |
|
234 | + } |
|
235 | + |
|
236 | + public function get_latest_backup() |
|
237 | + { |
|
238 | + $files = $this->get_backup_archives_list(); |
|
239 | + |
|
240 | + if (is_array($files)) { |
|
241 | + $this->sort_by($files, "timestamp", "desc"); |
|
242 | + } |
|
243 | + |
|
244 | + $new_list = array(); |
|
245 | + |
|
246 | + foreach ($files as $key => $file) { |
|
247 | + if (!isset($file['parent'])) { |
|
248 | + $new_list[] = ($files[$key]); |
|
249 | + } |
|
250 | + } |
|
251 | + |
|
252 | + if (isset($new_list[0])) { |
|
253 | + return $new_list[0]; |
|
254 | + } |
|
255 | + } |
|
256 | + |
|
257 | + public function get_latest_backups() |
|
258 | + { |
|
259 | + $files = $this->get_backup_archives_list(); |
|
260 | + |
|
261 | + if (is_array($files)) { |
|
262 | + $this->sort_by($files, "timestamp", "desc"); |
|
263 | + } |
|
687 | 264 | |
688 | - public function backup_storage_cleanup() |
|
689 | - { |
|
690 | - $this->logger->info(sprintf(("Cleaning the backup storage on matching rules"))); |
|
265 | + $new_list = array(); |
|
691 | 266 | |
692 | - $_storage_size = 0; |
|
693 | - $_backup_files_list = array(); |
|
267 | + foreach ($files as $key => $file) { |
|
268 | + if (!isset($file['parent'])) { |
|
269 | + $new_list[] = ($files[$key]); |
|
270 | + } |
|
271 | + } |
|
272 | + |
|
273 | + return $new_list; |
|
274 | + } |
|
275 | + |
|
276 | + public function get_storage_usage() |
|
277 | + { |
|
278 | + $files = $this->get_backup_archives_list(); |
|
279 | + $total = 0; |
|
694 | 280 | |
695 | - //rule date limit |
|
696 | - $current_timestamp = strtotime("-".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days')." days"); |
|
697 | - |
|
698 | - $files = $this->storage_filesystem->listContents(); |
|
699 | - |
|
700 | - if (is_array($files)) { |
|
701 | - foreach ($files as $file) { |
|
702 | - if (isset($file['extension']) and in_array($file['extension'], $this->backup_archive_extensions)) { |
|
703 | - $_storage_size += $file['size']; //bytes |
|
704 | - $_backup_files_list[] = $file; |
|
705 | - } |
|
706 | - } |
|
707 | - } |
|
281 | + if (is_array($files)) { |
|
282 | + foreach ($files as $file) { |
|
283 | + $total += $file['size']; |
|
284 | + } |
|
285 | + } |
|
286 | + |
|
287 | + return $total; |
|
288 | + } |
|
708 | 289 | |
290 | + public function is_part($backup_name) |
|
291 | + { |
|
292 | + if (stristr($backup_name, "-part")) { |
|
293 | + return true; |
|
294 | + } |
|
709 | 295 | |
710 | - $this->sort_by($_backup_files_list, "timestamp", "asc"); |
|
711 | - |
|
712 | - $_backups_counter = sizeof($_backup_files_list); |
|
713 | - |
|
714 | - foreach ($_backup_files_list as $file) { |
|
715 | - //processing rule folder capacity |
|
716 | - if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit') && |
|
717 | - $_storage_size >= ($set_storage_limit = 1024 * 1024 * $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit'))) //bytes |
|
718 | - { |
|
719 | - $this->storage_filesystem->delete($file['path']); |
|
720 | - $_storage_size -= $file['size']; |
|
721 | - $this->logger->info("Deleting backup ".$file['path']." matching rule", array( |
|
722 | - "STORAGE SIZE LIMIT", |
|
723 | - $_storage_size." >= ".$set_storage_limit |
|
724 | - )); |
|
725 | - } |
|
726 | - |
|
727 | - //processing rule days limit |
|
728 | - if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days') && $current_timestamp >= $file['timestamp']) { |
|
729 | - $this->storage_filesystem->delete($file['path']); |
|
730 | - $this->logger->info("Deleting backup ".$file['path']." matching rule", array( |
|
731 | - "RETENTION LIMIT TIMESTAMP", |
|
732 | - $file['timestamp']." =< ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days') |
|
733 | - )); |
|
734 | - } |
|
735 | - |
|
736 | - //processing backup countert limit |
|
737 | - if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives') && $_backups_counter >= $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives')) { |
|
738 | - $this->storage_filesystem->delete($file['path']); |
|
739 | - $_backups_counter--; |
|
740 | - $this->logger->info("Deleting backup ".$file['path']." matching rule", array( |
|
741 | - "BACKUP QUANTITY LIMIT", |
|
742 | - $_backups_counter." >= ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives') |
|
743 | - )); |
|
744 | - } |
|
745 | - |
|
746 | - |
|
747 | - } |
|
296 | + return false; |
|
297 | + } |
|
298 | + |
|
299 | + public function is_multipart($backup_name) |
|
300 | + { |
|
301 | + if (stristr($backup_name, "-multipart")) { |
|
302 | + return true; |
|
303 | + } |
|
304 | + |
|
305 | + return false; |
|
306 | + } |
|
307 | + |
|
308 | + public function get_backup_size($backup_name) |
|
309 | + { |
|
310 | + $backup_size = $this->get_storage_filesystem()->getSize($backup_name); |
|
311 | + if ($this->is_multipart($backup_name)) { |
|
312 | + $backup_parts = $this->get_multipart_files($backup_name); |
|
313 | + foreach ($backup_parts as $part_file) { |
|
314 | + $backup_size += $this->get_storage_filesystem()->getSize($part_file); |
|
315 | + } |
|
316 | + } |
|
317 | + |
|
318 | + return $backup_size; |
|
319 | + } |
|
320 | + |
|
321 | + public function get_multipart_files($backup_name, $storage_selection = "") |
|
322 | + { |
|
323 | + $files = array(); |
|
324 | + |
|
325 | + if ($this->is_multipart($backup_name)) { |
|
326 | + $lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($backup_name)); |
|
327 | + foreach ($lines as $line) { |
|
328 | + if ($line) { |
|
329 | + $data = str_getcsv($line); |
|
330 | + $files[] = $data[0]; |
|
331 | + } |
|
332 | + } |
|
333 | + } |
|
334 | + |
|
335 | + return $files; |
|
336 | + } |
|
337 | + |
|
338 | + public function delete_backup_by_name($backup_name, $storage_selection = "") |
|
339 | + { |
|
340 | + if ($this->is_multipart($backup_name)) { |
|
341 | + $lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($backup_name)); |
|
342 | + foreach ($lines as $line) { |
|
343 | + if ($line) { |
|
344 | + $data = str_getcsv($line); |
|
345 | + $this->get_storage_filesystem($storage_selection)->delete($data[0]); |
|
346 | + } |
|
347 | + } |
|
348 | + } |
|
349 | + |
|
350 | + if ($this->get_storage_filesystem($storage_selection)->delete($backup_name)) { |
|
351 | + $return = true; |
|
352 | + } else { |
|
353 | + $return = false; |
|
354 | + } |
|
355 | + |
|
356 | + return $return; |
|
357 | + } |
|
358 | + |
|
359 | + public function getMetadataFull($adapter = "storage_adapter", $path) |
|
360 | + { |
|
361 | + $location = $this->$adapter->applyPathPrefix($path); |
|
362 | + $spl_info = new SplFileInfo($location); |
|
363 | + |
|
364 | + return ($spl_info); |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + public function get_backup_archives_list($storage_selection = "") |
|
369 | + { |
|
370 | + $list = array(); |
|
371 | + |
|
372 | + |
|
373 | + if (method_exists($this->get_storage_filesystem($storage_selection), "listContents")) { |
|
374 | + $list = $this->get_storage_filesystem($storage_selection)->listContents(); |
|
375 | + } |
|
376 | + |
|
377 | + $backup_files = array(); |
|
378 | + $parents = array(); |
|
379 | + |
|
380 | + foreach ($list as $file_info) { |
|
381 | + if (isset($file_info['extension']) and $file_info['extension'] == "csv") { |
|
382 | + $data = array(); |
|
383 | + |
|
384 | + $lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($file_info['path'])); |
|
385 | + foreach ($lines as $line) { |
|
386 | + if ($line) { |
|
387 | + $data = str_getcsv($line); |
|
388 | + if (is_array($data)) { |
|
389 | + $parents[$data[0]] = $file_info['basename']; |
|
390 | + $file_info['childs'][] = $data; |
|
391 | + $file_info['size'] += $data[2]; |
|
392 | + } |
|
393 | + } |
|
394 | + } |
|
395 | + |
|
396 | + } |
|
397 | + |
|
398 | + if ($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'], |
|
399 | + $this->backup_archive_extensions)) { |
|
400 | + $backup_files[$file_info['path']] = $file_info; |
|
401 | + } |
|
402 | + } |
|
403 | + |
|
404 | + foreach ($backup_files as $key => $file_info) { |
|
405 | + if (!isset($backup_files[$key]['timestamp'])) { |
|
406 | + //$backup_files[$key]['timestamp'] = $this->get_storage_filesystem($storage_selection)->getTimestamp($file_info['path']); |
|
407 | + } |
|
408 | + |
|
409 | + if (isset($parents[$file_info['basename']])) { |
|
410 | + $backup_files[$key]['parent'] = $parents[$file_info['basename']]; |
|
411 | + } |
|
412 | + } |
|
413 | + |
|
414 | + return $backup_files; |
|
415 | + } |
|
416 | + |
|
417 | + public function start_file_recursion($init = 0) |
|
418 | + { |
|
419 | + if ($init) { |
|
420 | + $this->logger->info(sprintf(__("Starting the filesystem scanner on root folder %s"), |
|
421 | + $this->xcloner_settings->get_xcloner_start_path())); |
|
422 | + $this->do_system_init(); |
|
423 | + } |
|
424 | + |
|
425 | + if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) { |
|
426 | + //.dir exists, we presume we have files to iterate |
|
427 | + $content = $this->tmp_filesystem->read($this->get_temp_dir_handler()); |
|
428 | + $files = array_filter(explode("\n", $content)); |
|
429 | + $this->tmp_filesystem->delete($this->get_temp_dir_handler()); |
|
430 | + |
|
431 | + $counter = 0; |
|
432 | + foreach ($files as $file) { |
|
433 | + if ($counter < $this->folders_to_process_per_session) { |
|
434 | + $this->build_files_list($file); |
|
435 | + $counter++; |
|
436 | + } else { |
|
437 | + $this->tmp_filesystem_append->write($this->get_temp_dir_handler(), $file."\n"); |
|
438 | + } |
|
439 | + } |
|
440 | + } else { |
|
441 | + $this->build_files_list(); |
|
442 | + } |
|
443 | + |
|
444 | + if ($this->scan_finished()) { |
|
445 | + $metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata("index.html"); |
|
446 | + $this->store_file($metadata_dumpfile, 'tmp_filesystem'); |
|
447 | + $this->files_counter++; |
|
448 | + |
|
449 | + //adding included dump file to the included files list |
|
450 | + if ($this->get_tmp_filesystem()->has($this->get_included_files_handler())) { |
|
451 | + $metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->get_included_files_handler()); |
|
452 | + $this->store_file($metadata_dumpfile, 'tmp_filesystem'); |
|
453 | + $this->files_counter++; |
|
454 | + } |
|
455 | + |
|
456 | + //adding a default index.html to the temp xcloner folder |
|
457 | + if (!$this->get_tmp_filesystem()->has("index.html")) { |
|
458 | + $this->get_tmp_filesystem()->write("index.html", ""); |
|
459 | + } |
|
460 | + |
|
461 | + //adding the default log file |
|
462 | + if ($this->get_tmp_filesystem()->has($this->xcloner_settings->get_logger_filename(1))) { |
|
463 | + $metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->xcloner_settings->get_logger_filename(1)); |
|
464 | + $this->store_file($metadata_dumpfile, 'tmp_filesystem'); |
|
465 | + $this->files_counter++; |
|
466 | + } |
|
467 | + |
|
468 | + return false; |
|
469 | + } |
|
470 | + |
|
471 | + return true; |
|
472 | + } |
|
473 | + |
|
474 | + public function get_backup_attachments() |
|
475 | + { |
|
476 | + $return = array(); |
|
477 | + |
|
478 | + $files_list_file = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->get_included_files_handler(); |
|
479 | + if (file_exists($files_list_file)) { |
|
480 | + $return[] = $files_list_file; |
|
481 | + } |
|
482 | + |
|
483 | + if ($this->xcloner_settings->get_xcloner_option('xcloner_enable_log')) { |
|
484 | + $log_file = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->xcloner_settings->get_logger_filename(1); |
|
485 | + if (!file_exists($log_file)) { |
|
486 | + $log_file = $this->xcloner_settings->get_xcloner_store_path().DS.$this->xcloner_settings->get_logger_filename(); |
|
487 | + } |
|
488 | + |
|
489 | + if (file_exists($log_file)) { |
|
490 | + $return[] = $log_file; |
|
491 | + } |
|
492 | + } |
|
493 | + |
|
494 | + return $return; |
|
495 | + } |
|
496 | + |
|
497 | + public function remove_tmp_filesystem() |
|
498 | + { |
|
499 | + //delete the temporary folder |
|
500 | + $this->logger->debug(sprintf("Deleting the temporary storage folder %s", |
|
501 | + $this->xcloner_settings->get_xcloner_tmp_path())); |
|
502 | + |
|
503 | + $contents = $this->get_tmp_filesystem()->listContents(); |
|
504 | + |
|
505 | + if (is_array($contents)) { |
|
506 | + foreach ($contents as $file_info) { |
|
507 | + $this->get_tmp_filesystem()->delete($file_info['path']); |
|
508 | + } |
|
509 | + } |
|
510 | + |
|
511 | + try{ |
|
512 | + rmdir($this->xcloner_settings->get_xcloner_tmp_path()); |
|
513 | + }catch(Exception $e){ |
|
514 | + //silent continue |
|
515 | + } |
|
516 | + |
|
517 | + return; |
|
518 | + } |
|
519 | + |
|
520 | + public function cleanup_tmp_directories() |
|
521 | + { |
|
522 | + $adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(false), LOCK_EX | FILE_APPEND, 'SKIP_LINKS'); |
|
523 | + $tmp_filesystem = new Filesystem($adapter, new Config([ |
|
524 | + 'disable_asserts' => true, |
|
525 | + ])); |
|
748 | 526 | |
749 | - } |
|
527 | + $contents = $tmp_filesystem->listContents(); |
|
528 | + |
|
529 | + foreach ($contents as $file) { |
|
530 | + |
|
531 | + if (preg_match("/.xcloner-(.*)/", $file['path'])) { |
|
532 | + if ($file['timestamp'] < strtotime("-1days")) { |
|
533 | + $tmp_filesystem->deleteDir($file['path']); |
|
534 | + $this->logger->debug(sprintf("Delete temporary directory %s", $file['path'])); |
|
535 | + } |
|
536 | + } |
|
537 | + } |
|
538 | + |
|
539 | + return true; |
|
540 | + } |
|
541 | + |
|
542 | + private function do_system_init() |
|
543 | + { |
|
544 | + $this->files_counter = 0; |
|
545 | + |
|
546 | + if (!$this->storage_filesystem->has("index.html")) { |
|
547 | + $this->storage_filesystem->write("index.html", ""); |
|
548 | + } |
|
549 | + |
|
550 | + if (!$this->tmp_filesystem->has("index.html")) { |
|
551 | + $this->tmp_filesystem->write("index.html", ""); |
|
552 | + } |
|
553 | + |
|
554 | + if ($this->tmp_filesystem->has($this->get_included_files_handler())) { |
|
555 | + $this->tmp_filesystem->delete($this->get_included_files_handler()); |
|
556 | + } |
|
557 | + |
|
558 | + if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) { |
|
559 | + $this->tmp_filesystem->delete($this->get_temp_dir_handler()); |
|
560 | + } |
|
561 | + } |
|
562 | + |
|
563 | + public function get_scanned_files_num() |
|
564 | + { |
|
565 | + return $this->files_counter; |
|
566 | + } |
|
567 | + |
|
568 | + public function get_scanned_files_total_size() |
|
569 | + { |
|
570 | + return $this->files_size; |
|
571 | + } |
|
572 | + |
|
573 | + public function last_logged_file() |
|
574 | + { |
|
575 | + return $this->last_logged_file; |
|
576 | + } |
|
577 | + |
|
578 | + public static function is_regex($regex) |
|
579 | + { |
|
580 | + return preg_match("/^\^(.*)\$$/i", $regex); |
|
581 | + } |
|
582 | + |
|
583 | + public function set_excluded_files($excluded_files = array()) |
|
584 | + { |
|
585 | + if (!is_array($excluded_files)) { |
|
586 | + $excluded_files = array(); |
|
587 | + } |
|
588 | + |
|
589 | + foreach ($excluded_files as $excl) { |
|
590 | + |
|
591 | + if ($this->is_regex($excl)) { |
|
592 | + $this->additional_regex_patterns[] = $excl; |
|
593 | + } |
|
594 | + } |
|
595 | + |
|
596 | + $this->excluded_files = array_merge($excluded_files, $this->excluded_files_by_default); |
|
597 | + |
|
598 | + return $this->excluded_files; |
|
599 | + } |
|
600 | + |
|
601 | + public function get_excluded_files() |
|
602 | + { |
|
603 | + return $this->excluded_files_by_default; |
|
604 | + } |
|
605 | + |
|
606 | + public function list_directory($path) |
|
607 | + { |
|
608 | + return $this->start_filesystem->listContents($path); |
|
609 | + } |
|
610 | + |
|
611 | + public function build_files_list($folder = "") |
|
612 | + { |
|
613 | + $this->logger->debug(sprintf(("Building the files system list"))); |
|
614 | + |
|
615 | + //if we start with the root folder(empty value), we initializa the file system |
|
616 | + if (!$folder) { |
|
617 | + |
|
618 | + } |
|
619 | + |
|
620 | + try { |
|
621 | + |
|
622 | + $files = $this->start_filesystem->listContents($folder); |
|
623 | + foreach ($files as $file) { |
|
624 | + if (!is_readable($this->xcloner_settings->get_xcloner_start_path().DS.$file['path'])) { |
|
625 | + $this->logger->info(sprintf(__("Excluding %s from the filesystem list, file not readable"), |
|
626 | + $file['path']), array( |
|
627 | + "FILESYSTEM SCAN", |
|
628 | + "NOT READABLE" |
|
629 | + )); |
|
630 | + } elseif (!$matching_pattern = $this->is_excluded($file)) { |
|
631 | + $this->logger->info(sprintf(__("Adding %s to the filesystem list"), $file['path']), array( |
|
632 | + "FILESYSTEM SCAN", |
|
633 | + "INCLUDE" |
|
634 | + )); |
|
635 | + $file['visibility'] = $this->start_filesystem->getVisibility($file['path']); |
|
636 | + if ($this->store_file($file)) { |
|
637 | + $this->files_counter++; |
|
638 | + } |
|
639 | + if (isset($file['size'])) { |
|
640 | + $this->files_size += $file['size']; |
|
641 | + } |
|
642 | + |
|
643 | + } else { |
|
644 | + $this->logger->info(sprintf(__("Excluding %s from the filesystem list, matching pattern %s"), |
|
645 | + $file['path'], $matching_pattern), array( |
|
646 | + "FILESYSTEM SCAN", |
|
647 | + "EXCLUDE" |
|
648 | + )); |
|
649 | + } |
|
650 | + } |
|
651 | + |
|
652 | + }catch (Exception $e) { |
|
653 | + |
|
654 | + $this->logger->error($e->getMessage()); |
|
655 | + |
|
656 | + } |
|
657 | + |
|
658 | + } |
|
659 | + |
|
660 | + public function estimate_read_write_time() |
|
661 | + { |
|
662 | + $tmp_file = ".xcloner".substr(md5(time()), 0, 5); |
|
663 | + |
|
664 | + $start_time = microtime(true); |
|
665 | + |
|
666 | + $data = str_repeat(rand(0, 9), 1024 * 1024); //write 1MB data |
|
667 | + |
|
668 | + try { |
|
669 | + $this->tmp_filesystem->write($tmp_file, $data); |
|
670 | + |
|
671 | + $end_time = microtime(true) - $start_time; |
|
672 | + |
|
673 | + $return['writing_time'] = $end_time; |
|
674 | + |
|
675 | + $return['reading_time'] = $this->estimate_reading_time($tmp_file); |
|
676 | + |
|
677 | + $this->tmp_filesystem->delete($tmp_file); |
|
678 | + |
|
679 | + }catch (Exception $e) { |
|
680 | + |
|
681 | + $this->logger->error($e->getMessage()); |
|
682 | + |
|
683 | + } |
|
684 | + |
|
685 | + return $return; |
|
686 | + } |
|
687 | + |
|
688 | + public function backup_storage_cleanup() |
|
689 | + { |
|
690 | + $this->logger->info(sprintf(("Cleaning the backup storage on matching rules"))); |
|
691 | + |
|
692 | + $_storage_size = 0; |
|
693 | + $_backup_files_list = array(); |
|
694 | + |
|
695 | + //rule date limit |
|
696 | + $current_timestamp = strtotime("-".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days')." days"); |
|
697 | + |
|
698 | + $files = $this->storage_filesystem->listContents(); |
|
699 | + |
|
700 | + if (is_array($files)) { |
|
701 | + foreach ($files as $file) { |
|
702 | + if (isset($file['extension']) and in_array($file['extension'], $this->backup_archive_extensions)) { |
|
703 | + $_storage_size += $file['size']; //bytes |
|
704 | + $_backup_files_list[] = $file; |
|
705 | + } |
|
706 | + } |
|
707 | + } |
|
708 | + |
|
709 | + |
|
710 | + $this->sort_by($_backup_files_list, "timestamp", "asc"); |
|
711 | + |
|
712 | + $_backups_counter = sizeof($_backup_files_list); |
|
713 | + |
|
714 | + foreach ($_backup_files_list as $file) { |
|
715 | + //processing rule folder capacity |
|
716 | + if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit') && |
|
717 | + $_storage_size >= ($set_storage_limit = 1024 * 1024 * $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit'))) //bytes |
|
718 | + { |
|
719 | + $this->storage_filesystem->delete($file['path']); |
|
720 | + $_storage_size -= $file['size']; |
|
721 | + $this->logger->info("Deleting backup ".$file['path']." matching rule", array( |
|
722 | + "STORAGE SIZE LIMIT", |
|
723 | + $_storage_size." >= ".$set_storage_limit |
|
724 | + )); |
|
725 | + } |
|
726 | + |
|
727 | + //processing rule days limit |
|
728 | + if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days') && $current_timestamp >= $file['timestamp']) { |
|
729 | + $this->storage_filesystem->delete($file['path']); |
|
730 | + $this->logger->info("Deleting backup ".$file['path']." matching rule", array( |
|
731 | + "RETENTION LIMIT TIMESTAMP", |
|
732 | + $file['timestamp']." =< ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days') |
|
733 | + )); |
|
734 | + } |
|
735 | + |
|
736 | + //processing backup countert limit |
|
737 | + if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives') && $_backups_counter >= $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives')) { |
|
738 | + $this->storage_filesystem->delete($file['path']); |
|
739 | + $_backups_counter--; |
|
740 | + $this->logger->info("Deleting backup ".$file['path']." matching rule", array( |
|
741 | + "BACKUP QUANTITY LIMIT", |
|
742 | + $_backups_counter." >= ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives') |
|
743 | + )); |
|
744 | + } |
|
745 | + |
|
746 | + |
|
747 | + } |
|
748 | + |
|
749 | + } |
|
750 | + |
|
751 | + /** |
|
752 | + * @param string $tmp_file |
|
753 | + */ |
|
754 | + public function estimate_reading_time($tmp_file) |
|
755 | + { |
|
756 | + $this->logger->debug(sprintf(("Estimating file system reading time"))); |
|
757 | + |
|
758 | + $start_time = microtime(true); |
|
759 | + |
|
760 | + if ($this->tmp_filesystem->has($tmp_file)) { |
|
761 | + $this->tmp_filesystem->read($tmp_file); |
|
762 | + } |
|
763 | + |
|
764 | + $end_time = microtime(true) - $start_time; |
|
765 | + |
|
766 | + return $end_time; |
|
767 | + |
|
768 | + } |
|
769 | + |
|
770 | + public function process_backup_name($name = "", $max_length = 100) |
|
771 | + { |
|
772 | + if (!$name) { |
|
773 | + $name = $this->xcloner_settings->get_default_backup_name(); |
|
774 | + } |
|
775 | + |
|
776 | + foreach ($this->backup_name_tags as $tag) { |
|
777 | + if ($tag == '[time]') { |
|
778 | + $name = str_replace($tag, date("Y-m-d_H-i"), $name); |
|
779 | + } elseif ($tag == '[hostname]') { |
|
780 | + $name = str_replace($tag, gethostname(), $name); |
|
781 | + } elseif ($tag == '[domain]') { |
|
782 | + $domain = parse_url(admin_url(), PHP_URL_HOST); |
|
783 | + $name = str_replace($tag, $domain, $name); |
|
784 | + } |
|
785 | + } |
|
786 | + |
|
787 | + if ($max_length) { |
|
788 | + $name = substr($name, 0, $max_length); |
|
789 | + } |
|
790 | + |
|
791 | + return $name; |
|
792 | + } |
|
793 | + |
|
794 | + /** |
|
795 | + * @param string $field |
|
796 | + */ |
|
797 | + public function sort_by(&$array, $field, $direction = 'asc') |
|
798 | + { |
|
799 | + if (strtolower($direction) == "desc" || $direction == SORT_DESC) { |
|
800 | + $direction = SORT_DESC; |
|
801 | + } else { |
|
802 | + $direction = SORT_ASC; |
|
803 | + } |
|
804 | + |
|
805 | + $array = $this->array_orderby($array, $field, $direction); |
|
806 | + |
|
807 | + return true; |
|
808 | + } |
|
809 | + |
|
810 | + private function array_orderby() |
|
811 | + { |
|
812 | + $args = func_get_args(); |
|
813 | + $data = array_shift($args); |
|
814 | + |
|
815 | + foreach ($args as $n => $field) { |
|
816 | + if (is_string($field)) { |
|
817 | + $tmp = array(); |
|
818 | + foreach ($data as $key => $row) { |
|
819 | + if (is_array($row)) { |
|
820 | + $tmp[$key] = $row[$field]; |
|
821 | + } else { |
|
822 | + $tmp[$key] = $row->$field; |
|
823 | + } |
|
824 | + } |
|
825 | + $args[$n] = $tmp; |
|
826 | + } |
|
827 | + } |
|
828 | + $args[] = &$data; |
|
829 | + |
|
830 | + call_user_func_array('array_multisort', $args); |
|
831 | + |
|
832 | + return array_pop($args); |
|
833 | + } |
|
834 | + |
|
835 | + private function check_file_diff_time($file) |
|
836 | + { |
|
837 | + if ($this->get_diff_timestamp_start() != "") { |
|
838 | + $fileMeta = $this->getMetadataFull("start_adapter", $file['path']); |
|
839 | + $timestamp = $fileMeta->getMTime(); |
|
840 | + if ($timestamp < $fileMeta->getCTime()) { |
|
841 | + $timestamp = $fileMeta->getCTime(); |
|
842 | + } |
|
843 | + |
|
844 | + if ($timestamp <= $this->get_diff_timestamp_start()) { |
|
845 | + return " file DIFF timestamp ".$timestamp." < ".$this->diff_timestamp_start; |
|
846 | + } |
|
847 | + } |
|
848 | + |
|
849 | + return false; |
|
850 | + } |
|
851 | + |
|
852 | + public function is_excluded($file) |
|
853 | + { |
|
854 | + $this->logger->debug(sprintf(("Checking if %s is excluded"), $file['path'])); |
|
855 | + |
|
856 | + if ($xcloner_exclude_files_larger_than_mb = $this->xcloner_settings->get_xcloner_option('xcloner_exclude_files_larger_than_mb')) { |
|
857 | + if (isset($file['size']) && $file['size'] > $this->calc_to_bytes($xcloner_exclude_files_larger_than_mb)) { |
|
858 | + return "> ".$xcloner_exclude_files_larger_than_mb."MB"; |
|
859 | + } |
|
860 | + } |
|
861 | + |
|
862 | + if (!is_array($this->excluded_files) || !sizeof($this->excluded_files)) { |
|
863 | + $this->set_excluded_files(); |
|
864 | + } |
|
865 | + |
|
866 | + if (is_array($this->excluded_files)) { |
|
867 | + foreach ($this->excluded_files as $excluded_file_pattern) { |
|
868 | + if ($excluded_file_pattern == "/") { |
|
869 | + $needle = "$"; |
|
870 | + } else { |
|
871 | + $needle = "$".$excluded_file_pattern; |
|
872 | + } |
|
873 | + |
|
874 | + if (strstr("$".$file['path'], $needle)) { |
|
875 | + return $excluded_file_pattern; |
|
876 | + } |
|
877 | + } |
|
878 | + } |
|
879 | + |
|
880 | + if ($regex = $this->is_excluded_regex($file)) { |
|
881 | + return $regex; |
|
882 | + } |
|
883 | + |
|
884 | + if ($file['type'] == "file") { |
|
885 | + $check_file_diff_timestamp = $this->check_file_diff_time($file); |
|
886 | + if ($check_file_diff_timestamp) { |
|
887 | + return $check_file_diff_timestamp; |
|
888 | + } |
|
889 | + } |
|
890 | + |
|
891 | + return false; |
|
892 | + } |
|
750 | 893 | |
751 | - /** |
|
752 | - * @param string $tmp_file |
|
753 | - */ |
|
754 | - public function estimate_reading_time($tmp_file) |
|
755 | - { |
|
756 | - $this->logger->debug(sprintf(("Estimating file system reading time"))); |
|
757 | - |
|
758 | - $start_time = microtime(true); |
|
759 | - |
|
760 | - if ($this->tmp_filesystem->has($tmp_file)) { |
|
761 | - $this->tmp_filesystem->read($tmp_file); |
|
762 | - } |
|
763 | - |
|
764 | - $end_time = microtime(true) - $start_time; |
|
765 | - |
|
766 | - return $end_time; |
|
767 | - |
|
768 | - } |
|
769 | - |
|
770 | - public function process_backup_name($name = "", $max_length = 100) |
|
771 | - { |
|
772 | - if (!$name) { |
|
773 | - $name = $this->xcloner_settings->get_default_backup_name(); |
|
774 | - } |
|
775 | - |
|
776 | - foreach ($this->backup_name_tags as $tag) { |
|
777 | - if ($tag == '[time]') { |
|
778 | - $name = str_replace($tag, date("Y-m-d_H-i"), $name); |
|
779 | - } elseif ($tag == '[hostname]') { |
|
780 | - $name = str_replace($tag, gethostname(), $name); |
|
781 | - } elseif ($tag == '[domain]') { |
|
782 | - $domain = parse_url(admin_url(), PHP_URL_HOST); |
|
783 | - $name = str_replace($tag, $domain, $name); |
|
784 | - } |
|
785 | - } |
|
786 | - |
|
787 | - if ($max_length) { |
|
788 | - $name = substr($name, 0, $max_length); |
|
789 | - } |
|
790 | - |
|
791 | - return $name; |
|
792 | - } |
|
793 | - |
|
794 | - /** |
|
795 | - * @param string $field |
|
796 | - */ |
|
797 | - public function sort_by(&$array, $field, $direction = 'asc') |
|
798 | - { |
|
799 | - if (strtolower($direction) == "desc" || $direction == SORT_DESC) { |
|
800 | - $direction = SORT_DESC; |
|
801 | - } else { |
|
802 | - $direction = SORT_ASC; |
|
803 | - } |
|
804 | - |
|
805 | - $array = $this->array_orderby($array, $field, $direction); |
|
806 | - |
|
807 | - return true; |
|
808 | - } |
|
809 | - |
|
810 | - private function array_orderby() |
|
811 | - { |
|
812 | - $args = func_get_args(); |
|
813 | - $data = array_shift($args); |
|
814 | - |
|
815 | - foreach ($args as $n => $field) { |
|
816 | - if (is_string($field)) { |
|
817 | - $tmp = array(); |
|
818 | - foreach ($data as $key => $row) { |
|
819 | - if (is_array($row)) { |
|
820 | - $tmp[$key] = $row[$field]; |
|
821 | - } else { |
|
822 | - $tmp[$key] = $row->$field; |
|
823 | - } |
|
824 | - } |
|
825 | - $args[$n] = $tmp; |
|
826 | - } |
|
827 | - } |
|
828 | - $args[] = &$data; |
|
829 | - |
|
830 | - call_user_func_array('array_multisort', $args); |
|
831 | - |
|
832 | - return array_pop($args); |
|
833 | - } |
|
834 | - |
|
835 | - private function check_file_diff_time($file) |
|
836 | - { |
|
837 | - if ($this->get_diff_timestamp_start() != "") { |
|
838 | - $fileMeta = $this->getMetadataFull("start_adapter", $file['path']); |
|
839 | - $timestamp = $fileMeta->getMTime(); |
|
840 | - if ($timestamp < $fileMeta->getCTime()) { |
|
841 | - $timestamp = $fileMeta->getCTime(); |
|
842 | - } |
|
843 | - |
|
844 | - if ($timestamp <= $this->get_diff_timestamp_start()) { |
|
845 | - return " file DIFF timestamp ".$timestamp." < ".$this->diff_timestamp_start; |
|
846 | - } |
|
847 | - } |
|
848 | - |
|
849 | - return false; |
|
850 | - } |
|
851 | - |
|
852 | - public function is_excluded($file) |
|
853 | - { |
|
854 | - $this->logger->debug(sprintf(("Checking if %s is excluded"), $file['path'])); |
|
855 | - |
|
856 | - if ($xcloner_exclude_files_larger_than_mb = $this->xcloner_settings->get_xcloner_option('xcloner_exclude_files_larger_than_mb')) { |
|
857 | - if (isset($file['size']) && $file['size'] > $this->calc_to_bytes($xcloner_exclude_files_larger_than_mb)) { |
|
858 | - return "> ".$xcloner_exclude_files_larger_than_mb."MB"; |
|
859 | - } |
|
860 | - } |
|
861 | - |
|
862 | - if (!is_array($this->excluded_files) || !sizeof($this->excluded_files)) { |
|
863 | - $this->set_excluded_files(); |
|
864 | - } |
|
865 | - |
|
866 | - if (is_array($this->excluded_files)) { |
|
867 | - foreach ($this->excluded_files as $excluded_file_pattern) { |
|
868 | - if ($excluded_file_pattern == "/") { |
|
869 | - $needle = "$"; |
|
870 | - } else { |
|
871 | - $needle = "$".$excluded_file_pattern; |
|
872 | - } |
|
873 | - |
|
874 | - if (strstr("$".$file['path'], $needle)) { |
|
875 | - return $excluded_file_pattern; |
|
876 | - } |
|
877 | - } |
|
878 | - } |
|
879 | - |
|
880 | - if ($regex = $this->is_excluded_regex($file)) { |
|
881 | - return $regex; |
|
882 | - } |
|
883 | - |
|
884 | - if ($file['type'] == "file") { |
|
885 | - $check_file_diff_timestamp = $this->check_file_diff_time($file); |
|
886 | - if ($check_file_diff_timestamp) { |
|
887 | - return $check_file_diff_timestamp; |
|
888 | - } |
|
889 | - } |
|
890 | - |
|
891 | - return false; |
|
892 | - } |
|
893 | - |
|
894 | - /*REGEX examples |
|
894 | + /*REGEX examples |
|
895 | 895 | * |
896 | 896 | * exclude all except .php file |
897 | 897 | * PATTERN: ^(.*)\.(.+)$(?<!(php)) |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | $this->storage_filesystem_append = new Filesystem($this->storage_adapter, new Config([ |
103 | 103 | 'disable_asserts' => true, |
104 | 104 | ])); |
105 | - }catch (Exception $e) { |
|
105 | + } catch (Exception $e) { |
|
106 | 106 | $this->logger->error("Filesystem Initialization Error: ".$e->getMessage()); |
107 | 107 | } |
108 | 108 | |
@@ -510,7 +510,7 @@ discard block |
||
510 | 510 | |
511 | 511 | try{ |
512 | 512 | rmdir($this->xcloner_settings->get_xcloner_tmp_path()); |
513 | - }catch(Exception $e){ |
|
513 | + } catch(Exception $e){ |
|
514 | 514 | //silent continue |
515 | 515 | } |
516 | 516 | |
@@ -649,7 +649,7 @@ discard block |
||
649 | 649 | } |
650 | 650 | } |
651 | 651 | |
652 | - }catch (Exception $e) { |
|
652 | + } catch (Exception $e) { |
|
653 | 653 | |
654 | 654 | $this->logger->error($e->getMessage()); |
655 | 655 | |
@@ -676,7 +676,7 @@ discard block |
||
676 | 676 | |
677 | 677 | $this->tmp_filesystem->delete($tmp_file); |
678 | 678 | |
679 | - }catch (Exception $e) { |
|
679 | + } catch (Exception $e) { |
|
680 | 680 | |
681 | 681 | $this->logger->error($e->getMessage()); |
682 | 682 | |
@@ -714,9 +714,11 @@ discard block |
||
714 | 714 | foreach ($_backup_files_list as $file) { |
715 | 715 | //processing rule folder capacity |
716 | 716 | if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit') && |
717 | - $_storage_size >= ($set_storage_limit = 1024 * 1024 * $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit'))) //bytes |
|
717 | + $_storage_size >= ($set_storage_limit = 1024 * 1024 * $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit'))) { |
|
718 | + //bytes |
|
718 | 719 | { |
719 | 720 | $this->storage_filesystem->delete($file['path']); |
721 | + } |
|
720 | 722 | $_storage_size -= $file['size']; |
721 | 723 | $this->logger->info("Deleting backup ".$file['path']." matching rule", array( |
722 | 724 | "STORAGE SIZE LIMIT", |
@@ -983,7 +985,7 @@ discard block |
||
983 | 985 | if ($file['type'] == "dir") { |
984 | 986 | try { |
985 | 987 | $this->tmp_filesystem_append->write($this->get_temp_dir_handler(), $file['path']."\n"); |
986 | - }catch (Exception $e) { |
|
988 | + } catch (Exception $e) { |
|
987 | 989 | $this->logger->error($e->getMessage()); |
988 | 990 | } |
989 | 991 | } |
@@ -1009,7 +1011,7 @@ discard block |
||
1009 | 1011 | |
1010 | 1012 | $this->tmp_filesystem_append->write($this->get_included_files_handler(), $line); |
1011 | 1013 | |
1012 | - }catch (Exception $e) { |
|
1014 | + } catch (Exception $e) { |
|
1013 | 1015 | |
1014 | 1016 | $this->logger->error($e->getMessage()); |
1015 | 1017 | } |
@@ -489,7 +489,7 @@ |
||
489 | 489 | |
490 | 490 | try { |
491 | 491 | $this->xcloner_scheduler->xcloner_scheduler_callback(0, $schedule); |
492 | - }catch (Exception $e) { |
|
492 | + } catch (Exception $e) { |
|
493 | 493 | $this->get_xcloner_logger()->error($e->getMessage()); |
494 | 494 | } |
495 | 495 |
@@ -1324,18 +1324,18 @@ |
||
1324 | 1324 | if (file_exists($tmp_file)) { |
1325 | 1325 | header('Content-Description: File Transfer'); |
1326 | 1326 | header('Content-Type: application/octet-stream'); |
1327 | - header('Content-Disposition: attachment; filename="' . basename($tmp_file) . '"'); |
|
1327 | + header('Content-Disposition: attachment; filename="'.basename($tmp_file).'"'); |
|
1328 | 1328 | header('Expires: 0'); |
1329 | 1329 | header('Cache-Control: must-revalidate'); |
1330 | 1330 | header('Pragma: public'); |
1331 | - header('Content-Length: ' . filesize($tmp_file)); |
|
1331 | + header('Content-Length: '.filesize($tmp_file)); |
|
1332 | 1332 | readfile($tmp_file); |
1333 | 1333 | |
1334 | 1334 | } |
1335 | 1335 | |
1336 | 1336 | try { |
1337 | 1337 | unlink($tmp_file); |
1338 | - }catch(Exception $e) { |
|
1338 | + }catch (Exception $e) { |
|
1339 | 1339 | //We are not interested in the error here |
1340 | 1340 | } |
1341 | 1341 |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | try { |
138 | 138 | $this->xcloner_database->init($data); |
139 | 139 | |
140 | - }catch (Exception $e) { |
|
140 | + } catch (Exception $e) { |
|
141 | 141 | |
142 | 142 | $this->send_response($e->getMessage()); |
143 | 143 | $this->logger->error($e->getMessage()); |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | try { |
313 | 313 | $return = $this->archive_system->start_incremental_backup($this->form_params['backup_params'], |
314 | 314 | $this->form_params['extra'], $init); |
315 | - }catch (Exception $e) { |
|
315 | + } catch (Exception $e) { |
|
316 | 316 | $return = array(); |
317 | 317 | $return['error'] = true; |
318 | 318 | $return['status'] = 500; |
@@ -339,7 +339,7 @@ discard block |
||
339 | 339 | $additional['lines_total'] = $return['extra']['lines_total']; |
340 | 340 | $this->archive_system->send_notification($to, $from, $subject, $return['extra']['backup_parent'], |
341 | 341 | $this->form_params, "", $additional); |
342 | - }catch (Exception $e) { |
|
342 | + } catch (Exception $e) { |
|
343 | 343 | $this->logger->error($e->getMessage()); |
344 | 344 | } |
345 | 345 | } |
@@ -522,7 +522,7 @@ discard block |
||
522 | 522 | |
523 | 523 | try { |
524 | 524 | $files = $this->xcloner_file_system->list_directory($folder); |
525 | - }catch (Exception $e) { |
|
525 | + } catch (Exception $e) { |
|
526 | 526 | |
527 | 527 | print $e->getMessage(); |
528 | 528 | $this->logger->error($e->getMessage()); |
@@ -586,7 +586,7 @@ discard block |
||
586 | 586 | if ($database == "#") { |
587 | 587 | try { |
588 | 588 | $return = $this->xcloner_database->get_all_databases(); |
589 | - }catch (Exception $e) { |
|
589 | + } catch (Exception $e) { |
|
590 | 590 | $this->logger->error($e->getMessage()); |
591 | 591 | } |
592 | 592 | |
@@ -618,7 +618,7 @@ discard block |
||
618 | 618 | |
619 | 619 | try { |
620 | 620 | $return = $this->xcloner_database->list_tables($database, "", 1); |
621 | - }catch (Exception $e) { |
|
621 | + } catch (Exception $e) { |
|
622 | 622 | $this->logger->error($e->getMessage()); |
623 | 623 | } |
624 | 624 | |
@@ -848,7 +848,7 @@ discard block |
||
848 | 848 | $this->logger->info($iv); |
849 | 849 | $return = array_merge($return, |
850 | 850 | $this->xcloner_encryption->encrypt_file($backup_file, "", "", $start, base64_decode($iv))); |
851 | - }catch (\Exception $e) { |
|
851 | + } catch (\Exception $e) { |
|
852 | 852 | $return['error'] = true; |
853 | 853 | $return['message'] = $e->getMessage(); |
854 | 854 | $return['error_message'] = $e->getMessage(); |
@@ -905,7 +905,7 @@ discard block |
||
905 | 905 | try { |
906 | 906 | $return = array_merge($return, |
907 | 907 | $this->xcloner_encryption->decrypt_file($backup_file, "", $decryption_key, $start, base64_decode($iv))); |
908 | - }catch (\Exception $e) { |
|
908 | + } catch (\Exception $e) { |
|
909 | 909 | $return['error'] = true; |
910 | 910 | $return['message'] = $e->getMessage(); |
911 | 911 | } |
@@ -1009,7 +1009,8 @@ discard block |
||
1009 | 1009 | title="<?php echo __('Backup Decryption', 'xcloner-backup-and-restore') ?>"> |
1010 | 1010 | <i class="material-icons">enhanced_encryption</i> |
1011 | 1011 | </a> |
1012 | - <?php else: ?> |
|
1012 | + <?php else { |
|
1013 | + : ?> |
|
1013 | 1014 | <a href="#<?php echo $child[0] ?>" class="list-backup-content" |
1014 | 1015 | title="<?php echo __('List Backup Content', |
1015 | 1016 | 'xcloner-backup-and-restore') ?>"><i |
@@ -1028,7 +1029,9 @@ discard block |
||
1028 | 1029 | class="material-icons">file_upload</i></a> |
1029 | 1030 | <?php endif ?> |
1030 | 1031 | </li> |
1031 | - <?php endforeach; ?> |
|
1032 | + <?php endforeach; |
|
1033 | +} |
|
1034 | +?> |
|
1032 | 1035 | </ul> |
1033 | 1036 | <?php endif; ?> |
1034 | 1037 | <?php |
@@ -1036,11 +1039,12 @@ discard block |
||
1036 | 1039 | ob_end_clean(); |
1037 | 1040 | ?> |
1038 | 1041 | <?php ob_start(); ?> |
1039 | - <?php if (isset($file_info['timestamp'])) |
|
1042 | + <?php if (isset($file_info['timestamp'])) { |
|
1040 | 1043 | echo date("Y-m-d H:i", $file_info['timestamp']) |
1041 | 1044 | ?> |
1042 | 1045 | <?php |
1043 | 1046 | $return['data'][$i][] = ob_get_contents(); |
1047 | +} |
|
1044 | 1048 | ob_end_clean(); |
1045 | 1049 | ?> |
1046 | 1050 | |
@@ -1065,15 +1069,17 @@ discard block |
||
1065 | 1069 | <?php endif ?> |
1066 | 1070 | <?php |
1067 | 1071 | $basename = $file_info['basename']; |
1068 | - if (isset($file_info['childs']) and sizeof($file_info['childs'])) |
|
1069 | - $basename = $file_info['childs'][0][0]; |
|
1072 | + if (isset($file_info['childs']) and sizeof($file_info['childs'])) { |
|
1073 | + $basename = $file_info['childs'][0][0]; |
|
1074 | + } |
|
1070 | 1075 | ?> |
1071 | 1076 | <?php if ($this->xcloner_encryption->is_encrypted_file($basename)) :?> |
1072 | 1077 | <a href="#<?php echo $file_info['basename'] ?>" class="backup-decryption" |
1073 | 1078 | title="<?php echo __('Backup Decryption', 'xcloner-backup-and-restore') ?>"> |
1074 | 1079 | <i class="material-icons">enhanced_encryption</i> |
1075 | 1080 | </a> |
1076 | - <?php else: ?> |
|
1081 | + <?php else { |
|
1082 | + : ?> |
|
1077 | 1083 | <a href="#<?php echo $file_info['basename'] ?>" class="list-backup-content" |
1078 | 1084 | title="<?php echo __('List Backup Content', 'xcloner-backup-and-restore') ?>"><i |
1079 | 1085 | class="material-icons">folder_open</i></a> |
@@ -1083,7 +1089,9 @@ discard block |
||
1083 | 1089 | <i class="material-icons">no_encryption</i> |
1084 | 1090 | </a> |
1085 | 1091 | <?php endif?> |
1086 | - <?php endif; ?> |
|
1092 | + <?php endif; |
|
1093 | +} |
|
1094 | +?> |
|
1087 | 1095 | |
1088 | 1096 | <a href="#<?php echo $file_info['basename'] ?>" class="delete" |
1089 | 1097 | title="<?php echo __('Delete Backup', 'xcloner-backup-and-restore') ?>"> |
@@ -1137,7 +1145,7 @@ discard block |
||
1137 | 1145 | $tar->open($this->xcloner_settings->get_xcloner_store_path().DS.$backup_file, $start); |
1138 | 1146 | |
1139 | 1147 | $data = $tar->contents(get_option('xcloner_files_to_process_per_request')); |
1140 | - }catch (Exception $e) { |
|
1148 | + } catch (Exception $e) { |
|
1141 | 1149 | $return['error'] = true; |
1142 | 1150 | $return['message'] = $e->getMessage(); |
1143 | 1151 | $this->send_response($return, 0); |
@@ -1200,7 +1208,7 @@ discard block |
||
1200 | 1208 | "copy_backup_remote_to_local" |
1201 | 1209 | ), array($backup_file, $storage_type)); |
1202 | 1210 | } |
1203 | - }catch (Exception $e) { |
|
1211 | + } catch (Exception $e) { |
|
1204 | 1212 | |
1205 | 1213 | $return['error'] = 1; |
1206 | 1214 | $return['message'] = $e->getMessage(); |
@@ -1239,7 +1247,7 @@ discard block |
||
1239 | 1247 | "upload_backup_to_storage" |
1240 | 1248 | ), array($backup_file, $storage_type)); |
1241 | 1249 | } |
1242 | - }catch (Exception $e) { |
|
1250 | + } catch (Exception $e) { |
|
1243 | 1251 | |
1244 | 1252 | $return['error'] = 1; |
1245 | 1253 | $return['message'] = $e->getMessage(); |
@@ -1335,7 +1343,7 @@ discard block |
||
1335 | 1343 | |
1336 | 1344 | try { |
1337 | 1345 | unlink($tmp_file); |
1338 | - }catch(Exception $e) { |
|
1346 | + } catch(Exception $e) { |
|
1339 | 1347 | //We are not interested in the error here |
1340 | 1348 | } |
1341 | 1349 | |
@@ -1430,7 +1438,7 @@ discard block |
||
1430 | 1438 | $xcloner_file_transfer->set_target($target_url); |
1431 | 1439 | $return['start'] = $xcloner_file_transfer->transfer_file($file, $start, $hash); |
1432 | 1440 | |
1433 | - }catch (Exception $e) { |
|
1441 | + } catch (Exception $e) { |
|
1434 | 1442 | |
1435 | 1443 | $return = array(); |
1436 | 1444 | $return['error'] = true; |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | foreach ($list as $schedule) { |
104 | 104 | $hook = 'xcloner_scheduler_'.$schedule->id; |
105 | 105 | |
106 | - if($timestamp = wp_next_scheduled($hook, array($schedule->id))) { |
|
106 | + if ($timestamp = wp_next_scheduled($hook, array($schedule->id))) { |
|
107 | 107 | wp_unschedule_event($timestamp, $hook, array($schedule->id)); |
108 | 108 | } |
109 | 109 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | } |
128 | 128 | |
129 | 129 | } elseif (!$schedule->status) { |
130 | - if($timestamp = wp_next_scheduled($hook, array($schedule->id))) { |
|
130 | + if ($timestamp = wp_next_scheduled($hook, array($schedule->id))) { |
|
131 | 131 | wp_unschedule_event($timestamp, $hook, array($schedule->id)); |
132 | 132 | } |
133 | 133 | } |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | $schedule = $this->get_schedule_by_id_object($id); |
140 | 140 | $hook = 'xcloner_scheduler_'.$schedule->id; |
141 | 141 | |
142 | - if( $timestamp = wp_next_scheduled($hook, array($schedule->id))) { |
|
142 | + if ($timestamp = wp_next_scheduled($hook, array($schedule->id))) { |
|
143 | 143 | wp_unschedule_event($timestamp, $hook, array($schedule->id)); |
144 | 144 | } |
145 | 145 | |
@@ -156,9 +156,9 @@ discard block |
||
156 | 156 | |
157 | 157 | public function disable_single_cron($schedule_id) { |
158 | 158 | $schedule = array(); |
159 | - $hook = 'xcloner_scheduler_'.$schedule_id; |
|
159 | + $hook = 'xcloner_scheduler_'.$schedule_id; |
|
160 | 160 | |
161 | - if($timestamp = wp_next_scheduled($hook, array($schedule_id))) { |
|
161 | + if ($timestamp = wp_next_scheduled($hook, array($schedule_id))) { |
|
162 | 162 | wp_unschedule_event($timestamp, $hook, array($schedule_id)); |
163 | 163 | } |
164 | 164 | |
@@ -215,13 +215,13 @@ discard block |
||
215 | 215 | return $update; |
216 | 216 | } |
217 | 217 | |
218 | - private function _xcloner_scheduler_callback( $id, $schedule ) { |
|
219 | - set_time_limit( 0 ); |
|
218 | + private function _xcloner_scheduler_callback($id, $schedule) { |
|
219 | + set_time_limit(0); |
|
220 | 220 | |
221 | 221 | |
222 | 222 | $xcloner = new XCloner(); |
223 | 223 | $xcloner->init(); |
224 | - $this->set_xcloner_container( $xcloner ); |
|
224 | + $this->set_xcloner_container($xcloner); |
|
225 | 225 | $return_encrypted = array(); |
226 | 226 | $return = array(); |
227 | 227 | $additional = array(); |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | |
369 | 369 | $this->archive_system->send_notification($to, $from, $subject, $return['extra']['backup_parent'], $schedule, "", $additional); |
370 | 370 | |
371 | - }catch (Exception $e) { |
|
371 | + } catch (Exception $e) { |
|
372 | 372 | $this->logger->error($e->getMessage()); |
373 | 373 | } |
374 | 374 | } |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | } |
403 | 403 | $this->_xcloner_scheduler_callback($id, $schedule); |
404 | 404 | |
405 | - }catch (Exception $e) { |
|
405 | + } catch (Exception $e) { |
|
406 | 406 | |
407 | 407 | //send email to site admin if email notification is not set in the scheduler |
408 | 408 | if (!isset($schedule['backup_params']->email_notification) || !$schedule['backup_params']->email_notification) { |
@@ -62,7 +62,7 @@ |
||
62 | 62 | */ |
63 | 63 | public function is_encrypted_file($filename) { |
64 | 64 | $fp = fopen($this->get_xcloner_path().$filename, 'r'); |
65 | - if($fp) { |
|
65 | + if ($fp) { |
|
66 | 66 | $encryption_length = fread($fp, 16); |
67 | 67 | fclose($fp); |
68 | 68 | if (is_numeric($encryption_length)) { |
@@ -166,26 +166,26 @@ |
||
166 | 166 | * Xcloner_Remote_Storage constructor. |
167 | 167 | * @param Xcloner $xcloner_container |
168 | 168 | */ |
169 | - public function __construct( Xcloner $xcloner_container ) { |
|
169 | + public function __construct(Xcloner $xcloner_container) { |
|
170 | 170 | $this->xcloner_sanitization = $xcloner_container->get_xcloner_sanitization(); |
171 | 171 | $this->xcloner_file_system = $xcloner_container->get_xcloner_filesystem(); |
172 | - $this->logger = $xcloner_container->get_xcloner_logger()->withName( "xcloner_remote_storage" ); |
|
172 | + $this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_remote_storage"); |
|
173 | 173 | $this->xcloner = $xcloner_container; |
174 | 174 | |
175 | - foreach($this->storage_fields as $main_key=>$array){ |
|
175 | + foreach ($this->storage_fields as $main_key=>$array) { |
|
176 | 176 | |
177 | - if(is_array($array)) { |
|
177 | + if (is_array($array)) { |
|
178 | 178 | foreach ($array as $key => $type) { |
179 | 179 | |
180 | - if( $type == "raw") { |
|
181 | - add_filter("pre_update_option_" . $this->storage_fields['option_prefix'] . $key, |
|
182 | - function ($value) { |
|
180 | + if ($type == "raw") { |
|
181 | + add_filter("pre_update_option_".$this->storage_fields['option_prefix'].$key, |
|
182 | + function($value) { |
|
183 | 183 | |
184 | 184 | return $this->simple_crypt($value, 'e'); |
185 | 185 | |
186 | 186 | }, 10, 1); |
187 | 187 | |
188 | - add_filter("option_" . $this->storage_fields['option_prefix'] . $key, function ($value) { |
|
188 | + add_filter("option_".$this->storage_fields['option_prefix'].$key, function($value) { |
|
189 | 189 | |
190 | 190 | return $this->simple_crypt($value, 'd'); |
191 | 191 |
@@ -217,8 +217,7 @@ discard block |
||
217 | 217 | |
218 | 218 | if ($action == 'e' && function_exists('openssl_encrypt')) { |
219 | 219 | $output = base64_encode(openssl_encrypt($string, $encrypt_method, $key, 0, $iv)); |
220 | - } |
|
221 | - else if ($action == 'd' && function_exists('openssl_decrypt') && base64_decode($string)) { |
|
220 | + } else if ($action == 'd' && function_exists('openssl_decrypt') && base64_decode($string)) { |
|
222 | 221 | $decrypt = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv); |
223 | 222 | if ($decrypt) { |
224 | 223 | //we check if decrypt was succesful |
@@ -280,7 +279,7 @@ discard block |
||
280 | 279 | $this->verify_filesystem($action); |
281 | 280 | $this->xcloner->trigger_message(__("%s connection is valid.", 'xcloner-backup-and-restore'), "success", $this->storage_fields[$action]['text']); |
282 | 281 | $this->logger->debug(sprintf("Connection to remote storage %s is valid", strtoupper($action))); |
283 | - }catch (Exception $e) { |
|
282 | + } catch (Exception $e) { |
|
284 | 283 | $this->xcloner->trigger_message("%s connection error: ".$e->getMessage(), "error", $this->storage_fields[$action]['text']); |
285 | 284 | } |
286 | 285 | } |