<?php

// Upgrade Discuz! Board from 3.0F beta3 to 4.0.0

@set_time_limit(1000);
define('IN_DISCUZ', TRUE);
define('DISCUZ_ROOT', './');

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

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


$upgrade1 = <<<EOT
DROP TABLE IF EXISTS cdb_adminactions;
CREATE TABLE cdb_adminactions (
  admingid smallint(6) unsigned NOT NULL default '0',
  disabledactions text NOT NULL,
  PRIMARY KEY  (admingid)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_advertisements;
CREATE TABLE cdb_advertisements (
  advid mediumint(8) unsigned NOT NULL auto_increment,
  available tinyint(1) NOT NULL default '0',
  type varchar(50) NOT NULL default '0',
  displayorder tinyint(3) NOT NULL default '0',
  title varchar(50) NOT NULL default '',
  targets varchar(255) NOT NULL default '',
  parameters text NOT NULL,
  code text NOT NULL,
  starttime int(10) unsigned NOT NULL default '0',
  endtime int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (advid)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_orders;
CREATE TABLE cdb_orders (
  orderid char(32) NOT NULL default '',
  status char(3) NOT NULL default '',
  buyer char(50) NOT NULL default '',
  admin char(15) NOT NULL default '',
  uid mediumint(8) unsigned NOT NULL default '0',
  amount smallint(6) unsigned NOT NULL default '0',
  price float(7,2) unsigned NOT NULL default '0.00',
  submitdate int(10) unsigned NOT NULL default '0',
  confirmdate int(10) unsigned NOT NULL default '0',
  UNIQUE KEY orderid (orderid),
  KEY submitdate (submitdate),
  KEY uid (uid,submitdate)
) TYPE=MyISAM;


DROP TABLE IF EXISTS cdb_rsscaches;
CREATE TABLE cdb_rsscaches (
  lastupdate int(10) unsigned NOT NULL default '0',
  fid smallint(6) unsigned NOT NULL default '0',
  tid mediumint(8) unsigned NOT NULL default '0',
  dateline int(10) unsigned NOT NULL default '0',
  forum char(50) NOT NULL default '',
  author char(15) NOT NULL default '',
  subject char(80) NOT NULL default '',
  description char(255) NOT NULL default '',
  KEY fid (fid,dateline)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_sessions;
CREATE TABLE cdb_sessions (
  sid char(6) binary NOT NULL default '',
  ip1 tinyint(3) unsigned NOT NULL default '0',
  ip2 tinyint(3) unsigned NOT NULL default '0',
  ip3 tinyint(3) unsigned NOT NULL default '0',
  ip4 tinyint(3) unsigned NOT NULL default '0',
  uid mediumint(8) unsigned NOT NULL default '0',
  username char(15) NOT NULL default '',
  groupid smallint(6) unsigned NOT NULL default '0',
  styleid smallint(6) unsigned NOT NULL default '0',
  invisible tinyint(1) NOT NULL default '0',
  action tinyint(1) unsigned NOT NULL default '0',
  lastactivity int(10) unsigned NOT NULL default '0',
  lastolupdate int(10) unsigned NOT NULL default '0',
  pageviews smallint(6) unsigned NOT NULL default '0',
  seccode smallint(6) unsigned NOT NULL default '0',
  fid smallint(6) unsigned NOT NULL default '0',
  tid mediumint(8) unsigned NOT NULL default '0',
  bloguid mediumint(8) unsigned NOT NULL default '0',
  UNIQUE KEY sid (sid),
  KEY uid (uid),
  KEY bloguid (bloguid)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_validating;
CREATE TABLE cdb_validating (
  uid mediumint(8) unsigned NOT NULL default '0',
  submitdate int(10) unsigned NOT NULL default '0',
  moddate int(10) unsigned NOT NULL default '0',
  admin varchar(15) NOT NULL default '',
  submittimes tinyint(3) unsigned NOT NULL default '0',
  status tinyint(1) NOT NULL default '0',
  message text NOT NULL,
  remark text NOT NULL,
  PRIMARY KEY  (uid),
  KEY status (status)
) TYPE=MyISAM;
EOT;

$upgrade2 = <<<EOT
ALTER TABLE cdb_admingroups  DROP allowhighlight,  DROP allowdigestthread,  DROP allowclose,  DROP allowmove,  DROP allowmerge,  DROP allowsplit,  DROP allowadmincp,  DROP acpactions,  DROP allowrepair;

ALTER TABLE cdb_admingroups ADD allowmodpost TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER
allowstickthread , ADD allowmassprune TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER
allowdelpost ,ADD allowrefund TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER allowmassprune ,ADD allowcensorword TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER allowrefund , ADD allowbanip TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER
allowviewip ,ADD allowedituser TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER allowbanip ,
ADD allowmoduser TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER allowedituser , ADD allowbanuser TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER allowmoduser , ADD allowpostannounce TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER allowbanuser , ADD allowviewlog TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER allowpostannounce ;

ALTER TABLE cdb_announcements DROP extlink ;

ALTER TABLE cdb_attachments DROP permid ,
DROP displayorder ;
ALTER TABLE cdb_attachments CHANGE readperm readperm TINYINT( 3 ) UNSIGNED NOT NULL
DEFAULT '0' ;
UPDATE cdb_attachments SET readperm=0;
ALTER TABLE cdb_medals DROP description ;
ALTER TABLE cdb_buddys ADD dateline INT( 10 ) UNSIGNED DEFAULT '0' NOT NULL ,
ADD description CHAR( 255 ) NOT NULL DEFAULT '';
ALTER TABLE cdb_forumfields DROP threadperms ;
ALTER TABLE cdb_forums ADD alloweditrules TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER
allowtrade ;
ALTER TABLE cdb_members ADD extgroupids CHAR( 60 ) DEFAULT '' NOT NULL AFTER
groupexpiry ;
ALTER TABLE cdb_members ADD pageviews MEDIUMINT( 8 ) unsigned DEFAULT '0' NOT NULL
AFTER oltime ;
ALTER TABLE cdb_members ADD pmsound TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER
timeformat ;
ALTER TABLE cdb_members DROP disablesigviews ;
DROP TABLE IF EXISTS cdb_modworks;
ALTER TABLE cdb_onlinetime CHANGE lastolupdate lastupdate INT( 10 ) UNSIGNED NOT NULL
DEFAULT '0' ;
ALTER TABLE cdb_paymentlog DROP pid ,
DROP aid ,
DROP permid ;
ALTER TABLE cdb_ranks DROP INDEX postshigher ;
ALTER TABLE cdb_ratelog DROP authorid ;
ALTER TABLE cdb_smilies ADD displayorder TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER id ;
ALTER TABLE cdb_templates DROP charset ;
ALTER TABLE cdb_threads DROP permid ;
ALTER TABLE cdb_threads CHANGE readperm readperm TINYINT( 3 ) UNSIGNED NOT NULL
DEFAULT '0';
ALTER TABLE cdb_threads ADD price SMALLINT( 6 ) DEFAULT '0' NOT NULL AFTER readperm ;
UPDATE cdb_threads SET readperm=0;
ALTER TABLE cdb_usergroups CHANGE raterange raterange CHAR( 150 ) NOT NULL ;
ALTER TABLE cdb_usergroups ADD allowmultigroups TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER
allowvote ;
ALTER TABLE cdb_usergroups CHANGE allowsetpostperm allowsetreadperm TINYINT( 1 ) NOT
NULL DEFAULT '0' ;
ALTER TABLE cdb_usergroups DROP allowsetpostprice ,
DROP allowsetattachprice ;
ALTER TABLE cdb_usergroups ADD allowcusbbcode TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER
allowhtml ,
ADD allownickname TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER allowcusbbcode ;

ALTER TABLE cdb_usergroups ADD maxprice SMALLINT( 6 ) UNSIGNED DEFAULT '0' NOT NULL
AFTER reasonpm ;
ALTER TABLE cdb_usergroups ADD system char(8) NOT NULL default 'private' AFTER type ;
ALTER TABLE cdb_memberfields DROP origgroup ;
ALTER TABLE cdb_memberfields ADD nickname VARCHAR( 30 ) NOT NULL  default '' AFTER
uid ;
ALTER TABLE cdb_memberfields ADD taobao VARCHAR( 40 ) NOT NULL default '' AFTER msn ;
ALTER TABLE cdb_memberfields ADD groupterms TEXT NOT NULL AFTER ignorepm ;

EOT;

$upgrade3 = <<<EOT
DELETE FROM cdb_settings Where variable in ('admincpbanperiods','attachnumperpost',
'avatarshow_lastreg', 'avatarshow_license', 'ecommercestatus', 'cachelifespan',
'creditstitle', 'creditsunit', 'exchangebalance', 'exchangemax', 'exchangemin',
'exchangestatus', 'exchangetax', 'seccodestatus', 'forcesecques', 'karmaratelimituser',
'maxthreadspan', 'maxincperattach', 'maxattachspan', 'memberonlinestatus', 'modworkslist',
'modworkstatus', 'regfloodctrl', 'regmodperiods', 'regusersperday', 'stylejumpstatus',
'fastloginform', 'attachpricetax', 'custombackup', 'threadperms', 'threadpricetax',
'transferstatus', 'transfertax', 'transferbalance',
'transfermax','regbanperiods','transfermin');

INSERT INTO cdb_settings VALUES ('adminipaccess', '');
INSERT INTO cdb_settings VALUES ('bannedmessages', '1');
INSERT INTO cdb_settings VALUES ('censoremail', '');
INSERT INTO cdb_settings VALUES ('creditstax', '0.2');
INSERT INTO cdb_settings VALUES ('ec_account', '');
INSERT INTO cdb_settings VALUES ('ec_maxcredits', '1000');
INSERT INTO cdb_settings VALUES ('ec_maxcreditspermonth', '0');
INSERT INTO cdb_settings VALUES ('ec_mincredits', '0');
INSERT INTO cdb_settings VALUES ('ec_ratio', '0');
INSERT INTO cdb_settings VALUES ('ec_securitycode', '');
INSERT INTO cdb_settings VALUES ('wapcharset', '2');
INSERT INTO cdb_settings VALUES ('wapdateformat', 'n/j');
INSERT INTO cdb_settings VALUES ('wapmps', '500');
INSERT INTO cdb_settings VALUES ('wapppp', '5');
INSERT INTO cdb_settings VALUES ('wapstatus', '1');
INSERT INTO cdb_settings VALUES ('waptpp', '10');
INSERT INTO cdb_settings VALUES ('exchangemincredits', '100');
INSERT INTO cdb_settings VALUES ('fullmytopics', '1');
INSERT INTO cdb_settings VALUES ('ipaccess', '');
INSERT INTO cdb_settings VALUES ('ipregctrl', '');
INSERT INTO cdb_settings VALUES ('jscachelife', '1800');
INSERT INTO cdb_settings VALUES ('jsrefdomains', '');
INSERT INTO cdb_settings VALUES ('jsstatus', '0');
INSERT INTO cdb_settings VALUES ('maxsigrows', '20');
INSERT INTO cdb_settings VALUES ('maxsmilies', '3');
INSERT INTO cdb_settings VALUES ('maxthreadads', '0');
INSERT INTO cdb_settings VALUES ('visitedforums', '10');
INSERT INTO cdb_settings VALUES ('searchbanperiods', '');
INSERT INTO cdb_settings VALUES ('searchbox', '0');
INSERT INTO cdb_settings VALUES ('stylejump', '0');
INSERT INTO cdb_settings VALUES ('postbanperiods', '');
INSERT INTO cdb_settings VALUES ('postmodperiods', '');
INSERT INTO cdb_settings VALUES ('pvfrequence', '60');
INSERT INTO cdb_settings VALUES ('qihoostatus', '0');
INSERT INTO cdb_settings VALUES ('transfermincredits', '1000');
INSERT INTO cdb_settings VALUES ('seccodestatus', '0');
EOT;


if(!$action) {
	echo"本程序用於升級 Discuz! 3.0F beta3 到 Discuz! 4.0.0,請確認之前已經順利安裝 Discuz! 3.0F beta3<br><br><br>";
	echo"<b><font color=\"red\">本升級程序只能從 3.0F beta3 升級到 4.0.0,運行之前,請確認已經上傳 3.0F beta3 的全部文件和目錄</font></b><br>";
	echo"<b><font color=\"red\">升級前請打開瀏覽器 JavaScript 支持,整個過程是自動完成的,不需人工點擊和干預.<br>升級之前務必備份數據庫資料,否則可能產生無法恢復的後果!<br></font></b><br><br>";
	echo"正確的升級方法為:<br>1. 關閉原有論壇,上傳 Discuz! 4.0.0 版的全部文件和目錄,覆蓋服務器上的 3.0F beta3<br>2. 上傳本程序到 Discuz! 目錄中;<br>4. 運行本程序,直到出現升級完成的提示;<br><br>";
	echo"<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);

	if($step == 1) {

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

		runquery($upgrade1);

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

	} elseif($step == 2 ) {
		runquery($upgrade2);
		echo "第 2 步升級成功<br><br>";
		redirect("?action=upgrade&step=3");

	} elseif($step == 3) {

		runquery($upgrade3);
		echo "第 3 步升級成功<br><br>";
		redirect("?action=upgrade&step=4");

	} elseif($step == 4) {

		@unlink('./forumdata/cache/cache_settings.php');
		echo "恭喜您升級成功,請務必刪除本程序. <br>完成全部升級請務必進行以下操作：<li>以管理員身份登錄論壇，重新設置管理組權限以及各種特殊會員組<li>更新論壇基本設置。<li>更新論壇緩存";
	}
}

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

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 checkbbcodes($message, $bbcodeoff) {
	return !$bbcodeoff && !preg_match("/\[.+\].*\[\/.+\]/s", $message) ? -1 : $bbcodeoff;
}

function checksmilies($message, $smileyoff) {
	global $smilies;
	return !$smileyoff && !preg_match('/'.implode('|', $smilies).'/', $message) ? -1 : $smileyoff;
}

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

function redirect($url) {

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

}

function random($length) {
	$hash = '';
	$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
	$max = strlen($chars) - 1;
	mt_srand((double)microtime() * 1000000);
	for($i = 0; $i < $length; $i++) {
		$hash .= $chars[mt_rand(0, $max)];
	}
	return $hash;
}

?>
