query('SELECT topic_id, posted FROM '.$db->prefix.'posts WHERE id='.$pid) or error('Unable to fetch topic ID', __FILE__, __LINE__, $db->error()); if (!$db->num_rows($result)) message($lang_common['Bad request'], false, '404 Not Found'); list($id, $posted) = $db->fetch_row($result); // Determine on which page the post is located (depending on $forum_user['disp_posts']) $result = $db->query('SELECT COUNT(id) FROM '.$db->prefix.'posts WHERE topic_id='.$id.' AND posted<'.$posted) or error('Unable to count previous posts', __FILE__, __LINE__, $db->error()); $num_posts = $db->result($result) + 1; $_GET['p'] = ceil($num_posts / $pun_user['disp_posts']); } else { // If action=new, we redirect to the first new post (if any) if ($action == 'new') { if (!$pun_user['is_guest']) { // We need to check if this topic has been viewed recently by the user $tracked_topics = get_tracked_topics(); $last_viewed = isset($tracked_topics['topics'][$id]) ? $tracked_topics['topics'][$id] : $pun_user['last_visit']; $result = $db->query('SELECT MIN(id) FROM '.$db->prefix.'posts WHERE topic_id='.$id.' AND posted>'.$last_viewed) or error('Unable to fetch first new post info', __FILE__, __LINE__, $db->error()); $first_new_post_id = $db->result($result); if ($first_new_post_id) { header('Location: viewtopic.php?pid='.$first_new_post_id.'#p'.$first_new_post_id); exit; } } // If there is no new post, we go to the last post $action = 'last'; } // If action=last, we redirect to the last post if ($action == 'last') { $result = $db->query('SELECT MAX(id) FROM '.$db->prefix.'posts WHERE topic_id='.$id) or error('Unable to fetch last post info', __FILE__, __LINE__, $db->error()); $last_post_id = $db->result($result); if ($last_post_id) { header('Location: viewtopic.php?pid='.$last_post_id.'#p'.$last_post_id); exit; } } } // Fetch some info about the topic if (!$pun_user['is_guest']) $result = $db->query('SELECT t.subject, t.closed, t.num_replies, t.sticky, t.first_post_id, f.id AS forum_id, f.forum_name, f.moderators, fp.post_replies, s.user_id AS is_subscribed FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'topic_subscriptions AS s ON (t.id=s.topic_id AND s.user_id='.$pun_user['id'].') LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.id='.$id.' AND t.moved_to IS NULL') or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error()); else $result = $db->query('SELECT t.subject, t.closed, t.num_replies, t.sticky, t.first_post_id, f.id AS forum_id, f.forum_name, f.moderators, fp.post_replies, 0 AS is_subscribed FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.id='.$id.' AND t.moved_to IS NULL') or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error()); if (!$db->num_rows($result)) message($lang_common['Bad request'], false, '404 Not Found'); $cur_topic = $db->fetch_assoc($result); // Sort out who the moderators are and if we are currently a moderator (or an admin) $mods_array = ($cur_topic['moderators'] != '') ? unserialize($cur_topic['moderators']) : array(); $is_admmod = ($pun_user['g_id'] == PUN_ADMIN || ($pun_user['g_moderator'] == '1' && array_key_exists($pun_user['username'], $mods_array))) ? true : false; if ($is_admmod) $admin_ids = get_admin_ids(); // Can we or can we not post replies? if ($cur_topic['closed'] == '0') { if (($cur_topic['post_replies'] == '' && $pun_user['g_post_replies'] == '1') || $cur_topic['post_replies'] == '1' || $is_admmod) $post_link = "\t\t\t".'
'."\n"; else $post_link = ''; } else { $post_link = $lang_topic['Topic closed']; if ($is_admmod) $post_link .= ' / '.$lang_topic['Post reply'].''; $post_link = "\t\t\t".''.$post_link.'
'."\n"; } // Add/update this topic in our list of tracked topics if (!$pun_user['is_guest']) { $tracked_topics = get_tracked_topics(); $tracked_topics['topics'][$id] = time(); set_tracked_topics($tracked_topics); } // Determine the post offset (based on $_GET['p']) $num_pages = ceil(($cur_topic['num_replies'] + 1) / $pun_user['disp_posts']); $p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : intval($_GET['p']); $start_from = $pun_user['disp_posts'] * ($p - 1); // Generate paging links $paging_links = ''.$lang_common['Pages'].' '.paginate($num_pages, $p, 'viewtopic.php?id='.$id); if ($pun_config['o_censoring'] == '1') $cur_topic['subject'] = censor_words($cur_topic['subject']); $quickpost = false; if ($pun_config['o_quickpost'] == '1' && ($cur_topic['post_replies'] == '1' || ($cur_topic['post_replies'] == '' && $pun_user['g_post_replies'] == '1')) && ($cur_topic['closed'] == '0' || $is_admmod)) { // Load the post.php language file require PUN_ROOT.'lang/'.$pun_user['language'].'/post.php'; $required_fields = array('req_message' => $lang_common['Message']); if ($pun_user['is_guest']) { $required_fields['req_username'] = $lang_post['Guest name']; if ($pun_config['p_force_guest_email'] == '1') $required_fields['req_email'] = $lang_common['Email']; } $quickpost = true; } if (!$pun_user['is_guest'] && $pun_config['o_topic_subscriptions'] == '1') { $token_url = '&csrf_token='.pun_csrf_token(); if ($cur_topic['is_subscribed']) // I apologize for the variable naming here. It's a mix of subscription and action I guess :-) $subscraction = "\t\t".''.$lang_topic['Is subscribed'].' - '.$lang_topic['Unsubscribe'].'
'."\n"; else $subscraction = "\t\t".''."\n"; } else $subscraction = ''; // Add relationship meta tags $page_head = array(); $page_head['canonical'] = ''; if ($num_pages > 1) { if ($p > 1) $page_head['prev'] = ''; if ($p < $num_pages) $page_head['next'] = ''; } if ($pun_config['o_feed_type'] == '1') $page_head['feed'] = ''; else if ($pun_config['o_feed_type'] == '2') $page_head['feed'] = ''; $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), pun_htmlspecialchars($cur_topic['forum_name']), pun_htmlspecialchars($cur_topic['subject'])); define('PUN_ALLOW_INDEX', 1); define('PUN_ACTIVE_PAGE', 'index'); require PUN_ROOT.'header.php'; ?> query('SELECT id FROM '.$db->prefix.'posts WHERE topic_id='.$id.' ORDER BY id LIMIT '.$start_from.','.$pun_user['disp_posts']) or error('Unable to fetch post IDs', __FILE__, __LINE__, $db->error()); $post_ids = array(); for ($i = 0;$cur_post_id = $db->result($result, $i);$i++) $post_ids[] = $cur_post_id; if (empty($post_ids)) error('The post table and topic table seem to be out of sync!', __FILE__, __LINE__); // Retrieve the posts (and their respective poster/online status) $result = $db->query('SELECT u.email, u.title, u.url, u.location, u.signature, u.email_setting, u.num_posts, u.registered, u.admin_note, p.id, p.poster AS username, p.poster_id, p.poster_ip, p.poster_email, p.message, p.hide_smilies, p.posted, p.edited, p.edited_by, g.g_id, g.g_user_title, g.g_promote_next_group, o.user_id AS is_online FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'users AS u ON u.id=p.poster_id INNER JOIN '.$db->prefix.'groups AS g ON g.g_id=u.group_id LEFT JOIN '.$db->prefix.'online AS o ON (o.user_id=u.id AND o.user_id!=1 AND o.idle=0) WHERE p.id IN ('.implode(',', $post_ids).') ORDER BY p.id', true) or error('Unable to fetch post info', __FILE__, __LINE__, $db->error()); while ($cur_post = $db->fetch_assoc($result)) { $post_count++; $user_avatar = ''; $user_info = array(); $user_contacts = array(); $post_actions = array(); $is_online = ''; $signature = ''; // If the poster is a registered user if ($cur_post['poster_id'] > 1) { if ($pun_user['g_view_users'] == '1') $username = ''.pun_htmlspecialchars($cur_post['username']).''; else $username = pun_htmlspecialchars($cur_post['username']); $user_title = get_title($cur_post); if ($pun_config['o_censoring'] == '1') $user_title = censor_words($user_title); // Format the online indicator $is_online = ($cur_post['is_online'] == $cur_post['poster_id']) ? ''.$lang_topic['Online'].'' : ''.$lang_topic['Offline'].''; if ($pun_config['o_avatars'] == '1' && $pun_user['show_avatars'] != '0') { if (isset($user_avatar_cache[$cur_post['poster_id']])) $user_avatar = $user_avatar_cache[$cur_post['poster_id']]; else $user_avatar = $user_avatar_cache[$cur_post['poster_id']] = generate_avatar_markup($cur_post['poster_id']); } // We only show location, register date, post count and the contact links if "Show user info" is enabled if ($pun_config['o_show_user_info'] == '1') { if ($cur_post['location'] != '') { if ($pun_config['o_censoring'] == '1') $cur_post['location'] = censor_words($cur_post['location']); $user_info[] = ''.$is_online.'
'; ?>