25 // |
25 // |
26 // Cookie stuff! |
26 // Cookie stuff! |
27 // |
27 // |
28 function check_cookie(&$pun_user) |
28 function check_cookie(&$pun_user) |
29 { |
29 { |
30 global $db, $db_type, $pun_config, $cookie_name, $cookie_seed; |
30 // Import Enano |
31 |
31 global $db, $session, $paths, $template, $plugins; // Common objects |
32 $now = time(); |
32 |
33 $expire = $now + 31536000; // The cookie expires after a year |
33 // Import PunBB |
34 |
34 global $pun_db, $db_type, $pun_config, $cookie_name, $cookie_seed; |
35 // We assume it's a guest |
35 |
36 $cookie = array('user_id' => 1, 'password_hash' => 'Guest'); |
36 $now = time(); |
37 |
37 |
38 // If a cookie is set, we get the user_id and password hash from it |
38 if(!$session->started) |
39 if (isset($_COOKIE[$cookie_name])) |
39 $session->start(); |
40 list($cookie['user_id'], $cookie['password_hash']) = @unserialize($_COOKIE[$cookie_name]); |
40 |
41 |
41 if($session->user_logged_in) |
42 if ($cookie['user_id'] > 1) |
42 { |
43 { |
43 $result = $pun_db->query( |
44 // Check if there's a user with the user ID and password hash from the cookie |
44 'SELECT eu.*, u.*, eu.real_name AS realname, eu.user_level AS g_id, g.*, o.logged, o.idle |
45 $result = $db->query('SELECT u.*, g.*, o.logged, o.idle FROM '.$db->prefix.'users AS u INNER JOIN '.$db->prefix.'groups AS g ON u.group_id=g.g_id LEFT JOIN '.$db->prefix.'online AS o ON o.user_id=u.id WHERE u.id='.intval($cookie['user_id'])) or error('Unable to fetch user information', __FILE__, __LINE__, $db->error()); |
45 FROM '.$pun_db->prefix.'users AS u |
46 $pun_user = $db->fetch_assoc($result); |
46 LEFT JOIN '.table_prefix.'users AS eu |
47 |
47 ON eu.user_id=u.id |
48 // If user authorisation failed |
48 INNER JOIN '.$pun_db->prefix.'groups AS g |
49 if (!isset($pun_user['id']) || md5($cookie_seed.$pun_user['password']) !== $cookie['password_hash']) |
49 ON u.group_id=g.g_id |
50 { |
50 LEFT JOIN '.$pun_db->prefix.'online AS o |
51 pun_setcookie(0, random_pass(8), $expire); |
51 ON o.user_id=u.id |
52 set_default_user(); |
52 WHERE u.id='.intval($session->user_id)) |
53 |
53 or error('Unable to fetch user information', __FILE__, __LINE__, $pun_db->error()); |
54 return; |
54 $pun_user = $pun_db->fetch_assoc($result); |
55 } |
55 // Set a default language if the user selected language no longer exists |
56 |
|
57 // Set a default language if the user selected language no longer exists |
|
58 if (!@file_exists(PUN_ROOT.'lang/'.$pun_user['language'])) |
56 if (!@file_exists(PUN_ROOT.'lang/'.$pun_user['language'])) |
59 $pun_user['language'] = $pun_config['o_default_lang']; |
57 $pun_user['language'] = $pun_config['o_default_lang']; |
60 |
58 |
61 // Set a default style if the user selected style no longer exists |
59 // Set a default style if the user selected style no longer exists |
62 if (!@file_exists(PUN_ROOT.'style/'.$pun_user['style'].'.css')) |
60 // if (!@file_exists(PUN_ROOT.'style/'.$pun_user['style'].'.css')) |
63 $pun_user['style'] = $pun_config['o_default_style']; |
61 // $pun_user['style'] = $pun_config['o_default_style']; |
64 |
62 |
65 if (!$pun_user['disp_topics']) |
63 if (!$pun_user['disp_topics']) |
66 $pun_user['disp_topics'] = $pun_config['o_disp_topics_default']; |
64 $pun_user['disp_topics'] = $pun_config['o_disp_topics_default']; |
67 if (!$pun_user['disp_posts']) |
65 if (!$pun_user['disp_posts']) |
68 $pun_user['disp_posts'] = $pun_config['o_disp_posts_default']; |
66 $pun_user['disp_posts'] = $pun_config['o_disp_posts_default']; |
73 // Define this if you want this visit to affect the online list and the users last visit data |
71 // Define this if you want this visit to affect the online list and the users last visit data |
74 if (!defined('PUN_QUIET_VISIT')) |
72 if (!defined('PUN_QUIET_VISIT')) |
75 { |
73 { |
76 // Update the online list |
74 // Update the online list |
77 if (!$pun_user['logged']) |
75 if (!$pun_user['logged']) |
78 { |
76 $pun_db->query('INSERT INTO '.$pun_db->prefix.'online (user_id, ident, logged) VALUES('.$pun_user['id'].', \''.$pun_db->escape($pun_user['username']).'\', '.$now.')') or error('Unable to insert into online list', __FILE__, __LINE__, $pun_db->error()); |
79 $pun_user['logged'] = $now; |
|
80 |
|
81 // With MySQL/MySQLi, REPLACE INTO avoids a user having two rows in the online table |
|
82 switch ($db_type) |
|
83 { |
|
84 case 'mysql': |
|
85 case 'mysqli': |
|
86 $db->query('REPLACE INTO '.$db->prefix.'online (user_id, ident, logged) VALUES('.$pun_user['id'].', \''.$db->escape($pun_user['username']).'\', '.$pun_user['logged'].')') or error('Unable to insert into online list', __FILE__, __LINE__, $db->error()); |
|
87 break; |
|
88 |
|
89 default: |
|
90 $db->query('INSERT INTO '.$db->prefix.'online (user_id, ident, logged) VALUES('.$pun_user['id'].', \''.$db->escape($pun_user['username']).'\', '.$pun_user['logged'].')') or error('Unable to insert into online list', __FILE__, __LINE__, $db->error()); |
|
91 break; |
|
92 } |
|
93 } |
|
94 else |
77 else |
95 { |
78 { |
96 // Special case: We've timed out, but no other user has browsed the forums since we timed out |
79 // Special case: We've timed out, but no other user has browsed the forums since we timed out |
97 if ($pun_user['logged'] < ($now-$pun_config['o_timeout_visit'])) |
80 if ($pun_user['logged'] < ($now-$pun_config['o_timeout_visit'])) |
98 { |
81 { |
99 $db->query('UPDATE '.$db->prefix.'users SET last_visit='.$pun_user['logged'].' WHERE id='.$pun_user['id']) or error('Unable to update user visit data', __FILE__, __LINE__, $db->error()); |
82 $pun_db->query('UPDATE '.$pun_db->prefix.'users SET last_visit='.$pun_user['logged'].' WHERE id='.$pun_user['id']) or error('Unable to update user visit data', __FILE__, __LINE__, $pun_db->error()); |
100 $pun_user['last_visit'] = $pun_user['logged']; |
83 $pun_user['last_visit'] = $pun_user['logged']; |
101 } |
84 } |
102 |
85 |
103 $idle_sql = ($pun_user['idle'] == '1') ? ', idle=0' : ''; |
86 $idle_sql = ($pun_user['idle'] == '1') ? ', idle=0' : ''; |
104 $db->query('UPDATE '.$db->prefix.'online SET logged='.$now.$idle_sql.' WHERE user_id='.$pun_user['id']) or error('Unable to update online list', __FILE__, __LINE__, $db->error()); |
87 $pun_db->query('UPDATE '.$pun_db->prefix.'online SET logged='.$now.$idle_sql.' WHERE user_id='.$pun_user['id']) or error('Unable to update online list', __FILE__, __LINE__, $pun_db->error()); |
105 } |
88 } |
106 } |
89 } |
107 |
90 |
108 $pun_user['is_guest'] = false; |
91 $pun_user['is_guest'] = false; |
109 } |
92 } |
110 else |
93 else |
111 set_default_user(); |
94 { |
|
95 set_default_user(); |
|
96 } |
112 } |
97 } |
113 |
98 |
114 |
99 |
115 // |
100 // |
116 // Fill $pun_user with default values (for guests) |
101 // Fill $pun_user with default values (for guests) |
117 // |
102 // |
118 function set_default_user() |
103 function set_default_user() |
119 { |
104 { |
120 global $db, $db_type, $pun_user, $pun_config; |
105 global $pun_db, $pun_user, $pun_config; |
121 |
106 |
122 $remote_addr = get_remote_address(); |
107 $remote_addr = get_remote_address(); |
123 |
108 |
124 // Fetch guest user |
109 // Fetch guest user |
125 $result = $db->query('SELECT u.*, g.*, o.logged FROM '.$db->prefix.'users AS u INNER JOIN '.$db->prefix.'groups AS g ON u.group_id=g.g_id LEFT JOIN '.$db->prefix.'online AS o ON o.ident=\''.$remote_addr.'\' WHERE u.id=1') or error('Unable to fetch guest information', __FILE__, __LINE__, $db->error()); |
110 $result = $pun_db->query('SELECT u.*, g.*, o.logged FROM '.$pun_db->prefix.'users AS u INNER JOIN '.$pun_db->prefix.'groups AS g ON u.group_id=g.g_id LEFT JOIN '.$pun_db->prefix.'online AS o ON o.ident=\''.$remote_addr.'\' WHERE u.id=1') or error('Unable to fetch guest information', __FILE__, __LINE__, $pun_db->error()); |
126 if (!$db->num_rows($result)) |
111 if (!$pun_db->num_rows($result)) |
127 exit('Unable to fetch guest information. The table \''.$db->prefix.'users\' must contain an entry with id = 1 that represents anonymous users.'); |
112 exit('Unable to fetch guest information. The table \''.$pun_db->prefix.'users\' must contain an entry with id = 1 that represents anonymous users.'); |
128 |
113 |
129 $pun_user = $db->fetch_assoc($result); |
114 $pun_user = $pun_db->fetch_assoc($result); |
130 |
115 |
131 // Update online list |
116 // Update online list |
132 if (!$pun_user['logged']) |
117 if (!$pun_user['logged']) |
133 { |
118 $pun_db->query('INSERT INTO '.$pun_db->prefix.'online (user_id, ident, logged) VALUES(1, \''.$pun_db->escape($remote_addr).'\', '.time().')') or error('Unable to insert into online list', __FILE__, __LINE__, $pun_db->error()); |
134 $pun_user['logged'] = time(); |
|
135 |
|
136 // With MySQL/MySQLi, REPLACE INTO avoids a user having two rows in the online table |
|
137 switch ($db_type) |
|
138 { |
|
139 case 'mysql': |
|
140 case 'mysqli': |
|
141 $db->query('REPLACE INTO '.$db->prefix.'online (user_id, ident, logged) VALUES(1, \''.$db->escape($remote_addr).'\', '.$pun_user['logged'].')') or error('Unable to insert into online list', __FILE__, __LINE__, $db->error()); |
|
142 break; |
|
143 |
|
144 default: |
|
145 $db->query('INSERT INTO '.$db->prefix.'online (user_id, ident, logged) VALUES(1, \''.$db->escape($remote_addr).'\', '.$pun_user['logged'].')') or error('Unable to insert into online list', __FILE__, __LINE__, $db->error()); |
|
146 break; |
|
147 } |
|
148 } |
|
149 else |
119 else |
150 $db->query('UPDATE '.$db->prefix.'online SET logged='.time().' WHERE ident=\''.$db->escape($remote_addr).'\'') or error('Unable to update online list', __FILE__, __LINE__, $db->error()); |
120 $pun_db->query('UPDATE '.$pun_db->prefix.'online SET logged='.time().' WHERE ident=\''.$pun_db->escape($remote_addr).'\'') or error('Unable to update online list', __FILE__, __LINE__, $pun_db->error()); |
151 |
121 |
152 $pun_user['disp_topics'] = $pun_config['o_disp_topics_default']; |
122 $pun_user['disp_topics'] = $pun_config['o_disp_topics_default']; |
153 $pun_user['disp_posts'] = $pun_config['o_disp_posts_default']; |
123 $pun_user['disp_posts'] = $pun_config['o_disp_posts_default']; |
154 $pun_user['timezone'] = $pun_config['o_server_timezone']; |
124 $pun_user['timezone'] = $pun_config['o_server_timezone']; |
155 $pun_user['language'] = $pun_config['o_default_lang']; |
125 $pun_user['language'] = $pun_config['o_default_lang']; |
235 // |
205 // |
236 // Update "Users online" |
206 // Update "Users online" |
237 // |
207 // |
238 function update_users_online() |
208 function update_users_online() |
239 { |
209 { |
240 global $db, $pun_config, $pun_user; |
210 global $pun_db, $pun_config, $pun_user; |
241 |
211 |
242 $now = time(); |
212 $now = time(); |
243 |
213 |
244 // Fetch all online list entries that are older than "o_timeout_online" |
214 // Fetch all online list entries that are older than "o_timeout_online" |
245 $result = $db->query('SELECT * FROM '.$db->prefix.'online WHERE logged<'.($now-$pun_config['o_timeout_online'])) or error('Unable to fetch old entries from online list', __FILE__, __LINE__, $db->error()); |
215 $result = $pun_db->query('SELECT * FROM '.$pun_db->prefix.'online WHERE logged<'.($now-$pun_config['o_timeout_online'])) or error('Unable to fetch old entries from online list', __FILE__, __LINE__, $pun_db->error()); |
246 while ($cur_user = $db->fetch_assoc($result)) |
216 while ($cur_user = $pun_db->fetch_assoc($result)) |
247 { |
217 { |
248 // If the entry is a guest, delete it |
218 // If the entry is a guest, delete it |
249 if ($cur_user['user_id'] == '1') |
219 if ($cur_user['user_id'] == '1') |
250 $db->query('DELETE FROM '.$db->prefix.'online WHERE ident=\''.$db->escape($cur_user['ident']).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $db->error()); |
220 $pun_db->query('DELETE FROM '.$pun_db->prefix.'online WHERE ident=\''.$pun_db->escape($cur_user['ident']).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $pun_db->error()); |
251 else |
221 else |
252 { |
222 { |
253 // If the entry is older than "o_timeout_visit", update last_visit for the user in question, then delete him/her from the online list |
223 // If the entry is older than "o_timeout_visit", update last_visit for the user in question, then delete him/her from the online list |
254 if ($cur_user['logged'] < ($now-$pun_config['o_timeout_visit'])) |
224 if ($cur_user['logged'] < ($now-$pun_config['o_timeout_visit'])) |
255 { |
225 { |
256 $db->query('UPDATE '.$db->prefix.'users SET last_visit='.$cur_user['logged'].' WHERE id='.$cur_user['user_id']) or error('Unable to update user visit data', __FILE__, __LINE__, $db->error()); |
226 $pun_db->query('UPDATE '.$pun_db->prefix.'users SET last_visit='.$cur_user['logged'].' WHERE id='.$cur_user['user_id']) or error('Unable to update user visit data', __FILE__, __LINE__, $pun_db->error()); |
257 $db->query('DELETE FROM '.$db->prefix.'online WHERE user_id='.$cur_user['user_id']) or error('Unable to delete from online list', __FILE__, __LINE__, $db->error()); |
227 $pun_db->query('DELETE FROM '.$pun_db->prefix.'online WHERE user_id='.$cur_user['user_id']) or error('Unable to delete from online list', __FILE__, __LINE__, $pun_db->error()); |
258 } |
228 } |
259 else if ($cur_user['idle'] == '0') |
229 else if ($cur_user['idle'] == '0') |
260 $db->query('UPDATE '.$db->prefix.'online SET idle=1 WHERE user_id='.$cur_user['user_id']) or error('Unable to insert into online list', __FILE__, __LINE__, $db->error()); |
230 $pun_db->query('UPDATE '.$pun_db->prefix.'online SET idle=1 WHERE user_id='.$cur_user['user_id']) or error('Unable to insert into online list', __FILE__, __LINE__, $pun_db->error()); |
261 } |
231 } |
262 } |
232 } |
263 } |
233 } |
264 |
|
265 |
234 |
266 // |
235 // |
267 // Generate the "navigator" that appears at the top of every page |
236 // Generate the "navigator" that appears at the top of every page |
268 // |
237 // |
269 function generate_navlinks() |
238 function generate_navlinks() |
354 // |
323 // |
355 // Update posts, topics, last_post, last_post_id and last_poster for a forum (redirect topics are not included) |
324 // Update posts, topics, last_post, last_post_id and last_poster for a forum (redirect topics are not included) |
356 // |
325 // |
357 function update_forum($forum_id) |
326 function update_forum($forum_id) |
358 { |
327 { |
359 global $db; |
328 global $pun_db; |
360 |
329 |
361 $result = $db->query('SELECT COUNT(id), SUM(num_replies) FROM '.$db->prefix.'topics WHERE moved_to IS NULL AND forum_id='.$forum_id) or error('Unable to fetch forum topic count', __FILE__, __LINE__, $db->error()); |
330 $result = $pun_db->query('SELECT COUNT(id), SUM(num_replies) FROM '.$pun_db->prefix.'topics WHERE moved_to IS NULL AND forum_id='.$forum_id) or error('Unable to fetch forum topic count', __FILE__, __LINE__, $pun_db->error()); |
362 list($num_topics, $num_posts) = $db->fetch_row($result); |
331 list($num_topics, $num_posts) = $pun_db->fetch_row($result); |
363 |
332 |
364 $num_posts = $num_posts + $num_topics; // $num_posts is only the sum of all replies (we have to add the topic posts) |
333 $num_posts = $num_posts + $num_topics; // $num_posts is only the sum of all replies (we have to add the topic posts) |
365 |
334 |
366 $result = $db->query('SELECT last_post, last_post_id, last_poster FROM '.$db->prefix.'topics WHERE forum_id='.$forum_id.' AND moved_to IS NULL ORDER BY last_post DESC LIMIT 1') or error('Unable to fetch last_post/last_post_id/last_poster', __FILE__, __LINE__, $db->error()); |
335 $result = $pun_db->query('SELECT last_post, last_post_id, last_poster FROM '.$pun_db->prefix.'topics WHERE forum_id='.$forum_id.' AND moved_to IS NULL ORDER BY last_post DESC LIMIT 1') or error('Unable to fetch last_post/last_post_id/last_poster', __FILE__, __LINE__, $pun_db->error()); |
367 if ($db->num_rows($result)) // There are topics in the forum |
336 if ($pun_db->num_rows($result)) // There are topics in the forum |
368 { |
337 { |
369 list($last_post, $last_post_id, $last_poster) = $db->fetch_row($result); |
338 list($last_post, $last_post_id, $last_poster) = $pun_db->fetch_row($result); |
370 |
339 |
371 $db->query('UPDATE '.$db->prefix.'forums SET num_topics='.$num_topics.', num_posts='.$num_posts.', last_post='.$last_post.', last_post_id='.$last_post_id.', last_poster=\''.$db->escape($last_poster).'\' WHERE id='.$forum_id) or error('Unable to update last_post/last_post_id/last_poster', __FILE__, __LINE__, $db->error()); |
340 $pun_db->query('UPDATE '.$pun_db->prefix.'forums SET num_topics='.$num_topics.', num_posts='.$num_posts.', last_post='.$last_post.', last_post_id='.$last_post_id.', last_poster=\''.$pun_db->escape($last_poster).'\' WHERE id='.$forum_id) or error('Unable to update last_post/last_post_id/last_poster', __FILE__, __LINE__, $pun_db->error()); |
372 } |
341 } |
373 else // There are no topics |
342 else // There are no topics |
374 $db->query('UPDATE '.$db->prefix.'forums SET num_topics=0, num_posts=0, last_post=NULL, last_post_id=NULL, last_poster=NULL WHERE id='.$forum_id) or error('Unable to update last_post/last_post_id/last_poster', __FILE__, __LINE__, $db->error()); |
343 $pun_db->query('UPDATE '.$pun_db->prefix.'forums SET num_topics=0, num_posts=0, last_post=NULL, last_post_id=NULL, last_poster=NULL WHERE id='.$forum_id) or error('Unable to update last_post/last_post_id/last_poster', __FILE__, __LINE__, $pun_db->error()); |
375 } |
344 } |
376 |
345 |
377 |
346 |
378 // |
347 // |
379 // Delete a topic and all of it's posts |
348 // Delete a topic and all of it's posts |
380 // |
349 // |
381 function delete_topic($topic_id) |
350 function delete_topic($topic_id) |
382 { |
351 { |
383 global $db; |
352 global $pun_db; |
384 |
353 |
385 // Delete the topic and any redirect topics |
354 // Delete the topic and any redirect topics |
386 $db->query('DELETE FROM '.$db->prefix.'topics WHERE id='.$topic_id.' OR moved_to='.$topic_id) or error('Unable to delete topic', __FILE__, __LINE__, $db->error()); |
355 $pun_db->query('DELETE FROM '.$pun_db->prefix.'topics WHERE id='.$topic_id.' OR moved_to='.$topic_id) or error('Unable to delete topic', __FILE__, __LINE__, $pun_db->error()); |
387 |
356 |
388 // Create a list of the post ID's in this topic |
357 // Create a list of the post ID's in this topic |
389 $post_ids = ''; |
358 $post_ids = ''; |
390 $result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to fetch posts', __FILE__, __LINE__, $db->error()); |
359 $result = $pun_db->query('SELECT id FROM '.$pun_db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to fetch posts', __FILE__, __LINE__, $pun_db->error()); |
391 while ($row = $db->fetch_row($result)) |
360 while ($row = $pun_db->fetch_row($result)) |
392 $post_ids .= ($post_ids != '') ? ','.$row[0] : $row[0]; |
361 $post_ids .= ($post_ids != '') ? ','.$row[0] : $row[0]; |
393 |
362 |
394 // Make sure we have a list of post ID's |
363 // Make sure we have a list of post ID's |
395 if ($post_ids != '') |
364 if ($post_ids != '') |
396 { |
365 { |
397 strip_search_index($post_ids); |
366 strip_search_index($post_ids); |
398 |
367 |
399 // Delete posts in topic |
368 // Delete posts in topic |
400 $db->query('DELETE FROM '.$db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to delete posts', __FILE__, __LINE__, $db->error()); |
369 $pun_db->query('DELETE FROM '.$pun_db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to delete posts', __FILE__, __LINE__, $pun_db->error()); |
401 } |
370 } |
402 |
371 |
403 // Delete any subscriptions for this topic |
372 // Delete any subscriptions for this topic |
404 $db->query('DELETE FROM '.$db->prefix.'subscriptions WHERE topic_id='.$topic_id) or error('Unable to delete subscriptions', __FILE__, __LINE__, $db->error()); |
373 $pun_db->query('DELETE FROM '.$pun_db->prefix.'subscriptions WHERE topic_id='.$topic_id) or error('Unable to delete subscriptions', __FILE__, __LINE__, $pun_db->error()); |
405 } |
374 } |
406 |
375 |
407 |
376 |
408 // |
377 // |
409 // Delete a single post |
378 // Delete a single post |
410 // |
379 // |
411 function delete_post($post_id, $topic_id) |
380 function delete_post($post_id, $topic_id) |
412 { |
381 { |
413 global $db; |
382 global $pun_db; |
414 |
383 |
415 $result = $db->query('SELECT id, poster, posted FROM '.$db->prefix.'posts WHERE topic_id='.$topic_id.' ORDER BY id DESC LIMIT 2') or error('Unable to fetch post info', __FILE__, __LINE__, $db->error()); |
384 $result = $pun_db->query('SELECT id, poster, posted FROM '.$pun_db->prefix.'posts WHERE topic_id='.$topic_id.' ORDER BY id DESC LIMIT 2') or error('Unable to fetch post info', __FILE__, __LINE__, $pun_db->error()); |
416 list($last_id, ,) = $db->fetch_row($result); |
385 list($last_id, ,) = $pun_db->fetch_row($result); |
417 list($second_last_id, $second_poster, $second_posted) = $db->fetch_row($result); |
386 list($second_last_id, $second_poster, $second_posted) = $pun_db->fetch_row($result); |
418 |
387 |
419 // Delete the post |
388 // Delete the post |
420 $db->query('DELETE FROM '.$db->prefix.'posts WHERE id='.$post_id) or error('Unable to delete post', __FILE__, __LINE__, $db->error()); |
389 $pun_db->query('DELETE FROM '.$pun_db->prefix.'posts WHERE id='.$post_id) or error('Unable to delete post', __FILE__, __LINE__, $pun_db->error()); |
421 |
390 |
422 strip_search_index($post_id); |
391 strip_search_index($post_id); |
423 |
392 |
424 // Count number of replies in the topic |
393 // Count number of replies in the topic |
425 $result = $db->query('SELECT COUNT(id) FROM '.$db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to fetch post count for topic', __FILE__, __LINE__, $db->error()); |
394 $result = $pun_db->query('SELECT COUNT(id) FROM '.$pun_db->prefix.'posts WHERE topic_id='.$topic_id) or error('Unable to fetch post count for topic', __FILE__, __LINE__, $pun_db->error()); |
426 $num_replies = $db->result($result, 0) - 1; |
395 $num_replies = $pun_db->result($result, 0) - 1; |
427 |
396 |
428 // If the message we deleted is the most recent in the topic (at the end of the topic) |
397 // If the message we deleted is the most recent in the topic (at the end of the topic) |
429 if ($last_id == $post_id) |
398 if ($last_id == $post_id) |
430 { |
399 { |
431 // If there is a $second_last_id there is more than 1 reply to the topic |
400 // If there is a $second_last_id there is more than 1 reply to the topic |
432 if (!empty($second_last_id)) |
401 if (!empty($second_last_id)) |
433 $db->query('UPDATE '.$db->prefix.'topics SET last_post='.$second_posted.', last_post_id='.$second_last_id.', last_poster=\''.$db->escape($second_poster).'\', num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $db->error()); |
402 $pun_db->query('UPDATE '.$pun_db->prefix.'topics SET last_post='.$second_posted.', last_post_id='.$second_last_id.', last_poster=\''.$pun_db->escape($second_poster).'\', num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $pun_db->error()); |
434 else |
403 else |
435 // We deleted the only reply, so now last_post/last_post_id/last_poster is posted/id/poster from the topic itself |
404 // We deleted the only reply, so now last_post/last_post_id/last_poster is posted/id/poster from the topic itself |
436 $db->query('UPDATE '.$db->prefix.'topics SET last_post=posted, last_post_id=id, last_poster=poster, num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $db->error()); |
405 $pun_db->query('UPDATE '.$pun_db->prefix.'topics SET last_post=posted, last_post_id=id, last_poster=poster, num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $pun_db->error()); |
437 } |
406 } |
438 else |
407 else |
439 // Otherwise we just decrement the reply counter |
408 // Otherwise we just decrement the reply counter |
440 $db->query('UPDATE '.$db->prefix.'topics SET num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $db->error()); |
409 $pun_db->query('UPDATE '.$pun_db->prefix.'topics SET num_replies='.$num_replies.' WHERE id='.$topic_id) or error('Unable to update topic', __FILE__, __LINE__, $pun_db->error()); |
441 } |
410 } |
442 |
411 |
443 |
412 |
444 // |
413 // |
445 // Replace censored words in $text |
414 // Replace censored words in $text |
446 // |
415 // |
447 function censor_words($text) |
416 function censor_words($text) |
448 { |
417 { |
449 global $db; |
418 global $pun_db; |
450 static $search_for, $replace_with; |
419 static $search_for, $replace_with; |
451 |
420 |
452 // If not already built in a previous call, build an array of censor words and their replacement text |
421 // If not already built in a previous call, build an array of censor words and their replacement text |
453 if (!isset($search_for)) |
422 if (!isset($search_for)) |
454 { |
423 { |
455 $result = $db->query('SELECT search_for, replace_with FROM '.$db->prefix.'censoring') or error('Unable to fetch censor word list', __FILE__, __LINE__, $db->error()); |
424 $result = $pun_db->query('SELECT search_for, replace_with FROM '.$pun_db->prefix.'censoring') or error('Unable to fetch censor word list', __FILE__, __LINE__, $pun_db->error()); |
456 $num_words = $db->num_rows($result); |
425 $num_words = $pun_db->num_rows($result); |
457 |
426 |
458 $search_for = array(); |
427 $search_for = array(); |
459 for ($i = 0; $i < $num_words; ++$i) |
428 for ($i = 0; $i < $num_words; ++$i) |
460 { |
429 { |
461 list($search_for[$i], $replace_with[$i]) = $db->fetch_row($result); |
430 list($search_for[$i], $replace_with[$i]) = $pun_db->fetch_row($result); |
462 $search_for[$i] = '/\b('.str_replace('\*', '\w*?', preg_quote($search_for[$i], '/')).')\b/i'; |
431 $search_for[$i] = '/\b('.str_replace('\*', '\w*?', preg_quote($search_for[$i], '/')).')\b/i'; |
463 } |
432 } |
464 } |
433 } |
465 |
434 |
466 if (!empty($search_for)) |
435 if (!empty($search_for)) |