Example #1
0
function getLastPosts($latestPostOptions)
{
    global $scripturl, $txt, $user_info, $modSettings, $smcFunc, $context;
    // Find all the posts.  Newer ones will have higher IDs.  (assuming the last 20 * number are accessable...)
    // !!!SLOW This query is now slow, NEEDS to be fixed.  Maybe break into two?
    $request = $smcFunc['db_query']('substring', '
		SELECT
			m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg,
			IFNULL(mem.real_name, m.poster_name) AS poster_name, t.id_board, b.name AS board_name,
			SUBSTRING(m.body, 1, 385) AS body, m.smileys_enabled
		FROM {db_prefix}messages AS m
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
		WHERE m.id_msg >= {int:likely_max_msg}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
			AND b.id_board != {int:recycle_board}' : '') . '
			AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
			AND t.approved = {int:is_approved}
			AND m.approved = {int:is_approved}' : '') . '
		ORDER BY m.id_msg DESC
		LIMIT ' . $latestPostOptions['number_posts'], array('likely_max_msg' => max(0, $modSettings['maxMsgID'] - 50 * $latestPostOptions['number_posts']), 'recycle_board' => $modSettings['recycle_board'], 'is_approved' => 1));
    $posts = array();
    $context['MemberColor_ID_MEMBER'] = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        // Censor the subject and post for the preview ;).
        censorText($row['subject']);
        censorText($row['body']);
        $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br />' => '&#10;')));
        if ($smcFunc['strlen']($row['body']) > 128) {
            $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';
        }
        // Build the array.
        $posts[] = array('board' => array('id' => $row['id_board'], 'name' => $row['board_name'], 'href' => $scripturl . '?board=' . $row['id_board'] . '.0', 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'), 'topic' => $row['id_topic'], 'poster' => array('id' => $row['id_member'], 'name' => $row['poster_name'], 'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'], 'link' => empty($row['id_member']) ? (!empty($modSettings['MemberColorGuests']) ? '<span style="color:' . $modSettings['MemberColorGuests'] . ';">' : '') . $row['poster_name'] . (!empty($modSettings['MemberColorGuests']) ? '</span>' : '') : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" title="' . $txt['profile_of'] . ' ' . $row['poster_name'] . '">' . $row['poster_name'] . '</a>'), 'subject' => $row['subject'], 'short_subject' => shorten_subject($row['subject'], 24), 'preview' => $row['body'], 'time' => timeformat($row['poster_time']), 'timestamp' => forum_time(true, $row['poster_time']), 'raw_timestamp' => $row['poster_time'], 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'], 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>');
        //The Last Posters id for the MemberColor.
        if (!empty($modSettings['MemberColorRecentLastPost']) && !empty($row['id_member'])) {
            $context['MemberColor_ID_MEMBER'][$row['id_member']] = $row['id_member'];
        }
    }
    $smcFunc['db_free_result']($request);
    // Know set the colors for the Recent posts...
    if (!empty($context['MemberColor_ID_MEMBER'])) {
        $colorDatas = load_onlineColors($context['MemberColor_ID_MEMBER']);
        //So Let's Color The Recent Posts ;)
        if (!empty($modSettings['MemberColorRecentLastPost']) && is_array($posts)) {
            foreach ($posts as $postkey => $postid_memcolor) {
                if (!empty($colorDatas[$postid_memcolor['poster']['id']]['colored_link'])) {
                    $posts[$postkey]['poster']['link'] = $colorDatas[$postid_memcolor['poster']['id']]['colored_link'];
                }
            }
        }
    }
    return $posts;
}
Example #2
0
function list_getGroupRequests($start, $items_per_page, $sort, $where, $where_parameters)
{
    global $smcFunc, $txt, $scripturl;
    $request = $smcFunc['db_query']('', '
		SELECT lgr.id_request, lgr.id_member, lgr.id_group, lgr.time_applied, lgr.reason,
			mem.member_name, mg.group_name, mg.online_color, mem.real_name
		FROM {db_prefix}log_group_requests AS lgr
			INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
			INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
		WHERE ' . $where . '
		ORDER BY {raw:sort}
		LIMIT ' . $start . ', ' . $items_per_page, array_merge($where_parameters, array('sort' => $sort)));
    $group_requests = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        if (!empty($row['id_member'])) {
            $context['MemberColor_ID_MEMBER'][$row['id_member']] = $row['id_member'];
        }
        $group_requests[] = array('id' => $row['id_request'], 'id_member' => $row['id_member'], 'member_link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>', 'group_link' => '<span style="color: ' . $row['online_color'] . '">' . $row['group_name'] . '</span>', 'reason' => censorText($row['reason']), 'time_submitted' => timeformat($row['time_applied']));
    }
    $smcFunc['db_free_result']($request);
    //Give me some Colors baby ;D
    global $modSettings;
    if (!empty($modSettings['MemberColorModCenter']) && !empty($context['MemberColor_ID_MEMBER']) && !empty($group_requests)) {
        $colorDatas = load_onlineColors($context['MemberColor_ID_MEMBER']);
        foreach ($group_requests as $key => $item) {
            if (!empty($colorDatas[$item['id_member']]['colored_link'])) {
                $group_requests[$key]['member_link'] = $colorDatas[$item['id_member']]['colored_link'];
            }
        }
    }
    return $group_requests;
}
Example #3
0
function setupThemeContext($forceload = false)
{
    global $modSettings, $user_info, $scripturl, $context, $settings, $options, $txt, $maintenance;
    global $user_settings, $smcFunc;
    static $loaded = false;
    // Under SSI this function can be called more then once.  That can cause some problems.
    //   So only run the function once unless we are forced to run it again.
    if ($loaded && !$forceload) {
        return;
    }
    $loaded = true;
    $context['in_maintenance'] = !empty($maintenance);
    $context['current_time'] = timeformat(time(), false);
    $context['current_action'] = isset($_GET['action']) ? $_GET['action'] : '';
    $context['show_quick_login'] = !empty($modSettings['enableVBStyleLogin']) && $user_info['is_guest'];
    // Get some news...
    $context['news_lines'] = explode("\n", str_replace("\r", '', trim(addslashes($modSettings['news']))));
    $context['fader_news_lines'] = array();
    for ($i = 0, $n = count($context['news_lines']); $i < $n; $i++) {
        if (trim($context['news_lines'][$i]) == '') {
            continue;
        }
        // Clean it up for presentation ;).
        $context['news_lines'][$i] = parse_bbc(stripslashes(trim($context['news_lines'][$i])), true, 'news' . $i);
        // Gotta be special for the javascript.
        $context['fader_news_lines'][$i] = strtr(addslashes($context['news_lines'][$i]), array('/' => '\\/', '<a href=' => '<a hre" + "f='));
    }
    $context['random_news_line'] = $context['news_lines'][mt_rand(0, count($context['news_lines']) - 1)];
    if (!$user_info['is_guest']) {
        $context['user']['messages'] =& $user_info['messages'];
        $context['user']['unread_messages'] =& $user_info['unread_messages'];
        // Personal message popup...
        if ($user_info['unread_messages'] > (isset($_SESSION['unread_messages']) ? $_SESSION['unread_messages'] : 0)) {
            $context['user']['popup_messages'] = true;
        } else {
            $context['user']['popup_messages'] = false;
        }
        $_SESSION['unread_messages'] = $user_info['unread_messages'];
        if (allowedTo('moderate_forum')) {
            $context['unapproved_members'] = !empty($modSettings['registration_method']) && $modSettings['registration_method'] == 2 ? $modSettings['unapprovedMembers'] : 0;
        }
        $context['show_open_reports'] = empty($user_settings['mod_prefs']) || $user_settings['mod_prefs'][0] == 1;
        $context['user']['avatar'] = array();
        // Figure out the avatar... uploaded?
        if ($user_info['avatar']['url'] == '' && !empty($user_info['avatar']['id_attach'])) {
            $context['user']['avatar']['href'] = $user_info['avatar']['custom_dir'] ? $modSettings['custom_avatar_url'] . '/' . $user_info['avatar']['filename'] : $scripturl . '?action=dlattach;attach=' . $user_info['avatar']['id_attach'] . ';type=avatar';
        } elseif (substr($user_info['avatar']['url'], 0, 7) == 'http://') {
            $context['user']['avatar']['href'] = $user_info['avatar']['url'];
            if ($modSettings['avatar_action_too_large'] == 'option_html_resize' || $modSettings['avatar_action_too_large'] == 'option_js_resize') {
                if (!empty($modSettings['avatar_max_width_external'])) {
                    $context['user']['avatar']['width'] = $modSettings['avatar_max_width_external'];
                }
                if (!empty($modSettings['avatar_max_height_external'])) {
                    $context['user']['avatar']['height'] = $modSettings['avatar_max_height_external'];
                }
            }
        } elseif ($user_info['avatar']['url'] != '') {
            $context['user']['avatar']['href'] = $modSettings['avatar_url'] . '/' . htmlspecialchars($user_info['avatar']['url']);
        }
        if (!empty($context['user']['avatar'])) {
            $context['user']['avatar']['image'] = '<img src="' . $context['user']['avatar']['href'] . '"' . (isset($context['user']['avatar']['width']) ? ' width="' . $context['user']['avatar']['width'] . '"' : '') . (isset($context['user']['avatar']['height']) ? ' height="' . $context['user']['avatar']['height'] . '"' : '') . ' alt="" class="avatar" border="0" />';
        }
        // Figure out how long they've been logged in.
        $context['user']['total_time_logged_in'] = array('days' => floor($user_info['total_time_logged_in'] / 86400), 'hours' => floor($user_info['total_time_logged_in'] % 86400 / 3600), 'minutes' => floor($user_info['total_time_logged_in'] % 3600 / 60));
    } else {
        $context['user']['messages'] = 0;
        $context['user']['unread_messages'] = 0;
        $context['user']['avatar'] = array();
        $context['user']['total_time_logged_in'] = array('days' => 0, 'hours' => 0, 'minutes' => 0);
        $context['user']['popup_messages'] = false;
        if (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 1) {
            $txt['welcome_guest'] .= $txt['welcome_guest_activate'];
        }
        // If we've upgraded recently, go easy on the passwords.
        if (!empty($modSettings['disableHashTime']) && ($modSettings['disableHashTime'] == 1 || time() < $modSettings['disableHashTime'])) {
            $context['disable_login_hashing'] = true;
        } elseif ($context['browser']['is_ie5'] || $context['browser']['is_ie5.5']) {
            $context['disable_login_hashing'] = true;
        }
    }
    // Setup the main menu items.
    setupMenuContext();
    if (empty($settings['theme_version'])) {
        $context['show_vBlogin'] = $context['show_quick_login'];
    }
    // This is here because old index templates might still use it.
    $context['show_news'] = !empty($settings['enable_news']);
    // This is done to allow theme authors to customize it as they want.
    $context['show_pm_popup'] = $context['user']['popup_messages'] && !empty($options['popup_messages']) && (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'pm');
    // Resize avatars the fancy, but non-GD requiring way.
    if ($modSettings['avatar_action_too_large'] == 'option_js_resize' && (!empty($modSettings['avatar_max_width_external']) || !empty($modSettings['avatar_max_height_external']))) {
        $context['html_headers'] .= '
	<script type="text/javascript"><!-- // --><![CDATA[
		var smf_avatarMaxWidth = ' . (int) $modSettings['avatar_max_width_external'] . ';
		var smf_avatarMaxHeight = ' . (int) $modSettings['avatar_max_height_external'] . ';';
        if (!$context['browser']['is_ie'] && !$context['browser']['is_mac_ie']) {
            $context['html_headers'] .= '
	window.addEventListener("load", smf_avatarResize, false);';
        } else {
            $context['html_headers'] .= '
	var window_oldAvatarOnload = window.onload;
	window.onload = smf_avatarResize;';
        }
        // !!! Move this over to script.js?
        $context['html_headers'] .= '
	// ]]></script>';
    }
    // This looks weird, but it's because BoardIndex.php references the variable.
    $context['common_stats']['latest_member'] = array('id' => $modSettings['latestMember'], 'name' => $modSettings['latestRealName'], 'href' => $scripturl . '?action=profile;u=' . $modSettings['latestMember'], 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $modSettings['latestMember'] . '">' . $modSettings['latestRealName'] . '</a>');
    //Latest user need some colors... How could you put this into this file... i hate you... i hope no one read this :P
    if (!empty($modSettings['latestMember']) && !empty($modSettings['MemberColorLatestMember'])) {
        $colorDatas = load_onlineColors($modSettings['latestMember']);
        if (!empty($colorDatas[$modSettings['latestMember']]['colored_link'])) {
            $context['common_stats']['latest_member']['link'] = $colorDatas[$modSettings['latestMember']]['colored_link'];
        }
    }
    $context['common_stats'] = array('total_posts' => comma_format($modSettings['totalMessages']), 'total_topics' => comma_format($modSettings['totalTopics']), 'total_members' => comma_format($modSettings['totalMembers']), 'latest_member' => $context['common_stats']['latest_member']);
    if (empty($settings['theme_version'])) {
        $context['html_headers'] .= '
	<script type="text/javascript"><!-- // --><![CDATA[
		var smf_scripturl = "' . $scripturl . '";
	// ]]></script>';
    }
    if (!isset($context['page_title'])) {
        $context['page_title'] = '';
    }
    // Set some specific vars.
    $context['page_title_html_safe'] = $smcFunc['htmlspecialchars'](un_htmlspecialchars($context['page_title']));
    $context['meta_keywords'] = !empty($modSettings['meta_keywords']) ? $smcFunc['htmlspecialchars']($modSettings['meta_keywords']) : '';
}
Example #4
0
function Display()
{
    global $scripturl, $txt, $modSettings, $context, $settings;
    global $options, $sourcedir, $user_info, $board_info, $topic, $board;
    global $attachments, $messages_request, $topicinfo, $language, $smcFunc;
    // What are you gonna display if these are empty?!
    if (empty($topic)) {
        fatal_lang_error('no_board', false);
    }
    // Load the proper template and/or sub template.
    if (WIRELESS) {
        $context['sub_template'] = WIRELESS_PROTOCOL . '_display';
    } else {
        loadTemplate('Display');
    }
    // Not only does a prefetch make things slower for the server, but it makes it impossible to know if they read it.
    if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch') {
        ob_end_clean();
        header('HTTP/1.1 403 Prefetch Forbidden');
        die;
    }
    // How much are we sticking on each page?
    $context['messages_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) && !WIRELESS ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
    // Let's do some work on what to search index.
    if (count($_GET) > 2) {
        foreach ($_GET as $k => $v) {
            if (!in_array($k, array('topic', 'board', 'start', session_name()))) {
                $context['robot_no_index'] = true;
            }
        }
    }
    if (!empty($_REQUEST['start']) && (!is_numeric($_REQUEST['start']) || $_REQUEST['start'] % $context['messages_per_page'] != 0)) {
        $context['robot_no_index'] = true;
    }
    // Find the previous or next topic.  Make a fuss if there are no more.
    if (isset($_REQUEST['prev_next']) && ($_REQUEST['prev_next'] == 'prev' || $_REQUEST['prev_next'] == 'next')) {
        // No use in calculating the next topic if there's only one.
        if ($board_info['num_topics'] > 1) {
            // Just prepare some variables that are used in the query.
            $gt_lt = $_REQUEST['prev_next'] == 'prev' ? '>' : '<';
            $order = $_REQUEST['prev_next'] == 'prev' ? '' : ' DESC';
            $request = $smcFunc['db_query']('', '
				SELECT t2.id_topic
				FROM {db_prefix}topics AS t
					INNER JOIN {db_prefix}topics AS t2 ON (' . (empty($modSettings['enableStickyTopics']) ? '
					t2.id_last_msg ' . $gt_lt . ' t.id_last_msg' : '
					(t2.id_last_msg ' . $gt_lt . ' t.id_last_msg AND t2.is_sticky ' . $gt_lt . '= t.is_sticky) OR t2.is_sticky ' . $gt_lt . ' t.is_sticky') . ')
				WHERE t.id_topic = {int:current_topic}
					AND t2.id_board = {int:current_board}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
					AND (t2.approved = {int:is_approved} OR (t2.id_member_started != {int:id_member_started} AND t2.id_member_started = {int:current_member}))') . '
				ORDER BY' . (empty($modSettings['enableStickyTopics']) ? '' : ' t2.is_sticky' . $order . ',') . ' t2.id_last_msg' . $order . '
				LIMIT 1', array('current_board' => $board, 'current_member' => $user_info['id'], 'current_topic' => $topic, 'is_approved' => 1, 'id_member_started' => 0));
            // No more left.
            if ($smcFunc['db_num_rows']($request) == 0) {
                $smcFunc['db_free_result']($request);
                // Roll over - if we're going prev, get the last - otherwise the first.
                $request = $smcFunc['db_query']('', '
					SELECT id_topic
					FROM {db_prefix}topics
					WHERE id_board = {int:current_board}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
						AND (approved = {int:is_approved} OR (id_member_started != {int:id_member_started} AND id_member_started = {int:current_member}))') . '
					ORDER BY' . (empty($modSettings['enableStickyTopics']) ? '' : ' is_sticky' . $order . ',') . ' id_last_msg' . $order . '
					LIMIT 1', array('current_board' => $board, 'current_member' => $user_info['id'], 'is_approved' => 1, 'id_member_started' => 0));
            }
            // Now you can be sure $topic is the id_topic to view.
            list($topic) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);
            $context['current_topic'] = $topic;
        }
        // Go to the newest message on this topic.
        $_REQUEST['start'] = 'new';
    }
    // Add 1 to the number of views of this topic.
    if (empty($_SESSION['last_read_topic']) || $_SESSION['last_read_topic'] != $topic) {
        $smcFunc['db_query']('', '
			UPDATE {db_prefix}topics
			SET num_views = num_views + 1
			WHERE id_topic = {int:current_topic}', array('current_topic' => $topic));
        $_SESSION['last_read_topic'] = $topic;
    }
    // Get all the important topic info.
    $request = $smcFunc['db_query']('', '
		SELECT
			t.num_replies, t.num_views, t.locked, ms.subject, t.is_sticky, t.id_poll,
			t.id_member_started, t.id_first_msg, t.id_last_msg, t.approved, t.unapproved_posts,
			' . ($user_info['is_guest'] ? 't.id_last_msg + 1' : 'IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1') . ' AS new_from
			' . (!empty($modSettings['recycle_board']) && $modSettings['recycle_board'] == $board ? ', id_previous_board, id_previous_topic' : '') . '
		FROM {db_prefix}topics AS t
			INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)' . ($user_info['is_guest'] ? '' : '
			LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = {int:current_topic} AND lt.id_member = {int:current_member})
			LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = {int:current_board} AND lmr.id_member = {int:current_member})') . '
		WHERE t.id_topic = {int:current_topic}
		LIMIT 1', array('current_member' => $user_info['id'], 'current_topic' => $topic, 'current_board' => $board));
    if ($smcFunc['db_num_rows']($request) == 0) {
        fatal_lang_error('not_a_topic', false);
    }
    $topicinfo = $smcFunc['db_fetch_assoc']($request);
    $smcFunc['db_free_result']($request);
    $context['num_replies'] = $topicinfo['num_replies'];
    $context['topic_first_message'] = $topicinfo['id_first_msg'];
    // If this topic has unapproved posts, we need to work out how many posts the user can see, for page indexing.
    if ($modSettings['postmod_active'] && $topicinfo['unapproved_posts'] && !$user_info['is_guest'] && !allowedTo('approve_posts')) {
        $request = $smcFunc['db_query']('', '
			SELECT COUNT(id_member) AS my_unapproved_posts
			FROM {db_prefix}messages
			WHERE id_topic = {int:current_topic}
				AND id_member = {int:current_member}
				AND approved = 0', array('current_topic' => $topic, 'current_member' => $user_info['id']));
        list($myUnapprovedPosts) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);
        $context['total_visible_posts'] = $context['num_replies'] + $myUnapprovedPosts + ($topicinfo['approved'] ? 1 : 0);
    } else {
        $context['total_visible_posts'] = $context['num_replies'] + $topicinfo['unapproved_posts'] + ($topicinfo['approved'] ? 1 : 0);
    }
    // When was the last time this topic was replied to?  Should we warn them about it?
    $request = $smcFunc['db_query']('', '
		SELECT poster_time
		FROM {db_prefix}messages
		WHERE id_msg = {int:id_last_msg}
		LIMIT 1', array('id_last_msg' => $topicinfo['id_last_msg']));
    list($lastPostTime) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);
    $context['oldTopicError'] = !empty($modSettings['oldTopicDays']) && $lastPostTime + $modSettings['oldTopicDays'] * 86400 < time() && empty($sticky);
    // The start isn't a number; it's information about what to do, where to go.
    if (!is_numeric($_REQUEST['start'])) {
        // Redirect to the page and post with new messages, originally by Omar Bazavilvazo.
        if ($_REQUEST['start'] == 'new') {
            // Guests automatically go to the last topic.
            if ($user_info['is_guest']) {
                $context['start_from'] = $context['total_visible_posts'] - 1;
                $_REQUEST['start'] = empty($options['view_newest_first']) ? $context['start_from'] : 0;
            } else {
                // Find the earliest unread message in the topic. (the use of topics here is just for both tables.)
                $request = $smcFunc['db_query']('', '
					SELECT IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from
					FROM {db_prefix}topics AS t
						LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = {int:current_topic} AND lt.id_member = {int:current_member})
						LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = {int:current_board} AND lmr.id_member = {int:current_member})
					WHERE t.id_topic = {int:current_topic}
					LIMIT 1', array('current_board' => $board, 'current_member' => $user_info['id'], 'current_topic' => $topic));
                list($new_from) = $smcFunc['db_fetch_row']($request);
                $smcFunc['db_free_result']($request);
                // Fall through to the next if statement.
                $_REQUEST['start'] = 'msg' . $new_from;
            }
        }
        // Start from a certain time index, not a message.
        if (substr($_REQUEST['start'], 0, 4) == 'from') {
            $timestamp = (int) substr($_REQUEST['start'], 4);
            if ($timestamp === 0) {
                $_REQUEST['start'] = 0;
            } else {
                // Find the number of messages posted before said time...
                $request = $smcFunc['db_query']('', '
					SELECT COUNT(*)
					FROM {db_prefix}messages
					WHERE poster_time < {int:timestamp}
						AND id_topic = {int:current_topic}' . ($modSettings['postmod_active'] && $topicinfo['unapproved_posts'] && !allowedTo('approve_posts') ? '
						AND (approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR id_member = {int:current_member})') : ''), array('current_topic' => $topic, 'current_member' => $user_info['id'], 'is_approved' => 1, 'timestamp' => $timestamp));
                list($context['start_from']) = $smcFunc['db_fetch_row']($request);
                $smcFunc['db_free_result']($request);
                // Handle view_newest_first options, and get the correct start value.
                $_REQUEST['start'] = empty($options['view_newest_first']) ? $context['start_from'] : $context['total_visible_posts'] - $context['start_from'] - 1;
            }
        } elseif (substr($_REQUEST['start'], 0, 3) == 'msg') {
            $virtual_msg = (int) substr($_REQUEST['start'], 3);
            if (!$topicinfo['unapproved_posts'] && $virtual_msg >= $topicinfo['id_last_msg']) {
                $context['start_from'] = $context['total_visible_posts'] - 1;
            } elseif (!$topicinfo['unapproved_posts'] && $virtual_msg <= $topicinfo['id_first_msg']) {
                $context['start_from'] = 0;
            } else {
                // Find the start value for that message......
                $request = $smcFunc['db_query']('', '
					SELECT COUNT(*)
					FROM {db_prefix}messages
					WHERE id_msg < {int:virtual_msg}
						AND id_topic = {int:current_topic}' . ($modSettings['postmod_active'] && $topicinfo['unapproved_posts'] && !allowedTo('approve_posts') ? '
						AND (approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR id_member = {int:current_member}') . ')' : ''), array('current_member' => $user_info['id'], 'current_topic' => $topic, 'virtual_msg' => $virtual_msg, 'is_approved' => 1, 'no_member' => 0));
                list($context['start_from']) = $smcFunc['db_fetch_row']($request);
                $smcFunc['db_free_result']($request);
            }
            // We need to reverse the start as well in this case.
            $_REQUEST['start'] = empty($options['view_newest_first']) ? $context['start_from'] : $context['total_visible_posts'] - $context['start_from'] - 1;
        }
    }
    // Create a previous next string if the selected theme has it as a selected option.
    $context['previous_next'] = $modSettings['enablePreviousNext'] ? '<a href="' . $scripturl . '?topic=' . $topic . '.0;prev_next=prev#new">' . $txt['previous_next_back'] . '</a> <a href="' . $scripturl . '?topic=' . $topic . '.0;prev_next=next#new">' . $txt['previous_next_forward'] . '</a>' : '';
    // Check if spellchecking is both enabled and actually working. (for quick reply.)
    $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new');
    // Do we need to show the visual verification image?
    $context['require_verification'] = !$user_info['is_mod'] && !$user_info['is_admin'] && !empty($modSettings['posts_require_captcha']) && ($user_info['posts'] < $modSettings['posts_require_captcha'] || $user_info['is_guest'] && $modSettings['posts_require_captcha'] == -1);
    if ($context['require_verification']) {
        require_once $sourcedir . '/Subs-Editor.php';
        $verificationOptions = array('id' => 'post');
        $context['require_verification'] = create_control_verification($verificationOptions);
        $context['visual_verification_id'] = $verificationOptions['id'];
    }
    // Are we showing signatures - or disabled fields?
    $context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
    $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
    // Censor the title...
    censorText($topicinfo['subject']);
    $context['page_title'] = $topicinfo['subject'];
    // Is this topic sticky, or can it even be?
    $topicinfo['is_sticky'] = empty($modSettings['enableStickyTopics']) ? '0' : $topicinfo['is_sticky'];
    // Default this topic to not marked for notifications... of course...
    $context['is_marked_notify'] = false;
    // Let's get nosey, who is viewing this topic?
    if (!empty($settings['display_who_viewing'])) {
        // Start out with no one at all viewing it.
        $context['view_members'] = array();
        $context['view_members_list'] = array();
        $context['view_num_hidden'] = 0;
        // Search for members who have this topic set in their GET data.
        $request = $smcFunc['db_query']('', '
			SELECT
				lo.id_member, lo.log_time, mem.real_name, mem.member_name, mem.show_online,
				mg.online_color, mg.id_group, mg.group_name
			FROM {db_prefix}log_online AS lo
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lo.id_member)
				LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_id_group} THEN mem.id_post_group ELSE mem.id_group END)
			WHERE INSTR(lo.url, {string:in_url_string}) > 0 OR lo.session = {string:session}', array('reg_id_group' => 0, 'in_url_string' => 's:5:"topic";i:' . $topic . ';', 'session' => $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id()));
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            if (empty($row['id_member'])) {
                continue;
            }
            if (!empty($row['online_color']) && !empty($modSettings['MemberColorWhoIsOnline'])) {
                $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" style="color: ' . $row['online_color'] . ';">' . $row['real_name'] . '</a>';
            } else {
                $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
            }
            $is_buddy = in_array($row['id_member'], $user_info['buddies']);
            if ($is_buddy) {
                $link = '<strong>' . $link . '</strong>';
            }
            // Add them both to the list and to the more detailed list.
            if (!empty($row['show_online']) || allowedTo('moderate_forum')) {
                $context['view_members_list'][$row['log_time'] . $row['member_name']] = empty($row['show_online']) ? '<em>' . $link . '</em>' : $link;
            }
            $context['view_members'][$row['log_time'] . $row['member_name']] = array('id' => $row['id_member'], 'username' => $row['member_name'], 'name' => $row['real_name'], 'group' => $row['id_group'], 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], 'link' => $link, 'is_buddy' => $is_buddy, 'hidden' => empty($row['show_online']));
            if (empty($row['show_online'])) {
                $context['view_num_hidden']++;
            }
        }
        // The number of guests is equal to the rows minus the ones we actually used ;).
        $context['view_num_guests'] = $smcFunc['db_num_rows']($request) - count($context['view_members']);
        $smcFunc['db_free_result']($request);
        // Sort the list.
        krsort($context['view_members']);
        krsort($context['view_members_list']);
    }
    // If all is set, but not allowed... just unset it.
    $can_show_all = !empty($modSettings['enableAllMessages']) && $context['total_visible_posts'] > $context['messages_per_page'] && $context['total_visible_posts'] < $modSettings['enableAllMessages'];
    if (isset($_REQUEST['all']) && !$can_show_all) {
        unset($_REQUEST['all']);
    } elseif (isset($_REQUEST['all'])) {
        $_REQUEST['start'] = -1;
    }
    // Construct the page index, allowing for the .START method...
    $context['page_index'] = constructPageIndex($scripturl . '?topic=' . $topic . '.%1$d', $_REQUEST['start'], $context['total_visible_posts'], $context['messages_per_page'], true);
    $context['start'] = $_REQUEST['start'];
    // This is information about which page is current, and which page we're on - in case you don't like the constructed page index. (again, wireles..)
    $context['page_info'] = array('current_page' => $_REQUEST['start'] / $context['messages_per_page'] + 1, 'num_pages' => floor(($context['total_visible_posts'] - 1) / $context['messages_per_page']) + 1);
    // Figure out all the link to the next/prev/first/last/etc. for wireless mainly.
    $context['links'] = array('first' => $_REQUEST['start'] >= $context['messages_per_page'] ? $scripturl . '?topic=' . $topic . '.0' : '', 'prev' => $_REQUEST['start'] >= $context['messages_per_page'] ? $scripturl . '?topic=' . $topic . '.' . ($_REQUEST['start'] - $context['messages_per_page']) : '', 'next' => $_REQUEST['start'] + $context['messages_per_page'] < $context['total_visible_posts'] ? $scripturl . '?topic=' . $topic . '.' . ($_REQUEST['start'] + $context['messages_per_page']) : '', 'last' => $_REQUEST['start'] + $context['messages_per_page'] < $context['total_visible_posts'] ? $scripturl . '?topic=' . $topic . '.' . floor($context['total_visible_posts'] / $context['messages_per_page']) * $context['messages_per_page'] : '', 'up' => $scripturl . '?board=' . $board . '.0');
    // If they are viewing all the posts, show all the posts, otherwise limit the number.
    if ($can_show_all) {
        if (isset($_REQUEST['all'])) {
            // No limit! (actually, there is a limit, but...)
            $context['messages_per_page'] = -1;
            $context['page_index'] .= empty($modSettings['compactTopicPagesEnable']) ? '<strong>' . $txt['all'] . '</strong> ' : '[<strong>' . $txt['all'] . '</strong>] ';
            // Set start back to 0...
            $_REQUEST['start'] = 0;
        } else {
            $context['page_index'] .= '&nbsp;<a href="' . $scripturl . '?topic=' . $topic . '.0;all">' . $txt['all'] . '</a> ';
        }
    }
    // Build the link tree.
    $context['linktree'][] = array('url' => $scripturl . '?topic=' . $topic . '.0', 'name' => $topicinfo['subject'], 'extra_before' => $settings['linktree_inline'] ? $txt['topic'] . ': ' : '');
    // Build a list of this board's moderators.
    $context['moderators'] =& $board_info['moderators'];
    $context['link_moderators'] = array();
    if (!empty($board_info['moderators'])) {
        //I hate it in this way... but what should i do ;)
        if (!empty($modSettings['MemberColorModeratorLinks'])) {
            foreach ($board_info['moderators'] as $mod) {
                $c[$mod['id']] = $mod['id'];
            }
            $colorDatas = load_onlineColors($c);
            foreach ($board_info['moderators'] as $mod) {
                $context['link_moderators'][] = str_replace($txt['profile_of'], $txt['board_moderator'], $colorDatas[$mod['id']]['colored_link']);
            }
        } else {
            foreach ($board_info['moderators'] as $mod) {
                $context['link_moderators'][] = '<a href="' . $scripturl . '?action=profile;u=' . $mod['id'] . '" title="' . $txt['board_moderator'] . '">' . $mod['name'] . '</a>';
            }
        }
        // And show it after the board's name.
        $context['linktree'][count($context['linktree']) - 2]['extra_after'] = ' (' . (count($context['link_moderators']) == 1 ? $txt['moderator'] : $txt['moderators']) . ': ' . implode(', ', $context['link_moderators']) . ')';
    }
    // Information about the current topic...
    $context['is_locked'] = $topicinfo['locked'];
    $context['is_sticky'] = $topicinfo['is_sticky'];
    $context['is_very_hot'] = $topicinfo['num_replies'] >= $modSettings['hotTopicVeryPosts'];
    $context['is_hot'] = $topicinfo['num_replies'] >= $modSettings['hotTopicPosts'];
    $context['is_approved'] = $topicinfo['approved'];
    // We don't want to show the poll icon in the topic class here, so pretend it's not one.
    $context['is_poll'] = false;
    determineTopicClass($context);
    $context['is_poll'] = $topicinfo['id_poll'] > 0 && $modSettings['pollMode'] == '1' && allowedTo('poll_view');
    // Did this user start the topic or not?
    $context['user']['started'] = $user_info['id'] == $topicinfo['id_member_started'] && !$user_info['is_guest'];
    $context['topic_starter_id'] = $topicinfo['id_member_started'];
    // Set the topic's information for the template.
    $context['subject'] = $topicinfo['subject'];
    $context['num_views'] = $topicinfo['num_views'];
    $context['mark_unread_time'] = $topicinfo['new_from'];
    // Set a canonical URL for this page.
    $context['canonical_url'] = $scripturl . '?topic=' . $topic . '.' . $context['start'];
    // For quick reply we need a response prefix in the default forum language.
    if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix', 600))) {
        if ($language === $user_info['language']) {
            $context['response_prefix'] = $txt['response_prefix'];
        } else {
            loadLanguage('index', $language, false);
            $context['response_prefix'] = $txt['response_prefix'];
            loadLanguage('index');
        }
        cache_put_data('response_prefix', $context['response_prefix'], 600);
    }
    // If we want to show event information in the topic, prepare the data.
    if (allowedTo('calendar_view') && !empty($modSettings['cal_showInTopic']) && !empty($modSettings['cal_enabled'])) {
        // First, try create a better time format, ignoring the "time" elements.
        if (preg_match('~%[AaBbCcDdeGghjmuYy](?:[^%]*%[AaBbCcDdeGghjmuYy])*~', $user_info['time_format'], $matches) == 0 || empty($matches[0])) {
            $date_string = $user_info['time_format'];
        } else {
            $date_string = $matches[0];
        }
        // Any calendar information for this topic?
        $request = $smcFunc['db_query']('', '
			SELECT cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, mem.real_name
			FROM {db_prefix}calendar AS cal
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = cal.id_member)
			WHERE cal.id_topic = {int:current_topic}
			ORDER BY start_date', array('current_topic' => $topic));
        $context['linked_calendar_events'] = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            // Prepare the dates for being formatted.
            $start_date = sscanf($row['start_date'], '%04d-%02d-%02d');
            $start_date = mktime(12, 0, 0, $start_date[1], $start_date[2], $start_date[0]);
            $end_date = sscanf($row['end_date'], '%04d-%02d-%02d');
            $end_date = mktime(12, 0, 0, $end_date[1], $end_date[2], $end_date[0]);
            $context['linked_calendar_events'][] = array('id' => $row['id_event'], 'title' => $row['title'], 'can_edit' => allowedTo('calendar_edit_any') || $row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own'), 'modify_href' => $scripturl . '?action=post;msg=' . $topicinfo['id_first_msg'] . ';topic=' . $topic . '.0;calendar;eventid=' . $row['id_event'] . ';' . $context['session_var'] . '=' . $context['session_id'], 'start_date' => timeformat($start_date, $date_string, 'forum'), 'start_timestamp' => forum_time(true, $start_date), 'end_date' => timeformat($end_date, $date_string, 'forum'), 'end_timestamp' => forum_time(true, $start_date), 'is_last' => false);
        }
        $smcFunc['db_free_result']($request);
        if (!empty($context['linked_calendar_events'])) {
            $context['linked_calendar_events'][count($context['linked_calendar_events']) - 1]['is_last'] = true;
        }
    }
    // Create the poll info if it exists.
    if ($context['is_poll']) {
        // Get the question and if it's locked.
        $request = $smcFunc['db_query']('', '
			SELECT
				p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.change_vote,
				p.guest_vote, p.id_member, IFNULL(mem.real_name, p.poster_name) AS poster_name, p.num_guest_voters, p.reset_poll
			FROM {db_prefix}polls AS p
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = p.id_member)
			WHERE p.id_poll = {int:id_poll}
			LIMIT 1', array('id_poll' => $topicinfo['id_poll']));
        $pollinfo = $smcFunc['db_fetch_assoc']($request);
        $smcFunc['db_free_result']($request);
        $request = $smcFunc['db_query']('', '
			SELECT COUNT(DISTINCT id_member) AS total
			FROM {db_prefix}log_polls
			WHERE id_poll = {int:id_poll}
				AND id_member != {int:not_guest}', array('id_poll' => $topicinfo['id_poll'], 'not_guest' => 0));
        list($pollinfo['total']) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);
        // Total voters needs to include guest voters
        $pollinfo['total'] += $pollinfo['num_guest_voters'];
        // Get all the options, and calculate the total votes.
        $request = $smcFunc['db_query']('', '
			SELECT pc.id_choice, pc.label, pc.votes, IFNULL(lp.id_choice, -1) AS voted_this
			FROM {db_prefix}poll_choices AS pc
				LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_choice = pc.id_choice AND lp.id_poll = {int:id_poll} AND lp.id_member = {int:current_member} AND lp.id_member != {int:not_guest})
			WHERE pc.id_poll = {int:id_poll}', array('current_member' => $user_info['id'], 'id_poll' => $topicinfo['id_poll'], 'not_guest' => 0));
        $pollOptions = array();
        $realtotal = 0;
        $pollinfo['has_voted'] = false;
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            censorText($row['label']);
            $pollOptions[$row['id_choice']] = $row;
            $realtotal += $row['votes'];
            $pollinfo['has_voted'] |= $row['voted_this'] != -1;
        }
        $smcFunc['db_free_result']($request);
        // If this is a guest we need to do our best to work out if they have voted, and what they voted for.
        if ($user_info['is_guest'] && $pollinfo['guest_vote'] && allowedTo('poll_vote')) {
            if (!empty($_COOKIE['guest_poll_vote']) && preg_match('~^[0-9,;]+$~', $_COOKIE['guest_poll_vote']) && strpos($_COOKIE['guest_poll_vote'], ';' . $topicinfo['id_poll'] . ',') !== false) {
                // ;id,timestamp,[vote,vote...]; etc
                $guestinfo = explode(';', $_COOKIE['guest_poll_vote']);
                // Find the poll we're after.
                foreach ($guestinfo as $i => $guestvoted) {
                    $guestvoted = explode(',', $guestvoted);
                    if ($guestvoted[0] == $topicinfo['id_poll']) {
                        break;
                    }
                }
                // Has the poll been reset since guest voted?
                if ($pollinfo['reset_poll'] > $guestvoted[1]) {
                    // Remove the poll info from the cookie to allow guest to vote again
                    unset($guestinfo[$i]);
                    if (!empty($guestinfo)) {
                        $_COOKIE['guest_poll_vote'] = ';' . implode(';', $guestinfo);
                    } else {
                        unset($_COOKIE['guest_poll_vote']);
                    }
                } else {
                    // What did they vote for?
                    unset($guestvoted[0], $guestvoted[1]);
                    foreach ($pollOptions as $choice => $details) {
                        $pollOptions[$choice]['voted_this'] = in_array($choice, $guestvoted) ? 1 : -1;
                        $pollinfo['has_voted'] |= $pollOptions[$choice]['voted_this'] != -1;
                    }
                    unset($choice, $details, $guestvoted);
                }
                unset($guestinfo, $guestvoted, $i);
            }
        }
        // Set up the basic poll information.
        $context['poll'] = array('id' => $topicinfo['id_poll'], 'image' => 'normal_' . (empty($pollinfo['voting_locked']) ? 'poll' : 'locked_poll'), 'question' => parse_bbc($pollinfo['question']), 'total_votes' => $pollinfo['total'], 'change_vote' => !empty($pollinfo['change_vote']), 'is_locked' => !empty($pollinfo['voting_locked']), 'options' => array(), 'lock' => allowedTo('poll_lock_any') || $context['user']['started'] && allowedTo('poll_lock_own'), 'edit' => allowedTo('poll_edit_any') || $context['user']['started'] && allowedTo('poll_edit_own'), 'allowed_warning' => $pollinfo['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($pollOptions), $pollinfo['max_votes'])) : '', 'is_expired' => !empty($pollinfo['expire_time']) && $pollinfo['expire_time'] < time(), 'expire_time' => !empty($pollinfo['expire_time']) ? timeformat($pollinfo['expire_time']) : 0, 'has_voted' => !empty($pollinfo['has_voted']), 'starter' => array('id' => $pollinfo['id_member'], 'name' => $row['poster_name'], 'href' => $pollinfo['id_member'] == 0 ? '' : $scripturl . '?action=profile;u=' . $pollinfo['id_member'], 'link' => $pollinfo['id_member'] == 0 ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $pollinfo['id_member'] . '">' . $row['poster_name'] . '</a>'));
        // Make the lock and edit permissions defined above more directly accessible.
        $context['allow_lock_poll'] = $context['poll']['lock'];
        $context['allow_edit_poll'] = $context['poll']['edit'];
        // You're allowed to vote if:
        // 1. the poll did not expire, and
        // 2. you're either not a guest OR guest voting is enabled... and
        // 3. you're not trying to view the results, and
        // 4. the poll is not locked, and
        // 5. you have the proper permissions, and
        // 6. you haven't already voted before.
        $context['allow_vote'] = !$context['poll']['is_expired'] && (!$user_info['is_guest'] || $pollinfo['guest_vote'] && allowedTo('poll_vote')) && empty($pollinfo['voting_locked']) && allowedTo('poll_vote') && !$context['poll']['has_voted'];
        // You're allowed to view the results if:
        // 1. you're just a super-nice-guy, or
        // 2. anyone can see them (hide_results == 0), or
        // 3. you can see them after you voted (hide_results == 1), or
        // 4. you've waited long enough for the poll to expire. (whether hide_results is 1 or 2.)
        $context['allow_poll_view'] = allowedTo('moderate_board') || $pollinfo['hide_results'] == 0 || $pollinfo['hide_results'] == 1 && $context['poll']['has_voted'] || $context['poll']['is_expired'];
        $context['poll']['show_results'] = $context['allow_poll_view'] && (isset($_REQUEST['viewresults']) || isset($_REQUEST['viewResults']));
        $context['show_view_results_button'] = $context['allow_vote'] && (!$context['allow_poll_view'] || !$context['poll']['show_results'] || !$context['poll']['has_voted']);
        // You're allowed to change your vote if:
        // 1. the poll did not expire, and
        // 2. you're not a guest... and
        // 3. the poll is not locked, and
        // 4. you have the proper permissions, and
        // 5. you have already voted, and
        // 6. the poll creator has said you can!
        $context['allow_change_vote'] = !$context['poll']['is_expired'] && !$user_info['is_guest'] && empty($pollinfo['voting_locked']) && allowedTo('poll_vote') && $context['poll']['has_voted'] && $context['poll']['change_vote'];
        // You're allowed to return to voting options if:
        // 1. you are (still) allowed to vote.
        // 2. you are currently seeing the results.
        $context['allow_return_vote'] = $context['allow_vote'] && $context['poll']['show_results'];
        // Calculate the percentages and bar lengths...
        $divisor = $realtotal == 0 ? 1 : $realtotal;
        // Determine if a decimal point is needed in order for the options to add to 100%.
        $precision = $realtotal == 100 ? 0 : 1;
        // Now look through each option, and...
        foreach ($pollOptions as $i => $option) {
            // First calculate the percentage, and then the width of the bar...
            $bar = round($option['votes'] * 100 / $divisor, $precision);
            $barWide = $bar == 0 ? 1 : floor($bar * 8 / 3);
            // Now add it to the poll's contextual theme data.
            $context['poll']['options'][$i] = array('id' => 'options-' . $i, 'percent' => $bar, 'votes' => $option['votes'], 'voted_this' => $option['voted_this'] != -1, 'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'right' : 'left') . '.gif" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.gif" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'left' : 'right') . '.gif" alt="" /></span>', 'bar_ndt' => $bar > 0 ? '<div class="bar" style="width: ' . ($bar * 3.5 + 4) . 'px;"><div style="width: ' . $bar * 3.5 . 'px;"></div></div>' : '', 'bar_width' => $barWide, 'option' => parse_bbc($option['label']), 'vote_button' => '<input type="' . ($pollinfo['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '" class="input_' . ($pollinfo['max_votes'] > 1 ? 'check' : 'radio') . '" />');
        }
    }
    // Calculate the fastest way to get the messages!
    $ascending = empty($options['view_newest_first']);
    $start = $_REQUEST['start'];
    $limit = $context['messages_per_page'];
    $firstIndex = 0;
    if ($start >= $context['total_visible_posts'] / 2 && $context['messages_per_page'] != -1) {
        $ascending = !$ascending;
        $limit = $context['total_visible_posts'] <= $start + $limit ? $context['total_visible_posts'] - $start : $limit;
        $start = $context['total_visible_posts'] <= $start + $limit ? 0 : $context['total_visible_posts'] - $start - $limit;
        $firstIndex = $limit - 1;
    }
    // Get each post and poster in this topic.
    $request = $smcFunc['db_query']('display_get_post_poster', '
		SELECT id_msg, id_member, approved
		FROM {db_prefix}messages
		WHERE id_topic = {int:current_topic}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : (!empty($modSettings['db_mysql_group_by_fix']) ? '' : '
		GROUP BY id_msg') . '
		HAVING (approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR id_member = {int:current_member}') . ')') . '
		ORDER BY id_msg ' . ($ascending ? '' : 'DESC') . ($context['messages_per_page'] == -1 ? '' : '
		LIMIT ' . $start . ', ' . $limit), array('current_member' => $user_info['id'], 'current_topic' => $topic, 'is_approved' => 1, 'blank_id_member' => 0));
    $messages = array();
    $posters = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        if (!empty($row['id_member'])) {
            $posters[] = $row['id_member'];
        }
        $messages[] = $row['id_msg'];
    }
    $smcFunc['db_free_result']($request);
    $posters = array_unique($posters);
    // Guests can't mark topics read or for notifications, just can't sorry.
    if (!$user_info['is_guest']) {
        $mark_at_msg = max($messages);
        if ($mark_at_msg >= $topicinfo['id_last_msg']) {
            $mark_at_msg = $modSettings['maxMsgID'];
        }
        if ($mark_at_msg >= $topicinfo['new_from']) {
            $smcFunc['db_insert']($topicinfo['new_from'] == 0 ? 'ignore' : 'replace', '{db_prefix}log_topics', array('id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int'), array($user_info['id'], $topic, $mark_at_msg), array('id_member', 'id_topic'));
        }
        // Check for notifications on this topic OR board.
        $request = $smcFunc['db_query']('', '
			SELECT sent, id_topic
			FROM {db_prefix}log_notify
			WHERE (id_topic = {int:current_topic} OR id_board = {int:current_board})
				AND id_member = {int:current_member}
			LIMIT 2', array('current_board' => $board, 'current_member' => $user_info['id'], 'current_topic' => $topic));
        $do_once = true;
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            // Find if this topic is marked for notification...
            if (!empty($row['id_topic'])) {
                $context['is_marked_notify'] = true;
            }
            // Only do this once, but mark the notifications as "not sent yet" for next time.
            if (!empty($row['sent']) && $do_once) {
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}log_notify
					SET sent = {int:is_not_sent}
					WHERE (id_topic = {int:current_topic} OR id_board = {int:current_board})
						AND id_member = {int:current_member}', array('current_board' => $board, 'current_member' => $user_info['id'], 'current_topic' => $topic, 'is_not_sent' => 0));
                $do_once = false;
            }
        }
        // Have we recently cached the number of new topics in this board, and it's still a lot?
        if (isset($_REQUEST['topicseen']) && isset($_SESSION['topicseen_cache'][$board]) && $_SESSION['topicseen_cache'][$board] > 5) {
            $_SESSION['topicseen_cache'][$board]--;
        } elseif (isset($_REQUEST['topicseen'])) {
            // Use the mark read tables... and the last visit to figure out if this should be read or not.
            $request = $smcFunc['db_query']('', '
				SELECT COUNT(*)
				FROM {db_prefix}topics AS t
					LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = {int:current_board} AND lb.id_member = {int:current_member})
					LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
				WHERE t.id_board = {int:current_board}
					AND t.id_last_msg > IFNULL(lb.id_msg, 0)
					AND t.id_last_msg > IFNULL(lt.id_msg, 0)' . (empty($_SESSION['id_msg_last_visit']) ? '' : '
					AND t.id_last_msg > {int:id_msg_last_visit}'), array('current_board' => $board, 'current_member' => $user_info['id'], 'id_msg_last_visit' => (int) $_SESSION['id_msg_last_visit']));
            list($numNewTopics) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);
            // If there're no real new topics in this board, mark the board as seen.
            if (empty($numNewTopics)) {
                $_REQUEST['boardseen'] = true;
            } else {
                $_SESSION['topicseen_cache'][$board] = $numNewTopics;
            }
        } elseif (isset($_SESSION['topicseen_cache'][$board])) {
            $_SESSION['topicseen_cache'][$board]--;
        }
        // Mark board as seen if we came using last post link from BoardIndex. (or other places...)
        if (isset($_REQUEST['boardseen'])) {
            $smcFunc['db_insert']('replace', '{db_prefix}log_boards', array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'), array($modSettings['maxMsgID'], $user_info['id'], $board), array('id_member', 'id_board'));
        }
    }
    $attachments = array();
    // If there _are_ messages here... (probably an error otherwise :!)
    if (!empty($messages)) {
        // Fetch attachments.
        if (!empty($modSettings['attachmentEnable']) && allowedTo('view_attachments')) {
            $request = $smcFunc['db_query']('', '
				SELECT
					a.id_attach, a.id_folder, a.id_msg, a.filename, a.file_hash, IFNULL(a.size, 0) AS filesize, a.downloads, a.approved,
					a.width, a.height' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ',
					IFNULL(thumb.id_attach, 0) AS id_thumb, thumb.width AS thumb_width, thumb.height AS thumb_height') . '
				FROM {db_prefix}attachments AS a' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : '
					LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)') . '
				WHERE a.id_msg IN ({array_int:message_list})
					AND a.attachment_type = {int:attachment_type}' . (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
					AND a.approved = {int:is_approved}'), array('message_list' => $messages, 'attachment_type' => 0, 'is_approved' => 1));
            $temp = array();
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                $temp[$row['id_attach']] = $row;
                if (!isset($attachments[$row['id_msg']])) {
                    $attachments[$row['id_msg']] = array();
                }
            }
            $smcFunc['db_free_result']($request);
            // This is better than sorting it with the query...
            ksort($temp);
            foreach ($temp as $row) {
                $attachments[$row['id_msg']][] = $row;
            }
        }
        // What?  It's not like it *couldn't* be only guests in this topic...
        if (!empty($posters)) {
            loadMemberData($posters);
        }
        $messages_request = $smcFunc['db_query']('', '
			SELECT
				id_msg, icon, subject, poster_time, poster_ip, id_member, modified_time, modified_name, body,
				smileys_enabled, poster_name, poster_email, approved,
				id_msg_modified < {int:new_from} AS is_read
			FROM {db_prefix}messages
			WHERE id_msg IN ({array_int:message_list})
			ORDER BY id_msg' . (empty($options['view_newest_first']) ? '' : ' DESC'), array('message_list' => $messages, 'new_from' => $topicinfo['new_from']));
        // Go to the last message if the given time is beyond the time of the last message.
        if (isset($context['start_from']) && $context['start_from'] >= $topicinfo['num_replies']) {
            $context['start_from'] = $topicinfo['num_replies'];
        }
        // Since the anchor information is needed on the top of the page we load these variables beforehand.
        $context['first_message'] = isset($messages[$firstIndex]) ? $messages[$firstIndex] : $messages[0];
        if (empty($options['view_newest_first'])) {
            $context['first_new_message'] = isset($context['start_from']) && $_REQUEST['start'] == $context['start_from'];
        } else {
            $context['first_new_message'] = isset($context['start_from']) && $_REQUEST['start'] == $topicinfo['num_replies'] - $context['start_from'];
        }
    } else {
        $messages_request = false;
        $context['first_message'] = 0;
        $context['first_new_message'] = false;
    }
    $context['jump_to'] = array('label' => addslashes(un_htmlspecialchars($txt['jump_to'])), 'board_name' => htmlspecialchars(strtr(strip_tags($board_info['name']), array('&amp;' => '&'))), 'child_level' => $board_info['child_level']);
    // Set the callback.  (do you REALIZE how much memory all the messages would take?!?)
    $context['get_message'] = 'prepareDisplayContext';
    // Now set all the wonderful, wonderful permissions... like moderation ones...
    $common_permissions = array('can_approve' => 'approve_posts', 'can_ban' => 'manage_bans', 'can_sticky' => 'make_sticky', 'can_merge' => 'merge_any', 'can_split' => 'split_any', 'calendar_post' => 'calendar_post', 'can_mark_notify' => 'mark_any_notify', 'can_send_topic' => 'send_topic', 'can_send_pm' => 'pm_send', 'can_report_moderator' => 'report_any', 'can_moderate_forum' => 'moderate_forum', 'can_issue_warning' => 'issue_warning', 'can_restore_topic' => 'move_any', 'can_restore_msg' => 'move_any');
    foreach ($common_permissions as $contextual => $perm) {
        $context[$contextual] = allowedTo($perm);
    }
    // Permissions with _any/_own versions.  $context[YYY] => ZZZ_any/_own.
    $anyown_permissions = array('can_move' => 'move', 'can_lock' => 'lock', 'can_delete' => 'remove', 'can_add_poll' => 'poll_add', 'can_remove_poll' => 'poll_remove', 'can_reply' => 'post_reply', 'can_reply_unapproved' => 'post_unapproved_replies');
    foreach ($anyown_permissions as $contextual => $perm) {
        $context[$contextual] = allowedTo($perm . '_any') || $context['user']['started'] && allowedTo($perm . '_own');
    }
    // Cleanup all the permissions with extra stuff...
    $context['can_mark_notify'] &= !$context['user']['is_guest'];
    $context['can_sticky'] &= !empty($modSettings['enableStickyTopics']);
    $context['calendar_post'] &= !empty($modSettings['cal_enabled']);
    $context['can_add_poll'] &= $modSettings['pollMode'] == '1' && $topicinfo['id_poll'] <= 0;
    $context['can_remove_poll'] &= $modSettings['pollMode'] == '1' && $topicinfo['id_poll'] > 0;
    $context['can_reply'] &= empty($topicinfo['locked']) || allowedTo('moderate_board');
    $context['can_reply_unapproved'] &= $modSettings['postmod_active'] && (empty($topicinfo['locked']) || allowedTo('moderate_board'));
    $context['can_issue_warning'] &= in_array('w', $context['admin_features']) && $modSettings['warning_settings'][0] == 1;
    // Handle approval flags...
    $context['can_reply_approved'] = $context['can_reply'];
    $context['can_reply'] |= $context['can_reply_unapproved'];
    $context['can_mark_unread'] = !$user_info['is_guest'] && $settings['show_mark_read'];
    $context['can_send_topic'] = (!$modSettings['postmod_active'] || $topicinfo['approved']) && allowedTo('send_topic');
    // Start this off for quick moderation - it will be or'd for each post.
    $context['can_remove_post'] = allowedTo('delete_any') || allowedTo('delete_replies') && $context['user']['started'];
    // Can restore topic?  That's if the topic is in the recycle board and has a previous restore state.
    $context['can_restore_topic'] &= !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $board && !empty($topicinfo['id_previous_board']);
    $context['can_restore_msg'] &= !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $board && !empty($topicinfo['id_previous_topic']);
    // Wireless shows a "more" if you can do anything special.
    if (WIRELESS && WIRELESS_PROTOCOL != 'wap') {
        $context['wireless_more'] = $context['can_sticky'] || $context['can_lock'] || allowedTo('modify_any');
        $context['wireless_moderate'] = isset($_GET['moderate']) ? ';moderate' : '';
    }
    // Load up the "double post" sequencing magic.
    if (!empty($options['display_quick_reply'])) {
        checkSubmitOnce('register');
        $context['name'] = isset($_SESSION['guest_name']) ? $_SESSION['guest_name'] : '';
        $context['email'] = isset($_SESSION['guest_email']) ? $_SESSION['guest_email'] : '';
    }
}
Example #5
0
function loadMemberContext($user, $display_custom_fields = false)
{
    global $memberContext, $user_profile, $txt, $scripturl, $user_info;
    global $context, $modSettings, $board_info, $settings;
    global $smcFunc;
    static $dataLoaded = array();
    // If this person's data is already loaded, skip it.
    if (isset($dataLoaded[$user])) {
        return true;
    }
    // We can't load guests or members not loaded by loadMemberData()!
    if ($user == 0) {
        return false;
    }
    if (!isset($user_profile[$user])) {
        trigger_error('loadMemberContext(): member id ' . $user . ' not previously loaded by loadMemberData()', E_USER_WARNING);
        return false;
    }
    // Well, it's loaded now anyhow.
    $dataLoaded[$user] = true;
    $profile = $user_profile[$user];
    //Prepare some colored Datas :D Member Color Link give some Infos back 8)
    $coloredData = load_onlineColors($user, true, true);
    // Censor everything.
    censorText($profile['signature']);
    censorText($profile['personal_text']);
    censorText($profile['location']);
    // Set things up to be used before hand.
    $gendertxt = $profile['gender'] == 2 ? $txt['female'] : ($profile['gender'] == 1 ? $txt['male'] : '');
    $profile['signature'] = str_replace(array("\n", "\r"), array('<br />', ''), $profile['signature']);
    $profile['signature'] = parse_bbc($profile['signature'], true, 'sig' . $profile['id_member']);
    $profile['is_online'] = (!empty($profile['show_online']) || allowedTo('moderate_forum')) && $profile['is_online'] > 0;
    $profile['stars'] = empty($profile['stars']) ? array('', '') : explode('#', $profile['stars']);
    // Setup the buddy status here (One whole in_array call saved :P)
    $profile['buddy'] = in_array($profile['id_member'], $user_info['buddies']);
    $buddy_list = !empty($profile['buddy_list']) ? explode(',', $profile['buddy_list']) : array();
    // If we're always html resizing, assume it's too large.
    if ($modSettings['avatar_action_too_large'] == 'option_html_resize' || $modSettings['avatar_action_too_large'] == 'option_js_resize') {
        $avatar_width = !empty($modSettings['avatar_max_width_external']) ? ' width="' . $modSettings['avatar_max_width_external'] . '"' : '';
        $avatar_height = !empty($modSettings['avatar_max_height_external']) ? ' height="' . $modSettings['avatar_max_height_external'] . '"' : '';
    } else {
        $avatar_width = '';
        $avatar_height = '';
    }
    // What a monstrous array...
    $memberContext[$user] = array('username' => $profile['member_name'], 'name' => $profile['real_name'], 'colored_name' => $coloredData['colored_name'], 'online_color' => $coloredData['online_color'], 'id' => $profile['id_member'], 'is_buddy' => $profile['buddy'], 'is_reverse_buddy' => in_array($user_info['id'], $buddy_list), 'buddies' => $buddy_list, 'title' => !empty($modSettings['titlesEnable']) ? $profile['usertitle'] : '', 'href' => $scripturl . '?action=profile;u=' . $profile['id_member'], 'link' => $coloredData['colored_link'], 'colorless_link' => '<a href="' . $scripturl . '?action=profile;u=' . $profile['id_member'] . '" title="' . $txt['profile_of'] . ' ' . $profile['real_name'] . '">' . $profile['real_name'] . '</a>', 'email' => $profile['email_address'], 'show_email' => showEmailAddress(!empty($profile['hide_email']), $profile['id_member']), 'registered' => empty($profile['date_registered']) ? $txt['not_applicable'] : timeformat($profile['date_registered']), 'registered_timestamp' => empty($profile['date_registered']) ? 0 : forum_time(true, $profile['date_registered']), 'blurb' => $profile['personal_text'], 'gender' => array('name' => $gendertxt, 'image' => !empty($profile['gender']) ? '<img class="gender" src="' . $settings['images_url'] . '/' . ($profile['gender'] == 1 ? 'Male' : 'Female') . '.gif" alt="' . $gendertxt . '" border="0" />' : ''), 'website' => array('title' => $profile['website_title'], 'url' => $profile['website_url']), 'birth_date' => empty($profile['birthdate']) || $profile['birthdate'] === '0001-01-01' ? '0000-00-00' : (substr($profile['birthdate'], 0, 4) === '0004' ? '0000' . substr($profile['birthdate'], 4) : $profile['birthdate']), 'signature' => $profile['signature'], 'location' => $profile['location'], 'icq' => $profile['icq'] != '' && (empty($modSettings['guest_hideContacts']) || !$user_info['is_guest']) ? array('name' => $profile['icq'], 'href' => 'http://www.icq.com/whitepages/about_me.php?uin=' . $profile['icq'], 'link' => '<a class="icq new_win" href="http://www.icq.com/whitepages/about_me.php?uin=' . $profile['icq'] . '" target="_blank" title="' . $txt['icq_title'] . ' - ' . $profile['icq'] . '"><img src="http://status.icq.com/online.gif?img=5&amp;icq=' . $profile['icq'] . '" alt="' . $txt['icq_title'] . ' - ' . $profile['icq'] . '" width="18" height="18" border="0" /></a>', 'link_text' => '<a class="icq extern" href="http://www.icq.com/whitepages/about_me.php?uin=' . $profile['icq'] . '" title="' . $txt['icq_title'] . ' - ' . $profile['icq'] . '">' . $profile['icq'] . '</a>') : array('name' => '', 'add' => '', 'href' => '', 'link' => '', 'link_text' => ''), 'aim' => $profile['aim'] != '' && (empty($modSettings['guest_hideContacts']) || !$user_info['is_guest']) ? array('name' => $profile['aim'], 'href' => 'aim:goim?screenname=' . urlencode(strtr($profile['aim'], array(' ' => '%20'))) . '&amp;message=' . $txt['aim_default_message'], 'link' => '<a class="aim" href="aim:goim?screenname=' . urlencode(strtr($profile['aim'], array(' ' => '%20'))) . '&amp;message=' . $txt['aim_default_message'] . '" title="' . $txt['aim_title'] . ' - ' . $profile['aim'] . '"><img src="' . $settings['images_url'] . '/aim.gif" alt="' . $txt['aim_title'] . ' - ' . $profile['aim'] . '" border="0" /></a>', 'link_text' => '<a class="aim" href="aim:goim?screenname=' . urlencode(strtr($profile['aim'], array(' ' => '%20'))) . '&amp;message=' . $txt['aim_default_message'] . '" title="' . $txt['aim_title'] . ' - ' . $profile['aim'] . '">' . $profile['aim'] . '</a>') : array('name' => '', 'href' => '', 'link' => '', 'link_text' => ''), 'yim' => $profile['yim'] != '' && (empty($modSettings['guest_hideContacts']) || !$user_info['is_guest']) ? array('name' => $profile['yim'], 'href' => 'http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($profile['yim']), 'link' => '<a class="yim" href="http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($profile['yim']) . '" title="' . $txt['yim_title'] . ' - ' . $profile['yim'] . '"><img src="http://opi.yahoo.com/online?u=' . urlencode($profile['yim']) . '&amp;m=g&amp;t=0" alt="' . $txt['yim_title'] . ' - ' . $profile['yim'] . '" border="0" /></a>', 'link_text' => '<a class="yim" href="http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($profile['yim']) . '" title="' . $txt['yim_title'] . ' - ' . $profile['yim'] . '">' . $profile['yim'] . '</a>') : array('name' => '', 'href' => '', 'link' => '', 'link_text' => ''), 'msn' => $profile['msn'] != '' && (empty($modSettings['guest_hideContacts']) || !$user_info['is_guest']) ? array('name' => $profile['msn'], 'href' => 'http://members.msn.com/' . $profile['msn'], 'link' => '<a class="msn new_win" href="http://members.msn.com/' . $profile['msn'] . '" title="' . $txt['msn_title'] . ' - ' . $profile['msn'] . '"><img src="' . $settings['images_url'] . '/msntalk.gif" alt="' . $txt['msn_title'] . ' - ' . $profile['msn'] . '" border="0" /></a>', 'link_text' => '<a class="msn new_win" href="http://members.msn.com/' . $profile['msn'] . '" title="' . $txt['msn_title'] . ' - ' . $profile['msn'] . '">' . $profile['msn'] . '</a>') : array('name' => '', 'href' => '', 'link' => '', 'link_text' => ''), 'real_posts' => $profile['posts'], 'posts' => $profile['posts'] > 500000 ? $txt['geek'] : comma_format($profile['posts']), 'avatar' => array('name' => $profile['avatar'], 'image' => $profile['avatar'] == '' ? $profile['id_attach'] > 0 ? '<img class="avatar" src="' . (empty($profile['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $profile['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $profile['filename']) . '" alt="" border="0" />' : '' : (stristr($profile['avatar'], 'http://') ? '<img class="avatar" src="' . $profile['avatar'] . '"' . $avatar_width . $avatar_height . ' alt="" border="0" />' : '<img class="avatar" src="' . $modSettings['avatar_url'] . '/' . htmlspecialchars($profile['avatar']) . '" alt="" border="0" />'), 'href' => $profile['avatar'] == '' ? $profile['id_attach'] > 0 ? empty($profile['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $profile['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $profile['filename'] : '' : (stristr($profile['avatar'], 'http://') ? $profile['avatar'] : $modSettings['avatar_url'] . '/' . $profile['avatar']), 'url' => $profile['avatar'] == '' ? '' : (stristr($profile['avatar'], 'http://') ? $profile['avatar'] : $modSettings['avatar_url'] . '/' . $profile['avatar'])), 'last_login' => empty($profile['last_login']) ? $txt['never'] : timeformat($profile['last_login']), 'last_login_timestamp' => empty($profile['last_login']) ? 0 : forum_time(0, $profile['last_login']), 'karma' => array('good' => $profile['karma_good'], 'bad' => $profile['karma_bad'], 'allow' => !$user_info['is_guest'] && !empty($modSettings['karmaMode']) && $user_info['id'] != $user && allowedTo('karma_edit') && ($user_info['posts'] >= $modSettings['karmaMinPosts'] || $user_info['is_admin'])), 'ip' => htmlspecialchars($profile['member_ip']), 'ip2' => htmlspecialchars($profile['member_ip2']), 'online' => array('is_online' => $profile['is_online'], 'text' => $txt[$profile['is_online'] ? 'online' : 'offline'], 'href' => $scripturl . '?action=pm;sa=send;u=' . $profile['id_member'], 'link' => '<a href="' . $scripturl . '?action=pm;sa=send;u=' . $profile['id_member'] . '">' . $txt[$profile['is_online'] ? 'online' : 'offline'] . '</a>', 'image_href' => $settings['images_url'] . '/' . ($profile['buddy'] ? 'buddy_' : '') . ($profile['is_online'] ? 'useron' : 'useroff') . '.gif', 'label' => $txt[$profile['is_online'] ? 'online' : 'offline']), 'language' => $smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => ''))), 'is_activated' => isset($profile['is_activated']) ? $profile['is_activated'] : 1, 'is_banned' => isset($profile['is_activated']) ? $profile['is_activated'] >= 10 : 0, 'options' => $profile['options'], 'is_guest' => false, 'group' => $profile['member_group'], 'group_color' => $profile['member_group_color'], 'group_id' => $profile['id_group'], 'post_group' => $profile['post_group'], 'post_group_color' => $profile['post_group_color'], 'group_stars' => str_repeat('<img src="' . str_replace('$language', $context['user']['language'], isset($profile['stars'][1]) ? $settings['images_url'] . '/' . $profile['stars'][1] : '') . '" alt="*" border="0" />', empty($profile['stars'][0]) || empty($profile['stars'][1]) ? 0 : $profile['stars'][0]), 'warning' => $profile['warning'], 'warning_status' => !empty($modSettings['warning_mute']) && $modSettings['warning_mute'] <= $profile['warning'] ? 'mute' : (!empty($modSettings['warning_moderate']) && $modSettings['warning_moderate'] <= $profile['warning'] ? 'moderate' : (!empty($modSettings['warning_watch']) && $modSettings['warning_watch'] <= $profile['warning'] ? 'watch' : '')), 'local_time' => timeformat(time() + ($profile['time_offset'] - $user_info['time_offset']) * 3600, false), 'awards' => &$profile['awards']);
    // First do a quick run through to make sure there is something to be shown.
    $memberContext[$user]['has_messenger'] = false;
    foreach (array('icq', 'msn', 'aim', 'yim') as $messenger) {
        if (!isset($context['disabled_fields'][$messenger]) && !empty($memberContext[$user][$messenger]['link'])) {
            $memberContext[$user]['has_messenger'] = true;
            break;
        }
    }
    // Are we also loading the members custom fields into context?
    if ($display_custom_fields && !empty($modSettings['displayFields'])) {
        $memberContext[$user]['custom_fields'] = array();
        if (!isset($context['display_fields'])) {
            $context['display_fields'] = unserialize($modSettings['displayFields']);
        }
        foreach ($context['display_fields'] as $custom) {
            if (empty($custom['title']) || empty($profile['options'][$custom['colname']])) {
                continue;
            }
            $value = $profile['options'][$custom['colname']];
            // BBC?
            if ($custom['bbc']) {
                $value = parse_bbc($value);
            }
            // Enclosing the user input within some other text?
            if (!empty($custom['enclose'])) {
                $value = strtr($custom['enclose'], array('{SCRIPTURL}' => $scripturl, '{IMAGES_URL}' => $settings['images_url'], '{DEFAULT_IMAGES_URL}' => $settings['default_images_url'], '{INPUT}' => $value));
            }
            $memberContext[$user]['custom_fields'][] = array('title' => $custom['title'], 'colname' => $custom['colname'], 'value' => $value, 'placement' => !empty($custom['placement']) ? $custom['placement'] : 0);
        }
    }
    return true;
}
/**
 * This is a small script to load colors for SPortal.
 *
 * @param int[] $users
 */
function sp_loadColors($users = array())
{
    global $color_profile, $scripturl, $modSettings;
    $db = database();
    // This is for later, if you like to disable colors ;)
    if (!empty($modSettings['sp_disableColor'])) {
        return false;
    }
    // Can't just look for no users. :P
    if (empty($users)) {
        return false;
    }
    // MemberColorLink compatible, cache more data, handle also some special member color link colors
    if (!empty($modSettings['MemberColorLinkInstalled'])) {
        $colorData = load_onlineColors($users);
        // This happen only on not existing Members... but given ids...
        if (empty($colorData)) {
            return false;
        }
        $loaded_ids = array_keys($colorData);
        foreach ($loaded_ids as $id) {
            if (!empty($id) && !isset($color_profile[$id]['link'])) {
                $color_profile[$id]['link'] = $colorData[$id]['colored_link'];
                $color_profile[$id]['colored_name'] = $colorData[$id]['colored_name'];
            }
        }
        return empty($loaded_ids) ? false : $loaded_ids;
    }
    // Make sure it's an array.
    $users = !is_array($users) ? array($users) : array_unique($users);
    // Check up the array :)
    foreach ($users as $k => $u) {
        $u = (int) $u;
        if (empty($u)) {
            unset($users[$k]);
        } else {
            $users[$k] = $u;
        }
    }
    $loaded_ids = array();
    // Is this a totally new variable?
    if (empty($color_profile)) {
        $color_profile = array();
    } else {
        foreach ($users as $k => $u) {
            if (isset($color_profile[$u])) {
                $loaded_ids[] = $u;
                unset($users[$k]);
            }
        }
    }
    // Make sure that we have some users.
    if (empty($users)) {
        return empty($loaded_ids) ? false : $loaded_ids;
    }
    // Correct array pointer for the user
    reset($users);
    // Load the data.
    $request = $db->query('', '
		SELECT
			mem.id_member, mem.member_name, mem.real_name, mem.id_group,
			mg.online_color AS member_group_color, pg.online_color AS post_group_color
		FROM {db_prefix}members AS mem
			LEFT JOIN {db_prefix}membergroups AS pg ON (pg.id_group = mem.id_post_group)
			LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
		WHERE mem.id_member ' . (count($users) == 1 ? '= {int:current}' : 'IN ({array_int:users})'), array('users' => $users, 'current' => (int) current($users)));
    // Go through each of the users.
    while ($row = $db->fetch_assoc($request)) {
        $loaded_ids[] = $row['id_member'];
        $color_profile[$row['id_member']] = $row;
        $onlineColor = !empty($row['member_group_color']) ? $row['member_group_color'] : $row['post_group_color'];
        $color_profile[$row['id_member']]['color'] = $onlineColor;
        $color_profile[$row['id_member']]['link'] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '"' . (!empty($onlineColor) ? ' style="color: ' . $onlineColor . ';"' : '') . '>' . $row['real_name'] . '</a>';
        $color_profile[$row['id_member']]['colored_name'] = (!empty($onlineColor) ? '<span style="color: ' . $onlineColor . ';">' : '') . $row['real_name'] . (!empty($onlineColor) ? '</span>' : '');
    }
    $db->free_result($request);
    // Return the necessary data.
    return empty($loaded_ids) ? false : $loaded_ids;
}
Example #7
0
function BoardIndex()
{
    global $txt, $user_info, $sourcedir, $modSettings, $context, $settings, $scripturl;
    // For wireless, we use the Wireless template...
    if (WIRELESS) {
        $context['sub_template'] = WIRELESS_PROTOCOL . '_boardindex';
    } else {
        loadTemplate('BoardIndex');
    }
    // Set a canonical URL for this page.
    $context['canonical_url'] = $scripturl;
    // Do not let search engines index anything if there is a random thing in $_GET.
    if (!empty($_GET)) {
        $context['robot_no_index'] = true;
    }
    // Retrieve the categories and boards.
    require_once $sourcedir . '/Subs-BoardIndex.php';
    $boardIndexOptions = array('include_categories' => true, 'base_level' => 0, 'parent_id' => 0, 'set_latest_post' => true, 'countChildPosts' => !empty($modSettings['countChildPosts']));
    $context['categories'] = getBoardIndex($boardIndexOptions);
    //The latest member?
    if (!empty($modSettings['latestMember']) && !empty($modSettings['MemberColorLatestMember'])) {
        $context['MemberColor_ID_MEMBER'][$modSettings['latestMember']] = $modSettings['latestMember'];
    }
    // Know set the colors for the last post...
    if (!empty($context['MemberColor_ID_MEMBER'])) {
        $colorDatas = load_onlineColors($context['MemberColor_ID_MEMBER']);
        $cmemcolid = null;
        //So the BoardIndex need colored links
        if (!empty($modSettings['MemberColorBoardindex']) && !empty($context['MemberColor_board'])) {
            foreach ($context['MemberColor_board'] as $boardid_memcolor) {
                $cmemcolid = $context['categories'][$boardid_memcolor['cat']]['boards'][$boardid_memcolor['bid']]['last_post']['member']['id'];
                if (!empty($colorDatas[$cmemcolid]['colored_link'])) {
                    $context['categories'][$boardid_memcolor['cat']]['boards'][$boardid_memcolor['bid']]['last_post']['member']['link'] = $colorDatas[$cmemcolid]['colored_link'];
                }
            }
        }
        //You are not know what you do ;P Colors allready loaded :D
        if (!empty($modSettings['MemberColorModeratorLinks']) && !empty($context['MemberColor_ModBoard'])) {
            //Okay now... do a heavy serach for moderators... only jokeing... but you know... it look so ugly ;)
            $onlineColor = load_mod_color(true);
            global $scripturl, $color_profile;
            foreach ($context['MemberColor_ModBoard'] as $boardid_memcolor) {
                //Reset it :D
                $context['categories'][$boardid_memcolor['cat']]['boards'][$boardid_memcolor['bid']]['link_moderators'] = array();
                foreach ($context['categories'][$boardid_memcolor['cat']]['boards'][$boardid_memcolor['bid']]['moderators'] as $moderators) {
                    $cmemcolid = $moderators['id'];
                    //Replace "Profil of" with "Moderator" ;D
                    $link = str_replace($txt['profile_of'], $txt['board_moderator'], $colorDatas[$cmemcolid]['colored_link']);
                    if (empty($colorDatas[$cmemcolid]['online_color']) && !empty($onlineColor)) {
                        $link = '<a href="' . $scripturl . '?action=profile;u=' . $color_profile[$cmemcolid]['id_member'] . '" title="' . $txt['board_moderator'] . ' ' . $color_profile[$cmemcolid]['real_name'] . '"' . (!empty($modSettings['MemberColorLinkOldSpanStyle']) ? '><span style="color:' . $onlineColor . ';">' : ' style="color:' . $onlineColor . ';">') . $color_profile[$cmemcolid]['real_name'] . (!empty($modSettings['MemberColorLinkOldSpanStyle']) ? '</span>' : '') . '</a>';
                    }
                    $context['categories'][$boardid_memcolor['cat']]['boards'][$boardid_memcolor['bid']]['moderators'][$cmemcolid]['link'] = $link;
                    //Creat the new list...
                    $context['categories'][$boardid_memcolor['cat']]['boards'][$boardid_memcolor['bid']]['link_moderators'][] = $link;
                }
            }
        }
    }
    // Get the user online list.
    require_once $sourcedir . '/Subs-MembersOnline.php';
    $membersOnlineOptions = array('show_hidden' => allowedTo('moderate_forum'), 'sort' => 'log_time', 'reverse_sort' => true);
    $context += getMembersOnlineStats($membersOnlineOptions);
    $context['show_buddies'] = !empty($user_info['buddies']);
    // Are we showing all membergroups on the board index?
    if (!empty($settings['show_group_key'])) {
        $context['membergroups'] = cache_quick_get('membergroup_list', 'Subs-Membergroups.php', 'cache_getMembergroupList', array());
    }
    // Track most online statistics? (Subs-MembersOnline.php)
    if (!empty($modSettings['trackStats'])) {
        trackStatsUsersOnline($context['num_guests'] + $context['num_spiders'] + $context['num_users_online']);
    }
    // Retrieve the latests posts if the theme settings require it.
    if (isset($settings['number_recent_posts']) && $settings['number_recent_posts'] > 1) {
        $latestPostOptions = array('number_posts' => $settings['number_recent_posts']);
        $context['latest_posts'] = cache_quick_get('boardindex-latest_posts:' . md5($user_info['query_wanna_see_board'] . $user_info['language']), 'Subs-Recent.php', 'cache_getLastPosts', array($latestPostOptions));
    }
    $settings['display_recent_bar'] = !empty($settings['number_recent_posts']) ? $settings['number_recent_posts'] : 0;
    $settings['show_member_bar'] &= allowedTo('view_mlist');
    $context['show_stats'] = allowedTo('view_stats') && !empty($modSettings['trackStats']);
    $context['show_member_list'] = allowedTo('view_mlist');
    $context['show_who'] = allowedTo('who_view') && !empty($modSettings['who_enabled']);
    // Load the calendar?
    if (!empty($modSettings['cal_enabled']) && allowedTo('calendar_view')) {
        // Retrieve the calendar data (events, birthdays, holidays).
        $eventOptions = array('include_holidays' => $modSettings['cal_showholidays'] > 1, 'include_birthdays' => $modSettings['cal_showbdays'] > 1, 'include_events' => $modSettings['cal_showevents'] > 1, 'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index']);
        $context += cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));
        // Whether one or multiple days are shown on the board index.
        $context['calendar_only_today'] = $modSettings['cal_days_for_index'] == 1;
        // This is used to show the "how-do-I-edit" help.
        $context['calendar_can_edit'] = allowedTo('calendar_edit_any');
    } else {
        $context['show_calendar'] = false;
    }
    $context['page_title'] = sprintf($txt['forum_index'], $context['forum_name']);
}
Example #8
0
function DisplayStats()
{
    global $txt, $scripturl, $modSettings, $user_info, $context, $smcFunc;
    isAllowedTo('view_stats');
    if (!empty($_REQUEST['expand'])) {
        $context['robot_no_index'] = true;
        $month = (int) substr($_REQUEST['expand'], 4);
        $year = (int) substr($_REQUEST['expand'], 0, 4);
        if ($year > 1900 && $year < 2200 && $month >= 1 && $month <= 12) {
            $_SESSION['expanded_stats'][$year][] = $month;
        }
    } elseif (!empty($_REQUEST['collapse'])) {
        $context['robot_no_index'] = true;
        $month = (int) substr($_REQUEST['collapse'], 4);
        $year = (int) substr($_REQUEST['collapse'], 0, 4);
        if (!empty($_SESSION['expanded_stats'][$year])) {
            $_SESSION['expanded_stats'][$year] = array_diff($_SESSION['expanded_stats'][$year], array($month));
        }
    }
    // Handle the XMLHttpRequest.
    if (isset($_REQUEST['xml'])) {
        // Collapsing stats only needs adjustments of the session variables.
        if (!empty($_REQUEST['collapse'])) {
            obExit(false);
        }
        $context['sub_template'] = 'stats';
        getDailyStats('YEAR(date) = {int:year} AND MONTH(date) = {int:month}', array('year' => $year, 'month' => $month));
        $context['yearly'][$year]['months'][$month]['date'] = array('month' => sprintf('%02d', $month), 'year' => $year);
        return;
    }
    loadLanguage('Stats');
    loadTemplate('Stats');
    // Build the link tree......
    $context['linktree'][] = array('url' => $scripturl . '?action=stats', 'name' => $txt['stats_center']);
    $context['page_title'] = $context['forum_name'] . ' - ' . $txt['stats_center'];
    $context['show_member_list'] = allowedTo('view_mlist');
    // Get averages...
    $result = $smcFunc['db_query']('', '
		SELECT
			SUM(posts) AS posts, SUM(topics) AS topics, SUM(registers) AS registers,
			SUM(most_on) AS most_on, MIN(date) AS date, SUM(hits) AS hits
		FROM {db_prefix}log_activity', array());
    $row = $smcFunc['db_fetch_assoc']($result);
    $smcFunc['db_free_result']($result);
    // This would be the amount of time the forum has been up... in days...
    $total_days_up = ceil((time() - strtotime($row['date'])) / (60 * 60 * 24));
    $context['average_posts'] = comma_format(round($row['posts'] / $total_days_up, 2));
    $context['average_topics'] = comma_format(round($row['topics'] / $total_days_up, 2));
    $context['average_members'] = comma_format(round($row['registers'] / $total_days_up, 2));
    $context['average_online'] = comma_format(round($row['most_on'] / $total_days_up, 2));
    $context['average_hits'] = comma_format(round($row['hits'] / $total_days_up, 2));
    $context['num_hits'] = comma_format($row['hits'], 0);
    // How many users are online now.
    $result = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}log_online', array());
    list($context['users_online']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);
    // Statistics such as number of boards, categories, etc.
    $result = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}boards AS b
		WHERE b.redirect = {string:blank_redirect}', array('blank_redirect' => ''));
    list($context['num_boards']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);
    $result = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}categories AS c', array());
    list($context['num_categories']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);
    // Format the numbers nicely.
    $context['users_online'] = comma_format($context['users_online']);
    $context['num_boards'] = comma_format($context['num_boards']);
    $context['num_categories'] = comma_format($context['num_categories']);
    $context['num_members'] = comma_format($modSettings['totalMembers']);
    $context['num_posts'] = comma_format($modSettings['totalMessages']);
    $context['num_topics'] = comma_format($modSettings['totalTopics']);
    $context['most_members_online'] = array('number' => comma_format($modSettings['mostOnline']), 'date' => timeformat($modSettings['mostDate']));
    $context['latest_member'] =& $context['common_stats']['latest_member'];
    // Male vs. female ratio - let's calculate this only every four minutes.
    if (($context['gender'] = cache_get_data('stats_gender', 240)) == null) {
        $result = $smcFunc['db_query']('', '
			SELECT COUNT(*) AS total_members, gender
			FROM {db_prefix}members
			GROUP BY gender', array());
        $context['gender'] = array();
        while ($row = $smcFunc['db_fetch_assoc']($result)) {
            // Assuming we're telling... male or female?
            if (!empty($row['gender'])) {
                $context['gender'][$row['gender'] == 2 ? 'females' : 'males'] = $row['total_members'];
            }
        }
        $smcFunc['db_free_result']($result);
        // Set these two zero if the didn't get set at all.
        if (empty($context['gender']['males'])) {
            $context['gender']['males'] = 0;
        }
        if (empty($context['gender']['females'])) {
            $context['gender']['females'] = 0;
        }
        // Try and come up with some "sensible" default states in case of a non-mixed board.
        if ($context['gender']['males'] == $context['gender']['females']) {
            $context['gender']['ratio'] = '1:1';
        } elseif ($context['gender']['males'] == 0) {
            $context['gender']['ratio'] = '0:1';
        } elseif ($context['gender']['females'] == 0) {
            $context['gender']['ratio'] = '1:0';
        } elseif ($context['gender']['males'] > $context['gender']['females']) {
            $context['gender']['ratio'] = round($context['gender']['males'] / $context['gender']['females'], 1) . ':1';
        } elseif ($context['gender']['females'] > $context['gender']['males']) {
            $context['gender']['ratio'] = '1:' . round($context['gender']['females'] / $context['gender']['males'], 1);
        }
        cache_put_data('stats_gender', $context['gender'], 240);
    }
    $date = strftime('%Y-%m-%d', forum_time(false));
    // Members online so far today.
    $result = $smcFunc['db_query']('', '
		SELECT most_on
		FROM {db_prefix}log_activity
		WHERE date = {date:today_date}
		LIMIT 1', array('today_date' => $date));
    list($context['online_today']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);
    $context['online_today'] = comma_format((int) $context['online_today']);
    // Poster top 10.
    $members_result = $smcFunc['db_query']('', '
		SELECT id_member, real_name, posts
		FROM {db_prefix}members
		WHERE posts > {int:no_posts}
		ORDER BY posts DESC
		LIMIT 10', array('no_posts' => 0));
    $context['top_posters'] = array();
    $max_num_posts = 1;
    $context['MemberColor_ID_MEMBER'] = array();
    while ($row_members = $smcFunc['db_fetch_assoc']($members_result)) {
        $context['top_posters'][] = array('name' => $row_members['real_name'], 'id' => $row_members['id_member'], 'num_posts' => $row_members['posts'], 'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'], 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>');
        if ($max_num_posts < $row_members['posts']) {
            $max_num_posts = $row_members['posts'];
        }
        if (!empty($modSettings['MemberColorStats']) && !empty($row_members['id_member'])) {
            $context['MemberColor_ID_MEMBER'][$row_members['id_member']] = $row_members['id_member'];
        }
    }
    $smcFunc['db_free_result']($members_result);
    foreach ($context['top_posters'] as $i => $poster) {
        $context['top_posters'][$i]['post_percent'] = round($poster['num_posts'] * 100 / $max_num_posts);
        $context['top_posters'][$i]['num_posts'] = comma_format($context['top_posters'][$i]['num_posts']);
    }
    // Board top 10.
    $boards_result = $smcFunc['db_query']('', '
		SELECT id_board, name, num_posts
		FROM {db_prefix}boards AS b
		WHERE {query_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
			AND b.id_board != {int:recycle_board}' : '') . '
			AND b.redirect = {string:blank_redirect}
		ORDER BY num_posts DESC
		LIMIT 10', array('recycle_board' => $modSettings['recycle_board'], 'blank_redirect' => ''));
    $context['top_boards'] = array();
    $max_num_posts = 1;
    while ($row_board = $smcFunc['db_fetch_assoc']($boards_result)) {
        $context['top_boards'][] = array('id' => $row_board['id_board'], 'name' => $row_board['name'], 'num_posts' => $row_board['num_posts'], 'href' => $scripturl . '?board=' . $row_board['id_board'] . '.0', 'link' => '<a href="' . $scripturl . '?board=' . $row_board['id_board'] . '.0">' . $row_board['name'] . '</a>');
        if ($max_num_posts < $row_board['num_posts']) {
            $max_num_posts = $row_board['num_posts'];
        }
    }
    $smcFunc['db_free_result']($boards_result);
    foreach ($context['top_boards'] as $i => $board) {
        $context['top_boards'][$i]['post_percent'] = round($board['num_posts'] * 100 / $max_num_posts);
        $context['top_boards'][$i]['num_posts'] = comma_format($context['top_boards'][$i]['num_posts']);
    }
    // Are you on a larger forum?  If so, let's try to limit the number of topics we search through.
    if ($modSettings['totalMessages'] > 100000) {
        $request = $smcFunc['db_query']('', '
			SELECT id_topic
			FROM {db_prefix}topics
			WHERE num_replies != {int:no_replies}' . ($modSettings['postmod_active'] ? '
				AND approved = {int:is_approved}' : '') . '
			ORDER BY num_replies DESC
			LIMIT 100', array('no_replies' => 0, 'is_approved' => 1));
        $topic_ids = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $topic_ids[] = $row['id_topic'];
        }
        $smcFunc['db_free_result']($request);
    } else {
        $topic_ids = array();
    }
    // Topic replies top 10.
    $topic_reply_result = $smcFunc['db_query']('', '
		SELECT m.subject, t.num_replies, t.id_board, t.id_topic, b.name
		FROM {db_prefix}topics AS t
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
			AND b.id_board != {int:recycle_board}' : '') . ')
		WHERE {query_see_board}' . (!empty($topic_ids) ? '
			AND t.id_topic IN ({array_int:topic_list})' : ($modSettings['postmod_active'] ? '
			AND t.approved = {int:is_approved}' : '')) . '
		ORDER BY t.num_replies DESC
		LIMIT 10', array('topic_list' => $topic_ids, 'recycle_board' => $modSettings['recycle_board'], 'is_approved' => 1));
    $context['top_topics_replies'] = array();
    $max_num_replies = 1;
    while ($row_topic_reply = $smcFunc['db_fetch_assoc']($topic_reply_result)) {
        censorText($row_topic_reply['subject']);
        $context['top_topics_replies'][] = array('id' => $row_topic_reply['id_topic'], 'board' => array('id' => $row_topic_reply['id_board'], 'name' => $row_topic_reply['name'], 'href' => $scripturl . '?board=' . $row_topic_reply['id_board'] . '.0', 'link' => '<a href="' . $scripturl . '?board=' . $row_topic_reply['id_board'] . '.0">' . $row_topic_reply['name'] . '</a>'), 'subject' => $row_topic_reply['subject'], 'num_replies' => $row_topic_reply['num_replies'], 'href' => $scripturl . '?topic=' . $row_topic_reply['id_topic'] . '.0', 'link' => '<a href="' . $scripturl . '?topic=' . $row_topic_reply['id_topic'] . '.0">' . $row_topic_reply['subject'] . '</a>');
        if ($max_num_replies < $row_topic_reply['num_replies']) {
            $max_num_replies = $row_topic_reply['num_replies'];
        }
    }
    $smcFunc['db_free_result']($topic_reply_result);
    foreach ($context['top_topics_replies'] as $i => $topic) {
        $context['top_topics_replies'][$i]['post_percent'] = round($topic['num_replies'] * 100 / $max_num_replies);
        $context['top_topics_replies'][$i]['num_replies'] = comma_format($context['top_topics_replies'][$i]['num_replies']);
    }
    // Large forums may need a bit more prodding...
    if ($modSettings['totalMessages'] > 100000) {
        $request = $smcFunc['db_query']('', '
			SELECT id_topic
			FROM {db_prefix}topics
			WHERE num_views != {int:no_views}
			ORDER BY num_views DESC
			LIMIT 100', array('no_views' => 0));
        $topic_ids = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $topic_ids[] = $row['id_topic'];
        }
        $smcFunc['db_free_result']($request);
    } else {
        $topic_ids = array();
    }
    // Topic views top 10.
    $topic_view_result = $smcFunc['db_query']('', '
		SELECT m.subject, t.num_views, t.id_board, t.id_topic, b.name
		FROM {db_prefix}topics AS t
			INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
			AND b.id_board != {int:recycle_board}' : '') . ')
		WHERE {query_see_board}' . (!empty($topic_ids) ? '
			AND t.id_topic IN ({array_int:topic_list})' : ($modSettings['postmod_active'] ? '
			AND t.approved = {int:is_approved}' : '')) . '
		ORDER BY t.num_views DESC
		LIMIT 10', array('topic_list' => $topic_ids, 'recycle_board' => $modSettings['recycle_board'], 'is_approved' => 1));
    $context['top_topics_views'] = array();
    $max_num_views = 1;
    while ($row_topic_views = $smcFunc['db_fetch_assoc']($topic_view_result)) {
        censorText($row_topic_views['subject']);
        $context['top_topics_views'][] = array('id' => $row_topic_views['id_topic'], 'board' => array('id' => $row_topic_views['id_board'], 'name' => $row_topic_views['name'], 'href' => $scripturl . '?board=' . $row_topic_views['id_board'] . '.0', 'link' => '<a href="' . $scripturl . '?board=' . $row_topic_views['id_board'] . '.0">' . $row_topic_views['name'] . '</a>'), 'subject' => $row_topic_views['subject'], 'num_views' => $row_topic_views['num_views'], 'href' => $scripturl . '?topic=' . $row_topic_views['id_topic'] . '.0', 'link' => '<a href="' . $scripturl . '?topic=' . $row_topic_views['id_topic'] . '.0">' . $row_topic_views['subject'] . '</a>');
        if ($max_num_views < $row_topic_views['num_views']) {
            $max_num_views = $row_topic_views['num_views'];
        }
    }
    $smcFunc['db_free_result']($topic_view_result);
    foreach ($context['top_topics_views'] as $i => $topic) {
        $context['top_topics_views'][$i]['post_percent'] = round($topic['num_views'] * 100 / $max_num_views);
        $context['top_topics_views'][$i]['num_views'] = comma_format($context['top_topics_views'][$i]['num_views']);
    }
    // Try to cache this when possible, because it's a little unavoidably slow.
    if (($members = cache_get_data('stats_top_starters', 360)) == null) {
        $request = $smcFunc['db_query']('', '
			SELECT id_member_started, COUNT(*) AS hits
			FROM {db_prefix}topics' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
			WHERE id_board != {int:recycle_board}' : '') . '
			GROUP BY id_member_started
			ORDER BY hits DESC
			LIMIT 20', array('recycle_board' => $modSettings['recycle_board']));
        $members = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $members[$row['id_member_started']] = $row['hits'];
        }
        $smcFunc['db_free_result']($request);
        cache_put_data('stats_top_starters', $members, 360);
    }
    if (empty($members)) {
        $members = array(0 => 0);
    }
    // Topic poster top 10.
    $members_result = $smcFunc['db_query']('top_topic_starters', '
		SELECT id_member, real_name
		FROM {db_prefix}members
		WHERE id_member IN ({array_int:member_list})
		ORDER BY FIND_IN_SET(id_member, {string:top_topic_posters})
		LIMIT 10', array('member_list' => array_keys($members), 'top_topic_posters' => implode(',', array_keys($members))));
    $context['top_starters'] = array();
    $max_num_topics = 1;
    while ($row_members = $smcFunc['db_fetch_assoc']($members_result)) {
        $context['top_starters'][] = array('name' => $row_members['real_name'], 'id' => $row_members['id_member'], 'num_topics' => $members[$row_members['id_member']], 'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'], 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>');
        if ($max_num_topics < $members[$row_members['id_member']]) {
            $max_num_topics = $members[$row_members['id_member']];
        }
        if (!empty($modSettings['MemberColorStats'])) {
            $context['MemberColor_ID_MEMBER'][$row_members['id_member']] = $row_members['id_member'];
        }
    }
    $smcFunc['db_free_result']($members_result);
    foreach ($context['top_starters'] as $i => $topic) {
        $context['top_starters'][$i]['post_percent'] = round($topic['num_topics'] * 100 / $max_num_topics);
        $context['top_starters'][$i]['num_topics'] = comma_format($context['top_starters'][$i]['num_topics']);
    }
    // Time online top 10.
    $temp = cache_get_data('stats_total_time_members', 600);
    $members_result = $smcFunc['db_query']('', '
		SELECT id_member, real_name, total_time_logged_in
		FROM {db_prefix}members' . (!empty($temp) ? '
		WHERE id_member IN ({array_int:member_list_cached})' : '') . '
		ORDER BY total_time_logged_in DESC
		LIMIT 20', array('member_list_cached' => $temp));
    $context['top_time_online'] = array();
    $temp2 = array();
    $max_time_online = 1;
    while ($row_members = $smcFunc['db_fetch_assoc']($members_result)) {
        $temp2[] = (int) $row_members['id_member'];
        if (count($context['top_time_online']) >= 10) {
            continue;
        }
        // Figure out the days, hours and minutes.
        $timeDays = floor($row_members['total_time_logged_in'] / 86400);
        $timeHours = floor($row_members['total_time_logged_in'] % 86400 / 3600);
        // Figure out which things to show... (days, hours, minutes, etc.)
        $timelogged = '';
        if ($timeDays > 0) {
            $timelogged .= $timeDays . $txt['totalTimeLogged5'];
        }
        if ($timeHours > 0) {
            $timelogged .= $timeHours . $txt['totalTimeLogged6'];
        }
        $timelogged .= floor($row_members['total_time_logged_in'] % 3600 / 60) . $txt['totalTimeLogged7'];
        $context['top_time_online'][] = array('id' => $row_members['id_member'], 'name' => $row_members['real_name'], 'time_online' => $timelogged, 'seconds_online' => $row_members['total_time_logged_in'], 'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'], 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>');
        if ($max_time_online < $row_members['total_time_logged_in']) {
            $max_time_online = $row_members['total_time_logged_in'];
        }
        if (!empty($modSettings['MemberColorStats'])) {
            $context['MemberColor_ID_MEMBER'][$row_members['id_member']] = $row_members['id_member'];
        }
    }
    $smcFunc['db_free_result']($members_result);
    foreach ($context['top_time_online'] as $i => $member) {
        $context['top_time_online'][$i]['time_percent'] = round($member['seconds_online'] * 100 / $max_time_online);
    }
    if (!empty($modSettings['latestMember']) && !empty($modSettings['MemberColorLatestMember'])) {
        $context['MemberColor_ID_MEMBER'][$modSettings['latestMember']] = $modSettings['latestMember'];
    }
    //Yeah baby give me some colors =).
    if (!empty($context['MemberColor_ID_MEMBER'])) {
        $colorDatas = load_onlineColors($context['MemberColor_ID_MEMBER']);
        $cmemcolid = null;
        if (!empty($modSettings['MemberColorStats'])) {
            // First the Top Posters =)
            foreach ($context['top_posters'] as $key => $value) {
                $cmemcolid = $context['top_posters'][$key]['id'];
                if (!empty($colorDatas[$cmemcolid]['colored_link'])) {
                    $context['top_posters'][$key]['link'] = $colorDatas[$cmemcolid]['colored_link'];
                }
            }
            // First the Top Starter =)
            foreach ($context['top_starters'] as $key => $value) {
                $cmemcolid = $context['top_starters'][$key]['id'];
                if (!empty($colorDatas[$cmemcolid]['colored_link'])) {
                    $context['top_starters'][$key]['link'] = $colorDatas[$cmemcolid]['colored_link'];
                }
            }
            // First the Top Onliner =)
            foreach ($context['top_time_online'] as $key => $value) {
                $cmemcolid = $context['top_time_online'][$key]['id'];
                if (!empty($colorDatas[$cmemcolid]['colored_link'])) {
                    $context['top_time_online'][$key]['link'] = $colorDatas[$cmemcolid]['colored_link'];
                }
            }
        }
        if (!empty($modSettings['latestMember']) && !empty($modSettings['MemberColorLatestMember'])) {
            if (!empty($colorDatas[$modSettings['latestMember']]['colored_link'])) {
                $context['latest_member']['link'] = $colorDatas[$modSettings['latestMember']]['colored_link'];
            }
        }
    }
    // Cache the ones we found for a bit, just so we don't have to look again.
    if ($temp !== $temp2) {
        cache_put_data('stats_total_time_members', $temp2, 480);
    }
    // Activity by month.
    $months_result = $smcFunc['db_query']('', '
		SELECT
			YEAR(date) AS stats_year, MONTH(date) AS stats_month, SUM(hits) AS hits, SUM(registers) AS registers, SUM(topics) AS topics, SUM(posts) AS posts, MAX(most_on) AS most_on, COUNT(*) AS num_days
		FROM {db_prefix}log_activity
		GROUP BY stats_year, stats_month', array());
    $context['yearly'] = array();
    while ($row_months = $smcFunc['db_fetch_assoc']($months_result)) {
        $ID_MONTH = $row_months['stats_year'] . sprintf('%02d', $row_months['stats_month']);
        $expanded = !empty($_SESSION['expanded_stats'][$row_months['stats_year']]) && in_array($row_months['stats_month'], $_SESSION['expanded_stats'][$row_months['stats_year']]);
        if (!isset($context['yearly'][$row_months['stats_year']])) {
            $context['yearly'][$row_months['stats_year']] = array('year' => $row_months['stats_year'], 'new_topics' => 0, 'new_posts' => 0, 'new_members' => 0, 'most_members_online' => 0, 'hits' => 0, 'num_months' => 0, 'months' => array(), 'expanded' => false, 'current_year' => $row_months['stats_year'] == date('Y'));
        }
        $context['yearly'][$row_months['stats_year']]['months'][(int) $row_months['stats_month']] = array('id' => $ID_MONTH, 'date' => array('month' => sprintf('%02d', $row_months['stats_month']), 'year' => $row_months['stats_year']), 'href' => $scripturl . '?action=stats;' . ($expanded ? 'collapse' : 'expand') . '=' . $ID_MONTH . '#m' . $ID_MONTH, 'link' => '<a href="' . $scripturl . '?action=stats;' . ($expanded ? 'collapse' : 'expand') . '=' . $ID_MONTH . '#m' . $ID_MONTH . '">' . $txt['months'][(int) $row_months['stats_month']] . ' ' . $row_months['stats_year'] . '</a>', 'month' => $txt['months'][(int) $row_months['stats_month']], 'year' => $row_months['stats_year'], 'new_topics' => comma_format($row_months['topics']), 'new_posts' => comma_format($row_months['posts']), 'new_members' => comma_format($row_months['registers']), 'most_members_online' => comma_format($row_months['most_on']), 'hits' => comma_format($row_months['hits']), 'num_days' => $row_months['num_days'], 'days' => array(), 'expanded' => $expanded);
        $context['yearly'][$row_months['stats_year']]['new_topics'] += $row_months['topics'];
        $context['yearly'][$row_months['stats_year']]['new_posts'] += $row_months['posts'];
        $context['yearly'][$row_months['stats_year']]['new_members'] += $row_months['registers'];
        $context['yearly'][$row_months['stats_year']]['hits'] += $row_months['hits'];
        $context['yearly'][$row_months['stats_year']]['num_months']++;
        $context['yearly'][$row_months['stats_year']]['expanded'] |= $expanded;
        $context['yearly'][$row_months['stats_year']]['most_members_online'] = max($context['yearly'][$row_months['stats_year']]['most_members_online'], $row_months['most_on']);
    }
    krsort($context['yearly']);
    foreach ($context['yearly'] as $year => $data) {
        // This gets rid of the filesort on the query ;).
        krsort($context['yearly'][$year]['months']);
        $context['yearly'][$year]['new_topics'] = comma_format($data['new_topics']);
        $context['yearly'][$year]['new_posts'] = comma_format($data['new_posts']);
        $context['yearly'][$year]['new_members'] = comma_format($data['new_members']);
        $context['yearly'][$year]['most_members_online'] = comma_format($data['most_members_online']);
        $context['yearly'][$year]['hits'] = comma_format($data['hits']);
    }
    if (empty($_SESSION['expanded_stats'])) {
        return;
    }
    $condition_text = array();
    $condition_params = array();
    foreach ($_SESSION['expanded_stats'] as $year => $months) {
        if (!empty($months)) {
            $condition_text[] = 'YEAR(date) = {int:year_' . $year . '} AND MONTH(date) IN ({array_int:months_' . $year . '})';
            $condition_params['year_' . $year] = $year;
            $condition_params['months_' . $year] = $months;
        }
    }
    // No daily stats to even look at?
    if (empty($condition_text)) {
        return;
    }
    getDailyStats(implode(' OR ', $condition_text), $condition_params);
}
Example #9
0
function list_getUserWarnings($start, $items_per_page, $sort, $memID)
{
    global $smcFunc, $scripturl;
    $request = $smcFunc['db_query']('', '
		SELECT IFNULL(mem.id_member, 0) AS id_member, IFNULL(mem.real_name, lc.member_name) AS member_name,
			lc.log_time, lc.body, lc.counter, lc.id_notice
		FROM {db_prefix}log_comments AS lc
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lc.id_member)
		WHERE lc.id_recipient = {int:selected_member}
			AND lc.comment_type = {string:warning}
		ORDER BY ' . $sort . '
		LIMIT ' . $start . ', ' . $items_per_page, array('selected_member' => $memID, 'warning' => 'warning'));
    $previous_warnings = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        if (!empty($row['id_member'])) {
            $context['MemberColor_ID_MEMBER'][$row['id_member']] = $row['id_member'];
        }
        $previous_warnings[] = array('issuer' => array('id' => $row['id_member'], 'link' => $row['id_member'] ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['member_name'] . '</a>' : $row['member_name']), 'time' => timeformat($row['log_time']), 'reason' => $row['body'], 'counter' => $row['counter'] > 0 ? '+' . $row['counter'] : $row['counter'], 'id_notice' => $row['id_notice']);
    }
    $smcFunc['db_free_result']($request);
    //Color the Issue List
    global $modSettings;
    if (!empty($modSettings['MemberColorIssuePList']) && !empty($context['MemberColor_ID_MEMBER']) && !empty($previous_warnings)) {
        $colorDatas = load_onlineColors($context['MemberColor_ID_MEMBER']);
        foreach ($previous_warnings as $key => $warning) {
            if (!empty($colorDatas[$warning['issuer']['id']]['colored_link'])) {
                $previous_warnings[$key]['issuer']['link'] = $colorDatas[$warning['issuer']['id']]['colored_link'];
            }
        }
    }
    return $previous_warnings;
}
Example #10
0
function UnreadTopics()
{
    global $board, $txt, $scripturl, $sourcedir;
    global $user_info, $context, $settings, $modSettings, $smcFunc, $options;
    // Guests can't have unread things, we don't know anything about them.
    is_not_guest();
    // Prefetching + lots of MySQL work = bad mojo.
    if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch') {
        ob_end_clean();
        header('HTTP/1.1 403 Forbidden');
        die;
    }
    $context['showing_all_topics'] = isset($_GET['all']);
    $context['start'] = (int) $_REQUEST['start'];
    $context['topics_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page']) && !WIRELESS ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'];
    if ($_REQUEST['action'] == 'unread') {
        $context['page_title'] = $context['showing_all_topics'] ? $txt['unread_topics_all'] : $txt['unread_topics_visit'];
    } else {
        $context['page_title'] = $txt['unread_replies'];
    }
    if ($context['showing_all_topics'] && !empty($context['load_average']) && !empty($modSettings['loadavg_allunread']) && $context['load_average'] >= $modSettings['loadavg_allunread']) {
        fatal_lang_error('loadavg_allunread_disabled', false);
    } elseif ($_REQUEST['action'] != 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unreadreplies']) && $context['load_average'] >= $modSettings['loadavg_unreadreplies']) {
        fatal_lang_error('loadavg_unreadreplies_disabled', false);
    } elseif (!$context['showing_all_topics'] && $_REQUEST['action'] == 'unread' && !empty($context['load_average']) && !empty($modSettings['loadavg_unread']) && $context['load_average'] >= $modSettings['loadavg_unread']) {
        fatal_lang_error('loadavg_unread_disabled', false);
    }
    // Parameters for the main query.
    $query_parameters = array();
    // Are we specifying any specific board?
    if (isset($_REQUEST['children']) && (!empty($board) || !empty($_REQUEST['boards']))) {
        $boards = array();
        if (!empty($_REQUEST['boards'])) {
            $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
            foreach ($_REQUEST['boards'] as $b) {
                $boards[] = (int) $b;
            }
        }
        if (!empty($board)) {
            $boards[] = (int) $board;
        }
        // The easist thing is to just get all the boards they can see, but since we've specified the top of tree we ignore some of them
        $request = $smcFunc['db_query']('', '
			SELECT b.id_board, b.id_parent
			FROM {db_prefix}boards AS b
			WHERE {query_wanna_see_board}
				AND b.child_level > {int:no_child}
				AND b.id_board NOT IN ({array_int:boards})
			ORDER BY child_level ASC
			', array('no_child' => 0, 'boards' => $boards));
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            if (in_array($row['id_parent'], $boards)) {
                $boards[] = $row['id_board'];
            }
        }
        $smcFunc['db_free_result']($request);
        if (empty($boards)) {
            fatal_lang_error('error_no_boards_selected');
        }
        $query_this_board = 'id_board IN ({array_int:boards})';
        $query_parameters['boards'] = $boards;
        $context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%d';
    } elseif (!empty($board)) {
        $query_this_board = 'id_board = {int:board}';
        $query_parameters['board'] = $board;
        $context['querystring_board_limits'] = ';board=' . $board . '.%1$d';
    } elseif (!empty($_REQUEST['boards'])) {
        $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
        foreach ($_REQUEST['boards'] as $i => $b) {
            $_REQUEST['boards'][$i] = (int) $b;
        }
        $request = $smcFunc['db_query']('', '
			SELECT b.id_board
			FROM {db_prefix}boards AS b
			WHERE {query_see_board}
				AND b.id_board IN ({array_int:board_list})', array('board_list' => $_REQUEST['boards']));
        $boards = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $boards[] = $row['id_board'];
        }
        $smcFunc['db_free_result']($request);
        if (empty($boards)) {
            fatal_lang_error('error_no_boards_selected');
        }
        $query_this_board = 'id_board IN ({array_int:boards})';
        $query_parameters['boards'] = $boards;
        $context['querystring_board_limits'] = ';boards=' . implode(',', $boards) . ';start=%1$d';
    } elseif (!empty($_REQUEST['c'])) {
        $_REQUEST['c'] = explode(',', $_REQUEST['c']);
        foreach ($_REQUEST['c'] as $i => $c) {
            $_REQUEST['c'][$i] = (int) $c;
        }
        $see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
        $request = $smcFunc['db_query']('', '
			SELECT b.id_board
			FROM {db_prefix}boards AS b
			WHERE ' . $user_info[$see_board] . '
				AND b.id_cat IN ({array_int:id_cat})', array('id_cat' => $_REQUEST['c']));
        $boards = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $boards[] = $row['id_board'];
        }
        $smcFunc['db_free_result']($request);
        if (empty($boards)) {
            fatal_lang_error('error_no_boards_selected');
        }
        $query_this_board = 'id_board IN ({array_int:boards})';
        $query_parameters['boards'] = $boards;
        $context['querystring_board_limits'] = ';c=' . implode(',', $_REQUEST['c']) . ';start=%1$d';
    } else {
        $see_board = isset($_REQUEST['action']) && $_REQUEST['action'] == 'unreadreplies' ? 'query_see_board' : 'query_wanna_see_board';
        // Don't bother to show deleted posts!
        $request = $smcFunc['db_query']('', '
			SELECT b.id_board
			FROM {db_prefix}boards AS b
			WHERE ' . $user_info[$see_board] . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
				AND b.id_board != {int:recycle_board}' : ''), array('recycle_board' => (int) $modSettings['recycle_board']));
        $boards = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $boards[] = $row['id_board'];
        }
        $smcFunc['db_free_result']($request);
        if (empty($boards)) {
            fatal_lang_error('error_no_boards_selected');
        }
        $query_this_board = 'id_board IN ({array_int:boards})';
        $query_parameters['boards'] = $boards;
        $context['querystring_board_limits'] = ';start=%1$d';
        $context['no_board_limits'] = true;
    }
    $sort_methods = array('subject' => 'ms.subject', 'starter' => 'IFNULL(mems.real_name, ms.poster_name)', 'replies' => 't.num_replies', 'views' => 't.num_views', 'first_post' => 't.id_topic', 'last_post' => 't.id_last_msg');
    // The default is the most logical: newest first.
    if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']])) {
        $context['sort_by'] = 'last_post';
        $_REQUEST['sort'] = 't.id_last_msg';
        $ascending = isset($_REQUEST['asc']);
        $context['querystring_sort_limits'] = $ascending ? ';asc' : '';
    } else {
        $context['sort_by'] = $_REQUEST['sort'];
        $_REQUEST['sort'] = $sort_methods[$_REQUEST['sort']];
        $ascending = !isset($_REQUEST['desc']);
        $context['querystring_sort_limits'] = ';sort=' . $context['sort_by'] . ($ascending ? '' : ';desc');
    }
    $context['sort_direction'] = $ascending ? 'up' : 'down';
    if (!empty($_REQUEST['c']) && is_array($_REQUEST['c']) && count($_REQUEST['c']) == 1) {
        $request = $smcFunc['db_query']('', '
			SELECT name
			FROM {db_prefix}categories
			WHERE id_cat = {int:id_cat}
			LIMIT 1', array('id_cat' => (int) $_REQUEST['c'][0]));
        list($name) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);
        $context['linktree'][] = array('url' => $scripturl . '#c' . (int) $_REQUEST['c'][0], 'name' => $name);
    }
    $context['linktree'][] = array('url' => $scripturl . '?action=' . $_REQUEST['action'] . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'], 'name' => $_REQUEST['action'] == 'unread' ? $txt['unread_topics_visit'] : $txt['unread_replies']);
    if ($context['showing_all_topics']) {
        $context['linktree'][] = array('url' => $scripturl . '?action=' . $_REQUEST['action'] . ';all' . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'], 'name' => $txt['unread_topics_all']);
    } else {
        $txt['unread_topics_visit_none'] = strtr($txt['unread_topics_visit_none'], array('?action=unread;all' => '?action=unread;all' . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits']));
    }
    if (WIRELESS) {
        $context['sub_template'] = WIRELESS_PROTOCOL . '_recent';
    } else {
        loadTemplate('Recent');
        $context['sub_template'] = $_REQUEST['action'] == 'unread' ? 'unread' : 'replies';
    }
    // Setup the default topic icons... for checking they exist and the like ;)
    $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'moved', 'recycled', 'wireless', 'clip');
    $context['icon_sources'] = array();
    foreach ($stable_icons as $icon) {
        $context['icon_sources'][$icon] = 'images_url';
    }
    $is_topics = $_REQUEST['action'] == 'unread';
    // This part is the same for each query.
    $select_clause = '
				ms.subject AS first_subject, ms.poster_time AS first_poster_time, ms.id_topic, t.id_board, b.name AS bname,
				t.num_replies, t.num_views, ms.id_member AS id_first_member, ml.id_member AS id_last_member,
				ml.poster_time AS last_poster_time, IFNULL(mems.real_name, ms.poster_name) AS first_poster_name,
				IFNULL(meml.real_name, ml.poster_name) AS last_poster_name, ml.subject AS last_subject,
				ml.icon AS last_icon, ms.icon AS first_icon, t.id_poll, t.is_sticky, t.locked, ml.modified_time AS last_modified_time,
				IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from, SUBSTRING(ml.body, 1, 385) AS last_body,
				SUBSTRING(ms.body, 1, 385) AS first_body, ml.smileys_enabled AS last_smileys, ms.smileys_enabled AS first_smileys, t.id_first_msg, t.id_last_msg';
    if ($context['showing_all_topics']) {
        if (!empty($board)) {
            $request = $smcFunc['db_query']('', '
				SELECT MIN(id_msg)
				FROM {db_prefix}log_mark_read
				WHERE id_member = {int:current_member}
					AND id_board = {int:current_board}', array('current_board' => $board, 'current_member' => $user_info['id']));
            list($earliest_msg) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);
        } else {
            $request = $smcFunc['db_query']('', '
				SELECT MIN(lmr.id_msg)
				FROM {db_prefix}boards AS b
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
				WHERE {query_see_board}', array('current_member' => $user_info['id']));
            list($earliest_msg) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);
        }
        // This is needed in case of topics marked unread.
        if (empty($earliest_msg)) {
            $earliest_msg = 0;
        } else {
            // Using caching, when possible, to ignore the below slow query.
            if (isset($_SESSION['cached_log_time']) && $_SESSION['cached_log_time'][0] + 45 > time()) {
                $earliest_msg2 = $_SESSION['cached_log_time'][1];
            } else {
                // This query is pretty slow, but it's needed to ensure nothing crucial is ignored.
                $request = $smcFunc['db_query']('', '
					SELECT MIN(id_msg)
					FROM {db_prefix}log_topics
					WHERE id_member = {int:current_member}', array('current_member' => $user_info['id']));
                list($earliest_msg2) = $smcFunc['db_fetch_row']($request);
                $smcFunc['db_free_result']($request);
                // In theory this could be zero, if the first ever post is unread, so fudge it ;)
                if ($earliest_msg2 == 0) {
                    $earliest_msg2 = -1;
                }
                $_SESSION['cached_log_time'] = array(time(), $earliest_msg2);
            }
            $earliest_msg = min($earliest_msg2, $earliest_msg);
        }
    }
    // !!! Add modified_time in for log_time check?
    if ($modSettings['totalMessages'] > 100000 && $context['showing_all_topics']) {
        $smcFunc['db_query']('', '
			DROP TABLE IF EXISTS {db_prefix}log_topics_unread', array());
        // Let's copy things out of the log_topics table, to reduce searching.
        $have_temp_table = $smcFunc['db_query']('', '
			CREATE TEMPORARY TABLE {db_prefix}log_topics_unread (
				PRIMARY KEY (id_topic)
			)
			SELECT lt.id_topic, lt.id_msg
			FROM {db_prefix}topics AS t
				INNER JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic)
			WHERE lt.id_member = {int:current_member}
				AND t.' . $query_this_board . (empty($earliest_msg) ? '' : '
				AND t.id_last_msg > {int:earliest_msg}') . ($modSettings['postmod_active'] ? '
				AND t.approved = {int:is_approved}' : ''), array_merge($query_parameters, array('current_member' => $user_info['id'], 'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0, 'is_approved' => 1, 'db_error_skip' => true))) !== false;
    } else {
        $have_temp_table = false;
    }
    if ($context['showing_all_topics'] && $have_temp_table) {
        $request = $smcFunc['db_query']('', '
			SELECT COUNT(*), MIN(t.id_last_msg)
			FROM {db_prefix}topics AS t
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
			WHERE t.' . $query_this_board . (!empty($earliest_msg) ? '
				AND t.id_last_msg > {int:earliest_msg}' : '') . '
				AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
				AND t.approved = {int:is_approved}' : ''), array_merge($query_parameters, array('current_member' => $user_info['id'], 'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0, 'is_approved' => 1)));
        list($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);
        // Make sure the starting place makes sense and construct the page index.
        $context['page_index'] = constructPageIndex($scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . $context['querystring_board_limits'] . $context['querystring_sort_limits'], $_REQUEST['start'], $num_topics, $context['topics_per_page'], true);
        $context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
        $context['links'] = array('first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'] : '', 'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] - $context['topics_per_page']) . $context['querystring_sort_limits'] : '', 'next' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] + $context['topics_per_page']) . $context['querystring_sort_limits'] : '', 'last' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], floor(($num_topics - 1) / $context['topics_per_page']) * $context['topics_per_page']) . $context['querystring_sort_limits'] : '', 'up' => $scripturl);
        $context['page_info'] = array('current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1, 'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1);
        if ($num_topics == 0) {
            $context['topics'] = array();
            if ($context['querystring_board_limits'] == ';start=%1$d') {
                $context['querystring_board_limits'] = '';
            } else {
                $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
            }
            return;
        } else {
            $min_message = (int) $min_message;
        }
        $request = $smcFunc['db_query']('substring', '
			SELECT ' . $select_clause . '
			FROM {db_prefix}messages AS ms
				INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
				LEFT JOIN {db_prefix}boards AS b ON (b.id_board = ms.id_board)
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
			WHERE b.' . $query_this_board . '
				AND t.id_last_msg >= {int:min_message}
				AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
				AND ms.approved = {int:is_approved}' : '') . '
			ORDER BY {raw:sort}
			LIMIT {int:offset}, {int:limit}', array_merge($query_parameters, array('current_member' => $user_info['id'], 'min_message' => $min_message, 'is_approved' => 1, 'sort' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'), 'offset' => $_REQUEST['start'], 'limit' => $context['topics_per_page'])));
    } elseif ($is_topics) {
        $request = $smcFunc['db_query']('', '
			SELECT COUNT(*), MIN(t.id_last_msg)
			FROM {db_prefix}topics AS t' . (!empty($have_temp_table) ? '
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
				LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})') . '
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
			WHERE t.' . $query_this_board . ($context['showing_all_topics'] && !empty($earliest_msg) ? '
				AND t.id_last_msg > {int:earliest_msg}' : (!$context['showing_all_topics'] && empty($_SESSION['first_login']) ? '
				AND t.id_last_msg > {int:id_msg_last_visit}' : '')) . '
				AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
				AND t.approved = {int:is_approved}' : ''), array_merge($query_parameters, array('current_member' => $user_info['id'], 'earliest_msg' => !empty($earliest_msg) ? $earliest_msg : 0, 'id_msg_last_visit' => $_SESSION['id_msg_last_visit'], 'is_approved' => 1)));
        list($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);
        // Make sure the starting place makes sense and construct the page index.
        $context['page_index'] = constructPageIndex($scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . $context['querystring_board_limits'] . $context['querystring_sort_limits'], $_REQUEST['start'], $num_topics, $context['topics_per_page'], true);
        $context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
        $context['links'] = array('first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'] : '', 'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] - $context['topics_per_page']) . $context['querystring_sort_limits'] : '', 'next' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] + $context['topics_per_page']) . $context['querystring_sort_limits'] : '', 'last' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], floor(($num_topics - 1) / $context['topics_per_page']) * $context['topics_per_page']) . $context['querystring_sort_limits'] : '', 'up' => $scripturl);
        $context['page_info'] = array('current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1, 'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1);
        if ($num_topics == 0) {
            $context['topics'] = array();
            if ($context['querystring_board_limits'] == ';start=%d') {
                $context['querystring_board_limits'] = '';
            } else {
                $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
            }
            return;
        } else {
            $min_message = (int) $min_message;
        }
        $request = $smcFunc['db_query']('substring', '
			SELECT ' . $select_clause . '
			FROM {db_prefix}messages AS ms
				INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ms.id_topic AND t.id_first_msg = ms.id_msg)
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
				LEFT JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' . (!empty($have_temp_table) ? '
				LEFT JOIN {db_prefix}log_topics_unread AS lt ON (lt.id_topic = t.id_topic)' : '
				LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})') . '
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
			WHERE t.' . $query_this_board . '
				AND t.id_last_msg >= {int:min_message}
				AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < ml.id_msg' . ($modSettings['postmod_active'] ? '
				AND ms.approved = {int:is_approved}' : '') . '
			ORDER BY {raw:order}
			LIMIT {int:offset}, {int:limit}', array_merge($query_parameters, array('current_member' => $user_info['id'], 'min_message' => $min_message, 'is_approved' => 1, 'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'), 'offset' => $_REQUEST['start'], 'limit' => $context['topics_per_page'])));
    } else {
        if ($modSettings['totalMessages'] > 100000) {
            $smcFunc['db_query']('', '
				DROP TABLE IF EXISTS {db_prefix}topics_posted_in', array());
            $smcFunc['db_query']('', '
				DROP TABLE IF EXISTS {db_prefix}log_topics_posted_in', array());
            $sortKey_joins = array('ms.subject' => '
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)', 'IFNULL(mems.real_name, ms.poster_name)' => '
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
					LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)');
            // The main benefit of this temporary table is not that it's faster; it's that it avoids locks later.
            $have_temp_table = $smcFunc['db_query']('', '
				CREATE TEMPORARY TABLE {db_prefix}topics_posted_in (
					id_topic mediumint(8) unsigned NOT NULL default {string:string_zero},
					id_board smallint(5) unsigned NOT NULL default {string:string_zero},
					id_last_msg int(10) unsigned NOT NULL default {string:string_zero},
					id_msg int(10) unsigned NOT NULL default {string:string_zero},
					PRIMARY KEY (id_topic)
				)
				SELECT t.id_topic, t.id_board, t.id_last_msg, IFNULL(lmr.id_msg, 0) AS id_msg' . (!in_array($_REQUEST['sort'], array('t.id_last_msg', 't.id_topic')) ? ', ' . $_REQUEST['sort'] . ' AS sort_key' : '') . '
				FROM {db_prefix}messages AS m
					INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})' . (isset($sortKey_joins[$_REQUEST['sort']]) ? $sortKey_joins[$_REQUEST['sort']] : '') . '
				WHERE m.id_member = {int:current_member}' . (!empty($board) ? '
					AND t.id_board = {int:current_board}' : '') . ($modSettings['postmod_active'] ? '
					AND t.approved = {int:is_approved}' : '') . '
				GROUP BY m.id_topic', array('current_board' => $board, 'current_member' => $user_info['id'], 'is_approved' => 1, 'string_zero' => '0', 'db_error_skip' => true)) !== false;
            // If that worked, create a sample of the log_topics table too.
            if ($have_temp_table) {
                $have_temp_table = $smcFunc['db_query']('', '
					CREATE TEMPORARY TABLE {db_prefix}log_topics_posted_in (
						PRIMARY KEY (id_topic)
					)
					SELECT lt.id_topic, lt.id_msg
					FROM {db_prefix}log_topics AS lt
						INNER JOIN {db_prefix}topics_posted_in AS pi ON (pi.id_topic = lt.id_topic)
					WHERE lt.id_member = {int:current_member}', array('current_member' => $user_info['id'], 'db_error_skip' => true)) !== false;
            }
        }
        if (!empty($have_temp_table)) {
            $request = $smcFunc['db_query']('', '
				SELECT COUNT(*)
				FROM {db_prefix}topics_posted_in AS pi
					LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = pi.id_topic)
				WHERE pi.' . $query_this_board . '
					AND IFNULL(lt.id_msg, pi.id_msg) < pi.id_last_msg', array_merge($query_parameters, array()));
            list($num_topics) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);
        } else {
            $request = $smcFunc['db_query']('unread_fetch_topic_count', '
				SELECT COUNT(DISTINCT t.id_topic), MIN(t.id_last_msg)
				FROM {db_prefix}topics AS t
					INNER JOIN {db_prefix}messages AS m ON (m.id_topic = t.id_topic)
					LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
				WHERE t.' . $query_this_board . '
					AND m.id_member = {int:current_member}
					AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg' . ($modSettings['postmod_active'] ? '
					AND t.approved = {int:is_approved}' : ''), array_merge($query_parameters, array('current_member' => $user_info['id'], 'is_approved' => 1)));
            list($num_topics, $min_message) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);
        }
        // Make sure the starting place makes sense and construct the page index.
        $context['page_index'] = constructPageIndex($scripturl . '?action=' . $_REQUEST['action'] . $context['querystring_board_limits'] . $context['querystring_sort_limits'], $_REQUEST['start'], $num_topics, $context['topics_per_page'], true);
        $context['current_page'] = (int) $_REQUEST['start'] / $context['topics_per_page'];
        $context['links'] = array('first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], 0) . $context['querystring_sort_limits'] : '', 'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] - $context['topics_per_page']) . $context['querystring_sort_limits'] : '', 'next' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], $_REQUEST['start'] + $context['topics_per_page']) . $context['querystring_sort_limits'] : '', 'last' => $_REQUEST['start'] + $context['topics_per_page'] < $num_topics ? $scripturl . '?action=' . $_REQUEST['action'] . ($context['showing_all_topics'] ? ';all' : '') . sprintf($context['querystring_board_limits'], floor(($num_topics - 1) / $context['topics_per_page']) * $context['topics_per_page']) . $context['querystring_sort_limits'] : '', 'up' => $scripturl);
        $context['page_info'] = array('current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1, 'num_pages' => floor(($num_topics - 1) / $context['topics_per_page']) + 1);
        if ($num_topics == 0) {
            $context['topics'] = array();
            if ($context['querystring_board_limits'] == ';start=%d') {
                $context['querystring_board_limits'] = '';
            } else {
                $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
            }
            return;
        }
        if (!empty($have_temp_table)) {
            $request = $smcFunc['db_query']('', '
				SELECT t.id_topic
				FROM {db_prefix}topics_posted_in AS t
					LEFT JOIN {db_prefix}log_topics_posted_in AS lt ON (lt.id_topic = t.id_topic)
				WHERE t.' . $query_this_board . '
					AND IFNULL(lt.id_msg, t.id_msg) < t.id_last_msg
				ORDER BY {raw:order}
				LIMIT {int:offset}, {int:limit}', array_merge($query_parameters, array('order' => (in_array($_REQUEST['sort'], array('t.id_last_msg', 't.id_topic')) ? $_REQUEST['sort'] : 't.sort_key') . ($ascending ? '' : ' DESC'), 'offset' => $_REQUEST['start'], 'limit' => $context['topics_per_page'])));
        } else {
            $request = $smcFunc['db_query']('unread_replies', '
				SELECT DISTINCT t.id_topic
				FROM {db_prefix}topics AS t
					INNER JOIN {db_prefix}messages AS m ON (m.id_topic = t.id_topic AND m.id_member = {int:current_member})' . (strpos($_REQUEST['sort'], 'ms.') === false ? '' : '
					INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)') . (strpos($_REQUEST['sort'], 'mems.') === false ? '' : '
					LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)') . '
					LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
					LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
				WHERE t.' . $query_this_board . '
					AND t.id_last_msg >= {int:min_message}
					AND IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) < t.id_last_msg
					AND t.approved = {int:is_approved}
				ORDER BY {raw:order}
				LIMIT {int:offset}, {int:limit}', array_merge($query_parameters, array('current_member' => $user_info['id'], 'min_message' => (int) $min_message, 'is_approved' => 1, 'order' => $_REQUEST['sort'] . ($ascending ? '' : ' DESC'), 'offset' => $_REQUEST['start'], 'limit' => $context['topics_per_page'])));
        }
        $topics = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $topics[] = $row['id_topic'];
        }
        $smcFunc['db_free_result']($request);
        // Sanity... where have you gone?
        if (empty($topics)) {
            $context['topics'] = array();
            if ($context['querystring_board_limits'] == ';start=%d') {
                $context['querystring_board_limits'] = '';
            } else {
                $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
            }
            return;
        }
        $request = $smcFunc['db_query']('substring', '
			SELECT ' . $select_clause . '
			FROM {db_prefix}topics AS t
				INNER JOIN {db_prefix}messages AS ms ON (ms.id_topic = t.id_topic AND ms.id_msg = t.id_first_msg)
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
				INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
				LEFT JOIN {db_prefix}members AS mems ON (mems.id_member = ms.id_member)
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
				LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})
			WHERE t.id_topic IN ({array_int:topic_list})
			ORDER BY ' . $_REQUEST['sort'] . ($ascending ? '' : ' DESC') . '
			LIMIT ' . count($topics), array('current_member' => $user_info['id'], 'topic_list' => $topics));
    }
    $context['topics'] = array();
    $topic_ids = array();
    $context['MemberColor_ID_MEMBER'] = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0') {
            continue;
        }
        $topic_ids[] = $row['id_topic'];
        if (!empty($settings['message_index_preview'])) {
            // Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
            $row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br />' => '&#10;')));
            if ($smcFunc['strlen']($row['first_body']) > 128) {
                $row['first_body'] = $smcFunc['substr']($row['first_body'], 0, 128) . '...';
            }
            $row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br />' => '&#10;')));
            if ($smcFunc['strlen']($row['last_body']) > 128) {
                $row['last_body'] = $smcFunc['substr']($row['last_body'], 0, 128) . '...';
            }
            // Censor the subject and message preview.
            censorText($row['first_subject']);
            censorText($row['first_body']);
            // Don't censor them twice!
            if ($row['id_first_msg'] == $row['id_last_msg']) {
                $row['last_subject'] = $row['first_subject'];
                $row['last_body'] = $row['first_body'];
            } else {
                censorText($row['last_subject']);
                censorText($row['last_body']);
            }
        } else {
            $row['first_body'] = '';
            $row['last_body'] = '';
            censorText($row['first_subject']);
            if ($row['id_first_msg'] == $row['id_last_msg']) {
                $row['last_subject'] = $row['first_subject'];
            } else {
                censorText($row['last_subject']);
            }
        }
        // Decide how many pages the topic should have.
        $topic_length = $row['num_replies'] + 1;
        $messages_per_page = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) && !WIRELESS ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
        if ($topic_length > $messages_per_page) {
            $tmppages = array();
            $tmpa = 1;
            for ($tmpb = 0; $tmpb < $topic_length; $tmpb += $messages_per_page) {
                $tmppages[] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.' . $tmpb . ';topicseen">' . $tmpa . '</a>';
                $tmpa++;
            }
            // Show links to all the pages?
            if (count($tmppages) <= 5) {
                $pages = '&#171; ' . implode(' ', $tmppages);
            } else {
                $pages = '&#171; ' . $tmppages[0] . ' ' . $tmppages[1] . ' ... ' . $tmppages[count($tmppages) - 2] . ' ' . $tmppages[count($tmppages) - 1];
            }
            if (!empty($modSettings['enableAllMessages']) && $topic_length < $modSettings['enableAllMessages']) {
                $pages .= ' &nbsp;<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;all">' . $txt['all'] . '</a>';
            }
            $pages .= ' &#187;';
        } else {
            $pages = '';
        }
        // We need to check the topic icons exist... you can never be too sure!
        if (empty($modSettings['messageIconChecks_disable'])) {
            // First icon first... as you'd expect.
            if (!isset($context['icon_sources'][$row['first_icon']])) {
                $context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.gif') ? 'images_url' : 'default_images_url';
            }
            // Last icon... last... duh.
            if (!isset($context['icon_sources'][$row['last_icon']])) {
                $context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.gif') ? 'images_url' : 'default_images_url';
            }
        }
        // And build the array.
        $context['topics'][$row['id_topic']] = array('id' => $row['id_topic'], 'first_post' => array('id' => $row['id_first_msg'], 'member' => array('name' => $row['first_poster_name'], 'id' => $row['id_first_member'], 'href' => $scripturl . '?action=profile;u=' . $row['id_first_member'], 'link' => !empty($row['first_id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['first_id_member'] . '" title="' . $txt['profile_of'] . ' ' . $row['first_poster_name'] . '">' . $row['first_poster_name'] . '</a>' : (!empty($modSettings['MemberColorGuests']) ? '<span style="color:' . $modSettings['MemberColorGuests'] . ';">' : '') . $row['first_poster_name'] . (!empty($modSettings['MemberColorGuests']) ? '</span>' : '')), 'time' => timeformat($row['first_poster_time']), 'timestamp' => forum_time(true, $row['first_poster_time']), 'subject' => $row['first_subject'], 'preview' => $row['first_body'], 'icon' => $row['first_icon'], 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.gif', 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen', 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;topicseen">' . $row['first_subject'] . '</a>'), 'last_post' => array('id' => $row['id_last_msg'], 'member' => array('name' => $row['last_poster_name'], 'id' => $row['id_last_member'], 'href' => $scripturl . '?action=profile;u=' . $row['id_last_member'], 'link' => !empty($row['id_last_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_last_member'] . '" title="' . $txt['profile_of'] . ' ' . $row['last_poster_name'] . '">' . $row['last_poster_name'] . '</a>' : (!empty($modSettings['MemberColorGuests']) ? '<span style="color:' . $modSettings['MemberColorGuests'] . ';">' : '') . $row['last_poster_name'] . (!empty($modSettings['MemberColorGuests']) ? '</span>' : '')), 'time' => timeformat($row['last_poster_time']), 'timestamp' => forum_time(true, $row['last_poster_time']), 'subject' => $row['last_subject'], 'preview' => $row['last_body'], 'icon' => $row['last_icon'], 'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.gif', 'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . ';topicseen#msg' . $row['id_last_msg'], 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . ';topicseen#msg' . $row['id_last_msg'] . '" rel="nofollow">' . $row['last_subject'] . '</a>'), 'new_from' => $row['new_from'], 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . ';topicseen#new', 'href' => $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['new_from']) . ';topicseen' . ($row['num_replies'] == 0 ? '' : 'new'), 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['new_from']) . ';topicseen#msg' . $row['new_from'] . '" rel="nofollow">' . $row['first_subject'] . '</a>', 'is_sticky' => !empty($modSettings['enableStickyTopics']) && !empty($row['is_sticky']), 'is_locked' => !empty($row['locked']), 'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0, 'is_hot' => $row['num_replies'] >= $modSettings['hotTopicPosts'], 'is_very_hot' => $row['num_replies'] >= $modSettings['hotTopicVeryPosts'], 'is_posted_in' => false, 'icon' => $row['first_icon'], 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.gif', 'subject' => $row['first_subject'], 'pages' => $pages, 'replies' => comma_format($row['num_replies']), 'views' => comma_format($row['num_views']), 'board' => array('id' => $row['id_board'], 'name' => $row['bname'], 'href' => $scripturl . '?board=' . $row['id_board'] . '.0', 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>'));
        //The Thread Owners id for the MemberColor.
        if (!empty($modSettings['MemberColorRecentOwner']) && !empty($row['id_first_member'])) {
            $context['MemberColor_ID_MEMBER'][$row['id_first_member']] = $row['id_first_member'];
        }
        //The Last Posters id for the MemberColor.
        if (!empty($modSettings['MemberColorRecentLastPost']) && !empty($row['id_last_member'])) {
            $context['MemberColor_ID_MEMBER'][$row['id_last_member']] = $row['id_last_member'];
        }
        determineTopicClass($context['topics'][$row['id_topic']]);
    }
    $smcFunc['db_free_result']($request);
    // Know set the colors for the Recent posts...
    if (!empty($context['MemberColor_ID_MEMBER'])) {
        $colorDatas = load_onlineColors($context['MemberColor_ID_MEMBER']);
        $cmemcolid = null;
        //So Let's Color The Recent Posts ;)
        if (is_array($context['topics']) && !empty($context['topics'])) {
            foreach ($context['topics'] as $postkey => $postid_memcolor) {
                if (!empty($modSettings['MemberColorRecentLastPost'])) {
                    $cmemcolid = $postid_memcolor['last_post']['member']['id'];
                    if (!empty($colorDatas[$cmemcolid]['colored_link'])) {
                        $context['topics'][$postkey]['last_post']['member']['link'] = $colorDatas[$cmemcolid]['colored_link'];
                    }
                }
                if (!empty($modSettings['MemberColorRecentOwner'])) {
                    $cmemcolid = $postid_memcolor['first_post']['member']['id'];
                    if (!empty($colorDatas[$cmemcolid]['colored_link'])) {
                        $context['topics'][$postkey]['first_post']['member']['link'] = $colorDatas[$cmemcolid]['colored_link'];
                    }
                }
            }
        }
    }
    if ($is_topics && !empty($modSettings['enableParticipation']) && !empty($topic_ids)) {
        $result = $smcFunc['db_query']('', '
			SELECT id_topic
			FROM {db_prefix}messages
			WHERE id_topic IN ({array_int:topic_list})
				AND id_member = {int:current_member}', array('current_member' => $user_info['id'], 'topic_list' => $topic_ids));
        while ($row = $smcFunc['db_fetch_assoc']($result)) {
            if (empty($context['topics'][$row['id_topic']]['is_posted_in'])) {
                $context['topics'][$row['id_topic']]['is_posted_in'] = true;
                $context['topics'][$row['id_topic']]['class'] = 'my_' . $context['topics'][$row['id_topic']]['class'];
            }
        }
        $smcFunc['db_free_result']($result);
    }
    $context['querystring_board_limits'] = sprintf($context['querystring_board_limits'], $_REQUEST['start']);
    $context['topics_to_mark'] = implode('-', $topic_ids);
}
Example #11
0
function MessageIndex()
{
    global $txt, $scripturl, $board, $modSettings, $context;
    global $options, $settings, $board_info, $user_info, $smcFunc, $sourcedir;
    // If this is a redirection board head off.
    if ($board_info['redirect']) {
        $smcFunc['db_query']('', '
			UPDATE {db_prefix}boards
			SET num_posts = num_posts + 1
			WHERE id_board = {int:current_board}', array('current_board' => $board));
        redirectexit($board_info['redirect']);
    }
    if (WIRELESS) {
        $context['sub_template'] = WIRELESS_PROTOCOL . '_messageindex';
    } else {
        loadTemplate('MessageIndex');
    }
    //Set some standard Variables for the MemberColorLink
    $context['MemberColor_ID_MEMBER'] = array();
    $context['name'] = $board_info['name'];
    $context['description'] = $board_info['description'];
    // How many topics do we have in total?
    $board_info['total_topics'] = allowedTo('approve_posts') ? $board_info['num_topics'] + $board_info['unapproved_topics'] : $board_info['num_topics'] + $board_info['unapproved_user_topics'];
    // View all the topics, or just a few?
    $context['topics_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['topics_per_page']) && !WIRELESS ? $options['topics_per_page'] : $modSettings['defaultMaxTopics'];
    $context['messages_per_page'] = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) && !WIRELESS ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
    $maxindex = isset($_REQUEST['all']) && !empty($modSettings['enableAllMessages']) ? $board_info['total_topics'] : $context['topics_per_page'];
    // Right, let's only index normal stuff!
    if (count($_GET) > 1) {
        foreach ($_GET as $k => $v) {
            if (!in_array($k, array('board', 'start', session_name()))) {
                $context['robot_no_index'] = true;
            }
        }
    }
    if (!empty($_REQUEST['start']) && (!is_numeric($_REQUEST['start']) || $_REQUEST['start'] % $context['messages_per_page'] != 0)) {
        $context['robot_no_index'] = true;
    }
    // If we can view unapproved messages and there are some build up a list.
    if (allowedTo('approve_posts') && ($board_info['unapproved_topics'] || $board_info['unapproved_posts'])) {
        $untopics = $board_info['unapproved_topics'] ? '<a href="' . $scripturl . '?action=moderate;area=postmod;sa=topics;brd=' . $board . '">' . $board_info['unapproved_topics'] . '</a>' : 0;
        $unposts = $board_info['unapproved_posts'] ? '<a href="' . $scripturl . '?action=moderate;area=postmod;sa=posts;brd=' . $board . '">' . ($board_info['unapproved_posts'] - $board_info['unapproved_topics']) . '</a>' : 0;
        $context['unapproved_posts_message'] = sprintf($txt['there_are_unapproved_topics'], $untopics, $unposts, $scripturl . '?action=moderate;area=postmod;sa=' . ($board_info['unapproved_topics'] ? 'topics' : 'posts') . ';brd=' . $board);
    }
    // Make sure the starting place makes sense and construct the page index.
    if (isset($_REQUEST['sort'])) {
        $context['page_index'] = constructPageIndex($scripturl . '?board=' . $board . '.%1$d;sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $board_info['total_topics'], $maxindex, true);
    } else {
        $context['page_index'] = constructPageIndex($scripturl . '?board=' . $board . '.%1$d', $_REQUEST['start'], $board_info['total_topics'], $maxindex, true);
    }
    $context['start'] =& $_REQUEST['start'];
    // Set a canonical URL for this page.
    $context['canonical_url'] = $scripturl . '?board=' . $board . '.' . $context['start'];
    $context['links'] = array('first' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?board=' . $board . '.0' : '', 'prev' => $_REQUEST['start'] >= $context['topics_per_page'] ? $scripturl . '?board=' . $board . '.' . ($_REQUEST['start'] - $context['topics_per_page']) : '', 'next' => $_REQUEST['start'] + $context['topics_per_page'] < $board_info['total_topics'] ? $scripturl . '?board=' . $board . '.' . ($_REQUEST['start'] + $context['topics_per_page']) : '', 'last' => $_REQUEST['start'] + $context['topics_per_page'] < $board_info['total_topics'] ? $scripturl . '?board=' . $board . '.' . floor(($board_info['total_topics'] - 1) / $context['topics_per_page']) * $context['topics_per_page'] : '', 'up' => $board_info['parent'] == 0 ? $scripturl . '?' : $scripturl . '?board=' . $board_info['parent'] . '.0');
    $context['page_info'] = array('current_page' => $_REQUEST['start'] / $context['topics_per_page'] + 1, 'num_pages' => floor(($board_info['total_topics'] - 1) / $context['topics_per_page']) + 1);
    if (isset($_REQUEST['all']) && !empty($modSettings['enableAllMessages']) && $maxindex > $modSettings['enableAllMessages']) {
        $maxindex = $modSettings['enableAllMessages'];
        $_REQUEST['start'] = 0;
    }
    // Build a list of the board's moderators.
    $context['moderators'] =& $board_info['moderators'];
    $context['link_moderators'] = array();
    if (!empty($board_info['moderators'])) {
        //I hate it in this way... but what should i do ;)
        if (!empty($modSettings['MemberColorModeratorLinks'])) {
            foreach ($board_info['moderators'] as $mod) {
                $c[$mod['id']] = $mod['id'];
            }
            $colorDatas = load_onlineColors($c);
            foreach ($board_info['moderators'] as $mod) {
                $context['link_moderators'][] = str_replace($txt['profile_of'], $txt['board_moderator'], $colorDatas[$mod['id']]['colored_link']);
            }
        } else {
            foreach ($board_info['moderators'] as $mod) {
                $context['link_moderators'][] = '<a href="' . $scripturl . '?action=profile;u=' . $mod['id'] . '" title="' . $txt['board_moderator'] . '">' . $mod['name'] . '</a>';
            }
        }
        $context['linktree'][count($context['linktree']) - 1]['extra_after'] = ' (' . (count($context['link_moderators']) == 1 ? $txt['moderator'] : $txt['moderators']) . ': ' . implode(', ', $context['link_moderators']) . ')';
        $context['linktree'][count($context['linktree']) - 1]['extra_after'] = ' (' . (count($context['link_moderators']) == 1 ? $txt['moderator'] : $txt['moderators']) . ': ' . implode(', ', $context['link_moderators']) . ')';
    }
    // Mark current and parent boards as seen.
    if (!$user_info['is_guest']) {
        // We can't know they read it if we allow prefetches.
        if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch') {
            ob_end_clean();
            header('HTTP/1.1 403 Prefetch Forbidden');
            die;
        }
        $smcFunc['db_insert']('replace', '{db_prefix}log_boards', array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'), array($modSettings['maxMsgID'], $user_info['id'], $board), array('id_member', 'id_board'));
        if (!empty($board_info['parent_boards'])) {
            $smcFunc['db_query']('', '
				UPDATE {db_prefix}log_boards
				SET id_msg = {int:id_msg}
				WHERE id_member = {int:current_member}
					AND id_board IN ({array_int:board_list})', array('current_member' => $user_info['id'], 'board_list' => array_keys($board_info['parent_boards']), 'id_msg' => $modSettings['maxMsgID']));
            // We've seen all these boards now!
            foreach ($board_info['parent_boards'] as $k => $dummy) {
                if (isset($_SESSION['topicseen_cache'][$k])) {
                    unset($_SESSION['topicseen_cache'][$k]);
                }
            }
        }
        if (isset($_SESSION['topicseen_cache'][$board])) {
            unset($_SESSION['topicseen_cache'][$board]);
        }
        $request = $smcFunc['db_query']('', '
			SELECT sent
			FROM {db_prefix}log_notify
			WHERE id_board = {int:current_board}
				AND id_member = {int:current_member}
			LIMIT 1', array('current_board' => $board, 'current_member' => $user_info['id']));
        $context['is_marked_notify'] = $smcFunc['db_num_rows']($request) != 0;
        if ($context['is_marked_notify']) {
            list($sent) = $smcFunc['db_fetch_row']($request);
            if (!empty($sent)) {
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}log_notify
					SET sent = {int:is_sent}
					WHERE id_board = {int:current_board}
						AND id_member = {int:current_member}', array('current_board' => $board, 'current_member' => $user_info['id'], 'is_sent' => 0));
            }
        }
        $smcFunc['db_free_result']($request);
    } else {
        $context['is_marked_notify'] = false;
    }
    // 'Print' the header and board info.
    $context['page_title'] = strip_tags($board_info['name']);
    // Set the variables up for the template.
    $context['can_mark_notify'] = allowedTo('mark_notify') && !$user_info['is_guest'];
    $context['can_post_new'] = allowedTo('post_new') || $modSettings['postmod_active'] && allowedTo('post_unapproved_topics');
    $context['can_post_poll'] = $modSettings['pollMode'] == '1' && allowedTo('poll_post') && $context['can_post_new'];
    $context['can_moderate_forum'] = allowedTo('moderate_forum');
    $context['can_approve_posts'] = allowedTo('approve_posts');
    require_once $sourcedir . '/Subs-BoardIndex.php';
    $boardIndexOptions = array('include_categories' => false, 'base_level' => $board_info['child_level'] + 1, 'parent_id' => $board_info['id'], 'set_latest_post' => false, 'countChildPosts' => !empty($modSettings['countChildPosts']));
    $context['boards'] = getBoardIndex($boardIndexOptions);
    // Nosey, nosey - who's viewing this topic?
    if (!empty($settings['display_who_viewing'])) {
        $context['view_members'] = array();
        $context['view_members_list'] = array();
        $context['view_num_hidden'] = 0;
        $request = $smcFunc['db_query']('', '
			SELECT
				lo.id_member, lo.log_time, mem.real_name, mem.member_name, mem.show_online,
				mg.online_color, mg.id_group, mg.group_name
			FROM {db_prefix}log_online AS lo
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lo.id_member)
				LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_member_group} THEN mem.id_post_group ELSE mem.id_group END)
			WHERE INSTR(lo.url, {string:in_url_string}) > 0 OR lo.session = {string:session}', array('reg_member_group' => 0, 'in_url_string' => 's:5:"board";i:' . $board . ';', 'session' => $user_info['is_guest'] ? 'ip' . $user_info['ip'] : session_id()));
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            if (empty($row['id_member'])) {
                continue;
            }
            if (!empty($row['online_color']) && !empty($modSettings['MemberColorWhoIsOnline'])) {
                $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '" style="color: ' . $row['online_color'] . ';">' . $row['real_name'] . '</a>';
            } else {
                $link = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>';
            }
            $is_buddy = in_array($row['id_member'], $user_info['buddies']);
            if ($is_buddy) {
                $link = '<strong>' . $link . '</strong>';
            }
            if (!empty($row['show_online']) || allowedTo('moderate_forum')) {
                $context['view_members_list'][$row['log_time'] . $row['member_name']] = empty($row['show_online']) ? '<em>' . $link . '</em>' : $link;
            }
            $context['view_members'][$row['log_time'] . $row['member_name']] = array('id' => $row['id_member'], 'username' => $row['member_name'], 'name' => $row['real_name'], 'group' => $row['id_group'], 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], 'link' => $link, 'is_buddy' => $is_buddy, 'hidden' => empty($row['show_online']));
            if (empty($row['show_online'])) {
                $context['view_num_hidden']++;
            }
        }
        $context['view_num_guests'] = $smcFunc['db_num_rows']($request) - count($context['view_members']);
        $smcFunc['db_free_result']($request);
        // Put them in "last clicked" order.
        krsort($context['view_members_list']);
        krsort($context['view_members']);
    }
    // Default sort methods.
    $sort_methods = array('subject' => 'mf.subject', 'starter' => 'IFNULL(memf.real_name, mf.poster_name)', 'last_poster' => 'IFNULL(meml.real_name, ml.poster_name)', 'replies' => 't.num_replies', 'views' => 't.num_views', 'first_post' => 't.id_topic', 'last_post' => 't.id_last_msg');
    // They didn't pick one, default to by last post descending.
    if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']])) {
        $context['sort_by'] = 'last_post';
        $_REQUEST['sort'] = 'id_last_msg';
        $ascending = isset($_REQUEST['asc']);
    } else {
        $context['sort_by'] = $_REQUEST['sort'];
        $_REQUEST['sort'] = $sort_methods[$_REQUEST['sort']];
        $ascending = !isset($_REQUEST['desc']);
    }
    $context['sort_direction'] = $ascending ? 'up' : 'down';
    // Calculate the fastest way to get the topics.
    $start = $_REQUEST['start'];
    if ($start > ($board_info['total_topics'] - 1) / 2) {
        $ascending = !$ascending;
        $fake_ascending = true;
        $maxindex = $board_info['total_topics'] < $start + $maxindex + 1 ? $board_info['total_topics'] - $start : $maxindex;
        $start = $board_info['total_topics'] < $start + $maxindex + 1 ? 0 : $board_info['total_topics'] - $start - $maxindex;
    } else {
        $fake_ascending = false;
    }
    // Setup the default topic icons...
    $stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'moved', 'recycled', 'wireless', 'clip');
    $context['icon_sources'] = array();
    foreach ($stable_icons as $icon) {
        $context['icon_sources'][$icon] = 'images_url';
    }
    $topic_ids = array();
    $context['topics'] = array();
    // Sequential pages are often not optimized, so we add an additional query.
    $pre_query = $start > 0;
    if ($pre_query && $maxindex > 0) {
        $request = $smcFunc['db_query']('', '
			SELECT t.id_topic
			FROM {db_prefix}topics AS t' . ($context['sort_by'] === 'last_poster' ? '
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)' : (in_array($context['sort_by'], array('starter', 'subject')) ? '
				INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)' : '')) . ($context['sort_by'] === 'starter' ? '
				LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)' : '') . ($context['sort_by'] === 'last_poster' ? '
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)' : '') . '
			WHERE t.id_board = {int:current_board}' . (!$modSettings['postmod_active'] || $context['can_approve_posts'] ? '' : '
				AND (t.approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR t.id_member_started = {int:current_member}') . ')') . '
			ORDER BY ' . (!empty($modSettings['enableStickyTopics']) ? 'is_sticky' . ($fake_ascending ? '' : ' DESC') . ', ' : '') . $_REQUEST['sort'] . ($ascending ? '' : ' DESC') . '
			LIMIT {int:start}, {int:maxindex}', array('current_board' => $board, 'current_member' => $user_info['id'], 'is_approved' => 1, 'id_member_guest' => 0, 'start' => $start, 'maxindex' => $maxindex));
        $topic_ids = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $topic_ids[] = $row['id_topic'];
        }
    }
    // Grab the appropriate topic information...
    if (!$pre_query || !empty($topic_ids)) {
        // For search engine effectiveness we'll link guests differently.
        $context['pageindex_multiplier'] = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) && !WIRELESS ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
        $result = $smcFunc['db_query']('substring', '
			SELECT
				t.id_topic, t.num_replies, t.locked, t.num_views, t.is_sticky, t.id_poll, t.id_previous_board,
				' . ($user_info['is_guest'] ? '0' : 'IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1') . ' AS new_from,
				t.id_last_msg, t.approved, t.unapproved_posts, ml.poster_time AS last_poster_time,
				ml.id_msg_modified, ml.subject AS last_subject, ml.icon AS last_icon,
				ml.poster_name AS last_member_name, ml.id_member AS last_id_member,
				IFNULL(meml.real_name, ml.poster_name) AS last_display_name, t.id_first_msg,
				mf.poster_time AS first_poster_time, mf.subject AS first_subject, mf.icon AS first_icon,
				mf.poster_name AS first_member_name, mf.id_member AS first_id_member,
				IFNULL(memf.real_name, mf.poster_name) AS first_display_name, SUBSTRING(ml.body, 1, 385) AS last_body,
				SUBSTRING(mf.body, 1, 385) AS first_body, ml.smileys_enabled AS last_smileys, mf.smileys_enabled AS first_smileys
			FROM {db_prefix}topics AS t
				INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
				INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
				LEFT JOIN {db_prefix}members AS meml ON (meml.id_member = ml.id_member)
				LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)' . ($user_info['is_guest'] ? '' : '
				LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
				LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = {int:current_board} AND lmr.id_member = {int:current_member})') . '
			WHERE ' . ($pre_query ? 't.id_topic IN ({array_int:topic_list})' : 't.id_board = {int:current_board}') . (!$modSettings['postmod_active'] || $context['can_approve_posts'] ? '' : '
				AND (t.approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR t.id_member_started = {int:current_member}') . ')') . '
			ORDER BY ' . ($pre_query ? 'FIND_IN_SET(t.id_topic, {string:find_set_topics})' : (!empty($modSettings['enableStickyTopics']) ? 'is_sticky' . ($fake_ascending ? '' : ' DESC') . ', ' : '') . $_REQUEST['sort'] . ($ascending ? '' : ' DESC')) . '
			LIMIT ' . ($pre_query ? '' : '{int:start}, ') . '{int:maxindex}', array('current_board' => $board, 'current_member' => $user_info['id'], 'topic_list' => $topic_ids, 'is_approved' => 1, 'find_set_topics' => implode(',', $topic_ids), 'start' => $start, 'maxindex' => $maxindex));
        // Begin 'printing' the message index for current board.
        while ($row = $smcFunc['db_fetch_assoc']($result)) {
            if ($row['id_poll'] > 0 && $modSettings['pollMode'] == '0') {
                continue;
            }
            if (!$pre_query) {
                $topic_ids[] = $row['id_topic'];
            }
            //I need to collect some IDs for the MemberColor :).
            if (!empty($modSettings['MemberColorThreadOwner']) && !empty($row['first_id_member'])) {
                $context['MemberColor_ID_MEMBER'][$row['first_id_member']] = $row['first_id_member'];
            }
            //Oh i need the Last Post ID, too ;)
            if (!empty($modSettings['MemberColorThreadLastPost']) && !empty($row['last_id_member'])) {
                $context['MemberColor_ID_MEMBER'][$row['last_id_member']] = $row['last_id_member'];
            }
            if (!empty($settings['message_index_preview'])) {
                // Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
                $row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br />' => '&#10;')));
                if ($smcFunc['strlen']($row['first_body']) > 128) {
                    $row['first_body'] = $smcFunc['substr']($row['first_body'], 0, 128) . '...';
                }
                $row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br />' => '&#10;')));
                if ($smcFunc['strlen']($row['last_body']) > 128) {
                    $row['last_body'] = $smcFunc['substr']($row['last_body'], 0, 128) . '...';
                }
                // Censor the subject and message preview.
                censorText($row['first_subject']);
                censorText($row['first_body']);
                // Don't censor them twice!
                if ($row['id_first_msg'] == $row['id_last_msg']) {
                    $row['last_subject'] = $row['first_subject'];
                    $row['last_body'] = $row['first_body'];
                } else {
                    censorText($row['last_subject']);
                    censorText($row['last_body']);
                }
            } else {
                $row['first_body'] = '';
                $row['last_body'] = '';
                censorText($row['first_subject']);
                if ($row['id_first_msg'] == $row['id_last_msg']) {
                    $row['last_subject'] = $row['first_subject'];
                } else {
                    censorText($row['last_subject']);
                }
            }
            // Decide how many pages the topic should have.
            if ($row['num_replies'] + 1 > $context['messages_per_page']) {
                $pages = '&#171; ';
                // We can't pass start by reference.
                $start = -1;
                $pages .= constructPageIndex($scripturl . '?topic=' . $row['id_topic'] . '.%1$d', $start, $row['num_replies'] + 1, $context['messages_per_page'], true);
                // If we can use all, show all.
                if (!empty($modSettings['enableAllMessages']) && $row['num_replies'] + 1 < $modSettings['enableAllMessages']) {
                    $pages .= ' &nbsp;<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0;all">' . $txt['all'] . '</a>';
                }
                $pages .= ' &#187;';
            } else {
                $pages = '';
            }
            // We need to check the topic icons exist...
            if (empty($modSettings['messageIconChecks_disable'])) {
                if (!isset($context['icon_sources'][$row['first_icon']])) {
                    $context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.gif') ? 'images_url' : 'default_images_url';
                }
                if (!isset($context['icon_sources'][$row['last_icon']])) {
                    $context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.gif') ? 'images_url' : 'default_images_url';
                }
            } else {
                if (!isset($context['icon_sources'][$row['first_icon']])) {
                    $context['icon_sources'][$row['first_icon']] = 'images_url';
                }
                if (!isset($context['icon_sources'][$row['last_icon']])) {
                    $context['icon_sources'][$row['last_icon']] = 'images_url';
                }
            }
            // 'Print' the topic info.
            $context['topics'][$row['id_topic']] = array('id' => $row['id_topic'], 'first_post' => array('id' => $row['id_first_msg'], 'member' => array('username' => $row['first_member_name'], 'name' => $row['first_display_name'], 'id' => $row['first_id_member'], 'href' => !empty($row['first_id_member']) ? $scripturl . '?action=profile;u=' . $row['first_id_member'] : '', 'link' => !empty($row['first_id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['first_id_member'] . '" title="' . $txt['profile_of'] . ' ' . $row['first_display_name'] . '">' . $row['first_display_name'] . '</a>' : (!empty($modSettings['MemberColorGuests']) ? '<span style="color:' . $modSettings['MemberColorGuests'] . ';">' : '') . $row['first_display_name'] . (!empty($modSettings['MemberColorGuests']) ? '</span>' : '')), 'time' => timeformat($row['first_poster_time']), 'timestamp' => forum_time(true, $row['first_poster_time']), 'subject' => $row['first_subject'], 'preview' => $row['first_body'], 'icon' => $row['first_icon'], 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.gif', 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0', 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['first_subject'] . '</a>'), 'last_post' => array('id' => $row['id_last_msg'], 'member' => array('username' => $row['last_member_name'], 'name' => $row['last_display_name'], 'id' => $row['last_id_member'], 'href' => !empty($row['last_id_member']) ? $scripturl . '?action=profile;u=' . $row['last_id_member'] : '', 'link' => !empty($row['last_id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['last_id_member'] . '">' . $row['last_display_name'] . '</a>' : (!empty($modSettings['MemberColorGuests']) ? '<span style="color:' . $modSettings['MemberColorGuests'] . ';">' : '') . $row['last_display_name'] . (!empty($modSettings['MemberColorGuests']) ? '</span>' : '')), 'time' => timeformat($row['last_poster_time']), 'timestamp' => forum_time(true, $row['last_poster_time']), 'subject' => $row['last_subject'], 'preview' => $row['last_body'], 'icon' => $row['last_icon'], 'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.gif', 'href' => $scripturl . '?topic=' . $row['id_topic'] . ($user_info['is_guest'] ? '.' . (!empty($options['view_newest_first']) ? 0 : (int) ($row['num_replies'] / $context['pageindex_multiplier']) * $context['pageindex_multiplier']) . '#msg' . $row['id_last_msg'] : ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . '#new'), 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . ($user_info['is_guest'] ? '.' . (!empty($options['view_newest_first']) ? 0 : (int) ($row['num_replies'] / $context['pageindex_multiplier']) * $context['pageindex_multiplier']) . '#msg' . $row['id_last_msg'] : ($row['num_replies'] == 0 ? '.0' : '.msg' . $row['id_last_msg']) . '#new') . '" ' . ($row['num_replies'] == 0 ? '' : 'rel="nofollow"') . '>' . $row['last_subject'] . '</a>'), 'is_sticky' => !empty($modSettings['enableStickyTopics']) && !empty($row['is_sticky']), 'is_locked' => !empty($row['locked']), 'is_poll' => $modSettings['pollMode'] == '1' && $row['id_poll'] > 0, 'is_hot' => $row['num_replies'] >= $modSettings['hotTopicPosts'], 'is_very_hot' => $row['num_replies'] >= $modSettings['hotTopicVeryPosts'], 'is_posted_in' => false, 'icon' => $row['first_icon'], 'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.gif', 'subject' => $row['first_subject'], 'new' => $row['new_from'] <= $row['id_msg_modified'], 'new_from' => $row['new_from'], 'newtime' => $row['new_from'], 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new', 'pages' => $pages, 'replies' => comma_format($row['num_replies']), 'views' => comma_format($row['num_views']), 'approved' => $row['approved'], 'unapproved_posts' => $row['unapproved_posts']);
            determineTopicClass($context['topics'][$row['id_topic']]);
        }
        $smcFunc['db_free_result']($result);
        //Now let's replace the orginal link to the Color Link ;)
        if (!empty($context['MemberColor_ID_MEMBER'])) {
            $colorDatas = load_onlineColors($context['MemberColor_ID_MEMBER']);
            //You are not know what you do ;P Colors allready loaded :D
            if (!empty($modSettings['MemberColorModeratorLinks']) && !empty($context['boards'])) {
                //Okay now... do a heavy serach for moderators... only jokeing... but you know... it look so ugly ;)
                $onlineColor = load_mod_color(true);
                foreach ($context['boards'] as $ID_BOARD => $boardid_memcolor) {
                    //Reset it :D
                    $context['boards'][$ID_BOARD]['link_moderators'] = array();
                    global $color_profile;
                    foreach ($context['boards'][$ID_BOARD]['moderators'] as $moderators) {
                        $cmemcolid = $moderators['id'];
                        //Replace "Profil of" with "Moderator" ;D
                        $link = str_replace($txt['profile_of'], $txt['board_moderator'], $colorDatas[$cmemcolid]['colored_link']);
                        if (empty($colorDatas[$cmemcolid]['online_color']) && !empty($onlineColor)) {
                            $link = '<a href="' . $scripturl . '?action=profile;u=' . $color_profile[$cmemcolid]['id_member'] . '" title="' . $txt['board_moderator'] . ' ' . $color_profile[$cmemcolid]['real_name'] . '"' . (!empty($modSettings['MemberColorLinkOldSpanStyle']) ? '><span style="color:' . $onlineColor . ';">' : ' style="color:' . $onlineColor . ';">') . $color_profile[$cmemcolid]['real_name'] . (!empty($modSettings['MemberColorLinkOldSpanStyle']) ? '</span>' : '') . '</a>';
                        }
                        //Standard Color ;)
                        $context['boards'][$ID_BOARD]['moderators'][$cmemcolid]['link'] = $link;
                        //Creat the new list...
                        $context['boards'][$ID_BOARD]['link_moderators'][] = $link;
                    }
                }
            }
            //Need the Childs some Colors XD
            if (!empty($modSettings['MemberColorThreadChild']) && !empty($context['boards'])) {
                foreach ($context['boards'] as $ID_BOARD => $boardid_memcolor) {
                    if (empty($context['boards'][$ID_BOARD]['last_post']['member']['id'])) {
                        continue;
                    }
                    //My Childs need a Member Color ;)
                    $cmemcolid = $context['boards'][$ID_BOARD]['last_post']['member']['id'];
                    if (!empty($colorDatas[$cmemcolid]['colored_link'])) {
                        $context['boards'][$ID_BOARD]['last_post']['member']['link'] = $colorDatas[$cmemcolid]['colored_link'];
                    }
                }
            }
            //The Owner or the Last Post of the thread
            if (!empty($modSettings['MemberColorThreadOwner']) || !empty($modSettings['MemberColorThreadLastPost'])) {
                foreach ($topic_ids as $topicid_memcolor) {
                    if (empty($context['topics'][$topicid_memcolor])) {
                        continue;
                    }
                    //First the Thread Owner Link
                    if (!empty($modSettings['MemberColorThreadOwner'])) {
                        $cmemcolid = $context['topics'][$topicid_memcolor]['first_post']['member']['id'];
                        if (!empty($colorDatas[$cmemcolid]['colored_link'])) {
                            $context['topics'][$topicid_memcolor]['first_post']['member']['link'] = $colorDatas[$cmemcolid]['colored_link'];
                        }
                    }
                    //Or the Last Post Link ;)
                    if (!empty($modSettings['MemberColorThreadLastPost'])) {
                        $cmemcolid = $context['topics'][$topicid_memcolor]['last_post']['member']['id'];
                        if (!empty($colorDatas[$cmemcolid]['colored_link'])) {
                            $context['topics'][$topicid_memcolor]['last_post']['member']['link'] = $colorDatas[$cmemcolid]['colored_link'];
                        }
                    }
                }
            }
        }
        // Fix the sequence of topics if they were retrieved in the wrong order. (for speed reasons...)
        if ($fake_ascending) {
            $context['topics'] = array_reverse($context['topics'], true);
        }
        if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest'] && !empty($topic_ids)) {
            $result = $smcFunc['db_query']('', '
				SELECT id_topic
				FROM {db_prefix}messages
				WHERE id_topic IN ({array_int:topic_list})
					AND id_member = {int:current_member}
				GROUP BY id_topic
				LIMIT ' . count($topic_ids), array('current_member' => $user_info['id'], 'topic_list' => $topic_ids));
            while ($row = $smcFunc['db_fetch_assoc']($result)) {
                $context['topics'][$row['id_topic']]['is_posted_in'] = true;
                $context['topics'][$row['id_topic']]['class'] = 'my_' . $context['topics'][$row['id_topic']]['class'];
            }
            $smcFunc['db_free_result']($result);
        }
    }
    $context['jump_to'] = array('label' => addslashes(un_htmlspecialchars($txt['jump_to'])), 'board_name' => htmlspecialchars(strtr(strip_tags($board_info['name']), array('&amp;' => '&'))), 'child_level' => $board_info['child_level']);
    // Is Quick Moderation active/needed?
    if (!empty($options['display_quick_mod']) && !empty($context['topics'])) {
        $context['can_lock'] = allowedTo('lock_any');
        $context['can_sticky'] = allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']);
        $context['can_move'] = allowedTo('move_any');
        $context['can_remove'] = allowedTo('remove_any');
        $context['can_merge'] = allowedTo('merge_any');
        // Ignore approving own topics as it's unlikely to come up...
        $context['can_approve'] = allowedTo('approve_posts');
        // Can we restore topics?
        $context['can_restore'] = allowedTo('move_any') && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $board;
        // Set permissions for all the topics.
        foreach ($context['topics'] as $t => $topic) {
            $started = $topic['first_post']['member']['id'] == $user_info['id'];
            $context['topics'][$t]['quick_mod'] = array('lock' => allowedTo('lock_any') || $started && allowedTo('lock_own'), 'sticky' => allowedTo('make_sticky') && !empty($modSettings['enableStickyTopics']), 'move' => allowedTo('move_any') || $started && allowedTo('move_own'), 'modify' => allowedTo('modify_any') || $started && allowedTo('modify_own'), 'remove' => allowedTo('remove_any') || $started && allowedTo('remove_own'), 'approve' => $context['can_approve'] && $topic['unapproved_posts']);
            $context['can_lock'] |= $started && allowedTo('lock_own');
            $context['can_move'] |= $started && allowedTo('move_own');
            $context['can_remove'] |= $started && allowedTo('remove_own');
        }
        // Find the boards/cateogories they can move their topic to.
        if ($options['display_quick_mod'] == 1 && $context['can_move'] && !empty($context['topics'])) {
            require_once $sourcedir . '/Subs-MessageIndex.php';
            $boardListOptions = array('excluded_boards' => array($board), 'not_redirection' => true, 'use_permissions' => true, 'selected_board' => empty($_SESSION['move_to_topic']) ? null : $_SESSION['move_to_topic']);
            $context['move_to_boards'] = getBoardList($boardListOptions);
            // Make the boards safe for display.
            foreach ($context['move_to_boards'] as $id_cat => $cat) {
                $context['move_to_boards'][$id_cat]['name'] = strip_tags($cat['name']);
                foreach ($cat['boards'] as $id_board => $board) {
                    $context['move_to_boards'][$id_cat]['boards'][$id_board]['name'] = strip_tags($board['name']);
                }
            }
            // With no other boards to see, it's useless to move.
            if (empty($context['move_to_boards'])) {
                $context['can_move'] = false;
            }
        }
        // Can we use quick moderation checkboxes?
        if ($options['display_quick_mod'] == 1) {
            $context['can_quick_mod'] = $context['user']['is_logged'] || $context['can_approve'] || $context['can_remove'] || $context['can_lock'] || $context['can_sticky'] || $context['can_move'] || $context['can_merge'] || $context['can_restore'];
        } else {
            $context['can_quick_mod'] = $context['can_remove'] || $context['can_lock'] || $context['can_sticky'] || $context['can_move'];
        }
    }
    // If there are children, but no topics and no ability to post topics...
    $context['no_topic_listing'] = !empty($context['boards']) && empty($context['topics']) && !$context['can_post_new'];
}
Example #12
0
function list_getWarnings($start, $items_per_page, $sort)
{
    global $smcFunc, $txt, $scripturl, $modSettings, $user_info;
    $request = $smcFunc['db_query']('', '
		SELECT IFNULL(mem.id_member, 0) AS id_member, IFNULL(mem.real_name, lc.member_name) AS member_name_col,
			IFNULL(mem2.id_member, 0) AS id_recipient, IFNULL(mem2.real_name, lc.recipient_name) AS recipient_name,
			lc.log_time, lc.body, lc.id_notice, lc.counter
		FROM {db_prefix}log_comments AS lc
			LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lc.id_member)
			LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = lc.id_recipient)
		WHERE lc.comment_type = {string:warning}
		ORDER BY ' . $sort . '
		LIMIT ' . $start . ', ' . $items_per_page, array('warning' => 'warning'));
    $warnings = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        if (!empty($row['id_member'])) {
            $context['MemberColor_ID_MEMBER'][$row['id_member']] = $row['id_member'];
        }
        if (!empty($row['id_recipient'])) {
            $context['MemberColor_ID_MEMBER'][$row['id_recipient']] = $row['id_recipient'];
        }
        $warnings[] = array('issuer_link' => $row['id_member'] ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['member_name_col'] . '</a>' : $row['member_name_col'], 'recipient_link' => $row['id_recipient'] ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_recipient'] . '">' . $row['recipient_name'] . '</a>' : $row['recipient_name'], 'time' => timeformat($row['log_time']), 'reason' => $row['body'], 'counter' => $row['counter'] > 0 ? '+' . $row['counter'] : $row['counter'], 'id_notice' => $row['id_notice']);
    }
    $smcFunc['db_free_result']($request);
    //Warning Colors... second time... is one time not enouph?
    if (!empty($modSettings['MemberColorModCenter']) && !empty($context['MemberColor_ID_MEMBER']) && !empty($warnings)) {
        $colorDatas = load_onlineColors($context['MemberColor_ID_MEMBER']);
        foreach ($warnings as $wkey => $warning) {
            if (!empty($warning['id_issuer']) && !empty($colorDatas[$warning['id_issuer']]['colored_link'])) {
                $warnings[$wkey]['issuer_link'] = $colorDatas[$warning['id_issuer']]['colored_link'];
            }
            if (!empty($warning['id_recipient']) && !empty($colorDatas[$warning['id_recipient']]['colored_link'])) {
                $warnings[$wkey]['recipient_link'] = $colorDatas[$warning['id_recipient']]['colored_link'];
            }
        }
    }
    return $warnings;
}
Example #13
0
function prepareSearchContext($reset = false)
{
    global $txt, $modSettings, $scripturl, $user_info, $sourcedir;
    global $memberContext, $context, $settings, $options, $messages_request;
    global $boards_can, $participants, $smcFunc;
    // Remember which message this is.  (ie. reply #83)
    static $counter = null;
    if ($counter == null || $reset) {
        $counter = $_REQUEST['start'] + 1;
    }
    // If the query returned false, bail.
    if ($messages_request == false) {
        return false;
    }
    // Start from the beginning...
    if ($reset) {
        return @$smcFunc['db_data_seek']($messages_request, 0);
    }
    // Attempt to get the next message.
    $message = $smcFunc['db_fetch_assoc']($messages_request);
    if (!$message) {
        return false;
    }
    // Can't have an empty subject can we?
    $message['subject'] = $message['subject'] != '' ? $message['subject'] : $txt['no_subject'];
    $message['first_subject'] = $message['first_subject'] != '' ? $message['first_subject'] : $txt['no_subject'];
    $message['last_subject'] = $message['last_subject'] != '' ? $message['last_subject'] : $txt['no_subject'];
    // If it couldn't load, or the user was a guest.... someday may be done with a guest table.
    if (!loadMemberContext($message['id_member'])) {
        // Notice this information isn't used anywhere else.... *cough guest table cough*.
        $memberContext[$message['id_member']]['name'] = $message['poster_name'];
        $memberContext[$message['id_member']]['id'] = 0;
        $memberContext[$message['id_member']]['group'] = $txt['guest_title'];
        $memberContext[$message['id_member']]['link'] = (!empty($modSettings['MemberColorGuests']) ? '<span style="color:' . $modSettings['MemberColorGuests'] . ';">' : '') . $message['poster_name'] . (!empty($modSettings['MemberColorGuests']) ? '</span>' : '');
        $memberContext[$message['id_member']]['email'] = $message['poster_email'];
    }
    $memberContext[$message['id_member']]['ip'] = $message['poster_ip'];
    // Do the censor thang...
    censorText($message['body']);
    censorText($message['subject']);
    censorText($message['first_subject']);
    censorText($message['last_subject']);
    // Shorten this message if necessary.
    if ($context['compact']) {
        // Set the number of characters before and after the searched keyword.
        $charLimit = 50;
        $message['body'] = strtr($message['body'], array("\n" => ' ', '<br />' => "\n"));
        $message['body'] = parse_bbc($message['body'], $message['smileys_enabled'], $message['id_msg']);
        $message['body'] = strip_tags(strtr($message['body'], array('</div>' => '<br />', '</li>' => '<br />')), '<br>');
        if (strlen($message['body']) > $charLimit) {
            if (empty($context['key_words'])) {
                $message['body'] = $smcFunc['strlen']($message['body']) > $charLimit ? $smcFunc['substr']($message['body'], 0, $charLimit) . '<strong>...</strong>' : $message['body'];
            } else {
                $matchString = '';
                $force_partial_word = false;
                foreach ($context['key_words'] as $keyword) {
                    $keyword = preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~e', '$GLOBALS[\'smcFunc\'][\'entity_fix\'](\'\\1\')', strtr($keyword, array('\\\'' => '\'', '&' => '&amp;')));
                    if (preg_match('~[\'\\.,/@%&;:(){}\\[\\]_\\-+\\\\]$~', $keyword) != 0 || preg_match('~^[\'\\.,/@%&;:(){}\\[\\]_\\-+\\\\]~', $keyword) != 0) {
                        $force_partial_word = true;
                    }
                    $matchString .= strtr(preg_quote($keyword, '/'), array('\\*' => '.+?')) . '|';
                }
                $matchString = substr($matchString, 0, -1);
                $message['body'] = un_htmlspecialchars(strtr($message['body'], array('&nbsp;' => ' ', '<br />' => "\n", '&#91;' => '[', '&#93;' => ']', '&#58;' => ':', '&#64;' => '@')));
                if (empty($modSettings['search_method']) || $force_partial_word) {
                    preg_match_all('/([^\\s\\W]{' . $charLimit . '}[\\s\\W]|[\\s\\W].{0,' . $charLimit . '}?|^)(' . $matchString . ')(.{0,' . $charLimit . '}[\\s\\W]|[^\\s\\W]{' . $charLimit . '})/is' . ($context['utf8'] ? 'u' : ''), $message['body'], $matches);
                } else {
                    preg_match_all('/([^\\s\\W]{' . $charLimit . '}[\\s\\W]|[\\s\\W].{0,' . $charLimit . '}?[\\s\\W]|^)(' . $matchString . ')([\\s\\W].{0,' . $charLimit . '}[\\s\\W]|[\\s\\W][^\\s\\W]{' . $charLimit . '})/is' . ($context['utf8'] ? 'u' : ''), $message['body'], $matches);
                }
                $message['body'] = '';
                foreach ($matches[0] as $index => $match) {
                    $match = strtr(htmlspecialchars($match, ENT_QUOTES), array("\n" => '&nbsp;'));
                    $message['body'] .= '<strong>......</strong>&nbsp;' . $match . '&nbsp;<strong>......</strong>';
                }
            }
            // Re-fix the international characters.
            $message['body'] = preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~e', '$GLOBALS[\'smcFunc\'][\'entity_fix\'](\'\\1\')', $message['body']);
        }
    } else {
        // Run BBC interpreter on the message.
        $message['body'] = parse_bbc($message['body'], $message['smileys_enabled'], $message['id_msg']);
    }
    // Make sure we don't end up with a practically empty message body.
    $message['body'] = preg_replace('~^(?:&nbsp;)+$~', '', $message['body']);
    // Sadly, we need to check the icon ain't broke.
    if (empty($modSettings['messageIconChecks_disable'])) {
        if (!isset($context['icon_sources'][$message['first_icon']])) {
            $context['icon_sources'][$message['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $message['first_icon'] . '.gif') ? 'images_url' : 'default_images_url';
        }
        if (!isset($context['icon_sources'][$message['last_icon']])) {
            $context['icon_sources'][$message['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $message['last_icon'] . '.gif') ? 'images_url' : 'default_images_url';
        }
        if (!isset($context['icon_sources'][$message['icon']])) {
            $context['icon_sources'][$message['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $message['icon'] . '.gif') ? 'images_url' : 'default_images_url';
        }
    } else {
        if (!isset($context['icon_sources'][$message['first_icon']])) {
            $context['icon_sources'][$message['first_icon']] = 'images_url';
        }
        if (!isset($context['icon_sources'][$message['last_icon']])) {
            $context['icon_sources'][$message['last_icon']] = 'images_url';
        }
        if (!isset($context['icon_sources'][$message['icon']])) {
            $context['icon_sources'][$message['icon']] = 'images_url';
        }
    }
    //Generate the Started By Search Link... This is not fine, because i need to load each sepperate...
    //Ah and add correct Guest links ;D
    $first_post_link = !empty($message['first_member_id']) ? '<a href="' . $scripturl . '?action=profile;u=' . $message['first_member_id'] . '" title="' . $txt['profile_of'] . ' ' . $message['first_member_name'] . '">' . $message['first_member_name'] . '</a>' : (!empty($modSettings['MemberColorGuests']) ? '<span style="color:' . $modSettings['MemberColorGuests'] . ';">' : '') . $message['first_member_name'] . (!empty($modSettings['MemberColorGuests']) ? '</span>' : '');
    $last_post_link = !empty($message['last_member_id']) ? '<a href="' . $scripturl . '?action=profile;u=' . $message['last_member_id'] . '" title="' . $txt['profile_of'] . ' ' . $message['last_member_name'] . '">' . $message['last_member_name'] . '</a>' : (!empty($modSettings['MemberColorGuests']) ? '<span style="color:' . $modSettings['MemberColorGuests'] . ';">' : '') . $message['last_member_name'] . (!empty($modSettings['MemberColorGuests']) ? '</span>' : '');
    if (!empty($modSettings['MemberColorAllSearch'])) {
        $load = array();
        if (!empty($message['first_member_id'])) {
            $load[] = $message['first_member_id'];
        }
        if (!empty($message['first_member_id'])) {
            $load[] = $message['last_member_id'];
        }
        if (!empty($load)) {
            $colorDatas = load_onlineColors($load);
            if (!empty($colorDatas[$message['first_member_id']]['colored_link'])) {
                $first_post_link = $colorDatas[$message['first_member_id']]['colored_link'];
            }
            if (!empty($colorDatas[$message['last_member_id']]['colored_link'])) {
                $last_post_link = $colorDatas[$message['last_member_id']]['colored_link'];
            }
        }
    }
    $output = array_merge($context['topics'][$message['id_msg']], array('is_sticky' => !empty($modSettings['enableStickyTopics']) && !empty($message['is_sticky']), 'is_locked' => !empty($message['locked']), 'is_poll' => $modSettings['pollMode'] == '1' && $message['id_poll'] > 0, 'is_hot' => $message['num_replies'] >= $modSettings['hotTopicPosts'], 'is_very_hot' => $message['num_replies'] >= $modSettings['hotTopicVeryPosts'], 'posted_in' => !empty($participants[$message['id_topic']]), 'views' => $message['num_views'], 'replies' => $message['num_replies'], 'can_reply' => in_array($message['id_board'], $boards_can['post_reply_any']) || in_array(0, $boards_can['post_reply_any']), 'can_mark_notify' => in_array($message['id_board'], $boards_can['mark_any_notify']) || in_array(0, $boards_can['mark_any_notify']) && !$context['user']['is_guest'], 'first_post' => array('id' => $message['first_msg'], 'time' => timeformat($message['first_poster_time']), 'timestamp' => forum_time(true, $message['first_poster_time']), 'subject' => $message['first_subject'], 'href' => $scripturl . '?topic=' . $message['id_topic'] . '.0', 'link' => '<a href="' . $scripturl . '?topic=' . $message['id_topic'] . '.0">' . $message['first_subject'] . '</a>', 'icon' => $message['first_icon'], 'icon_url' => $settings[$context['icon_sources'][$message['first_icon']]] . '/post/' . $message['first_icon'] . '.gif', 'member' => array('id' => $message['first_member_id'], 'name' => $message['first_member_name'], 'href' => !empty($message['first_member_id']) ? $scripturl . '?action=profile;u=' . $message['first_member_id'] : '', 'link' => $first_post_link)), 'last_post' => array('id' => $message['last_msg'], 'time' => timeformat($message['last_poster_time']), 'timestamp' => forum_time(true, $message['last_poster_time']), 'subject' => $message['last_subject'], 'href' => $scripturl . '?topic=' . $message['id_topic'] . ($message['num_replies'] == 0 ? '.0' : '.msg' . $message['last_msg']) . '#msg' . $message['last_msg'], 'link' => '<a href="' . $scripturl . '?topic=' . $message['id_topic'] . ($message['num_replies'] == 0 ? '.0' : '.msg' . $message['last_msg']) . '#msg' . $message['last_msg'] . '">' . $message['last_subject'] . '</a>', 'icon' => $message['last_icon'], 'icon_url' => $settings[$context['icon_sources'][$message['last_icon']]] . '/post/' . $message['last_icon'] . '.gif', 'member' => array('id' => $message['last_member_id'], 'name' => $message['last_member_name'], 'href' => !empty($message['last_member_id']) ? $scripturl . '?action=profile;u=' . $message['last_member_id'] : '', 'link' => $last_post_link)), 'board' => array('id' => $message['id_board'], 'name' => $message['board_name'], 'href' => $scripturl . '?board=' . $message['id_board'] . '.0', 'link' => '<a href="' . $scripturl . '?board=' . $message['id_board'] . '.0">' . $message['board_name'] . '</a>'), 'category' => array('id' => $message['id_cat'], 'name' => $message['cat_name'], 'href' => $scripturl . '#c' . $message['id_cat'], 'link' => '<a href="' . $scripturl . '#c' . $message['id_cat'] . '">' . $message['cat_name'] . '</a>')));
    determineTopicClass($output);
    if ($output['posted_in']) {
        $output['class'] = 'my_' . $output['class'];
    }
    $body_highlighted = $message['body'];
    $subject_highlighted = $message['subject'];
    if (!empty($options['display_quick_mod'])) {
        $started = $output['first_post']['member']['id'] == $user_info['id'];
        $output['quick_mod'] = array('lock' => in_array(0, $boards_can['lock_any']) || in_array($output['board']['id'], $boards_can['lock_any']) || $started && (in_array(0, $boards_can['lock_own']) || in_array($output['board']['id'], $boards_can['lock_own'])), 'sticky' => (in_array(0, $boards_can['make_sticky']) || in_array($output['board']['id'], $boards_can['make_sticky'])) && !empty($modSettings['enableStickyTopics']), 'move' => in_array(0, $boards_can['move_any']) || in_array($output['board']['id'], $boards_can['move_any']) || $started && (in_array(0, $boards_can['move_own']) || in_array($output['board']['id'], $boards_can['move_own'])), 'remove' => in_array(0, $boards_can['remove_any']) || in_array($output['board']['id'], $boards_can['remove_any']) || $started && (in_array(0, $boards_can['remove_own']) || in_array($output['board']['id'], $boards_can['remove_own'])));
        $context['can_lock'] |= $output['quick_mod']['lock'];
        $context['can_sticky'] |= $output['quick_mod']['sticky'];
        $context['can_move'] |= $output['quick_mod']['move'];
        $context['can_remove'] |= $output['quick_mod']['remove'];
        $context['can_merge'] |= in_array($output['board']['id'], $boards_can['merge_any']);
        // If we've found a message we can move, and we don't already have it, load the destinations.
        if ($options['display_quick_mod'] == 1 && !isset($context['move_to_boards']) && $context['can_move']) {
            require_once $sourcedir . '/Subs-MessageIndex.php';
            $boardListOptions = array('use_permissions' => true, 'not_redirection' => true, 'selected_board' => empty($_SESSION['move_to_topic']) ? null : $_SESSION['move_to_topic']);
            $context['move_to_boards'] = getBoardList($boardListOptions);
        }
    }
    foreach ($context['key_words'] as $query) {
        // Fix the international characters in the keyword too.
        $query = strtr($smcFunc['htmlspecialchars']($query), array('\\\'' => '\''));
        $body_highlighted = preg_replace('/((<[^>]*)|' . preg_quote(strtr($query, array('\'' => '&#039;')), '/') . ')/ie' . ($context['utf8'] ? 'u' : ''), "'\$2' == '\$1' ? stripslashes('\$1') : '<strong class=\"highlight\">\$1</strong>'", $body_highlighted);
        $subject_highlighted = preg_replace('/(' . preg_quote($query, '/') . ')/i' . ($context['utf8'] ? 'u' : ''), '<strong class="highlight">$1</strong>', $subject_highlighted);
    }
    $output['matches'][] = array('id' => $message['id_msg'], 'attachment' => loadAttachmentContext($message['id_msg']), 'alternate' => $counter % 2, 'member' => &$memberContext[$message['id_member']], 'icon' => $message['icon'], 'icon_url' => $settings[$context['icon_sources'][$message['icon']]] . '/post/' . $message['icon'] . '.gif', 'subject' => $message['subject'], 'subject_highlighted' => $subject_highlighted, 'time' => timeformat($message['poster_time']), 'timestamp' => forum_time(true, $message['poster_time']), 'counter' => $counter, 'modified' => array('time' => timeformat($message['modified_time']), 'timestamp' => forum_time(true, $message['modified_time']), 'name' => $message['modified_name']), 'body' => $message['body'], 'body_highlighted' => $body_highlighted, 'start' => 'msg' . $message['id_msg']);
    $counter++;
    return $output;
}
Example #14
0
function getBirthdayRange($low_date, $high_date)
{
    global $scripturl, $modSettings, $smcFunc;
    // We need to search for any birthday in this range, and whatever year that birthday is on.
    $year_low = (int) substr($low_date, 0, 4);
    $year_high = (int) substr($high_date, 0, 4);
    // Collect all of the birthdays for this month.  I know, it's a painful query.
    $result = $smcFunc['db_query']('birthday_array', '
		SELECT id_member, real_name, YEAR(birthdate) AS birth_year, birthdate
		FROM {db_prefix}members
		WHERE YEAR(birthdate) != {string:year_one}
			AND MONTH(birthdate) != {int:no_month}
			AND DAYOFMONTH(birthdate) != {int:no_day}
			AND YEAR(birthdate) <= {int:max_year}
			AND (
				DATE_FORMAT(birthdate, {string:year_low}) BETWEEN {date:low_date} AND {date:high_date}' . ($year_low == $year_high ? '' : '
				OR DATE_FORMAT(birthdate, {string:year_high}) BETWEEN {date:low_date} AND {date:high_date}') . '
			)
			AND is_activated = {int:is_activated}', array('is_activated' => 1, 'no_month' => 0, 'no_day' => 0, 'year_one' => '0001', 'year_low' => $year_low . '-%m-%d', 'year_high' => $year_high . '-%m-%d', 'low_date' => $low_date, 'high_date' => $high_date, 'max_year' => $year_high));
    $bday = array();
    $MemberSetColor = false;
    $context['MemberColor_ID_MEMBER'] = array();
    while ($row = $smcFunc['db_fetch_assoc']($result)) {
        if ($year_low != $year_high) {
            $age_year = substr($row['birthdate'], 5) < substr($high_date, 5) ? $year_high : $year_low;
        } else {
            $age_year = $year_low;
        }
        $bday[$age_year . substr($row['birthdate'], 4)][] = array('id' => $row['id_member'], 'name' => $row['real_name'], 'age' => $row['birth_year'] > 4 && $row['birth_year'] <= $age_year ? $age_year - $row['birth_year'] : null, 'is_last' => false);
        //I need to collect some IDs for the MemberColor :).
        if (!empty($modSettings['MemberColorBirthday']) && !empty($row['id_member'])) {
            $context['MemberColor_ID_MEMBER'][$row['id_member']] = $row['id_member'];
        }
    }
    $smcFunc['db_free_result']($result);
    //Color the Birthday Links :D
    if (!empty($context['MemberColor_ID_MEMBER'])) {
        $colorDatas = load_onlineColors($context['MemberColor_ID_MEMBER']);
        $cmemcolid = null;
        //So i need only the colors... hmmm why schould i not use only the $user_profiles for this... (The Calenderlink is diffrent)
        if (!empty($modSettings['MemberColorBirthday'])) {
            if (!empty($bday) && is_array($bday)) {
                foreach ($bday as $memcalkv => $memcalva) {
                    foreach ($memcalva as $memcalidkv => $memcalid) {
                        if (!empty($colorDatas[$memcalid['id']]['colored_link'])) {
                            $bday[$memcalkv][$memcalidkv]['color'] = $colorDatas[$memcalid['id']]['online_color'];
                            $bday[$memcalkv][$memcalidkv]['colored_name'] = $colorDatas[$memcalid['id']]['colored_name'];
                        }
                    }
                }
            }
        }
    }
    // Set is_last, so the themes know when to stop placing separators.
    foreach ($bday as $mday => $array) {
        $bday[$mday][count($array) - 1]['is_last'] = true;
    }
    return $bday;
}
Example #15
0
function MessageFolder()
{
    global $txt, $scripturl, $modSettings, $context, $subjects_request;
    global $messages_request, $user_info, $recipients, $options, $smcFunc, $memberContext, $user_settings;
    // Changing view?
    if (isset($_GET['view'])) {
        $context['display_mode'] = $context['display_mode'] > 1 ? 0 : $context['display_mode'] + 1;
        updateMemberData($user_info['id'], array('pm_prefs' => $user_settings['pm_prefs'] & 252 | $context['display_mode']));
    }
    // Make sure the starting location is valid.
    if (isset($_GET['start']) && $_GET['start'] != 'new') {
        $_GET['start'] = (int) $_GET['start'];
    } elseif (!isset($_GET['start']) && !empty($options['view_newest_pm_first'])) {
        $_GET['start'] = 0;
    } else {
        $_GET['start'] = 'new';
    }
    // Set up some basic theme stuff.
    $context['from_or_to'] = $context['folder'] != 'sent' ? 'from' : 'to';
    $context['get_pmessage'] = 'prepareMessageContext';
    $context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
    $labelQuery = $context['folder'] != 'sent' ? '
			AND FIND_IN_SET(' . $context['current_label_id'] . ', pmr.labels) != 0' : '';
    //Correct a Modsettings sometime there is a problem, and load variables if i need them ;).
    $colormodcheck = 'MemberColorInbox';
    if ($context['folder'] != 'sent') {
        $colormodcheck = 'MemberColorOutbox';
    }
    $MemberSetColor = false;
    if (empty($modSettings[$colormodcheck]) && !empty($modSettings['MemberColorLink'])) {
        $modSettings['MemberColorLink'] = null;
    }
    if (!empty($modSettings[$colormodcheck])) {
        $context['MemberColor_ID_MEMBER'] = array();
        $MemberColor_IM_ID = array();
    }
    // Set the index bar correct!
    messageIndexBar($context['current_label_id'] == -1 ? $context['folder'] : 'label' . $context['current_label_id']);
    // Sorting the folder.
    $sort_methods = array('date' => 'pm.id_pm', 'name' => 'IFNULL(mem.real_name, \'\')', 'subject' => 'pm.subject');
    // They didn't pick one, use the forum default.
    if (!isset($_GET['sort']) || !isset($sort_methods[$_GET['sort']])) {
        $context['sort_by'] = 'date';
        $_GET['sort'] = 'pm.id_pm';
        // An overriding setting?
        $descending = !empty($options['view_newest_pm_first']);
    } else {
        $context['sort_by'] = $_GET['sort'];
        $_GET['sort'] = $sort_methods[$_GET['sort']];
        $descending = isset($_GET['desc']);
    }
    $context['sort_direction'] = $descending ? 'down' : 'up';
    // Why would you want access to your sent items if you're not allowed to send anything?
    if ($context['folder'] == 'sent') {
        isAllowedTo('pm_send');
    }
    // Set the text to resemble the current folder.
    $pmbox = $context['folder'] != 'sent' ? $txt['inbox'] : $txt['sent_items'];
    $txt['delete_all'] = str_replace('PMBOX', $pmbox, $txt['delete_all']);
    // Now, build the link tree!
    if ($context['current_label_id'] == -1) {
        $context['linktree'][] = array('url' => $scripturl . '?action=pm;f=' . $context['folder'], 'name' => $pmbox);
    }
    // Build it further for a label.
    if ($context['current_label_id'] != -1) {
        $context['linktree'][] = array('url' => $scripturl . '?action=pm;f=' . $context['folder'] . ';l=' . $context['current_label_id'], 'name' => $txt['pm_current_label'] . ': ' . $context['current_label']);
    }
    // Figure out how many messages there are.
    if ($context['folder'] == 'sent') {
        $request = $smcFunc['db_query']('', '
			SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
			FROM {db_prefix}personal_messages AS pm
			WHERE pm.id_member_from = {int:current_member}
				AND pm.deleted_by_sender = {int:not_deleted}', array('current_member' => $user_info['id'], 'not_deleted' => 0));
    } else {
        $request = $smcFunc['db_query']('', '
			SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
			FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
				INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
			WHERE pmr.id_member = {int:current_member}
				AND pmr.deleted = {int:not_deleted}' . $labelQuery, array('current_member' => $user_info['id'], 'not_deleted' => 0));
    }
    list($max_messages) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);
    // Only show the button if there are messages to delete.
    $context['show_delete'] = $max_messages > 0;
    // Start on the last page.
    if (!is_numeric($_GET['start']) || $_GET['start'] >= $max_messages) {
        $_GET['start'] = $max_messages - 1 - ($max_messages - 1) % $modSettings['defaultMaxMessages'];
    } elseif ($_GET['start'] < 0) {
        $_GET['start'] = 0;
    }
    // ... but wait - what if we want to start from a specific message?
    if (isset($_GET['pmid'])) {
        $pmID = (int) $_GET['pmid'];
        // Make sure you have access to this PM.
        if (!isAccessiblePM($pmID, $context['folder'] == 'sent' ? 'outbox' : 'inbox')) {
            fatal_lang_error('no_access', false);
        }
        $context['current_pm'] = $pmID;
        // With only one page of PM's we're gonna want page 1.
        if ($max_messages <= $modSettings['defaultMaxMessages']) {
            $_GET['start'] = 0;
        } elseif (!isset($_GET['kstart'])) {
            if ($context['folder'] == 'sent') {
                $request = $smcFunc['db_query']('', '
					SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
					FROM {db_prefix}personal_messages
					WHERE id_member_from = {int:current_member}
						AND deleted_by_sender = {int:not_deleted}
						AND id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}', array('current_member' => $user_info['id'], 'not_deleted' => 0, 'id_pm' => $pmID));
            } else {
                $request = $smcFunc['db_query']('', '
					SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
					FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
						INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
					WHERE pmr.id_member = {int:current_member}
						AND pmr.deleted = {int:not_deleted}' . $labelQuery . '
						AND pmr.id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}', array('current_member' => $user_info['id'], 'not_deleted' => 0, 'id_pm' => $pmID));
            }
            list($_GET['start']) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);
            // To stop the page index's being abnormal, start the page on the page the message would normally be located on...
            $_GET['start'] = $modSettings['defaultMaxMessages'] * (int) ($_GET['start'] / $modSettings['defaultMaxMessages']);
        }
    }
    // Sanitize and validate pmsg variable if set.
    if (isset($_GET['pmsg'])) {
        $pmsg = (int) $_GET['pmsg'];
        if (!isAccessiblePM($pmsg, $context['folder'] == 'sent' ? 'outbox' : 'inbox')) {
            fatal_lang_error('no_access', false);
        }
    }
    // Set up the page index.
    $context['page_index'] = constructPageIndex($scripturl . '?action=pm;f=' . $context['folder'] . (isset($_REQUEST['l']) ? ';l=' . (int) $_REQUEST['l'] : '') . ';sort=' . $context['sort_by'] . ($descending ? ';desc' : ''), $_GET['start'], $max_messages, $modSettings['defaultMaxMessages']);
    $context['start'] = $_GET['start'];
    // Determine the navigation context (especially useful for the wireless template).
    $context['links'] = array('first' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=0' : '', 'prev' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=' . ($_GET['start'] - $modSettings['defaultMaxMessages']) : '', 'next' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . ($_GET['start'] + $modSettings['defaultMaxMessages']) : '', 'last' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) * $modSettings['defaultMaxMessages'] : '', 'up' => $scripturl);
    $context['page_info'] = array('current_page' => $_GET['start'] / $modSettings['defaultMaxMessages'] + 1, 'num_pages' => floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) + 1);
    // First work out what messages we need to see - if grouped is a little trickier...
    if ($context['display_mode'] == 2) {
        // On a non-default sort due to PostgreSQL we have to do a harder sort.
        if ($smcFunc['db_title'] == 'PostgreSQL' && $_GET['sort'] != 'pm.id_pm') {
            $sub_request = $smcFunc['db_query']('', '
				SELECT MAX({raw:sort}) AS sort_param, pm.id_pm_head
				FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? $context['sort_by'] == 'name' ? '
					LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '' : '
					INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
						AND pmr.id_member = {int:current_member}
						AND pmr.deleted = {int:not_deleted}
						' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? '
					LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})' : '') . '
				WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
					AND pm.deleted_by_sender = {int:not_deleted}' : '1=1') . (empty($pmsg) ? '' : '
					AND pm.id_pm = {int:id_pm}') . '
				GROUP BY pm.id_pm_head
				ORDER BY sort_param' . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
				LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''), array('current_member' => $user_info['id'], 'not_deleted' => 0, 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from', 'id_pm' => isset($pmsg) ? $pmsg : '0', 'sort' => $_GET['sort']));
            $sub_pms = array();
            while ($row = $smcFunc['db_fetch_assoc']($sub_request)) {
                $sub_pms[$row['id_pm_head']] = $row['sort_param'];
            }
            $smcFunc['db_free_result']($sub_request);
            $request = $smcFunc['db_query']('', '
				SELECT pm.id_pm AS id_pm, pm.id_pm_head
				FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? $context['sort_by'] == 'name' ? '
					LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '' : '
					INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
						AND pmr.id_member = {int:current_member}
						AND pmr.deleted = {int:not_deleted}
						' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? '
					LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})' : '') . '
				WHERE ' . (empty($sub_pms) ? '0=1' : 'pm.id_pm IN ({array_int:pm_list})') . '
				ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
				LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''), array('current_member' => $user_info['id'], 'pm_list' => array_keys($sub_pms), 'not_deleted' => 0, 'sort' => $_GET['sort'], 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from'));
        } else {
            $request = $smcFunc['db_query']('pm_conversation_list', '
				SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
				FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? $context['sort_by'] == 'name' ? '
					LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '' : '
					INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
						AND pmr.id_member = {int:current_member}
						AND pmr.deleted = {int:deleted_by}
						' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? '
					LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})' : '') . '
				WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
					AND pm.deleted_by_sender = {int:deleted_by}' : '1=1') . (empty($pmsg) ? '' : '
					AND pm.id_pm = {int:pmsg}') . '
				GROUP BY pm.id_pm_head
				ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($_GET['pmsg']) ? '
				LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''), array('current_member' => $user_info['id'], 'deleted_by' => 0, 'sort' => $_GET['sort'], 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from', 'pmsg' => isset($pmsg) ? (int) $pmsg : 0));
        }
    } else {
        // !!!SLOW This query uses a filesort. (inbox only.)
        $request = $smcFunc['db_query']('', '
			SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
			FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' . ($context['sort_by'] == 'name' ? '
				LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
				INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
					AND pmr.id_member = {int:current_member}
					AND pmr.deleted = {int:is_deleted}
					' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? '
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})' : '') . '
			WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {raw:current_member}
				AND pm.deleted_by_sender = {int:is_deleted}' : '1=1') . (empty($pmsg) ? '' : '
				AND pm.id_pm = {int:pmsg}') . '
			ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'pmr.id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
			LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''), array('current_member' => $user_info['id'], 'is_deleted' => 0, 'sort' => $_GET['sort'], 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from', 'pmsg' => isset($pmsg) ? (int) $pmsg : 0));
    }
    // Load the id_pms and initialize recipients.
    $pms = array();
    $lastData = array();
    $posters = $context['folder'] == 'sent' ? array($user_info['id']) : array();
    $recipients = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        if (!isset($recipients[$row['id_pm']])) {
            if (isset($row['id_member_from'])) {
                $posters[$row['id_pm']] = $row['id_member_from'];
            }
            $pms[$row['id_pm']] = $row['id_pm'];
            $recipients[$row['id_pm']] = array('to' => array(), 'bcc' => array());
        }
        // Keep track of the last message so we know what the head is without another query!
        if (empty($pmID) && (empty($options['view_newest_pm_first']) || !isset($lastData)) || empty($lastData) || !empty($pmID) && $pmID == $row['id_pm']) {
            $lastData = array('id' => $row['id_pm'], 'head' => $row['id_pm_head']);
        }
    }
    $smcFunc['db_free_result']($request);
    if (!empty($pms)) {
        // Select the correct current message.
        if (empty($pmID)) {
            $context['current_pm'] = $lastData['id'];
        }
        // This is a list of the pm's that are used for "full" display.
        if ($context['display_mode'] == 0) {
            $display_pms = $pms;
        } else {
            $display_pms = array($context['current_pm']);
        }
        // At this point we know the main id_pm's. But - if we are looking at conversations we need the others!
        if ($context['display_mode'] == 2) {
            $request = $smcFunc['db_query']('', '
				SELECT pm.id_pm, pm.id_member_from, pm.deleted_by_sender, pmr.id_member, pmr.deleted
				FROM {db_prefix}personal_messages AS pm
					INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
				WHERE pm.id_pm_head = {int:id_pm_head}
					AND ((pm.id_member_from = {int:current_member} AND pm.deleted_by_sender = {int:not_deleted})
						OR (pmr.id_member = {int:current_member} AND pmr.deleted = {int:not_deleted}))
				ORDER BY pm.id_pm', array('current_member' => $user_info['id'], 'id_pm_head' => $lastData['head'], 'not_deleted' => 0));
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                // This is, frankly, a joke. We will put in a workaround for people sending to themselves - yawn!
                if ($context['folder'] == 'sent' && $row['id_member_from'] == $user_info['id'] && $row['deleted_by_sender'] == 1) {
                    continue;
                } elseif ($row['id_member'] == $user_info['id'] & $row['deleted'] == 1) {
                    continue;
                }
                if (!isset($recipients[$row['id_pm']])) {
                    $recipients[$row['id_pm']] = array('to' => array(), 'bcc' => array());
                }
                $display_pms[] = $row['id_pm'];
                $posters[$row['id_pm']] = $row['id_member_from'];
            }
            $smcFunc['db_free_result']($request);
        }
        // This is pretty much EVERY pm!
        $all_pms = array_merge($pms, $display_pms);
        $all_pms = array_unique($all_pms);
        // Get recipients (don't include bcc-recipients for your inbox, you're not supposed to know :P).
        $request = $smcFunc['db_query']('', '
			SELECT pmr.id_pm, mem_to.id_member AS id_member_to, mem_to.real_name AS to_name, pmr.bcc, pmr.labels, pmr.is_read
			FROM {db_prefix}pm_recipients AS pmr
				LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
			WHERE pmr.id_pm IN ({array_int:pm_list})', array('pm_list' => $all_pms));
        $context['message_labels'] = array();
        $context['message_replied'] = array();
        $context['message_unread'] = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            if ($context['folder'] == 'sent' || empty($row['bcc'])) {
                $recipients[$row['id_pm']][empty($row['bcc']) ? 'to' : 'bcc'][] = empty($row['id_member_to']) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_to'] . '">' . $row['to_name'] . '</a>';
            }
            if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent') {
                $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
                $context['message_unread'][$row['id_pm']] = $row['is_read'] == 0;
                $row['labels'] = $row['labels'] == '' ? array() : explode(',', $row['labels']);
                foreach ($row['labels'] as $v) {
                    if (isset($context['labels'][(int) $v])) {
                        $context['message_labels'][$row['id_pm']][(int) $v] = array('id' => $v, 'name' => $context['labels'][(int) $v]['name']);
                    }
                }
            }
            if (!empty($row['id_member_to']) && !empty($modSettings[$colormodcheck])) {
                $MemberSetColor = true;
                $posters[] = $row['id_member_to'];
                // Hmmm somehow i must know what i should replace... i hope i do it right...
                $MemberColor_IM_ID[$row['id_pm']][empty($row['bcc']) ? 'to' : 'bcc'][] = $row['id_member_to'];
            } elseif (empty($row['id_member_to']) && (!empty($modSettings['MemberColorOutbox']) || !empty($modSettings['MemberColorInbox']))) {
                $MemberColor_IM_ID[$row['id_pm']][empty($row['bcc']) ? 'to' : 'bcc'][] = null;
            }
        }
        $smcFunc['db_free_result']($request);
        // Make sure we don't load unnecessary data.
        if ($context['display_mode'] == 1) {
            foreach ($posters as $k => $v) {
                if (!in_array($k, $display_pms)) {
                    unset($posters[$k]);
                }
            }
        }
        // Load any users....
        $posters = array_unique($posters);
        if (!empty($posters)) {
            loadMemberData($posters);
        }
        //So i must correct the link ;) i need only the membercontext for this (a little bit faster)
        if ($MemberSetColor && !empty($MemberColor_IM_ID)) {
            $colorDatas = load_onlineColors($posters);
            //So let's replace the links...
            foreach ($MemberColor_IM_ID as $pmid => $pmidarray) {
                if (!empty($pmidarray['to'])) {
                    foreach ($pmidarray['to'] as $pm_meber_key => $pm_member_id) {
                        if (!empty($recipients[$pmid]['to'][$pm_meber_key]) && !empty($colorDatas[$pm_member_id]['colored_link'])) {
                            $recipients[$pmid]['to'][$pm_meber_key] = $colorDatas[$pm_member_id]['colored_link'];
                        }
                    }
                }
                if (!empty($pmidarray['bcc'])) {
                    foreach ($pmidarray['bcc'] as $pm_meber_key => $pm_member_id) {
                        if (!empty($recipients[$pmid]['bcc'][$pm_meber_key]) && !empty($colorDatas[$pm_member_id]['colored_link'])) {
                            $recipients[$pmid]['bcc'][$pm_meber_key] = $colorDatas[$pm_member_id]['colored_link'];
                        }
                    }
                }
            }
        }
        // If we're on grouped/restricted view get a restricted list of messages.
        if ($context['display_mode'] != 0) {
            // Get the order right.
            $orderBy = array();
            foreach (array_reverse($pms) as $pm) {
                $orderBy[] = 'pm.id_pm = ' . $pm;
            }
            // Seperate query for these bits!
            $subjects_request = $smcFunc['db_query']('', '
				SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.msgtime, IFNULL(mem.real_name, pm.from_name) AS from_name,
					IFNULL(mem.id_member, 0) AS not_guest
				FROM {db_prefix}personal_messages AS pm
					LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
				WHERE pm.id_pm IN ({array_int:pm_list})
				ORDER BY ' . implode(', ', $orderBy) . '
				LIMIT ' . count($pms), array('pm_list' => $pms));
        }
        // Execute the query!
        $messages_request = $smcFunc['db_query']('', '
			SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
			FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '
				LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') . ($context['sort_by'] == 'name' ? '
				LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})' : '') . '
			WHERE pm.id_pm IN ({array_int:display_pms})' . ($context['folder'] == 'sent' ? '
			GROUP BY pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name' : '') . '
			ORDER BY ' . ($context['display_mode'] == 2 ? 'pm.id_pm' : $_GET['sort']) . ($descending ? ' DESC' : ' ASC') . '
			LIMIT ' . count($display_pms), array('display_pms' => $display_pms, 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from'));
    } else {
        $messages_request = false;
    }
    $context['can_send_pm'] = allowedTo('pm_send');
    if (!WIRELESS) {
        $context['sub_template'] = 'folder';
    }
    $context['page_title'] = $txt['pm_inbox'];
    // Finally mark the relevant messages as read.
    if ($context['folder'] != 'sent' && !empty($context['labels'][(int) $context['current_label_id']]['unread_messages'])) {
        // If the display mode is "old sk00l" do them all...
        if ($context['display_mode'] == 0) {
            markMessages(null, $context['current_label_id']);
        } elseif (!empty($context['current_pm'])) {
            markMessages($display_pms, $context['current_label_id']);
        }
    }
}