PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.2
4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Backup / Ajax / Edit.php
wp-staging / Backup / Ajax Last commit date
Backup 2 years ago FileList 2 years ago Restore 2 years ago Backup.php 2 years ago BackupSpeedIndex.php 2 years ago Cancel.php 3 years ago Delete.php 3 years ago Edit.php 2 years ago FileInfo.php 3 years ago FileList.php 2 years ago Listing.php 2 years ago Parts.php 3 years ago PrepareJob.php 2 years ago Restore.php 2 years ago ScheduleList.php 2 years ago Status.php 3 years ago Upload.php 2 years ago
Edit.php
161 lines
1 <?php
2
3 // TODO PHP7.x; declare(strict_type=1);
4 // TODO PHP7.x; type hints & return types
5
6 namespace WPStaging\Backup\Ajax;
7
8 use WPStaging\Core\WPStaging;
9 use WPStaging\Framework\Component\AbstractTemplateComponent;
10 use WPStaging\Framework\Filesystem\FileObject;
11 use WPStaging\Framework\TemplateEngine\TemplateEngine;
12 use WPStaging\Framework\Utils\Sanitize;
13 use WPStaging\Backup\Service\BackupMetadataEditor;
14 use WPStaging\Backup\Service\BackupsFinder;
15 use WPStaging\Backup\Entity\BackupMetadata;
16
17 class Edit extends AbstractTemplateComponent
18 {
19 private $backupMetadataEditor;
20 private $backupsFinder;
21
22 /** @var Sanitize */
23 private $sanitize;
24
25 public function __construct(BackupsFinder $backupsFinder, BackupMetadataEditor $backupMetadataEditor, Sanitize $sanitize, TemplateEngine $templateEngine)
26 {
27 parent::__construct($templateEngine);
28 $this->backupsFinder = $backupsFinder;
29 $this->backupMetadataEditor = $backupMetadataEditor;
30 $this->sanitize = $sanitize;
31 }
32
33 public function render()
34 {
35 if (!$this->canRenderAjax()) {
36 return;
37 }
38
39 $md5 = sanitize_text_field(isset($_POST['md5']) ? $_POST['md5'] : '');
40
41 $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : 'Backup';
42
43 $name = substr(sanitize_text_field(html_entity_decode($name)), 0, 100);
44 $name = htmlentities($name, ENT_QUOTES);
45 $name = str_replace('\\\'', '\'', $name);
46 $name = str_replace('\\\"', '\"', $name);
47
48 $notes = isset($_POST['notes']) ? sanitize_textarea_field($_POST['notes']) : '';
49 $notes = substr($notes, 0, 1000);
50
51 if (strlen($md5) !== 32) {
52 wp_send_json([
53 'error' => true,
54 'message' => __('Invalid request.', 'wp-staging'),
55 ]);
56 }
57
58 $backups = $this->backupsFinder->findBackups();
59
60 // Early bail: No backups found, nothing to edit
61 if (empty($backups)) {
62 wp_send_json([
63 'error' => true,
64 'message' => __('No backups found, nothing to edit.', 'wp-staging'),
65 ]);
66 }
67
68 // Name must not be empty.
69 if (empty($name)) {
70 $name = __('Backup', 'wp-staging');
71 }
72
73 /** @var \SplFileInfo $backup */
74 foreach ($backups as $backup) {
75 if ($md5 === md5($backup->getBasename())) {
76 try {
77 $file = new FileObject($backup->getPathname(), FileObject::MODE_APPEND_AND_READ);
78 $metaData = (new BackupMetadata())->hydrateByFile($file);
79
80 $increment = strlen($name) + strlen($notes);
81
82 if ($metaData->getIsMultipartBackup()) {
83 $this->updateBackupParts($metaData, $name, $notes, $increment);
84 return;
85 }
86
87 $oldNote = $metaData->getNote();
88 $oldNoteLength = 2; // null takes 2 bytes!?
89 if ($oldNote !== null) {
90 $oldNoteLength = strlen($oldNote);
91 }
92
93 $backupSize = $metaData->getBackupSize();
94 $backupSize = $backupSize + $increment - strlen($metaData->getName()) - $oldNoteLength;
95
96 $metaData->setName($name);
97 $metaData->setNote($notes);
98 $metaData->setBackupSize($backupSize);
99
100 $this->backupMetadataEditor->setBackupMetadata($file, $metaData);
101 } catch (\Exception $e) {
102 wp_send_json([
103 'error' => true,
104 /* We might need to translate the error */
105 'message' => esc_html__($e->getMessage(), 'wp-staging'),
106 ]);
107 }
108 }
109 }
110
111 wp_send_json(true);
112 }
113
114 /**
115 * @param BackupMetadata $metaData
116 * @param string $name
117 * @param string $notes
118 * @param int $incrementSize
119 */
120 protected function updateBackupParts($metaData, $name, $notes, $incrementSize)
121 {
122 $backupSize = 0;
123 $backupsDirectory = WPStaging::make(BackupsFinder::class)->getBackupsDirectory();
124 $backupParts = [];
125 foreach ($metaData->getMultipartMetadata()->getBackupParts() as $part) {
126 $backupPart = $backupsDirectory . $part;
127 $partFile = new FileObject($backupPart, FileObject::MODE_APPEND_AND_READ);
128 $partMetadata = (new BackupMetadata())->hydrateByFile($partFile);
129 $partSize = filesize($backupPart);
130
131 $oldNote = $partMetadata->getNote();
132 $oldNoteLength = 2;
133 if ($oldNote !== null) {
134 $oldNoteLength = strlen($oldNote);
135 }
136
137 $partSize = $partSize + $incrementSize - strlen($partMetadata->getName()) - $oldNoteLength;
138 $backupSize += $partSize;
139 $backupParts[] = [
140 'metadata' => $partMetadata,
141 'file' => $partFile,
142 'size' => $partSize
143 ];
144 }
145
146 foreach ($backupParts as $part) {
147 /** @var BackupMetadata $partMetadata */
148 $partMetadata = $part['metadata'];
149 $partMetadata->setName($name);
150 $partMetadata->setNote($notes);
151 $partMetadata->setBackupSize($backupSize);
152
153 $multipartMetadata = $partMetadata->getMultipartMetadata();
154 $multipartMetadata->setPartSize($part['size']);
155 $partMetadata->setMultipartMetadata($multipartMetadata);
156
157 $this->backupMetadataEditor->setBackupMetadata($part['file'], $partMetadata);
158 }
159 }
160 }
161