-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathces_module_backup.php
182 lines (151 loc) · 5.01 KB
/
ces_module_backup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
if(empty($_GET['er'])) {
error_reporting(0);
} else {
ini_set('display_errors', 1);
error_reporting(E_ALL);
}
class BackupFiles {
public static $json = [];
public static $current_path = '';
public function updateJson($json_data, $current_path) {
self::$json = $json_data;
self::$current_path = $current_path;
}
public function recursive_copy($src, $dst) {
if(is_dir($src)) {
$dir = opendir($src);
@mkdir($dst, 0777, true);
while(( $file = readdir($dir)) ) {
if (( substr($file, -1) != '_' ) && ( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
$this->recursive_copy($src .'/'. $file, $dst .'/'. $file);
}
else {
$this->default_copy($src .'/'. $file, $dst .'/'. $file);
}
}
}
closedir($dir);
return;
} elseif(is_file($src)) {
$dir = substr($dst, 0, strrpos($dst, '/'));
mkdir($dir, 0777, true);
$this->default_copy($src, $dst);
} else {
die('<strong>Not found : </strong> ' . $src);
}
}
public function default_copy($source, $destination) {
$all_files_ignore = self::$json['all_files_ignore'];
$new_current_file = str_replace(self::$current_path, '', $source);
if(!in_array($new_current_file, $all_files_ignore)) {
if(strpos($source, '.ocmod.xml')) {
$prefix_destination = substr($destination, 0, strpos($destination, 'system'));
$destination = $prefix_destination . '../install.xml';
}
copy($source, $destination);
}
}
public function move_to_folder($uploadFolder, $zipFileName) {
if (rename($zipFileName, $uploadFolder . '/../' . $zipFileName)) {
echo "ZIP file moved to the 'upload' folder.";
} else {
echo "Failed to move the ZIP file to the 'upload' folder.";
}
echo "<br>";
}
public function zipFiles($folderToZip, $zipFileName, $current_path) {
// Create a new ZipArchive object
$zip = new ZipArchive();
// Open the ZIP file for writing
if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
// Add files and subdirectories to the ZIP file
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($folderToZip),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($files as $file) {
$file = realpath($file);
$localName = str_replace($current_path, '', $file);
if (is_dir($file)) {
// Do not add directories
continue;
} elseif (is_file($file)) {
$zip->addFile($file, $localName);
}
}
if (is_file($current_path . 'install.xml')) {
$localName = 'install.xml';
$zip->addFile($current_path . 'install.xml', $localName);
}
// Close the ZIP file
$zip->close();
echo "ZIP archive created successfully!";
} else {
echo "Failed to create ZIP archive.";
}
echo "<br>";
}
}
$backupFiles = new BackupFiles();
$done = '';
$current_path = getcwd() . '/';
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
try {
$json = file_get_contents($_FILES['file']['tmp_name']);
} catch(Exception $e) {
print_r($e->getMessage());
die;
}
$json = json_decode($json, true);
$backupFiles->updateJson($json, $current_path);
$folder_name = $json['default_copy_path'];
$json['default_copy_path'] .= '/upload';
$fix_dst = '';
if(isset($json['default_copy_path'])) {
$json['default_copy_path'] = str_replace("//", "/", $json['default_copy_path']);
if(strpos($json['default_copy_path'], 'home')) {
$fix_dst = $json['default_copy_path'] . '/';
} else {
$fix_dst = $current_path . $json['default_copy_path'] . '/';
}
} else {
$fix_dst = $current_path;
}
require_once $current_path . $json['default_path'] . '/admin/config.php';
require_once $current_path . $json['default_path'] . '/config.php';
$old_umask = umask();
umask(000);
$current_new_path = str_replace("//", "/", $current_path . $json['default_path'] . '/');
foreach ($json['all_files'] as $key => $folder) {
$backupFiles->recursive_copy($current_new_path . $folder, $fix_dst . $folder);
}
foreach ($json['single_file'] as $key => $file) {
$backupFiles->recursive_copy($current_new_path . $file, $fix_dst . $file);
}
echo "Zip Files<br>";
$zipFileName = 'upload.ocmod.zip';
$backupFiles->zipFiles($fix_dst, $zipFileName, $current_path . $folder_name . '/');
$backupFiles->move_to_folder($fix_dst, $zipFileName);
umask($old_umask);
$done = 'success';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>CES Module Backup</title>
</head>
<body>
<?php if($done == 'success') { ?>
<div style="display: block; width: 100%; background-color: lightgreen; font-size: 24px; text-align: center">Success</div>
<?php } ?>
<form action="" method="post" enctype="multipart/form-data">
<label>Upload Schema File</label> <input name="file" type="file" />
<br />
<br />
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>