-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.php
executable file
·552 lines (464 loc) · 15.5 KB
/
data.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
<?php
class Data {
public $dbc;
private $username;
private $password;
private $quota = "1000";
// constructor - open DB connection
public function __construct() {
try {
$this->dbc = new PDO('mysql:host=localhost;dbname=khadwen', $db_username="khadwen", $db_password="null");
$this->dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$this->send_response(500);
}
}
public function __destruct() {
$this->dbc = null;
}
public function isEmpty($array) {
@$array = array();
foreach ($array as $element) {
if ($element=="") {
// element is empty
return false;
}
}
// if no elements are empty
return true;
}
public function get_user_id_from_username($username) {
try {
$sql = "SELECT api_user_id, api_username, api_password FROM api_users WHERE api_username = :username";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('username', $username);
$stmt->execute();
$results = $stmt->fetch();
$user_id = $results['api_user_id'];
return $user_id;
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_user_id_from_users_table($username) {
try {
$sql = "SELECT user_id, username FROM users WHERE username = :username";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('username', $username);
$stmt->execute();
$results = $stmt->fetch();
$user_id = $results['user_id'];
return $user_id;
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_latest_comment_id() {
try {
$sql = "SELECT comment_id FROM comments ORDER BY comment_id DESC LIMIT 1";
$stmt = $this->dbc->prepare($sql);
$stmt->execute();
$results = $stmt->fetch();
return $results[0]+1;
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_article_id_latest() {
try {
$sql = "SELECT article_id FROM articles ORDER BY article_id DESC LIMIT 1";
$stmt = $this->dbc->prepare($sql);
$stmt->execute();
$results = $stmt->fetch();
return $results[0]+1;
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_user_id_latest() {
try {
$sql = "SELECT user_id FROM users ORDER BY user_id DESC LIMIT 1";
$stmt = $this->dbc->prepare($sql);
$stmt->execute();
$results = $stmt->fetch();
return $results[0]+1;
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function update_quota($username) {
// check if the date (today) is in the database for the user, if it's not, add it
try {
$user_id = $this->get_user_id_from_username($username);
// check if date is in there for today
$sql = "SELECT quota_id, user_id, date, quota FROM quota WHERE user_id = :user_id";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('user_id', $user_id);
$stmt->execute(array($user_id));
$results = $stmt->fetch();
$num_rows = $stmt->rowCount();
if ($num_rows==0) {
// no date, let's add it
$sql = "INSERT INTO quota (date, user_id) VALUES (CURDATE(), :user_id)";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('user_id', $user_id);
$stmt->execute();
}
$sql = "UPDATE quota SET user_id = :user_id, quota = quota + 1 WHERE user_id = :user_id";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('user_id', $user_id);
$stmt->execute();
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function check_quota($user_id) {
try {
$sql = "SELECT quota_id, user_id, date, quota FROM quota
WHERE date = CURDATE() AND user_id = :user_id";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('user_id', $user_id);
$stmt->execute();
$results = $stmt->fetch();
$num_rows = $stmt->rowCount();
if ($num_rows==1) {
// user/data exists
// date exists, next let's check that the quota isn't over 1000
if ($results['quota']>1000) {
$this->send_response(429);
die();
}
// if it doesn't exist, it should be fine as the user hasn't used the api today
}
return true;
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_users_articles($id, $table) {
// split the table because it will come in as 'users, articles'
try {
$sql = "SELECT users.user_id, users.username, users.first_name, users.last_name, users.email_address,
article_id, article_title, article_sub_title, article_content, article_views,
article_unique_views, article_author_id, article_post_date
FROM articles INNER JOIN users ON users.user_id = articles.article_author_id
WHERE users.user_id = :id
ORDER BY article_id DESC
LIMIT 100";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('id', $id);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!empty($results)) {
// HATEOAS
for ($counter=0; $counter<count($results); $counter++) {
$results[$counter]['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/users/".$results[$counter]['user_id'];
}
$this->send_response(200, json_encode($results));
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_article_comments($id, $table) {
// split the table because it will come in as 'users, articles'
try {
$sql = "SELECT article_id, comment_id, comment_article_id, comment_full_name, comment_full, comment_location,
comment_likes
FROM comments INNER JOIN articles ON comment_article_id = article_id
WHERE article_id = :id ORDER BY article_id DESC LIMIT 100";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('id', $id);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!empty($results)) {
// HATEOAS
for ($counter=0; $counter<count($results); $counter++) {
$results[$counter]['article_id']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/article_id/".$results[$counter]['article_id'];
$results[$counter]['comment_id']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/article_id/".$results[$counter]['comment_id'];
}
$this->send_response(200, json_encode($results));
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_pagination_hmtr_by_user($id, $table, $value) {
// split the table because it will come in as 'users, articles, pagination_hmtr'
try {
$sql = "SELECT users.user_id, users.username, users.first_name, users.last_name, users.email_address,
article_id, article_title, article_sub_title, article_content, article_views,
article_unique_views, article_author_id, article_post_date
FROM articles INNER JOIN users ON users.user_id = articles.article_author_id
WHERE users.user_id = :id
LIMIT 0, :value";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('id', $id);
$stmt->bindParam('value', $value);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num_rows = $stmt->rowCount();
// value is out of range, therefore we return a 400 because it doesn't exist in the db
if ($value > $num_rows) {
$this->send_response(400);
} else if (!empty($results)) {
// HATEOAS
for ($counter=0; $counter<count($results); $counter++) {
$results[$counter]['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/users/".$results[$counter]['user_id'];
}
$this->send_response(200, json_encode($results));
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_users_limit($table, $value) {
try {
$get_range = explode(",", $value);
$limit_start = $get_range[0];
$limit_end = $get_range[1];
$sql = "SELECT * FROM users ORDER BY user_id DESC LIMIT :limit_start, :limit_end";
$stmt = $this->dbc->prepare($sql);
$stmt->bindValue(':limit_start', (int) trim($get_range[0]), PDO::PARAM_INT);
$stmt->bindValue(':limit_end', (int) trim($get_range[1]), PDO::PARAM_INT);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num_rows = $stmt->rowCount();
if (!empty($results)) {
if ($limit_end > $num_rows) {
$this->send_response(200);
} else {
// HATEOAS
for ($counter=0; $counter<count($results); $counter++) {
$results[$counter]['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/users/".$results[$counter]['user_id'];
}
$this->send_response(200, json_encode($results));
}
} else {
$this->send_response(200);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_articles_limit($table, $value) {
$get_range = explode(",", $value);
array_shift($get_range);
try {
$sql = "SELECT * FROM articles ORDER BY article_id DESC LIMIT :limit_start, :limit_end";
$stmt = $this->dbc->prepare($sql);
@$limit_start = $get_range[0];
@$limit_end = $get_range[1];
$stmt->bindValue(':limit_start', (int) trim($get_range[0]), PDO::PARAM_INT);
$stmt->bindValue(':limit_end', (int) trim($get_range[1]), PDO::PARAM_INT);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num_rows = $stmt->rowCount();
if (!empty($results)) {
if ($limit_end > $num_rows) {
$this->send_response(400);
} else {
// HATEOAS
for ($counter=0; $counter<count($results); $counter++) {
$results[$counter]['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/articles/".$results[$counter]['article_id'];
}
$this->send_response(200, json_encode($results));
}
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_comments_limit($table, $value) {
$get_range = explode(",", $value);
try {
@$limit_start = $get_range[0];
@$limit_end = $get_range[1];
$sql = "SELECT * FROM comments ORDER BY comment_id DESC LIMIT :limit_start, :limit_end";
$stmt = $this->dbc->prepare($sql);
$stmt->bindValue(':limit_start', (int) trim($get_range[0]), PDO::PARAM_INT);
$stmt->bindValue(':limit_end', (int) trim($get_range[1]), PDO::PARAM_INT);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num_rows = $stmt->rowCount();
if (!empty($results)) {
if ($limit_end > $num_rows) {
$this->send_response(400);
} else {
// HATEOAS
for ($counter=0; $counter<count($results); $counter++) {
$results[$counter]['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/comments/".$results[$counter]['comment_id'];
}
$this->send_response(200, json_encode($results));
}
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_all_users() {
try {
$sql = "SELECT * FROM users ORDER BY user_id DESC LIMIT 100";
$stmt = $this->dbc->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!empty($results)) {
// HATEOAS
for ($counter=0; $counter<count($results); $counter++) {
$results[$counter]['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/users/".$results[$counter]['user_id'];
}
$this->send_response(200, json_encode($results));
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function check_user_exists($username, $password) {
try {
$sql = "SELECT api_username, api_password FROM api_users WHERE api_username = :username AND api_password = :password";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam('username', $username);
$stmt->bindParam('password', $password);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num_rows = $stmt->rowCount();
if ($num_rows==1) {
return true;
} else {
return false;
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_all_comments() {
try {
$sql = "SELECT * FROM comments ORDER BY comment_id DESC LIMIT 100";
$stmt = $this->dbc->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!empty($results)) {
// HATEOAS
for ($counter=0; $counter<count($results); $counter++) {
$results[$counter]['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/comments/".$results[$counter]['comment_id'];
}
$this->send_response(200, json_encode($results));
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_all_articles() {
try {
$sql = "SELECT * FROM articles ORDER BY article_id DESC LIMIT 100";
$stmt = $this->dbc->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!empty($results)) {
// HATEOAS
for ($counter=0; $counter<count($results); $counter++) {
$results[$counter]['href']="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/articles/".$results[$counter]['article_id'];
}
$this->send_response(200, json_encode($results));
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_user($id) {
try {
$sql = "SELECT * FROM users where user_id = :id";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$result = $stmt->fetch(\PDO::FETCH_OBJ);
if (!empty($result)) {
// HATEOAS
$result->href="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/users/".$id;
$this->send_response(200, json_encode($result));
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_comment($id) {
try {
$sql = "SELECT * FROM comments WHERE comment_id = :id";
$stmt = $this->dbc->prepare($sql);
$stmt->bindValue(':id', (int) trim($id), PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(\PDO::FETCH_OBJ);
if (!empty($result)) {
// HATEOAS
$result->href="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/users/".$id;
$this->send_response(200, json_encode($result));
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function get_article($id) {
try {
$sql = "SELECT * FROM articles where article_id = :id";
$stmt = $this->dbc->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$result = $stmt->fetch(\PDO::FETCH_OBJ);
if (!empty($result)) {
// HATEOAS
$result->href="http://fostvm.fost.plymouth.ac.uk/modules/soft338/khadwen/v1/articles/".$result->article_id;
$this->send_response(200, json_encode($result));
} else {
$this->send_response(400);
}
} catch (\PDOException $e) {
$this->send_response(400);
}
}
public function paths($url) {
$uri = parse_url($url);
return $uri['path'];
}
public static function get_status_code_message($status) {
$codes = Array (
200 => 'OK',
201 => 'Created',
400 => 'Bad Request',
401 => 'Unauthorized',
404 => 'Not Found',
405 => 'Method Not Allowed',
409 => 'Conflict',
429 => 'Too Many Requests',
500 => 'Internal Server Error'
);
return (isset($codes[$status])) ? $codes[$status] : '';
}
// helper method to send a HTTP response code/message
public function send_response($status = 200, $body = '', $content_type = 'text/html') {
$status_header = 'HTTP/1.1 ' . $status . ' ' . $this->get_status_code_message($status);
header($status_header);
header('Content-type: ' . $content_type);
echo $body;
}
}
?>