Advertisement
Guest User

Untitled

a guest
Sep 27th, 2013
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.06 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * api/site-templates
  5.  * api/site-pages
  6.  * api/site-pages/?path=foo/bar
  7.  * api/site-plugins
  8.  * api/sessions GET = checkAll, POST = login, DELETE = logoutAll
  9.  * api/sessions/current GET = check, DELETE = logout
  10.  * api/articles/1
  11.  * api/users/name
  12.  * api/users/id
  13. */
  14.  
  15. if (version_compare(PHP_VERSION, '5.4', '<')) { die('Error: Upgrade to PHP 5.4 or higher!'); }
  16.  
  17. define('EXEC', 1);
  18.  
  19. include 'sys/error-handling.php';
  20. include 'sys/config.php';
  21. include 'sys/db.php';
  22.  
  23. $config = new config();
  24. $db = new db($config);
  25.  
  26. //parsing the url seems silly here...
  27. $request = preg_split('/(api|\?)/', $_SERVER['REQUEST_URI']);
  28. $pieces = preg_split('/\/|\./', $request[1], NULL, PREG_SPLIT_NO_EMPTY);
  29. $collection = (isset($pieces[0])) ? $pieces[0] : NULL;
  30. $item = (isset($pieces[1])) ? $pieces[1] : NULL;
  31. $format = (isset($pieces[2])) ? $pieces[2] : 'json';
  32.  
  33. //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
  34. //since it derives from all of it. Seems sloppy, sloppy, sloppy.
  35. $handler = (!empty($collection)) ? "resource_{$collection}" : 'resource';
  36. $handler = new $handler($db, $config, $collection, $item, $format);
  37.  
  38. $handler->db->connect();
  39. $handler->respond();
  40. $handler->db->close();
  41.  
  42. class api {
  43.   public $db;
  44.   protected $config;
  45.  
  46.   public function __construct($db, $config, $collection, $item, $format) {
  47.     $this->db = $db;
  48.     $this->config = $config;
  49.  
  50.     $this->collection = $collection;
  51.     $this->item = $item;
  52.     $this->format = $format;
  53.  
  54.     //find input from raw data, post, get
  55.     $this->input = new stdClass;
  56.     foreach ([json_decode(file_get_contents('php://input')), $_POST, $_GET] as $k => $v) {
  57.       if (count($v)) {
  58.         $this->input = (is_object($v)) ? $v : json_decode(json_encode($v));
  59.         break;
  60.       }
  61.     }
  62.  
  63.     //allow get/post param to override request method
  64.     foreach ([$_GET, $_POST, $_SERVER] as $v) {
  65.       if (!empty($v['REQUEST_METHOD'])) {
  66.         $this->requestMethod = $v['REQUEST_METHOD'];
  67.         break;
  68.       }
  69.     }
  70.  
  71.     //$this->requesterAccess = 0;
  72.  
  73.     $this->output = new stdClass();
  74.     $this->output->data = [];
  75.     $this->output->messages = [];
  76.     $this->output->errors = [];
  77.   }
  78.   public function error($errorMsg) {
  79.     array_push($this->output->errors, $errorMsg);
  80.   }
  81.   public function message($msg) {
  82.     array_push($this->output->messages, $msg);
  83.   }
  84.   public function respond() {
  85.     $func = (empty($this->item)) ? $this->requestMethod : "{$this->requestMethod}_item";
  86.     $this->$func();
  87.     echo json_encode($this->output);
  88.   }
  89. }
  90.  
  91. class resource extends api {
  92.   public function GET($table) {
  93.     $query = "SELECT * FROM {$table}";
  94.     $this->output->data = $this->db->exec($query);
  95.     http_response_code(200);
  96.   }
  97.   public function POST() {
  98.     //add an item to this collection
  99.     //be sure to return href where item will be found! (id is generated by server)
  100.     http_response_code(201);
  101.   }
  102.   public function PUT() {
  103.     $this->error('Collections do not support method "PUT".');
  104.     http_response_code(405);
  105.   }
  106.   public function DELETE() {
  107.     $this->error('Collections do not support method "DELETE".');
  108.     http_response_code(405);
  109.   }
  110.  
  111.   public function GET_item() {
  112.  
  113.     http_response_code(200);
  114.   }
  115.   public function POST_item() { //on new - resource id is created by database, on update, replace only supplied data
  116.     http_response_code(201);
  117.   }
  118.   public function PUT_item() { //on new - resource id is supplied by client
  119.     //api/users/username, api/users/1
  120.     //create user available at those urls
  121.     http_response_code(201);
  122.   }
  123.   public function DELETE_item() {
  124.     $this->error('Collection cannot be removed.');
  125.     http_response_code(405);
  126.   }
  127. }
  128.  
  129. class resource_articles extends resource {
  130.   //this throws an warning since the base class method accepts a parameter and this doesn't.
  131.   //this is probably one of those cases where my overall structure is leading to more bad practices.
  132.   public function GET() {
  133.     parent::GET('articles');
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement