<?
/*
실험주소
http://www.owllab.com/ios/smartreport_upload.php
*/

header("Cache-Control: no-cache, must-revalidate");
header("Content-type: application/xml; charset=UTF-8");

include_once ("session.php");
include_once ("dbcon_light.php");

function saveFile($fileField, $rowid) {
	global $_FILES, $SCRIPT_FILENAME;
	for ($c=0;$c<(10-strlen($rowid));$c++) $filename .= "0";
	if ($fileField=="moviethumb") $filename .= $rowid."_movie";
	else $filename .= $rowid."_".$fileField;
	$filepath = dirname($SCRIPT_FILENAME)."/files/".$filename.".".pathinfo($_FILES[$fileField][name], PATHINFO_EXTENSION);
	if (is_file($filepath)) unlink($filepath); 
	move_uploaded_file($_FILES[$fileField][tmp_name], $filepath);
}

$table = "report_demo";
$usert = "user_demo";

$result = -1;
$msg = "죄송합니다.\n시스템장애로 작업을 취소했습니다.\n나중에 다시 시도해 주세요.";
	
if ($suserdb[userid]=="") {
	$result = -2;
	$msg = "먼저 로그인하세요.";
} else {
	$sql = " select * from $usert where userid='".$suserdb[userid]."' ";
	$rsusr = mysql_select_list($sql);
	if ($rsusr[userlevel][0]<0) {
		$result = -3;
		$msg = "업로드 권한을 상실했습니다.\n관리자에게 문의하세요.";
	} else {
		if ($mode=="sync") {
			$subect = sqlparse($subect);
			$writer = sqlparse($writer);
			$content = sqlparse($content);
			$latitude = sqlparse($latitude);
			$longitude = sqlparse($longitude);
	
			if ($cryptid!="") {
				$sql = " select rowid,listimage,image,movie from $table where cryptid='$cryptid' and reg_id='".$suserdb[userid]."' ";
				$rs = mysql_select_list($sql);
				$rowid = $rs[rowid][0];
				if (count($rs[rowid])>0) {
					if ($uploadtype=="") {
						saveFile("listimage", $rs[rowid][0]);
						saveFile("image", $rs[rowid][0]);
						saveFile("moviethumb", $rs[rowid][0]);
						saveFile("movie", $rs[rowid][0]);
					}
					$sqlup = "update $table set 
					subject='$subject', content='$content'
					, latitude='$latitude', longitude='$longitude'
					, publish='$publish' , edit_date=now(), type='$type' ";
					if ($uploadtype=="") {
						$sqlup .= ", listimage='".sqlparse($_FILES[listimage][name])."'";
						$sqlup .= ", image='".sqlparse($_FILES[image][name])."'";
						$sqlup .= ", movie='".sqlparse($_FILES[movie][name])."'";
					}
					$sqlup .= " where rowid='".$rs[rowid][0]."' ";
					mysql_query($sqlup);
				}//if rowid
			} else {
				$sqlup = "insert into $table (
				subject,content,latitude,longitude,publish,reg_date,reg_id,writer,type
				) values (
				'$subject', '$content', '$latitude', '$longitude', '$publish', now(), '".$suserdb[userid]."', '".$rsusr[alias][0]."' , '$type'
				)";
				$resup = mysql_query($sqlup);
				if ($resup) {
					$sql = "select rowid from $table where reg_id='".$suserdb[userid]."' order by rowid desc limit 1";
					$rsin = mysql_select_list($sql);
					$rowid = $rsin[rowid][0];
					
					if ($uploadtype=="") {
						saveFile("listimage", $rsin[rowid][0]);
						saveFile("image", $rsin[rowid][0]);
						saveFile("moviethumb", $rsin[rowid][0]);
						saveFile("movie", $rsin[rowid][0]);
					}
					
					$sqlup1 = "update $table set cryptid='".md5($rsin[rowid][0])."' ";
					if ($uploadtype=="") {
						$sqlup1 .= ", listimage='".sqlparse($_FILES[listimage][name])."'";
						$sqlup1 .= ", image='".sqlparse($_FILES[image][name])."'";
						$sqlup1 .= ", movie='".sqlparse($_FILES[movie][name])."'";
					}
					$sqlup1 .= " where rowid='".$rsin[rowid][0]."' ";
					mysql_query($sqlup1);
				}
			}//if cryptid
		}//if mode
	}//if userlevel
}//if userid

$sql = " select * from $table where rowid='$rowid' ";
$rs = mysql_select_list($sql);
if (count($rs[rowid])>0) {
	$result = 1;
	$msg = "서버에 저장했습니다.";
}

?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">"; ?>
<lists>
	<info>
		<cryptid><? echo $cryptid;?></cryptid>
		<result><? echo $result;?></result>
		<totrec><? echo count($rs[rowid]);?></totrec>
		<msg><![CDATA[<? echo $msg;?>]]></msg>
		<resup><? echo $resup;?></resup>
		<sqlup><![CDATA[<? echo $sqlup;?>]]></sqlup>
		<sqlup1><![CDATA[<? echo $sqlup1;?>]]></sqlup1>
		<sql><![CDATA[<? echo $sql;?>]]></sql>
		<listimage><![CDATA[<? echo $_FILES[listimage][tmp_name];?>]]></listimage>
		<image><![CDATA[<? echo $_FILES[image][tmp_name];?>]]></image>
		<moviethumb><![CDATA[<? echo $_FILES[moviethumb][tmp_name];?>]]></moviethumb>
		<movie><![CDATA[<? echo $_FILES[movie][tmp_name];?>]]></movie>
	</info>
	<? for ($i=0;$i<count($rs[rowid]);$i++) { ?>  
	<data>
		<rowid><? echo $rs[rowid][$i];?></rowid>
		<writer><![CDATA[<? echo $rs[writer][$i];?>]]></writer>
		<subject><![CDATA[<? echo $rs[subject][$i];?>]]></subject>
		<reg_date><? echo $rs[reg_date][$i];?></reg_date>
		<reg_id><![CDATA[<? echo $rs[reg_id][$i];?>]]></reg_id>
		<type><![CDATA[<? echo $rs[type][$i];?>]]></type>
		<publish><? echo $rs[publish][$i];?></publish>
		<cryptid><? echo md5($rs[rowid][$i]);?></cryptid>
		<latitude><? echo $rs[latitude][$i];?></latitude>
		<longitude><? echo $rs[longitude][$i];?></longitude>
		<content><![CDATA[<? echo $rs[content][$i];?>]]></content>
	</data>
	<? }// for i ?>
</lists>