db was intended, it should be $this->db = Database::getInstance(); $this->authService = new AuthService(Database::getInstance()); $this->langService = new LanguageService(APP_ROOT_DIR . '/src/Lang/'); // $this->db = Database::getInstance(); // Uncomment if $db should be a class property accessible in index() via $this->db } /** * Affiche le tableau de bord. * Redirige vers la page de connexion si l'utilisateur n'est pas connecté. */ public function index() { if (!$this->authService->isLoggedIn()) { header('Location: /login'); exit(); } // Récupère les traductions et les informations pour la vue global $translations; $currentLang = $this->langService->getLanguage(); $username = $this->authService->getUsername(); $darkModeClass = DarkMode::getBodyClass(); $userRole = $this->authService->getUserRole(); // Pour afficher/masquer certains éléments // Initialize database connection // If $this->db was initialized in constructor, use $db = $this->db; $db = Database::getInstance(); // Using a local instance as per original structure // Initialize structured certificates array $structuredCertificates = [ 'root' => null, 'intermediates' => [], ]; // Fetch Root Certificate $stmt = $db->prepare("SELECT name FROM certificates WHERE type = 'root' LIMIT 1"); $stmt->execute(); $rootCert = $stmt->fetch(); if ($rootCert) { $structuredCertificates['root'] = [ 'name' => $rootCert['name'], 'cert_path' => ROOT_CA_PATH . '/certs/ca.cert.pem', 'key_path' => ROOT_CA_PATH . '/private/ca.key.pem', // Corrected path as per instructions ]; } else { // Handle case where root certificate is not found, though unlikely // You might want to log this or set a default structure $structuredCertificates['root'] = [ 'name' => 'N/A', 'cert_path' => null, 'key_path' => null, ]; } // Fetch Intermediate Certificates $stmt = $db->prepare(" SELECT c.id, c.name, c.functional_perimeter_id, fp.name as perimeter_name FROM certificates c JOIN functional_perimeters fp ON c.functional_perimeter_id = fp.id WHERE c.type = 'intermediate' ORDER BY fp.name ASC, c.name ASC "); $stmt->execute(); $intermediateCerts = $stmt->fetchAll(); foreach ($intermediateCerts as $interCert) { $intermediateData = [ 'id' => $interCert['id'], 'name' => $interCert['name'], 'perimeter_name' => $interCert['perimeter_name'], 'functional_perimeter_id' => $interCert['functional_perimeter_id'], // Pass perimeter_id for linking 'final_certificates' => [], ]; // Fetch Final Certificates for this Intermediate // Ensure functional_perimeter_id is used in the query for 'simple' certificates $stmtFinal = $db->prepare(" SELECT name, type, expiration_date, is_revoked FROM certificates WHERE type = 'simple' AND functional_perimeter_id = ? ORDER BY name ASC "); // Use $interCert['functional_perimeter_id'] which is the ID of the perimeter for this intermediate cert $stmtFinal->execute([$interCert['functional_perimeter_id']]); $finalCerts = $stmtFinal->fetchAll(); if ($finalCerts) { foreach ($finalCerts as $finalCert) { $intermediateData['final_certificates'][] = $finalCert; } } $structuredCertificates['intermediates'][] = $intermediateData; } // Pass data to the view require_once APP_ROOT_DIR . '/src/Views/dashboard/index.php'; } }