<?php
if(isset($access['for_t_ed']) && isset($_GET['act']) && isset($_GET['ok']) && $_GET['act']=='mesto' && isset($_POST['razdel']) && is_numeric($_POST['razdel']) && (mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_r` WHERE `id` = '".intval($_POST['razdel'])."'"),0)==1 && $user['level']>0 || mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_r` WHERE `id` = '".intval($_POST['razdel'])."' WHERE `id_forum`='$forum[id]'"),0)==1 )){
	$razdel_new=mysql_fetch_array(mysql_query("SELECT * FROM `forum_r` WHERE `id` = '".intval($_POST['razdel'])."' LIMIT 1"));

mysql_query("UPDATE `forum_p` SET `id_forum` = '$razdel_new[id_forum]', `id_razdel` = '$razdel_new[id]' WHERE `id_forum` = '$forum[id]' AND `id_razdel` = '$razdel[id]' AND `id_them` = '$them[id]'");
mysql_query("UPDATE `forum_t` SET `id_forum` = '$razdel_new[id_forum]', `id_razdel` = '$razdel_new[id]' WHERE `id_forum` = '$forum[id]' AND `id_razdel` = '$razdel[id]' AND `id` = '$them[id]'");

$forum=mysql_fetch_array(mysql_query("SELECT * FROM `forum_f` WHERE `id` = '$razdel_new[id_forum]' LIMIT 1"));
$razdel=mysql_fetch_array(mysql_query("SELECT * FROM `forum_r` WHERE `id` = '$razdel_new[id]' LIMIT 1"));
$them=mysql_fetch_array(mysql_query("SELECT * FROM `forum_t` WHERE `id_razdel` = '$razdel[id]' AND `id` = '$them[id]' LIMIT 1"));

msg('Тема успешно перемещена');
}


if(($user['level']>$ank2['level'] || $user['level']==4) &&  isset($_GET['act']) && isset($_GET['ok']) && $_GET['act']=='delete'){
mysql_query("DELETE FROM `forum_t` WHERE `id` = '$them[id]'");
mysql_query("DELETE FROM `forum_p` WHERE `id_them` = '$them[id]'");
msg('Тема успешно удалена');

aut();
echo '<div class="menu">';
err();

echo '&#187;&nbsp;<a href="/forum/'.$forum['id'].'/'.$razdel['id'].'/">В раздел</a><br/>';
echo '&#187;&nbsp;<a href="/forum/'.$forum['id'].'/">В подфорум</a><br/>';
echo '&#187;&nbsp;<a href="/forum/">В форум</a></div>';
include_once '../sys/inc/tfoot.php';
}


if(isset($_GET['act']) && isset($_GET['ok']) && $_GET['act']=='set' && isset($_POST['name'])){
	$name=esc(stripcslashes(htmlspecialchars($_POST['name'])));

if(isset($_POST['translit1']) && $_POST['translit1']==1){
	$name=translit($name);
	}

if(strlen2($name)<3){
	$err='Слишком короткое название';
	}

if(strlen2($name)>32){
	$err='Слишком днинное название';
	}

$name=mysql_real_escape_string($name);

if($user['level']>0){

if(isset($_POST['up']) && $_POST['up']==1){
	$up=1;
	}else{
		$up=0;
		}

$add_q=" `up` = '$up',";

}else{
	$add_q=NULL;
	}

if(isset($_POST['close']) && $_POST['close']==1){
	$close=1;
	}else{
		$close=0;
		}

if(isset($_POST['autor']) && $_POST['autor']==1){
	$autor=$user['id'];
	}else{
		$autor=$ank2['id'];
		}

if(!isset($err)){
	mysql_query("UPDATE `forum_t` SET `name` = '$name', `id_user` = '$autor',$add_q `close` = '$close' WHERE `id` = '$them[id]' LIMIT 1");
	$them=mysql_fetch_array(mysql_query("SELECT * FROM `forum_t` WHERE `id` = '$them[id]' LIMIT 1"));
	$ank2=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$them[id_user]' LIMIT 1"));
	msg('Изменения успешно приняты');
	}
	}

if(isset($access['for_p_ed']) && isset($_GET['act']) && $_GET['act']=='post_delete' && isset($_GET['ok'])){
foreach ($_POST as $key => $value){

if(ereg('^post_([0-9]*)$',$key,$postnum) && $value='1'){
	$delpost[]=$postnum[1];
	}
	}

if(isset($delpost) && is_array($delpost) && mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_p` WHERE `id_them` = '$them[id]' AND `id_forum` = '$forum[id]' AND `id_razdel` = '$razdel[id]'"),0)>count($delpost)){
	mysql_query("DELETE FROM `forum_p` WHERE `id_them` = '$them[id]' AND (`id` = '".implode("'".' OR `id` = '."'", $delpost)."') LIMIT ".count($delpost));
	msg('Успешно удалено '.count($delpost).' постов');

aut();
echo '<div class="menu">';
err();

echo '&#187;&nbsp;<a href="/forum/'.$forum['id'].'/'.$razdel['id'].'/'.$them['id'].'/">Вернуться в тему</a><br/>';
echo '&#187;&nbsp;<a href="/forum/'.$forum['id'].'/'.$razdel['id'].'/">В раздел</a><br/>';
echo '&#187;&nbsp;<a href="/forum/'.$forum['id'].'/">В подфорум</a><br/>';
echo '&#187;&nbsp;<a href="/forum/">В форум</a></div>';
include_once '../sys/inc/tfoot.php';
}else{
	$err='Нельзя удалить 0 или все посты из темы';
	}
	}

if(isset($_GET['act']) && $_GET['act']=='post_delete' && isset($access['for_p_ed'])){
	echo '<form method="post" action="/forum/'.$forum['id'].'/'.$razdel['id'].'/'.$them['id'].'/?act=post_delete&amp;ok">';
	}
?>