<?php
	require_once "../../pageBase.php";
	require_once "../class/class.php";
	require_once "../../bin/system/conver.php";
	require_once "../../bin/web/tables/sends.php";
	require_once "../../dal/userDAL.php";
	require_once "../../dal/registryDAL.php";
	class _mylogin extends pageBase {
		private $dal;
		private string $protocol;
		private array $aryJ;

		public function __construct() {
			$this->dal = new userDAL();
			$this->aryJ = ["err" => "0", "html" => "", "msg" => ""];
		}

		public function load(): void {
			$this->aryJ["msg"] = "";

			$action = parent::getPost("do");
			switch ($action) {
				case "getcode":
					$this->getCode();
					break;
				default:
					$this->handleError("操作異常！請重新整理頁面再試一次！");
			}
		
			if ($this->aryJ["msg"] !== "") $this->aryJ["msg"] = urlencode($this->aryJ["msg"]);
			echo urldecode(json_encode($this->aryJ));
			parent::load();
		}
		
		private function getCode() {
			$login = parent::getPost("login");
			
			if ($login !== "") {
				$this->dal->LoginName = $login;
				$this->dal->loadRecord();
				
				if ($this->dal->UserID === 1) {
					$this->aryJ["html"] = "<label class=\"mb_1\">驗證碼</label><font class=\"d_flex f_justify_center f_aitems_center\"><input type=\"text\" id=\"checknum\" name=\"checknum\" onKeyUp=\"this.value=this.value.toUpperCase();\" autocomplete=\"off\" maxlength=\"4\" /><img class=\"ml_3\" src=\"/vdimgck.php\" id=\"rand-img\" alt=\"看不清楚？ 點圖更換新圖片\" align=\"absmiddle\" style=\"cursor:pointer; border:none; width:auto;\"></font>";
				} else {
					$word = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";		//避免誤認，沒有I和O
					$wlen = strlen($word);
					$checkCode = "";
					for($i=0;$i<6;$i++) $checkCode .= $word[mt_rand() % $wlen];
					$_SESSION["dd_ckstr"] = $checkCode;

					//系統信件內容
					$mtxt = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
					$mtxt .= "<html xmlns=\"https://www.w3.org/1999/xhtml\">";
					$mtxt .= "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head>";
					$mtxt .= "<body>";
					$mtxt .= "<p>您好：<br />您在" . registryDAL::getRegistryValue("webName") . "後台登錄需要進驗證。<br /></p>";
					$mtxt .= "<p>網站後台：<a href=\"" . parent::getProtocol() . "://" . $_SERVER["HTTP_HOST"] . "/iweb\">" . parent::getProtocol() . "://" . $_SERVER["HTTP_HOST"] . "/iweb</a></p>";
					$mtxt .= "<p>登入帳號：" . $login . "</p>";
					$mtxt .= "<p>驗證碼：" . $_SESSION["dd_ckstr"] . "<br /></p>";
					$mtxt .= "<p>如果您沒有嘗試登錄您的帳戶，您的密碼可能已洩露。請前往後台更改密碼。</p><br />";
					$mtxt .= "<p style=\"color:#f00; font-weight:bolder;\">此信件為系統自動發送!請勿直接回覆!</p>";
					$mtxt .= "</body>";
					$mtxt .= "</html>";
					
					$subject = "[" . registryDAL::getRegistryValue("webName") . " " . $_SERVER['HTTP_HOST'] . "] [網站後台登入驗證通知]";
					
					if ($this->dal->Email !== "") {
						$sends = new sends();
						if (registryDAL::getRegistryValue("sendMailType") === "") {
							$sends->sendemail(registryDAL::getRegistryValue("webName"), $this->dal->Email, $subject, $mtxt, $this->dal->Email);
						}else{
							$toAry = [ $this->dal->Email ];
							$sends->sendsmtpmail($subject, $mtxt, $toAry);
						}

						$this->aryJ["msg"] = "登入驗證碼已發送至您的信箱，但請注意若您的信箱是hotmail、gmail、yahoo…等免費信箱可能會被擋到垃圾郵件區，請記得將該信設定為非垃圾郵件喔!以免影響權益!";
					}else{
						$this->handleError("帳號【" . $login . "】尚未輸入信箱!請聯繫客服員!");
					}
				}
			}else{
				$this->handleError("請輸入帳號!");
			}
		}
		
		private function handleError(string $errorMessage): void {
			$this->aryJ["err"] = "1";
			$this->aryJ["msg"] = $errorMessage;
		}
	}

	$page = new _mylogin();
	$page->load();
?>