<?php
ob_start();

define('__ROOT__', realpath(dirname(__FILE__)).'/');
define('__DATA__', __ROOT__.'data/');

include_once('../confs/slex.s.php');
include_once('../confs/slex.i.php');

if(!is_file('install.lock')) {
	if($mysqli = @new mysqli($_SLEX['mysql']['host'], $_SLEX['mysql']['user'], $_SLEX['mysql']['pwd'], $_SLEX['mysql']['db'], $_SLEX['mysql']['port'])) {
		$mysqli->set_charset('utf8');
		// --- 导入 SQL ---
		$sql = file_get_contents('db.sql');
		$sql = str_replace('EXISTS `', 'EXISTS `'.TB_PREFIX, $sql);
		$sql = str_replace('TABLE `', 'TABLE `'.TB_PREFIX, $sql);
		$sql = str_replace('INTO `', 'INTO `'.TB_PREFIX, $sql);
		$sql = str_replace("\n\n","\n",str_replace("\r","\n",str_replace("\r\n","\n", $sql)));
		$sqlList = explode(";\n",$sql);
		$okey = true;
		$sqlStr = '';
		foreach($sqlList as $sqlStr) {
			$sqlStr = trim($sqlStr);
			if($sqlStr!='') {
				$r = $mysqli->query($sqlStr.';');
				if($r===false) {
					$okey = false;
					break;
				}
			}
		}
		if($okey) {
			echo json_encode(array(
				'result' => '1'
			));
		} else {
			echo json_encode(array(
				'result' => '0',
				'msg' => '数据表导入失败，在：'."\n\n".$sqlStr.";\n\n安装失败"
			));
		}
		$mysqli->close();
	} else {
		echo json_encode(array(
			'result' => '0',
			'msg' => '数据库连接失败'
		));
	}
}

