<?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 _seoSet extends pageBase {
		public string $filename;
		public function load() {
			$this->filename = "../.htaccess";

			$this->limitID = "57";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/seoSet.html");
			parent::load();
		}
		
		public function loadData() {
			$webConfig = registryDAL::getRegistryValues();
			foreach ($webConfig as $key => &$value) {
				$value = $value ?? "";
				parent::addParameter($key, $value);
			}
			parent::loadData();
		}
		
		public function saveData() {
			$webLinkType = parent::getPost("webLinkType");
			$aryR = [
				"webTitle" => parent::getPost("webTitle"),
				"webKeyword" => parent::getPost("webKeyword"),
				"webDescription" => parent::getPost("webDescription"),
				"webH1" => parent::getPost("webH1"),
				"webCopy" => parent::getPost("webCopy"),
				"webMarquee" => parent::getPost("webMarquee", "", 2),
				"webLinkType" => parent::getPost("webLinkType"),
				"webFooter" => parent::getPost("webFooter", "", 2),
				"twipLimit" => parent::getPost("twipLimit", "0"),
				"sitemapType" => parent::getPost("sitemapType", "0"),
				"ModifiedTime" => time(),
			];
			
			foreach ($aryR as $key => $value) {
				registryDAL::saveRegistry($key, $value);
			}

			if (file_exists($this->filename)) {
				$this->changeHtaccess($webLinkType);
			} else {
				$this->alertMsg = "網頁網址設定原始資料異常!請和管理者聯絡!!!";
				$this->locationHref = "seoSet.php";
			}
		}

		public function changeHtaccess($webLinkType) {
			$dal = new htaccessDAL();
			$txt = '';
			$flag = 0;
			$txtF = file_get_contents($this->filename);
			$aryF = explode(PHP_EOL, $txtF);
			
			if (empty($txtF) || count($aryF) < 2) {
				$this->alertMsg = "網頁網址設定原始資料異常!請和管理者聯絡!!";
				$this->locationHref = "seoSet.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 = "seoSet.php";
				} else {
					$this->alertMsg = "網頁網址設定原始資料異常!請和管理者聯絡!";
					$this->locationHref = "seoSet.php";
				}
			}
		}
	}

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