-
Notifications
You must be signed in to change notification settings - Fork 4
/
editpost.php
175 lines (146 loc) · 6.01 KB
/
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
<?php
use Sunlight\Post\PostService;
use Sunlight\Database\Database as DB;
use Sunlight\Extend;
use Sunlight\GenericTemplates;
use Sunlight\Message;
use Sunlight\Post\Post;
use Sunlight\PostForm;
use Sunlight\Router;
use Sunlight\User;
use Sunlight\Util\Form;
use Sunlight\Util\Html;
use Sunlight\Util\Request;
use Sunlight\Util\StringHelper;
defined('SL_ROOT') or exit;
if (!User::isLoggedIn()) {
$_index->unauthorized();
return;
}
$message = '';
$form = true;
$id = (int) Request::get('id');
[$columns, $joins, $cond] = Post::createFilter('p');
$userQuery = User::createQuery('p.author');
$columns .= ',home_page.layout page_layout,' . $userQuery['column_list'];
$joins .= ' ' . $userQuery['joins'];
$post = DB::queryRow('SELECT ' . $columns . Extend::buffer('posts.columns') . ' FROM ' . DB::table('post') . ' p ' . $joins . ' WHERE p.id=' . $id . ' AND ' . $cond);
if ($post !== false) {
if (isset($post['page_layout'])) {
$_index->changeTemplate($post['page_layout']);
}
if (!Post::checkAccess($userQuery, $post)) {
$_index->unauthorized();
return;
}
} else {
$_index->notFound();
return;
}
$_index->backlink = Router::postPermalink($id);
// save
if (isset($_POST['text'])) {
if (!Form::loadCheckbox('delete')) {
if ($post['author'] == -1) {
$guest = PostService::normalizeGuestName(Request::post('guest', ''));
} else {
$guest = '';
}
$text = Html::cut(_e(trim(Request::post('text', ''))), Post::getMaxLength($post['type']));
if ($post['xhome'] == -1 && in_array($post['type'], [Post::FORUM_TOPIC, Post::PRIVATE_MSG])) {
$subject = Html::cut(_e(StringHelper::trimExtraWhitespace(Request::post('subject'))), 48);
if ($subject === '') {
$subject = '-';
}
} else {
$subject = '';
}
// save
if ($text != '') {
$continue = true;
Extend::call('mod.editpost.edit', [
'id' => $id,
'post' => $post,
'subject' => &$subject,
'text' => &$text,
'continue' => &$continue,
]);
if ($continue) {
$update_data = [
'text' => $text,
'subject' => $subject,
'guest' => $guest,
];
DB::update('post', 'id=' . DB::val($id), $update_data);
$_index->redirect(Router::module('editpost', ['query' => ['id' => $id , 'saved' => 1], 'absolute' => true]));
return;
}
} else {
$message = Message::warning(_lang('mod.editpost.failed'));
}
} else {
// delete
if ($post['type'] != Post::PRIVATE_MSG || $post['xhome'] != -1) {
$continue = true;
Extend::call('mod.editpost.delete', [
'id' => $id,
'post' => $post,
'continue' => &$continue,
]);
if ($continue) {
// update topic bump time
if ($post['type'] == Post::FORUM_TOPIC && $post['xhome'] != -1) {
// check if this is the last reply
$chq = DB::query('SELECT id,time FROM ' . DB::table('post') . ' WHERE type=' . Post::FORUM_TOPIC . ' AND xhome=' . $post['xhome'] . ' ORDER BY id DESC LIMIT 2');
$chr = DB::row($chq);
if ($chr !== false && $chr['id'] == $id) {
// update bump time to last reply or topic creation time (if last reply is deleted)
$chr = DB::row($chq);
DB::update('post', 'id=' . $post['xhome'], ['bumptime' => $chr !== false ? $chr['time'] : DB::raw('time')]);
}
}
// remove replies
DB::delete('post', 'id=' . DB::val($id));
if ($post['xhome'] == -1) {
DB::delete('post', 'xhome=' . DB::val($id) . ' AND home=' . DB::val($post['home']) . ' AND type=' . DB::val($post['type']));
}
// info
$_index->backlink = PostService::getPostHomeUrl($post);
$message = Message::ok(_lang('mod.editpost.deleted'));
$form = false;
}
}
}
}
// output
$_index->title = _lang('mod.editpost');
// message
if (isset($_GET['saved']) && $message == '') {
$message = Message::ok(_lang('global.saved'));
}
$output .= $message;
// form
if ($form) {
$inputs = [];
if ($post['author'] == -1) {
$inputs[] = ['label' => _lang('posts.guestname'), 'content' => Form::input('text', 'guest', $post['guest'], ['class' => 'inputsmall', 'maxlength' => 24], false)];
}
if ($post['xhome'] == -1 && in_array($post['type'], [Post::FORUM_TOPIC, Post::PRIVATE_MSG])) {
$inputs[] = ['label' => _lang((($post['type'] != Post::FORUM_TOPIC) ? 'posts.subject' : 'posts.topic')), 'content' => Form::input('text', 'subject', $post['subject'], ['class' => 'inputmedium', 'maxlength' => 48], false), false];
}
$inputs[] = ['label' => _lang('posts.text'), 'content' => Form::textarea('text', $post['text'], ['class' => 'areamedium', 'rows' => 5, 'cols' => 33], false), 'top' => true];
$inputs[] = ['label' => '', 'content' => PostForm::renderControls('postform', 'text', $post['type'] != Post::SHOUTBOX_ENTRY)];
$inputs[] = Form::getSubmitRow([
'text' => _lang('global.save'),
'append' => ' ' . PostForm::renderPreviewButton('postform', 'text')
. (($post['type'] != Post::PRIVATE_MSG || $post['xhome'] != -1) ? '<br><br><label>' . Form::input('checkbox', 'delete', '1') . ' ' . _lang('mod.editpost.delete') . '</label>' : ''),
]);
$output .= Form::render(
[
'name' => 'postform',
'action' => Router::module('editpost', ['query' => ['id' => $id]]),
],
$inputs
);
$output .= GenericTemplates::jsLimitLength(Post::getMaxLength($post['type']), 'postform', 'text');
}