<?
header('Content-type: text/html; charset=utf-8');
define('ROOT','../..');
require_once(ROOT.'/.config.php');
require_once(LIBS_PATH.'/system/load_libs.php');

if(!empty($_GET['SID'])) @session_id($_GET['SID']);
session_start();
if(empty($_SESSION['backend']['user_id'])) exit;


$form_name		= addslashes($_POST['form_name']);
$pre_target 	= CACHE.'/uploadify/';
$user_target 	= CACHE.'/uploadify/'.session_id().'/';


if($_POST['action']=='delete') {
	$post_name=$_POST['field_id'];
	$file_name=$_POST['file_name'];
	$sess_data=$_SESSION['flash_upload'][$form_name.'_form'][$post_name]['config'];
	$sess_upld=$_SESSION['flash_upload'][$form_name.'_form'][$post_name]['uploaded'];

	$sess_file_index = @array_search($file_name.'_tmp', $sess_upld);

	$form_target= $user_target.$form_name.'_form/';
	$target		= $user_target.$form_name.'_form/'.$post_name.'/';


	if(!empty($sess_data) && !isset($_POST['file_name'])) {
		if(is_dir($target)) {
			$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($target), RecursiveIteratorIterator::SELF_FIRST);//CHILD_FIRST
			foreach($files as $file) { /* if(@filemtime($file) < time()-1) */ @unlink($file); }
		}
		unset($_SESSION['flash_upload'][$form_name.'_form'][$post_name]['uploaded']);
		exit;
	}

	if(!empty($sess_data) && ($sess_file_index>0 || $sess_file_index===0)) {
		if(file_exists($target.$file_name.'_tmp')) unlink($target.$file_name.'_tmp');
		unset($_SESSION['flash_upload'][$form_name.'_form'][$post_name]['uploaded'][$sess_file_index]);
		if(count($_SESSION['flash_upload'][$form_name.'_form'][$post_name]['uploaded'])==0) unset($_SESSION['flash_upload'][$form_name.'_form'][$post_name]['uploaded']);
	}
	exit;
}



$post_name=array_pop(@array_keys($_FILES));

if(!empty($post_name) && count($_FILES)==1 && !empty($_FILES[$post_name])) {
	$sess_data	= $_SESSION['flash_upload'][$form_name.'_form'][$post_name]['config'];
	$upl_error 	= $_FILES[$post_name]['error'];
	$temp_file 	= $_FILES[$post_name]['tmp_name'];
	$file_name 	= $_FILES[$post_name]['name'];
	$file_size 	= $_FILES[$post_name]['size'];

	$file_ext 	= $X->FILE->get_ext($file_name);
	$file_name_part = substr($file_name,0,-(strlen($file_ext)+1));

	$form_target= $user_target.$form_name.'_form/';
	$target		= $user_target.$form_name.'_form/'.$post_name.'/';

	if(!is_dir($pre_target)) @mkdir($pre_target, 0755);
	if(!is_dir($user_target)) @mkdir($user_target, 0755);
	if(!is_dir($form_target)) @mkdir($form_target, 0755);
	if(!is_dir($target)) @mkdir($target, 0755);
	//

	if(!@in_array($file_ext, @explode('|',$sess_data['ext']))) $error['file_type']=true;
	if($file_size>$sess_data['size']) $error['file_size']=true;

	$new_file_name=$X->URL->mod(str_replace('../','',$file_name_part)).'.'.$file_ext;
	if(file_exists($target.$new_file_name.'_tmp')) $error['file_exists']=true;


	if(count($error)==0 && $upl_error==0 && file_exists($temp_file)) {
		move_uploaded_file($temp_file, $target.$new_file_name.'_tmp');
		chmod($target.$new_file_name.'_tmp', 0644);
	} else {
		$error['file_save']=true;
	}


	if(count($error)==0) {
		$_SESSION['flash_upload'][$form_name.'_form'][$post_name]['uploaded'][] = $new_file_name.'_tmp';
		die('{{'.$new_file_name.'}}');
	}
} else {
	$error['upload']=true;
}


switch($error) {
	default:																		break;
	case 'upload':			echo 'Plik nie został wysłany...';		break;
	case 'file_save':		echo 'Plik nie został zapisany...'; 	break;
	case 'file_type':		echo 'Niedozwolony typ pliku...'; 		break;
	case 'file_size':		echo 'Wysłany plik jest za duży...'; 	break;
	case 'file_exists':	echo 'Wysłany plik już istnieje...'; 	break;
}
?>