<?php

if (!empty($_POST['form_wall']))
{

	$wall_post = check($_POST['wall_post']);
	$wall_post = substr($wall_post, 0, 10000);
	if (!empty($_POST['from_group']) && in_array($user_id, explode("||", substr($arr_group['admins'], 1, -1)))) $from_group = 1;
	else $from_group = 0;
	
	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']) || !empty($arr_wall_cache['video']))
	{
		if (mysql_query("INSERT INTO `groups_wall` SET
		`user_id` = '".$user_id."',
		`from_group` = '".$from_group."',
		`wall_id` = '".$id."',
		`text` = '".$wall_post."',
		`type` = 'post',
		`photos` = '".$arr_wall_cache['photos']."',
		`audio` = '".$arr_wall_cache['audio']."',
		`video` = '".$arr_wall_cache['video']."',
		`time` = '".time()."'
		") == true)
		{
			mysql_query("UPDATE `groups` SET count_wall=count_wall+1 WHERE `id` = '".$id."'");
			if (!empty($arr_wall_cache['photos']))
			{				
				if ($from_group == 1 && !is_dir(HOME.'/files/groups/wall/'.$id))
				{
					mkdir(HOME.'/files/groups/wall/'.$id,0777);
					mkdir(HOME.'/files/groups/wall/'.$id.'/photos',0777);
					mkdir(HOME.'/files/groups/wall/'.$id.'/photos/icons',0777);
					mkdir(HOME.'/files/groups/wall/'.$id.'/photos/mini',0777);
					mkdir(HOME.'/files/groups/wall/'.$id.'/photos/preview',0777);
				}
				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_count_wall_photos = 0;
				$new_count_group_wall_photos = 0;
				for ($i = 0; $i < $count; $i++)
				{
					if ($from_group == 1)
					{
						if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `groups_wall_photos` WHERE `name` = '".$q[$i]."' AND `user_id` = '".$user_id."' LIMIT 1"),0) == false)
						{
							$new_count_group_wall_photos++;
							mysql_query("INSERT INTO `groups_wall_photos` SET `user_id` = '".$user_id."', `wall_id` = '".$id."', `name` = '".$q[$i]."', `time` = '".time()."'");
						}
												
						if (copy(HOME.'/files/wall/'.$user_id.'/photos/'.$q[$i], HOME.'/files/groups/wall/'.$id.'/photos/'.$q[$i])) unlink(HOME.'/files/wall/'.$user_id.'/photos/'.$q[$i]);
						if (copy(HOME.'/files/wall/'.$user_id.'/photos/preview/'.$q[$i], HOME.'/files/groups/wall/'.$id.'/photos/preview/'.$q[$i])) unlink(HOME.'/files/wall/'.$user_id.'/photos/preview/'.$q[$i]);
						if (copy(HOME.'/files/wall/'.$user_id.'/photos/mini/'.$q[$i], HOME.'/files/groups/wall/'.$id.'/photos/mini/'.$q[$i])) unlink(HOME.'/files/wall/'.$user_id.'/photos/mini/'.$q[$i]);
						if (copy(HOME.'/files/wall/'.$user_id.'/photos/icons/'.$q[$i], HOME.'/files/groups/wall/'.$id.'/photos/icons/'.$q[$i])) unlink(HOME.'/files/wall/'.$user_id.'/photos/icons/'.$q[$i]);				
					}
					else
					{
						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_count_wall_photos++;
							mysql_query("INSERT INTO `wall_photos` SET `user_id` = '".$user_id."', `wall_id` = '".$id."', `name` = '".$q[$i]."', `time` = '".time()."'");
						}
					}
				}
				if ($new_count_group_wall_photos > 0) mysql_query("UPDATE `groups` SET `count_wall_photos` = '".($arr_group['count_wall_photos']+$new_count_group_wall_photos)."' WHERE `id` = '".$id."' LIMIT 1");
				if ($new_count_wall_photos > 0) mysql_query("UPDATE `users` SET `count_wall_photos` = '".($user['count_wall_photos']+$new_count_wall_photos)."' WHERE `id` = '".$user_id."' LIMIT 1");
			}
			if (!empty($arr_wall_cache['audio'])) mysql_query("UPDATE `wall_cache` SET `audio` = '', `count_audio` = 0 WHERE `user_id` = '".$user_id."' LIMIT 1");
		}
		if (!empty($arr_wall_cache['video'])) mysql_query("UPDATE `wall_cache` SET `video` = '', `count_video` = 0 WHERE `user_id` = '".$user_id."' LIMIT 1");
		}
	}


?>