. */ namespace SP\Modules\Web\Controllers\ConfigBackup; use Exception; use SP\Core\Acl\UnauthorizedPageException; use SP\Core\Context\SessionContext; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Core\Exceptions\SessionTimeout; use SP\Core\Exceptions\SPException; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Export\Services\BackupFiles; use SP\Infrastructure\File\FileHandler; use SP\Modules\Web\Controllers\SimpleControllerBase; use SP\Modules\Web\Controllers\Traits\JsonTrait; /** * Class ConfigBackupController * * @package SP\Modules\Web\Controllers */ final class DownloadBackupDbController extends SimpleControllerBase { use JsonTrait; /** * @return string */ public function downloadBackupDbAction(): string { if ($this->configData->isDemoEnabled()) { return __('Ey, this is a DEMO!!'); } try { SessionContext::close(); $filePath = BackupFiles::getDbBackupFilename( BACKUP_PATH, $this->configData->getBackupHash(), true ); $file = new FileHandler($filePath); $file->checkFileExists(); $this->eventDispatcher->notify( 'download.backupDbFile', new Event( $this, EventMessage::factory() ->addDescription(__u('File downloaded')) ->addDetail(__u('File'), str_replace(APP_ROOT, '', $file->getFile())) ) ); $this->router ->response() ->header('Cache-Control', 'max-age=60, must-revalidate') ->header('Content-length', $file->getFileSize()) ->header('Content-type', $file->getFileType()) ->header('Content-Description', ' sysPass file') ->header('Content-transfer-encoding', 'chunked') ->header('Content-Disposition', 'attachment; filename="'.basename($file->getFile()).'"') ->header('Set-Cookie', 'fileDownload=true; path=/') ->send(); $file->readChunked(); } catch (Exception $e) { processException($e); $this->eventDispatcher->notify('exception', new Event($e)); } return ''; } /** * initialize * * @throws SPException * @throws SessionTimeout */ protected function initialize(): void { try { $this->checks(); $this->checkAccess(AclActionsInterface::CONFIG_BACKUP); } catch (UnauthorizedPageException $e) { $this->eventDispatcher->notify('exception', new Event($e)); $this->returnJsonResponseException($e); } } }