forked from lincanbin/Carbon-Forum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.php
497 lines (490 loc) · 16 KB
/
manage.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
require(__DIR__ . '/common.php');
require(__DIR__ . '/language/' . ForumLanguage . '/manage.php');
SetStyle('api', 'API');
$ID = intval(Request('Post', 'ID', 0));
$Type = intval(Request('Post', 'Type', 0)); //1:Topic,2:Post,3:User
$Action = Request('Post', 'Action', false);
switch ($Type) {
//Topic
case 1:
$TopicInfo = $DB->row("SELECT * FROM " . $Prefix . "topics force index(PRI) Where ID=:ID", array(
"ID" => $ID
));
if (!$TopicInfo) {
AlertMsg('Topic Not Found', 'Topic Not Found');
}
switch ($Action) {
//将主题移动至回收站
case 'Delete':
Auth(4);
if ($TopicInfo['IsDel'] == 0) {
$DB->query("UPDATE " . $Prefix . "topics SET IsDel = 1 Where ID=:ID", array(
"ID" => $ID
));
//更新全站统计数据
$NewConfig = array(
"NumTopics" => $Config["NumTopics"] - 1
);
UpdateConfig($NewConfig);
//更新用户自身统计数据
$DB->query("UPDATE `" . $Prefix . "users` SET Topics=Topics-1 WHERE `ID`=?", array(
$TopicInfo['UserID']
));
//更新标签统计
if ($TopicInfo['Tags']) {
$DB->query("UPDATE `" . $Prefix . "tags` SET TotalPosts=TotalPosts-1 WHERE `Name` in (?)", explode('|', $TopicInfo['Tags']));
}
$Message = $Lang['Deleted'];
} else {
AlertMsg('Bad Request', $Lang['Deleted']);
}
break;
//从回收站恢复主题
case 'Recover':
Auth(4);
if ($TopicInfo['IsDel'] == 1) {
$DB->query("UPDATE " . $Prefix . "topics SET IsDel = 0 Where ID=:ID", array(
"ID" => $ID
));
//更新全站统计数据
$NewConfig = array(
"NumTopics" => $Config["NumTopics"] + 1
);
UpdateConfig($NewConfig);
//更新用户自身统计数据
$DB->query("UPDATE `" . $Prefix . "users` SET Topics=Topics+1 WHERE `ID`=?", array(
$TopicInfo['UserID']
));
//更新标签统计
if ($TopicInfo['Tags']) {
$DB->query("UPDATE `" . $Prefix . "tags` SET TotalPosts=TotalPosts+1 WHERE `Name` in (?)", explode('|', $TopicInfo['Tags']));
}
$Message = $Lang['Recovered'];
} else {
AlertMsg('Bad Request', $Lang['Failure_Recovery']);
}
break;
//永久删除主题(需要先将主题移动至回收站)
case 'PermanentlyDelete':
Auth(5);
if ($TopicInfo['IsDel'] == 1) {
$DB->query('DELETE FROM `' . $Prefix . 'posttags` WHERE TopicID=?', array(
$ID
));
$DB->query('DELETE FROM `' . $Prefix . 'posts` WHERE TopicID=?', array(
$ID
));
$DB->query('DELETE FROM `' . $Prefix . 'topics` WHERE ID=?', array(
$ID
));
$DB->query('DELETE FROM `' . $Prefix . 'notifications` WHERE TopicID=?', array(
$ID
));
$Message = $Lang['Permanently_Deleted'];
} else {
AlertMsg('Bad Request', $Lang['Failure_Permanent_Deletion']);
}
break;
//主题下沉(LastTime-7*86400)
case 'Sink':
Auth(4);
$DB->query("UPDATE " . $Prefix . "topics SET LastTime = LastTime-604800 Where ID=:ID", array(
"ID" => $ID
));
$Message = $Lang['Sunk'];
break;
//主题上浮(LastTime+7*86400)
case 'Rise':
Auth(4);
$DB->query("UPDATE " . $Prefix . "topics SET LastTime = LastTime+604800 Where ID=:ID", array(
"ID" => $ID
));
$Message = $Lang['Risen'];
break;
//主题锁定
case 'Lock':
Auth(4);
$DB->query("UPDATE " . $Prefix . "topics SET IsLocked = :IsLocked Where ID=:ID", array(
"ID" => $ID,
"IsLocked" => $TopicInfo['IsLocked'] ? 0 : 1
));
$Message = $TopicInfo['IsLocked'] ? $Lang['Lock'] : $Lang['Unlock'];
break;
//删除标签
case 'DeleteTag':
Auth(4, $TopicInfo['UserID'], true);
$TagName = Request('Post', 'TagName');
if ((count(explode('|', $TopicInfo['Tags'])) - 1) >= 1 && $DB->query("DELETE FROM `" . $Prefix . "posttags`
WHERE TopicID = ? AND TagID = (SELECT ID FROM `" . $Prefix . "tags` WHERE Name = ?)", array(
$ID,
$TagName
))) {
// 更新标签统计数据
$DB->query("UPDATE `" . $Prefix . "tags` SET TotalPosts=TotalPosts-1 WHERE `Name`=?", array(
$TagName
));
// 更新Topics表里的Tags缓存
$DB->query("UPDATE `" . $Prefix . "topics` SET Tags=? WHERE `ID`=?", array(
implode('|', TagsDiff(explode('|', $TopicInfo['Tags']), array(
$TagName
))),
$ID
));
$Message = 'Success';
} else {
AlertMsg('Bad Request', 'Bad Request');
}
break;
//添加标签
case 'AddTag':
Auth(4, $TopicInfo['UserID'], true);
$TagName = TagsDiff(array(
Request('Post', 'TagName')
), array());
if ($TagName && !in_array($TagName[0], explode('|', $TopicInfo['Tags'])) && (count(explode('|', $TopicInfo['Tags'])) + 1) <= $Config["MaxTagsNum"]) {
$TagName = $TagName[0];
$TagsExist = $DB->row("SELECT ID,Name FROM `" . $Prefix . "tags` WHERE `Name` = ?", array(
$TagName
));
if (!$TagsExist) {
$DB->query("INSERT INTO `" . $Prefix . "tags`
(`ID`, `Name`,`Followers`,`Icon`,`Description`, `IsEnabled`, `TotalPosts`, `MostRecentPostTime`, `DateCreated`)
VALUES (?,?,?,?,?,?,?,?,?)", array(
null,
htmlspecialchars(trim($TagName)),
0,
0,
null,
1,
1,
$TimeStamp,
$TimeStamp
));
$TagID = $DB->lastInsertId();
if ($TagID) {
$DB->query("INSERT INTO `" . $Prefix . "posttags`
(`TagID`, `TopicID`, `PostID`)
VALUES (" . $TagID . ", " . $ID . ", (SELECT ID FROM `" . $Prefix . "posts` WHERE TopicID = " . $ID . " AND IsTopic = 1 LIMIT 1))");
//更新全站统计数据
$NewConfig = array(
"NumTags" => $Config["NumTags"] + 1
);
UpdateConfig($NewConfig);
}
} else {
if ($DB->query("INSERT INTO `" . $Prefix . "posttags`
(`TagID`, `TopicID`, `PostID`)
VALUES (" . $TagsExist['ID'] . ", " . $ID . ", (SELECT ID FROM `" . $Prefix . "posts` WHERE TopicID = " . $ID . " AND IsTopic = 1 LIMIT 1))")) {
// 更新标签统计数据
$DB->query("UPDATE `" . $Prefix . "tags` SET TotalPosts=TotalPosts+1 WHERE `Name`=?", array(
$TagName
));
}
}
$DB->query("UPDATE `" . $Prefix . "topics` SET Tags=? WHERE `ID`=?", array(
implode('|', $TopicInfo['Tags'] ? array_merge(explode('|', $TopicInfo['Tags']), array(
$TagName
)) : array(
$TagName
)),
$ID
));
$Message = 'Success';
} else {
AlertMsg('Bad Request', 'Bad Request');
}
break;
default:
AlertMsg('Bad Request', 'Bad Request');
break;
}
if ($MCache) {
//清理首页内存缓存
$MCache->delete(MemCachePrefix . 'Homepage');
//清理主题缓存
$MCache->delete(MemCachePrefix . 'Topic_' . $ID);
}
break;
//Post
case 2:
$PostInfo = $DB->row("SELECT * FROM " . $Prefix . "posts force index(PRI) Where ID=:ID", array(
"ID" => $ID
));
if (!$PostInfo) {
AlertMsg('Post Not Found', 'Post Not Found');
}
switch ($Action) {
case 'Delete':
Auth(4);
$DB->query('DELETE FROM `' . $Prefix . 'posts` WHERE ID=?', array(
$ID
));
$DB->query('DELETE FROM `' . $Prefix . 'notifications` WHERE PostID=?', array(
$ID
));
//更新全站统计数据
$NewConfig = array(
"NumPosts" => $Config["NumPosts"] - 1
);
UpdateConfig($NewConfig);
//更新主题统计数据
$DB->query("UPDATE `" . $Prefix . "topics` SET Replies=Replies-1 WHERE `ID`=?", array(
$PostInfo['TopicID']
));
//更新用户自身统计数据
$DB->query("UPDATE `" . $Prefix . "users` SET Replies=Replies-1 WHERE `ID`=?", array(
$PostInfo['UserID']
));
$Message = $Lang['Permanently_Deleted'];
break;
//编辑帖子
case 'Edit':
//Auth(4, $PostInfo['UserID'], true);
Auth(4);
$Content = XssEscape(Request('Post', 'Content', $PostInfo['Content']));
if ($Content == $PostInfo['Content'])
AlertMsg($Lang['Do_Not_Modify'], $Lang['Do_Not_Modify']);
if ($DB->query("UPDATE " . $Prefix . "posts SET Content = :Content Where ID=:ID", array(
'ID' => $ID,
'Content' => $Content
))) {
//标记附件所对应的帖子标签
$DB->query("UPDATE `" . $Prefix . "upload` SET PostID=? WHERE `PostID`=0 and `UserName`=?", array(
$ID,
$CurUserName
));
$Message = $Lang['Edited'];
} else {
AlertMsg($Lang['Failure_Edit'], $Lang['Failure_Edit']);
}
break;
default:
AlertMsg('Bad Request', 'Bad Request');
break;
}
break;
//User
case 3:
$UserInfo = $DB->row("SELECT * FROM " . $Prefix . "users force index(PRI) Where ID=:ID", array(
"ID" => $ID
));
switch ($Action) {
case 'Delete':
Auth(4);
# code...
break;
//屏蔽用户
case 'Block':
Auth(4);
$NewUserAccountStatus = $UserInfo['UserAccountStatus'] ? 0 : 1;
if (UpdateUserInfo(array(
'UserAccountStatus' => $NewUserAccountStatus
), $ID)) {
$Message = $NewUserAccountStatus ? $Lang['Block_User'] : $Lang['Unblock_User'];
}
break;
//重置头像
case 'ResetAvatar':
Auth(4, $ID);
if (extension_loaded('gd')) {
require(__DIR__ . "/includes/MaterialDesign.Avatars.class.php");
$Avatar = new MDAvtars(mb_substr($UserInfo['UserName'], 0, 1, "UTF-8"), 256);
$Avatar->Save('upload/avatar/large/' . $ID . '.png', 256);
$Avatar->Save('upload/avatar/middle/' . $ID . '.png', 48);
$Avatar->Save('upload/avatar/small/' . $ID . '.png', 24);
$Avatar->Free();
$Message = $Lang['Reset_Avatar_Successfully'];
} else {
$Message = $Lang['Reset_Avatar_Successfully']; //Failure
}
break;
default:
AlertMsg('Bad Request', 'Bad Request');
break;
}
break;
//Follow or Favorite
case 4:
Auth(1);
$Action = intval($Action);
//检查主题/标签/用户/帖子是否存在
$IsFavorite = $DB->single("SELECT ID FROM " . $Prefix . "favorites Where UserID=:UserID and Type=:Type and FavoriteID=:FavoriteID", array(
'UserID' => $CurUserID,
'Type' => $Action,
'FavoriteID' => $ID
)); //添加索引
$MessageType = false; //false表示收藏,true表示关注
$SQLAction = intval($IsFavorite) ? '-1' : '+1';
switch ($Action) {
//1:Topic 2:Tag 3:User 4:Post 5:Blog
case 1: //Topic
$Title = $DB->single("SELECT Topic FROM " . $Prefix . "topics Where ID=:FavoriteID", array(
'FavoriteID' => $ID
));
break;
case 2: //Tag
$Title = $DB->single("SELECT Name FROM " . $Prefix . "tags Where ID=:FavoriteID", array(
'FavoriteID' => $ID
));
$MessageType = true;
break;
case 3: //User
$Title = $DB->single("SELECT UserName FROM " . $Prefix . "users Where ID=:FavoriteID", array(
'FavoriteID' => $ID
));
$MessageType = true;
break;
case 4: //Post
$Title = $DB->single("SELECT Subject FROM " . $Prefix . "posts Where ID=:FavoriteID", array(
'FavoriteID' => $ID
));
break;
case 5: //Blog
$Title = $DB->single("SELECT Subject FROM " . $Prefix . "blogs Where ID=:FavoriteID and ParentID=0", array(
'FavoriteID' => $ID
));
break;
default:
AlertMsg('Bad Request', 'Bad Request');
break;
}
if ($Title) {
if (!$IsFavorite) {
if (!$DB->query('INSERT INTO `' . $Prefix . 'favorites`(`ID`, `UserID`, `Category`, `Title`, `Type`, `FavoriteID`, `DateCreated`, `Description`) VALUES (?,?,?,?,?,?,?,?)', array(
null,
$CurUserID,
'',
$Title,
$Action,
$ID,
$TimeStamp,
''
)))
AlertMsg('Unknown Error', 'Unknown Error');
} else {
$DB->query('DELETE FROM `' . $Prefix . 'favorites` WHERE `UserID`=? and `Type`=? and `FavoriteID`=?', array(
$CurUserID,
$Action,
$ID
));
}
switch ($Action) {
//1:Topic 2:Tag 3:User 4:Post 5:Blog
case 1: //Topic
$DB->query('UPDATE ' . $Prefix . 'topics SET Favorites = Favorites' . $SQLAction . ' Where ID=:FavoriteID', array(
'FavoriteID' => $ID
));
$DB->query('UPDATE `' . $Prefix . 'users` SET NumFavTopics=NumFavTopics' . $SQLAction . ' WHERE `ID`=?', array(
$CurUserID
));
break;
case 2: //Tag
$DB->query('UPDATE ' . $Prefix . 'tags SET Followers = Followers' . $SQLAction . ' Where ID=:FavoriteID', array(
'FavoriteID' => $ID
));
$DB->query('UPDATE `' . $Prefix . 'users` SET NumFavTags=NumFavTags' . $SQLAction . ' WHERE `ID`=?', array(
$CurUserID
));
break;
case 3: //User
$DB->query('UPDATE ' . $Prefix . 'users SET Followers = Followers' . $SQLAction . ' Where ID=:FavoriteID', array(
'FavoriteID' => $ID
));
$DB->query('UPDATE `' . $Prefix . 'users` SET NumFavUsers=NumFavUsers' . $SQLAction . ' WHERE `ID`=?', array(
$CurUserID
));
break;
case 4: //Post
break;
case 5: //Blog
break;
default:
AlertMsg('Bad Request', 'Bad Request');
break;
}
//清理内存缓存
if ($MCache) {
$MCache->delete(MemCachePrefix . 'UserInfo_' . $CurUserID);
}
$Message = $IsFavorite ? ($MessageType ? $Lang['Follow'] : $Lang['Collect']) : ($MessageType ? $Lang['Unfollow'] : $Lang['Unsubscribe']);
//$FavoriteID = $DB->lastInsertId();
} else {
AlertMsg('404 Not Found', '404 Not Found', 404);
}
break;
//Tag
case 5:
$TagInfo = $DB->row("SELECT * FROM " . $Prefix . "tags Where ID=:ID", array(
"ID" => $ID
));
if (!$TagInfo) {
AlertMsg('Tag Not Found', 'Tag Not Found');
}
switch ($Action) {
// 修改标签描述
case 'EditDescription':
Auth(3);
$Content = CharCV(Request('Post', 'Content', $TagInfo['Description']));
if ($Content == $TagInfo['Description'])
AlertMsg($Lang['Do_Not_Modify'], $Lang['Do_Not_Modify']);
if($DB->query('UPDATE ' . $Prefix . 'tags SET Description = :Content WHERE ID=:TagID',
array(
'TagID' => $ID,
'Content' => $Content
)
)) {
$Message = $Lang['Edited'];
} else {
AlertMsg($Lang['Failure_Edit'], $Lang['Failure_Edit']);
}
break;
// 修改标签图标
case 'UploadIcon':
Auth(3);
if ($_FILES['TagIcon']['size'] && $_FILES['TagIcon']['size'] < 1048576) {
require(__DIR__ . "/includes/ImageResize.class.php");
$UploadIcon = new ImageResize('PostField', 'TagIcon');
$LUploadResult = $UploadIcon->Resize(256, 'upload/tag/large/' . $ID . '.png', 80);
$MUploadResult = $UploadIcon->Resize(48, 'upload/tag/middle/' . $ID . '.png', 90);
$SUploadResult = $UploadIcon->Resize(24, 'upload/tag/small/' . $ID . '.png', 90);
if ($LUploadResult && $MUploadResult && $SUploadResult) {
$SetTagIconStatus = $TagInfo['Icon'] == 0?
$DB->query('UPDATE ' . $Prefix . 'tags SET Icon = 1 Where ID=:TagID',
array('TagID' => $ID))
:true;
$Message = $SetTagIconStatus ? $Lang['Icon_Upload_Success'] : $Lang['Icon_Upload_Failure'];
} else {
$Message = $Lang['Icon_Upload_Failure'];
}
} else {
$Message = $Lang['Icon_Is_Oversize'];
}
break;
// 禁用/启用该标签
case 'SwitchStatus':
Auth(4);
if($DB->query('UPDATE ' . $Prefix . 'tags SET IsEnabled = :IsEnabled WHERE ID=:TagID',
array(
'TagID' => $ID,
'IsEnabled' => $TagInfo['IsEnabled']?0:1 //Bool -> Int
)
)){
$Message = $TagInfo['IsEnabled']?$Lang['Enable_Tag']:$Lang['Disable_Tag'];
}else {
AlertMsg('Bad Request', 'Bad Request');
}
break;
default:
AlertMsg('Bad Request', 'Bad Request');
break;
}
break;
//Error
default:
AlertMsg('Bad Request', 'Bad Request');
break;
}
$PageTitle = 'Manage';
$ContentFile = $TemplatePath . 'manage.php';
include($TemplatePath . 'layout.php');