<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	升級文件所用函數信息

	$RCSfile: func.php,v $
	$Revision: 1.8 $
	$Date: 2006/11/01 02:16:16 $
*/

if(!defined('IN_SUPESITE')) {
	exit('Access Denied');
}

/**
 * 數據庫結構升級
 *
 * @param string $sql
 */

function dbupdate() {
	global $dbhost, $dbuser, $dbpw, $dbname, $dbcharset, $charset, $tablepre, $stablepre;
	global $sqlfile;

	//連接數據庫
	if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
		exit('無法連接數據庫');
	}
	$version = mysql_get_server_info();
	if($version > '4.1') {
		//選擇字符集
		if(empty($dbcharset) && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
			$dbcharset = str_replace('-', '', $charset);
		}
		if(!empty($dbcharset)) mysql_query("SET NAMES '$dbcharset'");
	}
	if($version > '5.0') {
		mysql_query("SET sql_mode=''");
	}
	//選擇數據表
	if($dbname) {
		mysql_select_db($dbname);
	}

	//讀取sql，運行
	$fp = fopen($sqlfile, 'rb');
	$sql = fread($fp, 2048000);
	fclose($fp);

	//運行sql文
	runquery($sql);
}

/**
 * 運行SQL文
 *
 * @param string $sql
 */

function runquery($sql) {
	global $tablepre, $stablepre, $dbcharset;
	global $siteurl;

	//表名前綴
	$sql = str_replace("\r", "\n", str_replace(' supe_', ' '.$stablepre, $sql));
	$sql = str_replace("\r", "\n", str_replace(' `supe_', ' `'.$stablepre, $sql));
	$sql = str_replace("\r", "\n", str_replace(' cdb_', ' '.$tablepre, $sql));
	$sql = str_replace("\r", "\n", str_replace(' `cdb_', ' `'.$tablepre, $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[0].$query[1] == '--' ? '' : $query;
		}
		$num++;
	}
	unset($sql);

	foreach($ret as $query) {
		$query = trim($query);
		if(strpos($query, '$')) {
			$query = preg_replace("/\\$([a-zA-Z0-9_]+)/ie", "$\\1", $query);
		}
		if($query) {
			if(substr($query, 0, 12) == 'CREATE TABLE') {
				$name = preg_replace("/CREATE TABLE ([a-z0-9_]+) .*/is", "\\1", $query);
				echo '<li>建立數據表 '.$name.' ...</li>';
				if(!mysql_query(createtable($query, $dbcharset))) {
					echo '<li><b>發生錯誤</b>:<textarea cols="80" rows="3">'.shtmlspecialchars($query).'</textarea></li>';
					echo '<li><b>錯誤代碼</b>:'.mysql_errno().'</li>';
					echo '<li><b>錯誤反饋</b>:'.mysql_error().'</li>';
					echo '<li class="noborder"></li>';
					exit;
				}
			} else {
				if(!mysql_query($query)) {
					echo '<li><b>發生錯誤</b>:<textarea cols="80" rows="3">'.shtmlspecialchars($query).'</textarea></li>';
					echo '<li><b>錯誤代碼</b>:'.mysql_errno().'</li>';
					echo '<li><b>錯誤反饋</b>:'.mysql_error().'</li>';
					echo '<li class="noborder"></li>';
					exit;
				}
			}
		}
	}
}


/**
 * 建立數據表
 *
 * @param string $sql
 * @param string $dbcharset
 * @return string
 */
function createtable($sql, $dbcharset) {
	$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=$dbcharset" : " TYPE=$type");
}

function tplheader() {
	global $charset;
	echo '<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
	<html>
	<head>
	<title>SupeSite/X-Space程序升級</title>
	<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'">
	<style>
	* {
		font-family: Arial, Helvetica, sans-serif, "細明體";
		font-size: 14px;
		color: #000;
		word-break: break-all;
	}
	textarea {
		font-size: 12px;
	}
	li {
		line-height: 2em;
		height: 2em;
	}
	.noborder {
		list-style: none;
	}
	</style>
	</head>
	<body>
	';
}

function tplfooter() {
	echo '</body>
	</html>
	';
}

?>