You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.5 KiB
PHP

<?php
ob_start();
header('Content-Type: application/json');
require_once __DIR__ . "/../required.php";
require_once __DIR__ . "/../machine.php";
$VARS;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$VARS = $_POST;
} else {
$VARS = $_GET;
}
if (!$database->has('apikeys', ['key' => $VARS['key']])) {
http_response_code(403);
die('{"status": "ERROR", "message": "Invalid API key. Access denied."}');
}
function sendError($type, $msg = "An error occurred.") {
$code = 404;
switch ($type) {
case "nomachineid":
$code = 400;
$msg = "No machine ID sent.";
break;
case "dberror":
$code = 500;
$msg = "The database encountered an error: $msg";
}
http_response_code($code);
die(json_encode([
"status" => "ERROR",
"message" => $msg
]));
}
switch ($VARS['action']) {
/* Get info */
case "getmachineinfo":
if (empty($VARS['id'])) {
sendError("nomachineid");
}
try {
$machine = new Machine($VARS['id']);
echo json_encode($machine->getMachineInfo());
} catch (Exception $e) {
sendError("", $e->getMessage());
}
break;
case "getmachinehistory":
if (empty($VARS['id'])) {
sendError("nomachineid");
}
try {
$machine = new Machine($VARS['id']);
echo json_encode($machine->getHistory());
} catch (Exception $e) {
sendError("", $e->getMessage());
}
break;
case "getmachinecomponents":
if (empty($VARS['id'])) {
sendError("nomachineid");
}
try {
$machine = new Machine($VARS['id']);
echo json_encode($machine->getComponents());
} catch (Exception $e) {
sendError("", $e->getMessage());
}
break;
case "geteventtypes":
echo json_encode($database->select('event_types', ['eventid (id)', 'eventname (name)']));
break;
case "getcomponenttypes":
echo json_encode($database->select('component_types', ['typeid (id)', 'typename (name)']));
break;
/* Save info */
case "addmachine":
if (empty($VARS['id'])) {
sendError("nomachineid");
}
if ($database->has('machines', ['machineid' => $VARS['id']])) {
sendError("", "A machine with that ID already exists.");
}
$data = [];
$data['machineid'] = $VARS['id'];
if (empty($VARS['notes'])) {
$data['notes'] = "";
} else {
$data['notes'] = $VARS['notes'];
}
if (!empty($VARS['model'])) {
$data['model'] = $VARS['model'];
}
if (!empty($VARS['condition'])) {
if (is_numeric($VARS['condition']) && $VARS['condition'] > 0 && $VARS['condition'] < 10) {
$data['condition'] = $VARS['condition'] * 1.0;
} else {
sendError("", "Machine condition must be a number and 0 < condition < 10.");
}
}
if (!empty($VARS['price'])) {
if (is_numeric($VARS['price']) && $VARS['price'] > 0 && $VARS['price'] < 10000.0) {
$data['price'] = $VARS['price'] * 1.0;
} else {
sendError("", "Machine price must be a number and 0 < price < 10000.");
}
}
$database->insert('machines', $data);
if ($database->error()[1] != 0) {
sendError("dberror", $database->error()[2]);
}
exit(json_encode(["status" => "OK"]));
break;
case "addhistory":
if (empty($VARS['id'])) {
sendError("nomachineid");
}
try {
$machine = new Machine($VARS['id']);
$machine->addHistory($VARS['date'], $VARS['event'], $VARS['notes']);
exit(json_encode(["status" => "OK"]));
} catch (Exception $e) {
sendError("", $e->getMessage());
}
break;
case "addcomponent":
if (empty($VARS['id'])) {
sendError("nomachineid");
}
try {
$machine = new Machine($VARS['id']);
$machine->addComponent($VARS['serial'], $VARS['type'], $VARS['tested'], $VARS['notes'], $VARS['capacity'], $VARS['model']);
exit(json_encode(["status" => "OK"]));
} catch (Exception $e) {
sendError("", $e->getMessage());
}
break;
default:
sendError("", "Invalid action or no action sent.");
}