<?php
include("../include/config.php");
include("../include/install.php");
include("../include/function.php");
$Step=SafeRequest("action","get");
$StepInfo="";
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="阅读安装协议";
	}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>盘锦DJ舞曲网PHP版安装</title>
<style type="text/css"> 
* {font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 1.5em; word-break: break-all; }
body { background:#fff; background-image : url("images/body_bg.gif");background-repeat: repeat-x ;  }
input { border: 1px solid #B2C9D3;height:23px; background: #F5FCFF; }
.button { color: #135294; border:1px solid #666; height:21px; line-height:18px; background:url("images/button_bg.gif")}
div#nifty{background: #ABD4EF;width: 820px;word-break:break-all; margin-top:60px;}
b.rtop, b.rbottom{display:block;background: #FFF}
b.rtop b, b.rbottom b{display:block;height: 1px;overflow: hidden; background: #ABD4EF}
b.r1{margin: 0 5px}
b.r2{margin: 0 3px}
b.r3{margin: 0 2px}
b.rtop b.r4, b.rbottom b.r4{margin: 0 1px;height: 2px}
.showtable { width:642px; border: solid; border-color:#86B9D6 #B2C9D3 #B2C9D3; border-width: 3px 1px 1px; margin: 10px auto; background: #F5FCFF; }
.showtable td { padding: 3px; }
.showtable strong { color: #5086A5; }
.datatable { width: 640px; margin: 10px auto 25px; }
.datatable td { padding: 5px 0; border-bottom: 1px solid #EEE; }
.current { font-weight: bold; color: #ff0000 !important; border-bottom-color: #Ff0000 !important; }
h1 { font-size: 18px; margin: 1px 0 0; line-height: 50px; height: 50px; background: #E8F7FC; color: #5086A5; padding-left: 10px; }
</style>
<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>
</head>
<body onLoad="if(window.name!=''){document.body.style.width='100%';}">
<div class="menuskin" id="popmenu" onMouseOver="clearhidemenu()" onMouseOut="dynamichide(event)" style="z-index:100;"></div>
<center>
<div id="nifty">
<b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b>
<div style="width:803px; height:26px; line-height:26px; background:none;text-align:left;">&nbsp; <b><?php echo $StepInfo?></b></div>
<div style="width:801px !important; width:803px; height:120px; background:#fff; border-left:1px solid #649EB2; border-right:1px solid #649EB2; ">
	<h1>盘锦DJ舞曲网PHP版安装</h1><br>
	<table>
	<tr>
	<td <?php if($Step=="step1") echo 'class="current"';?>>1.安装开始</td>
	<td <?php if($Step=="step2") echo 'class="current"';?>>2.设置数据库连接信息</td>
	<td <?php if($Step=="step3") echo 'class="current"';?>>3.创建数据库结构</td>
	<td <?php if($Step=="step4") echo 'class="current"';?>>4.添加默认数据</td>
	<td <?php if($Step=="step5") echo 'class="current"';?>>5.安装完成</td>
	</tr>
	</table>
	<script type="text/javascript">
	function readme() {
		var tbl_readme = document.getElementById('tbl_readme');
		if(tbl_readme.style.display == '') {
			tbl_readme.style.display = 'none';
		} else {
			tbl_readme.style.display = '';
		}
	}
	</script>
<?php
$action=SafeRequest("action","get");
if((file_exists("../include/install.lock"))&&($action!="")) exit("<script>window.location='install.php';</script>");
if ($action==""){
	if((S_IsInstall==1) && file_exists("../include/install.lock")){
		echo '<table class=showtable>
	              <tr><td><br>警告!您已经安装过盘锦DJ舞曲网PHP版<br>
		      为了保证数据安全，请立即手动删除 install/install.php 文件夹<br>
		      如果需要重新安装,请删除 <font color=red>include/install.lock</font><br>
		      </td></tr>
	              <tr><td><a href="javascript:window.close()">关闭网页</a></td></tr>
	              </table><br>';
                show_footer();
                exit();
	}else{
	        echo "<script>window.location='install.php?action=step1';</script>";
        }
}elseif ($action=="step1"){
?>
	<table class="showtable">
	<tr><td>
	<strong>欢迎您使用盘锦DJ舞曲网PHP版</strong><br>
通过盘锦DJ舞曲网PHP版，建站者的您可以轻松构建一个音乐为核心的交流网络，让站点用户可以用一句话记录生活中的点点滴滴；方便快捷地发布音乐、发布日志、上传图片；更可以十分方便的与其好友们一起分享信息、讨论感兴趣的话题；轻松快捷的了解好友最新动态。
	<br><a href="javascript:;" onClick="readme()"><strong>请先认真阅读我们的软件使用授权协议</strong></a>
	</td></tr>
	</table>

	<table width="640" border="0" cellspacing="3" cellpadding="0">
	</td></tr>
	<tr><td>
	<strong>文件/目录权限设置</strong><br>
	在您执行安装文件进行安装之前，先要设置相关的目录属性，以便数据文件可以被程序正确读/写/删/创建子目录。<br>
	推荐您这样做：<br>使用 FTP 软件登录您的服务器，将服务器上以下目录、以及该目录下面的所有文件的属性设置为777，win主机请设置internet来宾帐户可读写属性<br>
	<table class="datatable">
	</td></tr>
	<tr style="font-weight:bold;"><td>名称</td><td>所需权限属性</td><td>说明</td><td>检测结果</td></tr>
	<tr><td><strong>./include/config.php</strong></td><td>读/写</td><td>系统配置文件</td><td><?php $checkok = true; if(is_writable("../include/config.php")){ echo "<font color=green>ok</font>";}else{ $checkok = false; echo "<font color=red>no</font>";}?></td></tr>
	<tr><td><strong>./include/install.php</strong></td><td>读/写</td><td>安装配置文件</td><td><?php if(is_writable("../include/install.php")){ echo "<font color=green>ok</font>";}else{ $checkok = false; echo "<font color=red>no</font>";}?></td></tr>
	<tr><td><strong>./install/</strong></td><td>读/写</td><td>安装配置目录</td><td><?php if(is_writable("../install/")){ echo "<font color=green>ok</font>";}else{ $checkok = false; echo "<font color=red>no</font>";}?></td></tr>
	</table>
	</td></tr>
	</table>
<?php
	if(!$checkok) {
		echo "<table><tr><td><b>出现问题</b>:<br>系统检测到以上文件权限没有正确设置<br>强烈建议正常设置权限后再刷新本页面以便继续安装<br>否则系统可能会出现无法预料的问题 [<a href=\"?action=step2\">强制继续</a>]</td></tr></table>";
	} else {
		echo '<form id="theform" method="post" action="?action=step2">
			<table class=button>
				<tr>
					<td><input type="submit" id="startsubmit" name="startsubmit" value="接受协议，开始安装盘锦DJ舞曲网PHP版"></td>
				</tr>
			</table>
		</form>';
	}
?>
	<table id="tbl_readme" style="display:none;" class="showtable">
	<tr>
	<td><strong>许可协议:</strong> </td></tr>
	<tr>
	<td>
	<div>中文版授权协议 适用于中文用户
	<p>无任何协议</p></div>
	</td></tr>
	</table>
<?php
       show_footer();
}elseif ($action=="step2"){
?>
	<form action="?action=exestep2" method="post" name="form2">
	<table class="showtable">
	<tr><td><strong># 设置盘锦DJ舞曲网PHP版数据库信息</strong></td></tr>
	<tr><td id="msg1">请确认您已经拥有：数据库主机地址 \ 数据库用户名 \ 数据库密码</td></tr>
	</table>
	<table class=datatable>
	<tr>
	<td width="25%">数据库服务器本地地址:</td>
	<td><input type="text" name="sqlserver" id="sqlserver" size="30" value="localhost"></td>
	<td width="30%"><font color="#FF0000">＊</font>一般为localhost</td>
	</tr>
	<tr>
	<td>数据库用户名:</td>
	<td><input type="text" name="sqlname" id="sqlname" size="30" value=""></td>
	<td>&nbsp;<font color="#FF0000">＊</font></td>
	</tr>
	<tr>
	<td>数据库密码:</td>
	<td><input type="text" name="sqlpass" id="sqlpass" size="30" value=""></td>
	<td>&nbsp;<font color="#FF0000">＊</font></td>
	</tr>
	<tr>
	<td>数据库名:</td>
	<td><input type="text" name="sqldbname" id="sqldbname" size="30" value=""></td>
	<td><font color="#FF0000">＊</font>如果不存在，则会尝试自动创建</td>
	</tr>
	<tr>
	<td>授权KEY:</td>
	<td><input type="text" size="30" value="盘锦DJ舞曲网已授权!" readonly></td>
	</tr>
	</table>
	<table>
	<tr><td><strong><a onClick="checkdb()" style="cursor:pointer;"><font color="red">>>MYSQL连接测试</font></a></strong><span id="checkinfo"></span></td></tr>
	</table>	<br>
	<table class=button>
	<tr><td><input type="button" id="" name="" onClick="form2.submit();" value="设置完毕,检测我的数据库配置"></td></tr>
	</table>
	<br>
	</form>
<?php
show_footer();
}elseif ($action=="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");
	$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_IsInstall",(.*?)\)/','"S_IsInstall",1)',$inscon);
	fwrite(fopen("../include/config.php","wb"),$config);
	fwrite(fopen("../include/install.php","wb"),$inscon);
	echo "<script>window.location='install.php?action=step3';</script>";

}elseif ($action=="step3"){
	$havedata = "no";
	if(!@mysql_connect(S_Sqlservername,S_Sqluserid,S_Sqlpwd)) {
		echo '<table class=showtable>
	              <tr><td>数据库连接信息填写错误，导致无法连接，请确认</td></tr>
	              <tr><td>&nbsp;</td></tr>
	              <tr><td><a href="install.php?action=step2">返回上一步</a> </td></tr>
	              </table>';
                show_footer();
                exit();
	}
	if(mysql_select_db(S_Sqldbname)) {
		if(mysql_query("SELECT COUNT(*) FROM s_video")) {
			$havedata = "yes";
		}
	} else {
		if(!mysql_query("CREATE DATABASE `".S_Sqldbname."`")) {
			echo '<table class=showtable>
	          	    <tr><td>设定的盘锦DJ舞曲网PHP版数据库无权限操作，请先手工操作后，再执行安装程序!</td></tr>
	          	    <tr><td>&nbsp;</td></tr>
	          	    <tr><td><a href="install.php?action=step2">返回上一步</a> </td></tr>
	          	    </table>';
                	show_footer();
               		exit();
		}
	}
	if($havedata=="yes") {
			echo '<table class=showtable>
	          	    <tr><td><font color=red>危险!指定的盘锦DJ舞曲网PHP版数据库已有数据，如果继续将会清空原有数据!</font></td></tr>
	          	    <tr><td>&nbsp;</td></tr>
	          	    <tr><td><a href="install.php?action=step2">返回上一步</a> &nbsp;<a href="install.php?action=step4">清空原数据并安装</a></td></tr>
	          	    </table>';
                	show_footer();
               		exit();
	} else {
		echo "<script>window.location='install.php?action=step4';</script>";
	}

}elseif ($action=="step4"){
	if(!@mysql_connect(S_Sqlservername,S_Sqluserid,S_Sqlpwd)){
	        echo '<table class=showtable>
	              <tr><td>连接MYSQL数据库出错，请确认!</td></tr>
	              <tr><td>&nbsp;</td></tr>
	              <tr><td><a href="install.php?action=step2">返回上一步</a> </td></tr>
	              </table>';
                show_footer();
                exit();
        }
        $lnk=@mysql_connect(S_Sqlservername,S_Sqluserid,S_Sqlpwd);
	if(!@mysql_select_db(S_Sqldbname,$lnk)){
	        echo '<table class=showtable>
	              <tr><td>连接MYSQL数据库出错，请确认!</td></tr>
	              <tr><td>&nbsp;</td></tr>
	              <tr><td><a href="install.php?action=step2">返回上一步</a> </td></tr>
	              </table>';
                show_footer();
                exit();
        }
	  echo '<table class=showtable>
	        <tr><td>数据库配置成功，进入下一步操作!</td></tr>
	        <tr><td>&nbsp;</td></tr>
	        <tr><td><a>请稍等...</a></td></tr>
	        </table>';
	mysql_query("SET NAMES gb2312", $lnk);
	$sql=file_get_contents("zhuangbi.sql");
	$sqlarr=explode(";",$sql);
	$datasql=file_get_contents("data.sql");
	$dataarr=explode("-chshdjcms-",$datasql);
	for($i=0;$i<count($sqlarr)-1;$i++){
		mysql_query($sqlarr[$i]);
		$intablepost=strpos($sqlarr[$i],"(");
		$intable=substr($sqlarr[$i],0,$intablepost);
	}
	  echo '<table class=showtable>
	        <tr><td>数据表已经全部填加完成，进入下一步操作!</td></tr>
	        <tr><td>&nbsp;</td></tr>
	        <tr><td><a>请稍等...</a></td></tr>
	        </table>';
	for($i=0;$i<count($dataarr);$i++){
		mysql_query($dataarr[$i]);
	}	
	  echo '<table class=showtable>
	        <tr><td>系统默认数据添加完毕，进入下一步操作!</td></tr>
	        <tr><td>&nbsp;</td></tr>
	        <tr><td><a href="install.php?action=step5">请稍等...</a><script>window.location="install.php?action=step5";</script></td></tr>
	        </table>';
                show_footer();
                exit();
}elseif ($action=="step5"){
	$path=$_SERVER['PHP_SELF'];
	$path=ReplaceStr(strtolower($path),"install/install.php","");
?>
	<form name="form4" id="form4" action="?action=exestep4" method="post">
	<table class="showtable">
	<tr><td><strong>网站基本信息配置</strong></td></tr>
	<tr><td>&nbsp;&nbsp;&nbsp; <font color="#FF0000"><b>请牢记您设置的管理员帐号,密码,认证码！</b></font></td></tr>
	</table>
	<table class=datatable>
	<tr>
	<td width="25%">网站名称:</td>
	<td><input type="text" name="SiteName" size="30" value="盘锦DJ舞曲网PHP版"></td>
	<td width="30%">&nbsp;<font color="#FF0000">＊</font>如：盘锦DJ舞曲网PHP版</td>
	</tr>
	<tr>
	<td>网站域名:</td>
	<td><input type="text" name="SiteDomain" size="30" value="http://<?php echo $_SERVER['HTTP_HOST']?>"></td>
	<td>&nbsp;<font color="#FF0000">＊</font>如：http://PjRen.Net</td>
	</tr>
	<tr>
	<td>网站路径:</td>
	<td><input type="text" name="SitePath" size="20" value="<?php echo $path?>"></td>
	<td>&nbsp;<font color="#FF0000">＊</font>自动获取,不需要修改!</td>
	</tr>
	<tr>
	<td>网站运行模式选择:</td>
	<td><input type="radio" name="WebMode" id="WebMode"  value="1" class="radio" checked="checked">动态模式<input type="radio" name="WebMode" id="WebMode"  value="0" class="radio">静态模式</td>
	<td>&nbsp;默认为动态</td>
	</tr>
	<tr>
	<td>管理员帐号:</td>
	<td><input type="text" name="admin_Name" id="admin_Name" size="20" value=""></td>
	<td>&nbsp;<font color="#FF0000">＊</font>必填</td>
	</tr>
	<tr>
	<td>管理员密码:</td>
	<td><input type="text" name="admin_Pass" id="admin_Pass" size="20" value=""></td>
	<td>&nbsp;<font color="#FF0000">＊</font>必填</td>
	</tr>
	<tr>
	<td>后台认证码:</td>
	<td><input type="text" name="checkcode" id="checkcode" size="20" value=""></td>
	<td>&nbsp;<font color="#FF0000">＊</font>必填</td>
	</tr>
	</table>
	<table class=button>
	<tr><td><input type="button" onClick="checkadmin();" name="" value="  设置完毕,安装确定"></td></tr>
	</table>
	<br>
	</form>
<?php
  show_footer();
}elseif ($action=="exestep4"){
	$sitename=SafeRequest("SiteName","post");
	$domain=SafeRequest("SiteDomain","post");
	$sitepath=SafeRequest("SitePath","post");
	$webmode=SafeRequest("WebMode","post");
	$adminname=SafeRequest("admin_Name","post");
	$adminpass=md5(SafeRequest("admin_Pass","post"));
	$checkcode=SafeRequest("checkcode","post");
	if(!@mysql_connect(S_Sqlservername,S_Sqluserid,S_Sqlpwd)){
	        echo '<table class=showtable>
	              <tr><td>连接MYSQL数据库出错，请确认!</td></tr>
	              <tr><td>&nbsp;</td></tr>
	              <tr><td><a href="install.php?action=step2">返回上一步</a> </td></tr>
	              </table>';
                show_footer();
                exit();
        }
        $lnk=@mysql_connect(S_Sqlservername,S_Sqluserid,S_Sqlpwd);
	if(!@mysql_select_db(S_Sqldbname,$lnk)){
	        echo '<table class=showtable>
	              <tr><td>连接MYSQL数据库出错，请确认!</td></tr>
	              <tr><td>&nbsp;</td></tr>
	              <tr><td><a href="install.php?action=step2">返回上一步</a> </td></tr>
	              </table>';
                show_footer();
                exit();
        }	
        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) values ('".$adminname."','".$adminpass."','1,2,3,4,5,6,7,8,9,10,11,12,13,14,15','0')";
	if(mysql_query($sql)){
		fwrite(fopen("../include/install.lock","wb"),"chshdjcms2.5");
	        echo '<table class=showtable>
	              <tr><td>&nbsp;</td></tr>
	              <tr><td>&nbsp;</td></tr>
	              <tr><td><font color="red">恭喜! 盘锦DJ舞曲网PHP版安装完成!</font>
		      <br>为了您的数据安全，请登录ftp，删除install目录<br>
		      您的管理员身份已经成功确认，并已经开通会员空间。接下来，您可以：</td></tr>
	              <tr><td><a href="../">进入网站首页</a>   <a href="../admin/">进入管理平台</a></td></tr>
	              <tr><td>&nbsp;</td></tr>
	              <tr><td>&nbsp;</td></tr>
	              </table>';
	}else{
		echo '<table class=showtable>
	              <tr><td>".mysql_error()."</td></tr>
	              </table>';
	}	
                show_footer();
}
//页面顶部
function show_footer() {
  echo '</div>
        <div style="width:801px !important; width:803px; height:50px; background:#F7F7E7 border:1px solid #649EB2; border-top:1px solid #ddd; margin-bottom:5px; font-size:12px; line-height:20px; padding-top:13px; color:#649EB2; ">Copyright Right&copy;2011 - 2012  <a href="http://www.pjren.net/" target="_blank">盘锦DJ舞曲网PHP版</a></div>
        <b class="rbottom"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b>
        </div>';
}
?>
</body>
</html>