API_Controller.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * v1 API Controller file.
  4. *
  5. * Provides a class to handle all posible API requests.
  6. *
  7. * @category Constroller
  8. */
  9. /**
  10. * v1 API controller.
  11. *
  12. * Handles every API request.
  13. *
  14. * @category Controller
  15. */
  16. class API_Controller {
  17. /**
  18. * Constructor.
  19. *
  20. * Handles every request, creating the required models and selecting the
  21. * view.
  22. *
  23. * @param string[] $query GET parameters of the request.
  24. */
  25. public function __construct($query){
  26. // Remove /API/
  27. array_shift($query);
  28. // Remove /v1/
  29. array_shift($query);
  30. // API call: Use API controller
  31. $method = $_SERVER["REQUEST_METHOD"];
  32. if (!in_array($method, ["GET", "POST", "PUT", "DELETE"])){
  33. $method = "GET";
  34. }
  35. if (count($query) == 0 || $query[0] == null || $query[0] == ""){
  36. require_once(__DIR__ . "/main.php");
  37. }
  38. else{
  39. $command = strtolower($query[0]);
  40. // Remove /<command>/
  41. array_shift($query);
  42. switch ($command){
  43. case "help":
  44. require_once(__DIR__ . "/help/index.php");
  45. break;
  46. case "profile":
  47. case "run":
  48. case "logbook":
  49. case "collection":
  50. case "run":
  51. case "units":
  52. if (file_exists(__DIR__ . "/$command/$method.php")){
  53. require_once(__DIR__ . "/$command/$method.php");
  54. }
  55. else{
  56. header("HTTP/1.1 404");
  57. }
  58. break;
  59. default:
  60. header("HTTP/1.1 404");
  61. }
  62. }
  63. }
  64. }
  65. ?>