<?php
	/* Note: This thumbnail creation script requires the GD PHP Extension.  
		If GD is not installed correctly PHP does not render this page correctly
		and SWFUpload will get "stuck" never calling uploadSuccess or uploadError
	 */

	// Get the session Id passed from SWFUpload. We have to do this to work-around the Flash Player Cookie Bug
	@set_time_limit(0);
	error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING);
	ini_set('html_errors', '0');

	define('DIRCMS_ROOT', str_replace("\\", '/',substr(dirname(__FILE__),0,-10)));
	include DIRCMS_ROOT.'data/config.inc.php';

	if (isset($_POST["PHPSESSID"])) {
		session_id($_POST["PHPSESSID"]);
	}

	session_start();

	ini_set("html_errors", "0");

	// Check the upload
	
	if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
		echo "ERROR:invalid upload";
		exit(0);
	}

	// Check the upload
	if (!is_array(@getimagesize($_FILES["Filedata"]["tmp_name"])))
	{
		echo "ERROR:invalid upload";
		exit(0);
	}

	if (!isset($_SESSION["file_info"])) {
		$_SESSION["file_info"] = array();
	}
	
	$fileName = time().mt_rand(1000,9999) .'.jpg';
	$path=date('Y-m-d',time()).'/';
	@mkdir('../../upload/image/'.$path);
	move_uploaded_file($_FILES["Filedata"]["tmp_name"], "../../upload/image/".$path. $fileName);

	// ¼ÓË®Ó¡

	if(WATERMARK_ENABLED)
	{
		include DIRCMS_ROOT.'include/image.class.php';
		$image=new image();
		$image->watermark("../../upload/image/" .$path.$fileName,WATERMARK_POS,WATERMARK_FILE,WATERMARK_PCT,WATERMARK_WORDS,5,WATERMARK_COLOR);
	}

	if(FTP && FTP_SERVER && FTP_USER && FTP_PWD)
	{
		include(DIRCMS_ROOT.'include/ftp.class.php');
		$ftpobj = new ftp(FTP_SERVER,FTP_USER,FTP_PWD,FTP_PORT,FTP_TIMEOUT,SSL,PASV);
		$rand=date('Ym',time());
		$remotedir=FTP_DIR.'/'.$rand.'/';
		if($ftpobj->ftp_mkdir($remotedir))
		{
			$result=$ftpobj->ftp_upload($remotedir.basename($fileName),DIRCMS_ROOT."upload/image/" .$path.$fileName);
		}
		$ftpobj->ftp_close();
		@unlink(DIRCMS_ROOT."upload/image/" .$path.$fileName);
		echo "FILEID:" . FTP_URL.'/'.$rand.'/'.$fileName;
	}
	else
	{
		echo "FILEID:" . $path.$fileName;
	}
	exit(0);
?>