PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 2.1.2
Backup Migration v2.1.2
2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / includes / zipper / zipping.php
backup-backup / includes / zipper Last commit date
src 3 months ago zipping.php 3 months ago
zipping.php
354 lines
1 <?php
2
3 // Namespace
4 namespace BMI\Plugin\Zipper;
5
6 // Use
7 use BMI\Plugin\Backup_Migration_Plugin as BMP;
8 use BMI\Plugin\BMI_Logger as Logger;
9 use BMI\Plugin\Progress\BMI_ZipProgress as Progress;
10 use BMI\Plugin\BMI_Zip_Explorer as ZipExplorer;
11
12 // Exit on direct access
13 if (!defined('ABSPATH')) {
14 exit;
15 }
16
17 /**
18 * BMI_Zipper
19 */
20 class BMI_Zipper {
21 public function makeZIP($files, $output, $name, &$zip_progress, $cron = false) {
22
23 // Verbose
24 Logger::log(__("Creating backup ", 'backup-backup'));
25 Logger::log(__("Found ", 'backup-backup') . sizeof($files) . __(" files to backup.", 'backup-backup'));
26
27 // Require Universal Zip Library
28 require_once BMI_INCLUDES . '/zipper/src/zip.php';
29
30 // Start microtime for ZIP Process
31 $start = microtime(true);
32
33 // Logs
34 $zip_progress->log(__("Preparing map of files...", 'backup-backup'), 'step');
35
36 // Try to catch error
37 try {
38
39 // Create new ZIP
40 $zip = new Zip();
41 $zip->zip_start($output, $files, $name, $zip_progress, $start);
42
43 // Logs
44 $zip_progress->log(__("Files prepared.", 'backup-backup'), 'success');
45 $zip_progress->log(__("Starting compression process...", 'backup-backup'), 'info');
46
47 // Close ZIP and Save
48 $lala = $zip->zip_end(2, $cron);
49 if (!$lala) {
50 $zip_progress->log(__("Something went wrong (pclzip) – removing backup files...", 'backup-backup'), 'error');
51
52 return false;
53 }
54
55 return $lala;
56 } catch (\Throwable $e) {
57
58 // Error print
59 $zip_progress->log(__("Reverting backup, removing file...", 'backup-backup'), 'step');
60 $zip_progress->log(__("There was an error during backup...", 'backup-backup'), 'error');
61 $zip_progress->log($e->getMessage(), 'error');
62
63 return false;
64 } catch (\Exception $e) {
65
66 // Error print
67 $zip_progress->log(__("Reverting backup, removing file...", 'backup-backup'), 'step');
68 $zip_progress->log(__("There was an error during backup...", 'backup-backup'), 'error');
69 $zip_progress->log($e->getMessage(), 'error');
70
71 return false;
72 }
73
74 return true;
75 }
76
77 public function getZipFileContent($zipname, $filename) {
78 if (class_exists('ZipArchive') || class_exists('\ZipArchive')) {
79 $zip = new \ZipArchive();
80
81 if ($zip->open($zipname) === true) {
82 if ($content = $zip->getFromName($filename)) {
83 return json_decode($content);
84 } else {
85 return false;
86 }
87 } else {
88 return false;
89 }
90 } else {
91 if (!class_exists('PclZip')) {
92 if (!defined('PCLZIP_TEMPORARY_DIR')) {
93 $bmi_tmp_dir = BMI_TMP;
94 if (!file_exists($bmi_tmp_dir)) {
95 @mkdir($bmi_tmp_dir, 0775, true);
96 }
97 define('PCLZIP_TEMPORARY_DIR', $bmi_tmp_dir . DIRECTORY_SEPARATOR . 'bmi-');
98 }
99 if (defined('BMI_PRO_PCLZIP') && file_exists(BMI_PRO_PCLZIP)) {
100 require_once BMI_PRO_PCLZIP;
101 } else {
102 require_once trailingslashit(ABSPATH) . 'wp-admin/includes/class-pclzip.php';
103 }
104 }
105 $lib = new \PclZip($zipname);
106 $content = $lib->extract(PCLZIP_OPT_BY_NAME, $filename, PCLZIP_OPT_EXTRACT_AS_STRING);
107 if (isset($content[0]) && isset($content[0]['content'])) {
108 return json_decode($content[0]['content']);
109 } else {
110 return false;
111 }
112 }
113 }
114
115 public function getZipContentList($zippath, $savepath) {
116
117 if (class_exists('ZipArchive') || class_exists('\ZipArchive')) {
118
119 $zip = new \ZipArchive();
120 $zip->open($zippath);
121
122 if (!isset($zip->numFiles) || $zip->numFiles == 0 || $zip->numFiles === false) {
123 $zip->close();
124 return false;
125 }
126
127 $tmpf = fopen($savepath, 'a+');
128 $totalAmount = $zip->numFiles;
129
130 for ($i = 0; $i < $zip->numFiles; ++$i) {
131
132 $stat = $zip->statIndex($i);
133 fwrite($tmpf, $stat['name'] . "\n");
134 unset($stat);
135
136 }
137
138 fclose($tmpf);
139 $zip->close();
140
141 return $totalAmount;
142
143 } else {
144
145 if (!class_exists('PclZip')) {
146 if (!defined('PCLZIP_TEMPORARY_DIR')) {
147 $bmi_tmp_dir = BMI_TMP;
148 if (!file_exists($bmi_tmp_dir)) {
149 @mkdir($bmi_tmp_dir, 0775, true);
150 }
151 define('PCLZIP_TEMPORARY_DIR', $bmi_tmp_dir . DIRECTORY_SEPARATOR . 'bmi-');
152 }
153 if (defined('BMI_PRO_PCLZIP') && file_exists(BMI_PRO_PCLZIP)) {
154 require_once BMI_PRO_PCLZIP;
155 } else {
156 require_once trailingslashit(ABSPATH) . 'wp-admin/includes/class-pclzip.php';
157 }
158 }
159
160 $zip = new \PclZip($zippath);
161 $list = $zip->listContent();
162 if ($list == 0) {
163 return false;
164 }
165
166 $tmpf = fopen($savepath, 'a+');
167
168 $totalAmount = sizeof($list);
169 for ($i = 0; $i < $totalAmount; ++$i) {
170
171 fwrite($tmpf, $list[$i]['filename'] . "\n");
172
173 }
174
175 fclose($tmpf);
176
177 return $totalAmount;
178
179 }
180
181 }
182
183 public function getPartsToRestore($zippath, $savepath) {
184 if (!defined('BMI_PRO_INC') || !file_exists(BMI_PRO_INC . '/zip-explorer.php')) {
185 return false;
186 }
187
188 require_once BMI_PRO_INC . '/zip-explorer.php';
189
190 $restorePartsFile = BMI_TMP . DIRECTORY_SEPARATOR . 'restore_parts.json';
191 if (!file_exists($restorePartsFile)) {
192 return false;
193 }
194
195 $restorePartsContent = json_decode(file_get_contents($restorePartsFile), true);
196 if (!$restorePartsContent || !isset($restorePartsContent['backupName'])) {
197 return false;
198 }
199
200 if ($restorePartsContent['backupName'] !== basename($zippath)) {
201 Logger::log('Ignoring restore parts file, because it does not match the backup name.');
202 @unlink($restorePartsFile);
203 return false;
204 }
205
206 $zipExplorer = new ZipExplorer($zippath);
207 $amount = 0;
208 if (isset($restorePartsContent['dirs']) && is_array($restorePartsContent['dirs'])) {
209 foreach ($restorePartsContent['dirs'] as $dir) {
210 $amount += $zipExplorer->scanDir($dir, $savepath);
211 }
212
213 }
214
215 if (isset($restorePartsContent['files']) && is_array($restorePartsContent['files'])) {
216 $amount += sizeof($restorePartsContent['files']);
217 $files = implode("\n", $restorePartsContent['files']);
218 file_put_contents($savepath, $files, FILE_APPEND);
219 }
220
221 file_put_contents($savepath, "\nbmi_backup_manifest.json", FILE_APPEND);
222
223
224 return $amount;
225 }
226
227 public function getZipFileContentPlain($zipname, $filename) {
228 if (class_exists('ZipArchive')) {
229 $zip = new \ZipArchive();
230
231 if ($zip->open($zipname) === true) {
232 if ($content = $zip->getFromName($filename)) {
233 return $content;
234 } else {
235 return false;
236 }
237 } else {
238 return false;
239 }
240 } else {
241 if (!class_exists('PclZip')) {
242 if (!defined('PCLZIP_TEMPORARY_DIR')) {
243 $bmi_tmp_dir = BMI_TMP;
244 if (!file_exists($bmi_tmp_dir)) {
245 @mkdir($bmi_tmp_dir, 0775, true);
246 }
247 define('PCLZIP_TEMPORARY_DIR', $bmi_tmp_dir . DIRECTORY_SEPARATOR . 'bmi-');
248 }
249 if (defined('BMI_PRO_PCLZIP') && file_exists(BMI_PRO_PCLZIP)) {
250 require_once BMI_PRO_PCLZIP;
251 } else {
252 require_once trailingslashit(ABSPATH) . 'wp-admin/includes/class-pclzip.php';
253 }
254 }
255 $lib = new \PclZip($zipname);
256
257 $content = $lib->extract(PCLZIP_OPT_BY_NAME, $filename, PCLZIP_OPT_EXTRACT_AS_STRING);
258 if (sizeof($content) > 0) {
259 return $content[0]['content'];
260 } else {
261 return false;
262 }
263 }
264 }
265
266 public function lock_zip($zippath, $unlock = false) {
267
268 // Require Universal Zip Library
269 require_once BMI_INCLUDES . '/zipper/src/zip.php';
270
271 try {
272
273 // Path to lock file
274 $filename = '.lock';
275
276 // Load lib
277 if (!class_exists('PclZip')) {
278 if (!defined('PCLZIP_TEMPORARY_DIR')) {
279 $bmi_tmp_dir = BMI_TMP;
280 if (!file_exists($bmi_tmp_dir)) {
281 @mkdir($bmi_tmp_dir, 0775, true);
282 }
283 define('PCLZIP_TEMPORARY_DIR', $bmi_tmp_dir . DIRECTORY_SEPARATOR . 'bmi-');
284 }
285 if (defined('BMI_PRO_PCLZIP') && file_exists(BMI_PRO_PCLZIP)) {
286 require_once BMI_PRO_PCLZIP;
287 } else {
288 require_once trailingslashit(ABSPATH) . 'wp-admin/includes/class-pclzip.php';
289 }
290 }
291 $lib = new \PclZip($zippath);
292
293 $md5_file_summary_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. 'md5summary.php';
294 if (file_exists($md5_file_summary_path)) {
295 $zip_name = basename($zippath);
296 $md5summary = file_get_contents($md5_file_summary_path);
297 $md5summary = substr($md5summary, 18, -2);
298 if (is_serialized($md5summary)) {
299 $md5summary = maybe_unserialize($md5summary);
300 }
301 }
302
303 if (isset($md5summary[$zip_name])) {
304 $md5s = $md5summary[$zip_name];
305 for ($i = 0; $i < sizeof($md5s); ++$i) {
306 $md5_file_path = BMI_BACKUPS . DIRECTORY_SEPARATOR. $md5s[$i] . '.json';
307 if (file_exists($md5_file_path)) {
308 @unlink($md5_file_path);
309 }
310 }
311 }
312
313 // Unlocking case
314 if ($unlock) {
315 if ($this->is_locked_zip($zippath)) {
316 $lib->delete(PCLZIP_OPT_BY_NAME, $filename);
317 } else {
318 return true;
319 }
320 } else {
321 if (!$this->is_locked_zip($zippath)) {
322
323 // Locking case
324 $content = json_encode(['locked' => 'true']);
325 $lib->add([[PCLZIP_ATT_FILE_NAME => $filename, PCLZIP_ATT_FILE_CONTENT => $content]]);
326 }
327 }
328
329 return true;
330 } catch (\Exception $e) {
331 Logger::error($e);
332
333 return false;
334 } catch (\Throwable $e) {
335 Logger::error($e);
336
337 return false;
338 }
339 }
340
341 public function is_locked_zip($zippath) {
342 $lock = $this->getZipFileContent($zippath, '.lock');
343 if ($lock) {
344 if ($lock->locked == 'true') {
345 return true;
346 } else {
347 return false;
348 }
349 } else {
350 return false;
351 }
352 }
353 }
354