2018-02-15 06:47:35 +00:00
|
|
|
<?php
|
2018-12-18 08:46:36 +00:00
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
2018-02-15 06:47:35 +00:00
|
|
|
|
|
|
|
// check whether API interface is enabled after all
|
2018-12-18 08:46:36 +00:00
|
|
|
if (\Froxlor\Settings::Get('api.enabled') != 1) {
|
2018-02-15 06:47:35 +00:00
|
|
|
// not enabled
|
|
|
|
header("Status: 404 Not found", 404);
|
|
|
|
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not found", 404);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're talking json here
|
|
|
|
header("Content-Type:application/json");
|
|
|
|
|
|
|
|
// get our request
|
2018-02-15 10:37:38 +00:00
|
|
|
$request = @file_get_contents('php://input');
|
2018-02-15 06:47:35 +00:00
|
|
|
|
|
|
|
// check if present
|
|
|
|
if (empty($request)) {
|
|
|
|
json_response(400, "Invalid request");
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode json request
|
|
|
|
$decoded_request = json_decode(stripslashes($request), true);
|
|
|
|
|
|
|
|
// is it valid?
|
|
|
|
if (is_null($decoded_request)) {
|
|
|
|
json_response(400, "Invalid JSON");
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate content
|
|
|
|
try {
|
2018-12-18 08:46:36 +00:00
|
|
|
$request = \Froxlor\Api\FroxlorRPC::validateRequest($decoded_request);
|
2018-02-15 06:47:35 +00:00
|
|
|
// now actually do it
|
2018-12-19 13:12:42 +00:00
|
|
|
$cls = "\\Froxlor\\Api\\Commands\\" . $request['command']['class'];
|
2018-02-15 06:47:35 +00:00
|
|
|
$method = $request['command']['method'];
|
|
|
|
$apiObj = new $cls($decoded_request['header'], $request['params']);
|
|
|
|
// call the method with the params if any
|
|
|
|
echo $apiObj->$method();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
json_response($e->getCode(), $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
exit();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* output json result
|
|
|
|
*
|
|
|
|
* @param int $status
|
|
|
|
* @param string $status_message
|
|
|
|
* @param mixed $data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-02-23 10:08:24 +00:00
|
|
|
function json_response($status, $status_message = '', $data = null)
|
2018-02-15 06:47:35 +00:00
|
|
|
{
|
2018-02-25 10:47:28 +00:00
|
|
|
if (isset($_SERVER["SERVER_PROTOCOL"]) && ! empty($_SERVER["SERVER_PROTOCOL"])) {
|
|
|
|
$resheader = $_SERVER["SERVER_PROTOCOL"] . " " . $status;
|
|
|
|
if (! empty($status_message)) {
|
|
|
|
$resheader .= ' ' . str_replace("\n", " ", $status_message);
|
|
|
|
}
|
|
|
|
header($resheader);
|
2018-02-23 10:08:24 +00:00
|
|
|
}
|
2018-11-17 19:14:58 +00:00
|
|
|
$response = array();
|
2018-02-15 06:47:35 +00:00
|
|
|
$response['status'] = $status;
|
|
|
|
$response['status_message'] = $status_message;
|
|
|
|
$response['data'] = $data;
|
|
|
|
|
2018-02-15 10:37:38 +00:00
|
|
|
$json_response = json_encode($response, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
|
2018-02-15 06:47:35 +00:00
|
|
|
echo $json_response;
|
|
|
|
exit();
|
|
|
|
}
|