| <?php |
| namespace DAFME\Covid; |
| |
| class API { |
| private static function returnJSON($array) { |
| echo json_encode($array); |
| exit(); |
| } |
| |
| public static function returnError($errorMessage = 'Unexpected error') { |
| http_response_code(400); |
| self::returnJson([ |
| 'status' => 'error', |
| 'errorMessage' => $errorMessage |
| ]); |
| } |
| |
| public static function returnPayload($payload) { |
| self::returnJson([ |
| 'status' => 'ok', |
| 'payload' => $payload |
| ]); |
| } |
| |
| public static function returnOk() { |
| self::returnJson([ |
| 'status' => 'ok' |
| ]); |
| } |
| |
| private static function checkSignInStatus() { |
| if (!Users::isSignedIn()) { |
| self::returnError('The user hasn\'t signed in.'); |
| } |
| } |
| |
| private static function checkRequestMethod(string $method) { |
| if ($_SERVER['REQUEST_METHOD'] !== $method) |
| self::returnError('This action requires using the '.$method.' method.'); |
| } |
| |
| private static function getJSONBody() { |
| self::checkRequestMethod('POST'); |
| |
| $rawBody = file_get_contents('php://input'); |
| $json = json_decode($rawBody, true); |
| if (json_last_error() !== JSON_ERROR_NONE) |
| self::returnError('The request body is malformed.'); |
| |
| return $json; |
| } |
| |
| public static function process($path) { |
| global $conf; |
| |
| header('Content-Type: application/json'); |
| |
| if (isset($conf['allowedOrigin']) && !empty($conf['allowedOrigin'])) |
| header('Access-Control-Allow-Origin: '.$conf['allowedOrigin']); |
| |
| $parts = explode('/', $path); |
| $method = $parts[0] ?? ''; |
| |
| switch ($method) { |
| case 'getAuthUrl': |
| self::checkRequestMethod('GET'); |
| $auth = new Auth(); |
| self::returnPayload([ |
| 'url' => $auth->getAuthUrl() |
| ]); |
| break; |
| |
| case 'isSignedIn': |
| self::checkRequestMethod('GET'); |
| $isSignedIn = \DAFME\Covid\Users::isSignedIn(); |
| self::returnPayload([ |
| 'signedIn' => $isSignedIn |
| ]); |
| break; |
| |
| case 'signOut': |
| self::checkRequestMethod('POST'); |
| \DAFME\Covid\Users::signOut(); |
| self::returnOk(); |
| break; |
| |
| case 'getAllSubjects': |
| self::checkRequestMethod('GET'); |
| $subjects = Subjects::getAll(); |
| |
| if ($subjects === false) |
| self::returnError(); |
| |
| self::returnPayload([ |
| 'subjects' => $subjects |
| ]); |
| break; |
| |
| case 'getUserSubjects': |
| self::checkRequestMethod('GET'); |
| self::checkSignInStatus(); |
| $subjects = Subjects::getUserSubjects(); |
| |
| if ($subjects === false) |
| self::returnError(); |
| |
| self::returnPayload([ |
| 'subjects' => $subjects |
| ]); |
| break; |
| |
| case 'addUserSubject': |
| self::checkSignInStatus(); |
| $body = self::getJSONBody(); |
| if (!isset($body['subject'])) |
| self::returnError(); |
| |
| if (Subjects::addUserSubject((int)$body['subject'])) |
| self::returnOk(); |
| else |
| self::returnError(); |
| break; |
| |
| case 'removeUserSubject': |
| self::checkSignInStatus(); |
| $body = self::getJSONBody(); |
| if (!isset($body['subject'])) |
| self::returnError(); |
| |
| if (Subjects::removeUserSubject((int)$body['subject'])) |
| self::returnOk(); |
| else |
| self::returnError(); |
| break; |
| |
| case 'getClasses': |
| self::checkSignInStatus(); |
| // @TODO: Implement this method |
| break; |
| |
| case 'setClassState': |
| self::checkSignInStatus(); |
| // @TODO: Handle this method |
| break; |
| |
| default: |
| self::returnError('The method requested doesn\'t exist.'); |
| break; |
| } |
| } |
| } |