<?

/*
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
*/



	// ========================================================================================
	// post new forum reply
	// ========================================================================================
	
		
	
	ReadFormArguments();
	Global $FormArguments;
	
	
	if (count($FormArguments) > 0) {
	
		
		if ($FormArguments['ShowPreview'] != '') {
		// display preview instead of posting the message
	
			include_once('./inc-mb-message_preview.php');		

		}
		else {
		// not in preview mode, so post the message
		
				
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[201] != 'y') {
		// if the user doesn't have permission to post
		
			$FormArguments['ThreadID'] = intval($FormArguments['ThreadID']);
		
		
			$query = "select * from `".$Settings['DBPrefix']."mb-Threads` where RecordNumber=".$FormArguments['ThreadID']." limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			$MessagesCount = ($row['Replies'] + 1);
			$ThreadTitle = $row['Title'];
			$BoardID = $row['BoardID'];
			$ViewsCount = $row['Views'];
			$RepliesCount = $row['Replies'];
		
		
			$query = "select Title,Category,Description from `".$Settings['DBPrefix']."mb-Boards` where RecordNumber=".$BoardID." limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			$BoardTitle = $row['Title'];
			$BoardDescription = $row['Description'];
			$CategoryID = $row['Category'];
			
			
			
			$query = "select Title,Description from `".$Settings['DBPrefix']."mb-Categories` where RecordNumber=".$CategoryID." limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			$CategoryTitle = $row['Title'];
			//$CategoryDescription = $row['Description'];
			
			
				
			
			$PageTitle = '<div align="'.$AlignmentCodes[$mbSettings['TitleAlignment']].'">'.$mbSettings['PageTitle'].'</div>';
			$BrowserTitle = $mbSettings['PageTitle'] . ' - '.$MsgText[2][59];
			$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="'.$MainURL.'/forum.html">'.$mbSettings['PageTitle'].'</a> > <a href="'.$MainURL.'/forum.html#'.$CategoryID.'">'.$CategoryTitle.'</a> > <a href="'.$MainURL.'/forum/'.$BoardID.'.html">'.$BoardTitle.'</a> > <a href="'.$MainURL.'/forum/'.$BoardID.'/'.$FormArguments['ThreadID'].'.html">'.$ThreadTitle.'</a> > '.$MsgText[2][59];
	
	
	
			$RedirectString = $Arguments1 . '/' . $Arguments2 . '/' . $Arguments3 . '/' . $Arguments4 . '/' . $Arguments5 . '/' . $Arguments6 . '/' . $Arguments7 . '/' . $Arguments8 . '/' . $Arguments9;
	
			$RedirectString = str_replace('//','',$RedirectString);
		
			if (substr($RedirectString,strlen($RedirectString)-1,1) == '/') {
				$RedirectString = substr($RedirectString,0,strlen($RedirectString)-1);
			}
		
			$RedirectString = str_replace('/','+++',$RedirectString);

				
			$LoginLink = $MainURL . '/login/' . $RedirectString;

	


			$PageContents = '<br><br>'.$MsgText[2][60].'<br><br>';

			if ($LoggedIn != 'y') {
				$PageContents .= $MsgText[2][61].': <a href="'.$LoginLink.'">'.$MsgText[0][192].'</a>.';
			
			
				if ($Settings['AllowGuestRegistrations'] == 'y') {
					$PageContents .= '  '.$MsgText[2][62].': <a href="'.$MainURL.'/register.html">'.$MsgText[0][124].'</a>.';
				}
			
			
				$PageContents .= '<br><br>'.$MsgText[2][63].' <a href="'.$MainURL.'/forum/'.$BoardID.'/'.$Arguments3.'.html">'.$MsgText[2][64].'</a>.';
			
			}
		
			else {
		
				$PageContents .= '<a href="'.$MainURL.'/forum/'.$BoardID.'/'.$Arguments3.'.html">'.$MsgText[2][64].'</a>.';
		
			}
	
		}
		else {
		// allowed to post
					
			
			// check status of the thread to make sure the post is allowed
			
			/*
			$query = "select `".$Settings['DBPrefix']."mb-Boards`.MinLevel as BMinLevel,`".$Settings['DBPrefix']."mb-Categories`.MinLevel as CMinLevel from `".$Settings['DBPrefix']."mb-Boards` left join `".$Settings['DBPrefix']."mb-Categories` on `".$Settings['DBPrefix']."mb-Categories`.RecordNumber=`".$Settings['DBPrefix']."mb-Boards`.Category where `".$Settings['DBPrefix']."mb-Boards`.RecordNumber='".$FormArguments['BoardID']."' limit 1";		
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			$BMinLevel = $row['BMinLevel'];
			$CMinLevel = $row['CMinLevel'];
			*/
			
			
			$FormArguments['ThreadID'] = intval($FormArguments['ThreadID']);
		
		
			$query = "select * from `".$Settings['DBPrefix']."mb-Threads` where RecordNumber=".$FormArguments['ThreadID']." limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			$ThreadStatus = $row['Status'];
			
			
			
			$BoardGroupsListSQL = '';
			
			$query66 = "select * from `".$Settings['DBPrefix']."mb-Boards` where RecordNumber=".$row['BoardID']." limit 1";
			$result66 = mysql_query($query66) or die (mysql_error());
			$row66 = mysql_fetch_array($result66);
			
			$BoardMinLevel = $row66['MinLevel'];
								
			if ($row66['GroupsList'] != '') {
				$GroupsListSplit = explode('-',substr($row66['GroupsList'],1,strlen($row66['GroupsList'])-2));
					
				for ($x = 0; $x < count($GroupsListSplit); $x++) {
					if ($BoardGroupsListSQL != '') {
						$BoardGroupsListSQL .= ' OR ';
					}
					
					$BoardGroupsListSQL .= 'GroupsList LIKE \'%-'.$GroupsListSplit[$x].'-%\'';
				}
				
				$BoardGroupsListSQL = ' OR ('.$BoardGroupsListSQL.')';			
			}
			
			
			
			$CatGroupsListSQL = '';
			
			$query66 = "select * from `".$Settings['DBPrefix']."mb-Categories` where RecordNumber=".$row66['Category']." limit 1";
			$result66 = mysql_query($query66) or die (mysql_error());
			$row66 = mysql_fetch_array($result66);
			
			$CatMinLevel = $row66['MinLevel'];
								
			if ($row66['GroupsList'] != '') {
				$GroupsListSplit = explode('-',substr($row66['GroupsList'],1,strlen($row66['GroupsList'])-2));
					
				for ($x = 0; $x < count($GroupsListSplit); $x++) {
					if ($CatGroupsListSQL != '') {
						$CatGroupsListSQL .= ' OR ';
					}
					
					$CatGroupsListSQL .= 'GroupsList LIKE \'%-'.$GroupsListSplit[$x].'-%\'';
				}
				
				$CatGroupsListSQL = ' OR ('.$CatGroupsListSQL.')';			
			}
			
			
			
			
			
			
			
			if ($ThreadStatus != 'L' || $Permissions[202] == 'y') {
			
			
				$query = "select RecordNumber from `".$Settings['DBPrefix']."core-Users` where Username='".$CookieUsername."' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$row = mysql_fetch_array($result);
			
				$UserNumber = $row['RecordNumber'];
			
			
				$query55 = "update `".$Settings['DBPrefix']."core-Users` set ForumPosts=ForumPosts+1 where RecordNumber='".$UserNumber."' limit 1";
				$result55 = mysql_query($query55) or die (mysql_error());
			
			
			
				$IPAddress = $HTTP_SERVER_VARS["REMOTE_ADDR"];
				$IPAddressParts = explode('.',$IPAddress);
				$PosterIP = dechex($IPAddressParts[0]) . dechex($IPAddressParts[1]) . dechex($IPAddressParts[2]) . dechex($IPAddressParts[3]);
				
			
				$PostTime = GetTimestamp();
			
				$query55 = "insert into `".$Settings['DBPrefix']."mb-Messages` (ThreadID,Poster,Subject,Message,PosterIP,DateCode) values ('".$FormArguments['ThreadID']."','".$UserNumber."','".$FormArguments['Subject']."','".$FormArguments['Message']."','".$PosterIP."','".$PostTime."')";
				$result55 = mysql_query($query55) or die (mysql_error());
			
			
				$query55 = "select count(RecordNumber) as MessagesInThread from `".$Settings['DBPrefix']."mb-Messages` where ThreadID='".$FormArguments['ThreadID']."' 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']));
						
			
				$query55 = "update `".$Settings['DBPrefix']."mb-Threads` set Replies=Replies+1, LastReply=".$PostTime.", LastUser='".$UserNumber."' where RecordNumber='".$FormArguments['ThreadID']."' limit 1";
				$result55 = mysql_query($query55) or die (mysql_error());
			
				$query55 = "update `".$Settings['DBPrefix']."mb-Boards` set Messages=Messages+1, LastReply=".$PostTime.", LastUser='".$UserNumber."' where RecordNumber='".$FormArguments['BoardID']."' limit 1";
				$result55 = mysql_query($query55) or die (mysql_error());
			
			
			
				$query = "select * from `".$Settings['DBPrefix']."mb-Threads` where RecordNumber=".$FormArguments['ThreadID']." limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$row = mysql_fetch_array($result);
				$BoardID = $row['BoardID'];
			
				$query55 = "select * from `".$Settings['DBPrefix']."core-Users` where (NotifyAllBoards='y' or NotifyBoards like '%-".$BoardID."-%' or NotifyThreads like '%-".$FormArguments['ThreadID']."-%') and Email != '' and RecordNumber != '".$ThisUserNumber."' and ((Level >= '".$CatMinLevel."'".$CatGroupsListSQL.") and (Level >= '".$BoardMinLevel."'".$BoardGroupsListSQL."))";				
				$result55 = mysql_query($query55) or die (mysql_error());
				while ($row55 = mysql_fetch_array($result55)) {
												
					if (strpos($row55['Email'],'@',0) !== false && strlen($row55['Email']) > 5) {
											
						if ($row55['DisplayName'] != '') {
							$UserDisplayName = $row55['DisplayName'];	
						}
						else {
							$UserDisplayName = $row55['Username'];
						}
					
									
						$Message = $UserDisplayName.','."\n\n".$MsgText[2][82]."\n\n".$MsgText[2][83].':'."\n\n".$MainURL . '/forum/thread/' . $FormArguments['ThreadID'] . '/' . $MessagePage . '.html#' . $ThreadMessageNumber."\n\n\n\n\n".$MsgText[2][84].','."\n".$Settings['SiteName']."\n".$MainURL.'/index.html';
						mail($row55['Email'], $MsgText[2][85].' '.$Settings['SiteName'].' '.$MsgText[2][1], $Message, 'From: '.$Settings['SiteName'].' <'.$Settings['ContactEmail'].'>','-f'.$Settings['ContactEmail']);
					
					}
				
				}
			
								
					
				header('Location: ' . $MainURL . '/forum/thread/' . $FormArguments['ThreadID'] . '/' . $MessagePage . '.html#' . $ThreadMessageNumber);
						
			}
			else {
			
				$PageContents = $MsgText[2][86];
			
			}
			
		} // end if allowed to post
		
		
		
		} // end message post (not preview)		
		
	}
	else {
		$PageContents = $MsgText[0][98];
	}	
			
	
	
?>
