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

$eDir = (AMINA == 'g4s') ? '/editor/' : '/cheditor4/';

$editorDir = G4_DATA_PATH.$eDir;
$editorUrl = G4_DATA_URL.$eDir;

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

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

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

if(!is_dir($datdDir)) {
	@mkdir($dataDir, G4_DIR_PERMISSION);
	@chmod($dataDir, G4_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);
?>