function sf_sendpm_tag($userid, $text = '')
{
    global $current_user, $sfvars;
    sf_initialise_globals($sfvars['forumid']);
    # dont display tag if not a registered user
    if ($current_user->ID == '' || !$current_user->sfusepm) {
        return;
    }
    $out = '';
    if ($userid) {
        $buddy_name = stripslashes(sf_get_member_item($userid, "display_name"));
        $url = sf_build_qurl("pmaction=sendpm&pms={$current_user->ID}&pmtoname={$buddy_name}");
        if ($text == '') {
            $out .= '<a class="sfsendpmtag" href="' . $url . '"><img src="' . SFRESOURCES . 'sendpm-small.png" title="' . __("Send PM", "sforum") . '" />&nbsp;' . sf_render_icons("Send PM") . '</a>';
        } else {
            $out .= str_replace('%%', $url, $text);
        }
    }
    echo $out;
    return;
}
function sf_is_forumpage()
{
    global $sfvars;
    sf_initialise_globals();
    return $sfvars['page'] == 1;
}
function sf_stats_tag($onlinestats = true, $forumstats = true, $memberstats = true, $topstats = true)
{
    global $wpdb, $sfvars;
    sf_initialise_globals($sfvars['forumid']);
    # do the header
    $out = '';
    # if requested, output the users online stats
    if ($onlinestats) {
        $guests = 0;
        $label = ' ' . __("Guests", "sforum");
        $online = sf_get_online_total();
        if ($online) {
            $members = sf_get_members_online();
            $out .= '<ul class="sfstatonline"><h3>' . __("Currently Online", "sforum") . ': </h3>' . "\n";
            if ($members) {
                foreach ($members as $user) {
                    $out .= '<li class="sfstatadmin">' . $user->display_name . '</li>' . "\n";
                }
            }
            if ($online > count($members)) {
                $guests = $online - count($members);
                if ($guests == 1) {
                    $label = ' ' . __("Guest", "sforum");
                }
                $out .= '<li class="sfstatguest">' . $guests . $label . '</li>' . "\n";
            }
            $out .= '<li class="sfstatmax">' . __("Maximum Online", "sforum") . ': ' . sf_update_max_online(count($online)) . '</li>' . "\n";
            $out .= '</ul>' . "\n";
        }
    }
    # if requested, output the forum stats
    if ($forumstats) {
        $cnt = sf_get_stats_counts();
        $out .= '<ul class="sfstatforums"><h3>' . __("Forum Stats", "sforum") . ': </h3>' . "\n";
        $out .= '<li class="sfstatforum">' . __("Forums: ", "sforum") . '</li>' . "\n";
        $out .= '<li class="sfstatgroup">' . __("Groups: ", "sforum") . $cnt->groups . '</li>' . "\n";
        $out .= '<li class="sfstatforum">' . __("Forums: ", "sforum") . $cnt->forums . '</li>' . "\n";
        $out .= '<li class="sfstattopic">' . __("Topics: ", "sforum") . $cnt->topics . '</li>' . "\n";
        $out .= '<li class="sfstatpost">' . __("Posts: ", "sforum") . $cnt->posts . '</li>' . "\n";
        $out .= '</ul>' . "\n";
    }
    if ($memberstats || $topstats) {
        $sfcontrols = get_option('sfcontrols');
    }
    # if requested, output the members stats
    if ($memberstats) {
        $members = $sfcontrols['membercount'];
        $guests = $sfcontrols['guestcount'];
        $out .= '<ul class="sfstatmembers"><h3>' . __("Members", "sforum") . ': </h3>' . "\n";
        $out .= '<li class="sfstatmembernum">' . sprintf(__("There are %s members", "sforum"), $membercount) . '</li>' . "\n";
        if ($guests) {
            $out .= '<li class="sfstatguestnum">' . sprintf(__("There are %s guests", "sforum"), $guests) . '</li>' . "\n";
        }
        $out .= '</ul>' . "\n";
    }
    # if requested, output the top posters
    if ($topstats) {
        $stats = sf_get_post_stats();
        if ($stats) {
            $out .= '<ul class="sfstattop"><h3>' . __("Top Posters:", "sforum") . '</h3>' . "\n";
            $done = 0;
            foreach ($stats as $stat) {
                if ($stat->admin == false && $stat->moderator == false && $stat->posts > 0) {
                    $out .= '<li class="sfstattopname">' . stripslashes($stat->display_name) . ' - ' . $stat->posts . '</li>' . "\n";
                    $done++;
                }
                if ($done == 6) {
                    break;
                }
            }
            $out .= '</ul>' . "\n";
        }
    }
    echo $out;
    return;
}
function sf_new_posts_tag($unreadmsg = '', $nonemsg = '')
{
    global $current_user, $sfglobals, $wpdb;
    sf_initialise_globals();
    if ($current_user->guest || $sfglobals['member']['newposts'][0] != 0) {
        $sfposts[0] = 1;
    } else {
        $checktime = $sfglobals['member']['checktime'];
        $sfposts = $wpdb->get_col("SELECT DISTINCT topic_id FROM " . SFPOSTS . " WHERE post_status = 0 AND (post_date > '" . $checktime . "') AND user_id != " . $current_user->ID . " ORDER BY topic_id DESC LIMIT 1;");
    }
    if ($sfposts[0] != 0) {
        if ($unreadmsg != '') {
            echo $unreadmsg;
        } else {
            echo __('You have Unread Messages in the', 'sforum') . ' <a href="' . SFURL . '">' . __('Forum', 'sforum') . ' </a>.';
        }
    } else {
        if ($nonemsg = '') {
            echo $nonemsg;
        } else {
            echo __('You have no Unread Messages in the', 'sforum') . ' <a href="' . SFURL . '">' . __('Forum', 'sforum') . ' </a>.';
        }
    }
    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_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_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;
    }
}