<?
require_once $_SERVER["DOCUMENT_ROOT"]."/common/common.php";
function file_upload($argu){

	$_uploaded = array();
	
	// 첨부파일 argu 를 정리한다.
	if(is_array($_FILES)){
		foreach($_FILES as $fname => $fileobj){
			$_FILE_VALUE = $_FILES[$fname];

			$tmp_file="tmp_".$fname;
			$del_file="del_".$fname;

			if($_FILE_VALUE !== null && $_FILE_VALUE[error] == 0 && $_FILE_VALUE[size] > 0 ){// 첨부파일이 있을경우


				if(preg_match('/\\.(php|htm|html|asp|jsp|js|com|bat|exe|dl|dll)$/i', strtolower($_FILE_VALUE["name"]))){ // 확장자 금지항목 체크
					go_url(_BASIC_HOME_URL,"금지된 확장자의 파일 업로드를 시도하셨습니다.");
					exit;
				}
				
				$_sf_type = array_pop(explode(".",$_FILE_VALUE["name"]));
				$_sf_type = strtolower($_sf_type);
				$now = date('YmdHi');
				$rand = rand(1000,9999);
				$_filename = $now."_".$rand.".".$_sf_type;

				if($_FILE_VALUE[size]>20*1024*1024){// 용량이 클 경우(20M)
					go_url(_BASIC_HOME_URL,"업로드 제한용량을 초과하였습니다.");
					exit;
				}else{
					if(file_exists($_SERVER["DOCUMENT_ROOT"]."/pds/contact".$_filename)){ // 파일이 존재할 경우
						@unlink($_SERVER["DOCUMENT_ROOT"]."/pds/contact".$_filename);
					}

					if(move_uploaded_file($_FILE_VALUE["tmp_name"], $_SERVER["DOCUMENT_ROOT"]."/pds/contact/".$_filename)){
						$_uploaded[$fname] = $_filename;
					}else{
						go_url("","파일 업로드에 실패했습니다.");
						exit;
					}
				}
			}else{
				$_uploaded[$fname] = $argu[$tmp_file];
			}

			if($argu[$del_file]==1){
				@unlink($_SERVER["DOCUMENT_ROOT"]."/pds/contact/".$argu[$tmp_file]);
				$_uploaded[$fname] = "";
			}
		}
	}

	return $_uploaded;
}

function attach_file($name, $file) {
   $fp = fopen($file, "r");
   $tmpfile = array(
	  "name" => $name,
	  "data" => $file);
	  //"data" => fread($fp, filesize($file)));
   fclose($fp);
   return $tmpfile;
}

if(_SITE_KEY != "" && _SECRET_KEY != ""){
	if(isset($_POST['g-recaptcha-response'])){
		$captcha=$_POST['g-recaptcha-response'];
	}
	else{
		$captcha = false;
	}
	if(!$captcha){
		go_url("/customer3.php",$lang["MSG1"]);
		exit;
	}
	$secret = _SECRET_KEY;
	$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);


	if($response.success==true){
		if(is_array($_POST)){
			
			$upfile = file_upload($argu);
			$argu['upfile'] = $upfile["upfile"];
			$argu['upfile_ori'] = $_FILES["upfile"]["name"];
			

			if($Obj_contact -> set_insert($argu)){

				
				if(_SMTP_MAIL_ID != ""){
					///메일 발송
					$HOST = _SMTP_MAIL_HOST;
					$SMTP_MAIL_ID = _SMTP_MAIL_ID;
					$SMTP_MAIL_PW = _SMTP_MAIL_PW;
					$FROM_EMAIL = _SMTP_MAIL_ID;
					$FROM_NAME = _MASTER_NAME;
	
					$SUBJECT = "["._MASTER_NAME."] ".$argu['name']."님 문의 접수.";
	
					$CONTENT = "<p>이름 : ".$argu['name']."</p>";
					$CONTENT .= "<p>연락처 : ".$argu['phone']."</p>";
					$CONTENT .= "<p>E-mail : ".$argu["email"]."</p>";
					$CONTENT .= nl2br($argu["content"]);
	
					$MAILTO = _MASTER_EMAIL;
					$MAILTONAME = _MASTER_NAME;
					if($SMTP_MAIL_ID != "" && $MAILTO != ""){
						//$file[0] = attach_file($_FILES["upfile"]["name"],$_SERVER["DOCUMENT_ROOT"]."/pds/contact/".$upfile["upfile"]);
						$file = "";
	
						$mail = send_smtp($HOST, $SMTP_MAIL_ID, $SMTP_MAIL_PW, $FROM_EMAIL, $FROM_NAME, $SUBJECT, $CONTENT, $MAILTO, $MAILTONAME, $file);
					}
	
				}
				//if($argu["phone"] != ""){
//					$_SMS_SENDER = _SMS_SENDER;
//					$_SMS_MESG = $argu["name"]."님 께서 문의 하셨습니다.\n TEL : ".$argu["phone"];
//					$_SMS_DIVISION = "sms";
//					$_SMS_RECEIVERS = str_replace("-","",_SMS_RECEIVERS);
//					$_SMS_SUBJECT = "블레싱 디자인";
//
//					if(_SMS_ID != "" && _SMS_PW != ""){
//						$_sms = SendSms($_SMS_SENDER,$_SMS_RECEIVERS,$_SMS_SUBJECT,$_SMS_MESG, $_SMS_DIVISION);
//					}
				//}

				go_url("/customer3.php",$lang["MSG2"]);
				exit;
			}
			else{
				go_url("/customer3.php",$lang["MSG3"]);
				exit;
			}
		}

	}
	else{
		go_url("/customer3.php",$lang["MSG1"]);
		exit;
	}
}
else{
	if(is_array($_POST)){
		
		$upfile = file_upload($argu);
		$argu['upfile'] = $upfile["upfile"];
		$argu['upfile_ori'] = $_FILES["upfile"]["name"];

		if($Obj_contact -> set_insert($argu)){

			
			if(_SMTP_MAIL_ID != ""){
				///메일 발송
				$HOST = _SMTP_MAIL_HOST;
				$SMTP_MAIL_ID = _SMTP_MAIL_ID;
				$SMTP_MAIL_PW = _SMTP_MAIL_PW;
				$FROM_EMAIL = _SMTP_MAIL_ID;
				$FROM_NAME = _MASTER_NAME;

				$SUBJECT = "["._MASTER_NAME."] ".$argu['name']."님 문의 접수.";

				$CONTENT = "<p>이름 : ".$argu['name']."</p>";
				$CONTENT .= "<p>연락처 : ".$argu['phone']."</p>";
				$CONTENT .= "<p>E-mail : ".$argu["email"]."</p>";
				$CONTENT .= nl2br($argu["content"]);

				$MAILTO = _MASTER_EMAIL;
				$MAILTONAME = _MASTER_NAME;
				if($SMTP_MAIL_ID != "" && $MAILTO != ""){
					//$file[0] = attach_file($_FILES["upfile"]["name"],$_SERVER["DOCUMENT_ROOT"]."/pds/contact/".$upfile["upfile"]);
					$file = "";

					$mail = send_smtp($HOST, $SMTP_MAIL_ID, $SMTP_MAIL_PW, $FROM_EMAIL, $FROM_NAME, $SUBJECT, $CONTENT, $MAILTO, $MAILTONAME, $file);
				}

			}
			//if($argu["phone"] != ""){
//				$_SMS_SENDER = _SMS_SENDER;
//				$_SMS_MESG = $argu["name"]."님 께서 문의 하셨습니다.\n TEL : ".$argu["phone"];
//				$_SMS_DIVISION = "sms";
//				$_SMS_RECEIVERS = str_replace("-","",_SMS_RECEIVERS);
//				$_SMS_SUBJECT = "블레싱 디자인";
//
//				if(_SMS_ID != "" && _SMS_PW != ""){
//					$_sms = SendSms($_SMS_SENDER,$_SMS_RECEIVERS,$_SMS_SUBJECT,$_SMS_MESG, $_SMS_DIVISION);
//				}
			//}

			go_url("/customer3.php",$lang["MSG2"]);
			exit;
		}
		else{
			go_url("/customer3.php",$lang["MSG3"]);
			exit;
		}
	}
}


?>