From b58a9ef2f0e8dc230ef3b7cf0391694a2153b130 Mon Sep 17 00:00:00 2001 From: Bandie Date: Mon, 18 Jan 2021 20:41:58 +0100 Subject: [PATCH] The init API-API --- README.md | 7 +++ config.php | 4 ++ statusTemplate.json | 1 + update.php | 115 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 README.md create mode 100644 config.php create mode 100644 statusTemplate.json create mode 100644 update.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..2e9be66 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# SpaceAPI-API + +Since we've got two doors we need to do a simple OR into our Open-Status. +This API manipulates our fine status.json. + +Here's the live endpoint: https://status.chaospott.de/api/update.php +Here's where to put all this stuff: /data/docker/status.chaospott.de/webroot/api/ diff --git a/config.php b/config.php new file mode 100644 index 0000000..b6fdc9f --- /dev/null +++ b/config.php @@ -0,0 +1,4 @@ + + */ + + +include('config.php'); + + +/** Set the status array for publishing the new™ status + * @param array &$status - Status array which will be written into the status.json + * @param array $decoded - Decoded json coming from client + * @param string $loc - 'aerie' or 'cellar' + * @return bool - Value has been updated or not? + */ +function setStatus(&$status, $decoded, $loc){ + $locno = null; + + switch($loc) { + case 'aerie': + $locno = 0; + break; + case 'cellar': + $locno = 1; + break; + } + + if(isset($decoded[$loc]) && is_bool($decoded[$loc])){ + if($status["sensors"]["door_locked"][$locno]["value"] != $decoded[$loc]){ + $status["sensors"]["door_locked"][$locno]["value"] = $decoded[$loc]; + return true; + } + } + + return false; +} + +// Is this a POST? +if ($_SERVER['REQUEST_METHOD'] != 'POST') { + header("HTTP/1.1 405 Method Not Allowed"); + echo('FAILED - not POST - '. $_SERVER['REQUEST_METHOD']); + exit(); +} + + +// Is it application/json? +$content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : ''; +if ($content_type != 'application/json') { + header("HTTP/1.1 403 Forbidden"); + echo('FAILED - not application/json - '. $content_type); + exit(); +} + + +// Do we even have a payload? +$payload = trim(file_get_contents("php://input")); +if (empty($payload)) { + header("HTTP/1.1 403 Forbidden"); + echo('FAILED - no payload'); + exit(); +} + +// Is the payload even JSON? +$decoded = json_decode($payload, true); +if (json_last_error() !== JSON_ERROR_NONE) { + header("HTTP/1.1 403 Forbidden"); + header("Content-Type: application/json"); + echo('"FAILED - json decode - '. json_last_error()); + exit(); +} + + +// Is the authorisation aight m8? +if($decoded['consumer_key'] != $consumer_key || $decoded['consumer_secret'] != $consumer_secret) { + header("HTTP/1.1 403 Forbidden"); + echo("WRONG SECRET"); + error_log('FAILED - wrong secret key'); + exit(); +} + + +// Is this API even active? +if(!$active){ + header("HTTP/1.1 403 Forbidden"); + echo(json_encode(["status" => "API deactivated."])); + error_log('API deactivated'); + exit(); +} + + +// Get me the previous status +$status = json_decode(file_get_contents("../status.json"), true); + +// Manipulate the $status variable. If we have an update, update the timestamp. +if(setStatus($status, $decoded, 'aerie') | setStatus($status, $decoded, 'cellar')){ + $status["state"]["lastchange"] = time(); +} + +// Are we open or not? +if(!$status["sensors"]["door_locked"][0]["value"] || !$status["sensors"]["door_locked"][1]["value"]){ + $status["state"]["open"] = true; +} else { + $status["state"]["open"] = false; +} + +// Encode this shit +$json = json_encode($status); + +// Write this shit +file_put_contents("../status.json", $json); + +// Show what we've done! +echo($json); + +?>