<!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 
	// 1.把老库(dl)数据$sourcedb、文件(dl)$source，更新到新站库$targetdb，对应文件夹
	// 2.按照flll库$targetdb结构,更新老库$sourcedb结构，复制 翻译表 fast slow
	// 本文件放本地 目标站点
	session_start(); 
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	require('inc/conn_var.php');
	$nocopyfiles=true; //只加多语种，不改版
	//$nocopyfiles=false;
	
 	//$sourcedb="flll";
 	//$targetdb="flll-3";
 	$sourcedb="flll-3";  // dl
 	$targetdb="flll";
	$source="../../may-rain.comdl";  //dl 老的
	$destination="..";
	
	if($sourcedb==""||$targetdb=="") die("源或目标数据库为空！");
	//$getfield=array("Num","enable"); //未使用
	//$getall=true; // 同步全部
	$getdata=explode(",", "ls_branchs,ls_certificate,ls_download,ls_functioncates,ls_links,ls_management,ls_news,ls_packing,ls_products,ls_propic,ls_prospec,ls_qualitycontrol,ls_rd,ls_revolvingpics,ls_singlepages,ls_users,ls_watermark,ls_workshop,ls_shop,ls_instruction,ls_tips,ls_transfast,ls_transslow"); // 需要同步的数据表
	if($nocopyfiles)$getdata=explode(",", "ls_tips,ls_transfast,ls_transslow"); 
	
	$ifclear=true;
	$noclear=explode(","," ls_tips,ls_transfast,ls_transslow"); //no use
					 
	$ls_msg="";
	$opendb=new mydb($sourcedb);
	require_once('inc/char_set.php');
	$syndb=trim($_POST['syndb']); // 测试转移的数据

	if($syndb=="确定"){
	
	 		//echo  $host."--".$username."--".$password;
		
			$targetconn=@mysql_connect($host,$username,$password) or die ("can't connect DBS syndb");
			$targetsldb=mysql_select_db($targetdb,$targetconn) or die ("syndb select $targetdb error");
		
			$sourceconn=@mysql_connect($host,$username,$password) or die ("can't connect DBS syndb");
			$sourcesldb=mysql_select_db($sourcedb,$sourceconn) or die ("syndb select $sourcedb error");
			
			$sqlflll="show table status from  `".$targetdb."`";
			$flllquery=@mysql_query($sqlflll,$targetconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$sqlflll);
			while($rowstable=mysql_fetch_assoc($flllquery))
		  	{				
				$tablename=trim($rowstable['Name']);
				if($tablename=="")continue;
				//echo  $tablename."<br/> ";
				//$sql="show tables like '".$tablename."' ";
				$sql="select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='".$sourcedb."' and TABLE_NAME='".$tablename."' " ;
				$ifquery=@mysql_query($sql,$sourceconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$sql);
				//echo   $tablename."==".mysql_num_rows($ifquery)."=="."<br/> ";
				if(mysql_num_rows($ifquery)!=1)  // 不存在表，创建，添加数据
				{				
					$addsql="create table `".$sourcedb."`.".$tablename." like `".$targetdb."`.".$tablename."  ";
					$query3=@mysql_query($addsql,$sourceconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$addsql);
					if($query3)
					{ 
						$ls_msg.="表 ".$tablename." 已添加<br/> "; 
/*						if( strin($tablename,$getdata) )
						{*/
							$adddata="insert into  `".$sourcedb."`.".$tablename." SELECT * FROM `".$targetdb."`.".$tablename."  ";
							$query4=@mysql_query($adddata,$sourceconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$adddata);
							if($query4) $ls_msg.="  ".$tablename." 数据已复制<br/> "; 
						//}
					}
					else
					{ $ls_msg.="表 ".$tablename." 添加失败<br/> ";}
	
				}
				else  // 存在表，添加字段，添加数据
				{
					  //echo  "<br/><br/><br/>".$tablename."<br/> ";
					  $sqlcolumns   ="show columns in  `".$targetdb."`.".$tablename." "; 
					  $querycolumns   = @mysql_query($sqlcolumns,$targetconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$sqlcolumns);
					  //$opendb->query($sqlcolumns); 
					  
					  echo "`".$targetdb."`.".$tablename."<br/>";
					  while($rowscolumns=mysql_fetch_assoc($querycolumns))
		  				{		
							//echo $fieldname."<br/>";
							$fieldname=$rowscolumns['Field'];
							$default=$rowscolumns['Default']!=""?" DEFAULT ".$rowscolumns['Default']:" ";
							$comment=$rowscolumns['Comment']!=""?" COMMENT ".$rowscolumns['Comment']:" ";
							$fllltype=$rowscolumns['Type'];
							
							  
							 if($fieldname=="")continue;
							 //echo  $tablename."==".$fieldname."<br/> ".$default.$comment;
							 $sqlcolumns11   ="show columns in  `".$sourcedb."`.".$tablename." ";  
							//$query11   = @mysql_query($sqlcolumns11,$targetconn);  //   
					  		 $querycolumns11   = @mysql_query($sqlcolumns11,$sourceconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$sqlcolumns11);
							 $hassame=false;
					 		 while($rowscolumns11=mysql_fetch_assoc($querycolumns11))
		  					{		
								$fieldname11=$rowscolumns11['Field'];
							 	if($fieldname11=="")continue;
								//echo $sourcedb."`".$tablename."`".$fieldname11."<br/>";
								if($fieldname==$fieldname11) //有相同字段
								{
									$hassame=true;
									$altsql="";
									if($rowscolumns['Type']!=$rowscolumns11['Type']) //判断类型，按最新改，11是sourcedb
									{
										//echo $fieldname.":". $rowscolumns['Type']."==".$rowscolumns11['Type']."<br/><br/><br/> ";

										if(!(strpos($rowscolumns['Type']."varchar")===false) && !(strpos($rowscolumns11['Type']."varchar")===false)  ) $altsql="alter table `".$sourcedb."`.".$tablename." modify column ".$fieldname11." ".$rowscolumns['Type']." ".$default.$comment; // 都是varchar 按目标库长度
										
										if(!(strpos($rowscolumns['Type']."int")===false) &&!(strpos($rowscolumns11['Type']."int")===false)  ) $altsql="alter table `".$sourcedb."`.".$tablename." modify column ".$fieldname11." ".$rowscolumns['Type']." ".$default.$comment;// 都是int 按目标库长度
										
										if(!(strpos($rowscolumns['Type']."varchar")===false) &&!(strpos($rowscolumns11['Type']."text")===false)  ) $altsql="alter table `".$sourcedb."`.".$tablename." modify column ".$fieldname11." ".$rowscolumns['Type']." ".$default.$comment; // 目标text，源是varchar 按目标库类型
										
										if(!(strpos($rowscolumns['Type']."text")===false) &&!(strpos($rowscolumns11['Type']."text")===false)  ) $altsql="alter table `".$sourcedb."`.".$tablename." modify column ".$fieldname11." ".$rowscolumns['Type']." ".$default.$comment;    // 目标text，源是text 按目标库text类型
										
										
										if($altsql!="")
										{
											//echo $altsql;
											if( @mysql_query($altsql,$sourceconn)) //  按照 rowscolumns 修改
				 								$ls_msg.=  "<br/> $tablename : $fieldname11 ".$rowscolumns['Type']." 修改 ";
											else
				 								$ls_msg.= "<br/>  $tablename : $fieldname11 ".$rowscolumns['Type']." 修改失败 ";																	                                        } 
												
										//目标字段类型是最新的。不更新
										$ls_msg.=  "<br/> $tablename : $fieldname11 ".$rowscolumns['Type']." 类型不一致 ";
									}
									//echo $ls_msg;
								}
							} //while($rowscolumns11=mysql_fetch_assoc($query11))
							if(!$hassame){  //同表名，目标没有这个字段 不添加
								$addfield="alter table  `".$sourcedb."`.".$tablename." add ".$fieldname." ".$fllltype."  ".$default.$comment;
								if( mysql_query($addfield,$sourceconn))  //  
										$ls_msg.= "<br/>  $tablename :  增加字段 ".$fieldname."成功, "; 
								else
										$ls_msg.= "<br/>  $tablename :  增加字段 ".$fieldname." 失败, "; 
							} //没有相同字段，增加
							
						}
						$sqlls_admin   ="update  `".$sourcedb."`.ls_admin set branchID=1 where 1 ";  
						//$query11   = @mysql_query($sqlcolumns11,$targetconn);  //   
					  	$querycolumns11   = @mysql_query($sqlls_admin,$sourceconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$sqlcolumns11); // 清理部门id

/*					  if( strin($tablename,$getdata) )  // 复制数据
					  {
						  if($ifclear) {
						 	 $deletedata="delete  from  `".$targetdb."`.".$tablename." where 1 ";
						 	 $query4=@mysql_query($deletedata,$targetconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$adddata);
							  
						  }
						  $adddata="insert into  `".$targetdb."`.".$tablename." SELECT * FROM `".$sourcedb."`.".$tablename."  ";
						  $query4=@mysql_query($adddata,$targetconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$adddata);
						  if($query4) $ls_msg.="  ".$tablename." 数据已复制<br/> "; 
					  }*/

				}
				 
			}
			
			if(trim($ls_msg)=="")$ls_msg="没有添加任何表、字段"; 
			else
			{
				
				$sqlflll2="UPDATE ls_config SET  siteKeywords = REPLACE(REPLACE(siteKeywords, CHAR(10), ''), CHAR(13), '')";
				$flllquery=@mysql_query($sqlflll2,$targetconn) or die ("\n syndb - Query Error: \n   ".mysql_error()."   \n".$sqlflll); //清除0D 0A 避免编辑器 菜单出不来

				if(!$nocopyfiles)$ls_msg.="把数据库 $sourcedb 改成 $targetdb 的名字"; 	
			}
	} // 	if($syndb=="确定"){

if(!$nocopyfiles){ //复制文件
	 
	 $nocopyext=".html";
	 $nocopyFile=explode(",","/Templates,/Templates1,/bootstrap,/css,/images,/ls_admin,/generalpics,/jq22css,/jq22js,/js,/motif,/revolvingpics,/m,/mail,/sitemap,/update,sendmail_inc.php,search.php,sitemap.xml,chlicktotal.php,.htaccess,select.php,sendmail_inc_ml.php,sendmail.php,robots.txt");
	 xCopy($source, $destination,$nocopyFile,true,$nocopyext);
	 
	 $source=$source."/ls_admin/kindeditor-4.1.10/attached";  //复制上传文件
	 if(is_dir( $source)){
		 $destination="kindeditor-4.1.10/attached";
		 //xCopy($source, $destination);
	 }
	
	 $source=$source."/ls_admin/uploadfiles";  //复制上传文件
	 if(is_dir( $source)){
		 $destination="uploadfiles";
		// xCopy($source, $destination);
	 }
}
?>
<title>更新数据库结构</title>
</head>
<body>
<table width="60%%" border="0" align="center">
  <tr>
    <td height="130" align="center">
      同步数据库从：  <?=$sourcedb." 到 ： ".$targetdb."".(  " 同步部分数据 getdata " )?></td>
  </tr>
  <tr>
    <td height="44" align="left"><?= $ls_msg?></td>
  </tr>
  <tr>
    <td align="center"><p>&nbsp;</p>
      <form id="adfastmanual" name="adfastmanual" method="post" enctype="multipart/form-data"  action="<?php echo $PHP_SELF;?>">
        <button name="syndb" type="submit" value="确定" onclick=" "/>确定</button>    
        
        <button name="test" type="reset" value="重置" onclick=" "/>重置</button>   
         
      </form>
    <p>&nbsp; </p></td>
  </tr>
</table>
<br/>
</body>
</html>