# HG changeset patch # User Dan # Date 1192672347 14400 # Node ID 253118325c65543c051a9dc9bfca392a57329179 # Parent 6f8b7c6fac02b4dcd937d68b11297b12739d98ea Pagination on topics and a whole crapload of other stuff. diff -r 6f8b7c6fac02 -r 253118325c65 decir/functions.php --- a/decir/functions.php Wed Oct 17 20:23:51 2007 -0400 +++ b/decir/functions.php Wed Oct 17 21:52:27 2007 -0400 @@ -13,15 +13,6 @@ */ /** - * Prints out breadcrumbs. - */ - -function decir_breadcrumbs() -{ - // placeholder -} - -/** * Inserts a post in reply to a topic. Does NOT check any type of authorization at all. * @param int Topic ID * @param string Post subject @@ -117,10 +108,12 @@ $post_text = bbcode_inject_uid($message, $bbcode_uid); $post_text = $db->escape($post_text); - $q = $db->sql_query('UPDATE '.table_prefix."decir_posts SET edit_count = edit_count + 1, edit_reason='$edit_reason', post_subject='$post_subject', last_edited_by=$last_edited_by WHERE post_id=$post_id;"); + // grace period: if the user is editing his/her own post 10 minutes or less after they originally submitted it, don't mark it as edited + $grace = time() - ( 10 * 60 ); + $q = $db->sql_query('UPDATE '.table_prefix."decir_posts SET post_subject='$post_subject', edit_count = edit_count + 1, edit_reason='$edit_reason', last_edited_by=$last_edited_by WHERE post_id=$post_id AND timestamp < $grace;"); if ( !$q ) $db->_die('Decir functions.php in decir_edit_post()'); - + $q = $db->sql_query('UPDATE '.table_prefix."decir_posts_text SET post_text='$post_text' WHERE post_id=$post_id;"); if ( !$q ) $db->_die('Decir functions.php in decir_edit_post()'); @@ -169,7 +162,7 @@ if ( $row['post_id'] == $post_id ) { // first post in the thread - return decir_delete_topic($topic_id, $del_reason); + return decir_delete_topic($topic_id, $del_reason, $for_real); } $del_reason = $db->escape($del_reason); @@ -352,4 +345,47 @@ return false; } +/** + * Un-deletes a topic so that the public can see it. + * @param int Topic ID + */ + +function decir_restore_topic($topic_id) +{ + global $db, $session, $paths, $template, $plugins; // Common objects + + if ( !is_int($topic_id) ) + return false; + + // Obtain a list of posts in the topic + $q = $db->sql_query('SELECT post_id FROM '.table_prefix.'decir_posts WHERE topic_id = ' . $topic_id . ';'); + if ( !$q ) + $db->_die('Decir functions.php in decir_delete_topic()'); + if ( $db->numrows() < 1 ) + return false; + $posts = array(); + while ( $row = $db->fetchrow() ) + { + $posts[] = $row['post_id']; + } + + // Obtain forum ID + $q = $db->sql_query('SELECT forum_id FROM '.table_prefix."decir_topics WHERE topic_id = $topic_id;"); + if ( !$q ) + $db->_die('Decir functions.php in decir_restore_topic()'); + list($forum_id) = $db->fetchrow_num(); + $db->free_result(); + + $q = $db->sql_query('UPDATE ' . table_prefix . "decir_topics SET topic_deleted = 0, topic_deletor = NULL, topic_delete_reason = NULL WHERE topic_id = $topic_id;"); + + // Update forum stats + $post_count = count($posts); + $q = $db->sql_query('UPDATE '.table_prefix."decir_forums SET num_topics = num_topics + 1, num_posts = num_posts + $post_count WHERE forum_id = $forum_id;"); + if ( !$q ) + $db->_die('Decir functions.php in decir_restore_topic()'); + decir_update_forum_stats($forum_id); + + return true; +} + ?> diff -r 6f8b7c6fac02 -r 253118325c65 decir/functions_viewtopic.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/decir/functions_viewtopic.php Wed Oct 17 21:52:27 2007 -0400 @@ -0,0 +1,216 @@ + + + +
+ + + + + + + + + + + + + + + + +
+ This post was deleted by {LAST_EDITED_BY}.
+ Reason: {EDIT_REASON}
+ +
+
+ The deleted post is shown below: + +
+ {USER_LINK} +
Posted: {TIMESTAMP}
+ + + + + + + + + + + + + + + +
+ {POST_TEXT} +
+ + Last edited by {LAST_EDITED_BY}; edited {EDIT_COUNT} time{EDIT_COUNT_S} in total
+ Reason: {EDIT_REASON}
+ +
+ edit | delete | + quote +
+ + restore post | physically delete +
+
+ + + {USER_TITLE}
+
+ Joined: {REG_TIME} + +
+ + Online + + Offline + + +
+
+ + + '; + + var $parser; + + /** + * Constructor. Sets up parser object. + */ + + function __construct() + { + global $db, $session, $paths, $template, $plugins; // Common objects + $this->parser = $template->makeParserText($this->post_template); + } + + /** + * Renders a post. + * @todo document + * @access private + */ + + function _render($_, $row) + { + global $db, $session, $paths, $template, $plugins; // Common objects + global $whos_online; + + $poster_name = ( $row['poster_id'] == 1 ) ? $row['poster_name'] : $row['username']; + $datetime = date('F d, Y h:i a', $row['timestamp']); + $post_text = render_bbcode($row['post_text'], $row['bbcode_uid']); + $post_text = RenderMan::smilieyize($post_text); + $regtime = date('F Y', $row['reg_time']); + + $user_color = '#0000AA'; + switch ( $row['user_level'] ) + { + case USER_LEVEL_ADMIN: $user_color = '#AA0000'; break; + case USER_LEVEL_MOD: $user_color = '#00AA00'; break; + } + if ( $row['poster_id'] > 1 ) + { + $user_link = "$poster_name"; + } + else + { + $user_link = ''.$poster_name.''; + } + $quote_link = makeUrlNS('Special', 'Forum/New/Quote/' . $row['post_id'], false, true); + $edit_link = makeUrlNS('Special', 'Forum/Edit/' . $row['post_id'], false, true); + $delete_link = makeUrlNS('Special', 'Forum/Delete/' . $row['post_id'], false, true); + $restore_link = makeUrlNS('Special', 'Forum/Delete/' . $row['post_id'], 'act=restore', true); + $user_title = 'Anonymous user'; + switch ( $row['user_level'] ) + { + case USER_LEVEL_ADMIN: $user_title = 'Administrator'; break; + case USER_LEVEL_MOD: $user_title = 'Moderator'; break; + case USER_LEVEL_MEMBER:$user_title = 'Member'; break; + case USER_LEVEL_GUEST: $user_title = 'Guest'; break; + } + $leb_link = ''; + if ( $row['editor'] ) + { + $userpage_url = makeUrlNS('User', sanitize_page_id($row['editor']), false, true); + $row['editor'] = htmlspecialchars($row['editor']); + $leb_link = "{$row['editor']}"; + } + $this->parser->assign_vars(Array( + 'POST_ID' => (string)$row['post_id'], + 'USERNAME' => $poster_name, + 'USER_LINK' => $user_link, + 'REG_TIME' => $regtime, + 'TIMESTAMP' => $datetime, + 'POST_TEXT' => $post_text, + 'USER_TITLE' => $user_title, + 'QUOTE_LINK' => $quote_link, + 'EDIT_LINK' => $edit_link, + 'DELETE_LINK' => $delete_link, + 'RESTORE_LINK' => $restore_link, + 'EDIT_COUNT' => $row['edit_count'], + 'EDIT_COUNT_S' => ( $row['edit_count'] == 1 ? '' : 's' ), + 'LAST_EDITED_BY' => $leb_link, + 'EDIT_REASON' => htmlspecialchars($row['edit_reason']) + )); + // Decir can integrate with the Who's Online plugin + $who_support = $plugins->loaded('WhosOnline') && $row['user_level'] >= USER_LEVEL_GUEST; + $user_online = false; + if ( $who_support && in_array($row['username'], $whos_online['users']) ) + { + $user_online = true; + } + elseif ( $row['poster_id'] < 2 ) + { + $who_support = false; + } + $this->parser->assign_bool(Array( + 'whos_online_support' => $who_support, + 'user_is_online' => $user_online, + 'post_edited' => ( $row['edit_count'] > 0 ), + 'post_deleted' => ( $row['post_deleted'] == 1 ), + // FIXME: This should check something on ACLs + 'show_post' => ( $row['post_deleted'] != 1 || $session->user_level >= USER_LEVEL_MOD ) + )); + return $this->parser->run(); + } +} + +?> diff -r 6f8b7c6fac02 -r 253118325c65 decir/restoretopic.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/decir/restoretopic.php Wed Oct 17 21:52:27 2007 -0400 @@ -0,0 +1,82 @@ +getParam(1); +if ( strval(intval($tid)) !== $tid ) +{ + die_friendly('Error', '

Invalid topic ID

'); +} + +$tid = intval($tid); + +// Obtain topic info +$q = $db->sql_query('SELECT t.forum_id, t.topic_id, t.topic_deleted, t.topic_deletor, t.topic_delete_reason, u.username AS deletor FROM '.table_prefix.'decir_topics AS t + LEFT JOIN '.table_prefix.'users AS u + ON ( u.user_id = t.topic_deletor OR t.topic_deletor IS NULL ) + WHERE t.topic_id='.$tid.';'); +if ( !$q ) + $db->_die('Decir restoretopic.php'); + +if ( $db->numrows() < 1 ) +{ + die_friendly('Error', '

The topic you requested does not exist.

'); +} + +$row = $db->fetchrow(); +$db->free_result(); + +$tid = intval($row['topic_id']); + +// $acl_type = ( $row['poster_id'] == $session->user_id && $session->user_logged_in ) ? 'decir_edit_own' : 'decir_edit_other'; + +// FIXME: This will eventually use an ACL rule + +$post_perms = $session->fetch_page_acl(strval($pid), 'DecirPost'); +if ( $session->user_level < USER_LEVEL_MOD ) // ( !$post_perms->get_permissions($acl_type) ) +{ + die_friendly('Error', '

You do not have permission to restore this topic.

'); +} + +$edit_reason = ''; +if ( isset($_GET['act']) && $_GET['act'] == 'submit' ) +{ + if ( isset($_POST['do']['restore']) ) + { + $result = decir_restore_topic($tid); + if ( $result ) + { + $url = makeUrlNS('Special', 'Forum/Topic/' . $tid, false, true); + redirect($url, 'Topic restored', 'The selected topic has been restored.', 4); + } + } + else if ( isset($_POST['do']['noop']) ) + { + $url = makeUrlNS('Special', 'Forum/Topic/' . $tid, false, true); + redirect($url, '', '', 0); + } +} + +$template->header(true); +$form_submit_url = makeUrlNS('Special', 'Forum/RestoreTopic/' . $tid, 'act=submit', true); +?> +
+

Are you sure you want to restore this topic? If you do this, the public will be able to view it (providing that an access rule hasn't specified otherwise).

+

+
+footer(true); + +?> diff -r 6f8b7c6fac02 -r 253118325c65 decir/viewforum.php --- a/decir/viewforum.php Wed Oct 17 20:23:51 2007 -0400 +++ b/decir/viewforum.php Wed Oct 17 21:52:27 2007 -0400 @@ -36,7 +36,7 @@ $sort_dir = ( isset($_GET['sort_dir']) && in_array($_GET['sort_dir'], array('ASC', 'DESC')) ) ? $_GET['sort_dir'] : 'DESC'; $q = $db->sql_query('SELECT t.topic_id,t.topic_title,t.topic_type,t.topic_icon, - COUNT(h.hit_id) AS num_views,t.topic_starter AS starter_id, u.username AS topic_starter, + t.num_views,t.topic_starter AS starter_id, u.username AS topic_starter, p.poster_name AS last_post_name, p.timestamp AS last_post_time, t.topic_deleted, u2.username AS deletor, t.topic_delete_reason FROM '.table_prefix.'decir_topics AS t @@ -75,6 +75,8 @@ $i++; if ( $last_row['topic_id'] != $row['topic_id'] || $i == $db->numrows() ) { + if ( $num_replies < 0 ) + $num_replies = 0; if ( $last_row['topic_deleted'] == 1 ) { $thread_link = ''; diff -r 6f8b7c6fac02 -r 253118325c65 decir/viewtopic.php --- a/decir/viewtopic.php Wed Oct 17 20:23:51 2007 -0400 +++ b/decir/viewtopic.php Wed Oct 17 21:52:27 2007 -0400 @@ -14,11 +14,10 @@ require('common.php'); require('bbcode.php'); +require('functions_viewtopic.php'); global $whos_online; -$template->header(); - if ( strtolower($paths->getParam(0)) == 'post' || isset($_GET['pid']) ) { $pid = ( $n = $paths->getParam(1) ) ? $n : ( ( isset($_GET['pid']) ) ? $_GET['pid'] : 0 ); @@ -26,6 +25,7 @@ if(empty($pid)) { + $template->header(); echo '

Invalid topic ID

'; $template->footer(); return; @@ -46,13 +46,19 @@ if(empty($tid)) { + $template->header(); echo '

Invalid topic ID

'; $template->footer(); return; } } -$q = $db->sql_query('SELECT forum_id,topic_id FROM '.table_prefix.'decir_topics WHERE topic_id='.$tid.';'); +$q = $db->sql_query('SELECT t.forum_id, t.topic_title, f.forum_name, f.forum_id, t.topic_id, t.topic_deleted, t.topic_deletor, t.topic_delete_reason, u.username AS deletor FROM '.table_prefix.'decir_topics AS t + LEFT JOIN '.table_prefix.'users AS u + ON ( u.user_id = t.topic_deletor OR t.topic_deletor IS NULL ) + LEFT JOIN '.table_prefix.'decir_forums AS f + ON ( f.forum_id = t.forum_id ) + WHERE t.topic_id='.$tid.';'); if ( !$q ) $db->_die(); @@ -65,97 +71,57 @@ $forum_id = $row['forum_id']; $topic_id = $row['topic_id']; $topic_exists = true; + // FIXME: This will be controlled by an ACL rule + if ( $row['topic_deleted'] == 1 && $session->user_level < USER_LEVEL_MOD ) + { + $topic_exists = false; + } } else { $topic_exists = false; } -$post_template = << -
- - - - - - - - - - - - - - - - -
- This post was deleted by {LAST_EDITED_BY}.
- Reason: {EDIT_REASON}
- -
-
- The deleted post is shown below: - -
- {USER_LINK} -
Posted: {TIMESTAMP}
- - - - - - - - - - - - - - - -
- {POST_TEXT} -
- - Last edited by {LAST_EDITED_BY}; edited {EDIT_COUNT} time{EDIT_COUNT_S} in total
- Reason: {EDIT_REASON}
- -
- edit | delete | + quote -
- - restore post | physically delete -
-
- - - {USER_TITLE}
-
- Joined: {REG_TIME} - -
- - Online - - Offline - - -
-
-TPLCODE; +// Set page title +$template->tpl_strings['PAGE_NAME'] = htmlspecialchars($row['topic_title']) . ' « ' . htmlspecialchars($row['forum_name']) . ' « Forums'; + +$template->header(); + +// build breadcrumbs +echo ''; + +if ( $row['topic_deleted'] == 1 ) +{ + // User will at this point have permission to read the deleted topic (and thus restore it) + $restore_url = makeUrlNS('Special', 'Forum/RestoreTopic/' . $topic_id, false, true); + echo '
This topic was deleted by ' . htmlspecialchars($row['deletor']) . '. You can restore this topic.
+ Reason for deletion: ' . htmlspecialchars($row['topic_delete_reason']) . '
'; +} + +if ( !$topic_exists ) +{ + die_friendly('Error', '

The topic you requested does not exist.

'); +} + +$sql = 'SELECT COUNT(post_id) AS np FROM '.table_prefix."decir_posts WHERE topic_id=$tid;"; +$q = $db->sql_query($sql); +if ( !$q ) + $db->_die(); +list($num_posts) = $db->fetchrow_num(); +$db->free_result(); + +$offset = 0; +if ( $p = $paths->getParam(2) ) +{ + if ( preg_match('/^start=([0-9]+)$/', $p, $m) ) + { + $offset = intval($m[1]); + } +} $sql = 'SELECT p.post_id,p.poster_name,p.poster_id,u.username,p.timestamp,p.edit_count,p.last_edited_by,p.post_deleted,u2.username AS editor,p.edit_reason,u.user_level,u.reg_time,t.post_text,t.bbcode_uid FROM '.table_prefix.'decir_posts AS p LEFT JOIN '.table_prefix.'users AS u @@ -168,96 +134,26 @@ GROUP BY p.post_id ORDER BY p.timestamp ASC;'; -$q = $db->sql_query($sql); +$q = $db->sql_unbuffered_query($sql); if ( !$q ) $db->_die(); -if ( $db->numrows() < 1 ) -{ - die_friendly('Error', '

The topic you requested does not exist.

'); -} - -$parser = $template->makeParserText($post_template); +$formatter = new DecirPostbit(); -while ( $row = $db->fetchrow() ) -{ - $poster_name = ( $row['poster_id'] == 1 ) ? $row['poster_name'] : $row['username']; - $datetime = date('F d, Y h:i a', $row['timestamp']); - $post_text = render_bbcode($row['post_text'], $row['bbcode_uid']); - $post_text = RenderMan::smilieyize($post_text); - $regtime = date('F Y', $row['reg_time']); - - $user_color = '#0000AA'; - switch ( $row['user_level'] ) - { - case USER_LEVEL_ADMIN: $user_color = '#AA0000'; break; - case USER_LEVEL_MOD: $user_color = '#00AA00'; break; - } - if ( $row['poster_id'] > 1 ) - { - $user_link = "$poster_name"; - } - else - { - $user_link = ''.$poster_name.''; - } - $quote_link = makeUrlNS('Special', 'Forum/New/Quote/' . $row['post_id'], false, true); - $edit_link = makeUrlNS('Special', 'Forum/Edit/' . $row['post_id'], false, true); - $delete_link = makeUrlNS('Special', 'Forum/Delete/' . $row['post_id'], false, true); - $restore_link = makeUrlNS('Special', 'Forum/Delete/' . $row['post_id'], 'act=restore', true); - $user_title = 'Anonymous user'; - switch ( $row['user_level'] ) - { - case USER_LEVEL_ADMIN: $user_title = 'Administrator'; break; - case USER_LEVEL_MOD: $user_title = 'Moderator'; break; - case USER_LEVEL_MEMBER:$user_title = 'Member'; break; - case USER_LEVEL_GUEST: $user_title = 'Guest'; break; - } - $leb_link = ''; - if ( $row['editor'] ) - { - $userpage_url = makeUrlNS('User', sanitize_page_id($row['editor']), false, true); - $row['editor'] = htmlspecialchars($row['editor']); - $leb_link = "{$row['editor']}"; - } - $parser->assign_vars(Array( - 'POST_ID' => (string)$row['post_id'], - 'USERNAME' => $poster_name, - 'USER_LINK' => $user_link, - 'REG_TIME' => $regtime, - 'TIMESTAMP' => $datetime, - 'POST_TEXT' => $post_text, - 'USER_TITLE' => $user_title, - 'QUOTE_LINK' => $quote_link, - 'EDIT_LINK' => $edit_link, - 'DELETE_LINK' => $delete_link, - 'RESTORE_LINK' => $restore_link, - 'EDIT_COUNT' => $row['edit_count'], - 'EDIT_COUNT_S' => ( $row['edit_count'] == 1 ? '' : 's' ), - 'LAST_EDITED_BY' => $leb_link, - 'EDIT_REASON' => htmlspecialchars($row['edit_reason']) - )); - // Decir can integrate with the Who's Online plugin - $who_support = $plugins->loaded('WhosOnline') && $row['user_level'] >= USER_LEVEL_GUEST; - $user_online = false; - if ( $who_support && in_array($row['username'], $whos_online['users']) ) - { - $user_online = true; - } - elseif ( $row['poster_id'] < 2 ) - { - $who_support = false; - } - $parser->assign_bool(Array( - 'whos_online_support' => $who_support, - 'user_is_online' => $user_online, - 'post_edited' => ( $row['edit_count'] > 0 ), - 'post_deleted' => ( $row['post_deleted'] == 1 ), - // FIXME: This should check something on ACLs - 'show_post' => ( $row['post_deleted'] != 1 || $session->user_level >= USER_LEVEL_MOD ) - )); - echo $parser->run(); -} +$html = paginate( + $q, // MySQL result resource + '{post_id}', + $num_posts, + makeUrlNS('Special', 'Forum/Topic/' . $tid . '/start=%s', false, true), + $offset, + 15, + array('post_id' => array($formatter, '_render')) + ); + +if ( $html ) + echo $html; +else + die_friendly('Error', '

The topic you requested does not exist.

'); $db->free_result(); @@ -294,6 +190,8 @@ // log the hit $time = time(); $q = $db->sql_query('INSERT INTO '.table_prefix."decir_hits(user_id, topic_id, timestamp) VALUES($session->user_id, $tid, $time);"); +$q = $db->sql_query('UPDATE '.table_prefix."decir_topics SET num_views = num_views + 1 WHERE topic_id = $tid;"); $template->footer(); +?> diff -r 6f8b7c6fac02 -r 253118325c65 plugins/Decir.php --- a/plugins/Decir.php Wed Oct 17 20:23:51 2007 -0400 +++ b/plugins/Decir.php Wed Oct 17 21:52:27 2007 -0400 @@ -86,6 +86,9 @@ case 'delete': require('delete.php'); break; + case 'restoretopic': + require('restoretopic.php'); + break; } chdir($curdir);