<?php
include_once("_common.php");

$eDir = AMINA ? '/editor/' : '/cheditor4/';

$editorDir = G5_DATA_PATH.$eDir;
$editorUrl = G5_DATA_URL.$eDir;

if(!is_dir($editorDir)) {
	@mkdir($editorDir, AMINA_DIR_PERMISSION);
	@chmod($editorDir, AMINA_DIR_PERMISSION);
}

$ym = date('ym', G5_SERVER_TIME);

$dataDir = $editorDir.$ym.'/';
$dataUrl = $editorUrl.$ym.'/';

if(!is_dir($datdDir)) {
	@mkdir($dataDir, AMINA_DIR_PERMISSION);
	@chmod($dataDir, AMINA_DIR_PERMISSION);
}

echo $_REQUEST["htImageInfo"];

// default redirection
$url = $_REQUEST["callback"].'?callback_func='.$_REQUEST["callback_func"];
$bSuccessUpload = is_uploaded_file($_FILES['Filedata']['tmp_name']);

// SUCCESSFUL
if(bSuccessUpload) {
	$tmp_name = $_FILES['Filedata']['tmp_name'];
	$name = $_FILES['Filedata']['name'];
	$pos = strrpos($name, '.');
    $extension = substr($name, $pos, strlen($name) - $pos);
    $filename = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR'])).'_'.get_microtime().$extension;

	$newPath = $dataDir.urlencode($filename);
	
	@move_uploaded_file($tmp_name, $newPath);

	$url .= "&bNewLine=true";
	$url .= "&sFileName=".urlencode(urlencode($name));
	$url .= "&sFileURL=".$dataUrl.urlencode(urlencode($filename));
}
// FAILED
else {
	$url .= '&errstr=error';
}

header('Location: '. $url);
?>