-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
executable file
·737 lines (629 loc) · 25.9 KB
/
api.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
<?php
class API {
private $data_obj;
private $user_id;
private $outside_username = null;
private $outside_password = null;
public function __construct() {
include ('data.php');
$this->data_obj = new Data;
// mod_php
if (isset($_SERVER['PHP_AUTH_USER'])) {
$this->outside_username = $_SERVER['PHP_AUTH_USER'];
$this->outside_password = $_SERVER['PHP_AUTH_PW'];
// most other servers
} elseif (isset($_SERVER['HTTP_AUTHENTICATION'])) {
if (strpos(strtolower($_SERVER['HTTP_AUTHENTICATION']),'basic')===0) {
list($this->outside_username,$this->outside_password) = explode(':',base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
}
}
// do a check here for the user, and check how many requests they've tried doing in the past hour
if ($this->data_obj->check_user_exists($this->outside_username, $this->outside_password)==true) {
// user exists
// query the username for the user_id and return the user_id in the obj below
$this->user_id = $this->data_obj->get_user_id_from_username($this->outside_username);
// check the quota
if ($this->data_obj->check_quota($this->user_id)!=true) {
// user is over the limit
$this->data_obj->send_response(429);
die();
}
} else if ($this->data_obj->check_user_exists($this->outside_username, $this->outside_password)==false) {
// user doesn't exist
$this->data_obj->send_response(401);
die();
}
header("Access-Control-Allow-Methods: *");
header("Content-Type: application/json");
}
public function api_do() {
// alright let's look for the first part of the string: 'users', then let's look to see if
// there's a second param to check for 'articles', if there is, show articles by the user id,
// example: users/1/articles, this would display all the articles by user 1
$uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];
$paths = explode('/', $this->data_obj->paths($uri));
// explode being strange giving "", we don't want that
if (end($paths)=="") { array_pop($paths); }
$original = $paths;
// store paths to check if both exist
$triple_resources = $paths;
$quad_resources = $paths;
@$pagination = $paths[3];
array_shift($quad_resources);
array_shift($quad_resources);
array_shift($quad_resources);
array_shift($quad_resources);
array_shift($quad_resources);
array_shift($paths); // Hack; get rid of initials empty string
array_shift($paths); // Hack; shift again
array_shift($paths); // Hack; shift again
array_shift($paths); // Hack; shift again
array_shift($paths); // Hack; shift again
$resource = array_shift($paths);
$id = array_shift($paths);
array_shift($triple_resources); // Hack; shift again
array_shift($triple_resources); // Hack; shift again
array_shift($triple_resources); // Hack; shift again
array_shift($triple_resources); // Hack; shift again
array_shift($triple_resources); // Hack; shift again
// we must first check for triple resources (users/id/articles), then if the double doesn't exist
// we can go ahead and check for the singles
// will return true if no elements are empty
if ($this->data_obj->isEmpty($triple_resources)==true && count($triple_resources)==3 && $triple_resources[1]!="results"
&& ($triple_resources[2] !="comments")) {
$id = $triple_resources[1];
// now we must query for the relationships because we know that the url is: (users/user_id/articles)
unset($triple_resources[1]);
// we now have 'users, articles that we can pass in table and split it up later for a join'
$table = implode(', ', $triple_resources);
array_splice($triple_resources, 1);
array_shift($triple_resources);
if (!empty($id)) {
$this->handle_name($method, $id, $table);
} else {
// don't return anything as we're looking for relationships here..
// send a http header response
$this->data_obj->send_response(401);
}
} else if ($this->data_obj->isEmpty(@$triple_resources)==true && count(@$triple_resources)==3 &&
end($triple_resources)=="comments") {
// now we must query for the relationships because we know that the url is: (articles/article_id/comments)
unset($triple_resources[1]);
// we now have 'users, articles that we can pass in table and split it up later for a join'
$table = implode(', ', $triple_resources);
array_splice($triple_resources, 1);
array_shift($triple_resources);
if (!empty($id)) {
$this->handle_name($method, $id, $table);
} else {
// don't return anything as we're looking for relationships here..
// send a http header response
$this->data_obj->send_response(401);
}
} else if ($this->data_obj->isEmpty(@$quad_resources)==true && count(@$quad_resources)==4 &&
is_numeric(end($quad_resources)) && $quad_resources[2]=="comments") {
// going to quad_resources we could get the following: (users/user_id/articles/article_id)
$id = $quad_resources[1];
$article_id = $quad_resources[3];
unset($quad_resources[1]);
unset($quad_resources[3]);
$table = implode(', ', $quad_resources) . ", pagination_hmtr";
if (!empty($id)) {
// let's pass the article_id as value but for $method
$this->handle_name($method, $id, $table, $article_id);
} else {
// don't return anything as we're looking for relationships here..
// send a http header response
$this->data_obj->send_response(400);
}
} else if ($resource == 'users') {
$table = 'users';
// let's check for pagination
if (in_array("results", $original)) {
// we must then get the users by pagination, start end
// let's pass it into table
$this->handle_base($method, $table.",".end($original));
}
if (empty($id)) {
// no id so return all users
$this->handle_base($method, $table);
} else {
if (is_numeric($id)) {
$this->handle_name($method, $id, $table);
} else {
// put
$username = end($original);
$this->handle_name($method, $id, $table, $username);
}
}
} else if ($resource == 'articles') {
$table = 'articles';
// let's check for pagination
if (in_array("results", $original)) {
// we must then get the users by pagination, start end
// let's pass it into table
$this->handle_base($method, $table.",".end($original));
}
if (empty($id)) {
// no id so return all users
$this->handle_base($method, $table);
} else {
if (is_numeric($id)) {
$this->handle_name($method, $id, $table);
}
}
} else if ($resource == 'comments') {
$table = 'comments';
// let's check for pagination
if (in_array("results", $original)) {
// we must then get the users by pagination, start end
// let's pass it into table
$this->handle_base($method, $table.",".end($original));
}
if (empty($id)) {
// no id so return all users
$this->handle_base($method, $table);
} else {
if (is_numeric($id)) {
$this->handle_name($method, $id, $table);
}
}
} else {
$this->data_obj->send_response(404);
}
}
private function handle_base($method, $table) {
switch($method) {
case 'GET':
@$users_table_limit = substr($table, 0, 5);
@$users_limit = substr($table, 6);
@$articles_table_limit = substr($table, 0, 8);
@$articles_limit = substr($table, 9);
@$comments_table_limit = substr($table, 0, 8);
@$comments_limit = substr($table, 9);
if ($table=="users") {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_all_users();
} else if (@$users_table_limit=="users" && !empty($users_table_limit) && !empty($users_limit) && is_numeric(substr($users_limit, 0,1))) {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_users_limit($users_table_limit, $users_limit);
} else if (@$articles_table_limit=="articles" && !empty($articles_table_limit) && !empty($articles_limit) && is_numeric(substr($articles_limit, 0,1))) {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_articles_limit($users_table_limit, $users_limit);
} else if (@$comments_table_limit=="comments" && !empty($comments_table_limit) && !empty($comments_limit) && is_numeric(substr($comments_limit, 0,1))) {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_comments_limit($comments_table_limit, $comments_limit);
} else if ($table=="articles") {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_all_articles();
} else if ($table=="comments") {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_all_comments();
}
break;
case 'PUT':
$this->data_obj->update_quota($this->outside_username);
$this->edit($table, 0);
break;
case 'POST':
$this->data_obj->update_quota($this->outside_username);
$this->insert($table, 0);
break;
case 'DELETE':
$this->data_obj->update_quota($this->outside_username);
$this->delete($table);
break;
default:
$this->data_obj->send_response(405);
break;
}
}
private function handle_name($method, $id, $table, $value="") {
$uri = $_SERVER['REQUEST_URI'];
$paths = explode('/', $this->data_obj->paths($uri));
switch ($method) {
case 'POST':
$this->data_obj->update_quota($this->outside_username);
$this->insert($table, $value);
break;
case 'PUT':
$this->data_obj->update_quota($this->outside_username);
$this->edit($table, $value);
break;
case 'DELETE':
$this->data_obj->update_quota($this->outside_username);
$this->delete($table);
break;
case 'GET':
if ($table == "users" && !empty($id) && !in_array("results", $paths)) {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_user($id);
} else if ($table == "articles" && !empty($id)) {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_article($id);
} else if ($table == "comments" && !empty($id)) {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_comment($id);
} else if ($table == "users, articles") {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_users_articles($id, $table);
} else if ($table == "users, articles, pagination_hmtr") {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_pagination_hmtr_by_user($id, $table, $value);
} else if ($table == "articles, comments") {
$this->data_obj->update_quota($this->outside_username);
$this->data_obj->get_article_comments($id, $table);
}
break;
default:
$this->data_obj->send_response(405);
break;
}
}
public function edit($table, $value) {
$data = json_decode(file_get_contents('php://input'));
if ($table=="users") {
if (empty($data->username)) {
$this->data_object->send_response(400);
} else {
if (!empty($data->first_name)) {
$sql = "UPDATE users SET first_name = :first_name WHERE username = :username";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('username', $data->username);
$stmt->bindParam("first_name", $data->first_name);
$count = $stmt->execute();
}
if (!empty($data->last_name)) {
$sql = "UPDATE users SET last_name = :last_name WHERE username = :username";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('username', $data->username);
$stmt->bindParam("last_name", $data->last_name);
$count = $stmt->execute();
}
if (!empty($data->email_address)) {
$sql = "UPDATE users SET email_address = :email_address WHERE username = :username";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('username', $data->username);
$stmt->bindParam("email_address", $data->email_address);
$count = $stmt->execute();
}
// row has been updated, therefore give back 200
if ($count>=1) {
$user_id = $this->data_obj->get_user_id_from_users_table($data->username);
$result['success']="true";
$result['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/users/".$user_id;
$this->data_obj->send_response(200, json_encode($result));
} else {
$this->data_obj->send_response(400);
}
}
} else if ($table=="articles") {
if (empty($data->article_id)) {
$this->data_object->send_response(400);
} else {
if ($table=="articles") {
// check article_title, article_sub_title, article_content isn't empty from $data
if (!empty($data->article_title)) {
$sql = "UPDATE articles SET article_title = :article_title WHERE article_id = :article_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('article_title', $data->article_title);
$stmt->bindParam("article_id", $data->article_id);
$count = $stmt->execute();
}
if (!empty($data->article_sub_title)) {
$sql = "UPDATE articles SET article_sub_title = :article_sub_title WHERE article_id = :article_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('article_sub_title', $data->article_sub_title);
$stmt->bindParam("article_id", $data->article_id);
$count = $stmt->execute();
}
if (!empty($data->article_content)) {
$sql = "UPDATE articles SET article_content = :article_content WHERE article_id = :article_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('article_content', $data->article_content);
$stmt->bindParam("article_id", $data->article_id);
$count = $stmt->execute();
}
if (!empty($data->article_views)) {
$sql = "UPDATE articles SET article_views = :article_views WHERE article_id = :article_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('article_views', $data->article_views);
$stmt->bindParam("article_id", $data->article_id);
$count = $stmt->execute();
}
if (!empty($data->article_unique_views)) {
$sql = "UPDATE articles SET article_unique_views = :article_unique_views WHERE article_id = :article_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('article_views', $data->article_unique_views);
$stmt->bindParam("article_id", $data->article_id);
$count = $stmt->execute();
}
if (!empty($data->article_author_id)) {
$sql = "UPDATE articles SET article_author_id = :article_author_id WHERE article_id = :article_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('article_author_id', $data->article_author_id);
$stmt->bindParam("article_id", $data->article_id);
$count = $stmt->execute();
}
// row has been updated, therefore give back 200
if ($count>=1) {
$result['success']="true";
$result['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/articles/".$data->article_id;
$this->data_obj->send_response(200, json_encode($result));
} else {
$this->data_obj->send_response(400);
}
}
}
} else if ($table=="comments") {
if (empty($data->comment_id)) {
$this->data_object->send_response(400);
} else {
if ($table=="comments") {
// check comment_full_name, comment_full, comment_location isn't empty from $data
if (!empty($data->comment_full_name)) {
$sql = "UPDATE comments SET comment_full_name = :comment_full_name WHERE comment_id = :comment_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('comment_full_name', $data->comment_full_name);
$stmt->bindParam("comment_id", $data->comment_id);
$count = $stmt->execute();
}
if (!empty($data->comment_full)) {
$sql = "UPDATE comments SET comment_full = :comment_full WHERE comment_id = :comment_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('comment_full', $data->comment_full);
$stmt->bindParam("comment_id", $data->comment_id);
$count = $stmt->execute();
}
if (!empty($data->comment_location)) {
$sql = "UPDATE comments SET comment_location = :comment_location WHERE comment_id = :comment_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('comment_location', $data->comment_location);
$stmt->bindParam("comment_id", $data->comment_id);
$count = $stmt->execute();
}
if (!empty($data->comment_likes)) {
$sql = "UPDATE comments SET comment_likes = :comment_likes WHERE comment_id = :comment_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('comment_likes', $data->comment_likes);
$stmt->bindParam("comment_id", $data->comment_id);
$count = $stmt->execute();
}
}
// row has been updated, therefore give back 200
if ($count>=1) {
$result['success']="true";
$result['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/comments/".$data->comment_id;
$this->data_obj->send_response(200, json_encode($result));
} else {
$this->data_obj->send_response(400);
}
}
}
}
public function insert($table, $value) {
$data = json_decode(file_get_contents('php://input'));
if ($table=="comments" && empty($data->comment_article_id)) {
$this->data_obj->send_response(400);
} else if ($table=="comments" && is_numeric($data->comment_article_id) && !empty($data->comment_article_id)) {
if (empty($data->comment_full_name)) {
$data->comment_full_name = "";
}
if (empty($data->comment_full)) {
$data->comment_full = "";
}
if (empty($data->comment_location)) {
$data->comment_location = "";
}
if (empty($data->comment_likes)) {
$data->comment_likes = 0;
}
$latest_comment_id = $this->data_obj->get_latest_comment_id();
$sql = "INSERT INTO comments (comment_id, comment_full_name, comment_full, comment_location, comment_likes, comment_article_id)
VALUES (:comment_id, :comment_full_name, :comment_full, :comment_location, :comment_likes, :comment_article_id)";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('comment_id', $latest_comment_id);
$stmt->bindParam('comment_article_id', $data->comment_article_id);
$stmt->bindParam('comment_full_name', $data->comment_full_name);
$stmt->bindParam("comment_full", $data->comment_full);
$stmt->bindParam("comment_location", $data->comment_location);
$stmt->bindParam("comment_likes", $data->comment_likes);
// if success insert, return 201
if ($stmt->execute()==true) {
$result['success']="true";
$result['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/comments/".$latest_comment_id;
$this->data_obj->send_response(201, json_encode($result));
} else {
// if something went wrong
$this->data_obj->send_response(409);
}
} else {
$this->data_obj->send_response(400);
}
if ($table=="articles" && empty($data->article_title)) {
$this->data_obj->send_response(400);
} else if ($table=="articles" && !empty($data->article_title)) {
if (empty($data->article_title)) {
$data->article_title = "";
}
if (empty($data->article_sub_title)) {
$data->article_sub_title = "";
}
if (empty($data->article_content)) {
$data->article_content = "";
}
if (empty($data->article_views)) {
$data->article_views = 0;
}
if (empty($data->article_unique_views)) {
$data->article_unique_views = 0;
}
if (empty($data->article_author_id)) {
$data->article_author_id = 0;
}
if (empty($data->article_post_date)) {
$data->article_post_date = "";
}
$latest_article_id = $this->data_obj->get_article_id_latest();
$sql = "INSERT INTO articles (article_id, article_title, article_sub_title, article_content, article_views, article_unique_views, article_author_id, article_post_date)
VALUES (:article_id, :article_title, :article_sub_title, :article_content, :article_views, :article_unique_views, :article_author_id, :article_post_date)";
$stmt = $this->data_obj->dbc->prepare($sql);
// if these are empty it will just insert empty values, which later can be updated using put
$stmt->bindParam('article_id', $latest_article_id);
$stmt->bindParam('article_title', $data->article_title);
$stmt->bindParam('article_sub_title', $data->article_sub_title);
$stmt->bindParam("article_content", $data->article_content);
$stmt->bindParam("article_views", $data->article_views);
$stmt->bindParam("article_unique_views", $data->article_unique_views);
$stmt->bindParam("article_author_id", $data->article_author_id);
$stmt->bindParam("article_post_date", $data->article_post_date);
// if success insert, return 201
if ($stmt->execute()==true) {
// HATEOAS
$result['success']="true";
$result['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/articles/".$latest_article_id;
$this->data_obj->send_response(201, json_encode($result));
} else {
// if something went wrong
$this->data_obj->send_response(409);
}
}
// if the username is empty for users, send back a 400
if ($table=="users" && empty($data->username)) {
$this->data_obj->send_response(400);
} else if ($table=="users" && !empty($data->username)) {
if (empty($data->first_name)) {
$data->first_name = "";
}
if (empty($data->last_name)) {
$data->last_name = "";
}
if (empty($data->email_address)) {
$data->email_address = "";
}
// check for same email_address or username, else insert
$sql = "SELECT email_address, username FROM users WHERE email_address = :email_address OR username = :username";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('email_address', $data->email_address);
$stmt->bindParam('username', $data->username);
$stmt->execute();
$result = $stmt->fetch(\PDO::FETCH_OBJ);
$num_rows = $stmt->rowCount();
// if empty result and email_address isn't found, go ahead and prepare to insert
if ($num_rows<=0) {
// let's insert because there's no duplicate email_address
try {
$sql = "INSERT INTO users (user_id, username, first_name, last_name, email_address) VALUES (:user_id, :username, :first_name,
:last_name, :email_address)";
$stmt = $this->data_obj->dbc->prepare($sql);
$last_id = $this->data_obj->get_user_id_latest();
// if these are empty it will just insert empty values, which later can be updated using put
$stmt->bindParam('user_id', $last_id);
$stmt->bindParam('username', $data->username);
$stmt->bindParam("first_name", $data->first_name);
$stmt->bindParam("last_name", $data->last_name);
$stmt->bindParam("email_address", $data->email_address);
// if success insert, return 201
if ($stmt->execute()==true) {
// HATEOAS
$result['success']="true";
$result['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/users/".$last_id;
$this->data_obj->send_response(201, json_encode($result));
} else {
// if something went wrong
$this->data_obj->send_response(409);
}
} catch (\PDOException $e) {
$this->data_obj->send_response(400);
}
} else {
// send 409 back if duplicate
$this->data_obj->send_response(409);
}
}
}
private function delete($table) {
try {
$data = json_decode(file_get_contents('php://input'));
if ($table=="users") {
// we need a username to delete a user
$username = $data->username;
$sql = "SELECT username FROM users where username = :username";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('username', $username);
$stmt->execute();
$result = $stmt->fetch(\PDO::FETCH_OBJ);
$num_rows = $stmt->rowCount();
if ($num_rows==1) {
// user is there
$result=array();
$result['success']="true";
$sql = "DELETE FROM users WHERE username = :username";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('username', $username);
$deleted = $stmt->execute();
if ($deleted==1) {
$this->data_obj->send_response(200, json_encode($result));
} else {
$this->data_obj->send_response(409);
}
} else {
$this->data_obj->send_response(409);
}
} else if ($table=="comments") {
// we need a comment id to delete a comment
$sql = "SELECT comment_id FROM comments where comment_id = :comment_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('comment_id', $data->comment_id);
$stmt->execute();
$num_rows = $stmt->rowCount();
if ($num_rows==1) {
// comment is there at that id
$result=array();
$result['success']="true";
$sql = "DELETE FROM comments WHERE comment_id = :comment_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('comment_id', $data->comment_id);
$deleted = $stmt->execute();
if ($deleted==1) {
$this->data_obj->send_response(200, json_encode($result));
} else {
$this->data_obj->send_response(409);
}
} else {
$this->data_obj->send_response(409);
}
} else if ($table=="articles") {
// we need an article id to delete an article
// we need a comment id to delete a comment
$sql = "SELECT article_id FROM articles where article_id = :article_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('article_id', $data->article_id);
$stmt->execute();
$num_rows = $stmt->rowCount();
if ($num_rows==1) {
$result=array();
$result['success']="true";
// comment is there at that id
$sql = "DELETE FROM articles WHERE article_id = :article_id";
$stmt = $this->data_obj->dbc->prepare($sql);
$stmt->bindParam('article_id', $data->article_id);
$deleted = $stmt->execute();
if ($deleted==1) {
$this->data_obj->send_response(200, json_encode($result));
} else {
$this->data_obj->send_response(409);
}
} else {
$this->data_obj->send_response(409);
}
}
} catch (\PDOException $e) {
$this->data_obj->send_response(404);
}
}
}
$api = new API;
$api->api_do();
?>