<?php
define ( '_LD_DIR_', dirname ( __FILE__ ) ); //当前目录
define ( '_LD_PATH_', dirname ( dirname ( __FILE__ ) ) ); //网站根目录
//引入安装配置
if (! defined ( "APP_PATH" )) {
	define ( "APP_PATH",dirname( dirname ( __FILE__ ) ));
}
define ( "I_PATH", APP_PATH . "/include" );
include (_LD_DIR_ . '/config/install.init.php');
include_once (I_PATH . "/functions.php");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php
echo _INSTALL_TITLE_;
?></title>
<link href="install.css" rel="stylesheet" type="text/css" />
<script language="JavaScript">
function formChk(){
if(document.f.host.value==""){alert("请填写Mysql地址");return false;}
if(document.f.port.value==""){alert("请填写Mysql端口");return false;}
if(document.f.database.value==""){alert("请填写数据库名");return false;}
if(document.f.login.value==""){alert("请填写数据库帐号");return false;}
if(document.f.prefix.value==""){alert("请填写表前缀");return false;}
if(document.f.auser.value==""){alert("请填写管理员帐号");return false;}
if(document.f.apass.value==""){alert("请填写管理员密码");return false;}
}
</script>
</head>
<body>
<div class="top"><span>系统安装</span><img src="logo.gif" /></div>
<div class="wp">
<div class="main">
<?php
error_reporting ( 0 );
if (is_file ( "install.txt" )) {
	echo '系统已经安装，如需要重新安装请删除install目录下的install.txt文件';
	exit ();
}
if (( int ) $_GET ['backup'] != 1) {
	$backup_db = array ();
	$fdir = '../include/backup/';
	if ($dp = @opendir ( $fdir )) {
		while ( false !== ($file = readdir ( $dp )) ) {
			if ($file != '.' && $file != '..' && is_file ( $fdir . $file )) {
				$backup_db = array_merge ( $backup_db, array ($fdir . $file ) );
			}
		}
		@closedir ( $dp );
	}
}
$go = ( int ) $_GET ['go'];
if (! $go) {
	function file_info($file_path) {
		$file_path = realpath ( $file_path );
		if (@is_readable ( $file_path ) && @is_writable ( $file_path )) {
			clearstatcache ();
			return true;
		} else {
			clearstatcache ();
			return false;
		}
	}
	if (substr ( PHP_VERSION, 0, 1 ) != '5') {
		$php = "<em>检测未通过</em>运行环境要求PHP版本5！";
	}
	if (! extension_loaded ( 'gd' )) {
		$gd = "<em>检测未通过</em>运行环境要求安装GD库！";
	} else {
		$gd_info = gd_info ();
		$gd_info = substr ( $gd_info ['GD Version'], 9, 1 );
		if (( int ) $gd_info < '2') {
			$gd = "<em>检测未通过</em>运行环境要求安装GD库版本大于等于2！";
		}
	}
	if (! file_info ( '../uploads' )) {
		$uploads = "<em>检测未通过</em>此目录要求可读写";
	}
	if (! file_info ( '../include/backup' )) {
		$backup = "<em>检测未通过</em>此目录要求可读写";
	}
	if (! file_info ( '../include/cache' )) {
		$cache = "<em>检测未通过</em>此目录要求可读写";
	}
	if (! file_info ( '../config.php' )) {
		$config = "<em>检测未通过</em>此文件要求可读写";
	}
	?>

<form action="index.php?go=2" method="post" name="f"
	onsubmit="return formChk()">
<div class="switchs">&nbsp;请填写数据库信息</div>
<div class="info">
<dl>
	<dt>网站名称：</dt>
	<dd><input name="site_title" type="text" class="int" value="东莞蓝鼎网络" /></dd>
	<dd class="t">填写网站名称</dd>
</dl>
<dl>
	<dt>Mysql地址：</dt>
	<dd><input name="host" type="text" class="int" value="localhost" /></dd>
	<dd class="t">一般无需修改</dd>
</dl>
<dl>
	<dt>Mysql端口：</dt>
	<dd><input name="port" type="text" class="int" value="3306" /></dd>
	<dd class="t">一般无需修改</dd>
</dl>
<dl>
	<dt>数据库名：</dt>
	<dd><input name="database" type="text" class="int" /></dd>
	<dd class="t">如果数据库未建立，系统将自动为你生成</dd>
</dl>
<dl>
	<dt>数据库帐号：</dt>
	<dd><input name="login" type="text" class="int" /></dd>
	<dd class="t">请输入</dd>
</dl>
<dl>
	<dt>数据库密码：</dt>
	<dd><input name="password" type="text" class="int" /></dd>
	<dd class="t">请输入</dd>
</dl>
<?php
	if (empty ( $backup_db )) {
		?>
        <dl>
	<dt>表前缀：</dt>
	<dd><input name="prefix" type="text" class="int"
		value="<?php
		echo _DB_DEFAULT_PREV_;
		?>" /></dd>
	<dd class="t">一般无需修改，同数据库安装多个请务必修改</dd>
</dl>
<dl>
	<dt>管理员帐号：</dt>
	<dd><input name="auser" type="text" class="int" /></dd>
	<dd class="t">请输入</dd>
</dl>
<dl>
	<dt>管理员密码：</dt>
	<dd><input name="apass" type="text" class="int" /></dd>
	<dd class="t">请输入</dd>
</dl>
<dl>
	<dt>验证码：</dt>
	<dd><input type="radio" name="vercode" value="1" checked="checked" />开启</label>&nbsp;<label><input
		type="radio" name="vercode" value="0" />关闭</label></dd>
	<dd><img src="../include/vercode.php" width="58" height="25" /></dd>
	<dd class="t">如无法看到左图验证码，请关闭验证码功能</dd>
</dl>
<dl>
	<dl>
		<dt>QQ同步：</dt>
		<dt>
		
		
		<dd><input type="radio" name="qq_sync" value="1" checked="checked" />开启</label>&nbsp;<label><input
			type="radio" name="qq_sync" value="0" />关闭</label></dd>
		<dd>开启后发步的文章同步到QQ空间</dd>
		</dt>
	</dl>
	<dl>
		<dt>新浪微博同步：</dt>
		<dt>
		
		
		<dd><input type="radio" name="weibo_sync" value="1" checked="checked" />开启</label>&nbsp;<label><input
			type="radio" name="weibo_sync" value="0" />关闭</label></dd>
		<dd>开启后发步的文章同步到新浪微博</dd>
		</dt>
	</dl>
	<!--  <dl><dt>域名</dt><dt><dd><input type="text" name="domain" class="int"/></dd><dt class="t">请输入</dt></dt></dl>-->
	<dl>
		<dt>电子邮箱：</dt>
		<dt>
		
		
		<dd><input type="text" name="email" class="int" /></dd>
		<dt class="t">请输入</dt>
		</dt>
	</dl>
	<dl>
		<dt>主营行业：</dt>
		<dt><select name=industry id="industry">
			<option value="empty">--请选择--</option>
			<option value="安全、防护">安全、防护</option>
			<option value="办公、文教">办公、文教</option>
			<option value="包装">包装</option>
			<option value="传媒、广电">传媒、广电</option>
			<option value="电工电气">电工电气</option>
			<option value="电子元器件">电子元器件</option>
			<option value="二手设备转让">二手设备转让</option>
			<option value="服饰">服饰</option>
			<option value="服装">服装</option>
			<option value="纺织、皮革">纺织、皮革</option>
			<option value="环保">环保</option>
			<option value="化工">化工</option>
			<option value="家居用品">家居用品</option>
			<option value="交通运输">交通运输</option>
			<option value="精细化学品">精细化学品</option>
			<option value="机械及行业设备">机械及行业设备</option>
			<option value="家用电器">家用电器</option>
			<option value="建筑、建材">建筑、建材</option>
			<option value="库存积压">库存积压</option>
			<option value="礼品、工艺品、饰品">礼品、工艺品、饰品</option>
			<option value="农业">农业</option>
			<option value="能源">能源</option>
			<option value="汽摩及配件">汽摩及配件</option>
			<option value="数码、电脑">数码、电脑</option>
			<option value="食品、饮料">食品、饮料</option>
			<option value="商务服务">商务服务</option>
			<option value="通信产品">通信产品</option>
			<option value="玩具">玩具</option>
			<option value="五金、工具">五金、工具</option>
			<option value="橡塑">橡塑</option>
			<option value="运动、休闲">运动、休闲</option>
			<option value="冶金矿产">冶金矿产</option>
			<option value="仪器仪表">仪器仪表</option>
			<option value="印刷">印刷</option>
			<option value="医药、保养">医药、保养</option>
			<option value="照明工业">照明工业</option>
			<option value="纸业">纸业</option>
		</select></dt>
	</dl>
	<dl>

		<dt>安装模式：</dt>
		<dd><input type="radio" name="installmodel" value="0" />普通安装&nbsp;&nbsp;
		<input type="radio" name="installmodel" value="1" checked="checked" />强行安装<font
			color="#ff000">(选择了强行安装将会覆盖同表名的数据)</font></dd>
	</dl>
<?php
	}
	?>




</div>
<?php
	if (count ( $backup_db ) > 0) {
		?>
	<input name="go_backup" type="hidden" value="1" />
<div class="switchs">&nbsp;检测到您的系统存在数据库备份</div>
<div class="info">
<dl>
	<dt>选择数据备份：</dt>
	<dd><select name="backup_db">
        <?php
		foreach ( $backup_db as $v ) {
			?>
    	  <option value="<?php
			echo $v;
			?>"><?php
			echo str_ireplace ( '../include/backup/', '', $v );
			?></option>
        <?php
		}
		?>
    	</select></dd>
</dl>
<dl>
	<dt>&nbsp;</dt>
	<dd><a href="?backup=1">不恢复数据备份，执行全新安装</a></dd>
</dl>
</div>
<?php
	}
	?>
	<div class="switchs" style="margin-top: 10px;">&nbsp;环境检测</div>
<div class="info">
<dl>
	<dt>PHP版本：</dt>
	<dd><?php
	if ($php) {
		echo $php;
	} else {
		echo '<span>检测通过</span>';
	}
	?></dd>
</dl>
<dl>
	<dt>GD库：</dt>
	<dd><?php
	if ($gd) {
		echo $gd;
	} else {
		echo '<span>检测通过</span>';
	}
	?></dd>
</dl>
<dl>
	<dt>目录权限：</dt>
	<dd>uploads <?php
	if ($uploads) {
		echo $uploads;
	} else {
		echo ' <span>检测通过</span>';
	}
	?><br />include/backup <?php
	if ($backup) {
		echo $backup;
	} else {
		echo ' <span>检测通过</span>';
	}
	?><br />include/cache <?php
	if ($cache) {
		echo $cache;
	} else {
		echo ' <span>检测通过</span>';
	}
	?><br />config.php <?php
	if ($config) {
		echo $config;
	} else {
		echo ' <span>检测通过</span>';
	}
	?></dd>
</dl>
<dl style="margin-bottom: 20px;">
	<dt>&nbsp;</dt>
	<dd><input type="submit" id="submit" value="开始安装" class="btnbig" /></dd>
</dl>
</div>
</form>
<?php
}

if ($go == 2) {
	$con = mysql_connect ( $_POST ['host'] . ':' . $_POST ['port'], $_POST ['login'], $_POST ['password'] );
	if (! mysql_select_db ( $_POST ['database'], $con )) {
		/*****************************************************/
		$ld_sql = "CREATE DATABASE `" . $_POST ['database'] . "`";
		mysql_query ( $ld_sql, $con );
		mysql_select_db ( $_POST ['database'], $con );
	/*****************************************************/
	//echo '<script>alert("数据库连接失败，请检查数据库信息输入是否正确：'.mysql_error().'");javascript:history.go(-1);</script>';exit;
	

	}
	$mysqlv = mysql_get_server_info ();
	if (substr ( $mysqlv, 0, 1 ) < 5) {
		echo '<script>alert("您的数据库版本过低，Mysql版本要求大于等于5");javascript:history.go(-1);</script>';
		exit ();
	}
	;
	mysql_query ( 'SET NAMES UTF8', $con );
	
	$configfile = '../config.php';
	$fp_tp = @fopen ( $configfile, "r" );
	$fp_txt = @fread ( $fp_tp, filesize ( $configfile ) );
	@fclose ( $fp_tp );
	if (( int ) $_POST ['go_backup'] == 1) {
		$db = array ('db', 'host', 'port', 'database', 'login', 'password' );
	} else {
		$db = array ('db', 'host', 'port', 'database', 'login', 'password', 'prefix' );
	}
	foreach ( $db as $v ) {
		if ($v == 'port') {
			$fp_txt = preg_replace ( "/'" . $v . "' => .*?,/", "'" . $v . "' => " . $_POST [$v] . ",", $fp_txt );
		} else {
			$fp_txt = preg_replace ( "/'" . $v . "' => '.*?',/", "'" . $v . "' => '" . $_POST [$v] . "',", $fp_txt );
		}
	}
	$ext = array ('ext', 'qq_sync', 'weibo_sync', 'http_path', 'site_title' );
	foreach ( $ext as $a ) {
		
		//获取网站安装地址，并写进配置文件
		if ($a == "http_path") {
			$str = "http://" . $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI'];
			$str = "'" . str_replace ( 'install/index.php?go=2', '', $str ) . "'";
			$fp_txt = preg_replace ( "/'" . $a . "' => .*?,/", "'" . $a . "' => " . $str . ",", $fp_txt );
		} elseif ($a == 'site_title') {
			$fp_txt = preg_replace ( "/'" . $a . "' => .*?,/", "'" . $a . "' => '" . $_POST [$a] . "',", $fp_txt );
		} else {
			
			$fp_txt = preg_replace ( "/'" . $a . "' => .*?,/", "'" . $a . "' => " . $_POST [$a] . ",", $fp_txt );
		}
	}
	$fpt_tpl = @fopen ( $configfile, "w" );
	@fwrite ( $fpt_tpl, $fp_txt );
	@fclose ( $fpt_tpl );
	if (( int ) $_POST ['vercode'] == 0) {
		$incfile = '../include/inc.php';
		$inc_tp = @fopen ( $incfile, "r" );
		$inc_txt = @fread ( $inc_tp, filesize ( $incfile ) );
		@fclose ( $inc_tp );
		$inc_txt = preg_replace ( "/'vercode' => .*?,/", "'vercode' => 0,", $inc_txt );
		$inct_tpl = @fopen ( $incfile, "w" );
		@fwrite ( $inct_tpl, $inc_txt );
		@fclose ( $inct_tpl );
	}
	ob_implicit_flush ( 1 );
	ob_end_flush ();
	?>
<div class="switchs">&nbsp;执行安装</div>
<div class="info">
<dl>
	<dt>建立数据库：</dt>
	<dd>
	
	
<?php
	//创建数据表
	

	$query = '';
	$fp = fopen ( dirname ( __FILE__ ) . '/' . _INSTALL_SQL_, 'r' );
	while ( ! feof ( $fp ) ) {
		$line = rtrim ( fgets ( $fp, 1024 ) );
		if (preg_match ( "#;$#", $line )) {
			$query .= $line . "\n";
			$query = str_replace ( '#ld_', $_POST ['prefix'], $query );
			if ($mysqlVersion < 4.1) {
				$rs = mysql_query ( $query, $con );
				
				if (! $rs) {
					exit ( 'SQL文件错误，请检查SQL文件后安装！或者数据重复，请手工删除数据后再安装。 </br><a href="javascript:history.go(-1)"><strong><font color="#ff0000">返回上一步</font></strong></a>' );
				}
			} else {
				if (preg_match ( '#CREATE#i', $query )) {
					$rs = mysql_query ( preg_replace ( "#TYPE=MyISAM#i", $sql4tmp, $query ), $con );
					if (! $rs) {
						exit ( 'SQL文件错误，请检查SQL文件后安装！或者数据重复，请手工删除数据后再安装。</br><a href="javascript:history.go(-1)"><strong><font color="#ff0000">返回上一步</font></strong></a>' );
					}
				} else {
					$rs = mysql_query ( $query, $con );
					if (! $rs) {
						exit ( 'SQL文件错误，请检查SQL文件后安装！或者数据重复，请手工删除数据后再安装。</br><a href="javascript:history.go(-1)"><strong><font color="#ff0000">返回上一步</font></strong></a>' );
					}
				}
			}
			$query = '';
		} else if (! preg_match ( "#^(\/\/|--)#", $line )) {
			$query .= $line;
		}
	}
	
	fclose ( $fp );
	$ip = ip ();
	$domain = $str;
	$email = $_POST ['email'];
	$industry = $_POST ['industry'];
	$version = $iConfig ['ext'] ['version']; // "";
	$installip = $ip;
	echo '<span>数据库安装成功</span>';
	
	?>
</dd>
</dl>
<?php
	$filename = "install.txt";
	$fp = @fopen ( "$filename", "w" );
	@fclose ( $fp );
	echo '<dl><dt></dt><dd class="oka">安装全部完成！[请删除install文件夹] <a href="../"><span>[浏览网站]</span></a><a href="../admin.php"><span>[进入后台]</span></a></dd></dl>';
	submitdata ( $domain, $email, $industry, $version, $installip );
	?>
</div>
<?php
}
?>
<div style="clear: both"></div>
</div>
</div>
<div class="bottom"><a href="#" target="_blank"><?php
echo _INSTALL_COMPANY_;
?></a> 1.0.0 © 2010-2013</div>
</body>
</html>
