##############################################################
## MOD Title: Edit Notes
## MOD Author: pentapenguin < pentapenguin@bluebottle.com > (n/a) http://www.pentapenguin.com
## MOD Description: When editing a post, you will have an option to leave a short note saying the reason why the post was edited. 
## This MOD is fully configurable with admin panel options for enabling/disabling the edit notes, edit notes permissions, and the total edit notes per post.
## MOD Version: 0.1.0
##
## Installation Level: Easy
## Installation Time: 15 Minutes
## Files To Edit: 11
## admin/admin_board.php
## admin/admin_users.php
## includes/constants.php
## includes/functions_post.php
## language/lang_english/lang_admin.php
## language/lang_english/lang_main.php
## templates/subSilver/admin/board_config_body.tpl
## templates/subSilver/posting_body.tpl
## templates/subSilver/viewtopic_body.tpl
## posting.php
## viewtopic.php
##
## Included Files: (n/a)
##############################################################
## For Security Purposes, Please Check: http://www.phpbb.com/mods/ for the
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code
## to enter into your phpBB Forum. As such, phpBB will not offer support for MODs not offered
## in our MOD-Database, located at: http://www.phpbb.com/mods/
##############################################################
## Author Notes:
## Support for this MOD may be found at http://www.phpbb.com/phpBB/viewtopic.php?t=304473 or at
## http://www.pentapenguin.com/forum/viewtopic.php?t=863
##
## This MOD is EasyMOD friendly! (http://area51.phpbb.com/phpBB/viewtopic.php?sid=&f=17&t=15391)
##
## Optional step: if you have CyberAlien's eXtreme Styles MOD (http://www.phpbb.com/phpBB/viewtopic.php?t=125251),
## you can copy the file in contrib/admin/edit_notes.cfg to admin/edit_notes.cfg and you will
## be automatically notified if there's an update available when you use the "check for updates" function.
##############################################################
## MOD History:
##   2005-07-05 - Version 0.1.0
##      - First Public Beta
##
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
##############################################################


#
#-----[ SQL ]------------------------------------------
#
CREATE TABLE phpbb_edit_notes (
edit_note_id MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
topic_id MEDIUMINT(8) UNSIGNED NOT NULL,
post_id MEDIUMINT(8) UNSIGNED NOT NULL,
user_id MEDIUMINT(8) UNSIGNED NOT NULL,
note VARCHAR(255) NOT NULL,
ip varchar(8) NOT NULL,
edit_note_time INT(11) NOT NULL,
PRIMARY KEY (edit_note_id)
);
INSERT INTO phpbb_config (config_name, config_value) VALUES ('edit_notes_enable', '1');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_edit_notes', '5');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('edit_notes_permissions', '1');


#
#-----[ OPEN ]------------------------------------------
#
admin/admin_board.php


#
#-----[ FIND ]------------------------------------------
#
		if ($config_name == 'cookie_name')
		{
			$cookie_name = str_replace('.', '_', $new['cookie_name']);
		}

	
#
#-----[ AFTER, ADD ]------------------------------------------
#

		// Start Edit Notes MOD
		$new['max_edit_notes'] = preg_replace("#[^0-9]{1,2}#", '5', $new['max_edit_notes']);
		// End Edit Notes MOD


#
#-----[ FIND ]------------------------------------------
#
$smtp_no = ( !$new['smtp_delivery'] ) ? "checked=\"checked\"" : "";


#
#-----[ AFTER, ADD ]------------------------------------------
#

// Start Edit Notes MOD
$edit_notes_enable = ( $new['edit_notes_enable'] ) ? 'checked="checked"' : '';
$edit_notes_disable = ( !$new['edit_notes_enable'] ) ? 'checked="checked"' : '';

$max_edit_notes = ( !$new['max_edit_notes'] ) ? '5' : $new['max_edit_notes'];

$edit_notes_admin = ( $new['edit_notes_permissions'] == 3 ) ? 'checked="checked"' : '';
$edit_notes_staff = ($new['edit_notes_permissions'] == 2 ) ? 'checked="checked"' : '';
$edit_notes_reg = ( $new['edit_notes_permissions'] == 1 ) ? 'checked="checked"' : '';
$edit_notes_all = ( $new['edit_notes_permissions'] == 0 ) ? 'checked="checked"' : '';
// End Edit Notes MOD


#
#-----[ FIND ]------------------------------------------
#
	"L_SMTP_PASSWORD_EXPLAIN" => $lang['SMTP_password_explain'], 

	
#
#-----[ AFTER, ADD ]------------------------------------------
#

	// Start Edit Notes MOD
	'L_EDIT_NOTES_SETTINGS' => $lang['Edit_notes_settings'],
	'L_EDIT_NOTES_SETTINGS_EXPLAIN' => $lang['Edit_notes_settings'],
	'L_EDIT_NOTES_ENABLE' => $lang['Edit_notes_enable'],
	'L_EDIT_NOTES_ENABLE_EXPLAIN' => $lang['Edit_notes_enable_explain'],
	'L_MAX_EDIT_NOTES' => $lang['Max_edit_notes'],
	'L_MAX_EDIT_NOTES_EXPLAIN' => $lang['Max_edit_notes_explain'],
	'L_EDIT_NOTES_PERMISSIONS' => $lang['Edit_notes_permissions'],
	'L_EDIT_NOTES_PERMISSIONS_EXPLAIN' => $lang['Edit_notes_permissions_explain'],
	'L_EDIT_NOTES_ADMIN' => $lang['Edit_notes_admin'],
	'L_EDIT_NOTES_STAFF' => $lang['Edit_notes_staff'],
	'L_EDIT_NOTES_REG' => $lang['Edit_notes_reg'],
	'L_EDIT_NOTES_ALL' => $lang['Edit_notes_all'],
	// End Edit Notes MOD

	
#
#-----[ FIND ]------------------------------------------
#
	"SMTP_PASSWORD" => $new['smtp_password'],

	
#
#-----[ AFTER, ADD ]------------------------------------------
#

	// Start Edit Notes MOD
	'EDIT_NOTES_ENABLE' => $edit_notes_enable,
	'EDIT_NOTES_DISABLE' => $edit_notes_disable,
	'MAX_EDIT_NOTES' => $max_edit_notes,
	'EDIT_NOTES_ADMIN' => $edit_notes_admin,
	'EDIT_NOTES_STAFF' => $edit_notes_staff,
	'EDIT_NOTES_REG' => $edit_notes_reg,
	'EDIT_NOTES_ALL' => $edit_notes_all,
	// End Edit Notes MOD

	
#
#-----[ OPEN ]------------------------------------------
#
admin/admin_users.php


#
#-----[ FIND ]------------------------------------------
#
		if( $HTTP_POST_VARS['deleteuser'] )
		{


#
#-----[ AFTER, ADD ]------------------------------------------
#		

			// Start Edit Notes MOD
			$sql = 'UPDATE ' . EDIT_NOTES_TABLE . ' SET user_id = ' . DELETED . " WHERE user_id = $user_id";
			if( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, 'Could not update edit notes for this user', '', __LINE__, __FILE__, $sql);
			}
			// End Edit Notes MOD


#
#-----[ OPEN ]------------------------------------------
#
includes/constants.php


#
#-----[ FIND ]------------------------------------------
#
?>


#
#-----[ BEFORE, ADD ]------------------------------------------
#

// Start Edit Notes MOD
define('EDIT_NOTES_TABLE', $table_prefix . 'edit_notes');
// End Edit Notes MOD


#
#-----[ OPEN ]------------------------------------------
#
includes/functions_post.php


#
#-----[ FIND ]------------------------------------------
#
function submit_post($mode, &$post_data, &$message


#
#-----[ IN-LINE FIND ]------------------------------------------
#
&$poll_length


#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
, &$edit_notes


#
#-----[ FIND ]------------------------------------------
#
	add_search_words('single', $post_id, stripslashes($post_message), stripslashes($post_subject));

	
#
#-----[ AFTER, ADD ]------------------------------------------
#

	// Start Edit Notes MOD
	if ( count($edit_notes) > 0 && $mode == 'editpost' )
	{
		while ( list($edit_note_id, $note) = each($edit_notes) )
		{
			// New edit note
			if ( $edit_note_id == 0 )
			{
				$sql = 'INSERT INTO ' . EDIT_NOTES_TABLE . " (topic_id, post_id, user_id, note, ip, edit_note_time) VALUES ($topic_id, $post_id, {$userdata['user_id']}, '$note', '$user_ip', $current_time)";
				$sql_mode = 'adding';
			}
			
			// Update edit note
			else
			{
				//  If edit note is blank, delete it
				if ( $note == '' )
				{
					$sql = 'DELETE FROM ' . EDIT_NOTES_TABLE . " WHERE edit_note_id = $edit_note_id LIMIT 1";
					$sql_mode = 'deleting';
				}
				
				else
				{
					$sql = 'UPDATE ' . EDIT_NOTES_TABLE . " SET note = '$note' WHERE edit_note_id = $edit_note_id LIMIT 1";
					$sql_mode = 'updating';
				}
			}
			
			if ( !($db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, "Error in $sql_mode this edit note", '', __LINE__, __FILE__, $sql);
			}
		}
	}
	// End Edit Notes MOD
	

#
#-----[ FIND ]------------------------------------------
#
	if ($mode != 'poll_delete')
	{
		include($phpbb_root_path . 'includes/functions_search.'.$phpEx);
		
		
#
#-----[ AFTER, ADD ]------------------------------------------
#

		// Start Edit Notes MOD
		$sql = 'DELETE FROM ' . EDIT_NOTES_TABLE . " WHERE post_id = $post_id";
		if ( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, 'Error in deleting edit note', '', __LINE__, __FILE__, $sql);
		}
		//End Edit Notes MOD
	

#
#-----[ OPEN ]------------------------------------------
#
language/lang_english/lang_admin.php


#
#-----[ FIND ]------------------------------------------
#
?>


#
#-----[ BEFORE, ADD ]------------------------------------------
#

// Start Edit Notes MOD
$lang['Edit_notes_settings'] = 'Valores de Notas de Edicion';
$lang['Edit_notes_enable'] = 'Habilitar Notas de Edicion';
$lang['Edit_notes_enable_explain'] = 'Habilitar/Deshabilitar Notas de Edicion en el Foro.';
$lang['Max_edit_notes'] = 'Notas de Edicion maximas';
$lang['Max_edit_notes_explain'] = 'Fija el numero maximo de Notas de Edicion por mensaje.';
$lang['Edit_notes_permissions'] = 'Permisos de Noteas de Edicion';
$lang['Edit_notes_permissions_explain'] = 'Fija que tipo de usuarios usaran las Notas de Edicion.';
$lang['Edit_notes_admin'] = 'Solo Administradores';
$lang['Edit_notes_staff'] = 'Equipo (Admins y Mods)';
$lang['Edit_notes_reg'] = 'Usuarios Registrados (Admins y Mods tambien)';
$lang['Edit_notes_all'] = 'Todos los Usuarios (Invitados, Usuarios Registrados, Admins, y Mods)';
// End Edit Notes MOD


#
#-----[ OPEN ]------------------------------------------
#
language/lang_english/lang_main.php


#
#-----[ FIND ]------------------------------------------
#
?>


#
#-----[ BEFORE, ADD ]------------------------------------------
#

// Start Edit Notes MOD
$lang['Edit_notes'] = 'Editar Notas';
$lang['Delete_note'] = 'Borrar Nota';
$lang['Edited_by'] = 'Editado por %s el %s:';
$lang['Confirm_delete_edit_note'] = 'Estas seguro de querer borrar esta Nota?';
$lang['Edit_note_deleted'] = 'La Nota a sido borrada correctamente.';
// End Edit Notes MOD


#
#-----[ OPEN ]------------------------------------------
#
templates/subSilver/admin/board_config_body.tpl


#
#-----[ FIND ]------------------------------------------
#
	<tr>
		<td class="row1">{L_SMTP_PASSWORD}<br /><span class="gensmall">{L_SMTP_PASSWORD_EXPLAIN}</span></td>
		<td class="row2"><input class="post" type="password" name="smtp_password" value="{SMTP_PASSWORD}" size="25" maxlength="255" /></td>
	</tr>

	
#
#-----[ AFTER, ADD ]------------------------------------------
#

	<!-- Start Edit Notes MOD -->
	<tr>
	  <th class="thHead" colspan="2">{L_EDIT_NOTES_SETTINGS}</th>
	</tr>
	<tr>
		<td class="row1" valign="top">{L_EDIT_NOTES_ENABLE}<br /><span class="gensmall">{L_EDIT_NOTES_ENABLE_EXPLAIN}</span></td>
		<td class="row2"><input type="radio" name="edit_notes_enable" value="1" {EDIT_NOTES_ENABLE} /> {L_YES}&nbsp;&nbsp;<input type="radio" name="edit_notes_enable" value="0" {EDIT_NOTES_DISABLE} /> {L_NO}</td>
	</tr>
	<tr>
		<td class="row1" valign="top">{L_MAX_EDIT_NOTES}<br /><span class="gensmall">{L_MAX_EDIT_NOTES_EXPLAIN}</span></td>
		<td class="row2"><input class="post" type="text" name="max_edit_notes" value="{MAX_EDIT_NOTES}" size="2" maxlength="2" /></td>
	</tr>
	<tr>
		<td class="row1" valign="top">{L_EDIT_NOTES_PERMISSIONS}<br /><span class="gensmall">{L_EDIT_NOTES_PERMISSIONS_EXPLAIN}</span></td>
		<td class="row2" nowrap="nowrap">
			<input type="radio" name="edit_notes_permissions" value="3" {EDIT_NOTES_ADMIN} />{L_EDIT_NOTES_ADMIN}<br />
			<input type="radio" name="edit_notes_permissions" value="2" {EDIT_NOTES_STAFF} />{L_EDIT_NOTES_STAFF}<br />
			<input type="radio" name="edit_notes_permissions" value="1" {EDIT_NOTES_REG} />{L_EDIT_NOTES_REG}<br />
			<input type="radio" name="edit_notes_permissions" value="0" {EDIT_NOTES_ALL} />{L_EDIT_NOTES_ALL}
		</td>
   	</tr>
   	<!-- End Edit Notes MOD -->

	
#
#-----[ OPEN ]------------------------------------------
#
templates/subSilver/posting_body.tpl


#
#-----[ FIND ]------------------------------------------
#
	<!-- BEGIN switch_privmsg -->
	<tr> 
		<td class="row1"><span class="gen"><b>{L_USERNAME}</b></span></td>
		<td class="row2"><span class="genmed"><input type="text"  class="post" name="username" maxlength="25" size="25" tabindex="1" value="{USERNAME}" />&nbsp;<input type="submit" name="usersubmit" value="{L_FIND_USERNAME}" class="liteoption" onClick="window.open('{U_SEARCH_USER}', '_phpbbsearch', 'HEIGHT=250,resizable=yes,WIDTH=400');return false;" /></span></td>
	</tr>
	<!-- END switch_privmsg -->

	
#
#-----[ AFTER, ADD ]------------------------------------------
#

	<!-- BEGIN switch_show_edit_notes -->
		<!-- BEGIN edit_notes -->
		<tr>
			<td class="row1"><span class="gen"><b>{L_EDIT_NOTES}</b></span></td>
			<td class="row2"><span class="gensmall">{edit_notes.L_EDITED_BY}</span><br /><span class="genmed"><input type="text" class="post" name="new_edit_notes[{edit_notes.S_NOTE_ID}]" style="width: 450px;" maxlength="255" size="45" value="{edit_notes.NOTE}" /></span></td>
		</tr>
		<!-- END edit_notes -->
		
	<tr>
		<td class="row1"><span class="gen"><b>{L_EDIT_NOTES}</b></span></td>
		<td class="row2"><span class="genmed"><input type="text" class="post" name="new_edit_note" style="width: 450px;" maxlength="255" size="45" tabindex="1" value="{S_NEW_EDIT_NOTE}" /></span></td>
	</tr>
	<!-- END switch_show_edit_notes -->

	
#
#-----[ OPEN ]------------------------------------------
#
templates/subSilver/viewtopic_body.tpl


#
#-----[ FIND ]------------------------------------------
#
{postrow.EDITED_MESSAGE}</span>


#
#-----[ IN-LINE FIND ]------------------------------------------
#
</span>


#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#

				<!-- Edit Notes MOD -->
				<!-- BEGIN post_edit_notes -->
					<!-- BEGIN edit_notes_loop -->
					<br />
					<table style="background-color: #A9B8C2;" cellspacing="1" width="100%">
					<tr>
						<td style="background-color: #C0C8D0; padding: 4px;"><span class="gensmall">{postrow.post_edit_notes.edit_notes_loop.L_EDITED_BY} {postrow.post_edit_notes.edit_notes_loop.U_IP} {postrow.post_edit_notes.edit_notes_loop.U_DELETE_NOTE}</span></td>
					</tr>
					<tr>
						<td style="background-color: #DCE1E5; padding: 4px;"><span class="genmed">{postrow.post_edit_notes.edit_notes_loop.NOTE}</span></td>
					</tr>
					</table>
					<!-- END edit_notes_loop -->
				<!-- END post_edit_notes -->
				<!-- Edit Notes MOD -->


#
#-----[ OPEN ]------------------------------------------
#
posting.php


#
#-----[ FIND ]------------------------------------------
#
	$is_auth = auth(AUTH_ALL, $forum_id, $userdata, $post_info);


#
#-----[ AFTER, ADD ]------------------------------------------
#

	// Start Edit Notes MOD
	if ( $mode == 'editpost' && $board_config['edit_notes_enable'] )
	{
		$show_edit_notes = false;
		$show_own_edit_notes = true;
		$edit_notes_permissions = $board_config['edit_notes_permissions'];
	
		// Let's see who can post or edit the notes
		if ( $userdata['user_level'] == ADMIN )
		{
			$show_edit_notes = true;
			$show_own_edit_notes = false;
		}
	
		else if ( $userdata['user_level'] == MOD && $is_auth['auth_mod'] && $edit_notes_permissions >= 0 )
		{
			$show_edit_notes = true;
			$show_own_edit_notes = false;
		}
	
		else if ( $userdata['user_level'] == USER && ( $edit_notes_permissions == 0 || $edit_notes_permissions == 1 ) )
		{
			$show_edit_notes = true;
		}
	
		else if ( $userdata['user_level'] == ANONYMOUS && $edit_notes_permissions == 0 && $post_info['poster_ip'] == $userdata['session_ip'] )
		{
			$show_edit_notes = true;
		}
	
		else
		{
			$show_edit_notes = false;
		}
	
		if ( $show_edit_notes )
		{
			$new_edit_note = ( isset($HTTP_GET_VARS['new_edit_note']) ) ? unprepare_message($HTTP_GET_VARS['new_edit_note']) : unprepare_message($HTTP_POST_VARS['new_edit_note']);
			$new_edit_notes = ( isset($HTTP_GET_VARS['new_edit_notes']) ) ? $HTTP_GET_VARS['new_edit_notes'] : $HTTP_POST_VARS['new_edit_notes'];
			
			$template->assign_block_vars('switch_show_edit_notes', array() );
			$template->assign_vars(array(
				'L_EDIT_NOTES' => $lang['Edit_notes'],
				'S_NEW_EDIT_NOTE' => htmlspecialchars(stripslashes($new_edit_note)),
			));
		
			if ( $show_own_edit_notes )
			{
				$own_edit_notes_sql = ' AND user_id = ' . $userdata['user_id'];
			}
	
			$sql = 'SELECT * FROM ' . EDIT_NOTES_TABLE . ' WHERE post_id = ' . $post_id . $own_edit_notes_sql . ' ORDER BY edit_note_time DESC LIMIT ' . ( $board_config['max_edit_notes'] - 1 );
		
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Could not look up edit notes for this post', '', __LINE__, __FILE__, $sql);
			}

			while ( $row = $db->sql_fetchrow($result) )
			{
				$note_id = $row['edit_note_id'];
				$user_id = $row['user_id'];
				$note = htmlspecialchars($row['note']);
				
				$time = create_date($board_config['default_dateformat'], $row['edit_note_time'], $board_config['board_timezone']);
	
				if ( $user_id != ANONYMOUS )
				{
					$sql2 = 'SELECT username, user_level FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user_id;
	
					if ( !($result2 = $db->sql_query($sql2)) )
					{
						message_die(GENERAL_ERROR, 'Could not look up username for this edit note', '', __LINE__, __FILE__, $sql);
					}

					if ( !($row2 = $db->sql_fetchrow($result2)) )
					{
						message_die(GENERAL_ERROR, 'Could not retrieve username for this edit note', '', __LINE__, __FILE__, $sql);
					}
		
					$username = $row2['username'];
					$user_level = $row2['user_level'];
					$user_style = ( $user_level == ADMIN ) ? ' style="color: #' . $theme['fontcolor3'] . '"' : ( ( $user_level == MOD ) ? ' style="color: #' . $theme['fontcolor2'] . '"' : '');
					$u_user_profile = '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=$user_id") . '"' . $user_style . '>' . $username . '</a>';
				}

				else
				{
					$u_user_profile = $lang['Guest'];
				}
			
				// Save a copy of the notes
				$original_edit_notes[$note_id] = $user_id;
			
				$template->assign_block_vars('switch_show_edit_notes.edit_notes', array(
					'S_NOTE_ID' => $note_id,
					'L_EDITED_BY' => sprintf($lang['Edited_by'], $u_user_profile, $time),
					'NOTE' => $note,
				));
			} // end while ( $row = $db->sql_fetchrow($result) )
			
			// Prepare an array of notes if ready to submit
			if ( $submit || $refresh )
			{
				$edit_notes = array();
				
				// Insert the new note into the array
				if ( !empty($new_edit_note) )
				{
					$new_edit_note = trim($new_edit_note);
					$new_edit_note = str_replace("\'", "''", $new_edit_note);
					$edit_notes[0] = $new_edit_note;
				}
				
				// Insert any existing notes into the array
				if ( count($new_edit_notes) > 0 )
				{
					while ( list($edit_note_id, $note) = each($new_edit_notes) )
					{
						// Cancel out any unauthorized notes
						if ( $show_own_edit_notes && $original_edit_notes[$edit_note_id] != $userdata['user_id'] )
						{
							unset($new_edit_notes[$edit_note_id]);
						}
					
						else
						{
							$note = trim($note);
							$note = str_replace("\'", "''", $note);
							$edit_notes[$edit_note_id] = $note;
						}
					}	
				}	
			} //end if ( $submit || $refresh )
		} // end if ( $show_edit_notes )
	} // end if ( $mode == 'editpost' && $board_config['edit_notes_enable'] )
	// End Edit Notes MOD

	
#
#-----[ FIND ]------------------------------------------
#
				submit_post($mode,


#
#-----[ IN-LINE FIND ]------------------------------------------
#
$poll_length


#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
, $edit_notes


#
#-----[ OPEN ]------------------------------------------
#
viewtopic.php


#
#-----[ FIND ]------------------------------------------
#
}

$template->pparse('body');


#
#-----[ BEFORE, ADD ]------------------------------------------
#

// Start Edit Notes MOD
	if ( $board_config['edit_notes_enable'] )
	{
		$mode = ( isset($HTTP_GET_VARS['mode']) ) ? htmlspecialchars($HTTP_GET_VARS['mode']) : htmlspecialchars($HTTP_POST_VARS['mode']);
		$sid = ( isset($HTTP_GET_VARS['sid']) ) ? htmlspecialchars($HTTP_GET_VARS['sid']) : htmlspecialchars($HTTP_POST_VARS['sid']);
		$u_post_id = ( isset($HTTP_GET_VARS[POST_POST_URL]) ) ? intval($HTTP_GET_VARS[POST_POST_URL]) : intval($HTTP_POST_VARS[POST_POST_URL]);
		$u_topic_id = ( isset($HTTP_GET_VARS[POST_TOPIC_URL]) ) ? intval($HTTP_GET_VARS[POST_TOPIC_URL]) : intval($HTTP_POST_VARS[POST_TOPIC_URL]);
		$u_edit_note_id = ( isset($HTTP_GET_VARS['edit_note_id']) ) ? intval($HTTP_GET_VARS['edit_note_id']) : intval($HTTP_POST_VARS['edit_note_id']);
		$confirm = isset($HTTP_POST_VARS['confirm']) ? true : false;
		$cancel = isset($HTTP_POST_VARS['cancel']) ? true : false;
		
		// Are we trying to delete a note?
		if ( $mode == 'deletenote' && $sid && $u_edit_note_id && $u_post_id )
		{
			// To delete a note requires a session ID. No SID or invalid one? Die to prevent session hijacking.
			if ( $sid == '' || $sid != $userdata['session_id'] )
			{
				message_die(GENERAL_ERROR, 'Invalid_session');
			}
			
			$sql = 'SELECT user_id FROM ' . EDIT_NOTES_TABLE . " WHERE edit_note_id = $u_edit_note_id";
			
			if ( !($result = $db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Could not look up user ID for this edit note', '', __LINE__, __FILE__, $sql);
			}
			
			if ( !($row = $db->sql_fetchrow($result)) )
			{
				message_die(GENERAL_ERROR, 'Could not retrieve user ID for this edit note', '', __LINE__, __FILE__, $sql);
			}
			
			$user_id = $row['user_id'];
			
			// Auth check. Is the user deleting the note the author or a mod/admin?
			if ( $is_auth['auth_delete'] != 1 || ( $is_auth['auth_mod'] == 0 && $user_id != $userdata['user_id'] ) )
			{
				message_die(GENERAL_ERROR, $lang['Not_Authorised']);
			}
			
			// Was cancel pressed when deleting a note?
			if ( $cancel )
			{
				redirect(append_sid("viewtopic.$phpEx?" . POST_POST_URL . "=$u_post_id", true) . "#$u_post_id");
			}
			
			// Confirm edit note deletion
			if ( !$confirm )
			{
				$s_hidden_fields = '<input type="hidden" name="' . POST_POST_URL . '" value="' . $u_post_id . '" />';
				$s_hidden_fields .= '<input type="hidden" name="mode" value="deletenote" />';
				$s_hidden_fields .= '<input type="hidden" name="sid" value="' . $sid . '" />';
				$s_hidden_fields .= '<input type="hidden" name="edit_note_id" value="' . $u_edit_note_id . '" />';
				$l_confirm = $lang['Confirm_delete_edit_note'];
				
				$template->set_filenames(array(
					'confirm_body' => 'confirm_body.tpl',
				));

				$template->assign_vars(array(
					'MESSAGE_TITLE' => $lang['Information'],
					'MESSAGE_TEXT' => $l_confirm,
					'L_YES' => $lang['Yes'],
					'L_NO' => $lang['No'],
					'S_CONFIRM_ACTION' => append_sid("viewtopic.$phpEx?" . POST_TOPIC_URL . "=$u_topic_id"),
					'S_HIDDEN_FIELDS' => $s_hidden_fields,
				));
				
				$template->pparse('confirm_body');
				include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
			}
			
			// Actually delete the note
			if ( $confirm )
			{
				$sql = 'DELETE FROM ' . EDIT_NOTES_TABLE . " WHERE edit_note_id = $u_edit_note_id LIMIT 1";
			
				if ( !($result = $db->sql_query($sql)) )
				{
					message_die(GENERAL_ERROR, 'Could not delete this edit note', '', __LINE__, __FILE__, $sql);
				}
				
				$message = $lang['Edit_note_deleted'];
				$message .=  '<br /><br />' . sprintf($lang['Click_view_message'], '<a href="' . append_sid("viewtopic.$phpEx?" . POST_POST_URL . "=$u_post_id") . "#$u_post_id" . '">', '</a>');
				message_die(GENERAL_MESSAGE, $message);
			}
		}

		// Core code. Pull all edit notes for this post.
		$max_edit_notes = $board_config['max_edit_notes'];
		$sql = 'SELECT * FROM ' . EDIT_NOTES_TABLE . ' WHERE post_id = ' . $postrow[$i]['post_id'] . ' ORDER BY edit_note_time DESC LIMIT ' . $max_edit_notes;

		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not look up edit notes for this post', '', __LINE__, __FILE__, $sql);
		}

		if ( $db->sql_numrows($result) )
		{
			$template->assign_block_vars('postrow.post_edit_notes', array() );
		}
		
		while ( $row = $db->sql_fetchrow($result) )
		{
			$edit_note_id = $row['edit_note_id'];
			$user_id = $row['user_id'];
			$note = htmlspecialchars($row['note']);
			
				// Replace censored words
				if ( count($orig_word) )
				{
					$note = preg_replace($orig_word, $replacement_word, $note);
				}
		
			$ip = decode_ip($row['ip']);
			$time = create_date($board_config['default_dateformat'], $row['edit_note_time'], $board_config['board_timezone']);
		
			// Match up user IDs with usernames
			if ( $user_id != ANONYMOUS )
			{
				$sql2 = 'SELECT username, user_level FROM ' . USERS_TABLE . ' WHERE user_id = ' . $user_id;
	
				if ( !($result2 = $db->sql_query($sql2)) )
				{
					message_die(GENERAL_ERROR, 'Could not look up username for this edit note', '', __LINE__, __FILE__, $sql);
				}

				if ( !($row2 = $db->sql_fetchrow($result2)) )
				{
					message_die(GENERAL_ERROR, 'Could not retrieve username for this edit note', '', __LINE__, __FILE__, $sql);
				}
		
				$username = $row2['username'];
				$user_level = $row2['user_level'];
				$user_style = ( $user_level == ADMIN ) ? ' style="color: #' . $theme['fontcolor3'] . '"' : ( ( $user_level == MOD ) ? ' style="color: #' . $theme['fontcolor2'] . '"' : '' );
				$u_user_profile = '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=$user_id") . '"' . $user_style . '>' . $username . '</a>';
			}
	
			else
			{
				$u_user_profile = $lang['Guest'];
			}
		
			// IP / Delete links for the appropriate user level
			$u_ip = ( $is_auth['auth_mod'] ) ? ' [ ' . $lang['IP'] . ': <a href="http://whois.sc/' . $ip . '" target="_blank">' . $ip . '</a> ]' : '';
			$u_delete_note = ( $is_auth['auth_mod'] || $user_id == $userdata['user_id'] ) ? ' [ <a href="' . "viewtopic.$phpEx?mode=deletenote&amp" . POST_POST_URL . '=' . $postrow[$i]['post_id'] . "&amp;" . POST_TOPIC_URL . "=$topic_id&amp;edit_note_id=$edit_note_id&amp;sid=" . $userdata['session_id'] . '">' . $lang['Delete_note'] . '</a> ]' : '';
			
			$template->assign_block_vars('postrow.post_edit_notes.edit_notes_loop', array(
				'L_EDITED_BY' => sprintf($lang['Edited_by'], $u_user_profile, $time),
				'NOTE' => $note,
				'U_IP' => $u_ip,
				'U_DELETE_NOTE' => $u_delete_note,
			));
		}
	}
	// End Edit Notes MOD

	
#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM