blob: 8ecfdd18eeccf236d46f445fc86b8f07e6542b04 [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
avm999633cc83b62020-09-27 21:03:44 +020053 private static function setCORSHeaders() {
54 global $conf;
55 if ((isset($conf['allowAllOrigins']) && $conf['allowAllOrigins']) ||
56 (isset($conf['allowedOrigins']) &&
57 isset($_SERVER['HTTP_ORIGIN']) &&
58 in_array($_SERVER['HTTP_ORIGIN'], $conf['allowedOrigins']))) {
59 header('Access-Control-Allow-Origin: '.($_SERVER['HTTP_ORIGIN'] ?? '*'));
60 header('Access-Control-Allow-Credentials: true');
61 }
62 }
63
64
avm9996370995382020-09-23 01:03:01 +020065 public static function process($path) {
66 global $conf;
67
68 header('Content-Type: application/json');
avm999633cc83b62020-09-27 21:03:44 +020069 self::setCORSHeaders();
avm9996370995382020-09-23 01:03:01 +020070
71 $parts = explode('/', $path);
72 $method = $parts[0] ?? '';
73
74 switch ($method) {
75 case 'getAuthUrl':
avm9996380afa682020-09-27 17:23:09 +020076 self::checkRequestMethod('GET');
avm9996370995382020-09-23 01:03:01 +020077 $auth = new Auth();
78 self::returnPayload([
79 'url' => $auth->getAuthUrl()
80 ]);
81 break;
82
83 case 'isSignedIn':
avm9996380afa682020-09-27 17:23:09 +020084 self::checkRequestMethod('GET');
avm9996370995382020-09-23 01:03:01 +020085 $isSignedIn = \DAFME\Covid\Users::isSignedIn();
86 self::returnPayload([
87 'signedIn' => $isSignedIn
88 ]);
89 break;
90
91 case 'signOut':
avm9996380afa682020-09-27 17:23:09 +020092 self::checkRequestMethod('POST');
avm9996370995382020-09-23 01:03:01 +020093 \DAFME\Covid\Users::signOut();
94 self::returnOk();
95 break;
96
97 case 'getAllSubjects':
avm9996380afa682020-09-27 17:23:09 +020098 self::checkRequestMethod('GET');
avm9996370995382020-09-23 01:03:01 +020099 $subjects = Subjects::getAll();
100
101 if ($subjects === false)
102 self::returnError();
103
104 self::returnPayload([
105 'subjects' => $subjects
106 ]);
107 break;
108
109 case 'getUserSubjects':
avm9996380afa682020-09-27 17:23:09 +0200110 self::checkRequestMethod('GET');
avm9996370995382020-09-23 01:03:01 +0200111 self::checkSignInStatus();
avm99963339e6f72020-09-27 17:12:43 +0200112 $subjects = Subjects::getUserSubjects();
113
114 if ($subjects === false)
115 self::returnError();
116
117 self::returnPayload([
118 'subjects' => $subjects
119 ]);
avm9996370995382020-09-23 01:03:01 +0200120 break;
121
avm99963339e6f72020-09-27 17:12:43 +0200122 case 'addUserSubject':
123 self::checkSignInStatus();
124 $body = self::getJSONBody();
125 if (!isset($body['subject']))
126 self::returnError();
127
128 if (Subjects::addUserSubject((int)$body['subject']))
129 self::returnOk();
130 else
131 self::returnError();
132 break;
133
134 case 'removeUserSubject':
135 self::checkSignInStatus();
136 $body = self::getJSONBody();
137 if (!isset($body['subject']))
138 self::returnError();
139
140 if (Subjects::removeUserSubject((int)$body['subject']))
141 self::returnOk();
142 else
143 self::returnError();
144 break;
145
avm99963282af722020-09-28 17:43:37 +0200146 case 'getCurrentClasses':
147 self::checkRequestMethod('GET');
148 $classes = Classes::getCurrentClasses();
149 if ($classes === false)
avm99963472b0de2020-09-28 17:15:42 +0200150 self::returnError();
151 else
avm99963282af722020-09-28 17:43:37 +0200152 self::returnPayload([
153 'classes' => $classes
154 ]);
avm9996370995382020-09-23 01:03:01 +0200155 break;
156
157 case 'setClassState':
158 self::checkSignInStatus();
159 // @TODO: Handle this method
160 break;
161
162 default:
163 self::returnError('The method requested doesn\'t exist.');
164 break;
165 }
166 }
167}