<?php

/**
 *--------------------------------------
 * 安装控制
 *--------------------------------------
 * @project		: install
 * @author		: cblee
 * @created		: 2013-12-27
 * @copyright	: (c)2013 AsThis
 *--------------------------------------
 */

class IndexCtrlr extends Ctrlr
{
	public function index()
	{
		check_lock();

		if(!isset($_GET[C('VAR.LANG')]))
		{
			$this->step0();
		}
		else
		{
			$step = isset($_POST['step']) ? $_POST['step'] : 1;
			switch ($step)
			{
				case '1':
					$this->step1();
					break;
				case '2':
					$this->step2();
					break;
				case '3':
					$this->step3();
					break;
				case '4':
					$this->step4();
					break;
				case '5':
					$errorMessage = check_post(array($_POST['founderName'],$_POST['founderPassword']));
					$errorMessage .= check_db($_POST['dbHost'], $_POST['dbPort'], $_POST['dbUser'], $_POST['dbPassword'], $_POST['dbDatabase'], $_POST['dbPrefix'], $_POST['dbConnection']);
					if('' != $errorMessage)
					{
						$this->step4($errorMessage);
					}
					else
					{
						$this->step5();
					}
					break;
				case '6':
					$this->step6();
					break;
				default:
					$this->step1();
					break;
			}
		}
	}

	/* choose language */
	public function step0()
	{
		$langSet = require_cache(LANG_PATH.D_S.'lang_set.php');
		$selectHight = count($langSet) > 5 ? count($langSet) : 5;
		$hal = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 5));
		$this->assign('selectHight', $selectHight);
		$this->assign('hal', $hal);
		$this->assign('langSet', $langSet);
		$this->display('index');
	}

	/* show introduce */
	public function step1()
	{
		$this->display('step1');
	}

	/* show license */
	public function step2()
	{
		$this->display('step2');
	}

	/* check environment */
	public function step3()
	{
		$checkNextStep = true;

		/* check system */
		$systemItems = C('CHECK_ITEMS.SYSTEM');
		if(!empty($systemItems))
		{
			check_system($systemItems, $checkNextStep);
			foreach($systemItems as $k => $v)
			{
				$systemItems[L($k)]['r'] = L($v['r']);
				$systemItems[L($k)]['b'] = L($v['b']);
				$systemItems[L($k)]['c'] = L($v['c']);
				if(1 == $v['s'])
				{
					$systemItems[L($k)]['c_c'] = 'class="y"';
				}
				else
				{
					$systemItems[L($k)]['c_c'] = 'class="n"';
				}
				unset($systemItems[$k]);
			}
		}

		/* check dir and file */
		$dirFileItems = C('CHECK_ITEMS.DIR_FILE');
		if(!empty($dirFileItems))
		{
			check_dirfile($dirFileItems, $checkNextStep);
			foreach($dirFileItems as $k => $v)
			{
				$dirFileItems[L($k)]['type'] = L($v['type']);
				$dirFileItems[L($k)]['c'] = L($v['c']);
				$dirFileItems[L($k)]['path'] = $v['path'];
				if(1 == $v['s'])
				{
					$dirFileItems[L($k)]['c_c'] = 'class="y"';
				}
				else
				{
					$dirFileItems[L($k)]['c_c'] = 'class="n"';
				}
				unset($dirFileItems[$k]);
			}
		}

		/* check extension */
		$extensionItems = C('CHECK_ITEMS.EXTENSION');
		if(!empty($extensionItems))
		{
			check_extension($extensionItems, $checkNextStep);
			foreach($extensionItems as $k => $v)
			{
				if(1 == $v['s'])
				{
					$extensionItems[$k]['c_c'] = 'class="y"';
					$extensionItems[$k]['s'] = L('SUPPORT');
				}
				else
				{
					$extensionItems[$k]['c_c'] = 'class="n"';
					$extensionItems[$k]['s'] = L('NONSUPPORT');
				}
			}
		}

		/* check function */
		$functionItems = C('CHECK_ITEMS.FUNCTION');
		if(!empty($functionItems))
		{
			check_function($functionItems, $checkNextStep);
			foreach($functionItems as $k => $v)
			{
				if(1 == $v['s'])
				{
					$functionItems[$k]['c_c'] = 'class="y"';
					$functionItems[$k]['s'] = L('SUPPORT');
				}
				else
				{
					$functionItems[$k]['c_c'] = 'class="n"';
					$functionItems[$k]['s'] = L('NONSUPPORT');
				}
			}
		}

		$this->assign('checkNextStep', $checkNextStep);
		$this->assign('systemItems', $systemItems);
		$this->assign('dirfileItems', $dirFileItems);
		$this->assign('extensionItems', $extensionItems);
		$this->assign('functionItems', $functionItems);
		$this->display('step3');
	}

	/* setup installation */
	public function step4($errorMessage = '')
	{
		$formV['dbHost'] = isset($_POST['dbHost']) ? $_POST['dbHost'] : 'localhost';
		$formV['dbPort'] = isset($_POST['dbPort']) ? $_POST['dbPort'] : '3306';
		$formV['dbUser'] = isset($_POST['dbUser']) ? $_POST['dbUser'] : 'root';
		$formV['dbPassword'] = isset($_POST['dbPassword']) ? $_POST['dbPassword'] : '';
		$formV['dbDatabase'] = isset($_POST['dbDatabase']) ? $_POST['dbDatabase'] : strtolower(SOFT_NAME);
		$formV['dbPrefix'] = isset($_POST['dbPrefix']) ? $_POST['dbPrefix'] : strtolower(SOFT_NAME).str_replace('.', '', strtolower(SOFT_CODENAME)).'_';
		$formV['dbConnection'] = isset($_POST['dbConnection']) ? $_POST['dbConnection'] : 'mysql';
		$formV['founderName'] = isset($_POST['founderName']) ? $_POST['founderName'] : 'admin';
		$formV['founderEmail'] = isset($_POST['founderEmail']) ? $_POST['founderEmail'] : 'admin@admin.com';
		$formV['founderPassword'] = isset($_POST['founderPassword']) ? $_POST['founderPassword'] : '';
		$formV['cookieKey'] = isset($_POST['cookieKey']) ? $_POST['cookieKey'] : random(8);
		$this->assign('formV', $formV);
		$this->assign('errorMessage', $errorMessage);
		$this->display('step4');
	}

	/* write data */
	function step5()
	{
		/* 替换管理员信息 */
		$administrator = ARequest::get('founderName');
		$password = md5(ARequest::get('founderName').md5(ARequest::get('founderPassword')));
		$email = ARequest::get('founderEmail');
		$sql = file_get_contents(SQL_FILE);
		$sql = str_replace(
			array(
				'{-time-}',
				'{-ip-}',
				'{-ausr-}',
				'{-apwd-}',
				'{-email-}',
				'{-site_url-}',
				'{-cookie_key-}'
			),
			array(
				time(),
				AServer::get_ip(),
				$administrator,
				$password,
				$email,
				SITE_URL,
				$_POST['cookieKey']
			),
			$sql);
		@file_put_contents(DATA_PATH.D_S.'~data.sql', $sql);

		$this->display('step5');

		connect_db($_POST['dbHost'].':'.$_POST['dbPort'], $_POST['dbUser'], $_POST['dbPassword'], $_POST['dbDatabase']);
		run_sql(DATA_PATH.D_S.'~data.sql');

		$siteSeed = random(8);
		$define = array(
			'SOFT_NAME' => SOFT_NAME,
			'SOFT_CODENAME' => SOFT_CODENAME,
			'SOFT_VERSION' => SOFT_VERSION,
			'SOFT_CHARSET' => SOFT_CHARSET,
			'SOFT_AUTHOR' => SOFT_AUTHOR,
			'SOFT_AUTHOR_URL' => SOFT_AUTHOR_URL,
			'SOFT_UPGRADE_URL' => SOFT_UPGRADE_URL,
			'SOFT_AUTHORIZATION_URL' => SOFT_AUTHORIZATION_URL,
			'SOFT_SEED' => $siteSeed
		);
		save_define_file(DEFINE_FILE, $define);

		$_C = include(CONFIG_FILE);
		$_C['DB']['TYPE'] = $_POST['dbConnection']; /* 数据库连接方式 */
		$_C['DB']['USER'] = $_POST['dbUser']; /* 数据库用户名 */
		$_C['DB']['PWD'] = $_POST['dbPassword']; /* 数据库密码 */
		$_C['DB']['HOST'] = $_POST['dbHost']; /* 服务器地址 */
		$_C['DB']['PORT'] = $_POST['dbPort']; /* 数据库端口 */
		$_C['DB']['NAME'] = $_POST['dbDatabase']; /* 数据库名 */
		$_C['DB']['CHARSET'] = SOFT_DB_CHARSET; /* 数据库默认编码 */
		$_C['DB']['PREFIX'] = $_POST['dbPrefix']; /* 数据库表前缀 */
		$_C['COOKIE']['KEY'] = $_POST['cookieKey']; /* cookie加密码 */
		save_config_file(CONFIG_FILE, $_C);
		/* 删除临时文件 */
		@unlink(DATA_PATH.D_S.'~data.sql');
		$runtimeFile = dirname(APP_PATH).D_S.'runtime'.D_S."~runtime.php";
		@unlink($runtimeFile);
	}

	/* lock installation */
	function step6()
	{
		lock();
		$this->display('step6');
	}

}
?>