bludit/bl-plugins/api/plugin.php

343 lines
8.3 KiB
PHP
Raw Normal View History

2016-05-29 19:21:11 +02:00
<?php
class pluginAPI extends Plugin {
2017-06-25 22:54:59 +02:00
private $method;
public function init()
{
// Generate the API Token
$token = md5( uniqid().time().DOMAIN );
$this->dbFields = array(
'token'=>$token, // API Token
2017-06-22 23:50:12 +02:00
'amountOfItems'=>15 // Amount of items to return
);
}
public function form()
{
2017-06-25 22:54:59 +02:00
global $Language;
2018-07-01 14:17:24 +02:00
$html = '<div class="alert alert-primary" role="alert">';
$html .= $this->description();
$html .= '</div>';
$html .= '<div>';
2017-06-22 23:50:12 +02:00
$html .= '<label>'.$Language->get('API Token').'</label>';
2017-06-25 22:54:59 +02:00
$html .= '<input name="token" type="text" value="'.$this->getValue('token').'">';
2017-06-22 23:50:12 +02:00
$html .= '<span class="tip">'.$Language->get('This token is for read only and is regenerated every time you install the plugin').'</span>';
$html .= '</div>';
$html .= '<div>';
2017-06-22 23:50:12 +02:00
$html .= '<label>'.$Language->get('Amount of pages').'</label>';
$html .= '<input id="jsamountOfItems" name="amountOfItems" type="text" value="'.$this->getValue('amountOfItems').'">';
$html .= '<span class="tip">'.$Language->get('This is the maximum of pages to return when you call to').'</span>';
$html .= '</div>';
2016-06-03 03:37:52 +02:00
return $html;
}
2016-12-01 02:39:16 +01:00
// API HOOKS
// ----------------------------------------------------------------------------
2017-07-05 23:30:30 +02:00
public function beforeAll()
2016-09-25 20:38:15 +02:00
{
global $url;
2016-12-02 00:59:58 +01:00
global $dbPages;
global $dbUsers;
2016-09-25 20:38:15 +02:00
// CHECK URL
// ------------------------------------------------------------
2017-07-29 01:20:47 +02:00
$URI = $this->webhook('api', $returnsAfterURI=true, $fixed=false);
2017-07-19 22:50:08 +02:00
if ($URI===false) {
2016-12-01 02:39:16 +01:00
return false;
}
2017-06-25 22:54:59 +02:00
// METHOD
// ------------------------------------------------------------
$method = $this->getMethod();
2017-07-19 22:50:08 +02:00
// METHOD INPUTS
2016-12-01 02:39:16 +01:00
// ------------------------------------------------------------
2017-07-19 22:50:08 +02:00
$inputs = $this->getMethodInputs();
2016-12-01 02:39:16 +01:00
2017-07-19 22:50:08 +02:00
if ( empty($inputs) ) {
2017-08-30 20:02:31 +02:00
$this->response(404, 'Not Found', array('message'=>'Missing method inputs.'));
2017-07-07 23:38:01 +02:00
}
2017-07-19 22:50:08 +02:00
// ENDPOINT PARAMETERS
2016-12-01 02:39:16 +01:00
// ------------------------------------------------------------
2017-07-19 22:50:08 +02:00
$parameters = $this->getEndpointParameters($URI);
2016-12-01 02:39:16 +01:00
2017-07-19 22:50:08 +02:00
if ( empty($parameters) ) {
2017-08-30 20:02:31 +02:00
$this->response(404, 'Not Found', array('message'=>'Missing endpoint parameters.'));
2017-07-07 23:38:01 +02:00
}
// API TOKEN
// ------------------------------------------------------------
2017-08-30 20:02:31 +02:00
// Token from the plugin, the user can change it on the settings of the plugin
$tokenAPI = $this->getValue('token');
2016-12-01 02:39:16 +01:00
// Check empty token
2017-09-22 23:11:08 +02:00
if (empty($inputs['token'])) {
2017-08-30 20:02:31 +02:00
$this->response(404, 'Not Found', array('message'=>'Missing API token.'));
2016-12-01 02:39:16 +01:00
}
2017-08-30 20:02:31 +02:00
// Check if the token is valid
2017-07-19 22:50:08 +02:00
if ($inputs['token']!==$tokenAPI) {
$this->response(401, 'Unauthorized', array('message'=>'Invalid API token.'));
2016-12-01 02:39:16 +01:00
}
// AUTHENTICATION TOKEN
2016-12-01 02:39:16 +01:00
// ------------------------------------------------------------
$writePermissions = false;
2017-07-19 22:50:08 +02:00
if ( !empty($inputs['authentication']) ) {
2017-09-23 13:10:05 +02:00
// Get the user with the authentication token, FALSE if doesn't exit
$username = $dbUsers->getByAuthToken($inputs['authentication']);
2017-07-19 22:50:08 +02:00
if ($username!==false) {
2017-09-23 13:10:05 +02:00
// Get the object user to check the role
$user = $dbUsers->getUser($username);
2017-09-23 15:15:29 +02:00
if (($user->role()=='admin') && ($user->enabled())) {
2017-09-23 13:10:05 +02:00
// Loggin the user to create the session
$login = new Login();
$login->setLogin($username, 'admin');
2017-09-23 13:10:05 +02:00
// Enable write permissions
$writePermissions = true;
}
2016-12-01 02:39:16 +01:00
}
}
2017-07-19 22:50:08 +02:00
// ENDPOINTS
2017-06-22 23:50:12 +02:00
// ------------------------------------------------------------
2016-12-01 02:39:16 +01:00
2017-06-22 23:50:12 +02:00
// (GET) /api/pages
2017-07-19 22:50:08 +02:00
if ( ($method==='GET') && ($parameters[0]==='pages') && empty($parameters[1]) ) {
2017-06-22 23:50:12 +02:00
$data = $this->getPages();
2016-12-01 02:39:16 +01:00
}
2017-06-22 23:50:12 +02:00
// (GET) /api/pages/<key>
2017-07-19 22:50:08 +02:00
elseif ( ($method==='GET') && ($parameters[0]==='pages') && !empty($parameters[1]) ) {
2017-09-21 20:42:03 +02:00
$pageKey = $parameters[1];
$data = $this->getPage($pageKey);
}
// (PUT) /api/pages/<key>
elseif ( ($method==='PUT') && ($parameters[0]==='pages') && !empty($parameters[1]) && $writePermissions ) {
$pageKey = $parameters[1];
$data = $this->editPage($pageKey, $inputs);
}
// (DELETE) /api/pages/<key>
elseif ( ($method==='DELETE') && ($parameters[0]==='pages') && !empty($parameters[1]) && $writePermissions ) {
$pageKey = $parameters[1];
$data = $this->deletePage($pageKey);
2016-12-01 02:39:16 +01:00
}
2017-06-22 23:50:12 +02:00
// (POST) /api/pages
2017-07-19 22:50:08 +02:00
elseif ( ($method==='POST') && ($parameters[0]==='pages') && empty($parameters[1]) && $writePermissions ) {
2017-09-21 20:42:03 +02:00
$data = $this->createPage($inputs);
2017-06-22 23:50:12 +02:00
}
else {
2017-07-19 22:50:08 +02:00
$this->response(401, 'Unauthorized', array('message'=>'Access denied or invalid endpoint.'));
2016-12-02 00:59:58 +01:00
}
2017-06-22 23:50:12 +02:00
2017-07-19 22:50:08 +02:00
$this->response(200, 'OK', $data);
2016-09-25 20:38:15 +02:00
}
// PRIVATE METHODS
2016-12-01 02:39:16 +01:00
// ----------------------------------------------------------------------------
2017-06-25 22:54:59 +02:00
private function getMethod()
{
// METHODS
// ------------------------------------------------------------
// GET
// POST
// PUT
// DELETE
$this->method = $_SERVER['REQUEST_METHOD'];
return $this->method;
}
2017-07-19 22:50:08 +02:00
private function getMethodInputs()
{
2017-06-25 22:54:59 +02:00
switch($this->method) {
case "POST":
$inputs = $_POST;
break;
case "GET":
case "DELETE":
$inputs = $_GET;
break;
case "PUT":
2017-10-20 20:34:22 +02:00
$inputs = '';
break;
default:
$inputs = json_encode(array());
break;
}
2017-10-20 20:34:22 +02:00
// Try to get raw/json data
2017-09-22 23:11:08 +02:00
if (empty($inputs)) {
$inputs = file_get_contents('php://input');
}
2017-07-07 23:38:01 +02:00
return $this->cleanInputs($inputs);
}
2017-10-20 20:34:22 +02:00
// Returns an array with key=>value
// If the content is JSON is parsed to array
private function cleanInputs($inputs)
{
$tmp = array();
if (is_array($inputs)) {
foreach ($inputs as $key=>$value) {
$tmp[$key] = Sanitize::html($value);
}
} elseif (is_string($inputs)) {
$tmp = json_decode($inputs, true);
if (json_last_error()!==JSON_ERROR_NONE) {
$tmp = array();
}
}
return $tmp;
}
2017-07-19 22:50:08 +02:00
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
2017-08-30 20:02:31 +02:00
$URI = ltrim($URI, '/');
2017-07-19 22:50:08 +02:00
$parameters = explode('/', $URI);
// Sanitize parameters
foreach ($parameters as $key=>$value) {
$parameters[$key] = Sanitize::html($value);
}
return $parameters;
}
private function response($code=200, $message='OK', $data=array())
2016-12-01 19:09:29 +01:00
{
2017-07-19 22:50:08 +02:00
header('HTTP/1.1 '.$code.' '.$message);
2017-08-30 20:02:31 +02:00
header('Access-Control-Allow-Origin: *');
2016-12-01 19:09:29 +01:00
header('Content-Type: application/json');
2017-07-19 22:50:08 +02:00
$json = json_encode($data);
2016-12-01 19:09:29 +01:00
exit($json);
}
2017-06-22 23:50:12 +02:00
private function getPages()
2016-06-06 04:24:15 +02:00
{
2017-06-22 23:50:12 +02:00
global $dbPages;
$onlyPublished = true;
$amountOfItems = $this->getValue('amountOfItems');
$pageNumber = 1;
$list = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
2016-06-06 04:24:15 +02:00
2016-12-01 19:09:29 +01:00
$tmp = array(
'status'=>'0',
2017-06-22 23:50:12 +02:00
'message'=>'List of pages, amount of items: '.$amountOfItems,
2016-12-02 00:59:58 +01:00
'data'=>array()
2016-12-01 19:09:29 +01:00
);
2016-06-06 04:24:15 +02:00
2017-06-22 23:50:12 +02:00
// Get keys of pages
2017-12-26 17:45:02 +01:00
foreach ($list as $pageKey) {
2017-06-22 23:50:12 +02:00
// Create the page object from the page key
$page = buildPage($pageKey);
2017-06-25 22:54:59 +02:00
array_push($tmp['data'], $page->json( $returnsArray=true ));
2016-06-06 04:24:15 +02:00
}
2016-12-01 19:09:29 +01:00
return $tmp;
2016-06-06 04:24:15 +02:00
}
2017-09-21 20:42:03 +02:00
private function getPage($key)
{
// Generate the object Page
$page = buildPage($key);
2017-09-21 20:42:03 +02:00
if (!$page) {
2017-09-21 20:42:03 +02:00
return array(
'status'=>'1',
'message'=>'Page not found.'
);
}
return array(
'status'=>'0',
'message'=>'Page filtered by key: '.$key,
'data'=>$page->json( $returnsArray=true )
2017-09-21 20:42:03 +02:00
);
}
2017-07-07 23:38:01 +02:00
private function createPage($args)
2017-06-22 23:50:12 +02:00
{
// Unsanitize content because all values are sanitized
if (isset($args['content'])) {
$args['content'] = Sanitize::htmlDecode($args['content']);
}
2017-06-22 23:50:12 +02:00
// This function is defined on functions.php
2017-09-21 20:42:03 +02:00
$key = createPage($args);
2017-09-23 13:10:05 +02:00
2017-09-21 20:42:03 +02:00
if ($key===false) {
return array(
'status'=>'1',
'message'=>'Error trying to create the new page.'
);
}
return array(
'status'=>'0',
'message'=>'Page created.',
'data'=>array('key'=>$key)
);
}
private function editPage($key, $args)
{
// Unsanitize content because all values are sanitized
if (isset($args['content'])) {
$args['content'] = Text::htmlDecode($args['content']);
}
2017-09-21 20:42:03 +02:00
$args['key'] = $key;
$newKey = editPage($args);
if ($newKey===false) {
return array(
'status'=>'1',
'message'=>'Error trying to edit the page.'
);
}
return array(
'status'=>'0',
'message'=>'Page edited.',
'data'=>array('key'=>$newKey)
);
}
private function deletePage($key)
{
if (deletePage($key)) {
return array(
'status'=>'0',
'message'=>'Page deleted.'
);
}
return array(
'status'=>'1',
'message'=>'Error trying to delete the page.'
);
2017-06-22 23:50:12 +02:00
}
2016-12-02 00:59:58 +01:00
}