<?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-4fix beta";
	$directoryRoot=$directory;
	
	$domain=trim($_POST['domain']);
	$vertype=$_POST['vertype'];
	$company=trim($_POST['company']);
	$ls_msg=trim($_GET['ls_msg']);
	

	if($domain!="" && $domain!=".com" && $domain!=".net" && $domain!=".cn" && $domain!=".com.cn" && $domain!=".org" )  //如果设置了域名，则
	{

		$outputDir="../../".str_replace("www.","",$domain);
		if(!file_exists($outputDir))
		{
			if( isset($_POST['dbname']))
			{
			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;
			$thispassword=$password;
			$thisdbname=$dbname;
			$thisbackupSQLFile="../ls_admin/backupdb/backup_crtypedb.sql";
			backuplsdb($thishost,$thisusername,$thispassword,$thisdbname,$thisbackupSQLFile);
			
			$host=$_POST['host'];
			$restoredb =$_POST['dbname'];
			$username=$_POST['dbusername'];
			$password=$_POST['dbpassword'];
			$backupSQLFile=$thisbackupSQLFile;
			restorelsdb($host,$username,$password,$restoredb,$backupSQLFile,$maxLenSQLStr);  //创建数据库
		
			//更新ls_config数据 
			$conn=@mysql_connect($host,$username,$password) or die ("can't connect DBS");
			$modi_sql="update ls_config set companyNameCn =  '".$_POST['company']."'  ,website  =  '".$_POST['domain']."'  ";
			$result= mysql_query($modi_sql,$conn);
			//清理新闻 
			//$modi_sql="update ls_config set companyNameCn =  '".$_POST['company']."'  ,website  =  '".$_POST['domain']."'  ";
			//$result= mysql_query($modi_sql,$conn);
	
			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 复制";
			$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;
			

			}
			}
			else
			{
				
				$ls_msg="数据库名设置出错：".$_POST['dbname']."，请重新设置";
			}
		}
		else  //存在生成目录
		{
			$ls_msg=$outputDir."已经存在，请删除后再生成";
		}
	    	echo "<script>location='createvertype.php?ls_msg=".$ls_msg."';</script>";           

	}
	
	/* 目录树遍历, 创建新版本 */
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>
</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 height="28" align="left" bgcolor="#E1EEFF"  style='padding-left:50px;'><a href="../../ru/ls_mantain/?vertype=1">宣传展示型</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="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="domain" type="text" id="domain" maxlength="48"  value=".com"  onblur="this.form.dbname.value=this.value.replace('.com','').replace('www.','')"/>
			      </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 width="30%" height="28" align="center" bgcolor="#E1EEFF"  style='padding-left:50px;'><div align="left">安装主机
			          <input name="L" type="button"  onclick="this.form.host.value='localhost'" value="L"/>
			      </div></td>
			      <td width="30%" align="center" bgcolor="#E1EEFF"><label>
			        <input name="host" type="text" id="textfield" value="<?=$initCreatTypeHost?>" />
			        </label></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="dbname" type="text" id="dbname" value="<?=$initDBName."_"?>" /></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="dbusername" type="text" id="textfield3" value="<?=$initDBUserName?>" /></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="dbpassword" type="text" id="textfield4" value="<?=$initDBPassword?>" /></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>
