Lighttpd conf file added for lighttpd webservers
[atbbs.git] / topic.php
blob2b4df9a0c045beb914a299bbea3cdfb6499a00d1
1 <?php
3 require('includes/header.php');
5 // Validate / fetch topic info.
6 if( ! ctype_digit($_GET['id']))
8 add_error('Invalid ID.', true);
11 if(ALLOW_IMAGES)
13 $stmt = $link->prepare('SELECT topics.time, topics.author, topics.visits, topics.replies, topics.headline, topics.body, topics.edit_time, topics.edit_mod, images.file_name FROM topics LEFT OUTER JOIN images ON topics.id = images.topic_id WHERE topics.id = ?');
15 else
17 $stmt = $link->prepare('SELECT time, author, visits, replies, headline, body, edit_time, edit_mod FROM topics WHERE id = ?');
19 $stmt->bind_param('i', $_GET['id']);
21 $stmt->execute();
23 $stmt->store_result();
24 if($stmt->num_rows < 1)
26 $page_title = 'Non-existent topic';
27 update_activity('nonexistent_topic');
28 add_error('There is no such topic. It may have been deleted.', true);
31 if(ALLOW_IMAGES)
33 $stmt->bind_result($topic_time, $topic_author, $topic_visits, $topic_replies, $topic_headline, $topic_body, $topic_edit_time, $topic_edit_mod, $topic_image_name);
35 else
37 $stmt->bind_result($topic_time, $topic_author, $topic_visits, $topic_replies, $topic_headline, $topic_body, $topic_edit_time, $topic_edit_mod);
39 $stmt->fetch();
40 $stmt->close();
42 update_activity('topic', $_GET['id']);
44 $page_title = 'Topic: ' . htmlspecialchars($topic_headline);
46 // Increment visit count.
47 if( ! isset($_SESSION['visited_topics'][$_GET['id']]) && isset($_COOKIE['SID']))
49 $_SESSION['visited_topics'][$_GET['id']] = 1;
51 $increment_visits = $link->prepare('UPDATE topics SET visits = visits + 1 WHERE id = ?');
52 $increment_visits->bind_param('i', $_GET['id']);
53 $increment_visits->execute();
54 $increment_visits->close();
57 // Set visited cookie...
58 $last_read_post = $visited_topics[$_GET['id']];
59 if($last_read_post !== $topic_replies)
61 // Build cookie.
62 // Add the current topic:
63 $visited_topics = array( $_GET['id'] => $topic_replies) + $visited_topics;
64 // Readd old topics.
65 foreach($visited_topics as $cur_topic_id => $num_replies)
67 // If the cookie is getting too long (4kb), stop.
68 if(strlen($cookie_string) > 3900)
70 break;
73 $cookie_string .= 't' . $cur_topic_id . 'n' . $num_replies;
76 setcookie('topic_visits', $cookie_string, $_SERVER['REQUEST_TIME'] + 604800, '/');
79 // If ostrich mode is enabled, fetch a list of blacklisted phrases.
80 $ignored_phrases = fetch_ignore_list();
82 // Output dummy form. (This is for JavaScript submissions to action.php.)
83 dummy_form();
85 // Output OP.
86 echo '<h3>';
87 if($topic_author == 'admin')
89 echo '<strong>' . ADMIN_NAME . '</strong> ';
91 else
93 if($moderator || $administrator)
95 echo '<a href="/profile/' . $topic_author . '">';
97 echo 'Anonymous <strong>A</strong>';
98 if($moderator || $administrator)
100 echo '</a>';
102 echo ' ';
104 if($topic_author == $_SESSION['UID'])
106 echo '(you) ';
108 echo 'started this discussion <strong><span class="help" title="' . format_date($topic_time) . '">' . calculate_age($topic_time) . ' ago</span> <span class="reply_id unimportant"><a href="/topic/' . $_GET['id'] . '">#' . number_format($_GET['id']) . '</a></span></strong></h3> <div class="body">';
110 if($topic_image_name)
112 echo '<a href="/img/' . htmlspecialchars($topic_image_name) . '"><img src="/thumbs/' . htmlspecialchars($topic_image_name) . '" alt="" /></a>';
115 echo parse($topic_body);
117 edited_message($topic_time, $topic_edit_time, $topic_edit_mod);
119 echo '<ul class="menu">';
123 $topic_author == $_SESSION['UID'] && TIME_TO_EDIT == 0 ||
124 $topic_author == $_SESSION['UID'] && ( $_SERVER['REQUEST_TIME'] - $topic_time < TIME_TO_EDIT ) ||
125 $moderator || $administrator
128 echo '<li><a href="/edit_topic/' . $_GET['id'] . '">Edit</a></li>';
131 if($moderator || $administrator)
133 echo '<li><a href="/delete_topic/' . $_GET['id'] . '" onclick="return submitDummyForm(\'/delete_topic/' . $_GET['id'] . '\', \'id\', ' . $_GET['id'] . ', \'Really delete this topic?\');">Delete</a></li>';
135 echo '<li><a href="/watch_topic/' . $_GET['id'] . '" onclick="return submitDummyForm(\'/watch_topic/' . $_GET['id'] . '\', \'id\', ' . $_GET['id'] . ', \'Really watch this topic?\');">Watch</a></li> <li><a href="/new_reply/' . $_GET['id'] . '/quote_topic">Quote</a></li><li><a href="/trivia_for_topic/' . $_GET['id'] . '" class="help" title="' . $topic_replies . ' repl' . ($topic_replies == 1 ? 'y' : 'ies') . '">' . $topic_visits . ' visit' . ($topic_visits == 1 ? '' : 's') . '</a></li></ul></div>';
137 // Output replies.
138 if(ALLOW_IMAGES)
140 $stmt = $link->prepare('SELECT replies.id, replies.time, replies.author, replies.poster_number, replies.body, replies.edit_time, replies.edit_mod, images.file_name FROM replies LEFT OUTER JOIN images ON replies.id = images.reply_id WHERE replies.parent_id = ? ORDER BY id');
142 else
144 $stmt = $link->prepare('SELECT id, time, author, poster_number, body, edit_time, edit_mod FROM replies WHERE parent_id = ? ORDER BY id');
146 $stmt->bind_param('i', $_GET['id']);
147 $stmt->execute();
148 if(ALLOW_IMAGES)
150 $stmt->bind_result($reply_id, $reply_time, $reply_author, $reply_poster_number, $reply_body, $reply_edit_time, $reply_edit_mod, $reply_image_name);
152 else
154 $stmt->bind_result($reply_id, $reply_time, $reply_author, $reply_poster_number, $reply_body, $reply_edit_time, $reply_edit_mod);
157 $reply_ids = array();
158 $posters = array();
159 $hidden_replies = array(); // ostrich mode
160 $previous_poster = $topic_author;
161 $previous_post_time = $topic_time;
162 $posts_in_row = 0; // number of posts in a row by one UID
163 $tuple = array
165 1 => 'double',
166 2 => 'triple',
167 3 => 'quadruple',
168 4 => 'quintuple'
171 while($stmt->fetch())
173 // Should we even bother?
174 if($_COOKIE['ostrich_mode'] == 1)
176 foreach($ignored_phrases as $ignored_phrase)
178 if(stripos($reply_body, $ignored_phrase) !== false)
180 $hidden_replies[] = $reply_id;
181 $reply_ids[$reply_id] = array
183 'body' => $reply_body,
184 'author' => $reply_author
186 // We've encountered an ignored phrase, so skip the rest of this while() iteration.
187 continue 2;
192 // We should!
193 $out = array(); // output variables
195 if($reply_author == 'admin')
197 $out['author'] = '<strong>' . ADMIN_NAME . '</strong>';
199 else
201 if($moderator || $administrator)
203 $out['author'] = '<a href="/profile/' . $reply_author . '">';
206 $out['author'] .= 'Anonymous <strong>';
207 if($reply_author == $topic_author)
209 $out['author'] .= 'A';
211 else
213 $out['author'] .= number_to_letter($reply_poster_number);
215 $out['author'] .= '</strong>';
217 if($moderator || $administrator)
219 $out['author'] .= '</a>';
223 if($reply_author == $topic_author)
225 $out['author_desc'] = '(OP';
226 if($reply_author == $_SESSION['UID'])
228 $out['author_desc'] .= ', you';
230 $out['author_desc'] .= ')';
232 else
234 if($reply_author == $_SESSION['UID'])
236 $out['author_desc'] .= '(you)';
238 if( ! in_array($reply_author, $posters))
240 $out['action'] = 'joined in and ';
244 if($reply_author == $previous_poster && $posts_in_row < 4)
246 $posts_in_row++;
247 $out['action'] .= $tuple[$posts_in_row] . '-posted';
249 else
251 $posts_in_row = 0;
252 $out['action'] .= 'replied with';
255 // Now, output the reply.
256 echo '<h3 id="reply_' . $reply_id . '">';
258 // If this is the newest unread post, let the #new anchor highlight it.
259 if(count($reply_ids) == $last_read_post)
261 echo '<span id="new"></span><input type="hidden" id="new_id" class="noscreen" value="' . $reply_id . '" />';
264 // The content of the header:
265 echo $out['author'] . ' ' . $out['author_desc'] . ' ' . $out['action'] . ' this <strong><span class="help" title="' . format_date($reply_time) . '">' . calculate_age($reply_time) . ' ago</span></strong>, ' . calculate_age($reply_time, $previous_post_time) . ' later';
266 if( ! empty($posters)) //if not first reply
268 echo ', ' . calculate_age($reply_time, $topic_time) . ' after the original post';
271 // Finish the header and begin outputting the body.
272 echo '<span class="reply_id unimportant"><a href="#reply_' . $reply_id . '" onclick="highlightReply(\'' . $reply_id . '\'); removeSnapbackLink">#' . number_format($reply_id) . '</a></span></h3> <div class="body" id="reply_box_' . $reply_id . '">';
273 if($reply_image_name)
275 echo '<a href="/img/' . htmlspecialchars($reply_image_name) . '"><img src="/thumbs/' . htmlspecialchars($reply_image_name) . '" alt="" /></a>';
278 $reply_body_parsed = parse($reply_body);
280 // Linkify citations. (This might be updated to use preg_replace_callback in the future.)
281 preg_match_all('/^@([0-9,]+)/m', $reply_body_parsed, $matches);
282 foreach($matches[0] as $formatted_id)
284 $you = '';
286 $pure_id = str_replace(array('@', ','), '', $formatted_id);
287 if(!array_key_exists($pure_id, $reply_ids))
289 $reply_body_parsed = str_replace($formatted_id, '<span class="unimportant">(Citing a deleted or non-existent reply.)</span>', $reply_body_parsed);
291 else if(in_array($pure_id, $hidden_replies))
293 $reply_body_parsed = str_replace($formatted_id, '<span class="unimportant help" title="' . snippet($reply_ids[$pure_id]['body']) . '">@hidden</span>', $reply_body_parsed);
295 else
297 if($pure_id == $previous_id)
299 $link_text = '@previous';
301 else
303 $link_text = $formatted_id;
306 if($reply_ids[$pure_id]['author'] == $_SESSION['UID'])
308 $you = '<span class="unimportant"> (you)</span>';
311 $reply_body_parsed = str_replace($formatted_id, '<a href="#reply_' . $pure_id . '" onclick="highlightReply(\'' . $pure_id . '\'); createSnapbackLink(\'' . $reply_id . '\')" class="unimportant help" title="' . snippet($reply_ids[$pure_id]['body']) . '">' . $link_text . '</a>' . $you, $reply_body_parsed);
314 $reply_body_parsed = preg_replace('/^@OP/', '<span class="unimportant">@OP</span>', $reply_body_parsed);
316 echo $reply_body_parsed;
318 edited_message($reply_time, $reply_edit_time, $reply_edit_mod);
320 echo '<ul class="menu">';
324 $reply_author == $_SESSION['UID'] && TIME_TO_EDIT == 0 ||
325 $reply_author == $_SESSION['UID'] && ( $_SERVER['REQUEST_TIME'] - $reply_time < TIME_TO_EDIT ) ||
326 $moderator || $administrator
329 echo '<li><a href="/edit_reply/' . $_GET['id'] . '/' . $reply_id . '">Edit</a></li>';
332 if($moderator || $administrator)
334 echo '<li><a href="/delete_reply/' . $reply_id . '" onclick="return submitDummyForm(\'/delete_reply/' . $reply_id . '\', \'id\', ' . $reply_id . ', \'Really delete this reply?\');">Delete</a></li>';
336 echo '<li><a href="/new_reply/' . $_GET['id'] . '/quote_reply/' . $reply_id . '">Quote</a></li><li><a href="/new_reply/' . $_GET['id'] . '/cite_reply/' . $reply_id . '">Cite</a></li></ul></div>';
338 // Store information for the next round.
339 $reply_ids[$reply_id] = array
341 'body' => $reply_body,
342 'author' => $reply_author
344 $posters[] = $reply_author;
345 $previous_poster = $reply_author;
346 $previous_id = $reply_id;
347 $previous_post_time = $reply_time;
349 $stmt->close();
351 echo '<ul class="menu"><li><a href="/new_reply/' . $_GET['id'] . '">New reply</a></li></ul>';
353 require('includes/footer.php');