rpicms

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

commit 3342c87d349edcd1bdd00f186bd9ef11d6f93a2b
parent 9a1dc6cef413fa4dab516edf016ae74ebe08865c
Author: MTRNord <mtrnord1@gmail.com>
Date:   Mon,  2 Feb 2015 20:11:37 +0100

next try

Diffstat:
Acore/api/v1/MyApi.class.php | 39+++++++++++++++++++++++++++++++++++++++
Mcore/api/v1/api.php | 64++++++++++++++--------------------------------------------------
2 files changed, 53 insertions(+), 50 deletions(-)

diff --git a/core/api/v1/MyApi.class.php b/core/api/v1/MyApi.class.php @@ -0,0 +1,38 @@ +<?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"; + } + } + } + ?> +\ No newline at end of file diff --git a/core/api/v1/api.php b/core/api/v1/api.php @@ -1,50 +1,14 @@ <?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 +require_once 'MyApi.class.php'; +// 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