<?php

require_once 'common.php';

echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";

//生成随机字符串
function generate_rand_string($length){
	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	$password = "";
	for( $i = 0; $i < $length; $i++ ){
		$password .= $chars[mt_rand(0, strlen($chars) - 1)];
	}
	return $password;
}

//密码加密
function set_password($password, $key){
	$pw = md5($password);
	$pw_b = $pw.$key;
	$pw_c = md5($pw_b);
	$pw_d = crypt($pw_c, $key);
	return sha1($pw_d);
}

//删除所有空格
function trimall($str){
	$qian = array(" ", "　", "\t", "\n", "\r");
	$hou = array("", "", "", "", "");
	return str_replace($qian,$hou,$str);
}


//引入数据库类
require_once '../class/MysqlDb.Class.php';

//引入配置文件
require_once '../config.php';

//判断是否提交
if(isset($_POST['SetAdmin'])){
	$username = trimall($_POST['username']);
	$password = trimall($_POST['password']);
	if($username == '' || $password == ''){
		echo "<script>alert('用户名或密码不能留空！');</script>";
		echo "<script>window.location = 'setadminuser.php'</script>";
		exit();		
	}
	$DataBase = new MysqlDb($DbHostAddress, $DbUserName, $DbPassword, $DbName, $DbTabpre);
	$DataBase->open_db();
	$sessionkey = generate_rand_string(50);
	$password = set_password($password, $sessionkey);
	if($DataBase->up('adminuser', "`username` = '".$username."', `password` = '".$password."', `key` = '".$sessionkey."'", "WHERE `id` = '1'") == false){
		$SetAdminuser = false;
		echo "<script>alert('管理员账户设置失败，请检查原因！');</script>";
		echo "<script>window.location = 'setadminuser.php'</script>";
		$DataBase->close_dblink();
		exit();
	}
	$DataBase->close_dblink();
	fopen("install.locked", "w+");
}
else{
	exit();
}

?>
<!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" />
<title>ZTJMessage 安装程序</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/script.js"></script>
</head>

<body>

<div class="box">

<div id="header">
<img src="images/logo.png" /><span>版本：3.5 Beta</span>
</div>

<div id="loac">
1.运行环境检测
&raquo;
2.系统设置
&raquo;
3.安装数据库
&raquo;
4.设置管理员账号
&raquo;
<b style="color:#F00; font-size:24px;">5.完成安装</b>
</div>

<div>
<table width="100%" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr style="height:50px; line-height:50px; text-align:center;">
    <td><b style="color:#F00; font-size:24px;">恭喜您！留言板安装成功！</b></td>
  </tr>
  <tr style="text-align:center; font-size:12px;">
    <td class="url1"><a href="../index.php">访问首页</a></td>
  </tr>
  <tr style="text-align:center; font-size:12px; color:#999;">
    <td>为了保证系统安全，请您务必删除install目录</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>  
</table>
</div>

</div>

</body>

</html>
