<?
/*********************************** 회원관련 *************************************/
//회원등급 등록
function createMemberLevel($level_no, $level_name){
	// 테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["member_level"];

	// 테이블에 입력
	$sql = "INSERT INTO ".$tbl." set 
		level_no='$level_no',
		level_name='$level_name',
		wdate = now()
	";

	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total = mysql_affected_rows($GLOBALS[dblink]);

	if($total > 0){
		return true;
	}else{
		return false;
	}
}



//회원등급 수정하기
function editMemberLevel($idx){
	// 테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["member_level"];

	// 테이블에 입력
	$sql = "UPDATE ".$tbl." set 
		level_no='".mysql_real_escape_string($_POST[level_no])."',
		level_name='".mysql_real_escape_string($_POST[level_name])."'
		WHERE idx='".mysql_real_escape_string($_POST[idx])."'
	";

	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total = mysql_affected_rows($GLOBALS[dblink]);


	if($rs){
		return true;
	}else{
		return false;
	}
}



//회원목록
function getMemberList($sw, $sk, $scale, $offset=0){
	// 테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["member"];

    $sql = "SELECT * FROM $tbl WHERE 1=1 ";


	if($sw == "id"){
		$sql .= " AND user_id like '%$sk%' ";
	}else if($sw == "name"){
		$sql .= " AND user_name like '%$sk%' ";
	}else if($sw == "all"){
		$sql .= " AND ( (user_name like '%$sk%') OR (user_id like '%$sk%') )";
	}

	if($_REQUEST[s_date]){
		$sql .= " AND wdate >= '".mysql_real_escape_string($_REQUEST[s_date])."' ";
	}
	if($_REQUEST[e_date]){
		$sql .= " AND wdate <= '".mysql_real_escape_string($_REQUEST[e_date])."' ";
	}
	$sql .= " order by idx desc ";


    $rs = mysql_query($sql, $GLOBALS[dblink]);
    $total_rs = mysql_num_rows($rs);


    if($total_rs > 0){
        $list['total'] = $total_rs;
        // 페이지 네비게이션 오프셋 지정.
		    if(!$offset){
		        $offset=0;
		    }else{
		        $offset=$offset;
		    }

		    // offset 이 전체 게시물수보다 작을때 offset 을 전체게시물 - 페이지당 보여줄 글 수로 offset 설정
		    if($total_rs<=$offset){
		        $offset = $total_rs - $scale;
		    }

				//scale 0 으로 지정시에는 전체 가져옴
				if($scale > 0){
		    	$sql .= " limit $offset,$scale ";
				}
		    $rs = mysql_query($sql,$GLOBALS[dblink]);
		
		    // offset 을 이용한 limit 가 적용된 갯수
		    $total = mysql_num_rows($rs);
		    $list['list']['total'] = $total;
		    // 페이지 네비게이션 오프셋 지정.
		    
        for($i=0; $i < $total; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
    }
    
    return $list;
}


//회원가입
function joinMember(){
	$tbl = $GLOBALS["_conf_tbl"]["member"];

	$birth = mysql_real_escape_string($_POST[byear])."-".mysql_real_escape_string($_POST[bmonth])."-".mysql_real_escape_string($_POST[bday]);
	$solar = mysql_real_escape_string($_POST[solar])!=""?mysql_real_escape_string($_POST[solar]):"E";
	$email = mysql_real_escape_string($_POST[email_id]) . "@" . mysql_real_escape_string($_POST[email_domain]);
	$email_accept = mysql_real_escape_string($_POST[email_accept])=="Y"?"Y":"N";
	if($email_accept=="Y"){
		$email_accept_date = date("Y-m-d H:i:s");
	}
	if($_POST[zip]!=""){
		$zip = mysql_real_escape_string($_POST[zip]);
	}else{
		$zip = mysql_real_escape_string($_POST[zip1]) . "-" . mysql_real_escape_string($_POST[zip2]);
	}
	$address_type = mysql_real_escape_string($_POST[address_type])=="자택"?"자택":"직장";
	$phone = mysql_real_escape_string($_POST[phone_1]) . "-" . mysql_real_escape_string($_POST[phone_2]) . "-" . mysql_real_escape_string($_POST[phone_3]);
	$mobile = mysql_real_escape_string($_POST[mobile_1]) . "-" . mysql_real_escape_string($_POST[mobile_2]) . "-" . mysql_real_escape_string($_POST[mobile_3]);
	$fax = mysql_real_escape_string($_POST[fax_1]) . "-" . mysql_real_escape_string($_POST[fax_2]) . "-" . mysql_real_escape_string($_POST[fax_3]);

	$etc_1 = mysql_real_escape_string($_POST[cmp1]) . "-" . mysql_real_escape_string($_POST[cmp2]) . "-" . mysql_real_escape_string($_POST[cmp3]);
	$marriage = mysql_real_escape_string($_POST[marriage])!=""?mysql_real_escape_string($_POST[marriage]):"E";
	$marriage_date = mysql_real_escape_string($_POST[marriage_date]);
	$sms_accept = mysql_real_escape_string($_POST[sms_accept])=="Y"?"Y":"N";
	if($sms_accept=="Y"){
		$sms_accept_date = date("Y-m-d H:i:s");
	}
	$arrCheck = getUserInfo(mysql_real_escape_string($_POST[user_id]));

	if($arrCheck["total"] > 0){
		return false;
	}else{
		$sql = "INSERT INTO ".$tbl." set 
			user_id = '".mysql_real_escape_string($_POST[user_id])."',
			user_pw = '".mysql_real_escape_string($_POST[user_pw])."',
			regnum1 = '".mysql_real_escape_string($_POST[regnum1])."',
			regnum2 = '".mysql_real_escape_string($_POST[regnum2])."',
			user_name = '".mysql_real_escape_string($_POST[user_name])."',
			user_status = '0',
			user_level = '1',
			user_memo = '".mysql_real_escape_string($_POST[user_memo])."',
			company = '".mysql_real_escape_string($_POST[company])."',
			department = '".mysql_real_escape_string($_POST[department])."',
			duty = '".mysql_real_escape_string($_POST[duty])."',
			birth = '$birth',
			solar = '$solar',
			sex = '".mysql_real_escape_string($_POST[sex])."',
			email = '".$email."',
			zip = '".$zip."',
			address = '".mysql_real_escape_string($_POST[address])."',
			address_ext = '".mysql_real_escape_string($_POST[address_ext])."',
			address_type = '".$address_type."',
			phone = '".$phone."',
			mobile = '".$mobile."',
			fax = '".$fax."',
			f_cat = '$implode_f_cat',
			f_product = '$implode_f_product',
			email_accept = '$email_accept',
			email_accept_date = '$email_accept_date',
			sms_accept = '$sms_accept',
			sms_accept_date = '$sms_accept_date',
			marriage = '$marriage',
			marriage_date = '$marriage_date',
			job = '".mysql_real_escape_string($_POST[job])."',
			etc_1 = '".$etc_1."',
			etc_2 = '".$filename."',
			etc_3 = '".$filerename."',
			etc_4 = '".mysql_real_escape_string($_POST[etc_4])."',
			etc_5 = '".mysql_real_escape_string($_POST[etc_5])."',
			etc_6 = '".mysql_real_escape_string($_POST[etc_6])."',
			etc_7 = '".mysql_real_escape_string($_POST[etc_7])."',
			etc_8 = '".mysql_real_escape_string($_POST[etc_8])."',
			etc_9 = '".mysql_real_escape_string($_POST[etc_9])."',
			etc_10 = '".mysql_real_escape_string($_POST[etc_10])."',
			wdate = now(),
			udate = now()
		";

		$rs = mysql_query($sql, $GLOBALS[dblink]);
		$total = mysql_affected_rows($GLOBALS[dblink]);

		if($total > 0){

			//$RS = setPlusPoint($_POST[user_id], "2000", "회원가입 포인트");

			return true;
		}else{
			return false;
		}
	}
}

//회원정보 수정 - 관리자용
function editMemberAdmin($id){
	$tbl = $GLOBALS["_conf_tbl"]["member"];
	
	if($_POST[user_pw] !="" && $_POST[user_pw] !="" && $_POST[user_pw] == $_POST[user_pw2]){
		$sql_pw = " user_pw = '".mysql_real_escape_string($_POST[user_pw])."', ";
	}

	$birth = mysql_real_escape_string($_POST[birth]);
	$solar = mysql_real_escape_string($_POST[solar])=="S"?"S":"L";
	$sex = mysql_real_escape_string($_POST[sex])=="M"?"M":"F";
	$email = mysql_real_escape_string($_POST[email_id]) . "@" . mysql_real_escape_string($_POST[email_domain]);
	$email_accept = mysql_real_escape_string($_POST[email_accept])=="Y"?"Y":"N";
	if($email_accept=="Y"){
		$email_accept_date = date("Y-m-d H:i:s");
	}
	$user_status = mysql_real_escape_string($_POST[user_status])=="1"?"1":"0";
	$user_level = mysql_real_escape_string($_POST[user_level]);
	if($_POST[zip]!=""){
		$zip = mysql_real_escape_string($_POST[zip]);
	}else{
		$zip = mysql_real_escape_string($_POST[zip1]) . "-" . mysql_real_escape_string($_POST[zip2]);
	}
	$address_type = mysql_real_escape_string($_POST[address_type])=="자택"?"자택":"직장";
	$phone = mysql_real_escape_string($_POST[phone_1]) . "-" . mysql_real_escape_string($_POST[phone_2]) . "-" . mysql_real_escape_string($_POST[phone_3]);
	$mobile = mysql_real_escape_string($_POST[mobile_1]) . "-" . mysql_real_escape_string($_POST[mobile_2]) . "-" . mysql_real_escape_string($_POST[mobile_3]);
	$fax = mysql_real_escape_string($_POST[fax_1]) . "-" . mysql_real_escape_string($_POST[fax_2]) . "-" . mysql_real_escape_string($_POST[fax_3]);

	$marriage = mysql_real_escape_string($_POST[marriage])!=""?mysql_real_escape_string($_POST[marriage]):"E";
	$marriage_date = mysql_real_escape_string($_POST[marriage_date]);
	$sms_accept = mysql_real_escape_string($_POST[sms_accept])=="Y"?"Y":"N";
	if($sms_accept=="Y"){
		$sms_accept_date = date("Y-m-d H:i:s");
	}

	$sql = "UPDATE ".$tbl." SET
		$sql_pw
		user_status = '$user_status',
		user_level = '$user_level',
		user_memo = '".mysql_real_escape_string($_POST[user_memo])."',
		company = '".mysql_real_escape_string($_POST[company])."',
		department = '".mysql_real_escape_string($_POST[department])."',
		duty = '".mysql_real_escape_string($_POST[duty])."',
		birth = '$birth',
		solar = '$solar',
		sex = '$sex',
		email = '".$email."',
		zip = '".$zip."',
		address = '".mysql_real_escape_string($_POST[address])."',
		address_ext = '".mysql_real_escape_string($_POST[address_ext])."',
		address_type = '".$address_type."',
		phone = '".$phone."',
		mobile = '".$mobile."',
		fax = '".$fax."',
		f_cat = '$implode_f_cat',
		f_product = '$implode_f_product',
		email_accept = '$email_accept',
		email_accept_date = '$email_accept_date',
		sms_accept = '$sms_accept',
		sms_accept_date = '$sms_accept_date',
		marriage = '$marriage',
		marriage_date = '$marriage_date',
		job = '".mysql_real_escape_string($_POST[job])."',
		etc_4 = '".mysql_real_escape_string($_POST[etc_4])."',
		etc_5 = '".mysql_real_escape_string($_POST[etc_5])."',
		etc_6 = '".mysql_real_escape_string($_POST[etc_6])."',
		etc_7 = '".mysql_real_escape_string($_POST[etc_7])."',
		etc_8 = '".mysql_real_escape_string($_POST[etc_8])."',
		etc_9 = '".mysql_real_escape_string($_POST[etc_9])."',
		etc_10 = '".mysql_real_escape_string($_POST[etc_10])."',
		udate = now()
		WHERE user_id='$id'
	";

	$rs = mysql_query($sql, $GLOBALS[dblink]);

	if($rs){
		return true;
	}else{
		return false;
	}
}

//회원정보 수정
function editMember($id){
	$tbl = $GLOBALS["_conf_tbl"]["member"];
	
	if($_POST[user_pw] !="" && $_POST[user_pw] !="" && $_POST[user_pw] == $_POST[user_pw2]){
		$sql_pw = " user_pw = '".mysql_real_escape_string($_POST[user_pw])."', ";
	}

	$birth = mysql_real_escape_string($_POST[byear])."-".mysql_real_escape_string($_POST[bmonth])."-".mysql_real_escape_string($_POST[bday]);
	$solar = mysql_real_escape_string($_POST[solar])=="S"?"S":"L";
	//$sex = substr($_POST[regnum2],0,1)%2==1?"M":"F";
	$email = mysql_real_escape_string($_POST[email_id]) . "@" . mysql_real_escape_string($_POST[email_domain]);
	$email_accept = mysql_real_escape_string($_POST[email_accept])=="Y"?"Y":"N";
	if($email_accept=="Y"){
		$email_accept_date = date("Y-m-d H:i:s");
	}
	if($_POST[zip]!=""){
		$zip = mysql_real_escape_string($_POST[zip]);
	}else{
		$zip = mysql_real_escape_string($_POST[zip1]) . "-" . mysql_real_escape_string($_POST[zip2]);
	}
	$address_type = mysql_real_escape_string($_POST[address_type])=="자택"?"자택":"직장";
	$phone = mysql_real_escape_string($_POST[phone_1]) . "-" . mysql_real_escape_string($_POST[phone_2]) . "-" . mysql_real_escape_string($_POST[phone_3]);
	$mobile = mysql_real_escape_string($_POST[mobile_1]) . "-" . mysql_real_escape_string($_POST[mobile_2]) . "-" . mysql_real_escape_string($_POST[mobile_3]);
	$fax = mysql_real_escape_string($_POST[fax_1]) . "-" . mysql_real_escape_string($_POST[fax_2]) . "-" . mysql_real_escape_string($_POST[fax_3]);

	$marriage = mysql_real_escape_string($_POST[marriage])!=""?mysql_real_escape_string($_POST[marriage]):"E";
	$marriage_date = mysql_real_escape_string($_POST[marriage_date]);
	$sms_accept = mysql_real_escape_string($_POST[sms_accept])=="Y"?"Y":"N";
	if($sms_accept=="Y"){
		$sms_accept_date = date("Y-m-d H:i:s");
	}

	$sql = "UPDATE ".$tbl." SET
		$sql_pw
		user_memo = '".mysql_real_escape_string($_POST[user_memo])."',
		company = '".mysql_real_escape_string($_POST[company])."',
		department = '".mysql_real_escape_string($_POST[department])."',
		duty = '".mysql_real_escape_string($_POST[duty])."',
		birth = '$birth',
		solar = '$solar',
		email = '".$email."',
		zip = '".$zip."',
		address = '".mysql_real_escape_string($_POST[address])."',
		address_ext = '".mysql_real_escape_string($_POST[address_ext])."',
		address_type = '".$address_type."',
		phone = '".$phone."',
		mobile = '".$mobile."',
		fax = '".$fax."',
		f_cat = '$implode_f_cat',
		f_product = '$implode_f_product',
		email_accept = '$email_accept',
		email_accept_date = '$email_accept_date',
		sms_accept = '$sms_accept',
		sms_accept_date = '$sms_accept_date',
		marriage = '$marriage',
		marriage_date = '$marriage_date',
		job = '".mysql_real_escape_string($_POST[job])."',
		etc_4 = '".mysql_real_escape_string($_POST[etc_4])."',
		etc_5 = '".mysql_real_escape_string($_POST[etc_5])."',
		etc_6 = '".mysql_real_escape_string($_POST[etc_6])."',
		etc_7 = '".mysql_real_escape_string($_POST[etc_7])."',
		etc_8 = '".mysql_real_escape_string($_POST[etc_8])."',
		etc_9 = '".mysql_real_escape_string($_POST[etc_9])."',
		etc_10 = '".mysql_real_escape_string($_POST[etc_10])."',
		udate = now()
		WHERE user_id='$id'
	";

	$rs = mysql_query($sql, $GLOBALS[dblink]);

	if($rs){
		return true;
	}else{
		return false;
	}
}

//회원정보 가져오기 - 사업자번호 중복체크용
function getUserFindCompanyNumber($etc_1){
	$tbl = $GLOBALS["_conf_tbl"]["member"];

	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl." ";
	$sql .= "WHERE etc_1='$etc_1' ";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}
	return $list;
}


//회원정보 가져오기
function getUserInfo($id){
	$tbl = $GLOBALS["_conf_tbl"]["member"];

	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl." ";
	$sql .= "WHERE user_id = '$id' ";

	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}
	return $list;
}

//기간별회원
function getMemberInfo($sdate, $edate) {
	$tbl = $GLOBALS["_conf_tbl"]["member"];

	$sql  = "SELECT count(*) as num ";
	$sql .= "FROM ".$tbl." ";
	$sql .= "WHERE 1=1 ";
	if($sdate){
		$sql .= " AND wdate >= '".mysql_real_escape_string($sdate)." 00:00:00' ";
	}
	if($edate){
		$sql .= " AND wdate <= '".mysql_real_escape_string($edate)." 23:59:59' ";
	}
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}
	return $list;
}

//회원정보 가져오기 - 주민등록번호 중복체크용
function getUserFindRegnum($regnum1, $regnum2){
	$tbl = $GLOBALS["_conf_tbl"]["member"];

	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl." ";
	$sql .= "WHERE regnum1='$regnum1' AND regnum2 = md5('$regnum2') ";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}
	return $list;
}


//회원정보 가져오기 - 아이디 찾기용
function getUserFindID($name, $email){
	$tbl = $GLOBALS["_conf_tbl"]["member"];

	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl." ";
	$sql .= "WHERE user_name = '$name' AND email='$email'";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}
	return $list;
}

//회원정보 가져오기 - 비밀번호 찾기용
function getUserFindPW($id, $email){
	$tbl = $GLOBALS["_conf_tbl"]["member"];

	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl." ";
	$sql .= "WHERE user_id = '$id' AND email='$email'";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}
	return $list;
}

//회원정보 가져오기 - 로그인용
function loginMember($id, $pw){
	$tbl = $GLOBALS["_conf_tbl"]["member"];

	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl." ";
	$sql .= "WHERE user_id = '$id' AND user_pw = '$pw' ";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			//로그인정보 기록
			mysql_query("update ".$GLOBALS["_conf_tbl"]["member"]." set login_count = login_count + 1, login_last = now() WHERE user_id='$id' ", $GLOBALS[dblink]);
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}
	return $list;
}

//회원정보 가져오기 - 회원탈퇴용
function withdrawalMember($id, $pw, $email){
	$tbl = $GLOBALS["_conf_tbl"]["member"];

	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl." ";
	$sql .= "WHERE user_id = '$id' AND user_pw = '$pw' AND email='$email' ";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}
	return $list;
}



//회원탈퇴 / 삭제
function deleteMember($id){
	//회원정보 테이블
	$tbl = $GLOBALS["_conf_tbl"]["member"];
	$tbl_point = $GLOBALS["_conf_tbl"]["point"];
	$tbl_order = $GLOBALS["_conf_tbl"]["shop_order_info"];

	//회원 정보 삭제
	$sql = "DELETE FROM ".$tbl." WHERE user_id='".$id."'	";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_affected_rows();

	//적립금 테이블 탈퇴회원아이디로 업데이트
	$sql = "UPDATE ".$tbl_point." SET user_id='deleted_user' WHERE user_id='".$id."'	";
	$rs = mysql_query($sql, $GLOBALS[dblink]);

	//주문정보 테이블  탈퇴회원아이디로 업데이트
	$sql = "UPDATE ".$tbl_order." SET order_id='deleted_user' WHERE order_id='".$id."'	";
	$rs = mysql_query($sql, $GLOBALS[dblink]);

	if($total_rs > 0){
		return true;
	}else{
		return false;
	}
}
?>
