<?php
error_reporting(0);
require_once("./install/template_class.php");
$p=new Template("./install");
//安装的第三步，执行SQL
$action=$_GET["action"];
if(!$action){
	$action=$_POST["action"];
}
if($action=="createDatabase"){
	$host    =$_POST["host"];      //获得数据库服务器
	$username=$_POST["username"];  //获得登陆用户名
	$password=$_POST["password"];  //获得登陆的密码
	$database=$_POST["database"];  //获得数据库名称
	$prefix  =$_POST["prefix"]."_";//获得表的前缀
	$installPath=$_SERVER['PHP_SELF'];//获得安装的路径
	$installPath=str_replace("/install.php","",$installPath);
	$server=$_SERVER['HTTP_HOST'];//服务器
  $charsetstring=$_POST["charset"];
  $charsetArray=explode("|",$charsetstring);
  $charset=$charsetArray[0];
  $collate=$charsetArray[1];
	if(!$host||!$username||!$password){
		print("<script> alert('必须填写完整'); window.history.go(-1);</script>");
		exit;
	}
	$conn=mysql_connect($host,$username,$password);
	if($conn){
		$db=mysql_select_db("$database",$conn);
		if(!$db){
			$sql="create database $database";
      if(mysql_get_server_info() > '4.1'){
				  @mysql_query("SET NAMES '{$charset}'");
				  $sql.=" DEFAULT CHARACTER SET {$charset} COLLATE $collate";
			}
			if(mysql_get_server_info() > '5.0') {
				  @mysql_query("SET sql_mode=''");
			}
			if(!mysql_query($sql)){
				print("<script> alert('数据库".$database."不存在,系统创建的时候也失败'); window.history.go(-1);</script>"); exit;
			}
			else{
				$db=mysql_select_db("$database",$conn);
			}
		}
		$db_config= <<< thesql
<?php
if (!defined('THINK_PATH')) exit();
return array(
    'APP_DEBUG' => 1,
    'DB_TYPE'=>'mysql',        //数据库类型
	'DB_HOST'=>'$host',        //服务器
	'DB_NAME'=>'$database',    //数据库名
	'DB_USER'=>'$username',     //用户名
	'DB_PWD'=>'$password',      //密码
	'DB_PORT'=>'3306',         // 数据库端口   
	'DB_PREFIX'=>'$prefix',    //表的前缀
	'LANG_SWITCH_ON' =>   true, 
    'DEFAULT_LANG'   =>    'zh-cn',     // 默认语言 
    'LANG_AUTO_DETECT'=>   true,     // 自动侦测语言 
 );
?>
thesql;

		$fp=fopen("./config.inc.php","w");
		fwrite($fp,$db_config,strlen($db_config));
		fclose($fp);
		require_once("./install/installsql.php");
		$sqlStr = explode(";",$sql);
		for($i=0;$i<count($sqlStr);$i++){
			if(trim($sqlStr[$i])!=""){
				$sql=$sqlStr[$i];
				if(mysql_get_server_info()>"4.1"){
					$sql=str_replace("TYPE=MyISAM", "TYPE=MyISAM DEFAULT CHARACTER SET {$charset} COLLATE {$collate}", $sqlStr[$i]);
				}
				if(mysql_get_server_info() > '4.1'){
				  @mysql_query("SET NAMES '{$charset}'");
			  }
			  if(mysql_get_server_info() > '5.0') {
				  @mysql_query("SET sql_mode=''");
			  }
				if(!mysql_query($sql)){
					exit("<script> alert('在创建数据表的时候出错'); window.history.go(-1);</script>");
				}
			}
		}
		$result=mysql_query("show table status from $database");
		while ($rs=mysql_fetch_object($result)){
			$p->set_file("tablerow","tablerow.html");
			$p->set_var("tablename",$rs->Name);
			$p->set_var("rows",$rs->Rows);
			$p->set_var("comment",$rs->Comment);
			$p->set_var("create_time",$rs->Create_time);
			$p->set_var("update_time",$rs->Update_time);
			$p->set_var("data_length",$rs->Data_length);
			$p->parse("row","tablerow",true);
		}
		$p->set_file("handle","settable.html");
		$p->set_var("cid",$cid);
		$p->parse("output","handle");
		$p->p("output");
	}
	else{
		exit("<script> alert('数据库连接不成功'); window.history.go(-1);</script>");
	}
}
//安装第二步
elseif ($action=="setConfig"){
	if(!is_writable("./config.inc.php")){
		print  ("<script> alert('config.inc.php不可写'); window.history.go(-1);</script>");exit;
	}
	$p->set_file("handle","config.html");
	$p->parse("output","handle");
	$p->p("output");
}
elseif ($action=="finishConfig"){
	$p->set_file("handle","username.html");
	$p->parse("output","handle");
	$p->p("output");
	
}
elseif($action=="setAdmin"){
	$user=$_POST["account"];
	$pass=md5($_POST["password"]);
/*	include_once("./ThinkPHP/Lib/Think/Db/Db.class.php");
	$db_dsn = “mysql://username:passwd@localhost:3306/DbName”;
	$db = new Db($db_dsn);*/
	include_once("./config.inc.php");

	$conn=mysql_connect($this->DB_HOST,$this->DB_USER,$this->DB_PWD);
	if(mysql_get_server_info()>4.1){
		@mysql_query("SET NAMES '{$charset}'",$conn);
	}
	if(mysql_get_server_info() > '5.0') {
		@mysql_query("SET sql_mode=''",$conn);
	}
	$db=mysql_select_db($this->DB_NAME,$conn);
	//----------------------------------------------
	//后台模块用平台安装路径加上模块名称为权限的标志
	//----------------------------------------------
	if(mysql_query("insert into `".$prefix."user`(`account`,`password`,`nickname`) values('$user','$pass',`管理员`)")){
		exit('安装全部完成，请删除install文件和install目录 <a href="admin.php">点此进入管理后台</a>');
	}
	else{
		print("<script> alert('写登陆用户时候数据库出错'); window.history.go(-1);</script>");exit;
	}
}
//安装第一步，读协议
else{
	$p->set_file("handle","index.html");
	$p->parse("output","handle");
	$p->p("output");
}
?>