<?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
	$selectcate=$_GET['selectcate'];
	$selectfunctionName=$_GET['selectfunctionName'];
	$fromInMenu=$_GET['fromInMenu'];
	$flag = $_POST['flag'];
	$sort=$_GET['sort'];
	$clickNum = $_POST['clickNum'];
	$headerurl = $_POST['headerurl'];

	$downloadfile=$_POST['downloadfile'];
	$delfile=$_POST['delfile'];
	$ls=$delfile=="删除"?"del":$_POST['ls'];
	$id=$_POST['id'];
	
	$title=clearname( $_POST['newsTitle']);
	$keywords=$_POST['newsKeywords'];
	$description=trim($_POST['newsDescription']);

	$description=$description==""?str_replace("'","''",substr(strip_tags(trim(preg_replace("/([\s]{2,})/"," ", $_POST['newsDescription']))), 0, $descriptionLen)):$description;
	
	$articleTitle=clearname( $_POST['articleTitle']);
	$articleText=editorprocess(str_replace("'","''",$_POST['articleText']),$_SERVER['PHP_SELF'],$articleTitle,$articleTitle); //综合处理

/*	$articleText=strreppattern($_POST['articleText']); //mp4
	 
	$articleText = editorPath('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'], $articleText);//内链
	$articleText = editorPath($_SERVER['PHP_SELF'], $articleText);//图片
	$articleText = addAltTitle($articleTitle, $articleTitle, $articleText);  //图片添加alt,title
*/
	
	$articleBrief= $_POST['articleBrief'] ;
	$articleBrief=editorprocess($articleBrief,$_SERVER['PHP_SELF'],$articleTitle,$articleTitle); //综合处理
	
	$recommend= isset($_POST['recommend']) ? 1 : 0;
	$articleDate=$_POST['articleDate'];
	$functionName=$_POST['functionName'];

	$title=clearname(trim($_POST['newsTitle']));
	$title=$title==""?$articleTitle." - ".$mycompanyName:$title;

	$keywords=trim($_POST['newsKeywords']);
	$keywords=$keywords==""?$articleTitle:$keywords;

	$ChangecatePagePic=$_POST['ChangecatePagePic'];
	$image=$_FILES['downloadName']['name'];   
    $size=$_FILES['downloadName']['size'];   
    $type=$_FILES['downloadName']['type'];

	//$description=trim(strip_tags($_POST['newsTitle']));
	//$description=($description==""?strip_tags($newsTitle):$description);

	$cateName=$_POST['cateName'];

	$Num=$_POST['Num'];

	$grade = $_POST['grade']!=''?$_POST['grade']:4;
	
	$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)){
		$articleText = internal_keyword_link($articleText, $in_rows['title'], $in_rows['url']);
		$articleBrief = internal_keyword_link($articleBrief, $in_rows['title'], $in_rows['url']);
	}

	switch($ls)
	{
		case "insert":
			 if($size < $uploadSize  ) //开始判断图像大小
			{
				if($image!=""){
					$pathBigPic="../".$downloadFolders;
						//echo $pathBigPic."<br>";
						//创建大图文件夹
					createFolder($pathBigPic);
																		
					$arr=array(" ",".");   
					//产生文件名   
					$productPic=str_replace($arr ,"",time()).".".substr($image,-3);
					
					$pathBigPic=$pathBigPic."/".$productPic;
					//echo $pathBigPic."<br>";
					//上传文件
					move_uploaded_file($_FILES['downloadName']['tmp_name'], $pathBigPic);
				}
				
				$select_sql2="select * from ls_download ";
				$select_query2=$opendb->query($select_sql2);
				$select_rows2=mysqli_num_rows($select_query2); //总数
				
				$insert_sql=sprintf("insert into ls_article (title, keywords, description, articleTitle, articleText, articleDate, enable, articleBrief, cateName, functionName, grade, Num,downloadName,downloadSize,flag,headerurl,clickNum) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
			  GetSQLValueString($title,"text"),
			  GetSQLValueString($keywords,"text"),
			  GetSQLValueString($description,"text"),
			  GetSQLValueString($articleTitle,"text"),
			  GetSQLValueString($articleText,"text"),
			  GetSQLValueString(date("Y-m-d"),"date"),
			  GetSQLValueString("1", "int"),
			  GetSQLValueString($articleBrief,"text"),
			  GetSQLValueString($cateName,"text"),
			  GetSQLValueString($functionName,"text"),
			  GetSQLValueString($grade,"int"),
			  GetSQLValueString($Num,"int"),
			  GetSQLValueString($productPic, "text"),
			  GetSQLValueString($size, "int"),
			  GetSQLValueString($flag, "text"),
			  GetSQLValueString($headerurl, "text"),
			  GetSQLValueString($clickNum, "text")
			  
			  );
				//echo $insert_sql."---";exit;
				
				$insert_query=$opendb->query($insert_sql);
				writelog($_SERVER['PHP_SELF'],"add article: ".$articleTitle,"cate: ".$cateName);
				echo "<script>location=\"managearticle.php?selectcate=".$selectcate."&selectfunctionName=".$selectfunctionName."&fromInMenu=".$fromInMenu."&ls_msg=".urlencode($banmsg)."&sort=".$sort."\";</script>";   
			}						
		 else
			{
				echo "	<script>alert('文件大小超过了限制，最大只能上传".($uploadSize/1000000)." M的文件！');history.go(-1);</script>";
			}


			break;
			
		case "update":
			if($ChangecatePagePic=="yes")
			{
				if($size < $uploadSize) //开始判断图像大小
				{
					  $pathBigPic="../".$downloadFolders;


						$arr=array(" ",".");   
						//产生文件名   
						//echo str_replace($arr ,"",time()).".".substr($image,-3) ."===".trim($_POST['Pic']);
						$pic=trim($_POST['Pic'])==""? (str_replace($arr ,"",time()).".".substr($image,-3)):trim($_POST['Pic']);
						$pic=trim(str_replace(substr($image,-4),"",$pic));
						//echo $pic;exit;
						$productPic=str_replace($arr ,"",$pic).".".substr($image,-3);
																				
						$pathBigPic=$pathBigPic."/".$productPic;
						//echo $_FILES['downloadName']['tmp_name']."==". $pathBigPic;exit;
						//上传文件
						move_uploaded_file($_FILES['downloadName']['tmp_name'], $pathBigPic);
							

				}
				else
				{
					echo "	<script>alert('文件大小超过了限制，最大只能上传".($uploadSize/1000000)." M的文件！');history.go(-1);</script>";
				}
			} 
		  
		  $update_sql=sprintf("update ls_article  set title=%s, keywords=%s, description=%s, functionName=%s, articleTitle=%s, articleText=%s, articleBrief=%s, articleDate=%s, enable=%s, cateName=%s, Num=%s, grade=%s, downloadSize=%s, downloadName=%s,flag=%s,clickNum=%s,headerurl=%s  where id=%s",  
			GetSQLValueString($title,"text"),
			GetSQLValueString($keywords,"text"),
			GetSQLValueString($description,"text"),
			GetSQLValueString($functionName,"text"),
			GetSQLValueString($articleTitle,"text"),
			GetSQLValueString($articleText,"text"),
			GetSQLValueString($articleBrief,"text"),
			GetSQLValueString($articleDate,"date"),
			GetSQLValueString("1", "int"),
			GetSQLValueString($cateName,"text"),
			GetSQLValueString($Num, "int"),
			GetSQLValueString($grade, "int"),
			GetSQLValueString($size, "int"),
			GetSQLValueString($productPic, "text"),
			GetSQLValueString($flag,"text"),
			GetSQLValueString($clickNum,"text"),
			GetSQLValueString($headerurl,"text"),
			GetSQLValueString($id,"int")
			);
			//echo $update_sql;
			$update_query=$opendb->query($update_sql);
			//echo $update_sql;exit;

			writelog($_SERVER['PHP_SELF'],"changed article: ".$articleTitle,"cate: ".$cateName);
			echo "<script>location=\"modiarticle.php?selectcate=".$selectcate."&selectfunctionName=".$selectfunctionName."&fromInMenu=".$fromInMenu."&ls_msg=No.".$id." 文章已更新".urlencode($banmsg)."&id=".$id."&sort=".$sort."\";</script>";
			break;
			
			case "del":
 			$update_sql= "update ls_article  set downloadName=''   where id=$id" ;

			$update_query=$opendb->query($update_sql);
			$pathdownloadfile="../".$downloadFolders."/".$downloadfile;
			//echo $pathdownloadfile;exit;
			unlink($pathdownloadfile);
			writelog($_SERVER['PHP_SELF'],"del downloadName: ".$downloadfile,"id: ".$id);
			echo "<script>location=\"modiarticle.php?selectcate=".$selectcate."&selectfunctionName=".$selectfunctionName."&fromInMenu=".$fromInMenu."&ls_msg=No.".$id." 附加文件已删除".urlencode($banmsg)."&id=".$id."&sort=".$sort."\";</script>";			
			break;

	}
	

?>
</body>
</html>