db = $db; } // ── Factory ─────────────────────────────────────────────────────────────── /** * Convenience factory: loads the Database singleton and returns a ready * controller instance. */ public static function create(): self { require_once APP_ROOT . '/src/Database.php'; return new self(Database::getInstance()); } // ── Main entry point ───────────────────────────────────────────────────── /** * Process the current request. * * On success returns an array of view variables. * On failure (missing id, thesis not found) sends a redirect and exits. * * @return array */ public function handle(): array { $thesisId = isset($_GET['id']) ? (int) $_GET['id'] : 0; if ($thesisId <= 0) { $this->redirectHome(); } try { $data = $this->db->getThesisById($thesisId); } catch (Exception $e) { error_log('TfeController: ' . $e->getMessage()); $this->redirectHome(); } if (empty($data)) { $this->redirectHome(); } // Access type (1 = open, 2 = restricted, 3 = forbidden) $accessTypeId = $this->db->getThesisAccessTypeId($thesisId) ?? 1; $isInterdit = ($accessTypeId === 3); // Check if restricted files feature is enabled and user has access $restrictedEnabled = $this->db->isRestrictedFilesEnabled(); $hasRestrictedAccess = false; if ($restrictedEnabled && $accessTypeId === 2) { // Check for cookie-based access $cookieToken = $_COOKIE['tfe_access_' . $thesisId] ?? null; if ($cookieToken) { $hasRestrictedAccess = $this->db->hasValidCookieAccess($cookieToken, $thesisId); } } // If access is restricted and user doesn't have valid access, hide files $shouldHideFiles = ($restrictedEnabled && $accessTypeId === 2 && !$hasRestrictedAccess); // Caption (WebVTT) files — N-th VTT is paired with the N-th