.htaccess
9 months ago
Restore.php
9 months ago
index.html
9 months ago
restore.template.php
9 months ago
web.config
9 months ago
Restore.php
139 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Restore; |
| 4 | |
| 5 | use Exception; |
| 6 | use JetBackup\Cron\Task\Task; |
| 7 | use JetBackup\Exception\ExecutionTimeException; |
| 8 | use JetBackup\Exception\RestoreException; |
| 9 | use JetBackup\Factory; |
| 10 | use JetBackup\IO\Lock; |
| 11 | use JetBackup\JetBackup; |
| 12 | use JetBackup\Queue\Queue; |
| 13 | use JetBackup\Queue\QueueItem; |
| 14 | use JetBackup\Wordpress\Wordpress; |
| 15 | use Throwable; |
| 16 | |
| 17 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 18 | |
| 19 | class Restore { |
| 20 | |
| 21 | const ACTION_RESTORE = 'restore'; |
| 22 | const ACTION_STATUS = 'status'; |
| 23 | const ACTION_CANCEL = 'cancel'; |
| 24 | const ACTION_COMPLETED = 'completed'; |
| 25 | |
| 26 | private QueueItem $_queue; |
| 27 | private Task $_task; |
| 28 | |
| 29 | public function __construct(QueueItem $queueItem) { |
| 30 | $this->_queue = $queueItem; |
| 31 | $this->_task = new \JetBackup\Cron\Task\Restore(); |
| 32 | $this->_task->setQueueItem($queueItem); |
| 33 | $this->_task->setExecutionTimeLimit(30); |
| 34 | $this->_task->setExecutionTimeDie(false); |
| 35 | } |
| 36 | |
| 37 | public function execute($action) { |
| 38 | switch ($action) { |
| 39 | case self::ACTION_CANCEL: $this->_actionCancel(); |
| 40 | case self::ACTION_COMPLETED: $this->_actionCompleted(); |
| 41 | case self::ACTION_STATUS: $this->_actionStatus(); |
| 42 | case self::ACTION_RESTORE: $this->_actionRestore(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private static function _getRestorePath() : string { |
| 47 | return Factory::getSettingsRestore()->isRestoreAlternatePathEnabled() ? WP_ROOT . JetBackup::SEP . JetBackup::CRON_PUBLIC_URL : WP_ROOT; |
| 48 | } |
| 49 | |
| 50 | private static function _getRestoreFileName(): string { |
| 51 | if (!isset($_SERVER['SCRIPT_FILENAME'])) return ''; |
| 52 | $script = Wordpress::getUnslash($_SERVER['SCRIPT_FILENAME']); |
| 53 | $script = Wordpress::sanitizeTextField($script); |
| 54 | return basename($script); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | private static function _getRestoreFile() : string { |
| 59 | return self::_getRestorePath() . JetBackup::SEP . self::_getRestoreFileName(); |
| 60 | } |
| 61 | |
| 62 | private static function _getLockFile() : string { |
| 63 | return self::_getRestoreFile() . '.lock'; |
| 64 | } |
| 65 | |
| 66 | private function _actionRestore() { |
| 67 | |
| 68 | if (!Lock::LockFile(self::_getLockFile())) { |
| 69 | self::_output(true, 'Already running...', [ |
| 70 | 'status' => $this->_queue->getStatus(), |
| 71 | ]); |
| 72 | } |
| 73 | |
| 74 | if($this->_queue->getStatus() < Queue::STATUS_DONE) { |
| 75 | try { |
| 76 | $this->_task->execute(); |
| 77 | } catch(ExecutionTimeException $e) { |
| 78 | self::_output(true, 'Execution time reached', [ 'status' => $this->_queue->getStatus() ]); |
| 79 | } catch(Exception $e) { |
| 80 | self::_output(false, "Error: " . $e->getMessage()); |
| 81 | } catch(Throwable $e) { |
| 82 | self::_output(false, "Fatal Error: " . $e->getMessage() . "\nTrace:\n" . $e->getTraceAsString()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | self::_output(true, 'Completed', [ |
| 87 | 'status' => $this->_queue->getStatus(), |
| 88 | ]); |
| 89 | } |
| 90 | |
| 91 | private static function _selfDelete() { |
| 92 | @unlink(self::_getRestoreFile()); |
| 93 | @unlink(self::_getLockFile()); |
| 94 | } |
| 95 | |
| 96 | private function _actionCompleted() { |
| 97 | |
| 98 | if($this->_queue->getStatus() < Queue::STATUS_DONE) |
| 99 | self::_output(false, 'Restore haven\'t completed yet'); |
| 100 | |
| 101 | self::_selfDelete(); |
| 102 | |
| 103 | self::_output(true, 'Completed Successfully'); |
| 104 | } |
| 105 | |
| 106 | private function _actionCancel() { |
| 107 | |
| 108 | $this->_queue->updateStatus(Queue::STATUS_ABORTED); |
| 109 | $this->_queue->updateProgress('Restore aborted by the user', QueueItem::PROGRESS_LAST_STEP); |
| 110 | |
| 111 | self::_selfDelete(); |
| 112 | |
| 113 | self::_output(true, 'Cancelled Successfully'); |
| 114 | } |
| 115 | |
| 116 | private function _actionStatus() { |
| 117 | |
| 118 | $log_entries = []; |
| 119 | $log_file = $this->_task->getLogFile(); |
| 120 | if(file_exists($log_file)) $log_entries = file($log_file); |
| 121 | |
| 122 | $progress = $this->_queue->getProgress(); |
| 123 | |
| 124 | self::_output(true, '', [ |
| 125 | 'status' => $this->_queue->getStatus(), |
| 126 | 'progress' => $progress->getDisplay(), |
| 127 | 'log_entries' => $log_entries, |
| 128 | ]); |
| 129 | } |
| 130 | |
| 131 | private static function _output($success, $message, $data=[]) { |
| 132 | die(json_encode([ |
| 133 | 'success' => $success ? 1 : 0, |
| 134 | 'message' => $message, |
| 135 | 'data' => $data, |
| 136 | ])); |
| 137 | } |
| 138 | |
| 139 | } |