<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// Delete Message
	// ========================================================================================
	
		
	
	ReadFormArguments();
	Global $FormArguments;
			
	if (count($FormArguments) > 0) {
	
	
		
		ReadPermissions();
		Global $Permissions;
		
		
		$FormArguments['MessageID'] = intval($FormArguments['MessageID']);
		
		
		$query = "select * from `".$Settings['DBPrefix']."mb-Messages` where RecordNumber=".$FormArguments['MessageID']." limit 1";
		$result = mysql_query($query) or die (mysql_error());
		$row = mysql_fetch_array($result);
		
		$MessageID = $row['RecordNumber'];
		$ThreadID = $row['ThreadID'];
		$PosterID = $row['Poster'];
		
		
		$query2 = "select * from `".$Settings['DBPrefix']."mb-Threads` where RecordNumber=".$ThreadID." limit 1";
		$result2 = mysql_query($query2) or die (mysql_error());
		$row2 = mysql_fetch_array($result2);
			
		$BoardID = $row2['BoardID'];
		$ThreadStatus = $row2['Status'];
		
				
		// if this user is the owner of the message, or has moderator permissions
		if ($row['Poster'] == $ThisUserNumber || $Permissions[202] == 'y') {
		
			
			$query55 = "update `".$Settings['DBPrefix']."core-Users` set ForumPosts=ForumPosts-1 where RecordNumber='".$PosterID."' limit 1";
			$result55 = mysql_query($query55) or die (mysql_error());
		
			
			$query55 = "delete from `".$Settings['DBPrefix']."mb-Messages` where RecordNumber='".$MessageID."' limit 1";
			$result55 = mysql_query($query55) or die (mysql_error());
			
			$query = "select * from `".$Settings['DBPrefix']."mb-Messages` where ThreadID=".$FormArguments['ThreadID']." order by DateCode desc limit 1";
			$result = mysql_query($query) or die (mysql_error());
			if (mysql_num_rows($result) == 0) {
			// no other messages in the thread, so delete it
				
				$query55 = "delete from `".$Settings['DBPrefix']."mb-Threads` where RecordNumber='".$ThreadID."' limit 1";
				$result55 = mysql_query($query55) or die (mysql_error());
				
				
				$query55 = "update `".$Settings['DBPrefix']."mb-Boards` set Threads=Threads-1 where RecordNumber='".$BoardID."' limit 1";
				$result55 = mysql_query($query55) or die (mysql_error());
				
				
				$ReturnURL = $MainURL . '/forum/'.$FormArguments['BoardID'].'.html';	
			}
			else {
			// other messages in the thread so find the next most recent one
			
				$row = mysql_fetch_array($result);
				
				$LastReply = $row['DateCode'];
				$LastUser = $row['Poster'];
				
				
				$query55 = "update `".$Settings['DBPrefix']."mb-Threads` set Replies=Replies-1, LastReply='".$LastReply."', LastUser='".$LastUser."' where RecordNumber='".$ThreadID."' limit 1";
				$result55 = mysql_query($query55) or die (mysql_error());
				
				
				
				$query55 = "select count(RecordNumber) as MessagesInThread from `".$Settings['DBPrefix']."mb-Messages` where ThreadID='".$ThreadID."' and RecordNumber<=".$MessageID." limit 1";
				$result55 = mysql_query($query55) or die (mysql_error());
				$row55 = mysql_fetch_array($result55);
			
				$MessagesInThread = $row55['MessagesInThread'];
			
				
				$MessagePage = intval($MessagesInThread / $mbSettings['MessagesPerPage']);
				if (($MessagesInThread / $mbSettings['MessagesPerPage']) != intval($MessagesInThread / $mbSettings['MessagesPerPage'])) {
					$MessagePage++;
				}
				if ($MessagePage == 0) {
					$MessagePage = 1;
				}
			
				$ThreadMessageNumber = ($MessagesInThread-(($MessagePage-1)*$mbSettings['MessagesPerPage']));
				
				
				
				//$ReturnURL = $MainURL . '/forum/'.$FormArguments['BoardID'].'/'.$FormArguments['ThreadID'].'.html';
				$ReturnURL = $MainURL . '/forum/' . $FormArguments['BoardID'] . '/' . $FormArguments['ThreadID'] . '/' . $MessagePage . '.html#' . $ThreadMessageNumber;
				
			}
			
			

			$LastReply = 0;
			$LastUser = 0;
			
			$query = "select * from `".$Settings['DBPrefix']."mb-Threads` where BoardID=".$BoardID;
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
			
				$query3 = "select * from `".$Settings['DBPrefix']."mb-Messages` where ThreadID=".$row['RecordNumber']." order by DateCode desc limit 1";
				$result3 = mysql_query($query3) or die (mysql_error());
				if (mysql_num_rows($result3) != 0) {
				
					$row3 = mysql_fetch_array($result3);
				
					if ($row3['DateCode'] > $LastReply) {
						$LastReply = $row3['DateCode'];
						$LastUser = $row3['Poster'];
					}
				}
			
			}
			
			
			$query55 = "update `".$Settings['DBPrefix']."mb-Boards` set Messages=Messages-1, LastReply='".$LastReply."', LastUser='".$LastUser."' where RecordNumber='".$BoardID."' limit 1";
			$result55 = mysql_query($query55) or die (mysql_error());
			
			
			
									
								
			
			header('Location: ' . $ReturnURL);
			
			
			
		} // end if allowed to post
		else {
		
			$PageContents = $MsgText[2][79];
		
		}
		
	}
	else {
		$PageContents = $MsgText[0][98];
	}	
			
	
	
?>
