Exemplo n.º 1
0
/**
* This function displays the comment control bar
*
* Prints the control that allows the user to interact with Geeklog Comments
*
* @param    string  $sid    ID of item in question
* @param    string  $title  Title of item
* @param    string  $type   Type of item (i.e. article, photo, etc)
* @param    string  $order  Order that comments are displayed in
* @param    string  $mode   Mode (nested, flat, etc.)
* @param    int     $ccode  Comment code: -1=no comments, 0=allowed, 1=closed
* @return   string          HTML Formated comment bar
* @see CMT_userComments
*
*/
function CMT_commentBar($sid, $title, $type, $order, $mode, $ccode = 0)
{
    global $_CONF, $_TABLES, $_USER, $LANG01;
    $parts = explode('/', $_SERVER['PHP_SELF']);
    $page = array_pop($parts);
    $nrows = DB_count($_TABLES['comments'], array('sid', 'type'), array($sid, $type));
    $commentbar = new Template($_CONF['path_layout'] . 'comment');
    $commentbar->set_file(array('commentbar' => 'commentbar.thtml'));
    $commentbar->set_var('xhtml', XHTML);
    $commentbar->set_var('site_url', $_CONF['site_url']);
    $commentbar->set_var('site_admin_url', $_CONF['site_admin_url']);
    $commentbar->set_var('layout_url', $_CONF['layout_url']);
    $commentbar->set_var('lang_comments', $LANG01[3]);
    $commentbar->set_var('lang_refresh', $LANG01[39]);
    $commentbar->set_var('lang_reply', $LANG01[60]);
    $commentbar->set_var('lang_disclaimer', $LANG01[26]);
    if ($ccode == 0) {
        $commentbar->set_var('reply_hidden_or_submit', 'submit');
    } else {
        $commentbar->set_var('reply_hidden_or_submit', 'hidden');
    }
    $commentbar->set_var('num_comments', COM_numberFormat($nrows));
    $commentbar->set_var('comment_type', $type);
    $commentbar->set_var('sid', $sid);
    $cmt_title = stripslashes($title);
    $commentbar->set_var('story_title', $cmt_title);
    // Article's are pre-escaped.
    if ($type != 'article') {
        $cmt_title = htmlspecialchars($cmt_title);
    }
    $commentbar->set_var('comment_title', $cmt_title);
    if ($type == 'article') {
        $articleUrl = COM_buildUrl($_CONF['site_url'] . "/article.php?story={$sid}");
    } else {
        // for a plugin
        /**
         * Link to plugin defined link or lacking that a generic link
         * that the plugin should support (hopefully)
         */
        list($plgurl, $plgid) = PLG_getCommentUrlId($type);
        $articleUrl = "{$plgurl}?{$plgid}={$sid}";
    }
    $commentbar->set_var('article_url', $articleUrl);
    if ($page == 'comment.php') {
        $link = COM_createLink($cmt_title, $articleUrl, array('class' => 'non-ul b'));
        $commentbar->set_var('story_link', $link);
        $commentbar->set_var('start_storylink_anchortag', '<a href="' . $articleUrl . '" class="non-ul">');
        $commentbar->set_var('end_storylink_anchortag', '</a>');
    } else {
        $commentbar->set_var('story_link', $articleUrl);
    }
    if (!empty($_USER['uid']) && $_USER['uid'] > 1) {
        $username = $_USER['username'];
        $fullname = $_USER['fullname'];
    } else {
        $result = DB_query("SELECT username,fullname FROM {$_TABLES['users']} WHERE uid = 1");
        $N = DB_fetchArray($result);
        $username = $N['username'];
        $fullname = $N['fullname'];
    }
    if (empty($fullname)) {
        $fullname = $username;
    }
    $commentbar->set_var('user_name', $username);
    $commentbar->set_var('user_fullname', $fullname);
    if (!empty($_USER['username'])) {
        $author = COM_getDisplayName($_USER['uid'], $username, $fullname);
        $commentbar->set_var('user_nullname', $author);
        $commentbar->set_var('author', $author);
        $commentbar->set_var('login_logout_url', $_CONF['site_url'] . '/users.php?mode=logout');
        $commentbar->set_var('lang_login_logout', $LANG01[35]);
    } else {
        $commentbar->set_var('user_nullname', '');
        $commentbar->set_var('login_logout_url', $_CONF['site_url'] . '/users.php?mode=new');
        $commentbar->set_var('lang_login_logout', $LANG01[61]);
    }
    if ($page == 'comment.php') {
        $commentbar->set_var('parent_url', $_CONF['site_url'] . '/comment.php');
        $hidden = '';
        if ($_REQUEST['mode'] == 'view') {
            $hidden .= '<input type="hidden" name="cid" value="' . $_REQUEST['cid'] . '"' . XHTML . '>';
            $hidden .= '<input type="hidden" name="pid" value="' . $_REQUEST['cid'] . '"' . XHTML . '>';
        } else {
            if ($_REQUEST['mode'] == 'display') {
                $hidden .= '<input type="hidden" name="pid" value="' . $_REQUEST['pid'] . '"' . XHTML . '>';
            }
        }
        $commentbar->set_var('hidden_field', $hidden . '<input type="hidden" name="mode" value="' . $_REQUEST['mode'] . '"' . XHTML . '>');
    } else {
        if ($type == 'article') {
            $commentbar->set_var('parent_url', $_CONF['site_url'] . '/article.php');
            $commentbar->set_var('hidden_field', '<input type="hidden" name="story" value="' . $sid . '"' . XHTML . '>');
        } else {
            // plugin
            // Link to plugin defined link or lacking that a generic link that the plugin should support (hopefully)
            list($plgurl, $plgid) = PLG_getCommentUrlId($type);
            $commentbar->set_var('parent_url', $plgurl);
            $commentbar->set_var('hidden_field', '<input type="hidden" name="' . $plgid . '" value="' . $sid . '"' . XHTML . '>');
        }
    }
    // Order
    $selector = '<select name="order">' . LB . COM_optionList($_TABLES['sortcodes'], 'code,name', $order) . LB . '</select>';
    $commentbar->set_var('order_selector', $selector);
    // Mode
    if ($page == 'comment.php') {
        $selector = '<select name="format">';
    } else {
        $selector = '<select name="mode">';
    }
    $selector .= LB . COM_optionList($_TABLES['commentmodes'], 'mode,name', $mode) . LB . '</select>';
    $commentbar->set_var('mode_selector', $selector);
    return $commentbar->finish($commentbar->parse('output', 'commentbar'));
}
Exemplo n.º 2
0
/**
 * Get view URL and name of unique identifier
 *
 * @param   string   $type   Plugin to delete comment
 * @return  array    string of URL of view page, name of unique identifier
 * @see     function PLG_getCommentUrlId
 */
function CMT_getCommentUrlId($type)
{
    global $_CONF;
    if ($type == 'article') {
        $retval[0] = COM_buildUrl($_CONF['site_url'] . '/article.php');
        $retval[1] = 'story';
    } else {
        $retval = PLG_getCommentUrlId($type);
    }
    return $retval;
}
Exemplo n.º 3
0
/**
* This function displays the comments in a high level format.
*
* Begins displaying user comments for an item
*
* @param    string      $sid       ID for item to show comments for
* @param    string      $title     Title of item
* @param    string      $type      Type of item (article, polls, etc.)
* @param    string      $order     How to order the comments 'ASC' or 'DESC'
* @param    string      $mode      comment mode (nested, flat, etc.)
* @param    int         $pid       id of parent comment
* @param    int         $page      page number of comments to display
* @param    boolean     $cid       true if $pid should be interpreted as a cid instead
* @param    boolean     $delete_option   if current user can delete comments
* @param    int         $ccode     Comment code: -1=no comments, 0=allowed, 1=closed
* @return   string  HTML Formated Comments
* @see CMT_commentBar
*
*/
function CMT_userComments($sid, $title, $type = 'article', $order = '', $mode = '', $pid = 0, $page = 1, $cid = false, $delete_option = false, $ccode = 0, $sid_author_id = '')
{
    global $_CONF, $_TABLES, $_USER, $LANG01;
    $retval = '';
    if (!isset($_CONF['comment_engine'])) {
        $_CONF['comment_engine'] = 'internal';
    }
    switch ($_CONF['comment_engine']) {
        case 'disqus':
            if ($type == 'article') {
                $pageURL = COM_buildUrl($_CONF['site_url'] . "/article.php?story={$sid}");
                $pageIdentifier = 'article_' . $sid;
            } else {
                // for a plugin
                // Link to plugin defined link or lacking that a generic link that the plugin should support (hopefully)
                list($pageURL, $plgid) = PLG_getCommentUrlId($type);
                $pageIdentifier = $type . '_' . $sid;
                $pageURL = PLG_getItemInfo($type, $sid, 'url');
            }
            $pageTitle = $title;
            $pageURL = str_replace('&amp;', '&', $pageURL);
            $retval = '
            <a name="comment_entry"></a>
            <div id="disqus_thread"></div>
            <script>
                var disqus_config = function () {
                    this.page.url = \'' . $pageURL . '\';
                    this.page.identifier = \'' . $pageIdentifier . '\';
                    this.page.title = \'' . addslashes($pageTitle) . '\';
                };
                (function() {
                    var d = document, s = d.createElement(\'script\');
                    s.src = \'//' . $_CONF['comment_disqus_shortname'] . '.disqus.com/embed.js\';
                    s.setAttribute(\'data-timestamp\', +new Date());
                    (d.head || d.body).appendChild(s);
                })();
            </script>
            <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
            ';
            break;
        case 'facebook':
            if ($type == 'article') {
                $pageURL = COM_buildUrl($_CONF['site_url'] . "/article.php?story={$sid}");
                $pageIdentifier = 'article_' . $sid;
            } else {
                // for a plugin
                // Link to plugin defined link or lacking that a generic link that the plugin should support (hopefully)
                list($pageURL, $plgid) = PLG_getCommentUrlId($type);
                $pageIdentifier = $type . '_' . $sid;
                $pageURL = PLG_getItemInfo($type, $sid, 'url');
            }
            $pageTitle = urlencode($title);
            $pageURL = str_replace('&amp;', '&', $pageURL);
            $retval = '<a name="comment_entry"></a><div class="fb-comments" data-href="' . $pageURL . '" data-numposts="20"></div>';
            break;
        case 'internal':
        default:
            $valid_modes = array('threaded', 'nested', 'flat', 'nocomment');
            if (in_array($mode, $valid_modes) === false) {
                $mode = 'nested';
            }
            if ($mode == 'threaded') {
                $mode = 'nested';
            }
            if (!COM_isAnonUser()) {
                $result = DB_query("SELECT commentorder,commentmode,commentlimit FROM {$_TABLES['usercomment']} WHERE uid = {$_USER['uid']}");
                $U = DB_fetchArray($result);
                if (empty($order)) {
                    $order = $U['commentorder'];
                }
                if (empty($mode)) {
                    $mode = $U['commentmode'];
                }
                $limit = $U['commentlimit'];
            }
            if ($order != 'ASC' && $order != 'DESC') {
                $order = 'ASC';
            }
            $validmodes = array('flat', 'nested', 'nocomment', 'nobar');
            if (!in_array($mode, $validmodes)) {
                $mode = $_CONF['comment_mode'];
            }
            if (empty($mode)) {
                $mode = $_CONF['comment_mode'];
            }
            if (empty($limit)) {
                $limit = $_CONF['comment_limit'];
            } else {
                $limit = (int) $limit;
            }
            if (!is_numeric($page) || $page < 1) {
                $page = 1;
            } else {
                $page = (int) $page;
            }
            $start = $limit * ($page - 1);
            $template = new Template($_CONF['path_layout'] . 'comment');
            $template->set_file(array('commentarea' => 'startcomment.thtml'));
            if ($mode != 'nobar') {
                $template->set_var('commentbar', CMT_commentBar($sid, $title, $type, $order, $mode, $ccode));
            }
            $template->set_var('sid', $sid);
            $template->set_var('comment_type', $type);
            if ($mode == 'nested' || $mode == 'threaded' || $mode == 'flat') {
                // build query
                switch ($mode) {
                    case 'flat':
                        if ($cid) {
                            $count = 1;
                            $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['users']} AS u " . "WHERE c.uid = u.uid AND c.cid = " . (int) $pid . " AND type='" . DB_escapeString($type) . "'";
                        } else {
                            $count = DB_count($_TABLES['comments'], array('sid', 'type'), array(DB_escapeString($sid), DB_escapeString($type)));
                            $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['users']} AS u " . "WHERE c.uid = u.uid AND c.sid = '" . DB_escapeString($sid) . "' AND type='" . DB_escapeString($type) . "' " . "ORDER BY date {$order} LIMIT {$start}, {$limit}";
                        }
                        break;
                    case 'nested':
                    case 'threaded':
                    default:
                        if ($order == 'DESC') {
                            $cOrder = 'c.rht DESC';
                        } else {
                            $cOrder = 'c.lft ASC';
                        }
                        // We can simplify the query, and hence increase performance
                        // when pid = 0 (when fetching all the comments for a given sid)
                        if ($cid) {
                            // pid refers to commentid rather than parentid
                            // count the total number of applicable comments
                            $q2 = "SELECT COUNT(*) " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['comments']} AS c2 " . "WHERE c.sid = '" . DB_escapeString($sid) . "' AND (c.lft >= c2.lft AND c.lft <= c2.rht) " . "AND c2.cid = " . (int) $pid . " AND c.type='" . DB_escapeString($type) . "'";
                            $result = DB_query($q2);
                            list($count) = DB_fetchArray($result);
                            $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, c2.indent AS pindent, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['comments']} AS c2, " . "{$_TABLES['users']} AS u " . "WHERE c.sid = '" . DB_escapeString($sid) . "' AND (c.lft >= c2.lft AND c.lft <= c2.rht) " . "AND c2.cid = " . (int) $pid . " AND c.uid = u.uid AND c.type='" . DB_escapeString($type) . "' " . "ORDER BY {$cOrder} LIMIT {$start}, {$limit}";
                        } else {
                            // pid refers to parentid rather than commentid
                            if ($pid == 0) {
                                // the simple, fast case
                                // count the total number of applicable comments
                                $count = DB_count($_TABLES['comments'], array('sid', 'type'), array(DB_escapeString($sid), DB_escapeString($type)));
                                $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, 0 AS pindent, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['users']} AS u " . "WHERE c.sid = '" . DB_escapeString($sid) . "' AND c.uid = u.uid  AND type='" . DB_escapeString($type) . "' " . "ORDER BY {$cOrder} LIMIT {$start}, {$limit}";
                            } else {
                                // count the total number of applicable comments
                                $q2 = "SELECT COUNT(*) " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['comments']} AS c2 " . "WHERE c.sid = '" . DB_escapeString($sid) . "' AND (c.lft > c2.lft AND c.lft < c2.rht) " . "AND c2.cid = " . (int) $pid . " AND c.type='" . DB_escapeString($type) . "'";
                                $result = DB_query($q2);
                                list($count) = DB_fetchArray($result);
                                $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, c2.indent + 1 AS pindent, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['comments']} AS c2, " . "{$_TABLES['users']} AS u " . "WHERE c.sid = '" . DB_escapeString($sid) . "' AND (c.lft > c2.lft AND c.lft < c2.rht) " . "AND c2.cid = " . (int) $pid . " AND c.uid = u.uid AND c.type='" . DB_escapeString($type) . "' " . "ORDER BY {$cOrder} LIMIT {$start}, {$limit}";
                            }
                        }
                        break;
                }
                $thecomments = '';
                $result = DB_query($q);
                $thecomments .= CMT_getComment($result, $mode, $type, $order, $delete_option, false, $ccode, $sid_author_id);
                // Pagination
                $tot_pages = ceil($count / $limit);
                if ($type == 'article') {
                    $pLink = $_CONF['site_url'] . "/article.php?story={$sid}&amp;type={$type}&amp;order={$order}&amp;mode={$mode}";
                    $pageStr = 'page=';
                } else {
                    // plugin
                    // Link to plugin defined link or lacking that a generic link that the plugin should support (hopefully)
                    list($plgurl, $plgid, $plg_page_str) = PLG_getCommentUrlId($type);
                    $pLink = $plgurl . '?' . $plgid . '=' . $sid . "&amp;type={$type}&amp;order={$order}&amp;mode={$mode}";
                    if ($plg_page_str != '') {
                        $pageStr = $plg_page_str;
                    } else {
                        $pageStr = 'page=';
                    }
                }
                $template->set_var('pagenav', COM_printPageNavigation($pLink, $page, $tot_pages, $pageStr, false, '', '', '#comments'));
                $template->set_var('comments', $thecomments);
            }
            $retval = $template->finish($template->parse('output', 'commentarea'));
            break;
    }
    return $retval;
}
Exemplo n.º 4
0
/**
 * Handles a comment edit submission
 *
 * @copyright Jared Wenerd 2008
 * @author Jared Wenerd <wenerd87 AT gmail DOT com>
 * @return string HTML (possibly a refresh)
 */
function handleEditSubmit()
{
    global $_CONF, $_TABLES, $_USER, $LANG03, $_PLUGINS;
    $type = COM_applyFilter($_POST['type']);
    $sid = COM_sanitizeID(COM_applyFilter($_POST['sid']));
    $cid = COM_applyFilter($_POST['cid'], true);
    $postmode = COM_applyFilter($_POST['postmode']);
    if ($type != 'article') {
        if (!in_array($type, $_PLUGINS)) {
            $type = '';
        }
    }
    $commentuid = DB_getItem($_TABLES['comments'], 'uid', "cid = " . (int) $cid);
    if (COM_isAnonUser()) {
        $uid = 1;
    } else {
        $uid = $_USER['uid'];
    }
    $comment = $_POST['comment_text'];
    //check for bad input
    if (empty($sid) || empty($_POST['title']) || empty($comment) || !is_numeric($cid) || $cid < 1) {
        COM_errorLog("handleEditSubmit(): {{$_USER['uid']} from {$_SERVER['REMOTE_ADDR']} tried " . 'to edit a comment with one or more missing values.');
        return COM_refresh($_CONF['site_url'] . '/index.php');
    } elseif ($uid != $commentuid && !SEC_inGroup('Root')) {
        //check permissions
        COM_errorLog("handleEditSubmit(): {{$_USER['uid']} from {$_SERVER['REMOTE_ADDR']} tried " . 'to edit a comment without proper permission.');
        return COM_refresh($_CONF['site_url'] . '/index.php');
    }
    $comment = CMT_prepareText($comment, $postmode, true, $cid);
    $title = COM_checkWords(strip_tags($_POST['title']));
    if (!empty($title) && !empty($comment)) {
        COM_updateSpeedlimit('comment');
        $title = DB_escapeString($title);
        $comment = DB_escapeString($comment);
        // save the comment into the comment table
        DB_query("UPDATE {$_TABLES['comments']} SET comment = '{$comment}', title = '{$title}'" . " WHERE cid=" . (int) $cid . " AND sid='" . DB_escapeString($sid) . "'");
        if (DB_error()) {
            //saving to non-existent comment or comment in wrong article
            COM_errorLog("handleEditSubmit(): {$_USER['uid']} from {$_SERVER['REMOTE_ADDR']} tried " . 'to edit to a non-existent comment or the cid/sid did not match');
            return COM_refresh($_CONF['site_url'] . '/index.php');
        }
        $safecid = (int) $cid;
        $safeuid = (int) $uid;
        DB_save($_TABLES['commentedits'], 'cid,uid,time', "{$safecid},{$safeuid},NOW()");
    } else {
        COM_errorLog("handleEditSubmit(): {$_USER['uid']} from {$_SERVER['REMOTE_ADDR']} tried " . 'to submit a comment with invalid $title and/or $comment.');
        return COM_refresh($_CONF['site_url'] . '/index.php');
    }
    PLG_commentEditSave($type, $cid, $sid);
    $urlArray = PLG_getCommentUrlId($type);
    if (is_array($urlArray)) {
        $url = $urlArray[0] . '?' . $urlArray[1] . '=' . $sid;
        echo COM_refresh($url);
        exit;
    }
    return COM_refresh($_CONF['site_url'] . '/index.php');
}
Exemplo n.º 5
0
/**
* This function displays the comments in a high level format.
*
* Begins displaying user comments for an item
*
* @param    string      $sid       ID for item to show comments for
* @param    string      $title     Title of item
* @param    string      $type      Type of item (article, polls, etc.)
* @param    string      $order     How to order the comments 'ASC' or 'DESC'
* @param    string      $mode      comment mode (nested, flat, etc.)
* @param    int         $pid       id of parent comment
* @param    int         $page      page number of comments to display
* @param    boolean     $cid       true if $pid should be interpreted as a cid instead
* @param    boolean     $delete_option   if current user can delete comments
* @param    int         $ccode     Comment code: -1=no comments, 0=allowed, 1=closed
* @return   string  HTML Formated Comments
* @see CMT_commentBar
*
*/
function CMT_userComments($sid, $title, $type = 'article', $order = '', $mode = '', $pid = 0, $page = 1, $cid = false, $delete_option = false, $ccode = 0)
{
    global $_CONF, $_TABLES, $_USER, $LANG01;
    $retval = '';
    if (!COM_isAnonUser()) {
        $result = DB_query("SELECT commentorder,commentmode,commentlimit FROM {$_TABLES['usercomment']} WHERE uid = '{$_USER['uid']}'");
        $U = DB_fetchArray($result);
        if (empty($order)) {
            $order = $U['commentorder'];
        }
        if (empty($mode)) {
            $mode = $U['commentmode'];
        }
        $limit = $U['commentlimit'];
    }
    if ($order != 'ASC' && $order != 'DESC') {
        $order = 'ASC';
    }
    if (empty($mode)) {
        $mode = $_CONF['comment_mode'];
    }
    if (empty($limit)) {
        $limit = $_CONF['comment_limit'];
    }
    if (!is_numeric($page) || $page < 1) {
        $page = 1;
    }
    $start = $limit * ($page - 1);
    $template = COM_newTemplate($_CONF['path_layout'] . 'comment');
    $template->set_file(array('commentarea' => 'startcomment.thtml'));
    $template->set_var('commentbar', CMT_commentBar($sid, $title, $type, $order, $mode, $ccode));
    $template->set_var('sid', $sid);
    $template->set_var('comment_type', $type);
    if ($mode == 'nested' || $mode == 'threaded' || $mode == 'flat') {
        // build query
        switch ($mode) {
            case 'flat':
                if ($cid) {
                    $count = 1;
                    $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['users']} AS u " . "WHERE c.uid = u.uid AND c.cid = {$pid} AND type='{$type}'";
                } else {
                    $count = DB_count($_TABLES['comments'], array('sid', 'type'), array($sid, $type));
                    $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['users']} AS u " . "WHERE c.uid = u.uid AND c.sid = '{$sid}' AND type='{$type}' " . "ORDER BY date {$order} LIMIT {$start}, {$limit}";
                }
                break;
            case 'nested':
            case 'threaded':
            default:
                if ($order == 'DESC') {
                    $cOrder = 'c.rht DESC';
                } else {
                    $cOrder = 'c.lft ASC';
                }
                // We can simplify the query, and hence increase performance
                // when pid = 0 (when fetching all the comments for a given sid)
                if ($cid) {
                    // pid refers to commentid rather than parentid
                    // count the total number of applicable comments
                    $q2 = "SELECT COUNT(*) " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['comments']} AS c2 " . "WHERE c.sid = '{$sid}' AND (c.lft >= c2.lft AND c.lft <= c2.rht) " . "AND c2.cid = {$pid} AND c.type='{$type}'";
                    $result = DB_query($q2);
                    list($count) = DB_fetchArray($result);
                    $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, c2.indent AS pindent, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['comments']} AS c2, " . "{$_TABLES['users']} AS u " . "WHERE c.sid = '{$sid}' AND (c.lft >= c2.lft AND c.lft <= c2.rht) " . "AND c2.cid = {$pid} AND c.uid = u.uid AND c.type='{$type}' " . "ORDER BY {$cOrder} LIMIT {$start}, {$limit}";
                } else {
                    // pid refers to parentid rather than commentid
                    if ($pid == 0) {
                        // the simple, fast case
                        // count the total number of applicable comments
                        $count = DB_count($_TABLES['comments'], array('sid', 'type'), array($sid, $type));
                        $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, 0 AS pindent, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['users']} AS u " . "WHERE c.sid = '{$sid}' AND c.uid = u.uid  AND type='{$type}' " . "ORDER BY {$cOrder} LIMIT {$start}, {$limit}";
                    } else {
                        // count the total number of applicable comments
                        $q2 = "SELECT COUNT(*) " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['comments']} AS c2 " . "WHERE c.sid = '{$sid}' AND (c.lft > c2.lft AND c.lft < c2.rht) " . "AND c2.cid = {$pid} AND c.type='{$type}'";
                        $result = DB_query($q2);
                        list($count) = DB_fetchArray($result);
                        $q = "SELECT c.*, u.username, u.fullname, u.photo, u.email, c2.indent + 1 AS pindent, " . "UNIX_TIMESTAMP(c.date) AS nice_date " . "FROM {$_TABLES['comments']} AS c, {$_TABLES['comments']} AS c2, " . "{$_TABLES['users']} AS u " . "WHERE c.sid = '{$sid}' AND (c.lft > c2.lft AND c.lft < c2.rht) " . "AND c2.cid = {$pid} AND c.uid = u.uid AND c.type='{$type}' " . "ORDER BY {$cOrder} LIMIT {$start}, {$limit}";
                    }
                }
                break;
        }
        $thecomments = '';
        $result = DB_query($q);
        $thecomments .= CMT_getComment($result, $mode, $type, $order, $delete_option, false, $ccode);
        // Pagination
        $tot_pages = ceil($count / $limit);
        if ($type == 'article') {
            $pLink = $_CONF['site_url'] . "/article.php?story={$sid}";
        } else {
            list($plgurl, $plgid) = PLG_getCommentUrlId($type);
            $pLink = "{$plgurl}?{$plgid}={$sid}";
        }
        $pLink .= "&amp;type={$type}&amp;order={$order}&amp;mode={$mode}";
        $page_str = "cpage=";
        $template->set_var('pagenav', COM_printPageNavigation($pLink, $page, $tot_pages, $page_str, false));
        $template->set_var('comments', $thecomments);
        $retval = $template->finish($template->parse('output', 'commentarea'));
    }
    return $retval;
}