edu-senex/edu/api/helpers.php

42 Zeilen
917 B
PHP

<?php
header('Content-Type: application/json; charset=utf-8');
function json_ok($data) {
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;
}
function json_error($message, $code = 400) {
http_response_code($code);
echo json_encode(['error' => $message], JSON_UNESCAPED_UNICODE);
exit;
}
function require_auth() {
if (empty($_SESSION['user_id'])) {
json_error('Nicht angemeldet', 401);
}
return $_SESSION['user_id'];
}
function require_admin() {
require_auth();
if (empty($_SESSION['is_admin'])) {
json_error('Keine Admin-Berechtigung', 403);
}
return $_SESSION['user_id'];
}
function get_method() {
return $_SERVER['REQUEST_METHOD'];
}
function get_json_body() {
$body = file_get_contents('php://input');
return json_decode($body, true) ?: [];
}
function get_param($name, $default = null) {
return $_GET[$name] ?? $default;
}