<?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, 0707);
		@chmod($editorDir, 0707);
	}

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

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

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

	$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;
 ?>