<?php
	require_once "pageBase.php";
	require_once "bin/system/conver.php";
	require_once "bin/system/email.php";
	require_once "bin/system/smtp_mail.php";
	require_once "bin/web/repeater.php";
	require_once "dal/customerDAL.php";
	require_once "Facebook/autoload.php";
	require_once "Facebook/Facebook.php";

	class _newmember extends pageBase{
		public function load(){
			if(isset($_SESSION["CustomerID"])){
				parent::alert2("Bạn đã đăng ký thành công, không cần phải đăng ký nửa","/");
			}

			if(!parent::isPostBack())
	 			$this->loadData();
 			else
 				$this->saveData();
			
			parent::load();
		}
		public function loadData(){
			//加載模板
			parent::loadTemplateFile("templates/".$this->registry->webTemplates."/newmember.html");
			
			//應用程式配置載入
			$fb = new Facebook\Facebook([
				'app_id' => '460847270964486',
				'app_secret' => '760b9abdc5df67d9cf92e2285c5be4e3',
				'default_graph_version' => 'v2.10'
			]);
			
			$helper = $fb->getRedirectLoginHelper();
			$permissions = ['email', 'user_likes']; // optional
			$loginUrl = $helper->getLoginUrl('http://543th.com/fbloging.php', $permissions);
			//parent::addParameter("loginUrl",$loginUrl);

			//橫幅廣告
			$this->picturetype = "50";

			//父類方法
			parent::loadData();
		}
		
		public function saveData(){
			$dal = new customerDAL();
			$LoginName = conver::toDbString(trim($_POST["LoginName"]));
			$LoginPassword = conver::toDbString(trim($_POST["LoginPassword"]));
			$LoginPassword2 = conver::toDbString(trim($_POST["LoginPassword2"]));
			$CustomerName = conver::toDbString(trim($_POST["CustomerName"]));
			$Mobile = conver::toDbString(trim($_POST["Mobile"]));
			$LineID = conver::toDbString(trim($_POST["LineID"]));
			$Checknum = conver::toDbString(trim($_POST["Checknum"]));
			
			$errorTxt = 'Không đúng!\r\n';
			$reMail = "/^[A-Za-z0-9\-_\.]+@[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)+$/";
			if(!preg_match($reMail, $LoginName)){
				$newmemberBack = "Y";
				$errorTxt .= 'Địa chỉ email bị sai!\r\n';
			}else{
				if($dal->countRecordset("a.`LoginName`='".$LoginName."'") > 0){
					$newmemberBack = "Y";
					$errorTxt .= '「'.$LoginName.'」Đã từng đăng ký!!\r\n';
				}
			}
			$rePass = "/^[A-Za-z0-9]{8,20}$/";
			if(!preg_match($rePass,$LoginPassword)){
				$newmemberBack = "Y";
				$errorTxt .= 'Mật khẩu bị sai!\r\n';
			}
			if($LoginPassword != $LoginPassword2){
				$newmemberBack = "Y";
				$errorTxt .= 'Mật khẩu hai bên không giống nhau!\r\n';
			}
			if($CustomerName == ""){
				$newmemberBack = "Y";
				$errorTxt .= 'Họ tên không được để trống!\r\n';
			}			
			$reMobile = "/^09[0-9]{8}$/";
			if(!preg_match($reMobile,$Mobile)){
				$newmemberBack = "Y";
				$errorTxt .= 'Số điện thoại di động không đúng!\r\n';
			}
			if($Checknum == "" || !isset($_SESSION["dd_ckstr"]) || $Checknum != $_SESSION["dd_ckstr"]){
				$newmemberBack = "Y";
				$errorTxt .= 'Mã xác nhận không đúng!\r\n';
			}
			
			if($newmemberBack == "Y"){
				parent::alert2($errorTxt,"newmember.html");
				exit;
			}else{
				$dal->LoginName = $LoginName;
				$dal->LoginPassword = $LoginPassword;
				$dal->CustomerName = $CustomerName;
				$dal->Mobile = $Mobile;
				$dal->LineID = $LineID;
				if(!empty($_SERVER["HTTP_CLIENT_IP"])){
					$ip = $_SERVER["HTTP_CLIENT_IP"];
				}else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
					$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
				}else{
					$ip = $_SERVER["REMOTE_ADDR"];
				}
				$dal->CreatedIP = $ip;
				
				//系統信件內容
				$mailtxt = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
				$mailtxt .= '<html xmlns="http://www.w3.org/1999/xhtml">';
				$mailtxt .= '<head>';
				$mailtxt .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
				$mailtxt .= '</head>';
				$mailtxt .= '<body>';
				$mailtxt .= '<table border="1" align="center" cellpadding="0" cellspacing="0" style="width:660px; border:1px #000 solid;">';
				$mailtxt .= '<tr>';
				$mailtxt .= '<td colspan="2" style="border:0; border-bottom:1px #ccc solid; text-align:center;"><img src="';
				if($dalP->Image == ''){
					$mailtxt .= '';
				}else{
					$mailtxt .= 'http://'.$_SERVER["HTTP_HOST"].$dalP->Image;
				}
				$mailtxt .= '" style="margin:30px;" /></td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '<tr style="background:#E7E7E7;">';
				$mailtxt .= '<td colspan="2" style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
				$mailtxt .= '親愛的會員「'.$dal->CustomerName.'」您好<br />';
				$mailtxt .= '您已經在「<a href="http://'.$_SERVER["HTTP_HOST"].'">'.$this->registry->webName.'</a>」中完成會員註冊!!!<br /><br />';
				$mailtxt .= '</td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '<tr>';
				$mailtxt .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
				$mailtxt .= '<br />會員帳號：<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
				$mailtxt .= '<br />'.$dal->LoginName.'<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '<tr style="background:#E7E7E7;">';
				$mailtxt .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
				$mailtxt .= '<br />登入密碼：<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
				$mailtxt .= '<br />'.substr($dal->LoginPassword,0,2);
				$passLen = strlen($dal->LoginPassword) - 4;
				for($i=0;$i<$passLen;$i++){
					$mailtxt .= "*";
				}				
				$mailtxt .= substr($dal->LoginPassword,-2);
				$mailtxt .= '</td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '</table><br />';
				$mailtxt .= '<p style="padding:10px;">如果您並未在本網站註冊，請來信到我們的客服信箱「<a href="mailto:'.$this->registry->webEmail.'">'.$this->registry->webEmail.'</a>」</p>';
				$mailtxt .= '<p style="padding:10px; color:#f00; font-weight:bolder;">此信件為系統自動發送!請勿直接回覆!</p>';
				$mailtxt .= '</body>';
				$mailtxt .= '</html>';

				if($this->registry->sendMailType == ""){
					$email = new email();
					$email->fromAddress = "=?UTF-8?B?".base64_encode($this->registry->webName)."?=<".$this->registry->webEmail.">";
					$email->replyToAddress = "=?UTF-8?B?".base64_encode($dal->CustomerName)."?=<".$dal->LoginName.">";
					$email->subject = "[".$_SERVER['HTTP_HOST']."][會員註冊通知信]";
					$email->content = $mailtxt;
					$email->toAddress = $dal->LoginName;
					if($email->send()){
						if(!$dal->insertRecord()){
							parent::alert("Khi đăng ký tài khoản xảy ra lỗi, vui lòng nhập lại lần nửa註冊時發生無法預期的錯誤!請再試一次!");
						}else{
							parent::alert2("Đăng ký thành công!","memberlogin.html");
						}
					}else{
						parent::alert("Thành thật xin lỗi, Đã xảy ra lỗi khi đăng ký");
					}
				}else{
					$toAry = array();
					$toAry[] = $dal->LoginName;
					$smtp_mail = new smtp_mail();
					$smtp_mail->toAry = $toAry;
					$smtp_mail->name = "=?UTF-8?B?".base64_encode($this->registry->webName)."?=";
					$smtp_mail->subject = "[".$_SERVER['HTTP_HOST']."][會員註冊通知信]";
					$smtp_mail->body = $mailtxt;
					if($smtp_mail->send()){
						if(!$dal->insertRecord()){
							parent::alert("Khi đăng ký tài khoản xảy ra lỗi, vui lòng nhập lại lần nửa");
						}else{
							parent::alert2("Đăng ký thành công!","memberlogin.html");
						}
					}else{
						parent::alert("Thành thật xin lỗi, Đã xảy ra lỗi khi đăng ký");
					}
				}
			}
		}
	}

	$page=new _newmember();
	$page->load();
	$page->render();
?>