Completed
Pull Request — master (#6)
by Richard
02:13
created

action.post.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * NewBB 5.0x,  the forum module for XOOPS project
4
 *
5
 * @copyright      XOOPS Project (https://xoops.org)
6
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
7
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
8
 * @since          4.00
9
 * @package        module::newbb
10
 */
11
12
use Xmf\Request;
13
14
include_once __DIR__ . '/header.php';
15
16
$topic_id = Request::getInt('topic_id', 0, 'POST');
17
$post_id  = Request::getArray('post_id', Request::getArray('post_id', 0, 'POST'), 'GET');
18
$uid      = Request::getInt('uid', 0, 'GET');
19
20
$op   = Request::getCmd('op', Request::getCmd('op', '', 'POST'), 'GET');
21
$op   = in_array($op, ['approve', 'delete', 'restore', 'split'], true) ? $op : '';
22
$mode = Request::getInt('mode', 1, 'GET');
23
24 View Code Duplication
if (0 === count($post_id) || 0 === count($op)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    // irmtfan - issue with javascript:history.go(-1)
26
    redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_NORIGHTTOACCESS);
27
}
28
/** @var NewbbPostHandler $postHandler */
29
$postHandler = xoops_getModuleHandler('post', 'newbb');
30
/** @var NewbbTopicHandler $topicHandler */
31
$topicHandler = xoops_getModuleHandler('topic', 'newbb');
32
/** @var NewbbForumHandler $forumHandler */
33
$forumHandler = xoops_getModuleHandler('forum', 'newbb');
34
if (empty($topic_id)) {
35
    $forumObject = null;
36
} else {
37
    $topicObject = $topicHandler->get($topic_id);
38
    $forum_id  = $topicObject->getVar('forum_id');
39
    $forumObject = $forumHandler->get($forum_id);
40
}
41
$isAdmin = newbbIsAdmin($forumObject);
42
43
if (!$isAdmin) {
44
    redirect_header(XOOPS_URL . '/index.php', 2, _MD_NEWBB_NORIGHTTOACCESS);
45
}
46
47
switch ($op) {
48
    case 'restore':
49
        $post_id = array_values($post_id);
50
        sort($post_id);
51
        $topics = [];
52
        $forums = [];
53
        foreach ($post_id as $post) {
54
            $postObject = $postHandler->get($post);
55
            if ($postObject->getVar('topic_id') < 1) {
56
                continue;
57
            }
58
59
            $postHandler->approve($postObject, true);
60
            $topics[$postObject->getVar('topic_id')] = 1;
61
            $forums[$postObject->getVar('forum_id')] = 1;
62
            unset($postObject);
63
        }
64
        foreach (array_keys($topics) as $topic) {
65
            $topicHandler->synchronization($topic);
66
        }
67
        foreach (array_keys($forums) as $forum) {
68
            $forumHandler->synchronization($forum);
69
        }
70
        break;
71
    case 'approve':
72
        $post_id = array_values($post_id);
73
        sort($post_id);
74
        $topics    = [];
75
        $forums    = [];
76
        $criteria  = new Criteria('post_id', '(' . implode(',', $post_id) . ')', 'IN');
77
        $postsObject = $postHandler->getObjects($criteria, true);
78 View Code Duplication
        foreach ($post_id as $post) {
79
            /** @var \NewbbPost $postObject */
80
            $postObject = $postsObject[$post];
81
            if (!empty($topic_id) && $topic_id !== $postObject->getVar('topic_id')) {
82
                continue;
83
            }
84
            $postHandler->approve($postObject);
85
            $topics[$postObject->getVar('topic_id')] = $post;
86
            $forums[$postObject->getVar('forum_id')] = 1;
87
        }
88
        foreach (array_keys($topics) as $topic) {
89
            $topicHandler->synchronization($topic);
90
        }
91
        foreach (array_keys($forums) as $forum) {
92
            $forumHandler->synchronization($forum);
93
        }
94
95
        if (empty($GLOBALS['xoopsModuleConfig']['notification_enabled'])) {
96
            break;
97
        }
98
99
        $criteria_topic = new Criteria('topic_id', '(' . implode(',', array_keys($topics)) . ')', 'IN');
100
        $topic_list     = $topicHandler->getList($criteria_topic, true);
101
102
        $criteria_forum = new Criteria('forum_id', '(' . implode(',', array_keys($forums)) . ')', 'IN');
103
        $forum_list     = $forumHandler->getList($criteria_forum);
104
105
        include_once __DIR__ . '/include/notification.inc.php';
106
        /** @var \XoopsNotificationHandler $notificationHandler */
107
        $notificationHandler = xoops_getHandler('notification');
108
        foreach ($post_id as $post) {
109
            $tags = [];
110
            /** @var \NewbbPost[] $postsObject [$post] */
111
            $tags['THREAD_NAME'] = $topic_list[$postsObject[$post]->getVar('topic_id')];
112
            $tags['THREAD_URL']  = XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . '/viewtopic.php?topic_id=' . $postsObject[$post]->getVar('topic_id') . '&amp;forum=' . $postsObject[$post]->getVar('forum_id');
113
            $tags['FORUM_NAME']  = $forum_list[$postsObject[$post]->getVar('forum_id')];
114
            $tags['FORUM_URL']   = XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . '/viewforum.php?forum=' . $postsObject[$post]->getVar('forum_id');
115
            $tags['POST_URL']    = $tags['THREAD_URL'] . '#forumpost' . $post;
116
            $notificationHandler->triggerEvent('thread', $postsObject[$post]->getVar('topic_id'), 'new_post', $tags);
117
            $notificationHandler->triggerEvent('forum', $postsObject[$post]->getVar('forum_id'), 'new_post', $tags);
118
            $notificationHandler->triggerEvent('global', 0, 'new_post', $tags);
119
            $tags['POST_CONTENT'] = $postsObject[$post]->getVar('post_text');
120
            $tags['POST_NAME']    = $postsObject[$post]->getVar('subject');
121
            $notificationHandler->triggerEvent('global', 0, 'new_fullpost', $tags);
122
            $notificationHandler->triggerEvent('forum', $postsObject[$post]->getVar('forum_id'), 'new_fullpost', $tags);
123
        }
124
        break;
125
    case 'delete':
126
        $post_id = array_values($post_id);
127
        rsort($post_id);
128
        $topics = [];
129
        $forums = [];
130 View Code Duplication
        foreach ($post_id as $post) {
131
            $postObject = $postHandler->get($post);
132
            if (!empty($topic_id) && $topic_id !== $postObject->getVar('topic_id')) {
133
                continue;
134
            }
135
            $topics[$postObject->getVar('topic_id')] = 1;
136
            $forums[$postObject->getVar('forum_id')] = 1;
137
            $postHandler->delete($postObject, true);
138
            unset($postObject);
139
        }
140
        foreach (array_keys($topics) as $topic) {
141
            $topicHandler->synchronization($topic);
142
        }
143
        foreach (array_keys($forums) as $forum) {
144
            $forumHandler->synchronization($forum);
145
        }
146
        break;
147
    case 'split':
148
        /** @var \NewbbPost $postObject */
149
        $postObject = $postHandler->get($post_id);
150
        if (0 === count($post_id) || $postObject->isTopic()) {
151
            break;
152
        }
153
        $topic_id = $postObject->getVar('topic_id');
154
155
        $newtopic = $topicHandler->create();
156
        $newtopic->setVar('topic_title', $postObject->getVar('subject'), true);
157
        $newtopic->setVar('topic_poster', $postObject->getVar('uid'), true);
158
        $newtopic->setVar('forum_id', $postObject->getVar('forum_id'), true);
159
        $newtopic->setVar('topic_time', $postObject->getVar('post_time'), true);
160
        $newtopic->setVar('poster_name', $postObject->getVar('poster_name'), true);
161
        $newtopic->setVar('approved', 1, true);
162
        $topicHandler->insert($newtopic, true);
163
        $new_topic_id = $newtopic->getVar('topic_id');
164
165
        $pid = $postObject->getVar('pid');
166
167
        $postObject->setVar('topic_id', $new_topic_id, true);
168
        $postObject->setVar('pid', 0, true);
169
        $postHandler->insert($postObject);
170
171
        /* split a single post */
172
        if ($mode === 1) {
173
            $criteria = new CriteriaCompo(new Criteria('topic_id', $topic_id));
174
            $criteria->add(new Criteria('pid', $post_id));
175
            $postHandler->updateAll('pid', $pid, $criteria, true);
176
            /* split a post and its children posts */
177
        } elseif ($mode === 2) {
178
            include_once $GLOBALS['xoops']->path('class/xoopstree.php');
179
            $mytree = new XoopsTree($GLOBALS['xoopsDB']->prefix('newbb_posts'), 'post_id', 'pid');
180
            $posts  = $mytree->getAllChildId($post_id);
181
            if (count($posts) > 0) {
182
                $criteria = new Criteria('post_id', '(' . implode(',', $posts) . ')', 'IN');
183
                $postHandler->updateAll('topic_id', $new_topic_id, $criteria, true);
184
            }
185
            /* split a post and all posts coming after */
186
        } elseif ($mode === 3) {
187
            $criteria = new CriteriaCompo(new Criteria('topic_id', $topic_id));
188
            $criteria->add(new Criteria('post_id', $post_id, '>'));
189
            $postHandler->updateAll('topic_id', $new_topic_id, $criteria, true);
190
191
            unset($criteria);
192
            $criteria = new CriteriaCompo(new Criteria('topic_id', $new_topic_id));
193
            $criteria->add(new Criteria('post_id', $post_id, '>'));
194
            $postHandler->identifierName = 'pid';
195
            $posts                       = $postHandler->getList($criteria);
196
197
            unset($criteria);
198
            $post_update = [];
199
            foreach ($posts as $postid => $pid) {
200
                //                if (!in_array($pid, array_keys($posts))) {
201
                if (!array_key_exists($pid, $posts)) {
202
                    $post_update[] = $pid;
203
                }
204
                if (!array_key_exists($pid, $posts)) {
205
                    $post_update2[] = $pid;
206
                }
207
            }
208 View Code Duplication
            if (count($post_update)) {
209
                $criteria = new Criteria('post_id', '(' . implode(',', $post_update) . ')', 'IN');
210
                $postHandler->updateAll('pid', $post_id, $criteria, true);
211
            }
212
        }
213
214
        $forum_id = $postObject->getVar('forum_id');
215
        $topicHandler->synchronization($topic_id);
216
        $topicHandler->synchronization($new_topic_id);
217
        $sql    = sprintf('UPDATE "%s" SET forum_topics = forum_topics+1 WHERE forum_id = "%u"', $GLOBALS['xoopsDB']->prefix('newbb_forums'), $forum_id);
218
        $result = $GLOBALS['xoopsDB']->queryF($sql);
219
220
        break;
221
}
222
if (!empty($topic_id)) {
223
    redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?topic_id=$topic_id", 2, _MD_NEWBB_DBUPDATED);
224
} elseif (!empty($forum_id)) {
225
    redirect_header(XOOPS_URL . "/modules/newbb/viewforum.php?forum=$forum_id", 2, _MD_NEWBB_DBUPDATED);
226
} else {
227
    redirect_header(XOOPS_URL . "/modules/newbb/viewpost.php?uid=$uid", 2, _MD_NEWBB_DBUPDATED);
228
}
229
// irmtfan move to footer.php
230
include_once __DIR__ . '/footer.php';
231
include $GLOBALS['xoops']->path('footer.php');
232