<?php
	require_once "pageBase.php";
	require_once "../../bin/system/conver.php";
	require_once "../../dal/autokeywordDAL.php";
	require_once "../../dal/autokeywordlinkDAL.php";
	require_once "../../dal/articleDAL.php";
	require_once "../../dal/newsDAL.php";
	require_once "../../dal/productDAL.php";
	require_once "../../dal/faqDAL.php";

	class _autokeyword extends pageBase{
		public function load(){
			$action = conver::toDbString(trim($_POST["do"]));
			switch(trim(strtolower($action))){
				case "delk":		//刪除關鍵字
					$this->deletekey();
					break;
				case "dell":		//刪除超連結
					$this->deletelink();
					break;
				case "imp":			//變更重要關鍵字
					$this->changeimportant();
					break;
				case "plugin":		//在文章中插入關鍵字
					$this->plugin();
					break;
				default:
					break;
			}
			parent::load();
		}
		private function deletekey(){
			$dal = new autokeywordDAL();
			$id = conver::toDbString(trim($_POST["id"]));
			if($dal->deleteRecord($id))
				echo "1";
			else
				echo "-1";
		}
		private function deletelink(){
			$dal = new autokeywordlinkDAL();
			$id = conver::toDbString(trim($_POST["id"]));
			if($dal->deleteRecord($id))
				echo "1";
			else
				echo "-1";
		}
		private function changeimportant(){
			$dal = new autokeywordDAL();
			$id = conver::toDbString(trim($_POST["id"]));
			if($dal->changeRecord($id))
				echo "1";
			else
				echo "-1";
		}
		private function plugin(){
			$dal = new autokeywordDAL();
			$dalL = new autokeywordlinkDAL();
			$type = conver::toDbString(trim($_POST["typ"]));
			$start = (int)conver::toDbString(trim($_POST["no"]));
			$pd = conver::toDbString(trim($_POST["pd"]));
			$over = "0";
			$re = "/[^A-Za-z0-9\-_=!@#$\?\.\,\^\\%&:;\(\)\{\}\[\]<>\/'\"\s]/";

			if($pd != "7277"){
				$over = "1";
				$return = "確認碼錯誤!";
			}else{
				$data = $dal->executeRecordset("a.`Visible`='1' AND a.`Important`='1'","",0,1);
				if(count($data) == 0){
					$data2 = $dal->executeRecordset("a.`Visible`='1' AND a.`Important`='0'","RAND()",0,3);
					if(count($data2) > 0 && count($data2) < 3){
						for($i=count($data2);$i<3;$i++){
							$data2[] = $data2[0];
						}
					}
				}else{
					$data2 = $dal->executeRecordset("a.`Visible`='1' AND a.`Important`='0'","RAND()",0,2);
					if(count($data2) < 2){
						for($i=count($data2);$i<2;$i++){
							$data2[] = $data[0];
						}
					}
				}
				foreach($data2 as $row){
					$data[] = $row;
				}
				for($i=0;$i<count($data);$i++){
					$dataL = $dalL->executeRecordset("a.`KeywordID`='".$data[$i]["KeywordID"]."' AND a.`Visible`='1'","RAND()",0,1);
					if(count($dataL) == 0){
						$data[$i]["LinkURL"] = "http://".$_SERVER['HTTP_HOST'];
					}else{
						$data[$i]["LinkURL"] = $dataL[0]["LinkURL"];
					}
				}

				if($type == "A"){
					$dalA = new articleDAL();
					$dataA = $dalA->executeRecordset("a.`Visible`='1'","a.`ArticleID`",$start,1);
					if(count($dataA) > 0){
						$description = $dataA[0]["Description"];
						$description = preg_replace("/<a.*>/iU","",$description);
						$description = preg_replace("/<\/a>/iU","",$description);
						$description = preg_replace("/\r/iU","",$description);
						$description = preg_replace("/\n/iU","",$description);
						$description = preg_replace("/\t/iU"," ",$description);
						$aryA = array();
						$aryY = array();
						for($i=0;$i<mb_strlen($description,"utf-8");$i++){
							$word = mb_substr($description,$i,1,"utf-8");
							$aryA[] = $word;
							if(preg_match($re,$word)){
								$aryY[] = $i;
							}
						}

						if(count($aryY) < count($data)){
							foreach($data as $row){
								$description .= "<a href=\"".$row["LinkURL"]."\" target=\"\">".$row["KeywordTitle"]."</a>";
							}
						}else{
							shuffle($aryY);
							for($i=0;$i<count($data);$i++){
								$aryA[$aryY[$i]] .= "<a href=\"".$data[$i]["LinkURL"]."\" target=\"\">".$data[$i]["KeywordTitle"]."</a>";
							}
							$description = "";
							foreach($aryA as $value){
								$description .= $value;
							}
						}

						$dalA->ArticleID = $dataA[0]["ArticleID"];
						$dalA->loadRecord();
						$dalA->Description = $description;
						$dalA->ModifiedUserID = parent::getCurrentUserID();
						$saved = false;
						$saved = $dalA->updateRecord();
						if($saved === false){
							$return = "文章「".$dalA->Title."」插入關鍵字失敗!";
						}else{
							$return = "<p>文章「".$dalA->Title."」</p><textarea style=\"width:95%; margin:0 auto;\">".$description."</textarea>";
						}
						$start = "".((int)$start + 1);
					}else{
						$type = "N";
						$start = "0";
					}
				}else if($type == "N"){
					$dalN = new newsDAL();
					$dataN = $dalN->executeRecordset("a.`Visible`='1'","a.`NewsID`",$start,1);
					if(count($dataN) > 0){
						$description = $dataN[0]["Description"];
						$description = preg_replace("/<a.*>/iU","",$description);
						$description = preg_replace("/<\/a>/iU","",$description);
						$description = preg_replace("/\r/iU","",$description);
						$description = preg_replace("/\n/iU","",$description);
						$description = preg_replace("/\t/iU"," ",$description);
						$aryN = array();
						$aryY = array();
						for($i=0;$i<mb_strlen($description,"utf-8");$i++){
							$word = mb_substr($description,$i,1,"utf-8");
							$aryN[] = $word;
							if(preg_match($re,$word)){
								$aryY[] = $i;
							}
						}

						if(count($aryY) < count($data)){
							foreach($data as $row){
								$description .= "<a href=\"".$row["LinkURL"]."\" target=\"\">".$row["KeywordTitle"]."</a>";
							}
						}else{
							shuffle($aryY);
							for($i=0;$i<count($data);$i++){
								$aryN[$aryY[$i]] .= "<a href=\"".$data[$i]["LinkURL"]."\" target=\"\">".$data[$i]["KeywordTitle"]."</a>";
							}
							$description = "";
							foreach($aryN as $value){
								$description .= $value;
							}
						}

						$dalN->NewsID = $dataN[0]["NewsID"];
						$dalN->loadRecord();
						$dalN->Description = $description;
						$dalN->ModifiedUserID = parent::getCurrentUserID();
						$saved = false;
						$saved = $dalN->updateRecord();
						if($saved === false){
							$return = "最新消息「".$dalN->Title."」插入關鍵字失敗!";
						}else{
							$return = "<p>最新消息「".$dalN->Title."」</p><textarea style=\"width:95%; margin:0 auto;\">".$description."</textarea>";
						}
						$start = "".((int)$start + 1);
					}else{
						$type = "P";
						$start = "0";
					}
				}else if($type == "P"){
					$dalP = new productDAL();
					$dataP = $dalP->executeRecordset("a.`Visible`='1' AND a.`Title1`!=''","a.`ProductID`",$start,1);
					if(count($dataP) > 0){
						$description = $dataP[0]["Description1"];
						$description = preg_replace("/<a.*>/iU","",$description);
						$description = preg_replace("/<\/a>/iU","",$description);
						$description = preg_replace("/\r/iU","",$description);
						$description = preg_replace("/\n/iU","",$description);
						$description = preg_replace("/\t/iU"," ",$description);
						$aryP = array();
						$aryY = array();
						for($i=0;$i<mb_strlen($description,"utf-8");$i++){
							$word = mb_substr($description,$i,1,"utf-8");
							$aryP[] = $word;
							if(preg_match($re,$word)){
								$aryY[] = $i;
							}
						}

						if(count($aryY) < count($data)){
							foreach($data as $row){
								$description .= "<a href=\"".$row["LinkURL"]."\" target=\"\">".$row["KeywordTitle"]."</a>";
							}
						}else{
							shuffle($aryY);
							for($i=0;$i<count($data);$i++){
								$aryP[$aryY[$i]] .= "<a href=\"".$data[$i]["LinkURL"]."\" target=\"\">".$data[$i]["KeywordTitle"]."</a>";
							}
							$description = "";
							foreach($aryP as $value){
								$description .= $value;
							}
						}

						$dalP->ProductID = $dataP[0]["ProductID"];
						$dalP->loadRecord();
						$dalP->Description1 = $description;
						$dalP->ModifiedUserID = parent::getCurrentUserID();
						$saved = false;
						$saved = $dalP->updateRecord();
						if($saved === false){
							$return = "產品「".$dalP->ProductName."」插入關鍵字失敗!";
						}else{
							$return = "<p>產品「".$dalP->ProductName."」</p><textarea style=\"width:95%; margin:0 auto;\">".$description."</textarea>";
						}
						$start = "".((int)$start + 1);
					}else{
						$type = "F";
						$start = "0";
					}
				}else if($type == "F"){
					$dalF = new faqDAL();
					$dataF = $dalF->executeRecordset("a.`Visible`='1'","a.`FaqID`",$start,1);
					if(count($dataF) > 0){
						$description = $dataF[0]["Description"];
						$description = preg_replace("/<a.*>/iU","",$description);
						$description = preg_replace("/<\/a>/iU","",$description);
						$description = preg_replace("/\r/iU","",$description);
						$description = preg_replace("/\n/iU","",$description);
						$description = preg_replace("/\t/iU"," ",$description);
						$aryF = array();
						$aryY = array();
						for($i=0;$i<mb_strlen($description,"utf-8");$i++){
							$word = mb_substr($description,$i,1,"utf-8");
							$aryF[] = $word;
							if(preg_match($re,$word)){
								$aryY[] = $i;
							}
						}

						if(count($aryY) < count($data)){
							foreach($data as $row){
								$description .= "<a href=\"".$row["LinkURL"]."\" target=\"\">".$row["KeywordTitle"]."</a>";
							}
						}else{
							shuffle($aryY);
							for($i=0;$i<count($data);$i++){
								$aryF[$aryY[$i]] .= "<a href=\"".$data[$i]["LinkURL"]."\" target=\"\">".$data[$i]["KeywordTitle"]."</a>";
							}
							$description = "";
							foreach($aryF as $value){
								$description .= $value;
							}
						}

						$dalF->FaqID = $dataF[0]["FaqID"];
						$dalF->loadRecord();
						$dalF->Description = $description;
						$dalF->ModifiedUserID = parent::getCurrentUserID();
						$saved = false;
						$saved = $dalF->updateRecord();
						if($saved === false){
							$return = "問與答「".$dalF->Title."」插入關鍵字失敗!";
						}else{
							$return = "<p>問與答「".$dalF->Title."」</p><textarea style=\"width:95%; margin:0 auto;\">".$description."</textarea>";
						}
						$start = "".((int)$start + 1);
					}else{
						$over = "1";
					}
				}else{
					$over = "1";
					$return = "系統異常!";
				}
			}

			$aryJ = array();
			$aryJ["type"] = $type;
			$aryJ["start"] = $start;
			$aryJ["over"] = $over;
			$aryJ["return"] = urlencode(str_replace('"','\"',$return));
			echo urldecode(json_encode($aryJ));
		}
	}

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