<?php

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


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

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

	if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `blog_like` WHERE `fid` = '".$id."' AND `user_id` = '".$user_id."'"),0) == false)
	{
		$arr = mysql_fetch_array(mysql_query("SELECT `user_id`, `clike` FROM `blog` WHERE `id` = '".$id."' LIMIT 1"));

		$arr_user_id = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '".$arr['user_id']."' LIMIT 1"));
		if ($arr_user_id['ban'] == 1) {exit;}

		if ($user_id == $arr['user_id']) {exit;}

		if (mysql_query("INSERT INTO `blog_like` SET
		`fid` = '".$id."',
		`user_id` = '".$user_id."',
		`time` = '".time()."'
		") == true)
		{
			mysql_query("UPDATE `blog` SET clike=clike+1 WHERE `id` = '".$id."'");
			mysql_query("UPDATE `users` SET count_like_blog=count_like_blog+1 WHERE `id` = '".$user_id."'");

			if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `notifications` WHERE `refid` = '".$id."' AND `mod` = 'blog' AND `type` = 'like' AND `for_id` = '".$arr['user_id']."' LIMIT 1"),0) == true)
			{
				mysql_query("UPDATE `notifications` SET `user_id` = '".$user_id."', count=count+1, `time` = '".time()."', `new` = 1 WHERE `refid` = '".$id."' AND `mod` = 'blog' AND `type` = 'like' AND `for_id` = '".$arr['user_id']."' LIMIT 1");
			}
			else
			{
				mysql_query("INSERT INTO `notifications` SET
				`user_id` = '".$user_id."',
				`sex` = '".$user['sex']."',
				`for_id` = '".$arr['user_id']."',
				`mod` = 'blog',
				`type` = 'like',
				`refid` = '".$id."',
				`time` = '".time()."',
				`count` = 1,
				`new` = 1
				");
			}
			mysql_query("UPDATE `users` SET `notifications` = 1, notifications_journal=notifications_journal+1 WHERE `id` = '".$arr['user_id']."'");
	
			echo '<img src="'.URL.'/design/img/like_ok.png"/> <b>'.($arr['clike']+1).'</b>';
		}
	}
}
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) or strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {go(URL.'/response/post.php?id='.$id.'');}

 
?>