--- /dev/null
+<?php
+
+/**
+ * RESTAPI is a abstract permite to easy create a subclass with API functinnaliolaty (get, put, post, delete)
+ *
+ * RestAPI description.
+ *
+ * @version 1.0
+ * @author Ferrand
+ */
+abstract class RestAPI {
+ /* Method for subclass */
+ abstract protected function GETAction();
+ abstract protected function GETCollectionAction();
+ abstract protected function PUTAction();
+ abstract protected function PUTCollectionAction();
+ abstract protected function POSTAction();
+ abstract protected function POSTCollectionAction();
+ abstract protected function DELETEAction();
+ abstract protected function DELETECollectionAction();
+ protected $element;
+ /* ------------------- */
+
+ protected $code = 200; // may be changed by precedents functions
+ protected $data; // data client and may be changed by precedents functions
+
+ final protected function responde($code, $data = null) {
+ $this->code = $code;
+ if($data === null) {
+ $this->data = "";
+ }
+ else {
+ $this->data = json_encode($data);
+ }
+ }
+
+ final protected function respondeError() {
+ $this->responde(500);
+ }
+
+ final protected function respondeNoContent() {
+ $this->responde(204, new ArrayObject());
+ }
+
+ final protected function respondeUnauthorized() {
+ $this->responde(401);
+ }
+
+ final protected function respondeForbidden() {
+ $this->responde(403);
+ }
+
+ final protected function respondeNoResult() {
+ $this->responde(404);
+ }
+
+ final protected function respondeMethodNotAllowed() {
+ $this->responde(405);
+ }
+
+ final protected function respondeNotAcceptable() {
+ $this->responde(406);
+ }
+
+ final protected function respondeConflict() {
+ $this->responde(409);
+ }
+
+ final public function serve() {
+ $requestMethod = $_SERVER['REQUEST_METHOD'] . ($this->element === null ? "Collection" : "") . "Action";
+ $this->$requestMethod();
+ header("HTTP/1.1 " . $this->code . " " . self::$STATUS[$this->code]);
+ header("Content-Type:application/json; charset=utf-8");
+ print($this->data);
+ }
+
+ final protected function databaseHelper() {
+ return (new PDO("mysql:host=".$GLOBALS['PF_HOST'].";dbname=".$GLOBALS['PF_DATABASE_NAME'].";charset=utf8", $GLOBALS['PF_USERNAME'], $GLOBALS['PF_PASSWORD']));
+ }
+
+ final public function compare($serverResourceCurrent, $serverResourceToInsert) {
+ $fieldChanged = array();
+ $newValue = array();
+ foreach($serverResourceToInsert as $key => $value) {
+ if($value !== $serverResourceCurrent->$key) {
+ array_push($fieldChanged, $key);
+ array_push($newValue, $value);
+ }
+ }
+
+ $setLine = "";
+ for($i = 0; $i < count($fieldChanged); $i++) {
+ $setLine .= '`' . ($fieldChanged[$i] . '` = "' . $newValue[$i] . '", ');
+ }
+ $setLine = substr_replace($setLine, '', -2);
+
+ return [
+ 'fieldChanged' => $fieldChanged,
+ 'setLine' => $setLine
+ ];
+ }
+
+ public function __construct($element = null) {
+ $this->element = $element;
+ }
+
+ final protected function authentication() {
+ $username = "";
+ $password = "";
+
+ if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
+ $username = $_SERVER['PHP_AUTH_USER'];
+ $password = $_SERVER['PHP_AUTH_PW'];
+ }
+ else if (isset(getallheaders()['User']) && isset(getallheaders()['Password'])) {
+ $username = getallheaders()['User'];
+ $password = getallheaders()['Password'];
+ }
+ else {
+ return null;
+ }
+
+ $encryptedpassword = crypt($password, $GLOBALS['PF_KEY']);
+ $dbh = $this->databaseHelper();
+
+ $response = $dbh->prepare('SELECT * FROM pub_Account WHERE `username` = :username AND `password` = :password');
+ $response->execute([':username' => $username, ':password' => $encryptedpassword]);
+ if ($response === false) {
+ return null;
+ }
+
+ $row = $response->fetch();
+ if ($row === false) {
+ return null;
+ }
+
+ return [
+ 'username' => $row['username'],
+ 'administrator' => $row['administrator'],
+ 'pseudo' => $row['user']
+ ];
+ }
+
+ final protected function isAuthenticated() {
+ return ($this->authentication() !== null);
+ }
+
+ final protected function getBodyData() {
+ $fp = fopen('php://input', 'r');
+ $rawData = stream_get_contents($fp);
+
+ return json_decode($rawData);
+ }
+
+ final protected function authenticationAdministrator() {
+ $user = $this->authentication();
+ if ($user === null) {
+ return null;
+ } else {
+ if ($user['administrator'] == 0) {
+ return null;
+ } else {
+ return $user;
+ }
+ }
+ }
+
+ protected static $STATUS = array(
+ 100 => 'Continue',
+ 101 => 'Switching Protocols',
+ 200 => 'OK',
+ 201 => 'Created',
+ 202 => 'Accepted',
+ 203 => 'Non-Authoritative Information',
+ 204 => 'No Content',
+ 205 => 'Reset Content',
+ 206 => 'Partial Content',
+ 300 => 'Multiple Choices',
+ 301 => 'Moved Permanently',
+ 302 => 'Found',
+ 303 => 'See Other',
+ 304 => 'Not Modified',
+ 305 => 'Use Proxy',
+ 306 => '(Unused)',
+ 307 => 'Temporary Redirect',
+ 400 => 'Bad Request',
+ 401 => 'Unauthorized',
+ 402 => 'Payment Required',
+ 403 => 'Forbidden',
+ 404 => 'Not Found',
+ 405 => 'Method Not Allowed',
+ 406 => 'Not Acceptable',
+ 407 => 'Proxy Authentication Required',
+ 408 => 'Request Timeout',
+ 409 => 'Conflict',
+ 410 => 'Gone',
+ 411 => 'Length Required',
+ 412 => 'Precondition Failed',
+ 413 => 'Request Entity Too Large',
+ 414 => 'Request-URI Too Long',
+ 415 => 'Unsupported Media Type',
+ 416 => 'Requested Range Not Satisfiable',
+ 417 => 'Expectation Failed',
+ 500 => 'Internal Server Error',
+ 501 => 'Not Implemented',
+ 502 => 'Bad Gateway',
+ 503 => 'Service Unavailable',
+ 504 => 'Gateway Timeout',
+ 505 => 'HTTP Version Not Supported');
+}
+
+trait dontAllowGet {
+ protected function GETAction() {
+ $this->respondeMethodNotAllowed();
+ }
+}
+
+trait dontAllowGetCollection {
+ protected function GETCollectionAction() {
+ $this->respondeMethodNotAllowed();
+ }
+}
+
+trait dontAllowPut {
+ protected function PUTAction() {
+ $this->respondeMethodNotAllowed();
+ }
+}
+
+trait dontAllowPutCollection {
+ protected function PUTCollectionAction() {
+ $this->respondeMethodNotAllowed();
+ }
+}
+
+trait dontAllowPost {
+ protected function POSTAction() {
+ $this->respondeMethodNotAllowed();
+ }
+}
+
+trait dontAllowPostCollection {
+ protected function POSTCollectionAction() {
+ $this->respondeMethodNotAllowed();
+ }
+}
+
+trait dontAllowDelete {
+ protected function DELETEAction() {
+ $this->respondeMethodNotAllowed();
+ }
+}
+
+trait dontAllowDeleteCollection {
+ protected function DELETECollectionAction() {
+ $this->respondeMethodNotAllowed();
+ }
+}
+
+class DoNothingRestAPI extends RestAPI {
+ private function respondeBadRequest() {
+ $this->responde(400);
+ }
+
+ protected function GETAction() {
+ $this->respondeBadRequest();
+ }
+
+ protected function GETCollectionAction() {
+ $this->respondeBadRequest();
+ }
+
+ protected function PUTAction() {
+ $this->respondeBadRequest();
+ }
+
+ protected function PUTCollectionAction() {
+ $this->respondeBadRequest();
+ }
+
+ protected function POSTAction() {
+ $this->respondeBadRequest();
+ }
+
+ protected function POSTCollectionAction() {
+ $this->respondeBadRequest();
+ }
+
+ protected function DELETEAction() {
+ $this->respondeBadRequest();
+ }
+
+ protected function DELETECollectionAction() {
+ $this->respondeBadRequest();
+ }
+
+}