<?php
/*
分离3种网站套餐 当前目录分离ls-4fix beta， 数据库 来源 conn_var
更新： 1清理文件，2载入模版，写入 数据库参数，清理新闻， 改下 conn_var
*/
	session_start(); 
	
	require_once('../../ru/ls_admin/inc/conn.php');
	require_once('../../ru/ls_admin/inc/constant.php');
	require_once('../../ru/ls_admin/inc/ls_syscode.php');
	require_once('../../ru/ls_admin/inc/ls_inccode.php');
	require_once('../../ru/ls_admin/inc/check.php');
	require_once('../../ru/ls_admin/inc/conn_var.php');
	$opendb=new mydb();
	require_once('../../ru/ls_admin/inc/char_set.php'); //不能少，否则backuplsdb无输出
	
	//$directory="../../ls_4fixsn";
	$directory="../../ls-4fix beta";
	//$getpasshost="192.168.2.90";	//getpass
	$getpasshost="localhost";	//getpass
	$getpassusername="root";
	$getpasspassword="bHM4MTZzaGVuaWFu";
	$getpassdbname="getpass";
	$outputDir_BBB='';

	$directoryRoot=$directory;
	//$localhost=$_POST['localhost']==''?$initCreatTypeHost:$_POST['localhost'];  //默认 本地服务器
	$newdbname =$_POST['newdbname'];
	
	$vhdbname =$_POST['vhdbname'];
	$vhdbusername=$_POST['vhdbusername'];
	$vhdbpassword=$_POST['vhdbpassword'];
	$vhdbhost=$_POST['vhdbhost']==''?'localhost':$_POST['vhdbhost'];  //默认 localhost

	if($initCreatTypeHost!=$_SERVER["SERVER_ADDR"]) 
	{
		echo "只能在本地服务器运行，程序已终止";
		exit();
	}

				
	$domain=trim($_POST['domain']);
	$vertype=$_POST['vertype'];
	$company=trim($_POST['company']);
	$ls_msg=trim($_GET['ls_msg']);
	$outputDir="../../".str_replace("www.","",$domain);
	$outputDir_BBB=$outputDir."_BBB";
	if($domain!="" && $domain!=".com" && $domain!=".net" && $domain!=".cn" && $domain!=".com.cn" && $domain!=".org" )  //如果设置了域名，则
	{
	if(!file_exists($outputDir_BBB))
	{

		if(file_exists($outputDir))
		{
			rename($outputDir,$outputDir_BBB ) ;
			
		}
			if( isset($_POST['newdbname']))
			{
			echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
			echo "<link type=\"text/css\" rel=\"stylesheet\" href=\"../ls_admin/css/admin.css\" />";

			$thishost=$host;	//来源 conn_var
			$thisusername=$username;	//来源 conn_var
			$thispassword=$password;	//来源 conn_var
			$thisdbname=$dbname;	//来源 conn_var
			$thisbackupSQLFile="../ls_admin/backupdb/backup_crtypedb.sql";
			backuplsdb($thishost,$thisusername,$thispassword,$thisdbname,$thisbackupSQLFile);
			
			$restoredb =$_POST['newdbname'];
			$backupSQLFile=$thisbackupSQLFile;
			restorelsdb($host,$username,$password,$restoredb,$backupSQLFile,$maxLenSQLStr);  //创建数据库
		
			//清除news等数据 
			$newconn=@mysql_connect($host,$username,$password) or die ("can't connect DBS");
			$newdbselect=mysql_select_db($newdbname,$newconn);
			
			$modi_sql="delete from ls_news where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_qualitycontrol where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_prospec where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_propic where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_products where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_packing where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_management where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_rd where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_select_id where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_singlepages where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
 			$modi_sql="delete from ls_users where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_workshop where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_singlepages where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_log where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_links where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_fetchpass where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_download where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_certificate where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
			
			$modi_sql="delete from ls_brand where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
 
			$modi_sql="delete from ls_article  where 1 ";
			$result= mysql_query($modi_sql,$newdbname);

			$modi_sql="delete from ls_functioncates  where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
		
			$modi_sql="delete from ls_agentcates   where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
		
			$modi_sql="delete from ls_agents   where 1 ";
			$result= mysql_query($modi_sql,$newdbname);
		
		
 
			mkdir($outputDir);
			//echo $outputDir."ss";


			//更新ls_menu.php数据
			$menufile=$outputDir."/ls_admin/menu.php";
			$tagBegin="//tag-vertype\r\n";
			$tagEnd="//tag-vertypeend\r\n";

			switch ($vertype)
			{
			case "1":	//宣传展示型
			$nocopyfiles=       array("Templates1/packing.html","Templates2/packing.html","Templates3/packing.html","Templates4/packing.html","Templates1/quality.html","Templates2/quality.html","Templates3/quality.html","Templates4/quality.html","Templates1/rd.html","Templates2/rd.html","Templates3/rd.html","Templates4/rd.html","Templates1/download.html","Templates2/download.html","Templates3/download.html","Templates4/download.html","Templates1/brand.html","Templates2/brand.html","Templates3/brand.html","Templates4/brand.html","Templates1/management.html","Templates2/management.html","Templates3/management.html","Templates4/management.html","Templates1/certificate.html","Templates2/certificate.html","Templates3/certificate.html","Templates4/certificate.html"); 
			$nocopyfiles[]=array_push($nocopyfiles,"ls_mantain/getupdate.php","ls_mantain/createvertype.php.php","publish");
			$nocopyfiles[]=array_push($nocopyfiles,"ls_admin/widget/ls_config_template.php","update/ftpupdatefiles.php");
			echo "正在生成，请稍后...<br/>";
			 treecopyfiles($directory,$outputDir,$nocopyfiles) ;
			 $replaceStr="	\$vertype=1;\r\n	";
			 writeTag($menufile,$tagBegin,$tagEnd,$replaceStr);
	
			$ls_msg="所选类型已经创建， 请从 $outputDir 复制";
			break;

			case "2": 	//优化更新型
			
			$nocopyfiles=       array("Templates1/brand.html","Templates2/brand.html","Templates3/brand.html","Templates4/brand.html","Templates1/management.html","Templates2/management.html","Templates3/management.html","Templates4/management.html"); 
			$nocopyfiles[]=array_push($nocopyfiles,"ls_mantain/getupdate.php","ls_mantain/createvertype.php.php","publish");  
			$nocopyfiles[]=array_push($nocopyfiles,"ls_admin/widget/ls_config_template.php","update/ftpupdatefiles.php");	//widget
			echo "正在生成，请稍后...<br/>";

			 treecopyfiles($directory,$outputDir,$nocopyfiles) ;
			 $replaceStr="	\$vertype=2;\r\n	";
			 writeTag($menufile,$tagBegin,$tagEnd,$replaceStr);
	
			$ls_msg="所选类型已经创建， 请从 $outputDir 复制";
				
			//menu
			
			break;
			
	 		case "3":	//推广营销型
			$nocopyfiles=       array(); 	//tempplate
			$nocopyfiles[]=array_push($nocopyfiles,"ls_mantain/getupdate.php","ls_mantain/createvertype.php.php","publish");	
			$nocopyfiles[]=array_push($nocopyfiles,"ls_admin/widget/ls_config_template.php","update/ftpupdatefiles.php");	//widget

			 treecopyfiles($directory,$outputDir,$nocopyfiles) ;
			 $replaceStr="	\$vertype=3;\r\n	";
			 writeTag($menufile,$tagBegin,$tagEnd,$replaceStr);

			 break; 
			
			case "4":	//集团公司网站
			$nocopyfiles=       array(); 
			$nocopyfiles[]=array_push($nocopyfiles,"ls_mantain/getupdate.php","ls_mantain/createvertype.php.php","publish");
			$nocopyfiles[]=array_push($nocopyfiles,"ls_admin/widget/ls_config_template.php","update/ftpupdatefiles.php");

			treecopyfiles($directory,$outputDir,$nocopyfiles) ;
			 $replaceStr="	\$vertype=4;\r\n	";
			 writeTag($menufile,$tagBegin,$tagEnd,$replaceStr);
			$ls_msg="所选类型已经创建， 请从 $outputDir 复制";
			break;
			

			}
			
			//更新conn_var数据 
			$conn_getpass=@mysql_connect($getpasshost,$getpassusername,base64_decode($getpasspassword)) or die ("can't connect DBS ".$getpasshost.",".$getpassusername.base64_decode($getpasspassword));
			mysql_select_db($getpassdbname,$conn_getpass);
			$sql_getpass="select * from ls_acc where accType='mysql数据库' and domain='".$domain."' order by date DESC   ";
			$result= mysql_query($sql_getpass,$conn_getpass);
			$row_getpass=mysql_fetch_assoc($result);
			//echo print_r($row_getpass).$row_getpass['username'];
			
			$getuser=$row_getpass['user'];
			if( $getuser!='')
			{
				$sql_getuser="select * from ls_users  where userRealName='".$getuser."'   ";
				$result= mysql_query($sql_getuser,$conn_getpass);
				$row_getuser=mysql_fetch_assoc($result);
			} 
			//更新ls_config数据 
			$modi_sql="update ls_config set companyNameCn =  '".($row_getuser['userCompany']!=''?$row_getuser['userCompany']:$_POST['company'])."'  ,website  =  '".$_POST['domain']."' ,tel   =  '".$row_getuser['userTel']."',fax =  '".$row_getuser['userFax']."',email   =  '".$row_getuser['userEmail']."',feedbackEmail   =  '".$row_getuser['userEmail']."',MSN  =  '".$row_getuser['userMSN']."',skype   =  '".$row_getuser['userSkype']."' where ID=1";
			mysql_select_db($restoredb,$conn);
			$result= mysql_query($modi_sql,$conn);
			$conn_str="<?php 
			if('192.168.2.20'==\$_SERVER['SERVER_ADDR'] || '192.168.2.90'==\$_SERVER['SERVER_ADDR']){
			\$host='localhost';
			\$dbname='".$restoredb."';
			\$username='".$thisusername."';
			\$password='".$thispassword."';
		}else{
			\$host='localhost';
			\$dbname='".$row_getpass['ip']."';
			\$username='".$row_getpass['username']."';
			\$password='".$row_getpass['password']."';
		}		
		?>			";
		//echo $conn_str;
			$filename=$outputDir."/ls_admin/inc/conn_var.php";
			writegeneralfile($filename,$conn_str);
			if($row_getpass['username']==''){$ls_msg="<br/>数据库用户名为空，可能业务管理getpass没有".$domain."数据库账号";}
		}
		else
		{
			
			$ls_msg="数据库名设置出错：".$_POST['dbname']."，请重新设置";
		}
		if(file_exists($outputDir_BBB))//拷贝模版
		{
			$nocopyfiles=array();
			
			if(file_exists($outputDir_BBB."/Templates")) recurse_copy($outputDir_BBB."/Templates",$outputDir."/Templates"); //拷贝模版
			if(file_exists($outputDir_BBB."/Templates1")) recurse_copy($outputDir_BBB."/Templates1",$outputDir."/Templates1"); //
			if(file_exists($outputDir_BBB."/Templates2")) recurse_copy($outputDir_BBB."/Templates2",$outputDir."/Templates2"); //
			if(file_exists($outputDir_BBB."/Templates3")) recurse_copy($outputDir_BBB."/Templates3",$outputDir."/Templates3"); //
			if(file_exists($outputDir_BBB."/Templates4")) recurse_copy($outputDir_BBB."/Templates4",$outputDir."/Templates4"); //
		
			$ls_msg.=$outputDir."<br/>已经存在,已改成".$outputDir_BBB.",并将其下摸板文件拷入当前网站目录。";
		}
	   echo "<script>location='createvertype.php?ls_msg=".$ls_msg."';</script>";           

	  }
	  else
	  {
		  $ls_msg.=$outputDir_BBB."已经存在,请删除后再生成。";
	  }

	}//if($domain

	
	/* 目录树遍历, 创建新版本 */
function treecopyfiles($directory,$outputDir,$nocopyfiles ) 
{ 
	global $directoryRoot;
	$mydir=dir($directory); 
	while($file=$mydir->read())
	{ 
		//echo $file;
		if(($file!=".") AND ($file!=".."))
		{ 
			if((is_dir("$directory/$file")) )
			{
				treecopyfiles("$directory/$file",$outputDir,$nocopyfiles ); //递归查询目录
			}
			else
			{ 
				$wrdirectory=str_replace($directoryRoot,"","$directory");
				$wrdirectoryfile=substr("$wrdirectory/$file",1,strlen("$wrdirectory/$file"));
				if(!in_array("$wrdirectoryfile",$nocopyfiles))
				{
					//echo "$wrdirectoryfile"."----";
					if((strpos(iconv('GB2312', 'UTF-8', $file),"拷贝")===false) )
					//已更新目录下的已更新文件
					{ 
							if(!file_exists("$outputDir$wrdirectory")){crmkdir("$outputDir$wrdirectory");}  //
							//echo "$outputDir$wrdirectory/$file"."--";
							copy("$directory/$file","$outputDir$wrdirectory/$file");  //拷贝文件
					
					}
				}
			} 
			
		}
	} 
	$mydir->close(); 
	return ;
	
} 

	
	


?>
<!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="../../ru/ls_admin/css/admin.css" rel="stylesheet" type="text/css">
<title>无标题文档</title>
<script type="text/javascript" src="../../ru/js/jquery-1.4.2.min.js"></script>
<script language="JavaScript"  >
var getvars;

function getjson(form1,var1) {
$.get('../ls_admin/inc/getjson.php',{table:'ls_acc',sqlstr:"select * from ls_acc where accType='mysql数据库' and domain like '%"+var1+"%' order by date DESC  ",iftext:''},function(data){
		var getvars =  data  ; 
		alert(getvars.myjson[0].ip);
		form1.vhdbname.value=getvars.myjson[0].ip;
		form1.vhdbusername.value=getvars.myjson[0].username;
		form1.vhpassword.value=getvars.myjson[0].password;
		form1.vhdbname.value=getvars.myjson[0].url.indexOf("http") =-1|| getvars.myjson[0].url==''?'localhost':getvars.myjson[0].url;
		
	  }); 
}


</script>


</head>

<body>
<table width="96%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#6396D6">
  <tr> 
	<td height="18" background="../../ru/ls_admin/images/admin_bg.gif"><div align="center">创建套餐</div></td>
  </tr>

  <tr>
  	<td bgcolor="#E8F1FF" valign="top" align="center">
	<br>
		<form name="form1" method="post" action="../../ru/ls_mantain/?ls=eidt">
		<table width="371" border="0">
			<tr>
			  <td width="493" align="center"><table width="500" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
			    <tr bgcolor="#B9DAFF">
			      <td height="28" colspan="2" align="center">套餐相关信息</td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td width="30%" height="28" align="left" bgcolor="#E1EEFF"  style='padding-left:50px;'><a href="../../ru/ls_mantain/?vertype=1">宣传展示型</a></td>
			      <td width="30%" align="center" bgcolor="#E1EEFF"><span style="padding-left:50px;"><a href="../../ru/ls_mantain/?vertype=1">
			        <input type="radio" name="vertype" id="radio" value="1" />
                  </a></span></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="left" bgcolor="#E1EEFF"  style='padding-left:50px;'><a href="../../ru/ls_mantain/?vertype=2">优化更新型</a></td>
			      <td align="center" bgcolor="#E1EEFF"><span style="padding-left:50px;"><a href="../../ru/ls_mantain/?vertype=1">
			        <input name="vertype" type="radio" id="radio2" value="2" checked="checked" />
			      </a></span></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="left" bgcolor="#E1EEFF"  style='padding-left:50px;'><a href="../../ru/ls_mantain/?vertype=3">推广营销型</a></td>
			      <td align="center" bgcolor="#E1EEFF"><span style="padding-left:50px;"><a href="../../ru/ls_mantain/?vertype=1">
			        <input type="radio" name="vertype" id="radio3" value="3" />
			      </a></span></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="left" bgcolor="#E1EEFF"  style='padding-left:50px;'><a href="../../ru/ls_mantain/?vertype=1"> </a><a href="../../ru/ls_mantain/?vertype=4">集团公司网站</a></td>
			      <td align="center" bgcolor="#E1EEFF"><span style="padding-left:50px;"><a href="../../ru/ls_mantain/?vertype=1">
			        <input type="radio" name="vertype" id="radio4" value="4" />
			      </a></span></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="left" bgcolor="#E1EEFF"  style='padding-left:50px;'>域名 </td>
			      <td align="center" bgcolor="#E1EEFF"><input name="domain2" type="text" id="domain2" maxlength="48"  value="<?=($domain!=''?$domain:'.com')?>"  onblur="this.form.newdbname.value=this.value.replace('.com','').replace('www.','');getjson(this.form,this.value)"/></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="center" bgcolor="#E1EEFF"  style='padding-left:50px;'><div align="left">本地数据库名</div></td>
			      <td align="center" bgcolor="#E1EEFF"><input name="newdbname" type="text" id="newdbname" value="<?=$newdbname?>" /></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="left" bgcolor="#E1EEFF"  style='padding-left:50px;'>公司</td>
			      <td align="center" bgcolor="#E1EEFF"  ><input name="company" type="text" id="company" maxlength="48" /></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" colspan="2" align="center" bgcolor="#E1EEFF"  style='padding-left:50px;'>远程虚拟主机数据库参数</td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="center" bgcolor="#E1EEFF"  style='padding-left:50px;'><div align="left">数据库名</div></td>
			      <td align="center" bgcolor="#E1EEFF"><input name="vhdbname" type="text" id="vhdbname" value="<?=$vhdbname?>" /></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="center" bgcolor="#E1EEFF"  style='padding-left:50px;'><div align="left">用户名</div></td>
			      <td align="center" bgcolor="#E1EEFF"><input name="vhdbusername" type="text" id="vhdbusername" value="<?=$vhusername?>" /></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="center" bgcolor="#E1EEFF" style='padding-left:50px;'><div align="left">密码</div></td>
			      <td align="center" bgcolor="#E1EEFF"><input name="vhpassword" type="text" id="vhpassword" value="<?=$vhpassword?>" /></td>
		        </tr>
			    <tr bgcolor="#E8F2FF">
			      <td height="28" align="center" bgcolor="#E1EEFF" style='padding-left:50px;'><div align="left">服务器</div></td>
			      <td align="center" bgcolor="#E1EEFF"><input name="vhdbhost" type="text" id="vhdbhost" value="<?=$vhdbhost?>" /></td>
		        </tr>
		      </table></td>
		  </tr>
			<tr>
			  <td align="center"><br>
			    <label>
			      <input type="submit" name="button" id="button" value="提交" />
		        </label>
			    <label>
			      <input type="reset" name="button2" id="button2" value="重置" />
		        </label>
			    <br />
			    <br>
		  <font color="#FF0000"><?php echo $ls_msg; ?></font>
		  </td>
			</tr>
		</table>
		
      </form>
		<br>
	</td>
 </tr>
</table>

</body>
</html>
