<?php
set_time_limit( 0 );
?><html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WebMagiK安装页面</title>
<meta name="keywords" content="<?php echo _conf( 'WEB_SITE_SEO_META_KEYWORD' ) ?>">
<meta name="description" content="<?php echo _conf( 'WEB_SITE_SEO_META_DESCRIP' ) ?>">
<?php echo _conf('WEB_SITE_SEO_HEADER_CODE'); ?>
<link href="res/css/douya.css" rel="stylesheet" type="text/css">
</head>

<body>
<br/>
<table border="0" width="98%" id="table1" cellspacing="0" cellpadding="0" height="40" align="center">
	<tr>
		<td valign="top"><br/><span class="title_2">WebMagik快速安装页面</span>&nbsp;&nbsp;&nbsp;
		</td>
		<td rowspan="2" valign="bottom">
		<p align="right">
		<a href="http://webmagik.cn/"><img border="0" src="res/img/logo.png"  vspace="8" hspace="20"></a><br/>
				</td>
	</tr>
	</table>
	<table border="0" width="98%" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td valign="top">
		</td>
		<td align="right" valign="top">
		</td>
	</tr>
</table>
<hr align="left" color="#eff1f2" noShade SIZE="1">
<div style="padding:30px">
<?php
/*
Array
(
    [i] => install_save
    [db_host] => localhost
    [db_user] => root
    [db_password] => 
    [db_name] => webmagik
    [admin_username] => admin
    [admin_email] => admin@admin.com
    [admin_password] => admin
    [cdb_cookietime] => 0
    [cdb_cpcollapsed] => 0
    [cdb_sid] => HJ1uYR
    [PHPSESSID] => 157b23d7a17deb2b759af9f784b7802f
)
*/

// 1 创建数据配置文档
$config['DB_HOST'] = $_REQUEST['db_host'];
$config['DB_USER'] = $_REQUEST['db_user'];
$config['DB_PASS'] = $_REQUEST['db_password'];
$config['DB_NAME'] = $_REQUEST['db_name'];

$db_config_file = '<?php $__db_config = ' . var_export( $config , 1 ) . ';  $__config = array_merge( $__config , $__db_config );  ?>' ;

file_put_contents( ROOT . 'conf/db.config.php' , $db_config_file );

// 2 初始化数据库和数据表
if(!$connect=mysql_connect( $config['DB_HOST'] , $config['DB_USER'] , $config['DB_PASS'] ))
{
	echo "数据库联接失败";
	exit;
}

mysql_query( "set names 'utf8'" , $connect );

$sql = "CREATE DATABASE IF NOT EXISTS `{$config['DB_NAME']}`";
mysql_query(trim($sql),$connect);

if(!$select=mysql_select_db( $config['DB_NAME'] , $connect ))
{
	echo "无法选择数据库,或数据库不存在";
	exit;
}

$files = preg_replace( "/(#.+[\r|\n]*)/" , '' , file_get_contents( ROOT . 'install/douya.sql' ) );
$sqls = split_sql_file( $files );

foreach( $sqls as $sql )
{
	mysql_query(trim($sql),$connect);
}

if( mysql_errno() == 0 )
{
	//echo '安装成功！';
	//rename( './install.php' , './installed-'.md5(rand(1,100).time()).'.php' );

}
else
{
	echo mysql_error();
}


// 3 初始化城市表
//require( ROOT . 'install/init_city_table.php' );


// 4 初始化管理账号
$sql = "INSERT INTO `dy_user` (  `dy_email` , `dy_password` , `dy_joindate` , `dy_nickname` , `dy_isactive` , `dy_level`  ) 
VALUES (  '" . _s( $_REQUEST['admin_email'] ) . "', '" .  _s( MD5($_REQUEST['admin_password']) ) . "', NOW( ) , '" . _s( $_REQUEST['admin_username'] ) . "', '1', '9');";

mysql_query( $sql ,$connect );

echo mysql_error();

/*
*/

$_REQUEST['a'] = 'install';

echo '安装完成，请删除install目录后，<a href="index.php">访问index.php</a>';

function _s( $str )
{
	return mysql_real_escape_string( $str );
}


 function split_sql_file($sql, $delimiter = ';') 
 {
        $sql               = trim($sql);
        $char              = '';
        $last_char         = '';
        $ret               = array();
        $string_start      = '';
        $in_string         = FALSE;
        $escaped_backslash = FALSE;

        for ($i = 0; $i < strlen($sql); ++$i) {
            $char = $sql[$i];

            // if delimiter found, add the parsed part to the returned array
            if ($char == $delimiter && !$in_string) {
                $ret[]     = substr($sql, 0, $i);
                $sql       = substr($sql, $i + 1);
                $i         = 0;
                $last_char = '';
            }

            if ($in_string) {
                // We are in a string, first check for escaped backslashes
                if ($char == '\\') {
                    if ($last_char != '\\') {
                        $escaped_backslash = FALSE;
                    } else {
                        $escaped_backslash = !$escaped_backslash;
                    }
                }
                // then check for not escaped end of strings except for
                // backquotes than cannot be escaped
                if (($char == $string_start)
                    && ($char == '`' || !(($last_char == '\\') && !$escaped_backslash))) {
                    $in_string    = FALSE;
                    $string_start = '';
                }
            } else {
                // we are not in a string, check for start of strings
                if (($char == '"') || ($char == '\'') || ($char == '`')) {
                    $in_string    = TRUE;
                    $string_start = $char;
                }
            }
            $last_char = $char;
        } // end for

        // add any rest to the returned array
        if (!empty($sql)) {
            $ret[] = $sql;
        }
        return $ret;
 }

?>
</body>
</form>
</html>