-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
99 lines (86 loc) · 2.76 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
<?php
require_once("./class.MySQL.php");
require_once("./sanitizer.php");
$db = new MySQL();
if ($_GET['q'] == "quotes" && $_GET['n'] == "all") {
$query = "SELECT q.id as id, q.quote as quote, q.date as date FROM qdb q LEFT JOIN qdb_votes qv ON qv.quote_id = q.id";
$rows = $db->setRunBuild($query);
echo json_encode($rows);
}
elseif ($_GET['q'] == "quote" && is_numeric($_GET['id'])) {
$query = "SELECT q.id as id, q.quote as quote, q.date as date FROM qdb q LEFT JOIN qdb_votes qv ON qv.quote_id = q.id WHERE q.id = ".sanitize($_GET['id']);
$rows = $db->setRunBuild($query);
echo json_encode($rows);
}
elseif ($_GET['q'] == "delete" && is_admin($_GET['user'])) {
if (!is_numeric($_GET['id']) || $_GET['code'] != "IAMASECRETCODEAMA") {
$ret['success'] = 'false';
echo json_encode($ret);
header(':', true, 400);
return;
}
else {
# check first
$query = "SELECT * FROM qdb WHERE id = ". sanitize($_GET['id']);
$return = $db->setRunBuild($query);
if (gettype($return) != "array") {
$ret['success'] = 'false';
echo json_encode($ret);
header(':', true, 400);
}
$query = "DELETE FROM qdb WHERE id = " . sanitize($_GET['id']);
$db->setRunBuild($query);
$query = "DELETE FROM qdb_votes WHERE quote_id = ".sanitize($_GET['id']);
$db->setRunBuild($query);
$ret['success'] = 'true';
echo json_encode($ret);
}
}
elseif ($_POST['q'] == "new") {
$f = submit();
echo json_encode($f);
}
elseif ($_POST['q'] == "search") {
if (strlen($_POST['terms']) < 1) {
$ret['success'] = "false";
echo json_encode($ret);
return;
}
$query_substring = " LIKE '%";
foreach (explode(" ", sanitize($_POST['terms'])) as $value) {
$query_substring = $query_substring.$value;
}
$query_substring = $query_substring."%'";
$query = "SELECT quote, date, id FROM qdb WHERE quote ".$query_substring;
echo $query;
$rows = $db->setRunBuild($query);
echo json_encode($rows);
}
else {
# bad request
header(':', true, 400);
}
function submit() {
$dirtyquote = $_POST['quote'];
$db = new MySQL();
$cleaned = mysqli_real_escape_string($db->getDB(), $dirtyquote);
$query = "INSERT INTO qdb (quote) VALUES ('".$cleaned."')";
$return = array();
if ($db->isConnected()) $return['connected'] = "true";
else $return['connected'] = "false";
if ($db->executeSQL($query)) {
$return['status'] = "success";
}
else $return['status'] = "failure";
if ($return['status'] == "failure") $return['reason'] = $db->getError();
$id = $db->getLastInsertID();
$return['id'] = $id;
$return['submitted'] = $cleaned;
return $return;
}
#exteremly hackish but i didn't want to bother with another db table right now
function is_admin($user) {
if ($user == "hlmtre" || $user == "BoneKin")
return True;
return False;
}