blob: a3607bb2100b2f9aa472699a265f12b9819decde [file] [log] [blame]
avm9996370995382020-09-23 01:03:01 +02001<?php
2namespace DAFME\Covid;
3
4class API {
5 private static function returnJSON($array) {
6 echo json_encode($array);
avm99963339e6f72020-09-27 17:12:43 +02007 exit();
avm9996370995382020-09-23 01:03:01 +02008 }
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.');
avm9996370995382020-09-23 01:03:01 +020034 }
35 }
36
avm9996380afa682020-09-27 17:23:09 +020037 private static function checkRequestMethod(string $method) {
38 if ($_SERVER['REQUEST_METHOD'] !== $method)
39 self::returnError('This action requires using the '.$method.' method.');
40 }
41
avm99963339e6f72020-09-27 17:12:43 +020042 private static function getJSONBody() {
avm9996380afa682020-09-27 17:23:09 +020043 self::checkRequestMethod('POST');
avm99963339e6f72020-09-27 17:12:43 +020044
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
avm9996370995382020-09-23 01:03:01 +020053 public static function process($path) {
54 global $conf;
55
56 header('Content-Type: application/json');
57
avm99963dbd9a122020-09-24 18:51:45 +020058 if (isset($conf['allowedOrigin']) && !empty($conf['allowedOrigin']))
59 header('Access-Control-Allow-Origin: '.$conf['allowedOrigin']);
avm9996370995382020-09-23 01:03:01 +020060
61 $parts = explode('/', $path);
62 $method = $parts[0] ?? '';
63
64 switch ($method) {
65 case 'getAuthUrl':
avm9996380afa682020-09-27 17:23:09 +020066 self::checkRequestMethod('GET');
avm9996370995382020-09-23 01:03:01 +020067 $auth = new Auth();
68 self::returnPayload([
69 'url' => $auth->getAuthUrl()
70 ]);
71 break;
72
73 case 'isSignedIn':
avm9996380afa682020-09-27 17:23:09 +020074 self::checkRequestMethod('GET');
avm9996370995382020-09-23 01:03:01 +020075 $isSignedIn = \DAFME\Covid\Users::isSignedIn();
76 self::returnPayload([
77 'signedIn' => $isSignedIn
78 ]);
79 break;
80
81 case 'signOut':
avm9996380afa682020-09-27 17:23:09 +020082 self::checkRequestMethod('POST');
avm9996370995382020-09-23 01:03:01 +020083 \DAFME\Covid\Users::signOut();
84 self::returnOk();
85 break;
86
87 case 'getAllSubjects':
avm9996380afa682020-09-27 17:23:09 +020088 self::checkRequestMethod('GET');
avm9996370995382020-09-23 01:03:01 +020089 $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':
avm9996380afa682020-09-27 17:23:09 +0200100 self::checkRequestMethod('GET');
avm9996370995382020-09-23 01:03:01 +0200101 self::checkSignInStatus();
avm99963339e6f72020-09-27 17:12:43 +0200102 $subjects = Subjects::getUserSubjects();
103
104 if ($subjects === false)
105 self::returnError();
106
107 self::returnPayload([
108 'subjects' => $subjects
109 ]);
avm9996370995382020-09-23 01:03:01 +0200110 break;
111
avm99963339e6f72020-09-27 17:12:43 +0200112 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
avm9996370995382020-09-23 01:03:01 +0200136 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}