true, 'user' => session::getName()]); } else { // L'utilisateur n'est pas connecté return json_encode(['authenticated' => false]); } } /** * Authentifier l'utilisateur et maintenir la session PHP via un cookie */ public static function authenticate(array $_input) { $ch = curl_init(); $data = [ 'email' => $_input["email"], 'password' => $_input["password"], ]; curl_setopt($ch, CURLOPT_URL, API_AUTHENT); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Activer le suivi des redirections curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Désactiver la vérification SSL (à ne pas utiliser en production) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Gérer les cookies pour maintenir la session curl_setopt($ch, CURLOPT_COOKIEJAR, self::$cookieFile); curl_setopt($ch, CURLOPT_COOKIEFILE, self::$cookieFile); // Exécuter la requête cURL $response = curl_exec($ch); // Fermer la session cURL curl_close($ch); return $response; } /** * Faire une requête API authentifiée avec la session PHP active */ public static function makeAuthenticatedRequest($endpoint, $method = 'GET', $data = []) { $ch = curl_init(); $url = DOMAIN_API . $endpoint; curl_setopt($ch, CURLOPT_URL, $url . "/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Utiliser les cookies pour maintenir la session curl_setopt($ch, CURLOPT_COOKIEFILE, self::$cookieFile); // Configuration des méthodes GET/POST/PUT/DELETE if ($method === 'POST') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); } elseif ($method === 'PUT') { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); } elseif ($method === 'DELETE') { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); } $response = curl_exec($ch); curl_close($ch); if ($response === false) { throw new Exception('Erreur lors de la requête API.'); } return json_decode($response, true); } /** * Déconnexion (facultatif) */ public static function logout() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, API_LOGOUT); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIEFILE, self::$cookieFile); $response = curl_exec($ch); curl_close($ch); // Supprimer le fichier cookie après déconnexion unlink(self::$cookieFile); } }