Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4782a2c401 | ||
|
|
3d8c4291be | ||
|
|
a0afeabb27 | ||
|
|
96c27a8722 | ||
|
|
4478f7e82f | ||
|
|
c1f3c3ea4e | ||
|
|
2a8564d3bf | ||
|
|
14f4143207 |
No files matched your search
+67
-54
@@ -1,71 +1,84 @@
|
||||
<?php
|
||||
include_once '../controller/DatabaseController.php';
|
||||
include_once '../controller/DataController.php';
|
||||
include_once __DIR__ . '/../modules/Mail.php';
|
||||
include_once(__DIR__ . '/../controller/DatabaseController.php');
|
||||
include_once(__DIR__ . '/../controller/DataController.php');
|
||||
include_once(__DIR__ . '/../modules/Mail.php');
|
||||
|
||||
header("Content-Type: application/json");
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Headers: Authorization, Function, Content-Type");
|
||||
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit();
|
||||
}
|
||||
// 1. Daten aus der URL (GET) statt aus dem Header holen
|
||||
$token = $_GET['token'] ?? '';
|
||||
$function = $_GET['action'] ?? ''; // Ich habe es 'action' genannt, passend zur URL
|
||||
|
||||
$validTokens = [
|
||||
"15a437d128c918633e57dc7d18118a073b807644f21731ed5a7ebcf3eca9e242",
|
||||
];
|
||||
|
||||
$headers = getallheaders();
|
||||
$token = $headers['Authorization'] ?? $headers['authorization'] ?? $_SERVER['HTTP_AUTHORIZATION'] ?? '';
|
||||
$function = $headers['Function'] ?? $headers['function'] ?? '';
|
||||
|
||||
if (!in_array($token, $validTokens)) {
|
||||
http_response_code(401);
|
||||
$error = json_encode([
|
||||
"error" => "Unauthorized",
|
||||
"debug_received_token" => $token,
|
||||
"debug_all_headers" => $headers
|
||||
]);
|
||||
errorMail(
|
||||
"API Login failed",
|
||||
$error,
|
||||
true,
|
||||
);
|
||||
echo $error;
|
||||
// Prüfen, ob überhaupt etwas übergeben wurde
|
||||
if (empty($token) || empty($function)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(["error" => "Token oder Action fehlt in der URL"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (in_array($token, $validTokens)) {
|
||||
// 2. Token-Validierung direkt in der Datenbank (Performanter!)
|
||||
// Annahme: Deine DatabaseShow kann Filter. Wenn nicht, hier das SQL anpassen.
|
||||
$checkToken = DatabaseShow(
|
||||
"API_Credentials",
|
||||
"TOKEN",
|
||||
"TOKEN = ?",
|
||||
$token
|
||||
);
|
||||
|
||||
if (empty($checkToken)) {
|
||||
http_response_code(401);
|
||||
|
||||
// Loggen des Fehlversuchs
|
||||
DatabaseInsert(
|
||||
"API",
|
||||
"CLIENT_IP, TOKEN, API, FUNKTION",
|
||||
"API_LOGS",
|
||||
"CLIENT_IP, TOKEN, API, FUNKTION, JSON",
|
||||
(new getSpecificData())->getClientIP(),
|
||||
$token,
|
||||
(new getSpecificData())->getRequestURL(),
|
||||
$function
|
||||
$function,
|
||||
json_encode(["error" => "Unauthorized"])
|
||||
);
|
||||
|
||||
if ($function === "IPs") {
|
||||
$data = DatabaseShow(
|
||||
"logs",
|
||||
"CLIENT_IP"
|
||||
);
|
||||
}
|
||||
|
||||
if ($function === "Ueberstunden") {
|
||||
$data = DatabaseShow(
|
||||
"Ueberstunden",
|
||||
"MINUTEN"
|
||||
);
|
||||
}
|
||||
|
||||
echo json_encode(
|
||||
[
|
||||
"success" => true,
|
||||
"data" => $data,
|
||||
]
|
||||
);
|
||||
echo json_encode(["error" => "Zugriff verweigert", "hint" => "Token ungültig"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 3. Logik-Teil mit switch (sauberer als viele IFs)
|
||||
$data = null;
|
||||
|
||||
switch ($function) {
|
||||
case "UeberstundenAbbau":
|
||||
$data = DatabaseShow("Ueberstunden", "DATUM, MINUTEN", "GRUND = ?", "Abbau");
|
||||
break;
|
||||
|
||||
case "UeberstundenAktuell":
|
||||
$conn = db_connect();
|
||||
$sql = "SELECT
|
||||
(SELECT COALESCE(SUM(OVERTIME), 0) FROM Firmenberichte) AS berichte,
|
||||
(SELECT COALESCE(SUM(MINUTEN), 0) FROM Ueberstunden WHERE GRUND = 'Abbau') AS abbau,
|
||||
(SELECT COALESCE(SUM(MINUTEN), 0) FROM Ueberstunden WHERE GRUND = 'Aufbau') AS aufbau";
|
||||
$res = $conn->query($sql);
|
||||
$row = $res->fetch_object();
|
||||
$data = ($row->berichte - $row->abbau + $row->aufbau) / 60;
|
||||
break;
|
||||
|
||||
case "Berichte":
|
||||
$data = DatabaseShowWithFK("Berufsschulberichte", $Berufsschulberichte);
|
||||
break;
|
||||
|
||||
case "Stundenplan":
|
||||
$data = DatabaseShowWithFK("Stundenplan", $Stundenplan, "ZEITEN, MO, DI, MI, DO, FR");
|
||||
break;
|
||||
|
||||
default:
|
||||
http_response_code(404);
|
||||
echo json_encode(["error" => "Funktion '$function' nicht gefunden"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 4. Erfolgreiche Antwort
|
||||
echo json_encode([
|
||||
"success" => true,
|
||||
"data" => $data
|
||||
]);
|
||||
@@ -3,7 +3,7 @@
|
||||
class getSpecificData {
|
||||
|
||||
public function getWeather() {
|
||||
$url = "https://api.openweathermap.org/data/2.5/weather?q=Berlin,DE&APPID=5bc3e1a438c58fca444c4370ac0517b2&units=metric";
|
||||
$url = "https://api.openweathermap.org/data/2.5/weather?q=Jena,DE&APPID=5bc3e1a438c58fca444c4370ac0517b2&units=metric";
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
|
||||
@@ -147,10 +147,37 @@ function DatabaseUpdate($table, $set, $where, ...$values) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function getFKValue($conn, $table, $key, $value, $id, &$cache) {
|
||||
if ($id === null) return null;
|
||||
|
||||
$cacheKey = $table . "_" . $id;
|
||||
|
||||
if (isset($cache[$cacheKey])) {
|
||||
return $cache[$cacheKey];
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("SELECT `$value` FROM `$table` WHERE `$key` = ? LIMIT 1");
|
||||
if (!$stmt) {
|
||||
die("FK Prepare Fehler: " . $conn->error);
|
||||
}
|
||||
|
||||
$stmt->bind_param("i", $id);
|
||||
$stmt->execute();
|
||||
|
||||
$result = $stmt->get_result()->fetch_assoc();
|
||||
$valueResult = $result[$value] ?? null;
|
||||
|
||||
$cache[$cacheKey] = $valueResult;
|
||||
|
||||
return $valueResult;
|
||||
}
|
||||
|
||||
function DatabaseShowWithFK($table, $fkMap = [], $columns = "*", $where = "", ...$values) {
|
||||
include_once 'DatabaseConnect.php';
|
||||
$conn = db_connect();
|
||||
|
||||
$cache = [];
|
||||
|
||||
$select = is_array($columns) ? implode(", ", $columns) : $columns;
|
||||
|
||||
$sql = "SELECT $select FROM `$table`";
|
||||
@@ -161,33 +188,32 @@ function DatabaseShowWithFK($table, $fkMap = [], $columns = "*", $where = "", ..
|
||||
$stmt = $conn->prepare($sql);
|
||||
if (!$stmt) die("Fehler beim Prepare: " . $conn->error);
|
||||
|
||||
if (!$stmt->execute($values)) die("Fehler beim Execute: " . $stmt->error);
|
||||
if (!empty($values)) {
|
||||
$stmt->execute($values);
|
||||
} else {
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
$result = $stmt->get_result();
|
||||
if (!$result) die("Fehler beim get_result: " . $stmt->error);
|
||||
|
||||
$columnsResult = [];
|
||||
while ($fieldinfo = $result->fetch_field()) {
|
||||
$columnsResult[] = $fieldinfo->name;
|
||||
}
|
||||
if (!$result) die("Fehler beim get_result: " . $conn->error);
|
||||
|
||||
$rows = $result->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
foreach ($rows as &$row) {
|
||||
foreach ($fkMap as $fkColumn => $fkInfo) {
|
||||
if (isset($row[$fkColumn]) && $row[$fkColumn] !== null) {
|
||||
$fkStmt = $conn->prepare(
|
||||
"SELECT {$fkInfo['value']} FROM `{$fkInfo['table']}` WHERE {$fkInfo['key']} = ?"
|
||||
);
|
||||
if (!$fkStmt) die("Fehler beim Prepare FK: " . $conn->error);
|
||||
|
||||
$fkStmt->bind_param("i", $row[$fkColumn]);
|
||||
$fkStmt->execute();
|
||||
$fkResult = $fkStmt->get_result();
|
||||
$fkRow = $fkResult->fetch_assoc();
|
||||
|
||||
$row[$fkColumn] = $fkRow ? $fkRow[$fkInfo['value']] : null;
|
||||
if (!isset($row[$fkColumn]) || $row[$fkColumn] === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$row[$fkColumn] = getFKValue(
|
||||
$conn,
|
||||
$fkInfo['table'],
|
||||
$fkInfo['key'],
|
||||
$fkInfo['value'],
|
||||
$row[$fkColumn],
|
||||
$cache
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user