Backup
2 years ago
FileList
2 years ago
Restore
3 years ago
Backup.php
2 years ago
BackupSpeedIndex.php
2 years ago
Cancel.php
3 years ago
Delete.php
3 years ago
Edit.php
3 years ago
FileInfo.php
3 years ago
FileList.php
3 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
159 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 = str_replace('\\\'', '\'', $name); |
| 45 | |
| 46 | $notes = isset($_POST['notes']) ? sanitize_textarea_field($_POST['notes']) : ''; |
| 47 | $notes = substr($notes, 0, 1000); |
| 48 | |
| 49 | if (strlen($md5) !== 32) { |
| 50 | wp_send_json([ |
| 51 | 'error' => true, |
| 52 | 'message' => __('Invalid request.', 'wp-staging'), |
| 53 | ]); |
| 54 | } |
| 55 | |
| 56 | $backups = $this->backupsFinder->findBackups(); |
| 57 | |
| 58 | // Early bail: No backups found, nothing to edit |
| 59 | if (empty($backups)) { |
| 60 | wp_send_json([ |
| 61 | 'error' => true, |
| 62 | 'message' => __('No backups found, nothing to edit.', 'wp-staging'), |
| 63 | ]); |
| 64 | } |
| 65 | |
| 66 | // Name must not be empty. |
| 67 | if (empty($name)) { |
| 68 | $name = __('Backup', 'wp-staging'); |
| 69 | } |
| 70 | |
| 71 | /** @var \SplFileInfo $backup */ |
| 72 | foreach ($backups as $backup) { |
| 73 | if ($md5 === md5($backup->getBasename())) { |
| 74 | try { |
| 75 | $file = new FileObject($backup->getPathname(), FileObject::MODE_APPEND_AND_READ); |
| 76 | $metaData = (new BackupMetadata())->hydrateByFile($file); |
| 77 | |
| 78 | $increment = strlen($name) + strlen($notes); |
| 79 | |
| 80 | if ($metaData->getIsMultipartBackup()) { |
| 81 | $this->updateBackupParts($metaData, $name, $notes, $increment); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | $oldNote = $metaData->getNote(); |
| 86 | $oldNoteLength = 2; // null takes 2 bytes!? |
| 87 | if ($oldNote !== null) { |
| 88 | $oldNoteLength = strlen($oldNote); |
| 89 | } |
| 90 | |
| 91 | $backupSize = $metaData->getBackupSize(); |
| 92 | $backupSize = $backupSize + $increment - strlen($metaData->getName()) - $oldNoteLength; |
| 93 | |
| 94 | $metaData->setName($name); |
| 95 | $metaData->setNote($notes); |
| 96 | $metaData->setBackupSize($backupSize); |
| 97 | |
| 98 | $this->backupMetadataEditor->setBackupMetadata($file, $metaData); |
| 99 | } catch (\Exception $e) { |
| 100 | wp_send_json([ |
| 101 | 'error' => true, |
| 102 | /* We might need to translate the error */ |
| 103 | 'message' => esc_html__($e->getMessage(), 'wp-staging'), |
| 104 | ]); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | wp_send_json(true); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param BackupMetadata $metaData |
| 114 | * @param string $name |
| 115 | * @param string $notes |
| 116 | * @param int $incrementSize |
| 117 | */ |
| 118 | protected function updateBackupParts($metaData, $name, $notes, $incrementSize) |
| 119 | { |
| 120 | $backupSize = 0; |
| 121 | $backupsDirectory = WPStaging::make(BackupsFinder::class)->getBackupsDirectory(); |
| 122 | $backupParts = []; |
| 123 | foreach ($metaData->getMultipartMetadata()->getBackupParts() as $part) { |
| 124 | $backupPart = $backupsDirectory . $part; |
| 125 | $partFile = new FileObject($backupPart, FileObject::MODE_APPEND_AND_READ); |
| 126 | $partMetadata = (new BackupMetadata())->hydrateByFile($partFile); |
| 127 | $partSize = filesize($backupPart); |
| 128 | |
| 129 | $oldNote = $partMetadata->getNote(); |
| 130 | $oldNoteLength = 2; |
| 131 | if ($oldNote !== null) { |
| 132 | $oldNoteLength = strlen($oldNote); |
| 133 | } |
| 134 | |
| 135 | $partSize = $partSize + $incrementSize - strlen($partMetadata->getName()) - $oldNoteLength; |
| 136 | $backupSize += $partSize; |
| 137 | $backupParts[] = [ |
| 138 | 'metadata' => $partMetadata, |
| 139 | 'file' => $partFile, |
| 140 | 'size' => $partSize |
| 141 | ]; |
| 142 | } |
| 143 | |
| 144 | foreach ($backupParts as $part) { |
| 145 | /** @var BackupMetadata $partMetadata */ |
| 146 | $partMetadata = $part['metadata']; |
| 147 | $partMetadata->setName($name); |
| 148 | $partMetadata->setNote($notes); |
| 149 | $partMetadata->setBackupSize($backupSize); |
| 150 | |
| 151 | $multipartMetadata = $partMetadata->getMultipartMetadata(); |
| 152 | $multipartMetadata->setPartSize($part['size']); |
| 153 | $partMetadata->setMultipartMetadata($multipartMetadata); |
| 154 | |
| 155 | $this->backupMetadataEditor->setBackupMetadata($part['file'], $partMetadata); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 |