Compare commits

...

3 Commits

Author SHA1 Message Date
drg
f2e410e7cb bumped to spaceAPI v14 2023-10-29 20:49:47 +01:00
c50584018e JSON responses; missing var name 2021-01-18 21:35:25 +01:00
3fcff9803e Path to status.json in config 2021-01-18 20:51:33 +01:00
3 changed files with 19 additions and 9 deletions

View File

@ -2,3 +2,4 @@
$active = false;
$consumer_key = 'asdf';
$consumer_secret = 'qwer';
$statusJSONFile = '../status.json';

View File

@ -1 +1 @@
{"api":"0.13","contact":{"email":"info@chaospott.de","facebook":"https:\/\/www.facebook.com\/pages\/foobar-eV\/127246337351928","foursquare":"4f41445ce4b0c868de54bb9f","google":[],"irc":"irc:\/\/irc.hackint.org\/#chaospott","issue_mail":"support@chaospott.de","mastodon":"https:\/\/chaos.social\/@chaospott","matrix":"#chaospott:matrix.chaospott.de","ml":"discuss@lists.chaospott.de","phone":"+49 201 75915275","twitter":"@chaospott"},"feeds":{"blog":{"type":"application\/rss+xml","url":"https:\/\/chaospott.de\/feed.xml"},"calendar":{"type":"ical","url":"https:\/\/cloud.chaospott.ru\/remote.php\/dav\/public-calendars\/5HM7B0ZOLEYC3QD0?export"}},"issue_report_channels":["issue_mail","ml"],"location":{"address":"Sibyllastr. 9, 45136 Essen, Germany","lat":51.438476,"lon":7.024991},"logo":"http:\/\/chaospott.de\/images\/logo.png","projects":["https:\/\/wiki.chaospott.de\/projekte:start","https:\/\/git.chaospott.de\/","https:\/\/github.com\/c3e"],"sensors":{"door_locked":[{"location":"aerie","value":true},{"location":"cellar","value":true}]},"space":"chaospott","state":{"lastchange":1610991840,"open":false},"url":"https:\/\/chaospott.de"}
{"api_compatibility":["14"],"contact":{"email":"info@chaospott.de","irc":"irc:\/\/irc.hackint.org\/#chaospott","issue_mail":"support@chaospott.de","mastodon":"https:\/\/chaos.social\/@chaospott","matrix":"#chaospott:matrix.chaospott.de","ml":"discuss@lists.chaospott.de","mumble":"mumble:\/\/mumble.chaospott.de","phone":"+49 201 85892243"},"feeds":{"blog":{"type":"application\/rss+xml","url":"https:\/\/chaospott.de\/feed.xml"},"calendar":{"type":"ical","url":"https:\/\/cloud.chaospott.de\/remote.php\/dav\/public-calendars\/5HM7B0ZOLEYC3QD0?export"}},"issue_report_channels":["issue_mail","ml"],"location":{"address":"Sibyllastr. 9, 45136 Essen, Germany","lat":51.438476,"lon":7.024991},"logo":"https:\/\/chaospott.de\/images\/logo.png","projects":["https:\/\/wiki.chaospott.de\/projekte:start","https:\/\/git.chaospott.de\/"],"sensors":{"door_locked":[{"location":"aerie","value":true},{"location":"cellar","value":true}]},"space":"Chaospott","state":{"icon":{"open":"https:\/\/img.shields.io\/badge\/chaospott-open-lightgreen","closed":"https:\/\/img.shields.io\/badge\/chaospott-closed-red"},"lastchange":1698532531,"open":false},"url":"https:\/\/chaospott.de","ext_ccc":"erfa","links":[{"name":"Sibyllinische Neuigkeiten","description":"In unserem Podcast berichten wir \u00fcber unser Clubleben und unterhalten uns \u00fcber verschiedene Technikthemen.","url":"https:\/\/podcast.chaospott.de"}]}

View File

@ -38,7 +38,9 @@ function setStatus(&$status, $decoded, $loc){
// Is this a POST?
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
header("HTTP/1.1 405 Method Not Allowed");
echo('FAILED - not POST - '. $_SERVER['REQUEST_METHOD']);
header("Content-Type: application/json");
echo(json_encode(['status' => 'No POST. Received: '. $_SERVER['REQUEST_METHOD']]));
error_log('No POST.');
exit();
}
@ -47,7 +49,9 @@ if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$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);
header("Content-Type: application/json");
echo(json_encode(['status' => 'FAILED - not application/json - '. $content_type]));
error_log('No application/json');
exit();
}
@ -56,7 +60,9 @@ if ($content_type != 'application/json') {
$payload = trim(file_get_contents("php://input"));
if (empty($payload)) {
header("HTTP/1.1 403 Forbidden");
echo('FAILED - no payload');
header("Content-Type: application/json");
echo(json_encode(['status' => 'No payload']));
error_log('No payload');
exit();
}
@ -65,7 +71,8 @@ $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());
echo(json_encode(['status' => 'FAILED - json decode - '. json_last_error()]));
error_log('JSON decode error');
exit();
}
@ -73,8 +80,9 @@ if (json_last_error() !== JSON_ERROR_NONE) {
// 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');
header("Content-Type: application/json");
echo(json_encode(['status' => 'Wrong key/secret.']));
error_log('Wrong key/secret.');
exit();
}
@ -82,6 +90,7 @@ if($decoded['consumer_key'] != $consumer_key || $decoded['consumer_secret'] != $
// Is this API even active?
if(!$active){
header("HTTP/1.1 403 Forbidden");
header("Content-Type: application/json");
echo(json_encode(["status" => "API deactivated."]));
error_log('API deactivated');
exit();
@ -89,7 +98,7 @@ if(!$active){
// Get me the previous status
$status = json_decode(file_get_contents("../status.json"), true);
$status = json_decode(file_get_contents($statusJSONFile), true);
// Manipulate the $status variable. If we have an update, update the timestamp.
if(setStatus($status, $decoded, 'aerie') | setStatus($status, $decoded, 'cellar')){
@ -107,7 +116,7 @@ if(!$status["sensors"]["door_locked"][0]["value"] || !$status["sensors"]["door_l
$json = json_encode($status);
// Write this shit
file_put_contents("../status.json", $json);
file_put_contents($statusJSONFile, $json);
// Show what we've done!
echo($json);