<?php
header("Content-Type: text/html; charset=gb2312");
include("../include/config.php");
include("../include/install.php");
include("../include/function.php");

$Step=SafeRequest("action","get");
switch($Step){
	case 'step1':
		$StepNum=1;$StepInfo="阅读安装协议";
		break;
	case 'step2':
		$StepNum=2;$StepInfo="MYSQL数据库信息配置";
		break;
	case 'step3':
		$StepNum=3;$StepInfo="创建SQL数据表";
		break;
	case 'step4':
		$StepNum=4;$StepInfo="网站信息配置";
		break;
	case 'step5':
		$StepNum=5;$StepInfo="安装完成了";
		break;
	default:
		$StepNum=1;$StepInfo="阅读安装协议";
	}

?>
<!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=gb2312" />
<title>搜一次PHP版安装程序</title>
<script language="javascript" src="../js/jquery.js"></script>
<script language="javascript">
	function checkadmin(){
		var names=$("#admin_Name").val();
		var pass=$("#admin_Pass").val();
		var codes=$("#checkcode").val();
		if (names==""){
			$("#admin_Name").css("border","1px solid red");
			}
		else if (pass==""){
			$("#admin_Pass").css("border","1px solid red");
			}
		else if (codes==""){
			$("#checkcode").css("border","1px solid red");
			}
		else{
			document.form4.submit();
			}	
		}
	function checkdb(){
		var server=$("#sqlserver").val();
		var dbname=$("#sqldbname").val();
		var id=$("#sqlname").val();
		var pwd=$("#sqlpass").val();
		$("#checkinfo").html("<font color=red>&nbsp;&nbsp;连接中.....</font>");
		$.post('function.php?server='+server+'&db='+dbname+'&id='+id+'&pwd='+pwd,function(obj){
			if(obj=='ok'){
				$("#checkinfo").html("<font color=green>&nbsp;&nbsp;连接成功!</font>");
				}
			else if(obj=='dberror'){
				$("#checkinfo").html("<font color=red>&nbsp;&nbsp;连接数据库失败!</font>");
				}	
			else {
				$("#checkinfo").html("<font color=red>&nbsp;&nbsp;连接服务器失败!</font>");
				}
			});
		}	
</script>
<link type="text/css" rel="stylesheet" href="images/style.css" />
</head>

<body>
<div class="wrapA">
  <div class="wrapB">
    <div class="wrapC">
      <div id="header">
        <div class="logo left"></div>
        <div class="right"> <a href="http://www.YiCiCms.com" target="_blank" class="linkA">官方主页</a> | <a href="http://bbs.YiCiCms.com" target="_blank" class="linkA">官方论坛</a></div>
      </div>
      <div id="main">
        <h2><span class="step"><?php echo $StepNum?></span><?php echo $StepInfo?></h2>
        <div class="installInfo">欢迎来到搜一次CMS安装向导，安装前请仔细阅读安装说明！　　
          <div style="margin-top:.5em">安装过程中遇到任何问题 &nbsp;<a href="http://bbs.YiCiCms.com/" target="_blank" class="black"><u><b>请到官方讨论区寻求帮助</b></u></a></div>
        </div>
        <div class="c"></div>

<?php
$action=SafeRequest("action","get");
switch($action){
	case 'step1':
		step1();
		break;
	case 'step2':
		step2();
		break;
	case 'exestep2':
		exestep2();
		break;
	case 'step3':
		step3();
		break;
	case 'step4':
		step4();
		break;
	case 'exestep4':
		exestep4();
		break;
	case 'step5':
		step5();
		break;
	default:
		step1();
		break;
		
	}

function step1(){
	if(S_IsInstall==1){
		echo "<script>if(!confirm('你已经安装过,是否重新安装!')){window.close();}</script>";
		}
?>
        <div id="content">
          <div class="wrapD">
            <div class="wrapE">
              <div class="agreement" style="height:290px; overflow:auto"> <strong>软件使用协议</strong><br />
                <br />
				请您在使用(搜一次 Content Management System)前仔细阅读如下条款。包括免除或者限制作者责任的免责条款及对用户的权利限制。您的安装使用行为将视为对本《用户许可协议》的接受，并同意接受本《用户许可协议》各项条款的约束。 <br /><br />
				一、安装和使用： <br />搜一次CMS是免费和开源提供给您使用的，您可安装无限制数量副本。 您必须保证在不进行非法活动，不违反国家相关政策法规的前提下使用本软件。 <br /><br />
二：郑重声明： <br />
1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息。 <br />
2、搜一次网络有限公司保留此软件的法律追究权利。
<br /><br />
				三、免责声明：  <br />
				本软件并无附带任何形式的明示的或暗示的保证，包括任何关于本软件的适用性, 无侵犯知识产权或适合作某一特定用途的保证。  <br />
				在任何情况下，对于因使用本软件或无法使用本软件而导致的任何损害赔偿，作者均无须承担法律责任。作者不保证本软件所包含的资料,文字、图形、链接或其它事项的准确性或完整性。作者可随时更改本软件，无须另作通知。  <br />
				所有由用户自己制作、下载、使用的第三方信息数据和插件所引起的一切版权问题或纠纷，本软件概不承担任何责任。<br /><br />
                <B>版权所有 <font face="Arial">&nbsp;&copy; 2009-2010， YiCiCms.CoM&nbsp;&nbsp;&nbsp;搜一次CMS开发团队</font>
                保留所有权力。 </B>
              </div>
              <div class="c"></div>
            </div>
          </div>
          
          <div class="stepbt">  <a class="btA colorA next" style="cursor:pointer;" href="?action=step2"><span>同意协议，进入下一步</span></a> </div>
        </div>
 <?php
}

function step2(){
?>
<form action="?action=exestep2" method="post" name="form2">
 <div id="content">
          <div class="wrapD">
            <div class="wrapE">
              <div class="agreement" style="height:290px; overflow:auto">
              	<strong>文件权限检测：</strong>
                <p>include/config.php:<?php
				$returndown=0;
				if(is_writable("../include/config.php")){
					echo "<font color=green>ok</font>";
				}else{
					echo "<font color=red>no(请将此文件修改为777权限)</font>";
					$returndown=1;
				}?>&nbsp;include/install.php:<?php
				if(is_writable("../include/install.php")){
					echo "<font color=green>ok</font>";
				}else{
					echo "<font color=red>no(请将此文件修改为777权限)</font>";
					$returndown=1;}?></p>
				<p><?php if($returndown==1){echo '对不起，您的权限设置不正确，请设置正确后刷新本页';?>
				<script type="text/javascript">$(function(){$('#isreturndown').hide();})</script>
				<?php }else{echo '恭喜，权限设置正确，可以继续设置';}?></p>
                <strong>请确认您已经拥有以下信息：</strong>
				<p>数据库名称 \ 数据库用户名 \ 数据库密码 \ 数据库主机地址</p>
			  <br />
			  <p>&nbsp;</p>
			  <strong>数据库信息配置</strong>
                <p>MySql服务器：&nbsp;&nbsp;<input type="text"  id="sqlserver" name="sqlserver" size="30" value="localhost">
                <font color="red">＊</font></p>
                <p>MySql数据名称：<input type="text" value="" id="sqldbname" name="sqldbname" size="30">
                <font color="red">＊</font></p>  
                <p>MySql数据帐号：<input type="text" value="" id="sqlname" name="sqlname" size="30">
                <font color="red">＊</font></p>       
                <p>MySql数据密码：<input type="text" value="" id="sqlpass" name="sqlpass" size="30">
                <font color="red">＊</font></p> 
                <p>&nbsp;</p>
                <p ><strong><a onclick="checkdb()" style="cursor:pointer;"><font color="red">>>MYSQL连接测试</font></a></strong><span id="checkinfo"></span></p>
              </div>
              <div class="c"></div>
            </div>
          </div>
        
          <div class="stepbt"><a class="last" href="javascript:history.go(-1);"><strong class="last">上一步：阅读安装协议</strong></a> <a class="btA colorA next" id="isreturndown" style="cursor:pointer;" href="#" onclick="form2.submit();"><span>继续下一步</span></a> </div>
        </div>

</form>
<?php	
	}
	
function exestep2(){
	$server=SafeRequest("sqlserver","post");
	$dbname=SafeRequest("sqldbname","post");
	$id=SafeRequest("sqlname","post");
	$pwd=SafeRequest("sqlpass","post");
	chmod("../include/config.php",0777);
	chmod("../include/install.php",0777);
	$config=file_get_contents("../include/config.php");
	$inscon=file_get_contents("../include/install.php");
	$version = file_get_contents('./version.txt');
	$config=preg_replace('/"S_Sqlservername","(.*?)"/','"S_Sqlservername","'.$server.'"',$config);
	$config=preg_replace('/"S_Sqldbname","(.*?)"/','"S_Sqldbname","'.$dbname.'"',$config);
	$config=preg_replace('/"S_Sqluserid","(.*?)"/','"S_Sqluserid","'.$id.'"',$config);
	$config=preg_replace('/"S_Sqlpwd","(.*?)"/','"S_Sqlpwd","'.$pwd.'"',$config);
	
	$inscon=preg_replace('/"S_UrlList","(.*?)"/','"S_UrlList","a'.substr(md5(date('Y-m-d:h:i:s')),15).'"',$inscon);
	$inscon=preg_replace('/"S_UrlContent","(.*?)"/','"S_UrlContent","b'.substr(md5(date('h:i:s:Y-m-d')),15).'"',$inscon);
	$inscon=preg_replace('/"S_Version","(.*?)"/','"S_Version","'.$version.'"',$inscon);
	$inscon=preg_replace('/"S_IsInstall",(.*?)\)/','"S_IsInstall",1)',$inscon);
	
	fwrite(fopen("../include/config.php","wb"),$config);
	fwrite(fopen("../include/install.php","wb"),$inscon);
	//header("Location:install.php?action=step3");
	echo "<script>window.location='install.php?action=step3';</script>";
	}
	
function step3(){
	$lnk=mysql_connect(S_Sqlservername,S_Sqluserid,S_Sqlpwd)
	or die('<div align=center><br/><br/><br/><b><font color=red>连接MYSQL服务器出错!</font></b></div>');
mysql_select_db(S_Sqldbname,$lnk)
	or die('<div align=center><br/><br/><br/><b><font color=red>连接MYSQL数据库出错!</font></b></div>');
	mysql_query("SET NAMES gb2312", $lnk);
	$sql=file_get_contents("syc.sql");
	$sqlarr=explode(";",$sql);
	$datasql=file_get_contents("datas.sql");
	$dataarr=explode("-YiCiCms-",$datasql);
	
?>
 <div id="content">
          <div class="wrapD">
            <div class="wrapE">
              <div class="agreement" style="height:290px; overflow:auto"><strong>开始创建表</strong><br />
<?php	
	for($i=0;$i<count($sqlarr)-1;$i++){
		//$db->query($sqlarr[$i]);
		mysql_query($sqlarr[$i]);
		$intablepost=strpos($sqlarr[$i],"(");
		//$intable=cnsubstr($sqlarr[$i],$intablepost);
		$intable=substr($sqlarr[$i],0,$intablepost);
		echo "<p>".$intable."---<font color=#00FF00>执行成功</font></p>";
		}
	for($i=0;$i<count($dataarr);$i++){
		//$db->query($dataarr[$i]);
		mysql_query($dataarr[$i]);
		echo "<p><font color=#00FF00>初始数据导入成功</font></p>";
		}	
	echo  "<p><font color=#00FF00>恭喜！!数据初始化完成!!!!!</font></p>";
?>
              </div>
              <div class="c"></div>
            </div>
          </div>
          <div class="stepbt">  <a class="btA colorA next" style="cursor:pointer;" href="install.php?action=step4"><span>进入下一步</span></a> </div>
         </div>
<?php
}

function step4(){
	
	$path=$_SERVER['PHP_SELF'];
	$path=ReplaceStr(strtolower($path),"install/install.php","");
?>
<form name="form4" id="form4" action="?action=exestep4" method="post">
 <div id="content">
          <div class="wrapD">
            <div class="wrapE">
              <div class="agreement" style="height:290px; overflow:auto"><strong>网站基本信息配置</strong><br />
					<p>网站名称：&nbsp;&nbsp;<input type="text" value="搜一次php版" id="SiteName" name="SiteName" size="30"></p>
                    <p>网站域名：&nbsp;&nbsp;<input type="text" value="<?php echo "http://".$_SERVER['HTTP_HOST']?>" id="SiteDomain" name="SiteDomain" size="30"> 
                    如：http://www.YiCiCms.com</p>
                    <p>网站路径：&nbsp;&nbsp;<input type="text" value="<?php echo $path?>" id="SitePath" name="SitePath" size="30">
                    &nbsp;＊自动获取</p>
                    <br/><strong>网站运行模式选择</strong><br />  
                  	<p>&nbsp;<input type="radio" name="WebMode" id="WebMode"  value="1" class="radio" checked="checked">动态模式<input type="radio" name="WebMode" id="WebMode"  value="0" class="radio">静态模式</p>
                <strong>后台登陆设置</strong><br />
                <p>管理员帐号：&nbsp;&nbsp;<input type="text" name="admin_Name" id="admin_Name">
                <font color="#FF0000">＊</font></p>
                <p>管理员密码：&nbsp;&nbsp;<input type="text" value="" id="admin_Pass" name="admin_Pass" >
                <font color="#FF0000">＊</font></p>
                <p>后台认证码：&nbsp;&nbsp;<input type="text"  id="checkcode" name="checkcode"  value="">
                <font color="#FF0000">＊</font></p>
                &nbsp;&nbsp;&nbsp; <font color="#FF0000"><b>请牢记您设置的管理员帐号,密码,认证码！</b></font> </p>
              </div>
              <div class="c"></div>
            </div>
          </div>
        
          <div class="stepbt"><a class="last" href="javascript:history.go(-2);"><strong class="last">上一步：数据库信息配置</strong></a> <a class="btA colorA next" style="cursor:pointer;" href="#"  onclick="checkadmin();"><span>继续下一步</span></a> </div>
        </div>
</form>

<?php	
	}
	
function exestep4(){
	$sitename=SafeRequest("SiteName","post");
	$domain=SafeRequest("SiteDomain","post");
	$sitepath=SafeRequest("SitePath","post");
	$webmode=SafeRequest("WebMode","post");
	$adminname=SafeRequest("admin_Name","post");
	$adminpass=SafeRequest("admin_Pass","post");
	$checkcode=SafeRequest("checkcode","post");
	$lnk=mysql_connect(S_Sqlservername,S_Sqluserid,S_Sqlpwd) or die('servererror');
	
	mysql_select_db(S_Sqldbname,$lnk) or die('dberror');	
	mysql_query("SET NAMES gb2312", $lnk);
	$config=file_get_contents("../include/config.php");
	$config=preg_replace('/"S_SiteName","(.*?)"/','"S_SiteName","'.$sitename.'"',$config);
	$config=preg_replace('/"S_SiteDomain","(.*?)"/','"S_SiteDomain","'.$domain.'"',$config);
	$config=preg_replace('/"S_CheckCode","(.*?)"/','"S_CheckCode","'.$checkcode.'"',$config);
	$config=preg_replace('/"S_SitePath","(.*?)"/','"S_SitePath","'.$sitepath.'"',$config);
	$config=preg_replace('/"S_Webmode",(.*?)\)/','"S_Webmode",'.$webmode.')',$config);
	fwrite(fopen("../include/config.php","wb"),$config);
	@$sql="INSERT INTO `s_admin` (`S_AdminUserName`,`S_AdminPassWord`,`S_Permission`,`S_IsLock`,`S_PreIP`) values ('".$adminname."','".md5($adminpass)."','1,2,3,4,5,6,7,8,9,10,11','0','0')";
	//echo $sql;
	if(mysql_query($sql)){
		rename("install.php","install.phpbak");
		echo "<script>alert('恭喜你安装完毕,请删除根目录下的install文件夹');window.location='../admin/admin_login.php';</script>";
		}
	else{
		echo mysql_error();
		}	
	}	
 ?>     
</div>
    </div>
  </div>
</div> 

</body>
</html>