<?php
define('IN_59MIAO', true);
header('Content-Type:text/html;charset=utf-8');
$domain="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$domain=substr($domain,0,-18);
//创建一个实际路径
define('ROOT_PATH',substr(dirname(__FILE__),0,-7));
define('IN_PHPUP',1);
$step=!empty($_GET['step'])?intval($_GET['step']):1;
if(is_file(ROOT_PATH.'data/install.lock'))
{
	header('Location: ../index.php');
	exit;	
}
else{
	include_once 'includes/include.php';
}
//检测数据库是否存在的方法
check_db();
if (isset($_POST)) $_POST = setFormString($_POST);  //post表单数据转义
switch($step)
{
	case 1:
		include ROOT_PATH."install/template/step1.php"; //协议说明
	break;
	case 2:
		if($_GET['agree'])
		{
			$filelist=array('configs','cache','data','data/smarty_cache','data/smarty_compile','install','data/uploads','data/adjs');
		
			if(function_exists('gd_info'))
			{
				$v=gd_info();
				$gd_v=$v["GD Version"];
			}
			else
			{
				$gd_v=false;
			}
			$d=dirname(dirname(__FILE__));
			$disk_total=intval(disk_free_space($d)/1024/1024).'M';
			include ROOT_PATH."install/template/step2.php"; //文件检测
		}
		else
		{
			echo "<script language='javascript'>alert('必须同意协议规定才能继续安装');window.location='index.php?step=1';</script>";
		}
	break;
	case 3:		
        if(isset($_POST['error']))
		{
			echo "<script language='javascript'>alert('对不起，您的环境不支持此系统，请与您的空间服务商联系');window.location='index.php?step=2&agree=1';</script>";
		}
		else
		{
			include ROOT_PATH."install/template/step3.php";
			
		}
	break;
	case 4:
		if(!isset($_POST['wp-submit'])){
			echo "<script language='javascript'>alert('非法操作');window.location='index.php?step=3';</script>";
			exit;
		}
		if(!empty($_POST['dbname']) && isset($_POST['wp-submit']))
		{
			if(trim($_POST['password'])=='' || trim($_POST['pwdconfirm'])=='' || trim($_POST['password'])!=trim($_POST['pwdconfirm'])){
				echo "<script language='javascript'>alert('对不起，密码和确认密码不一致');window.location='index.php?step=3'</script>";
				exit;
			}
			$mydbhost		=trim($_POST['dbhost']);		//配置主机
			$mydbuser		=trim($_POST['dbuser']);			//数据库用户
			$mydbpw			=trim($_POST['dbpassword']);				//数据库密码
			$mydbname		=trim($_POST['dbname']);		//数据库密码		
			$mydbcharset	="UTF8";	
			$conn=mysql_connect($mydbhost,$mydbuser,$mydbpw);
			if(!$conn){
				echo "<script language='javascript'>alert('数据库配置有误，请重新配置');window.location='index.php?step=3'</script>";
				exit;
			}			
			$sql = "CREATE DATABASE IF NOT EXISTS `".$mydbname."` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin";
			$rel=mysql_query($sql);		
			//引入mysql文件
			include_once 'includes/mysql_class.php';		
			//数据库实例化函数
			$db = new mysql($mydbhost, $mydbuser, $mydbpw, $mydbname, 'comm', $mydbcharset);
			
			
			
			if($db)
			{			
			if(is_file('install.sql') && is_readable('install.sql'))
			{
				$sql=readf('install.sql');				
				
				$_br = "\r\n";
				$_tab = "\t";
				
				$_profile = '<?php'.$_br;		
				
				$_profile .= $_tab."//数据库配置文件".$_br;
			
				$_profile .= $_tab."if (!defined('IN_59MIAO'))
				{
					exit('Access Defined');
				}".$_br;				
				$_profile .= $_tab."define('DB_HOST', 'localhost');".$_br;
				$_profile .= $_tab."define('DB_USER', '{$_POST['dbuser']}');".$_br;
				$_profile .= $_tab."define('DB_PASS', '{$_POST['dbpassword']}');".$_br;
				$_profile .= $_tab."define('DB_NAME', '{$_POST['dbname']}');".$_br;
				$_profile .= $_tab."define('DB_CHARSET','UTF8');".$_br;
				$_profile .= $_tab."define('DB_FREFIX','{$_POST['tablepre']}');".$_br;	
				$_profile .= '?>'.$_br;					
				if(function_exists('file_put_contents'))
				{
					@file_put_contents(ROOT_PATH.'configs/db_config.inc.php', $_profile);
				}
				else
				{
					$fp=fopen(ROOT_PATH.'configs/db_config.inc.php','w');
					fwrite($fp,$_profile);
					fclose($fp);
				}
				
				$sql=str_replace('{tableprefix}',trim(strip_tags($_POST['tablepre'])),str_replace('{admin}',trim(strip_tags($_POST['username'])),str_replace('{password}',trim(strip_tags(sha1($_POST['password']))),str_replace('{domain}',$domain,$sql))));
				$sql=explode(';',$sql);		
				include ROOT_PATH."install/template/step4.php"; //安装数据库
				$error='';
				foreach($sql as $k=>$v){					
					if(@$rel=$db->query($v))
					{
						
					}
					else{
						$error.=$v.'安装失败\r\n';
					}
				}
				echo $error;
				$db->__destruct();
			  }
			}
			
			else
			{
				echo "<script language='javascript'>alert('数据库连接错误');window.location='index.php?step=3';</script>";
				exit;
			}
		}
		else
		{
			include ROOT_PATH."install/template/step3.php"; //配置信息
		}		
		break;
	case 5:
		
			include ROOT_PATH."install/template/step5.php"; //完成信息
			if(checkdir(ROOT_PATH.'data/')){
				file_put_contents(ROOT_PATH.'data/install.lock','');
			}
			if(file_exists('install.sql')){ //删除安装文件
				@unlink('install.sql');
			}		
	break;	
	default:
		include ROOT_PATH."/install/template/step1.php"; //协议说明
	break;	
}