149 lines
5.0 KiB
PHP
149 lines
5.0 KiB
PHP
<?php
|
|
/**
|
|
* API PROTOTYPE : Connexion LibreOffice Calc → MySQL
|
|
* Version : 3.0 FINALE
|
|
* Date : 29 décembre 2025
|
|
*/
|
|
|
|
error_reporting(0);
|
|
ini_set('display_errors', 0);
|
|
ob_start();
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
require_once '../config/database.php';
|
|
|
|
try {
|
|
$db = Database::getInstance();
|
|
$conn = $db->getConnection();
|
|
|
|
$log_file = '/var/www/mathematiques/logs/api_calc.log';
|
|
function write_log($message) {
|
|
global $log_file;
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
@file_put_contents($log_file, "[$timestamp] $message\n", FILE_APPEND);
|
|
}
|
|
|
|
write_log("=== NOUVELLE REQUÊTE ===");
|
|
|
|
$raw_input = file_get_contents('php://input');
|
|
write_log("Raw input: " . $raw_input);
|
|
|
|
$data = json_decode($raw_input, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
throw new Exception('JSON invalide: ' . json_last_error_msg());
|
|
}
|
|
|
|
$action = $data['action'] ?? 'test';
|
|
write_log("Action: $action");
|
|
|
|
switch ($action) {
|
|
|
|
case 'test':
|
|
write_log("Test OK");
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => '✅ API Calc opérationnelle',
|
|
'server_time' => date('Y-m-d H:i:s'),
|
|
'database' => 'mathematiques_db',
|
|
'version' => '3.0-finale'
|
|
], JSON_UNESCAPED_UNICODE);
|
|
break;
|
|
|
|
case 'login_calc':
|
|
// Accepter login OU nom_utilisateur
|
|
$login = $data['login'] ?? $data['nom_utilisateur'] ?? null;
|
|
$mot_de_passe = $data['mot_de_passe'] ?? null;
|
|
$computer_name = $data['computer_name'] ?? 'INCONNU';
|
|
$computer_username = $data['computer_username'] ?? 'INCONNU';
|
|
|
|
write_log("Login attempt: login=$login, mdp=" . (empty($mot_de_passe) ? 'vide' : 'présent'));
|
|
|
|
if (empty($login) || empty($mot_de_passe)) {
|
|
write_log("ERREUR: Paramètres manquants");
|
|
throw new Exception("Login et mot de passe requis");
|
|
}
|
|
|
|
write_log("Recherche utilisateur: $login");
|
|
|
|
$stmt = $conn->prepare("
|
|
SELECT id_utilisateur, nom, prenom, id_classe, mot_de_passe, id_type
|
|
FROM utilisateurs
|
|
WHERE login = ? AND actif = 1
|
|
");
|
|
$stmt->execute([$login]);
|
|
$eleve = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$eleve) {
|
|
write_log("ERREUR: Utilisateur non trouvé");
|
|
throw new Exception("Utilisateur non trouvé ou inactif");
|
|
}
|
|
|
|
write_log("Utilisateur trouvé: id=" . $eleve['id_utilisateur'], JSON_UNESCAPED_UNICODE);
|
|
|
|
if (!password_verify($mot_de_passe, $eleve['mot_de_passe'])) {
|
|
write_log("ERREUR: Mot de passe incorrect");
|
|
throw new Exception("Mot de passe incorrect");
|
|
}
|
|
|
|
$session_token = bin2hex(random_bytes(32));
|
|
|
|
write_log("Login réussi: token=$session_token");
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Authentification réussie',
|
|
'data' => [
|
|
'id_eleve' => $eleve['id_utilisateur'],
|
|
'nom_complet' => $eleve['prenom'] . ' ' . $eleve['nom'],
|
|
'id_classe' => $eleve['id_classe'],
|
|
'session_token' => $session_token
|
|
]
|
|
], JSON_UNESCAPED_UNICODE);
|
|
break;
|
|
|
|
case 'save_response':
|
|
$session_token = $data['session_token'] ?? null;
|
|
$id_question = $data['id_question'] ?? null;
|
|
$reponse = $data['reponse'] ?? null;
|
|
|
|
if (!$session_token || !$id_question || !$reponse) {
|
|
throw new Exception("Paramètres manquants");
|
|
}
|
|
|
|
write_log("Sauvegarde simulée: Q$id_question -> $reponse");
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Réponse enregistrée (simulation)',
|
|
'data' => [
|
|
'id_question' => $id_question,
|
|
'reponse' => $reponse,
|
|
'saved_at' => date('Y-m-d H:i:s')
|
|
]
|
|
], JSON_UNESCAPED_UNICODE);
|
|
break;
|
|
|
|
default:
|
|
throw new Exception("Action inconnue: $action");
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
write_log("EXCEPTION: " . $e->getMessage());
|
|
http_response_code(400);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $e->getMessage()
|
|
], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
ob_end_flush();
|
|
?>
|