<?php
/*
	(C) 2007-2008 lmcms Inc.
	$Id: index.php 7443 2008-05-23 03:25:55Z xujun $
*/

error_reporting(2048);

define('DBCHARSET', 'UTF8');
define('CHARSET', 'UTF-8');
define('UC_API',true);

include_once('./install.func.php');
include_once('./install.lang.php');
include_once('./db.class.php');

$step = empty($_GET['step'])? 1 : intval($_GET['step']);
//检查是否开启rewrite块

//检查是否用的apache 服务器
if(function_exists("apache_get_modules")){
	if(in_array("mod_rewrite",apache_get_modules())){
		$is_rewirte	=	"checked";
		$no_rewirte	=	"";
	}
	else{
		$is_rewirte	=	"";
		$no_rewirte	=	"checked";
	}
}
else{
	$is_rewirte	=	"";
	$no_rewirte	=	"checked";
}

$dbhost 	= getgpc('dbhost', 'P');
$dbname 	= getgpc('dbname', 'P');
$dbuser	 	= getgpc('dbuser', 'P');
$dbpw 		= getgpc('dbpw', 'P');
$username 	= getgpc('username', 'P');
$userpw 	= getgpc('userpw', 'P');
$dbcharset 	= getgpc('dbcharset', 'P');
$db_suffix 	= getgpc('db_suffix', 'P');
$IS_URI		= getgpc('static', 'P');
$licence	= getgpc('licence','P');
$username 	= empty($username) ? 'admin' : trim($username);
$userpw 	= empty($userpw) ? 'admin' : trim($userpw);
$dbcharset = !empty($dbcharset) ? $dbcharset : DBCHARSET;

$dirname = substr(dirname(__FILE__), 0, -7);

define('UC_ROOT', $dirname);
define('S_ROOT', $dirname);
define('LMCMS_ROOT', $dirname);

$hide = '';
$hide .= var_to_hidden('dbhost' ,$dbhost);
$hide .= var_to_hidden('dbuser' ,$dbuser);
$hide .= var_to_hidden('dbpw' ,$dbpw);
$hide .= var_to_hidden('dbname' ,$dbname);
$hide .= var_to_hidden('username' ,$username);
$hide .= var_to_hidden('userpw' ,$userpw);
$hide .= var_to_hidden('db_suffix' ,$db_suffix);
$hide .= var_to_hidden('licence',$licence);
$uri = $_SERVER['REQUEST_URI']?$_SERVER['REQUEST_URI']:($_SERVER['PHP_SELF']?$_SERVER['PHP_SELF']:$_SERVER['SCRIPT_NAME']);
$uri = substr($uri, 0, strrpos($uri, '/'));
$uri = substr($uri,0,-7);
$theurl = 'http://'.$_SERVER['HTTP_HOST'].$uri;

install_header();

$ucpwclass = '';
$adminpwclass = '';

if(submitchecki('dbsubmit')) {

	$link = mysql_connect($dbhost, $dbuser, $dbpw);
	if(empty($link)) {
		echo '<script>alert("'.$lang['password_error'].'");history.go(-1);</script>';
		exit;
	}
	if(!mysql_select_db($dbname, $link)) {
		$dberror = '';
		mysql_query("CREATE DATABASE `$dbname`");
		$dberror = mysql_error();
		if(!empty($dberror)) {
			echo '<script>alert("'.$lang['create_databaase_error'].'");history.go(-1);</script>';
			exit;
		}
	}

	mysql_close($link);
	$step = 3;
}

$setuppass = true;
$langstep = $lang['step_'.$step];
$lis = '';
for($i=1;$i<6;$i++) {
	if($i == $step) {
		$lis .= "<li class=\"current\">$i</li>";
	} elseif($i > $step) {
		$lis .= "<li class=\"unactivated\">$i</li>";
	} elseif($i < $step) {
		$lis .= "<li>$i</li>";
	}
}

echo <<<END
	<div class="header">
		<h1>$lang[install_wizard]</h1>
		<div class="setup step$step">
			<h2>$langstep</h2>
		</div>
		<div class="stepstat">
			<ul>
				$lis
			</ul>
			<div class="stepstatbg stepstat$step"></div>
		</div>
	</div>
	<div class="main">
END;
if(file_exists('install.lock')) {
		echo '<h4>'.$lang['all_install'].'<h4>';
		install_footer();
		exit;
	} elseif(file_exists(LMCMS_ROOT.'./forumdata/install.lock')) {
		echo '<h4>'.$lang['discuz_install'].'<h4>';
		install_footer();
		exit;
	} elseif(file_exists(UC_ROOT.'./data/install.lock')) {
		echo '<h4>'.$lang['ucenter_install'].'<h4>';
		install_footer();
		exit;
	} elseif(file_exists(S_ROOT.'./data/install.lock')) {
		echo '<h4>'.$lang['ucenterhome_install'].'<h4>';
		install_footer();
		exit;
	}

if($step == 1) {
	$phpos = PHP_OS;
	$result = array();
	if(function_exists('mysql_connect')) {
		$result['mysql'] = '<td class="w pdleft1">'.$lang['supportted'].'</td>';
	} else {
		$result['mysql'] = '<td class="nw pdleft1">'.$lang['unsupportted'].'</td>';
		$pass = FALSE;
	}
	if(PHP_VERSION > '5.0') {
		$result['phpversion'] = '<td class="w pdleft1">'.PHP_VERSION.'</td>';
	} else {
		$result['phpversion'] = '<td class="nw pdleft1">'.PHP_VERSION.'</td>';
		$pass = FALSE;
		$setuppass = false;
		$checkok = false;
	}
	if(@ini_get(file_uploads)) {
		$max_size = @ini_get(upload_max_filesize);
		$result['upload'] = '<td class="w pdleft1">'.$lang['max_size'].$max_size.'</td>';
	} else {
		$result['upload'] = '<td class="nw pdleft1">'.$lang['unsupportted'].'</td>';
		$pass = FALSE;
	}
	$curr_disk_space = intval(diskfreespace('.') / (1024 * 1024)).'M';
	if($curr_disk_space > 10) {
		$result['diskfree'] = '<td class="w pdleft1">'.$curr_disk_space.'</td>';
	} else {
		$result['diskfree'] = '<td class="nw pdleft1">'.$curr_disk_space.'</td>';
		$pass = FALSE;
	}
	echo <<<END
	<div class="main">
		<h2 class="title">$lang[env_check];</h2>
		<table class="tb" style="margin:20px 0 20px 55px;">
			<tr>
				<th>$lang[project];</th>
				<th class="padleft">$lang[ucenter_required];</th>
				<th class="padleft">$lang[ucenter_best];</th>
				<th class="padleft">$lang[curr_server];</th>
			</tr>
			<tr>
				<td>$lang[os]</td>
				<td class="padleft">$lang[unlimit]</td>
				<td class="padleft">UNIX/Linux/FreeBSD</td>
				<td class="padleft">$phpos</td>
			</tr>
			<tr>
				<td>PHP $lang[version]</td>
				<td class="padleft">5.0.0+</td>
				<td class="padleft">5.2.0+</td>
				$result[phpversion]
			</tr>
			<tr>
				<td>$lang[attach_upload]</td>
				<td class="padleft">$lang[allow]</td>
				<td class="padleft">$lang[allow]</td>
				$result[upload]
			</tr>
			<tr>
				<td>MySQL $lang[supportted]</td>
				<td class="padleft">MySQL5.0+</td>
				<td class="padleft">MySQL5.0.51+</td>
				$result[mysql]
			</tr>
			<tr>
				<td>$lang[disk_free]</td>
				<td class="padleft">30M+</td>
				<td class="padleft">$lang[unlimit]</td>
				$result[diskfree]
			</tr>
			<tr>
				<td></td>
				<td></td>
				<td></td>
			</tr>
		</table>
END;
	echo <<<END
	<h2 class="title">$lang[func_depend]</h2>
		<table class="tb" style="margin:20px 0 20px 55px;width:90%;">
			<tr>
				<th>$lang[func_name]</th>
				<th class="padleft">$lang[check_result]</th>
				<th class="padleft">$lang[suggestion]</th>
			</tr>
END;
	$functions = array('mysql_connect'=>FALSE, 'fsockopen'=>FALSE, 'gethostbyname'=>FALSE, 'file_get_contents'=>FALSE, 'xml_parser_create'=>FALSE);
	$advices = array(
		'mysql_connect' => $lang['advice_mysql'],
		'fsockopen' => $lang['advice_fopen'],
		'file_get_contents' => $lang['advice_file_get_contents'],
		'xml_parser_create' => $lang['advice_xml']
	);
	foreach($functions as $name=>$status) {
		$status = function_exists($name);
		echo '<tr><td>'.$name.'()</td>'.($status ? '<td class="w pdleft1">'.$lang['supportted'].'</td>' : '<td class="nw pdleft1">'.$lang['unsupportted'].'</td>').($status ? '<td class="padleft">'.$lang['none'].'</td>' : '<td><font color="red">'.$advices[$name].'</font></td>').'</tr>';
	}
	echo '</table>';
	
	//需要检测的目录
	
	$dirs = array(
		'/upload/',
		'/data/'
	);
	
	echo <<<END
		<h2 class="title">$lang[priv_check]</h2>
		<table class="tb" style="margin:20px 0 20px 55px;width:90%;">
			<tr>
				<th width="61%">$lang[step1_file]</th>
				<th class="padleft">$lang[step1_need_status]</th>
				<th class="padleft">$lang[step1_status]</th>
			</tr>
END;

	if(!checkfdperm(LMCMS_ROOT.'app/config/config.php',1)) {
		$setuppass = false;
		$checkok = false;
	} else {
		$checkok = true;
	}
	echo '<tr><td>./app/config/config.php</td><td class="w pdleft1">'.$lang['writeable'].'</td><td'.($checkok ? ' class="w pdleft1">'.$lang['writeable'] : ' class="nw pdleft1">'.$lang['unwriteable']).'</td></tr>';

	foreach($dirs as $dir) {
		if(!checkfdperm(LMCMS_ROOT.$dir)) {
			$setuppass = false;
			$checkok = false;
		} else {
			$checkok = true;
		}
		echo '<tr><td>.'.$dir.'</td><td class="w pdleft1">'.$lang['writeable'].'</td><td'.($checkok ? ' class="w pdleft1">'.$lang['writeable'] : ' class="nw pdleft1">'.$lang['unwriteable']).'</td></tr>';
	}
	if(!checkfdperm(LMCMS_ROOT.'system/config/database.php',1)) {
		$setuppass = false;
		$checkok = false;
	} else {
		$checkok = true;
	}
	echo '<tr><td>./system/config/database.php</td><td class="w pdleft1">'.$lang['writeable'].'</td><td'.($checkok ? ' class="w pdleft1">'.$lang['writeable'] : ' class="nw pdleft1">'.$lang['unwriteable']).'</td></tr>';


	echo '</table>';
	echo '<form action="index.php?step=2" method="post">';
	if($setuppass) {
		$nextstep = ' <input type="button" onclick="history.back();" value="'.$lang['old_step'].'"><input type="submit" value="'.$lang['new_step'].'">';
	} else {
		$nextstep = ' <input type="button" disabled="disabled" value="'.$lang['step1_unwriteable'].'">';
	}
	echo '<div class="btnbox marginbot"> '.$nextstep.'</div>';
	echo '</form>';
} elseif($step == 2) {
	echo <<<END
	<div class="main">
		<form method="post" action="index.php" id="setupinfoform" >
		<input type="hidden" name="dbsubmit" value="1"  />
		<h2 class="title">$lang[step_2]:</h2>
		<table class="tb2">
			
			
			<tr>
				<th>$lang[step2_dbhost]:</th>
				<td><input name="dbhost" id="dbhost" type="text" class="txt" value="localhost" /></td>
			</tr>
			<tr>
				<th>$lang[step2_dbname]</th>
				<td><input name="dbname" id="dbname" type="text" class="txt" value="" /></td>
			</tr>
			<tr>
				<th>$lang[step2_dbuser]:</th>
				<td><input name="dbuser" id="dbuser" type="text" class="txt" value="" /></td>
			</tr>
			<tr>
				<th>$lang[step2_dbpw]:</th>
				<td><input name="dbpw" id="dbpw" type="password" class="txt" value="" /></td>
			</tr>
			<tr>
				<th>$lang[db_suffix]:</th>
				<td><input name="db_suffix" id="db_suffix" type="text" class="txt" value="lm_" /></td>
			</tr>
		</table>
		<h2 class="title">$lang[base_set]</h2>
		<table class="tb2">
			<tr>
				<th>$lang[manager_name]:</th>
				<td><input name="username" id="manager_name" type="text" class="txt" value="" /></td>
			</tr>
			<tr>
				<th>$lang[manager_pwd]</th>
				<td><input name="userpw" id="manager_pwd" type="text" class="txt" value="" /></td>
			</tr>
			<tr>
				<th>$lang[manager_pwd1]:</th>
				<td><input name="userpw1" id="manager_pwd1" type="text" class="txt" value="" /></td>
			</tr>
			
		</table>
		
		
		
		<div class="btnbox marginbot">
		<input type="button" value="$lang[old_step]" onclick="history.back();" /><input type="button" name="dbsubmit" value="$lang[new_step]" onclick="checkpass();" /></div>
		</form>
END;
} elseif($step == 3) {
	echo <<<END
	<div class="btnbox"><textarea name="ucenter" style="width: 80%;"  readonly="readonly" id="notice"></textarea></div>

END;

	$db = new db();
	$db->connect($dbhost, $dbuser, $dbpw, $dbname, $dbcharset, 0);

	$sql = sreadfilei('tables.sql');
	$sql = str_replace("\r\n", "\n", $sql);
	
	//替换表名
	$findtable[]		=	"#{suffix}#i";
	$replacetable[]		=	$db_suffix;
	
	$sql = preg_replace($findtable,$replacetable,$sql);

	runquery($sql);
	//插入默认管理用户
	$userSql="INSERT INTO `".$db_suffix."user` (`user_name`, `user_pwd`,`user_status`,`user_role`) VALUES('$username', '".md5($userpw)."',1,10)";
	$db->query($userSql);
	
	//修改数据库配置文件
	$db_cfg_file	=	LMCMS_ROOT."system/config/database.php";				//数据库配置文件
	$db_cfg_str		=	file($db_cfg_file);
	//搜索替换
	$search[]		=	"#'user'\s*\=>\s*'.*'\s*,#i";
	$replace[]		=	"'user' => '$dbuser',";
	
	$search[]		=	"#'pass'\s*\=>\s*'.*'\s*,#i";
	$replace[]		=	"'pass' => '$dbpw',";
	
	$search[]		=	"#'host'\s*\=>\s*'.*'\s*,#i";
	$replace[]		=	"'host' => '$dbhost',";	
	
	$search[]		=	"#'database'\s*\=>\s*'.*'\s*,#i";
	$replace[]		=	"'database' => '$dbname',";
	
	$search[]		=	"#'table_prefix'\s*\=>\s*'.*'\s*,#i";
	$replace[]		=	"'table_prefix' => '$db_suffix',";

	foreach ($db_cfg_str as $tmp_str){
		@$new_dbcfg.= preg_replace($search,$replace,$tmp_str);
	}
	//写入数据库配置文件 
	$fp = fopen($db_cfg_file, 'w');
	fwrite($fp, $new_dbcfg);
	fclose($fp);

	echo '<h3>'.$lang['create_db_tables_ok'].'</h3>';
	$nextstep = ' <input type="button" onclick="history.back();" value="'.$lang['old_step'].'"><input type="submit" value="'.$lang['new_step'].'">';
	echo '<form method="post" action="index.php?step=4" ><div class="btnbox marginbot"> '.$hide.$nextstep.'</div></form>';
} elseif($step == 4) {
		
	echo '<h3>'.$lang['init_data'].'</h3>';
	echo '<form action="index.php?step=5" method="post">';
	echo <<<END
		<table width="80%" cellspacing=0 cellpadding=0 align=center class="tb2">
END;
		echo <<<END
		<tr>
			<th>否是使用伪静态</th>
			<td>
				<input type="radio" name="static" value="1"  >是&nbsp;&nbsp;
				<input type="radio" name="static" value="0" checked="checked">否<br>
				服务器端必须支持".htaccess",才能选择“是”。
			</td>
			
		</tr>
END;
echo <<<END
		<tr>
			<th>网站地本地址</th>
			<td><input type='text' name='web_http_url' value="$theurl" class="txt"></td>
		</tr>
		</table>
END;
	if($setuppass) {
		$nextstep = ' <input type="button" onclick="history.back();" value="'.$lang['old_step'].'"><input type="submit" value="'.$lang['new_step'].'">';
	} else {
		$nextstep = ' <input type="button" disabled="disabled" value="'.$lang['step1_unwriteable'].'">';
	}
	echo '<div class="btnbox marginbot"> '.$hide.$nextstep.'</div>';
	echo '</form>';
	
} elseif($step == 5) {
	$base_dir	=	str_replace("http://".$_SERVER['HTTP_HOST'],"",$theurl);
	if((int)$IS_URI==1){
		$htaccess	=	file_get_contents("htaccess.txt");
		$htaccess	=	str_replace("{uri}",$base_dir."index.php",$htaccess);
		$fp	=	fopen("../.htaccess","w+");
		fwrite($fp,$htaccess);
		fclose($fp);
		
		$setupinfo	.=	"网站后台地址：".getgpc("web_http_url","P")."admin<br>";

	}else{
		$setupinfo	.=	"网站后台地址：".getgpc("web_http_url","P")."index.php/admin<br>";

	}
	//修改前端控制器
	$index			=	file_get_contents("../index.php");
	$f[]			=	"#define\('BASEURI','.*'\)#i";
	if((int)$IS_URI==1){
		$r[]			=	"define('BASEURI','$base_dir')";
		$replace[]		=	"config['site_domain']	= '".$base_dir."';";
	}
	else{
		if($base_dir==''){
			$r[]			=	"define('BASEURI','".$base_dir."/index.php/')";
			$replace[]		=	"config['site_domain']	= '".$base_dir."/';";
		}
		else{
			$r[]			=	"define('BASEURI','".$base_dir."index.php/')";
			$replace[]		=	"config['site_domain']	= '".$base_dir."';";
		}
	}
	$index			=	preg_replace($f,$r,$index);
	file_put_contents("../index.php",$index);
	//修改config配置文件
	$cfg_file		=	LMCMS_ROOT."app/config/config.php";				//数据库配置文件
	$cfg_str		=	file($cfg_file);
	//搜索替换
	$search[]		=	"#config\['site_domain'\]\s*\=\s*'.*';#i";
	foreach ($cfg_str as $tmp_str){
		@$new_cfg.= preg_replace($search,$replace,$tmp_str);
	}
	
	//写入数据库配置文件 
	$fp = fopen($cfg_file,'w');
	fwrite($fp,$new_cfg);
	fclose($fp);
	$fp	= fopen("install.lock","w+");
	fclose($fp);
	$setupinfo	.=	"网站地址:".getgpc("web_http_url","P")."<br>";
	$setupinfo	.=	"超级管理员用户名：$username<br>";
	$setupinfo	.=	"超级管理员密码：$userpw<br>";
	
	echo '<h3>'.$lang['save_setup_info'].'</h3>';
	echo '<h4>'.$setupinfo.'</h4>';
	echo ' <div class="btnbox marginbot"><input type="button" onclick="document.getElementById(\'info\').select();" value="'.$lang['copy'].'"><input type="button" onclick="window.open(\''.$theurl.'\',\'_blank\');window.close();" value="'.$lang['go_home'].'"></div>';
}
install_footer();
