<?
/*
실험주소
http://www.owllab.com/ios/myreports_delete.php?mode=del&cryptid=6512bd43d9caa6e02c990b0a82652dca
*/

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 deleteFile($fileField, $rowid, $filename) {
	$filepath = getFilePath($fileField,$rowid, pathinfo($filename, PATHINFO_EXTENSION));
	if (is_file($filepath)) unlink($filepath); 
}

function getFilePath($fileField, $rowid, $ext) {
	global $_FILES, $SCRIPT_FILENAME;
	for ($c=0;$c<(10-strlen($rowid));$c++) $filename .= "0";
	$filename .= $rowid."_".$fileField;
	$filepath = dirname($SCRIPT_FILENAME)."/files/".$filename.".".$ext;
	return $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=="del") {
			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) {
					deleteFile("listimage", $rs[rowid][0], $rs[listimage][0]);
					deleteFile("image", $rs[rowid][0], $rs[image][0]);
					deleteFile("movie", $rs[rowid][0], $rs[movie][0]);
					deleteFile("movie", $rs[rowid][0], "xxx.jpg");
					$sqlup = "delete from $table where rowid='".$rs[rowid][0]."' ";
					$resup = mysql_query($sqlup);
					if ($resup) {
						$result = 1;
						$msg = "서버에서 지정한 리포트를 삭제했습니다.";
					}
				}//if rowid
			}//if cryptid
		}//if mode
	}//if userlevel
}//if userid

?>
<?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>
		<sql><![CDATA[<? echo $sql;?>]]></sql>
		<listimage><![CDATA[<? echo getFilePath("listimage",$rowid, pathinfo($rs[listimage][0], PATHINFO_EXTENSION));?>]]></listimage>
		<image><![CDATA[<? echo getFilePath("image",$rowid, pathinfo($rs[image][0], PATHINFO_EXTENSION));?>]]></image>
		<movie><![CDATA[<? echo getFilePath("movie",$rowid, pathinfo($rs[movie][0], PATHINFO_EXTENSION));?>]]></movie>
	</info>
	<? for ($i=0;$i<count($rs[rowid]);$i++) { ?>  
	<data>
		<rowid><? echo $rowid;?></rowid>
	</data>
	<? }// for i ?>
</lists>