############################################ 
## Mod Title:   'View Newest Post'-Fix for phpBB 2.0.x 
## Mod Version: 1.0.2 
## Author:   Acyd Burn < acyd.burn@gmx.de > - Meik Sievertsen - http://www.opentools.de 
## 
## Description: 
##      This Mod is more a 'bugfix' for phpBB 2.0.x. 
## 
##      At some circumstances, the Newest Post Icon in Forums View (if you have all topics listed) 
##      does not work properly. 
##      For example, if you have visited a thread before and haven't logged out 
##      of the board, another person writes a reply and the thread becomes this little orange icon again. 
##      But when you click on it now, it brings you to the post where you have looked before. 
## 
##      With this Mod the 'View Newest' Feature will work the correct way. ;) 
## 
## Revision History: 
## 
##      2002-11-12 - 1.0.2 - redirect to last post if no new posts there 
##      2002-08-13 - 1.0.1 - updated to phpBB 2.0.x 
##      2002-03-23 - 1.0.0 - initial version - named Viewtopic Post Tracking 
## 
## Installation Level:  easy 
## Installation Time:   1-5 Minutes 
## Files To Edit:       viewtopic.php 
## Included Files:      0 
## 
############################################ 
# 
#-----[ Open viewtopic.php ]--------------------------------------------------- 
# 

# 
#-----[ FIND ]---------- around line 69 ------------------------------------ 
# 
            $sql = "SELECT p.post_id 
               FROM " . POSTS_TABLE . " p, " . SESSIONS_TABLE . " s,  " . USERS_TABLE . " u 
               WHERE s.session_id = '$session_id' 
                  AND u.user_id = s.session_user_id 
                  AND p.topic_id = $topic_id 
                  AND p.post_time >= u.user_lastvisit 
               ORDER BY p.post_time ASC 
               LIMIT 1"; 
            if ( !($result = $db->sql_query($sql)) ) 
            { 
               message_die(GENERAL_ERROR, 'Could not obtain newer/older topic information', '', __LINE__, __FILE__, $sql); 
            } 

            if ( !($row = $db->sql_fetchrow($result)) ) 
            { 
               message_die(GENERAL_MESSAGE, 'No_new_posts_last_visit'); 
            } 

# 
#-----[ REPLACE WITH ]---------------------------------------------------------------- 
# 
            $tracking_topics = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) : array(); 
            $tracking_forums = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) : array(); 

            if ( !empty($tracking_topics[$topic_id]) && !empty($tracking_forums[$forum_id]) ) 
            { 
               $topic_last_read = ( $tracking_topics[$topic_id] > $tracking_forums[$forum_id] ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id]; 
            } 
            else if ( !empty($tracking_topics[$topic_id]) || !empty($tracking_forums[$forum_id]) ) 
            { 
               $topic_last_read = ( !empty($tracking_topics[$topic_id]) ) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id]; 
            } 
            else 
            { 
               $topic_last_read = 'u.user_lastvisit'; 
            } 

            $sql = "SELECT p.post_id, p.post_time 
            FROM " . POSTS_TABLE . " p, " . SESSIONS_TABLE . " s,  " . USERS_TABLE . " u 
            WHERE s.session_id = '$session_id' 
            AND u.user_id = s.session_user_id 
            AND p.topic_id = " . intval($topic_id) . " 
            AND p.post_time >= " . $topic_last_read . " 
            LIMIT 1"; 

            if ( !($result = $db->sql_query($sql)) ) 
            { 
               message_die(GENERAL_ERROR, 'Could not obtain newer/older topic information', '', __LINE__, __FILE__, $sql); 
            } 

            if ( !($row = $db->sql_fetchrow($result)) ) 
            { 
               $sql = "SELECT topic_last_post_id as post_id FROM " . TOPICS_TABLE . " WHERE topic_id = " . intval($topic_id); 
               if ( !($result = $db->sql_query($sql)) ) 
               { 
                  message_die(GENERAL_ERROR, 'Could not obtain newer/older topic information', '', __LINE__, __FILE__, $sql); 
               } 

               if ( !($row = $db->sql_fetchrow($result)) ) 
               { 
                  message_die(GENERAL_MESSAGE, 'No_new_posts_last_visit'); 
               } 
            } 

# 
#-----[ Save and Close viewtopic.php ]----------------------------------------- 
#