Passed
Pull Request — master (#70)
by Pierre-Henry
03:15
created

PostHandler::getPostForPrint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Newbb;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 42 and the first side effect is on line 37.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
//
4
//  ------------------------------------------------------------------------ //
5
//                XOOPS - PHP Content Management System                      //
6
//                  Copyright (c) 2000-2016 XOOPS.org                        //
7
//                       <https://xoops.org/>                             //
8
//  ------------------------------------------------------------------------ //
9
//  This program is free software; you can redistribute it and/or modify     //
10
//  it under the terms of the GNU General Public License as published by     //
11
//  the Free Software Foundation; either version 2 of the License, or        //
12
//  (at your option) any later version.                                      //
13
//                                                                           //
14
//  You may not change or alter any portion of this comment or credits       //
15
//  of supporting developers from this source code or any supporting         //
16
//  source code which is considered copyrighted (c) material of the          //
17
//  original comment or credit authors.                                      //
18
//                                                                           //
19
//  This program is distributed in the hope that it will be useful,          //
20
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
21
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
22
//  GNU General Public License for more details.                             //
23
//                                                                           //
24
//  You should have received a copy of the GNU General Public License        //
25
//  along with this program; if not, write to the Free Software              //
26
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
27
//  ------------------------------------------------------------------------ //
28
//  Author: phppp (D.J., [email protected])                                  //
29
//  URL: https://xoops.org                                                    //
30
//  Project: Article Project                                                 //
31
//  ------------------------------------------------------------------------ //
32
33
use XoopsModules\Newbb;
34
35
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
37
defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
38
39
/**
40
 * Class PostHandler
41
 */
42
class PostHandler extends \XoopsPersistableObjectHandler
0 ignored issues
show
Bug introduced by
The type XoopsPersistableObjectHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
{
44
    /**
45
     * @param \XoopsDatabase $db
46
     */
47
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
    {
49
        parent::__construct($db, 'newbb_posts', Post::class, 'post_id', 'subject');
50
    }
51
52
    /**
53
     * @param  mixed $id
54
     * @param  null  $var
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $var is correct as it would always require null to be passed?
Loading history...
55
     * @return null|\XoopsObject
0 ignored issues
show
Bug introduced by
The type XoopsObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
     */
57
    public function get($id = null, $var = null) //get($id)
58
    {
59
        $id   = (int)$id;
60
        $post = null;
61
        $sql  = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' t ON p.post_id=t.post_id WHERE p.post_id=' . $id;
62
        if ($array = $this->db->fetchArray($this->db->query($sql))) {
63
            $post = $this->create(false);
64
            $post->assignVars($array);
65
        }
66
67
        return $post;
68
    }
69
70
    /**
71
     * @param  int             $limit
72
     * @param  int             $start
73
     * @param  \CriteriaElement $criteria
74
     * @param  null            $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
75
     * @param  bool            $asObject
76
     * @param  int             $topic_id
77
     * @param  int             $approved
78
     * @return array
79
     */
80
    //    public function getByLimit($topic_id, $limit, $approved = 1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
    public function &getByLimit(
82
        $limit = 0,
83
        $start = 0,
84
        \CriteriaElement $criteria = null,
0 ignored issues
show
Bug introduced by
The type CriteriaElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
85
        $fields = null,
86
        $asObject = true,
87
        $topic_id = 0,
88
        $approved = 1
89
    ) {
90
        $sql    = 'SELECT p.*, t.*, tp.topic_status FROM '
91
                  . $this->db->prefix('newbb_posts')
92
                  . ' p LEFT JOIN '
93
                  . $this->db->prefix('newbb_posts_text')
94
                  . ' t ON p.post_id=t.post_id LEFT JOIN '
95
                  . $this->db->prefix('newbb_topics')
96
                  . ' tp ON tp.topic_id=p.topic_id WHERE p.topic_id='
97
                  . $topic_id
98
                  . ' AND p.approved ='
99
                  . $approved
100
                  . ' ORDER BY p.post_time DESC';
101
        $result = $this->db->query($sql, $limit, 0);
102
        $ret    = [];
103
        while (false !== ($myrow = $this->db->fetchArray($result))) {
104
            $post = $this->create(false);
105
            $post->assignVars($myrow);
106
107
            $ret[$myrow['post_id']] = $post;
108
            unset($post);
109
        }
110
111
        return $ret;
112
    }
113
114
    /**
115
     * @param Post $post
116
     * @return mixed
117
     */
118
    public function getPostForPDF(&$post)
119
    {
120
        return $post->getPostBody(true);
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Newbb\Post::getPostBody() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
        return $post->/** @scrutinizer ignore-call */ getPostBody(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
121
    }
122
123
    /**
124
     * @param Post $post
125
     * @return mixed
126
     */
127
    public function getPostForPrint(&$post)
128
    {
129
        return $post->getPostBody();
130
    }
131
132
    /**
133
     * @param  int|Post $post
134
     * @param  bool $force
135
     * @return bool
136
     */
137
    public function approve(&$post, $force = false)
138
    {
139
        if (empty($post)) {
140
            return false;
141
        }
142
        if (is_numeric($post)) {
143
            $post = $this->get($post);
144
        }
145
146
        $wasApproved = $post->getVar('approved');
147
        // irmtfan approve post if the approved = 0 (pending) or -1 (deleted)
148
        if (empty($force) && $wasApproved > 0) {
149
            return true;
150
        }
151
        $post->setVar('approved', 1);
152
        $this->insert($post, true);
153
154
        /** @var Newbb\TopicHandler $topicHandler */
155
        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
156
        $topicObject  = $topicHandler->get($post->getVar('topic_id'));
157
        if ($topicObject->getVar('topic_last_post_id') < $post->getVar('post_id')) {
158
            $topicObject->setVar('topic_last_post_id', $post->getVar('post_id'));
159
        }
160
        if ($post->isTopic()) {
161
            $topicObject->setVar('approved', 1);
162
        } else {
163
            $topicObject->setVar('topic_replies', $topicObject->getVar('topic_replies') + 1);
164
        }
165
        $topicHandler->insert($topicObject, true);
166
167
        /** @var Newbb\ForumHandler $forumHandler */
168
        $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
169
        $forumObject  = $forumHandler->get($post->getVar('forum_id'));
170
        if ($forumObject->getVar('forum_last_post_id') < $post->getVar('post_id')) {
171
            $forumObject->setVar('forum_last_post_id', $post->getVar('post_id'));
172
        }
173
        $forumObject->setVar('forum_posts', $forumObject->getVar('forum_posts') + 1);
174
        if ($post->isTopic()) {
175
            $forumObject->setVar('forum_topics', $forumObject->getVar('forum_topics') + 1);
176
        }
177
        $forumHandler->insert($forumObject, true);
178
179
        // Update user stats
180
        if ($post->getVar('uid') > 0) {
181
            /** @var \XoopsMemberHandler $memberHandler */
182
            $memberHandler = xoops_getHandler('member');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
            $memberHandler = /** @scrutinizer ignore-call */ xoops_getHandler('member');
Loading history...
183
            $poster        = $memberHandler->getUser($post->getVar('uid'));
184
            if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
185
                $poster->setVar('posts', $poster->getVar('posts') + 1);
186
                $res = $memberHandler->insertUser($poster, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
187
                unset($poster);
188
            }
189
        }
190
191
        // Update forum stats
192
        /** @var StatsHandler $statsHandler */
193
        $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats');
194
        $statsHandler->update($post->getVar('forum_id'), 'post');
195
        if ($post->isTopic()) {
196
            $statsHandler->update($post->getVar('forum_id'), 'topic');
197
        }
198
199
        return true;
200
    }
201
202
    /**
203
     * @param \XoopsObject $post
204
     * @param  bool        $force
205
     * @return bool
206
     */
207
    public function insert(\XoopsObject $post, $force = true) //insert(&$post, $force = true)
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
208
    {
209
        $topicObject = null;
210
        // Set the post time
211
        // The time should be "publish" time. To be adjusted later
212
        if (!$post->getVar('post_time')) {
213
            $post->setVar('post_time', time());
214
        }
215
216
        /** @var Newbb\TopicHandler $topicHandler */
217
        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
218
        // Verify the topic ID
219
        if ($topic_id = $post->getVar('topic_id')) {
220
            $topicObject = $topicHandler->get($topic_id);
221
            // Invalid topic OR the topic is no approved and the post is not top post
222
            if (!$topicObject//    || (!$post->isTopic() && $topicObject->getVar("approved") < 1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
223
            ) {
224
                return false;
225
            }
226
        }
227
        if (empty($topic_id)) {
228
            $post->setVar('topic_id', 0);
229
            $post->setVar('pid', 0);
230
            $post->setNew();
231
            $topicObject = $topicHandler->create();
232
        }
233
        $textHandler    = Newbb\Helper::getInstance()->getHandler('Text');
234
        $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr'];
235
        if ($post->isNew()) {
236
            if (!$topic_id = $post->getVar('topic_id')) {
237
                $topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
238
                $topicObject->setVar('topic_poster', $post->getVar('uid'));
239
                $topicObject->setVar('forum_id', $post->getVar('forum_id'));
240
                $topicObject->setVar('topic_time', $post->getVar('post_time'));
241
                $topicObject->setVar('poster_name', $post->getVar('poster_name'));
242
                $topicObject->setVar('approved', $post->getVar('approved'));
243
244
                if (!$topic_id = $topicHandler->insert($topicObject, $force)) {
245
                    $post->deleteAttachment();
246
                    $post->setErrors('insert topic error');
247
248
                    //xoops_error($topicObject->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
249
                    return false;
250
                }
251
                $post->setVar('topic_id', $topic_id);
252
253
                $pid = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $pid is dead and can be removed.
Loading history...
254
                $post->setVar('pid', 0);
255
            } elseif (!$post->getVar('pid')) {
256
                $pid = $topicHandler->getTopPostId($topic_id);
257
                $post->setVar('pid', $pid);
258
            }
259
260
            $textObject = $textHandler->create();
261
            foreach ($post_text_vars as $key) {
262
                $textObject->vars[$key] = $post->vars[$key];
263
            }
264
            $post->destroyVars($post_text_vars);
265
266
            //            if (!$post_id = parent::insert($post, $force)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
267
            //                return false;
268
            //            }
269
270
            if (!$post_id = parent::insert($post, $force)) {
271
                return false;
272
            } else {
273
                $post->unsetNew();
274
            }
275
276
            $textObject->setVar('post_id', $post_id);
277
            if (!$textHandler->insert($textObject, $force)) {
278
                $this->delete($post);
279
                $post->setErrors('post text insert error');
280
281
                //xoops_error($textObject->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
282
                return false;
283
            }
284
            if ($post->getVar('approved') > 0) {
285
                $this->approve($post, true);
286
            }
287
            $post->setVar('post_id', $post_id);
288
        } else {
289
            if ($post->isTopic()) {
290
                if ($post->getVar('subject') !== $topicObject->getVar('topic_title')) {
291
                    $topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
292
                }
293
                if ($post->getVar('approved') !== $topicObject->getVar('approved')) {
294
                    $topicObject->setVar('approved', $post->getVar('approved'));
295
                }
296
                $topicObject->setDirty();
297
                if (!$result = $topicHandler->insert($topicObject, $force)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
298
                    $post->setErrors('update topic error');
299
300
                    //xoops_error($topicObject->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
301
                    return false;
302
                }
303
            }
304
            $textObject = $textHandler->get($post->getVar('post_id'));
305
            $textObject->setDirty();
306
            foreach ($post_text_vars as $key) {
307
                $textObject->vars[$key] = $post->vars[$key];
308
            }
309
            $post->destroyVars($post_text_vars);
310
            if (!$post_id = parent::insert($post, $force)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $post_id is dead and can be removed.
Loading history...
311
                //xoops_error($post->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
312
                return false;
313
            } else {
314
                $post->unsetNew();
315
            }
316
            if (!$textHandler->insert($textObject, $force)) {
317
                $post->setErrors('update post text error');
318
319
                //xoops_error($textObject->getErrors());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
320
                return false;
321
            }
322
        }
323
324
        return $post->getVar('post_id');
325
    }
326
327
    /**
328
     * @param \XoopsObject $post
329
     * @param  bool        $isDeleteOne
330
     * @param  bool        $force
331
     * @return bool
332
     */
333
    public function delete(\XoopsObject $post, $isDeleteOne = true, $force = false)
334
    {
335
        if (!is_object($post) || 0 == $post->getVar('post_id')) {
336
            return false;
337
        }
338
339
        if ($isDeleteOne) {
340
            if ($post->isTopic()) {
341
                $criteria = new \CriteriaCompo(new \Criteria('topic_id', $post->getVar('topic_id')));
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
342
                $criteria->add(new \Criteria('approved', 1));
343
                $criteria->add(new \Criteria('pid', 0, '>'));
344
                if ($this->getPostCount($criteria) > 0) {
345
                    return false;
346
                }
347
            }
348
349
            return $this->myDelete($post, $force);
350
        } else {
351
            require_once $GLOBALS['xoops']->path('class/xoopstree.php');
352
            $mytree = new \XoopsTree($this->db->prefix('newbb_posts'), 'post_id', 'pid');
0 ignored issues
show
Bug introduced by
The type XoopsTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
353
            $arr    = $mytree->getAllChild($post->getVar('post_id'));
354
            // irmtfan - delete childs in a reverse order
355
            for ($i = count($arr) - 1; $i >= 0; $i--) {
356
                $childpost = $this->create(false);
357
                $childpost->assignVars($arr[$i]);
358
                $this->myDelete($childpost, $force);
359
                unset($childpost);
360
            }
361
            $this->myDelete($post, $force);
362
        }
363
364
        return true;
365
    }
366
367
    /**
368
     * @param  Post $post
369
     * @param  bool $force
370
     * @return bool
371
     */
372
    public function myDelete(Post $post, $force = false)
373
    {
374
        global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
375
376
        if (!is_object($post) || 0 == $post->getVar('post_id')) {
377
            return false;
378
        }
379
380
        /* Set active post as deleted */
381
        if ($post->getVar('approved') > 0 && empty($force)) {
382
            $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id');
383
            if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
384
            }
385
            /* delete pending post directly */
386
        } else {
387
            $sql = sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts'), $post->getVar('post_id'));
388
            if (!$result = $this->db->queryF($sql)) {
389
                $post->setErrors('delete post error: ' . $sql);
390
391
                return false;
392
            }
393
            $post->deleteAttachment();
394
395
            $sql = sprintf('DELETE FROM %s WHERE post_id = %u', $this->db->prefix('newbb_posts_text'), $post->getVar('post_id'));
396
            if (!$result = $this->db->queryF($sql)) {
397
                $post->setErrors('Could not remove post text: ' . $sql);
398
399
                return false;
400
            }
401
        }
402
403
        if ($post->isTopic()) {
404
            $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
405
            /** @var Topic $topicObject */
406
            $topicObject = $topicHandler->get($post->getVar('topic_id'));
407
            if (is_object($topicObject) && $topicObject->getVar('approved') > 0 && empty($force)) {
408
                $topiccount_toupdate = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $topiccount_toupdate is dead and can be removed.
Loading history...
409
                $topicObject->setVar('approved', -1);
410
                $topicHandler->insert($topicObject);
411
                xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
0 ignored issues
show
Bug introduced by
The function xoops_notification_deletebyitem was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

411
                /** @scrutinizer ignore-call */ 
412
                xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
Loading history...
412
            } else {
413
                if (is_object($topicObject)) {
414
                    if ($topicObject->getVar('approved') > 0) {
415
                        xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
416
                    }
417
418
                    $poll_id = $topicObject->getVar('poll_id');
419
                    // START irmtfan poll_module
420
                    $topicObject->deletePoll($poll_id);
421
                    // END irmtfan poll_module
422
                }
423
424
                $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->db->prefix('newbb_topics'), $post->getVar('topic_id'));
425
                if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
426
                    //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
427
                }
428
                $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->db->prefix('newbb_votedata'), $post->getVar('topic_id'));
429
                if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
430
                    //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
431
                }
432
            }
433
        } else {
434
            $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . ' t
435
                            LEFT JOIN ' . $this->db->prefix('newbb_posts') . ' p ON p.topic_id = t.topic_id
436
                            SET t.topic_last_post_id = p.post_id
437
                            WHERE t.topic_last_post_id = ' . $post->getVar('post_id') . '
438
                                    AND p.post_id = (SELECT MAX(post_id) FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id=t.topic_id)';
439
            if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
440
            }
441
        }
442
443
        $postcount_toupdate = $post->getVar('approved');
444
445
        if ($postcount_toupdate > 0) {
446
447
            // Update user stats
448
            if ($post->getVar('uid') > 0) {
449
                /** @var \XoopsMemberHandler $memberHandler */
450
                $memberHandler = xoops_getHandler('member');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

450
                $memberHandler = /** @scrutinizer ignore-call */ xoops_getHandler('member');
Loading history...
451
                $poster        = $memberHandler->getUser($post->getVar('uid'));
452
                if (is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
453
                    $poster->setVar('posts', $poster->getVar('posts') - 1);
454
                    $res = $memberHandler->insertUser($poster, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
455
                    unset($poster);
456
                }
457
            }
458
            // irmtfan - just update the pid for approved posts when the post is not topic (pid=0)
459
            if (!$post->isTopic()) {
460
                $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id');
461
                if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
462
                    //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
463
                }
464
            }
465
        }
466
467
        return true;
468
    }
469
470
    // START irmtfan enhance getPostCount when there is join (read_mode = 2)
471
472
    /**
473
     * @param  null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
474
     * @param  null $join
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $join is correct as it would always require null to be passed?
Loading history...
475
     * @return int|null
476
     */
477
    public function getPostCount($criteria = null, $join = null)
478
    {
479
        // if not join get the count from XOOPS/class/model/stats as before
480
        if (empty($join)) {
481
            return parent::getCount($criteria);
482
        }
483
484
        $sql = 'SELECT COUNT(*) as count' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
485
        // LEFT JOIN
486
        $sql .= $join;
487
        // WHERE
488
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
489
            $sql .= ' ' . $criteria->renderWhere();
490
        }
491
        if (!$result = $this->db->query($sql)) {
492
            //xoops_error($this->db->error().'<br>'.$sql);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
493
            return null;
494
        }
495
        $myrow = $this->db->fetchArray($result);
496
        $count = $myrow['count'];
497
498
        return $count;
499
    }
500
    // END irmtfan enhance getPostCount when there is join (read_mode = 2)
501
    /*
502
     * TODO: combining viewtopic.php
503
     */
504
    /**
505
     * @param  null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
506
     * @param  int  $limit
507
     * @param  int  $start
508
     * @param  null $join
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $join is correct as it would always require null to be passed?
Loading history...
509
     * @return array
510
     */
511
    public function getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null)
512
    {
513
        $ret = [];
514
        $sql = 'SELECT p.*, t.* ' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
515
        if (!empty($join)) {
516
            $sql .= $join;
517
        }
518
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
519
            $sql .= ' ' . $criteria->renderWhere();
520
            if ('' !== $criteria->getSort()) {
521
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
522
            }
523
        }
524
        $result = $this->db->query($sql, (int)$limit, (int)$start);
525
        if (!$result) {
526
            //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
527
            return $ret;
528
        }
529
        while (false !== ($myrow = $this->db->fetchArray($result))) {
530
            $post = $this->create(false);
531
            $post->assignVars($myrow);
532
            $ret[$myrow['post_id']] = $post;
533
            unset($post);
534
        }
535
536
        return $ret;
537
    }
538
539
    /**
540
     * @return bool
541
     */
542
    public function synchronization()
543
    {
544
        //$this->cleanOrphan();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
545
        return true;
546
    }
547
548
    /**
549
     * clean orphan items from database
550
     *
551
     * @param  string $table_link
552
     * @param  string $field_link
553
     * @param  string $field_object
554
     * @return bool   true on success
555
     */
556
    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
557
    {
558
        $this->deleteAll(new \Criteria('post_time', 0), true, true);
559
        parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id');
560
        parent::cleanOrphan($this->db->prefix('newbb_posts_text'), 'post_id');
561
562
        $sql = 'DELETE FROM ' . $this->db->prefix('newbb_posts_text') . ' WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )';
563
        if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
564
            //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
565
            return false;
566
        }
567
568
        return true;
569
    }
570
571
    /**
572
     * clean expired objects from database
573
     *
574
     * @param  int $expire time limit for expiration
575
     * @return bool true on success
576
     */
577
    public function cleanExpires($expire = 0)
578
    {
579
        // irmtfan if 0 no cleanup look include/plugin.php
580
        if (!func_num_args()) {
581
            $newbbConfig = newbbLoadConfig();
582
            $expire      = isset($newbbConfig['pending_expire']) ? (int)$newbbConfig['pending_expire'] : 7;
583
            $expire      = $expire * 24 * 3600; // days to seconds
584
        }
585
        if (empty($expire)) {
586
            return false;
587
        }
588
        $crit_expire = new \CriteriaCompo(new \Criteria('approved', 0, '<='));
589
        //if (!empty($expire)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
590
        $crit_expire->add(new \Criteria('post_time', time() - (int)$expire, '<'));
591
592
        //}
593
        return $this->deleteAll($crit_expire, true/*, true*/);
594
    }
595
}
596