<?php

		require_once('../ls_admin/inc/constant.php');
		require_once('../ls_admin/inc/ls_syscode.php');
		require_once('../update/newver.php');  //new ver No

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?
		if($lsVer< $newVer )		//设置,更新版本
		{
			$dirName="../update/".$newVer;
			if(file_exists($dirName))
			{
				updateconstant($newVer);
				updatedb($newVer);
				updatefiles($newVer);		
				writeconstant("lsVer",$newVer ,"int","","//----版本号参数");	   		//更新版本号	
				writeconstant("date",date('Y-m-d   H:m:s') ,"str","","//----版本号参数");	   		//更新日期	
			}
			else
			{ echo "目录 $dirName 不存在，不能更新。 <br/>";}
			
			echo "	<script>alert('蓝信外贸管理系统 ls-webmotor 已尝试更新');</script>";

		}
		else
		{ echo "目前版本已是 $newVer ";}
		


/*
更新参数
*/
function updateconstant($newVer)
{
	$filename="../update/".$newVer."/updateconstant.txt"; 
	//echo $filename;
	if(file_exists($filename))
	{
		$handle = fopen($filename,"r");
		$section ="";
		$partstr0="";
		while (!feof ($handle)) 
		{
			$partstr0 = trim( fgets ($handle, 6000));
			//echo ($partstr0);
			if( strpos($partstr0,"#constant")!==0)  //不是注释
			{
				if( strpos($partstr0, "#")!==0 && $partstr0!="") //不是注释 和空行
				{
					if( strpos($partstr0,"//----")===0)  //不是注释
					{
						$section =$partstr0;
					}
					else
					{
						$splitPosition=strpos($partstr0,"=");
						if( $splitPosition >0 &&  strpos($partstr0,"\$")===0)  //不是注释
						{
							$varName=substr($partstr0,1,($splitPosition-1));
							$varValue=str_replace(";","",trim(substr($partstr0,($splitPosition+1),strlen($partstr0))));  //删除；
							if(strpos($varValue,"\"")===0 || strpos($varValue,"'")===0)
							{$ifstr="str";}
							else
							{$ifstr="int";}
							$varValue=str_replace("'","",$varValue); //删除'；
							$varValue=str_replace("\"","",$varValue); //删除"；

							//echo $varName."=".$varValue."  ".$ifstr."  ".$section."<br/>";
							writeconstant($varName,$varValue ,$ifstr,"",$section);	
							
						}
					
					}
				}

			}
		
		}
		fclose ($handle);	
		return "常量文件 $filename 已更新。";	
	}
	else
	{
		return "文件 $filename 不存在。";		

	}

}

/*
更新数据库
*/
function updatedb($newVer)
{
	$filename="../update/".$newVer."/updatedb.txt"; 
	if(file_exists($filename))
	{
	require_once('../ls_admin/inc/conn.php');
	$openck=new cktb();
		
	 //更新数据库处理	

	$handle = fopen($filename,"r");
	$partstr0="";
	while (!feof ($handle)) 
	{
		$partstr0 =  fgets ($handle, 6000);
		//echo ($partstr0);
		if( stripos(trim($partstr0),"CREATE TABLE ")===0)  //增加表
		{
			$partstr1=	str_replace('CREATE TABLE','',$partstr0);
			$partstr1=	trim(str_replace('`','',$partstr1));
			$partpos=strpos( $partstr1," ");  //按空格分割
				//echo $partstr1;
			$partstr11=trim(substr($partstr1,0,$partpos));
			
			$tableName=$partstr11;
			$addTBSQL=$partstr0;
			//echo $tableName."  <br/> ".$addTBSQL."<br/>";
			if(stripos(trim($tableName), ".")===false)	//没有数据库
			{if($openck->checktable($tableName,$addTBSQL))	echo $openck->ls_msg;}
			else
			{ echo "忽略 $tableName<br/>"; }

		}
		else
		{
			if( stripos(trim($partstr0),"ALTER TABLE")===0 && stripos($partstr0," ADD ")>0 )  //增加字段
			{
				$partstr1=	str_replace('ALTER TABLE','',$partstr0);
				$partstr1=	trim(str_replace('`','',$partstr1));
				$partpos=strpos( $partstr1," ");
				//echo $partstr1;
				$partstr11=trim(substr($partstr1,0,$partpos));
				$partstr1=	trim(str_replace($partstr11,'',$partstr1));
				$partstr1=	trim(str_replace('ADD','',$partstr1));
				$partpos=strpos( $partstr1," ");
				$partstr12=trim(substr($partstr1,0,$partpos));
				$tableName=$partstr11;
				$fieldName=$partstr12;
				//echo $tableName."   ".$fieldName."<br/>".$partstr0."<br/>";
				if($openck->checkfield($tableName,$fieldName,$partstr0))	echo $openck->ls_msg;

			}
			else
			{
				if( strpos(trim($partstr0), "#")!==0 && trim($partstr0)!="" && !(stripos(trim($partstr0), "INSERT INTO")!==false && stripos(trim($partstr0), "FROM")!==false) ) //不是注释 和空行, 忽略 insert into  from
				{
					$qureyTBSQL=$partstr0;
					$openck->qureytable($qureyTBSQL);		//执行一般query操作

				}
				else
				{
				}
			}
		}
		
	}
	fclose ($handle);
	return "数据库文件 $filename 已更新。";		
	}
	else
	{
			return "文件 $filename 不存在。";		
	}
	

}



/*
更新文件
*/
function updatefiles($sourceFolder ="")
{
	$filename="../update/".$sourceFolder."/updatefiles.txt"; 
	
	if( file_exists($filename))
	{
		$handle = fopen($filename,"r");
		$modifyLog="";
		$partstr0="";
		while (!feof ($handle)) 
		{
			$partstr0 = trim( fgets ($handle, 6000));
			//echo ($partstr0);
			if( strpos($partstr0,"#file")!==0)  //不是注释
			{
				if( strpos($partstr0, "#")!==0  && $partstr0!="") //不是注释 和空行
				{
					if( strpos($partstr0,"--")===0)  //文件夹
					{
						//$desFolder ="/ls/".substr($partstr0,2,(strlen($partstr0)-2));
						$desFolder ="../".substr($partstr0,2,(strlen($partstr0)-2));
					
					}
					else
					{
					
						if( strpos($partstr0, "//")===false && strpos($partstr0, ".")>0  )  //文件
						{

							$updateFile=$partstr0;
							$folderUpdateFile="../update/".$sourceFolder."/".$updateFile;
							$desfolderUpdateFile=$desFolder."/".$updateFile;
							copy($folderUpdateFile,$desfolderUpdateFile);
							echo $folderUpdateFile."   ".$desfolderUpdateFile."<br/>";
						}
						else	//是注释
						{
							if(strpos($partstr0, "//")===0 )
							{
								$addlog=substr($partstr0,2,(strlen($partstr0)-2));
								$addlog=str_ireplace ("add","增加文件：".$updateFile."，",$addlog);
								$addlog=str_ireplace ("mod","功能调整：".$updateFile."，",$addlog);
								$modifyLog.=$addlog."<br/>";
							}
							else  //错误语句
							{
								echo " Maybe "	.$partstr0." is error <br/>";
							}
						}
								
					}
				}

			}
		
		} //while (!feof ($handle)) 
		fclose ($handle);	
		echo $modifyLog;
		//return "新文件 $filename 已更新。";	
	}
	else
	{
		return "文件 $filename 不存在。";		
	}


}

?>