<?php
function deaddslashes(&$str) {
	if (is_array($str))
		foreach ($str as $key => $v)
			deaddslashes($str[$key]);
	elseif (is_string($str))
		$str = stripslashes($str);
}

if (get_magic_quotes_gpc())	{
	deaddslashes($_GET);
	deaddslashes($_POST);
	deaddslashes($_COOKIE);
	deaddslashes($_REQUEST);
}

function strRand($length, $source = 'abcdefghijklmnopqrstuvwxyz0123456789') {
	$sourceLength = strlen($source);
	$temp = '';
	for ($i = $length; $i; $i--)
		$temp .= $source[rand(0, $sourceLength - 1)];
	return $temp;
}

if(!is_file('install.lock')) {
	ob_start();

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

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

	if($mysqli = @new mysqli($_SLEX['mysql']['host'], $_SLEX['mysql']['user'], $_SLEX['mysql']['pwd'], $_SLEX['mysql']['db'])) {
		$mysqli->set_charset('utf8');
		// --- 创建创始人信息 ---
		$_POST['pwd'] = trim($_POST['pwd']);
		if($_POST['pwd']!='') {
			$_POST['username'] = trim($_POST['username']);
			if($_POST['username']!='') {
				// --- 整理数据 ---
				$salt = strRand(16);
				$pwdhash = md5(md5($salt . $_POST['pwd']) . $salt);
				$mysqli->query('INSERT INTO `'.TB_PREFIX.'users` (`id` ,`username` ,`passhash` ,`salt` ,`email` ,`class` ,`regdate`, `regip` ,`lastip`, `msg_max`, `disk_max`) SELECT "1", "'.$mysqli->real_escape_string($_POST['username']).'", "'.$mysqli->real_escape_string($pwdhash).'", "'.$mysqli->real_escape_string($salt).'", "'.$mysqli->real_escape_string($_POST['email']).'", "'.$mysqli->real_escape_string(USR_CLS_ADMIN).'", "'.time().'", "'.ip2long('127.0.0.1').'", "'.ip2long('127.0.0.1').'", `msg_max`, `disk_max` FROM `'.TB_PREFIX.'groups` WHERE `id` = "'.$mysqli->real_escape_string(USR_CLS_ADMIN).'" LIMIT 1;');
				$mysqli->close();
				// --- 写入站点信息 ---
				$slexInfo = file_get_contents('../confs/slex.i.php');
				$slexInfo = preg_replace('/_SLEX\[\'site\'\].+?\);/s', "_SLEX['site'] = array(
'title' => '".$_POST['sitename']."',
'app' => 'Filiant',
'open' => true,
'lang' => 'zh-cn',
'icp' => '',
'cpy' => 'MAIYUN.NET',
'statcode' => '',
'closeinfo' => ''
);", $slexInfo);
				file_put_contents('../confs/slex.i.php', $slexInfo);
				echo json_encode(array(
					'result' => '1'
				));
			} else {
				echo json_encode(array(
					'result' => '0',
					'msg' => '请输入创始人账户'
				));
			}
		} else {
			echo json_encode(array(
				'result' => '0',
				'msg' => '请输入创始人密码'
			));
		}
	} else {
		echo json_encode(array(
			'result' => '0',
			'msg' => '数据库连接失败'
		));
	}
}

