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); |
| 7 | } |
| 8 | |
| 9 | public static function returnError($errorMessage = 'Unexpected error') { |
| 10 | http_response_code(400); |
| 11 | self::returnJson([ |
| 12 | 'status' => 'error', |
| 13 | 'errorMessage' => $errorMessage |
| 14 | ]); |
| 15 | } |
| 16 | |
| 17 | public static function returnPayload($payload) { |
| 18 | self::returnJson([ |
| 19 | 'status' => 'ok', |
| 20 | 'payload' => $payload |
| 21 | ]); |
| 22 | } |
| 23 | |
| 24 | public static function returnOk() { |
| 25 | self::returnJson([ |
| 26 | 'status' => 'ok' |
| 27 | ]); |
| 28 | } |
| 29 | |
| 30 | private static function checkSignInStatus() { |
| 31 | if (!Users::isSignedIn()) { |
| 32 | self::returnError('The user hasn\'t signed in.'); |
| 33 | exit(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public static function process($path) { |
| 38 | global $conf; |
| 39 | |
| 40 | header('Content-Type: application/json'); |
| 41 | |
| 42 | if (isset($conf['frontendUrl']) && !empty($conf['frontendUrl'])) |
| 43 | header('Access-Control-Allow-Origin: '.$conf['frontendUrl']); |
| 44 | |
| 45 | $parts = explode('/', $path); |
| 46 | $method = $parts[0] ?? ''; |
| 47 | |
| 48 | switch ($method) { |
| 49 | case 'getAuthUrl': |
| 50 | $auth = new Auth(); |
| 51 | self::returnPayload([ |
| 52 | 'url' => $auth->getAuthUrl() |
| 53 | ]); |
| 54 | break; |
| 55 | |
| 56 | case 'isSignedIn': |
| 57 | $isSignedIn = \DAFME\Covid\Users::isSignedIn(); |
| 58 | self::returnPayload([ |
| 59 | 'signedIn' => $isSignedIn |
| 60 | ]); |
| 61 | break; |
| 62 | |
| 63 | case 'signOut': |
| 64 | \DAFME\Covid\Users::signOut(); |
| 65 | self::returnOk(); |
| 66 | break; |
| 67 | |
| 68 | case 'getAllSubjects': |
| 69 | $subjects = Subjects::getAll(); |
| 70 | |
| 71 | if ($subjects === false) |
| 72 | self::returnError(); |
| 73 | |
| 74 | self::returnPayload([ |
| 75 | 'subjects' => $subjects |
| 76 | ]); |
| 77 | break; |
| 78 | |
| 79 | case 'getUserSubjects': |
| 80 | self::checkSignInStatus(); |
| 81 | // @TODO: Implement this method |
| 82 | break; |
| 83 | |
| 84 | case 'setUserSubjects': |
| 85 | self::checkSignInStatus(); |
| 86 | // @TODO: Implement this method |
| 87 | break; |
| 88 | |
| 89 | case 'getClasses': |
| 90 | self::checkSignInStatus(); |
| 91 | // @TODO: Implement this method |
| 92 | break; |
| 93 | |
| 94 | case 'setClassState': |
| 95 | self::checkSignInStatus(); |
| 96 | // @TODO: Handle this method |
| 97 | break; |
| 98 | |
| 99 | default: |
| 100 | self::returnError('The method requested doesn\'t exist.'); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |