Completed
Pull Request — master (#16)
by Michael
01:51
created

class/xoopstopic.php (10 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * 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       XOOPS Project (https://xoops.org)
13
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-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
 * @deprecated
18
 */
19
20
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
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...
21
//$GLOBALS['xoopsLogger']->addDeprecated("'/class/xoopstopic.php' is deprecated since XOOPS 2.5.4, please create your own class instead.");
22
23
require_once XOOPS_ROOT_PATH . '/modules/news/class/xoopstree.php';
24
25
/**
26
 * Class MyXoopsTopic
27
 */
28
class MyXoopsTopic
29
{
30
    public $table;
31
    public $topic_id;
32
    public $topic_pid;
33
    public $topic_title;
34
    public $topic_imgurl;
35
    public $prefix; // only used in topic tree
36
    public $use_permission = false;
37
    public $mid; // module id used for setting permission
38
39
    /**
40
     * @param     $table
41
     * @param int $topicid
42
     */
43 View Code Duplication
    public function __construct($table, $topicid = 0)
0 ignored issues
show
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();
46
        $this->table = $table;
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 $value
58
     */
59
    public function setTopicTitle($value)
60
    {
61
        $this->topic_title = $value;
62
    }
63
64
    /**
65
     * @param $value
66
     */
67
    public function setTopicImgurl($value)
68
    {
69
        $this->topic_imgurl = $value;
70
    }
71
72
    /**
73
     * @param $value
74
     */
75
    public function setTopicPid($value)
76
    {
77
        $this->topic_pid = $value;
78
    }
79
80
    /**
81
     * @param $topicid
82
     */
83 View Code Duplication
    public function getTopic($topicid)
0 ignored issues
show
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...
84
    {
85
        $topicid = (int)$topicid;
86
        $sql     = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . '';
87
        $array   = $this->db->fetchArray($this->db->query($sql));
88
        $this->makeTopic($array);
89
    }
90
91
    /**
92
     * @param $array
93
     */
94
    public function makeTopic($array)
95
    {
96
        foreach ($array as $key => $value) {
97
            $this->$key = $value;
98
        }
99
    }
100
101
    /**
102
     * @param $mid
103
     */
104
    public function usePermission($mid)
105
    {
106
        $this->mid            = $mid;
107
        $this->use_permission = true;
108
    }
109
110
    /**
111
     * @return bool
112
     */
113
    public function store()
114
    {
115
        $myts   = MyTextSanitizer::getInstance();
116
        $title  = '';
117
        $imgurl = '';
118
        if (isset($this->topic_title) && '' !== $this->topic_title) {
119
            $title = $myts->addSlashes($this->topic_title);
120
        }
121
        if (isset($this->topic_imgurl) && '' !== $this->topic_imgurl) {
122
            $imgurl = $myts->addSlashes($this->topic_imgurl);
123
        }
124
        if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) {
125
            $this->topic_pid = 0;
126
        }
127
        if (empty($this->topic_id)) {
128
            $this->topic_id = $this->db->genId($this->table . '_topic_id_seq');
129
            $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);
130
        } else {
131
            $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);
132
        }
133
        if (!$result = $this->db->query($sql)) {
134
            ErrorHandler::show('0022');
135
        }
136
        if (true === $this->use_permission) {
137
            if (empty($this->topic_id)) {
138
                $this->topic_id = $this->db->getInsertId();
139
            }
140
            $xt            = new MyXoopsTree($this->table, 'topic_id', 'topic_pid');
141
            $parent_topics = $xt->getAllParentId($this->topic_id);
142 View Code Duplication
            if (!empty($this->m_groups) && is_array($this->m_groups)) {
143
                foreach ($this->m_groups as $m_g) {
144
                    $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g);
145
                    $add             = true;
146
                    // only grant this permission when the group has this permission in all parent topics of the created topic
147
                    foreach ($parent_topics as $p_topic) {
148
                        if (!in_array($p_topic, $moderate_topics)) {
149
                            $add = false;
150
                            continue;
151
                        }
152
                    }
153
                    if (true === $add) {
154
                        $xp = new XoopsPerms();
155
                        $xp->setModuleId($this->mid);
156
                        $xp->setName('ModInTopic');
157
                        $xp->setItemId($this->topic_id);
158
                        $xp->store();
159
                        $xp->addGroup($m_g);
160
                    }
161
                }
162
            }
163 View Code Duplication
            if (!empty($this->s_groups) && is_array($this->s_groups)) {
164
                foreach ($s_groups as $s_g) {
165
                    $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g);
166
                    $add           = true;
167
                    foreach ($parent_topics as $p_topic) {
168
                        if (!in_array($p_topic, $submit_topics)) {
169
                            $add = false;
170
                            continue;
171
                        }
172
                    }
173
                    if (true === $add) {
174
                        $xp = new XoopsPerms();
175
                        $xp->setModuleId($this->mid);
176
                        $xp->setName('SubmitInTopic');
177
                        $xp->setItemId($this->topic_id);
178
                        $xp->store();
179
                        $xp->addGroup($s_g);
180
                    }
181
                }
182
            }
183 View Code Duplication
            if (!empty($this->r_groups) && is_array($this->r_groups)) {
184
                foreach ($r_groups as $r_g) {
185
                    $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g);
186
                    $add         = true;
187
                    foreach ($parent_topics as $p_topic) {
188
                        if (!in_array($p_topic, $read_topics)) {
189
                            $add = false;
190
                            continue;
191
                        }
192
                    }
193
                    if (true === $add) {
194
                        $xp = new XoopsPerms();
195
                        $xp->setModuleId($this->mid);
196
                        $xp->setName('ReadInTopic');
197
                        $xp->setItemId($this->topic_id);
198
                        $xp->store();
199
                        $xp->addGroup($r_g);
200
                    }
201
                }
202
            }
203
        }
204
205
        return true;
206
    }
207
208
    public function delete()
209
    {
210
        $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->table, $this->topic_id);
211
        $this->db->query($sql);
212
    }
213
214
    /**
215
     * @return int
216
     */
217
    public function topic_id()
218
    {
219
        return $this->topic_id;
220
    }
221
222
    public function topic_pid()
223
    {
224
        return $this->topic_pid;
225
    }
226
227
    /**
228
     * @param string $format
229
     *
230
     * @return mixed
231
     */
232 View Code Duplication
    public function topic_title($format = 'S')
0 ignored issues
show
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...
233
    {
234
        $myts = MyTextSanitizer::getInstance();
235
        switch ($format) {
236
            case 'S':
237
            case 'E':
238
                $title = $myts->htmlSpecialChars($this->topic_title);
239
                break;
240
            case 'P':
241
            case 'F':
242
                $title = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_title));
243
                break;
244
        }
245
246
        return $title;
247
    }
248
249
    /**
250
     * @param string $format
251
     *
252
     * @return mixed
253
     */
254 View Code Duplication
    public function topic_imgurl($format = 'S')
0 ignored issues
show
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...
255
    {
256
        $myts = MyTextSanitizer::getInstance();
257
        switch ($format) {
258
            case 'S':
259
            case 'E':
260
                $imgurl = $myts->htmlSpecialChars($this->topic_imgurl);
261
                break;
262
            case 'P':
263
            case 'F':
264
                $imgurl = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_imgurl));
265
                break;
266
        }
267
268
        return $imgurl;
269
    }
270
271
    /**
272
     * @return null
273
     */
274
    public function prefix()
275
    {
276
        if (isset($this->prefix)) {
277
            return $this->prefix;
278
        }
279
280
        return null;
281
    }
282
283
    /**
284
     * @return array
285
     */
286 View Code Duplication
    public function getFirstChildTopics()
0 ignored issues
show
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...
287
    {
288
        $ret       = [];
289
        $xt        = new MyXoopsTree($this->table, 'topic_id', 'topic_pid');
290
        $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title');
291
        if (is_array($topic_arr) && count($topic_arr)) {
292
            foreach ($topic_arr as $topic) {
293
                $ret[] = new MyXoopsTopic($this->table, $topic);
294
            }
295
        }
296
297
        return $ret;
298
    }
299
300
    /**
301
     * @return array
302
     */
303 View Code Duplication
    public function getAllChildTopics()
0 ignored issues
show
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...
304
    {
305
        $ret       = [];
306
        $xt        = new MyXoopsTree($this->table, 'topic_id', 'topic_pid');
307
        $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title');
308
        if (is_array($topic_arr) && count($topic_arr)) {
309
            foreach ($topic_arr as $topic) {
310
                $ret[] = new MyXoopsTopic($this->table, $topic);
311
            }
312
        }
313
314
        return $ret;
315
    }
316
317
    /**
318
     * @return array
319
     */
320 View Code Duplication
    public function getChildTopicsTreeArray()
0 ignored issues
show
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...
321
    {
322
        $ret       = [];
323
        $xt        = new MyXoopsTree($this->table, 'topic_id', 'topic_pid');
324
        $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title');
325
        if (is_array($topic_arr) && count($topic_arr)) {
326
            foreach ($topic_arr as $topic) {
327
                $ret[] = new MyXoopsTopic($this->table, $topic);
328
            }
329
        }
330
331
        return $ret;
332
    }
333
334
    /**
335
     * @param int    $none
336
     * @param        $seltopic
337
     * @param string $selname
338
     * @param string $onchange
339
     */
340
    public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '')
341
    {
342
        $xt = new MyXoopsObjectTree($this->table, 'topic_id', 'topic_pid');
343
        if ($seltopic != -1) {
344
            $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);
345
        } elseif (!empty($this->topic_id)) {
346
            $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange);
347
        } else {
348
            $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange);
349
        }
350
    }
351
352
    //generates nicely formatted linked path from the root id to a given id
353
354
    /**
355
     * @param $funcURL
356
     *
357
     * @return mixed
358
     */
359 View Code Duplication
    public function getNiceTopicPathFromId($funcURL)
0 ignored issues
show
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...
360
    {
361
        $xt  = new MyXoopsObjectTree($this->table, 'topic_id', 'topic_pid');
362
        $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL);
363
364
        return $ret;
365
    }
366
367
    /**
368
     * @return mixed
369
     */
370 View Code Duplication
    public function getAllChildTopicsId()
0 ignored issues
show
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...
371
    {
372
        $xt  = new MyXoopsObjectTree($this->table, 'topic_id', 'topic_pid');
373
        $ret = $xt->getAllChildId($this->topic_id, 'topic_title');
374
375
        return $ret;
376
    }
377
378
    /**
379
     * @return array
380
     */
381
    public function getTopicsList()
382
    {
383
        $result = $this->db->query('SELECT topic_id, topic_pid, topic_title FROM ' . $this->table);
384
        $ret    = [];
385
        $myts   = MyTextSanitizer::getInstance();
386 View Code Duplication
        while ($myrow = $this->db->fetchArray($result)) {
387
            $ret[$myrow['topic_id']] = [
388
                'title' => $myts->htmlspecialchars($myrow['topic_title']),
389
                'pid'   => $myrow['topic_pid']
390
            ];
391
        }
392
393
        return $ret;
394
    }
395
396
    /**
397
     * @param $pid
398
     * @param $title
399
     *
400
     * @return bool
401
     */
402
    public function topicExists($pid, $title)
403
    {
404
        $sql = 'SELECT COUNT(*) FROM ' . $this->table . ' WHERE topic_pid = ' . (int)$pid . " AND topic_title = '" . trim($title) . "'";
405
        $rs  = $this->db->query($sql);
406
        list($count) = $this->db->fetchRow($rs);
407
        if ($count > 0) {
408
            return true;
409
        } else {
410
            return false;
411
        }
412
    }
413
}
414