-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.php
405 lines (351 loc) · 13.6 KB
/
rest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
require_once('orm/Authentication.php');
require_once('orm/DirectedMessage.php');
require_once('orm/Favorite.php');
require_once('orm/Group.php');
require_once('orm/Member.php');
require_once('orm/Message.php');
require_once('orm/User.php');
$path_components = (isset($_SERVER['PATH_INFO']) ? explode('/', $_SERVER['PATH_INFO']) : '');
$path_length = count($path_components);
/*
print("path_components: ");
print_r($path_components);
print("<br>");
print("path_components length: " . $path_length . "<br>");*/
if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($path_length >= 2 && $path_components[1] != "") {
// GET user
if ($path_components[1] == "user") {
if (isset($path_components[2]) && $path_components[2] != "") {
$user_id = intval($path_components[2]);
$user = User::find_by_id($user_id);
if ($user == null) {
// User did not exist
header("HTTP/1.0 404 Not Found");
print("User id: " . $user_id . " not found.<br>");
exit();
}
}
// Looking for the json of a specific user
if ($path_length == 3 && $path_components[2] != ""|| ($path_length == 4 && $path_components[3] == "")) {
header("Content-type: application/json");
print($user->getJSON());
exit();
}
else if ($path_length >= 4) {
// Return message ids owned by user
if ($path_components[3] == "message") {
header("Content-type: application/json");
print(json_encode(Message::get_message_ids_of_user($user_id)));
exit();
}
// Return ids of groups to which the user id belongs
else if ($path_components[3] == "group") {
header("Content-type: application/json");
print(json_encode(Member::get_groups_of_user($user_id)));
exit();
}
// Return ids of messages that the user id favorited
else if ($path_components[3] == "favorite") {
header("Content-type: application/json");
print(json_encode(Favorite::get_favorited_message_ids_of_user($user_id)));
exit();
}
}
// Id not specified, looking for index of Users
header("Content-type: application/json");
print(json_encode(User::get_all_ids()));
exit();
}
// GET message
else if ($path_components[1] == "message") {
if (isset($path_components[2]) && $path_components[2] != "") {
$message_id = intval($path_components[2]);
$message = Message::find_by_id($message_id);
if ($message == null) {
// Message did not exist
header("HTTP/1.0 404 Not Found");
print("Message id: " . $message_id . " not found.<br>");
exit();
}
// Looking for the json of a specific message
if ($path_length == 3 || ($path_length == 4 && $path_components[3] == "")) {
header("Content-type: application/json");
print($message->getJSON());
exit();
}
else if ($path_length == 4) {
// Return user ids that favorited a given message id
if ($path_components[3] == "favorite") {
header("Content-type: application/json");
print(json_encode(Favorite::get_user_ids_of_favorited_message($message_id)));
exit();
}
}
}
// Id not specified, looking for index of Messages
header("Content-type: application/json");
print(json_encode(Message::get_all_ids()));
exit();
}
// GET group
else if ($path_components[1] == "group") {
if (isset($path_components[2]) && $path_components[2] != "") {
$group_id = intval($path_components[2]);
$group = Group::find_by_id($group_id);
if ($group == null) {
// Group did not exist
header("HTTP/1.0 404 Not Found");
print("Group id: " . $group_id . " not found.<br>");
exit();
}
// Looking for the json of a specific user
if ($path_length == 3 || ($path_length == 4 && $path_components[3] == "")) {
header("Content-type: application/json");
print($group->getJSON());
exit();
}
else if ($path_length >= 4) {
// Return message ids belonging to a given group id
if ($path_components[3] == "message") {
header("Content-type: application/json");
print(json_encode(Message::get_message_ids_of_group($group_id)));
exit();
}
// Return ids of users belonging to a given group id
else if ($path_components[3] == "user") {
header("Content-type: application/json");
print(json_encode(Member::get_user_ids_in_group($group_id)));
exit();
}
}
}
// Id not specified, looking for index of Groups
header("Content-type: application/json");
print(json_encode(Group::get_all_ids()));
exit();
}
}
}
else if ($_SERVER['REQUEST_METHOD'] == "PUT"){
$message_id = intval($path_components[1]);
$message = Message::find_by_id($message_id);
}
else if ($_SERVER['REQUEST_METHOD'] == "POST"){
if ($path_length >= 2 && $path_components[1] != "") {
// POST message - create a new message
if ($path_components[1] == "message") {
// Create new message
if (!isset($_REQUEST['user_id'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing user_id");
exit();
}
$user_id = intval(trim($_REQUEST['user_id']));
if (!isset($_REQUEST['group_id'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing group_id");
exit();
}
$group_id = intval(trim($_REQUEST['group_id']));
if (!isset($_REQUEST['text'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing text");
exit();
}
$text = trim($_REQUEST['text']);
if (!isset($_REQUEST['code_flag'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing code flag");
exit();
}
$code_flag = trim($_REQUEST['code_flag']);
$new_message = Message::create($user_id, $group_id, $text, $code_flag);
// Report if failed
if ($new_message == null) {
header("HTTP/1.0 500 Server Error");
print("Server couldn't create new message");
exit();
}
// Generate JSON encoding of new message
header("Content-type: application/json");
print($new_message->getJSON());
exit();
}
// POST group
else if ($path_components[1] == "group") {
if (!isset($_REQUEST['name'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing name");
exit();
}
$name = trim($_REQUEST['name']);
$description = null;
if (isset($_REQUEST['description'])) {
$description = trim($_REQUEST['description']);
}
$github_url = null;
if (isset($_REQUEST['github_url'])) {
$github_url = trim($_REQUEST['github_url']);
}
$new_group = Group::create($name, $description, $github_url);
// Report if failed
if ($new_group == null) {
header("HTTP/1.0 500 Server Error");
print("Server couldn't create new group");
exit();
}
// Generate JSON encoding of new group
header("Content-type: application/json");
print($new_group->getJSON());
exit();
}
// POST member
else if ($path_components[1] == "member") {
if (!isset($_REQUEST['group_id'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing group_id");
exit();
}
$group_id = intval(trim($_REQUEST['group_id']));
if (!isset($_REQUEST['email']) && !isset($_REQUEST['user_id'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing user_id or email");
exit();
}
if (isset($_REQUEST['email'])) {
$email = trim($_REQUEST['email']);
$user_id = User::find_by_email($email)->get_id();
} else {
$user_id = trim($_REQUEST['user_id']);
}
$new_member = Member::create($user_id, $group_id);
// Report if failed
if ($new_member == null) {
header("HTTP/1.0 500 Server Error");
print("Server couldn't create new member");
exit();
}
// Generate JSON encoding of new member
header("Content-type: application/json");
print($new_member->getJSON());
//Member::notify($user_id, $group_id);
exit();
}
// POST user
else if ($path_components[1] == "user" && $path_length <= 3 && isset($path_components[2]) && $path_components[2] == "") {
if (!isset($_REQUEST['name'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing name");
exit();
}
$name = trim($_REQUEST['name']);
if (!isset($_REQUEST['password'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing password");
exit();
}
$password = trim($_REQUEST['password']);
if (!isset($_REQUEST['email'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing email");
exit();
}
$email = trim($_REQUEST['email']);
$github = null;
if (isset($_REQUEST['github'])) {
$github = trim($_REQUEST['github']);
}
$new_user = User::create($name, $email, $github);
// Report if failed
if ($new_user == null) {
header("HTTP/1.0 500 Server Error");
print("Server couldn't create new user");
exit();
}
Authentication::create($new_user->get_id(), $password);
// Generate JSON encoding of new user
header("Content-type: application/json");
print($new_user->getJSON());
exit();
}
// POST user - update existing user
else if ($path_components[1] == "user" && $path_components[2] != "") {
$user_id = $path_components[2];
$user = User::find_by_id($user_id);
if ($user == null) {
// User did not exist
header("HTTP/1.0 404 Not Found");
print("User id: " . $user_id . " not found.<br>");
exit();
}
// Delete user
if (isset($_REQUEST['remove']) && $_REQUEST['remove'] == "remove") {
$user->remove();
}
// Update user
else {
if (isset($_REQUEST['name']) && $_REQUEST['name'] !== "") {
$user->set_name(trim($_REQUEST['name']));
}
if (isset($_REQUEST['email']) && $_REQUEST['email'] !== "") {
$user->set_email(trim($_REQUEST['email']));
}
if (isset($_REQUEST['github']) && $_REQUEST['github'] !== "") {
$user->set_github(trim($_REQUEST['github']));
}
if (isset($_REQUEST['old_password']) && isset($_REQUEST['new_password']) && $_REQUEST['old_password'] !== "" && $_REQUEST['new_password'] !== "") {
header("Content-type: application/json");
if ($auth->set_password(trim($_REQUEST['old_password']), trim($_REQUEST['new_password']))) {
print("HTTP/1.0 200 OK");
} else {
print("HTTP/1.0 401 UNAUTHORIZED");
}
exit();
}
header("Content-type: application/json");
print($user->getJSON());
exit();
}
}
// POST user - authentication
else if ($path_components[1] == "auth") {
if (!isset($_REQUEST['email'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing email");
exit();
}
$email = trim($_REQUEST['email']);
if (!isset($_REQUEST['password'])) {
header("HTTP/1.0 400 Bad Request");
print("Missing password");
exit();
}
$password = trim($_REQUEST['password']);
$user = User::find_by_email($email);
if ($user == null) {
// User did not exist
header("HTTP/1.0 401 UNAUTHORIZED");
exit();
}
$user_id = $user->get_id();
$auth = Authentication::find_by_user_id($user_id);
if ($auth->authenticate($password)) {
header("HTTP/1.0 200 OK");
//$_SESSION['user_id'] = $user_id;
//$_SESSION['authsalt'] = time();
//$auth_cookie_val = md5($_SESSION['user_id'] . $_SERVER['REMOTE_ADDR'] . $_SESSION['authsalt']);
//setcookie('codechat_session', $auth_cookie_val, 0, "/");
setcookie('codechat_session', $user_id, time() + (86400 * 30), "/");
} else {
header("HTTP/1.0 401 UNAUTHORIZED");
}
exit();
}
}
}
//None of the above works
header("HTTP/1.0 400 Bad Request");
print("Did not understand URL");
?>