<?php
PHP_VERSION < '5.1' && die('System is only available for use with PHP 5.1.0 or later.');

// 执行时间开启
$MY_START_TIME = microtime(TRUE);
$cfg_rewrite = '1';
define('IN_MY', TRUE);
// 系统物理根目录
define('APP_ROOT', dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR);
define('MY_ROOT', dirname(__FILE__).DIRECTORY_SEPARATOR);
// data目录
define('MY_DATADIR', APP_ROOT.'data'.DIRECTORY_SEPARATOR);
// 加载配置文件
require_once APP_ROOT.'config.php';

// 设置编码
header('Content-type:text/html; charset='.$MyConfig['Common']['charset']);
// 设置时区
@date_default_timezone_set($MyConfig['Common']['timezone']);
// 不进行魔术过滤 php5.3废除了 set_magic_quotes_runtime(0);
ini_set('magic_quotes_runtime', 0);
// 开启页面压缩
function_exists('ob_gzhandler') ? ob_start('ob_gzhandler') : ob_start();
// 页面报错
//error_reporting(0);
session_start();
// 包含类文件
require_once 'install.class.php';
$install = new install();

$step = isset($_GET['step']{0}) ? $_GET['step'] : 1;
if($step == 1) {
	$license = file_get_contents(MY_ROOT.'license.html');
	include MY_ROOT.'templates'.DIRECTORY_SEPARATOR.'step-1.html';
} elseif($step == 2) {
	$isError = FALSE;
	$sp_gd = $install->checkGD();
	$isError = ($sp_gd > 0 ? $isError : TRUE);
	$sp_allow_url_fopen = (ini_get('allow_url_fopen') ? array('<img src="images/ok.gif" alt="成功" />', 'allow_url_fopen 已开启！') : array('<img src="images/error.gif" alt="失败" />', 'allow_url_fopen 未开启！'));
	$sp_safe_mode = (ini_get('safe_mode') ? array('<img src="images/error.gif" alt="失败" />', 'safe_mode 已开启！') : array('<img src="images/ok.gif" alt="成功" />', 'safe_mode 已关闭！'));
	$sp_gd = ($sp_gd > 0 ? array('<img src="images/ok.gif" alt="成功" />', 'GD 扩展已开启！') : array('<img src="images/error.gif" alt="失败" />', 'GD 扩展未开启！'));
	$sp_mysql = (function_exists('mysql_connect') ? array('<img src="images/ok.gif" alt="成功" />', 'MySQL 扩展已开启！') : array('<img src="images/error.gif" alt="失败" />', 'MySQL 扩展未开启！'));
	$isError = (function_exists('mysql_connect') ? $isError : TRUE);
	$sys_file = array(
		'data' => array('Cache', 'Config', 'Xml'),
		'lib' => array('Controller' => 'admin', 'Model' => 'admin', 'My', 'Plugin', 'View' ),
		'theme',
		'upload'
	);
	$auth = $install->checkAuthority(APP_ROOT, $sys_file);
	include MY_ROOT.'templates'.DIRECTORY_SEPARATOR.'step-2.html';
} elseif($step == 3) {
	$return = '';
	if(isset($_POST['sql_name']{0})) {
		$return = $install->initDb();
		if($return === TRUE) {
			header('Location:index.php?step=4');
			exit();
		}
	}
	include MY_ROOT.'templates'.DIRECTORY_SEPARATOR.'step-3.html';
} elseif($step == 4) {
	$return = '';
	if(isset($_POST['adminName']{0})) {
		$return = $install->initAdmin();
		if($return === TRUE) {
			header('Location:index.php?step=5');
			exit();
		}
	}
	include MY_ROOT.'templates'.DIRECTORY_SEPARATOR.'step-4.html';
} elseif($step == 5) {
	$admin = $install->getAdmin();
	$file = fopen(MY_DATADIR.'install.lock', 'ab');
	if(!$file) {
		fwrite($file, '');
		fflush($file);
		fclose($file);
	}
	include MY_ROOT.'templates'.DIRECTORY_SEPARATOR.'step-5.html';
}
?>