<?php
    session_start(); 
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	$opendb=new mydb();
	require_once('inc/char_set.php');
	require_once('generalpicrequire.php');  //generalpic类型选择语句, 表名、图片文件存储目录、字段变量名 存储变量
?>
<!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'];
	
	$modiGeneralpicsTitle=$_POST['modiGeneralpicsTitle'];
	$modiGeneralpicsText=$_POST['modiGeneralpicsText'];
	$matealt=$_POST['matealt'];
	$matetitle=$_POST['matetitle'];
	
	$newstitle=$_POST['newsTitle'];
	$newskeywords=$_POST['newsKeywords'];
//	$newsdescription=substr(strip_tags(trim(preg_replace("/([\s]{2,})/"," ",$modiGeneralpicsText))), 0, 200);
	$newsdescription=substr(strip_tags(trim(preg_replace("/([\s]{2,})/"," ",$_POST['newsDescription']))), 0, 200);
	
	//编辑器中的内链与图片转换成相对路径
	$modiGeneralpicsText = editorPath('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'], $modiGeneralpicsText);//内链
	$modiGeneralpicsText = editorPath($_SERVER['PHP_SELF'], $modiGeneralpicsText);//图片
	$modiGeneralpicsText = addAltTitle($modiGeneralpicsTitle, $modiGeneralpicsTitle, $modiGeneralpicsText);  //图片添加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=mysql_fetch_assoc($in_query)){
		$modiGeneralpicsText = internal_keyword_link($modiGeneralpicsText, $in_rows['title'], $in_rows['url']);
	}
	
	$ChangecatePagePic=$_POST['ChangecatePagePic'];
	$image=$_FILES['productPic']['name'];   
	$size=$_FILES['productPic']['size'];   
	$type=$_FILES['productPic']['type'];
	//widget
	file_exists("widget/generalpics/savevars.php")? include "widget/generalpics/savevars.php" :"";  //generalpicrequire.php 中定义
	//end_widget
	
	$videoname=$_FILES['productVideo']['name'];   
  	$videosize=$_FILES['productVideo']['size']/1000000;   
  	$videotype=$_FILES['productVideo']['type'];
	
	$pathBigPic="../".$_SESSION['dda'].$folder1;
	$pathSmallPic="../".$_SESSION['dda'].$folder2;


	switch ($ls)
	{
		case "insert":
			if ($type == "image/gif" || $type == "image/png" || $type == "image/pjpeg" || $type == "image/jpeg" )
			{
			 	if($size < $PicSize) //开始判断图像大小
				{
					//echo $pathBigPic."<br>";
					//创建大图文件夹
					createFolder($pathBigPic);
					//echo $pathSmallPic."<br>";
					//创建小图文件夹
					createFolder($pathSmallPic);
										
					$arr=array(" ",".");   
					//产生文件名   
					$productPic=str_replace($arr ,"",time()).".".get_extension($image);
					
					$pathBigPic=$pathBigPic."/".$productPic;
					//echo $pathBigPic."<br>";
					//上传文件
					move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
					
					$srcFile=$pathBigPic; //源地址
					$toW=(isset($thisPicW)?$thisPicW:$SmallPicW);			//宽度
					$toH=(isset($thisPicH)?$thisPicH:$SmallPicH);			//高度
					$toFile=$pathSmallPic."/".$productPic; //目标地址
					//生成缩略图
					ImageResize($srcFile,$toW,$toH,$toFile,false,9,(isset($sameSide)?$sameSide:"N"));
					if ( $videoname!=""){ // 有视频
					//echo  $videoname."--";exit;
						if ($videotype == "video/mp4"  || $videotype == "video/webm" || $videotype == "video/ogg" ){ //视频处理
							 if($videosize < $videoSizeMax){
																		
									$arr=array(" ",".");   
									
									//产生文件名   
									$productVideo=str_replace($arr ,"",time()).".".get_extension($videoname); //产品名
																	
									$pathBigVdo="../".$folder1."/".$productVideo;
									//echo  $pathBigVdo."--".$_FILES['productVideo']['tmp_name'];exit;
									move_uploaded_file($_FILES['productVideo']['tmp_name'], $pathBigVdo);
									chmod($pathBigVdo, 0644);
								}						
								else
								{
									echo "	<script>alert('视频最大为".$videoSizeMax."！');history.go(-1);</script>";
								}
						
							}
							else
							{
									 echo "	<script>alert('请选视频上传，或者你选择的文件不符合要求!\\n只允许格式  .mp4 .webm .ogg');history.go(-1);</script>";
						}//视频处理
					}
					
					
					$select_sql2="select * from ".$generalTable." ";
					$select_query2=$opendb->query($select_sql2);
					$select_rows2=mysql_num_rows($select_query2); //总数
					
					$insert_sql=sprintf("insert into ".$generalTable." (".$generalpicsPic.", newsTitle, newsKeywords, newsDescription, ".$generalpicsTitle.",".$generalpicsText.",Num,enable,matealt,matetitle,productVideo) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
					GetSQLValueString($productPic, "text"),
					GetSQLValueString($newstitle, "text"),
					GetSQLValueString($newskeywords, "text"),
					GetSQLValueString($newsdescription, "text"),
					GetSQLValueString($modiGeneralpicsTitle, "text"),
					GetSQLValueString($modiGeneralpicsText, "text"),
					GetSQLValueString(($select_rows2+1), "int"),
					GetSQLValueString("1", "int"),
					GetSQLValueString($matealt, "text"),
					GetSQLValueString($matetitle, "text"),
					GetSQLValueString($productVideo, "text")
					);
					$insert_query=$opendb->query($insert_sql);
					//echo $insert_sql;
					//widget
					file_exists("widget/generalpics/saveins.php")?include "widget/generalpics/saveins.php" :"";
					//end_widget
					
					echo "
									<form name=\"fm\" method=\"post\" action=\"addmodigenpics.php?gen=".$gen."&ls=insert\">
									<input type=\"hidden\" value=\"".$generalpicsText."\" name=\"generalpicsText\">
									";
									//widget
									file_exists("widget/generalpics/savehidevars.php")?include "widget/generalpics/savehidevars.php" :"";
									//end_widget
									echo "
									<table width='96%' border='0' align='center' cellpadding='1' cellspacing='1' bgcolor='#6396d6'>
					<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>
							<table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#FFFFFF\">
					 
							  <tr bgcolor=\"#B9DAFF\"> 
								<td height=\"28\" colspan=\"2\"></td>
							  </tr>
							  <tr bgcolor=\"#f4f4f4\"> 
								<td style=\"padding-left:50px;\" >*缩略图：</td>
								<td style=\"padding-left:5px;\" >
									<img src=\" ".$toFile." \" width=\"".(isset($thisPicW)?$thisPicW:$SmallPicW)."\" height=\"".(isset($thisPicH)?$thisPicH:$SmallPicH)."\" border=\"1\" >
									
								</td>
								</tr>
  <tr bgcolor=\"#f4f4f4\"> 
								<td style=\"padding-left:50px;\" >视频：</td>
								<td style=\"padding-left:5px;\" >
								<video  width=\"". $SmallPicW  ."\"  id=\"vdo\" style=\"float:left;\" border=\"1\" poster=\"".$pathBigVdo."\" autoplay=\"\" preload=\"Metadata\" controls=\"controls\"><source src=\"images/picture.gif\" /></video>
								</td>
								</tr>							  
							  <tr bgcolor=\"#f4f4f4\"> 
								<td style=\"padding-left:50px;\" width=\"30%\">简介：</td>
								<td>
								".htmlspecialchars_decode($modiGeneralpicsText,ENT_QUOTES)."
								</td>
							  </tr>
							  <tr bgcolor=\"#B9DAFF\"> 
								<td height=\"28\" colspan=\"2\" align=\"center\">
								<input type=\"submit\" value=\"继续添加\">
								<input type=\"button\" value=\"返回列表\" onclick=\"javascript:location='generalpics.php?gen=".$gen."';\">
								</td>
							  </tr>
							
							</table>
							<br/>
						</td>
					</tr>
					</table>
					</form>
					";
					}						
				 else
					{
						echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
					}
				}
			else
				{
					 echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 .gif .png .jpeg');history.go(-1);</script>";
				}
			
			break;
		
	
		case "update":
		$deletevideo=$_POST['deletevideo'];
		$products_sql="select * from ".$generalTable." where ID=".$id." ";
		$products_query=$opendb->query($products_sql);
		$products_rows=mysql_fetch_assoc($products_query);
		$thisVdo=$products_rows['productVideo']==""?(time().".".get_extension($videoname)):$products_rows['productVideo'];
		$pathBigVdo="../".$folder1."/".$thisVdo;
		
		if($deletevideo=="deletevideo"){
			//echo $products_sql."--".$pathBigVdo;exit;
			unlink($pathBigVdo);	//delete old small pic
			$update_vdo_sql="update ".$generalTable." set `productVideo`='' where ID=$id "; //clear productVideo
			$update_query=$opendb->query($update_vdo_sql);
			echo "<script>alert('视频已删除!\\n');location='addmodigenpics.php?gen=".$gen."&id=".$id."&ls=update';</script>";
			
		}
		
		
		
					
			  if($ChangecatePagePic=="yes")
				{
					//判断图像格式
					if ($type == "image/gif" || $type == "image/png" || $type == "image/pjpeg" || $type == "image/jpeg" )
						{
							if($size < $PicSize) //开始判断图像大小
							{
				
								$arr=array(" ",".");   
								//产生文件名   
								$pic=$_POST['Pic'];
								$pic=trim(str_replace(get_extension($pic),"",$pic));
								//echo $pic;
								$productPic=str_replace($arr ,"",$pic).".".get_extension($image);
																		
								$pathBigPic="../".$folder1."/".$productPic;
								//echo $pathBigPic;
								//上传文件
								
								move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
								
								$srcFile=$pathBigPic; //源地址
								$toW=(isset($thisPicW)?$thisPicW:$SmallPicW);			//宽度
								$toH=(isset($thisPicH)?$thisPicH:$SmallPicH);			//高度
								$toFile=$pathSmallPic."/".$productPic; //目标地址
								//echo $toFile;
								//生成缩略图
								ImageResize($srcFile,$toW,$toH,$toFile,false,9,(isset($sameSide)?$sameSide:"N"));	
								
								$updateimg_sql=sprintf("update ".$generalTable." set ".$generalpicsPic."=%s where ID=%s",
								GetSQLValueString($pic.get_extension($image), "text"),
								GetSQLValueString($id, "int")
								);
								$update_query=$opendb->query($updateimg_sql);
							}
								else
								{
									echo "	<script>alert('图像最大为".$PicSize."！');history.go(-1);</script>";
								}
							}
							else
							{
								 echo "	<script>alert('请选择图片上传，或者你选择的文件不符合要求!\\n只允许格式 .gif .png .jpeg');history.go(-1);</script>";
							}
				} 
				if ( $videoname!=""){ // 有视频

						if ($videotype == "video/mp4" || $videotype == "video/webm" || $videotype == "video/ogg"  ){ //视频处理
						   if($videosize < $videoSizeMax){
																	  
								  // echo $pathBigVdo."--".$_FILES['productVideo']['tmp_name'];exit;
								  move_uploaded_file($_FILES['productVideo']['tmp_name'], $pathBigVdo);
								  chmod($pathBigVdo, 0644);
								  
								  $update_pic_sql=sprintf("update  ".$generalTable."  set   productVideo=%s  where ID=%s",
				   
								  GetSQLValueString($thisVdo, "text"),
								  GetSQLValueString($id, "int"));
				   
								  $update_query=$opendb->query($update_pic_sql);
		
							  }						
							  else
							  {
								  echo "	<script>alert('视频最大为".$videoSizeMax."！');history.go(-1);</script>";
							  }
					  
						  }
						  else
						  {
								   echo "	<script>alert('请选视频上传，或者你选择的文件不符合要求!\\n只允许格式  .mp4 .webm .ogg');history.go(-1);</script>";
					  }//视频处理
			  }
  
				
				$update_sql=sprintf("update ".$generalTable." set newsTitle=%s, newsKeywords=%s, newsDescription=%s, ".$generalpicsTitle."=%s, ".$generalpicsText."=%s,  matealt=%s,  matetitle=%s where ID=%s",
				GetSQLValueString($newstitle, "text"),
				GetSQLValueString($newskeywords, "text"),
				GetSQLValueString($newsdescription, "text"),
				GetSQLValueString($modiGeneralpicsTitle, "text"),
				GetSQLValueString($modiGeneralpicsText, "text"),
				GetSQLValueString($matealt, "text"),
				GetSQLValueString($matetitle, "text"),
				GetSQLValueString($id, "int")
				);
				
				//widget
				file_exists("widget/generalpics/savemod.php")?include "widget/generalpics/savemod.php" :"";
				//end_widget
				
				//更新数据
				$update_query=$opendb->query($update_sql);
				
				$_SESSION["refreshModi"]=true;
		
				echo "<script>location='generalpics.php?gen=".$gen."&id=".$id."&ls=update&ls_msg=No.".$id." ".$gen." 已更新';</script>";
				
		break;	
	}
?>
	</body>
</html>