|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of MythicalDash. |
| 5 | + * Please view the LICENSE file that was distributed with this source code. |
| 6 | + * |
| 7 | + * # MythicalSystems License v2.0 |
| 8 | + * |
| 9 | + * ## Copyright (c) 2021–2025 MythicalSystems and Cassian Gherman |
| 10 | + * |
| 11 | + * Breaking any of the following rules will result in a permanent ban from the MythicalSystems community and all of its services. |
| 12 | + */ |
| 13 | + |
| 14 | +use MythicalDash\App; |
| 15 | +use MythicalDash\Chat\User\Can; |
| 16 | +use MythicalDash\Chat\User\Session; |
| 17 | +use MythicalDash\Hooks\MythicalCloud; |
| 18 | +use MythicalDash\Config\ConfigInterface; |
| 19 | +use MythicalDash\Chat\columns\UserColumns; |
| 20 | + |
| 21 | +// Get all backups |
| 22 | +$router->get('/api/admin/cloud/backups', function (): void { |
| 23 | + App::init(); |
| 24 | + $appInstance = App::getInstance(true); |
| 25 | + $appInstance->allowOnlyGET(); |
| 26 | + $session = new Session($appInstance); |
| 27 | + |
| 28 | + if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) { |
| 29 | + try { |
| 30 | + $licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL'); |
| 31 | + if (!$licenseKey) { |
| 32 | + throw new Exception('Mythical Cloud license key not configured'); |
| 33 | + } |
| 34 | + |
| 35 | + $backups = MythicalCloud::getBackups($licenseKey); |
| 36 | + if ($backups === null) { |
| 37 | + throw new Exception('Failed to retrieve backups'); |
| 38 | + } |
| 39 | + |
| 40 | + $appInstance->OK('Backups retrieved successfully', $backups); |
| 41 | + } catch (Exception $e) { |
| 42 | + $appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']); |
| 43 | + } |
| 44 | + } else { |
| 45 | + $appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']); |
| 46 | + } |
| 47 | +}); |
| 48 | + |
| 49 | +// Download backup |
| 50 | +$router->get('/api/admin/cloud/backup/(.*)/download', function (string $backupId): void { |
| 51 | + App::init(); |
| 52 | + $appInstance = App::getInstance(true); |
| 53 | + $appInstance->allowOnlyGET(); |
| 54 | + $session = new Session($appInstance); |
| 55 | + |
| 56 | + if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) { |
| 57 | + try { |
| 58 | + $licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL'); |
| 59 | + |
| 60 | + if (!$licenseKey) { |
| 61 | + throw new Exception('Mythical Cloud license key not configured'); |
| 62 | + } |
| 63 | + |
| 64 | + $backupDir = __DIR__ . '/../../../../storage/backups'; |
| 65 | + if (!is_dir($backupDir)) { |
| 66 | + mkdir($backupDir, 0755, true); |
| 67 | + } |
| 68 | + $savePath = $backupDir . '/backup_' . $backupId . '.mydb'; |
| 69 | + $backupContent = MythicalCloud::downloadBackup($licenseKey, $backupId, $savePath); |
| 70 | + if ($backupContent === false) { |
| 71 | + throw new Exception('Failed to download backup'); |
| 72 | + } |
| 73 | + |
| 74 | + $appInstance->OK('Backup downloaded successfully', []); |
| 75 | + exit; |
| 76 | + } catch (Exception $e) { |
| 77 | + $appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']); |
| 78 | + } |
| 79 | + } else { |
| 80 | + $appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']); |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +// Delete specific backup |
| 85 | +$router->post('/api/admin/cloud/backup/(.*)/delete', function (string $backupId): void { |
| 86 | + App::init(); |
| 87 | + $appInstance = App::getInstance(true); |
| 88 | + $appInstance->allowOnlyPOST(); |
| 89 | + $session = new Session($appInstance); |
| 90 | + |
| 91 | + if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) { |
| 92 | + try { |
| 93 | + $licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL'); |
| 94 | + |
| 95 | + if (!$licenseKey) { |
| 96 | + throw new Exception('Mythical Cloud license key not configured'); |
| 97 | + } |
| 98 | + |
| 99 | + $result = MythicalCloud::deleteBackup($licenseKey, $backupId); |
| 100 | + if ($result === null) { |
| 101 | + throw new Exception('Failed to delete backup'); |
| 102 | + } |
| 103 | + |
| 104 | + $appInstance->OK('Backup deleted successfully', $result); |
| 105 | + } catch (Exception $e) { |
| 106 | + $appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']); |
| 107 | + } |
| 108 | + } else { |
| 109 | + $appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']); |
| 110 | + } |
| 111 | +}); |
| 112 | + |
| 113 | +// Get specific backup info |
| 114 | +$router->get('/api/admin/cloud/backup/(.*)', function (string $backupId): void { |
| 115 | + App::init(); |
| 116 | + $appInstance = App::getInstance(true); |
| 117 | + $appInstance->allowOnlyGET(); |
| 118 | + $session = new Session($appInstance); |
| 119 | + |
| 120 | + if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) { |
| 121 | + try { |
| 122 | + $licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL'); |
| 123 | + |
| 124 | + if (!$licenseKey) { |
| 125 | + throw new Exception('Mythical Cloud license key not configured'); |
| 126 | + } |
| 127 | + |
| 128 | + $backupInfo = MythicalCloud::getBackupInfo($licenseKey, $backupId); |
| 129 | + if ($backupInfo === null) { |
| 130 | + throw new Exception('Failed to retrieve backup info'); |
| 131 | + } |
| 132 | + |
| 133 | + $appInstance->OK('Backup info retrieved successfully', $backupInfo); |
| 134 | + } catch (Exception $e) { |
| 135 | + $appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']); |
| 136 | + } |
| 137 | + } else { |
| 138 | + $appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']); |
| 139 | + } |
| 140 | +}); |
| 141 | + |
| 142 | +// Delete all backups |
| 143 | +$router->post('/api/admin/cloud/backups/wipe', function (): void { |
| 144 | + App::init(); |
| 145 | + $appInstance = App::getInstance(true); |
| 146 | + $appInstance->allowOnlyPOST(); |
| 147 | + $session = new Session($appInstance); |
| 148 | + |
| 149 | + if (Can::canAccessAdminUI($session->getInfo(UserColumns::ROLE_ID, false))) { |
| 150 | + try { |
| 151 | + $licenseKey = $appInstance->getConfig()->getSetting(ConfigInterface::LICENSE_KEY, 'NULL'); |
| 152 | + |
| 153 | + if (!$licenseKey) { |
| 154 | + throw new Exception('Mythical Cloud license key not configured'); |
| 155 | + } |
| 156 | + |
| 157 | + $result = MythicalCloud::deleteAllBackups($licenseKey); |
| 158 | + if ($result === null) { |
| 159 | + throw new Exception('Failed to delete all backups'); |
| 160 | + } |
| 161 | + |
| 162 | + $appInstance->OK('All backups deleted successfully', $result); |
| 163 | + } catch (Exception $e) { |
| 164 | + $appInstance->InternalServerError($e->getMessage(), ['error_code' => 'CLOUD_ERROR']); |
| 165 | + } |
| 166 | + } else { |
| 167 | + $appInstance->Unauthorized('Unauthorized', ['error_code' => 'INVALID_SESSION']); |
| 168 | + } |
| 169 | +}); |
0 commit comments