<?php

if(file_exists("../config/install.lock"))
{
	header("Location: ../"); 
	exit;
}
header("Content-type:text/html;charset=utf-8");
/*加密函数*/
function umd5($str)
{
	return md5(md5($str));	
}
/*检测权限*/
function checkmode($dirs,$type=2){
	$data=array();
	foreach($dirs as $dir){
		if(!file_exists("../".$dir)){
			mkdir("../".$dir);
			chmod("../".$dir,"0777");
		}
		switch($type){
			case 2://写入
				if(!is_writeable("../".$dir)){
					$data[]=$dir." <font style='color:red;'>不可写</font><br>";
				}
				break;
			case 4://读取
				if(!is_readable("../".$dir)){
					$data[]=$dir." <font style='color:red;'>不可读</font><br>";
				}
				break;
			case 1://执行
				if(!is_executable("../".$dir)){
					$data[]=$dir." <font style='color:red;'>不可执行</font><br>";
				}
				break;
			case 6://读写权限
				$s="";
				if(!is_writeable("../".$dir)){
					$s.=" <font style='color:red;'>不可写</font>";
				}
				
				if(!is_readable("../".$dir)){
					$s.=" <font style='color:red;'>不可读</font>";
				}
				if(!empty($s)){
					$data[]=$dir.$s."<br>";
				}
				break;
			case 7://读写执行权限
				$s="";
				if(!is_writeable("../".$dir)){
					$s.=" <font style='color:red;'>不可写</font>";
				}
				
				if(!is_readable("../".$dir)){
					$s.=" <font style='color:red;'>不可读</font>";
				}
				
				if(!is_executable("../".$dir)){
					$s.=" <font style='color:red;'>不可执行</font>";
				}
				if(!empty($s)){
					$data[]=$dir.$s."<br>";
				}
				break;
				
		}
		
	}
	return $data;
}
set_time_limit(0);
require("cls_smarty.php");
$smarty= new Smarty();
$smarty->template_dir="skins";
$smarty->compile_dir="compiled";
if(empty($_REQUEST['step']) || $_REQUEST['step']==1)
{
	//检测目录权限
	$dirs=array(
		"api",
		"attach",
		"config",
		"config/dict",
		"static",
		"skins",
		"skymvc",
		"skymvc/class",
		"skymvc/function",
		"skymvc/library",
		"source",
		"source/index",
		"source/model",
		"temp",
		"temp/caches",
		"temp/compiled",
		"temp/html",
	);
	$data=checkmode($dirs,6);
	if(!empty($data)){
		$smarty->assign("dirs",$data);
	}
	
	$smarty->assign("step",1);
	
	$smarty->display("index.html");
	
}elseif($_REQUEST['step']==2)
{
	$mysql_host=trim($_POST['mysql_host']);
	$mysql_user=trim($_POST['mysql_user']);
	$mysql_pwd=trim($_POST['mysql_pwd']);
	$mysql_db=trim($_POST['mysql_db']);
	$tblpre=trim($_POST['tblpre']);
	$domain=trim($_POST['domain']);
	$wapurl=trim($_POST['wapurl']);
	$str='<?php 
define("MYSQL_CHARSET","utf8");
define("TABLE_PRE","'.$tblpre.'");
define("SESSIONDB",0);
$_ENV[\'database\']=array(
	"master"=>array("'.$mysql_host.'","'.$mysql_user.'","'.$mysql_pwd.'","'.$mysql_db.'"),
	/*"slave"=>array(
			array("'.$mysql_host.'","'.$mysql_user.'","'.$mysql_pwd.'","'.$mysql_db.'"),
			array("'.$mysql_host.'","'.$mysql_user.'","'.$mysql_pwd.'","'.$mysql_db.'")
	)*/
);
define("DOMAIN","'.$domain.'");
define("WAP_DOMAIN","'.$wapurl.'");
define("TESTMODEL",0);
define("REWRITE_ON",0);
?>';
	file_put_contents("../config/config.php",$str);
	$smarty->assign("step",2);
	
	echo "<script language=\"JavaScript\">\nfunction moveNew(){\nlocation.href=\"index.php?m=index&step=3\";\n}\nwindow.setTimeout('moveNew()','2000');\n</script>";
	$smarty->display("index.html");
	
}elseif($_REQUEST['step']==3)
{
	require_once("../config/config.php");
	//开始创建数据库
	if(!$link=mysql_connect($_ENV['database']['master'][0],$_ENV['database']['master'][1],$_ENV['database']['master'][2]))
	{
		echo "<script>alert('服务器连接失败');history.go(-1);</script>";
		exit();
	}
	if(!mysql_select_db($_ENV['database']['master'][3],$link))
	{
		
		mysql_query("create database ".$_ENV['database']['master'][3]);
		if(!mysql_select_db($_ENV['database']['master'][3],$link))
		{
			echo "<script>alert('创建数据库失败');history.go(-1);</script>";
			exit();
		}
	}
	mysql_query("SET sql_mode=''");
	mysql_query("SET NAMES utf8");
	$dbfile="install.sql";
	$content=file_get_contents($dbfile);
	$content.=file_get_contents("data.sql");
	if(file_exists("district.sql")){
		$content.=file_get_contents("district.sql");
	}
	//获取创建的数据
	//去掉注释
	$content=preg_replace("/--.*\n/iU","",$content);
	//替换前缀
	$content=str_replace("tao_",TABLE_PRE,$content);
	
	$carr=array();
	$iarr=array();
	//提取create
	preg_match_all("/Create table .*\(.*\).*;[\r\n]+/iUs",$content,$carr);
	$carr=$carr[0];
	foreach($carr as $c)
	{
		@mysql_query($c,$link);
	}
	
	//提取insert
	preg_match_all("/INSERT INTO .*\(.*\);[\r\n]+/iUs",$content,$iarr);
	$iarr=$iarr[0];
	//插入数据
	foreach($iarr as $c)
	{
		@mysql_query($c,$link);
	}
	$smarty->assign("step",3);
	
	echo "<script language=\"JavaScript\">\nfunction moveNew(){\nlocation.href=\"index.php?m=index&step=4\";\n}\nwindow.setTimeout('moveNew()','2000');\n</script>";
	$smarty->display("index.html");

}elseif($_REQUEST['step']==4)
{
	
	$smarty->assign("step",4);
	$smarty->display("index.html");
	
}elseif($_REQUEST['step']==5)
{
	if($_POST)
	{	require_once("../config/config.php");
		$link=mysql_connect($_ENV['database']['master'][0],$_ENV['database']['master'][1],$_ENV['database']['master'][2]);
		mysql_select_db($_ENV['database']['master'][3],$link);
		mysql_query("SET NAMES utf8");
		 mysql_query("SET sql_mode=''");
		$adminname=trim($_POST['adminname']);
		$pwd1=trim($_POST['pwd1']);
		$pwd2=trim($_POST['pwd2']);
		$salt=rand(1000,9999);
		if(empty($adminname))
		{
			echo "<script>alert('管理员不能为空');history.go(-1);</script>";
			exit();
		}
		if(($pwd1!=$pwd2) or empty($pwd1))
		{
			echo "<script>alert('两次输入的密码不一致');history.go(-1);</script>";
		}
		$res=mysql_query("SELECT * FROM ".TABLE_PRE."admin WHERE username='".$adminname."' ");
		if(mysql_num_rows($res)){
			mysql_query("update ".TABLE_PRE."admin set password='".umd5($pwd1.$salt)."',isfounder=1,salt='".$salt."' ");
		}else{
			mysql_query("insert into ".TABLE_PRE."admin(username,password,isfounder,salt) values('$adminname','".umd5($pwd1.$salt)."',1,'".$salt."')");
		}
		//添加默认站点
		 
		
	}

	file_put_contents("../config/install.lock","");
	$smarty->assign("step",5);
	$smarty->display("index.html");
}

?>