forked from XoopsModules25x/newbb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.post.php
264 lines (239 loc) · 11.4 KB
/
action.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
<?php declare(strict_types=1);
/**
* NewBB, the forum module for XOOPS project
*
* @copyright XOOPS Project (https://xoops.org)
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @author Taiwen Jiang (phppp or D.J.) <[email protected]>
* @since 4.00
*/
use Xmf\Request;
use XoopsModules\Newbb\{
Helper,
Topic,
TopicHandler,
Forum,
ForumHandler,
Post,
PostHandler,
Tree
};
/** @var TopicHandler $topicHandler */
/** @var ForumHandler $forumHandler */
/** @var PostHandler $postHandler */
/** @var Post $postObject */
require_once __DIR__ . '/header.php';
$topic_id = Request::getInt('topic_id', 0, 'POST');
$post_id = Request::getArray('post_id', Request::getArray('post_id', [], 'POST'), 'GET');
// Change by BigKev73, changed this code back to what this was before as with this following change, trying to permanently delete a deleted post reports no selection made.
//$post_id = Request::getInt('post_id', 0, 'GET');
//if (Request::hasVar('post_id', 'POST')) {
// Request::getArray('post_id', $post_id, 'POST');
//}*/
// !empty($_POST['post_id']) ? $_POST['post_id'] : $post_id;
$uid = Request::getInt('uid', 0, 'GET');
$op = Request::getCmd('op', Request::getCmd('op', '', 'POST'), 'GET');
$op = in_array($op, ['approve', 'delete', 'restore', 'split'], true) ? $op : '';
$mode = Request::getInt('mode', 1, 'GET');
if (0 === (is_countable($post_id) ? count($post_id) : 0) || ('' === $op)) {
// irmtfan - issue with javascript:history.go(-1)
redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, \_MD_NEWBB_NO_SELECTION);
}
///** @var PostHandler $postHandler */
//$postHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Post');
///** @var TopicHandler $topicHandler */
//$topicHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic');
///** @var NewbbForumHandler $forumHandler */
//$forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
if (0 === $topic_id) {
$forumObject = null;
} else {
/** @var Topic $topicObject */
$topicObject = $topicHandler->get($topic_id);
$forum_id = $topicObject->getVar('forum_id');
$forumObject = $forumHandler->get($forum_id);
}
$isAdmin = false;
if (assert($forumObject instanceof Forum)) {
$isAdmin = newbbIsAdmin($forumObject);
}
if (!$isAdmin) {
redirect_header(XOOPS_URL . '/index.php', 2, _MD_NEWBB_NORIGHTTOACCESS);
}
$post_update2 = null;
switch ($op) {
case 'restore':
$post_id = array_values($post_id);
sort($post_id);
$topics = [];
$forums = [];
foreach ($post_id as $post) {
$postObject = $postHandler->get($post);
assert($postObject instanceof Post);
if ($postObject->getVar('topic_id') < 1) {
continue;
}
$postHandler->approve($postObject, true);
$topics[$postObject->getVar('topic_id')] = 1;
$forums[$postObject->getVar('forum_id')] = 1;
unset($postObject);
}
foreach (array_keys($topics) as $topic) {
$topicHandler->synchronization($topic);
}
foreach (array_keys($forums) as $forum) {
$forumHandler->synchronization($forum);
}
break;
case 'approve':
$post_id = array_values($post_id);
sort($post_id);
$topics = [];
$forums = [];
$criteria = new \Criteria('post_id', '(' . implode(',', $post_id) . ')', 'IN');
$postArray = $postHandler->getObjects($criteria, true);
foreach ($post_id as $post) {
/** @var Post $postObject */
$postObject = $postArray[$post];
assert($postObject instanceof Post);
if (!empty($topic_id) && $topic_id !== $postObject->getVar('topic_id')) {
continue;
}
$postHandler->approve($postObject);
$topics[$postObject->getVar('topic_id')] = $post;
$forums[$postObject->getVar('forum_id')] = 1;
}
foreach (array_keys($topics) as $topic) {
$topicHandler->synchronization($topic);
}
foreach (array_keys($forums) as $forum) {
$forumHandler->synchronization($forum);
}
if (empty($GLOBALS['xoopsModuleConfig']['notification_enabled'])) {
break;
}
$criteria_topic = new \Criteria('topic_id', '(' . implode(',', array_keys($topics)) . ')', 'IN');
$topic_list = $topicHandler->getList($criteria_topic);
$criteria_forum = new \Criteria('forum_id', '(' . implode(',', array_keys($forums)) . ')', 'IN');
$forum_list = $forumHandler->getList($criteria_forum);
require_once __DIR__ . '/include/notification.inc.php';
/** @var \XoopsNotificationHandler $notificationHandler */
$notificationHandler = xoops_getHandler('notification');
foreach ($post_id as $post) {
$tags = [];
/** @var Post[] $postArray [$post] */
$tags['THREAD_NAME'] = $topic_list[$postArray[$post]->getVar('topic_id')];
$tags['THREAD_URL'] = XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . '/viewtopic.php?topic_id=' . $postArray[$post]->getVar('topic_id') . '&forum=' . $postArray[$post]->getVar('forum_id');
$tags['FORUM_NAME'] = $forum_list[$postArray[$post]->getVar('forum_id')];
$tags['FORUM_URL'] = XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . '/viewforum.php?forum=' . $postArray[$post]->getVar('forum_id');
$tags['POST_URL'] = $tags['THREAD_URL'] . '&topic_id=' . $postArray[$post]->getVar('topic_id') . '#forumpost' . $post;
$notificationHandler->triggerEvent('thread', $postArray[$post]->getVar('topic_id'), 'new_post', $tags);
$notificationHandler->triggerEvent('forum', $postArray[$post]->getVar('forum_id'), 'new_post', $tags);
$notificationHandler->triggerEvent('global', 0, 'new_post', $tags);
$tags['POST_CONTENT'] = $postArray[$post]->getVar('post_text');
$tags['POST_NAME'] = $postArray[$post]->getVar('subject');
$notificationHandler->triggerEvent('global', 0, 'new_fullpost', $tags);
$notificationHandler->triggerEvent('forum', $postArray[$post]->getVar('forum_id'), 'new_fullpost', $tags);
}
break;
case 'delete':
$post_id = array_values($post_id);
rsort($post_id);
$topics = [];
$forums = [];
foreach ($post_id as $post) {
$postObject = $postHandler->get($post);
assert($postObject instanceof Post);
if (!empty($topic_id) && $topic_id !== $postObject->getVar('topic_id')) {
continue;
}
$topics[$postObject->getVar('topic_id')] = 1;
$forums[$postObject->getVar('forum_id')] = 1;
$postHandler->myDelete($postObject, true);
unset($postObject);
}
foreach (array_keys($topics) as $topic) {
$topicHandler->synchronization($topic);
}
foreach (array_keys($forums) as $forum) {
$forumHandler->synchronization($forum);
}
break;
case 'split':
$postObject = $postHandler->get($post_id);
assert($postObject instanceof Post);
if ((is_array($post_id) && 0 === count($post_id)) || $postObject->isTopic()) {
break;
}
$topic_id = (int)$postObject->getVar('topic_id');
$newtopic = $topicHandler->create();
$newtopic->setVar('topic_title', $postObject->getVar('subject'), true);
$newtopic->setVar('topic_poster', $postObject->getVar('uid'), true);
$newtopic->setVar('forum_id', $postObject->getVar('forum_id'), true);
$newtopic->setVar('topic_time', $postObject->getVar('post_time'), true);
$newtopic->setVar('poster_name', $postObject->getVar('poster_name'), true);
$newtopic->setVar('approved', 1, true);
$topicHandler->insert($newtopic, true);
$new_topic_id = $newtopic->getVar('topic_id');
$pid = $postObject->getVar('pid');
$postObject->setVar('topic_id', $new_topic_id, true);
$postObject->setVar('pid', 0, true);
$postHandler->insert($postObject);
/* split a single post */
if (1 === $mode) {
$criteria = new \CriteriaCompo(new \Criteria('topic_id', (string)$topic_id));
$criteria->add(new \Criteria('pid', $post_id));
$postHandler->updateAll('pid', $pid, $criteria, true);
/* split a post and its children posts */
} elseif (2 === $mode) {
require_once $GLOBALS['xoops']->path('class/xoopstree.php');
$mytree = new Tree($GLOBALS['xoopsDB']->prefix('newbb_posts'), 'post_id', 'pid');
$posts = $mytree->getAllChildId($post_id);
if ((is_countable($posts) ? count($posts) : 0) > 0) {
$criteria = new \Criteria('post_id', '(' . implode(',', $posts) . ')', 'IN');
$postHandler->updateAll('topic_id', $new_topic_id, $criteria, true);
}
/* split a post and all posts coming after */
} elseif (3 === $mode) {
$criteria = new \CriteriaCompo(new \Criteria('topic_id', (string)$topic_id));
$criteria->add(new \Criteria('post_id', $post_id, '>'));
$postHandler->updateAll('topic_id', $new_topic_id, $criteria, true);
unset($criteria);
$criteria = new \CriteriaCompo(new \Criteria('topic_id', $new_topic_id));
$criteria->add(new \Criteria('post_id', (string)$post_id, '>'));
$postHandler->identifierName = 'pid';
$posts = $postHandler->getList($criteria);
unset($criteria);
$post_update = [];
foreach ($posts as $postid => $pid) {
// if (!in_array($pid, array_keys($posts))) {
if (!array_key_exists($pid, $posts)) {
$post_update[] = $pid;
}
if (!array_key_exists($pid, $posts)) {
$post_update2[] = $pid;
}
}
if (count($post_update)) {
$criteria = new \Criteria('post_id', '(' . implode(',', $post_update) . ')', 'IN');
$postHandler->updateAll('pid', $post_id, $criteria, true);
}
}
$forum_id = $postObject->getVar('forum_id');
$topicHandler->synchronization($topic_id);
$topicHandler->synchronization($new_topic_id);
// $sql = sprintf('UPDATE "%s" SET forum_topics = forum_topics+1 WHERE forum_id = "%u"', $GLOBALS['xoopsDB']->prefix('newbb_forums'), $forum_id);
$sql = sprintf('UPDATE %s SET forum_topics = forum_topics+1 WHERE forum_id = %u', $GLOBALS['xoopsDB']->prefix('newbb_forums'), $forum_id);
$result = $GLOBALS['xoopsDB']->queryF($sql);
break;
}
if (!empty($topic_id)) {
redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?topic_id=$topic_id", 2, _MD_NEWBB_DBUPDATED);
} elseif (!empty($forum_id)) {
redirect_header(XOOPS_URL . "/modules/newbb/viewforum.php?forum=$forum_id", 2, _MD_NEWBB_DBUPDATED);
} else {
redirect_header(XOOPS_URL . "/modules/newbb/viewpost.php?uid=$uid", 2, _MD_NEWBB_DBUPDATED);
}
// irmtfan move to footer.php
require_once __DIR__ . '/footer.php';
require_once $GLOBALS['xoops']->path('footer.php');