<?
define('ADMIN',true);
define('AJAX',true);
define('ROOT','../..');
header('Content-Type: text/javascript; charset=utf-8');
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;

if(count($_POST)==0) {
	echo '
<script type="text/javascript">$.colorbox.setTitle("Ściągnij zdalny plik z adresu");</script>

<div style="width:600px; height:80px;">
	<form id="download_file_form" name="download_file_form" action="" method="post" onsubmit="flash_download_process(\''.$_GET['field_id'].'\'); return false;">
		<table class="form" cellpadding="3">
			<tr>
				<td>'.$X->FORM->label('download_file','Link:',true).'</td>
				<td>'.$X->FORM->input('download_file',520).'</td>
			</tr>
			<tr><td></td><td><br />'.$X->FORM->submit('Pobierz plik').'<div style="display:inline-block; vertical-align:middle; padding-left:30px;" id="download_cont"></div></td></tr>
		</table>
	</form>
</div>

<script type="text/javascript">setTimeout(function(){ $("#download_file").focus(); },250)</script>

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


	$post_name=$_POST['post_name'];
	$download_file=rawurldecode($_POST['download_file']);
	if(!stristr($download_file,'http://') && !stristr($download_file,'https://')) $download_file='http://'.$download_file;


	if(!empty($post_name) && !empty($download_file)) {
		$sess_data	= $_SESSION['flash_upload'][$form_name.'_form'][$post_name]['config'];

		$file_name 	= @array_pop(explode('/',$download_file));
		$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;

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

		//
		if(count($error)==0) {
			$handle=fopen(str_replace(' ','%20',stripslashes($download_file)), "r");
			if($handle) {
				$zapis=@fopen($target.$new_file_name,'a+');
				while(!@feof($handle)) {
					$packet=@fread($handle,32768);
					@fputs($zapis, $packet);
				}
				@fclose($handle);
		      @fclose($zapis);
			} else {
				$error['download']=true;
			}
			//
			$file_size 	= @filesize($target.$new_file_name);
			if($file_size>$sess_data['size']) $error['file_size']=true;
		} else {
			$error['file_save']=true;
		}


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


	if($error['file_type']==true)		die('Niedozwolony typ pliku...');
	if($error['file_size']==true)		die('Ten plik jest za duży...');
	if($error['file_exists']==true)	die('Ten plik już istnieje...');
	if($error['download']==true) 		die('Plik nie został pobrany...');
	if($error['file_save']==true)		die('Plik nie został zapisany...');
}
?>