<?php
session_start();
header( 'Content-type: text/html; charset=windows-1251' );
error_reporting( 1 );
require($_SERVER['DOCUMENT_ROOT']."/sfb.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(); }

if($_POST['func'] == "summ") {
if ($komu == 0){
	echo "2";}}
// добавление реферера
if($_POST['func'] == "add_referer") {
$id = valideint($_POST["id"]);
$sql = mysql_query("SELECT `referer`,`new_rb` FROM `b_users` WHERE `id`='$id'") or die(mysql_error());
$us_ref = mysql_fetch_assoc($sql);
$us_ref = $row["referer"];
$new_rb_n = $row["new_rb"];
$exist_us = mysql_num_rows($sql);
if($us_ref == $id_user){
echo "5";exit();
}elseif($id == $id_user){
echo "4";exit();
}elseif($referer > 0){
echo "3";exit();
}elseif($exist_us < 1 OR $id == ''){
echo "2";exit();
}else{
mysql_query("UPDATE `b_users` SET `referals`=`referals`+'1' WHERE `id`='$id'") or die(mysql_error());
mysql_query("UPDATE `b_users` SET `referer`='$id',`autoproc`='$new_rb_n' WHERE `id`='$id_user'") or die(mysql_error());
echo "1";
}
}
// удаление отзыва
if($_POST['func'] == "dell_comm") {
$id = valideint($_POST["id"]);

if($user_status == "admin") {
mysql_query("DELETE FROM `b_comment` WHERE `id_from`='$id'") or die(mysql_error());
echo "1";exit();
}

$sql = mysql_query("SELECT `id_from`, `id_to` FROM `b_comment` WHERE `id`='$id' AND (`id_from`='$id_user' OR `id_to`='$id_user')") or die(mysql_error());
$row = mysql_fetch_assoc($sql);
$exist_comm = mysql_num_rows($sql);
$coun_reyt3 = mysql_result(mysql_query("SELECT `min_reyt` FROM `b_status` WHERE `id`='3'"),0,0);
$coun_reyt4 = mysql_result(mysql_query("SELECT `min_reyt` FROM `b_status` WHERE `id`='4'"),0,0);
if($exist_comm == 0) {
echo "3";exit();
}elseif($id_user == $row["id_from"] AND $reyting < $coun_reyt3){
echo "2";exit();
}elseif($id_user == $row["id_to"] AND $reyting < $coun_reyt4){
echo "2";exit();
}else{
mysql_query("DELETE FROM `b_comment` WHERE `id_from`='$id'") or die(mysql_error());
echo "1";exit();
}
}
// добавление отзыва
if($_POST['func'] == "add_comm") {
$id_wall = valideint($_POST["id_wall"]);
$text = strip_tags($_POST["text"]);
$text = iconv('UTF-8', 'windows-1251', $text);
$commwhat = valideint($_POST["commwhat"]);
if($commwhat == 1) {$bg_color = "#E5F9B1"; $img_comm = "<img src='/css/images/good-comm.png' title='Положительный' align='absmiddle'>";}else{$bg_color = "#F9CEB1"; $img_comm = "<img src='/css/images/bad-comm.png' title='Отрицательный' align='absmiddle'>";}
$coun_reyt2 = mysql_result(mysql_query("SELECT `min_reyt` FROM `b_status` WHERE `id`='2'"),0,0);
$coun_reyt3 = mysql_result(mysql_query("SELECT `min_reyt` FROM `b_status` WHERE `id`='3'"),0,0);
$num_comm = mysql_num_rows(mysql_query("SELECT `id` FROM `b_comment` WHERE `id_from`='$id_user' AND `id_to`='$id_wall'"));

$sql = mysql_query("SELECT * FROM `b_users` WHERE `id`='$id_wall'") or die(mysql_error());
if(mysql_num_rows($sql) == 0){
echo "1";exit();
}elseif($reyting <= $coun_reyt2){
echo "2";exit();
}elseif($id_user == $id_wall){
echo "3";exit();
}elseif($num_comm > 0){
echo "4";exit();
}elseif($my_comm_war >= 5){
echo "5";exit();
}else{
mysql_query("INSERT INTO `b_comment` (`type`, `what`, `id_from`, `id_to`, `text`, `date`) VALUES ('1', '$commwhat', '$id_user', '$id_wall', '$text', '".time()."')") or die(mysql_error());

$wall_id = mysql_result(mysql_query("SELECT `id` FROM `b_comment` WHERE `id_from`='$id_user' AND `id_to`='$id_wall'"),0);
$ad_like = mysql_num_rows(mysql_query("SELECT `id` FROM `b_comment_like` WHERE `id_comm`='$wall_id' AND `id_liker`='$id_user'"));
$good_like = mysql_num_rows(mysql_query("SELECT `id` FROM `b_comment_like` WHERE `id_comm`='$wall_id' AND `what`='1'"));
$bad_like = mysql_num_rows(mysql_query("SELECT `id` FROM `b_comment_like` WHERE `id_comm`='$wall_id' AND `what`='2'"));

echo "<tr id='id_comm_$id_user'><td colspan='3'>";
echo "<table class='us-wall-tab'><tr style='border:1px solid #ccc;'>";
echo "<td valign='top' align='center' width='100px;'><a href='/users-$id_user-wall'><span style='color:#8DC109;font-weight: bold;'>".ucfirst($byme["username"])."</span></a>";
echo "<td><div style='text-align:left;margin-left:5px;color:#EF47DE;'>Отзыв добавлен: <b>".date("H:i d-m-Y",time())."</b> $img_comm</div></td>";
echo "<td align='right'>( <span id='good-$wall_id' style='color:#609046;cursor:help;' title='Положительных'>$good_like</span> ) ";
echo "<span id='like-1-$wall_id' style='cursor:pointer;' onclick='like_comm(\"$wall_id\",1);'><img src='/css/img/like_g.png' align='absmiddle'></span> ";
echo "( <span id='bad-$wall_id' style='color:#FF0000;cursor:help;' title='Отрицательных'>$bad_like</span> ) ";
echo "<span id='like-2-$wall_id' style='cursor:pointer;' onclick='like_comm(\"$wall_id\",2);'><img src='/css/img/like_b.png' align='absmiddle'></span></td>";
echo "<td width='15px'>";
if($id_user != false){
if(($user_status == "admin") OR ($reyting >= $coun_reyt3)){
echo "<img src='/css/img/cross.png' title='Удалить отзыв' style='cursor:pointer;' onclick='dell_comment($id_user);' />";
} }
echo "</td>";
echo "</tr><tr>";
echo "<td valign='top' align='center' style='border-bottom: 1px solid #ccc;padding-bottom:5px;'><img src='$avatar' border='0' width='60' height='60' alt='avatar' /></td>";
echo "<td valign='top' colspan='3' style='border-bottom: 1px solid #ccc;padding-bottom:5px;'><div style='background:$bg_color;padding:7px;border:1px solid #ccc;border-radius:5px 15px;text-indent: 0.7em;color:#7C2073;'>".nl2br(bbTags(smile($text)))."</div></td>";
echo "</tr></table></td></tr>";
}
}
// подгрузка отзыва
if($_POST['func'] == "load_comm") {
$num = valideint($_POST["num"]);
$id_wall = valideint($_POST["id_wall"]);
$coun_reyt3 = mysql_result(mysql_query("SELECT `min_reyt` FROM `b_status` WHERE `id`='3'"),0,0);
$result = mysql_query("SELECT * FROM `b_comment` WHERE `id_to`='$id_wall' ORDER BY `id` DESC LIMIT $num, 5"); //Вытаскиваем из таблицы 10 комментариев начиная с $num
if(mysql_num_rows($result) > 0){            
while($row = mysql_fetch_assoc($result)){
if($row["what"] == 1) {$bg_color = "#E5F9B1"; $img_comm = "";}else{$bg_color = "#F9CEB1"; $img_comm = "";}
$usql = mysql_query("SELECT * FROM `b_users` WHERE `id`='".$row["id_from"]."'") or die(mysql_error());
$urow = mysql_fetch_assoc($usql);
$ad_like = mysql_num_rows(mysql_query("SELECT `id` FROM `b_comment_like` WHERE `id_comm`='".$row["id"]."' AND `id_liker`='$id_user'"));
$good_like = mysql_num_rows(mysql_query("SELECT `id` FROM `b_comment_like` WHERE `id_comm`='".$row["id"]."' AND `what`='1'"));
$bad_like = mysql_num_rows(mysql_query("SELECT `id` FROM `b_comment_like` WHERE `id_comm`='".$row["id"]."' AND `what`='2'"));

echo "<tr id='id_comm_".$row["id_from"]."'><td colspan='3'>";
echo "<table class='us-wall-tab'><tr style='border:1px solid #ccc;'>";
echo "<td valign='top' align='center' width='100px;'><a href='/users-".$urow["id"]."-wall'><span style='color:#8DC109;font-weight: bold;'>".ucfirst($urow["username"])."</span></a>";
echo "<td><div style='text-align:left;margin-left:5px;color:#EF47DE;'>Отзыв добавлен: <b>".date("H:i d-m-Y",$row["date"])."</b> $img_comm</div></td>";
echo "<td align='right'>(<span id='good-".$row["id"]."' style='color:#609046;cursor:help;' title='Положительных'><b>$good_like</b></span>) ";
if($ad_like == 0){ echo "<span id='like-1-".$row["id"]."' style='cursor:pointer;' onclick='like_comm(\"".$row["id"]."\",1);'><img src='/css/img/like_g.png' align='absmiddle'></span> ";}
echo "(<span id='bad-".$row["id"]."' style='color:#FF0000;cursor:help;' title='Отрицательных'><b>$bad_like</b></span>) ";
if($ad_like == 0){ echo "<span id='like-2-".$row["id"]."' style='cursor:pointer;' onclick='like_comm(\"".$row["id"]."\",2);'><img src='/css/img/like_b.png' align='absmiddle'></span></td>";}
echo "<td width='15px'>";
if(($user_status == "admin") OR ($reyting >= $coun_reyt3)){
echo "<img src='/css/img/cross.png' title='Удалить отзыв' style='cursor:pointer;' onclick='dell_comment(".$row["id_from"].");' />";
}
echo "</td>";
echo "</tr><tr>";
echo "<td valign='top' align='center' style='border-bottom: 1px solid #ccc;padding-bottom:5px;'><img src='/avp/".$urow["avatar"]."' border='0' width='60' height='60' alt='avatar' /></td>";
echo "<td valign='top' colspan='3' style='border-bottom: 1px solid #ccc;padding-bottom:5px;'><div style='background:$bg_color;padding:7px;border:1px solid #ccc;border-radius:5px 15px;text-indent: 0.7em;color:#7C2073;'>".nl2br(bbTags(smile($row["text"])))."</div></td>";
echo "</tr></table></td></tr>";
}
sleep(1); //Сделана задержка в 1 секунду чтобы можно проследить выполнение запроса
}else{
echo 0; //Если записи закончились
}
}

// подгрузка отзыва
if($_POST['func'] == "like_comm") {
$id = valideint($_POST["id"]);
$num = valideint($_POST["num"]);
if($num < 1 AND $num > 2){exit();}
$sql = mysql_query("SELECT `type`,`id_from` FROM `b_comment` WHERE `id`='$id'") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
$row = mysql_fetch_assoc($sql);
if($row["id_from"] == $id_user) { echo "1"; exit();}
$sql_l = mysql_query("SELECT `type` FROM `b_comment_like` WHERE `id_comm`='$id' AND `id_liker`='$id_user'") or die(mysql_error());
if(mysql_num_rows($sql_l) == 0){
mysql_query("INSERT INTO `b_comment_like` (`type`,`id_comm`,`id_liker`,`what`) VALUES ('1','$id','$id_user','$num')") or die(mysql_error());
exit();
}else{
exit();
}
}else{exit();}
}
?>