This repository has been archived by the owner on Jan 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
post.php
294 lines (288 loc) · 11.5 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
<?php
require_once('connection/SQL.php');
require_once('config.php');
require_once('include/view.php');
if(isset($_SESSION['username']) && isset($_POST['pid']) && isset($_POST['title']) && isset($_POST['content'])){
$level = getResult("SELECT `level` FROM `user` WHERE `username` = '%s'",array($_SESSION['username']))['row']['level'];
if (intval($level) < 1) {
header('Location: post.php');
exit;
}
if($_POST['pid'] == "-1"){
$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,$_SESSION['username']));
$pid = getResult("SELECT `pid` FROM `post` WHERE `time` = '%s'",array($current))['row']['pid'];
header('Location: post.php?pid='.$pid);
exit;
} else {
$author = getResult("SELECT `username` FROM `post` WHERE `pid` = '%d'",array($_POST['pid']))['row']['username'];
if ($author != $_SESSION['username']){
header('Location: post.php?err=edit');
exit;
}
$SQL->query("UPDATE `post` SET `title`='%s', `content`='%s' WHERE `pid`='%d' AND `username`='%s'",array(htmlspecialchars($_POST['title']),htmlspecialchars($_POST['content']),abs($_POST['pid']),$_SESSION['username']));
header('Location: post.php?pid='.$_POST['pid']);
exit;
}
}
if(isset($_SESSION['username'])){
$islogin = true;
} else {
$islogin = false;
}
if($islogin && isset($_GET['del']) && trim($_GET['del'])!=''){
$author = getResult("SELECT `username` FROM `post` WHERE `pid` = '%d'",array($_GET['del']))['row']['username'];
if ($author != $_SESSION['username']) {
header('Location: post.php?err=del');
exit;
} else {
$SQL->query("DELETE FROM `post` WHERE `pid`='%d' AND `username` = '%s'",array($_GET['del'],$_SESSION['username']));
$SQL->query("DELETE FROM `like` WHERE `pid`='%d'",array($_GET['del']));
header('Location: post.php?ok=del');
exit;
}
} else if (!$islogin && isset($_GET['del'])) {
header('Location: index.php?err=nologin');
exit;
}
// View
if(isset($_GET['pid'])){
$pid = abs($_GET['pid']);
$post = getResult("SELECT * FROM `post` WHERE `pid`=%d",array($pid));
if($post['num_rows']<1){ // not found post
header('Location: index.php?err=post');
exit;
}
// get post data
$name = getResult("SELECT `name` FROM `user` WHERE `username`='%s'",array($post['row']['username']))['row']['name'];
$title = $post['row']['title'];
$content = $post['row']['content'];
$time = $post['row']['time'];
$likes = getResult("SELECT * FROM `like` WHERE `pid`='%d'",array($pid));
$islike = false;
if ($likes['num_rows']>0 && isset($_SESSION['username'])){
do {
if($_SESSION['username'] == $likes['row']['username']){
$islike = true;
}
}while($likes['row'] = $likes['query']->fetch_assoc());
}
if ($likes['num_rows']<1){
$likes = 0;
} else {
$likes = $likes['num_rows'];
}
if($islogin){
$view = new View('theme/default.html','theme/nav/util.php','theme/sidebar.php',$blog['name'],$title);
$view->addScript("<script>ts('.ts.dropdown:not(.basic)').dropdown();</script>");
$view->addScript('<script src="./include/js/markdown.js"></script>');
$view->addScript('<script src="./include/js/like.js"></script>');
} else {
$view = new View('theme/default.html','theme/nav/default.html','theme/sidebar.php',$blog['name'],$title);
$view->addScript('<script src="./include/js/markdown.js"></script>');
$view->addScript('<script src="./include/js/like.js"></script>');
}
?>
<div class="ts stackable grid">
<div class="stretched column">
<h1 style="margin-left:5px;word-break:break-all;"><?php echo $title;?></h1>
</div>
<div class="column">
<div class="ts buttons">
<button class="ts secondary icon button" id="like" data-id="<?php echo $pid;?>">
<i class="thumbs <?php if(!$islike){echo "outline";}?> up icon"></i> <?php echo $likes;?>
</button>
<?php
if (isset($_SESSION['username'])) {
if ($post['row']['username'] == $_SESSION['username']) {?>
<a class="ts secondary icon button" href="post.php?edit=<?php echo $pid;?>">
<i class="edit icon"></i>
</a>
<a class="ts secondary icon button" href="post.php?del=<?php echo $pid;?>">
<i class="trash icon"></i>
</a>
<?php }
} ?>
</div>
</div>
</div>
<div class="ts segments">
<div class="ts flatted segment" id="markdown" style="font-size:15px; line-height:1.8em;">
<?php echo $content;?>
</div>
<div class="ts tertiary segment">
<i class="user icon"></i> <?php echo $name;?> <i class="calendar icon"></i> <?php echo $time;?>
</div>
</div>
<?php $view->render();
} else if (isset($_GET['new'])) {
// New
if (!$islogin){
header('Location: index.php?err=nologin');
exit;
}
$view = new View('theme/default.html','theme/nav/util.php','theme/sidebar.php',$blog['name'],"文章");
$level = getResult("SELECT `level` FROM `user` WHERE `username` = '%s'",array($_SESSION['username']))['row']['level'];
if (intval($level) < 1) { ?>
<div class="ts inverted negative message">
<p>你沒有權限發文!</p>
</div>
<?php
$view->render();
exit;
}
$view->addScript('<script src="./include/js/markdown.js"></script>');
$view->addScript('<script src="./include/js/edit.js"></script>');
$view->addScript("<script>ts('.ts.dropdown:not(.basic)').dropdown();textarea.value = txtTrim(textarea.value);</script>");
?>
<form action="post.php" method="POST" name="edit" id="edit">
<div class="ts stackable grid">
<div class="stretched column">
<div class="ts huge fluid underlined input">
<input placeholder="標題" name="title" value="">
</div>
</div>
<div class="column" style="display:flex; justify-content:center; align-items:center;">
<div class="ts buttons">
<a href="javascript:post();" class="ts positive button">發布</a>
<a href="index.php" class="ts button">取消</a>
</div>
</div>
</div>
<div class="editor-wrapper">
<textarea id="markdown" name="content"></textarea>
</div>
<input type="hidden" name="pid" id="pid "value="-1">
</form>
<?php $view->render();
} else if (isset($_GET['edit'])) {
// Edit
if (!$islogin){
header('Location: index.php?err=nologin');
exit;
}
$level = getResult("SELECT `level` FROM `user` WHERE `username` = '%s'",array($_SESSION['username']))['row']['level'];
if (intval($level) != 1) {
$view = new View('theme/default.html','theme/nav/util.php','theme/sidebar.php',$blog['name'],"文章"); ?>
<div class="ts inverted negative message">
<p>你沒有權限發文!</p>
</div>
<?php
$view->render();
exit;
}
$pid = abs($_GET['edit']);
$post = getResult("SELECT * FROM `post` WHERE `pid`=%d",array($pid));
if($post['num_rows']<1){ // not found post
header('Location: index.php?err=post');
exit;
}
if($post['row']['username'] != $_SESSION['username']){
header('Location: post.php?err=edit');
exit;
}
// get post data
$title = $post['row']['title'];
$content = $post['row']['content'];
$time = $post['row']['time'];
$view = new View('theme/default.html','theme/nav/util.php','theme/sidebar.php',$blog['name'],$title);
$view->addScript('<script src="./include/js/markdown.js"></script>');
$view->addScript('<script src="./include/js/edit.js"></script>');
$view->addScript("<script>ts('.ts.dropdown:not(.basic)').dropdown();textarea.value = txtTrim(textarea.value);</script>");
?>
<form action="post.php" method="POST" name="edit" id="edit">
<div class="ts stackable grid">
<div class="stretched column">
<div class="ts huge fluid underlined input">
<input placeholder="標題" name="title" value="<?php echo $title;?>">
</div>
</div>
<div class="column" style="display:flex; justify-content:center; align-items:center;">
<div class="ts buttons">
<a href="javascript:post();" class="ts positive button">發布</a>
<a href="post.php?del=<?php echo $pid;?>" class="ts negative button">刪除</a>
<a href="index.php" class="ts button">取消</a>
</div>
</div>
</div>
<div class="editor-wrapper">
<textarea id="markdown" name="content"><?php echo $content;?></textarea>
</div>
<input type="hidden" name="pid" id="pid "value="<?php echo $pid;?>">
</form>
<?php $view->render();
} else {
// List all
if(!$islogin){
header('Location: index.php?err=nologin');
exit;
}
$post_list=getResult("SELECT * FROM `post` WHERE `username`='%s' ORDER BY `time`",array($_SESSION['username']));
$view = new View('theme/default.html','theme/nav/util.php','theme/sidebar.php',$blog['name'],"文章");
$view->addScript("<script>ts('.ts.dropdown').dropdown();ts('.ts.sortable.table').tablesort();</script>");
if (isset($_GET['ok'])){
if ($_GET['ok'] == "del"){ ?>
<div class="ts inverted positive message">
<p>刪除成功</p>
</div>
<?php }
}
if (isset($_GET['err'])) {
if ($_GET['err'] == "del"){ ?>
<div class="ts inverted negative message">
<p>刪除失敗</p>
</div>
<?php } else if ($_GET['err'] == "edit") {?>
<div class="ts inverted negative message">
<p>編輯失敗</p>
</div>
<?php }
}
?>
<div class="ts big dividing header">文章</div>
<table class="ts sortable celled striped table">
<thead>
<tr>
<th>標題</th>
<th>讚</th>
<th>日期</th>
<th>管理</th>
</tr>
</thead>
<tbody>
<?php
if($post_list['num_rows']>0){
do {
$author = getResult("SELECT `name` FROM `user` WHERE `username`='%s'",array($post_list['row']['username']));
$pid = $post_list['row']['pid'];
$title = $post_list['row']['title'];
$time = $post_list['row']['time'];
$likes = getResult("SELECT * FROM `like` WHERE `pid`='%d'",array($pid));
if ($likes['num_rows']<1) {
$likes = 0;
} else {
$likes = $likes['num_rows'];
}
?>
<tr>
<td><a href="post.php?pid=<?php echo $pid;?>"><?php echo $title;?></a></td>
<td class="collapsing"><?php echo $likes;?></td>
<td class="collapsing"><?php echo $time;?></td>
<td class="right aligned collapsing">
<a class="ts primary button" href="post.php?edit=<?php echo $pid;?>">編輯</a>
<a class="ts negative button" href="post.php?del=<?php echo $pid;?>">刪除</a>
</td>
</tr>
<?php }while($post_list['row'] = $post_list['query']->fetch_assoc()); ?>
</tbody>
</table>
<?php } else {?>
<tr>
<td colspan="4">沒有文章</td>
<tr>
</tbody>
</table>
<?php }
$view->render();
}
?>