<?php
error_reporting(7);
if($executeAction=='1'){
	//插入基本平台数据
	$db=@mysql_connect($database_host,$database_username,$database_password);
	if(!$db)$error="数据库主机、用户名、密码中一个或多个有误码";
	if(!$error){
		if(!@mysql_select_db($database_name,$db)){
			$error="数据库名有错或您没有访问该数据库的权限";
		}
	}
	if(!$error){
		//更新数据库
		foreach ($_POST as $key => $val)
		{
			if(!$val) continue;
			$sql = "UPDATE config SET value = '" . trim($val) . "' WHERE code = '" . $key . "'";
			mysql_query($sql,$db);
		}
		$sql = "UPDATE admins set UserID='$AdminUserID',iStatus='1',Password='".md5($AdminPassword)."',Perm='|SYSTEM|'";
		mysql_query($sql,$db);
	}
	@mysql_close($db);
	if(!$error){
		//生成配置文件
		$filename = "../common/Config.inc.php";
		$fp = @fopen($filename, "w+");
		if($API_WEB_URL == null || $API_WEB_URL == 'http://')
		{
			$API_WEB_URL='http://'.$_SERVER['SERVER_NAME'];
		}
		else
		{
			$arr = explode('/',$API_WEB_URL);
			$NUM =count($arr);
			if($arr[$NUM-1] =='')
			{
				$API_WEB_URL = substr($API_WEB_URL,0,-1);
			}
		}
		
		if($fp){
			$content = "<?php \r\n";
			$content.= "define (\"DB_SERVER\",'".$database_host."'); \r\n";
			$content.= "define (\"DB_NAME\",'".$database_name."'); \r\n";
			$content.= "define (\"DB_LOGIN\",'".$database_username."'); \r\n";
			$content.= "define (\"DB_PW\",'".$database_password."'); \r\n";
			$content.= "define (\"DB_PORT\",3306); \r\n\r\n";
			$content.= "define (\"API_WEB_URL\",''); \r\n";
			$content.= "?>";
			fwrite($fp, $content);
			include "finish.inc.php";
	   }else{
			$error = "<font color='red'>不能打开文件 ../common/Config.inc.php 进行读写</font>";
			$error .= "<br><a href='#' onclick='history.back()'><b>返回</b></a>";
	   }
	   @fclose($fp);

	   /* 写入安装锁定文件 */
		$fp = @fopen('../common/install.lock', 'wb+');
		if (!$fp)
		{
			$error .= "<br>打开install.lock文件失败";
		}
		if (!@fwrite($fp, "NICENIC API WEB INSTALLED"))
		{
			$error .= "<br>写入install.lock文件失败";
		}
		@fclose($fp);
	}
	if($error) echo "$error";	

}else{
?>
<script language="javascript">
function check(form){
	
	if(form.API_WEB_URL.value=='' || form.API_WEB_URL.value=='http://' ){
		alert("请输入网站URL");
		form.API_WEB_URL.focus();
		return false;
	}
	if(form.AdminUserID.value==''){
		alert("请输入后台用户名");
		form.AdminUserID.focus();
		return false;
	}
	if(form.AdminPassword.value==''){
		alert("请输入后台密码");
		form.AdminPassword.focus();
		return false;
	}
	if(form.AdminPassword.value != form.AdminPasswordC.value){
		alert("后台密码与确认密码不一致");
		form.AdminPassword.focus();
		return false;
	}
	
	
}
</script>
<form action='index.php?step=sysset' method='post' onSubmit="return check(this)">
<input type="hidden" name="database_type" value="<?=$database_type?>">
<input type="hidden" name="database_host" value="<?=$database_host?>">
<input type="hidden" name="database_name" value="<?=$database_name?>">
<input type="hidden" name="database_username" value="<?=$database_username?>">
<input type="hidden" name="database_password" value="<?=$database_password?>">
<input type="hidden" name="aUrl" value="http://sq.yinxi.net/">
  <table width="80%" border="0" cellspacing="1" cellpadding="3" align="center" class="inputline" style="border-bottom:0px;">
	<tr>
	  <td colspan="2" align="left"><strong>系统设置</strong>　(注意：以下填写的信息在安装完成后都可以在管理后台修改)</td>
	</tr>
	<tr>
	  <td colspan="2" align="left"><strong>管理后台安全设置</strong></td>
	</tr>
    <tr>
	  <td align="right">网站域名（如果放在二级目录请写出二级目录）:</td>
	  <td><input name="API_WEB_URL" type="text" value="http://"> <font color='red'>* （例如：http://www.yinxi.net）</font></td>
	</tr>
	<tr>
	  <td align="right">后台用户名:</td>
	  <td><input name="AdminUserID" type="text" value="admin"> <font color='red'>*</font></td>
	</tr>
	<tr>
	  <td align="right">后台密码:</td>
	  <td><input name="AdminPassword" type="Password"> <font color='red'>*</font></td>
	</tr>
	<tr>
	  <td align="right">密码确认:</td>
	  <td><input name="AdminPasswordC" type="Password"> <font color='red'>*</font></td>
	</tr>
	
	<tr>
	  <td colspan="2" align="left"><strong>网站信息相关设定</strong></td>
	</tr>
	<tr>
	  <td align="right">网站标题:</td>
	  <td><input name="SiteTitle" type="text"></td>
	</tr>
	<tr>
	  <td align="right">公司名:</td>
	  <td><input name="Company" type="text" size="40"></td>
	</tr>
	<tr>
	  <td align="right">地址:</td>
	  <td><input name="Addr" type="text" size="40"></td>
	</tr>
	<tr>
	  <td align="right">邮箱:</td>
	  <td><input name="Email" type="text"></td>
	</tr>
	<tr>
	  <td align="right">电话:</td>
	  <td><input name="Tel" type="text"></td>
	</tr>
	<tr>
	  <td align="right">传真:</td>
	  <td><input name="Fax" type="text"></td>
	</tr>
	<tr>
	  <td align="right">邮编:</td>
	  <td><input name="Zip" type="text"></td>
	</tr>
  </table>

  <div style="text-align:right;padding:20px 20px 20px 20px;width:90%;">
  	  <input type="hidden" value="1" name="executeAction">

	<?
		echo '<div style="float:left"><input type="button" value="上一步" class="BUTTON" onClick="location=\'index.php?step=syscheck\'"></div>';
		echo '<input type="submit" value="下一步" class="BUTTON"">';
	?>
	</div>
</form>
<? }?>