diff --git a/build/api/core/Documentation.php b/build/api/core/Documentation.php new file mode 100644 index 0000000..693fcb1 --- /dev/null +++ b/build/api/core/Documentation.php @@ -0,0 +1,50 @@ + exit */ + if( !isset(Config::get()->index[$rq->get('id')['path']]) ) + return new Response(new Error(Err::WrongPathModule)); + + /* (2) Local store: configuration for this path */ + $cfg = Config::get()->index[$rq->get('id')['path']]; + + + + $response = new Response(); + $response->append('methods', $cfg); + + return $response; + } + + + + + + + + + + } \ No newline at end of file diff --git a/build/api/core/Request.php b/build/api/core/Request.php index d3c493d..0912e1e 100755 --- a/build/api/core/Request.php +++ b/build/api/core/Request.php @@ -23,9 +23,6 @@ private $options; // options private $http_method; // methode HTTP appelante - // Contiendra la reponse a la requete - public $answer; - // Contiendra l'etat de la requete public $error; @@ -68,7 +65,6 @@ return $this->error->set(Err::MissingPath); - /* (2) On vérifie la configuration ---------------------------------------------------------*/ /* (1) Dispatch if error */ @@ -95,35 +91,41 @@ $this->http_method = is_string($forced_method) ? strtoupper($forced_method) : strtoupper($_SERVER['REQUEST_METHOD']); - /* (4) Verification du chemin (existence module+methode) ---------------------------------------------------------*/ if( !$this->checkURI($uri) ) // Verification de la coherence du chemin + attribution return false; // checkURI() sets the error itself - /* (5) Verification des permissions + /* (5) Si requête de documentation -> on arrête la vérification + ---------------------------------------------------------*/ + if( $this->id['doc_request'] ) + return true; + + + /* (6) Verification des permissions ---------------------------------------------------------*/ if( !$this->checkPermission() ) // Si on a pas les droits return false; // checkPermission() sets the error itself - /* (6) Verification des parametres (si @type est defini) + /* (7) Verification des parametres (si @type est defini) ---------------------------------------------------------*/ if( !$this->checkParams() ) // Verification de tous les types return false; // checkParams() sets the error itself - /* (7) Récupèration des options + /* (8) Récupèration des options ---------------------------------------------------------*/ $this->buildOptions(); - /* (8) Construction de l'objet + /* (9) Construction de l'objet ---------------------------------------------------------*/ $this->error->set(Err::Success); return true; // On retourne que tout s'est bien passe + } @@ -160,7 +162,7 @@ /* (1) Verification format general ---------------------------------------------------------*/ /* (1) If wrong format -> exit */ - if( !preg_match('@^\/[^\/]*(\/[^\/]+)*\/?$@', $uri) ) + if( !preg_match('@^\/[^\/]*(\/[^\/]*)*\/?$@', $uri) ) return $this->error->set(Err::WrongPathModule); /* (2) Add ending '/' if not there */ @@ -195,28 +197,34 @@ ---------------------------------------------------------*/ /* (1) Extract URI string after @path */ $uri_end = substr($uri, $exists_size); + /* (2) Special case: add / if root uri arguments */ if( strlen($uri_end) > 0 && $uri_end[0] != '/' ) $uri_end = "/$uri_end"; /* (3) If invalid format, return error */ - if( !preg_match('@^((?:\/[^\/]+)*)\/?$@', $uri_end, $uri_match) ) + if( !preg_match('@^((?:\/[^\/]*)*)\/?$@', $uri_end, $uri_match) ) return $this->error->set(Err::InvalidURI); /* (4) Add each URI parameter to the parameter store */ $uri_args = array_slice( explode('/', $uri_match[1]), 1); foreach($uri_args as $index=>$value) - $this->raw_params["URL$index"] = $value; + if( strlen($value) > 0 ) // do not store '//' empty values + $this->raw_params["URL$index"] = $value; + /* (4) Verification de l'existence de la methode (conf) ---------------------------------------------------------*/ - /* (1) Check if HTTP method is in allowed methods */ - if( !in_array($this->http_method, Config::$allowed_http_methods) ) + /* (1) If it is a documentation request */ + $doc_req = $this->http_method == 'OPTIONS'; + + /* (2) Check if HTTP method is in allowed methods */ + if( !in_array($this->http_method, Config::$allowed_http_methods) && !$doc_req ) return $this->error->set(Err::UnknownHttpMethod, $this->http_method); - /* (2) Check if HTTP method is defined for this @path */ - if( !isset(Config::get()->index[$path][$this->http_method]) ) + /* (3) Check if HTTP method is defined for this @path */ + if( !isset(Config::get()->index[$path][$this->http_method]) && !$doc_req ) return $this->error->set(Err::UnknownMethod, $this->http_method); @@ -224,8 +232,9 @@ /* (5) Enregistrement du chemin et renvoi de SUCCESS ---------------------------------------------------------*/ $this->id = [ - 'path'=> $path, - 'method'=> $this->http_method + 'path' => $path, + 'method' => $this->http_method, + 'doc_request' => $doc_req ]; return true; @@ -439,10 +448,15 @@ ---------------------------------------------------------*/ public function dispatch(){ - /* (1) On verifie qu'aucune erreur n'a ete signalee + /* (1) Vérifications de niveau 0 ---------------------------------------------------------*/ - if( $this->error->get() !== Err::Success ) // si il y a une erreur - return new Response($this->error); // on la passe a la reponse + /* (1) Si erreur -> on dispatch à la réponse */ + if( $this->error->get() !== Err::Success ) + return new Response($this->error); + + /* (2) S'il requête de documentation -> on génère la documentation */ + if( $this->id['doc_request'] ) + return Documentation::generate($this); /* (2) On essaie d'instancier le module @@ -578,6 +592,27 @@ } + + /* (9) Getter générique + * + * @index Index de l'attribut + * + ---------------------------------------------------------*/ + public function get($index=null){ + + switch($index){ + + case 'id': return $this->id; break; + case 'raw_params': return $this->raw_params; break; + case 'params': return $this->params; break; + case 'options': return $this->options; break; + case 'http_method': return $this->http_method; break; + } + + return null; + } + + } ?> diff --git a/config/routes.json b/config/routes.json index 746489f..5063950 100755 --- a/config/routes.json +++ b/config/routes.json @@ -1,12 +1,12 @@ { - "methods": [ "GET", "POST", "PUT", "DELETE", "VIEW" ], + "methods": [ "GET", "POST", "PUT", "DELETE", "OPTIONS" ], "routes": { "/api/v/1.0/{uri}": { - "methods": ["GET", "POST", "PUT", "DELETE", "VIEW"], + "methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"], "controller": "api:call", "arguments": { "uri": ".*" diff --git a/public_html/index.php b/public_html/index.php index 8238b65..f84887e 100755 --- a/public_html/index.php +++ b/public_html/index.php @@ -16,5 +16,5 @@ Request::setAuthSystem(new AuthSystemDefault); /* (4) launch router */ - Router::launch($_GET['url']); + Router::launch($_SERVER['REQUEST_URI']);