function asb_top_poster_build_template($args)
{
    extract($args);
    global ${$template_var}, $db, $templates, $lang, $theme;
    if (!$lang->asb_addon) {
        $lang->load('asb_addon');
    }
    if (!$settings['time_frame']) {
        $settings['time_frame'] = 1;
    }
    $timesearch = TIME_NOW - 86400 * $settings['time_frame'];
    // build user group exclusions (if any)
    $show = asb_build_id_list($settings['group_show_list'], 'u.usergroup');
    $hide = asb_build_id_list($settings['group_hide_list'], 'u.usergroup');
    $where['show'] = asb_build_SQL_where($show, ' OR ');
    $where['hide'] = asb_build_SQL_where($hide, ' OR ', ' NOT ');
    $group_where = asb_build_SQL_where($where, ' AND ', ' AND ');
    $group_by = 'p.uid';
    if ($db->type == 'pgsql') {
        $group_by = $db->build_fields_string('users', 'u.');
    }
    $query = $db->query(<<<EOF
SELECT u.uid, u.username, u.usergroup, u.displaygroup, u.avatar, COUNT(*) AS poststoday
FROM {$db->table_prefix}posts p
LEFT JOIN {$db->table_prefix}users u ON (p.uid=u.uid)
WHERE p.dateline > {$timesearch}{$group_where}
GROUP BY {$group_by} ORDER BY poststoday DESC
LIMIT 1
EOF
);
    // some defaults
    $top_poster = $lang->asb_top_poster_no_one;
    $top_poster_posts = $lang->asb_top_poster_no_posts;
    $top_poster_text = $lang->asb_top_poster_no_top_poster;
    $top_poster_avatar = '';
    $ret_val = false;
    // adjust language for time frame
    switch ($settings['time_frame']) {
        case 7:
            $top_poster_timeframe = $lang->asb_top_poster_one_week;
            break;
        case 14:
            $top_poster_timeframe = $lang->asb_top_poster_two_weeks;
            break;
        case 30:
            $top_poster_timeframe = $lang->asb_top_poster_one_month;
            break;
        case 90:
            $top_poster_timeframe = $lang->asb_top_poster_three_months;
            break;
        case 180:
            $top_poster_timeframe = $lang->asb_top_poster_six_months;
            break;
        case 365:
            $top_poster_timeframe = $lang->asb_top_poster_one_year;
            break;
        default:
            $top_poster_timeframe = $lang->asb_top_poster_one_day;
    }
    $user = $db->fetch_array($query);
    // if we have a user . . .
    if ($user['poststoday']) {
        // default to default :p
        $avatar_width = (int) $width * 0.83;
        if ((int) $settings['avatar_size']) {
            $avatar_width = (int) $settings['avatar_size'];
        }
        // default to guest
        $top_poster = $lang->guest;
        if ($user['uid']) {
            $username = format_name($user['username'], $user['usergroup'], $user['displaygroup']);
            $top_poster = build_profile_link($username, $user['uid']);
        }
        $top_poster_posts = $user['poststoday'];
        $post_lang = $lang->asb_top_poster_posts;
        if ($top_poster_posts == 1) {
            $post_lang = $lang->asb_top_poster_post;
        }
        $top_poster_avatar_src = "{$theme['imgdir']}/default_avatar.gif";
        if ($user['avatar'] != '') {
            $top_poster_avatar_src = $user['avatar'];
        }
        eval("\$top_poster_avatar = \"" . $templates->get('asb_top_poster_avatar') . "\";");
        $top_poster_text = $lang->sprintf($lang->asb_top_poster_congrats, $top_poster, $top_poster_timeframe, $top_poster_posts, $post_lang);
        $ret_val = true;
    }
    eval("\$\$template_var = \"" . $templates->get('asb_top_poster') . "\";");
    // return true if your box has something to show, or false if it doesn't.
    return $ret_val;
}
function latest_threads_get_threadlist($settings, $width)
{
    global $db, $mybb, $templates, $lang, $cache, $gotounread, $theme;
    if (!$lang->asb_addon) {
        $lang->load('asb_addon');
    }
    if ($mybb->user['uid'] == 0) {
        $query = $db->query("\n\t\t\tSELECT\n\t\t\t\tfid\n\t\t\tFROM {$db->table_prefix}forums\n\t\t\tWHERE\n\t\t\t\tactive != 0\n\t\t\tORDER BY\n\t\t\t\tpid, disporder\n\t\t");
        $forumsread = my_unserialize($mybb->cookies['mybb']['forumread']);
    } else {
        $query = $db->query("\n\t\t\tSELECT\n\t\t\t\tf.fid, fr.dateline AS lastread\n\t\t\tFROM {$db->table_prefix}forums f\n\t\t\tLEFT JOIN {$db->table_prefix}forumsread fr ON (fr.fid=f.fid AND fr.uid='{$mybb->user['uid']}')\n\t\t\tWHERE\n\t\t\t\tf.active != 0\n\t\t\tORDER BY\n\t\t\t\tpid, disporder\n\t\t");
    }
    while ($forum = $db->fetch_array($query)) {
        if ($mybb->user['uid'] == 0) {
            if ($forumsread[$forum['fid']]) {
                $forum['lastread'] = $forumsread[$forum['fid']];
            }
        }
        $readforums[$forum['fid']] = $forum['lastread'];
    }
    // Build a post parser
    require_once MYBB_ROOT . 'inc/class_parser.php';
    $parser = new postParser();
    // get forums user cannot view
    $unviewable = get_unviewable_forums(true);
    if ($unviewable) {
        $unviewwhere = " AND t.fid NOT IN ({$unviewable})";
    }
    // get inactive forums
    $inactive = get_inactive_forums();
    if ($inactive) {
        $inactivewhere = " AND t.fid NOT IN ({$inactive})";
    }
    // new threads only?
    if ((int) $settings['new_threads_only'] > 0) {
        // use admin's time limit
        $thread_time_limit = TIME_NOW - 60 * 60 * 24 * (int) $settings['new_threads_only'];
        $new_threads = " AND t.dateline > {$thread_time_limit}";
    }
    if ($settings['important_threads_only']) {
        $important_threads = ' AND NOT t.sticky=0';
    }
    // build the exclude conditions
    $show['fids'] = asb_build_id_list($settings['forum_show_list'], 't.fid');
    $show['tids'] = asb_build_id_list($settings['thread_show_list'], 't.tid');
    $hide['fids'] = asb_build_id_list($settings['forum_hide_list'], 't.fid');
    $hide['tids'] = asb_build_id_list($settings['thread_hide_list'], 't.tid');
    $where['show'] = asb_build_SQL_where($show, ' OR ');
    $where['hide'] = asb_build_SQL_where($hide, ' OR ', ' NOT ');
    $query_where = $new_threads . $important_threads . $unviewwhere . $inactivewhere . asb_build_SQL_where($where, ' AND ', ' AND ');
    $altbg = alt_trow();
    $maxtitlelen = 48;
    $threadlist = '';
    // query for the latest forum discussions
    $query = $db->query("\n\t\tSELECT\n\t\t\tt.*,\n\t\t\tu.username, u.avatar, u.usergroup, u.displaygroup\n\t\tFROM {$db->table_prefix}threads t\n\t\tLEFT JOIN {$db->table_prefix}users u ON (u.uid=t.lastposteruid)\n\t\tWHERE\n\t\t\tt.visible='1' AND t.closed NOT LIKE 'moved|%'{$query_where}\n\t\tORDER BY\n\t\t\tt.lastpost DESC\n\t\tLIMIT\n\t\t\t0, " . (int) $settings['max_threads']);
    if ($db->num_rows($query) == 0) {
        // no content
        return false;
    }
    $thread_cache = array();
    while ($thread = $db->fetch_array($query)) {
        $thread_cache[$thread['tid']] = $thread;
    }
    $thread_ids = implode(",", array_keys($thread_cache));
    // fetch the read threads.
    if ($mybb->user['uid'] && $mybb->settings['threadreadcut'] > 0) {
        $query = $db->simple_select('threadsread', 'tid,dateline', "uid='{$mybb->user['uid']}' AND tid IN({$thread_ids})");
        while ($readthread = $db->fetch_array($query)) {
            $thread_cache[$readthread['tid']]['lastread'] = $readthread['dateline'];
        }
    }
    foreach ($thread_cache as $thread) {
        $forumpermissions[$thread['fid']] = forum_permissions($thread['fid']);
        // make sure we can view this thread
        if ($forumpermissions[$thread['fid']]['canview'] == 0 || $forumpermissions[$thread['fid']]['canviewthreads'] == 0 || $forumpermissions[$thread['fid']]['canonlyviewownthreads'] == 1 && $thread['uid'] != $mybb->user['uid']) {
            continue;
        }
        $lastpostdate = my_date($mybb->settings['dateformat'], $thread['lastpost']);
        $lastposttime = my_date($mybb->settings['timeformat'], $thread['lastpost']);
        // don't link to guest's profiles (they have no profile).
        if ($thread['lastposteruid'] == 0) {
            $lastposterlink = $thread['lastposter'];
        } else {
            if ($settings['last_poster_avatar']) {
                if (strlen(trim($thread['avatar'])) == 0) {
                    $thread['avatar'] = "{$theme['imgdir']}/default_avatar.gif";
                }
                $avatar_width = (int) min($width / 2, max($width / 8, $settings['avatar_width']));
                $last_poster_name = <<<EOF
<img src="{$thread['avatar']}" alt="{$thread['last_post']}" title="{$thread['lastposter']}'s profile" style="width: {$avatar_width}px;"/>
EOF;
                format_name($thread['lastposter'], $thread['usergroup'], $thread['displaygroup']);
                $lp_template = 'asb_latest_threads_last_poster_avatar';
            } else {
                $last_poster_name = format_name($thread['lastposter'], $thread['usergroup'], $thread['displaygroup']);
                $lp_template = 'asb_latest_threads_last_poster_name';
            }
            $lastposterlink = build_profile_link($last_poster_name, $thread['lastposteruid']);
        }
        if (my_strlen($thread['subject']) > $maxtitlelen) {
            $thread['subject'] = my_substr($thread['subject'], 0, $maxtitlelen) . "...";
        }
        $thread['subject'] = htmlspecialchars_uni($parser->parse_badwords($thread['subject']));
        $thread['threadlink'] = get_thread_link($thread['tid']);
        $thread['lastpostlink'] = get_thread_link($thread['tid'], 0, "lastpost");
        eval("\$last_poster = \"" . $templates->get($lp_template) . "\";");
        $gotounread = '';
        $last_read = 0;
        if ($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid']) {
            $forum_read = $readforums[$thread['fid']];
            $read_cutoff = TIME_NOW - $mybb->settings['threadreadcut'] * 60 * 60 * 24;
            if ($forum_read == 0 || $forum_read < $read_cutoff) {
                $forum_read = $read_cutoff;
            }
        } else {
            $forum_read = $forumsread[$thread['fid']];
        }
        if ($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid'] && $thread['lastpost'] > $forum_read) {
            if ($thread['lastread']) {
                $last_read = $thread['lastread'];
            } else {
                $last_read = $read_cutoff;
            }
        } else {
            $last_read = my_get_array_cookie('threadread', $thread['tid']);
        }
        if ($forum_read > $last_read) {
            $last_read = $forum_read;
        }
        if ($thread['lastpost'] > $last_read && $last_read) {
            $thread['newpostlink'] = get_thread_link($thread['tid'], 0, 'newpost');
            eval("\$gotounread = \"" . $templates->get("asb_latest_threads_gotounread") . "\";");
            $unreadpost = 1;
        }
        eval("\$threadlist .= \"" . $templates->get("asb_latest_threads_thread") . "\";");
        $altbg = alt_trow();
    }
    if ($threadlist) {
        return $threadlist;
    }
    // no content
    return false;
}
function asb_rand_quote_get_quote($settings, $width)
{
    global $db, $mybb, $templates, $lang, $theme;
    if (!$lang->asb_addon) {
        $lang->load('asb_addon');
    }
    // get forums user cannot view
    $unviewable = get_unviewable_forums(true);
    if ($unviewable) {
        $unviewwhere = " AND p.fid NOT IN ({$unviewable})";
    }
    // get inactive forums
    $inactive = get_inactive_forums();
    if ($inactive) {
        $inactivewhere = " AND p.fid NOT IN ({$inactive})";
    }
    if ($settings['important_threads_only']) {
        $important_threads = ' AND NOT t.sticky=0';
    }
    // build the exclude conditions
    $show['fids'] = asb_build_id_list($settings['forum_show_list'], 'p.fid');
    $show['tids'] = asb_build_id_list($settings['thread_show_list'], 'p.tid');
    $hide['fids'] = asb_build_id_list($settings['forum_hide_list'], 'p.fid');
    $hide['tids'] = asb_build_id_list($settings['thread_hide_list'], 'p.tid');
    $where['show'] = asb_build_SQL_where($show, ' OR ');
    $where['hide'] = asb_build_SQL_where($hide, ' OR ', ' NOT ');
    $query_where = $important_threads . $unviewwhere . $inactivewhere . asb_build_SQL_where($where, ' AND ', ' AND ');
    $post_query = $db->query("\n\t\tSELECT\n\t\t\tp.pid, p.message, p.fid, p.tid, p.subject, p.uid,\n\t\t\tu.username, u.usergroup, u.displaygroup, u.avatar,\n\t\t\tt.sticky\n\t\tFROM {$db->table_prefix}posts p\n\t\tLEFT JOIN {$db->table_prefix}users u ON (u.uid=p.uid)\n\t\tLEFT JOIN {$db->table_prefix}threads t ON (t.tid=p.tid)\n\t\tWHERE\n\t\t\tp.visible='1'{$query_where}\n\t\tORDER BY\n\t\t\tRAND()\n\t\tLIMIT 1;");
    // if there was 1 . . .
    if ($db->num_rows($post_query) == 0) {
        return false;
    }
    $rand_post = $db->fetch_array($post_query);
    // build a post parser
    require_once MYBB_ROOT . 'inc/class_parser.php';
    $parser = new postParser();
    // we just need the text and smilies (we'll parse them after we check length)
    $pattern = "|[[\\/\\!]*?[^\\[\\]]*?]|si";
    $new_message = asb_strip_url(preg_replace($pattern, '$1', $rand_post['message']));
    // get some dimensions that make sense in relation to column width
    $asb_width = (int) $width;
    $asb_inner_size = $asb_width * 0.83;
    $avatar_size = (int) ($asb_inner_size / 5);
    $font_size = $asb_width / 4.5;
    $font_size = max(10, min(16, $font_size));
    $username_font_size = (int) ($font_size * 0.9);
    $title_font_size = (int) ($font_size * 0.65);
    $message_font_size = (int) $font_size;
    if (strlen($new_message) < $settings['min_length']) {
        if ($settings['default_text']) {
            $new_message = $settings['default_text'];
        } else {
            // nothing to show
            return false;
        }
    }
    if ($settings['max_length'] && strlen($new_message) > $settings['max_length']) {
        $new_message = substr($new_message, 0, $settings['max_length']) . ' . . .';
    }
    // set up the user name link so that it displays correctly for the display group of the user
    $plain_text_username = htmlspecialchars_uni($rand_post['username']);
    $username = format_name($plain_text_username, $rand_post['usergroup'], $rand_post['displaygroup']);
    $author_link = get_profile_link($rand_post['uid']);
    $post_link = get_post_link($rand_post['pid'], $rand_post['tid']) . '#pid' . $rand_post['pid'];
    $thread_link = get_thread_link($rand_post['tid']);
    // allow smilies, but kill
    $parser_options = array("allow_smilies" => 1);
    $new_message = str_replace(array('<br />', '/me'), array('', " * {$plain_text_username}"), $parser->parse_message($new_message . ' ', $parser_options));
    // if the user has an avatar then display it, otherwise force the default avatar.
    $avatar_filename = "{$theme['imgdir']}/default_avatar.gif";
    if ($rand_post['avatar'] != '') {
        $avatar_filename = $rand_post['avatar'];
    }
    $avatar_alt = $lang->sprintf($lang->asb_random_quote_users_profile, $plain_text_username);
    eval("\$read_more = \"" . $templates->get('asb_rand_quote_read_more') . "\";");
    if (my_strlen($rand_post['subject']) > 40) {
        $rand_post['subject'] = my_substr($rand_post['subject'], 0, 40) . ' . . .';
    }
    if (substr(strtolower($rand_post['subject']), 0, 3) == 're:') {
        $rand_post['subject'] = substr($rand_post['subject'], 3);
    }
    $rand_post['subject'] = htmlspecialchars_uni($parser->parse_badwords($rand_post['subject']));
    $thread_title_link = <<<EOF
<strong><a href="{$thread_link}" title="{$lang->asb_random_quotes_read_more_threadlink_title}"><span style="font-size: {$title_font_size}px;">{$rand_post['subject']}</span></a></strong>
EOF;
    // eval() the template
    eval("\$this_quote = \"" . $templates->get("asb_rand_quote_sidebox") . "\";");
    return $this_quote;
}
function recent_posts_get_postlist($settings)
{
    global $db, $mybb, $templates, $lang, $cache, $postlist, $gotounread, $theme;
    // load custom language phrases
    if (!$lang->asb_addon) {
        $lang->load('asb_addon');
    }
    // get forums user cannot view
    $unviewable = get_unviewable_forums(true);
    if ($unviewable) {
        $unviewwhere = " AND p.fid NOT IN ({$unviewable})";
    }
    // get inactive forums
    $inactive = get_inactive_forums();
    if ($inactive) {
        $inactivewhere = " AND p.fid NOT IN ({$inactive})";
    }
    if ($settings['important_threads_only']) {
        $important_threads = ' AND NOT t.sticky=0';
    }
    // build the exclude conditions
    $show['fids'] = asb_build_id_list($settings['forum_show_list'], 'p.fid');
    $show['tids'] = asb_build_id_list($settings['thread_show_list'], 'p.tid');
    $hide['fids'] = asb_build_id_list($settings['forum_hide_list'], 'p.fid');
    $hide['tids'] = asb_build_id_list($settings['thread_hide_list'], 'p.tid');
    $where['show'] = asb_build_SQL_where($show, ' OR ');
    $where['hide'] = asb_build_SQL_where($hide, ' OR ', ' NOT ');
    $query_where = $important_threads . $unviewwhere . $inactivewhere . asb_build_SQL_where($where, ' AND ', ' AND ');
    $altbg = alt_trow();
    $maxtitlelen = 48;
    $postlist = '';
    // Query for the latest forum discussions
    $query = $db->query("\n\t\tSELECT p.tid, p.pid, p.message, p.fid, p.dateline, p.subject,\n\t\t\tu.username, u.uid, u.displaygroup, u.usergroup,\n\t\t\tt.sticky\n\t\tFROM {$db->table_prefix}posts p\n\t\tLEFT JOIN {$db->table_prefix}users u ON (u.uid=p.uid)\n\t\tLEFT JOIN {$db->table_prefix}threads t ON (t.tid=p.tid)\n\t\tWHERE\n\t\t\tp.visible='1'{$query_where}\n\t\tORDER BY\n\t\t\tp.dateline DESC\n\t\tLIMIT\n\t\t\t0, " . (int) $settings['max_posts']);
    if ($db->num_rows($query) == 0) {
        // no content
        return false;
    }
    // Build a post parser
    require_once MYBB_ROOT . 'inc/class_parser.php';
    $parser = new postParser();
    $post_cache = array();
    while ($post = $db->fetch_array($query)) {
        $post_cache[$post['pid']] = $post;
    }
    foreach ($post_cache as $post) {
        $forumpermissions[$post['fid']] = forum_permissions($post['fid']);
        // make sure we can view this post
        if ($forumpermissions[$post['fid']]['canview'] == 0 || $forumpermissions[$post['fid']]['canviewthreads'] == 0 || $forumpermissions[$post['fid']]['canonlyviewownthreads'] == 1 && $post['uid'] != $mybb->user['uid']) {
            continue;
        }
        $lastposttime = my_date($mybb->settings['timeformat'], $post['dateline']);
        // don't link to guest's profiles (they have no profile).
        if ($post['uid'] == 0) {
            $post_author = $post['username'];
        } else {
            $post_author_name = format_name($post['username'], $post['usergroup'], $post['displaygroup']);
            $post_author = build_profile_link($post_author_name, $post['uid']);
        }
        if (my_strlen($post['subject']) > $maxtitlelen) {
            $post['subject'] = my_substr($post['subject'], 0, $maxtitlelen) . '...';
        }
        if (substr(strtolower($post['subject']), 0, 3) == 're:') {
            $post['subject'] = substr($post['subject'], 3);
        }
        $post['subject'] = htmlspecialchars_uni($parser->parse_badwords($post['subject']));
        $post['link'] = get_thread_link($post['tid']) . "&amp;pid={$post['pid']}#pid{$post['pid']}";
        // we just need the text and smilies (we'll parse them after we check length)
        $pattern = "|[[\\/\\!]*?[^\\[\\]]*?]|si";
        $post_excerpt = strip_tags(str_replace('<br />', '', asb_strip_url(preg_replace($pattern, '$1', $post['message']))));
        if (strlen($post_excerpt) > $settings['max_length']) {
            $post_excerpt = substr($post_excerpt, 0, $settings['max_length']) . ' . . .';
        }
        eval("\$postlist .= \"" . $templates->get("asb_recent_posts_post") . "\";");
        $altbg = alt_trow();
    }
    return $postlist;
}