feat: PHP API foundation - router, config, helpers, SM-2 algorithm
Dieser Commit ist enthalten in:
Ursprung
9f942a0e56
Commit
581888e142
4 geänderte Dateien mit 152 neuen und 0 gelöschten Zeilen
41
edu/api/helpers.php
Normale Datei
41
edu/api/helpers.php
Normale Datei
|
|
@ -0,0 +1,41 @@
|
|||
<?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;
|
||||
}
|
||||
Laden …
Tabelle hinzufügen
Einen Link hinzufügen
In neuem Issue referenzieren