<?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 `groups_wall` WHERE `id` = '".abs(intval($_GET['id']))."'"),0) == true)
{
	$id = abs(intval($_GET['id']));
	$arr = mysql_fetch_array(mysql_query("SELECT * FROM `groups_wall` WHERE `id` = '".$id."' LIMIT 1"));
	$arr_group = mysql_fetch_array(mysql_query("SELECT * FROM `groups` WHERE `id` = '".$arr['wall_id']."' LIMIT 1"));
	if (in_array($user_id, explode("||", substr($arr_group['blacklist'], 1, -1)))) exit;
	
	if (!in_array($user_id, explode("||", substr($arr['ulike'], 1, -1))))
	{
		if (!empty($arr['ulike'])) $new_like = '|'.$user_id.'|'.$arr['ulike']; else $new_like = '|'.$user_id.'|';
		
		if (mysql_query("UPDATE `groups_wall` SET
		`ulike` = '".$new_like."',
		`clike` = '".($arr['clike']+1)."'
		WHERE `id` = '".$id."' LIMIT 1") == true)
		{
			if ($user_id != $arr['user_id'] && $arr['from_group'] == 0)
			{
				mysql_query("INSERT INTO `notifications` SET
				`from_id` = '".$user_id."',
				`sex` = '".$user['sex']."',
				`for_id` = '".$arr['user_id']."',
				`mod` = 'group_wall',
				`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 '<a class="crl" href="'.URL.'/groups/wall/like.php?id='.$id.'" onclick="Like.Go(this.href,'.$id.'); return false"><img src="'.URL.'/design/img/like_ok.png"/><b>'.($arr['clike']+1).'</b></a>';
			
			if ($arr['type'] == 'repost' && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `groups_wall` WHERE `id` = '".$arr['repost_id']."' AND `user_id` != '".$user_id."' LIMIT 1"),0) == true)
			{
				$arr_post = mysql_fetch_array(mysql_query("SELECT * FROM `groups_wall` WHERE `id` = '".$arr['repost_id']."' LIMIT 1"));
				if (!in_array($user_id, explode("||", substr($arr_post['ulike'], 1, -1)))) mysql_query("UPDATE `groups_wall` SET `ulike` = '|".$user_id."|".$arr_post['ulike']."', `clike` = '".($arr_post['clike']+1)."' WHERE `id` = '".$arr['repost_id']."' LIMIT 1");
				?>
				<script type="text/javascript">
				$('#like_<?echo $arr_post['id'];?>').html(<?echo '<a class="crl" href="'.URL.'/groups/wall/like.php?id='.$arr_post['id'].'" onclick="Like.Go(this.href,'.$arr_post['id'].'); return false"><img src="'.URL.'/design/img/like_ok.png"/><b>'.($arr_post['clike']+1).'</b></a>';?>);
				</script>
				<?
				if ($arr_post['type'] == 'update_avatar' && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `groups_avatars` WHERE `name` = '".$arr_post['photos']."' AND `user_id` != '".$user_id."' LIMIT 1"),0) == true)
				{
					$arr_ava = mysql_fetch_array(mysql_query("SELECT * FROM `groups_avatars` WHERE `name` = '".$arr_post['photos']."' LIMIT 1"));
					if (!in_array($user_id, explode("||", substr($arr_ava['ulike'], 1, -1)))) mysql_query("UPDATE `groups_avatars` SET `ulike` = '|".$user_id."|".$arr_ava['ulike']."', `clike` = '".($arr_ava['clike']+1)."' WHERE `id` = '".$arr_ava['id']."' LIMIT 1");
				}
			}
			else if ($arr['type'] == 'update_avatar' && mysql_result(mysql_query("SELECT COUNT(`id`) FROM `groups_avatars` WHERE `name` = '".$arr['photos']."' AND `user_id` != '".$user_id."' LIMIT 1"),0) == true)
			{
				$arr_ava = mysql_fetch_array(mysql_query("SELECT * FROM `groups_avatars` WHERE `name` = '".$arr['photos']."' LIMIT 1"));
				if (!in_array($user_id, explode("||", substr($arr_ava['ulike'], 1, -1)))) mysql_query("UPDATE `groups_avatars` SET `ulike` = '|".$user_id."|".$arr_ava['ulike']."', `clike` = '".($arr_ava['clike']+1)."' WHERE `id` = '".$arr_ava['id']."' LIMIT 1");
			}
		}
	}
	else
	{
		$del_ulike = str_replace("|".$user_id."|", "", $arr['ulike']);
		
		if (mysql_query("UPDATE `groups_wall` SET
		`ulike` = '".$del_ulike."',
		`clike` = '".($arr['clike']-1)."'
		WHERE `id` = '".$id."' LIMIT 1") == true)
		{
			echo '<a class="crl" href="'.URL.'/groups/wall/like.php?id='.$id.'" onclick="Like.Go(this.href,'.$id.'); return false"><img src="'.URL.'/design/img/like.png"/>'.(($arr['clike']-1)>0?'<b>'.($arr['clike']-1).'</b>':'').'</a>';
		}
	}
}

if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) or strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {go(URL.'/groups/wall/?id='.$arr['wall_id'].'');}

 
?>