function thread_get($tid, $inc_deleted = false, $inc_empty = false)
{
    if (!($db = db::get())) {
        return false;
    }
    $fidlist = folder_get_available();
    if (!($table_prefix = get_table_prefix())) {
        return false;
    }
    if (($uid = session::get_value('UID')) === false) {
        return false;
    }
    if (!is_numeric($tid)) {
        return false;
    }
    $unread_cutoff_timestamp = threads_get_unread_cutoff();
    $sql = "SELECT THREAD.TID, THREAD.FID, THREAD.DELETED, THREAD.LENGTH, ";
    $sql .= "TRIM(CONCAT_WS(' ', COALESCE(FOLDER.PREFIX, ''), THREAD.TITLE)) AS TITLE, ";
    $sql .= "THREAD.POLL_FLAG, THREAD.STICKY, THREAD.UNREAD_PID, ";
    $sql .= "THREAD_STATS.VIEWCOUNT, USER_THREAD.LAST_READ, USER_THREAD.INTEREST, ";
    $sql .= "THREAD.BY_UID, UNIX_TIMESTAMP(THREAD.CLOSED) AS CLOSED, ";
    $sql .= "UNIX_TIMESTAMP(THREAD.ADMIN_LOCK) AS ADMIN_LOCK, ";
    $sql .= "UNIX_TIMESTAMP(THREAD.CREATED) AS CREATED, THREAD.ADMIN_LOCK, ";
    $sql .= "UNIX_TIMESTAMP(THREAD.STICKY_UNTIL) AS STICKY_UNTIL, ";
    $sql .= "UNIX_TIMESTAMP(THREAD.MODIFIED) AS MODIFIED, USER.UID, USER.LOGON, ";
    $sql .= "USER.NICKNAME, USER_PEER.PEER_NICKNAME, USER_PEER.RELATIONSHIP, ";
    $sql .= "FOLDER.TITLE AS FOLDER_TITLE FROM `{$table_prefix}THREAD` THREAD ";
    $sql .= "LEFT JOIN `{$table_prefix}THREAD_STATS` THREAD_STATS ";
    $sql .= "ON (THREAD_STATS.TID = THREAD.TID) ";
    $sql .= "LEFT JOIN `{$table_prefix}USER_THREAD` USER_THREAD ";
    $sql .= "ON (THREAD.TID = USER_THREAD.TID AND USER_THREAD.UID = '{$uid}') ";
    $sql .= "LEFT JOIN `{$table_prefix}USER_PEER` USER_PEER ";
    $sql .= "ON (USER_PEER.PEER_UID = THREAD.BY_UID AND USER_PEER.UID = '{$uid}') ";
    $sql .= "LEFT JOIN USER USER ON (USER.UID = THREAD.BY_UID) ";
    $sql .= "LEFT JOIN `{$table_prefix}FOLDER` FOLDER ON (FOLDER.FID = THREAD.FID) ";
    $sql .= "WHERE THREAD.TID = '{$tid}' AND THREAD.FID IN ({$fidlist}) ";
    if ($inc_deleted === false) {
        $sql .= "AND THREAD.DELETED = 'N' ";
    }
    if ($inc_empty === false) {
        $sql .= "AND THREAD.LENGTH > 0 ";
    }
    if (!($result = $db->query($sql))) {
        return false;
    }
    if ($result->num_rows == 0) {
        return false;
    }
    $thread_data = $result->fetch_assoc();
    if (!isset($thread_data['INTEREST'])) {
        $thread_data['INTEREST'] = 0;
    }
    if (!session::logged_in()) {
        $thread_data['LAST_READ'] = 0;
    } else {
        if (!isset($thread_data['LAST_READ']) || !is_numeric($thread_data['LAST_READ'])) {
            $thread_data['LAST_READ'] = 0;
            if (isset($thread_data['MODIFIED']) && $unread_cutoff_timestamp !== false && $thread_data['MODIFIED'] < $unread_cutoff_timestamp) {
                $thread_data['LAST_READ'] = $thread_data['LENGTH'];
            } else {
                if (isset($thread_data['UNREAD_PID']) && is_numeric($thread_data['UNREAD_PID'])) {
                    $thread_data['LAST_READ'] = $thread_data['UNREAD_PID'];
                }
            }
        }
    }
    if (!isset($thread_data['STICKY_UNTIL'])) {
        $thread_data['STICKY_UNTIL'] = 0;
    }
    if (!isset($thread_data['ADMIN_LOCK'])) {
        $thread_data['ADMIN_LOCK'] = 0;
    }
    if (!isset($thread_data['CLOSED'])) {
        $thread_data['CLOSED'] = 0;
    }
    if (!isset($thread_data['DELETED'])) {
        $thread_data['DELETED'] = 'N';
    }
    if (isset($thread_data['LOGON']) && isset($thread_data['PEER_NICKNAME'])) {
        if (!is_null($thread_data['PEER_NICKNAME']) && strlen($thread_data['PEER_NICKNAME']) > 0) {
            $thread_data['NICKNAME'] = $thread_data['PEER_NICKNAME'];
        }
    }
    if (!isset($thread_data['LOGON'])) {
        $thread_data['LOGON'] = gettext("Unknown user");
    }
    if (!isset($thread_data['NICKNAME'])) {
        $thread_data['NICKNAME'] = "";
    }
    thread_has_attachments($thread_data);
    return $thread_data;
}
function threads_process_list($sql)
{
    if (!($db = db::get())) {
        return array(0, 0, 0);
    }
    if (!($result = $db->query($sql))) {
        return array(0, 0, 0);
    }
    if (($thread_count = $result->num_rows) == 0) {
        return array(0, 0, 0);
    }
    $unread_cutoff_timestamp = threads_get_unread_cutoff();
    $threads_array = array();
    $folder_order = array();
    while (($thread = $result->fetch_assoc()) !== null) {
        if (isset($thread['LOGON']) && isset($thread['PEER_NICKNAME'])) {
            if (!is_null($thread['PEER_NICKNAME']) && strlen($thread['PEER_NICKNAME']) > 0) {
                $thread['NICKNAME'] = $thread['PEER_NICKNAME'];
            }
        }
        if (!isset($thread['LOGON'])) {
            $thread['LOGON'] = gettext("Unknown user");
        }
        if (!isset($thread['NICKNAME'])) {
            $thread['NICKNAME'] = "";
        }
        if (!isset($thread['RELATIONSHIP']) || is_null($thread['RELATIONSHIP'])) {
            $thread['RELATIONSHIP'] = 0;
        }
        if (!isset($thread['INTEREST']) || is_null($thread['INTEREST'])) {
            $thread['INTEREST'] = 0;
        }
        if (!isset($thread['STICKY']) || is_null($thread['STICKY'])) {
            $thread['STICKY'] = 0;
        }
        if (!isset($thread['VIEWCOUNT']) || is_null($thread['VIEWCOUNT'])) {
            $thread['VIEWCOUNT'] = 0;
        }
        if (!isset($thread['TRACK_TYPE']) || is_null($thread['TRACK_TYPE'])) {
            $thread['TRACK_TYPE'] = -1;
        }
        if (!isset($thread['DELETED']) || is_null($thread['DELETED'])) {
            $thread['DELETED'] = 'N';
        }
        if (!in_array($thread['FID'], $folder_order)) {
            $folder_order[] = $thread['FID'];
        }
        if (!session::logged_in()) {
            $thread['LAST_READ'] = 0;
        } else {
            if (!isset($thread['LAST_READ']) || is_null($thread['LAST_READ'])) {
                $thread['LAST_READ'] = 0;
                if (isset($thread['MODIFIED']) && $unread_cutoff_timestamp !== false && $thread['MODIFIED'] < $unread_cutoff_timestamp) {
                    $thread['LAST_READ'] = $thread['LENGTH'];
                } else {
                    if (isset($thread['UNREAD_PID']) && is_numeric($thread['UNREAD_PID'])) {
                        $thread['LAST_READ'] = $thread['UNREAD_PID'];
                    }
                }
            }
        }
        $threads_array[$thread['TID']] = $thread;
    }
    threads_have_attachments($threads_array);
    return array($threads_array, $folder_order, $thread_count);
}
function folder_move_threads($from, $to)
{
    if (!($db = db::get())) {
        return false;
    }
    if (!is_numeric($from)) {
        return false;
    }
    if (!is_numeric($to)) {
        return false;
    }
    if (!($table_prefix = get_table_prefix())) {
        return false;
    }
    $current_datetime = date(MYSQL_DATETIME, time());
    $modified_cutoff_datetime = date(MYSQL_DATETIME, threads_get_unread_cutoff());
    $sql = "UPDATE LOW_PRIORITY `{$table_prefix}THREAD` SET FID = '{$to}', ";
    $sql .= "MODIFIED = IF(MODIFIED < CAST('{$modified_cutoff_datetime}' AS DATETIME), ";
    $sql .= "MODIFIED, CAST('{$current_datetime}' AS DATETIME)) WHERE FID = '{$from}'";
    if (!($result = $db->query($sql))) {
        return false;
    }
    return $result;
}
function messages_get_most_recent_unread($uid, $fid = false)
{
    if (!($db = db::get())) {
        return false;
    }
    if (is_numeric($fid)) {
        $fidlist = $fid;
    } else {
        $fidlist = folder_get_available();
    }
    if (!is_numeric($uid)) {
        return false;
    }
    if (!($table_prefix = get_table_prefix())) {
        return false;
    }
    if (($unread_cutoff_datetime = forum_get_unread_cutoff_datetime()) === false) {
        return false;
    }
    $unread_cutoff_timestamp = threads_get_unread_cutoff();
    $user_ignored_completely = USER_IGNORED_COMPLETELY;
    $user_ignored = USER_IGNORED;
    $sql = "SELECT THREAD.TID, UNIX_TIMESTAMP(THREAD.MODIFIED) AS MODIFIED, ";
    $sql .= "THREAD.LENGTH, USER_THREAD.LAST_READ, USER_PEER.RELATIONSHIP, ";
    $sql .= "THREAD.UNREAD_PID FROM `{$table_prefix}THREAD` THREAD ";
    $sql .= "LEFT JOIN `{$table_prefix}USER_PEER` USER_PEER ON ";
    $sql .= "(USER_PEER.UID = '{$uid}' AND USER_PEER.PEER_UID = THREAD.BY_UID) ";
    $sql .= "LEFT JOIN `{$table_prefix}USER_THREAD` USER_THREAD ";
    $sql .= "ON (USER_THREAD.TID = THREAD.TID AND USER_THREAD.UID = '{$uid}') ";
    $sql .= "LEFT JOIN `{$table_prefix}USER_FOLDER` USER_FOLDER ";
    $sql .= "ON (USER_FOLDER.FID = THREAD.FID AND USER_FOLDER.UID = '{$uid}') ";
    $sql .= "WHERE THREAD.FID in ({$fidlist}) AND THREAD.DELETED = 'N' ";
    $sql .= "AND THREAD.LENGTH > 0 AND (USER_PEER.RELATIONSHIP IS NULL ";
    $sql .= "OR (USER_PEER.RELATIONSHIP & {$user_ignored_completely}) = 0) ";
    $sql .= "AND ((USER_PEER.RELATIONSHIP & {$user_ignored}) = 0 ";
    $sql .= "OR USER_PEER.RELATIONSHIP IS NULL OR THREAD.LENGTH > 1) ";
    $sql .= "AND (USER_THREAD.LAST_READ < THREAD.LENGTH OR USER_THREAD.LAST_READ IS NULL) ";
    $sql .= "AND THREAD.MODIFIED > CAST('{$unread_cutoff_datetime}' AS DATETIME) ";
    $sql .= "AND (USER_THREAD.INTEREST IS NULL OR USER_THREAD.INTEREST > -1) ";
    $sql .= "AND (USER_FOLDER.INTEREST IS NULL OR USER_FOLDER.INTEREST > -1) ";
    $sql .= "ORDER BY THREAD.MODIFIED DESC LIMIT 0, 1";
    if (!($result = $db->query($sql))) {
        return false;
    }
    if ($result->num_rows == 0) {
        return false;
    }
    $message_data = $result->fetch_assoc();
    if (!session::logged_in()) {
        return "{$message_data['TID']}.1";
    } else {
        if (!isset($message_data['LAST_READ']) || !is_numeric($message_data['LAST_READ'])) {
            $message_data['LAST_READ'] = 1;
            if (isset($message_data['MODIFIED']) && $unread_cutoff_timestamp !== false && $message_data['MODIFIED'] < $unread_cutoff_timestamp) {
                $message_data['LAST_READ'] = $message_data['LENGTH'];
            } else {
                if (isset($message_data['UNREAD_PID']) && is_numeric($message_data['UNREAD_PID'])) {
                    $message_data['LAST_READ'] = $message_data['UNREAD_PID'];
                }
            }
            return "{$message_data['TID']}.{$message_data['LAST_READ']}";
        } else {
            if ($message_data['LAST_READ'] < $message_data['LENGTH']) {
                $message_data['LAST_READ']++;
            }
            return "{$message_data['TID']}.{$message_data['LAST_READ']}";
        }
    }
}
Exemple #5
0
function light_draw_thread_list($mode = ALL_DISCUSSIONS, $folder = false, $page = 1)
{
    $webtag = get_webtag();
    forum_check_webtag_available($webtag);
    $error_msg_array = array();
    $available_views = thread_list_available_views();
    $visible_threads_array = array();
    if (!isset($_SESSION['UID']) || !is_numeric($_SESSION['UID'])) {
        return;
    }
    light_thread_list_draw_top($mode, $folder);
    switch ($mode) {
        case UNREAD_DISCUSSIONS:
            list($thread_info, $folder_order, $thread_count) = threads_get_unread($_SESSION['UID'], $folder, $page);
            break;
        case UNREAD_DISCUSSIONS_TO_ME:
            list($thread_info, $folder_order, $thread_count) = threads_get_unread_to_me($_SESSION['UID'], $folder, $page);
            break;
        case TODAYS_DISCUSSIONS:
            list($thread_info, $folder_order, $thread_count) = threads_get_by_days($_SESSION['UID'], $folder, $page, 1);
            break;
        case UNREAD_TODAY:
            list($thread_info, $folder_order, $thread_count) = threads_get_unread_by_days($_SESSION['UID'], $folder, $page);
            break;
        case TWO_DAYS_BACK:
            list($thread_info, $folder_order, $thread_count) = threads_get_by_days($_SESSION['UID'], $folder, $page, 2);
            break;
        case SEVEN_DAYS_BACK:
            list($thread_info, $folder_order, $thread_count) = threads_get_by_days($_SESSION['UID'], $folder, $page, 7);
            break;
        case HIGH_INTEREST:
            list($thread_info, $folder_order, $thread_count) = threads_get_by_interest($_SESSION['UID'], $folder, $page, THREAD_INTERESTED);
            break;
        case UNREAD_HIGH_INTEREST:
            list($thread_info, $folder_order, $thread_count) = threads_get_unread_by_interest($_SESSION['UID'], $folder, $page, THREAD_INTERESTED);
            break;
        case RECENTLY_SEEN:
            list($thread_info, $folder_order, $thread_count) = threads_get_recently_viewed($_SESSION['UID'], $folder, $page);
            break;
        case IGNORED_THREADS:
            list($thread_info, $folder_order, $thread_count) = threads_get_by_interest($_SESSION['UID'], $folder, $page, THREAD_IGNORED);
            break;
        case BY_IGNORED_USERS:
            list($thread_info, $folder_order, $thread_count) = threads_get_by_relationship($_SESSION['UID'], $folder, $page, USER_IGNORED_COMPLETELY);
            break;
        case SUBSCRIBED_TO:
            list($thread_info, $folder_order, $thread_count) = threads_get_by_interest($_SESSION['UID'], $folder, $page, THREAD_SUBSCRIBED);
            break;
        case STARTED_BY_FRIEND:
            list($thread_info, $folder_order, $thread_count) = threads_get_by_relationship($_SESSION['UID'], $folder, $page, USER_FRIEND);
            break;
        case UNREAD_STARTED_BY_FRIEND:
            list($thread_info, $folder_order, $thread_count) = threads_get_unread_by_relationship($_SESSION['UID'], $folder, $page, USER_FRIEND);
            break;
        case STARTED_BY_ME:
            list($thread_info, $folder_order, $thread_count) = threads_get_started_by_me($_SESSION['UID'], $folder, $page);
            break;
        case POLL_THREADS:
            list($thread_info, $folder_order, $thread_count) = threads_get_polls($_SESSION['UID'], $folder, $page);
            break;
        case STICKY_THREADS:
            list($thread_info, $folder_order, $thread_count) = threads_get_sticky($_SESSION['UID'], $folder, $page);
            break;
        case MOST_UNREAD_POSTS:
            list($thread_info, $folder_order, $thread_count) = threads_get_longest_unread($_SESSION['UID'], $folder, $page);
            break;
        case DELETED_THREADS:
            list($thread_info, $folder_order, $thread_count) = threads_get_deleted($_SESSION['UID'], $folder, $page);
            break;
        default:
            list($thread_info, $folder_order, $thread_count) = threads_get_all($_SESSION['UID'], $folder, $page);
            break;
    }
    // Now, the actual bit that displays the threads...
    // Get folder FIDs and titles
    if (!($folder_info = threads_get_folders())) {
        light_html_display_error_msg(gettext("There are no folders available."));
        return;
    }
    // Get total number of messages for each folder
    $folder_msgs = threads_get_folder_msgs();
    // Check that the folder order is a valid array.
    if (!is_array($folder_order)) {
        $folder_order = array();
    }
    // Check the folder display order.
    if (isset($_SESSION['THREADS_BY_FOLDER']) && $_SESSION['THREADS_BY_FOLDER'] == 'Y') {
        $folder_order = array_keys($folder_info);
    }
    // Check for a message to display and re-order the thread list.
    if (isset($_REQUEST['msg']) && validate_msg($_REQUEST['msg'])) {
        list($selected_tid) = explode('.', $_REQUEST['msg']);
        if (($thread = thread_get($selected_tid)) !== false) {
            if (!isset($thread['RELATIONSHIP'])) {
                $thread['RELATIONSHIP'] = 0;
            }
            // Check the folder display order / user is a guest.
            if (!isset($_SESSION['THREADS_BY_FOLDER']) || $_SESSION['THREADS_BY_FOLDER'] != 'Y' || !session::logged_in()) {
                // Remove the folder from the list of folders.
                if (in_array($thread['FID'], $folder_order)) {
                    array_splice($folder_order, array_search($thread['FID'], $folder_order), 1);
                }
                // Re-add it at the top of the list.
                array_unshift($folder_order, $thread['FID']);
            }
            // Check $thread_info is an array.
            if (!is_array($thread_info)) {
                $thread_info = array();
            }
            // Check to see if the thread is already in the list.
            // If it is remove it, otherwise take the last thread
            // off the list so we always only have 50 threads on display.
            if (isset($thread_info[$selected_tid])) {
                unset($thread_info[$selected_tid]);
            } else {
                $thread_info = array_slice($thread_info, 0, 50, true);
            }
            // Add the requested thread to the top of the list of threads.
            array_unshift($thread_info, $thread);
        }
    }
    // Check for a specified folder and move it to the top of the thread list.
    if (isset($folder) && is_numeric($folder)) {
        if (in_array($folder, $folder_order)) {
            array_splice($folder_order, array_search($folder, $folder_order), 1);
        }
        array_unshift($folder_order, $folder);
    }
    if ($_SESSION['UID'] > 0) {
        // Array to hold our ignored folders in.
        $ignored_folders = array();
        // Loop through the list of folders and check their status.
        // If they're ignored and not already set to be on display
        // they need to be added to $ignored_folders so that they
        // appear at the bottom of the thread list.
        foreach ($folder_info as $fid => $folder_data) {
            if (!in_array($fid, $folder_order) && !in_array($fid, $ignored_folders)) {
                if ($folder_data['INTEREST'] != FOLDER_IGNORED || isset($folder) && $folder == $fid) {
                    array_push($folder_order, $fid);
                } else {
                    array_push($ignored_folders, $fid);
                }
            }
        }
        // Append ignored folders onto the end of the folder list.
        // This will make them appear at the bottom of the thread list.
        $folder_order = array_merge($folder_order, $ignored_folders);
    } else {
        foreach ($folder_info as $fid => $folder_data) {
            if (!in_array($fid, $folder_order)) {
                $folder_order[] = $fid;
            }
        }
    }
    // If no threads are returned, say something to that effect
    if (isset($_REQUEST['mark_read_success'])) {
        light_html_display_success_msg(gettext("Successfully marked selected threads as read"));
    } else {
        if (!is_array($thread_info)) {
            if (is_numeric($folder) && ($folder_title = folder_get_title($folder))) {
                $all_discussions_link = sprintf("<a href=\"lthread_list.php?webtag={$webtag}&amp;folder={$folder}&amp;mode=0\">%s</a>", gettext("click here"));
                light_html_display_warning_msg(sprintf(gettext("No &quot;%s&quot; in &quot;%s&quot; folder. Please select another folder, or %s for all threads."), $available_views[$mode], $folder_title, $all_discussions_link));
            } else {
                $all_discussions_link = sprintf("<a href=\"lthread_list.php?webtag={$webtag}&amp;mode=0\">%s</a>", gettext("click here"));
                light_html_display_warning_msg(sprintf(gettext("No &quot;%s&quot; available. Please %s for all threads."), $available_views[$mode], $all_discussions_link));
            }
        } else {
            if (isset($error_msg_array) && sizeof($error_msg_array) > 0) {
                light_html_display_error_array($error_msg_array);
            } else {
                if (is_numeric($folder) && ($folder_title = folder_get_title($folder))) {
                    $all_folders_link = sprintf("<a href=\"lthread_list.php?webtag={$webtag}&amp;mode={$mode}\">%s</a>", gettext("click here"));
                    light_html_display_warning_msg(sprintf(gettext("Viewing &quot;%s&quot; in &quot;%s&quot; only. To view threads in all folders %s."), $available_views[$mode], $folder_title, $all_folders_link));
                }
            }
        }
    }
    if ($page > 1 && !is_numeric($folder)) {
        echo "<div class=\"thread_pagination\"><a href=\"lthread_list.php?webtag={$webtag}&amp;mode={$mode}&amp;page=", $page - 1, "\">", gettext("Previous 50 threads"), "</a></div>\n";
    }
    // Unread cut-off
    $thread_unread_cutoff = threads_get_unread_cutoff();
    // Iterate through the information we've just got and display it in the right order
    foreach ($folder_order as $folder_number) {
        if (isset($folder_info[$folder_number]) && is_array($folder_info[$folder_number])) {
            echo "<div class=\"folder\">\n";
            echo "  <h3>", html_style_image('folder'), "<a href=\"lthread_list.php?webtag={$webtag}&amp;mode={$mode}&amp;folder={$folder_number}\">", word_filter_add_ob_tags($folder_info[$folder_number]['TITLE'], true), "</a></h3>";
            echo "  <div class=\"folder_inner\">\n";
            if (!session::logged_in() || $folder_info[$folder_number]['INTEREST'] > FOLDER_IGNORED || $mode == UNREAD_DISCUSSIONS_TO_ME || isset($folder) && $folder == $folder_number) {
                if (is_array($thread_info)) {
                    echo "  <div class=\"folder_info\">";
                    if (isset($folder_msgs[$folder_number])) {
                        echo $folder_msgs[$folder_number];
                    } else {
                        echo "0";
                    }
                    echo " ", gettext("threads"), "";
                    if (is_null($folder_info[$folder_number]['STATUS']) || $folder_info[$folder_number]['STATUS'] & USER_PERM_THREAD_CREATE) {
                        if ($folder_info[$folder_number]['ALLOWED_TYPES'] & FOLDER_ALLOW_NORMAL_THREAD) {
                            echo "<span><a href=\"lpost.php?webtag={$webtag}&amp;fid={$folder_number}\">", gettext("Post New"), "</a></span>";
                        }
                    }
                    echo "  </div>\n";
                    if ($page > 1 && is_numeric($folder) && $folder_number == $folder) {
                        echo "<div class=\"folder_pagination\"><a href=\"lthread_list.php?webtag={$webtag}&amp;mode={$mode}&amp;folder={$folder}&amp;page=", $page - 1, "\">", gettext("Previous 50 threads"), "</a></div>\n";
                    }
                    $folder_list_start = false;
                    $folder_list_end = false;
                    foreach ($thread_info as $thread) {
                        if (!in_array($thread['TID'], $visible_threads_array)) {
                            $visible_threads_array[] = $thread['TID'];
                        }
                        if ($thread['FID'] == $folder_number) {
                            if ($folder_list_start === false) {
                                echo "<ul>\n";
                                $folder_list_start = true;
                            }
                            echo "<li>";
                            if (!session::logged_in() || ($thread['LAST_READ'] == 0 || $thread['LAST_READ'] < $thread['LENGTH']) && $thread['MODIFIED'] > $thread_unread_cutoff) {
                                $new_posts = $thread['LENGTH'] - $thread['LAST_READ'];
                                if ($new_posts == $thread['LENGTH']) {
                                    $number = "[{$thread['LENGTH']}&nbsp;new]";
                                } else {
                                    $number = "[{$new_posts}&nbsp;new&nbsp;of&nbsp;{$thread['LENGTH']}]";
                                }
                                $latest_post = $thread['LAST_READ'] + 1;
                            } else {
                                $number = "[{$thread['LENGTH']}]";
                                $latest_post = 1;
                            }
                            // work out how long ago the thread was posted and format the time to display
                            $thread_time = format_date_time($thread['MODIFIED']);
                            echo "<span class=\"thread_icon\">", html_style_image('bullet'), "</span>";
                            echo "<span class=\"thread_title\">";
                            echo "<a href=\"lmessages.php?webtag={$webtag}&amp;msg={$thread['TID']}.{$latest_post}\" ";
                            echo "title=\"", sprintf(gettext("Thread #%s Started by %s. Viewed %s"), $thread['TID'], word_filter_add_ob_tags(format_user_name($thread['LOGON'], $thread['NICKNAME']), true), $thread['VIEWCOUNT'] == 1 ? gettext("1 time") : sprintf(gettext("%d times"), $thread['VIEWCOUNT'])), "\">";
                            echo word_filter_add_ob_tags($thread['TITLE'], true), "</a> ";
                            echo "<span class=\"thread_detail\">";
                            if (isset($thread['INTEREST']) && $thread['INTEREST'] == THREAD_INTERESTED) {
                                echo html_style_image('high_interest', gettext("High Interest"));
                            }
                            if (isset($thread['INTEREST']) && $thread['INTEREST'] == THREAD_SUBSCRIBED) {
                                echo html_style_image('subscribe', gettext("Subscribed"));
                            }
                            if (isset($thread['POLL_FLAG']) && $thread['POLL_FLAG'] == 'Y') {
                                echo html_style_image('poll', gettext("Poll"));
                            }
                            if (isset($thread['STICKY']) && $thread['STICKY'] == 'Y') {
                                echo html_style_image('sticky', gettext("Sticky"));
                            }
                            if (isset($thread['RELATIONSHIP']) && $thread['RELATIONSHIP'] & USER_FRIEND) {
                                echo html_style_image('friend', gettext("Friend"));
                            }
                            if (isset($thread['TRACK_TYPE']) && $thread['TRACK_TYPE'] == THREAD_TYPE_SPLIT) {
                                echo html_style_image('split_thread', gettext("Thread has been split"));
                            }
                            if (isset($thread['TRACK_TYPE']) && $thread['TRACK_TYPE'] == THREAD_TYPE_MERGE) {
                                echo html_style_image('merge_thread', gettext("Thread has been merged"));
                            }
                            if (isset($thread['ATTACHMENT_COUNT']) && $thread['ATTACHMENT_COUNT'] > 0) {
                                echo html_style_image('attach', gettext("Attachment"));
                            }
                            echo "<span class=\"thread_length\">{$number}</span>";
                            echo "</span>";
                            echo "</span>";
                            echo "<span class=\"thread_time\">{$thread_time}</span>";
                            echo "</li>\n";
                        }
                    }
                    if ($folder_list_end === false && $folder_list_start === true) {
                        echo "</ul>\n";
                    }
                    if (is_numeric($folder) && $folder_number == $folder && $thread_count >= 50) {
                        echo "<div class=\"folder_pagination\"><a href=\"lthread_list.php?webtag={$webtag}&amp;mode={$mode}&amp;folder={$folder}&amp;page=", $page + 1, "\">", gettext("Next 50 threads"), "</a></div>\n";
                    }
                } else {
                    if ($folder_info[$folder_number]['INTEREST'] != FOLDER_IGNORED) {
                        echo "<div class=\"folder_info\"><a href=\"lthread_list.php?webtag={$webtag}&amp;mode={$mode}&amp;folder={$folder_number}\">";
                        if (isset($folder_msgs[$folder_number])) {
                            echo $folder_msgs[$folder_number];
                        } else {
                            echo "0";
                        }
                        echo " ", gettext("threads"), "</a>";
                        if ($folder_info[$folder_number]['ALLOWED_TYPES'] & FOLDER_ALLOW_NORMAL_THREAD) {
                            echo "<span><a href=\"lpost.php?webtag={$webtag}&amp;fid={$folder_number}\">", gettext("Post New"), "</a></span>";
                        }
                        echo "</div>\n";
                    }
                }
            }
            echo "  </div>\n";
            echo "</div>\n";
            if (is_array($thread_info)) {
                reset($thread_info);
            }
        }
    }
    if (!is_numeric($folder) && $thread_count >= 50) {
        echo "<div class=\"thread_pagination\"><a href=\"lthread_list.php?webtag={$webtag}&amp;mode={$mode}&amp;page=", $page + 1, "\">", gettext("Next 50 threads"), "</a></div>\n";
    }
    if (session::logged_in()) {
        echo "<div id=\"thread_mark_read\">\n";
        echo "<h3>", gettext("Mark as Read"), "</h3>\n";
        echo "<form accept-charset=\"utf-8\" name=\"f_mark\" method=\"post\" action=\"lthread_list.php\">\n";
        echo "  ", form_csrf_token_field(), "\n";
        echo form_input_hidden("webtag", htmlentities_array($webtag)), "\n";
        echo form_input_hidden("mode", htmlentities_array($mode)), "\n";
        echo form_input_hidden("page", htmlentities_array($page)), "\n";
        echo form_input_hidden("mark_read_confirm", 'N'), "\n";
        $labels = array(gettext("All Discussions"), gettext("Next 50 discussions"));
        $selected_option = THREAD_MARK_READ_ALL;
        if (sizeof($visible_threads_array) > 0) {
            $labels[] = gettext("Visible discussions");
            $selected_option = THREAD_MARK_READ_VISIBLE;
            $visible_threads = implode(',', array_filter($visible_threads_array, 'is_numeric'));
            echo form_input_hidden("mark_read_threads", htmlentities_array($visible_threads)), "\n";
        }
        if (isset($_GET['folder']) && is_numeric($_GET['folder'])) {
            echo form_input_hidden('folder', htmlentities_array($folder)), "\n";
            $labels[] = gettext("Selected folder");
            $selected_option = THREAD_MARK_READ_FOLDER;
        }
        echo "<ul>\n";
        echo "<li>", light_form_dropdown_array("mark_read_type", $labels, $selected_option), "</li>\n";
        echo "<li class=\"right_col\">", light_form_submit("mark_read_submit", gettext("Go!")), "</li>\n";
        echo "</ul>\n";
        echo "</form>\n";
        echo "</div>\n";
    }
}
if ($page > 1 && !is_numeric($folder)) {
    echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
    echo "  <tr>\n";
    echo "    <td align=\"left\" valign=\"top\" colspan=\"2\">", html_style_image('current_thread'), "&nbsp;<a href=\"thread_list.php?webtag={$webtag}&amp;mode={$mode}&amp;page=", $page - 1, "\" title=\"", gettext("Show previous 50 threads"), "\">", gettext("Previous 50 threads"), "</a></td>\n";
    echo "  </tr>\n";
    echo "  <tr>\n";
    echo "    <td align=\"left\">&nbsp;</td>\n";
    echo "  </tr>\n";
    echo "</table>\n";
}
// Array to track visible threads for mark as read
$visible_threads_array = array();
// Variable to track first thread
$first_thread = false;
// Unread cut-off
$thread_unread_cutoff = threads_get_unread_cutoff();
// Iterate through the information we've just got and display it in the right order
foreach ($folder_order as $folder_number) {
    if (isset($folder_info[$folder_number]) && is_array($folder_info[$folder_number])) {
        echo "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">\n";
        echo "  <tr>\n";
        //Added break to allow space between thread list sections and show more background. Members feature request.
        echo "    <td align=\"left\" colspan=\"2\"><br />\n";
        echo "      <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
        echo "        <tr>\n";
        echo "          <td align=\"left\" valign=\"top\" class=\"foldername\">\n";
        //Added table to include rss and mod list icons in with foldername, allows one smooth border to surround them all. Members feature Request.
        echo "      <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
        echo "        <tr>\n";
        echo "          <td>\n";
        if (session::logged_in() && $folder_info[$folder_number]['INTEREST'] == FOLDER_SUBSCRIBED) {