rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

commit 05856f266edfe20ae2a3357185abc8de40da1728
parent 577f5876420efe14faee2d05b0e1e10320d1fe1b
Author: MTRNord <mtrnord1@gmail.com>
Date:   Mon,  2 Feb 2015 19:59:28 +0100

preparing for RestApi

Diffstat:
M.htaccess | 10+++++++---
Acore/api/v1/api.class.php | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acore/api/v1/api.php | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Adocs/nginx.htaccess | 12++++++++++++
4 files changed, 180 insertions(+), 3 deletions(-)

diff --git a/.htaccess b/.htaccess @@ -1,5 +1,9 @@ +<IfModule mod_rewrite.c> RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f -RewriteRule /^([^\.]+)$ %{REQUEST_URI}.php [NC,L] -Redirect 301 /oauth2callback /core/backend/admin/modules/modul_g-plus-login/handle.php -\ No newline at end of file +RewriteRule /^([^\.]+)$ %{REQUEST_URI}.php [NC] +Redirect 301 /oauth2callback /core/backend/admin/modules/modul_g-plus-login/handle.php +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule api/v1/(.*)$ api/v1/api.php?request=$1 [QSA,NC,L] +</IfModule> +\ No newline at end of file diff --git a/core/api/v1/api.class.php b/core/api/v1/api.class.php @@ -0,0 +1,110 @@ +<?php +abstract class API +{ + /** + * Property: method + * The HTTP method this request was made in, either GET, POST, PUT or DELETE + */ + protected $method = ''; + /** + * Property: endpoint + * The Model requested in the URI. eg: /files + */ + protected $endpoint = ''; + /** + * Property: verb + * An optional additional descriptor about the endpoint, used for things that can + * not be handled by the basic methods. eg: /files/process + */ + protected $verb = ''; + /** + * 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> + */ + protected $args = Array(); + /** + * Property: file + * Stores the input of the PUT request + */ + protected $file = Null; + + /** + * Constructor: __construct + * Allow for CORS, assemble and pre-process the data + */ + public function __construct($request) { + header("Access-Control-Allow-Orgin: *"); + header("Access-Control-Allow-Methods: *"); + header("Content-Type: application/json"); + + $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; + } + } + public function processAPI() { + if ((int)method_exists($this, $this->endpoint) > 0) { + 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]; + } +} +?> +\ No newline at end of file diff --git a/core/api/v1/api.php b/core/api/v1/api.php @@ -0,0 +1,49 @@ +<?php +require_once 'API.class.php'; +class MyAPI extends API +{ + protected $User; + + public function __construct($request, $origin) { + parent::__construct($request); + + // Abstracted out for example + $APIKey = new Models\APIKey(); + $User = new Models\User(); + + if (!array_key_exists('apiKey', $this->request)) { + throw new Exception('No API Key provided'); + } else if (!$APIKey->verifyKey($this->request['apiKey'], $origin)) { + throw new Exception('Invalid API Key'); + } else if (array_key_exists('token', $this->request) && + !$User->get('token', $this->request['token'])) { + + throw new Exception('Invalid User Token'); + } + + $this->User = $User; + } + + /** + * Example of an Endpoint + */ + protected function example() { + if ($this->method == 'GET') { + return "Your name is " . $this->User->name; + } else { + return "Only accepts GET requests"; + } + } + // Requests from the same server don't have a HTTP_ORIGIN header + if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) { + $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME']; + } + + try { + $API = new MyAPI($_REQUEST['request'], $_SERVER['HTTP_ORIGIN']); + echo $API->processAPI(); + } catch (Exception $e) { + echo json_encode(Array('error' => $e->getMessage())); + } + } + ?> +\ No newline at end of file diff --git a/docs/nginx.htaccess b/docs/nginx.htaccess @@ -0,0 +1,11 @@ +location /oauth2callback { +rewrite ^(.*)$ [PFAD_TO_PROJECT]/core/backend/admin/modules/modul_g-plus-login/handle.php redirect; +} +location / { +if (!-e $request_filename){ +rewrite /^([^\.]+)$ /$request_uri.php; +} +if (!-e $request_filename){ +rewrite api/v1/(.*)$ [PFAD_TO_PROJECT]/core/api/v1/api.php?request=$1 break; +} +} +\ No newline at end of file