juggl/public/api/services/apiBranch.inc.php

45 lines
No EOL
1.3 KiB
PHP

<?php
require_once(__DIR__."/authenticator.inc.php");
require_once(__DIR__."/responses.inc.php");
require_once(__DIR__."/requestTypes.inc.php");
require_once(__DIR__."/paramCleaner.inc.php");
abstract class ApiBranch {
function get (ParamCleaner $params) {}
function post (ParamCleaner $params) {}
function authenticationMissing (ParamCleaner $params) {
respondStatus(403);
}
function execute ($authenticationRequired = true) {
$params = $this->getParams();
$currentType = currentRequestType();
if ($currentType === RequestType::OPTIONS) {
respondStatus(200);
}
if ($authenticationRequired) {
$auth = new Authenticator();
if (!$auth->isAuthenticated($params)) {
$this->authenticationMissing($params);
return;
}
}
if ($currentType === RequestType::GET) {
$this->get($params);
} else if ($currentType === RequestType::POST) {
$this->post($params);
} else {
respondStatus(405);
}
}
private function getParams() {
$content = json_decode(file_get_contents('php://input'), true);
if ($content == NULL) $content = array();
return new ParamCleaner(array_merge($content, $_REQUEST, $_SESSION, $_FILES));
}
}
?>