diff --git a/api/index.php b/api/index.php new file mode 100644 index 0000000..86dc528 --- /dev/null +++ b/api/index.php @@ -0,0 +1,151 @@ +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."); +} \ No newline at end of file diff --git a/database.mwb b/database.mwb index 8801ccf..faa20ce 100644 Binary files a/database.mwb and b/database.mwb differ diff --git a/machine.php b/machine.php index 4db1bc9..a2a6204 100644 --- a/machine.php +++ b/machine.php @@ -59,4 +59,50 @@ class Machine { } return $info; } + + public function addHistory($date, $event, $notes = "") { + global $database; + if (strtotime($date) === false) { + throw new Exception("Invalid date."); + } + $date = date("Y-m-d H:i:s", strtotime($date)); + if (!$database->has('event_types', ['eventid' => $event])) { + throw new Exception("Invalid event type."); + } + $event = (int) $event; + if (empty($notes)) { + $notes = ""; + } + $database->insert('history', ['date' => $date, 'eventid' => $event, 'notes' => $notes, 'machineid' => $this->machineid]); + } + + public function addComponent($serial, $type, $tested = null, $notes = "", $capacity = null, $model = null) { + global $database; + if (empty($serial)) { + throw new Exception("Invalid serial number."); + } + if (!$database->has('component_types', ['typeid' => $type])) { + throw new Exception("Invalid component type."); + } + $type = (int) $type; + + if (!is_null($tested)) { + if (strtotime($tested) === false) { + throw new Exception("Invalid tested date."); + } + $tested = date("Y-m-d H:i:s", strtotime($tested)); + } + + if (empty($notes)) { + $notes = ""; + } + if (empty($capacity)) { + $capacity = null; + } + if (empty($model)) { + $model = null; + } + + $database->insert('components', ['serial' => $serial, 'typeid' => $type, 'tested' => $tested, 'notes' => $notes, 'capacity' => $capacity, 'model' => $model, 'machineid' => $this->machineid]); + } }