Completed
Pull Request — master (#53)
by Michael
10:39
created

XoopsTopic::topic_imgurl()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 12

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
rs 8.8571
cc 5
eloc 12
nc 5
nop 1
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 30 and the first side effect is on line 21.

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
 * XOOPS news topic
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17
 * @version             $Id: xoopstopic.php 13090 2015-06-16 20:44:29Z beckmi $
18
 * @deprecated
19
 */
20
21
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
22
23
$GLOBALS['xoopsLogger']->addDeprecated("'/class/xoopstopic.php' is deprecated since XOOPS 2.5.4, please create your own class instead.");
24
25
include_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
26
27
/**
28
 * Class XoopsTopic
29
 */
30
class XoopsTopic
31
{
32
    public $table;
33
    public $topic_id;
34
    public $topic_pid;
35
    public $topic_title;
36
    public $topic_imgurl;
37
    public $prefix; // only used in topic tree
38
    public $use_permission = false;
39
    public $mid; // module id used for setting permission
40
41
    /**
42
     * @param     $table
43
     * @param int $topicid
44
     */
45
    public function __construct($table, $topicid = 0)
46
    {
47
        $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...
48
        $this->table = $table;
49
        if (is_array($topicid)) {
50
            $this->makeTopic($topicid);
51
        } elseif ($topicid != 0) {
52
            $this->getTopic((int)$topicid);
53
        } else {
54
            $this->topic_id = $topicid;
55
        }
56
    }
57
58
    /**
59
     * @param $value
60
     */
61
    public function setTopicTitle($value)
62
    {
63
        $this->topic_title = $value;
64
    }
65
66
    /**
67
     * @param $value
68
     */
69
    public function setTopicImgurl($value)
70
    {
71
        $this->topic_imgurl = $value;
72
    }
73
74
    /**
75
     * @param $value
76
     */
77
    public function setTopicPid($value)
78
    {
79
        $this->topic_pid = $value;
80
    }
81
82
    /**
83
     * @param $topicid
84
     */
85 View Code Duplication
    public function getTopic($topicid)
86
    {
87
        $topicid = (int)$topicid;
88
        $sql     = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . '';
89
        $array   = $this->db->fetchArray($this->db->query($sql));
90
        $this->makeTopic($array);
91
    }
92
93
    /**
94
     * @param $array
95
     */
96
    public function makeTopic($array)
97
    {
98
        foreach ($array as $key => $value) {
99
            $this->$key = $value;
100
        }
101
    }
102
103
    /**
104
     * @param $mid
105
     */
106
    public function usePermission($mid)
107
    {
108
        $this->mid            = $mid;
109
        $this->use_permission = true;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function store()
116
    {
117
        $myts   = MyTextSanitizer::getInstance();
118
        $title  = '';
119
        $imgurl = '';
120
        if (isset($this->topic_title) && $this->topic_title != '') {
121
            $title = $myts->addSlashes($this->topic_title);
122
        }
123
        if (isset($this->topic_imgurl) && $this->topic_imgurl != '') {
124
            $imgurl = $myts->addSlashes($this->topic_imgurl);
125
        }
126
        if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) {
127
            $this->topic_pid = 0;
128
        }
129
        if (empty($this->topic_id)) {
130
            $this->topic_id = $this->db->genId($this->table . '_topic_id_seq');
131
            $sql            = sprintf("INSERT INTO %s (topic_id, topic_pid, topic_imgurl, topic_title) VALUES (%u, %u, '%s', '%s')", $this->table, $this->topic_id, $this->topic_pid, $imgurl, $title);
132
        } else {
133
            $sql = sprintf("UPDATE %s SET topic_pid = %u, topic_imgurl = '%s', topic_title = '%s' WHERE topic_id = %u", $this->table, $this->topic_pid, $imgurl, $title, $this->topic_id);
134
        }
135
        if (!$result = $this->db->query($sql)) {
136
            ErrorHandler::show('0022');
137
        }
138
        if ($this->use_permission == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
139
            if (empty($this->topic_id)) {
140
                $this->topic_id = $this->db->getInsertId();
141
            }
142
            $xt            = new XoopsTree($this->table, 'topic_id', 'topic_pid');
143
            $parent_topics = $xt->getAllParentId($this->topic_id);
144 View Code Duplication
            if (!empty($this->m_groups) && is_array($this->m_groups)) {
145
                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...
146
                    $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g);
147
                    $add             = true;
148
                    // only grant this permission when the group has this permission in all parent topics of the created topic
149
                    foreach ($parent_topics as $p_topic) {
150
                        if (!in_array($p_topic, $moderate_topics)) {
151
                            $add = false;
152
                            continue;
153
                        }
154
                    }
155
                    if ($add == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
156
                        $xp = new XoopsPerms();
157
                        $xp->setModuleId($this->mid);
158
                        $xp->setName('ModInTopic');
159
                        $xp->setItemId($this->topic_id);
160
                        $xp->store();
161
                        $xp->addGroup($m_g);
162
                    }
163
                }
164
            }
165 View Code Duplication
            if (!empty($this->s_groups) && is_array($this->s_groups)) {
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...
166
                foreach ($s_groups as $s_g) {
0 ignored issues
show
Bug introduced by
The variable $s_groups does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
167
                    $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g);
168
                    $add           = true;
169
                    foreach ($parent_topics as $p_topic) {
170
                        if (!in_array($p_topic, $submit_topics)) {
171
                            $add = false;
172
                            continue;
173
                        }
174
                    }
175
                    if ($add == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
176
                        $xp = new XoopsPerms();
177
                        $xp->setModuleId($this->mid);
178
                        $xp->setName('SubmitInTopic');
179
                        $xp->setItemId($this->topic_id);
180
                        $xp->store();
181
                        $xp->addGroup($s_g);
182
                    }
183
                }
184
            }
185 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...
186
                foreach ($r_groups as $r_g) {
0 ignored issues
show
Bug introduced by
The variable $r_groups does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
187
                    $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g);
188
                    $add         = true;
189
                    foreach ($parent_topics as $p_topic) {
190
                        if (!in_array($p_topic, $read_topics)) {
191
                            $add = false;
192
                            continue;
193
                        }
194
                    }
195
                    if ($add == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
196
                        $xp = new XoopsPerms();
197
                        $xp->setModuleId($this->mid);
198
                        $xp->setName('ReadInTopic');
199
                        $xp->setItemId($this->topic_id);
200
                        $xp->store();
201
                        $xp->addGroup($r_g);
202
                    }
203
                }
204
            }
205
        }
206
207
        return true;
208
    }
209
210
    public function delete()
211
    {
212
        $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->table, $this->topic_id);
213
        $this->db->query($sql);
214
    }
215
216
    /**
217
     * @return int
218
     */
219
    public function topic_id()
220
    {
221
        return $this->topic_id;
222
    }
223
224
    public function topic_pid()
225
    {
226
        return $this->topic_pid;
227
    }
228
229
    /**
230
     * @param string $format
231
     *
232
     * @return mixed
233
     */
234 View Code Duplication
    public function topic_title($format = 'S')
235
    {
236
        $myts = MyTextSanitizer::getInstance();
237
        switch ($format) {
238
            case 'S':
239
            case 'E':
240
                $title = $myts->htmlSpecialChars($this->topic_title);
241
                break;
242
            case 'P':
243
            case 'F':
244
                $title = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_title));
245
                break;
246
        }
247
248
        return $title;
0 ignored issues
show
Bug introduced by
The variable $title 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...
249
    }
250
251
    /**
252
     * @param string $format
253
     *
254
     * @return mixed
255
     */
256 View Code Duplication
    public function topic_imgurl($format = 'S')
257
    {
258
        $myts = MyTextSanitizer::getInstance();
259
        switch ($format) {
260
            case 'S':
261
            case 'E':
262
                $imgurl = $myts->htmlSpecialChars($this->topic_imgurl);
263
                break;
264
            case 'P':
265
            case 'F':
266
                $imgurl = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_imgurl));
267
                break;
268
        }
269
270
        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...
271
    }
272
273
    public function prefix()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
274
    {
275
        if (isset($this->prefix)) {
276
            return $this->prefix;
277
        }
278
        return null;
279
    }
280
281
    /**
282
     * @return array
283
     */
284 View Code Duplication
    public function getFirstChildTopics()
285
    {
286
        $ret       = array();
287
        $xt        = new XoopsTree($this->table, 'topic_id', 'topic_pid');
288
        $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title');
289
        if (is_array($topic_arr) && count($topic_arr)) {
290
            foreach ($topic_arr as $topic) {
291
                $ret[] = new XoopsTopic($this->table, $topic);
292
            }
293
        }
294
295
        return $ret;
296
    }
297
298
    /**
299
     * @return array
300
     */
301 View Code Duplication
    public function getAllChildTopics()
302
    {
303
        $ret       = array();
304
        $xt        = new XoopsTree($this->table, 'topic_id', 'topic_pid');
305
        $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title');
306
        if (is_array($topic_arr) && count($topic_arr)) {
307
            foreach ($topic_arr as $topic) {
308
                $ret[] = new XoopsTopic($this->table, $topic);
309
            }
310
        }
311
312
        return $ret;
313
    }
314
315
    /**
316
     * @return array
317
     */
318 View Code Duplication
    public function getChildTopicsTreeArray()
319
    {
320
        $ret       = array();
321
        $xt        = new XoopsTree($this->table, 'topic_id', 'topic_pid');
322
        $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title');
323
        if (is_array($topic_arr) && count($topic_arr)) {
324
            foreach ($topic_arr as $topic) {
325
                $ret[] = new XoopsTopic($this->table, $topic);
326
            }
327
        }
328
329
        return $ret;
330
    }
331
332
    /**
333
     * @param int    $none
334
     * @param        $seltopic
335
     * @param string $selname
336
     * @param string $onchange
337
     */
338
    public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '')
339
    {
340
        $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
341
        if ($seltopic != -1) {
342
            $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);
343
        } elseif (!empty($this->topic_id)) {
344
            $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange);
345
        } else {
346
            $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange);
347
        }
348
    }
349
350
    //generates nicely formatted linked path from the root id to a given id
351
    /**
352
     * @param $funcURL
353
     *
354
     * @return string
355
     */
356 View Code Duplication
    public function getNiceTopicPathFromId($funcURL)
357
    {
358
        $xt  = new XoopsTree($this->table, 'topic_id', 'topic_pid');
359
        $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL);
360
361
        return $ret;
362
    }
363
364
    /**
365
     * @return array
366
     */
367 View Code Duplication
    public function getAllChildTopicsId()
368
    {
369
        $xt  = new XoopsTree($this->table, 'topic_id', 'topic_pid');
370
        $ret = $xt->getAllChildId($this->topic_id, 'topic_title');
371
372
        return $ret;
373
    }
374
375
    /**
376
     * @return array
377
     */
378
    public function getTopicsList()
379
    {
380
        $result = $this->db->query('SELECT topic_id, topic_pid, topic_title FROM ' . $this->table);
381
        $ret    = array();
382
        $myts   = MyTextSanitizer::getInstance();
383
        while ($myrow = $this->db->fetchArray($result)) {
384
            $ret[$myrow['topic_id']] = array('title' => $myts->htmlspecialchars($myrow['topic_title']), 'pid' => $myrow['topic_pid']);
385
        }
386
387
        return $ret;
388
    }
389
390
    /**
391
     * @param $pid
392
     * @param $title
393
     *
394
     * @return bool
395
     */
396
    public function topicExists($pid, $title)
397
    {
398
        $sql = 'SELECT COUNT(*) from ' . $this->table . ' WHERE topic_pid = ' . (int)$pid . " AND topic_title = '" . trim($title) . "'";
399
        $rs  = $this->db->query($sql);
400
        list($count) = $this->db->fetchRow($rs);
401
        if ($count > 0) {
402
            return true;
403
        } else {
404
            return false;
405
        }
406
    }
407
}
408