<?
@session_start();
include $_SERVER['DOCUMENT_ROOT']."/front/include/dehead.php";
require_once PATH_LIB."PasswordHash.php"; // 암호화 클래스
require_once PATH_LIB."Valid.php"; // 유효성 검사 클래스

$Db = new Db(); // 데이터 베이스 클래스
$valid = new Valid(); // 유효성 검사 클래스
if($_POST['userId']!='' && $_POST['userEmail']!='' && $_POST['userName']!='' ){//&& $_POST['accessToken']!=''
	//========================================
    // Data Process
    //========================================
	
	if($_POST['userId'] != ''){ //카카오 로그인일때
		$param['id'] = $_POST['userId'];
	}
    //========================================
    // Query
    //========================================
    $check = $Db->single("SELECT COUNT(*) FROM $TABLE_MEMBER WHERE id = :id", $param);
	
	
	$param = array_merge($param, array(
		'type' => 'F',
		'name' => $_POST['userName'],
		'email' => $_POST['userEmail'],
		'mailing' => PARAMETER_Y,
		'sms' => PARAMETER_Y
	));

	$del_query = "SELECT del FROM $TABLE_MEMBER WHERE id = '{$param['id']}' ";
	$del_row = $Db->row($del_query);
	
	if($del_row['del'] == 'Y')
	{
?>
	<script type="text/javascript">
		alert('탈퇴한 회원입니다.');
		window.close();
	</script>
<?php
		exit;
	}

	//AlertBack($_POST['userId']."/////".$check);
    if($check == '0'){ // 아이디가 없을때 회원가입
        $write = $Db->query("INSERT INTO $TABLE_MEMBER (type, id, name, email, mailing, sms, reg_date, mod_date) VALUES(:type, :id, :name, :email, :mailing, :sms, now(), now())", $param);
    }

	$_SESSION['SOCIALNAME'] = "Facebook";
    $_SESSION['MEMBER_LOGIN_EXIST'] = true;
    $_SESSION['MEMBER_ID'] = $param['id'];
    $_SESSION['MEMBER_NAME'] = $param['name'];
	$_SESSION['FBaccessToken']=$_POST['accessToken'];
	
	$logParam = array();
	$logParam = array_merge($logParam, array(
		'result' => '00',
		'message' => 'F_succ',
		'id' => $_POST['userId'],
		'name' => $_POST['userName'],
		'email' => $_POST['userEmail'],
		'ip' => $_SERVER['REMOTE_ADDR']
	));
	
	$log = $Db->query("INSERT INTO $TABLE_NAVERLOGINLOG (result, message, id, name, email, ip, reg_date) VALUES(:result, :message, :id, :name, :email, INET_ATON(:ip), now())", $logParam);
    $Db->CloseConnection;

	$reUrlArr = explode('/',$_POST['returnUrl']);
    if($reUrlArr[0] == "real_view" && $reUrlArr[1] != ""){ // 휜코, 매부리코에서 로그인페이지로 넘어갔을때
        ?><script>location.href = "../epilogue/real_view.php?sort=B&no="+"<?php echo $reUrlArr[1]; ?>";</script><?
    }else if($reUrlArr[0] == "self_view" && $reUrlArr[1] != ""){
		?><script>location.href = "http://www.ssdream.co.kr/front/epilogue/self_view.php?no="+"<?php echo $reUrlArr[1]; ?>";</script><?
	}else if($reUrlArr[0] == "sstv_view" && $reUrlArr[1] != ""){
		?><script>location.href = "http://www.ssdream.co.kr/front/introduction/sstv_view.php?no="+"<?php echo $reUrlArr[1]; ?>";</script><?
	}else if($_POST['returnUrl'] == "real_list"){ // 수술자필후기 리스트에서 팝업레이어
		?><script>jQuery("#searchFrm").attr('action', 'real_view.php').submit();</script><?
	}else if($_POST['returnUrl'] == "self_list"){ // 수술자필후기 리스트에서 팝업레이어
		?><script>jQuery("#searchFrm").attr('action', 'self_view.php#contents').submit();</script><?
	}else if($_POST['returnUrl'] == "sstv_list"){ // 삼성드림TV 리스트에서 팝업레이어
		?><script>jQuery("#searchFrm").attr('action', 'sstv_view.php#contents').submit();</script><?	
	}else if($_POST['returnUrl'] == 'online_write'){ // 수술 후 상담에서 팝업레이어
		?><script>location.href = "../online/after_write.php?md=write";</script><?
	}else if($_POST['returnUrl'] == 'landing'){ // 랜딩 실험1
		?><script>location.href = "../nplastic/nplastic11_.php?scr=1";</script><?
	}else if($_POST['returnUrl'] == 'n_dns2'){ // 랜딩 실험2
		?><script>location.href = "../nsurgery/n_dns2.php";</script><?
	}else if($_POST['returnUrl'] == 'seo'){ // SEO
		?><script>location.href = <? echo "'".$_SERVER['HTTP_REFERER']."'"; ?></script><?
	}else if($reUrlArr[0] == 'beaf'){ // 전후사진
		?><script>location.href = "../nbeaf/nbeaf.php?page="+"<?php echo $reUrlArr[1]; ?>";</script><?
	}else{
		?><script>location.href = '/';</script><?
        //window.location.reload();
    }
	

}else {
    AlertBack("이메일 제공에 동의 해주세요.");
	?><script>facebooklogout();</script><?
}


?>