162 lines
4.7 KiB
PHP
162 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Routes;
|
|
|
|
/**************************************************/
|
|
/********************* Route **********************/
|
|
/**************************************************/
|
|
|
|
abstract class Route {
|
|
public function __construct() {}
|
|
|
|
public function Answer() {
|
|
$subroute = \Request::Subroute();
|
|
if (is_null($subroute)) {
|
|
switch (\Request::Method()) {
|
|
case "OPTIONS":
|
|
return $this->info();
|
|
case "HEAD":
|
|
$id = \Request::ID();
|
|
return $this->restrictFilter($id) && $this->head($id);
|
|
case "GET":
|
|
$id = \Request::ID();
|
|
if (is_null($id)) {
|
|
return $this->restrictFilter() && $this->head() && $this->getAll();
|
|
} else {
|
|
$json = [];
|
|
return $this->head($id) && $this->getOne($id, $json) && $this->checkOutput($id, $json);
|
|
}
|
|
case "POST":
|
|
$json = \Request::Input("content");
|
|
return $this->checkInput("insert", null, $json) && $this->insert($json) && $this->head();
|
|
case "PATCH":
|
|
$json = \Request::Input("content");
|
|
$id = \Request::ID();
|
|
return $this->checkInput("update", $id, $json) && $this->update($id, $json) && $this->head();
|
|
case "DELETE":
|
|
$id = \Request::ID();
|
|
return $this->checkInput("remove", $id) && $this->remove($id) && $this->head();
|
|
default:
|
|
\Response::Get()->NotImplemented();
|
|
return false;
|
|
}
|
|
} else {
|
|
$id = \Request::ID();
|
|
return $this->answerSub($subroute, $id) && $this->head($id) && $this->get($id) && $this->checkOutput($id, );
|
|
} }
|
|
|
|
abstract public static function Rights();
|
|
|
|
protected function info() {
|
|
$class = get_called_class();
|
|
\Response::Get()->Good()->Json("rights", $class::Rights()->Export());
|
|
return true;
|
|
}
|
|
|
|
protected function answerSub($subroute, $id) {
|
|
$class = "\\Resources\\".$this->resource;
|
|
$answerFnc = "answer_".$class::Get()->Table()."_".$subroute;
|
|
if (method_exists($this, $answerFnc)) {
|
|
$subid = \Request::SubID();
|
|
return $this->$answerFnc($id, $subid);
|
|
} else if (method_exists($this, "answer_Files") && $class::Get()->HasFile($subroute)) {
|
|
return $this->answer_Files($this->resource, $id, $subroute);
|
|
}
|
|
\Response::Get()->SubRouteNotExisting($subroute);
|
|
return false;
|
|
}
|
|
|
|
protected function head($id = null) {
|
|
$class = "\\Resources\\".$this->resource;
|
|
\Response::Get()->Good()->Etag($class::Get()->Checksum($id));
|
|
return true; // Needed for &&-chain-call
|
|
}
|
|
|
|
protected function getOne($id, &$json) {
|
|
$class = "\\Resources\\".$this->resource;
|
|
$resource = $class::Get()->Ref($id);
|
|
if ($resource->Load()) {
|
|
$json = $resource->Json();
|
|
return true;
|
|
}
|
|
|
|
\Response::Get()->NotFound();
|
|
return false;
|
|
}
|
|
|
|
protected function getAll() {
|
|
$class = "\\Resources\\".$this->resource;
|
|
$list = $class::Get()->RefAll();
|
|
$json = [];
|
|
foreach ($list as $resource) {
|
|
$json[] = $resource->Json();
|
|
}
|
|
\Response::Get()->Good()->Content($json);
|
|
return true;
|
|
\Response::Get()->NotFound();
|
|
return false;
|
|
}
|
|
|
|
protected function insert($json) {
|
|
$class = "\\Resources\\".$this->resource;
|
|
$newid = null;
|
|
if ($class::Get()->Insert($json, $newid)) {
|
|
\Response::Get()->Inserted($this->resource)->Content($class::Get()->Ref($newid)->Json());
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
protected function update($id, $json) {
|
|
$class = "\\Resources\\".$this->resource;
|
|
$resource = $class::Get()->Ref($id);
|
|
if ($resource->Load()) {
|
|
$resource->Patch($json);
|
|
if ($resource->Store()) {
|
|
\Response::Get()->Updated($this->resource)->Content($resource->Json());
|
|
return true;
|
|
} }
|
|
return false;
|
|
}
|
|
|
|
protected function remove($id) {
|
|
$class = "\\Resources\\".$this->resource;
|
|
if ($class::Get()->Remove($id)) {
|
|
\Response::Get()->Deleted($this->resource);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
protected function restrictFilter() {
|
|
if (get_called_class()::Rights()->RestrictFilter()) {
|
|
return true;
|
|
}
|
|
\Response::Get()->IllegalQuery();
|
|
return false;
|
|
}
|
|
|
|
protected function checkOutput($id, $json) {
|
|
if (get_called_class()::Rights()->CheckInput("get", $id, $json)) {
|
|
\Response::Get()->Good()->Content($json);
|
|
return true;
|
|
}
|
|
\Response::Get()->IllegalQuery();
|
|
return false;
|
|
}
|
|
|
|
protected function checkInput($action, $id = null, $json = []) {
|
|
if (is_null($id) && in_array($action, ["update", "remove"])) {
|
|
\Response::Get()->MissingID();
|
|
return false;
|
|
} else if ((is_null($json) || empty($json)) && in_array($action, ["insert", "update"])) {
|
|
\Response::Get()->MissingContent();
|
|
return false;
|
|
} else if (!get_called_class()::Rights()->CheckInput($action, $id, $json)) {
|
|
\Response::Get()->IllegalInput();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|