<?php
// Author: Bandie <bandie@chaospott.de>
// Project: Yipanic
// License: GNU-GPLv3

define("_KEY_", "somekey");
define("_SEC_", "somesecret");

$json = json_decode(file_get_contents('php://input', true));

function checkCreds($json){
	return (_KEY_ == $json->key && _SEC_ == $json->secret);
}

function execCmd($cmd) {
  exec("/usr/bin/python3 /var/www/ding/ding -c /var/www/ding/ding.cfg $cmd >> /dev/null", $output, $retval);
  return $retval;
}

function execCmdInfra($cmd) {
  exec("/usr/bin/python3 /var/www/dingInfra/ding -c /var/www/dingInfra/ding.cfg $cmd >> /dev/null", $output, $retval);
  return $retval;
}

if(checkCreds($json)){
	$access = true;

	switch($json->cmd) {
	  case "lock":
	    $retval = execCmd("lock");
	    break;
	  case "shutdown":
	    $retval = execCmd("shutdown");
	    break;
	  case "panic":
	    $retval = execCmd("panic");
	    break;
	  case "infraShutdown":
	    $retval = execCmdInfra("shutdown");
	    break;
	  case "infraPanic":
	    $retval = execCmdInfra("panic");
	    break;
	}

} else {
	$access = false;
}


$ret = array(
	"access" => $access,
	"error" => $retval
);


header('Content-type: application/json');

echo(json_encode($ret));

?>


