<?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($dataDir)){
		@mkdir($dataDir, AMINA_DIR_PERMISSION);
		@chmod($dataDir, AMINA_DIR_PERMISSION);
	}

	$sFileInfo = '';
	$headers = array();
	 
	foreach($_SERVER as $k => $v) {
		if(substr($k, 0, 9) == "HTTP_FILE") {
			$k = substr(strtolower($k), 5);
			$headers[$k] = $v;
		} 
	}
	
	$file = new stdClass;
	$file->name = rawurldecode($headers['file_name']);

	$name = rawurldecode($headers['file_name']);
	$pos = strrpos($name, '.');
    $extension = substr($name, $pos, strlen($name) - $pos);
    $file->filename = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR'])).'_'.get_microtime().$extension;

	$file->size = $headers['file_size'];
	$file->content = file_get_contents("php://input");

	$newPath = $dataDir.iconv("utf-8", "cp949", $file->filename);
	
	if(file_put_contents($newPath, $file->content)) {
		$sFileInfo .= "&bNewLine=true";
		$sFileInfo .= "&sFileName=".$file->name;
		$sFileInfo .= "&sFileURL=".$dataUrl.$file->filename;
	}
	
	echo $sFileInfo;
 ?>