<?php

require_once '../incfiles/core.php';
require_once '../incfiles/func.php';
require_once '../incfiles/auth.php';
require_once '../incfiles/user.php';

?>
<script type="text/javascript">
$(function(){
	$('#form').on('submit', function(e){
	Page.Loading('start');
    e.preventDefault();
    var $that = $(this),
        formData = new FormData($that.get(0));
    $.ajax({
      url: $that.attr('action'),
      type: $that.attr('method'),
      contentType: false,
      processData: false,
      data: formData,
      success: function(response){
        if(response){
 			$('.content').html(response);
			Page.Loading('stop');
      }
     }
    });
  });
});
</script>
<?

if (empty($user_id)) {go(URL.'/');}

if (!empty($_GET['id']) && is_numeric($_GET['id']) && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `blog_comm` WHERE `id` = '".abs(intval($_GET['id']))."' AND `user_id` != '".$user_id."' LIMIT 1"),0) == true)
{

	$id = abs(intval($_GET['id']));
	
	$arr_comm = mysql_fetch_array(mysql_query("SELECT * FROM `blog_comm` WHERE `id` = '".$id."' LIMIT 1"));
	$arr_blog = mysql_fetch_array(mysql_query("SELECT * FROM `blog` WHERE `id` = '".$arr_comm['fid']."' LIMIT 1"));

	$arr_user = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".$arr_blog['user_id']."' LIMIT 1"));
	if ($arr_user['ban'] == 1) {go(URL.'/user/?id='.$arr_blog['user_id']);}

	if ($user_id == $arr_comm['user_id']) {go(URL.'/');}
	
	if ($user_id == $arr_comm['quote_id'])
	{
		if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `notifications` WHERE `refid` = '".$id."' AND `mod` = 'blog' AND `type` = 're_comm' AND `from_id` = '".$user_id."' LIMIT 1"),0) == true)
		{
			mysql_query("DELETE FROM `notifications` WHERE `refid` = '".$id."' AND `mod` = 'blog' AND `type` = 're_comm' AND `from_id` = '".$user_id."' LIMIT 1");
		}
	}
	
	if (!empty($_POST['comm']))
	{
		// Проверка на флуд
		$flood = time() - 5;
		if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `blog_comm` WHERE `time` > '".$flood."' AND `user_id` = '".$user_id."' LIMIT 1"),0) == true)
		{
			require_once('../incfiles/head.php');
			echo '<div class="title">Антифлуд</div><div class="block2">Вы не можете так часто писать.<br />Подождите 5 сек.
			<meta http-equiv="refresh" content="5;URL='.URL.'/blog/post.php?id='.$arr_blog['id'].'"/></div>';
			require_once('../incfiles/foot.php');
			exit;
		}

		$comm = check($_POST['comm']);
		$comm = substr($comm, 0, 1000);
		if (empty($comm)) {go(URL.'/blog/post.php?id='.$arr_blog['id'].'');}
		$quote = cut($arr_comm['text'],500);

		if (mysql_query("INSERT INTO `blog_comm` SET
		`fid` = '".$arr_comm['fid']."',
		`user_id` = '".$user_id."',
		`text` = '".$comm."',
		`quote` = '".$arr_comm['text']."',
		`quote_id` = '".$arr_comm['user_id']."',
		`time` = '".time()."'
		") == true)
		{
			mysql_query("UPDATE `blog` SET comm=comm+1 WHERE `id` = '".$arr_blog['id']."'");
	
			if ($user_id != $arr_blog['user_id'] && $arr_comm['user_id'] != $arr_blog['user_id'])
			{
				if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `notifications` WHERE `refid` = '".$arr_ava['id']."' AND `mod` = 'blog' AND `type` = 'comm' AND `from_id` = '".$arr_blog['user_id']."' LIMIT 1"),0) == true)
				{
					mysql_query("UPDATE `notifications` SET `user_id` = '".$user_id."', count=count+1, `time` = '".time()."', `new` = 1 WHERE `refid` = '".$arr_blog['id']."' AND `mod` = 'blog' AND `type` = 'comm' AND `from_id` = '".$arr_photo['user_id']."' LIMIT 1");
				}
				else
				{
					mysql_query("INSERT INTO `notifications` SET
					`user_id` = '".$user_id."',
					`sex` = '".$user['sex']."',
					`from_id` = '".$arr_blog['user_id']."',
					`mod` = 'blog',
					`type` = 'comm',
					`refid` = '".$arr_blog['id']."',
					`time` = '".time()."',
					`count` = 1,
					`new` = 1
					");
				}
				mysql_query("UPDATE `users` SET `notifications` = 1, notifications_journal=notifications_journal+1 WHERE `id` = '".$arr_blog['user_id']."'");
			}
				$cid = mysql_fetch_array(mysql_query("SELECT `id` FROM `blog_comm` ORDER by `id` DESC LIMIT 1"));
				
				if (mysql_query("INSERT INTO `notifications` SET
				`user_id` = '".$user_id."',
				`sex` = '".$user['sex']."',
				`from_id` = '".$arr_comm['user_id']."',
				`mod` = 'blog',
				`type` = 're_comm',
				`refid` = '".$cid['id']."',
				`time` = '".time()."',
				`count` = 1,
				`new` = 1
				") == true)
				{
					mysql_query("UPDATE `users` SET `notifications` = 1, notifications_journal=notifications_journal+1 WHERE `id` = '".$arr_comm['user_id']."'");
				}
			go(URL.'/response/post.php?id='.$arr_blog['id']);
		}
	}
	else if (!empty($_POST['back']))
	{
		go(URL.'/response/post.php?id='.$arr_blog['id']);
	}
	

	$title = 'Відповісти';
require_once('../incfiles/head.php');

	echo '<div class="title">Відповісти</div>
	<div class="block2">';
	echo user_name($arr_comm['user_id']).'<br/>'.vtime($arr_comm['time']).'<ul>';
	if (!empty($arr_comm['quote_id'])) echo '<div class="quote">'.user_name($arr_comm['quote_id']).' пишет:<ul>'.$arr_comm['quote'].'</ul></div>';
	echo text($arr_comm['text']).'</ul></div>
	<form id="form" action="'.URL.'/response/re_comm.php?id='.$id.'" method="post">
	<div class="block2">
	<textarea id="textarea" class="input" name="comm"></textarea>
	</div><div class="block2">
	<input class="button2" type="submit" value="'.lang('Написать','Надіслати').'"/>
	<input class="button2" type="submit" value="назад"/>
	</div>
	</form>';
require_once('../incfiles/foot.php');
exit;

}
else
{

	if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `notifications` WHERE `refid` = '".abs(intval($_GET['id']))."' AND `mod` = 'blog' AND `type` = 're_comm' AND `from_id` = '".$user_id."' LIMIT 1"),0) == true)
	{
		mysql_query("DELETE FROM `notifications` WHERE `refid` = '".abs(intval($_GET['id']))."' AND `mod` = 'blog' AND `type` = 're_comm' AND `from_id` = '".$user_id."' LIMIT 1");
	}

require_once('../incfiles/head.php');
			//echo '<div class="title">Ошибка</div><div class="block2">Сообщение не найдено!</div>';
			require_once('../incfiles/foot.php');
			exit;
require_once('../incfiles/foot.php');
exit;
}

?>