Completed
Push — master ( 7bb5bf...9a4e50 )
by Richard
28:24 queued 22s
created

htdocs/class/xoopscomments.php (4 issues)

Labels
Severity
1
<?php
2
/**
3
 * XOOPS comments
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 (https://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
 */
18
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
include_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
22
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
23
include_once XOOPS_ROOT_PATH . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/comment.php';
24
25
$GLOBALS['xoopsLogger']->addDeprecated("'/class/xoopscommments.php' is deprecated since XOOPS 2.5.4, please use '/kernel/comment.php' instead.");
26
27
/**
28
 * Xoops Comments Object Class
29
 *
30
 * @author              Kazumi Ono <[email protected]>
31
 * @author              John Neill <[email protected]>
32
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
33
 * @package             kernel
34
 * @subpackage          comments
35
 * @access              public
36
 */
37
class XoopsComments extends XoopsObject
38
{
39
    public $ctable;
40
    /**
41
     * @var \XoopsMySQLDatabase
42
     */
43
    public $db;
44
45
    /**
46
     * @param      $ctable
47
     * @param null|array $id
48
     */
49
    public function __construct($ctable, $id = null)
50
    {
51
        $this->ctable = $ctable;
52
        $this->db     = XoopsDatabaseFactory::getDatabaseConnection();
53
        parent::__construct();
54
        $this->initVar('comment_id', XOBJ_DTYPE_INT, null, false);
55
        $this->initVar('item_id', XOBJ_DTYPE_INT, null, false);
56
        $this->initVar('order', XOBJ_DTYPE_INT, null, false);
57
        $this->initVar('mode', XOBJ_DTYPE_OTHER, null, false);
58
        $this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, false, 255);
59
        $this->initVar('comment', XOBJ_DTYPE_TXTAREA, null, false, null);
60
        $this->initVar('ip', XOBJ_DTYPE_OTHER, null, false);
61
        $this->initVar('pid', XOBJ_DTYPE_INT, 0, false);
62
        $this->initVar('date', XOBJ_DTYPE_INT, null, false);
63
        $this->initVar('nohtml', XOBJ_DTYPE_INT, 1, false);
64
        $this->initVar('nosmiley', XOBJ_DTYPE_INT, 0, false);
65
        $this->initVar('noxcode', XOBJ_DTYPE_INT, 0, false);
66
        $this->initVar('user_id', XOBJ_DTYPE_INT, null, false);
67
        $this->initVar('icon', XOBJ_DTYPE_OTHER, null, false);
68
        $this->initVar('prefix', XOBJ_DTYPE_OTHER, null, false);
69
        if (!empty($id)) {
70
            if (is_array($id)) {
71
                $this->assignVars($id);
72
            } else {
73
                $this->load((int)$id);
74
            }
75
        }
76
    }
77
78
    /**
79
     * Load Comment by ID
80
     *
81
     * @param int $id
82
     */
83
    public function load($id)
84
    {
85
        $id  = (int)$id;
86
        $sql = 'SELECT * FROM ' . $this->ctable . ' WHERE comment_id=' . $id;
87
        $arr = $this->db->fetchArray($this->db->query($sql));
0 ignored issues
show
It seems like $this->db->query($sql) can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

87
        $arr = $this->db->fetchArray(/** @scrutinizer ignore-type */ $this->db->query($sql));
Loading history...
88
        $this->assignVars($arr);
0 ignored issues
show
It seems like $arr can also be of type false; however, parameter $var_arr of XoopsObject::assignVars() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

88
        $this->assignVars(/** @scrutinizer ignore-type */ $arr);
Loading history...
89
    }
90
91
    /**
92
     * Save Comment
93
     *
94
     * @return int|false
95
     */
96
    public function store()
97
    {
98
        if (!$this->cleanVars()) {
99
            return false;
100
        }
101
        foreach ($this->cleanVars as $k => $v) {
102
            $$k = $v;
103
        }
104
        $isnew = false;
105
        if (empty($comment_id)) {
106
            $isnew      = true;
107
            $comment_id = $this->db->genId($this->ctable . '_comment_id_seq');
108
            $sql        = sprintf("INSERT INTO %s (comment_id, pid, item_id, date, user_id, ip, subject, comment, nohtml, nosmiley, noxcode, icon) VALUES (%u, %u, %u, %u, %u, '%s', '%s', '%s', %u, %u, %u, '%s')", $this->ctable, $comment_id, $pid, $item_id, time(), $user_id, $ip, $subject, $comment, $nohtml, $nosmiley, $noxcode, $icon);
109
        } else {
110
            $sql = sprintf("UPDATE %s SET subject = '%s', comment = '%s', nohtml = %u, nosmiley = %u, noxcode = %u, icon = '%s'  WHERE comment_id = %u", $this->ctable, $subject, $comment, $nohtml, $nosmiley, $noxcode, $icon, $comment_id);
111
        }
112
        if (!$result = $this->db->query($sql)) {
113
            //echo $sql;
114
            return false;
115
        }
116
        if (empty($comment_id)) {
117
            $comment_id = $this->db->getInsertId();
118
        }
119
        if ($isnew != false) {
120
            $sql = sprintf('UPDATE %s SET posts = posts+1 WHERE uid = %u', $this->db->prefix('users'), $user_id);
121
            if (!$result = $this->db->query($sql)) {
122
                echo 'Could not update user posts.';
123
            }
124
        }
125
126
        return $comment_id;
127
    }
128
129
    /**
130
     * Enter description here...
131
     *
132
     * @return int
133
     */
134
    public function delete()
135
    {
136
        $sql = sprintf('DELETE FROM %s WHERE comment_id = %u', $this->ctable, $this->getVar('comment_id'));
137
        if (!$result = $this->db->query($sql)) {
138
            return false;
139
        }
140
        $sql = sprintf('UPDATE %s SET posts = posts-1 WHERE uid = %u', $this->db->prefix('users'), $this->getVar('user_id'));
141
        if (!$result = $this->db->query($sql)) {
142
            echo 'Could not update user posts.';
143
        }
144
        $mytree = new XoopsTree($this->ctable, 'comment_id', 'pid');
145
        $arr    = $mytree->getAllChild($this->getVar('comment_id'), 'comment_id');
146
        $size   = count($arr);
147
        if ($size > 0) {
148
            for ($i = 0; $i < $size; ++$i) {
149
                $sql = sprintf('DELETE FROM %s WHERE comment_bid = %u', $this->ctable, $arr[$i]['comment_id']);
150
                if (!$result = $this->db->query($sql)) {
151
                    echo 'Could not delete comment.';
152
                }
153
                $sql = sprintf('UPDATE %s SET posts = posts-1 WHERE uid = %u', $this->db->prefix('users'), $arr[$i]['user_id']);
154
                if (!$result = $this->db->query($sql)) {
155
                    echo 'Could not update user posts.';
156
                }
157
            }
158
        }
159
160
        return ($size + 1);
161
    }
162
163
    /**
164
     * Get Comments Tree
165
     *
166
     * @return mixed
167
     */
168
    public function getCommentTree()
169
    {
170
        $mytree = new XoopsTree($this->ctable, 'comment_id', 'pid');
171
        $ret    = array();
172
        $tarray = $mytree->getChildTreeArray($this->getVar('comment_id'), 'comment_id');
173
        foreach ($tarray as $ele) {
174
            $ret[] = new XoopsComments($this->ctable, $ele);
175
        }
176
177
        return $ret;
178
    }
179
180
    /**
181
     * Get All Comments using criteria match
182
     *
183
     * @param  array  $criteria
184
     * @param  bool   $asobject
185
     * @param  string $orderby
186
     * @param  int    $limit
187
     * @param  int    $start
188
     * @return array
189
     */
190
    public function getAllComments($criteria = array(), $asobject = true, $orderby = 'comment_id ASC', $limit = 0, $start = 0)
191
    {
192
        $ret         = array();
193
        $where_query = '';
194
        if (is_array($criteria) && count($criteria) > 0) {
195
            $where_query = ' WHERE';
196
            foreach ($criteria as $c) {
197
                $where_query .= " $c AND";
198
            }
199
            $where_query = substr($where_query, 0, -4);
200
        }
201
        if (!$asobject) {
202
            $sql    = 'SELECT comment_id FROM ' . $this->ctable . "$where_query ORDER BY $orderby";
203
            $result = $this->db->query($sql, $limit, $start);
204
            while (false !== ($myrow = $this->db->fetchArray($result))) {
0 ignored issues
show
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

204
            while (false !== ($myrow = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
205
                $ret[] = $myrow['comment_id'];
206
            }
207
        } else {
208
            $sql    = 'SELECT * FROM ' . $this->ctable . '' . $where_query . " ORDER BY $orderby";
209
            $result = $this->db->query($sql, $limit, $start);
210
            while (false !== ($myrow = $this->db->fetchArray($result))) {
211
                $ret[] = new XoopsComments($this->ctable, $myrow);
212
            }
213
        }
214
215
        //echo $sql;
216
        return $ret;
217
    }
218
219
    /**
220
     * Enter printNavBar
221
     *
222
     * @param int    $item_id
223
     * @param string $mode
224
     * @param int    $order
225
     */
226
    public function printNavBar($item_id, $mode = 'flat', $order = 1)
227
    {
228
        global $xoopsConfig, $xoopsUser;
229
        echo "<form method='get' action='" . $_SERVER['PHP_SELF'] . "'><table width='100%' border='0' cellspacing='1' cellpadding='2'><tr><td class='bg1' align='center'><select name='mode'><option value='nocomments'";
230
        if ($mode === 'nocomments') {
231
            echo " selected";
232
        }
233
        echo '>' . _NOCOMMENTS . "</option><option value='flat'";
234
        if ($mode === 'flat') {
235
            echo " selected";
236
        }
237
        echo '>' . _FLAT . "</option><option value='thread'";
238
        if ($mode === 'thread' || $mode == '') {
239
            echo " selected";
240
        }
241
        echo '>' . _THREADED . "</option></select><select name='order'><option value='0'";
242
        if ($order != 1) {
243
            echo " selected";
244
        }
245
        echo '>' . _OLDESTFIRST . "</option><option value='1'";
246
        if ($order == 1) {
247
            echo " selected";
248
        }
249
        echo '>' . _NEWESTFIRST . "</option></select><input type='hidden' name='item_id' value='" . (int)$item_id . "' /><input type='submit' value='" . _CM_REFRESH . "' />";
250
        if ($xoopsConfig['anonpost'] == 1 || $xoopsUser) {
251
            if ($mode !== 'flat' || $mode !== 'nocomments' || $mode !== 'thread') {
252
                $mode = 'flat';
253
            }
254
            echo "&nbsp;<input type='button' onclick='location=\"newcomment.php?item_id=" . (int)$item_id . '&amp;order=' . (int)$order . '&amp;mode=' . $mode . "\"' value='" . _CM_POSTCOMMENT . "' />";
255
        }
256
        echo '</td></tr></table></form>';
257
    }
258
259
    /**
260
     * Show Thread
261
     *
262
     */
263
    public function showThreadHead()
264
    {
265
        openThread();
266
    }
267
268
    /**
269
     * Enter description here...
270
     *
271
     * @param string $order
272
     * @param string $mode
273
     * @param int    $adminview
274
     * @param int    $color_num
275
     */
276
    public function showThreadPost($order, $mode, $adminview = 0, $color_num = 1)
277
    {
278
        global $xoopsConfig, $xoopsUser;
279
        $edit_image   = '';
280
        $reply_image  = '';
281
        $delete_image = '';
282
        $post_date    = formatTimestamp($this->getVar('date'), 'm');
283
        if ($this->getVar('user_id') != 0) {
284
            $poster = new XoopsUser($this->getVar('user_id'));
285
            if (!$poster->isActive()) {
286
                $poster = 0;
287
            }
288
        } else {
289
            $poster = 0;
290
        }
291
        if ($this->getVar('icon') != null && $this->getVar('icon') != '') {
292
            $subject_image = "<a name='" . $this->getVar('comment_id') . "' id='" . $this->getVar('comment_id') . "'></a><img src='" . XOOPS_URL . '/images/subject/' . $this->getVar('icon') . "' alt='' />";
293
        } else {
294
            $subject_image = "<a name='" . $this->getVar('comment_id') . "' id='" . $this->getVar('comment_id') . "'></a><img src='" . XOOPS_URL . "/images/icons/no_posticon.gif' alt='' />";
295
        }
296
        if ($adminview) {
297
            $ip_image = "<img src='" . XOOPS_URL . "/images/icons/ip.gif' alt='" . $this->getVar('ip') . "' />";
298
        } else {
299
            $ip_image = "<img src='" . XOOPS_URL . "/images/icons/ip.gif' alt='' />";
300
        }
301
        if ($adminview || ($xoopsUser && $this->getVar('user_id') == $xoopsUser->getVar('uid'))) {
302
            $edit_image = "<a href='editcomment.php?comment_id=" . $this->getVar('comment_id') . '&amp;mode=' . $mode . '&amp;order=' . (int)$order . "'><img src='" . XOOPS_URL . "/images/icons/edit.gif' alt='" . _EDIT . "' /></a>";
303
        }
304
        if ($xoopsConfig['anonpost'] || $xoopsUser) {
305
            $reply_image = "<a href='replycomment.php?comment_id=" . $this->getVar('comment_id') . '&amp;mode=' . $mode . '&amp;order=' . (int)$order . "'><img src='" . XOOPS_URL . "/images/icons/reply.gif' alt='" . _REPLY . "' /></a>";
306
        }
307
        if ($adminview) {
308
            $delete_image = "<a href='deletecomment.php?comment_id=" . $this->getVar('comment_id') . '&amp;mode=' . $mode . '&amp;order=' . (int)$order . "'><img src='" . XOOPS_URL . "/images/icons/delete.gif' alt='" . _DELETE . "' /></a>";
309
        }
310
311
        if ($poster) {
312
            $text = $this->getVar('comment');
313
            if ($poster->getVar('attachsig')) {
314
                $text .= '<p><br>_________________<br>' . $poster->user_sig() . '</p>';
315
            }
316
            $reg_date = _CM_JOINED;
317
            $reg_date .= formatTimestamp($poster->getVar('user_regdate'), 's');
318
            $posts = _CM_POSTS;
319
            $posts .= $poster->getVar('posts');
320
            $user_from = _CM_FROM;
321
            $user_from .= $poster->getVar('user_from');
322
            $rank = $poster->rank();
323
            if ($rank['image'] != '') {
324
                $rank['image'] = "<img src='" . XOOPS_UPLOAD_URL . '/' . $rank['image'] . "' alt='' />";
325
            }
326
            $avatar_image = "<img src='" . XOOPS_UPLOAD_URL . '/' . $poster->getVar('user_avatar') . "' alt='' />";
327
            $online_image = '';
328
            if ($poster->isOnline()) {
329
                $online_image = "<span style='color:#ee0000;font-weight:bold;'>" . _CM_ONLINE . '</span>';
330
            }
331
            $profile_image = "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $poster->getVar('uid') . "'><img src='" . XOOPS_URL . "/images/icons/profile.gif' alt='" . _PROFILE . "' /></a>";
332
            $pm_image      = '';
333
            if ($xoopsUser) {
334
                $pm_image = "<a href='javascript:openWithSelfMain(\"" . XOOPS_URL . '/pmlite.php?send2=1&amp;to_userid=' . $poster->getVar('uid') . "\",\"pmlite\",565,500);'><img src='" . XOOPS_URL . "/images/icons/pm.gif' alt='" . sprintf(_SENDPMTO, $poster->getVar('uname', 'E')) . "' /></a>";
335
            }
336
            $email_image = '';
337
            if ($poster->getVar('user_viewemail')) {
338
                $email_image = "<a href='mailto:" . $poster->getVar('email', 'E') . "'><img src='" . XOOPS_URL . "/images/icons/email.gif' alt='" . sprintf(_SENDEMAILTO, $poster->getVar('uname', 'E')) . "' /></a>";
339
            }
340
            $posterurl = $poster->getVar('url');
341
            $www_image = '';
342
            if ($posterurl != '') {
343
                $www_image = "<a href='$posterurl' rel='external'><img src='" . XOOPS_URL . "/images/icons/www.gif' alt='" . _VISITWEBSITE . "' /></a>";
344
            }
345
            $icq_image = '';
346
            if ($poster->getVar('user_icq') != '') {
347
                $icq_image = "<a href='http://wwp.icq.com/scripts/search.dll?to=" . $poster->getVar('user_icq', 'E') . "'><img src='" . XOOPS_URL . "/images/icons/icq_add.gif' alt='" . _ADD . "' /></a>";
348
            }
349
            $aim_image = '';
350
            if ($poster->getVar('user_aim') != '') {
351
                $aim_image = "<a href='aim:goim?screenname=" . $poster->getVar('user_aim', 'E') . '&message=Hi+' . $poster->getVar('user_aim') . "+Are+you+there?'><img src='" . XOOPS_URL . "/images/icons/aim.gif' alt='aim' /></a>";
352
            }
353
            $yim_image = '';
354
            if ($poster->getVar('user_yim') != '') {
355
                $yim_image = "<a href='http://edit.yahoo.com/config/send_webmesg?.target=" . $poster->getVar('user_yim', 'E') . "&.src=pg'><img src='" . XOOPS_URL . "/images/icons/yim.gif' alt='yim' /></a>";
356
            }
357
            $msnm_image = '';
358
            if ($poster->getVar('user_msnm') != '') {
359
                $msnm_image = "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $poster->getVar('uid') . "'><img src='" . XOOPS_URL . "/images/icons/msnm.gif' alt='msnm' /></a>";
360
            }
361
            showThread($color_num, $subject_image, $this->getVar('subject'), $text, $post_date, $ip_image, $reply_image, $edit_image, $delete_image, $poster->getVar('uname'), $rank['title'], $rank['image'], $avatar_image, $reg_date, $posts, $user_from, $online_image, $profile_image, $pm_image, $email_image, $www_image, $icq_image, $aim_image, $yim_image, $msnm_image);
0 ignored issues
show
$color_num of type integer is incompatible with the type unknown_type expected by parameter $color_number of showThread(). ( Ignorable by Annotation )

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

361
            showThread(/** @scrutinizer ignore-type */ $color_num, $subject_image, $this->getVar('subject'), $text, $post_date, $ip_image, $reply_image, $edit_image, $delete_image, $poster->getVar('uname'), $rank['title'], $rank['image'], $avatar_image, $reg_date, $posts, $user_from, $online_image, $profile_image, $pm_image, $email_image, $www_image, $icq_image, $aim_image, $yim_image, $msnm_image);
Loading history...
362
        } else {
363
            showThread($color_num, $subject_image, $this->getVar('subject'), $this->getVar('comment'), $post_date, $ip_image, $reply_image, $edit_image, $delete_image, $xoopsConfig['anonymous']);
364
        }
365
    }
366
367
    /**
368
     * Show Thread Footer
369
     *
370
     */
371
    public function showThreadFoot()
372
    {
373
        closeThread();
374
    }
375
376
    /**
377
     * Show Thread Head
378
     *
379
     * @param int|string $width
380
     */
381
    public function showTreeHead($width = '100%')
382
    {
383
        echo "<table border='0' class='outer' cellpadding='0' cellspacing='0' align='center' width='$width'><tr class='bg3' align='center'><td colspan='3'>" . _CM_REPLIES . "</td></tr><tr class='bg3' align='left'><td width='60%' class='fg2'>" . _CM_TITLE . "</td><td width='20%' class='fg2'>" . _CM_POSTER . "</td><td class='fg2'>" . _CM_POSTED . '</td></tr>';
384
    }
385
386
    /**
387
     * Show Tree Items
388
     *
389
     * @param string $order
390
     * @param string $mode
391
     * @param int    $color_num
392
     */
393
    public function showTreeItem($order, $mode, $color_num)
394
    {
395
        $bg = 'odd';
396
        if ($color_num == 1) {
397
            $bg = 'even';
398
        }
399
        $prefix = str_replace('.', '&nbsp;&nbsp;&nbsp;&nbsp;', $this->getVar('prefix'));
400
        $date   = formatTimestamp($this->getVar('date'), 'm');
401
        $icon   = 'icons/no_posticon.gif';
402
        if ($this->getVar('icon') != '') {
403
            $icon = 'subject/' . $this->getVar('icon', 'E');
404
        }
405
        echo "<tr class='$bg' align='left'><td>" . $prefix . "<img src='" . XOOPS_URL . '/images/' . $icon . "'>&nbsp;<a href='" . $_SERVER['PHP_SELF'] . '?item_id=' . $this->getVar('item_id') . '&amp;comment_id=' . $this->getVar('comment_id') . '&amp;mode=' . $mode . '&amp;order=' . $order . '#' . $this->getVar('comment_id') . "'>" . $this->getVar('subject') . "</a></td><td><a href='" . XOOPS_URL . '/userinfo.php?uid=' . $this->getVar('user_id') . "'>" . XoopsUser::getUnameFromId($this->getVar('user_id')) . '</a></td><td>' . $date . '</td></tr>';
406
    }
407
408
    /**
409
     * Show Thread Foot
410
     *
411
     */
412
    public function showTreeFoot()
413
    {
414
        echo '</table><br>';
415
    }
416
}
417