<?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
	$_SESSION['savepcate']=$_POST;
	$ls=$_POST['ls'];
	$id=$_POST['id'];
	$picNameOld=$_POST['PicOld'];
	$ChangecatePagePic=$_POST['ChangecatePagePic'];
	$ChangecateIconPic=$_POST['ChangecateIconPic'];
	$Changeattachedfile=$_POST['Changeattachedfile'];
	//$cateName=replaceCatesName($_POST['cateName']);
	$cateName=clearname($_POST['cateName']);
	 
	
	$cateFolders_str = $_POST['cateFolders'];
	if(preg_match("/[\x7f-\xff]/", $cateFolders_str)){
		$cateFolders = Pinyin($cateFolders_str,1);
	}else{
		$cateFolders = $cateFolders_str;
	}
	if($cateFolders=="")		//如果没有设目录则截取
	{$cateFolders=$cateName;}
	$cateFolders=trim(clearFolder(strtolower(substr_cut($cateFolders,$FolderLength))));

	$cateTitle=clearname($_POST['cateTitle']);
	$cateKeywords=clearname($_POST['cateKeywords']);
	$cateDescription=clearname($_POST['cateDescription']);
	//$catePagePic = $_POST['catePagePic'];
	$catePagePicWidth=$_POST['width'];
	$catePagePicHeight=$_POST['height'];
	
	$cateBrief=clearname( htmlspecialchars($_POST['cateBrief'], ENT_QUOTES));
	$cateBrief=editorprocess($cateBrief,$_SERVER['PHP_SELF'],$cateName,$cateName); //综合处理
	$cateDetail=clearname( htmlspecialchars($_POST['cateDetail'], ENT_QUOTES));
	$cateDetail=editorprocess($cateDetail,$_SERVER['PHP_SELF'],$cateName,$cateName); //综合处理

	//编辑器中的内链与图片转换成相对路径
	$characteristics = strreppattern(clearname($_POST['characteristics']) ); //video
	$characteristics=editorprocess($characteristics,$_SERVER['PHP_SELF'],$cateName,$cateName); //综合处理
	
	$advantages	 = strreppattern(clearname($_POST['advantages']) ); //video
	$advantages=editorprocess($advantages,$_SERVER['PHP_SELF'],$cateName,$cateName); //综合处理
	
	$parameters=  strreppattern(clearname($_POST['parameters']) ); //video
	$parameters=editorprocess($parameters,$_SERVER['PHP_SELF'],$cateName,$cateName); //综合处理

	$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)){
		$characteristics = internal_keyword_link($characteristics, $in_rows['title'], $in_rows['url']);
		$advantages = internal_keyword_link($advantages, $in_rows['title'], $in_rows['url']);
		$parameters = internal_keyword_link($parameters, $in_rows['title'], $in_rows['url']);
	} 
	

	$image=$_FILES['catePagePic']['name'];   
 	$size=$_FILES['catePagePic']['size'];   
 	$type=$_FILES['catePagePic']['type'];
	
	$imageicon=$_FILES['cateIconPic']['name'];   
	
	$image2=$_FILES['productPic']['name'];   
	$size2=$_FILES['productPic']['size'];   
	$type2=$_FILES['productPic']['type'];

	$attachedfile=$_FILES['attachedfile']['name']; 
	$attachedfile=pathProName($attachedfile,"true","false");
	$attachedfilesize=$_FILES['attachedfile']['size'];    
	$attachedfiletype=$_FILES['attachedfile']['type'];

	//echo "图像名称：".$image."<br>";
	//echo "图像大小：".$size."<br>";
	//echo "图像类型：".$type."<br>";
	
	switch ($ls)
	{
		case "insert":  ///添加大类 Start
		   
		//开始判断图像类型
		// 开始判断是否有重复的
		$cateName = str_replace("'","''",$cateName);
		$select_sql="select * from ls_products where cateName='".$cateName."' ";
		$select_query=$opendb->query($select_sql);
		$select_rows=mysqli_num_rows($select_query);
		if ($select_rows == 0)
		{
											
			$path = "../".$cateFolders;
			//调用建立文件夹函数 createFolder
			createFolder($path);
															
			$arr=array(" ",".");   
			//产生文件名   
			$catePagePic=str_replace($arr ,"",$cateFolders."_banner").".".substr($image,-3);
			$BannerPath =$path ."/".$catePagePic;
			//echo $BannerPath;
			//上传文件
			if($_FILES['catePagePic']['tmp_name']!="")
			{
				if (!(strpos($imagetypes,$type)===false)  )
				{
					if($size < $PicSize) //开始判断图像大小
					{
						move_uploaded_file($_FILES['catePagePic']['tmp_name'], $BannerPath);

						//cateIconpic
					}
					else
					{
						echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
					}
				}
				else
				{
					 echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 ".$imagetypess."');history.go(-1);</script>";
				}
			}
			$cateIconPic=str_replace($arr ,"",$cateFolders."_icon").".".substr($imageicon,-3);
			$IconPath =$path ."/".$cateIconPic;
			if($_FILES['cateIconPic']['tmp_name']!="")// 上传了Iconpic
			{
				move_uploaded_file($_FILES['cateIconPic']['tmp_name'], $IconPath);
				list($iconwidth, $iconheight, $icontype, $iconattr)  = getimagesize($IconPath);//获取文件类型

				$toW=$cateIconPicW;			//宽度
				$toH=$cateIconPicH;			//高度
				$toFile=$IconPath;		//目标地址
				$srcFile=$IconPath;	//目标地址
				ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
			}
			
			//上传附加文件
			if($_FILES['attachedfile']['tmp_name']!="")
			{
				move_uploaded_file($_FILES['attachedfile']['tmp_name'],  "../".$cateFolders.$attachedfile);
			}
			$select_sql2="select * from ls_products where cateName<>'' ";
			$select_query2=$opendb->query($select_sql2);
			$select_rows2=mysqli_num_rows($select_query2); //总类数

			$insert_sql=sprintf("insert into ls_products 												(cateName,cateFolders,cateTitle,cateKeywords,cateDescription,catePagePic,catePagePicWidth,catePagePicHeight,cateIconPic,cateBrief,cateDetail,Num,enable,date,attachedfile,attachedfilesize,attachedfiletype,characteristics,advantages,parameters) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ", 
						GetSQLValueString($cateName, "text"),
						GetSQLValueString($cateFolders, "text"),
						GetSQLValueString($cateTitle, "text"),
						GetSQLValueString($cateKeywords, "text"),
						GetSQLValueString($cateDescription, "text"),
						GetSQLValueString($catePagePic, "text"),
						GetSQLValueString($catePagePicWidth, "text"),
						GetSQLValueString($catePagePicHeight, "text"),
						GetSQLValueString($cateIconPic, "text"),
						GetSQLValueString($cateBrief, "text"),
						GetSQLValueString($cateDetail, "text"),
						GetSQLValueString($select_rows2+1, "int"),
						GetSQLValueString("1", "int"),
						GetSQLValueString(date('Y-m-d H:i:s'), "text"),
						GetSQLValueString($attachedfile, "text"),
						GetSQLValueString($attachedfilesize, "text"),
						GetSQLValueString($attachedfiletype, "text"),
						GetSQLValueString($characteristics, "text"),
						GetSQLValueString($advantages, "text"),
						GetSQLValueString($parameters, "text")
			);
															//echo $insert_sql; 
			$insert_query=$opendb->query($insert_sql);
			unset($_SESSION['savepcate']);
			echo "<script>alert('数据添加成功！');location='cate.php';</script>";
		}
		else
		{
			echo "<script>alert('已存在此大类名！\\n请填写别的大类名！');history.go(-1);</script>";	
		}
										
		writelog($_SERVER['PHP_SELF'],"add category: ".$cateName,"ID: ".$id);

		 break;	 ///添加大类 End
	  case "modi":   ///修改大类 Start
	  
	  		if($ChangecatePagePic=="yes")
	  		{
				//判断图像格式
				if(!(strpos($imagetypes,$type)===false) )
				{
					if($size2 < $PicSize) //开始判断图像大小
					{
						$path = "../".$cateFolders;
						$oldbigpic=$path."/".$picNameOld;
						if($picNameOld != null)
						unlink($oldbigpic);		//delete old big pic
						
						$randomnum=NULL;
						for($j=0;$j<=5;$j++)//随机数字的长度，本例随机数长度为6
						{
						srand((double)microtime()*1000000);
						$randomnumber=rand(!$j ? 1: 0,9);//产生随机数，不以0为第一个数，有些特殊的地方0开头被系统省略
						$randomnum.=$randomnumber;
						}
						
						$dest=date("Ymd").time().$randomnum;//设置文件名为时间戳加上随机数避免重复

						$pic = $_FILES['productPic']['name'];
						$pic=substr($pic,(strlen($pic)-4),strlen($pic));
						$picName = $dest.$pic;
						$pathBigPic = $path."/".$picName;
						
						//上传文件
						move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
						
						$toW=$BigPicW;			//宽度
						$toH=$BigPicH;			//高度
						$toFile=$pathBigPic;		//目标地址
						$srcFile=$pathBigPic;	//目标地址
						
						ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
					}
					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_products set catePic=%s where ID=%s",
					GetSQLValueString($picName, "text"),
					GetSQLValueString($id, "int")
					);
				//更新产品图片
					$update_query=$opendb->query($update_pic_sql);
				//echo $update_sql;
				//更新数据
			}
	   	
	  		$select_sql="select * from ls_products where ID=".$id." ";
			$select_query=$opendb->query($select_sql);
			$select_rows=mysqli_fetch_assoc($select_query);
	  						
			//获取文件路径
			$pathNew = "../".$cateFolders;
			$pathOld = "../".$select_rows['cateFolders'];
	  		//echo $pathNew."<br>".$pathOld."<br>";
	  		//文件夹换名
	  		if($pathNew!=$pathOld)
			{
  			  rename($pathOld,$pathNew);
	
	  		}
	  		$update_sql=sprintf("update ls_products set cateName=%s, cateFolders=%s,cateTitle=%s, cateKeywords=%s, cateDescription=%s, catePagePicWidth=%s, catePagePicHeight=%s, cateBrief=%s,cateDetail=%s,characteristics=%s,advantages=%s,parameters=%s where ID=%s",
			GetSQLValueString($cateName, "text"),
			GetSQLValueString($cateFolders, "text"),
			GetSQLValueString($cateTitle, "text"),
			GetSQLValueString($cateKeywords, "text"),
			GetSQLValueString($cateDescription, "text"),
			GetSQLValueString($catePagePicWidth, "text"),
			GetSQLValueString($catePagePicHeight, "text"),
			GetSQLValueString($cateBrief, "text"),
			GetSQLValueString($cateDetail, "text"),
			GetSQLValueString($characteristics, "text"),
			GetSQLValueString($advantages, "text"),
			GetSQLValueString($parameters, "text"),
			GetSQLValueString($id, "int")
			);
			//echo $update_sql;  
			$update_query=$opendb->query($update_sql);
			unset($_SESSION['savepcate']);
			
			if($ChangecateIconPic=="yes")
	  		{
				
				$cateIconPic=str_replace($arr ,"",$cateFolders."_icon").".".substr($imageicon,-3);
				$IconPath =$pathNew ."/".$cateIconPic;
				//echo $IconPath;
				$delIconFile=$pathNew ."/".$select_rows['cateIconPic'];
				@unlink($delIconFile) ; //del the old pic
				move_uploaded_file($_FILES['cateIconPic']['tmp_name'], $IconPath);
				list($iconwidth, $iconheight, $icontype, $iconattr)  = getimagesize($IconPath);//获取文件类型
				$toW=$cateIconPicW;			//宽度
				$toH=$cateIconPicH;			//高度
				$toFile=$IconPath;			//目标地址
				$srcFile=$IconPath;			//目标地址
				ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
				$update_sql=sprintf("update ls_products set cateIconPic=%s where ID=%s",
			GetSQLValueString($cateIconPic, "text"),
			GetSQLValueString($id, "int")
			);
			$update_query=$opendb->query($update_sql);
	  		}
			
			if($Changeattachedfile=="yes") //修改附加文件
	  		{
				$delattachedfile="../".$cateFolders."/".$select_rows['attachedfile'];
				@unlink($delIconFile) ; //del the old pic
				//上传附加文件
				if($_FILES['attachedfile']['tmp_name']!="")
				{
					//echo "../".$cateFolders."/".$attachedfile;
					move_uploaded_file($_FILES['attachedfile']['tmp_name'],  "../".$cateFolders."/".$attachedfile);
				}
				

				$update_sql=sprintf("update ls_products set attachedfile=%s,attachedfiletype=%s,attachedfilesize=%s where ID=%s",
			GetSQLValueString($attachedfile, "text"),
			GetSQLValueString($attachedfiletype, "text"),
			GetSQLValueString($attachedfilesize, "text"),
			GetSQLValueString($id, "int")
			);
			$update_query=$opendb->query($update_sql);
				
	  		}
	  				 	//开始更新数据
			writelog($_SERVER['PHP_SELF'],"modify category: ".$cateName,"ID: ".$id);

			echo "<script>alert('类别已更新！');location='cate.php';</script>";
			
	   
	   break;  ///修改大类 End
	  
	  }
	 
	  ///删除大类  Start
	 if ($_GET['ls']=="del")
	 {
	 		$id=$_GET['id'];
	   		
	   		$select_sql="select * from ls_products where ID=".$id." ";
			$select_query=$opendb->query($select_sql);
			$select_rows=mysqli_fetch_assoc($select_query);
	   		
	   		//echo $select_rows['cateFolders']."<br>".$select_rows['catePagePic']."<br>";
	   		//获得图片路径
	   		$pathFoldersPic="../".$select_rows['cateFolders']."/".$select_rows['catePagePic'];
	   		//获得文件路径
	   		$pathFolders="../".$select_rows['cateFolders'];
	   		//echo $pathFolders.$pathFoldersPic."<br>";
	   		
	   		$pathFoldersIcon="../".$select_rows['cateFolders']."/".$select_rows['cateIconPic'];
	   		//删除图片
	   		@unlink($pathFoldersPic);
	   		//删除Icon
	   		@unlink($pathFoldersIcon);
	   		//删除文件夹
			 @unlink($pathFolders."/indexdetails.html");
			@unlink($pathFolders."/index.html");
	   		@rmdir($pathFolders);

	   		$del_sql="delete from ls_products where ID=".$id."";
	   		//echo $del_sql;
	   		$del_query=$opendb->query($del_sql);
			$num_sql2="update ls_products set Num=Num-1 where Num>".$select_rows['Num']." ";//all order number-1
			$down_query=$opendb->query($num_sql2);
			writelog($_SERVER['PHP_SELF'],"delete category: ".$cateName,"ID: ".$id);

	   		echo "<script>alert('类别已删除！');location='cate.php';</script>";
	 }
	 ///删除大类  End
?>
</body>
</html>
