avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 1 | <?php |
| 2 | namespace DAFME\Covid; |
| 3 | |
| 4 | class API { |
| 5 | private static function returnJSON($array) { |
| 6 | echo json_encode($array); |
avm99963 | 339e6f7 | 2020-09-27 17:12:43 +0200 | [diff] [blame] | 7 | exit(); |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 8 | } |
| 9 | |
| 10 | public static function returnError($errorMessage = 'Unexpected error') { |
| 11 | http_response_code(400); |
| 12 | self::returnJson([ |
| 13 | 'status' => 'error', |
| 14 | 'errorMessage' => $errorMessage |
| 15 | ]); |
| 16 | } |
| 17 | |
| 18 | public static function returnPayload($payload) { |
| 19 | self::returnJson([ |
| 20 | 'status' => 'ok', |
| 21 | 'payload' => $payload |
| 22 | ]); |
| 23 | } |
| 24 | |
| 25 | public static function returnOk() { |
| 26 | self::returnJson([ |
| 27 | 'status' => 'ok' |
| 28 | ]); |
| 29 | } |
| 30 | |
| 31 | private static function checkSignInStatus() { |
| 32 | if (!Users::isSignedIn()) { |
| 33 | self::returnError('The user hasn\'t signed in.'); |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
avm99963 | 80afa68 | 2020-09-27 17:23:09 +0200 | [diff] [blame^] | 37 | private static function checkRequestMethod(string $method) { |
| 38 | if ($_SERVER['REQUEST_METHOD'] !== $method) |
| 39 | self::returnError('This action requires using the '.$method.' method.'); |
| 40 | } |
| 41 | |
avm99963 | 339e6f7 | 2020-09-27 17:12:43 +0200 | [diff] [blame] | 42 | private static function getJSONBody() { |
avm99963 | 80afa68 | 2020-09-27 17:23:09 +0200 | [diff] [blame^] | 43 | self::checkRequestMethod('POST'); |
avm99963 | 339e6f7 | 2020-09-27 17:12:43 +0200 | [diff] [blame] | 44 | |
| 45 | $rawBody = file_get_contents('php://input'); |
| 46 | $json = json_decode($rawBody, true); |
| 47 | if (json_last_error() !== JSON_ERROR_NONE) |
| 48 | self::returnError('The request body is malformed.'); |
| 49 | |
| 50 | return $json; |
| 51 | } |
| 52 | |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 53 | public static function process($path) { |
| 54 | global $conf; |
| 55 | |
| 56 | header('Content-Type: application/json'); |
| 57 | |
avm99963 | dbd9a12 | 2020-09-24 18:51:45 +0200 | [diff] [blame] | 58 | if (isset($conf['allowedOrigin']) && !empty($conf['allowedOrigin'])) |
| 59 | header('Access-Control-Allow-Origin: '.$conf['allowedOrigin']); |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 60 | |
| 61 | $parts = explode('/', $path); |
| 62 | $method = $parts[0] ?? ''; |
| 63 | |
| 64 | switch ($method) { |
| 65 | case 'getAuthUrl': |
avm99963 | 80afa68 | 2020-09-27 17:23:09 +0200 | [diff] [blame^] | 66 | self::checkRequestMethod('GET'); |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 67 | $auth = new Auth(); |
| 68 | self::returnPayload([ |
| 69 | 'url' => $auth->getAuthUrl() |
| 70 | ]); |
| 71 | break; |
| 72 | |
| 73 | case 'isSignedIn': |
avm99963 | 80afa68 | 2020-09-27 17:23:09 +0200 | [diff] [blame^] | 74 | self::checkRequestMethod('GET'); |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 75 | $isSignedIn = \DAFME\Covid\Users::isSignedIn(); |
| 76 | self::returnPayload([ |
| 77 | 'signedIn' => $isSignedIn |
| 78 | ]); |
| 79 | break; |
| 80 | |
| 81 | case 'signOut': |
avm99963 | 80afa68 | 2020-09-27 17:23:09 +0200 | [diff] [blame^] | 82 | self::checkRequestMethod('POST'); |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 83 | \DAFME\Covid\Users::signOut(); |
| 84 | self::returnOk(); |
| 85 | break; |
| 86 | |
| 87 | case 'getAllSubjects': |
avm99963 | 80afa68 | 2020-09-27 17:23:09 +0200 | [diff] [blame^] | 88 | self::checkRequestMethod('GET'); |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 89 | $subjects = Subjects::getAll(); |
| 90 | |
| 91 | if ($subjects === false) |
| 92 | self::returnError(); |
| 93 | |
| 94 | self::returnPayload([ |
| 95 | 'subjects' => $subjects |
| 96 | ]); |
| 97 | break; |
| 98 | |
| 99 | case 'getUserSubjects': |
avm99963 | 80afa68 | 2020-09-27 17:23:09 +0200 | [diff] [blame^] | 100 | self::checkRequestMethod('GET'); |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 101 | self::checkSignInStatus(); |
avm99963 | 339e6f7 | 2020-09-27 17:12:43 +0200 | [diff] [blame] | 102 | $subjects = Subjects::getUserSubjects(); |
| 103 | |
| 104 | if ($subjects === false) |
| 105 | self::returnError(); |
| 106 | |
| 107 | self::returnPayload([ |
| 108 | 'subjects' => $subjects |
| 109 | ]); |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 110 | break; |
| 111 | |
avm99963 | 339e6f7 | 2020-09-27 17:12:43 +0200 | [diff] [blame] | 112 | case 'addUserSubject': |
| 113 | self::checkSignInStatus(); |
| 114 | $body = self::getJSONBody(); |
| 115 | if (!isset($body['subject'])) |
| 116 | self::returnError(); |
| 117 | |
| 118 | if (Subjects::addUserSubject((int)$body['subject'])) |
| 119 | self::returnOk(); |
| 120 | else |
| 121 | self::returnError(); |
| 122 | break; |
| 123 | |
| 124 | case 'removeUserSubject': |
| 125 | self::checkSignInStatus(); |
| 126 | $body = self::getJSONBody(); |
| 127 | if (!isset($body['subject'])) |
| 128 | self::returnError(); |
| 129 | |
| 130 | if (Subjects::removeUserSubject((int)$body['subject'])) |
| 131 | self::returnOk(); |
| 132 | else |
| 133 | self::returnError(); |
| 134 | break; |
| 135 | |
avm99963 | 7099538 | 2020-09-23 01:03:01 +0200 | [diff] [blame] | 136 | case 'getClasses': |
| 137 | self::checkSignInStatus(); |
| 138 | // @TODO: Implement this method |
| 139 | break; |
| 140 | |
| 141 | case 'setClassState': |
| 142 | self::checkSignInStatus(); |
| 143 | // @TODO: Handle this method |
| 144 | break; |
| 145 | |
| 146 | default: |
| 147 | self::returnError('The method requested doesn\'t exist.'); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | } |