<?php
session_start();
header( 'Content-type: text/html; charset=windows-1251' );
error_reporting( 1 );
include_once($_SERVER['DOCUMENT_ROOT']."/sfb.php");
include_once($_SERVER['DOCUMENT_ROOT']."/funciones.php");

$id_user = (isset($_SESSION["iduser"]) && preg_match("|^[\d]{1,11}$|", trim($_SESSION["iduser"]))) ? intval(trim($_SESSION["iduser"])) : false;

if(!isset($_SESSION['iduser'])) { exit(); }
if(!isset($_POST['func'])) { exit(); }
if( $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) { exit(); }

$sql = mysql_query("SELECT `reyting`, `user_status`, `forum_status` FROM `b_users` WHERE `id`='$id_user' LIMIT 1") or die(mysql_error());
$row = mysql_fetch_assoc($sql);
$my_reyting = $row["reyting"];
$us_ad_status = $row["user_status"];
$my_forum_st = $row["forum_status"];
unset($sql, $row);

#вывод смайлов
if($_POST['func'] == 'smyles'){
$idis = validetext($_POST["idis"]);
echo '<span class="f-smyles-del" onclick="hide_smyle(); return false;"></span>';
echo "<span style='display: block;' id='smyles-view'>";
for($x = 1; $x <= 50; $x++){
echo '<a href="javascript:smyle(`[:'.$x.':]`, `'.$idis.'`);">'.smyle_v($x).'</a>';
}
echo "</span>";
}

#добавление сообщения
if($_POST['func'] == 'reply'){
$id = (isset($_POST["id"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["id"]))) ? intval(trim($_POST["id"])) : false;
$msg = iconv("UTF-8", "windows-1251", check_text($_POST["msg"]));
$msg = validetext($msg);
$msg = text_limit($msg, 10000);
if ($id != false){
$sql = mysql_query("SELECT `id` FROM `b_forum_topic` WHERE `id`='$id' AND `status`='0' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
unset($sql);
$sql = mysql_query("SELECT `id_user` FROM `b_forum_topic_answer` WHERE `id_topic`='$id' ORDER BY `id` DESC LIMIT 1") or die(mysql_error());
$last_post = mysql_result($sql,0);
if($last_post != $id_user){
if($my_reyting >= $min_f_reyt OR ($us_ad_status == "admin" OR $my_forum_st == "admin" OR $my_forum_st == "moder")){
mysql_query("INSERT INTO `b_forum_topic_answer` (`id_topic`, `id_user`, `message`, `time`) VALUES ('$id', '$id_user', '$msg', '".time()."')") or die(mysql_error());
die('4');
}else{ die('3');}
}else{ die('2');}
}else{ die('1');}
}else{ die('1');}
}

#добавление темы
if($_POST['func'] == 'create'){
$id = (isset($_POST["id"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["id"]))) ? intval(trim($_POST["id"])) : false;
$topic = iconv("UTF-8", "windows-1251", check_text($_POST["topic"]));
$topic = validetext($topic);
$msg = iconv("UTF-8", "windows-1251", check_text($_POST["msg"]));
$msg = validetext($msg);
$topic = text_limit($topic, 70);
$msg = text_limit($msg, 10000);
if ($id != false){
$sql = mysql_query("SELECT `id` FROM `b_forum_sub_sect` WHERE `id`='$id' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
unset($sql);
if($my_reyting >= $min_f_t_reyt OR ($us_ad_status == "admin" OR $my_forum_st == "admin" OR $my_forum_st == "moder")){
mysql_query("INSERT INTO `b_forum_topic` (`id_sub`, `id_user`, `topic`, `time`) VALUES ('$id', '$id_user', '$topic', '".time()."')") or die(mysql_error());
$id_topic = mysql_result(mysql_query("SELECT `id` FROM `b_forum_topic` WHERE `id_sub`='$id' AND `id_user`='$id_user' ORDER BY `id` DESC LIMIT 1"),0);
mysql_query("INSERT INTO `b_forum_topic_answer` (`id_topic`, `id_user`, `message`, `time`) VALUES ('$id_topic', '$id_user', '$msg', '".time()."')") or die(mysql_error());
echo $id_topic;
}else{ die('2');}
}else{ die('1');}
}else{ die('1');}
}

#открыть тему
if($_POST['func'] == 'open_topic'){
$id = (isset($_POST["id"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["id"]))) ? intval(trim($_POST["id"])) : false;
if ($id != false){
$sql = mysql_query("SELECT `id` FROM `b_forum_topic` WHERE `id`='$id' AND `status`='1' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
if($us_ad_status == "admin" OR $my_forum_st == "admin" OR $my_forum_st == "moder"){
mysql_query("UPDATE `b_forum_topic` SET `status`='0' WHERE `id`='$id'") or die(mysql_error());
die('1');
}
}
}
}

#закрыть тему
if($_POST['func'] == 'close_topic'){
$id = (isset($_POST["id"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["id"]))) ? intval(trim($_POST["id"])) : false;
if ($id != false){
$sql = mysql_query("SELECT `id` FROM `b_forum_topic` WHERE `id`='$id' AND `status`='0' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
if($us_ad_status == "admin" OR $my_forum_st == "admin" OR $my_forum_st == "moder"){
mysql_query("UPDATE `b_forum_topic` SET `status`='1' WHERE `id`='$id'") or die(mysql_error());
die('1');
}
}
}
}

#удалить тему
if($_POST['func'] == 'dell_topic'){
$id = (isset($_POST["id"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["id"]))) ? intval(trim($_POST["id"])) : false;
if ($id != false){
$sql = mysql_query("SELECT `id` FROM `b_forum_topic` WHERE `id`='$id' AND `status`='1' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
if($us_ad_status == "admin" OR $my_forum_st == "admin"){
mysql_query("DELETE FROM `b_forum_topic` WHERE `id`='$id' AND `status`='1'") or die(mysql_error());
mysql_query("DELETE FROM `b_forum_topic_answer` WHERE `id_topic`='$id'") or die(mysql_error());
die('1');
}
}
}
}

#удалить сообщение
if($_POST['func'] == 'dell_post'){
$id = (isset($_POST["id"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["id"]))) ? intval(trim($_POST["id"])) : false;
if ($id != false){
$sql = mysql_query("SELECT `id` FROM `b_forum_topic_answer` WHERE `id`='$id' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
if($us_ad_status == "admin" OR $my_forum_st == "admin" OR $my_forum_st == "moder"){
mysql_query("DELETE FROM `b_forum_topic_answer` WHERE `id`='$id'") or die(mysql_error());
die('1');
}
}
}
}

#редактировать сообщение
if($_POST['func'] == 'edit_post'){
$id = (isset($_POST["id"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["id"]))) ? intval(trim($_POST["id"])) : false;
if ($id != false){
$sql = mysql_query("SELECT `id`, `message` FROM `b_forum_topic_answer` WHERE `id`='$id' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
$row = mysql_fetch_assoc($sql);
$id_post = $row["id"];
$message = $row["message"];
echo <<<form
<table class='f-reply' width='100%'>
<tr><td>
<span class='f-smyles' onclick='smyles(`f-edit_area-$id_post`);'></span>
<span class="btn-bold" title="Выделить жирным" onClick="wrapText('f-edit_area-$id_post','[b]','[/b]','counter-$id_post'); return false;"></span>
<span class="btn-italic" title="Выделить курсивом" onClick="wrapText('f-edit_area-$id_post','[i]','[/i]','counter-$id_post'); return false;"></span>
<span class="btn-uline" title="Выделить подчёркиванием" onClick="wrapText('f-edit_area-$id_post','[u]','[/u]','counter-$id_post'); return false;"></span>
<span class="btn-url" title="Выделить URL" onClick="wrapText('f-edit_area-$id_post','[url]','[/url]','counter-$id_post'); return false;"></span>
<input class="f-scount" type="text" id="counter-$id_post" value="Осталось 30000 символов" readonly="readonly" />
</td></tr>
<tr><td>
<textarea id='f-edit_area-$id_post' maxlength='30000' onkeyup="descchange('f-edit_area-$id_post','counter-$id_post');">$message</textarea>
</td></tr><tr><td>
<div style='float:right;display: block;'>
<a class='f-quote' href='javascript:void(0);' style='display: block;margin: 0;' onclick="f_save_post_edit('$id_post');">Принять исправления</a>
</div>
</td></tr></table>
form;
}
}
}

#редактировать сообщение
if($_POST['func'] == 'save_edit_post'){
$id = (isset($_POST["id"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["id"]))) ? intval(trim($_POST["id"])) : false;
$msg = iconv("UTF-8", "windows-1251", check_text($_POST["msg"]));
$msg = validetext($msg);
if ($id != false){
mysql_query("UPDATE `b_forum_topic_answer` SET `message`='$msg', `id_chenge`='$id_user',`time_chenge`='".time()."' WHERE `id`='$id'") or die(mysql_error());
$f_mess = view_text($msg);
$f_mess = smyle_cng($f_mess);
echo quotes($f_mess);
}
}

#редактировать репутации
if($_POST['func'] == 'edit_rep'){
$id = (isset($_POST["id"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["id"]))) ? intval(trim($_POST["id"])) : false;
$msg = iconv("UTF-8", "windows-1251", check_text($_POST["msg"]));
$msg = validetext($msg);
$type = (isset($_POST["type"]) && preg_match("|^[\d]{1,11}$|", trim($_POST["type"])) && $_POST["type"] == 1 | $_POST["type"] == 2) ? intval(trim($_POST["type"])) : false;
if ($id != false AND $msg != '' AND $type != false){
if($id != $id_user) {
$msg = text_limit($msg, 10000);
$sql = mysql_query("SELECT `id` FROM `b_forum_reputation` WHERE `id_from`='$id_user' AND `id_to`='$id' AND `time`>'".(time()-24*3600)."' ORDER BY `time` DESC LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sql) == 0){
mysql_query("INSERT INTO `b_forum_reputation` (`id_from`,`id_to`,`text`,`type`,`time`) VALUES ('$id_user','$id','$msg','$type','".time()."')") or die(mysql_error());
die('2');
}else{ die('1'); }
}
}
}
?>