-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnews-editpost.php
344 lines (290 loc) · 10.8 KB
/
news-editpost.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
<?php
/*
News editor
Edits the contents in the news table
*/
require "lib/function.php";
require "lib/news_function.php";
if (!$canwrite)
news_errorpage("You aren't allowed to edit posts.<br>Click <a href='news.php'>here</a> to return to the main page.");
$_GET['id'] = filter_int($_GET['id']);
$smilies = readsmilies();
if (isset($_GET['edit'])){
if (!$_GET['id'])
news_errorpage("No post ID specified.");
$news = $sql->fetchq("
SELECT n.*, $userfields uid
FROM news n
LEFT JOIN users u ON n.user = u.id
WHERE n.id = {$_GET['id']}
");
if (!$news)
news_errorpage("The post doesn't exist!");
if (!$ismod && $loguser['id'] != $news['user'])
news_errorpage("You have no permission to do this!");
$_POST['title'] = isset($_POST['nname']) ? $_POST['title'] : $news['title'];
$_POST['text'] = isset($_POST['text']) ? $_POST['text'] : $news['text'];
$_POST['nosmilies'] = isset($_POST['nosmilies']) ? $_POST['nosmilies'] : $news['nosmilies'];
$_POST['nohtml'] = isset($_POST['nohtml']) ? $_POST['nohtml'] : $news['nohtml'];
if (!isset($_POST['tags']))
$_POST['tags'] = $sql->getresults("SELECT tag FROM news_tags_assoc WHERE post = {$_GET['id']}");
else
$_POST['tags'] = array_map('intval', filter_array($_POST['tags']));
$_POST['customtags'] = filter_string($_POST['customtags']);
if (isset($_POST['submit'])){
check_token($_POST['auth']);
if (!$_POST['title'] || !$_POST['text'])
news_errorpage("You have left one of the required fields blank!");
$valid = (int)$sql->resultq("SELECT COUNT(*) FROM news_tags WHERE id IN (".implode(',', $_POST['tags']).")");
if ($valid != count($_POST['tags']))
news_errorpage("At least one invalid tag was selected.");
$sql->beginTransaction();
// Get a filtered list of new tags to enter
$in = $sql->prepare("INSERT IGNORE INTO news_tags (title) VALUES (?)");
$taglist = explode(",", $_POST['customtags']);
foreach ($taglist as $tag) {
if ($tag = trim($tag)) {
$sql->execute($in, [$tag]);
if ($lastid = $sql->insert_id()) // If the tag already exists, $lastid will be 0
$_POST['tags'][] = $lastid;
}
}
// Create the post
$values = array(
'title' => $_POST['title'],
'text' => $_POST['text'],
'lastedituser' => $loguser['id'],
'lasteditdate' => ctime(),
);
$sql->queryp("UPDATE news SET ".mysql::setplaceholders($values)." WHERE id = {$_GET['id']}", $values);
// Assoc things
$inassoc = $sql->prepare("INSERT IGNORE INTO news_tags_assoc (post, tag) VALUES ({$_GET['id']},?)");
foreach ($_POST['tags'] as $key => $tagid) {
$sql->execute($inassoc, [$tagid]);
}
// Remove de-selected
$sql->query("DELETE FROM news_tags_assoc WHERE post = {$_GET['id']} AND tag NOT IN (".implode(',', $_POST['tags']).")");
$sql->commit();
return header("Location: news.php?id={$_GET['id']}");
}
$nosmilies_chk = $_POST['nosmilies'] ? " checked" : "";
$nohtml_chk = $_POST['nohtml'] ? " checked" : "";
$windowtitle = "Edit post";
news_header($windowtitle);
$links = array(
[$config['news-name'] , "news.php"],
[$windowtitle , NULL],
);
$barlinks = dobreadcrumbs($links);
print $barlinks;
if (isset($_POST['preview'])) {
$preview = array(
'id' => $_GET['id'],
'user' => $news['user'],
'date' => $news['date'],
'userdata' => $news,
'lastedituser' => $loguser['id'],
'lasteditdate' => ctime(),
'edituserdata' => $loguser,
'deleted' => 0,
'comments' => $sql->resultq("SELECT COUNT(*) FROM news_comments WHERE pid = {$_GET['id']}"),
'tags' => array(), // TODO: Make tags work in the preview
'text' => $_POST['text'],
'title' => $_POST['title'],
'nosmilies' => $_POST['nosmilies'],
'nohtml' => $_POST['nohtml'],
);
?>
<table class='table'>
<tr><td class='tdbgh center b'>Message preview</td></tr>
<tr><td class='tdbg1'><?= news_format($preview) ?></td></tr>
</table>
<br>
<?php
}
}
else if (isset($_GET['new'])){
// ACTION : New news
$_POST['title'] = filter_string($_POST['title']);
$_POST['text'] = filter_string($_POST['text']);
$_POST['tags'] = array_map('intval', filter_array($_POST['tags']));
$_POST['customtags'] = filter_string($_POST['customtags']);
$_POST['nosmilies'] = filter_int($_POST['nosmilies']);
$_POST['nohtml'] = filter_int($_POST['nohtml']);
if (isset($_POST['submit'])){
check_token($_POST['auth']);
if (!$_POST['title'] || !$_POST['text'])
news_errorpage("You have left one of the required fields blank!");
$valid = (int)$sql->resultq("SELECT COUNT(*) FROM news_tags WHERE id IN (".implode(',', $_POST['tags']).")");
if ($valid != count($_POST['tags']))
news_errorpage("At least one invalid tag was selected.");
$sql->beginTransaction();
// Get a filtered list of new tags to enter
$in = $sql->prepare("INSERT IGNORE INTO news_tags (title) VALUES (?)");
$taglist = explode(",", $_POST['customtags']);
foreach ($taglist as $tag) {
if ($tag = trim($tag)) {
$sql->execute($in, [$tag]);
if ($lastid = $sql->insert_id()) // If the tag already exists, $lastid will be 0
$_POST['tags'][] = $lastid;
}
}
// Create the post
$values = array(
'title' => $_POST['title'],
'text' => $_POST['text'],
'user' => $loguser['id'],
'date' => ctime(),
);
$sql->queryp("INSERT INTO news SET ".mysql::setplaceholders($values), $values);
$id = $sql->insert_id();
// Assoc things
$inassoc = $sql->prepare("INSERT INTO news_tags_assoc (post, tag) VALUES ({$id},?)");
foreach ($_POST['tags'] as $tagid) {
$sql->execute($inassoc, [$tagid]);
}
$sql->commit();
return header("Location: news.php?id=$id");
}
$nosmilies_chk = $_POST['nosmilies'] ? " checked" : "";
$nohtml_chk = $_POST['nohtml'] ? " checked" : "";
$windowtitle = "New post";
news_header($windowtitle);
$links = array(
[$config['news-name'] , "news.php"],
[$windowtitle , NULL],
);
$barlinks = dobreadcrumbs($links);
print $barlinks;
if (isset($_POST['preview'])) {
$preview = array(
'id' => 0,
'user' => $loguser['id'],
'date' => ctime(),
'userdata' => $loguser,
'deleted' => 0,
'comments' => 0,
'tags' => array(), // TODO: Make tags work in the preview
'text' => $_POST['text'],
'title' => $_POST['title'],
'nosmilies' => $_POST['nosmilies'],
'nohtml' => $_POST['nohtml'],
);
?>
<table class='table'>
<tr><td class='tdbgh center b'>Message preview</td></tr>
<tr><td class='tdbg1'><?= news_format($preview) ?></td></tr>
</table>
<br>
<?php
}
}
else if (isset($_GET['del'])){
// ACTION: Hide/Unhide from normal users and guests
if (!$_GET['id'])
news_errorpage("No news ID specified.");
// Sanity check. Don't allow this unless you're the news author or an admin
$news = $sql->fetchq("SELECT user, deleted FROM news WHERE id = {$_GET['id']}");
if (!$news)
news_errorpage("The post doesn't exist!");
if (!$ismod && $loguser['id'] != $news['user'])
news_errorpage("You have no permission to do this!");
if ($news['deleted']) {
$message = "Do you want to undelete this post?";
$btntext = "Yes";
} else {
$message = "Are you sure you want to <b>DELETE</b> this post?";
$btntext = "Delete post";
}
$form_link = "news-editpost.php?del&id={$_GET['id']}";
$buttons = array(
0 => [$btntext],
1 => ["Cancel", "news.php?id={$_GET['id']}"]
);
if (confirmpage($message, $form_link, $buttons)) {
$sql->query("UPDATE news SET deleted = 1 - deleted WHERE id = {$_GET['id']}");
return header("Location: news.php");
}
}
else if (isset($_GET['erase']) && $sysadmin) {
// ACTION: Delete from database
if (!$_GET['id'])
news_errorpage("No post ID specified.");
$news = $sql->resultq("SELECT COUNT(*) FROM news WHERE id = {$_GET['id']}");
if (!$news)
news_errorpage("The post doesn't exist!");
$message = "Are you sure you want to <b>permanently DELETE</b> this post from the database?";
$form_link = "news-editpost.php?erase&id={$_GET['id']}";
$buttons = array(
0 => ["Delete post"],
1 => ["Cancel", "news.php?id={$_GET['id']}"]
);
if (confirmpage($message, $form_link, $buttons, TOKEN_SLAMMER)) {
$sql->beginTransaction();
$sql->query("DELETE FROM news WHERE id = {$_GET['id']}");
$sql->query("DELETE FROM news_comments WHERE pid = {$_GET['id']}");
$sql->query("DELETE FROM news_tags_assoc WHERE post = {$_GET['id']}");
$sql->commit();
return header("Location: news.php");
}
}
else {
news_errorpage("No action specified.");
}
?>
<center>
<form method='POST' action='?id=<?= $_GET['id'] ?>&new'>
<input type='hidden' name='auth' value='$token'>
<table class='table'>
<tr><td class='tdbgh center b' colspan='2'>Create post</td></tr>
<tr>
<td class='tdbg1 center b'>Title:</td>
<td class='tdbg2'>
<input type='text' name='title' style='width: 580px' value="<?= htmlspecialchars($_POST['title']) ?>">
</td>
</tr>
<tr>
<td class='tdbg1 center b'>Message:</td>
<td class='tdbg2' id='msgtd'>
<textarea id='msgtxt' name='text' rows='21' cols='80' width='800px' style='resize:both' wrap='virtual'><?= htmlspecialchars($_POST['text']) ?></textarea>
</td>
</tr>
<tr>
<td class='tdbg1 center'><b>Tags:</b><div class="fonts">hold CTRL to select multiple</div></td>
<td class='tdbg2'><?= tag_select($_POST['tags'], $_POST['customtags']) ?></td>
</tr>
<tr>
<td class='tdbg1 center b'>Options:</td>
<td class='tdbg2'>
<label><input type="checkbox" name="nosmilies" value=1 <?= $nosmilies_chk ?>> Disable smilies</label>
<label><input type="checkbox" name="nohtml" value=1 <?= $nohtml_chk ?>> Disable HTML</label>
</td>
</tr>
<tr>
<td class='tdbg1'></td>
<td class='tdbg2'>
<input type='submit' name='submit' value='Submit post'>
<input type='submit' name='preview' value='Preview post'><?= auth_tag() ?>
</td>
</tr>
</table>
</form>
</center>
<?php
print $barlinks;
replytoolbar('msg', $smilies);
news_footer();
function tag_select($sel = array(), $custom = "") {
global $sql;
$tags = load_news_tags();
$selected = array_flip($sel);
$out = "";
foreach ($tags as $id => $data) {
$out .= "<option value='{$id}' ".(isset($selected[$id]) ? "selected" : "").">".htmlspecialchars($data['title'])."</option>\r\n";
}
return
"<select multiple='multiple' name='tags[]' id='tags' style='min-width: 180px'>{$out}</select>".
" - or for <i>new</i> only: ".
"<input type='text' name='customtags' style='width: 250px' value=\"".htmlspecialchars($custom)."\">".
" (comma separated)";
}