<?php
	require_once "pageBase.php";
	require_once "../bin/system/conver.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/htaccessDAL.php";
	require_once "../function/htaccess.php";

	class _htaccessSet extends pageBase {
		public string $filename;
		private $dal;
		public function load() {
			$this->filename = "../.htaccess";
			$this->dal = new htaccessDAL();

			$this->limitID = "59";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/htaccessSet.html");
			parent::load();
		}

		public function loadData() {
			$data = $this->dal->executeRecordset();
			$num = 0;
			foreach ($data as &$item) {
				$item["NO"] = $num; // 添加 NO 屬性，不必計算索引
				$item["checked"] = ($item["Visible"] === 1) ? "checked" : ""; // 使用三元運算符設置 checked 屬性
				$num++;
			}
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);
		
			parent::loadData();
		}
		
		public function saveData() {
			$webLinkType = parent::getPost("webLinkType");
			$webLinkType = (preg_match("/^[1-3]$/", $webLinkType)) ? $webLinkType : "1"; 
			
			$str = '';
			$id = parent::getPostArray("ID");
			$htmlName = parent::getPostArray("HtmlName");
			$visible = parent::getPostArray("Visible");
			
			if (count($id) === count($htmlName) && count($id) === $this->dal->countRecordset()) {
				for ($i = 0; $i < count($id); $i++) {
					$hName = $htmlName[$i];

					if (preg_match("/^(ajax|iweb|signature|userfiles|css|fonts?|images|jquery|js|scripts|cate|page)$/i", $hName) || !preg_match("/^[A-Za-z]+$/", $hName)) {
						$this->alertMsg .= '「' . $hName . '」是不合法的設定值，變更失敗，請重新設定合法的值!\n';
					} else {
						$intoAry = [
							parent::addArray(":id", $id[$i], "int"),
							parent::addArray(":htmlName", $hName, "str", 128),
						];

						$count = $this->dal->countRecordset($intoAry, "a.`ID`!=:id AND a.`HtmlName`=:htmlName");
						$visible[$i] = isset($visible[$i]) ? ($visible[$i] != 1 && $visible[$i] != 0 ? 0 : 1) : 0;
						if ($count == 0) {
							$this->dal->ID = $id[$i];
							$this->dal->loadRecord();
							$this->dal->HtmlName = $hName;
							$this->dal->Visible = $visible[$i];
							$this->dal->updateRecord();
						} else {
							$this->alertMsg .= '「' . $hName . '」設定值重複，變更失敗，請重新設定其它的值!\n';
						}
					}
				}
				
				if (file_exists($this->filename)) {
					$this->changeHtaccess();
				} else {
					$this->alertMsg = "網頁網址設定原始資料異常!請和管理者聯絡!!!";
					$this->locationHref = "htaccessSet.php";
				}
			} else {
				$this->alertMsg = "資料異常!請重新整理再試一次!";
				$this->locationHref = "htaccessSet.php";
			}
		}

		public function changeHtaccess() {
			$dal = new htaccessDAL();
			$webLinkType = parent::getRegistryValue("webLinkType");
			$txt = '';
			$flag = 0;
			$txtF = file_get_contents($this->filename);
			$aryF = explode(PHP_EOL, $txtF);
			
			if (empty($txtF) || count($aryF) < 2) {
				$this->alertMsg = "網頁網址設定原始資料異常!請和管理者聯絡!!";
				$this->locationHref = "htaccessSet.php";
			} else {
				foreach ($aryF as $v) {
					if ($flag == 0) {
						$txt .= $v . PHP_EOL;
						if (preg_match("/^#\s*WebSite Page Start/i", $v)) {
							$flag = 1;
							$data = $dal->executeRecordset([], "a.`Visible`=1");
							foreach ($data as $rows) {
								$txt .= '#' . $rows["ActionName"] . ' start /' . $rows["HtmlName"] . '/' . PHP_EOL;
								switch ($webLinkType) {
									case "2":
										$txt .= setPageLine2($rows["ActionName"], $rows["HtmlName"]);
										break;
									case "3":
										$txt .= setPageLine3($rows["ActionName"], $rows["HtmlName"]);
										break;
									default:
										$txt .= setPageLine1($rows["ActionName"], $rows["HtmlName"]);
								}
								$txt .= '#' . $rows["ActionName"] . ' end' . PHP_EOL;
							}
						}
					} else if ($flag == 1) {
						if (preg_match("/^#\s*WebSite Page End/i", $v)) {
							$flag = 2;
							$txt .= $v . PHP_EOL;
						}
					} else {
						$txt .= $v . PHP_EOL;
					}
				}
				if ($flag == 2) {
					file_put_contents($this->filename, $txt);
					$this->alertMsg = "儲存完畢!";
					$this->locationHref = "htaccessSet.php";
				} else {
					$this->alertMsg = "網頁網址設定原始資料異常!請和管理者聯絡!";
					$this->locationHref = "htaccessSet.php";
				}
			}
		}
	}

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