-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.php
461 lines (420 loc) · 16.4 KB
/
post.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
<?php
require_once('connection/SQL.php');
require_once('config.php');
set_include_path('include/');
require_once('view.php');
require_once('user.php');
require_once('article.php');
require_once('security.php');
require_once('notification.php');
$user = validate_user();
if (!$user->valid) {
http_response_code(403);
header("Location: index.php?err=account");
exit;
}
if ($user->islogin && isset($_POST['pid']) && isset($_POST['title']) && isset($_POST['content'])) {
if ($user->level < 1 || $user->muted == 1) {
http_response_code(403);
header('axios-location: post.php?err=level');
exit;
}
if (!validate_csrf()) {
http_response_code(403);
header('axios-location: post.php?err=level');
}
if ($_POST['pid'] == "-1") {
// new post
if (trim($_POST['content']) == "") {
http_response_code(400);
header('axios-location: post.php?err=empty');
exit;
}
if (trim($_POST['title']) == "") {
$_POST['title'] = "(無標題)";
}
$current = date('Y-m-d H:i:s');
$SQL->query("INSERT INTO `post` (`title`, `content`, `time`, `username`) VALUES ('%s', '%s', '%s', '%s')", array(htmlspecialchars($_POST['title']), htmlspecialchars($_POST['content']), $current, $user->username));
$pid = $SQL->insert_id();
// notify tagged user
// the user who tag himself is unnecessary to notify
$username_list = parse_user_tag($_POST['content']);
foreach ($username_list as $key => $id) {
if ($id == $user->username) continue;
$_title = htmlspecialchars($_POST['title']);
cavern_notify_user($id, "{{$user->name}}@{$user->username} 在 [$_title] 中提到了你", "post.php?pid=$pid");
}
http_response_code(201); // 201 Created
header('axios-location: post.php?pid='.$pid);
exit;
} else {
// edit old post
$pid = abs($_POST['pid']);
try {
$post = new Article($pid);
} catch (NoPostException $e) {
// post not found
http_response_code(404);
header("axios-location: index.php?err=post");
exit;
}
if ($post->author !== $user->username && $user->level < 8) {
http_response_code(403);
header('axios-location: post.php?err=edit');
exit;
}
if (trim($_POST['content']) == "") {
http_response_code(400);
header('axios-location: post.php?err=empty');
exit;
}
if (trim($_POST['title']) == "") {
$_POST['title'] = "(無標題)";
}
$post->modify($user, "title", htmlspecialchars($_POST['title']));
$post->modify($user, "content", htmlspecialchars($_POST['content']));
$post->save();
header('axios-location: post.php?pid='.$_POST['pid']);
exit;
}
}
if ($user->islogin && isset($_GET['del']) && trim($_GET['del']) != '') {
if (!validate_csrf()) {
http_response_code(403);
header('axios-location: post.php?err=level');
exit;
}
try {
$post = new Article(intval($_GET['del']));
} catch (NoPostException $e) {
http_response_code(404);
header('axios-location: index.php?err=post');
exit;
}
if ($post->author !== $user->username && $user->level < 8) {
http_response_code(403);
header('axios-location: post.php?err=del');
exit;
} else {
$SQL->query("DELETE FROM `post` WHERE `pid`='%d' AND `username` = '%s'", array($_GET['del'], $_SESSION['cavern_username']));
http_response_code(204);
header('axios-location: post.php?ok=del');
exit;
}
} else if (!$user->islogin && isset($_GET['del'])) {
http_response_code(204);
header('axios-location: index.php?err=nologin');
exit;
}
// View
if (isset($_GET['pid'])) {
$pid = abs($_GET['pid']);
try {
$post = new Article($pid);
} catch (NoPostException $e) {
http_response_code(404);
header('Location: index.php?err=post');
exit;
}
if ($user->islogin) {
$view = new View('theme/default.html', 'theme/nav/util.php', 'theme/sidebar.php', $blog['name'], $post->title);
$view->add_script_source("ts('.ts.dropdown:not(.basic)').dropdown();");
$owner_view = ($post->author === $user->username);
} else {
$view = new View('theme/default.html', 'theme/nav/default.html', 'theme/sidebar.php', $blog['name'], $post->title);
$owner_view = FALSE;
}
$view->add_script("https://unpkg.com/[email protected]");
$view->add_script("./include/js/lib/editormd.js");
$view->add_script("./include/js/lib/css.min.js");
$view->add_script("./include/js/security.js");
$view->add_script("./include/js/markdown.js");
$view->add_script("./include/js/comment.js");
$view->add_script("./include/js/post.js");
$view->add_script("./include/js/like.js");
$view->add_script_source("ts('.ts.tabbed.menu .item').tab();");
if (isset($_GET['ok'])) {
if ($_GET['ok'] == "login" && $user->islogin) {
$greeting = cavern_greeting();
$view->show_message("inverted positive", "{$greeting}!我的朋友!");
}
}
?>
<div class="ts<?php echo ($owner_view ? " stackable " : " "); ?>grid">
<div class="stretched column" id="header">
<h2 class="ts header">
<?= $post->title ?>
<div class="sub header"><a href="user.php?username=<?= $post->author ?>"><?= $post->name ?></a></div>
</h2>
</div>
<div class="action column">
<div class="ts secondary icon buttons">
<button class="ts secondary icon like button" data-id="<?= $pid ?>">
<i class="thumbs <?php if (!$post->is_like($user)) {echo "outline";}?> up icon"></i> <?= $post->likes_count ?>
</button>
<?php
if ($owner_view) { ?>
<a class="ts secondary icon button" href="post.php?edit=<?= $pid ?>">
<i class="edit icon"></i>
</a>
<a class="ts secondary icon delete button" href="post.php?del=<?= $pid ?>">
<i class="trash icon"></i>
</a>
<?php } ?>
</div>
</div>
</div>
<div class="ts segments">
<div class="ts flatted segment" id="post" data-id="<?= $pid ?>">
<div class="markdown"><?= $post->content ?></div>
</div>
<div class="ts right aligned tertiary segment">
<i class="clock icon"></i><?= $post->time ?>
</div>
</div>
<div class="comment header">
<div class="ts grid">
<div class="stretched header column">
<div class="ts big header">留言</div>
</div>
<div class="column">
<span class="fetch time">Last fetch: --:--</span>
<div class="ts active inline loader"></div>
<button class="ts fetch icon button">
<i class="refresh icon"></i>
</button>
</div>
</div>
<div class="ts comment divider"></div>
</div>
<div class="ts comments">
<div class="ts borderless flatted no-comment segment">現在還沒有留言!</div>
</div>
<div class="ts segments" id="comment">
<div class="ts fitted secondary segment">
<div class="ts tabbed menu">
<a class="active item" data-tab="textarea">Write</a>
<a class="item" data-tab="preview">Preview</a>
</div>
</div>
<div class="ts clearing active tab segment" data-tab="textarea">
<?php if ($user->islogin) {
if ($user->muted) {
$disabled = " disabled";
$placeholder = "你被禁言了。";
$button_text = "你被禁言了";
} else {
$disabled = "";
$placeholder = "留言,然後開戰。";
$button_text = "留言";
}
} else {
$disabled = " disabled";
$placeholder = "請先登入";
$button_text = "留言";
} ?>
<div class="ts<?= $disabled ?> fluid input">
<textarea placeholder="<?= $placeholder ?>" rows="5" autocomplete="off"<?= $disabled ?>></textarea>
</div>
<div class="ts<?= $disabled ?> right floated separated action buttons">
<button class="ts positive submit button"><?= $button_text ?></button>
</div>
</div>
<div class="ts tab segment" id="preview" data-tab="preview"></div>
</div>
<?php $view->render();
} else if (isset($_GET['new']) || isset($_GET['edit'])) {
// New or Edit
if (!$user->islogin) {
header('Location: index.php?err=nologin');
exit;
}
if ($user->level < 1 || $user->muted) {
header('Location: post.php?err=level');
exit;
}
if (isset($_GET['new'])) {
$mode = "new";
$pid = -1;
$title = "";
$content = "";
} else if (isset($_GET['edit'])) {
$mode = "edit";
$pid = abs($_GET['edit']);
try {
$post = new Article($pid);
} catch (NoPostException $e) {
http_response_code(404);
header('Location: index.php?err=post');
exit;
}
if ($post->author != $user->username && $user->level < 8) {
http_response_code(403);
header('Location: post.php?err=edit');
exit;
}
$title = $post->title;
$content = $post->content;
}
$view = new View('theme/default.html', 'theme/nav/util.php', 'theme/sidebar.php', $blog['name'], ($title == "" ? "文章" : $title));
$view->add_script_source("ts('.ts.dropdown:not(.basic)').dropdown();");
$view->add_script("./include/js/lib/editormd.js");
$view->add_script("./include/js/lib/zh-tw.js");
$view->add_script("./include/js/lib/css.min.js");
$view->add_script("./include/js/security.js");
$view->add_script("./include/js/edit.js");
?>
<form action="post.php" method="POST" name="edit" id="edit" autocomplete="off"> <!-- prevent Firefox from autocompleting -->
<div class="ts stackable grid">
<div class="stretched column">
<div class="ts huge fluid underlined input">
<input placeholder="標題" name="title" value="<?= $title ?>">
</div>
</div>
<div class="action column">
<div class="ts buttons">
<button class="ts positive button">發布</button>
<?php if ($mode == "edit") { ?>
<a href="post.php?del=<?= $pid ?>" class="ts negative delete button">刪除</a>
<?php } ?>
<a href="index.php" class="ts button">取消</a>
</div>
</div>
</div>
<div id="markdownEditor">
<textarea id="markdown" name="content" stlye="display: none;" autocomplete="off" autocorrect="off" spellcheck="false"><?= $content ?></textarea>
</div>
<input type="hidden" name="pid" id="pid" value="<?= $pid ?>">
</form>
<?php $view->render();
} else {
// List all
if (!$user->islogin && (!isset($_GET['username']) || trim($_GET['username']) == "")) {
http_response_code(403);
header('Location: index.php?err=nologin');
exit;
}
if (isset($_GET['username']) && trim($_GET['username']) != "") {
$username = trim($_GET['username']);
try {
$target_user = new User($username);
} catch (NoUserException $e) {
http_response_code(404);
header('Location: user.php?err=no');
exit;
}
$post_list = article_list(cavern_query_result(
"SELECT * FROM `post` WHERE `username`='%s' ORDER BY `time`",
array($username))
);
} else if ($user->islogin) {
$username = $user->username;
$post_list = article_list(cavern_query_result(
"SELECT * FROM `post` WHERE `username`='%s' ORDER BY `time`",
array($username))
);
}
$owner_view = ($user->islogin && $username === $user->username);
if ($user->islogin) {
$view = new View('theme/default.html','theme/nav/util.php', 'theme/sidebar.php', $blog['name'], "文章");
$view->add_script_source("$('tbody').on('click', 'a.negative.button', function(e) {
e.preventDefault();
let el = e.currentTarget;
let href = el.getAttribute('href');
swal({
type: 'question',
title: '確定要刪除嗎?',
showCancelButton: true,
confirmButtonText: '確定',
cancelButtonText: '取消',
}).then((result) => {
if (result.value) { // confirm
axios.request({
method: 'GET',
maxRedirects: 0,
url: href
}).then(function (res) {
location.href = res.headers['axios-location'];
});
}
});
});");
} else {
$view = new View('theme/default.html','theme/nav/default.html', 'theme/sidebar.php', $blog['name'], "文章");
}
$view->add_script("./include/js/security.js");
$view->add_script_source("ts('.ts.dropdown').dropdown();\nts('.ts.sortable.table').tablesort();");
if (isset($_GET['ok'])) {
if ($_GET['ok'] == "del") {
$view->show_message("inverted positive", "刪除成功");
} else if ($_GET['ok'] == "login" && $user->islogin) {
$greeting = cavern_greeting();
$view->show_message("inverted positive", "{$greeting}!我的朋友!");
}
}
if (isset($_GET['err'])) {
switch ($_GET['err']) {
case 'del':
$view->show_message("inverted negative", "刪除失敗");
break;
case 'edit':
$view->show_message("inverted negative", "編輯失敗");
break;
case 'empty':
$view->show_message("warning", "文章內容不能為空!");
break;
case 'level':
$view->show_message("inverted negative", "你沒有權限發文!");
break;
}
}
?>
<div class="ts big dividing header">
文章
<?php if (!$owner_view) { // List other one's post ?>
<div class="sub header"><a href="user.php?username=<?= $username ?>"><?= $target_user->name ?></a></div>
<?php } ?>
</div>
<div class="table wrapper">
<table class="ts sortable celled striped table">
<thead>
<tr>
<th>標題</th>
<th>讚</th>
<th>留言</th>
<th>日期</th>
<?php if ($owner_view) { // Only owner could manage post ?>
<th>管理</th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
if (sizeof($post_list) > 0) {
foreach ($post_list as $key => $article) {
?>
<tr>
<td><a href="post.php?pid=<?= $article->pid ?>"><?= $article->title ?></a></td>
<td class="center aligned collapsing"><?= $article->likes_count ?></td>
<td class="center aligned collapsing"><?= $article->comments_count ?></td>
<td class="collapsing"><?= $article->time ?></td>
<?php if ($owner_view) { // Only owner could manage post ?>
<td class="right aligned collapsing">
<a class="ts circular icon button" href="post.php?edit=<?= $article->pid ?>"><i class="pencil icon"></i></a>
<a class="ts negative circular icon button" href="post.php?del=<?= $article->pid ?>"><i class="trash icon"></i></a>
</td>
<?php } ?>
</tr>
<?php }
} else { ?>
<tr>
<td colspan="<?php echo ($owner_view ? 5 : 4); ?>">沒有文章</td>
<tr>
<?php } ?>
</tbody>
</table>
</div>
<?php
$view->render();
}
?>