<?php
error_reporting(7);
@set_time_limit(0);
$_dbhost='localhost';
$_dbuser='';
$_dbpw='';
$_dbname='';
$_Charset='latin1';


foreach($_POST as $_key=>$_value){
	!ereg("^\_",$_key) && $$_key=$_POST[$_key];
}
foreach($_GET as $_key=>$_value){
	!ereg("^\_",$_key) && $$_key=$_GET[$_key];
}



if(!$job&&!$page){
print <<<EOT

<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<form name="form1" method="post" action="?a">
  <p>數據庫主機: 
    <input type="text" name="dbhost" value="$_dbhost">一般為:localhost
    <br>
    數據庫帳號: 
    <input type="text" name="dbuser" value="$_dbuser">
    <br>
    數據庫密碼: 
    <input type="text" name="dbpw" value="$_dbpw">
    <br>
    數據庫名: 
    <input type="text" name="dbname" value="$_dbname">
    <br>
    數據庫編碼: 
    <input type="text" name="Charset" value="$_Charset">
    可供選擇的有<font color="#FF0000">latin1</font>,<font color="#FF0000">gbk</font>,<font color="#FF0000">utf8</font>,<font color="#FF0000">big5</font> 
    最好選擇 <font color="#FF0000">latin1</font> <br>
  </p>
  <p> 
    <input type="submit" name="Submit" value="開始導入">
    <input type="hidden" name="job" value="1">
  </p>
</form>


EOT;
exit;
}

if($page>0){
	list($dbhost,$dbuser,$dbpw,$dbname,$Charset)=explode("\t",$_COOKIE[mysqlconfig]);
}

if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
	die('MYSQL 連接數據庫失敗,請確定數據庫用戶名,密碼設置正確<br><A HREF="#" onclick="history.back(-1)">點擊返回</A>');
}
if(!@mysql_select_db($dbname)){
	die("MYSQL 連接succeed,但當前使用的數據庫 {$dbname} 不存在<br><A HREF=\"#\" onclick=\"history.back(-1)\">點擊返回</A>");
}

$mysqlV=mysql_get_server_info();

if($mysqlV>'4.1'){
	mysql_query("SET NAMES '$Charset'");
}

if( mysql_get_server_info() > '5.0' ){
	mysql_query("SET sql_mode=''");
}

if(!$page){
	setcookie("mysqlconfig","$dbhost\t$dbuser\t$dbpw\t$dbname\t$Charset");
}

$page=intval($page);
if(is_file("$page.sql")){
	insert_file("$page.sql");
	$page++;
	echo "正在導入第{$page}卷,請稍候...<META HTTP-EQUIV=REFRESH CONTENT='0;URL=?page=$page'>";
	exit;
}else{
	echo "數據導入完畢,如果你是從舊空間轉移數據到新空間,請把舊空間php168目錄下的全部文件移到新空間對應的php168目錄里.但mysql_config.php不必移.因為新的數據庫配置與舊的一般不相同,因而你需要手工配置好新空間的這個文件mysql_config.php";
}

function insert_file($file,$replace=''){
	global $Charset;
	$readfiles=read_file($file);
	if($replace){
		$readfiles=str_replace('$timestamp',"$timestamp",$readfiles);
	}
	$detail=explode("\n",$readfiles);
	$count=count($detail);
	for($j=0;$j<$count;$j++){
		$ck=substr($detail[$j],0,4);
		if( ereg("#",$ck)||ereg("--",$ck) ){
			continue;
		}
		$array[]=$detail[$j];
	}
	$read=implode("\n",$array); 
	$sql=str_replace("\r",'',$read);
	$detail=explode(";\n",$sql);
	$count=count($detail);
	for($i=0;$i<$count;$i++){
		$sql=str_replace("\r",'',$detail[$i]);
		$sql=str_replace("\n",'',$sql);
		$sql=trim($sql);
		if($sql){
			if(eregi("CREATE TABLE",$sql)){
				$mysqlV=mysql_get_server_info();
				$sql=preg_replace("/DEFAULT CHARSET=([a-z0-9]+)/is","",$sql);
				$sql=preg_replace("/TYPE=MyISAM/is","ENGINE=MyISAM",$sql);
				if($mysqlV>'4.1'){
					$sql=str_replace("ENGINE=MyISAM"," ENGINE=MyISAM DEFAULT CHARSET=$Charset ",$sql);
				}
			}
			
			$query=mysql_query($sql);
			if (!$query) die("數據庫出錯:$sql");
			$check++;
		}	
	}
	return $check;
}
function read_file($filename,$method="rb"){
	if($handle=@fopen($filename,$method)){
		@flock($handle,LOCK_SH);
		$filedata=@fread($handle,@filesize($filename));
		@fclose($handle);
	}
	return $filedata;
}