function sf_hot_topics($limit = 10, $days = 30, $forum = true, $listtags = true, $forumids = 0)
{
    global $wpdb, $current_user;
    sf_initialise_globals();
    $out = '';
    # are we passing forum ID's?
    $where = '';
    if ($forumids != 0) {
        $flist = explode(",", $forumids);
        $x = 0;
        $where = ' AND (';
        for ($x; $x < count($flist); $x++) {
            $where .= ' ' . SFTOPICS . '.forum_id = ' . $flist[$x];
            if ($x != count($flist) - 1) {
                $where .= " OR ";
            }
        }
        $where .= ')';
    }
    # 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 != '') {
            $where .= " AND " . SFPOSTS . ".forum_id IN (" . implode(",", $forum_ids) . ") ";
        }
    }
    # get any posts that meeet date criteria
    $posts = $wpdb->get_results("\n\t\tSELECT " . SFPOSTS . ".topic_id, DATEDIFF(CURDATE(), post_date) AS delta, " . SFPOSTS . ".forum_id, forum_name, forum_slug, forum_slug, topic_name, topic_slug\n\t\tFROM " . SFPOSTS . "\n\t\tJOIN " . SFTOPICS . " ON " . SFTOPICS . ".topic_id = " . SFPOSTS . ".topic_id\n\t\tJOIN " . SFFORUMS . " ON " . SFFORUMS . ".forum_id = " . SFPOSTS . ".forum_id\n\t\tWHERE DATE_SUB(CURDATE(),INTERVAL " . $days . " DAY) <= post_date" . $where);
    if ($posts) {
        # give each topic with posts a score - currently ln(cur date - post date) for each post
        $score = $forum_id = $forum_name = $forum_slug = $topic_slug = $topic_name = array();
        foreach ($posts as $post) {
            if ($post->delta != $days) {
                $score[$post->topic_id] = $score[$post->topic_id] + log($days - $post->delta);
                $forum_id[$post->topic_id] = $post->forum_id;
                $forum_name[$post->topic_id] = $post->forum_name;
                $forum_slug[$post->topic_id] = $post->forum_slug;
                $topic_slug[$post->topic_id] = $post->topic_slug;
                $topic_name[$post->topic_id] = $post->topic_name;
            }
        }
        # reverse sort the posts and limit to number to display
        arsort($score);
        $topics = array_slice($score, 0, $limit, true);
        # now output the popular topics
        foreach ($topics as $id => $topic) {
            $p = false;
            # Start contruction
            if ($listtags) {
                $out .= "<li class='sftagli'>\n";
            }
            $out .= sf_get_topic_url($forum_slug[$id], $topic_slug[$id], $topic_name[$id]);
            if ($forum) {
                $out .= "<p class='sftagp'>" . __("posted in forum", "sforum") . ' ' . stripslashes($forum_name[$id]) . "&nbsp;" . "\n";
                $p = true;
            }
            if ($p) {
                $out .= "</p>\n";
            }
            if ($listtags) {
                $out .= "</li>\n";
            }
        }
    } else {
        if ($listtags) {
            $out .= "<li class='sftagli'>\n";
        }
        $out .= '<p>' . __("No Topics to Display", "sforum") . '</p>' . "\n";
        if ($listtags) {
            $out .= "</li>\n";
        }
    }
    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;
    }
}