<!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">
<?php
	//同步ftp上传更新的英文站文件到，其他语种，主要是 变化的程序，新增，本次服务追踪开始时间，或上次上传时间后 修改上传: 文件、文件夹时间大于 数据库上次上传时间
	session_start();
	$getpasshost="192.168.2.78";
	
	if(strpos($_SERVER['HTTP_REFERER'],$getpasshost)===false) die("from error"); //访问来源错误
	include("inc/ftp.php");
	ini_set('date.timezone','Asia/Shanghai');
/*	$config=array('ftp_ip'=>"slatestones.cn",
	'ftp_port'=>"21",
	'ftp_username'=>"slatestonesftp@slatestones.cn",
	'ftp_password'=>"T2kll&-lBm]N",
	'ftp_pasv'=>"");
	*/
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/ls_inccode.php');
	
	$opendb=new mydb();
	require_once('inc/char_set.php');
	
	$ls=$_GET['ls'];
	 

	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	$getpassusername="root";
	$getpasspassword =base64_decode('bHM4MTZzaGVuaWFu');
	$getpassdbname="getpass";
	
	$conn_gp=@mysql_connect($getpasshost,$getpassusername, $getpasspassword ) or die ("can't connect DBS getpass: ".$getpasshost.",".$getpassusername.",".$getpassdbname.",".$getpasspassword);
	mysql_select_db($getpassdbname,$conn_gp);
	mysql_query("SET character_set_client = utf8",$conn_gp);
	mysql_query("SET character_set_results = utf8",$conn_gp);
	mysql_query("SET character_set_connection = utf8",$conn_gp);
	$select_sql2="select * from ls_acc where domain='".$ls."' and  accType='虚拟主机' order  by date DESC ";
	//echo $select_sql2;
	$select_query2=mysql_query($select_sql2,$conn_gp);
	$rows=mysql_fetch_assoc($select_query2); //
	
	$select_sql3="select * from ls_traceservice where domain='".$ls."'   order  by ID DESC ";
	//echo $select_sql2;
	$select_query3=mysql_query($select_sql3,$conn_gp);
	$rows3=mysql_fetch_assoc($select_query3); // $rows3['contractStartDate']

	$ifhtdocs= (  strpos($rows['ip'],"pm")===false && strpos($rows['ip'],"lanscend.cn")===false );// ali 及其他 htdocs, SOFTLAYER非独立机 直接 根
	$rootfolder=(($ifhtdocs )?"/htdocs/":""); //ftp 根目录
	$thisurl=strtolower( trim($rows['url']));
	$thisurl=str_replace("https://","",str_replace("http://","",$thisurl));
 
	$thishost= ($thisurl !=""?  substr($thisurl,0,(strpos($thisurl,"/")===false?100:strpos($thisurl,"/"))) :  trim($rows['domain']) ); // 有临时网址用临时网址，没有用域名
	
	$config=array('ftp_ip'=>$thishost,
	'ftp_port'=>"21",
	'ftp_username'=>$rows['username'],
	'ftp_password'=>base64_decode("NzE=").$rows['password'],
	'ftp_pasv'=>"");
	$scrDir="../../".$ls;
	$localscrDir=$scrDir;

	$bakfolder=$rootfolder.($rootfolder==""?"/bak":"bak");
	$select_sql2="select * from ls_ftplog where ip='".$config['ftp_ip']."' order  by date DESC ";
	//echo $select_sql2;
	$select_query2=$opendb->query($select_sql2);
	$select_rows2=mysql_fetch_assoc($select_query2); //
	$lasttime=strtotime($select_rows2['date']);
	$lasttime3=strtotime($rows3['contractStartDate']); //服务追踪开始时间
	if($lasttime3>$lasttime) $lasttime=$lasttime3;
	$lasttime=($lasttime==0?strtotime('2019-04-01'):$lasttime); //上次更新时间
	
	echo $lasttime."--bakfolder=".$bakfolder."<br/>"."scrDir=".$scrDir."--rootfolder=".$rootfolder."--thishost=".$thishost."--bakfolder=".$bakfolder."；--".$rows['username']."--".substr($rows['password'],3,30)."<br/>" ;
	 
	$ftp = new FtpFile($config); 
	 

	$nocopyfolder=explode(",","sitemap,m,update,certificate,download,blog,management,newspage,workshop,packing,certificate,history");
	$changecopyfolder=explode(",","Templates,Templates1,bootstrap,css,images,ls_admin,generalpics,jq22css,jq22js,js,motif,revolvingpics"); //增量
	$increasecopyfolder=array();   // 变化的，增量
	$skipfile=array("conn_var.php"); // 是这些的跳过 
	$ls_admininc=explode(",","inc,css,widget,kindeditor-4.1.10\attached,images,img,js"); //增量; 
	

	 xftpincrease($scrDir, $ftp,$nocopyfolder,$rootfolder,$skipfile,$increasecopyfolder,$changecopyfolder,$ls_admininc, $bakfolder);


 	echo "<br/><br/>".$ls_msg;



	$ftp->close();
 


/* 目录树遍历,ftp文件
   
//参数说明：   
// $source:源目录名   
// $destination:目的目录名   
// $child:复制时，是不是包含的子目录
// $nocopyfolder 无需复制的文件、目录（/folder）放到数组里
// $skipfile 无需复制的文件、目录包含就跳过
// $increasecopyfolder  目录里新增的拷贝
// $changecopyfolder 目录里变化的拷贝
 */
function xftpincrease($source, $ftp,$nocopyfolder=array(), $destination,$skipfile,$increasecopyfolder,$changecopyfolder,$ls_admininc,$bakfolder)
{   
	global $lasttime,$localscrDir;
	$timestr=round((time()-1554627274)/20); //文件时间戳
	
	//$filearr="12.php.bak";
	//$ls_msg.=$ftp->uploadFile($filearr)."<br/>";
	 
	
	if(!is_dir($source)){   
	echo("Error:the $source is not a direction!<br/>");   
	return 0;   
	}   
	 include("inc/constant.php");
	// echo $transFlags;
	$handle=dir($source);   
	while($entry=$handle->read()) {   
		if(($entry!=".")&&($entry!="..")){   
			  if(is_dir($source."/".$entry)) {    //目录 
				  if(1) { //跳过不考的  
				  	
					  if(!in_array( $entry,$nocopyfolder ))  { 
//上级不是ls_admin ，上级是ls_admin，且本级在$ls_admininc $destination."/".str_replace("../","",$source)."/".$entry
					   //echo "==1==".$destination."--/".$entry."<br/>";
						//if($ftp->dir_mkdirs($destination."/".$entry)) $ls_msg.="create ".$destination."/".$entry. " ok <br/>"; ; //建目录
						// echo "local $entry : ".filemtime($source."/".$entry);
						if(  ($source=="ls_admin" && !strin($entry,$ls_admininc)) || $source!="ls_admin"  )
						{xftpincrease($source."/".$entry, $ftp ,$nocopyfolder,$destination."/".$entry,$skipfile,$increasecopyfolder,$changecopyfolder,$ls_admininc,$bakfolder);
						 // echo "local $entry : ".filemtime($source."/".$entry);
						}
					  }
				  }
			  }   
			  else
			  {    //不同复制
			   	   $thisfold=substr($source,(strrpos($source,"/")+1));
			  	  // echo $source."/-".$entry."---". $thisfold."---".$destination."/-".$entry."  <br/>";
			  		//文件//检查复制文件 
				  if( in_array($thisfold,$changecopyfolder ) && !strin($entry,$skipfile ) ){
					  		//if($ftp->dir_mkdirs($destination)) $ls_msg.="create ".$destination. " ok <br/>"; ; //建目录
					  		if( filemtime($source."/".$entry) > $lasttime )  {  //不存在目标文件，复制
							// echo $source."/".$entry."=2=". $destination."/".$entry."<br/>";
				 			   $ls_msg.=$ftp->move_file( $destination."/".$entry, $bakfolder.str_replace($localscrDir,"",$source)."/".$entry.$timestr)."<br/>"; //移动旧文件
			  					//echo  $localscrDir."====".$destination."/".$entry."====".$bakfolder.str_replace($localscrDir,"",$source)."/".$entry.$timestr."<br/>" ;
								exec(" chmod -R 755  ".$source."/".$entry);  //改权限
								if( $ftp->upafile($source."/".$entry,$destination."/".$entry ))
								  	{$ls_msg.="up1 ".$source."/".$entry." -- ". $destination."/".$entry." ok <br/>";
									  echo "local2 $entry : ".filemtime($source."/".$entry);
									}
								exec(" chmod -R 775  ".$source."/".$entry);  //恢复权限
							} 
					  }
					  else { //增量复制
						  if( strin($source,$increasecopyfolder )   ){ 
							  if( filemtime($source."/".$entry) > $lasttime )  { //判断文件存在
							  	 $ls_msg.=$ftp->move_file( $destination."/".$entry, $bakfolder.str_replace($localscrDir,"",$source)."/".$entry.$timestr)."<br/>"; //移动旧文件

								  exec(" chmod -R 755  ".$source."/".$entry);  //改权限
								  if($ftp->upafile($source."/".$entry,$destination."/")) 
								  	{$ls_msg.="up2 ".$source."/".$entry." -- ".  $destination."/".$entry." ok <br/>";
									  echo "local3 $entry : ".filemtime($source."/".$entry);
									}
								  exec(" chmod -R 775  ".$source."/".$entry);  //恢复权限
							  }
						  }
					  }
				  
			  }   
		}   
	}   
	echo "".$ls_msg;
	return 1;   
}  
 
?>