Completed
Push — master ( b610c1...53db9c )
by Michael
03:25 queued 01:39
created

NewsTopic::topic_rssurl()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 14

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 1
dl 18
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
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 32 and the first side effect is on line 24.

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
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author         XOOPS Development Team
18
 */
19
20
// defined('XOOPS_ROOT_PATH') || exit('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...
21
22
use Xmf\Module\Helper;
23
24
require_once XOOPS_ROOT_PATH . '/modules/news/class/xoopsstory.php';
25
require_once XOOPS_ROOT_PATH . '/modules/news/class/xoopstopic.php';
26
require_once XOOPS_ROOT_PATH . '/modules/news/class/tree.php';
27
require_once XOOPS_ROOT_PATH . '/modules/news/class/utility.php';
28
29
/**
30
 * Class NewsTopic
31
 */
32
class NewsTopic extends MyXoopsTopic
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
33
{
34
    public $menu;
35
    public $topic_description;
36
    public $topic_frontpage;
37
    public $topic_rssurl;
38
    public $topic_color;
39
40
    /**
41
     * @param int $topicid
42
     */
43 View Code Duplication
    public function __construct($topicid = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
44
    {
45
        $this->db    = XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug introduced by
The property db does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
        $this->table = $this->db->prefix('news_topics');
47
        if (is_array($topicid)) {
48
            $this->makeTopic($topicid);
49
        } elseif (0 != $topicid) {
50
            $this->getTopic((int)$topicid);
51
        } else {
52
            $this->topic_id = $topicid;
53
        }
54
    }
55
56
    /**
57
     * @param int    $none
58
     * @param        $seltopic
59
     * @param string $selname
60
     * @param string $onchange
61
     * @param bool   $checkRight
62
     * @param string $perm_type
63
     *
64
     * @return null|string
65
     */
66
    public function makeMyTopicSelBox(
67
        $none = 0,
68
        $seltopic = -1,
69
        $selname = '',
70
        $onchange = '',
71
        $checkRight = false,
72
        $perm_type = 'news_view'
73
    ) {
74
        $perms = '';
75 View Code Duplication
        if ($checkRight) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
76
            global $xoopsUser;
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...
77
            /** @var XoopsModuleHandler $moduleHandler */
78
            $moduleHandler = xoops_getHandler('module');
79
            $newsModule    = $moduleHandler->getByDirname('news');
80
            $groups        = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
81
            $gpermHandler  = xoops_getHandler('groupperm');
82
            $topics        = $gpermHandler->getItemIds($perm_type, $groups, $newsModule->getVar('mid'));
83
            if (count($topics) > 0) {
84
                $topics = implode(',', $topics);
85
                $perms  = ' AND topic_id IN (' . $topics . ') ';
86
            } else {
87
                return null;
88
            }
89
        }
90
91
        if ($seltopic != -1) {
92
            return $this->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange, $perms);
93
        } elseif (!empty($this->topic_id)) {
94
            return $this->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange, $perms);
95
        } else {
96
            return $this->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange, $perms);
97
        }
98
    }
99
100
    /**
101
     * makes a nicely ordered selection box
102
     *
103
     * @param        $title
104
     * @param string $order
105
     * @param int    $preset_id is used to specify a preselected item
106
     * @param int    $none      set $none to 1 to add a option with value 0
107
     *
108
     * @param string $sel_name
109
     * @param string $onchange
110
     * @param        $perms
111
     *
112
     * @return string
113
     */
114
    public function makeMySelBox(
115
        $title,
116
        $order = '',
117
        $preset_id = 0,
118
        $none = 0,
119
        $sel_name = 'topic_id',
120
        $onchange = '',
121
        $perms
122
    ) {
123
        $myts      = MyTextSanitizer::getInstance();
124
        $outbuffer = '';
0 ignored issues
show
Unused Code introduced by
$outbuffer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
125
        $outbuffer = "<select name='" . $sel_name . "'";
126
        if ('' !== $onchange) {
127
            $outbuffer .= " onchange='" . $onchange . "'";
128
        }
129
        $outbuffer .= ">\n";
130
        $sql       = 'SELECT topic_id, ' . $title . ' FROM ' . $this->table . ' WHERE (topic_pid=0)' . $perms;
131
        if ('' !== $order) {
132
            $sql .= " ORDER BY $order";
133
        }
134
        $result = $this->db->query($sql);
135
        if ($none) {
136
            $outbuffer .= "<option value='0'>----</option>\n";
137
        }
138
        while (list($catid, $name) = $this->db->fetchRow($result)) {
139
            $sel = '';
140
            if ($catid == $preset_id) {
141
                $sel = ' selected';
142
            }
143
            $name      = $myts->displayTarea($name);
144
            $outbuffer .= "<option value='$catid'$sel>$name</option>\n";
145
            $sel       = '';
146
            $arr       = $this->getChildTreeArray($catid, $order, $perms);
147
            foreach ($arr as $option) {
148
                $option['prefix'] = str_replace('.', '--', $option['prefix']);
149
                $catpath          = $option['prefix'] . '&nbsp;' . $myts->displayTarea($option[$title]);
150
151
                if ($option['topic_id'] == $preset_id) {
152
                    $sel = ' selected';
153
                }
154
                $outbuffer .= "<option value='" . $option['topic_id'] . "'$sel>$catpath</option>\n";
155
                $sel       = '';
156
            }
157
        }
158
        $outbuffer .= "</select>\n";
159
160
        return $outbuffer;
161
    }
162
163
    /**
164
     * @param int    $sel_id
165
     * @param string $order
166
     * @param string $perms
167
     * @param array  $parray
168
     * @param string $r_prefix
169
     *
170
     * @return array
171
     */
172
    public function getChildTreeArray($sel_id = 0, $order = '', $perms = '', $parray = [], $r_prefix = '')
173
    {
174
        $sql = 'SELECT * FROM ' . $this->table . ' WHERE (topic_pid=' . $sel_id . ')' . $perms;
175
        if ('' !== $order) {
176
            $sql .= " ORDER BY $order";
177
        }
178
        $result = $this->db->query($sql);
179
        $count  = $this->db->getRowsNum($result);
180
        if (0 == $count) {
181
            return $parray;
182
        }
183 View Code Duplication
        while ($row = $this->db->fetchArray($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
184
            $row['prefix'] = $r_prefix . '.';
185
            array_push($parray, $row);
186
            $parray = $this->getChildTreeArray($row['topic_id'], $order, $perms, $parray, $row['prefix']);
187
        }
188
189
        return $parray;
190
    }
191
192
    /**
193
     * @param $var
194
     *
195
     * @return mixed
196
     */
197
    public function getVar($var)
198
    {
199
        if (method_exists($this, $var)) {
200
            return $this->{$var}();
201
        } else {
202
            return $this->$var;
203
        }
204
    }
205
206
    /**
207
     * Get the total number of topics in the base
208
     * @param  bool $checkRight
209
     * @return null
210
     */
211
    public function getAllTopicsCount($checkRight = true)
212
    {
213
        $perms = '';
214 View Code Duplication
        if ($checkRight) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
215
            global $xoopsUser;
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...
216
            /** @var XoopsModuleHandler $moduleHandler */
217
            $moduleHandler = xoops_getHandler('module');
218
            $newsModule    = $moduleHandler->getByDirname('news');
219
            $groups        = is_object($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
220
            $gpermHandler  = xoops_getHandler('groupperm');
221
            $topics        = $gpermHandler->getItemIds('news_submit', $groups, $newsModule->getVar('mid'));
222
            if (count($topics) > 0) {
223
                $topics = implode(',', $topics);
224
                $perms  = ' WHERE topic_id IN (' . $topics . ') ';
225
            } else {
226
                return null;
227
            }
228
        }
229
230
        $sql   = 'SELECT count(topic_id) AS cpt FROM ' . $this->table . $perms;
231
        $array = $this->db->fetchArray($this->db->query($sql));
232
233
        return $array['cpt'];
234
    }
235
236
    /**
237
     * @param bool   $checkRight
238
     * @param string $permission
239
     *
240
     * @return array
241
     */
242
    public function getAllTopics($checkRight = true, $permission = 'news_view')
243
    {
244
        $topics_arr = [];
245
        $db         = XoopsDatabaseFactory::getDatabaseConnection();
246
        $table      = $db->prefix('news_topics');
247
        $sql        = 'SELECT * FROM ' . $table;
248
        if ($checkRight) {
249
            $topics = NewsUtility::getMyItemIds($permission);
250
            if (0 == count($topics)) {
251
                return [];
252
            }
253
            $topics = implode(',', $topics);
254
            $sql    .= ' WHERE topic_id IN (' . $topics . ')';
255
        }
256
        $sql    .= ' ORDER BY topic_title';
257
        $result = $db->query($sql);
258
        while ($array = $db->fetchArray($result)) {
259
            $topic = new NewsTopic();
260
            $topic->makeTopic($array);
261
            $topics_arr[$array['topic_id']] = $topic;
262
            unset($topic);
263
        }
264
265
        return $topics_arr;
266
    }
267
268
    /**
269
     * Returns the number of published news per topic
270
     */
271
    public function getNewsCountByTopic()
272
    {
273
        $ret    = [];
274
        $sql    = 'SELECT count(storyid) AS cpt, topicid FROM ' . $this->db->prefix('news_stories') . ' WHERE (published > 0 AND published <= ' . time() . ') AND (expired = 0 OR expired > ' . time() . ') GROUP BY topicid';
275
        $result = $this->db->query($sql);
276
        while ($row = $this->db->fetchArray($result)) {
277
            $ret[$row['topicid']] = $row['cpt'];
278
        }
279
280
        return $ret;
281
    }
282
283
    /**
284
     * Returns some stats about a topic
285
     * @param $topicid
286
     * @return array
287
     */
288
    public function getTopicMiniStats($topicid)
289
    {
290
        $ret          = [];
291
        $sql          = 'SELECT count(storyid) AS cpt1, sum(counter) AS cpt2 FROM ' . $this->db->prefix('news_stories') . ' WHERE (topicid=' . $topicid . ') AND (published>0 AND published <= ' . time() . ') AND (expired = 0 OR expired > ' . time() . ')';
292
        $result       = $this->db->query($sql);
293
        $row          = $this->db->fetchArray($result);
294
        $ret['count'] = $row['cpt1'];
295
        $ret['reads'] = $row['cpt2'];
296
297
        return $ret;
298
    }
299
300
    /**
301
     * @param $value
302
     */
303
    public function setMenu($value)
304
    {
305
        $this->menu = $value;
306
    }
307
308
    /**
309
     * @param $value
310
     */
311
    public function setTopic_color($value)
312
    {
313
        $this->topic_color = $value;
314
    }
315
316
    /**
317
     * @param $topicid
318
     */
319
    public function getTopic($topicid)
320
    {
321
        $sql   = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . '';
322
        $array = $this->db->fetchArray($this->db->query($sql));
323
        $this->makeTopic($array);
324
    }
325
326
    /**
327
     * @param $array
328
     */
329
    public function makeTopic($array)
330
    {
331
        if (is_array($array)) {
332
            foreach ($array as $key => $value) {
333
                $this->$key = $value;
334
            }
335
        }
336
    }
337
338
    /**
339
     * @return bool
340
     */
341
    public function store()
342
    {
343
        $myts              = MyTextSanitizer::getInstance();
344
        $title             = '';
345
        $imgurl            = '';
346
        $topic_description = $myts->censorString($this->topic_description);
347
        $topic_description = $myts->addSlashes($topic_description);
348
        $topic_rssurl      = $myts->addSlashes($this->topic_rssurl);
349
        $topic_color       = $myts->addSlashes($this->topic_color);
350
351
        $dirname = basename(dirname(__DIR__));
352
353
        if (isset($this->topic_title) && '' !== $this->topic_title) {
354
            $title = $myts->addSlashes($this->topic_title);
355
        }
356
        if (isset($this->topic_imgurl) && '' !== $this->topic_imgurl) {
357
            $imgurl = $myts->addSlashes($this->topic_imgurl);
358
        }
359
        if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) {
360
            $this->topic_pid = 0;
361
        }
362
        $topic_frontpage = (int)$this->topic_frontpage;
363
        $insert          = false;
364
        if (empty($this->topic_id)) {
365
            $insert         = true;
366
            $this->topic_id = $this->db->genId($this->table . '_topic_id_seq');
367
            $sql            = sprintf(
368
                "INSERT INTO %s (topic_id, topic_pid, topic_imgurl, topic_title, menu, topic_description, topic_frontpage, topic_rssurl, topic_color) VALUES (%u, %u, '%s', '%s', %u, '%s', %d, '%s', '%s')",
369
                $this->table,
370
                (int)$this->topic_id,
371
                (int)$this->topic_pid,
372
                $imgurl,
373
                                      $title,
374
                (int)$this->menu,
375
                $topic_description,
376
                $topic_frontpage,
377
                $topic_rssurl,
378
                $topic_color
379
            );
380
        } else {
381
            $sql = sprintf(
382
                "UPDATE %s SET topic_pid = %u, topic_imgurl = '%s', topic_title = '%s', menu=%d, topic_description='%s', topic_frontpage=%d, topic_rssurl='%s', topic_color='%s' WHERE topic_id = %u",
383
                $this->table,
384
                (int)$this->topic_pid,
385
                $imgurl,
386
                $title,
387
                (int)$this->menu,
388
                           $topic_description,
389
                $topic_frontpage,
390
                $topic_rssurl,
391
                $topic_color,
392
                (int)$this->topic_id
393
            );
394
        }
395
        if (!$result = $this->db->query($sql)) {
396
            // TODO: Replace with something else
397
            //            ErrorHandler::show('0022');
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...
398
399
            $helper = Helper::getHelper($dirname);
400
            $helper->redirect('admin/index.php', 5, $this->db->error());
401
        } else {
402
            if ($insert) {
403
                $this->topic_id = $this->db->getInsertId();
404
            }
405
        }
406
407
        if (true === $this->use_permission) {
408
            $xt            = new MyXoopsTree($this->table, 'topic_id', 'topic_pid');
409
            $parent_topics = $xt->getAllParentId($this->topic_id);
410 View Code Duplication
            if (!empty($this->m_groups) && is_array($this->m_groups)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
411
                foreach ($this->m_groups as $m_g) {
0 ignored issues
show
Bug introduced by
The property m_groups does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
412
                    $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g);
413
                    $add             = true;
414
                    // only grant this permission when the group has this permission in all parent topics of the created topic
415
                    foreach ($parent_topics as $p_topic) {
416
                        if (!in_array($p_topic, $moderate_topics)) {
417
                            $add = false;
418
                            continue;
419
                        }
420
                    }
421
                    if (true === $add) {
422
                        $xp = new XoopsPerms();
423
                        $xp->setModuleId($this->mid);
424
                        $xp->setName('ModInTopic');
425
                        $xp->setItemId($this->topic_id);
426
                        $xp->store();
427
                        $xp->addGroup($m_g);
428
                    }
429
                }
430
            }
431 View Code Duplication
            if (!empty($this->s_groups) && is_array($this->s_groups)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
432
                foreach ($this->s_groups as $s_g) {
0 ignored issues
show
Bug introduced by
The property s_groups does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
433
                    $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g);
434
                    $add           = true;
435
                    foreach ($parent_topics as $p_topic) {
436
                        if (!in_array($p_topic, $submit_topics)) {
437
                            $add = false;
438
                            continue;
439
                        }
440
                    }
441
                    if (true === $add) {
442
                        $xp = new XoopsPerms();
443
                        $xp->setModuleId($this->mid);
444
                        $xp->setName('SubmitInTopic');
445
                        $xp->setItemId($this->topic_id);
446
                        $xp->store();
447
                        $xp->addGroup($s_g);
448
                    }
449
                }
450
            }
451 View Code Duplication
            if (!empty($this->r_groups) && is_array($this->r_groups)) {
0 ignored issues
show
Bug introduced by
The property r_groups does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
452
                foreach ($this->s_groups as $r_g) {
453
                    $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g);
454
                    $add         = true;
455
                    foreach ($parent_topics as $p_topic) {
456
                        if (!in_array($p_topic, $read_topics)) {
457
                            $add = false;
458
                            continue;
459
                        }
460
                    }
461
                    if (true === $add) {
462
                        $xp = new XoopsPerms();
463
                        $xp->setModuleId($this->mid);
464
                        $xp->setName('ReadInTopic');
465
                        $xp->setItemId($this->topic_id);
466
                        $xp->store();
467
                        $xp->addGroup($r_g);
468
                    }
469
                }
470
            }
471
        }
472
473
        return true;
474
    }
475
476
    /**
477
     * @param $value
478
     */
479
    public function setTopicRssUrl($value)
480
    {
481
        $this->topic_rssurl = $value;
482
    }
483
484
    /**
485
     * @param string $format
486
     *
487
     * @return mixed
488
     */
489 View Code Duplication
    public function topic_rssurl($format = 'S')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
490
    {
491
        $myts = MyTextSanitizer::getInstance();
492
        switch ($format) {
493
            case 'S':
494
                $topic_rssurl = $myts->displayTarea($this->topic_rssurl);
495
                break;
496
            case 'P':
497
                $topic_rssurl = $myts->previewTarea($this->topic_rssurl);
498
                break;
499
            case 'F':
500
            case 'E':
501
                $topic_rssurl = $myts->htmlSpecialChars($this->topic_rssurl);
502
                break;
503
        }
504
505
        return $topic_rssurl;
0 ignored issues
show
Bug introduced by
The variable $topic_rssurl does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
506
    }
507
508
    /**
509
     * @param string $format
510
     *
511
     * @return mixed
512
     */
513 View Code Duplication
    public function topic_color($format = 'S')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
514
    {
515
        $myts = MyTextSanitizer::getInstance();
516
        switch ($format) {
517
            case 'S':
518
                $topic_color = $myts->displayTarea($this->topic_color);
519
                break;
520
            case 'P':
521
                $topic_color = $myts->previewTarea($this->topic_color);
522
                break;
523
            case 'F':
524
            case 'E':
525
                $topic_color = $myts->htmlSpecialChars($this->topic_color);
526
                break;
527
        }
528
529
        return $topic_color;
0 ignored issues
show
Bug introduced by
The variable $topic_color does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
530
    }
531
532
    /**
533
     * @return mixed
534
     */
535
    public function menu()
536
    {
537
        return $this->menu;
538
    }
539
540
    /**
541
     * @param string $format
542
     *
543
     * @return mixed
544
     */
545 View Code Duplication
    public function topic_description($format = 'S')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
546
    {
547
        $myts = MyTextSanitizer::getInstance();
548
        switch ($format) {
549
            case 'S':
550
                $topic_description = $myts->displayTarea($this->topic_description, 1);
551
                break;
552
            case 'P':
553
                $topic_description = $myts->previewTarea($this->topic_description);
554
                break;
555
            case 'F':
556
            case 'E':
557
                $topic_description = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_description));
558
                break;
559
        }
560
561
        return $topic_description;
0 ignored issues
show
Bug introduced by
The variable $topic_description does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
562
    }
563
564
    /**
565
     * @param string $format
566
     *
567
     * @return mixed
568
     */
569 View Code Duplication
    public function topic_imgurl($format = 'S')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
570
    {
571
        if ('' === trim($this->topic_imgurl)) {
572
            $this->topic_imgurl = 'blank.png';
573
        }
574
        $myts = MyTextSanitizer::getInstance();
575
        switch ($format) {
576
            case 'S':
577
                $imgurl = $myts->htmlSpecialChars($this->topic_imgurl);
578
                break;
579
            case 'E':
580
                $imgurl = $myts->htmlSpecialChars($this->topic_imgurl);
581
                break;
582
            case 'P':
583
                $imgurl = $myts->stripSlashesGPC($this->topic_imgurl);
584
                $imgurl = $myts->htmlSpecialChars($imgurl);
585
                break;
586
            case 'F':
587
                $imgurl = $myts->stripSlashesGPC($this->topic_imgurl);
588
                $imgurl = $myts->htmlSpecialChars($imgurl);
589
                break;
590
        }
591
592
        return $imgurl;
0 ignored issues
show
Bug introduced by
The variable $imgurl does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
593
    }
594
595
    /**
596
     * @param $topic
597
     * @param $topicstitles
598
     *
599
     * @return mixed
600
     */
601
    public function getTopicTitleFromId($topic, &$topicstitles)
602
    {
603
        $myts = MyTextSanitizer::getInstance();
604
        $sql  = 'SELECT topic_id, topic_title, topic_imgurl FROM ' . $this->table . ' WHERE ';
605 View Code Duplication
        if (!is_array($topic)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
606
            $sql .= ' topic_id=' . (int)$topic;
607
        } else {
608
            if (count($topic) > 0) {
609
                $sql .= ' topic_id IN (' . implode(',', $topic) . ')';
610
            } else {
611
                return null;
612
            }
613
        }
614
        $result = $this->db->query($sql);
615
        while ($row = $this->db->fetchArray($result)) {
616
            $topicstitles[$row['topic_id']] = [
617
                'title'   => $myts->displayTarea($row['topic_title']),
618
                'picture' => XOOPS_URL . '/uploads/news/image/' . $row['topic_imgurl']
619
            ];
620
        }
621
622
        return $topicstitles;
623
    }
624
625
    /**
626
     * @param bool $frontpage
627
     * @param bool $perms
628
     *
629
     * @return array|string
630
     */
631
    public function &getTopicsList($frontpage = false, $perms = false)
632
    {
633
        $sql = 'SELECT topic_id, topic_pid, topic_title, topic_color FROM ' . $this->table . ' WHERE 1 ';
634
        if ($frontpage) {
635
            $sql .= ' AND topic_frontpage=1';
636
        }
637
        if ($perms) {
638
            $topicsids = [];
0 ignored issues
show
Unused Code introduced by
$topicsids is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
639
            $topicsids = NewsUtility::getMyItemIds();
640
            if (0 == count($topicsids)) {
641
                return '';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ''; (string) is incompatible with the return type of the parent method MyXoopsTopic::getTopicsList of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
642
            }
643
            $topics = implode(',', $topicsids);
644
            $sql    .= ' AND topic_id IN (' . $topics . ')';
645
        }
646
        $result = $this->db->query($sql);
647
        $ret    = [];
648
        $myts   = MyTextSanitizer::getInstance();
649 View Code Duplication
        while ($myrow = $this->db->fetchArray($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
650
            $ret[$myrow['topic_id']] = [
651
                'title' => $myts->displayTarea($myrow['topic_title']),
652
                'pid'   => $myrow['topic_pid'],
653
                'color' => $myrow['topic_color']
654
            ];
655
        }
656
657
        return $ret;
658
    }
659
660
    /**
661
     * @param $value
662
     */
663
    public function setTopicDescription($value)
664
    {
665
        $this->topic_description = $value;
666
    }
667
668
    /**
669
     * @return mixed
670
     */
671
    public function topic_frontpage()
672
    {
673
        return $this->topic_frontpage;
674
    }
675
676
    /**
677
     * @param $value
678
     */
679
    public function setTopicFrontpage($value)
680
    {
681
        $this->topic_frontpage = (int)$value;
682
    }
683
}
684