function sf_most_rated_posts($limit = 10, $forum = true, $user = true, $postdate = true, $listtags = true, $forumids = 0)
{
    global $wpdb, $current_user, $sfvars;
    sf_initialise_globals($sfvars['forumid']);
    $out = '';
    $postratings = get_option('sfpostratings');
    if (!$postratings['sfpostratings']) {
        if ($listtags) {
            $out .= "<li class='sftagli'>\n";
        }
        $out .= __("Post Rating is not Enabled!", "sforum") . "\n";
        if ($listtags) {
            $out .= "</li>\n";
        }
        return;
    }
    # are we passing forum ID's?
    if ($forumids == 0) {
        $where = '';
    } else {
        $flist = explode(",", $forumids);
        $where = ' WHERE ';
        $x = 0;
        for ($x; $x < count($flist); $x++) {
            $where .= SFPOSTS . ".forum_id = " . $flist[$x];
            if ($x != count($flist) - 1) {
                $where .= " OR ";
            }
        }
    }
    # limit to viewable forums based on permissions
    if (!$current_user->forumadmin) {
        $allforums = sf_get_forum_memberships($current_user->ID);
        if ($allforums) {
            $forum_ids = '';
            foreach ($allforums as $thisforum) {
                if (sf_can_view_forum($thisforum->forum_id)) {
                    $forum_ids[] = $thisforum->forum_id;
                }
            }
        } else {
            return '';
        }
        # create where clause based on forums that current user can view
        if ($forum_ids != '') {
            if ($where == '') {
                $where = ' WHERE ';
            } else {
                $where .= ' AND ';
            }
            $where .= SFPOSTS . ".forum_id IN (" . implode(",", $forum_ids) . ") ";
        }
    }
    $sfposts = $wpdb->get_results("SELECT " . SFPOSTRATINGS . ".post_id, ratings_sum, vote_count, " . SFPOSTS . ".topic_id, " . SFPOSTS . ".forum_id, " . SFPOSTS . ".user_id, post_date, post_index, topic_slug, topic_name, forum_slug, forum_name, display_name, guest_name\n\t\t\tFROM " . SFPOSTRATINGS . "\n\t\t\tJOIN " . SFPOSTS . " ON " . SFPOSTRATINGS . ".post_id = " . SFPOSTS . ".post_id\n\t\t\tJOIN " . SFTOPICS . " ON " . SFPOSTS . ".topic_id = " . SFTOPICS . ".topic_id\n\t\t\tJOIN " . SFFORUMS . " ON " . SFPOSTS . ".forum_id = " . SFFORUMS . ".forum_id\n\t\t\tLEFT JOIN " . SFMEMBERS . " ON " . SFPOSTS . ".user_id = " . SFMEMBERS . ".user_id\n\t\t\t" . $where . "\n\t\t\tORDER BY vote_count DESC\n\t\t\tLIMIT " . $limit);
    if ($sfposts) {
        foreach ($sfposts as $sfpost) {
            if (sf_can_view_forum($sfpost->forum_id)) {
                # Start contruction
                if ($listtags) {
                    $out .= "<li class='sftagli'>\n";
                }
                $out .= '<a href="' . sf_build_url($sfpost->forum_slug, $sfpost->topic_slug, 1, $sfpost->post_id, $sfpost->post_index) . '">';
                $out .= $sfpost->topic_name;
                if ($forum) {
                    $out .= ' ' . __("posted in", "sforum") . ' ' . stripslashes($sfpost->forum_name);
                    $p = true;
                }
                if ($user) {
                    $out .= ' ' . __("by", "sforum") . ' ';
                    $poster = sf_build_name_display($sfpost->user_id, stripslashes($sfpost->display_name));
                    if (empty($poster)) {
                        $poster = apply_filters('sf_show_post_name', stripslashes($sfpost->guest_name));
                    }
                    $out .= $poster;
                    $p = true;
                }
                if ($postdate) {
                    $out .= ' ' . __("on", "sforum") . ' ' . mysql2date(SFDATES, $sfpost->post_date);
                    $p = true;
                }
                $out .= '</a>';
                if ($listtags) {
                    $out .= "</li>\n";
                }
            }
        }
    } else {
        if ($listtags) {
            $out .= "<li class='sftagli'>\n";
        }
        $out .= __("No Rated Posts to Display", "sforum") . "\n";
        if ($listtags) {
            $out .= "</li>\n";
        }
    }
    echo $out;
    return;
}
function sf_add_new_topic_tag($forumid, $linktext = "Add new topic in the %FORUMNAME% forum", $beforelink = '', $afterlink = '', $beforetext = '', $aftertext = '')
{
    global $current_user;
    sf_initialise_globals($forumid);
    if (sf_can_view_forum($forum_id)) {
        $forum = sf_get_forum_record($forumid);
        $linktext = str_replace("%FORUMNAME%", $forum->forum_name, $linktext);
        $url = trailingslashit(sf_build_url($forum->forum_slug, '', 0, 0));
        $url = sf_get_sfqurl($url) . 'new=topic';
        $out = '<p>' . $beforelink . '<a href="' . $url . '">' . $beforetext . $linktext . $aftertext . '</a>' . $afterlink . '</p>';
        echo $out;
    }
    return;
}
function sf_related_topics($limit = 10, $topic_id, $listtags = true, $forum = true, $echo = true)
{
    global $wpdb, $sfvars;
    sf_initialise_globals($sfvars['forumid']);
    $out = '';
    $tags = $wpdb->get_results("SELECT tag_slug\n\t\t\t\t\t\t\t\tFROM " . SFTAGS . "\n\t\t\t\t\t\t\t \tJOIN " . SFTAGMETA . " ON " . SFTAGMETA . ".tag_id = " . SFTAGS . ".tag_id\n\t\t\t\t\t\t\t\tWHERE topic_id=" . $topic_id);
    if ($tags) {
        # build list of tags for the topic id
        $taglist = '';
        foreach ($tags as $tag) {
            if ($taglist == '') {
                $taglist = "('" . $tag->tag_slug . "'";
            } else {
                $taglist .= ",'" . $tag->tag_slug . "'";
            }
        }
        $taglist .= ")";
        # now grab the results
        $LIMIT = ' LIMIT ' . $limit;
        $ORDER = ' ORDER BY topic_id DESC';
        $WHERE = SFTOPICS . ".topic_id IN (SELECT topic_id FROM " . SFTAGMETA . " JOIN " . SFTAGS . " ON " . SFTAGMETA . ".tag_id = " . SFTAGS . ".tag_id\n\t\t\tWHERE tag_slug IN " . $taglist . ")";
        $topics = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS DISTINCT\n\t\t\t\t " . SFTOPICS . ".topic_id, topic_name, topic_slug, " . SFTOPICS . ".forum_id, forum_name, forum_slug\n\t\t\t\t FROM " . SFTOPICS . "\n\t\t\t\t JOIN " . SFFORUMS . " ON " . SFTOPICS . ".forum_id = " . SFFORUMS . ".forum_id\n\t\t\t\t JOIN " . SFPOSTS . " ON " . SFTOPICS . ".topic_id = " . SFPOSTS . ".topic_id\n\t\t\t\t WHERE " . $WHERE . $ORDER . $LIMIT . ";", ARRAY_A);
        # now output the related topics
        if ($topics) {
            foreach ($topics as $topic) {
                if (sf_can_view_forum($topic['forum_id'])) {
                    $p = false;
                    # Start contruction
                    if ($listtags) {
                        $out .= "<li class='sftagli'>\n";
                    }
                    $out .= sf_get_topic_url($topic['forum_slug'], $topic['topic_slug'], $topic['topic_name']);
                    if ($forum) {
                        $out .= "<p class='sftagp'>" . __("posted in forum", "sforum") . ' ' . stripslashes($topic['forum_name']) . "&nbsp;" . "\n";
                        $p = true;
                    }
                    if ($p) {
                        $out .= "</p>\n";
                    }
                    if ($listtags) {
                        $out .= "</li>\n";
                    }
                }
            }
        } else {
            $out .= "<li class='sftagli'>\n";
            $out .= __("No Related Topics", "sforum") . "\n";
            $out .= "</li>\n";
        }
    } else {
        $out .= "<li class='sftagli'>\n";
        $out .= __("No Related Topics", "sforum") . "\n";
        $out .= "</li>\n";
    }
    if ($echo) {
        echo $out;
        return;
    } else {
        return $out;
    }
}