|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace phpMyFAQ\Controller\Api; |
| 4 | + |
| 5 | +use OpenApi\Attributes as OA; |
| 6 | +use phpMyFAQ\Administration\Backup; |
| 7 | +use phpMyFAQ\Controller\AbstractController; |
| 8 | +use phpMyFAQ\Core\Exception; |
| 9 | +use phpMyFAQ\Database\DatabaseHelper; |
| 10 | +use phpMyFAQ\Enums\BackupType; |
| 11 | +use phpMyFAQ\Enums\PermissionType; |
| 12 | +use phpMyFAQ\Filter; |
| 13 | +use SodiumException; |
| 14 | +use Symfony\Component\HttpFoundation\HeaderUtils; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\Response; |
| 17 | + |
| 18 | +class BackupController extends AbstractController |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @throws Exception |
| 22 | + */ |
| 23 | + #[OA\Get( |
| 24 | + path: '/api/v3.0/backup/{type}', |
| 25 | + operationId: 'createBackup', |
| 26 | + tags: ['Endpoints with Authentication'], |
| 27 | + )] |
| 28 | + #[OA\Header( |
| 29 | + header: 'Accept-Language', |
| 30 | + description: 'The language code for the login.', |
| 31 | + schema: new OA\Schema(type: 'string') |
| 32 | + )] |
| 33 | + #[OA\Header( |
| 34 | + header: 'x-pmf-token', |
| 35 | + description: 'phpMyFAQ client API Token, generated in admin backend', |
| 36 | + schema: new OA\Schema(type: 'string') |
| 37 | + )] |
| 38 | + #[OA\Parameter( |
| 39 | + name: 'type', |
| 40 | + description: 'The backup type. Can be "data" or "logs".', |
| 41 | + in: 'path', |
| 42 | + required: true, |
| 43 | + schema: new OA\Schema(type: 'string') |
| 44 | + )] |
| 45 | + #[OA\Response( |
| 46 | + response: 200, |
| 47 | + description: 'The current backup as a file.', |
| 48 | + content: new OA\MediaType( |
| 49 | + mediaType: 'application/octet-stream', |
| 50 | + schema: new OA\Schema(type: 'string') |
| 51 | + ) |
| 52 | + )] |
| 53 | + #[OA\Response( |
| 54 | + response: 400, |
| 55 | + description: 'If the backup type is wrong', |
| 56 | + content: new OA\MediaType( |
| 57 | + mediaType: 'application/octet-stream', |
| 58 | + schema: new OA\Schema(type: 'string') |
| 59 | + ) |
| 60 | + )] |
| 61 | + #[OA\Response( |
| 62 | + response: 401, |
| 63 | + description: 'If the user is not authenticated and/or does not have sufficient permissions.' |
| 64 | + )] |
| 65 | + public function download(Request $request): Response |
| 66 | + { |
| 67 | + $this->userHasPermission(PermissionType::BACKUP); |
| 68 | + |
| 69 | + $type = Filter::filterVar($request->get('type'), FILTER_SANITIZE_SPECIAL_CHARS); |
| 70 | + |
| 71 | + switch ($type) { |
| 72 | + case 'data': |
| 73 | + $backupType = BackupType::BACKUP_TYPE_DATA; |
| 74 | + break; |
| 75 | + case 'logs': |
| 76 | + $backupType = BackupType::BACKUP_TYPE_LOGS; |
| 77 | + break; |
| 78 | + default: |
| 79 | + return new Response('Invalid backup type.', Response::HTTP_BAD_REQUEST); |
| 80 | + } |
| 81 | + |
| 82 | + $dbHelper = new DatabaseHelper($this->configuration); |
| 83 | + $backup = new Backup($this->configuration, $dbHelper); |
| 84 | + $tableNames = $backup->getBackupTableNames($backupType); |
| 85 | + $backupQueries = $backup->generateBackupQueries($tableNames); |
| 86 | + |
| 87 | + try { |
| 88 | + $backupFileName = $backup->createBackup($backupType->value, $backupQueries); |
| 89 | + |
| 90 | + $response = new Response($backupQueries); |
| 91 | + |
| 92 | + $disposition = HeaderUtils::makeDisposition( |
| 93 | + HeaderUtils::DISPOSITION_ATTACHMENT, |
| 94 | + urlencode($backupFileName) |
| 95 | + ); |
| 96 | + |
| 97 | + $response->headers->set('Content-Type', 'application/octet-stream'); |
| 98 | + $response->headers->set('Content-Disposition', $disposition); |
| 99 | + $response->setStatusCode(Response::HTTP_OK); |
| 100 | + return $response->send(); |
| 101 | + } catch (SodiumException) { |
| 102 | + return new Response('An error occurred while creating the backup.', Response::HTTP_INTERNAL_SERVER_ERROR); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments