<?php
/**
 * 金戈企业建站系统：系统安装界面
 *
 *
 * @name 
 * @author 烟鬼 QQ:460874 Email:460874@qq.com
 * @version 0.1  $id  install.php
 * @copyright Copyright (c) 2007 - 2008 烟鬼
 */
require('../config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>金戈企业建站系统：安装界面</title>
<link href="" rel="stylesheet" type="text/css" />
<link href="css/css.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.STYLE2 {color: #FF0000}
.STYLE3 {
	font-size: 24px;
	font-family: "黑体";
	font-weight: bold;
}
-->
</style>
</head>

<body>
<div id="error_a">
	<div id="error_a_a">
	<div id="op"><img src="img/logo.gif" />执行安装界面</div><br />
		<div id="error_ok">
<?php

$host = $_POST['host'];
$name = $_POST['name'];
$mysqlpass = $_POST['mysqlpass'];
$db = $_POST['db'];
$admin = $_POST['admin'];
$adminpass = $_POST['adminpass'];
$adminpassi = $_POST['adminpassi'];
$file = 'jnngii.sql';
if ($adminpass != $adminpassi) 
{
     echo '两次管理员密码输入不一致';
     exit;
}
//尝试连接服务器
echo "尝试连接数据库主机：";
if (!@$conn = mysql_connect($host, $name, $mysqlpass)) 
	{
        echo "无法连接<br />" . mysql_error();
        exit;
    }
echo "成功连接到 {$host}<br />";

//检查Mysql服务器版本
$rs = mysql_query('SELECT VERSION()');
$row = mysql_fetch_row($rs);
$version = $row[0];
if ($version >= '4.1.3') 
{
	mysql_query("SET NAMES 'gb2312'");
	$version = 5;
} 
else 
{
	$version = 3;
}
echo "尝试打开数据库 {$db}：";
if (!@mysql_select_db($db))
	{
        echo "数据库不存在<br />";
        echo "尝试建立数据库 {$db}：";

        if (!@mysql_query("CREATE DATABASE {$db}"))
        {
            echo "失败<br />" .mysql_error();
            exit;
        }
        $result = mysql_select_db($db);
    }
echo "成功<br />";

if (file_exists($file)) 
{
     if (!is_readable($file)) 
     {
          echo '请将jnngii.sql文件属性改为只读或全部';
          exit;
     }
     $script = file_get_contents($file);
     $scr = explode(';', $script);
     foreach ($scr as $sql) 
     {
            $sql = trim($sql);
            if (strtoupper(substr($sql, 0, 12)) == 'CREATE TABLE') 
            {
            	if ($sql == '') { continue; }
            	if (!@mysql_query($sql)) 
            	{
                	echo "\n执行错误：\n" . mysql_error() . "<br />";
	                exit;
            	}
            }
     }
}
    echo "$file.....OK<br />";
	$cons = new mysqli($host,$name,$mysqlpass,$db);
	$result = $cons->query("set names 'gb2312'");
	$result = $cons->query("INSERT INTO root VALUES 
			(NULL,'$admin',sha1('$adminpass'))");
	$result = $cons->query("INSERT INTO nav VALUES 
							(NULL,'1','网站首页','index.php'),
							(NULL,'1','企业新闻','news.php'),
							(NULL,'1','企业简介','qyjj.php'),
							(NULL,'1','企业荣誉','qyry.php'),
							(NULL,'1','企业风采','qyfc.php'),
							(NULL,'1','产品展示','chanpin.php'),
							(NULL,'1','企业招聘','job.php'),
							(NULL,'1','在线留言','book.php')");
	if (!$result) 
	{
	     echo '添加失败，请登陆Mysql删除之前安装的所有表重新安装<br />';
	}
	else 
	{
		echo '原始数据以及管理员添加成功....<br />';
	}
	echo '写入配置文件：';
	echo $configname = APP_DIR.'/dsn.inc.php';
	if (file_exists($configname)) 
	{
	      echo "<br />* 配置文件已经存在，该文件将被覆盖 *<br />";
	}
	if (!get_magic_quotes_gpc())
	{
		$host = addslashes($host);
    	$name = addslashes($name);
    	$mysqlpass = addslashes($mysqlpass);
    	$db = addslashes($db);
	}
$config = <<<EOT
<?php
//服务器地址
\$localhost = '{$host}';

//用户名
\$user = '{$name}';

//登陆密码
\$pass = '{$mysqlpass}';

//选择数据库
\$db = '{$db}';
?>
EOT;
	$fp = fopen($configname, 'w');
    if (!$fp) {
        echo "尝试写入文件失败。<br />";
        exit;
    }

    fwrite($fp, $config);
    fclose($fp);
    echo "<br />写入配置文件成功。<br /><span class='STYLE2'><span class='STYLE3'>安装完成</span></span><br />
			<span class='STYLE2'>请务必将install目录以及目录内的所有文件删除</span>";
?>
		</div>
		<div id="see"><a href="../isadmin/login.php"><img src="img/admin.gif" border="0" /></a>　　　　　
	    <a href="../index.php"><img src="img/go.gif" border="0" /></a></div>
	</div>
</div>
</body>
</html>