add files
This commit is contained in:
253
api.php
Normal file
253
api.php
Normal file
@@ -0,0 +1,253 @@
|
||||
<?php
|
||||
include("config/config.php");
|
||||
|
||||
$json_data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
// Überprüfen, welcher Endpunkt aufgerufen wurde
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// Login-Endpunkt
|
||||
if (isset($json_data['action']) && $json_data['action'] === 'login') {
|
||||
handleLogin(false);
|
||||
} elseif (isset($json_data['action']) && $json_data['action'] === 'stats') {
|
||||
// Statistik-Endpunkt
|
||||
handleStats();
|
||||
}elseif (isset($json_data['action']) && $json_data['action'] === 'customers') {
|
||||
// Statistik-Endpunkt
|
||||
handleCustomer();
|
||||
}elseif (isset($json_data['action']) && $json_data['action'] === 'orders') {
|
||||
// Statistik-Endpunkt
|
||||
handleOrders();
|
||||
} else {
|
||||
// Fehler: Ungültiger Endpunkt
|
||||
respondWithError('Ungültiger Endpunkt.');
|
||||
}
|
||||
} else {
|
||||
// Fehler: Ungültige Anfrage-Methode
|
||||
respondWithError('Ungültige Anfrage-Methode.');
|
||||
}
|
||||
|
||||
// Funktion zum Verarbeiten des Login-Endpunkts
|
||||
function handleLogin($intern) {
|
||||
global $db, $schluessel_api, $json_data, $secretKey, $login_response;
|
||||
|
||||
// Antwort-Array initialisieren
|
||||
$response = array(
|
||||
'login' => false,
|
||||
'schluessel' => false
|
||||
);
|
||||
|
||||
|
||||
|
||||
// Prüfen, ob der Schlüssel vorhanden und korrekt ist
|
||||
if (isset($json_data['schluessel']) && $json_data['schluessel'] == $schluessel_api) {
|
||||
$response['schluessel'] = true;
|
||||
|
||||
// Nur wenn der Schlüssel korrekt ist, die Benutzeranmeldeinformationen prüfen
|
||||
if (isset($json_data['benutzer']) && isset($json_data['passwort'])) {
|
||||
$myusername = mysqli_real_escape_string($db, $json_data['benutzer']);
|
||||
$mypassword = mysqli_real_escape_string($db, $json_data['passwort']);
|
||||
$mypassword = hash_hmac('md5', $mypassword, $secretKey); // Passwort hashen
|
||||
|
||||
// SQL-Abfrage mit Prepared Statement ausführen
|
||||
$stmt = mysqli_prepare($db, "SELECT * FROM admin WHERE username = ? AND passcode = ?");
|
||||
mysqli_stmt_bind_param($stmt, 'ss', $myusername, $mypassword);
|
||||
mysqli_stmt_execute($stmt);
|
||||
mysqli_stmt_store_result($stmt);
|
||||
|
||||
// Überprüfen, ob ein Datensatz gefunden wurde
|
||||
if (mysqli_stmt_num_rows($stmt) == 1) {
|
||||
$response['login'] = true;
|
||||
$login_response = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// JSON-Antwort senden
|
||||
if (!$intern) {
|
||||
echo json_encode($response);
|
||||
}
|
||||
}
|
||||
|
||||
// Funktion zum Verarbeiten des Statistik-Endpunkts
|
||||
function handleStats() {
|
||||
global $db, $schluessel_api, $json_data, $login_response;
|
||||
|
||||
// Antwort-Array initialisieren
|
||||
$response = array(
|
||||
'stats' => null
|
||||
);
|
||||
|
||||
// Prüfen, ob die Anfrage authentifiziert ist
|
||||
if (isset($json_data['schluessel']) && $json_data['schluessel'] == $schluessel_api && isset($json_data['benutzer']) && isset($json_data['passwort']) && isset($json_data['hash'])) {
|
||||
// Login-Endpunkt intern aufrufen, um die Authentifizierung durchzuführen
|
||||
handleLogin(true);
|
||||
|
||||
// Überprüfen, ob der Login erfolgreich war
|
||||
if ($login_response) {
|
||||
// Statistikdaten abrufen und in das Antwort-Array einfügen
|
||||
$yearlyStats = array();
|
||||
|
||||
// SQL-Abfrage für die Statistiken
|
||||
$sql = "SELECT * FROM aufträge";
|
||||
$result = mysqli_query($db, $sql);
|
||||
|
||||
while ($row = mysqli_fetch_assoc($result)) {
|
||||
$year = date('Y', strtotime($row['auftrag_beginn']));
|
||||
|
||||
if (!isset($yearlyStats[$year])) {
|
||||
$yearlyStats[$year] = array(
|
||||
'offeneAufträge' => 0,
|
||||
'beendeteAufträge' => 0,
|
||||
'umsatz' => 0,
|
||||
'gewinn' => 0,
|
||||
);
|
||||
}
|
||||
|
||||
if ($row['beendet'] == '1') {
|
||||
$yearlyStats[$year]['beendeteAufträge']++;
|
||||
} else {
|
||||
$yearlyStats[$year]['offeneAufträge']++;
|
||||
}
|
||||
|
||||
$json = json_decode($row['materialien'], true);
|
||||
$preis = $row['preis'];
|
||||
|
||||
$materialienSum = 0;
|
||||
foreach ($json as $key => $value) {
|
||||
$split = explode(":", $value);
|
||||
$materialienSum += str_replace(',', '.', $split[1]);
|
||||
}
|
||||
|
||||
$yearlyStats[$year]['umsatz'] += $preis;
|
||||
$yearlyStats[$year]['gewinn'] += ($preis - $materialienSum);
|
||||
}
|
||||
|
||||
|
||||
// Konvertieren des Arrays in einen JSON-String
|
||||
$jsonString = json_encode($yearlyStats);
|
||||
|
||||
// Hashen des JSON-Strings
|
||||
$hash = hash('sha256', $jsonString);
|
||||
|
||||
|
||||
if ( $json_data["hash"] != $hash)
|
||||
{
|
||||
$response['stats'] = $yearlyStats;
|
||||
$response['hash'] = $hash;
|
||||
}else {
|
||||
$response['hash'] = $hash;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// JSON-Antwort senden
|
||||
echo json_encode($response);
|
||||
}
|
||||
|
||||
|
||||
// Funktion zum Verarbeiten des Kunden-Endpunkts
|
||||
function handleCustomer() {
|
||||
global $db, $schluessel_api, $json_data, $login_response;
|
||||
|
||||
// Antwort-Array initialisieren
|
||||
$response = array(
|
||||
'customer' => array()
|
||||
);
|
||||
|
||||
// Prüfen, ob die Anfrage authentifiziert ist
|
||||
if (isset($json_data['schluessel']) && $json_data['schluessel'] == $schluessel_api && isset($json_data['benutzer']) && isset($json_data['passwort'])) {
|
||||
|
||||
// Login-Endpunkt intern aufrufen, um die Authentifizierung durchzuführen
|
||||
handleLogin(true);
|
||||
|
||||
// Überprüfen, ob der Login erfolgreich war
|
||||
if ($login_response) {
|
||||
$sql = "SELECT * FROM kunden ORDER BY vorname";
|
||||
$result = mysqli_query($db, $sql);
|
||||
|
||||
if (mysqli_num_rows($result) > 0) {
|
||||
while ($row = mysqli_fetch_assoc($result)) {
|
||||
$customer = array(
|
||||
'id' => $row['id'],
|
||||
'vorname' => $row['vorname'],
|
||||
'nachname' => $row['nachname'],
|
||||
'straße' => $row['straße'],
|
||||
'ort' => $row['ort'],
|
||||
'plz' => $row['plz'],
|
||||
'telefonnummer' => json_decode($row['telefonnummer'], true),
|
||||
'fahrzeuge' => json_decode($row['fahrzeuge'], true)
|
||||
);
|
||||
$response['customer'][] = $customer;
|
||||
}
|
||||
} else {
|
||||
$response['message'] = "Keine Kundendaten gefunden.";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$response['error'] = "Ungültige Anfrage oder Authentifizierung fehlgeschlagen.";
|
||||
}
|
||||
|
||||
// JSON-Antwort senden
|
||||
echo json_encode($response);
|
||||
}
|
||||
|
||||
// Funktion zum Verarbeiten des Aufträge-Endpunkts
|
||||
function handleOrders() {
|
||||
global $db, $schluessel_api, $json_data, $login_response;
|
||||
|
||||
// Antwort-Array initialisieren
|
||||
$response = array(
|
||||
'orders' => array()
|
||||
);
|
||||
|
||||
// Prüfen, ob die Anfrage authentifiziert ist
|
||||
if (isset($json_data['schluessel']) && $json_data['schluessel'] == $schluessel_api && isset($json_data['benutzer']) && isset($json_data['passwort'])) {
|
||||
|
||||
// Login-Endpunkt intern aufrufen, um die Authentifizierung durchzuführen
|
||||
handleLogin(true);
|
||||
|
||||
// Überprüfen, ob der Login erfolgreich war
|
||||
if ($login_response) {
|
||||
$sql = "SELECT * FROM aufträge";
|
||||
$result = mysqli_query($db, $sql);
|
||||
|
||||
if (mysqli_num_rows($result) > 0) {
|
||||
while ($row = mysqli_fetch_assoc($result)) {
|
||||
$orders = array(
|
||||
'id' => $row['id'],
|
||||
'beendet' => $row['beendet'],
|
||||
'kunden_id' => $row['kunden_id'],
|
||||
'auftragname' => $row['auftragname'],
|
||||
'auftrag_beschreibung' => $row['auftrag_beschreibung'],
|
||||
'auftrag_beginn' => $row['auftrag_beginn'],
|
||||
'auftrag_ende' => $row['auftrag_ende'],
|
||||
'materialien' => json_decode($row['materialien'], true),
|
||||
'preis' => $row['preis'],
|
||||
'anzahlung' => $row['anzahlung'],
|
||||
'fahrzeug' => $row['fahrzeug']
|
||||
);
|
||||
$response['orders'][] = $orders;
|
||||
}
|
||||
} else {
|
||||
$response['message'] = "Keine Kundendaten gefunden.";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$response['error'] = "Ungültige Anfrage oder Authentifizierung fehlgeschlagen.";
|
||||
}
|
||||
|
||||
// JSON-Antwort senden
|
||||
echo json_encode($response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Funktion zum Senden einer Fehlerantwort im JSON-Format
|
||||
function respondWithError($message) {
|
||||
$response = array('error' => $message);
|
||||
echo json_encode($response);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user