Completed
Branch master (4e8684)
by Michael
05:31 queued 02:57
created

topicmanager.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
use Xmf\Request;
4
5
//
6
//  ------------------------------------------------------------------------ //
7
//                XOOPS - PHP Content Management System                      //
8
//                  Copyright (c) 2000-2016 XOOPS.org                        //
9
//                       <http://xoops.org/>                             //
10
//  ------------------------------------------------------------------------ //
11
//  This program is free software; you can redistribute it and/or modify     //
12
//  it under the terms of the GNU General Public License as published by     //
13
//  the Free Software Foundation; either version 2 of the License, or        //
14
//  (at your option) any later version.                                      //
15
//                                                                           //
16
//  You may not change or alter any portion of this comment or credits       //
17
//  of supporting developers from this source code or any supporting         //
18
//  source code which is considered copyrighted (c) material of the          //
19
//  original comment or credit authors.                                      //
20
//                                                                           //
21
//  This program is distributed in the hope that it will be useful,          //
22
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
23
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
24
//  GNU General Public License for more details.                             //
25
//                                                                           //
26
//  You should have received a copy of the GNU General Public License        //
27
//  along with this program; if not, write to the Free Software              //
28
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
29
//  ------------------------------------------------------------------------ //
30
//  Author: phppp (D.J., [email protected])                                  //
31
//  URL: http://xoops.org                                                    //
32
//  Project: Article Project                                                 //
33
//  ------------------------------------------------------------------------ //
34
include_once __DIR__ . '/header.php';
35
36
if (Request::getString('submit', '', 'POST')) {
37
    foreach (['forum', 'newforum', 'newtopic'] as $getint) {
38
        ${$getint} = Request::getInt($getint, 0, 'POST');// (int)(@$_POST[$getint]);
39
    }
40
    foreach (['topic_id'] as $getint) {
41
        ${$getint} = Request::getInt($getint, 0, 'POST');// (int)(@$_POST[$getint]);
42
    }
43
    if (!is_array($topic_id)) {
44
        $topic_id = [$topic_id];
45
    }
46
} else {
47
    foreach (['forum', 'topic_id'] as $getint) {
48
        ${$getint} = Request::getInt($getint, 0, 'GET');// (int)(@$_GET[$getint]);
49
    }
50
}
51
52 View Code Duplication
if (!$topic_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $topic_id of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
    $redirect = empty($forum_id) ? 'index.php' : 'viewforum.php?forum={$forum}';
54
    $redirect = XOOPS_URL . '/modules/newbb/' . $redirect;
55
    redirect_header($redirect, 2, _MD_ERRORTOPIC);
56
}
57
58
/** @var \NewbbTopicHandler $topicHandler */
59
$topicHandler = xoops_getModuleHandler('topic', 'newbb');
60
/** @var \NewbbForumHandler $forumHandler */
61
$forumHandler = xoops_getModuleHandler('forum', 'newbb');
62
63
if (!$forum) {
64
    /** @var \Topic $topic_obj */
65
    $topic_obj = $topicHandler->get((int)$topic_id);
66
    if (is_object($topic_obj)) {
67
        $forum = $topic_obj->getVar('forum_id');
68
    } else {
69
        $redirect = XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $topic_id;
70
        redirect_header($redirect, 2, _MD_FORUMNOEXIST);
71
    }
72
    unset($topic_obj);
73
}
74
75
if ($GLOBALS['xoopsModuleConfig']['wol_enabled']) {
76
    /** @var \NewbbOnlineHandler $onlineHandler */
77
    $onlineHandler = xoops_getModuleHandler('online', 'newbb');
78
    $onlineHandler->init($forum);
79
}
80
// irmtfan add restore to viewtopic
81
$action_array = [
82
    'merge',
83
    'delete',
84
    'restore',
85
    'move',
86
    'lock',
87
    'unlock',
88
    'sticky',
89
    'unsticky',
90
    'digest',
91
    'undigest'
92
];
93
foreach ($action_array as $_action) {
94
    $action[$_action] = [
95
        'name'   => $_action,
96
        'desc'   => constant(strtoupper("_MD_DESC_{$_action}")),
97
        'submit' => constant(strtoupper("_MD_{$_action}")),
98
        'sql'    => "topic_{$_action}=1",
99
        'msg'    => constant(strtoupper("_MD_TOPIC{$_action}"))
100
    ];
101
}
102
$action['lock']['sql']     = 'topic_status = 1';
103
$action['unlock']['sql']   = 'topic_status = 0';
104
$action['unsticky']['sql'] = 'topic_sticky = 0';
105
$action['undigest']['sql'] = 'topic_digest = 0';
106
$action['digest']['sql']   = 'topic_digest = 1, digest_time = ' . time();
107
108
// Disable cache
109
$GLOBALS['xoopsConfig']['module_cache'][$xoopsModule->getVar('mid')] = 0;
110
// irmtfan include header.php after defining $xoopsOption['template_main']
111
include_once $GLOBALS['xoops']->path('header.php');
112
113
if (Request::getString('submit', '', 'POST')) {
114
    $mode = Request::getString('mode', '', 'POST');// $_POST['mode'];
115
116
    if ('delete' === $mode) {
117
        foreach ($topic_id as $tid) {
118
            $topic_obj = $topicHandler->get($tid);
119
            $topicHandler->delete($topic_obj, false);
120
            // irmtfan - sync topic after delete
121
            $topicHandler->synchronization($topic_obj);
122
            $forumHandler->synchronization($forum);
123
            //$topic_obj->loadFilters("delete");
124
            //sync($topic_id, "topic");
125
            //xoops_notification_deletebyitem ($xoopsModule->getVar('mid'), 'thread', $topic_id);
126
        }
127
        // irmtfan full URL
128
        echo $action[$mode]['msg'] . "<p><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/viewforum.php?forum=$forum'>" . _MD_RETURNTOTHEFORUM . "</a></p><p><a href='index.php'>" . _MD_RETURNFORUMINDEX . '</a></p>';
129
    } elseif ('restore' === $mode) {
130
        //$topicHandler = xoops_getModuleHandler('topic', 'newbb');
131
        $forums     = [];
132
        $topics_obj = $topicHandler->getAll(new Criteria('topic_id', '(' . implode(',', $topic_id) . ')', 'IN'));
133
        foreach (array_keys($topics_obj) as $id) {
134
            $topic_obj = $topics_obj[$id];
135
            $topicHandler->approve($topic_obj);
136
            $topicHandler->synchronization($topic_obj);
137
            $forums[$topic_obj->getVar('forum_id')] = 1;
138
        }
139
        //irmtfan remove - no need to approve posts manually - see class/post.php approve function
140
        $criteria_forum = new Criteria('forum_id', '(' . implode(',', array_keys($forums)) . ')', 'IN');
141
        $forums_obj     = $forumHandler->getAll($criteria_forum);
142
        foreach (array_keys($forums_obj) as $id) {
143
            $forumHandler->synchronization($forums_obj[$id]);
144
        }
145
        unset($topics_obj, $forums_obj);
146
        // irmtfan add restore to viewtopic
147
        $restoretopic_id = $topic_obj->getVar('topic_id');
148
        // irmtfan / missing in URL
149
        echo $action[$mode]['msg'] . "<p><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/viewtopic.php?topic_id=$restoretopic_id'>" . _MD_VIEWTHETOPIC . '</a></p>' . "<p><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname')
150
             . "/viewforum.php?forum=$forum'>" . _MD_RETURNTOTHEFORUM . '</a></p>' . "<p><a href='index.php'>" . _MD_RETURNFORUMINDEX . '</a></p>';
151
    } elseif ('merge' === $mode) {
152
        /** @var NewbbPostHandler $postHandler */
153
        $postHandler = xoops_getModuleHandler('post', 'newbb');
154
        /** @var \NewbbRateHandler $rateHandler */
155
        $rateHandler = xoops_getModuleHandler('rate', 'newbb');
156
157
        foreach ($topic_id as $tid) {
158
            $topic_obj    = $topicHandler->get($tid);
159
            $newtopic_obj = $topicHandler->get($newtopic);
160
161
            /* return false if destination topic is not existing */
162
            // irmtfan bug fix: the old topic will be deleted if user input a not exist new topic
163
            if (!is_object($newtopic_obj)) {
164
                $redirect = XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $tid;
165
                redirect_header($redirect, 2, _MD_ERRORTOPIC);
166
            }
167
            $criteria_topic = new Criteria('topic_id', $tid);
168
            $criteria       = new CriteriaCompo($criteria_topic);
169
            $criteria->add(new Criteria('pid', 0));
170
            // irmtfan OR change to this for less query?:
171
            // $postHandler->updateAll("pid", $newtopic_obj->getVar("topic_last_post_id"), $criteria, true);
172
            $postHandler->updateAll('pid', $topicHandler->getTopPostId($newtopic), $criteria, true);
173
            $postHandler->updateAll('topic_id', $newtopic, $criteria_topic, true);
174
            // irmtfan update vote data instead of deleting them
175
            $rateHandler->updateAll('topic_id', $newtopic, $criteria_topic, true);
176
177
            $topic_views = $topic_obj->getVar('topic_views') + $newtopic_obj->getVar('topic_views');
178
            // irmtfan better method to update topic_views in new topic
179
            //$criteria_newtopic = new Criteria('topic_id', $newtopic);
180
            //$topicHandler->updateAll('topic_views', $topic_views, $criteria_newtopic, true);
181
            $newtopic_obj->setVar('topic_views', $topic_views);
182
            // START irmtfan poll_module and rewrite the method
183
            // irmtfan only move poll in old topic to new topic if new topic has not a poll
184
            $poll_id = $topic_obj->getVar('poll_id');
185
            if ($poll_id > 0 && ($newtopic_obj->getVar('poll_id') == 0)) {
186
                $newtopic_obj->setVar('topic_haspoll', 1);
187
                $newtopic_obj->setVar('poll_id', $poll_id);
188
                $poll_id = 0;// set to not delete the poll
189
                $topic_obj->setVar('topic_haspoll', 0); // set to not delete the poll
190
                $topic_obj->setVar('poll_id', 0);// set to not delete the poll
191
            }
192
            //update and sync newtopic after merge
193
            //$topicHandler->insert($newtopic_obj, true);
194
            $topicHandler->synchronization($newtopic_obj); // very important: only use object
195
            //sync newforum after merge
196
            $newforum = $newtopic_obj->getVar('forum_id');
197
            $forumHandler->synchronization($newforum);
198
            //hardcode remove force to delete old topic from database
199
            //$topicHandler->delete($topic_obj,true); // cannot use this
200
            $topicHandler->deleteAll($criteria_topic, true); // $force = true
201
            //delete poll if old topic had a poll
202
            $topic_obj->deletePoll($poll_id);
203
            //sync forum after delete old topic
204
            $forumHandler->synchronization($forum);
205
            // END irmtfan poll_module and rewrite the method
206
        }
207
        echo $action[$mode]['msg'] . // irmtfan full URL
208
             "<p><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/viewtopic.php?topic_id=$newtopic'>" . _MD_VIEWTHETOPIC . '</a></p>' . "<p><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/viewforum.php?forum=$forum'>" . _MD_RETURNTOTHEFORUM
209
             . '</a></p>' . "<p><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/index.php'>" . _MD_RETURNFORUMINDEX . '</a></p>';
210
    } elseif ('move' === $mode) {
211
        if ($newforum > 0) {
212
            $topic_id  = $topic_id[0];
213
            $topic_obj = $topicHandler->get($topic_id);
214
            $topic_obj->loadFilters('update');
215
            $topic_obj->setVar('forum_id', $newforum, true);
216
            $topicHandler->insert($topic_obj, true);
217
            $topic_obj->loadFilters('update');
218
219
            $sql = sprintf('UPDATE %s SET forum_id = %u WHERE topic_id = %u', $GLOBALS['xoopsDB']->prefix('bb_posts'), $newforum, $topic_id);
220
            if (!$r = $GLOBALS['xoopsDB']->query($sql)) {
221
                return false;
222
            }
223
            $forumHandler->synchronization($forum);
224
            $forumHandler->synchronization($newforum);
225
            // irmtfan full URL
226
            echo $action[$mode]['msg'] . "<p><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/viewtopic.php?topic_id=$topic_id&amp;forum=$newforum'>" . _MD_GOTONEWFORUM . "</a></p><p><a href='" . XOOPS_URL . "/modules/newbb/index.php'>" . _MD_RETURNFORUMINDEX . '</a></p>';
227
        } else {
228
            // irmtfan - issue with javascript:history.go(-1)
229
            redirect_header($_SERVER['HTTP_REFERER'], 2, _MD_ERRORFORUM);
230
        }
231
    } else {
232
        $topic_id  = $topic_id[0];
233
        $forum     = $topicHandler->get($topic_id, 'forum_id');
234
        $forum_new = !empty($newtopic) ? $topicHandler->get($newtopic, 'forum_id') : 0;
235
236
        if (!$forumHandler->getPermission($forum, 'moderate')
237
            || (!empty($forum_new)
238
                && !$forumHandler->getPermission($forum_new, 'reply'))        // The forum for the topic to be merged to
239
            || (!empty($newforum) && !$forumHandler->getPermission($newforum, 'post')) // The forum to be moved to
240
        ) {
241
            redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?forum=$forum&amp;topic_id=$topic_id", 2, _NOPERM);
242
        }
243
244
        if (!empty($action[$mode]['sql'])) {
245
            $sql = sprintf('UPDATE %s SET ' . $action[$mode]['sql'] . ' WHERE topic_id = %u', $GLOBALS['xoopsDB']->prefix('bb_topics'), $topic_id);
246
            if (!$r = $GLOBALS['xoopsDB']->query($sql)) {
247
                redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?forum=$forum&amp;topic_id=$topic_id&amp;order=$order&amp;viewmode=$viewmode", 2, _MD_ERROR_BACK . '<br>sql:' . $sql);
248
            }
249
        } else {
250
            redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?forum=$forum&amp;topic_id=$topic_id", 2, _MD_ERROR_BACK);
251
        }
252
        if ('digest' === $mode && $GLOBALS['xoopsDB']->getAffectedRows()) {
253
            $topic_obj    = $topicHandler->get($topic_id);
254
            /** @var \NewbbStatsHandler $statsHandler */
255
            $statsHandler = xoops_getModuleHandler('stats', 'newbb');
256
            $statsHandler->update($topic_obj->getVar('forum_id'), 'digest');
257
            $userstatsHandler = xoops_getModuleHandler('userstats', 'newbb');
258
            if ($user_stat = $userstatsHandler->get($topic_obj->getVar('topic_poster'))) {
259
                $z = $user_stat->getVar('user_digests') + 1;
260
                $user_stat->setVar('user_digests', (int)$z);
261
                $userstatsHandler->insert($user_stat);
262
            }
263
        }
264
        // irmtfan full URL
265
        echo $action[$mode]['msg'] . "<p><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/viewtopic.php?topic_id=$topic_id&amp;forum=$forum'>" . _MD_VIEWTHETOPIC . "</a></p><p><a href='" . XOOPS_URL . "/modules/newbb/viewforum.php?forum=$forum'>" . _MD_RETURNFORUMINDEX
266
             . '</a></p>';
267
    }
268
} else {  // No submit
269
    $mode = Request::getString('mode', '', 'GET'); //$_GET['mode'];
270
    echo "<form action='" . Request::getString('PHP_SELF', '', 'SERVER') . "' method='post'>";
271
    echo "<table border='0' cellpadding='1' cellspacing='0' align='center' width='95%'>";
272
    echo "<tr><td class='bg2'>";
273
    echo "<table border='0' cellpadding='1' cellspacing='1' width='100%'>";
274
    echo "<tr class='bg3' align='left'>";
275
    echo "<td colspan='2' align='center'>" . $action[$mode]['desc'] . '</td></tr>';
276
277
    if ($mode === 'move') {
278
        echo '<tr><td class="bg3">' . _MD_MOVETOPICTO . '</td><td class="bg1">';
279
        $box = '<select name="newforum" size="1">';
280
281
        /** @var \NewbbCategoryHandler $categoryHandler */
282
        $categoryHandler = xoops_getModuleHandler('category', 'newbb');
283
        $categories      = $categoryHandler->getByPermission('access');
284
        $forums          = $forumHandler->getForumsByCategory(array_keys($categories), 'post', false);
285
286 View Code Duplication
        if (count($categories) > 0 && count($forums) > 0) {
287
            foreach (array_keys($forums) as $key) {
288
                /** @var \NewbbCategory[] $categories */
289
                $box .= "<option value='-1'>[" . $categories[$key]->getVar('cat_title') . ']</option>';
290
                foreach ($forums[$key] as $forumid => $_forum) {
291
                    $box .= "<option value='" . $forumid . "'>-- " . $_forum['title'] . '</option>';
292
                    if (!isset($_forum['sub'])) {
293
                        continue;
294
                    }
295
                    foreach (array_keys($_forum['sub']) as $fid) {
296
                        $box .= "<option value='" . $fid . "'>---- " . $_forum['sub'][$fid]['title'] . '</option>';
297
                    }
298
                }
299
            }
300
        } else {
301
            $box .= "<option value='-1'>" . _MD_NOFORUMINDB . '</option>';
302
        }
303
        unset($forums, $categories);
304
305
        echo $box;
306
        echo '</select></td></tr>';
307
    }
308
    if ($mode === 'merge') {
309
        echo '<tr><td class="bg3">' . _MD_MERGETOPICTO . '</td><td class="bg1">';
310
        echo _MD_TOPIC . "ID-$topic_id -> ID: <input name='newtopic' value='' />";
311
        echo '</td></tr>';
312
    }
313
    echo '<tr class="bg3"><td colspan="2" align="center">';
314
    echo "<input type='hidden' name='mode' value='" . $action[$mode]['name'] . "' />";
315
    echo "<input type='hidden' name='topic_id' value='" . $topic_id . "' />";
316
    echo "<input type='hidden' name='forum' value='" . $forum . "' />";
317
    echo "<input type='submit' name='submit' value='" . $action[$mode]['submit'] . "' />";
318
    echo '</td></tr></form></table></td></tr></table>';
319
}
320
// irmtfan move to footer.php
321
include_once __DIR__ . '/footer.php';
322
include $GLOBALS['xoops']->path('footer.php');
323