<?php
/**
 *	Dvbbs.PHP 2.0 RC1 Copyright 2000 - 2007 
 */
@header("Expires: -1");
@header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
@header("Pragma: no-cache");
//Start PHP相关设置
ini_set('display_errors', true);
error_reporting(7);
set_magic_quotes_runtime(0);
//End PHP相关设置
@set_time_limit(1000);
//Start 定义常量
define('ROOT_PATH', '../');
define('INC_PATH', ROOT_PATH.'inc/');
define('CACHE_PATH', ROOT_PATH.'cache/');
define('CONFIG_PATH', INC_PATH);
define('RELATED_CACHE_PATH', 'cache/');
define('INSTALL_INCLUDE_PATH', './');
define('ISDVBBS', true);
//End 定义常量
function_exists('ini_set') AND @ini_set('track_errors', 1);
ob_start();

//Start 初始化变量
if(PHP_VERSION < '4.1.0') {
	$_GET  =& $HTTP_GET_VARS;
	$_POST =& $HTTP_POST_VARS;
	$_SERVER =& $HTTP_SERVER_VARS;
}

//parse get variables
foreach ($_GET as $key=>$val){
	$key="get_".$key;
	$$key=trim($val);
}
//end parse $get variables

//parse post variables
foreach ($_POST as $key=>$val){
	$key="post_".$key;
	$$key=trim($val);
}
//end parse post variables

$command = empty($get_command)?0:intval($get_command);
$language = empty($get_language)?'sc_gbk':trim($get_language);
//安装程序版本
$version = '2.0';
//End 初始化变量

$errno=0;
$errmsg="";

//Start 设置脚本超时时间
@set_time_limit(1000);
//End 设置脚本超时时间

//包含文件
$configfile = ROOT_PATH.'inc/config.php';
require ROOT_PATH.'inc/general_funcs.php';
require ROOT_PATH.'inc/filehandle.misc.php';
require ROOT_PATH.'inc/FileHandler.class.php';
require ROOT_PATH.'inc/dv_clsmain.php';
require ROOT_PATH.'inc/dv_clssql.php';

//Start 初始化程序安装语言
switch ($language) {
	case 'sc_gbk':
		$dbcharset = $webcharset = 'gbk';
		break;
	case 'sc_utf8':
		$dbcharset = 'utf8';
		$webcharset = 'utf-8';
		break;
	case 'tc_big5':
		$dbcharset = 'big5';
		$webcharset = 'utf-8';
		break;
	case 'tc_utf8':
		$dbcharset = 'utf8';
		$webcharset = 'utf-8';
		break;
	case 'en_utf8':
		$dbcharset = 'utf8';
		$webcharset = 'utf-8';
		break;
	default:
		$language = 'sc_gbk';
		$dbcharset = 'gbk';
		$webcharset = 'gbk';
		break;
}
//End 初始化程序安装语言
header("Content-Type: text/html; charset={$webcharset}");

//Start 定义语言文件和数据库安装SQL路径，并检查相关安装文件是否存在
$langfile = INSTALL_INCLUDE_PATH.$language.'.lang.php';
$sqlfile = INSTALL_INCLUDE_PATH.'install.sql';
clearstatcache();
if (!is_readable($langfile) OR !is_readable($sqlfile)) {
	exit('<div style="width:100%;height:100%;text-align:middle;">Please upload ./install and all its files completely.</div>');
}
//包含语言文件
require $langfile;
//End 定义语言文件和数据库安装SQL路径，并检查相关安装文件是否存在


if (!file_exists($configfile) ||! is_writeable($configfile)){
	exit("@@@1@@@@@@alert('{$lang['install.info17']}')@@@");
}
require $configfile;
if($command===0){
	chdir_all_mod();
	show_select_language();
}elseif($command===1){
	show_welcome();
}elseif($command===2){
	show_db_config();
}elseif($command===3){
	show_forum_config();
}elseif($command===4){
	check_old_install();
}elseif($command===5){
	save_config();
}elseif($command===6){
	install();
}elseif($command===7){
	create_cache();
}elseif($command===8){
	show_admin_config();
}elseif($command===9){
	show_upgrade_config();
}elseif($command===10){
	save_upgrade_config();
}elseif($command===11){
	install_finished();
}else{
	show_select_language();
}

function chdir_all_mod(){
	$arrdirs=array(
	 array('path'=>'cache','mod'=>0777)
	 ,array('path'=>'uploadfile','mod'=>0777)
	 ,array('path'=>'admin/data_backup','mod'=>0777)
	 ,array('path'=>'admin/sysupgrade','mod'=>0777)
	);
	$arrfiles=array(
	  array('path'=>'inc/config.php','mod'=>0777)
	);
	foreach ($arrdirs as $dir){
		chdir_mod(ROOT_PATH.$dir['path'],$dir['mod']);
	}
	foreach ($arrfiles as $file){
		@FileHandler::chmod(ROOT_PATH.$file['path'],$file['mod']);
	}
}

function chdir_mod($dirpath,$mod=0777){
	if(false!==($fdh = opendir($dirpath))){
		@FileHandler::chmod($dirpath,$mod);
		while($entry = readdir($fdh)) {
			if($entry != '.' && $entry != '..') {
				$f1=$dirpath."/".$entry;
				if(is_dir($f1)){
					chdir_mod($f1,$mod);
				}else{
					@FileHandler::chmod($f1,$mod);
				}
			}
		}
	}
}

function show_select_language($show=true){
	$strhtml=<<<EOT
	@@@0@@@
	<table width="96%" height="177" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#e5f0f6">
					<form name="" id="frmsetup" onsubmit="return false;">
<tr bgcolor="white"><td width="100%" class="tit1" height="20" colspan="3" style="text-indent:5px;"><h4> choose your prefered language</h4></td></tr>
						<tr>
						  <td width="100%" height="25" style="text-align:center;"><table width="98%" height="74" border="0">
                            <tr>
                              <td width="9%"><a href="?language=sc_gbk">
                                <input type="hidden" id="language" value="sc_gbk" />
                                <input name="rlanguage" type="radio" value="sc_gbk" checked="checked" onclick="document.getElementById('language').value='sc_gbk'" />
                              </a></td>
                              <td width="36%"><a href="#" onclick="return false">[&#31616;&#20307;&#20013;&#25991; GBK]</a></td>
                              <td width="10%"><input type="radio" name="rlanguage" value="sc_utf8" onclick="document.getElementById('language').value='sc_utf8'" disabled=true />
                              </td>
                              <td width="45%"><a href="#" onclick="return false">[&#31616;&#20307;&#20013;&#25991; UTF-8]
                               
                              </a></td>
                            </tr>
                            <tr>
                              <td><input type="radio" name="rlanguage" value="tc_big5"  onclick="document.getElementById('language').value='tc_big5'" disabled=true/></td>
                              <td><a href="#" onclick="return false">[&#32321;&#39636;&#20013;&#25991; BIG5]</a></td>
                              <td><input type="radio" name="rlanguage" value="tc_utf8" onclick="document.getElementById('language').value='tc_utf8'" disabled=true/></td>
                              <td><a href="#" onclick="return false">[&#32321;&#39636;&#20013;&#25991; UTF-8]</a></td>
                            </tr>
                          </table>
					      <!--<a disabled href="?language=en_utf8">[English UTF-8]</a><br /><br />--></td>
						
			</tr>
		  </form>
		</table>
		@@@@@@
EOT;
	if($show===true){
		print $strhtml;
		return true;
	}else{
		return $strhtml;
	}
}


function show_welcome($show=true){
	global $lang,$language,$webcharset;
	$strhtml=<<<EOT
     @@@0@@@{$lang["install.welcome"]}<input type="hidden" id="language" value="{$language}" />@@@g_encode="{$webcharset}";@@@
EOT;
	if($show===true){
		print $strhtml;
		return true;
	}else{
		return $strhtml;
	}
}

function show_db_config($show=true){
	global $lang,$language;
	global $dbhost, $dbuser, $dbpw, $dbname , $charset;
	$strhtml=<<<EOT
@@@0@@@<table width="96%" height="177" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#999999">
<form name="" id="frmsetup" onsubmit="return false;">
<tr bgcolor="white"><td width="100%" class="tit1" height="20" colspan="3" style="text-indent:5px;">{$lang["install.info5"]}</td></tr>
<tr bgcolor1="#E3E3EA" bgcolor="#D6D3CE">
											<th width="25%" align="center">{$lang['install.info7']}</th>
											<th width="39%" align="center">{$lang['install.info8']}</th>
											<th width="36%" align="center">{$lang['install.info9']}</th>
										</tr>
										<input type="hidden" name="language" value="{$language}" size="15"/>
										<tr bgcolor="white">
											<td height="25"><font color="red">{$lang['install.info10']}</font></td>
											<td><input type="text" name="dbhost" value="{$dbhost}" size="15" /></td>
											<td>{$lang['install.info11']}</td>
										</tr>
										<tr bgcolor="white">
											<td height="25">{$lang['install.info12']}</td>
											<td><input type="text" name="dbuser" value="{$dbuser}" size="15"/></td>
											<td>{$lang['install.info13']}</td>
										</tr>
										<tr bgcolor="white">
											<td height="25">{$lang['install.info14']}</td>
											<td><input type="password" name="dbpw" value="{$dbpw}" size="15"/></td>
											<td>{$lang['install.info15']}</td>
										</tr>
										<tr bgcolor="white">
											<td height="25">{$lang['install.info16']}</td>
											<td><input type="text" name="dbname" value="{$dbname}" size="15"/></td>
											<td>{$lang['install.info16']}</td>
										</tr>
										<!--<tr bgcolor="white">
											<td>系统 Email</td>
											<td><input type="text" name="" value="admin@your.com" size="30"/></td>
											<td>用于发送程序错误报告</td>
										</tr>-->
										</form>
									</table>@@@@@@
EOT;
	if($show===true){
		print $strhtml;
		return true;
	}else{
		return $strhtml;
	}
}

function show_forum_config($show=true){
	global $lang,$language,$get_dbhost,$get_dbuser,$get_dbpw,$get_dbname,$errno,$dbcharset,$pconnect,$errmsg;
	global $dbhost,$dbuser,$dbpw,$dbname,$dv,$cookiedomain,$cookiepath,$cookieprename;
	(empty($get_dbhost) AND ($errmsg= $lang['err.str1']) AND ($errno=1))||(empty($get_dbuser) AND ($errmsg= $lang['err.str2']) AND ($errno=2))||(empty($get_dbpw) AND ($errmsg= $lang['err.str3']) AND ($errno=4))||(empty($get_dbname) AND ($errmsg= $lang['err.str4']) AND ($errno=8));
	if($errmsg!==""){
		$strhtml="@@@1@@@''@@@alert('{$errmsg}')@@@";
	}else{
		//测试数据库连接
		$db =& new sqldb();
		$db->connectSilent = true;
		if (!$db->connect($get_dbhost, $get_dbuser,$get_dbpw, '' , $dbcharset, $pconnect)) {	//	数据库错误，连接错误
			$dberrno=$db->errno();
			$dberrmsg="";
			if($dberrno==2005){
				$dberrmsg=$lang['err.str7.0'].":".$dbhost;
			}elseif($dberrno==1045){
				$dberrmsg=$lang['err.str7.1'];
			}elseif($dberrno==1130){
				$dberrmsg=$lang['err.str7.2'];
			}
			$errmsg = str_replace('{$TPL_SQLDB_ERROR}', $db->error(), $lang['err.str7']);
			exit("@@@$dberrno@@@{$errmsg}@@@alert('".addslashes($errmsg.$dberrmsg)."')@@@");
		}

		$db->query("CREATE DATABASE /*!32200 IF NOT EXISTS */ ".$get_dbname ." DEFAULT CHARACTER SET $dbcharset", array('type'=>'SILENT'));

		if (!@$db->select_db($get_dbname)) {	//	数据库错误，指定的库不存在
			$errmsg =$lang['err.str7.3'];
			$errmsg =str_replace('{$DBNAME}',$get_dbname ,$errmsg);
			exit("@@@$dberrno@@@{$errmsg}@@@alert('".addslashes($errmsg)."')@@@");
		}
		//End 测试数据库连接

		$strhtml=<<<EOT
	@@@0@@@<table width="96%" height="177" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#999999">
	<tr bgcolor="white"><td width="100%" class="tit1" height="20" colspan="2" style="text-indent:5px;">{$lang['install.info19']}</td></tr>
	<form name="" id="frmsetup" onsubmit="return false;">
<input type="hidden" name="language" value="{$language}"/>
<input type="hidden" name="dbhost" value="{$get_dbhost}" />
<input type="hidden" name="dbuser" value="{$get_dbuser}"/>
<input type="hidden" name="dbpw" value="{$get_dbpw}" />
<input type="hidden" name="dbname" value="{$get_dbname}"/>
										<tr bgcolor="white">
											<td><font color="red">{$lang['install.info18']}</font></td>
											<td><input type="text" name="dv" value="{$dv}" size="15"/></td>
										</tr>
										<tr bgcolor="white">
											<td><font color="red">{$lang['install.info20']}</font></td>
											<td><input type="text" name="cookieprename" value="{$cookieprename}" size="15"/></td>
										</tr>
										<tr bgcolor="white">
											<td><font color="red">{$lang['install.info22']}</font></td>
											<td><input type="text" name="cookiedomain" value="{$cookiedomain}" size="15"/></td>
										</tr>
										<tr bgcolor="white">
											<td><font color="red">{$lang['install.info23']}</font></td>
											<td><input type="text" name="cookiepath" value="{$cookiepath}" size="15"/></td>
										</tr>
										</form>
										</table>@@@@@@
EOT;

}
if($show===true){
	print $strhtml;
	return true;
}else{
	return $strhtml;
}
}


function check_old_install(){
	global $lang,$get_dbhost,$get_dbuser,$get_dbpw,$get_dbname,$get_dv,$errno,$dbcharset,$pconnect,$errmsg;
	global $get_dv,$get_cookieprename,$get_cookiedomain,$get_cookiepath;
	$db =& new sqldb();
	$db->connectSilent = true;
	$err=true;
	($db->connect($get_dbhost, $get_dbuser,$get_dbpw, '' , $dbcharset, $pconnect))
	AND (@$db->select_db($get_dbname))
	AND ($db->query("SELECT COUNT(*) FROM {$get_dv}setup", array('type'=>'SILENT')))
	AND ($err=(0 !== $db->errno()));
	//Start	检查以前安装
	$db->close();
	$retHtml=<<<EOT
<form name="" id="frmsetup" onsubmit="return false;">
<input type="hidden" name="language" value="{$language}"/>
<input type="hidden" name="dbhost" value="{$get_dbhost}" />
<input type="hidden" name="dbuser" value="{$get_dbuser}"/>
<input type="hidden" name="dbpw" value="{$get_dbpw}" />
<input type="hidden" name="dbname" value="{$get_dbname}"/>
<input type="hidden" name="dv" value="{$get_dv}"/>
<input type="hidden" name="cookieprename" value="{$get_cookieprename}"/>
<input type="hidden" name="cookiedomain" value="{$get_cookiedomain}" />
<input type="hidden" name="cookiepath" value="{$get_cookiepath}"/>
</form>
EOT;

	if (!$err){
		$errmsg="if(confirm('".addslashes(str_replace('{$DBNAME}',$get_dbname ,$lang['install.info28']))."')===false){window.close()}else{execCmdNext()}";
		exit("@@@0@@@{$retHtml}@@@{$errmsg}@@@");
	}else{
		exit("@@@0@@@{$retHtml}@@@execCmdNext()@@@");
	}

	//End	检查以前安装
}
function save_config(){
	global $lang,$language,$configfile,$errno,$dbcharset,$pconnect,$errmsg;
	global $_GET;
	empty($_GET['dv']) AND ($_GET['dv'] = 'dv_');
	empty($_GET['cookieprename']) AND ($_GET['cookieprename'] = 'dvbbs_');
	empty($_GET['cookiepath']) AND ($_GET['cookiepath'] = '/');

	if (!is_writable($configfile)) {	//	无法以写方式打开配置文件{$TPL_CFG_FILE}
		$errmsg= str_replace('{$TPL_CFG_FILE}',$configfile, $lang['err.str6']);
		exit("@@@$dberrno@@@{$errmsg}@@@alert('".addslashes($errmsg)."')@@@");
	}

	//Start	检查数据库和COOKIE配置通过，开始将这些信息写入配置文件
	$cfg_file_content = '';
	if ($fp = fopen($configfile, 'rb')) {
		$cfg_file_content = fread($fp,filesize($configfile));
		fclose($fp);
	}else{
		$errmsg= str_replace('{$TPL_CFG_FILE}',$configfile, $lang['err.str5']);
		exit("@@@$dberrno@@@{$errmsg}@@@alert('".addslashes($errmsg)."')@@@");
	}
	if ('' != $cfg_file_content AND ($fp = @fopen($configfile, 'wb'))) {
		flock($fp, 2);
		foreach($_GET as $key=>$val){
			$re='/\$'.$key.'\s*\=\s*["\'].*?["\']/is';
			$cfg_file_content = preg_replace($re, "\${$key} = '".str_replace("'", "\\'", $val)."'", $cfg_file_content);
		}
		fwrite($fp, trim($cfg_file_content));
		fclose($fp);
		$retmsg= str_replace('{$TPL_CFG_FILE}',$configfile, $lang['err.str6.1'])."<BR>";
		//END 检查数据库和COOKIE配置通过，开始将这些信息写入配置文件
		//Start	检查相关缓存目录的写入权限
		$is_error = 0;
		$arrWritableDir=array(
		array(ROOT_PATH.'templates/',$lang['install.info29'],"",0)
		,array(ROOT_PATH.'templates/lang/',$lang['install.info30'],"",0)
		,array(ROOT_PATH.'templates/default/',$lang['install.info31'],"",0)
		,array(ROOT_PATH.'templates/space/',$lang['install.info31_1'],"",0)
		,array(ROOT_PATH.'cache/',$lang['install.info32_1'],"",1)
		,array(ROOT_PATH.'cache/argumentation/',$lang['install.info32_2'],"",1)
		,array(ROOT_PATH.'cache/style/',$lang['install.info32'],"",1)
		,array(ROOT_PATH.'cache/board/',$lang['install.info33'],"",1)
		,array(ROOT_PATH.'cache/space/',$lang['install.info34'],"",1)
		,array(ROOT_PATH.'cache/group/',$lang['install.info35'],"",1)
		,array(ROOT_PATH.'uploadfile/',$lang['install.info36'],"",1)
		,array(ROOT_PATH.'uploadfile/faces',$lang['install.info36__6'],"",1)
		,array(ROOT_PATH.'uploadfile/tempimage',$lang['install.info36__1'],"",1)
		,array(ROOT_PATH.'uploadfile/forum',$lang['install.info36__2'],"",1)
		,array(ROOT_PATH.'uploadfile/forum/previewimage',$lang['install.info36__3'],"",1)
		,array(ROOT_PATH.'uploadfile/space',$lang['install.info36__4'],"",1)
		,array(ROOT_PATH.'uploadfile/space/previewimage',$lang['install.info36__5'],"",1)
		,array(ROOT_PATH.'admin/data_backup',$lang['install.info36_7'],"",1)
		,array(ROOT_PATH.'admin/templates/tmp_post',$lang['install.info36__8'],"",1)
		);

		foreach ($arrWritableDir as $arrdir){
			if (!FileHandler::_dir_is_writable($arrdir[0])) {
				$retmsg .= '<font color=red>'.$arrdir[1].'</font><BR>';
				$is_error = $arrdir[3];
			}else{
				$retmsg .= "<font color=black>{$lang['install.info36_9']}{$arrdir[0]}</font><font color=green>&nbsp;&nbsp;OK!</font><BR>";
			}
		}
		//End	检查相关缓存目录的写入权限

		if ($is_error!==0) {
			$errmsg .=$lang["err.dir1"];
			exit("@@@0@@@{$retmsg}@@@alert('{$errmsg}')@@@");
		}else{
			$retmsg .=$lang["install.info37"];
			exit("@@@0@@@{$retmsg}@@@@@@");
		}
	} else {
		$errmsg = str_replace('{$TPL_CFG_FILE}', $configfile, $lang['err.str6']);
		exit("@@@32@@@{$errmsg}@@@alert('".addslashes($errmsg)."')@@@");
	}
}

function install() {
	global $lang,$sqlfile,$configfile,$db,$errno,$errmsg,$dbcharset,$pconnect;
	global $dbhost,$dbuser,$dbpw,$dbname,$dv,$cookiedomain,$cookiepath,$cookieprename;
	if (!is_readable($sqlfile)) {
		exit("@@@1@@@@@@alert('{$lang['install.info42']}')@@@");
	}
	if (filesize($sqlfile) <= 0) {
		exit("@@@1@@@@@@alert('{$lang['install.info43']}')@@@");
	}

	$retHtml='';
	$strsql = '';
	$stmtarray = array();
	$dbcharset = str_replace('-', '',$dbcharset);
	if (empty($dbcharset)) {
		$dbcharset = 'gbk';
	}

	if ($fp = fopen($sqlfile, 'rb')) {
		$strsql = fread($fp, filesize($sqlfile));
		@fclose($fp);
		if (!empty($strsql)) {
			$strsql = preg_replace('/^(?:#|--).*$/im', '', str_replace("\r", "\n", $strsql));
			if ('gbk' != $dbcharset) {
				$strsql = preg_replace(array('#^/\*![0-9]+\s+SET\s+NAMES\s+gbk\s*\*/;$#im', '#^/\*![0-9]+\s+SET\s+CHARACTER\s+SET\s+\'gbk\'\s*\*/;$#im'), array("/*!40101 SET NAMES {$dbcharset} */;\n", "/*!40101 SET CHARACTER SET '{$dbcharset}' */;\n"), $strsql);
			}
			$stmtarray = explode(";\n", $strsql);
		}
		if (!empty($stmtarray)) {
			$retHtml.="{$lang['install.info44']}<BR>";
			$dberror = '';
			$db =& new sqldb();
			$db->connect($dbhost, $dbuser, $dbpw, $dbname , $dbcharset, $pconnect);
			if($db->errno()!=0)exit("");
			$tablelist = array();
			foreach ($stmtarray AS $stmt) {
				$stmt = trim(preg_replace("/`dv_/i", "`".$dv, $stmt));
				$stmt = str_replace("'dv_bbs1'", "'{$dv}bbs1'", $stmt);
				if (substr($stmt, 0, 12) == 'CREATE TABLE'){
					$name = preg_replace("/CREATE TABLE `?([a-z0-9_]+)`? .*/is", "\\1", $stmt);
					$tablelist[] = $name;
					$stmt = preg_replace('#(?:DEFAULT\s+CHARSET\s*=\s*gbk)#i', "DEFAULT CHARSET={$dbcharset}", $stmt);
					$db->query($stmt, array('type' =>'SILENT'));
					if (0 == $db->errno()) {
						$retHtml.="创建表({$name})&nbsp;&nbsp;<font color=#0000EE>{$lang['install.info47']}</font><BR>";
					} else {
						$retHtml.="<font color=red>".str_replace('{$TPL_ERR_REASON}', $db->error(), $lang['install.info48']).'</font><BR>';
					}

				} else {
					$db->query($stmt, array('type' =>'SILENT'));
				}
				if (0 != $db->errno()) {
					$dberror = $db->error();
					break;
				} else {
				}
			}
			if (!empty($dberror) AND is_array($tablelist)) {
				foreach ($tablelist AS $tablename) {
					$db->query("DROP TABLE /*! IF EXISTS */ {$tablename}", array('type' =>'SILENT'));
				}//end foreach
				$db->close();
				$retHtml.=$lang['install.info51'].$dberror;

			} else {
				$db->close();
				$retHtml.=$lang['install.info46'];
			}
		}
		exit("@@@0@@@$retHtml@@@alert('{$lang["install.info95"]}');@@@");
	} else {
		exit("@@@1@@@@@@alert('Error: Cannot open ".addslashes($sqlfile)."')@@@");
		//Cannot open sql file
	}
}

function create_cache(){
	global $lang,$configfile,$db,$errno,$errmsg,$dbcharset,$pconnect;
	global $dbhost,$dbuser,$dbpw,$dbname,$dv,$cookiedomain,$cookiepath,$cookieprename;
	include_once ROOT_PATH.'inc/BoardCache.class.php';
	$db =& new sqldb();
	$db->connect($dbhost, $dbuser, $dbpw, $dbname , $dbcharset, $pconnect);
	$cache =& new BoardCache($db);
	$execsuccess = '<font color="#0000EE">'.$lang['install.info47'].'</font>';
	$execfailed = '<font color="red">'.$lang['install.info48'].'</font>';
	$allcreated = true;
	if (true === $cache->createAll()) {
		$cache->Create_HonorShowForum_File();
		$str_createAll = $execsuccess;
	} else {
		$str_createAll = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
		$allcreated = false;
	}
	if (true === $cache->createforumlink()) {
		$str_createforumlink = $execsuccess;
	} else {
		$str_createforumlink = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
		$allcreated = false;
	}
	if (true === $cache->createmailconfig()) {
		$str_createmailconfig = $execsuccess;
	} else {
		$str_createforumlink = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
		$allcreated = false;
	}
	require ROOT_PATH.'cache/style/template.cache.php';
	if (!empty($_TPL) AND is_array($_TPL)) {
		$db->query("TRUNCATE TABLE {$dv}styles");
		foreach ($_TPL AS $key => $cacheinfo) {
			$stmt = "";
			$comma = '';
			foreach ($cacheinfo AS $styleid => $dbstruct) {
				$db->query("INSERT INTO {$dv}styles(styleid, name, ename, content, directory, copyright, stype, parentid, charset) VALUES({$styleid}, '".$db->escape_string($dbstruct['name'])."', '".$db->escape_string($dbstruct['ename'])."', '".$db->escape_string(base64_encode(serialize($dbstruct['content'])))."', '".$db->escape_string($dbstruct['directory'])."', '".$db->escape_string($dbstruct['copyright'])."', {$dbstruct['stype']}, {$dbstruct['parentid']}, '{$dbstruct['charset']}')");
			}
		}

		if (true === $cache->createForumSetting()) {
			$str_createForumSetting = $execsuccess;
		} else {
			$str_createForumSetting = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
			$allcreated = false;
		}
		if (true === $cache->createstylecss()) {
			$str_createstylecss = $execsuccess;
		} else {
			$str_createstylecss = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
			$allcreated = false;
		}
	} else {
		$str_createstylecss = $execfailed.$lang['install.info49'].ROOT_PATH.'cache/style/template.cache.php'.$lang['install.info50'];
		$allcreated = false;
	}
	if (true === $cache->createusergroup()) {
		$str_createusergroup = $execsuccess;
	} else {
		$str_createusergroup = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
		$allcreated = false;
	}
	if (true === $cache->createboardgroup()) {
		$str_createboardgroup = $execsuccess;
	} else {
		$str_createboardgroup = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
		$allcreated = false;
	}
	if (true === $cache->createuserpermission()) {
		$str_createuserpermission = $execsuccess;
	} else {
		$str_createuserpermission = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
		$allcreated = false;
	}
	if (true === $cache->createforumface()) {
		$str_createforumface = $execsuccess;
	} else {
		$str_createforumface = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
		$allcreated = false;
	}
	if (true === $cache->createforumpic()) {
		$str_createforumpic = $execsuccess;
	} else {
		$str_createforumpic = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
		$allcreated = false;
	}
	if (true === $cache->createspaceConfig()) {
		$str_createspaceConfig = $execsuccess;
	} else {
		$str_createspaceConfig = str_replace('{$TPL_ERR_REASON}', $php_errormsg, $execfailed);
		$allcreated = false;
	}
	$retmsg.="<BR>".$lang['install.info52'].$str_createForumSetting;
	$retmsg.="<BR>".$lang['install.info54'].$str_createforumlink;
	$retmsg.="<BR>".$lang['install.info55'].$str_createstylecss;
	$retmsg.="<BR>".$lang['install.info56'].$str_createusergroup;
	$retmsg.="<BR>".$lang['install.info57'].$str_createboardgroup;
	$retmsg.="<BR>".$lang['install.info58'].$str_createuserpermission;
	$retmsg.="<BR>".$lang['install.info59'].$str_createforumface;
	$retmsg.="<BR>".$lang['install.info60'].$str_createforumpic;
	$retmsg.="<BR>".$lang['install.info61'].$str_createspaceConfig;
	$retmsg.="<BR>".$lang['install.info53'].$str_createAll;

	$db->close();
	unset($db, $cache);
	if (true === $allcreated) {
		exit("@@@0@@@$retmsg@@@alert('{$lang["install.info96"]}');@@@");
	}else{
		exit("@@@0@@@$retmsg@@@alert('{$lang["err.cache"]}');@@@");
	}

}

function show_admin_config(){
	global $lang;
	$strhtml=<<<EOT
	@@@0@@@<table width="96%" height="177" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#999999">
	<form name="" id="frmsetup" onsubmit="return false">
	<tr bgcolor="white"><td width="100%" class="tit1" height="20" colspan="2" style="text-indent:5px;">{$lang['install.info79']}</td></tr>

<tr>
					<td width="100%" class="forumrow" height="20" colspan="2" style="text-indent:5px;">{$lang['install.info77']}<font color="red">{$lang['install.info78']}</font><br /></td>
				</tr>
										<tr bgcolor="white">
											<td><font color="red">{$lang['install.info85']}</font></td>
											<td><input type="text" name="backend_admin_account" value="admin" size="15"/></td>
										</tr>
										<tr bgcolor="white">
											<td><font color="red">{$lang['install.info86']}</font></td>
											<td><input type="password" name="backend_admin_pwd" value="admin888" size="15"/></td>
										</tr>
										<tr bgcolor="white">
											<td><font color="red">{$lang['install.info87']}</font></td>
											<td><input type="password" name="backend_confirm_admin_pwd" value="admin888" size="15"/></td>
										</tr>
										</form>
										</table>@@@enabledbtn();@@@
EOT;
	exit($strhtml);
}

function show_upgrade_config($show=true){
	global $lang,$db,$errno,$errmsg,$dbcharset,$pconnect;
	global $dbhost,$dbuser,$dbpw,$dbname,$dv,$get_backend_admin_pwd,$get_backend_confirm_admin_pwd,$get_backend_admin_account;
	if($get_backend_admin_pwd!==$get_backend_confirm_admin_pwd)exit("1@@@@@@alert('{$lang["install.info87.1"]}')");
	$db =& new sqldb();
	$db->connect($dbhost, $dbuser, $dbpw, $dbname , $charset, $pconnect);
	$get_backend_admin_pwd = dv_md5($get_backend_admin_pwd);
	$db->query("UPDATE {$dv}admin SET username='{$get_backend_admin_account}', password='{$get_backend_admin_pwd}' WHERE username='admin' AND adduser='admin'", array('type' => 'SILENT'));
	$dberror = $db->error();
	if (!$dberror) {
		$errmsg="{$lang['install.info84.1']},{$lang['install.info85']}:{$get_backend_admin_account},{$lang['install.info86']}:{$get_backend_confirm_admin_pwd}";
	}else{
		$errmsg="{$lang['install.info86.1']}";
	}
	$strhtml=<<<EOT
@@@0@@@<table width="96%" height="177" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#999999">
<form name="" id="frmsetup" onsubmit="return false;">
<tr bgcolor="white"><td width="100%" class="tit1" height="20" colspan="3" style="text-indent:5px;">{$lang["install.info97"]}</td></tr>
<tr bgcolor1="#E3E3EA" bgcolor="#D6D3CE">
											<th width="25%" align="center">{$lang['install.info7']}</th>
											<th width="39%" align="center">{$lang['install.info8']}</th>
											<th width="36%" align="center">{$lang['install.info9']}</th>
										</tr>
										<tr bgcolor="white">
											<td height="25"><font color="red">{$lang['install.info98']}</font></td>
											<td><input type="text" name="ftpport" value="21" size="15" /></td>
											<td>{$lang['install.info102']}</td>
										</tr>
										<tr bgcolor="white">
											<td height="25">{$lang['install.info99']}</td>
											<td><input type="text" name="ftpuser" value="" size="15"/></td>
											<td>{$lang['install.info103']}</td>
										</tr>
										<tr bgcolor="white">
											<td height="25">{$lang['install.info100']}</td>
											<td><input type="text" name="ftppwd" value="" size="15"/></td>
											<td>{$lang['install.info104']}</td>
										</tr>
										<tr bgcolor="white">
											<td height="25">{$lang['install.info101']}</td>
											<td><input type="text" name="ftppath" value="/" size="15"/></td>
											<td>{$lang['install.info105']}</td>
										</tr>
										</form>
									</table>@@@alert('{$errmsg}');@@@
EOT;
	if($show===true){
		print $strhtml;
		return true;
	}else{
		return $strhtml;
	}

	exit($strhtml);
}

function save_upgrade_config(){
	global $lang,$db,$errno,$errmsg,$dbcharset,$pconnect;
	global $dbhost,$dbuser,$dbpw,$dbname,$dv,$get_ftpport,$get_ftpuser,$get_ftppwd,$get_ftppath;
	if($get_ftpuser===""||$get_ftppwd===""){
		exit("@@@1@@@please type your ftp count@@@if(confirm('{$lang["err.str12"]}')===true){execCmdNext();cmdIp++;}@@@");
	}else{
		$get_ftpport=intval($get_ftpport) ;
		empty($get_ftpport) AND ($get_ftpport=21);
		empty($get_ftppath) AND ($get_ftppath="/");
		$err=true;
		if(!($ftpconn=ftp_connect("127.0.0.1",21,5))){
			$errmsg="{$lang["err.str8"]}";
		}elseif(!ftp_login($ftpconn,$get_ftpuser,$get_ftppwd)){
			$errmsg="{$lang["err.str9"]}";
		}elseif(!ftp_chdir($ftpconn,$get_ftppath)){
			$errmsg="{$lang["err.str10"]}";
		}elseif(ftp_mdtm($ftpconn,"admin/dvbbs_upgrade_authentication.key")===-1){
			$errmsg="{$lang["err.str11"]}";
		}else{
			$err=false;
		}
		if($err){
			exit("@@@1@@@FTP ERROR@@@if(confirm('{$errmsg}')===true){execCmdNext();cmdIp++;}@@@");
		}else{
			$db =& new sqldb();
			$db->connect($dbhost, $dbuser, $dbpw, $dbname , $charset, $pconnect);
			$db->query("UPDATE {$dv}sysupgrade SET ftpport='{$get_ftpport}', ftpuser='{$get_ftpuser}' , ftppass='{$get_ftppwd}' , ftproot='{$get_ftppath}'", array('type' => 'SILENT'));
			$dberror = $db->errno();
			if ($dberror!==0) {
				exit("@@@1@@@@@@if(confirm('{$lang["err.str12.1"]}')===true){execCmdNext();cmdIp++;}@@@");
			}else{
				install_finished();
			}
			$db->close();
		}
	}
}

function install_finished(){
	global $lang;
	require_once ROOT_PATH.'inc/FileHandler.class.php';
	$installdir = dirname(__FILE__);
	@chmod($installdir, 0777);
	if (@FileHandler::recursion_removedir($installdir, 0777)) {
		@unlink(__FILE__);
		@unlink($installdir.'/install.sql');
		@unlink($installdir.'/index.php');
		@unlink($installdir.'/sc_gbk.lang.php');
		@chmod($installdir, 600);
		@unlink('../setup.php');
		exit("@@@0@@@OK!@@@alert('{$lang['install.info107']}');window.location='../index.php';@@@");
	}else{
		exit("@@@0@@@@@@alert('{$lang['err.str13']}');window.location='../index.php';@@@");
	}
}
?>