<?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 _htaccessBase extends pageBase {
		public function load() {
			$filename = "../.htaccess";
			$webLinkType = parent::getRegistryValue("webLinkType");
			$dal = new htaccessDAL();

			$this->limitID = "59";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				if (file_exists($filename)) {
					$this->alertMsg = "htaccess檔已經存在!無法產生新檔!";
					$this->locationHref = "index.php";
				} else {
					$data = $dal->executeRecordset([], "a.`Visible`=1");

					$txt = 'RewriteEngine On' . PHP_EOL;
					$txt .= '#https設定';
					$txt .= 'RewriteCond %{HTTPS} off' . PHP_EOL;
					$txt .= 'RewriteRule ^(.*)$											https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]' . PHP_EOL . PHP_EOL;
					$txt .= '#www設定';
					$txt .= 'RewriteCond %{HTTP_HOST} ^(.*)$' . PHP_EOL;
					$txt .= 'RewriteCond %{HTTP_HOST} !^www\.(.*) [NC]' . PHP_EOL;
					$txt .= 'RewriteRule (.*)											http://www\.%1/$1 [R=301,L]' . PHP_EOL . PHP_EOL;
					$txt .= 'RewriteRule ^index\.(htm|html|asp|aspx|jsp)				http://%1/ [NC,R=301,L]' . PHP_EOL . PHP_EOL;
					$txt .= 'RewriteRule ^css(.*)$										/templates/css$1 [NC]' . PHP_EOL;
					$txt .= 'RewriteRule ^font(.*)$										/templates/font$1 [NC]' . PHP_EOL;
					$txt .= 'RewriteRule ^images(.*)$									/templates/images$1 [NC]' . PHP_EOL;
					$txt .= 'RewriteRule ^jquery(.*)$									/templates/jquery$1 [NC]' . PHP_EOL;
					$txt .= 'RewriteRule ^js(.*)$										/templates/js$1 [NC]' . PHP_EOL;
					$txt .= 'RewriteRule ^scripts(.*)$									/templates/scripts$1 [NC]' . PHP_EOL;
					$txt .= 'RewriteRule ^svg(.*)$										/templates/svg [NC]' . PHP_EOL;
					$txt .= 'RewriteRule ^less(.*)$										/templates/less [NC]' . PHP_EOL;
					$txt .= 'RewriteBase /' . PHP_EOL . PHP_EOL;
					$txt .= '#禁止讀取目錄中檔案列表' . PHP_EOL;
					$txt .= 'Options -Indexes' . PHP_EOL . PHP_EOL;
					$txt .= '#404頁面' . PHP_EOL;
					$txt .= 'ErrorDocument 404											/404.php' . PHP_EOL . PHP_EOL;
					$txt .= '#配置文件' . PHP_EOL;
					$txt .= 'RewriteRule config\.php									404.php' . PHP_EOL . PHP_EOL;
					$txt .= 'RewriteRule log\.txt										404.php' . PHP_EOL . PHP_EOL;
					$txt .= '#demo頁' . PHP_EOL;
					$txt .= 'RewriteRule ^demo\.html$									demo.php' . PHP_EOL;
					$txt .= 'RewriteRule ^demo-(\d+)\.html$								demo.php?id=$1' . PHP_EOL . PHP_EOL;
					$txt .= '#XML' . PHP_EOL;
					$txt .= 'RewriteRule ^sitemap\.xml									sitemap.php' . PHP_EOL;
					$txt .= 'RewriteRule ^rss\.xml										rss.php' . PHP_EOL . PHP_EOL;
					$txt .= '#網站地圖' . PHP_EOL;
					$txt .= 'RewriteRule ^sitemap\.html									webmap.php' . PHP_EOL . PHP_EOL;
					$txt .= '#後台' . PHP_EOL;
					$txt .= 'RewriteRule ^iweb\/$										/iweb/index.php' . PHP_EOL . PHP_EOL;
					$txt .= '#受保護的目錄' . PHP_EOL;
					$txt .= 'RewriteRule ^templates/webParts/							404.php' . PHP_EOL;
					$txt .= 'RewriteRule ^templates/([0-9]+)/							404.php' . PHP_EOL;
					$txt .= 'RewriteRule ^iweb/templates/								404.php' . PHP_EOL . PHP_EOL;
					$txt .= '#WebSite Page Start' . PHP_EOL;
					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;
					}
					$txt .= '#WebSite Page End' . PHP_EOL . PHP_EOL;
					$txt .= '#IP訪問限制';
					file_put_contents($filename, $txt);
					$this->alertMsg = "htaccess已經產生了!";
					$this->locationHref = "htaccessSet.php";
				}
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/htaccessBase.html");
			parent::load();
		}
	}

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