<?php

if (!empty($_POST['form_wall']))
{
	$wall_post = check($_POST['wall_post']);
	$wall_post = substr($wall_post, 0, 10000);

	if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `wall_cache` WHERE `user_id` = '".$user_id."' LIMIT 1"),0) == true) $arr_wall_cache = mysql_fetch_array(mysql_query("SELECT * FROM `wall_cache` WHERE `user_id` = '".$user_id."' LIMIT 1")); else $arr_wall_cache = false;
	if ($arr_wall_cache == false) $arr_wall_cache['photos'] = '';

	if (!empty($wall_post) || !empty($arr_wall_cache['photos']) || !empty($arr_wall_cache['audio']))
	{
		if (mysql_query("INSERT INTO `wall` SET
		`user_id` = '".$user_id."',
		`wall_id` = '".$id."',
		`text` = '".$wall_post."',
		`type` = 'post',
		`photos` = '".$arr_wall_cache['photos']."',
		`audio` = '".$arr_wall_cache['audio']."',
		`time` = '".time()."'
		") == true)
		{
			if ($user_id != $id && mysql_query("INSERT INTO `notifications` SET
			`from_id` = '".$user_id."',
			`sex` = '".$user['sex']."',
			`for_id` = '".$id."',
			`mod` = 'wall',
			`type` = 'post',
			`refid` = '".$id."',
			`time` = '".time()."',
			`count` = 1,
			`new` = 1
			") == true) mysql_query("UPDATE `users` SET `notifications` = 1, notifications_journal=notifications_journal+1 WHERE `id` = '".$id."'");

			mysql_query("UPDATE `users` SET count_wall=count_wall+1 WHERE `id` = '".$id."'");
			if (!empty($arr_wall_cache['photos']))
			{
				mysql_query("UPDATE `wall_cache` SET `photos` = '', `count_photos` = 0 WHERE `user_id` = '".$user_id."' LIMIT 1");
				$q = explode("||", substr($arr_wall_cache['photos'], 1, -1));
				$count = count($q);
				$new_wall_count_photos = 0;
				for ($i = 0; $i < $count; $i++)
				{
					if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `wall_photos` WHERE `name` = '".$q[$i]."' AND `user_id` = '".$user_id."' LIMIT 1"),0) == false)
					{
						$new_wall_count_photos++;
						mysql_query("INSERT INTO `wall_photos` SET `user_id` = '".$user_id."', `wall_id` = '".$id."', `name` = '".$q[$i]."', `time` = '".time()."'");
					}
				}
				if ($new_wall_count_photos > 0) mysql_query("UPDATE `users` SET `count_wall_photos`= '".($user['count_wall_photos']+$new_wall_count_photos)."' WHERE `id` = '".$user_id."'");

			}
			if (!empty($arr_wall_cache['audio'])) mysql_query("UPDATE `wall_cache` SET `audio` = '', `count_audio` = 0 WHERE `user_id` = '".$user_id."' LIMIT 1");
		}
	}
}

?>