2015-07-07 12:09:38 +00:00
|
|
|
<?php
|
2015-09-12 17:52:04 +00:00
|
|
|
/**
|
|
|
|
* FuzeWorks
|
|
|
|
*
|
|
|
|
* The FuzeWorks MVC PHP FrameWork
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 TechFuze
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author TechFuze
|
|
|
|
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
|
|
|
|
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
|
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
|
|
|
|
* @link http://fuzeworks.techfuze.net
|
|
|
|
* @since Version 0.0.1
|
|
|
|
* @version Version 0.0.1
|
|
|
|
*/
|
2015-07-07 12:09:38 +00:00
|
|
|
|
|
|
|
namespace Module\Api;
|
|
|
|
use \FuzeWorks\Module;
|
2015-09-12 17:52:04 +00:00
|
|
|
use \FuzeWorks\Layout;
|
2015-07-07 12:09:38 +00:00
|
|
|
|
2015-09-12 17:52:04 +00:00
|
|
|
/**
|
|
|
|
* RestAPI class for creating API's out of modules or contrllers
|
|
|
|
*
|
|
|
|
* Extend a Controller with this class, and be sure to return the data from methods of your controller.
|
|
|
|
* This data will be parsed by this class and returned as valid JSON data.
|
|
|
|
* The necessity of API keys can be configured in the controller by settings $this->requireApiKey = false;
|
|
|
|
* @package net.techfuze.fuzeworks.core
|
|
|
|
* @author Abel Hoogeveen <abel@techfuze.net>
|
|
|
|
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
|
|
|
|
*/
|
|
|
|
abstract class RestAPI
|
|
|
|
{
|
2015-07-07 12:09:38 +00:00
|
|
|
/**
|
|
|
|
* Property: method
|
|
|
|
* The HTTP method this request was made in, either GET, POST, PUT or DELETE
|
|
|
|
*/
|
2015-09-12 17:52:04 +00:00
|
|
|
protected $method = '';
|
2015-07-07 12:09:38 +00:00
|
|
|
/**
|
|
|
|
* Property: endpoint
|
|
|
|
* The Model requested in the URI. eg: /files
|
|
|
|
*/
|
2015-09-12 17:52:04 +00:00
|
|
|
protected $endpoint = '';
|
2015-07-07 12:09:38 +00:00
|
|
|
/**
|
|
|
|
* Property: verb
|
|
|
|
* An optional additional descriptor about the endpoint, used for things that can
|
|
|
|
* not be handled by the basic methods. eg: /files/process
|
|
|
|
*/
|
2015-09-12 17:52:04 +00:00
|
|
|
protected $verb = '';
|
2015-07-07 12:09:38 +00:00
|
|
|
/**
|
|
|
|
* Property: args
|
|
|
|
* Any additional URI components after the endpoint and verb have been removed, in our
|
|
|
|
* case, an integer ID for the resource. eg: /<endpoint>/<verb>/<arg0>/<arg1>
|
|
|
|
* or /<endpoint>/<arg0>
|
|
|
|
*/
|
2015-09-12 17:52:04 +00:00
|
|
|
protected $args = Array();
|
2015-07-07 12:09:38 +00:00
|
|
|
/**
|
|
|
|
* Property: file
|
|
|
|
* Stores the input of the PUT request
|
|
|
|
*/
|
2015-09-12 17:52:04 +00:00
|
|
|
protected $file = Null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether API authentication is needed before interacting with the API
|
|
|
|
*/
|
|
|
|
protected $requireApiKey = true;
|
2015-07-07 12:09:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-12 17:52:04 +00:00
|
|
|
* Constructor: __construct
|
2015-07-07 12:09:38 +00:00
|
|
|
* Allow for CORS, assemble and pre-process the data
|
|
|
|
*/
|
2015-09-12 17:52:04 +00:00
|
|
|
public function __construct($request) {
|
2015-07-07 12:09:38 +00:00
|
|
|
header("Access-Control-Allow-Orgin: *");
|
|
|
|
header("Access-Control-Allow-Methods: *");
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
|
2015-09-12 17:52:04 +00:00
|
|
|
// Return layout data as string
|
|
|
|
Layout::setEngine('JSON');
|
|
|
|
Layout::returnAsString(false);
|
|
|
|
|
2015-07-07 12:09:38 +00:00
|
|
|
$this->args = explode('/', rtrim($request, '/'));
|
|
|
|
$this->endpoint = array_shift($this->args);
|
|
|
|
if (array_key_exists(0, $this->args) && !is_numeric($this->args[0])) {
|
|
|
|
$this->verb = array_shift($this->args);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
if ($this->method == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
|
|
|
|
if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'DELETE') {
|
|
|
|
$this->method = 'DELETE';
|
|
|
|
} else if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') {
|
|
|
|
$this->method = 'PUT';
|
|
|
|
} else {
|
|
|
|
throw new Exception("Unexpected Header");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch($this->method) {
|
|
|
|
case 'DELETE':
|
|
|
|
case 'POST':
|
|
|
|
$this->request = $this->_cleanInputs($_POST);
|
|
|
|
break;
|
|
|
|
case 'GET':
|
|
|
|
$this->request = $this->_cleanInputs($_GET);
|
|
|
|
break;
|
|
|
|
case 'PUT':
|
|
|
|
$this->request = $this->_cleanInputs($_GET);
|
|
|
|
$this->file = file_get_contents("php://input");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->_response('Invalid Method', 405);
|
|
|
|
break;
|
|
|
|
}
|
2015-09-12 17:52:04 +00:00
|
|
|
|
|
|
|
// And afterwards process the data
|
|
|
|
echo $this->processAPI();
|
|
|
|
|
|
|
|
// Halter for when the RestApi is used by a controller
|
|
|
|
$this->halt = true;
|
|
|
|
Layout::reset();
|
2015-07-07 12:09:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 17:52:04 +00:00
|
|
|
/**
|
|
|
|
* Process an API request when retrieving
|
|
|
|
* @return String JSON encoded response
|
|
|
|
*/
|
2015-07-07 12:09:38 +00:00
|
|
|
public function processAPI() {
|
2015-09-12 17:52:04 +00:00
|
|
|
if (method_exists($this, $this->endpoint)) {
|
2015-07-07 12:09:38 +00:00
|
|
|
return $this->_response($this->{$this->endpoint}($this->args));
|
|
|
|
}
|
|
|
|
return $this->_response("No Endpoint: $this->endpoint", 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _response($data, $status = 200) {
|
|
|
|
header("HTTP/1.1 " . $status . " " . $this->_requestStatus($status));
|
|
|
|
return json_encode($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _cleanInputs($data) {
|
|
|
|
$clean_input = Array();
|
|
|
|
if (is_array($data)) {
|
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
$clean_input[$k] = $this->_cleanInputs($v);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$clean_input = trim(strip_tags($data));
|
|
|
|
}
|
|
|
|
return $clean_input;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _requestStatus($code) {
|
|
|
|
$status = array(
|
|
|
|
200 => 'OK',
|
|
|
|
404 => 'Not Found',
|
|
|
|
405 => 'Method Not Allowed',
|
|
|
|
500 => 'Internal Server Error',
|
|
|
|
);
|
|
|
|
return ($status[$code])?$status[$code]:$status[500];
|
|
|
|
}
|
|
|
|
}
|