41 lines
1.0 KiB
PHP
41 lines
1.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
if (session_status() === PHP_SESSION_NONE) session_start();
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
require_once __DIR__ . '/../public/includes/init_pr_cl.php';
|
|
|
|
|
|
$PUSHCFG = require __DIR__ . '/config.php';
|
|
|
|
function json_response(array $data, int $status = 200): never {
|
|
http_response_code($status);
|
|
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Auth JSON (pas de redirect HTML)
|
|
*/
|
|
function require_auth_json(?array $roles = null): void {
|
|
if (!is_logged_in()) {
|
|
json_response(['ok'=>false, 'error'=>'auth_required'], 401);
|
|
}
|
|
if ($roles) {
|
|
$role = strtolower((string)($_SESSION['role'] ?? ''));
|
|
$allowed = array_map(fn($r)=>strtolower((string)$r), $roles);
|
|
if (!in_array($role, $allowed, true)) {
|
|
json_response(['ok'=>false, 'error'=>'forbidden'], 403);
|
|
}
|
|
}
|
|
}
|
|
|
|
function read_json_body(): array {
|
|
$raw = file_get_contents('php://input');
|
|
if (!$raw) return [];
|
|
$data = json_decode($raw, true);
|
|
return is_array($data) ? $data : [];
|
|
}
|