<?php
/*
+-----------------------------------------------------------------------
| 项目名称: gatewei 建站引擎(gatewei web engine) version 2.0
+-----------------------------------------------------------------------
| copyright (c) 2008-2009 彭景康 版权所有
| author: pengjingkang
+-----------------------------------------------------------------------
| site: http://www.gatewei.com/
| demo site: http://demo.gatewei.com/
| borad: http://www.gatewei.com/forums.php
| email: gatewei@gmail.com
+-----------------------------------------------------------------------
*/
define('IN_GATEWEI', TRUE);
error_reporting(0);
set_magic_quotes_runtime(0);
$projectfile_exists=file_exists('./engine/project.inc.php');
$configfile_exists=file_exists('./config.inc.php');
if($projectfile_exists && $configfile_exists){
	@include('./engine/project.inc.php');
	@include('./config.inc.php');
}else{
	page_head();
	installmsg("请确认程序拷贝是否完整","install.php",0);
	page_footer();
    exit;
}
	$softname=$g_zh_name.'('.$g_project_name .') '.$g_project_version;
	$title=$g_zh_name.'('.$g_project_name .')'.$g_project_version.' 安装向导';
       foreach ($_POST as $key => $val)
       {
		   if ( ! get_magic_quotes_gpc()){
				$val = addslashes($val);
		   }
	   }
$page = (! isset($_GET['page']) || $_GET['page'] > 4) ? 1 : $_GET['page'];

if (phpversion() < '5.0.0'){
	page_head();
	installmsg("系统要求php版本在5.0.0以上","install.php",false);
	page_footer();
}

page_head();

$installed=0;
if($installed){
	installmsg("程序已经安装，不能重复进行安装","index.php",false);
}

// 步骤一
if ($page == 1){
    echo license();
}

// 步骤二
elseif ($page == 2)
{
	$datadir='clientdata';
    if(@is_writable($datadir)){
		if(!is_dir('clientdata/logs')){
			if(!@mkdir('clientdata/logs', 0777)){
				installmsg("logs目录没有创建成功","install.php?page=1",false);
			}
		}
		if(!is_dir('clientdata/cache')){
			if(!@mkdir('clientdata/cache', 0777)){
				installmsg("cache目录没有创建成功","install.php?page=1",false);
			}
		}
		if(!is_dir('clientdata/attachment')){
			if(!@mkdir('clientdata/attachment', 0777)){
				installmsg("attachment目录没有创建成功","install.php?page=1",false);
			}
		}
	}else{
		installmsg("clientdata目录不存在或不能写入,请确认程序拷贝是否完整及其权限","install.php?page=1",false);
	}
	echo databaseform();

}
// 步骤三
elseif ($page == 3)
   {
	  if($_GET['skipw2c']!="configisok"){
		   if(!is_writable('config.inc.php')){
				installmsg("config.inc.php文件不能写入,请手工配置","install.php?page=1",false);
		   }else{
			   write2config();
		   }
	  }
    $sqlfile="db.sql";
 	$fp = fopen($sqlfile, 'rb');
	$sql = fread($fp, filesize($sqlfile));
	fclose($fp);
       $errors = array();
	   $conn=@mysql_connect($db_hostname,$db_username,$db_password);
	   if(!$conn){
		   $errors[] = "链接数据库服务器出错";
	}
	   @mysql_query("set character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $conn);
	   if(!@mysql_select_db($db_name, $conn)){
             if(!mysql_query("CREATE DATABASE $db_name",$conn)){
				 $errors[] = "数据库没有创建成功，请向提供商确认数据库相关事项";
			 }else{
				mysql_select_db($db_name, $conn);
			 }
		}
	if (count($errors) > 0) {
			installmsg($errors,"install.php?page=2",false); 
	}
	runquery($sql);
    require_once('./engine/cache.func.php');
	require_once('./engine/db_mysql.class.php');
	$db = new db;
    $db->connect($db_hostname,$db_username,$db_password,$db_name);
	w2setting();
	w2friendlylink();
	w2articlecategory();
	w2productcategory();
	w2forumcategory();
	$navigation[]='网站首页;index.php;index;0';
	$navigation[]='新闻中心;articledisplay.php?pid=0&categoryid=1;article;1';
	$navigation[]='公司产品;product.php;product;0';
	$navigation[]='服务中心;forums.php;forum;0';
	w2navigation($navigation,$act="save");
	installmsg("网站安装成功,请手动删除install.php安装文件与db.sql数据库脚本文件<br><b>默认管理账号:admin</b><br><b>默认登录密码:admin888</b>","index.php",true);
   }

//  安装文件的头文件函数
// --------------------------------------------------------------------
// --------------------------------------------------------------------

function page_head()
{
?>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"
"http://www.w3.org/tr/html4/loose.dtd">
<head>
<title>gatewei 建站引擎(gatewei web engine)安装向导</title>
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
<meta http-equiv= 'pragma' content='no-cache' />
<style type='text/css'>

body {	margin-left: 0px;	margin-right: 0px;background-color: #3a6ea5;font-size:12px;font-family:microsoft yahei, arial, helvetica, sans-serif ; }
#widthtitlebox{	height:420px;	width:600px;	border:1px solid #cccccc;	margin:0px auto;	background-color:#f1ede5;margin-top:80px}
#widthnav{	padding:0px;	margin:0px;	list-style:none;}
#title{	margin-left:10px;	line-height:30px;	font-weight:bold;}
#widthnav li{ color:#ffffff;height:25px;width:600px;border-bottom:1px solid #cccccc;margin:0px auto;background-color:#000084;}
#mainbox{	height:320px;	width:560px;	border-bottom:1px solid #a9a9a9;	float:left;	margin:10px;}
#mainbox textarea{font-family:microsoft yahei, arial, helvetica, sans-serif; width: 500px;background-color: #ffffff;height: 220px; clip:  rect(); font-size: 12px;margin:20px; }
.input{font-family: microsoft yahei, arial, helvetica, sans-serif ; height:15px;padding: 2px 3px;}
.button{font-family:microsoft yahei, arial, helvetica, sans-serif ; font-size: 12px;color: #000000;background-color: #ffffff;border: 1px solid #7789aa;padding: 2px 3px;cursor:pointer;height:23;float:right;margin-right:30px;}
#formbox {width: 400px;border:1px solid #cccccc;height: 220px; font-size: 12px;margin:0px auto;margin-top:10px;text-align:center;
background-color:#f1ede5;
}
#formbox table{	margin-top:20px;	border-collapse:collapse;	width:100%;margin-left:20px;}
#formbox table td{	border-collapse:collapse;	margin-left:10px;	height:30px;text-align:left;}
#siteformbox {width: 400px;border:1px solid #cccccc;height: 200px; font-size: 12px;margin:0px auto;margin-top:30px;text-align:center;background-color:#f1ede5; }
#siteformbox ul{	padding:0px;	margin:0px;	list-style:none;}
#siteformbox ul li{	height:20px;	width:300px;	margin:0px auto;	margin-top:20px;} 
</style>
</head>
<body>
<?php
}

//  安装文件尾文件
// --------------------------------------------------------------------
// --------------------------------------------------------------------

function page_footer()
{
?>
</body>
</html>
<?php
}

function license() {
	global $title,$softname;?>
<form method='post' action='install.php?page=2'>
<div id="widthtitlebox">
<ul id="widthnav">
<li><span id='title'><?=$title?></span></li>
</ul>
<div id="mainbox"> 
<strong><?=$softname?>&nbsp;安装程序</strong><br>
      <br>
   请阅读下面的许可协议.<br>
<textarea name="textarea" readonly>
+-----------------------------------------------------------------------
| 项目名称:<?=$softname?>

| copyright (c) 2008-2010 pengjingkang
+-----------------------------------------------------------------------
| 声明：
| Gatewei 建站引擎(Gatewei Engine Enterprise)属于免费软件，您可以免费使用[注⑴]和
| 完整地传播该程序，
| 如果您愿意可以在您启用该程序后告诉我们，有任何疑问或建议请到讨论区：
| http://www.gatewei.com/forums.php，我们会尽快处理。
| 
| 重要声明：Gatewei 建站引擎是免费程序，但不意味着您可以二次发布[注⑵]
| 该程序，如果你有开发建议或对其作相应修改或改进，欢迎与我们联系。
| 
| 定制服务：Gatewei 建站引擎(gatewei web engine)提供按需定制服务，但定制
| 是收费的。
| 
| 注⑴：该软件免费受共创联盟许可证规则保护，使用时请保留我们的版权声明。
| 我们承诺该软件终身免费使用并享受软件的升级服务。
| 联盟许可证规则地址：http://cosoft.org.cn/html/about/lmxkz.php
| 
| 注⑵：二次发布指未经我们许可修改或未修改源代码后以相同或不同的软件名称
| 发布该程序。
| 
+-----------------------------------------------------------------------
| copyright (c) 2008-2010 彭景康 版权所有
| author: pengjingkang
+-----------------------------------------------------------------------
| site: http://www.gatewei.com/
| demo site: http://demo.www.gatewei.com/
| borad: http://www.gatewei.com/forums.php
| email: gatewei@gmail.com
+-----------------------------------------------------------------------
| 2010/08/01 08:00:00 beijing time zone
</textarea>
</div> <input type="submit" class="button" value="同意并下一步">
</div>
</form>
<?php }?>

<?php
// 数据库配置表单
function databaseform() {
	global $title , $db_hostname, $db_username, $db_password, $db_name;
	if($db_hostname && $db_username && $db_password && $db_name){
		$formaction="install.php?page=3&skipw2c=configisok";
	}else{
		$formaction="install.php?page=3";
	}
?>
<form method="post" action="<?php echo $formaction?>">
<div id="top"></div>
<div id="widthtitlebox">
<ul id="widthnav">
<li><span id='title'><?=$title?></span></li>
</ul>
<div id="mainbox"> 
<strong>步骤二</strong><br>
配置数据库相关信息<br>
<div id="formbox">
<table>
<tr>
<td width="30%" align="left">数据库服务器：</td>
<td width="70%"><input name="db_hostname" type="text" class="input" value="<?php echo $db_hostname?>" ></td>
</tr>
<tr>
<td align="left">数据库用户：</td>
<td><input name="db_username" type="text" class="input" value="<?php echo $db_username?>" ></td>
</tr>
<tr>
<td align="left">数据库密码：</td>
<td> <input name="db_password" type="password" class="input" value="<?php echo $db_password?>" ></td>
</tr>
<tr>
<td align="left">数据库名称：</td>
<td> <input name="db_name" type="text" class="input" value="<?php echo $db_name?>"></td>
</tr>
</table>
</div>
</div>
  <input type="submit" class="button" value="下一步">
</div>
</form>
<?php
}


	function installmsg($msg,$url,$ok)
	{
		global $title;
		$btnval=$ok?'完  成':'返  回';
	    echo "<form method='post' action='".$url."'>";
        echo "<div id='widthtitlebox'><ul id='widthnav'><li><span id='title'>".$title."</span></li></ul></ul><div id='mainbox'>安装提示：<br><br>";
		if(is_array($msg))
		{
		foreach($msg as $val)
			{
		    echo  $val.'<br>';
		    }
		}
		else{echo  $msg;}
		echo "</div>";
		echo "<input name='submit' type='submit' class='button' value='".$btnval."'></div></form>";
		page_footer();
        exit;
	}



	function write2config(){
	global $_POST;

    $w  = "<?php";
	$w .= "\n if(!defined('IN_GATEWEI')){ \n\texit('Access Denied'); \n }";
	$w .= "\n//字符编码集类型\n\n";
	$w .= "\$dbcharset=\"utf8\";\n";
	$w .= "\$charset=\"utf-8\";\n";
 
	foreach ($_POST as $key => $val){
		$val = str_replace('\\', '\\\\', $val);
		$val = str_replace("'", "\\'", $val);
		$val = str_replace("\"", "\\\"", $val);
		$w .= "\$".$key." = \"".$val."\";\n";       
	} 
	$w .= "?>";
	$cfile = "config.inc.php";

	if ( ! $fp = @fopen($cfile, 'wb')){
		 $errors[] = "配置文件创建失败";
	}                
	fwrite($fp, $w, strlen($w));
	fclose($fp);
}



function runquery($sql) {
	global $dbcharset, $conn;
	$sql = str_replace("\r", "\n", $sql);
	$ret = array();
	$num = 0;
	$g=explode(";\n", trim($sql));
	foreach($g as $query) {
		$queries = explode("\n", trim($query));
		foreach($queries as $query) {
			$ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
		}
		$num++;
	}
	unset($sql);
	foreach($ret as $query) {
		$query = trim($query);
		if($query) {
			mysql_query($query,$conn);
		}
	}
}

function gaddslashes($string,$force = 0) {
	!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
	if(!MAGIC_QUOTES_GPC || $force) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = gaddslashes($val,$force);
			}
		} else {
			$string = addslashes($string);
		}
	}
	return $string;
}
?>