| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- /**
- * v1 API Controller file.
- *
- * Provides a class to handle all posible API requests.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
- * @package SWDB
- */
- /**
- * v1 API controller.
- *
- * Handles every API request.
- *
- * @category API
- */
- class API_Controller {
- /**
- * Constructor.
- *
- * Handles every request, creating the required models and selecting the
- * view.
- *
- * @param string[] $query GET parameters of the request.
- */
- public function __construct($query){
- // Remove /API/
- array_shift($query);
- // Remove /v1/
- array_shift($query);
- // API call: Use API controller
- $method = $_SERVER["REQUEST_METHOD"];
- if (!in_array($method, ["GET", "POST", "PUT", "DELETE"])){
- $method = "GET";
- }
- if (count($query) == 0 || $query[0] == null || $query[0] == ""){
- require_once(__DIR__ . "/main.php");
- }
- else{
- $command = strtolower($query[0]);
- // Remove /<command>/
- array_shift($query);
- switch ($command){
- case "help":
- require_once(__DIR__ . "/help/index.php");
- break;
- case "profile":
- case "run":
- case "logbook":
- case "collection":
- case "run":
- case "units":
- if (file_exists(__DIR__ . "/$command/$method.php")){
- require_once(__DIR__ . "/$command/$method.php");
- }
- else{
- header("HTTP/1.1 404");
- }
- break;
- default:
- header("HTTP/1.1 404");
- }
- }
- }
- }
|