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