<?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">
<?php
	$ls_msg="自动添加产品操作";
	
	$filepath=$_POST['filepath'];
	$ls=$_POST['ls'];
	
	function getSubstr($str, $leftStr, $rightStr)
	{
		//echo $leftStr."<br />";
	 $left = strpos($str, $leftStr);
	 //echo '左边:'.$left;
	 if($rightStr != ""){
		$right = strpos($str, $rightStr,$left + strlen($leftStr));
		$r_postion=$right-$left-strlen($leftStr);
	 }else{
		$r_postion=strlen($str)-strlen($leftStr);
	 }
	 //echo '<br>右边:'.$right;
	 //if($left < 0 or $right < $left) return '';
	 return substr($str, $left + strlen($leftStr), $r_postion);
	}
	
	function getCateName($allcate){
		$numcate = substr($allcate, 0, 2);
		if(is_numeric($numcate)){
			$allcate = str_replace($numcate,"",$allcate);
		}else{
			$allcate = str_replace(substr($allcate, 0, 1),"",$allcate);
		}
		$allcate =  str_replace(";",",",$allcate);
		$allcate =  str_replace("；",",",$allcate);
		$allcate =  str_replace("，",",",$allcate);
		$allcate =  str_replace("。",",",$allcate);

		$allcate =  str_replace("，",",",$allcate);
		$allcate =  str_replace(" , ",",",$allcate);
		$allcate =  str_replace(" ,",",",$allcate);
		$allcate =  str_replace(" ,",",",$allcate);
		$allcate =  str_replace(", ",",",$allcate);
		$allcate =  str_replace(", ",",",$allcate);
		$allcate =  str_replace(",","",$allcate);

		return $allcate;
	}

	function get_filetree($filepath){
		
		$path = "../".$filepath;
		
		$catename1 = array();
		$catename2 = array();
		
		$catemidnameArray1 = array();
		$catemidnameArray2 = array();
		
		$tree = array();
		$tree2 = array();
		$tree3 = array();
	
		foreach(glob($path.'/*') as $single){
			if(is_dir($single)){
				//$tree = array_merge($tree,get_filetree($single));
				$tree[] = $single."<br />";
				
				$allcate = getSubstr($single, $filepath."/", "");
				$numcate = substr($allcate, 0, 2);
				if(is_numeric($numcate)){
					$catename2[] = str_replace($numcate,"",$allcate);  //大类两位编号
				}else{
					$catename1[] = str_replace(substr($allcate, 0, 1),"",$allcate);  //大类一位编号
				}
	
				foreach(glob($single.'/*') as $single2){
					if(is_dir($single2)){
						$tree2[] = $single2."<br />";
						
						$allcate = getSubstr($single2, $filepath."/", "/");
						$allmidcate = getSubstr($single2, $filepath."/".$allcate."/", "");
						
						$numcate = substr($allmidcate, 0, 2);
						if(is_numeric($numcate)){  //中类两位编号
							$catemidnameArray2[]['catename'] = getCateName($allcate);
							$catemidnameArray2[]['catemidname'] = getCateName($allmidcate);
						}else{    //中类一位编号
							$catemidnameArray1[]['catename'] = getCateName($allcate);
							$catemidnameArray1[]['catemidname'] = getCateName($allmidcate);
						}
						
						foreach(glob($single2.'/*') as $single3){
							if(is_dir($single3)){
								$tree3[] = $single3."<br />";
							}
						}
					}
				}
			}
			else{
				$tree[] = $single;
			}
		}
		//添加大类  开始
		foreach ($catename1 as $cate1){
			$cateName=$cate1;
			$cateName=str_replace("'","`",$cateName);
			$cateFolders=$cate1;
			$cateFolders=clearFolder(strtolower($cate1));
			$cateTitle=$cate1;
			$cateKeywords=$cate1;
			$cateDescription=$cate1;
			//$catePagePic = $_POST['catePagePic'];
			$catePagePicWidth=$_POST['width'];
			$catePagePicHeight=$_POST['height'];
			
			$cateBrief=str_replace("\n","<br />",htmlspecialchars($_POST['cateBrief'], ENT_QUOTES));
			$cateDetail=$_POST['cateDetail'];
			
			$imageicon=$_FILES['cateIconPic']['name'];
			$ls = "insert";
			
			switch ($ls)
			{
				case "insert":  ///添加大类 Start
				
				//开始判断图像类型
				// 开始判断是否有重复的
				$cateName = str_replace("'","''",$cateName);
				$select_sql="select * from ls_products where cateName='".$cateName."' ";
				$opendb=new mydb();
				$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;
					//上传文件
					$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) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
								GetSQLValueString(ucwords($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")
					);
					//echo $insert_sql;
					$insert_query=$opendb->query($insert_sql);
					echo "添加大类".ucwords($cateName)."成功。<br />";
												
				}
				else
				{
					echo "<script>alert('已存在此大类名！\\n请填写别的大类名！');history.go(-1);</script>";	
				}
												
				writelog($_SERVER['PHP_SELF'],"add category: ".$cateName,"ID: ".$id);
		
				 break;	 ///添加大类 End
			  
			  }
		}
		foreach ($catename2 as $cate1){
			$cateName=$cate1;
			$cateName=str_replace("'","`",$cateName);
			$cateFolders=$cate1;
			$cateFolders=clearFolder(strtolower($cate1));
			$cateTitle=$cate1;
			$cateKeywords=$cate1;
			$cateDescription=$cate1;
			//$catePagePic = $_POST['catePagePic'];
			$catePagePicWidth=$_POST['width'];
			$catePagePicHeight=$_POST['height'];
			
			$cateBrief=str_replace("\n","<br />",htmlspecialchars($_POST['cateBrief'], ENT_QUOTES));
			$cateDetail=$_POST['cateDetail'];
			
			$imageicon=$_FILES['cateIconPic']['name'];
			$ls = "insert";
			
			switch ($ls)
			{
				case "insert":  ///添加大类 Start
				
				//开始判断图像类型
				// 开始判断是否有重复的
				$cateName = str_replace("'","''",$cateName);
				$select_sql="select * from ls_products where cateName='".$cateName."' ";
				$opendb=new mydb();
				$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;
					
					$cateIconPic=str_replace($arr ,"",$cateFolders."_icon").".".substr($imageicon,-3);
					$IconPath =$path ."/".$cateIconPic;
	
					$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) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
								GetSQLValueString(ucwords($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")
					);
					//echo $insert_sql;
					$insert_query=$opendb->query($insert_sql);
					echo "添加大类".ucwords($cateName)."成功。<br />";
												
				}
				else
				{
					echo "<script>alert('已存在此大类名！\\n请填写别的大类名！');history.go(-1);</script>";	
				}
												
				writelog($_SERVER['PHP_SELF'],"add category: ".$cateName,"ID: ".$id);
		
				 break;	 ///添加大类 End
			  
			  }
		}
		//添加大类  结束
	
		//添加中类  开始
		$i = 0;
		foreach($catemidnameArray1 as $k=>$val){
			$i++;
			if($i%2 != 0){
				$name = $val['catename'];
			}
			
			if($i%2 == 0){
				$catemidname2 = $val['catemidname'];
				$picNameOld=$_POST['PicOld'];
				$ls=$_POST['ls'];
				//$midcateName=replaceCatesName($_POST['midcateName']);
				$midcateName=trim($val['catemidname']);
				$ChangecatePagePic=$_POST['ChangecatePagePic'];
				
				//获得大类名称，用来判读此类下是否有同名的中类
				$cateName = trim($val['catemidname']);
				$cateTitle=$val['catemidname'];
				$cateKeywords=$val['catemidname'];
				$cateDescription=$val['catemidname'];
			
			
				$midcateBrief=str_replace("\n","<br />",htmlspecialchars($_POST['midcateBrief'], ENT_QUOTES));
				$midcateDetail=$_POST['midcateDetail'];
			
				$midcateFolders=$val['catemidname'];
				if($midcateFolders=="")				//如果没有设目录则截取
				{$midcateFolders=$midcateName;}
				$midcateFolders=trim(clearFolder(strtolower($midcateFolders)));
				
				$title_sql = "select * from ls_products where cateName='".$name."'";
				$opendb = new mydb();
				$title_query = $opendb->query($title_sql);
				$title_value = mysqli_fetch_assoc($title_query);
				
				$midcateID=$title_value['ID'];
				$cate_id=$title_value['ID'];
				$cateName=trim($val['catename']);
				
				if($midcateID!="")
				{
					$cate_sql="select * from ls_products where ID='".$cate_id."' ";
					$cate_query=$opendb->query($cate_sql);
					$cate_rows=mysqli_fetch_assoc($cate_query);
					$cateFolders=$cate_rows['cateFolders'];
				}
				
				$path = "../".$cateFolders."/".$midcateFolders;
				$ls = "insert";
				
				switch ($ls)
				{
					case "insert":
									
						$midcateName = str_replace("'","''",$midcateName);
						// 开始判断是否有重复的
						$select_sql="select * from ls_products where midcateName='".$midcateName."' and cateName='".$cateName."'";
						$select_query=$opendb->query($select_sql);
						$select_rows=mysqli_num_rows($select_query);
						if($select_rows==0)
						{
			
							//调用建立文件夹函数 createFolder
							createFolder($path);
					
							$insert_sql=sprintf("insert into ls_products (cateTitle,cateKeywords,cateDescription,midcateName,midcateFolders,midcateBrief,catePic,midcateDetail,midcateID,Num,enable) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
															GetSQLValueString($cateTitle, "text"),
															GetSQLValueString($cateKeywords, "text"),
															GetSQLValueString($cateDescription, "text"),
															GetSQLValueString(ucwords($midcateName), "text"),
															GetSQLValueString($midcateFolders, "text"),
															GetSQLValueString($midcateBrief, "text"),
															GetSQLValueString($picName, "text"),
															GetSQLValueString($midcateDetail, "text"),	
															GetSQLValueString($midcateID, "text"),
															GetSQLValueString($select_rows2+1, "int"),
															GetSQLValueString("1", "int")
														 );
								//echo $insert_sql;
								$insert_query=$opendb->query($insert_sql);
								//writelog($_SERVER['PHP_SELF'],"add mid-category: ".$midcateName,"ID: ".$id);
								echo "添加中类".ucwords($midcateName)."成功。<br />";
								/*echo "<script>alert('数据添加成功！');location='cate.php';</script>";  */
							}
							else
							{
								/*echo "<script>alert('已存在此中类名！\\n请填写别的中类名！');history.go(-1);</script>";	*/
							}
				
							break;
		
				}
			}
		}
		foreach($catemidnameArray2 as $k=>$val){
			$i++;
			if($i%2 != 0){
				$name = $val['catename'];
			}
			
			if($i%2 == 0){
				$catemidname2 = $val['catemidname'];
				$picNameOld=$_POST['PicOld'];
				$ls=$_POST['ls'];
				//$midcateName=replaceCatesName($_POST['midcateName']);
				$midcateName=trim($val['catemidname']);
				$ChangecatePagePic=$_POST['ChangecatePagePic'];
				
				//获得大类名称，用来判读此类下是否有同名的中类
				$cateName = trim($val['catemidname']);
				$cateTitle=$val['catemidname'];
				$cateKeywords=$val['catemidname'];
				$cateDescription=$val['catemidname'];
			
			
				$midcateBrief=str_replace("\n","<br />",htmlspecialchars($_POST['midcateBrief'], ENT_QUOTES));
				$midcateDetail=$_POST['midcateDetail'];
			
				$midcateFolders=$val['catemidname'];
				if($midcateFolders=="")				//如果没有设目录则截取
				{$midcateFolders=$midcateName;}
				$midcateFolders=trim(clearFolder(strtolower($midcateFolders)));
				
				$title_sql = "select * from ls_products where cateName='".$name."'";
				$opendb = new mydb();
				$title_query = $opendb->query($title_sql);
				$title_value = mysqli_fetch_assoc($title_query);
				
				$midcateID=$title_value['ID'];
				$cate_id=$title_value['ID'];
				$cateName=trim($val['catename']);
				
				if($midcateID!="")
				{
					$cate_sql="select * from ls_products where ID='".$cate_id."' ";
					$cate_query=$opendb->query($cate_sql);
					$cate_rows=mysqli_fetch_assoc($cate_query);
					$cateFolders=$cate_rows['cateFolders'];
				}
				
				$path = "../".$cateFolders."/".$midcateFolders;
				$ls = "insert";
				
				switch ($ls)
				{
					case "insert":
									
						$midcateName = str_replace("'","''",$midcateName);
						// 开始判断是否有重复的
						$select_sql="select * from ls_products where midcateName='".$midcateName."' and cateName='".$cateName."'";
						$select_query=$opendb->query($select_sql);
						$select_rows=mysqli_num_rows($select_query);
						if($select_rows==0)
						{
			
							//调用建立文件夹函数 createFolder
							createFolder($path);
					
							$insert_sql=sprintf("insert into ls_products (cateTitle,cateKeywords,cateDescription,midcateName,midcateFolders,midcateBrief,catePic,midcateDetail,midcateID,Num,enable) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
															GetSQLValueString($cateTitle, "text"),
															GetSQLValueString($cateKeywords, "text"),
															GetSQLValueString($cateDescription, "text"),
															GetSQLValueString(ucwords($midcateName), "text"),
															GetSQLValueString($midcateFolders, "text"),
															GetSQLValueString($midcateBrief, "text"),
															GetSQLValueString($picName, "text"),
															GetSQLValueString($midcateDetail, "text"),
															GetSQLValueString($midcateID, "text"),
															GetSQLValueString($select_rows2+1, "int"),
															GetSQLValueString("1", "int")
														 );
								//echo $insert_sql;
								$insert_query=$opendb->query($insert_sql);
								//writelog($_SERVER['PHP_SELF'],"add mid-category: ".$midcateName,"ID: ".$id);
								echo "添加中类".ucwords($midcateName)."成功。<br />";
								/*echo "<script>alert('数据添加成功！');location='cate.php';</script>";  */
							}
							else
							{
								/*echo "<script>alert('已存在此中类名！\\n请填写别的中类名！');history.go(-1);</script>";	*/
							}
				
							break;
		
				}
			}
		}
		//添加中类  结束
		
		//return array_merge($tree,$tree2,$tree3);
	}
	
	function copyfiles($file1,$file2){
		$contentx =@file_get_contents($file1);
		$openedfile = fopen($file2, "w");
		fwrite($openedfile, $contentx);
		fclose($openedfile);
		if($contentx === FALSE){
			$status=false;
		}else{
			$status=true;
		}
		return $status;
	} 
	
	function checkstr($str, $needle){
		$tmparray = explode($needle,$str);
		if(count($tmparray)>1){
			return true;
		}else{
			return false;
		}
    }
	
	function text($text_path,$text_name,$text_type,$text_category,$text_similarcate,$text_keyworks){
		$fp = fopen ($text_path,"r");
		$newscontent = fread ($fp,filesize ($text_path));
		$newscontent = str_replace("{name}",$text_name,$newscontent);
		$newscontent = str_replace("{type}",$text_type,$newscontent);
		$newscontent = str_replace("{category}",$text_category,$newscontent);
		$newscontent = str_replace("{similarcate}",$text_similarcate,$newscontent);
		$newscontent = str_replace("{itemno}",$text_type,$newscontent);
		$newscontent = str_replace("{keywords}",$text_keyworks,$newscontent);

		return $newscontent;
	}
	
	function add_imgs($path){ 
		$tree_imgs = array(); 
		foreach(glob($path.'/*') as $single){ 
			if(is_dir($single)){ 
				$tree_imgs = array_merge($tree_imgs,add_imgs($single)); 
			}else{ 
				$tree_imgs[] = $single; 
			} 
		} 
		return $tree_imgs; 
	}

	//添加图片
	function get_treeimgs($filepath){
		
		require('inc/constant.php');
		require_once('inc/ls_syscode.php');
		
		$path = "../".$filepath;
		$tree_imgs = array();
		foreach(glob($path.'/*') as $single){
			if(is_dir($single)){
				$tree_imgs = array_merge($tree_imgs,add_imgs($single));
			}
		}
		echo "<br />";
		$numi = 0;
		foreach ($tree_imgs as $val){
			$numi++;
			if(substr_count($val, "/") == 3){  //添加大类产品
					
					$allcate2 = getSubstr(dirname($val), $filepath."/", "");
					
					$numcate = substr($allcate2, 0, 2);
					if(is_numeric($numcate)){
						$allcate = str_replace($numcate,"",$allcate2);  //大类两位编号
					}else{
						$allcate = str_replace(substr($allcate2, 0, 1),"",$allcate2);  //大类一位编号
					}
					
					$title_sql = "select * from ls_products where cateName='".$allcate."'";
					$opendb = new mydb();
					$title_query = $opendb->query($title_sql);
					$title_value = mysqli_fetch_assoc($title_query);
					
					$productID = $title_value['ID'];
					
					$catefolderspro = $title_value['cateFolders'];
					
					$productItemNo=htmlspecialchars($allcate.$numi, ENT_QUOTES);
					$productName=htmlspecialchars($allcate, ENT_QUOTES);
					$num = $numi;
					
					// 写入产品描述
					if(checkstr($allcate, "bronze")){
						$text_path = "../text/1.html";
					}else{
						$text_path = "../text/2.html";
					}
					$productSpecification=text($text_path,$allcate,$allcate.$numi,$allcate,$allcate,$allcate);
					
					/* 产品详细页内关键词加链接程序   开始 */
					$text_key = getSubstr($productSpecification, "<tt>", "</tt>");
					$text_keywords = $text_key;
					$array_keywords = explode("; ", $text_keywords);
					$array_keys = array();
					foreach($array_keywords as $keywords){
						$array_keys[]['key_a'] = 0;
						$array_keys[]['keyword'] = $keywords;
					}
					$i = 0;
					$bool = false;
					foreach($array_keys as $key){
						$i++;
						if($i%2 == 0){
							$select_sql="select * from ls_products where cateName='".$key['keyword']."'";
							$select_query=$opendb->query($select_sql);
							$select_rows=mysqli_num_rows($select_query);
							if($select_rows != 0){
								$select_value = mysqli_fetch_assoc($select_query);
								$mid_sql="select * from ls_products where midcateID='".$select_value['ID']."'";
								$mid_query=$opendb->query($mid_sql);
								$mid_rows=mysqli_num_rows($mid_query);
								if($mid_rows == 0){
									$text_keywords = str_replace($key['keyword'],"<a href='../../".$select_value['cateFolders']."/index.html'>".$key['keyword']."</a>",$text_keywords);
									$bool = true;
								}
							}
						}
						if($i%2 != 0){
							if($bool){
								$key['key_a'] = 1;
								$bool = false;
							}
						}
					}
					
					foreach($array_keys as $keys){
						if($keys['key_a'] == 0){
							$i++;
							if($i%2 == 0){
								$midcate_sql="select * from ls_products where midcateName='".$keys['keyword']."'";
								$midcate_query=$opendb->query($midcate_sql);
								$midcate_rows=mysqli_num_rows($midcate_query);
								if($midcate_rows != 0){
									$midcate_value = mysqli_fetch_assoc($midcate_query);
									$cate_sql="select * from ls_products where ID='".$midcate_value['midcateID']."'";
									$cate_query=$opendb->query($cate_sql);
									$cate_rows=mysqli_fetch_assoc($cate_query);
									$text_keywords = str_replace($keys['keyword'],"<a href='../../../".$cate_rows['cateFolders']."/".$midcate_value['midcateFolders']."/index.html'>".$keys['keyword']."</a>",$text_keywords);
									$bool = true;
								}
							}
							if($i%2 != 0){
								if($bool){
									$keys['key_a'] = 1;
									$bool = false;
								}
							}
						}
					}
					$productSpecification = str_replace("<tt>".$text_key."</tt>", $text_keywords, $productSpecification);
					/* 产品详细页内关键词加链接程序   结束 */
					
					$characteristics=$_POST['characteristics'];
					$advantages=$_POST['advantages'];
					$parameters=$_POST['parameters'];
					
					$canshu = htmlspecialchars(trim($_POST['canshu']), ENT_QUOTES);
					$productTitle=$allcate;
					$productDescription=$allcate;
					$productKeywords=$allcate;
					$_POST['branchID'] == ''? $branchID = 0 : $branchID = $_POST['branchID'];
					$image=basename($val);
					$type=exif_imagetype($val);
					
					$ls = "insert";
					
					switch ($ls)
					{
						case "insert":
						
							if ($type == "1" || $type == "2" || $type == "3"){
								$pathBigPic="../".$catefolderspro."/".$BigFoldersProducts;
								//echo $pathBigPic."<br>";
								//创建大图文件夹
								createFolder($pathBigPic);
								$pathSmallPic="../".$catefolderspro."/".$SmallFoldersProducts;
								//echo $pathSmallPic."<br>";
								//创建小图文件夹
								createFolder($pathSmallPic);
								$pathPagePic="../".$catefolderspro."/".$showproductsFolders;
								//echo $pathSmallPic."<br>";
								//创建大图浏览（.htm）文件夹
								createFolder($pathPagePic);
																	
								$arr=array(" ",".");
								//产生文件名
								$productPic=str_replace($arr,"",time()+rand(100,1000)).".".substr($image,-3);
																
								$pathBigPic=$pathBigPic."/".$productPic;
								//echo  $pathBigPic."--".$_FILES['productPic']['tmp_name'];
								if(copyfiles($val, $pathBigPic)){
								//copy($val, $pathBigPic);
								
									chmod($pathBigPic, 0644);
									$toW=$BigPicW;			//宽度
									$toH=$BigPicH;			//高度
									$toFile=$pathBigPic;	//目标地址
									$srcFile=$pathBigPic;	//目标地址
									
									if(!$_POST['automationGif'])  //是动画则不缩小
									{
										ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
									}
									chmod($toFile,0644);
			
									$srcFile=$pathBigPic; //源地址
									$toW=$SmallPicW;			//宽度
									$toH=$SmallPicH;			//高度
									$toFile=$pathSmallPic."/".$productPic; //目标地址
									if(!$_POST['automationGif']) //是动画则不缩小
									{
										ImageResize($srcFile,$toW,$toH,$toFile);
									}
									else
									{
										copy($pathBigPic,$toFile);
									}
									chmod($toFile,0644);
									$select_sql2="select * from ls_products where productID=".$productID." ";
									$select_query2=$opendb->query($select_sql2);
									$select_rows2=mysqli_num_rows($select_query2); //总产品数
									$insert_sql=sprintf("insert into ls_products 															(productPic,productItemNo,productName,productSpecification,productID,Num,No,AdditionalAttributes,AdditionalAttributesClassPro,NoClassPro,canshu,enable,automationGif,productTitle,productKeywords,productDescription,characteristics,advantages,parameters,isbranch,branchID) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
									GetSQLValueString($productPic, "text"),
									GetSQLValueString($productItemNo, "text"),
									GetSQLValueString($productName, "text"),
									GetSQLValueString($productSpecification, "text"),
									GetSQLValueString($productID, "int"),
									GetSQLValueString($num, "int"),
									GetSQLValueString($No, "int"),
									GetSQLValueString($additionalAttributes, "int"),
									GetSQLValueString($classproduct, "int"),
									GetSQLValueString($NoClassPro, "int"),
									GetSQLValueString($canshu, "text"),
									GetSQLValueString("1", "int"),
									GetSQLValueString($automationGif, "int"),
									GetSQLValueString($productTitle, "text"),
									GetSQLValueString($productKeywords, "text"),
									GetSQLValueString($productDescription, "text"),
									GetSQLValueString($characteristics, "text"),
									GetSQLValueString($advantages, "text"),
									GetSQLValueString($parameters, "text"),
									GetSQLValueString($isbranch, "int"),
									GetSQLValueString($branchID, "int")
									);
									//echo $insert_sql;
									//添加数据
									
									echo "添加大类产品".$val."成功<br />";
									
									$insert_query=$opendb->query($insert_sql);
									writelog($_SERVER['PHP_SELF'],"add product: ".$productName,"ID: ".$opendb->insert_id,"admin");
									
									//增加代码参数 ls_prospec
									$select_sql2="select * from ls_prospec  ";
									$select_query2=$opendb->query($select_sql2);
									$select_rows2=mysqli_num_rows($select_query2); //总产品数
									$product_insert_id=$opendb->insert_id();
									//echo $specificationName."ll";
									for($i=0;$i<(count($specificationName));$i++)  //循环处理代码参数变量名和值，代码参数去掉: 
									{	
										$select_rows2++;
										$sql_spec="INSERT INTO `ls_prospec` (`productID`, `specName`, `specValue`, `Num`, `enable`) 
							 VALUES ('$product_insert_id', '".str_replace(':','',$specificationName[$i])."', '$specificationValue[$i]','$select_rows2', '1');";
			
										$insert_query=$opendb->query($sql_spec);
										
									}
								}

						 }
						else
						{
							 echo "请选择图片上传，或者你选择的文件不符合要求!只允许格式 .gif .png .jpeg<br />";
						}
						break;
					}
					
				}elseif(substr_count($val, "/") == 4){   //添加中类产品
				
					$allcate2 = getSubstr(dirname($val), $filepath."/", "/");
					$allmidcate2 = getSubstr(dirname($val), $filepath."/".$allcate2."/", "");
					
					$numcate = substr($allcate2, 0, 2);
					if(is_numeric($numcate)){
						$allcate = str_replace($numcate,"",$allcate2);  //大类两位编号
					}else{
						$allcate = str_replace(substr($allcate2, 0, 1),"",$allcate2);  //大类一位编号
					}
					
					$numcate2 = substr($allmidcate2, 0, 2);
					if(is_numeric($numcate2)){
						$allmidcate = str_replace($numcate2,"",$allmidcate2);  //中类两位编号
					}else{
						$allmidcate = str_replace(substr($allmidcate2, 0, 1),"",$allmidcate2);  //中类一位编号
					}
					
					$title_sql = "select * from ls_products where midcateName='".$allmidcate."'";
					$opendb = new mydb();
					$title_query = $opendb->query($title_sql);
					$title_value = mysqli_fetch_assoc($title_query);
					
					$productID = $title_value['ID'];
					
					$p_sql = "select * from ls_products where ID='".$title_value['midcateID']."'";
					$p_query = $opendb->query($p_sql);
					$p_value = mysqli_fetch_assoc($p_query);
					
					$catefolderspro = $p_value['cateFolders']."/".$title_value['midcateFolders'];
					
					$productItemNo=htmlspecialchars($allmidcate.$numi, ENT_QUOTES);
					$productName=htmlspecialchars($allmidcate, ENT_QUOTES);
					$num = $numi;
					
					// 写入产品描述
					if(checkstr($allmidcate, "bronze")){
						$text_path = "../text/1.html";
					}else{
						$text_path = "../text/2.html";
					}
					$productSpecification=text($text_path,$allmidcate,$allmidcate.$numi,$allcate,$allmidcate,$allmidcate);
					
					/* 产品详细页内关键词加链接程序   开始 */
					$text_key = getSubstr($productSpecification, "<tt>", "</tt>");
					$text_keywords = $text_key;
					$array_keywords = explode("; ", $text_keywords);
					$array_keys = array();
					foreach($array_keywords as $keywords){
						$array_keys[]['key_a'] = 0;
						$array_keys[]['keyword'] = $keywords;
					}
					$i = 0;
					$bool = false;
					foreach($array_keys as $key){
						$i++;
						if($i%2 == 0){
							$select_sql="select * from ls_products where cateName='".$key['keyword']."'";
							$select_query=$opendb->query($select_sql);
							$select_rows=mysqli_num_rows($select_query);
							if($select_rows != 0){
								$select_value = mysqli_fetch_assoc($select_query);
								$mid_sql="select * from ls_products where midcateID='".$select_value['ID']."'";
								$mid_query=$opendb->query($mid_sql);
								$mid_rows=mysqli_num_rows($mid_query);
								if($mid_rows == 0){
									$text_keywords = str_replace($key['keyword'],"<a href='../../".$select_value['cateFolders']."/index.html'>".$key['keyword']."</a>",$text_keywords);
									$bool = true;
								}
							}
						}
						if($i%2 != 0){
							if($bool){
								$key['key_a'] = 1;
								$bool = false;
							}
						}
					}
					
					foreach($array_keys as $keys){
						if($keys['key_a'] == 0){
							$i++;
							if($i%2 == 0){
								$midcate_sql="select * from ls_products where midcateName='".$keys['keyword']."'";
								$midcate_query=$opendb->query($midcate_sql);
								$midcate_rows=mysqli_num_rows($midcate_query);
								if($midcate_rows != 0){
									$midcate_value = mysqli_fetch_assoc($midcate_query);
									$cate_sql="select * from ls_products where ID='".$midcate_value['midcateID']."'";
									$cate_query=$opendb->query($cate_sql);
									$cate_rows=mysqli_fetch_assoc($cate_query);
									$text_keywords = str_replace($keys['keyword'],"<a href='../../../".$cate_rows['cateFolders']."/".$midcate_value['midcateFolders']."/index.html'>".$keys['keyword']."</a>",$text_keywords);
									$bool = true;
								}
							}
							if($i%2 != 0){
								if($bool){
									$keys['key_a'] = 1;
									$bool = false;
								}
							}
						}
					}
					$productSpecification = str_replace("<tt>".$text_key."</tt>", $text_keywords, $productSpecification);
					/* 产品详细页内关键词加链接程序   结束 */
					
					$characteristics=$_POST['characteristics'];
					$advantages=$_POST['advantages'];
					$parameters=$_POST['parameters'];
					
					$canshu = htmlspecialchars(trim($_POST['canshu']), ENT_QUOTES);
					$productTitle=$allmidcate;
					$productDescription=$allmidcate;
					$productKeywords=$allmidcate;
					$_POST['branchID'] == ''? $branchID = 0 : $branchID = $_POST['branchID'];
					$image=basename($val);
					$type=exif_imagetype($val);
					
					$ls = "insert";
					
					switch ($ls)
					{
						case "insert":
						
							if ($type == "1" || $type == "2" || $type == "3"){

								$pathBigPic="../".$catefolderspro."/".$BigFoldersProducts;
								//echo $pathBigPic."<br>";
								//创建大图文件夹
								createFolder($pathBigPic);
								$pathSmallPic="../".$catefolderspro."/".$SmallFoldersProducts;
								//echo $pathSmallPic."<br>";
								//创建小图文件夹
								createFolder($pathSmallPic);
								$pathPagePic="../".$catefolderspro."/".$showproductsFolders;
								//echo $pathSmallPic."<br>";
								//创建大图浏览（.htm）文件夹
								createFolder($pathPagePic);
																	
								$arr=array(" ",".");
								//产生文件名
								$productPic=str_replace($arr ,"",time()+rand(100,1000)).".".substr($image,-3);
																
								$pathBigPic=$pathBigPic."/".$productPic;
								//echo  $pathBigPic."--".$_FILES['productPic']['tmp_name'];
								if(copyfiles($val, $pathBigPic)){
								//copy($val, $pathBigPic);
								
									chmod($pathBigPic, 0644);
									$toW=$BigPicW;			//宽度
									$toH=$BigPicH;			//高度
									$toFile=$pathBigPic;	//目标地址
									$srcFile=$pathBigPic;	//目标地址
									
									if(!$_POST['automationGif'])  //是动画则不缩小
									{
										ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
									}
									chmod($toFile,0644);
			
									$srcFile=$pathBigPic; //源地址
									$toW=$SmallPicW;			//宽度
									$toH=$SmallPicH;			//高度
									$toFile=$pathSmallPic."/".$productPic; //目标地址
									if(!$_POST['automationGif']) //是动画则不缩小
									{
										ImageResize($srcFile,$toW,$toH,$toFile);
									}
									else
									{
										copy($pathBigPic,$toFile);
									}
									chmod($toFile,0644);
									$select_sql2="select * from ls_products where productID=".$productID." ";
									$select_query2=$opendb->query($select_sql2);
									$select_rows2=mysqli_num_rows($select_query2); //总产品数
									$insert_sql=sprintf("insert into ls_products 															(productPic,productItemNo,productName,productSpecification,productID,Num,No,AdditionalAttributes,AdditionalAttributesClassPro,NoClassPro,canshu,enable,automationGif,productTitle,productKeywords,productDescription,characteristics,advantages,parameters,isbranch,branchID) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
									GetSQLValueString($productPic, "text"),
									GetSQLValueString($productItemNo, "text"),
									GetSQLValueString($productName, "text"),
									GetSQLValueString($productSpecification, "text"),
									GetSQLValueString($productID, "int"),
									GetSQLValueString($num, "int"),
									GetSQLValueString($No, "int"),
									GetSQLValueString($additionalAttributes, "int"),
									GetSQLValueString($classproduct, "int"),
									GetSQLValueString($NoClassPro, "int"),
									GetSQLValueString($canshu, "text"),
									GetSQLValueString("1", "int"),
									GetSQLValueString($automationGif, "int"),
									GetSQLValueString($productTitle, "text"),
									GetSQLValueString($productKeywords, "text"),
									GetSQLValueString($productDescription, "text"),
									GetSQLValueString($characteristics, "text"),
									GetSQLValueString($advantages, "text"),
									GetSQLValueString($parameters, "text"),
									GetSQLValueString($isbranch, "int"),
									GetSQLValueString($branchID, "int")
									);
									//echo $insert_sql;
									//添加数据
									echo "添加中类产品".$val."成功<br />";
									
									$insert_query=$opendb->query($insert_sql);
									writelog($_SERVER['PHP_SELF'],"add product: ".$productName,"ID: ".$opendb->insert_id,"admin");
									
									//增加代码参数 ls_prospec
									$select_sql2="select * from ls_prospec  ";
									$select_query2=$opendb->query($select_sql2);
									$select_rows2=mysqli_num_rows($select_query2); //总产品数
									$product_insert_id=$opendb->insert_id();
									//echo $specificationName."ll";
									for($i=0;$i<(count($specificationName));$i++)  //循环处理代码参数变量名和值，代码参数去掉: 
									{	
										$select_rows2++;
										$sql_spec="INSERT INTO `ls_prospec` (`productID`, `specName`, `specValue`, `Num`, `enable`) 
							 VALUES ('$product_insert_id', '".str_replace(':','',$specificationName[$i])."', '$specificationValue[$i]','$select_rows2', '1');";
			
										$insert_query=$opendb->query($sql_spec);
										
									}
								}
						 }
						else
						{
							 echo "请选择图片上传，或者你选择的文件不符合要求!只允许格式 .gif .png .jpeg<br />";
						}
						break;
					}
				}
		}
	}
	
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>无标题文档</title>
		<link href="css/admin.css" rel="stylesheet" type="text/css">
		<link rel="stylesheet" href="../themes/default/default.css" />
		<script language="JavaScript" src="js/ls.js"></script>
</head>
<body>
<?php
switch($ls)
{
	case "insert":
		if(file_exists("../".$filepath) && $filepath != ""){
			get_filetree($filepath);  //添加类别（大、中类）
			get_treeimgs($filepath);  //添加产品图片
		}else{
			echo "	<script>alert('您输入的文件夹不存在或者内容错误，请重新输入！');history.go(-1);</script>";
		}
		
	break;
}
?>
		<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#6396d6" <?php if($ls == "insert") echo "style='display:none;' ";?>>
			<tr>
				<td height="18" background="images/admin_bg.gif"><div align="center"><font color="#ffffff"><strong>自动添加产品</strong></font></div>
				</td>
			</tr>
			<tr>
				<td height="213" valign="top" bgcolor="#dee7ff"><br>
				 <form name="newsform" enctype="multipart/form-data" method="post" action="autopro.php" >
					<input type="hidden" value="insert" name="ls">
					<table border="0" width="90%" align="center">
						<tr>
							<td>提示：<font color="#FF0000"><?php echo $ls_msg; ?></font></td>
							<td align="right">&nbsp;</td>
						</tr>
					</table>
					<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
					 
					  <tr bgcolor="#B9DAFF"> 
						<td width="40%" height="28" ></td>
                        <td width="60%" height="28" ></td>
					  </tr>
					  <tr bgcolor="#f4f4f4"> 
						<td align="right">
                        产品目录名称：
						</td>
                        <td><input name="filepath" type="text" size="50" /></td>
						</tr>
					  <tr bgcolor="#B9DAFF"> 
						<td height="25" colspan="2" align="center"><input type="submit" value="提交">
						  <label>
						    <input type="reset" name="button" id="button" value="重置" />
				        </label></td>
					  </tr>
					</table>
				</form>
				</td>
			</tr>
		</table>
	</body>
</html>

