<?php    
header("Content-Type: text/html;charset=utf-8"); 
$a=isset($_REQUEST['a'])?$_REQUEST['a']:'';
if($a=='do'){
	function populate_db( $DBname, $DBPrefix, $sqlfile ) {    
	    global $errors;    
	    
	    @mysql_select_db($DBname);    
	    mysql_query("SET NAMES utf8");    
	    $mqr = @get_magic_quotes_runtime();    
	    @set_magic_quotes_runtime(0);    
	    $query = fread(fopen($sqlfile, "r"), filesize($sqlfile));    
	    @set_magic_quotes_runtime($mqr);    
	    $pieces  = split_sql($query);    
	    
	    for ($i=0; $i<count($pieces); $i++) {    
	        $pieces[$i] = trim($pieces[$i]);    
	        if(!empty($pieces[$i]) && $pieces[$i] != "#") {    
	            $pieces[$i] = str_replace( "#__", $DBPrefix, $pieces[$i]);    
	            if (!$result = @mysql_query ($pieces[$i])) {    
	                $errors[] = array ( mysql_error(), $pieces[$i] );    
	            }    
	        }    
	    }    
	}    
	    
	function split_sql($sql) {    
	    $sql = trim($sql);    
	    $sql = preg_replace("/[\n#[^\n]*\n]/", "\n", $sql); 
	//	define('DEDEADMIN', ereg_replace("[/\\]{1,}", '/', dirname(__FILE__) ) );
	//改为
	//define('DEDEADMIN', preg_replace("/[\/\\\\]{1,}/", '/', dirname(__FILE__) ) );   
	    
	    $buffer = array();    
	    $ret = array();    
	    $in_string = false;    
	    
	    for($i=0; $i<strlen($sql)-1; $i++) {    
	        if($sql[$i] == ";" && !$in_string) {    
	            $ret[] = substr($sql, 0, $i);    
	            $sql = substr($sql, $i + 1);    
	            $i = 0;    
	        }    
	    
	        if($in_string && ($sql[$i] == $in_string) && $buffer[1] != "\\") {    
	            $in_string = false;    
	        }    
	        elseif(!$in_string && ($sql[$i] == '"' || $sql[$i] == "'") && (!isset($buffer[0]) || $buffer[0] != "\\")) {    
	            $in_string = $sql[$i];    
	        }    
	        if(isset($buffer[1])) {    
	            $buffer[0] = $buffer[1];    
	        }    
	        $buffer[1] = $sql[$i];    
	    }    
	    
	    if(!empty($sql)) {    
	        $ret[] = $sql;    
	    }    
	    return($ret);    
	}    
	    
	$lnk = mysql_connect($_REQUEST['dbhost'],$_REQUEST['dbuser'],$_REQUEST['dbpwd'])    
	   or die ('连接失败'.mysql_error());    
	    
	if (!mysql_select_db($_REQUEST['dbname'],$lnk)){    
	  exit();  
	}  
	
	define('ROOT', str_replace("\\", '/', substr(str_replace("\\", '/', dirname(__FILE__) ),0) ) );
	$str=file_get_contents(ROOT.'/system/config/database.inc.php');
	$patterns = array();
	$patterns[0] = '/dbhost/';
	$patterns[1] = '/dbname/';
	$patterns[2] = '/dbuser/';
	$patterns[3] = '/dbpwd/';
//	$patterns[4] = '/dbprefix/';
	$replacements = array();
	$replacements[0] = $_REQUEST['dbhost'];
	$replacements[1] = $_REQUEST['dbname'];
	$replacements[2] = $_REQUEST['dbuser'];
	$replacements[3] = $_REQUEST['dbpwd'];
//	$replacements[4] = $_REQUEST['dbprefix'];
	$str=preg_replace($patterns, $replacements, $str);
	$str=file_put_contents(ROOT.'/system/config/database.inc.php',$str);
	
	populate_db( $_REQUEST['dbname'],'go_','./Data/data.sql' );  
	rename('install.php', 'install.php.back');
	echo "数据库导入成功";
	//header("location:/");
}  
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
		*{margin: 0; padding: 0; list-style: none;}
		input{width: 300px; height: 30px;}
		button{ text-align: center; width: 150px; height: 30px;}
		table{width:450px; margin:200px auto 0;}
		table,td{border: none;}
		td{padding: 5px;}
		</style>
	</head>
	<body>
		<form action="install.php">
			<input type="hidden" name="a" value="do" />
			<table border="" cellspacing="" cellpadding="">
				<tr>
					<td>数据库地址：</td>
					<td><input type="text" name="dbhost" value="localhost"></td>
				</tr>
				<tr>
					<td>数据库用户名：</td>
					<td><input type="text" name="dbuser"></td>
				</tr>
				<tr>
					<td>数据库密码：</td>
					<td><input type="text" name="dbpwd"></td>
				</tr>
				<tr>
					<td>数据库名：</td>
					<td><input type="text" name="dbname"></td>
				</tr>
				<!--<tr>
					<td>数据库前缀：</td>
					<td><input type="text" value="aftg_" name="dbprefix"></td>
				</tr>-->
				<tr>
					<td colspan="2" align="center"><button type="submit">安装</button></td>
				</tr>
			</table>
		</form>
	</body>
</html>
