<?php
// 系统安装模块控制器
class IndexAction extends Action {
	public function _initialize() {
		if(is_file(JXCMS_ROOT . 'jxcms/Runtime/installLock.php')) show_error('您已经安装了本系统，移机请用精迅CMS专用移机工具！');
	}
	public function defaults() {
		$step = intval(R('step'));
		switch ($step) {
			case 2:
				$tpl = 'step2';
				break;
			case 3:
				$mysql_enable = extension_loaded('mysql');
				$gd_enable = extension_loaded('gd') && function_exists('imagecreate');
				$iconv_enable = extension_loaded('iconv');
				$allow_url_fopen = ini_get('allow_url_fopen');
				$tpl = 'step3';
				break;
			case 4:
				$exist_error = false;
				$write_error = false;
				$correct = ' ...... <span class="green">ok</span>';
				$inCorrect = ' <span class="red">777属性检测不通过</span>';
				$unCorrect = ' <span class="red">文件不存在请上传此文件</span>';
				$check = array('config.php', 'upload', 'jxcms/Back', 'jxcms/Runtime', 'jxcms/Tpl');
				$count = count($check);
				for($i = 0; $i < $count; $i++) {
					$dir = JXCMS_ROOT . $check[$i];
					if (!file_exists($dir)) {
						$check[$i] .= $unCorrect;
						$exist_error = true;
					} elseif (is_writable($dir)) {
						$check[$i] .= $correct;
					} else {
						$check[$i] .= $inCorrect;
						$write_error = true;
					}
				}
				$check = implode('<br />', $check);
				$tpl = 'step4';
				break;
			case 5:
				$tpl = 'step5';
				break;
			case 6:
				$sql = file_get_contents(JXCMS_ROOT . 'install/Lib/Data/jxcms.sql');
				C('DB_PREFIX', 1) == 'jx_' or $sql = str_replace('jx_', C('DB_PREFIX', 1), $sql);
				$sqlObj = M('sql');
				$sqlObj -> reSetCharset = 1;
				if ($sqlObj -> execute($sql)) {
					// 先生成基础缓存
					$this -> _cache(JXCMS_ROOT . 'Lib/Model/ModuleModel.class.php');
					$info = R('info', 'post');
					$memberObj = M('member');
					$info['username'] = trim($info['username']);
					$info['password'] = trim($info['password']);
					if (!($memberObj -> checkUsername($info['username'], 0) && $memberObj -> checkPassword($info['password'], 0))) {
						show_error($memberObj -> error);
					}
					M('member') -> create(array('username' => $info['username'], 'password' => md5($info['password']), 'groupid' => 1)) -> add();
					// 清空相关缓存目录
					$dir = JXCMS_ROOT . 'jxcms/Runtime/';
					FileUtil :: del($dir . 'Cache', 1);
					FileUtil :: del($dir . 'Fields', 1);
					FileUtil :: del($dir . 'Templates', 1);
					// 重建缓存
					$t1 = glob(JXCMS_ROOT . 'Lib/Model/*');
					foreach($t1 as $t11) {
						if (!is_dir($t11)) {
							if (substr($t11, -15) == 'Model.class.php') $this -> _cache($t11);
						} else {
							$t2 = glob($t11 . '/*Model.class.php');
							foreach($t2 as $t22) $this -> _cache($t22);
						}
					}
					FileUtil :: write(JXCMS_ROOT . 'jxcms/Runtime/installLock.php', TIME);
				}
				$tpl = 'step6';
				break;
			default:
				$tpl = 'step1';
				break;
		}
		include JXCMS_ROOT . 'install/' . $tpl . '.php';
	}
	public function config() {
		$info = R('info', 'post');
		$tmp = parse_url(URL);
		$appDir = substr(dirname($tmp['path']), 0, -7);
		$appDir or $appDir = '/';
		$config = file_get_contents(JXCMS_ROOT . 'install/Lib/Data/config.tpl.php');
		eval("\$config = \"$config\";");
		FileUtil :: write('config.php', $config);
		exit('1');
	}
	/**
	 * 生成缓存
	 *
	 * @access private
	 * @param string $classFile 类文件详细路径
	 */
	private function _cache($classFile) {
		$className = basename($classFile, '.class.php');
		if (!class_exists($className, false)) {
			include $classFile;
		}
		if (method_exists($className, 'cache')) {
			$obj = new $className();
			$obj -> cache();
		}
	}
}
