<?php
	session_start(); 
	require_once('../ls_admin/inc/conn.php');
	require_once('../ls_admin/inc/ls_syscode.php');
	require_once('../ls_admin/inc/constant.php');
	require_once('../ls_admin/inc/check.php');
	$opendb=new mydb();
	require_once('../ls_admin/inc/char_set.php');
	$pubDir="../publish/";
	require_once($pubDir.'newver.php');
	if($_POST['lastVerdate']!="")
	{$lastupdate = $_POST['lastVerdate'];}
	else
	{$lastupdate = $lastVerdate;}
	

if($_POST["update"]== "确认")
{
	$newVer=trim($_POST["newVer"]);
	$lastVerdate=time();
	$oldVer=trim($_POST["oldVer"]);
	
	
	$select_sql2="select * from ls_ver where ver=".$newVer." ";
	$select_query2=$opendb->query($select_sql2);
	$select_rows2=mysql_num_rows($select_query2); //总数
	if($select_rows2>0)
	{ $ls_msg="版本 $newVer 已经存在，请重新选择"; }
	else
	{
	
		$filestr="#file Ver: $oldVer - $newVer,   thisVerdate:$lastVerdate,    Datetime: ".date('Y-m-d H:m:s',$lastupdate)." - ".date('Y-m-d H:m:s',$lastVerdate)."\r\n";  //全局变量，在tree函数里面被更新
		$newVerDir=$pubDir.$newVer;
	
		if(mkdir($newVerDir))
		{
			tree("..",$lastupdate  );
			if(writegeneralfile($newVerDir."/updatefiles.txt",$filestr))
			{ 
			 
				writeconstant("newVer",substr($newVer,-4,4),"int","","",$pubDir."newver.php");
				writeconstant("lastVerdate",$lastVerdate,"int","","",$pubDir."newver.php");
			
				$insert_sql="insert into ls_ver(lastVerdate,verDateTime,ver,memo) values (".$lastVerdate.",'".date('Y-m-d H:m:s',$lastVerdate)."','".$newVer."',1)";
				//echo $insert_sql;
				$insert_query=$opendb->query($insert_sql);
				
				$handle = fopen ($pubDir."ver.txt", "r");
				$buffer="";
				while (!feof ($handle)) {    $buffer .= fgets($handle, 4096);  }  
				fclose ($handle);
				$updateverstr=$buffer."\r\n# ver".$newVer." ".date('Y-m-d H:m:s',$lastupdate);  //记录版本文件
				writegeneralfile($pubDir."ver.txt",$updateverstr);
				
				$ls_msg= "生成 $newVer ok";
			
				 
			} 
			else
			{ echo "写入 $newVerDir /updatefiles.txt 失败。"; }
		}
		else
		{echo "目录 $newVerDir 建立失败" ;}
	}
}
?>
<!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>
<title><?php echo $engineName?>d</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link href="../ls_admin/css/admin.css" rel="stylesheet" type="text/css">
<title>无标题文档</title>
</head>

<body>
<table width="96%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#6396D6">
  <tr> 
	<td height="18" background="../ls_admin/images/admin_bg.gif"><div align="center"><font color="#FFFFFF"><strong>创建新版本</strong></font></div></td>
  </tr>

  <tr>
  	<td bgcolor="#E8F1FF" valign="top" align="center">
	<br>
		<form name="form1" method="post" action="?ls=eidt">
	  <p>&nbsp;</p>
	  <table border="0">
		  <tr>
				<td width="92" align="right">上次版本：</td>
				<td width="216" align="center"><input name="oldVer" type="text" id="oldVer" size="26" maxlength="26" value="<?=$newVer?>"  readonly="readonly"  style="color:#666"></td>
				<td width="216" align="center"><span style="padding-left:20px;">
				  <select name="select2" id="select2"  onchange="form1.oldDate.value=this.options[this.selectedIndex].text.substr(0,19);form1.oldVer.value=this.options[this.selectedIndex].text.substr(this.options[this.selectedIndex].text.length-4,4);form1.lastVerdate.value=this.value;form1.newVer.value=form1.newVer.value.substr(form1.newVer.value.length-4,4);">
				    <option value="0">请选择一个版本：</option>
			<?
			$news_sql="SELECT * FROM  ls_ver  order by ID Desc";//count the error by searchtype
			$news_query=$opendb->query($news_sql);
			while($news_rows2=mysql_fetch_assoc($news_query))
		  	{
				
				echo "<option value=".$news_rows2['lastVerdate'].">".$news_rows2['verDateTime']."  ".$news_rows2['ver']."</option>";
				
			}
			?>
			    </select>
				</span></td>
			</tr>
			<tr>
				<td align="right">上次更新日期：</td>
				<td align="center"> <input name="oldDate" type="text" id="oldDate" size="26" maxlength="26" value="<?=date('Y-m-d   H:m:s',$lastupdate)?>"   readonly="readonly" style="color:#666"></td>
				<td align="center">&nbsp;</td>
			</tr>
			<tr>
				<td align="right">新版本：</td>
				<td align="center"><input name="newVer" type="text" id="newVer" size="26" maxlength="26" value="<?=substr($newVer,-4,4)+0.01?>"></td>
				<td align="center"><input type="hidden" name="lastVerdate" id="hiddenField" /></td>
			</tr>
			<tr>
			  <td height="41" align="center">&nbsp;</td>
			  <td height="41" colspan="2" align="center"><input name="update" type="submit" id="submit" value="确认" />
              <input type="reset" name="Submit2" value="重填" /></td>
		  </tr>
			<tr>
			  <td height="41" align="center">&nbsp;</td>
			  <td height="41" align="center"><font color="#FF0000"><?php echo $ls_msg; ?></font></td>
			  <td align="center">&nbsp;</td>
	      </tr>
 <tr>
<td colspan="3" align="left">

<?php 
if($_POST["update"]== "确认" && $select_rows2==0 && $newVerDir!="")  //没有新版本
{
	//echo mktime(0,0,0,12,20,2012);
	echo "<br/><br/>";
	$handle = fopen ($newVerDir."/updatefiles.txt", "r");
	while (!feof ($handle)) {    $buffer = fgets($handle, 4096);    
	echo $buffer."<br/>";}
	fclose ($handle);
}
?>

	</td>
 </tr>
		</table>
	  </form>
		<br>

	</td>
 </tr>
	

</table>

</body>
</html>

<?
/* 目录树遍历,写入 updatefiles.txt*/ 
function tree($directory,$lastupdate ) 
{ 
	$updatedir=       array("Templates1","Templates2","Templates3","Templates4","Templates","css","js","img");
	$updatedir[]=array_push($updatedir,"ls_admin","ls_admin/inc","ls_admin/css","ls_admin/js","update","ls_mantain");
	global $dirFlagstr,$filestr,$newVerDir;
	isset($dirFlagstr)?:$dirFlagstr="";
	
	
	$mydir=dir($directory); 
	while($file=$mydir->read())
	{ 
		if(($file!=".") AND ($file!=".."))
		{ 
			if((is_dir("$directory/$file")) )
			{
				
				tree("$directory/$file",$lastupdate ); //递归查询目录
			}
			else
			{ 
				if(in_array(str_replace("../","","$directory"),$updatedir))
				{
					if((filemtime ( "$directory/$file" )>$lastupdate) && (strpos(iconv('GB2312', 'UTF-8', $file),"拷贝")===false)&& (strpos( $file,"constant.php")===false)) 
					//已更新目录下的已更新文件
					{ 
						if($directory!=$dirFlagstr) 
						{
							$wrdirectory=str_replace("../","","$directory");
							$filestr.="\n--$wrdirectory\n";
							//echo "$newVerDir/$wrdirectory";
							if(!file_exists("$newVerDir/$wrdirectory")){crmkdir("$newVerDir/$wrdirectory");}
							$dirFlagstr="$directory";
							$filestr.="$wrdirectory/$file\n";
							copy("$directory/$file","$newVerDir/$wrdirectory/$file");
						}
						else
						{
							$wrdirectory=str_replace("../","","$directory");
							$filestr.="$wrdirectory/$file\n";
							copy("$directory/$file","$newVerDir/$wrdirectory/$file");
						}
						
					
					}
				}
			} 
			
		}
	} 
	$mydir->close(); 
	return ($filestr);
	
} 
?>