* @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 // 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"); } } } }