function sp_update_users_newposts()
{
    global $spThisUser;
    # Check the users checktime against the last post timestamp to see if we need to do this
    $checkTime = spdb_zone_mysql_checkdate($spThisUser->checktime);
    $postTime = sp_get_option('poststamp');
    if (strtotime($checkTime) > strtotime($postTime) && !isset($_GET['mark-read'])) {
        return;
    }
    # so there must have been a new post since the last page load for this user
    $newPostList = $spThisUser->newposts;
    if (empty($newPostList['topics'])) {
        # clean it up to be on the safe side
        unset($newPostList);
        $newPostList = array();
        $newPostList['topics'] = array();
        $newPostList['forums'] = array();
    }
    # create new holding array and new checktime (now)
    $addPostList = array();
    $addPostList['topics'] = array();
    $addPostList['forums'] = array();
    sp_set_server_timezone();
    $newCheckTime = sp_apply_timezone(time(), 'mysql');
    # Use the current checktime for any new posts since users session began
    $records = spdb_select('set', "SELECT DISTINCT topic_id, forum_id FROM " . SFPOSTS . "\n\t\t\t\t\t\t\t\t   WHERE post_status = 0 AND post_date > '" . $checkTime . "' AND user_id != " . $spThisUser->ID . "\n\t\t\t\t\t\t\t\t   ORDER BY post_id DESC LIMIT " . $spThisUser->unreadposts . ";", ARRAY_A);
    if ($records) {
        foreach ($records as $r) {
            if (sp_get_auth('view_forum', $r['forum_id']) && !in_array($r['topic_id'], $newPostList['topics'])) {
                $addPostList['topics'][] = $r['topic_id'];
                $addPostList['forums'][] = $r['forum_id'];
            }
        }
    }
    $addPostList = apply_filters('sph_new_post_list', $addPostList, $newPostList);
    # now merge the arrays and truncate if necessary
    $newPostList['topics'] = array_merge($addPostList['topics'], $newPostList['topics']);
    $newPostList['forums'] = array_merge($addPostList['forums'], $newPostList['forums']);
    if (count($newPostList['topics']) > $spThisUser->unreadposts) {
        array_splice($newPostList['topics'], $spThisUser->unreadposts);
        array_splice($newPostList['forums'], $spThisUser->unreadposts);
    }
    # update sfmembers - do it here to ensure both are updated together
    spdb_query("UPDATE " . SFMEMBERS . " SET newposts='" . serialize($newPostList) . "', checktime='" . $newCheckTime . "' WHERE user_id=" . $spThisUser->ID);
    $spThisUser->newpostlist = true;
    $spThisUser->checktime = $newCheckTime;
    $spThisUser->newposts = $newPostList;
}
    function sp_listview_populate_newposts($topicIds)
    {
        global $spThisUser;
        $newList = array();
        # First filter topics by those in the users new post list
        $newTopicIds = array();
        foreach ($topicIds as $topic) {
            if (sp_is_in_users_newposts($topic)) {
                $newTopicIds[] = $topic;
            }
        }
        if ($newTopicIds) {
            # construct the query - need to add in sfwaiting for admins
            $where = SFPOSTS . '.topic_id IN (' . implode(',', $newTopicIds) . ') AND (post_date > "' . spdb_zone_mysql_checkdate($spThisUser->lastvisit) . '")';
            if ($spThisUser->admin || $spThisUser->moderator) {
                $wPosts = spdb_select('col', 'SELECT post_id FROM ' . SFWAITING);
                if ($wPosts) {
                    $where .= ' OR (' . SFPOSTS . '.post_id IN (' . implode(",", $wPosts) . '))';
                }
            }
            $spdb = new spdbComplex();
            $spdb->table = SFPOSTS;
            $spdb->fields = SFPOSTS . '.topic_id, ' . SFPOSTS . '.post_id, post_index, ' . spdb_zone_datetime('post_date') . ',
									guest_name, ' . SFPOSTS . '.user_id, display_name, post_count-post_index+1 AS new_post_count';
            $spdb->left_join = array(SFMEMBERS . ' ON ' . SFMEMBERS . '.user_id = ' . SFPOSTS . '.user_id');
            $spdb->join = array(SFTOPICS . ' ON ' . SFPOSTS . '.topic_id = ' . SFTOPICS . '.topic_id');
            $spdb->where = $where;
            $spdb->orderby = 'topic_id, post_id';
            $spdb = apply_filters('sph_listview_newposts_query', $spdb, $this);
            $postrecords = $spdb->select();
            if ($postrecords) {
                $cTopic = 0;
                foreach ($postrecords as $p) {
                    if ($p->topic_id != $cTopic) {
                        $cTopic = $p->topic_id;
                        $newList[$cTopic] = new stdClass();
                        $newList[$cTopic]->topic_id = $cTopic;
                        $newList[$cTopic]->new_post_count = $p->new_post_count;
                        $newList[$cTopic]->new_post_post_id = $p->post_id;
                        $newList[$cTopic]->new_post_post_index = $p->post_index;
                        $newList[$cTopic]->new_post_post_date = $p->post_date;
                        $newList[$cTopic]->new_post_user_id = $p->user_id;
                        $newList[$cTopic]->new_post_display_name = sp_filter_name_display($p->display_name);
                        $newList[$cTopic]->new_post_guest_name = sp_filter_name_display($p->guest_name);
                    }
                }
            }
        }
        return $newList;
    }