API_Controller.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 mixed[] $query GET parameters of the request.
  24. */
  25. public function __construct($query){
  26. // Remove host
  27. array_shift($query);
  28. // Remove /API/
  29. array_shift($query);
  30. // Remove /v1/
  31. array_shift($query);
  32. // API call: Use API controller
  33. $method = $_SERVER["REQUEST_METHOD"];
  34. if (!in_array($method, ["GET", "POST", "PUT", "DELETE"])){
  35. $method = "GET";
  36. }
  37. if (count($query) == 0 || $query[0] == null || $query[0] == ""){
  38. require_once(__DIR__ . "/main.php");
  39. }
  40. else{
  41. $command = strtolower($query[0]);
  42. // Remove /<command>/
  43. array_shift($query);
  44. switch ($command){
  45. case "help":
  46. require_once(__DIR__ . "/help/index.php");
  47. break;
  48. case "profile":
  49. case "run":
  50. case "logbook":
  51. case "collection":
  52. case "run":
  53. case "units":
  54. if (file_exists(__DIR__ . "/$command/$method.php")){
  55. require_once(__DIR__ . "/$command/$method.php");
  56. }
  57. else{
  58. header("HTTP/1.1 404");
  59. }
  60. break;
  61. default:
  62. header("HTTP/1.1 404");
  63. }
  64. }
  65. }
  66. }
  67. ?>