Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * api/site-templates
- * api/site-pages
- * api/site-pages/?path=foo/bar
- * api/site-plugins
- * api/sessions GET = checkAll, POST = login, DELETE = logoutAll
- * api/sessions/current GET = check, DELETE = logout
- * api/articles/1
- * api/users/name
- * api/users/id
- */
- if (version_compare(PHP_VERSION, '5.4', '<')) { die('Error: Upgrade to PHP 5.4 or higher!'); }
- define('EXEC', 1);
- include 'sys/error-handling.php';
- include 'sys/config.php';
- include 'sys/db.php';
- $config = new config();
- $db = new db($config);
- //parsing the url seems silly here...
- $request = preg_split('/(api|\?)/', $_SERVER['REQUEST_URI']);
- $pieces = preg_split('/\/|\./', $request[1], NULL, PREG_SPLIT_NO_EMPTY);
- $collection = (isset($pieces[0])) ? $pieces[0] : NULL;
- $item = (isset($pieces[1])) ? $pieces[1] : NULL;
- $format = (isset($pieces[2])) ? $pieces[2] : 'json';
- //now I'm going to end up with a class that handles whatever collection is requested, but that class "knows" about EVERYTHINg in the api
- //since it derives from all of it. Seems sloppy, sloppy, sloppy.
- $handler = (!empty($collection)) ? "resource_{$collection}" : 'resource';
- $handler = new $handler($db, $config, $collection, $item, $format);
- $handler->db->connect();
- $handler->respond();
- $handler->db->close();
- class api {
- public $db;
- protected $config;
- public function __construct($db, $config, $collection, $item, $format) {
- $this->db = $db;
- $this->config = $config;
- $this->collection = $collection;
- $this->item = $item;
- $this->format = $format;
- //find input from raw data, post, get
- $this->input = new stdClass;
- foreach ([json_decode(file_get_contents('php://input')), $_POST, $_GET] as $k => $v) {
- if (count($v)) {
- $this->input = (is_object($v)) ? $v : json_decode(json_encode($v));
- break;
- }
- }
- //allow get/post param to override request method
- foreach ([$_GET, $_POST, $_SERVER] as $v) {
- if (!empty($v['REQUEST_METHOD'])) {
- $this->requestMethod = $v['REQUEST_METHOD'];
- break;
- }
- }
- //$this->requesterAccess = 0;
- $this->output = new stdClass();
- $this->output->data = [];
- $this->output->messages = [];
- $this->output->errors = [];
- }
- public function error($errorMsg) {
- array_push($this->output->errors, $errorMsg);
- }
- public function message($msg) {
- array_push($this->output->messages, $msg);
- }
- public function respond() {
- $func = (empty($this->item)) ? $this->requestMethod : "{$this->requestMethod}_item";
- $this->$func();
- echo json_encode($this->output);
- }
- }
- class resource extends api {
- public function GET($table) {
- $query = "SELECT * FROM {$table}";
- $this->output->data = $this->db->exec($query);
- http_response_code(200);
- }
- public function POST() {
- //add an item to this collection
- //be sure to return href where item will be found! (id is generated by server)
- http_response_code(201);
- }
- public function PUT() {
- $this->error('Collections do not support method "PUT".');
- http_response_code(405);
- }
- public function DELETE() {
- $this->error('Collections do not support method "DELETE".');
- http_response_code(405);
- }
- public function GET_item() {
- http_response_code(200);
- }
- public function POST_item() { //on new - resource id is created by database, on update, replace only supplied data
- http_response_code(201);
- }
- public function PUT_item() { //on new - resource id is supplied by client
- //api/users/username, api/users/1
- //create user available at those urls
- http_response_code(201);
- }
- public function DELETE_item() {
- $this->error('Collection cannot be removed.');
- http_response_code(405);
- }
- }
- class resource_articles extends resource {
- //this throws an warning since the base class method accepts a parameter and this doesn't.
- //this is probably one of those cases where my overall structure is leading to more bad practices.
- public function GET() {
- parent::GET('articles');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement