<?

/*
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/create new thread
	// ========================================================================================
	
			
	ReadFormArguments();
	Global $FormArguments;
			
	if (count($FormArguments) > 0) {
	
		if ($FormArguments['ShowPreview'] != '') {
		// display preview instead of posting the message
	
			include_once('./inc-mb-thread_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['BoardID'] = intval($FormArgumentsp['BoardID']);
			
						
			$query = "select * from `".$Settings['DBPrefix']."mb-Boards` where RecordNumber=".$FormArguments['BoardID']." limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			
			$BoardID = $row['RecordNumber'];
			$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 = $mbSettings['PageTitle'];
			$BrowserTitle = $mbSettings['PageTitle'] . ' - '.$MsgText[2][94];
			$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> > '.$MsgText[2][94];


		
				$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/board 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'];
			
			
			
			
			
			$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();
			$FormArguments['Subject'] = substr($FormArguments['Subject'],0,255);
			
			
			if ($FormArguments['StickyThread'] == 'yes') {
				$StickySetting = 'y';			
			}
			
			
			$query55 = "insert into `".$Settings['DBPrefix']."mb-Threads` (BoardID,StartedBy,StartedDate,Title,LastReply,LastUser,Replies,Views,Sticky) values ('".$FormArguments['BoardID']."','".$UserNumber."','".$PostTime."','".$FormArguments['Subject']."','".$PostTime."','".$UserNumber."','0','0','".$StickySetting."')";
			$result55 = mysql_query($query55) or die (mysql_error());
		
			$query = "select RecordNumber from `".$Settings['DBPrefix']."mb-Threads` where Title='".$FormArguments['Subject']."' and LastReply='".$PostTime."' and StartedBy='".$UserNumber."' limit 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			$ThreadID = $row['RecordNumber'];
			
			
			$query55 = "insert into `".$Settings['DBPrefix']."mb-Messages` (ThreadID,Poster,Subject,Message,PosterIP,DateCode) values ('".$ThreadID."','".$UserNumber."','".$FormArguments['Subject']."','".$FormArguments['Message']."','".$PosterIP."','".$PostTime."')";
			$result55 = mysql_query($query55) or die (mysql_error());
								
				
			$query55 = "update `".$Settings['DBPrefix']."mb-Boards` set Threads=Threads+1, Messages=Messages+1, LastReply=".$PostTime.", LastUser='".$UserNumber."' where RecordNumber='".$FormArguments['BoardID']."' limit 1";
			$result55 = mysql_query($query55) or die (mysql_error());
			
			
			
			
			$query55 = "select RecordNumber from `".$Settings['DBPrefix']."core-Users` where NotifyAllBoards='y' or NotifyBoards like '%-".$FormArguments['BoardID']."-%'";
			$result55 = mysql_query($query55) or die (mysql_error());
			if (mysql_num_rows($result55) > 0) {
				// read user emails from the database
				$query = "select RecordNumber,Username,DisplayName,Email,Level from `".$Settings['DBPrefix']."core-Users`";
				$result = mysql_query($query) or die (mysql_error());
				while ($row = mysql_fetch_array($result)) {
	
					if (strpos($row['Email'],'@',0) !== false && strlen($row['Email']) > 5) {
						$UserEmails[$row['RecordNumber']] = $row['Email'];
					}
					
					$UserLevels[$row['RecordNumber']] = $row['Level'];
					
					if ($row['DisplayName'] != '') {
						$DisplayNames[$row['RecordNumber']] = $row['DisplayName'];	
					}
					else {
						$DisplayNames[$row['RecordNumber']] = $row['Username'];
					}
		
				}
			}
			while ($row55 = mysql_fetch_array($result55)) {
			
				if ($row55['RecordNumber'] != $ThisUserNumber && $UserEmails[$row55['RecordNumber']] != '' && $UserLevels[$row55['RecordNumber']] >= $BMinLevel && $UserLevels[$row55['RecordNumber']] >= $CMinLevel) {
					
					$Message = $DisplayNames[$row55['RecordNumber']].','."\n\n".$MsgText[2][82]."\n\n".$MsgText[2][83].':'."\n\n".$MainURL . '/forum/thread/' . $ThreadID . '/1.html'."\n\n\n\n\n".$MsgText[2][84].','."\n".$Settings['SiteName']."\n".$MainURL.'/index.html';
					mail($UserEmails[$row55['RecordNumber']], $MsgText[2][85].' '.$Settings['SiteName'].' '.$MsgText[2][1], $Message, 'From: '.$Settings['SiteName'].' <'.$Settings['ContactEmail'].'>','-f'.$Settings['ContactEmail']);
					
				}
				
			}
			
			
			
					
			
			header('Location: ' . $MainURL . '/forum/thread/'.$ThreadID.'.html');		
	
		
		} // end if allowed to post
		
		
		} // end post message (not preview)
		
	}
	else {
		$PageContents = $MsgText[0][98];
	}	
	
?>
