<?
/*
실험주소
http://www.owllab.com/ios/user_proc.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");

$usert = "user_demo";
$result = -1;
$msg = "죄송합니다.\n시스템장애로 작업을 취소했습니다.\n나중에 다시 시도해 주세요.";

$phone = sqlparse($phone);
$userid = sqlparse($userid);
$name = sqlparse($name);
$alias = sqlparse($alias);
$email = sqlparse($email);
$postal = sqlparse($postal);
$address = sqlparse($address);

if ($mode=="add") {
	if ($suserdb[addhint]!=$addhint) {
			$result = -4;
			$msg = "등록코드가 올바르지 않습니다.\n다시 시도해 주세요.";
	} else {
		$sql = " select * from $usert where userid='$userid' or phone='$phone' ";
		$rs = mysql_select_list($sql);
		if ($rs[userid][0]==$userid) {
				$result = -3;
				$msg = "이미 사용중인 아이디입니다.";
		} else if ($rs[phone][0]==$phone) {
				$result = -2;
				$msg = "이미 사용중인 휴대전화번호입니다.";
		} else {
			$sql = "insert into $usert (
			phone,userid,passwd,name,alias,email,postal,address,userlevel
			) values (
			'$phone', '$userid', md5('$passwd'), '$name', '$alias', '$email', '$postal', '$address', 0
			)";
			$res = mysql_query($sql);
			if ($res) {
				$result = 1;
				$msg = "등록을 완료했습니다.";
				$suserdb[userid] = $userid;
			}
		}//if
	}
} else if ($mode=="edit") {
	$sql = " select * from $usert where userid='".$suserdb[userid]."' ";
	$rs = mysql_select_list($sql);
	if ($rs[userlevel][0]>=0 && $rs[passwd][0]==md5($passwd)) {
		$sql = "update $usert set ";
		if ($phone!="") $sql .= " phone='$phone', ";
		if ($alias!="") $sql .= " alias='$alias', ";
		if ($newpwd!="") $sql .= " passwd=md5('$newpwd'), ";
		if ($name!="") $sql .= " name='$name', ";
		if ($email!="") $sql .= " email='$email', ";
		if ($postal!="") $sql .= " postal='$postal', ";
		if ($address!="") $sql .= " address='$address' ";
		$sql .= " where userid='".$suserdb[userid]."' ";
		$res = mysql_query($sql);
		if ($res) {
			$result = 1;
			$msg = "수정을 완료했습니다.";
		}
	}
	
} else if ($mode=="del") {
	$sql = "delete from $usert where userid='".$suserdb[userid]."' ";
	$res = mysql_query($sql);
	if ($res) {
		$result = 1;
		$msg = "탈퇴를 완료했습니다.";
		$suserdb = array();
		$suserdb[userlevel] = -1;
	}
} else {
	$result = 1;
	$msg = "";
}// mode

$sql = " select * from $usert where userid='".$suserdb[userid]."' ";
$rs = mysql_select_list($sql);
if ($mode=="" && count($rs[rowid])<=0) {
	$result = -5;
	$msg = "로그인하세요.";
} else {
	$suserdb[userlevel] = $rs[userlevel][0];
	$suserdb[alias] = $rs[alias][0];
}

if ($result==1) {
	setcookie ("suserdb.addhint", "", time() + $_cookieLifeTime, "/", $_SERVER["HTTP_HOST"]);
	setcookie ("suserdb.userid", $suserdb[userid], time() + $_cookieLifeTime, "/", $_SERVER["HTTP_HOST"]);
	setcookie ("suserdb.userlevel", $suserdb[userlevel], time() + $_cookieLifeTime, "/", $_SERVER["HTTP_HOST"]);
	setcookie ("suserdb.alias", $suserdb[alias], time() + $_cookieLifeTime, "/", $_SERVER["HTTP_HOST"]);
}

?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">"; ?>
<lists>
	<info>
		<result><? echo $result;?></result>
		<totrec><? echo count($rs[rowid]);?></totrec>
		<msg><? echo $msg;?></msg>
		<addhint><? echo $suserdb[addhint];?></addhint>
		
	</info>
	<? $i=0; // for ($i=0;$i<count($rs[rowid]);$i++) { ?>  
	<data>
		<rowid><? echo $rs[rowid][$i];?></rowid>
		<phone><![CDATA[<? echo $rs[phone][$i];?>]]></phone>
		<userid><![CDATA[<? echo $rs[userid][$i];?>]]></userid>
		<name><![CDATA[<? echo $rs[name][$i];?>]]></name>
		<alias><![CDATA[<? echo $rs[alias][$i];?>]]></alias>
		<email><![CDATA[<? echo $rs[email][$i];?>]]></email>
		<postal><![CDATA[<? echo $rs[postal][$i];?>]]></postal>
		<address><![CDATA[<? echo $rs[address][$i];?>]]></address>
		<userlevel><![CDATA[<? echo $rs[userlevel][$i];?>]]></userlevel>
	</data>
	<? // }// for i?>
</lists>