<?php
/*---------------------------------------------------+
| PHP-Fusion 6 Content Management System
+----------------------------------------------------+
| Copyright © 2002 - 2006 Nick Jones
| http://www.php-fusion.co.uk/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------*/
if (!defined("IN_FUSION")) header("Location:../index.php");

if (isset($_POST['previewchanges'])) {
	$disable_smileys_check = isset($_POST['disable_smileys']) ? " checked" : "";
	$del_check = isset($_POST['delete']) ? " checked" : "";
	$del_attach_check = isset($_POST['delete_attach']) ? " checked" : "";

	opentable($locale['405']);
	$subject = trim(stripinput(censorwords($_POST['subject'])));
	$message = trim(stripinput(censorwords($_POST['message'])));
	if ($subject == "") $subject = $pdata['post_subject'];
	if ($message == "") {
		$previewmessage = $locale['421'];
	} else {
		$previewmessage = $message;
		if (!$disable_smileys_check) { $previewmessage = parsesmileys($previewmessage); }
		$previewmessage = parseubb($previewmessage);
		$previewmessage = nl2br($previewmessage);
	}
	$udata = dbarray(dbquery("SELECT * FROM ".$db_prefix."users WHERE user_id='".$pdata['post_author']."'"));
	$is_mod = in_array($udata['user_id'], $forum_mods) && $udata['user_level'] < "102" ? true : false;

	echo "
	<table cellspacing='0' cellpadding='0' border='0' width='100%' class='tbl-border'>
	<tr><td>	<table cellpadding='0' cellspacing='1' width='100%'>
	<tr><td width='145' class='tbl2'>".$locale['422']."</td>
	<td class='tbl2'>$subject</td></tr><tr>
	<td valign='top' rowspan='2' width='145' class='tbl1'>".$udata['user_name']."<br>
	<span class='alt'>".($is_mod ? $locale['userf1'] : getuserlevel($udata['user_level']))."</span><br><br>\n";

	if ($udata['user_avatar']) {
		echo "<img src='".IMAGES."avatars/".$udata['user_avatar']."'><br><br>\n";
		$height = "200";
	} else {
		$height = "60";
	}
	echo "<span class='alt'>".$locale['423']."</span> ".$udata['user_posts']."<br>\n";
	if ($udata['user_location']) {
		include_once INCLUDES."countries.php";
		if (!isset($ft_csm['user'][$udata['user_id']])) { $ft_csm['user'][$udata['user_id']] = csm_getLocation($udata['user_location']); }
		echo "<span class='alt'>".$locale['424']."</span> ".$ft_csm['user'][$udata['user_id']]."<br>\n";
	}
	echo "
	<span class='alt'>".$locale['425']."</span> ".showdate("%d.%m.%y", $udata['user_joined'])."</td>
	<td class='tbl1'><span class='small'>".$locale['426'].showdate("forumdate", $pdata['post_datestamp'])."</span></td>
	</tr><tr><td height='50' valign='top' class='tbl1'>$previewmessage<br>	<br>
	<span class='small'>".$locale['427'].$userdata['user_name'].$locale['428'].showdate("forumdate", time())."</span>
	</td></tr></table></td></tr></table>\n";
	closetable();
	tablebreak();
}

if (isset($_POST['savechanges'])) {
	if (isset($_POST['delete'])) {
		$result = dbquery("DELETE FROM ".$db_prefix."posts WHERE post_id='$post_id' AND thread_id='$thread_id'");
		$result = dbquery("SELECT * FROM ".$db_prefix."forum_attachments WHERE post_id='$post_id'");
		if (dbrows($result) != 0) {
			$attach = dbarray($result);
			unlink(FORUM."attachments/".$attach['attach_name']);
			$result2 = dbquery("DELETE FROM ".$db_prefix."forum_attachments WHERE post_id='$post_id'");
		}
		$posts = dbcount("(post_id)", "posts", "thread_id='$thread_id'");
		if ($posts == 0) $result = dbquery("DELETE FROM ".$db_prefix."threads WHERE thread_id='$thread_id' AND forum_id='$forum_id'");
		$result = dbquery("SELECT * FROM ".$db_prefix."forums WHERE forum_id='$forum_id' AND forum_lastuser='".$pdata['post_author']."' AND forum_lastpost='".$pdata['post_datestamp']."'");
		if (dbrows($result)) {
			$result = dbquery("SELECT forum_id,post_author,post_datestamp FROM ".$db_prefix."posts WHERE forum_id='$forum_id' ORDER BY post_datestamp DESC LIMIT 1");
			if (dbrows($result)) { 
				$pdata2 = dbarray($result);
				$result = dbquery("UPDATE ".$db_prefix."forums SET forum_lastpost='".$pdata2['post_datestamp']."', forum_lastuser='".$pdata2['post_author']."' WHERE forum_id='$forum_id'");
			} else {
				$result = dbquery("UPDATE ".$db_prefix."forums SET forum_lastpost='0', forum_lastuser='0' WHERE forum_id='$forum_id'");
			}
		}
		if ($posts > 0) {
			$result = dbquery("SELECT * FROM ".$db_prefix."threads WHERE thread_id='$thread_id' AND thread_lastpost='".$pdata['post_datestamp']."' AND thread_lastuser='".$pdata['post_author']."'");
			if (dbrows($result)) {
				$result = dbquery("SELECT thread_id,post_author,post_datestamp FROM ".$db_prefix."posts WHERE thread_id='$thread_id' ORDER BY post_datestamp DESC LIMIT 1");
				$pdata2 = dbarray($result);
				$result = dbquery("UPDATE ".$db_prefix."threads SET thread_lastpost='".$pdata2['post_datestamp']."', thread_lastuser='".$pdata2['post_author']."' WHERE thread_id='$thread_id'");
			}
		}

		opentable($locale['407']);
			echo "<center><br>\n".$locale['445']."<br><br>\n";
			if ($posts > 0) echo "Подождите, сейчас Вы будете перемещены.<br /> <a href='viewthread.php?forum_id=$forum_id&amp;thread_id=$thread_id'>(или нажмите сюда, если нехотите ждать)</a>\n</center>\n";
echo "<meta http-equiv='refresh' content='3; url=viewthread.php?forum_id=$forum_id&amp;thread_id=$thread_id'>";
		closetable();
	} else {

	for($d=0;$d<$_POST['attach_tcount'];$d++){
		$attach_id = $_POST['attach_id'][$d];
		if ($_POST['attach_option'.$d] == "delete_attach") {
			$result = dbquery("SELECT * FROM ".$db_prefix."forum_attachments WHERE post_id='$post_id' and attach_id='$attach_id'");
			if (dbrows($result) != 0) {
				$attach = dbarray($result);
				unlink(FORUM."attachments/".$attach['attach_name']);
				$result2 = dbquery("DELETE FROM ".$db_prefix."forum_attachments WHERE post_id='$post_id' and attach_id='$attach_id'");
			}
		}
	}

	$error = "0";
	$smileys = isset($_POST['disable_smileys']) ? "0" : "1";
	$subject = trim(stripinput(censorwords($_POST['subject'])));
	$message = trim(stripinput(censorwords($_POST['message'])));
	if (iMEMBER) {
		if ($subject != "" && $message != "") {
			//$result = dbquery("UPDATE ".$db_prefix."posts SET post_subject='$subject', post_message='$message', post_smileys='$smileys', post_edituser='".$userdata['user_id']."', post_edittime='".time()."' WHERE post_id='$post_id'");
			$silent_sql = ", post_edituser='".$userdata['user_id']."', post_edittime='".time()."'";
if (iADMIN && isset($_POST['silent_edit']) && $_POST['silent_edit'] == 1) {
$silent_sql = "";
}
$result = dbquery("UPDATE ".$db_prefix."posts SET post_subject='$subject', post_message='$message', post_smileys='$smileys'".$silent_sql." WHERE post_id='$post_id'");
			$data = dbarray(dbquery("SELECT * FROM ".$db_prefix."posts WHERE thread_id='$thread_id' ORDER BY post_id ASC LIMIT 1"));
			if ($data['post_id'] == $post_id) {
				$result = dbquery("UPDATE ".$db_prefix."threads SET thread_subject='$subject' WHERE thread_id='$thread_id'");
			}
			if (isset($_POST['delete_attach'])) {
				$result = dbquery("SELECT * FROM ".$db_prefix."forum_attachments WHERE post_id='$post_id'");
				if (dbrows($result) != 0) {
					$adata = dbarray($result);
					unlink(FORUM."attachments/".$adata['attach_name']);
					$result = dbquery("DELETE FROM ".$db_prefix."forum_attachments WHERE post_id='$post_id'");
				}
			}

			$attach = $_FILES['attach'];

			if ($attach['name'] != "" && !empty($attach['name']) && is_uploaded_file($attach['tmp_name'])) {
				$attachname = substr($attach['name'], 0, strrpos($attach['name'], "."));
				$attachext = strtolower(strrchr($attach['name'],"."));
				if (preg_match("/^[-0-9A-Z_\[\]]+$/i", $attachname) && $attach['size'] <= $settings['attachmax']) {
					$attachtypes = explode(",", $settings['attachtypes']);
					if (in_array($attachext, $attachtypes)) {
						$attachname = attach_exists(strtolower($attach['name']));
						move_uploaded_file($attach['tmp_name'], FORUM."attachments/".$attachname);
						chmod(FORUM."attachments/".$attachname,0644);
						if (in_array($attachext, $imagetypes) && (!@getimagesize(FORUM."attachments/".$attachname) || !@verify_image(FORUM."attachments/".$attachname))) {
							unlink(FORUM."attachments/".$attachname);
							$error = 1;
						}
						if (!$error) $result = dbquery("INSERT INTO ".$db_prefix."forum_attachments (thread_id, post_id, attach_name, attach_ext, attach_size) VALUES ('$thread_id', '$newpost_id', '$attachname', '$attachext', '".$attach['size']."')");
				} else {
					@unlink($attach['tmp_name']);
					$error = "1";
				}
			} else {
				@unlink($attach['tmp_name']);
				$error = "1";
			}
		}


for ($r=0;$r<$_POST['attach_tcount'];$r++) {
	$attach_id = $_POST['attach_id'][$r];

	if ($_POST['attach_option'.$r] == "replace") {
		$new_attach = $_FILES['new_attach'.$r];

		if ($new_attach['name'] != "" && !empty($new_attach['name']) && is_uploaded_file($new_attach['tmp_name']) && $new_attach['size'] <= $settings['attachmax']) {
			$attachext = strtolower(strrchr($new_attach['name'],"."));
			$attachtypes = explode(",", $settings['attachtypes']);

			if (in_array($attachext, $attachtypes)) {
				$result = dbquery("SELECT * FROM ".$db_prefix."forum_attachments WHERE post_id='$post_id' and attach_id='$attach_id'");

				if (dbrows($result) != 0) {
					$attach1 = dbarray($result);
					unlink(FORUM."attachments/".$attach1['attach_name']."[".$attach1['attach_id']."]".$attach1['attach_ext']);
					$result2 = dbquery("DELETE FROM ".$db_prefix."forum_attachments WHERE post_id='$post_id' and attach_id='$attach_id'");
				}

				$attachname = substr($new_attach['name'], 0, strrpos($new_attach['name'], "."));

				$attachname = $attachname.$attachext;

				$result = dbquery("INSERT
					INTO ".$db_prefix."forum_attachments
					VALUES('', '$thread_id', '$post_id', '$attachname', '$attachext', '".$new_attach['size']."')");

				$file_id = mysql_insert_id();
				move_uploaded_file($new_attach['tmp_name'], FORUM."attachments/".$attachname);
				chmod(FORUM."attachments/".$attachname,0644);

			} else {
				$error = "1";
			}
		}
	}
}

//add new attachment
for($a=0;$a<$_POST['attach_count'];$a++){
	$attach_add = $_FILES['attach_add'.$a];

	if ($attach_option_up == "add") {

		if ($attach_add['name'] != "" && !empty($attach_add['name']) && is_uploaded_file($attach_add['tmp_name']) && $attach_add['size'] <= $settings['attachmax']) {
			$attachext = strtolower(strrchr($attach_add['name'],"."));
			$attachtypes = explode(",", $settings['attachtypes']);

			if (in_array($attachext, $attachtypes)) {
				$attachname = substr($attach_add['name'], 0, strrpos($attach_add['name'], "."));
				$attachname = $attachname.$attachext;
				$result = dbquery("INSERT
					INTO ".$db_prefix."forum_attachments
					VALUES('', '$thread_id', '$post_id', '$attachname', '$attachext', '".$attach_add['size']."')");

				$file_id = mysql_insert_id();
				move_uploaded_file($attach_add['tmp_name'], FORUM."attachments/".$attachname);
				chmod(FORUM."attachments/".$attachname,0644);
			} else {
				$error = "1";
			}
		}
	}
}

				$reply_count = dbcount("(thread_id)", "posts", "post_id<='$post_id' AND thread_id='$thread_id'");
				$rstart = ($reply_count > 20 ? ((ceil($reply_count / 20)-1)*20) : "0");
			} else {
				$error = "2";
			}
		} else {
			$error = "3";
		}
		redirect("postify.php?post=edit&error=$error&forum_id=$forum_id&thread_id=$thread_id&rstart=$rstart&post_id=$post_id");
	}
} else {
	if (!isset($_POST['previewchanges'])) {
		$subject = $pdata['post_subject'];
		$message = $pdata['post_message'];
		$disable_smileys_check = ($pdata['post_smileys'] == "0" ? " checked" : "");
		$del_check = "";
	}

	opentable($locale['408']);
	echo "
	<form name='inputform' method='post' action='".FUSION_SELF."?action=edit&forum_id=$forum_id&thread_id=$thread_id&post_id=$post_id' enctype='multipart/form-data'>
	<table cellpadding='0' cellspacing='0' width='80%' class='tbl-border' align='center'><tr><td>
	<table width='100%' border='0' cellspacing='1' cellpadding='0'><tr>
	<td width='145' class='tbl2'>".$locale['460']."</td>
	<td class='tbl2' align='center'><input type='text' name='subject' value='$subject' class='textbox' maxlength='255' style='width:250px'></td>
	</tr><tr><td valign='top' width='145' class='tbl2'>".$locale['461']."</td><td class='tbl1' align='center'><textarea name='message' cols='90' rows='15' class='textbox'>$message</textarea></td>
</tr>
<tr>
<td width='145' class='tbl2'>&nbsp;</td>
<td class='tbl2' align='center'>
	<input type='button' value='b' class='button' style='font-weight:bold;width:20px;' onClick=\"addText('message', '[b]', '[/b]');\">
<input type='button' value='i' class='button' style='font-style:italic;width:20px;' onClick=\"addText('message', '[i]', '[/i]');\">
<input type='button' value='u' class='button' style='text-decoration:underline;width:20px;' onClick=\"addText('message', '[u]', '[/u]');\">
<input type='button' value='url' class='button' style='width:35px;' onClick=\"insertURL('message')\";>
<input type='button' value='img' class='button' style='width:30px;' onClick=\"addText('message', '[img]', '[/img]');\">
<input type='button' value='left' class='button' style='width:40px;' onClick=\"addText('message', '[left]', '[/left]');\">
<input type='button' value='center' class='button' style='width:45px;' onClick=\"addText('message', '[center]', '[/center]');\">
<input type='button' value='right' class='button' style='width:40px;' onClick=\"addText('message', '[right]', '[/right]');\">
<input type='button' value='small' class='button' style='width:40px;' onClick=\"addText('message', '[small]', '[/small]');\">
<input type='button' value='offtopic' class='button' style='width:50px;' onClick=\"addText('message', '[offtopic]', '[/offtopic]');\">
<input type='button' value='color' class='button' style='width:40px;' onClick=\"addText('message', '[color=]', '[/color]');\">
<input type='button' value='size' class='button' style='width:40px;' onClick=\"insertSize('message')\";>
<input type='button' value='code' class='button' style='width:40px;' onClick=\"addText('message', '[code]', '[/code]');\">
<input type='button' value='quote' class='button' style='width:45px;' onClick=\"addText('message', '[quote]', '[/quote]');\"><br /><br />";
opensidex("Все bb тэги ->","off");
echo "<div align='center'><input type='button' value='flash' class='button' style='width:45px;' onClick=\"addText('message', '[flash=400:300]', '[/flash]');\">
<input type='button' value='hide' class='button' style='width:45px;' onClick=\"addText('message', '[hide]', '[/hide]');\">
<input type='button' value='scroll' class='button' style='width:40px;' onClick=\"addText('message', '[scroll]', '[/scroll]');\">
<input type='button' value='marquee' class='button' style='width:55px;' onClick=\"addText('message', '[marquee]', '[/marquee]');\">
<input type='button' value='blink' class='button' style='width:50px;' onClick=\"addText('message', '[blink]', '[/blink]');\">
<input type='button' value='border' class='button' style='width:40px;' onClick=\"addText('message', '[border]', '[/border]');\">
<input type='button' value='s' class='button' style='font-style:italic;width:20px;' onClick=\"addText('message', '[s]', '[/s]');\">
<input type='button' value='mail' class='button' style='width:35px;' onClick=\"addText('message', '[mail]', '[/mail]');\">
<input type='button' value='magnet' class='button' style='width:50px;' onClick=\"insertMagnet('message')\";></div>
";
closesidex();
echo"</td></tr><tr><td width='145' class='tbl2'>&nbsp;</td><td class='tbl1'>
	".$locale['462']."<select name='bbcolor' class='textbox' style='width:90px;' onChange=\"addText('message', '[color=' + this.options[this.selectedIndex].value + ']', '[/color]');this.selectedIndex=0;\">

	<option value=''>Default</option>
<option value='black' style='background-color:black'>Black</option>
<option value='white' style='background-color:white'>White</option>
<option value='skyblue' style='background-color:skyblue'>Skyblue</option>
<option value='royalblue' style='background-color:royalblue'>Royalblue</option>
<option value='blue' style='background-color:blue'>Blue</option>
<option value='darkblue' style='background-color:darkblue'>Darkblue</option>
<option value='purple' style='background-color:purple'>Purple</option>
<option value='orange' style='background-color:orange'>Orange</option>
<option value='yellow' style='background-color:yellow'>Yellow</option>
<option value='gold' style='background-color:gold'>Gold</option>
<option value='tomato' style='background-color:tomato'>Tomato</option>
<option value='coral' style='background-color:coral'>Coral</option>
<option value='orangered' style='background-color:orangered'>Orangered</option>
<option value='crimson' style='background-color:crimson'>Crimson</option>
<option value='red' style='background-color:red'>Red</option>
<option value='firebrick' style='background-color:firebrick'>Firebrick</option>
<option value='darkred' style='background-color:darkred'>Darkred</option>
<option value='green' style='background-color:green'>Green</option>
<option value='limegreen' style='background-color:limegreen'>Limegreen</option>
<option value='seagreen' style='background-color:seagreen'>Seagreen</option>
<option value='deeppink' style='background-color:deeppink'>Deeppink</option>
<option value='purple' style='background-color:purple'>Purple</option>
<option value='indigo' style='background-color:indigo'>Indigo</option>
<option value='burlywood' style='background-color:burlywood'>Burlywood</option>
<option value='sandybrown' style='background-color:sandybrown'>Sandybrown</option>
<option value='sienna' style='background-color:sienna'>Sienna</option>
<option value='choclate' style='background-color:chocolate'>Chocolate</option>
<option value='teal' style='background-color:teal'>Teal</option>
<option value='silver' style='background-color:silver'>Silver</option>
<option value='gray' style='background-color:gray'>Gray</option>
	</select>
</td>
</tr>
<tr>
<td width='145' class='tbl2'>&nbsp;</td>
<td class='tbl2'>
<div align='center'>".displaysmileys("message")."<br /><br /></div>";
opensidex("Все смайлы ->","off");
echo "".displayemo("message")."";
closesidex();
echo "</td></tr><tr>
<td valign='top' width='145' class='tbl2'>".$locale['463']."</td>
	<td class='tbl1'>

	<input type='checkbox' id='disable_smileys' name='disable_smileys' value='1'$disable_smileys_check style='vertical-align:middle'>
<label for='disable_smileys'>".$locale['483']."</label><br>\n";
if (iADMIN) {
echo "<input type='checkbox' id='silent_edit' name='silent_edit' value='1' style='vertical-align:middle'>
<label for='silent_edit'> Неотображать \"[Изменил(а)...\"</label><br>\n";
}
echo "<input type='checkbox' id='delete' name='delete' value='1'$del_check style='vertical-align:middle'>
<label for='delete'>".$locale['482']."</label>

	";

	$result = dbquery("SELECT * FROM ".$db_prefix."forum_attachments WHERE post_id='$post_id'");
	if (dbrows($result) != 0) {

	echo "</td></tr><tr><td valign='top' width='145' class='tbl2'>Опции</td><td class='tbl1'>";

	$x=0;
	while ($attach = dbarray($result)){
		if ($attach['attach_ext'] == ".gif" || $attach['attach_ext'] == ".jpg" || $attach['attach_ext'] == ".png") {
			$view_attach = "<br><hr>
			<a href='".FORUM."attachments/".$attach['attach_name']."' target='_blank'>
			<img src='".FORUM."attachments/".$attach['attach_name']."'
			width='100' height='100' alt=''>
			</a>";
		} else {
			$view_attach = "<br><hr>
			- <a href='".FORUM."attachments/".$attach['attach_name']."'>
			".$attach['attach_name']."
			</a>";
		}
		echo "$view_attach <br>

		<input type='hidden' name='attach_id[]' value='".$attach['attach_id']."'>
		<input type='radio' name='attach_option".$x."' value='none' checked> Сохранить файл<br>";
		if ($settings['attachments'] == "1") {
			echo "<input type='radio' name='attach_option".$x."' value='replace'> Заменить другим файлом: <input type='file' name='new_attach".$x."' enctype='multipart/form-data' class='textbox' style='width:200px;'><br>";
		}
		echo "<input type='radio' name='attach_option".$x."' value='delete_attach'> Удалить<hr>";
		$x++;
	}

	$tcount = dbrows($result);
	echo "
	<input type='hidden' name='attach_tcount' value='".$tcount."'>\n";
}

$settings3 = dbarray(dbquery("SELECT * FROM ".$db_prefix."attachment_settings"));
if ($settings3['allow_multi'] == "1") {
	$scount = $settings3['files_num'];
}else{
	$scount = "1";
}

if ($settings['attachments'] == "1" && dbrows($result) < $scount) {
	echo "</td></tr><tr><td width='145' class='tbl2'>".$locale['464']."</td><td class='tbl1'>";
	if ($settings3['allow_multi'] == "1") {
		$fcount = $settings3['files_num'] - dbrows($result);
	}else{
		$fcount = "1";
	}

	for($i=0;$i<$fcount;$i++){
		echo "
		<input type='file' name='attach_add".$i."' enctype='multipart/form-data'
		class='textbox' style='margin-bottom:4px;width:280px;'><br>";
	}
	echo "
	<input type='hidden' name='attach_count' value='".$fcount."'>
	<input type='hidden' name='attach_option_up' value='add'>\n";
}
echo"
</td></tr></table></td></tr></table><table cellpadding='0' cellspacing='0' width='100%'>
<tr><td align='center' colspan='2' class='tbl1'>

<input type='submit' name='previewchanges' value='".$locale['405']."' class='button'>
<input type='submit' name='savechanges' value='".$locale['409']."' class='button'>

</td></tr></table></form>\n";

closetable();
}
?>