-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
276 lines (179 loc) · 7.11 KB
/
ajax.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
<?php
require_once('config.php');
error_log('called');
error_log($isAdmin?'admin':'not admin');
function getTags() {
global $con,$isAdmin;
$lista = [];
$sql="SELECT name, tags.id FROM tags,quote_tags,quotes WHERE tags.id=quote_tags.tid AND quotes.id=quote_tags.qid".($isAdmin?"":" AND quotes.verified=1")." ORDER BY name";
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
$lastname = '';
while($row = mysqli_fetch_assoc($query)) {
if($row['name'] == $lastname && $lastname != '') {
$lista[count($lista)-1]['count']++;
}
else {
$row['count'] = 1;
$lista[] = $row;
$lastname = $row['name'];
}
}
return $lista;
}
function getQuoteNumber() {
global $con, $isAdmin;
$sql="SELECT id FROM quotes WHERE ".($isAdmin ? '' : "quotes.verified=")."1";
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
return mysqli_num_rows($query);
}
function getQuotes() {
global $con, $isAdmin;
$verified = $isAdmin ? '' : " AND quotes.verified=1";
$tags = '';
// get tag filters
if(isset($_POST['tag_filter']) && preg_match('/^[-A-Za-z0-9_ ,]+$/',$_POST['tag_filter']) === 1) {
$tagsta = explode(',',$_POST['tag_filter']);
$tagsa = [];
foreach($tagsta as $tag) {
$tag = mysqli_real_escape_string($con,$tag);
$tagsa[] = $tag;
}
$tags = " AND tags.name IN ('".implode('\',\'',$tagsa)."')";
}
$query = '';
// get search term
if(isset($_POST['query']) && strlen($_POST['query']) > 2 && strlen($_POST['query']) < 255) {
if(substr($_POST['query'],0,1) == '"' && substr($_POST['query'],-1,1) == '"')
$query = " AND quotes.quote LIKE '%".substr($_POST['query'],1,-1)."%'";
else {
$terms = explode(" ",$_POST['query']);
foreach($terms as $term) {
$query .= " AND quotes.quote LIKE '%".$term."%'";
}
}
}
$order = $_POST['form_id'] == 'randquote' ? " ORDER BY RAND() LIMIT 1" :" ORDER BY quotes.quote";
$sql="SELECT quotes.id AS id, quotes.quote AS quote, quotes.source AS source, quotes.sourcename AS sourcename, quotes.verified AS verified FROM quotes, quote_tags, tags WHERE quotes.id=quote_tags.qid AND quote_tags.tid=tags.id".$verified.$query.$tags.$order;
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
error_log(mysqli_num_rows($query));
$lista = [];
// add tags
$qids = [];
while($row = mysqli_fetch_assoc($query)) {
if($qids[$row['id']])
continue;
$qids[$row['id']] = true;
// tag fudge
$sql="SELECT tags.name AS tag FROM quotes, quote_tags, tags WHERE quotes.id=quote_tags.qid AND quote_tags.tid=tags.id AND quotes.id=".$row['id'];
$querya = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
$row['tag'] = '';
while($rowa = mysqli_fetch_assoc($querya)) {
$row['tag'] .= (strlen($row['tag']) >0?',':''). $rowa['tag'];
}
error_log($row['tag']);
$lista[] = $row;
}
return $lista;
}
function insertTags($qid,$tags) {
global $con;
// insert tags
$tagsta = explode(',',$tags);
$tagsa = [];
foreach($tagsta as $tag) {
$tag = mysqli_real_escape_string($con,$tag);
$tagsa[] = $tag;
}
foreach($tagsa as $tag) {
// insert new tag
$sql="INSERT IGNORE INTO tags (name) VALUES('".$tag."')";
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
// get (new) tag id
$sql="SELECT id FROM tags WHERE name='".$tag."'";
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
$tid = mysqli_fetch_assoc($query)['id'];
// insert tags
$sql="INSERT INTO quote_tags (qid,tid) VALUES('".$qid."','".$tid."')";
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
}
}
$data = [];
// submissions
if(isset($_POST['form_id']) && $_POST['form_id'] != "") {
if($_POST['form_id'] == 'randquote') {
error_log('randquote called');
$data['quotes'] = getQuotes();
}
else if($_POST['form_id'] == 'addquote') {
error_log('addquote called');
require_once "recaptchalib.php";
// your secret key
$secret = "6LfW-g4TAAAAAKc1mjqZjw6oBzIOwrjjn9iCOUCZ";
// check secret key
$reCaptcha = new ReCaptcha($secret);
// empty response
$cresponse = null;
if ($_POST["g-recaptcha-response"]) {
$cresponse = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
if(($isAdmin || ($cresponse != null && $cresponse->success)) && !filter_var($_POST['source'], FILTER_VALIDATE_URL) === false && strlen($_POST['quote']) > 0 && strlen($_POST['quote']) < 2000 && strlen($_POST['tags']) > 0 && preg_match('/^[-a-zA-Z0-9_ ,]+$/',$_POST['tags']) === 1) {
error_log('addquote validated');
$quote = mysqli_real_escape_string($con,$_POST['quote']);
$source = mysqli_real_escape_string($con,$_POST['source']);
$sourcename = mysqli_real_escape_string($con,$_POST['sourcename']);
// insert quote
$sql="INSERT INTO quotes (quote,source,sourcename) VALUES('".$quote."','".$source."','".$sourcename."')";
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
// get quote id
$qid = mysqli_insert_id($con);
insertTags($qid,$_POST['tags']);
$data = array(
'success' => 1,
);
die(json_encode($data));
}
else {
$data = array(
'success' => -1,
);
die(json_encode($data));
}
}
else if($isAdmin && strpos($_POST['form_id'],'quote-admin-') === 0) {
error_log($_POST['form_id'].' called');
$quote = mysqli_real_escape_string($con,$_POST['quote']);
$source = mysqli_real_escape_string($con,$_POST['source']);
$sourcename = mysqli_real_escape_string($con,$_POST['sourcename']);
$qid = (int)mysqli_real_escape_string($con,$_POST['qid']);
error_log($qid.' quote');
$sql="UPDATE quotes SET quote='".$quote."', source='".$source."', sourcename='".$sourcename."', verified=".(isset($_POST['verified'])?1:0)." WHERE id=".$qid;
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
// remove existing tags
$sql="DELETE FROM quote_tags WHERE qid=".$qid;
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
insertTags($qid,$_POST['tags']);
}
else if($isAdmin && strpos($_POST['form_id'],'delete-') === 0) {
error_log($_POST['form_id'].' called');
$qid = (int)mysqli_real_escape_string($con,substr($_POST['form_id'],7));
error_log($qid.' quote');
$sql="DELETE FROM quotes WHERE id=".$qid;
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
// remove tag relationships
$sql="DELETE FROM quote_tags WHERE qid=".$qid;
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
// remove orphan tags
$sql="DELETE FROM tags WHERE id NOT IN (SELECT tid FROM quote_tags)";
$query = mysqli_query($con, $sql) or trigger_error("Query Failed: " . mysqli_error($con));
}
else if($_POST['form_id'] == 'getquotes') {
$data['quotes'] = getQuotes();
}
$data['tags'] = getTags();
$data['total'] = getQuoteNumber();
}
$json = json_encode($data);
echo $json;