<?php
	require_once('_approot.php');	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<link href="css/admin.css" rel="stylesheet" type="text/css">
		<title>无标题文档</title>
	</head>

	<body>
<?php

	$ls=$_POST['ls'];
	$id=$_POST['id'];
	$pageTitle=$_POST['pageTitle'];
//	$pageText=$_POST['pageText'];
	$banner=str_replace("_banner.jpg","",trim($_POST['banner']));
	$pageText=editorprocess($_POST['pageText'],$_SERVER['PHP_SELF'],$matealt==""?$pageTitle:$matealt,$matetitle==""?$pageTitle:$matetitle); 
/*	
	//编辑器中的内链与图片转换成相对路径
	$pageText = editorPath('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'], $pageText);//内链
	$pageText = editorPath($_SERVER['PHP_SELF'], $pageText);//图片
	$pageText = addAltTitle($pageTitle, $pageTitle, $pageText);  //图片添加alt,title
*/	
	$in_sql="SELECT * FROM ls_internal where enable=1 order by Num asc"; //count the error by searchtype
	$in_query=$opendb->query($in_sql);
	while ($in_rows=mysqli_fetch_assoc($in_query)){
		$pageText = internal_keyword_link($pageText, $in_rows['title'], $in_rows['url']);
	}
	
	$pageBrief=$_POST['pageBrief'];
	$pageDate=$_POST['pageDate'];
	$tpl=(trim($_POST['tpl'])==""?"single_page.html":trim($_POST['tpl']));
	$name=trim($_POST['name']);
	$name=($name==""?"single_page.html":$name);
	$cate="singlePages";
	$Num=$_POST['Num'];
	
	$title = $_POST['newsTitle'];
	$keywords = $_POST['newsKeywords'];
	$description = $_POST['newsDescription'];

	switch($ls)
	{
		case "insert":
		
			$insert_sql=sprintf("insert into ls_singlepages (newsTitle, newsKeywords, newsDescription, pageTitle,static_tpl,singlepage_name,pageText,pageDate,enable,pageBrief,cate,banner,Num) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
																																																 						GetSQLValueString($title, "text"),
						GetSQLValueString($keywords, "text"),
						GetSQLValueString($description, "text"),
						GetSQLValueString($pageTitle,"text"),
						GetSQLValueString($tpl,"text"),
						GetSQLValueString($name,"text"),
						GetSQLValueString($pageText,"text"),
						GetSQLValueString(date("Y-m-d"),"date"),
						GetSQLValueString("1", "int"),
						GetSQLValueString($pageBrief,"text"),
						GetSQLValueString($cate,"text"),
						GetSQLValueString($banner,"text"),
						GetSQLValueString($Num,"int")
						);
			$insert_query=$opendb->query($insert_sql);
			if(!file_exists("../Templates1/".$tpl)) 
				if(!copy("../Templates1/singlepage.html","../Templates1/".$tpl)) echo  "<script>alert('复制模板错误！');location='singlepage.php';</script>";  //复制模板
				else echo "<script>alert('模板已复制，添加成功！');location='singlepage.php';</script>";
			else echo "<script>alert('模板存在，添加成功！');location='singlepage.php';</script>";
			break;
			
		case "update":
			$in_sql="SELECT * from ls_singlepages where ID=".$id.""; //count the error by searchtype
			$in_query=$opendb->query($in_sql);
			$in_rows=mysqli_fetch_assoc($in_query);
			$oldtpl="../Templates1/".$in_rows['static_tpl'];

			$update_sql=sprintf("update ls_singlepages  set  newsTitle=%s, newsKeywords=%s, newsDescription=%s, pageTitle=%s,static_tpl=%s,singlepage_name=%s,pageText=%s,pageBrief=%s,enable=%s,cate=%s,banner=%s,Num=%s where id=%s", 
						GetSQLValueString($title, "text"),
						GetSQLValueString($keywords, "text"),
						GetSQLValueString($description, "text"),
						GetSQLValueString($pageTitle,"text"),
						GetSQLValueString($tpl,"text"),
						GetSQLValueString($name,"text"),
						GetSQLValueString($pageText,"text"),
						GetSQLValueString($pageBrief,"text"),
						
						GetSQLValueString("1", "int"),
						GetSQLValueString($cate,"text"),
						GetSQLValueString($banner,"text"),
						GetSQLValueString($Num, "int"),
						GetSQLValueString($id,"int")
						); // pageDate=%s, GetSQLValueString($pageDate,"date"),
									//echo $update_sql;
			$update_query=$opendb->query($update_sql);
			
			if(!file_exists("../Templates1/".$tpl)) 
				if(file_exists($oldtpl)) 
					if(!rename($oldtpl,"../Templates1/".$tpl)) echo  "<script>alert('模板改名错误，单页已更新。');location='singlepage.php';</script>";  //复制模板
					else
						if(!copy("../Templates1/singlepage.html","../Templates1/".$tpl)) echo  "<script>alert('模板复制错误，单页已更新。');location='singlepage.php';</script>";  //复制模板
						else 	
 						echo "<script>alert('模板已复制，更新成功！');location='singlepage.php';</script>";
				else if(!copy("../Templates1/singlepage.html","../Templates1/".$tpl)) echo  "<script>alert('模板复制错误，单页已更新。');location='singlepage.php';</script>";  //复制模板
						else 	
 						echo "<script>alert('模板已复制，更新成功！');location='singlepage.php';</script>";
			
			else echo "<script>alert('模板已存在，更新成功！');location='singlepage.php';</script>";
	}
?>
</body>
</html>