-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessAssetsBackups.module
793 lines (681 loc) · 24.9 KB
/
ProcessAssetsBackups.module
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
<?php
namespace ProcessWire;
use ReflectionClass;
use ZipArchive;
/**
* ProcessWire Assets/Files Backup and Restore
*
* License: MPL v2
*
* For ProcessWire 3.x
* Copyright (C) 2019 by Michael Jaros
*
* https://processwire.com
*
*/
class ProcessAssetsBackups extends Process {
/**
* Minimum required version for this module
*
*/
const minVersion = '3.0.62';
/**
* Shared translation labels
*
*/
protected $labels = array();
/**
* This is an optional initialization function called before any execute functions.
*
*/
public function init() {
parent::init(); // required
$this->labels = array(
"downloadZIP" => $this->_('ZIP file'),
"backup" => $this->_('Backup'),
"delete" => $this->_('Delete'),
"restore" => $this->_('Restore'),
"upload" => $this->_('Upload'),
"description" => $this->_('Description'),
"valid" => $this->_('Valid?'),
"time" => $this->_('Date/Time'),
"user" => $this->_('Exported by'),
"size" => $this->_('File size'),
"pathname" => $this->_('Filename'),
);
}
/**
* Returns the class name without namespace
*
* @return string
*/
protected function getClassShortName() {
return (new ReflectionClass($this))->getShortName();
}
/**
* Returns the fully-qualified path where backups are stored
*
* @return string
*
*/
protected function getBackupPath() {
$backupPath = $this->wire('config')->paths->root . "site/assets/backups/" . $this->getClassShortName();
return $backupPath;
}
/**
* Returns the fully-qualified assets/files path
*
* @return string
*
*/
protected function getAssetsFilesPath() {
return $this->wire('config')->paths->root . "site/assets/files";
}
/**
* Get array of all backup files
*
* @param string $onlyID Specify ID of file if you only want to get a specific backup file
* @return array Array of file info arrays indexed by file 'id'
*
*/
protected function getBackupFiles($onlyID = '') {
$files = array();
$n = 0;
foreach (self::getDirectoryEntries($this->getBackupPath()) as $file) {
$fullFilePath = $this->getBackupPath() . "/" . $file;
if (is_dir($file)) {
continue;
}
$id = "$n:$file";
if ($onlyID && $id != $onlyID) {
continue;
}
$info = self::getFileInfo($fullFilePath, $id);
$files[$id] = $info;
$n++;
}
return $files;
}
/**
* Get actions for given $file array
*
* @param array $file File info array
* @return array
*
*/
protected function getFileActions(array $file) {
$url = $this->wire('page')->url();
$actions = array(
'downloadZIP' => array(
'href' => $url . "download/?id=$file[id]&zip=1",
'label' => $this->labels['downloadZIP'],
'icon' => 'download',
'secondary' => false,
'head' => true,
),
'restore ' => array(
'href' => $url . "restore/?id=$file[id]",
'label' => $this->labels['restore'],
'icon' => 'life-ring',
'secondary' => true,
'head' => false,
),
'delete' => array(
'href' => $url . "delete/?id=$file[id]",
'label' => $this->labels['delete'],
'icon' => 'trash',
'secondary' => true,
'head' => false,
),
);
if (!class_exists("\\ZipArchive"))
unset($actions['downloadZIP']);
return $actions;
}
/**
* Get information about file requested in URL via $_GET['id']
*
* @return mixed
* @throws WireException
*
*/
protected function getFile() {
$id = $this->input->get('id');
if (is_null($id))
throw new WireException("No file specified");
$files = $this->getBackupFiles();
if (!isset($files[$id]))
throw new WireException("Unrecognized file");
$file = $files[$id];
$basename = basename($file['pathname'], '.zip');
$file['zip'] = $basename;
return $file;
}
/**
* Process submitted restore form, restoring the given backup file to site/assets
*
* @param type $restoreFileName
* @return boolean
*/
protected function processRestore($restoreFileName) {
$assetsFilesPath = $this->getAssetsFilesPath();
$randomDirectoryName = uniqid();
$restoreFilePath = $this->getBackupPath() . "/" . $restoreFileName;
$restoringAssetsFilesDirectoryTemporaryPath = $assetsFilesPath . "." . $randomDirectoryName . ".restoring";
$existingAssetsFilesDirectoryRenamedPath = $assetsFilesPath . "." . $randomDirectoryName . ".existing";
/* create the temporary directory for unzip in site/assets (so later moves are on same filesystem): */
mkdir($restoringAssetsFilesDirectoryTemporaryPath);
/* first rename the existing files directory to '.<random>.existing': */
$this->message("Renaming '{$assetsFilesPath}' -> '{$existingAssetsFilesDirectoryRenamedPath}'...");
if (!rename($assetsFilesPath, $existingAssetsFilesDirectoryRenamedPath)) {
$this->error("Could not rename '{$assetsFilesPath}' to '{$existingAssetsFilesDirectoryRenamedPath}'.");
return false;
}
/* unzip the zip file to '.<random>.restoring': */
$this->message("Restoring '{$restoreFilePath}'...");
$zipInfo = wireUnzipFile($restoreFilePath, $restoringAssetsFilesDirectoryTemporaryPath);
if (!empty($zipInfo['errors'])) {
$this->error("Error unzipping '{$restoreFilePath}' to '{$restoringAssetsFilesDirectoryTemporaryPath}': " . implode(", ", $zipInfo['errors']));
return false;
}
/* second rename the '.<random>.restoring' temporary directory to 'files': */
$this->message("Renaming '{$restoringAssetsFilesDirectoryTemporaryPath}' -> '{$assetsFilesPath}'...");
if (!rename($restoringAssetsFilesDirectoryTemporaryPath, $assetsFilesPath)) {
$this->error("Could not rename '{$restoringAssetsFilesDirectoryTemporaryPath}' to '{$assetsFilesPath}', undoing rename of assets/files directory...");
if (!rename($existingAssetsFilesDirectoryRenamedPath, $assetsFilesPath)) {
$this->error("Undo failed: Could not rename '{$existingAssetsFilesDirectoryRenamedPath}' to '{$assetsFilesPath}', leaving both directories for manual cleanup...");
return false;
}
unlink($restoringAssetsFilesDirectoryTemporaryPath);
return false;
}
$this->message("Restored '{$restoreFilePath}' to '{$assetsFilesPath}'.");
/* remove old sites/assets from'.<random>.existing': */
$this->message("Cleaning up '{$existingAssetsFilesDirectoryRenamedPath}'...");
if (!self::delTree($existingAssetsFilesDirectoryRenamedPath)) {
$this->error("Could not recursively remove '$existingAssetsFilesDirectoryRenamedPath', directory contents may have been partly deleted.");
}
return true;
}
/**
* Process submitted backup form, creating a new backup file
*
*/
protected function processBackup() {
/** @var WireInput $post */
$input = $this->wire('input');
$filename = basename($this->wire('sanitizer')->filename($input->post('backup_name')), '.zip');
if (empty($filename)) {
$filename = date_format("c") . "-assets-files";
}
$_filename = $filename;
$filename .= '.zip';
if (preg_match('/^(.+)-(\d+)$/', $_filename, $matches)) {
$_filename = $matches[1];
$n = $matches[2];
} else {
$n = 0;
}
while (file_exists($this->getBackupPath() . $filename)) {
$filename = $_filename . "-" . ( ++$n) . ".zip";
}
$options = array(
'filename' => $filename,
'description' => $this->wire('sanitizer')->text($input->post('description')),
);
$backupPath = $this->getBackupPath();
if (!is_dir($backupPath)) {
mkdir($backupPath, 0777, true);
}
$assetsFilesDirectoryPath = $this->getAssetsFilesPath();
$backupFilePath = $backupPath . "/" . $filename;
$zipInfo = wireZipFile($backupFilePath, $assetsFilesDirectoryPath . "/.");
if (!empty($zipInfo['errors']) || !is_file($backupFilePath)) {
foreach ($zipInfo['errors'] as $error) {
$this->error($error);
}
$this->error(sprintf($this->_('Failed to create ZIP file: %s'), $backupFilePath));
if (is_file($backupFilePath)) {
unlink($backupFilePath);
}
$this->wire('session')->redirect('../');
return;
}
$this->message($this->_('Saved new backup:') . " $backupFilePath");
}
/**
* This function is executed when a page with the Process assigned is accessed.
*
* This can be seen as the main or index function.
*
*/
public function ___execute() {
$numFiles = 0;
/** @var InputfieldForm $form */
$form = $this->modules->get('InputfieldForm');
/** @var MarkupAdminDataTable $table */
$table = $this->modules->get('MarkupAdminDataTable');
$table->setEncodeEntities(false);
$table->headerRow(array(
$this->_x('file', 'th'),
$this->_x('date', 'th'),
$this->_x('size', 'th'),
$this->_x('actions', 'th'),
));
$backupPath = $this->getBackupPath();
if (!is_dir($backupPath)) {
mkdir($backupPath, 0777, true);
}
foreach ($this->getBackupFiles() as $id => $file) {
$numFiles++;
$basename = $file['basename'];
$time = $file['time'] ? $file['time'] : $file['mtime'];
if ($file['description']) {
$basename .= '*';
}
$actions = array();
foreach ($this->getFileActions($file) as $action) {
$actions[] = $this->renderATooltip($action['href'], wireIconMarkup($action['icon'], 'fw'), $action['label']);
}
$table->row(array(
$basename => "./info/?id=$id",
$this->renderTdSort(strtotime($time), wireRelativeTimeStr($time)),
$this->renderTdSort($file['size'], wireBytesStr($file['size'])),
implode(' ', $actions)
));
}
if (!$numFiles) {
$form->description = $this->_('No database backup files yet.');
}
$form->value = $table->render();
/** @var InputfieldButton $f */
$f = $this->modules->get('InputfieldButton');
$f->value = $this->labels['backup'];
$f->icon = 'database';
$f->href = "./backup/";
$f->showInHeader(true);
$form->add($f);
/** @var InputfieldButton $f */
$f = $this->modules->get('InputfieldButton');
$f->value = $this->labels['upload'];
$f->href = "./upload/";
$f->icon = 'cloud-upload';
$f->setSecondary();
$f->showInHeader(true);
$form->add($f);
return $form->render();
}
/**
* Execute upload
*
* @return string
*
*/
public function ___executeUpload() {
/** @var InputfieldForm $form */
$form = $this->wire('modules')->get('InputfieldForm');
$form->attr('id', 'upload_form');
$form->description = $this->_('Add new assets ZIP archive');
/** @var InputfieldFile $f */
$f = $this->modules->get("InputfieldFile");
$f->name = 'upload_file';
$f->label = $this->_('Upload File');
$f->extensions = 'zip';
$f->maxFiles = 0;
$f->overwrite = false;
$f->destinationPath = $this->getBackupPath() . "/";
if (method_exists($f, 'setMaxFilesize'))
$f->setMaxFilesize('100g');
$form->add($f);
/** @var InputfieldSubmit $b */
$b = $this->modules->get('InputfieldSubmit');
$b->attr('name', 'submit_upload_file');
$b->attr('value', $this->labels['upload']);
$form->add($b);
if ($this->input->post('submit_upload_file')) {
$form->processInput($this->input->post);
foreach ($f->value as $pagefile) {
$this->message(sprintf($this->_('Added file: %s'), $pagefile->basename));
}
$this->session->redirect('../');
}
return $form->render();
}
/**
* Execute backup info
*
* @return string
*
*/
public function ___executeInfo() {
/** @var Modules $modules */
$modules = $this->wire('modules');
$file = $this->getFile();
$this->headline($file['basename']);
$info = self::getFileInfo($file['pathname']);
$info = array_merge($file, $info);
if ($info['valid']) {
$info['valid'] = $this->_('Yes');
} else {
$info['valid'] = $this->_('Unknown (likely not created by this tool)');
}
unset($info['basename'], $info['id']);
$info['pathname'] = str_replace($this->wire('config')->paths->root, '/', $info['pathname']);
if (empty($info['time'])) {
$info['mtime'] = date('Y-m-d H:i:s') . " (" . wireRelativeTimeStr($info['mtime']) . ")";
} else {
unset($info['mtime']);
$time = strtotime($info['time']);
$info['time'] = "$info[time] (" . wireRelativeTimeStr($time) . ")";
}
$info['size'] = number_format($info['size']) . " " . $this->_x('bytes', 'file-details');
/** @var MarkupAdminDataTable $table */
$table = $modules->get('MarkupAdminDataTable');
foreach ($info as $key => $value) {
if (is_array($value))
$value = implode(', ', $value);
if (!strlen($value))
continue;
$label = isset($this->labels[$key]) ? $this->labels[$key] : $key;
$table->row(array($label, $value));
}
/** @var InputfieldForm $form */
$form = $modules->get('InputfieldForm');
$form->value = $table->render();
$n = 0;
foreach ($this->getFileActions($file) as $name => $action) {
/** @var InputfieldButton $f */
$f = $modules->get('InputfieldButton');
$f->href = $action['href'];
$f->value = $action['label'];
$f->icon = $action['icon'];
if ($action['secondary'])
$f->setSecondary();
if ($action['head'])
$f->showInHeader(true);
$form->add($f);
$n++;
}
return $form->render();
}
/**
* Execute backup
*
* @return string
*
*/
public function ___executeBackup() {
$this->headline($this->labels['backup']);
/** @var WireInput $input */
$input = $this->wire('input');
if ($input->post('submit_backup')) {
$this->processBackup();
$this->session->redirect('../');
}
/** @var InputfieldForm $form */
$form = $this->wire('modules')->get('InputfieldForm');
/** @var InputfieldName $f */
$f = $this->wire('modules')->get('InputfieldName');
$f->attr('name', 'backup_name');
$f->label = $this->_('Backup name');
$f->description = $this->_('This will be used for the backup filename. The extension .zip will be added to it automatically.');
$f->notes = $this->_('If omitted, a unique filename will be automatically generated.');
$f->required = false;
// $f->attr('value', $this->wire('config')->dbName . '_' . date('Y-m-d'));
$form->add($f);
/** @var InputfieldText $f */
$f = $this->wire('modules')->get('InputfieldText');
$f->attr('name', 'description');
$f->label = $this->_('Backup description');
$f->collapsed = Inputfield::collapsedBlank;
$form->add($f);
/** @var InputfieldSubmit $f */
$f = $this->wire('modules')->get('InputfieldSubmit');
$f->attr('name', 'submit_backup');
$f->icon = 'database';
$f->showInHeader(true);
$form->add($f);
$form->appendMarkup = "<p class='detail'>" .
$this->_('Please be patient after clicking submit. Backups may take some time, depending on how much there is to backup.') .
"</p>";
return $form->render();
}
/**
* Execute download
*
*/
public function ___executeDownload() {
$file = $this->getFile();
$fileFullPath = $file['pathname'];
$filename = $file['basename'];
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($fileFullPath));
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
/* disable output buffering so large downloads will work despite little
* php memory: */
while (ob_get_level() > 0) {
ob_end_flush();
}
$fp = fopen($fileFullPath, 'rb');
fpassthru($fp);
fclose($fp);
exit;
}
/**
* Execute delete
*
* @return string
*
*/
public function ___executeDelete() {
$this->headline($this->_('Delete Backup'));
$file = $this->getFile();
$submitDelete = $this->input->post('submit_delete');
if ($submitDelete && $this->input->post('delete_confirm')) {
// confirmed delete
foreach (array($file['pathname'], $file['zip']) as $pathname) {
if (empty($pathname) || !is_file($pathname))
continue;
unlink($pathname);
$this->message(sprintf($this->_('Deleted: %s'), basename($pathname)));
}
$this->session->redirect('../');
} else if ($submitDelete) {
// not confirmed
$this->session->redirect('../');
} else {
// render confirmation form
/** @var InputfieldForm $form */
$form = $this->modules->get('InputfieldForm');
$form->action = "./?id=$file[id]";
$form->description = sprintf($this->_('Delete %s?'), $file['basename']);
/** @var InputfieldCheckbox $f */
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'delete_confirm');
$f->label = $this->_('Check the box to confirm');
$form->add($f);
/** @var InputfieldSubmit $f */
$f = $this->modules->get('InputfieldSubmit');
$f->attr('name', 'submit_delete');
$form->add($f);
return $form->render();
}
return '';
}
/**
* Execute restore
*
* @return string
*
*/
public function ___executeRestore() {
$this->headline($this->_('Restore Backup'));
$file = $this->getFile();
if ($this->input->post('submit_restore')) {
if ($this->input->post('restore_confirm') && file_exists($file['pathname'])) {
$success = $this->processRestore($file['basename']);
if ($success) {
$this->message(sprintf($this->_('Restored: %s'), "$file[basename]"));
$this->session->redirect('../');
} else {
$this->error(sprintf($this->_('Error restoring: %s'), "$file[pathname]"));
}
}
} else {
/** @var InputfieldForm $form */
$form = $this->modules->get('InputfieldForm');
$form->action = "./?id=$file[id]";
$form->description = $this->_('Warning: the current assets/files will be removed and replaced (this has potential to break your site!)');
/** @var InputfieldCheckbox $f */
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'restore_confirm');
$f->label = sprintf($this->_('Restore %s?'), $file['basename']);
$form->add($f);
/** @var InputfieldSubmit $f */
$f = $this->modules->get('InputfieldSubmit');
$f->attr('name', 'submit_restore');
$form->add($f);
return $form->render();
}
return '';
}
/**
* Install
*
* @throws WireException
*
*/
public function ___install() {
// check that they have the required PW version
if (version_compare($this->wire('config')->version, self::minVersion, '<')) {
throw new WireException("This module requires ProcessWire " . self::minVersion . " or newer.");
}
parent::___install();
}
/**
* Uninstall
*
*/
public function ___uninstall() {
$path = $this->getBackupPath(true);
$this->warning("Please note that the backup files in '{$path}' remain. If you don't want them there, please remove them manually.");
parent::___uninstall();
}
/**
* Render a sortable column for a list table
*
* @param string|int $unformatted Unformatted sortable value
* @param string $formatted Formatted value
* @return string
*
*/
protected function renderTdSort($unformatted, $formatted) {
return "<span style='display:none'>$unformatted</span>$formatted";
}
/**
* Render an <a> link with tooltip
*
* @param string $href Link URL
* @param string $label Link text
* @param string $description Tooltip text
* @return string
*
*/
protected function renderATooltip($href, $label, $description) {
return "<a href='$href' class='tooltip' title='$description'>$label</a>";
}
/**
* @return array
*
*/
protected static function getDirectoryEntries($path) {
$entries = [];
$dirHandle = opendir($path);
if ($dirHandle) {
while (false !== ($entry = readdir($dirHandle))) {
if ($entry != "." && $entry != "..") {
array_push($entries, $entry);
}
}
closedir($dirHandle);
} else {
$this->error("Could not open directory '{$path}'.");
}
return $entries;
}
/**
*
* @param type $pattern
* @param type $subject
* @return true if $subject starts with $pattern, false otherwise
*/
protected static function startsWith($pattern, $subject) {
$len = strlen($pattern);
return (substr($subject, 0, $len) === $pattern);
}
/**
*
* @param string $filePath
* @return boolean if all entries in the zipfile start with './'
*/
protected static function isValidZipFile($filePath) {
$zip = new ZipArchive;
$result = true;
if ($zip->open($filePath)) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex(0);
if (!self::startsWith("./", $filename)) {
$result = false;
break;
}
}
} else {
$result = false;
}
return $result;
}
/**
*
* @param string $filePath
* @param int $id
* @return array
*/
protected static function getFileInfo($filePath, $id = 0) {
return [
'description' => '',
'valid' => self::isValidZipFile($filePath),
'time' => strftime("%c", filemtime($filePath)), // ISO-8601
'mtime' => filemtime($filePath), // timestamp
'user' => '',
'size' => filesize($filePath),
'basename' => basename($filePath),
'pathname' => $filePath,
'id' => $id,
];
}
/**
* recursively delete a directory with all its subdirectories:
*
* @param string $dir path to any directory except '/'
* @return boolean true on success, false on failure
*/
protected static function delTree($dir) {
if (empty($dir) || !is_dir($dir) || $dir == "/") {
return false;
}
$files = scandir($dir);
foreach ($files as $file) {
if ($file == "." || $file == "..") {
continue;
} elseif (is_dir("$dir/$file")) {
self::delTree("$dir/$file");
} else {
unlink("$dir/$file");
}
}
return rmdir($dir);
}
}