<?php

/*
	[DISCUZ!] upgrade1.php - upgrade 3.0RC1 to Discuz! 1.0/1.01
	This is NOT a freeware, use is subject to license terms

	Version: 1.0.0
	Author: Crossday (info@discuz.net)
	Copyright: Crossday Studio (www.crossday.com)
	Last Modified: 2002/10/6 17:00
*/



header("Content-Type: text/html; charset=gb2312");
define("IN_CDB", TRUE);
require "./config.php";
require "./functions.php";
require "./lib/$database.php";

error_reporting(E_ERROR | E_WARNING | E_PARSE);
@set_magic_quotes_runtime(0);

$action = ($HTTP_POST_VARS["action"]) ? $HTTP_POST_VARS["action"] : $HTTP_GET_VARS["action"];

$upgrade = <<<EOT

UPDATE cdb_settings SET version='1.0';
ALTER TABLE cdb_themes DROP top;
UPDATE cdb_themes SET headercolor='header_bg.gif' WHERE themename='標準界面';
UPDATE cdb_members SET status='正式會員' WHERE status='遊客';

ALTER TABLE cdb_settings ADD postcredits1 tinyint(3) NOT NULL AFTER smcols, ADD digistcredits1 tinyint(3) NOT NULL AFTER postcredits1;
UPDATE cdb_settings SET postcredits1=postcredits, digistcredits1=digistcredits;
ALTER TABLE cdb_settings DROP postcredits, DROP digistcredits, CHANGE postcredits1 postcredits tinyint(3) NOT NULL, CHANGE digistcredits1 digistcredits tinyint(3) NOT NULL;
ALTER TABLE cdb_settings ADD karmactrl smallint(6) UNSIGNED NOT NULL AFTER floodctrl;
UPDATE cdb_settings SET karmactrl='600';


ALTER TABLE cdb_posts DROP INDEX tid, ADD INDEX tid (tid, dateline);
ALTER TABLE cdb_threads DROP hide;

ALTER TABLE cdb_attachments CHANGE filename filename VARCHAR(255) NOT NULL;
ALTER TABLE cdb_attachments CHANGE attachment attachment VARCHAR(255) NOT NULL;

EOT;


if(!$action) {
	echo"本程序用於升級 CDB 3.0 RC1 到 Discuz! 1.0,請確認之前已經順利安裝 CDB 3.0 RC1<br><br><br>";
	echo"<b><font color=\"red\">運行本升級程序之前,請確認已經上傳 2.0.0 RC1 的全部文件和目錄</font></b><br><br>";
	echo"<b><font color=\"red\">本程序只能從 CDB 3.0 RC1 升級到 Discuz! 1.0,切勿使用本程序從其他版本升級,否則可能會破壞掉數據庫資料.<br><br>強烈建議您升級之前備份數據庫資料!</font></b><br><br>";
	echo"正確的升級方法為:<br>1. 上傳 Discuz! 1.0 版的全部文件和目錄,覆蓋服務器上的 CDB 3.0 RC1;<br>2. 上傳本程序($PHP_SELF)到 Discuz! 目錄中;<br>3. 運行本程序,直到出現升級完成的提示;<br>4. 在 Discuz! 系統設置中恢復默認模板,更新緩存,升級完成.<br><br>";
	echo"<a href=\"$PHP_SELF?action=upgrade\">如果您已確認完成上面的步驟,請點這裡升級</a>";
} else {

	$tables = array('attachments', 'announcements', 'banned', 'caches', 'favorites', 'forumlinks', 'forums', 'members', 'memo',
		'news', 'posts', 'searchindex', 'sessions', 'settings', 'smilies', 'stats', 'subscriptions', 'templates', 'themes',
		'threads', 'u2u', 'usergroups', 'words', 'buddys');
	foreach($tables as $tablename) {
		${"table_".$tablename} = $tablepre.$tablename;
	}
	unset($tablename);

	$db = new dbstuff;
	$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
	$db->select_db($dbname);
	unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);

	runquery($upgrade);
	echo "升級完成,請恢復默認模板,更新緩存以便完成升級.";

}

function runquery($query) {
	global $tablepre;
	$expquery = explode(";", $query);
	foreach($expquery as $sql) {
		$sql = trim($sql);
		if($sql != "" && $sql[0] != "#") {
			mysql_query(str_replace("cdb_", $tablepre, $sql)) or die(mysql_error());
		}
	}
}

?>