<?php

// Upgrade Discuz! Board from 4.1.0 to 5.0.0

@set_time_limit(1000);

define('IN_DISCUZ', TRUE);
define('DISCUZ_ROOT', './');

$version_old = 'Discuz! 4.1.0';
$version_new = 'Discuz! 5.0.0';

if(@(!include("./config.inc.php")) || @(!include("./include/db_mysql.class.php"))) {
	exit("請先上傳所有新版本的程序文件後再運行本升級程序");
}

header("Content-Type: text/html; charset=$charset");

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

if(empty($dbcharset) && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
	$dbcharset = str_replace('-', '', $charset);
}

if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
	$_COOKIE = &$HTTP_COOKIE_VARS;
	$_SERVER = &$HTTP_SERVER_VARS;
	$_ENV = &$HTTP_ENV_VARS;
	$_FILES = &$HTTP_POST_FILES;
}

$action = ($_POST['action']) ? $_POST['action'] : $_GET['action'];
$step = $_GET['step'];
$start = $_GET['start'];

$cron_pushthread_week = rand(1, 7);
$cron_pushthread_hour = rand(1, 8);

$upgrade1 = <<<EOT

REPLACE INTO cdb_settings (variable, value) VALUES ('ratelogrecord', 0);
REPLACE INTO cdb_settings (variable, value) VALUES ('maxbdays', '0');
REPLACE INTO cdb_settings (variable, value) VALUES ('jsmenustatus', 15);
REPLACE INTO cdb_settings (variable, value) VALUES ('attachbanperiods', '');
REPLACE INTO cdb_settings (variable, value) VALUES ('maxonlinelist', 0);
REPLACE INTO cdb_settings (variable, value) VALUES ('icp', '');
REPLACE INTO cdb_settings (variable, value) VALUES ('regadvance', '0');
REPLACE INTO cdb_settings (variable, value) VALUES ('myrecorddays', '30');
REPLACE INTO cdb_settings (variable, value) VALUES ('maxfavorites', '100');
REPLACE INTO cdb_settings (variable, value) VALUES ('maxsubscriptions', '100');
REPLACE INTO cdb_settings (variable, value) VALUES ('accessemail', '');
REPLACE INTO cdb_settings (variable, value) VALUES ('ec_id', '');
REPLACE INTO cdb_settings (variable, value) VALUES ('ec_commision', '0');
REPLACE INTO cdb_settings (variable, value) VALUES ('allowcsscache', '1');
REPLACE INTO cdb_settings (variable, value) VALUES ('qihoo_location', '1');
REPLACE INTO cdb_settings (variable, value) VALUES ('qihoo_jammer', '1');
REPLACE INTO cdb_settings (variable, value) VALUES ('qihoo_searchboxtxt', '輸入關鍵詞，快速搜索本論壇');
REPLACE INTO cdb_settings (variable, value) VALUES ('qihoo_relatedsort', '1');
REPLACE INTO cdb_settings (variable, value) VALUES ('qihoo_allsearch', '1');
REPLACE INTO cdb_settings (variable, value) VALUES ('qihoo_ustyle', '0');
REPLACE INTO cdb_settings (variable, value) VALUES ('showsettings', '7');
REPLACE INTO cdb_settings (variable, value) VALUES ('editoroptions', '3');
REPLACE INTO cdb_settings (variable, value) VALUES ('threadsticky', '全局置頂,分類置頂,本版置頂');
REPLACE INTO cdb_settings (variable, value) VALUES ('smcols', '4');
DELETE FROM cdb_settings WHERE variable = 'fullmytopics' LIMIT 1;

INSERT INTO cdb_smilies VALUES ('', 0, 'smiley', ':loveliness:', 'loveliness.gif');
INSERT INTO cdb_smilies VALUES ('', 0, 'smiley', ':funk:', 'funk.gif');

INSERT INTO cdb_crons VALUES ('', 1, 'system', '每月主題清理','cleanup_monthly.inc.php', 0, 1143770387, -1, 1, 6, '00');
INSERT INTO cdb_crons VALUES ('', 1, 'system', '每日 X-Space更新用戶', 'supe_daily.inc.php', 1149645106, 1149696000, -1, -1, 0, '0');
INSERT INTO cdb_crons VALUES ('', 1, 'system', '每週主題更新', 'pushthreads_weekly.inc.php', 1150122554, 1150660800, $cron_pushthread_week, -1, $cron_pushthread_hour, '0');

EOT;

$upgrade2 = <<<EOT

ALTER TABLE cdb_announcements ADD redirect tinyint(1) NOT NULL default 0 AFTER subject;

ALTER TABLE cdb_favorites ADD fid smallint(6) unsigned NOT NULL default '0';

ALTER TABLE cdb_forums
	CHANGE allowblog allowshare tinyint (1)  default '0' NOT NULL,
	CHANGE allowtrade allowpostspecial tinyint(1) NOT NULL default '15',
	ADD forumcolumns tinyint(3) unsigned NOT NULL default '0',
	ADD threadcaches tinyint(1) default '0' NOT NULL,
	ADD allowpaytoauthor tinyint(1) unsigned NOT NULL default '1';

ALTER TABLE cdb_forumfields
	ADD digestcredits varchar(255) NOT NULL default '' AFTER replycredits,
	ADD postattachcredits varchar(255) NOT NULL default '' AFTER replycredits,
	ADD getattachcredits varchar(255) NOT NULL default '' AFTER replycredits;

ALTER TABLE cdb_usergroups
	ADD allowpostactivity tinyint(1) NOT NULL default '0'AFTER allowpostpoll,
	ADD allowposttrade tinyint(1) NOT NULL default '0'AFTER allowpostpoll,
	ADD allowpostreward tinyint(1) NOT NULL default '0' AFTER allowpostpoll,
	ADD tradetaxtype tinyint (1) NOT NULL default '1',
	ADD tradetaxs smallint(6) unsigned NOT NULL default '0',
	ADD mintradeprice smallint(6) unsigned NOT NULL default '1',
	ADD maxtradeprice smallint(6) unsigned NOT NULL default '0',
	ADD minrewardprice smallint(6) unsigned NOT NULL default '1',
	ADD maxrewardprice smallint(6) unsigned NOT NULL default '0',
	ADD maxpostsperhour tinyint(3) unsigned NOT NULL default '0' AFTER maxsizeperday;

UPDATE cdb_usergroups SET allowpostactivity=1, allowposttrade=1, allowpostreward=1 WHERE radminid>0 OR creditshigher>=200;

ALTER TABLE cdb_buddys ADD grade tinyint(3) unsigned default '1' NOT NULL AFTER buddyid;

ALTER TABLE cdb_bbcodes ADD icon varchar(255) NOT NULL AFTER tag ;
UPDATE cdb_bbcodes SET icon = 'bb_flash.gif' WHERE tag ='flash';
UPDATE cdb_bbcodes SET icon = 'bb_wmv.gif' WHERE tag ='wmv';
UPDATE cdb_bbcodes SET icon = 'bb_fly.gif' WHERE tag ='fly';
UPDATE cdb_bbcodes SET icon = 'bb_qq.gif' WHERE tag ='qq';
UPDATE cdb_bbcodes SET icon = 'bb_ra.gif' WHERE tag ='ra';
UPDATE cdb_bbcodes SET icon = 'bb_rm.gif' WHERE tag ='rm';
UPDATE cdb_bbcodes SET icon = 'bb_wma.gif' WHERE tag ='wma';

EOT;

$upgrade3 = <<<EOT

ALTER TABLE cdb_members
	ADD editormode tinyint( 1 ) unsigned NOT NULL default '2',
	ADD customshow tinyint( 1 ) unsigned  NOT NULL default '26';
EOT;

$upgrade4 = <<<EOT

ALTER TABLE cdb_threads
	CHANGE poll special tinyint(1) NOT NULL default '0';
EOT;

$upgrade5 = <<<EOT

ALTER TABLE cdb_attachments
	ADD isimage tinyint(1) unsigned NOT NULL default '0',
	ADD uid mediumint(8) unsigned NOT NULL default '0',
	ADD INDEX uid (uid);
EOT;

$upgrade6 = <<<EOT

DROP TABLE IF EXISTS cdb_myposts;
CREATE TABLE cdb_myposts (
  uid mediumint(8) unsigned NOT NULL default '0',
  tid mediumint(8) unsigned NOT NULL default '0',
  pid int(10) unsigned NOT NULL default '0',
  position smallint(6) unsigned NOT NULL default '0',
  dateline int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (uid,tid),
  KEY tid (tid,dateline)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_mythreads;
CREATE TABLE cdb_mythreads (
  uid mediumint(8) unsigned NOT NULL default '0',
  tid mediumint(8) unsigned NOT NULL default '0',
  dateline int(10) NOT NULL default '0',
  PRIMARY KEY  (uid,tid),
  KEY tid (tid,dateline)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_polls_temp;
RENAME TABLE cdb_polls TO cdb_polls_temp;

DROP TABLE IF EXISTS cdb_polls;
CREATE TABLE cdb_polls (
 tid mediumint(8) unsigned NOT NULL default '0',
 multiple tinyint(1) NOT NULL default '0',
 visible tinyint(1) NOT NULL default '0',
 maxchoices tinyint(3) unsigned NOT NULL default '0',
 expiration int(10) unsigned NOT NULL default '0',
 PRIMARY KEY(tid)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_polloptions;
CREATE TABLE cdb_polloptions (
 polloptionid int(10) unsigned NOT NULL auto_increment,
 tid mediumint(8) unsigned NOT NULL default '0',
 votes mediumint(8) unsigned NOT NULL default '0',
 displayorder tinyint(3) NOT NULL default '0',
 polloption varchar(80) NOT NULL default '',
 voterids mediumtext NOT NULL,
 PRIMARY KEY(polloptionid),
 KEY tid (tid, displayorder)
)TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_trades;
CREATE TABLE cdb_trades (
  tid mediumint(8) unsigned NOT NULL,
  sellerid mediumint(8) unsigned NOT NULL,
  seller char(15) NOT NULL,
  account char(50) NOT NULL,
  subject char(100) NOT NULL,
  price decimal(6,2) NOT NULL,
  amount smallint(6) unsigned NOT NULL default '1',
  quality tinyint(1) unsigned NOT NULL default '0',
  locus char(20) NOT NULL,
  transport tinyint(1) NOT NULL default '0',
  ordinaryfee smallint(4) unsigned NOT NULL default '0',
  expressfee smallint(4) unsigned NOT NULL default '0',
  emsfee smallint(4) unsigned NOT NULL default '0',
  itemtype tinyint(1) NOT NULL default '0',
  dateline int(10) unsigned NOT NULL default '0',
  expiration int(10) unsigned NOT NULL default '0',
  lastbuyer char(15) NOT NULL,
  lastupdate int(10) unsigned NOT NULL default '0',
  totalitems smallint(5) unsigned NOT NULL default '0',
  tradesum decimal(8,2) NOT NULL default '0.00',
  closed tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (tid),
  KEY sellerid (sellerid),
  KEY totalitems (totalitems),
  KEY tradesum (tradesum)
)TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_tradelog;
CREATE TABLE cdb_tradelog (
  tid mediumint(8) unsigned NOT NULL,
  orderid char(32) NOT NULL,
  tradeno char(32) NOT NULL,
  subject char(100) NOT NULL,
  price decimal(6,2) NOT NULL default '0.00',
  quality tinyint(1) unsigned NOT NULL default '0',
  itemtype tinyint(1) NOT NULL default '0',
  number smallint(5) unsigned NOT NULL default '0',
  tax decimal(6,2) unsigned NOT NULL default '0.00',
  locus char(100) NOT NULL,
  sellerid mediumint(8) unsigned NOT NULL,
  seller char(15) NOT NULL,
  selleraccount char(50) NOT NULL,
  buyerid mediumint(8) unsigned NOT NULL,
  buyer char(15) NOT NULL,
  buyercontact char(50) NOT NULL,
  buyercredits smallint(5) unsigned NOT NULL default '0',
  buyermsg char(200) default NULL,
  status tinyint(1) NOT NULL default '0',
  lastupdate int(10) unsigned NOT NULL default '0',
  UNIQUE KEY orderid (orderid),
  KEY sellerid (sellerid),
  KEY buyerid (buyerid),
  KEY tid (tid),
  KEY status (status),
  KEY buyerlog (buyerid,status,lastupdate),
  KEY sellerlog (sellerid,status,lastupdate)
)TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_rewardlog;
CREATE TABLE cdb_rewardlog (
 tid mediumint(8) unsigned NOT NULL default '0',
 authorid mediumint(8) unsigned NOT NULL default '0',
 answererid mediumint(8) unsigned NOT NULL default '0',
 dateline int(10) unsigned default '0',
 netamount int(10) unsigned NOT NULL default '0',
 KEY userid (authorid,answererid)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_activities;
CREATE TABLE cdb_activities (
  tid mediumint(8) unsigned NOT NULL default '0',
  uid mediumint(8) unsigned NOT NULL default '0',
  cost mediumint(8) unsigned NOT NULL default '0',
  starttimefrom int(10) unsigned NOT NULL default '0',
  starttimeto int(10) unsigned NOT NULL default '0',
  place char(40) NOT NULL default '',
  class char(20) NOT NULL default '',
  gender tinyint(1) NOT NULL default '0',
  number smallint(5) unsigned NOT NULL default '0',
  expiration int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (tid),
  KEY uid (uid,starttimefrom)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_activityapplies;
CREATE TABLE cdb_activityapplies (
  applyid int(10) unsigned NOT NULL auto_increment,
  tid mediumint(8) unsigned NOT NULL default '0',
  username char(15) NOT NULL default '',
  uid mediumint(8) unsigned NOT NULL default '0',
  message char(200) NOT NULL default '',
  verified tinyint(1) NOT NULL default '0',
  dateline int(10) unsigned NOT NULL default '0',
  payment mediumint(8) NOT NULL default '0',
  PRIMARY KEY  (applyid),
  KEY uid (uid),
  KEY tid (tid),
  KEY dateline (tid,dateline)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_pushedthreads;
CREATE TABLE cdb_pushedthreads (
  id mediumint(8) unsigned NOT NULL,
  tid mediumint(8) unsigned NOT NULL default '0',
  status tinyint(1) NOT NULL default '0',
  dateline int(10) unsigned NOT NULL default '0',
  subject char(80) NOT NULL,
  message text NOT NULL,
  PRIMARY KEY  (id),
  KEY displayorder (status,dateline)
) TYPE=MyISAM;

EOT;

$upgrademsg = array(
	1 => '論壇升級第 1 步: 增加基本設置<br>',
	2 => '論壇升級第 2 步: 調整論壇數據表結構<br>',
	3 => '論壇升級第 3 步: 調整會員數據表結構<br>',
	4 => '論壇升級第 4 步: 增加主題數據表結構<br>',
	5 => '論壇升級第 5 步: 增加附件數據表結構<br>',
	6 => '論壇升級第 6 步: 新增部分數據表<br>',
	7 => '論壇升級第 7 步: 轉換投票數據，如果您的投票主題比較多，此步驟需要較長時間<br>',
	8 => '論壇升級第 8 步: 轉換附件數據，如果您的附件比較多，此步驟需要較長時間<br>',
	9 => '論壇升級第 9 步: 轉換論壇欄目資料，如果您的論壇欄目比較多，此步驟需要較長時間<br>',
	10 => '論壇升級第 10 步: 安裝 Discuz!5.0.0 新風格<br>',
	11 => '論壇升級第 11 步: 增加 X-Space 和 SupeSite 支持<br>',
	12 => '論壇升級第 12 步: 升級全部完畢<br>',
);

if(!$action) {
	echo "本程序用於升級 $version_old 到 $version_new,請確認之前已經順利安裝 $version_old <br><br><br>",
		"<b><font color=\"red\">本升級程序只能從 $version_old 升級到 $version_new ,運行之前,請確認已經上傳 $version_new 的全部文件和目錄</font></b><br>",
		"<b><font color=\"red\">升級前請打開瀏覽器 JavaScript 支持,整個過程是自動完成的,不需人工點擊和干預.<br>升級之前務必備份數據庫資料,否則可能產生無法恢復的後果!<br></font></b><br><br>",
		"正確的升級方法為:<br><ol><li>關閉原有論壇,上傳 $version_new 的全部文件和目錄,覆蓋\服務器上的 $version_old <li>上傳本程序到 Discuz! 目錄中，並確認已經重新配置好 config.inc.php <li>運行本程序,直到出現升級完成的提示</ol><br>",
		"<a href=\"$PHP_SELF?action=upgrade&step=1\">如果您已確認完成上面的步驟,請點這裡升級</a>";
} else {

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

	$step = intval($step);
	echo $upgrademsg[$step];
	flush();

	if($step == 1) {

		dir_clear('./forumdata/cache');
		dir_clear('./forumdata/templates');

		if(!dir_writeable('./forumdata/threadcaches')) {
			echo '升級檢測失敗，無法建立目錄 /forumdata/threadcaches，請手工建立此目錄，然後重新運行升級程序';
			exit;
		}

		runquery($upgrade1);

		echo "第 $step 步升級成功\<br><br>";
		redirect("?action=upgrade&step=".($step+1));

	} elseif($step == 2) {

		runquery($upgrade2);

		echo "第 $step 步升級成功\<br><br>";
		redirect("?action=upgrade&step=".($step+1));

	} elseif($step == 3) {

		runquery($upgrade3);

		echo "第 $step 步升級成功\<br><br>";
		redirect("?action=upgrade&step=".($step+1));

	} elseif($step == 4) {

		runquery($upgrade4);

		echo "第 $step 步升級成功\<br><br>";
		redirect("?action=upgrade&step=".($step+1));

	} elseif($step == 5) {

		runquery($upgrade5);

		echo "第 $step 步升級成功\<br><br>";
		redirect("?action=upgrade&step=".($step+1));

	} elseif($step == 6) {

		runquery($upgrade6);

		echo "第 $step 步升級成功\<br><br>";
		redirect("?action=upgrade&step=".($step+1));

	} elseif($step == 7) {

		$limit = 500; // 每次升級多少投票主題
		$start = intval($start);
		$next = FALSE;

		$query = $db->query("SELECT tid, pollopts from {$tablepre}polls_temp LIMIT $start, $limit");
		while($array = $db->fetch_array($query)) {

			$next = TRUE;

			$polltid = $array['tid'];
			$pollopts = unserialize($array['pollopts']);

			if(!empty($pollopts['options'])) {
				foreach($pollopts['options'] as $temp_options) {
					$db->query("REPLACE INTO {$tablepre}polloptions (tid, votes, polloption) VALUES ('$polltid', '$temp_options[1]', '$temp_options[0]')");
				}
			}

			if($pollopts['multiple']) {
				$maxchoices = 0;
			} else {
				$maxchoices = 1;
			}

			$closedquery = $db->query("SELECT closed FROM {$tablepre}threads WHERE tid='$polltid'");
			$closed = $db->result($closedquery, 0);
			$expiration = $closed ? $timestamp : 0;

			$db->query("REPLACE INTO {$tablepre}polls (tid, multiple, visible, maxchoices, expiration) VALUES ('$polltid', '$pollopts[multiple]', '1', '$maxchoices', '$expiration')");

		}

		if($next) {
			echo "正在進行第 $step 步升級 起始列數: $start<br><br>";
			redirect("?action=upgrade&step=$step&start=".($start+$limit));
		} else {
			$db->query("DROP TABLE IF EXISTS cdb_polls_temp");
			echo "第 $step 步升級成功\<br><br>";
			redirect("?action=upgrade&step=".($step+1));
		}

	} elseif($step == 8) {

		$limit = 500; // 每次升級多少附件
		$next = FALSE;
		$start = $start ? intval($start) : 0;

		$query = $db->query("SELECT a.aid, a.filetype, p.authorid FROM {$tablepre}attachments a, {$tablepre}posts p WHERE a.pid=p.pid LIMIT $start, $limit");
		while($data = $db->fetch_array($query)) {
			$next = TRUE;
			$isimage = substr($data['filetype'], 0, 5) == 'image' ? '1' : '0';
			$db->query("UPDATE {$tablepre}attachments SET isimage='$isimage', uid='$data[authorid]' WHERE aid='$data[aid]'");
		}

		if($next) {
			redirect("?action=upgrade&step=$step&start=".($start + $limit));
		} else {
			echo "第 $step 步升級成功\<br><br>";
			redirect("?action=upgrade&step=".($step+1));
		}

	} elseif($step == 9) {

		$limit = 100; // 每次升級多少欄目板塊
		$next = FALSE;
		$start = $start ? intval($start) : 0;

		$query = $db->query("SELECT fid, threadtypes FROM {$tablepre}forumfields WHERE 1 LIMIT $start, $limit");
		while($forum = $db->fetch_array($query)) {
			$next = TRUE;
			$forum['threadtypes'] = unserialize($forum['threadtypes']);

			if(!empty($forum['threadtypes']['types'])) {
				$newthreadtypes = addslashes(serialize(array
					(
					'required' => (bool)$forum['threadtypes']['required'],
					'listable' => (bool)$forum['threadtypes']['listable'],
					'prefix' => (bool)$forum['threadtypes']['prefix'],
					'types' => $forum['threadtypes']['types'],
					'selectbox' => '',
					'flat' => $forum['threadtypes']['types'],
					)));
			} else {
				$newthreadtypes = '';
			}
			$db->query("UPDATE {$tablepre}forumfields SET threadtypes='$newthreadtypes' WHERE fid='$forum[fid]'");
		}

		if($next) {
			redirect("?action=upgrade&step=$step&start=".($start + $limit));
		} else {
			echo "第 $step 步升級成功\<br><br>";
			redirect("?action=upgrade&step=".($step+1));
		}

	} elseif($step == 10) {

		$query = $db->query("SELECT styleid FROM {$tablepre}styles ORDER BY styleid DESC LIMIT 1");
		$styleid = intval($db->result($query, 0));
		$newstyleid = $styleid + 1;

		$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('indexname', 'index.php')");
		$db->query("REPLACE INTO {$tablepre}styles (styleid, name, available, templateid) VALUES ('$newstyleid', 'Discuz! 5 Default', '1', '1')");
		$db->query("REPLACE INTO {$tablepre}stylevars (styleid, variable, substitute) VALUES
			('$newstyleid', 'boardimg', 'logo.gif'),
			('$newstyleid', 'nobold', '0'),
			('$newstyleid', 'msgfontsize', '12px'),
			('$newstyleid', 'fontsize', '12px'),
			('$newstyleid', 'font', 'Tahoma, Verdana'),
			('$newstyleid', 'tablespace', '4'),
			('$newstyleid', 'tablewidth', '98%'),
			('$newstyleid', 'borderwidth', '1'),
			('$newstyleid', 'text', '#333333'),
			('$newstyleid', 'tabletext', '#333333'),
			('$newstyleid', 'catcolor', '#FFFFD9'),
			('$newstyleid', 'headertext', '#154BA0'),
			('$newstyleid', 'headercolor', 'header_bg.gif'),
			('$newstyleid', 'bordercolor', '#86B9D6'),
			('$newstyleid', 'link', '#154BA0'),
			('$newstyleid', 'altbg2', '#FFFFFF'),
			('$newstyleid', 'altbg1', '#F5FBFF'),
			('$newstyleid', 'bgcolor', '#FFFFFF'),
			('$newstyleid', 'imgdir', 'images/default'),
			('$newstyleid', 'smdir', 'images/smilies'),
			('$newstyleid', 'cattext', '#92A05A'),
			('$newstyleid', 'smfontsize', '11px'),
			('$newstyleid', 'smfont', 'Arial, Tahoma'),
			('$newstyleid', 'maintablewidth', '98%'),
			('$newstyleid', 'maintablecolor', '#FFFFFF'),
			('$newstyleid', 'innerborderwidth', '0'),
			('$newstyleid', 'innerbordercolor', '#D6E0EF'),
			('$newstyleid', 'bgborder', '#BBE9FF'),
			('$newstyleid', 'inputborder', '#7AC4EA'),
			('$newstyleid', 'mainborder', '#154BA0'),
			('$newstyleid', 'catborder', '#DEDEB8'),
			('$newstyleid', 'lighttext', '#666666'),
			('$newstyleid', 'headermenu', 'menu_bg.gif'),
			('$newstyleid', 'postnoticebg', '#FDFFF2'),
			('$newstyleid', 'msgheader', '#F3F8D7'),
			('$newstyleid', 'msgheadertext', '#000000'),
			('$newstyleid', 'msgtext', '#FDFFF2'),
			('$newstyleid', 'headermenutext', '#154BA0'),
			('$newstyleid', 'navtext', '#154BA0'),
			('$newstyleid', 'menubg', '#D9EEF9'),
			('$newstyleid', 'menutext', '#154BA0'),
			('$newstyleid', 'menuhltext', '#FFFFFF'),
			('$newstyleid', 'menuhlbg', '#7AC4EA'),
			('$newstyleid', 'calendartext', '#000000'),
			('$newstyleid', 'calendarexpire', '#999999'),
			('$newstyleid', 'calendarchecked', '#FF0000'),
			('$newstyleid', 'calendartoday', '#00BB00');");
		$db->query("UPDATE {$tablepre}settings SET value = '$newstyleid' WHERE variable = 'styleid'");
		$db->query("UPDATE {$tablepre}members SET styleid = '0'");
		$db->query("UPDATE {$tablepre}styles SET available = '0' WHERE styleid != '$newstyleid'");

		echo "第 $step 步升級成功\<br><br>";
		redirect("?action=upgrade&step=".($step+1));

	} elseif($step == 11) {

		$start = $start ? intval($start) : 1;

		if($start == 1) {
			$supe = array();
			$query = $db->query("SELECT * FROM {$tablepre}settings WHERE variable IN ('supe_maxupdateusers', 'supe_status', 'supe_siteurl', 'supe_sitename', 'supe_tablepre')");
			while($setting = $db->fetch_array($query)) {
				$supe[$setting['variable']] = $setting['value'];
			}
			$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('supe_maxupdateusers', '$supe[supe_maxupdateusers]')");
			$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('supe_status', '$supe[supe_status]')");
			$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('supe_siteurl', '$supe[supe_siteurl]')");
			$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('supe_sitename', '$supe[supe_sitename]')");
			$db->query("REPLACE INTO {$tablepre}settings (variable, value) VALUES ('supe_tablepre', '$supe[supe_tablepre]')");

			$db->query("ALTER TABLE {$tablepre}forumfields ADD supe_pushsetting TEXT NOT NULL", 'SILENT');
			$db->query("ALTER TABLE {$tablepre}admingroups ADD supe_allowpushthread tinyint(1) NOT NULL default '0'", 'SILENT');
			$db->query("UPDATE {$tablepre}admingroups SET supe_allowpushthread='1' WHERE admingid='1'", 'SILENT');

			echo "第 $step 步 步驟[ $start ] 升級成功\<br><br>";
			redirect("?action=upgrade&step=$step&start=2");

		} else {

			$db->query("ALTER TABLE {$tablepre}members ADD xspacestatus tinyint(1) NOT NULL default '0'", 'SILENT');
			$db->query("ALTER TABLE {$tablepre}threads ADD itemid mediumint(8) unsigned NOT NULL default '0'", 'SILENT');
			$db->query("ALTER TABLE {$tablepre}threads ADD supe_pushstatus tinyint(1) NOT NULL default '0'", 'SILENT');

			echo "第 $step 步 步驟[ $start ] 升級成功\<br><br>";
			redirect("?action=upgrade&step=".($step+1));

		}

	} else {

		echo '<br>恭喜您論壇數據升級成功\，接下來請您：<ol><li><b>必刪除本程序</b>'.
			'<li>使用管理員身份登錄論壇，進入後台，更新緩存'.
			'<li>進行論壇註冊、登錄、發貼等常規測試，看看運行是否正常'.
			'<li>如果您希望啟用 <b>'.$version_new.'</b> 提供的新功\能，你還需要對於論壇基本設置、欄目、會員組等等進行重新設置</ol><br><br>'.
			'<b>感謝您選用我們的產品！</b>';
		exit;
	}
}

function dir_clear($dir) {
	$directory = dir($dir);
	while($entry = $directory->read()) {
		$filename = $dir.'/'.$entry;
		if(is_file($filename)) {
			@unlink($filename);
		}
	}
	$directory->close();
}

function dir_writeable($dir) {
	if(!is_dir($dir)) {
		@mkdir($dir, 0777);
	}
	if(is_dir($dir)) {
		if($fp = @fopen("$dir/test.test", 'w')) {
			@fclose($fp);
			@unlink("$dir/test.test");
			$writeable = 1;
		} else {
			$writeable = 0;
		}
	}
	return $writeable;
}

function daddslashes($string) {
	if(is_array($string)) {
		foreach($string as $key => $val) {
			$string[$key] = daddslashes($val, $force);
		}
	} else {
		$string = addslashes($string);
	}
	return $string;
}

function loginit($log) {
	global $lang;

	$fp = @fopen('./forumdata/'.$log.'.php');
	@fwrite($fp, "<?PHP exit(\"Access Denied\"); ?>\n");
	@fclose($fp);
}

function runquery($query) {
	global $db, $tablepre, $dbcharset;
	$expquery = explode(";", $query);
	foreach($expquery as $sql) {
		$sql = trim($sql);
		if($sql == '' || $sql[0] == '#') continue;

		$sql = str_replace(' cdb_', ' '.$tablepre, $sql);
		if(strtoupper(substr($sql, 0, 12)) == 'CREATE TABLE') {
			$db->query(createtable($sql, $dbcharset));
		} else {
			$db->query($sql);
		}
	}
}

function redirect($url) {

	echo "<script>",
		"function redirect() {window.location.replace('$url');}\n",
		"setTimeout('redirect();', 500);\n",
		"</script>",
		"<br><br><a href=\"$url\">瀏覽器會自動跳轉頁面，無需人工干預。<br>除非當您的瀏覽器沒有自動跳轉時，請點擊這裡</a>";
	flush();

}

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");
}

?>