-
Notifications
You must be signed in to change notification settings - Fork 0
/
candidate.php
377 lines (353 loc) · 13.9 KB
/
candidate.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
<?php
ob_start();
session_start();
require_once("util/user_input.php");
require_once("util/db.php");
require_once("util/ALD.php");
require_once("config/constants.php");
require_once("util/privilege.php");
require_once('modules/semver/semver.php');
require_once('util/get_privilege_symbols.php');
require_once('partials/Notice.php');
$db_connection = db_ensure_connection();
for ($i = 0; $i < 1; $i++)
{
$api = new ALD( API_URL );
$error = true; # assume error here, reset on success
$page_title = 'ERROR';
if (isset($_GET["id"]))
{
$id = mysql_real_escape_string($_GET["id"], $db_connection);
$logged_in = isset($_SESSION["userID"]);
$can_close = $logged_in && hasPrivilege($_SESSION["privileges"], PRIVILEGE_STDLIB);
$diff = false;
if (!empty($_POST) && $logged_in)
{
if (isset($_POST["newcomment"]))
{
if (isset($_POST["vote"]))
{
$vote = (int)(mysql_real_escape_string($_POST["vote"]));
if (in_array($vote, array(-1, 0, 1)))
{
$db_query = "SELECT COUNT(*) FROM $db_table_candidate_comments WHERE id = '$id' AND vote != '0' AND user = UNHEX('{$_SESSION["userID"]}')";
$db_result = mysql_query($db_query, $db_connection);
if (!$db_result)
{
$error_message = "Failed to get previous votes: MySQL error";
$error_description = "Could not check if current user has already voted. MySQL error was: '" . mysql_error() . "'";
break;
}
$can_vote = mysql_fetch_object($db_result)->{'COUNT(*)'} == 0; # set to false if there's already a comment by the current user with a vote
if (!$can_vote)
$vote = 0;
}
else
$vote = 0;
}
$db_query = "INSERT INTO $db_table_candidate_comments (id, user, comment, vote) VALUES ($id, UNHEX('{$_SESSION["userID"]}'), '" . mysql_real_escape_string($_POST["newcomment"]) . "', '" . $vote . "')";
$db_result = mysql_query($db_query, $db_connection);
if (!$db_result)
{
$error_message = "Failed to save comment: MySQL error";
$error_description = "Could not save your last comment on this thread. MySQL error was: '" . mysql_error() . "'";
break;
}
}
else if (isset($_POST["accept"]) || isset($_POST["reject"]))
{
if ($can_close)
{
$db_query = "UPDATE $db_table_candidates Set closed = '1', closed-by = UNHEX('{$_SESSION["userID"]}'), closed-date = NOW(), closed-comment = '" . mysql_real_escape_string($_POST["closecomment"]) . "' WHERE id = '$id'";
$db_result = mysql_query($db_query, $db_connection);
if (!$db_result)
{
$error_message = "Failed to close this thread: MySQL error";
$error_description = "Could not close the thread. MySQL error was: '" . mysql_error() . "'";
break;
}
$db_query = "UPDATE $db_table_main Set default_include = '1' WHERE id = UNHEX('')"; # todo
# TODO
}
}
header("Location: " . $_SERVER["REQUEST_URI"]); # reload to clear POST data and avoid repost of comment
}
$db_query = "SELECT *, HEX(libid), HEX(userid), HEX(`closed-by`) FROM $db_table_candidates WHERE id = '$id'";
$db_result = mysql_query($db_query, $db_connection);
if (!$db_result)
{
$error_message = "Failed to retrieve thread: MySQL error";
$error_description = "Could not retrieve data on this thread. MySQL error was: '" . mysql_error() . "'";
break;
}
if (mysql_num_rows($db_result) != 1)
{
$error_message = "Failed to retrieve thread: not found";
$error_description = "Could not find this thread. Most likely, the URL is incorrect.";
break;
}
$candidate = mysql_fetch_assoc($db_result);
$lib = $api->getItemById($candidate["HEX(libid)"]);
$candidate["libname"] = $lib["name"];
$candidate["libversion"] = $lib["version"];
$temp = $api->getUserById($candidate["HEX(userid)"]);
$candidate["username"] = $temp["name"];
if ($candidate["closed"])
{
$temp = $api->getUserById($candidate["HEX(`closed-by`)"]);
$candidate["closed-by"] = $temp["name"];
}
else
{
# get previous version in stdlib if existing
$list = $api->getItemList(0, 'all', NULL, NULL, $candidate['libname'], NULL, NULL, 'yes');
if (count($list) > 0)
{
$diff = true;
usort($list, "semver_sort"); # sort by version numbers (descending)
$diff_base = $list[0]['version'];
}
}
$comments = array();
$db_query = "SELECT *, HEX(user) FROM $db_table_candidate_comments WHERE id = '$id'";
$db_result = mysql_query($db_query, $db_connection);
if (!$db_result)
{
$error_message = "Failed to retrieve comments: MySQL error";
$error_description = "Could not read the comments on this thread. MySQL error was: '" . mysql_error() . "'";
break;
}
while ($comment = mysql_fetch_assoc($db_result))
{
$temp = $api->getUserById($comment["HEX(user)"]);
$comment["user"] = $temp["name"];
$comment["user-mail"] = $temp["mail-md5"];
$comment['user-privilege'] = $temp['privileges'];
$comments[] = $comment;
}
$db_query = "SELECT COUNT(*) FROM $db_table_candidate_comments WHERE id = '$id' AND vote > '0'"; # get upvote count
$db_result = mysql_query($db_query, $db_connection);
if (!$db_result)
{
$error_message = "Failed to retrieve upvote count: MySQL error";
$error_description = "The number of upvotes could not be read. MySQL error was: '" . mysql_error() . "'";
break;
}
$up_vote_count = mysql_fetch_object($db_result)->{'COUNT(*)'};
$db_query = "SELECT COUNT(*) FROM $db_table_candidate_comments WHERE id = '$id' AND vote < '0'"; # get downvote count
$db_result = mysql_query($db_query, $db_connection);
if (!$db_result)
{
$error_message = "Failed to retrieve downvote count: MySQL error";
$error_description = "The number of downvotes could not be read. MySQL error was: '" . mysql_error() . "'";
break;
}
$down_vote_count = mysql_fetch_object($db_result)->{'COUNT(*)'};
$total_vote_count = $up_vote_count - $down_vote_count;
if ($logged_in)
{
$db_query = "SELECT COUNT(*) FROM $db_table_candidate_comments WHERE id = '$id' AND vote != '0' AND user = UNHEX('{$_SESSION["userID"]}')";
$db_result = mysql_query($db_query, $db_connection);
if (!$db_result)
{
$error_message = "Failed to get previous votes: MySQL error";
$error_description = "Could not check if current user has already voted. MySQL error was: '" . mysql_error() . "'";
break;
}
$can_vote = mysql_fetch_object($db_result)->{'COUNT(*)'} == 0; # set to false if there's already a comment by the current user with a vote
}
$page_title = ($candidate["closed"] ? "closed: " : "") . $candidate["libname"] . " v" . $candidate["libversion"] . " | Candidate for stdlib";
}
else
{
$page_title = "Candidates for the standard library";
$db_cond = "closed != '1'";
if (isset($_GET["mode"]))
{
if (strtolower($_GET["mode"]) == "closed")
{
$db_cond = "closed = '1'";
$page_title .= " (closed)";
}
else if (strtolower($_GET["mode"]) == "all")
{
$db_cond = "'1' = '1'";
}
}
$db_query = "SELECT id, HEX(libid), HEX(userid), date, closed FROM $db_table_candidates WHERE $db_cond";
$db_result = mysql_query($db_query, $db_connection);
if (!$db_result)
{
$error_message = "Failed to retrieve list of candidates: MySQL error";
$error_description = "The list of candidates could not be read. MySQL error was: '" . mysql_error() . "'";
break;
}
$candidates = array();
while ($candidate = mysql_fetch_assoc($db_result))
{
$lib = $api->getItemById($candidate["HEX(libid)"]);
$candidate["lib-name"] = $lib["name"];
$candidate["lib-version"] = $lib["version"];
$temp = $api->getUserById($candidate["HEX(userid)"]);
$candidate["user"] = $temp["name"];
$candidates[] = $candidate;
}
}
$error = false;
}
?>
<!DOCTYPE html>
<html>
<head>
<?php require("partials/html.head.php"); ?>
<?php if (isset($id)) { ?>
<link rel="stylesheet" type="text/css" href="style/candidates/view.css"/>
<?php } else { ?>
<link rel="stylesheet" type="text/css" href="style/candidates/list.css"/>
<?php } ?>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="javascript/default.js"></script>
</head>
<body>
<h1 id="page-title"><?php echo $page_title; ?></h1>
<div id="page-content">
<?php
if ($error)
{
error($error_message, $error_description, true);
}
else if (isset($id))
{
?>
<table id="candidate">
<tbody>
<tr>
<th>Library:</th>
<td><a href="items/<?php echo $candidate["HEX(libid)"]; ?>"><?php echo $candidate["libname"]; ?> (v<?php echo $candidate["libversion"]; ?>)</a></td>
</tr>
<tr>
<th>User:</th>
<td><a href="users/<?php echo $candidate["username"]; ?>/profile"><?php echo $candidate["username"]; ?></a></td>
</tr>
<tr>
<th>Applied:</th>
<td><?php echo $candidate["date"]; ?></td>
</tr>
<?php if ($diff) { ?>
<tr>
<th>Diff:</th>
<td><a class='compare' href='items/compare/<?php echo $candidate['libname'], '/', $diff_base, '...', $candidate['libversion']; ?>'>compare with latest version in the stdlib (<?php echo $diff_base; ?>)</a></td>
</tr>
<?php } ?>
<tr>
<td colspan="2" class="topic-details"><div class='markdown'><?php echo user_input_process($candidate["text"]); ?></div></td>
</tr>
</tbody>
</table>
<div id="votes"><div class="vote upvote">+<?php echo $up_vote_count; ?></div><div class="vote downvote">-<?php echo $down_vote_count; ?></div><div class="vote"><?php echo ($total_vote_count > 0 ? "+" : "-") . $total_vote_count; ?> votes</div></div>
<h2>Comments</h2>
<table id="candidate-comments">
<tbody>
<?php
foreach ($comments AS $comment)
{
$symbols = get_privilege_symbols($comment['user-privilege']);
echo "<tr><td><img alt=\"avatar\" src=\"http://gravatar.com/avatar/{$comment['user-mail']}?s=50&d=mm\" class=\"comment-avatar\"/><br/><a href=\"users/{$comment["user"]}/profile\">{$comment["user"]}</a>$symbols<hr/>{$comment["date"]}</td>"
. "<td><div class='markdown'>" . user_input_process($comment["comment"]) . '</div>' . (!empty($comment["vote"]) ? "<div class=\"vote\" style=\"float: right\">+1</div>" : "") . "</td></tr>";
}
if (!$candidate["closed"] && $logged_in)
{
?>
<tr>
<td><a href="users/<?php echo $_SESSION["user"]; ?>/profile">You</a><hr/>Now</td>
<td>
<form action="#" method="post">
<textarea class="preview-source" name="newcomment" style="width: 99.5%" placeholder="Enter your comment..."></textarea>
<?php
if ($can_vote)
{
?>
<div class="vote-option"><input type="radio" name="vote" value="-1"> ⇓ Vote down ⇓ </input></div>
<div class="vote-option"><input type="radio" name="vote" value="0" checked="checked">⇐ neutral ⇒ </input></div>
<div class="vote-option"><input type="radio" name="vote" value="1"> ⇑ Vote up ⇑ </input></div>
<?php
}
?>
<input type="submit" value="Submit" style="float: right"/>
</form>
</td>
</tr>
<?php
if ($can_close)
{
?>
<tr>
<td><a href="users/teams/stdlib">Stdlib team</a></td>
<td>
<form action="#" method="post">
<textarea class="preview-source" name="closecomment" style="width: 99.5%" placeholder="Enter a comment..."></textarea>
<input style="width: 49%; display: inline-block" type="submit" value="accept" name="accept"/>
<input style="width: 49%; display: inline-block" type="submit" value="reject" name="reject"/>
</form>
</td>
</tr>
<?php
}
}
else if ($candidate["closed"])
{
echo "<tr><td><a href=\"users/{$candidate["closed-by"]}/profile\">{$candidate["closed-by"]}</a><hr/>{$candidate["closed-date"]}</td>"
. "<td id=\"close-comment\" class=\"" . ( /* todo: get if included in stdlib or not */ "") . "\"><div class='markdown'>" . user_input_process($candidate["closed-comment"]) . "</div></td></tr>";
/*
if ($can_close && !$in_standard)
{
reopen for discussion
}
*/
}
?>
</tbody>
</table>
<a href="http://htmlpurifier.org/"><img src="http://htmlpurifier.org/live/art/powered.png" alt="Powered by HTML Purifier" border="0" /></a>
<?php
}
else
{
?>
<div id="candidate-list" class="js-ui-accordion">
<?php
foreach ($candidates AS $cand)
{
$status = $cand['closed'] ? 'closed' : 'open';
echo '<div class="candidate-entry">'
. "<h3 class='candidate-header js-ui-accordion-header'>{$cand['lib-name']} (v{$cand['lib-version']})</h3>"
. '<dl class="candidate-details">'
. "<dt>Name</dt><dd><a href='items/{$cand['HEX(libid)']}'>{$cand['lib-name']}</a></dd>"
. "<dt>Version</dt><dd>{$cand['lib-version']}</dd>"
. "<dt>User</dt><dd><a href='users/$cand[user]/profile'>$cand[user]</a></dd>"
. "<dt>Date</dt><dd>$cand[date]</dd>"
. "<dt>Status</dt><dd class='cand-$status'>$status</dd>"
. "<dt>Link</dt><dd>▶ <a href='./$cand[id]'>Go to discussion thread</a> ▶</dd>"
. '</dl></div>';
}
?>
</div>
<?php
}
?>
</div>
<?php require("partials/footer.php"); require("partials/header.php"); ?>
</body>
</html>
<?php
require_once("util/rewriter.php");
echo rewrite();
ob_end_flush();
?>
<?php
function semver_sort($a, $b)
{
return semver_compare($b['version'], $a['version']);
}
?>