<?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['from']) && is_numeric($_GET['from']) && !empty($_GET['fid']) && is_numeric($_GET['fid']) && !empty($_GET['target']))
{
	$from = abs(intval($_GET['from']));
	$fid = abs(intval($_GET['fid']));
	if ($_GET['target'] == 'group_wall') $result1 = mysql_query("SELECT * FROM `groups` WHERE `id` = '".$from."' LIMIT 1");
	else $result1 = mysql_query("SELECT * FROM `users` WHERE `id` = '".$from."' LIMIT 1");
	if ($_GET['album'] == 'wall_photos') $result2 = mysql_query("SELECT * FROM `wall_photos` WHERE `id` = '".$fid."' AND `user_id` = '".$user_id."' LIMIT 1");
	else $result2 = mysql_query("SELECT * FROM `photos_file` WHERE `id` = '".$fid."' AND `user_id` = '".$user_id."' LIMIT 1");
	if (mysql_num_rows($result1) == true && mysql_num_rows($result2) == true)
	{
		if ($_GET['album'] == 'wall_photos') $arr_photo = mysql_fetch_array(mysql_query("SELECT * FROM `wall_photos` WHERE `id` = '".$fid."' AND `user_id` = '".$user_id."' LIMIT 1"));
		else $arr_photo = mysql_fetch_array(mysql_query("SELECT * FROM `photos_file` WHERE `id` = '".$fid."' AND `user_id` = '".$user_id."' LIMIT 1"));
	}
	else go(URL);
}
else go(URL);



if (!is_dir(HOME.'/files/wall/'.$user_id))
{
	mkdir(HOME.'/files/wall/'.$user_id,0777);
	mkdir(HOME.'/files/wall/'.$user_id.'/photos',0777);
	mkdir(HOME.'/files/wall/'.$user_id.'/photos/icons',0777);
	mkdir(HOME.'/files/wall/'.$user_id.'/photos/mini',0777);
	mkdir(HOME.'/files/wall/'.$user_id.'/photos/preview',0777);
}

if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `wall_cache` WHERE `user_id` = '".$user_id."' LIMIT 1"),0) == false) mysql_query("INSERT INTO `wall_cache` SET `user_id` = '".$user_id."'");
$arr_wall_cache = mysql_fetch_array(mysql_query("SELECT * FROM `wall_cache` WHERE `user_id` = '".$user_id."' LIMIT 1"));



if ($_GET['target'] == 'wall' || $_GET['target'] == 'group_wall')
{
	if ($arr_wall_cache['count_photos'] < 10)
	{
		if ($_GET['album'] != 'wall_photos')
		{
			$file_name = time().'.jpg';
			mysql_query("UPDATE `wall_cache` SET `photos` = '".$arr_wall_cache['photos']."|".$file_name."|' , count_photos=count_photos+1 WHERE `user_id` = '".$user_id."' LIMIT 1");
			copy(HOME.'/files/photos/'.$user_id.'/'.$arr_photo['name'], HOME.'/files/wall/'.$user_id.'/photos/'.$file_name);
			copy(HOME.'/files/photos/'.$user_id.'/icons/'.$arr_photo['name'], HOME.'/files/wall/'.$user_id.'/photos/icons/'.$file_name);
			copy(HOME.'/files/photos/'.$user_id.'/mini/'.$arr_photo['name'], HOME.'/files/wall/'.$user_id.'/photos/mini/'.$file_name);
			copy(HOME.'/files/photos/'.$user_id.'/preview/'.$arr_photo['name'], HOME.'/files/wall/'.$user_id.'/photos/preview/'.$file_name);
		}
		else mysql_query("UPDATE `wall_cache` SET `photos` = '".$arr_wall_cache['photos']."|".$arr_photo['name']."|' , count_photos=count_photos+1 WHERE `user_id` = '".$user_id."' LIMIT 1");
	}
	go(URL.'/'.($_GET['target']=='group_wall'?'groups/':'').'?id='.$from);
}

?>