<?php
/**
 * 安装
 * @copyright reginx.com
 * $Id$
 */
class install_mod extends mod{

	/**
     * 默认页
     */
	public function indexAct(){
        $this->settpldir('default');
		if(!file_exists(DATA_PATH . 'install.lock')){
			$this->settpldir('default');
			$this->assign('ptitle', '蘑菇视频管理系统  - 在线安装 --  Powered By ReGinX');
			$this->assign('items', config_lib::$ins);
			$this->display('install/install.tpl.html');
		}
		$this->showmsg('程序已经安装过了!' , BASE_URL , 1 , 2);
	}

	/**
     * 写入mysql配置信息
     */
	public function procAct(){
		$this->nocache();
		set_time_limit(0);
        $this->settpldir('default');
		if(!file_exists(DATA_PATH . 'install.lock')){
			$mysql = $this->get('mysql', 'P');
			$pass = true;
			$link = mysql_connect($mysql[0]['host'], $mysql[0]['user'], $mysql[0]['passwd']);
			if(!$link){
				$pass = false;
			}
			if(!mysql_select_db($mysql[0]['name'], $link)){
				if(!mysql_query('CREATE DATABASE ' . $mysql[0]['name'] . ' default charset utf8 COLLATE utf8_general_ci'  , $link)){
					$pass = false;
				}else{
					mysql_select_db($mysql[0]['name'], $link);
				}
			}
			if($pass){
				$conf = config_lib::load();
				$conf['db']['class'] = 'mysql';
				$conf['db']['mysql'] = $mysql;
				config_lib::store($conf);
				clearstatcache();
				file_put_contents(DATA_PATH . 'install.lock' , 'reginx.com');
				$this->setob(false);
				$this->settpldir('default');
				ob_implicit_flush(1);
				$struct = file_get_contents(DATA_PATH . 'struct.sql');
				// 处理表前缀
				if($mysql[0]['pre'] != 'pre_'){
					$struct = str_replace('pre_', $mysql[0]['pre'], $struct);
				}
				$data = file_get_contents(DATA_PATH . 'data.sql');
				// 处理表前缀
				if($mysql[0]['pre'] != 'pre_'){
					$data = str_replace('pre_', $mysql[0]['pre'], $data);
				}
				$this->assign('struct', explode(';', $struct));
				$this->assign('data',  explode(';', $data));
                if($this->has('data' , 'P')){
                    $vod   =  explode("\n" , str_replace('pre_', $mysql[0]['pre'], file_get_contents(DATA_PATH . 'vod.sql')));
                    $this->assign('vod' , $vod);
                }
				$this->assign('link' , $link);
				$this->display('install/proc.tpl.html');
			}else{
				$this->showmsg('数据库配置信息错误!' , -1 , 1 , 2);
			}
		}
		$this->showmsg('程序已经安装过了!' , BASE_URL , 1 , 2);
	}


	/**
     * 环境检测
     */
	public function verifyAct(){
		clearstatcache();
		$this->nocache();
        $this->settpldir('default');
		if(!file_exists(DATA_PATH . 'install.lock')){
			$key = $this->get('k');
			$env = array_keys(config_lib::$ins[0]['list']);
			$dir = array_keys(config_lib::$ins[1]['list']);
			if(in_array($key, $env)){
				if(function_exists($key)){
					$this->ajaxout('<span class="suc">支持</span>', '');
				}else{
					$this->ajaxout("<span class='fail'>不支持</span>", '');
				}
			}
			if(in_array($key, $dir)){
				$cdir = '';
				if($key == 'cp'){
					$cdir = BASE_PATH . 'config/';
				}
				if($key == 'dp'){
					$cdir = DATA_PATH;
				}
				if($key == 'ip'){
					$cdir = INC_PATH;
				}
				if(is_writable($cdir)){
					$this->ajaxout('<span class="suc">可写</span>', '');
				}else{
					$this->ajaxout("<span class='fail'>" . $cdir . " 不可写</span>", '');
				}
			}
		}else{
			die('Error');
		}
	}
}
?>