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

TopicRenderer::parseVars()   B

Complexity

Conditions 8
Paths 25

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 25
nop 0
dl 0
loc 29
rs 8.2114
c 0
b 0
f 0
1
<?php namespace XoopsModules\Newbb;
2
3
/**
4
 * NewBB 5.0x,  the forum module for XOOPS project
5
 *
6
 * @copyright      XOOPS Project (https://xoops.org)
7
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
8
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
9
 * @since          4.00
10
 * @package        module::newbb
11
 */
12
13
use Xmf\Request;
14
use XoopsModules\Newbb;
15
16
// 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...
17
18
/**
19
 * Topic Renderer
20
 *
21
 * @author    D.J. (phppp)
22
 * @copyright copyright &copy; Xoops Project
23
 * @package   module::newbb
24
 *
25
 */
26
class TopicRenderer
27
{
28
    public $vars = [];
29
30
    /**
31
     * reference to moduleConfig
32
     */
33
    public $config;
34
35
    /**
36
     * Current user has no access to current page
37
     */
38
    private $noperm = false;
39
40
    /**
41
     * For multiple forums
42
     */
43
    public $is_multiple = false;
44
45
    /**
46
     * force to parse vars (run against static vars) irmtfan
47
     */
48
    public $force = false;
49
50
    /**
51
     * Vistitor's level: 0 - anonymous; 1 - user; 2 - moderator or admin
52
     */
53
    public $userlevel = 0;
54
55
    public $query = [];
56
57
    /**
58
     * reference to an object handler
59
     */
60
    private $handler;
61
62
    /**
63
     * Requested page
64
     */
65
    private $page = 'list.topic.php';
66
67
    /**
68
     * query variables
69
     */
70
    private $args = ['forum', 'uid', 'lastposter', 'type', 'status', 'mode', 'sort', 'order', 'start', 'since'];
71
72
    /**
73
     * Constructor
74
     */
75
    //    public function TopicRenderer()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
76
    public function __construct()
77
    {
78
        $this->handler = Newbb\Helper::getInstance()->getHandler('Topic');
79
    }
80
81
    /**
82
     * Access the only instance of this class
83
     * @return TopicRenderer
84
     */
85
    public static function getInstance()
86
    {
87
        static $instance;
88
        if (null === $instance) {
89
            $instance = new static();
90
        }
91
92
        return $instance;
93
    }
94
95
    public function init()
96
    {
97
        $this->noperm = false;
98
        $this->query  = [];
99
    }
100
101
    /**
102
     * @param $var
103
     * @param $val
104
     * @return array|int|string
105
     */
106
    public function setVar($var, $val)
107
    {
108
        switch ($var) {
109
            case 'forum':
110
                if (is_numeric($val)) {
111
                    $val = (int)$val;
112
                // START irmtfan - if the forum is array
113
                } elseif (is_array($val)) {
114
                    $val = implode('|', $val);
115
                    //} elseif (!empty($val)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
116
                    //    $val = implode("|", array_map("intval", explode(", ", $val)));
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
117
                }
118
                // END irmtfan - if the forum is array
119
                break;
120
121
            case 'type':
122
            case 'mode':
123
            case 'order':
124
            case 'start':
125
            case 'since':
126
                $val = (int)$val;
127
                break;
128
129
            case 'uid': // irmtfan add multi topic poster
130
            case 'lastposter': // irmtfan add multi lastposter
131
                break;
132
133
            case 'status':
134
                // START irmtfan to accept multiple status
135
                $val = is_array($val) ? $val : [$val];
136
                $val = implode(',', $val);
137
                //$val = (in_array($val, array_keys($this->getStatus( $this->userlevel ))) ) ? $val : "all"; //irmtfan no need to check if status is empty or not
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
138
                //if ($val === "all" && !$this->is_multiple) $val = ""; irmtfan commented because it is done in sort
139
                // END irmtfan to accept multiple status
140
                break;
141
142
            default:
143
                break;
144
        }
145
146
        return $val;
147
    }
148
149
    /**
150
     * @param array $vars
151
     */
152
    public function setVars(array $vars = [])
153
    {
154
        $this->init();
155
156
        foreach ($vars as $var => $val) {
157
            if (!in_array($var, $this->args)) {
158
                continue;
159
            }
160
            $this->vars[$var] = $this->setVar($var, $val);
161
        }
162
        $this->parseVars();
163
    }
164
165
    /**
166
     * @param null $status
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $status is correct as it would always require null to be passed?
Loading history...
167
     */
168
    public function myParseStatus($status = null)
169
    {
170
        switch ($status) {
171
            case 'digest':
172
                $this->query['where'][] = 't.topic_digest = 1';
173
                break;
174
175
            case 'undigest':
176
                $this->query['where'][] = 't.topic_digest = 0';
177
                break;
178
179
            case 'sticky':
180
                $this->query['where'][] = 't.topic_sticky = 1';
181
                break;
182
183
            case 'unsticky':
184
                $this->query['where'][] = 't.topic_sticky = 0';
185
                break;
186
187
            case 'lock':
188
                $this->query['where'][] = 't.topic_status = 1';
189
                break;
190
191
            case 'unlock':
192
                $this->query['where'][] = 't.topic_status = 0';
193
                break;
194
195
            case 'poll':
196
                $this->query['where'][] = 't.topic_haspoll = 1';
197
                break;
198
199
            case 'unpoll':
200
                $this->query['where'][] = 't.topic_haspoll = 0';
201
                break;
202
203
            case 'voted':
204
                $this->query['where'][] = 't.votes > 0';
205
                break;
206
207
            case 'unvoted':
208
                $this->query['where'][] = 't.votes < 1';
209
                break;
210
211
            case 'replied':
212
                $this->query['where'][] = 't.topic_replies > 0';
213
                break;
214
215
            case 'unreplied':
216
                $this->query['where'][] = 't.topic_replies < 1';
217
                break;
218
219
            case 'viewed':
220
                $this->query['where'][] = 't.topic_views > 0';
221
                break;
222
223
            case 'unviewed':
224
                $this->query['where'][] = 't.topic_views < 1';
225
                break;
226
227
            case 'read':
228
                // Skip
229
                if (empty($this->config['read_mode'])) {
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...
230
                    // Use database
231
                } elseif (2 == $this->config['read_mode']) {
232
                    // START irmtfan use read_uid to find the unread posts when the user is logged in
233
                    $read_uid = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
234
                    if (!empty($read_uid)) {
235
                        $this->query['join'][]  = 'LEFT JOIN ' . $this->handler->db->prefix('newbb_reads_topic') . ' AS r ON r.read_item = t.topic_id AND r.uid = ' . $read_uid . ' ';
0 ignored issues
show
Bug introduced by
The property db does not exist on boolean.
Loading history...
236
                        $this->query['where'][] = 'r.post_id = t.topic_last_post_id';
237
                    } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
238
                    }
239
                    // END irmtfan change criteria to get from uid p.uid = last post submit user id
240
                    // User cookie
241
                } elseif (1 == $this->config['read_mode']) {
242
                    // START irmtfan fix read_mode = 1 bugs - for all users (member and anon)
243
                    $startdate = !empty($this->vars['since']) ? (time() - newbbGetSinceTime($this->vars['since'])) : 0;
244
                    if ($lastvisit = max($GLOBALS['last_visit'], $startdate)) {
245
                        $readmode1query = '';
246
                        if ($lastvisit > $startdate) {
247
                            $readmode1query = 'p.post_time < ' . $lastvisit;
248
                        }
249
                        $topics         = [];
250
                        $topic_lastread = newbbGetCookie('LT', true);
251
                        if (count($topic_lastread) > 0) {
252
                            foreach ($topic_lastread as $id => $time) {
253
                                if ($time > $lastvisit) {
254
                                    $topics[] = $id;
255
                                }
256
                            }
257
                        }
258
                        if (count($topics) > 0) {
259
                            $topicquery = ' t.topic_id IN (' . implode(',', $topics) . ')';
260
                            // because it should be OR
261
                            $readmode1query = !empty($readmode1query) ? '(' . $readmode1query . ' OR ' . $topicquery . ')' : $topicquery;
262
                        }
263
                        $this->query['where'][] = $readmode1query;
264
                    }
265
                    // END irmtfan fix read_mode = 1 bugs - for all users (member and anon)
266
                }
267
                break;
268
269
            case 'unread':
270
                // Skip
271
                if (empty($this->config['read_mode'])) {
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...
272
                    // Use database
273
                } elseif (2 == $this->config['read_mode']) {
274
                    // START irmtfan use read_uid to find the unread posts when the user is logged in
275
                    $read_uid = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
276
                    if (!empty($read_uid)) {
277
                        $this->query['join'][]  = 'LEFT JOIN ' . $this->handler->db->prefix('newbb_reads_topic') . ' AS r ON r.read_item = t.topic_id AND r.uid = ' . $read_uid . ' ';
278
                        $this->query['where'][] = '(r.read_id IS NULL OR r.post_id < t.topic_last_post_id)';
279
                    } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
280
                    }
281
                    // END irmtfan change criteria to get from uid p.uid = last post submit user id
282
                    // User cookie
283
                } elseif (1 == $this->config['read_mode']) {
284
                    // START irmtfan fix read_mode = 1 bugs - for all users (member and anon)
285
                    $startdate = !empty($this->vars['since']) ? (time() - newbbGetSinceTime($this->vars['since'])) : 0;
286
                    if ($lastvisit = max($GLOBALS['last_visit'], $startdate)) {
287
                        if ($lastvisit > $startdate) {
288
                            $this->query['where'][] = 'p.post_time > ' . $lastvisit;
289
                        }
290
                        $topics         = [];
291
                        $topic_lastread = newbbGetCookie('LT', true);
292
                        if (count($topic_lastread) > 0) {
293
                            foreach ($topic_lastread as $id => $time) {
294
                                if ($time > $lastvisit) {
295
                                    $topics[] = $id;
296
                                }
297
                            }
298
                        }
299
                        if (count($topics) > 0) {
300
                            $this->query['where'][] = ' t.topic_id NOT IN (' . implode(',', $topics) . ')';
301
                        }
302
                    }
303
                    // END irmtfan fix read_mode = 1 bugs - for all users (member and anon)
304
                }
305
                break;
306
307
            case 'pending':
308
                if ($this->userlevel < 2) {
309
                    $this->noperm = true;
310
                } else {
311
                    $this->query['where'][] = 't.approved = 0';
312
                }
313
                break;
314
315
            case 'deleted':
316
                if ($this->userlevel < 2) {
317
                    $this->noperm = true;
318
                } else {
319
                    $this->query['where'][] = 't.approved = -1';
320
                }
321
                break;
322
323
            case 'all': // For viewall.php; do not display sticky topics at first
324
            case 'active': // same as 'all'
325
                $this->query['where'][] = 't.approved = 1';
326
                break;
327
328
            default: // irmtfan do nothing
329
                break;
330
        }
331
    }
332
333
    /**
334
     * @param $var
335
     * @param $val
336
     */
337
    public function parseVar($var, $val)
338
    {
339
        switch ($var) {
340
            case 'forum':
341
                /** @var Newbb\ForumHandler $forumHandler */
342
                $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
343
                // START irmtfan - get forum Ids by values. parse positive values to forum IDs and negative values to category IDs. value=0 => all valid forums
344
                // Get accessible forums
345
                $accessForums = $forumHandler->getIdsByValues(array_map('intval', @explode('|', $val)));
0 ignored issues
show
Bug introduced by
array_map('intval', @explode('|', $val)) of type array is incompatible with the type XoopsModules\Newbb\text|integer expected by parameter $values of XoopsModules\Newbb\ForumHandler::getIdsByValues(). ( Ignorable by Annotation )

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

345
                $accessForums = $forumHandler->getIdsByValues(/** @scrutinizer ignore-type */ array_map('intval', @explode('|', $val)));
Loading history...
346
                // Filter specified forums if any
347
                //if (!empty($val) && $_forums = @explode('|', $val)) {
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...
348
                //$accessForums = array_intersect($accessForums, array_map('intval', $_forums));
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...
349
                //}
350
                $this->vars['forum'] = $this->setVar('forum', $accessForums);
351
                // END irmtfan - get forum Ids by values. parse positive values to forum IDs and negative values to category IDs. value=0 => all valid forums
352
353
                if (empty($accessForums)) {
354
                    $this->noperm = true;
355
                // irmtfan - it just return return the forum_id only when the forum_id is the first allowed forum - no need for this code implode is enough removed.
356
                    //} elseif (count($accessForums) === 1) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
357
                    //$this->query["where"][] = "t.forum_id = " . $accessForums[0];
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
358
                } else {
359
                    $this->query['where'][] = 't.forum_id IN ( ' . implode(', ', $accessForums) . ' )';
360
                }
361
                break;
362
363
            case 'uid': // irmtfan add multi topic poster
364
                if (-1 !== $val) {
365
                    $val                    = implode(',', array_map('intval', explode(',', $val)));
366
                    $this->query['where'][] = 't.topic_poster IN ( ' . $val . ' )';
367
                }
368
                break;
369
            case 'lastposter': // irmtfan add multi lastposter
370
                if (-1 !== $val) {
371
                    $val                    = implode(',', array_map('intval', explode(',', $val)));
372
                    $this->query['where'][] = 'p.uid IN ( ' . $val . ' )';
373
                }
374
                break;
375
376
            case 'since':
377
                if (!empty($val)) {
378
                    // START irmtfan if unread && read_mode = 1 and last_visit > startdate do not add where query | to accept multiple status
379
                    $startdate = time() - newbbGetSinceTime($val);
380
                    if (in_array('unread', explode(',', $this->vars['status'], true)) && 1 == $this->config['read_mode']
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $limit of explode(). ( Ignorable by Annotation )

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

380
                    if (in_array('unread', explode(',', $this->vars['status'], /** @scrutinizer ignore-type */ true)) && 1 == $this->config['read_mode']
Loading history...
381
                        && $GLOBALS['last_visit'] > $startdate) {
382
                        break;
383
                    }
384
                    // irmtfan digest_time | to accept multiple status
385
                    if (in_array('digest', explode(',', $this->vars['status'], true))) {
386
                        $this->query['where'][] = 't.digest_time > ' . $startdate;
387
                    }
388
                    // irmtfan - should be >= instead of =
389
                    $this->query['where'][] = 'p.post_time >= ' . $startdate;
390
                    // END irmtfan if unread && read_mode = 1 and last_visit > startdate do not add where query
391
                }
392
                break;
393
394
            case 'type':
395
                if (!empty($val)) {
396
                    $this->query['where'][] = 't.type_id = ' . $val;
397
                }
398
                break;
399
400
            case 'status':
401
                // START irmtfan to accept multiple status
402
                $val = explode(',', $val);
403
                // irmtfan - add 'all' to always parse t.approved = 1
404
                if (0 === count(array_intersect($val, ['all', 'active', 'pending', 'deleted']))) {
405
                    $val[] = 'all';
406
                }
407
                foreach ($val as $key => $status) {
408
                    $this->myParseStatus($status);
409
                }
410
                // END irmtfan to accept multiple status
411
                break;
412
413
            case 'sort':
414
                if ($sort = $this->getSort($val, 'sort')) {
415
                    $this->query['sort'][] = $sort . (empty($this->vars['order']) ? ' DESC' : ' ASC');
0 ignored issues
show
Bug introduced by
Are you sure $sort of type array can be used in concatenation? ( Ignorable by Annotation )

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

415
                    $this->query['sort'][] = /** @scrutinizer ignore-type */ $sort . (empty($this->vars['order']) ? ' DESC' : ' ASC');
Loading history...
416
                } else { // irmtfan if sort is not in the list
417
                    $this->query['sort'][] = 't.topic_last_post_id' . (empty($this->vars['order']) ? ' DESC' : ' ASC');
418
                }
419
                break;
420
421
            default:
422
                break;
423
        }
424
    }
425
426
    /**
427
     * @return bool
428
     */
429
    public function parseVars()
430
    {
431
        static $parsed;
432
        // irmtfan - force to parse vars (run against static vars)
433
        if (isset($parsed) && !$this->force) {
434
            return true;
435
        }
436
437
        if (!isset($this->vars['forum'])) {
438
            $this->vars['forum'] = null;
439
        }
440
        //irmtfan parse status for rendering topic correctly - if empty($_GET(status)) it will show all topics include deleted and pendings. 'all' instead of all
441
        if (!isset($this->vars['status'])) {
442
            $this->vars['status'] = 'all';
443
        }
444
        // irmtfan if sort is not set or is empty get a default sort- if empty($_GET(sort)) | if sort=null eg: /list.topic.php?sort=
445
        if (empty($this->vars['sort'])) {
446
            $this->vars['sort'] = 'lastpost';
447
        } // use lastpost instead of sticky
448
449
        foreach ($this->vars as $var => $val) {
450
            $this->parseVar($var, $val);
451
            if (empty($val)) {
452
                unset($this->vars[$var]);
453
            }
454
        }
455
        $parsed = true;
456
457
        return true;
458
    }
459
460
    /**
461
     * @param  null $header
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $header is correct as it would always require null to be passed?
Loading history...
462
     * @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...
463
     * @return array|null
464
     */
465
    public function getSort($header = null, $var = null)
466
    {
467
        $headers = [
468
            'topic'           => [
469
                'title' => _MD_NEWBB_TOPICS,
470
                'sort'  => 't.topic_title'
471
            ],
472
            'forum'           => [
473
                'title' => _MD_NEWBB_FORUM,
474
                'sort'  => 't.forum_id'
475
            ],
476
            'poster'          => [
477
                'title' => _MD_NEWBB_TOPICPOSTER, /*irmtfan _MD_NEWBB_POSTER to _MD_NEWBB_TOPICPOSTER*/
478
                'sort'  => 't.topic_poster'
479
            ],
480
            'replies'         => [
481
                'title' => _MD_NEWBB_REPLIES,
482
                'sort'  => 't.topic_replies'
483
            ],
484
            'views'           => [
485
                'title' => _MD_NEWBB_VIEWS,
486
                'sort'  => 't.topic_views'
487
            ],
488
            'lastpost'        => [ // irmtfan show topic_page_jump_icon smarty
489
                                   'title' => _MD_NEWBB_LASTPOST,
490
                                   /*irmtfan _MD_NEWBB_DATE to _MD_NEWBB_LASTPOSTTIME again change to _MD_LASTPOST*/
491
                                   'sort'  => 't.topic_last_post_id'
492
            ],
493
            // START irmtfan add more sorts
494
            'lastposttime'    => [ // irmtfan same as lastpost
495
                                   'title' => _MD_NEWBB_LASTPOSTTIME,
496
                                   'sort'  => 't.topic_last_post_id'
497
            ],
498
            'lastposter'      => [ // irmtfan
499
                                   'title' => _MD_NEWBB_POSTER,
500
                                   'sort'  => 'p.uid',// poster uid
501
            ],
502
            'lastpostmsgicon' => [ // irmtfan
503
                                   'title' => _MD_NEWBB_MESSAGEICON,
504
                                   'sort'  => 'p.icon',// post message icon
505
            ],
506
            'ratings'         => [
507
                'title' => _MD_NEWBB_RATINGS,
508
                'sort'  => 't.rating', // irmtfan t.topic_rating to t.rating
509
            ],
510
            'votes'           => [
511
                'title' => _MD_NEWBB_VOTES,
512
                'sort'  => 't.votes'
513
            ],
514
            'publish'         => [
515
                'title' => _MD_NEWBB_TOPICTIME,
516
                'sort'  => 't.topic_id'
517
            ],
518
            'digest'          => [
519
                'title' => _MD_NEWBB_DIGEST,
520
                'sort'  => 't.digest_time'
521
            ],
522
            'sticky'          => [
523
                'title' => _MD_NEWBB_STICKY,
524
                'sort'  => 't.topic_sticky'
525
            ],
526
            'lock'            => [
527
                'title' => _MD_NEWBB_LOCK,
528
                'sort'  => 't.topic_status'
529
            ],
530
            'poll'            => [
531
                'title' => _MD_NEWBB_POLL_POLL,
532
                'sort'  => 't.poll_id'
533
            ]
534
        ];
535
        $types   = $this->getTypes();
536
        if (!empty($types)) {
537
            $headers['type'] = [
538
                'title' => _MD_NEWBB_TYPE,
539
                'sort'  => 't.type_id'
540
            ];
541
        }
542
        if (2 == $this->userlevel) {
543
            $headers['approve'] = [
544
                'title' => _MD_NEWBB_APPROVE,
545
                'sort'  => 't.approved'
546
            ];
547
        }
548
        // END irmtfan add more sorts
549
        if (empty($header) && empty($var)) {
550
            return $headers;
551
        }
552
        if (!empty($var) && !empty($header)) {
553
            return @$headers[$header][$var];
554
        }
555
        if (empty($var)) {
556
            return @$headers[$header];
557
        }
558
        $ret = null;
559
        foreach (array_keys($headers) as $key) {
560
            $ret[$key] = @$headers[$key][$var];
561
        }
562
563
        return $ret;
564
    }
565
566
    // START irmtfan add Display topic headers function
567
568
    /**
569
     * @param  null $header
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $header is correct as it would always require null to be passed?
Loading history...
570
     * @return array
571
     */
572
    public function getHeader($header = null)
573
    {
574
        $headersSort = $this->getSort('', 'title');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $headersSort is correct as $this->getSort('', 'title') targeting XoopsModules\Newbb\TopicRenderer::getSort() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
575
        // additional headers - important: those cannot be in sort anyway
576
        $headers = array_merge($headersSort, [
577
            'attachment' => _MD_NEWBB_TOPICSHASATT, // show attachment smarty
578
            'read'       => _MD_NEWBB_MARK_UNREAD . '|' . _MD_NEWBB_MARK_READ, // read/unread show topic_folder smarty
579
            'pagenav'    => _MD_NEWBB_PAGENAV_DISPLAY, // show topic_page_jump smarty - sort by topic_replies?
580
        ]);
581
582
        return $this->getFromKeys($headers, $header);
583
    }
584
585
    // END irmtfan add Display topic headers function
586
587
    /**
588
     * @param  null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
589
     * @param  null $status
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $status is correct as it would always require null to be passed?
Loading history...
590
     * @return array
591
     */
592
    public function getStatus($type = null, $status = null)
593
    {
594
        $links       = [
595
            //""            => "", /* irmtfan remove empty array */
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% 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...
596
            'all'       => _ALL,
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\_ALL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
597
            'digest'    => _MD_NEWBB_DIGEST,
598
            'undigest'  => _MD_NEWBB_UNDIGEST, // irmtfan add
599
            'sticky'    => _MD_NEWBB_STICKY, // irmtfan add
600
            'unsticky'  => _MD_NEWBB_UNSTICKY, // irmtfan add
601
            'lock'      => _MD_NEWBB_LOCK, // irmtfan add
602
            'unlock'    => _MD_NEWBB_UNLOCK, // irmtfan add
603
            'poll'      => _MD_NEWBB_TOPICHASPOLL, // irmtfan add
604
            'unpoll'    => _MD_NEWBB_TOPICHASNOTPOLL, // irmtfan add
605
            'voted'     => _MD_NEWBB_VOTED, // irmtfan add
606
            'unvoted'   => _MD_NEWBB_UNVOTED, // irmtfan add
607
            'viewed'    => _MD_NEWBB_VIEWED, // irmtfan add
608
            'unviewed'  => _MD_NEWBB_UNVIEWED, // irmtfan add
609
            'replied'   => _MD_NEWBB_REPLIED, // irmtfan add
610
            'unreplied' => _MD_NEWBB_UNREPLIED,
611
            'read'      => _MD_NEWBB_READ, // irmtfan add
612
            'unread'    => _MD_NEWBB_UNREAD
613
        ];
614
        $links_admin = [
615
            'active'  => _MD_NEWBB_TYPE_ADMIN,
616
            'pending' => _MD_NEWBB_TYPE_PENDING,
617
            'deleted' => _MD_NEWBB_TYPE_DELETED
618
        ];
619
620
        // all status, for admin
621
        if ($type > 1) {
622
            $links = array_merge($links, $links_admin);// irmtfan to accept multiple status
623
        }
624
625
        return $this->getFromKeys($links, $status); // irmtfan to accept multiple status
626
    }
627
628
    /**
629
     * @param \Smarty $xoopsTpl
630
     * @throws \RuntimeException
631
     */
632
    public function buildSelection(\Smarty $xoopsTpl)
0 ignored issues
show
Bug introduced by
The type Smarty 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...
633
    {
634
        $selection         = ['action' => $this->page];
635
        $selection['vars'] = $this->vars;
636
        require_once __DIR__ . '/../include/functions.forum.php';
637
        $forum_selected     = empty($this->vars['forum']) ? null : explode('|', @$this->vars['forum']);
638
        $selection['forum'] = '<select name="forum[]" multiple="multiple">';
639
        $selection['forum'] .= '<option value="0">' . _MD_NEWBB_ALL . '</option>';
640
        $selection['forum'] .= newbbForumSelectBox($forum_selected);
641
        $selection['forum'] .= '</select>';
642
643
        $sort_selected     = $this->vars['sort'];
644
        $sorts             = $this->getSort('', 'title');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $sorts is correct as $this->getSort('', 'title') targeting XoopsModules\Newbb\TopicRenderer::getSort() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
645
        $selection['sort'] = "<select name='sort'>";
646
        if (!is_array($sorts)) {
0 ignored issues
show
introduced by
The condition is_array($sorts) is always false.
Loading history...
647
            throw new \RuntimeException('$sorts must be an array.');
648
        }
649
        foreach ($sorts as $sort => $title) {
650
            $selection['sort'] .= "<option value='" . $sort . "' " . (($sort == $sort_selected) ? " selected='selected'" : '') . '>' . $title . '</option>';
651
        }
652
        $selection['sort'] .= '</select>';
653
654
        $selection['order'] = "<select name='order'>";
655
        $selection['order'] .= "<option value='0' " . (empty($this->vars['order']) ? " selected='selected'" : '') . '>' . _DESCENDING . '</option>';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\_DESCENDING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
656
        $selection['order'] .= "<option value='1' " . (!empty($this->vars['order']) ? " selected='selected'" : '') . '>' . _ASCENDING . '</option>';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\_ASCENDING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
657
        $selection['order'] .= '</select>';
658
659
        $since              = isset($this->vars['since']) ? $this->vars['since'] : $this->config['since_default'];
660
        $selection['since'] = newbbSinceSelectBox($since);
661
662
        $xoopsTpl->assign_by_ref('selection', $selection);
663
    }
664
665
    /**
666
     * @param \Smarty $xoopsTpl
667
     */
668
    public function buildSearch(\Smarty $xoopsTpl)
669
    {
670
        $search             = [];
671
        $search['forum']    = @$this->vars['forum'];
672
        $search['since']    = @$this->vars['since'];
673
        $search['searchin'] = 'both';
674
675
        $xoopsTpl->assign_by_ref('search', $search);
676
    }
677
678
    /**
679
     * @param \Smarty $xoopsTpl
680
     * @throws \RuntimeException
681
     */
682
    public function buildHeaders(\Smarty $xoopsTpl)
683
    {
684
        $args = [];
685
        foreach ($this->vars as $var => $val) {
686
            if ('sort' === $var || 'order' === $var) {
687
                continue;
688
            }
689
            $args[] = "{$var}={$val}";
690
        }
691
692
        $headers = $this->getSort('', 'title');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $headers is correct as $this->getSort('', 'title') targeting XoopsModules\Newbb\TopicRenderer::getSort() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
693
        if (!is_array($headers)) {
0 ignored issues
show
introduced by
The condition is_array($headers) is always false.
Loading history...
694
            throw new \RuntimeException('$headers must be an array.');
695
        }
696
        foreach ($headers as $header => $title) {
697
            $_args = ["sort={$header}"];
698
            if (@$this->vars['sort'] == $header) {
699
                $_args[] = 'order=' . ((@$this->vars['order'] + 1) % 2);
700
            }
701
            $headers_data[$header]['title'] = $title;
702
            $headers_data[$header]['link']  = $this->page . '?' . implode('&amp;', array_merge($args, $_args));
703
        }
704
        $xoopsTpl->assign_by_ref('headers', $headers_data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $headers_data seems to be defined by a foreach iteration on line 696. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
705
    }
706
707
    /**
708
     * @param \Smarty $xoopsTpl
709
     */
710
    public function buildFilters(\Smarty $xoopsTpl)
711
    {
712
        $args = [];
713
        foreach ($this->vars as $var => $val) {
714
            if ('status' === $var) {
715
                continue;
716
            }
717
            $args[] = "{$var}={$val}";
718
        }
719
720
        $links = $this->getStatus($this->userlevel);
721
722
        $status = [];
723
        foreach ($links as $link => $title) {
724
            $_args                  = ["status={$link}"];
725
            $status[$link]['title'] = $title;
726
            $status[$link]['link']  = $this->page . '?' . implode('&amp;', array_merge($args, $_args));
727
        }
728
        $xoopsTpl->assign_by_ref('filters', $status);
729
    }
730
731
    /**
732
     * @param  null $type_id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type_id is correct as it would always require null to be passed?
Loading history...
733
     * @return mixed
734
     */
735
    public function getTypes($type_id = null)
736
    {
737
        static $types;
738
        if (!isset($types)) {
739
            /** @var Newbb\TypeHandler $typeHandler */
740
            $typeHandler = Newbb\Helper::getInstance()->getHandler('Type');
741
742
            $types = $typeHandler->getByForum(explode('|', @$this->vars['forum']));
743
        }
744
745
        if (empty($type_id)) {
746
            return $types;
747
        }
748
749
        return @$types[$type_id];
750
    }
751
752
    /**
753
     * @param  \Smarty $xoopsTpl
754
     * @return bool
755
     */
756
    public function buildTypes(\Smarty $xoopsTpl)
757
    {
758
        $status = '';
759
        if (!$types = $this->getTypes()) {
760
            return true;
761
        }
762
763
        $args = [];
764
        foreach ($this->vars as $var => $val) {
765
            if ('type' === $var) {
766
                continue;
767
            }
768
            $args[] = "{$var}={$val}";
769
        }
770
771
        foreach ($types as $id => $type) {
772
            $_args                = ["type={$id}"];
773
            $status[$id]['title'] = $type['type_name'];
774
            $status[$id]['link']  = $this->page . '?' . implode('&amp;', array_merge($args, $_args));
775
        }
776
        $xoopsTpl->assign_by_ref('types', $status);
777
    }
778
779
    /**
780
     * @param  \Smarty $xoopsTpl
781
     * @return bool
782
     */
783
    public function buildCurrent(\Smarty $xoopsTpl)
784
    {
785
        if (empty($this->vars['status']) && !$this->is_multiple) {
786
            return true;
787
        }
788
789
        $args = [];
790
        foreach ($this->vars as $var => $val) {
791
            $args[] = "{$var}={$val}";
792
        }
793
794
        $status          = [];
795
        $status['title'] = implode(',', $this->getStatus($this->userlevel, $this->vars['status'])); // irmtfan to accept multiple status
796
        //$status['link'] = $this->page.(empty($this->vars['status']) ? '' : '?status='.$this->vars['status']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
797
        $status['link'] = $this->page . (empty($args) ? '' : '?' . implode('&amp;', $args));
798
799
        $xoopsTpl->assign_by_ref('current', $status);
800
    }
801
802
    /**
803
     * @param \Smarty $xoopsTpl
804
     */
805
    public function buildPagenav(\Smarty $xoopsTpl)
806
    {
807
        $count_topic = $this->getCount();
808
        if ($count_topic > $this->config['topics_per_page']) {
809
            $args = [];
810
            foreach ($this->vars as $var => $val) {
811
                if ('start' === $var) {
812
                    continue;
813
                }
814
                $args[] = "{$var}={$val}";
815
            }
816
            require_once $GLOBALS['xoops']->path('class/pagenav.php');
817
            $nav = new \XoopsPageNav($count_topic, $this->config['topics_per_page'], @$this->vars['start'], 'start', implode('&amp;', $args));
0 ignored issues
show
Bug introduced by
The type XoopsPageNav 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...
818
            if (isset($GLOBALS['xoopsModuleConfig']['do_rewrite'])) {
819
                $nav->url = formatURL(Request::getString('SERVER_NAME', '', 'SERVER')) . ' /' . $nav->url;
0 ignored issues
show
Bug introduced by
The function formatURL 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

819
                $nav->url = /** @scrutinizer ignore-call */ formatURL(Request::getString('SERVER_NAME', '', 'SERVER')) . ' /' . $nav->url;
Loading history...
820
            }
821
            if ('select' === $this->config['pagenav_display']) {
822
                $navi = $nav->renderSelect();
823
            } elseif ('image' === $this->config['pagenav_display']) {
824
                $navi = $nav->renderImageNav(4);
825
            } else {
826
                $navi = $nav->renderNav(4);
827
            }
828
            $xoopsTpl->assign('pagenav', $navi);
829
        } else {
830
            $xoopsTpl->assign('pagenav', '');
831
        }
832
    }
833
834
    /**
835
     * @return int
836
     */
837
    public function getCount()
838
    {
839
        if ($this->noperm) {
840
            return 0;
841
        }
842
843
        $selects = [];
844
        $froms   = [];
845
        $joins   = [];
846
        $wheres  = [];
847
848
        // topic fields
849
        $selects[] = 'COUNT(*)';
850
851
        $froms[]  = $this->handler->db->prefix('newbb_topics') . ' AS t ';
0 ignored issues
show
Bug introduced by
The property db does not exist on boolean.
Loading history...
852
        $joins[]  = 'LEFT JOIN ' . $this->handler->db->prefix('newbb_posts') . ' AS p ON p.post_id = t.topic_last_post_id';
853
        $wheres[] = '1 = 1';
854
855
        $sql = '    SELECT ' . implode(', ', $selects) . '     FROM ' . implode(', ', $froms) . '        ' . implode(' ', $joins) . (!empty($this->query['join']) ? '        ' . implode(' ', $this->query['join']) : '') . // irmtfan bug fix: Undefined index: join when post_excerpt = 0
856
               '     WHERE ' . implode(' AND ', $wheres) . '        AND ' . @implode(' AND ', @$this->query['where']);
857
858
        if (!$result = $this->handler->db->query($sql)) {
859
            return 0;
860
        }
861
        list($count) = $this->handler->db->fetchRow($result);
862
863
        return $count;
864
    }
865
866
    /**
867
     * @param  \Smarty $xoopsTpl
868
     * @return array|void
869
     */
870
    public function renderTopics(\Smarty $xoopsTpl = null)
871
    {
872
        $myts = \MyTextSanitizer::getInstance(); // irmtfan Instanciate
0 ignored issues
show
Bug introduced by
The type MyTextSanitizer 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...
873
874
        $ret = [];
875
        //$this->parseVars();
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...
876
877
        if ($this->noperm) {
878
            if (is_object($xoopsTpl)) {
879
                $xoopsTpl->assign_by_ref('topics', $ret);
880
881
                return;
882
            }
883
884
            return $ret;
885
        }
886
887
        $selects = [];
888
        $froms   = [];
889
        $joins   = [];
890
        $wheres  = [];
891
892
        // topic fields
893
        $selects[] = 't.*';
894
        // post fields
895
        $selects[] = 'p.post_time as last_post_time, p.poster_name as last_poster_name, p.icon, p.post_id, p.uid';
896
897
        $froms[]  = $this->handler->db->prefix('newbb_topics') . ' AS t ';
0 ignored issues
show
Bug introduced by
The property db does not exist on boolean.
Loading history...
898
        $joins[]  = 'LEFT JOIN ' . $this->handler->db->prefix('newbb_posts') . ' AS p ON p.post_id = t.topic_last_post_id';
899
        $wheres[] = '1 = 1';
900
901
        if (!empty($this->config['post_excerpt'])) {
902
            $selects[]             = 'p.post_karma, p.require_reply, pt.post_text';
903
            $this->query['join'][] = 'LEFT JOIN ' . $this->handler->db->prefix('newbb_posts_text') . ' AS pt ON pt.post_id = t.topic_last_post_id';
904
        }
905
        //if (empty($this->query["sort"])) $this->query["sort"][] = 't.topic_last_post_id DESC'; // irmtfan commented no need
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
906
907
        $sql = '    SELECT ' . implode(', ', $selects) . '     FROM ' . implode(', ', $froms) . '        ' . implode(' ', $joins) . (!empty($this->query['join']) ? '        ' . implode(' ', $this->query['join']) : '') . // irmtfan bug fix: Undefined index join when post_excerpt = 0
908
               '     WHERE ' . implode(' AND ', $wheres) . '        AND ' . @implode(' AND ', @$this->query['where']) . '     ORDER BY ' . implode(', ', $this->query['sort']);
909
910
        if (!$result = $this->handler->db->query($sql, $this->config['topics_per_page'], @$this->vars['start'])) {
911
            if (is_object($xoopsTpl)) {
912
                $xoopsTpl->assign_by_ref('topics', $ret);
913
914
                return;
915
            }
916
917
            return $ret;
918
        }
919
920
        require_once __DIR__ . '/../include/functions.render.php';
921
        require_once __DIR__ . '/../include/functions.session.php';
922
        require_once __DIR__ . '/../include/functions.time.php';
923
        require_once __DIR__ . '/../include/functions.read.php';
924
        require_once __DIR__ . '/../include/functions.topic.php';
925
926
        $sticky    = 0;
927
        $topics    = [];
928
        $posters   = [];
929
        $reads     = [];
930
        $types     = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $types is dead and can be removed.
Loading history...
931
        $forums    = [];
932
        $anonymous = $myts->htmlSpecialChars($GLOBALS['xoopsConfig']['anonymous']);
933
934
        while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
935
            if ($myrow['topic_sticky']) {
936
                ++$sticky;
937
            }
938
939
            // ------------------------------------------------------
940
            // START irmtfan remove topic_icon hardcode smarty
941
            // topic_icon: just regular topic_icon
942
            if (!empty($myrow['icon'])) {
943
                $topic_icon = '<img align="middle" src="' . XOOPS_URL . '/images/subject/' . htmlspecialchars($myrow['icon'], ENT_QUOTES | ENT_HTML5) . '" alt="" />';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
944
            } else {
945
                $topic_icon = '<img align="middle" src="' . XOOPS_URL . '/images/icons/no_posticon.gif" alt="" />';
946
            }
947
            // END irmtfan remove topic_icon hardcode smarty
948
949
            // ------------------------------------------------------
950
            // rating_img
951
            $rating = number_format($myrow['rating'] / 2, 0);
952
            // irmtfan - add alt key for rating
953
            if ($rating < 1) {
954
                $rating_img = newbbDisplayImage('blank');
955
            } else {
956
                $rating_img = newbbDisplayImage('rate' . $rating, constant('_MD_NEWBB_RATE' . $rating));
957
            }
958
959
            // ------------------------------------------------------
960
            // topic_page_jump
961
            $topic_page_jump      = '';
962
            $topic_page_jump_icon = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $topic_page_jump_icon is dead and can be removed.
Loading history...
963
            $totalpages           = ceil(($myrow['topic_replies'] + 1) / $this->config['posts_per_page']);
964
            if ($totalpages > 1) {
965
                $topic_page_jump .= '&nbsp;&nbsp;';
966
                $append          = false;
967
                for ($i = 1; $i <= $totalpages; ++$i) {
968
                    if ($i > 3 && $i < $totalpages) {
969
                        if (!$append) {
970
                            $topic_page_jump .= '...';
971
                            $append          = true;
972
                        }
973
                    } else {
974
                        $topic_page_jump .= '[<a href="' . XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $myrow['topic_id'] . '&amp;start=' . (($i - 1) * $this->config['posts_per_page']) . '">' . $i . '</a>]';
975
                        // irmtfan remove here and move
976
                        //$topic_page_jump_icon = "<a href='" . XOOPS_URL . "/modules/newbb/viewtopic.php?topic_id=" . $myrow['topic_id'] . "&amp;start=" . (($i - 1) * $this->config['posts_per_page']) . "" . "'>" . newbbDisplayImage('document',_MD_NEWBB_GOTOLASTPOST) . '</a>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% 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...
977
                    }
978
                }
979
            }
980
            // irmtfan - move here for both topics with and without pages - change topic_id to post_id
981
            $topic_page_jump_icon = "<a href='" . XOOPS_URL . '/modules/newbb/viewtopic.php?post_id=' . $myrow['topic_last_post_id'] . '' . "'>" . newbbDisplayImage('lastposticon', _MD_NEWBB_GOTOLASTPOST) . '</a>';
982
983
            // ------------------------------------------------------
984
            // => topic array
985
986
            $topic_title = $myts->htmlSpecialChars($myrow['topic_title']);
987
            // irmtfan use topic_title_excerpt for block topic title length
988
            $topic_title_excerpt = $topic_title;
989
            if (!empty($this->config['topic_title_excerpt'])) {
990
                $topic_title_excerpt = xoops_substr($topic_title, 0, $this->config['topic_title_excerpt']);
0 ignored issues
show
Bug introduced by
The function xoops_substr 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

990
                $topic_title_excerpt = /** @scrutinizer ignore-call */ xoops_substr($topic_title, 0, $this->config['topic_title_excerpt']);
Loading history...
991
            }
992
            // irmtfan hardcode class commented
993
            //if ($myrow['topic_digest']) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% 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...
994
            //   $topic_title = "<span class='digest'>" . $topic_title . "</span>";
995
            //}
996
997
            if (empty($this->config['post_excerpt'])) {
998
                $topic_excerpt = '';
999
            } elseif (($myrow['post_karma'] > 0 || $myrow['require_reply'] > 0) && !newbbIsAdmin($myrow['forum_id'])) {
1000
                $topic_excerpt = '';
1001
            } else {
1002
                $topic_excerpt = xoops_substr(newbbHtml2text($myts->displayTarea($myrow['post_text'])), 0, $this->config['post_excerpt']);
1003
                $topic_excerpt = str_replace('[', '&#91;', $myts->htmlSpecialChars($topic_excerpt));
1004
            }
1005
1006
            $topics[$myrow['topic_id']] = [
1007
                'topic_id'               => $myrow['topic_id'],
1008
                'topic_icon'             => $topic_icon,
1009
                'type_id'                => $myrow['type_id'],
1010
                'topic_title_excerpt'    => $topic_title_excerpt,
1011
                //irmtfan use topic_title_excerpt
1012
                //'topic_link'    => XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $myrow['topic_id'], // . '&amp;forum=' . $myrow['forum_id'], // irmtfan comment
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
1013
                'topic_link'             => 'viewtopic.php?topic_id=' . $myrow['topic_id'],
1014
                // irmtfan remove hardcode link
1015
                'rating_img'             => $rating_img,
1016
                'votes'                  => $myrow['votes'],
1017
                //irmtfan added
1018
                'topic_page_jump'        => $topic_page_jump,
1019
                'topic_page_jump_icon'   => $topic_page_jump_icon,
1020
                'topic_replies'          => $myrow['topic_replies'],
1021
                'topic_poster_uid'       => $myrow['topic_poster'],
1022
                'topic_poster_name'      => !empty($myrow['poster_name']) ? $myts->htmlSpecialChars($myrow['poster_name']) : $anonymous,
1023
                'topic_views'            => $myrow['topic_views'],
1024
                'topic_time'             => newbbFormatTimestamp($myrow['topic_time']),
1025
                'topic_last_post_id'     => $myrow['topic_last_post_id'],
1026
                //irmtfan added
1027
                'topic_last_posttime'    => newbbFormatTimestamp($myrow['last_post_time']),
1028
                'topic_last_poster_uid'  => $myrow['uid'],
1029
                'topic_last_poster_name' => !empty($myrow['last_poster_name']) ? $myts->htmlSpecialChars($myrow['last_poster_name']) : $anonymous,
1030
                'topic_forum'            => $myrow['forum_id'],
1031
                'topic_excerpt'          => $topic_excerpt,
1032
                'sticky'                 => $myrow['topic_sticky'] ? newbbDisplayImage('topic_sticky', _MD_NEWBB_TOPICSTICKY) : '',
1033
                // irmtfan bug fixed
1034
                'lock'                   => $myrow['topic_status'] ? newbbDisplayImage('topic_locked', _MD_NEWBB_TOPICLOCK) : '',
1035
                //irmtfan added
1036
                'digest'                 => $myrow['topic_digest'] ? newbbDisplayImage('topic_digest', _MD_NEWBB_TOPICDIGEST) : '',
1037
                //irmtfan added
1038
                'poll'                   => $myrow['topic_haspoll'] ? newbbDisplayImage('poll', _MD_NEWBB_TOPICHASPOLL) : '',
1039
                //irmtfan added
1040
                'approve'                => $myrow['approved'],
1041
                //irmtfan added
1042
            ];
1043
1044
            /* users */
1045
            $posters[$myrow['topic_poster']] = 1;
1046
            $posters[$myrow['uid']]          = 1;
1047
            // reads
1048
            if (!empty($this->config['read_mode'])) {
1049
                $reads[$myrow['topic_id']] = (1 == $this->config['read_mode']) ? $myrow['last_post_time'] : $myrow['topic_last_post_id'];
1050
            }
1051
            // types
1052
            if (!empty($myrow['type_id'])) {
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...
1053
                //$types[$myrow['type_id']] = 1;
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
1054
            }
1055
            // forums
1056
            $forums[$myrow['forum_id']] = 1;
1057
        }
1058
        $posters_name = newbbGetUnameFromIds(array_keys($posters), $this->config['show_realname'], true);
1059
        $topic_isRead = newbbIsRead('topic', $reads);
1060
        /*
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...
1061
        $type_list = array();
1062
        if (count($types) > 0) {
1063
            $typeHandler =  Newbb\Helper::getInstance()->getHandler('Type');
1064
            $type_list = $typeHandler->getAll(new \Criteria("type_id", "(".implode(", ", array_keys($types)).")", "IN"), null, false);
1065
        }
1066
        */
1067
        $type_list = $this->getTypes();
1068
        /** @var Newbb\ForumHandler $forumHandler */
1069
        $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
1070
1071
        if (count($forums) > 0) {
1072
            $forum_list = $forumHandler->getAll(new \Criteria('forum_id', '(' . implode(', ', array_keys($forums)) . ')', 'IN'), ['forum_name', 'hot_threshold'], false);
0 ignored issues
show
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...
1073
        } else {
1074
            $forum_list = $forumHandler->getAll();
1075
        }
1076
1077
        foreach (array_keys($topics) as $id) {
1078
            $topics[$id]['topic_read']       = empty($topic_isRead[$id]) ? 0 : 1; // add topic-read/topic-new smarty variable
1079
            $topics[$id]['topic_forum_link'] = '<a href="' . XOOPS_URL . '/modules/newbb/viewforum.php?forum=' . $topics[$id]['topic_forum'] . '">' . $forum_list[$topics[$id]['topic_forum']]['forum_name'] . '</a>';
1080
1081
            //irmtfan use topic_title_excerpt -- add else
1082
            if (!empty($topics[$id]['type_id']) && isset($type_list[$topics[$id]['type_id']])) {
1083
                $topics[$id]['topic_title'] = getTopicTitle($topics[$id]['topic_title_excerpt'], $type_list[$topics[$id]['type_id']]['type_name'], $type_list[$topics[$id]['type_id']]['type_color']);
1084
            } else {
1085
                $topics[$id]['topic_title'] = $topics[$id]['topic_title_excerpt'];
1086
            }
1087
            $topics[$id]['topic_poster']      = !empty($posters_name[$topics[$id]['topic_poster_uid']]) ? $posters_name[$topics[$id]['topic_poster_uid']] : $topics[$id]['topic_poster_name'];
1088
            $topics[$id]['topic_last_poster'] = !empty($posters_name[$topics[$id]['topic_last_poster_uid']]) ? $posters_name[$topics[$id]['topic_last_poster_uid']] : $topics[$id]['topic_last_poster_name'];
1089
            // ------------------------------------------------------
1090
            // START irmtfan remove hardcodes from topic_folder smarty
1091
            // topic_folder: priority: newhot -> hot/new -> regular
1092
            //list($topic_status, $topic_digest, $topic_replies) = $topics[$id]["stats"]; irmtfan
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...
1093
            // START irmtfan - add topic_folder_text for alt
1094
            //if ($topics[$id]["lock"] === 1) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% 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...
1095
            //    $topic_folder = 'topic_locked';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
1096
            //    $topic_folder_text = _MD_NEWBB_TOPICLOCKED;
1097
            //} else {
1098
            //if ($topic_digest) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
1099
            //    $topic_folder = 'topic_digest';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
1100
            //    $topic_folder_text = _MD_NEWBB_TOPICDIGEST;
1101
            if ($topics[$id]['topic_replies'] >= $forum_list[$topics[$id]['topic_forum']]['hot_threshold']) {
1102
                $topic_folder      = empty($topic_isRead[$id]) ? 'topic_hot_new' : 'topic_hot';
1103
                $topic_folder_text = empty($topic_isRead[$id]) ? _MD_NEWBB_MORETHAN : _MD_NEWBB_MORETHAN2;
1104
            } else {
1105
                $topic_folder      = empty($topic_isRead[$id]) ? 'topic_new' : 'topic';
1106
                $topic_folder_text = empty($topic_isRead[$id]) ? _MD_NEWBB_NEWPOSTS : _MD_NEWBB_NONEWPOSTS;
1107
            }
1108
            //}
1109
            // END irmtfan remove hardcodes from topic_folder smarty
1110
            $topics[$id]['topic_folder'] = newbbDisplayImage($topic_folder, $topic_folder_text);
1111
            // END irmtfan - add topic_folder_text for alt
1112
1113
            unset($topics[$id]['topic_poster_name'], $topics[$id]['topic_last_poster_name']);// irmtfan remove $topics[$id]["stats"] because it is not exist now
1114
        }
1115
1116
        if (count($topics) > 0) {
1117
            $sql = ' SELECT DISTINCT topic_id FROM ' . $this->handler->db->prefix('newbb_posts') . " WHERE attachment != ''" . ' AND topic_id IN (' . implode(',', array_keys($topics)) . ')';
1118
            if ($result = $this->handler->db->query($sql)) {
1119
                while (false !== (list($topic_id) = $this->handler->db->fetchRow($result))) {
1120
                    $topics[$topic_id]['attachment'] = '&nbsp;' . newbbDisplayImage('attachment', _MD_NEWBB_TOPICSHASATT);
1121
                }
1122
            }
1123
        }
1124
1125
        if (is_object($xoopsTpl)) {
1126
            $xoopsTpl->assign_by_ref('sticky', $sticky);
1127
            $xoopsTpl->assign_by_ref('topics', $topics);
1128
1129
            return;
1130
        }
1131
1132
        return [$topics, $sticky];
1133
    }
1134
1135
    // START irmtfan to create an array from selected keys of an array
1136
1137
    /**
1138
     * @param        $array
1139
     * @param  null  $keys
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $keys is correct as it would always require null to be passed?
Loading history...
1140
     * @return array
1141
     */
1142
    public function getFromKeys($array, $keys = null)
1143
    {
1144
        if (empty($keys)) {
1145
            return $array;
1146
        } // all keys
1147
        $keyarr = is_string($keys) ? explode(',', $keys) : $keys;
1148
        $keyarr = array_intersect(array_keys($array), $keyarr); // keys should be in array
1149
        $ret    = [];
1150
        foreach ($keyarr as $key) {
1151
            $ret[$key] = $array[$key];
1152
        }
1153
1154
        return $ret;
1155
    }
1156
    // END irmtfan to create an array from selected keys of an array
1157
}
1158