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

//-- 이미지 정보를 담을 테이블
$sql_create	= "CREATE TABLE IF NOT EXISTS zm_se_images (
	db_recno int(11) NOT NULL auto_increment,
	db_table varchar(50) NOT NULL,
	db_wrid int(11) NOT NULL,
	db_folder varchar(10) NOT NULL,
	db_filename varchar(255) NOT NULL,
	db_datetime datetime NOT NULL default '0000-00-00 00:00:00',
	PRIMARY KEY  (db_recno)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
sql_query($sql_create);

//-- upload 위치
if(!$bo_table) $bo_table	= "_zmb";
$date_folder	= date("Ym");
$upload_pos		= "/data/zm.exp/se/";
$upload_path	= $g4[path] . $upload_pos . $bo_table . "/" . $date_folder;
$upload_url		= $g4[url] . $upload_pos . $bo_table . "/" . $date_folder;
if(!file_exists($upload_path)) zm_mkdir($upload_path);				//-- 없으면 생성

 	$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']);
	$file->size = $headers['file_size'];
	$file->content = file_get_contents("php://input");

	$tmp		= explode(".", $file->name);
	$new_name	= md5(time() . $file->name) . "." . $tmp[count($tmp)-1];		//--저장할 이름
	$newPath	= $upload_path . '/' . $new_name;

	//-- 썸네일 옵션
	if($zm_board[thumbwidthsize] > 0 && $zm_board[thumbheightsize] > 0) $thumbsize	= $zm_board[thumbwidthsize] . "x" . $zm_board[thumbheightsize];
	//-- 워터마크 옵션
	if($zm_board[watermark])	$wmarkpath	= $zm_config[watermarkpath];
	else						$wmarkpath	= "";

	if(file_put_contents($newPath, $file->content)) {
		//-- 리사이징
		$ofile		= $newPath;
		$fileinfo	= @GetImageSize($ofile);
		$limitsize	= $zm_board[imagewidthsize];
		$new_w		= $fileinfo[0];
		$new_h		= $fileinfo[1];

		//-- 가로 폭이 지정 됐을 때
		if($limitsize > 0) {
			$ratio_s	=  $limitsize / $fileinfo[0];
			$new_w		= $limitsize;
			$new_h		= round($fileinfo[1] * $ratio_s);

			//-- 이미지를 축소한다.(원본 이미지 이름, 폭, 높이, 1=축소 0=썸네일)
			zm_makethumb($ofile, $new_w, $new_h, 1);
		}

		//-- 썸네일
		if($zm_board[thumbwidthsize] > 0 && $zm_board[thumbheightsize] > 0) zm_makethumb($ofile, $zm_board[thumbwidthsize], $zm_board[thumbheightsize], 0);

		$sFileInfo .= "&bNewLine=true";
		$sFileInfo .= "&sFileName=".$new_name;
		$sFileInfo .= "&sFileURL=".$upload_url . "/" . $new_name;

		//-- 테이블에 이미지 정보 기록 zm_se_images
		$n		= date("Y-m-d H:i:s", $g4[server_time]);
		sql_query("insert into zm_se_images set db_table='$bo_table', db_folder='$date_folder', db_filename='$new_name', db_datetime='$n'");
	}
	echo $sFileInfo;
 ?>
