Backup #103

ID103
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Controller/BackupRestoreController.php
Version1
Typ modified
Größe3.4 KB
Hashc60b1bc320938f975652cb005fb410c7baa16682b673173644978e109f7e4b36
Datum2025-12-20 19:19:08
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Controller;

use Framework\Controller;
use Infrastructure\Persistence\FileBackupRepository;

/**
 * Controller for File Backup management.
 */
class BackupRestoreController extends Controller
{
    private FileBackupRepository $repository;

    public function __construct()
    {
        $this->repository = new FileBackupRepository();
    }

    /**
     * GET /backup-restore
     * List all backups.
     */
    public function index(): void
    {
        $filters = [];

        if (isset($_GET['search']) && $_GET['search'] !== '') {
            $filters['search'] = $_GET['search'];
        }

        if (isset($_GET['change_type']) && $_GET['change_type'] !== '') {
            $filters['change_type'] = $_GET['change_type'];
        }

        $limit = 50;
        $offset = (int) ($_GET['offset'] ?? 0);

        $backups = $this->repository->findAll($filters, $limit, $offset);
        $total = $this->repository->count($filters);
        $stats = $this->repository->getStatistics();

        $this->view('backup-restore.index', [
            'title' => 'File Backup',
            'backups' => $backups,
            'stats' => $stats,
            'total' => $total,
            'filters' => $filters,
        ]);
    }

    /**
     * GET /backup-restore/{id}
     * Show backup details.
     */
    public function show(string $id): void
    {
        $backup = $this->repository->findById((int) $id);

        if ($backup === null) {
            http_response_code(404);
            echo '404 - Backup nicht gefunden';

            return;
        }

        // Get content preview (max 500 lines)
        $contentPreview = $this->repository->getContentPreview((int) $id, 500);

        // Get other versions of this file
        $versions = $this->repository->findByFilePath($backup['file_path']);

        // Check if file currently exists
        $fileExists = file_exists($backup['file_path']);

        $this->view('backup-restore.show', [
            'title' => 'Backup #' . $id,
            'backup' => $backup,
            'contentPreview' => $contentPreview,
            'versions' => $versions,
            'fileExists' => $fileExists,
        ]);
    }

    /**
     * POST /backup-restore/{id}/restore
     * Restore file from backup.
     */
    public function restore(string $id): void
    {
        try {
            $this->repository->restore((int) $id);

            // Redirect with success message
            header('Location: /backup-restore/' . $id . '?restored=1');
        } catch (\RuntimeException $e) {
            // Redirect with error message
            header('Location: /backup-restore/' . $id . '?error=' . urlencode($e->getMessage()));
        }
    }

    /**
     * GET /backup-restore/{id}/download
     * Download full backup content as file.
     */
    public function download(string $id): void
    {
        $backup = $this->repository->findById((int) $id);

        if ($backup === null) {
            http_response_code(404);
            echo '404 - Backup nicht gefunden';

            return;
        }

        $filename = basename($backup['file_path']) . '.backup-v' . $backup['version'];

        header('Content-Type: text/plain; charset=utf-8');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Content-Length: ' . strlen($backup['file_content']));

        echo $backup['file_content'];
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

← Zurück zur Übersicht