API improves

This commit is contained in:
Diego 2017-07-19 22:50:08 +02:00
parent 48a4077ebb
commit b7bb0449b9
1 changed files with 47 additions and 62 deletions

View File

@ -55,26 +55,20 @@ class pluginAPI extends Plugin {
// ------------------------------------------------------------ // ------------------------------------------------------------
$method = $this->getMethod(); $method = $this->getMethod();
// INPUTS // METHOD INPUTS
// ------------------------------------------------------------ // ------------------------------------------------------------
$inputs = $this->getInputs(); $inputs = $this->getMethodInputs();
if ( empty($inputs) ) { if ( empty($inputs) ) {
$this->response(array( $this->response(404,'Not Found', array('message'=>'Missing method inputs.'));
'status'=>'1',
'message'=>'Missing inputs.'
));
} }
// PARAMETERS // ENDPOINT PARAMETERS
// ------------------------------------------------------------ // ------------------------------------------------------------
$parameters = $this->getParameters($URI); $parameters = $this->getEndpointParameters($URI);
if ( empty($parameters) ) { if ( empty($parameters) ) {
$this->response(array( $this->response(404,'Not Found', array('message'=>'Missing endpoint parameters.'));
'status'=>'1',
'message'=>'Missing parameters.'
));
} }
// API TOKEN // API TOKEN
@ -83,18 +77,12 @@ class pluginAPI extends Plugin {
// Check empty token // Check empty token
if ( empty($inputs['token']) ) { if ( empty($inputs['token']) ) {
$this->response(array( $this->response(404,'Not Found', array('message'=>'Missing API token.'));
'status'=>'1',
'message'=>'Missing API token.'
));
} }
// Check the token is valid // Check the token is valid
if( $inputs['token']!=$tokenAPI ) { if ($inputs['token']!==$tokenAPI) {
$this->response(array( $this->response(401, 'Unauthorized', array('message'=>'Invalid API token.'));
'status'=>'1',
'message'=>'Invalid API token.'
));
} }
// AUTHENTICATION TOKEN // AUTHENTICATION TOKEN
@ -109,7 +97,7 @@ class pluginAPI extends Plugin {
} }
} }
// REQUESTS // ENDPOINTS
// ------------------------------------------------------------ // ------------------------------------------------------------
// (GET) /api/pages // (GET) /api/pages
@ -125,13 +113,10 @@ class pluginAPI extends Plugin {
$data = $this->newPage($inputs); $data = $this->newPage($inputs);
} }
else { else {
$data = array( $this->response(401, 'Unauthorized', array('message'=>'Access denied or invalid endpoint.'));
'status'=>'1',
'message'=>'Error: URI not found or Access denied.'
);
} }
$this->response($data); $this->response(200, 'OK', $data);
} }
// PRIVATE METHODS // PRIVATE METHODS
@ -150,25 +135,7 @@ class pluginAPI extends Plugin {
return $this->method; return $this->method;
} }
private function getParameters($URI) private function getMethodInputs()
{
// PARAMETERS
// ------------------------------------------------------------
// /api/pages | GET | returns all pages
// /api/pages/{key} | GET | returns the page with the {key}
// /api/cli/regenerate | POST | check for new posts and pages
$parameters = explode('/', $URI);
// Sanitize parameters
foreach($parameters as $key=>$value) {
$parameters[$key] = Sanitize::html($value);
}
return $parameters;
}
private function getInputs()
{ {
switch($this->method) { switch($this->method) {
case "POST": case "POST":
@ -189,6 +156,24 @@ class pluginAPI extends Plugin {
return $this->cleanInputs($inputs); return $this->cleanInputs($inputs);
} }
private function getEndpointParameters($URI)
{
// ENDPOINT Parameters
// ------------------------------------------------------------
// /api/pages | GET | returns all pages
// /api/pages/{key} | GET | returns the page with the {key}
// /api/pages | POST | create a new page
$parameters = explode('/', $URI);
// Sanitize parameters
foreach ($parameters as $key=>$value) {
$parameters[$key] = Sanitize::html($value);
}
return $parameters;
}
private function cleanInputs($inputs) private function cleanInputs($inputs)
{ {
$tmp = array(); $tmp = array();
@ -196,8 +181,7 @@ class pluginAPI extends Plugin {
foreach($inputs as $key=>$value) { foreach($inputs as $key=>$value) {
$tmp[$key] = Sanitize::html($value); $tmp[$key] = Sanitize::html($value);
} }
} } elseif ( is_string($inputs) ) {
elseif( is_string($inputs) ) {
$tmp = json_decode($inputs, true); $tmp = json_decode($inputs, true);
if(json_last_error()===0) { if(json_last_error()===0) {
$tmp = array(); $tmp = array();
@ -206,10 +190,11 @@ class pluginAPI extends Plugin {
return $tmp; return $tmp;
} }
private function response($data=array()) private function response($code=200, $message='OK', $data=array())
{ {
$json = json_encode($data); header('HTTP/1.1 '.$code.' '.$message);
header('Content-Type: application/json'); header('Content-Type: application/json');
$json = json_encode($data);
exit($json); exit($json);
} }