Initial commit

This commit is contained in:
root
2026-11-11 09:35:51 +01:00
commit 981066958d
18 changed files with 2904 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
<?php
require_once __DIR__ . '/config.php';
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }
start_session();
$action = $_GET['action'] ?? '';
$body = json_decode(file_get_contents('php://input'), true) ?? [];
try {
$db = getDB();
switch ($action) {
case 'login':
$username = trim($body['username'] ?? '');
$password = $body['password'] ?? '';
if (!$username || !$password)
json_response(['error' => 'Identifiant et mot de passe requis.'], 400);
$stmt = $db->prepare("SELECT * FROM accounts WHERE BINARY username = :u LIMIT 1");
$stmt->execute([':u' => $username]);
$account = $stmt->fetch();
if (!$account || !password_verify($password, $account['password'])) {
sleep(1);
json_response(['error' => 'Identifiant ou mot de passe incorrect.'], 401);
}
session_regenerate_id(true);
$_SESSION['account_id'] = $account['id'];
$_SESSION['username'] = $account['username'];
$_SESSION['role'] = $account['role'];
$_SESSION['display_name'] = $account['display_name'];
$_SESSION['color'] = $account['color'];
$_SESSION['avatar'] = $account['avatar'];
$_SESSION['profile_id'] = $account['profile_id'];
$_SESSION['expires_at'] = time() + SESSION_LIFETIME;
$db->prepare("UPDATE accounts SET last_login=NOW() WHERE id=:id")
->execute([':id' => $account['id']]);
json_response(['success' => true, 'role' => $account['role'], 'redirect' => 'index.html']);
case 'logout':
session_destroy();
json_response(['success' => true]);
case 'me':
$acc = current_account();
if (!$acc) json_response(['authenticated' => false], 401);
json_response(['authenticated' => true, 'account' => $acc]);
case 'change_password':
$acc = require_auth(true);
$old = $body['old_password'] ?? '';
$new = $body['new_password'] ?? '';
if (strlen($new) < 8)
json_response(['error' => 'Minimum 8 caractères.'], 400);
$stmt = $db->prepare("SELECT password FROM accounts WHERE id=:id");
$stmt->execute([':id' => $acc['id']]);
if (!password_verify($old, $stmt->fetchColumn()))
json_response(['error' => 'Mot de passe actuel incorrect.'], 403);
$db->prepare("UPDATE accounts SET password=:p WHERE id=:id")
->execute([':p' => password_hash($new, PASSWORD_BCRYPT, ['cost'=>12]), ':id' => $acc['id']]);
json_response(['success' => true]);
default:
json_response(['error' => 'Action inconnue'], 404);
}
} catch (PDOException $e) {
json_response(['error' => 'DB: ' . $e->getMessage()], 500);
}