}
if ($to != 0) {
    $appends .= '&t=' . $to;
}
foreach ($k_blocks as $blk) {
    if ($blk['html_file_name'] == 'block_style_select.html') {
        $block_cache_time = $blk['block_cache_time'];
        break;
    }
}
$block_cache_time = isset($block_cache_time) ? $block_cache_time : $k_config['k_block_cache_time_default'];
$sql = 'SELECT style_id, style_name
	FROM ' . STYLES_TABLE . '
	WHERE style_active = 1
	ORDER BY LOWER(style_name) ASC';
$result = $db->sql_query($sql, $block_cache_time);
while ($row = $db->sql_fetchrow($result)) {
    $style = $request->variable('style', 0);
    if ($style) {
        $url = str_replace('style=' . $style, 'style=' . $row['style_id'], append_sid("{$phpbb_root_path}{$this_page[0]}", $appends));
    } else {
        $url = append_sid("{$phpbb_root_path}{$this_page[0]}", 'style=' . $row['style_id'] . $appends);
    }
    ++$style_count;
    $style_select .= '<option value="' . $url . '"' . ($row['style_id'] == $user->data['user_style'] ? ' selected="selected"' : '') . '>' . strip_tags(sgp_checksize($row['style_name'], 16)) . '</option>';
}
$db->sql_freeresult($result);
if (strlen($style_select)) {
    $template->assign_var('STYLE_SELECT', $style_select);
}
$template->assign_vars(array('STYLE_COUNT' => $style_count, 'S_SHOW_PERM' => $this_page[0] == 'portal' ? true : false));
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB')) {
    exit;
}
global $k_config, $k_blocks;
$queries = $cached_queries = 0;
foreach ($k_blocks as $blk) {
    if ($blk['html_file_name'] == 'block_top_posters.html') {
        $block_cache_time = $blk['block_cache_time'];
    }
}
$block_cache_time = isset($block_cache_time) ? $block_cache_time : $k_config['block_cache_time_default'];
include $phpbb_root_path . 'includes/sgp_functions.' . $phpEx;
$max_top_posters = $k_config['number_of_top_posters_to_display'];
$sql = 'SELECT user_id, username, user_posts, user_colour, user_type, group_id, user_avatar, user_avatar_type, user_avatar_width , user_avatar_height, user_website
	FROM ' . USERS_TABLE . '
	WHERE user_type <> 2
		AND user_posts <> 0
	ORDER BY user_posts DESC';
$result = $db->sql_query_limit($sql, $max_top_posters, 0, $block_cache_time);
while ($row = $db->sql_fetchrow($result)) {
    if (!$row['username']) {
        continue;
    }
    $template->assign_block_vars('top_posters', array('S_SEARCH_ACTION' => append_sid("{$phpbb_root_path}search.{$phpEx}", 'author_id=' . $row['user_id'] . '&amp;sr=posts'), 'USERNAME_FULL' => get_username_string('full', $row['user_id'], sgp_checksize($row['username'], 15), $row['user_colour']), 'POSTER_POSTS' => $row['user_posts'], 'USER_AVATAR_IMG' => sgp_get_user_avatar($row['user_avatar'], $row['user_avatar_type'], '16', '16', $user->lang['USER_AVATAR']), 'URL' => $row['user_website']));
    $template->assign_vars(array('TOP_POSTERS_DEBUG' => sprintf($user->lang['PORTAL_DEBUG_QUERIES'], $queries ? $queries : '0', $cached_queries ? $cached_queries : '0', $total_queries ? $total_queries : '0')));
}
$db->sql_freeresult($result);
//Numbers of users to show in the last online is configurable via ACP
$queries = $cached_queries = 0;
// Can this user view profiles/memberlist/onlinelist?
if ($auth->acl_gets('u_viewprofile')) {
    $this->template->assign_vars(array('VIEWONLINE' => true));
    //Fetch all the block data
    $sql = 'SELECT u.user_id, u.username, u.user_colour, u.user_type, u.user_avatar, u.user_avatar_width, u.user_avatar_height, u.user_avatar_type, u.user_lastvisit, s.session_user_id, MAX(s.session_time) AS session_time
		FROM ' . USERS_TABLE . ' u
		LEFT JOIN ' . SESSIONS_TABLE . ' s ON (u.user_id = s.session_user_id AND session_time >= ' . (time() - $config['session_length']) . ')
		WHERE u.user_type <> ' . USER_IGNORE . '
			AND u.user_lastvisit <> 0
		GROUP BY s.session_user_id, u.user_id
		ORDER BY session_time DESC, u.user_lastvisit DESC';
    $result = $db->sql_query_limit($sql, $k_last_online_max, 0, $block_cache_time);
    $session_times = array();
    while ($row = $db->sql_fetchrow($result)) {
        if (!$row['username']) {
            continue;
        }
        $session_times[$row['session_user_id']] = $row['session_time'];
        $row['session_time'] = !empty($session_times[$row['user_id']]) ? $session_times[$row['user_id']] : 0;
        $row['last_visit'] = !empty($row['session_time']) ? $row['session_time'] : $row['user_lastvisit'];
        $last_visit = !empty($row['session_time']) ? $row['session_time'] : $row['user_lastvisit'];
        $this->template->assign_block_vars('last_online', array('USERNAME_FULL' => get_username_string('full', $row['user_id'], sgp_checksize($row['username'], 15), $row['user_colour']), 'ONLINE_TIME' => empty($last_visit) ? ' - ' : $user->format_date($last_visit, '|d M Y|, H:i'), 'USER_AVATAR_IMG' => phpbb_get_user_avatar($row, $user->lang['USER_AVATAR'], false), 'U_REGISTER' => 'append_sid("{$phpbb_root_path}ucp.$phpEx", mode=register)'));
    }
    $db->sql_freeresult($result);
}
//Is user logged in and have no auth  to view profiles/memberlist/onlinelist?
if ($user->data['user_type'] != USER_IGNORE && !$auth->acl_gets('u_viewprofile')) {
    $template->assign_vars(array('NO_VIEWONLINE_R' => true));
}
    } else {
        $template->assign_vars(array('PROCESS' => true));
    }
}
for ($i = 0; $i < $display_this_many; $i++) {
    if ($recent_topic_row[$i]['user_id'] != -1) {
        // this is a left or right block ? //
        if ($display_center != 'C') {
            $my_title_long = $recent_topic_row[$i]['topic_title'];
            $recent_topic_row[$i]['topic_title'] = sgp_checksize($recent_topic_row[$i]['topic_title'], 20);
            // Michaelo's function to stop page from stretching due to long names in form select options... Names are truncated//
        }
        $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.{$phpEx}", 'f=' . ($recent_topic_row[$i]['forum_id'] ? $recent_topic_row[$i]['forum_id'] : $forum_id));
        // add spaces for nice scrolling
        $my_title = smilies_pass($recent_topic_row[$i]['topic_title']);
        $length = strlen($my_title);
        // Truncate title and padd with ... if too long
        if ($length > 25) {
            sgp_checksize($my_title, 25);
        }
        $my_forum = smilies_pass($recent_topic_row[$i]['forum_name']);
        $length = strlen($my_forum);
        if ($length > 25) {
            sgp_checksize($my_forum, 25);
        }
        $forum_image = isset($recent_topic_row[$i]['forum_image']) ? $recent_topic_row[$i]['forum_image'] : 'images/forum_icons/default.png';
        $template->assign_block_vars($style_row . '_recent_topic_row', array('U_FORUM' => append_sid("viewforum.{$phpEx}", POST_FORUM_URL . '=' . $recent_topic_row[$i]['forum_id']), 'U_TITLE' => append_sid("viewtopic.{$phpEx}", POST_POST_URL . '=' . $recent_topic_row[$i]['post_id']), 'U_LAST_POST' => append_sid($view_topic_url . '&amp;p=' . $recent_topic_row[$i]['topic_last_post_id'] . '#p' . $recent_topic_row[$i]['topic_last_post_id']), 'S_POSTER' => get_username_string('full', $recent_topic_row[$i]['user_id'], $recent_topic_row[$i]['username'], $recent_topic_row[$i]['user_colour']), 'S_POSTTIME' => $user->format_date($recent_topic_row[$i]['post_time']), 'S_ROW_COUNT' => $i, 'S_FORUM' => $last_forum_name == $my_forum ? '' : $my_forum, 'S_TITLE' => $my_title, 'S_TITLE_LONG' => $my_title_long, 'LAST_POST_IMG' => $user->img('icon_topic_newest', 'VIEW_LATEST_POST'), 'FORUM_IMG' => '', 'FORUM_LNK' => append_sid("{$phpbb_root_path}viewforum.{$phpEx}", 'f=' . $recent_topic_row[$i]['forum_id'])));
        $last_forum_name = $my_forum;
    }
}
$template->assign_vars(array('S_DISPLAY_CENTRE' => $display_center, 'S_COUNT' => $display_this_many, 'RECENT_TOPICS_DEBUG' => sprintf($user->lang['PORTAL_DEBUG_QUERIES'], $queries ? $queries : '0', $cached_queries ? $cached_queries : '0', $total_queries ? $total_queries : '0')));
    exit;
}
/**
*/
$queries = $cached_queries = 0;
include $phpbb_root_path . 'includes/sgp_functions.' . $phpEx;
global $k_config, $k_blocks;
foreach ($k_blocks as $blk) {
    if ($blk['html_file_name'] == 'block_bot_tracker.html') {
        $block_cache_time = $blk['block_cache_time'];
    }
}
$block_cache_time = isset($block_cache_time) ? $block_cache_time : $k_config['block_cache_time_default'];
$number_of_bots_to_show = $k_config['number_of_bots_to_display'];
$show_bot_tracker = $k_config['allow_bot_display'];
$after_date = $config['board_startdate'];
$loop_count = 0;
$sql = 'SELECT username, user_colour, user_lastvisit
		FROM ' . USERS_TABLE . '
		WHERE user_type = ' . USER_IGNORE . '
		AND user_lastvisit > ' . $after_date . '
		ORDER BY user_lastvisit DESC';
$result = $db->sql_query_limit($sql, $number_of_bots_to_show, 0, $block_cache_time);
while ($row = $db->sql_fetchrow($result)) {
    $bot_name = get_username_string('full', '', sgp_checksize($row['username'], 23), $row['user_colour']);
    $template->assign_block_vars('bot_tracker', array('BOT_NAME' => $bot_name, 'BOT_TRACKER_VISIT_DATE' => $user->format_date($row['user_lastvisit'], 'D. M. d Y, H:i')));
    $loop_count = $loop_count + 1;
}
$db->sql_freeresult($result);
// assign vars
$template->assign_vars(array('NO_DATA' => $loop_count == 0 ? true : false, 'BOT_TRACKER' => sprintf($user->lang['BOT_TRACKER'], $number_of_bots_to_show), 'S_BOT_TRACKER_SHOW' => $show_bot_tracker ? true : false, 'BOT_TRACKER_DEBUG' => sprintf($user->lang['PORTAL_DEBUG_QUERIES'], $queries ? $queries : '0', $cached_queries ? $cached_queries : '0', $total_queries ? $total_queries : '0')));
	WHERE style_active = 1
	ORDER BY LOWER(style_name) ASC';
$result = $db->sql_query($sql, $block_cache_time);
$styles_num = sizeof($db->sql_fetchrowset($result));
if (!($result = $db->sql_query($sql))) {
    trigger_error($user->lang['ERROR_PORTAL_STYLE_SELECT'] . basename(dirname(__FILE__)) . '/' . basename(__FILE__) . ', line ' . __LINE__);
}
$select_theme = "<select class=\"inputbox autowidth\" onchange=\"this.form.submit();\" name=\"style\" >\n";
while ($row = $db->sql_fetchrow($result)) {
    if ($new_style) {
        $selected = $new_style == $row['style_id'] ? " selected=\"selected\"" : "";
    } else {
        $selected = $style == $row['style_id'] ? " selected=\"selected\"" : "";
    }
    $class = "\"list_release\"";
    $row['style_name'] = sgp_checksize($row['style_name'], 16);
    $select_theme .= "<option class=" . $class . " value=\"" . $row['style_id'] . "\"" . $selected . ">" . $row['style_name'] . "</option>";
}
$select_theme .= "</select>\n";
$select_theme_ok = $user->lang['WARNING_LOGIN_STYLE_SELECT'];
$s_hidden = '<input type="hidden"  id="change" name="change" value="' . $row['style_id'] . '" />';
// as $new_style will be empty for first call we need to set it to something else it will not work on first call //
if (!$new_style) {
    $new_style = $current_style;
}
switch ($style_download_count) {
    case 0:
        $style_download_count = sprintf($user->lang['DOWNLOAD_COUNT_NONE'], $style_download_count);
        break;
    case 1:
        $style_download_count = sprintf($user->lang['DOWNLOAD_COUNT'], $style_download_count);
foreach ($k_blocks as $blk) {
    if ($blk['html_file_name'] == 'block_top_topics.html') {
        $block_cache_time = $blk['block_cache_time'];
    }
}
$block_cache_time = isset($block_cache_time) ? $block_cache_time : $k_config['block_cache_time_default'];
$max_top_topics = $k_config['max_top_topics'];
$days_top_topics = $k_config['days_top_topics'];
$sql = 'SELECT topic_id, topic_title, topic_replies, forum_id
	FROM ' . TOPICS_TABLE . '
	WHERE topic_approved = 1
		AND topic_replies <> 0
		AND topic_status <> 2
		AND topic_last_post_time > ' . (time() - $days_top_topics * 86400) . '
	ORDER BY topic_replies DESC';
$result = $db->sql_query_limit($sql, $max_top_topics, 0, $block_cache_time);
while ($row = $db->sql_fetchrow($result)) {
    if (!$row['topic_title']) {
        continue;
    }
    if ($auth->acl_gets('f_list', 'f_read', $row['forum_id'])) {
        // reduce length and pad with ... if too long //
        $my_title = smilies_pass($row['topic_title']);
        if (strlen($my_title) > 16) {
            $my_title = sgp_checksize($my_title, 14);
        }
        $template->assign_block_vars('top_topics', array('TOPIC_TITLE' => $my_title, 'FULL_T_TITLE' => smilies_pass($row['topic_title']), 'S_SEARCH_ACTION' => append_sid("{$phpbb_root_path}viewtopic.{$phpEx}", 'f=' . $row['forum_id'] . '&amp;t=' . $row['topic_id']), 'TOPIC_REPLIES' => $row['topic_replies']));
    }
}
$db->sql_freeresult($result);
$template->assign_vars(array('TOP_TOPICS_DAYS' => sprintf($user->lang['TOP_TOPICS_DAYS'], $k_config['days_top_topics']), 'TOP_TOPICS_DEBUG' => sprintf($user->lang['PORTAL_DEBUG_QUERIES'], $queries ? $queries : '0', $cached_queries ? $cached_queries : '0', $total_queries ? $total_queries : '0')));
                $sql2 = 'UPDATE ' . K_REFERRALS_TABLE . ' SET hits = ' . ($row['hits'] + 1) . ' , ' . ' lastvisit = ' . $http_time . " WHERE host = '" . $db->sql_escape($http_host) . "'";
                $db->sql_query($sql2);
            } else {
                $sql2 = 'INSERT INTO ' . K_REFERRALS_TABLE . ' (host, hits, firstvisit, lastvisit, enabled)' . " VALUES ('" . $db->sql_escape($http_host) . "' , 1 , " . $http_time . ' , ' . $http_time . ' , 1 )';
                $db->sql_query($sql2);
            }
        }
    }
}
// lets see how many referrals in total (include all)//
$sql = "SELECT hits, SUM(hits) \n\tFROM " . K_REFERRALS_TABLE . "\n\tWHERE enabled = 1";
$result = $db->sql_query($sql, $block_cache_time);
$row = $db->sql_fetchrow($result);
$total_hits = $row['SUM(hits)'];
$db->sql_freeresult($result);
$sql = 'SELECT *
		FROM ' . K_REFERRALS_TABLE . ' 
		WHERE enabled = 1 ORDER BY hits DESC, lastvisit DESC';
$result = $db->sql_query_limit($sql, $num_refviews, 0, $block_cache_time);
if (!$result) {
    trigger_error('ERROR_PORTAL_HTTP');
}
while ($row = $db->sql_fetchrow($result)) {
    $host_name = $row['host'];
    if (strlen($row['host']) > 17) {
        $host_name = sgp_checksize($row['host'], 15);
    }
    $template->assign_block_vars('datarow', array('S_HTTP_HOST' => $host_name, 'S_HHTP_HOST_FULL' => $row['host'], 'U_HTTP_HOST' => 'http://' . $row['host'], 'S_HITS' => $row['hits']));
}
$db->sql_freeresult($result);
$template->assign_vars(array('S_REF_LIMIT' => $num_refviews, 'S_TOT_REF' => $total_hits, 'TOP_REFERRALS_DEBUG' => sprintf($user->lang['PORTAL_DEBUG_QUERIES'], $queries ? $queries : '0', $cached_queries ? $cached_queries : '0', $total_queries ? $total_queries : '0')));
    $next_img = '<img src="' . $phpbb_root_path . "{$web_path}styles/" . $user->theme['theme_path'] . '/theme/images/next_line.gif" height="9px" width="11px" alt="" />';
} else {
    $next_img = '<img src="' . $phpbb_root_path . 'images/next_line.gif" height="9px" width="11px" alt="" />';
}
for ($i = 0; $i < $display_this_many; $i++) {
    $unique = $row[$i]['forum_id'] == $last_forum ? false : true;
    //echo 'Forum ID = ' . $row[$i]['forum_id'] . ' Compare to: '; echo $row[$i - $number_of_topics_per_forum]['forum_id'] . '<br />';
    if ($i >= $number_of_topics_per_forum && $row[$i]['forum_id'] == $row[$i - $number_of_topics_per_forum]['forum_id']) {
        continue;
    }
    // reduce length and pad with ... if too long //
    $my_title = smilies_pass($row[$i]['topic_title']);
    if (strlen($my_title) > 25) {
        sgp_checksize($my_title, 25);
    }
    // reduce length and pad with ... if too long //
    $forum_name = smilies_pass($row[$i]['forum_name']);
    if (strlen($forum_name) > 25) {
        sgp_checksize($forum_name, 25);
    }
    //$view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . (($row[$i]['forum_id']) ? $row[$i]['forum_id'] : $forum_id) );
    $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.{$phpEx}", 'f=' . $row[$i]['forum_id']);
    if ($row[$i]['post_edit_time'] > $row[$i]['topic_last_post_time']) {
        $this_post_time = '*<span style="font-style:italic">' . $user->format_date($row[$i]['post_edit_time']) . '</span>';
    } else {
        $this_post_time = $user->format_date($row[$i]['topic_last_post_time']);
    }
    $template->assign_block_vars($style_row . 'recent_topic_row', array('LAST_POST_IMG_W' => $user->img('icon_topic_newest', 'VIEW_LATEST_POST'), 'LAST_POST_IMG_W' => $next_img, 'FORUM_W' => $forum_name, 'U_FORUM_W' => append_sid("viewforum.{$phpEx}?" . POST_FORUM_URL . '=' . $row[$i]['forum_id']), 'TITLE_W' => censor_text($my_title), 'U_TITLE_W' => $view_topic_url . '&amp;p=' . $row[$i]['topic_last_post_id'] . '#p' . $row[$i]['topic_last_post_id'], 'POSTER_FULL_W' => get_username_string('full', $row[$i]['topic_last_poster_id'], $row[$i]['topic_last_poster_name'], $row[$i]['topic_last_poster_colour']), 'POSTTIME_W' => $this_post_time, 'S_ROW_COUNT_W' => $i, 'S_UNIQUE_W' => $unique, 'S_TYPE_W' => $row[$i]['topic_type'], 'TOOLTIP_W' => bbcode_strip($row[$i]['post_text']), 'TOOLTIP2_W' => bbcode_strip($row[$i]['forum_desc'])));
    $last_forum = $row[$i]['forum_id'];
}
$template->assign_vars(array('S_COUNT_RECENT' => $i > 0 ? true : false, 'SEARCH_TYPE' => !$search_days ? $user->lang['FULL_SEARCH'] : $user->lang['SEARCH_DAYS'] . $search_days, 'SEARCH_LIMIT' => $user->lang['T_LIMITS'] . $number_of_topics_per_forum . $user->lang['TOPICS_PER_FORUM_DISPLAY'] . $display_this_many . ' ' . $user->lang['POSTS'], 'RECENT_TOPICS_WIDE_DEBUG' => sprintf($user->lang['PORTAL_DEBUG_QUERIES'], $queries ? $queries : '0', $cached_queries ? $cached_queries : '0', $total_queries ? $total_queries : '0')));