<?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'];
	$selectcate=$_GET['selectcate'];
	$page=$_GET['page'];
	$newsTitle=clearname($_POST['newsTitle2']);
	$newsText=clearname($_POST['newsText']);
	$newsText=editorprocess($newsText,$_SERVER['PHP_SELF'],$newsTitle,$newsTitle); 

	$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)){
		$newsText = internal_keyword_link($newsText, $in_rows['title'], $in_rows['url']);
	}
	
	$cate=$_POST['cate'];
	$Num=$_POST['Num'];
	$newsDate=$_POST['newsDate'];
	$title =clearname( $_POST['newsTitle']);
	$keywords = clearname($_POST['newsKeywords']);
	$description=clearname( trim($_POST['newsDescription']));
	$description =clearname( $description==""? trim(substr(strip_tags(trim(preg_replace("/([\s]{2,})/"," ",$newsText)))), 0, 155):$description);
	$ChangecatePagePic=$_POST['ChangecatePagePic'];
	$keywords = str_replace("，",",",$keywords);
	
	$pic=$_POST['Pic'];
	
	$image=$_FILES['blogPic']['name'];   
  	$size=$_FILES['blogPic']['size'];   
  	$type=$_FILES['blogPic']['type'];
	
	switch($ls){
		case "insert":
		  if($image != ""){
			if (!(strpos($imagetypes,$type)===false)  ){
				 if($size < $PicSize){
					 $blogPath="../blog/".$SmallFoldersProducts;
					 createFolder($blogPath);
					 
					 $arr=array(" ",".");
					 
					 //找出产品表中最大的ID，加1 
					$maxsql = "select * from ls_news as a where ID = (select max(ID) from ls_news as b)";
					$max_query = $opendb->query($maxsql);
					$maxrow = mysqli_fetch_assoc($max_query);
					$pid = $maxrow['ID']+1;
					
					$pro_name = time().$pid;

					//产生文件名   
					$productPic=str_replace($arr ,"",$pro_name).".".get_extension($image);
					$pathPic=$blogPath."/".$productPic;
					//echo  $pathBigPic."--".$_FILES['productPic']['tmp_name'];
					move_uploaded_file($_FILES['blogPic']['tmp_name'], $pathPic);
					echo $pathPic;
					$toW=$SmallPicW;			//宽度
					$toH=$SmallPicH;			//高度
					$toFile=$pathPic;	//目标地址
					$srcFile=$pathPic;	//目标地址
					ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
					chmod($toFile,0644);
					
					$insert_sql=sprintf("insert into ls_news(title,keywords,description,newsTitle,newsText,img,newsDate,enable,cate,Num) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
						GetSQLValueString($title,"text"),
						GetSQLValueString($keywords,"text"),
						GetSQLValueString($description,"text"),
						GetSQLValueString($newsTitle,"text"),
						GetSQLValueString($newsText,"text"),
						GetSQLValueString($productPic,"text"),
						GetSQLValueString($newsDate,"date"),
						GetSQLValueString("1", "int"),
						GetSQLValueString($cate,"text"),
						GetSQLValueString($Num,"int")
						);
					$insert_query=$opendb->query($insert_sql);
					writelog($_SERVER['PHP_SELF'],"add blog: ".$newsTitle,"cate: ".$cate);
					echo "<script>alert('数据添加成功！');location='blog.php?selectcate=".$selectcate."';</script>";
				 }else
					{
						echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
					}
			}else
			{
					 echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式".$imagetypess."');history.go(-1);</script>";
			}
		  }else{
			  $insert_sql=sprintf("insert into ls_news(title,keywords,description,newsTitle,newsText,newsDate,enable,cate,Num) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)",
					GetSQLValueString($title,"text"),
					GetSQLValueString($keywords,"text"),
					GetSQLValueString($description,"text"),
					GetSQLValueString($newsTitle,"text"),
					GetSQLValueString($newsText,"text"),
					GetSQLValueString($newsDate,"date"),
					GetSQLValueString("1", "int"),
					GetSQLValueString($cate,"text"),
					GetSQLValueString($Num,"int")
					);
				$insert_query=$opendb->query($insert_sql);
				writelog($_SERVER['PHP_SELF'],"add blog: ".$newsTitle,"cate: ".$cate);
				echo "<script>alert('数据添加成功！');location='blog.php?selectcate=".$selectcate."';</script>";
		  }
		break;
			
		case "update":
		if($ChangecatePagePic=="yes"){
			if (!(strpos($imagetypes,$type)===false)  ){
				if($size < $PicSize) //开始判断图像大小
				{
					$blogPath="../blog/".$SmallFoldersProducts;
					
					$oldsmallpic=$blogPath."/".$pic;
					unlink($oldsmallpic);	//delete old small pic
					
					$arr=array(" ",".");   
					//产生文件名   
					$pic=substr($pic,0,(strlen($pic)-4));
					//echo $pic;
					$productPic=$pic.".".substr($image,-3);
																									
					$pathPic=$blogPath."/".$productPic;
					//echo $pathBigPic;
					//上传文件
					move_uploaded_file($_FILES['blogPic']['tmp_name'], $pathPic);
					$toW=$SmallPicW;			//宽度
					$toH=$SmallPicH;			//高度
					$toFile=$pathPic;		//目标地址
					$srcFile=$pathPic;	//目标地址
					ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
					chmod($toFile,0644);
				}else
				{
					echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
				}
			}else
			{
				echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 ".$imagetypess."');history.go(-1);</script>";
			}
			$update_pic_sql=sprintf("update ls_news set img=%s where id=%s",
					GetSQLValueString($productPic, "text"),
					GetSQLValueString($id, "int")
				);
			//更新图片
			$update_query=$opendb->query($update_pic_sql);
		}
	$update_sql=sprintf("update ls_news set title=%s, keywords=%s, description=%s, newsTitle=%s,newsText=%s,newsDate=%s,enable=%s,cate=%s,Num=%s where id=%s",
						GetSQLValueString($title, "text"),
						GetSQLValueString($keywords, "text"),
						GetSQLValueString($description, "text"),
						GetSQLValueString($newsTitle,"text"),
						GetSQLValueString($newsText,"text"),
						GetSQLValueString($newsDate,"date"),
						GetSQLValueString("1", "int"),
						GetSQLValueString($cate,"text"),
						GetSQLValueString($Num, "int"),
						GetSQLValueString($id,"int")
						);
									//echo $update_sql;
			$update_query=$opendb->query($update_sql);
			writelog($_SERVER['PHP_SELF'],"changed news: ".$newsTitle,"cate: ".$cate);

			echo "<script>alert('数据更新成功！');location='blog.php?selectcate=".$selectcate."&page=".$page."';</script>";
	}
?>
</body>
</html>