<?php
error_reporting(0);
define('VERSION_5W',8.0);
ob_start();
include_once('../config.php');
$installcharset = "utf8";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>5w - 安装脚本</title>
<link href="install.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main">
  <form method="post" action="<?php echo $php_self;?>">
    <p class="title">5w V<?php echo VERSION_5W;?> 安装向导</p>
    <hr noshade="noshade" />

<?php

			$dbhost 	= DBHOST;
			$dbuser 	= DBUSER;
			$dbpw 		= DBPWD;
			$dbname 	= DBNAME;
			$dbprefix 	= DBPREFIX;

		$db_prefix   = '';
		$dbresult = array();
		$rs = mysql_connect(DBHOST,DBUSER,DBPWD);
		if(!$rs)echo 'error';else echo '数据库连接成功<br />';

		$rs = mysql_query("use ".DBNAME);
		if(!$rs)
			$dbresult[] = 'use '.DBNAME.' 异常，请检查是否有此数据库';
		$rs = mysql_query("set names utf8 ");
		//@mysql_select_db(DBNAME);
$sql = 'SELECT version FROM '.DBPREFIX.'site_setting';
$query = mysql_query($sql);
$old_version = mysql_fetch_row($query);
if($old_version[0]=='8.0'){
	echo "您已经更新过了！";exit;
}else{
	$sqlfile = 'database80.sql';
}
if(!is_readable($sqlfile)) {
	exit('数据库文件不存在或者读取<font color="red">失败</font>');
}
$fp = fopen($sqlfile, 'rb');
$sql = fread($fp, 2048000);
fclose($fp);

	  $sql = str_replace("\r", "\n", str_replace('py_', $db_prefix, $sql));
	$ret = array();
	$num = 0;
	foreach(explode(";\n", trim($sql)) as $query) {
		$queries = explode("\n", trim($query));
		foreach($queries as $query) {
			$ret[$num] .= $query[0] == '#' ? '' : $query;
		}
		$num++;
	}
	unset($sql);
	foreach($ret as $query) {
		$query = trim($query);
		if($query) {
			if(substr($query, 0, 12) == 'CREATE TABLE') {
				$name = preg_replace("/CREATE TABLE IF NOT EXISTS `([a-z0-9_]+)` .*/is", "\\1", $query);
				
				$query = preg_replace("/CREATE TABLE IF NOT EXISTS `([a-z0-9_]+)` (.*)/is", "CREATE TABLE IF NOT EXISTS `".$dbprefix."\\1` \\2", $query);
				$rs = mysql_query(createtable($query, $installcharset));
				if(!$rs)
					$dbresult[] = '创建表 '.$dbprefix.$name.' ... <font color="red">失败</font>';
				else{
					echo '创建表 '.$dbprefix.$name.' ... <font color="#0000EE">成功</font><br />';
					$tablenum++;
				}
				
			} else if(substr($query, 0, 10) == 'DROP TABLE'){
				$name = preg_replace("/DROP TABLE IF EXISTS `([a-z0-9_]+)`/is","\\1",$query);
				$query = preg_replace("/DROP TABLE IF EXISTS `([a-z0-9_]+)`/is","DROP TABLE IF EXISTS `".$dbprefix."\\1`",$query);
				$rs = mysql_query($query);
				if(!$rs)
					$dbresult[] = '删除表 '.$dbprefix.$name.' ... <font color="red">失败</font>';
			}else if(substr($query, 0, 6) == 'UPDATE'){
				$name = preg_replace("/UPDATE `([a-z0-9_]+)`/is","\\1",$query);
				$query = preg_replace("/UPDATE `([a-z0-9_]+)`/is","UPDATE `".$dbprefix."\\1`",$query);
				$rs = mysql_query($query);
			}else if(substr($query, 0, 5) == 'ALTER'){
				$name = preg_replace("/ALTER TABLE `([a-z0-9_]+)`/is","\\1",$query);
				$query = preg_replace("/ALTER TABLE `([a-z0-9_]+)`/is","ALTER TABLE `".$dbprefix."\\1`",$query);
				$rs = mysql_query($query);
				if(!$rs)
					$dbresult[] = '添加字段 '.$dbprefix.$name.' ... <font color="red">失败</font>';
			} else {
				$name = preg_replace("/INSERT INTO `([a-z0-9_]+)` .*/is","\\1",$query);
				$query = preg_replace("/INSERT INTO `([a-z0-9_]+)` (.*)/is","INSERT INTO `".$dbprefix."\\1` \\2",$query);
				$rs = mysql_query($query);
				if(!$rs)
					$dbresult[] = '插入数据 '.$dbprefix.$name.' ... <font color="red">失败</font>';
			}
		}
	}
	if(count($dbresult)==0){
		echo '<br>升级成功，请记得覆盖相关文件，请<font color="blue"><a href="../admin/">登录后台</a></font>重新生成所有页面';
	}else{
		echo '<br>升级失败，请手动执行数据库修改文件';
	}
?>
  </form>
</div>
<strong>Powered by <a href="http://www.5w.com/" target="_blank">5W</a>&nbsp;&nbsp;V<?php echo VERSION_5W;?>&nbsp;&nbsp;</strong>
</body>
</html>
<?php

function result($result = 1, $output = 1) {
	if($result) {
		$text = '... <font color="#0000EE">Yes</font><br />';
		if(!$output) {
			return $text;
		}
		echo $text;
	} else {
		$text = '... <font color="#FF0000">No</font><br />';
		if(!$output) {
			return $text;
		}
		echo $text;
	}
}

function runquery($sql) {
	global $installcharset, $db_prefix, $DB, $tablenum;


}

function createtable($sql, $installcharset) {

	$type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql));
	$type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
	return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
		(mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=$installcharset" : " TYPE=$type");
}


?>