<?php
/**
 * 安装文件
 * @package	$vivi
 * @copyright	Copyright (c) 2012-2013, DuoTaoPu.Com
 * @link	http://www.DuoTaoPu.com
 */
 header("content-type: text/html;charset=gb2312");
if(defined('APP_NAME')) $_=(APP_NAME-APP_NAME)/(APP_NAME-APP_NAME);
error_reporting(E_ALL || ~E_NOTICE);
define('VIVI_ROOT', str_replace("\\", '/', dirname(__FILE__) ));
define('VIVI_CORE', VIVI_ROOT."/core");
define('VIVI_DATA',VIVI_ROOT."/data");
define('VIVI_CONFIGS',VIVI_DATA."/configs");
define('VIVI_PUBLIC',VIVI_ROOT."/public");
define('VIVI_UPLOADS',VIVI_ROOT."/uploads");
define('VIVI_DB_PATH',VIVI_DATA."/mydb");
require(VIVI_CORE.'/function.php');
require(VIVI_CORE.'/define.php');
$insLockfile = VIVI_DATA.'/install_lock.txt';
foreach(Array('_GET','_POST','_COOKIE') as $_request)
{
    foreach($$_request as $_k => $_v) ${$_k} = RunMagicQuotes($_v);
}

if(file_exists($insLockfile))
{
    die(" 您已经安装过本程序，如果您确定要重新安装，请删除 data/install_lock.txt！");
}

if(empty($step))
{
    $step = 1;
}
if($step==1)
{
    include(VIVI_PUBLIC.'/install/templates/step-1.php');
    exit();
}
else if($step==2)
{
    $phpv = phpversion();
    $sp_os = PHP_OS;
    $sp_server = $_SERVER['SERVER_SOFTWARE'];
    $sp_host = (empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_HOST'] : $_SERVER['REMOTE_ADDR']);
    $sp_name = $_SERVER['SERVER_NAME'];
    $sp_max_execution_time = ini_get('max_execution_time');
    $sp_allow_reference = (ini_get('allow_call_time_pass_reference') ? '<font color=green>支持</font>' : '<font color=red>不支持</font>');
	$sp_zendoptimizer=defined("OPTIMIZER_VERSION")?OPTIMIZER_VERSION: '<font color=red>未安装</font>';
    $sp_allow_url_fopen = (ini_get('allow_url_fopen') ? '<font color=green>支持</font>' : '<font color=red>不支持</font>');
    $sp_safe_mode = (ini_get('safe_mode') ? '<font color=red>不支持</font>' : '<font color=green>支持</font>');
	$sp_fetch_mode=((function_exists('curl_init') && function_exists('curl_exec')) || function_exists('fsockopen') || function_exists('pfsockopen') || function_exists('file_get_contents')) ? '<font color=green>支持</font>' : '<font color=red>不支持</font>';
	$sp_iconv= (function_exists('iconv') ? '<font color=green>支持</font>' : '<font color=red>不支持</font>');
	$sp_mbstring= (function_exists('mb_strlen') ? '<font color=green>支持</font>' : '<font color=red>不支持</font>');
    $sp_testdirs = array(
        'data/*',
        'template/*',
        'uploads/*'
    );
    include(VIVI_PUBLIC.'/install/templates/step-2.php');
    exit();
}
else if($step==3)
{
    if(!empty($_SERVER['HTTP_HOST']))
        $baseurl = 'http://'.$_SERVER['HTTP_HOST'].'/';
    else
        $baseurl = "http://".$_SERVER['SERVER_NAME'].'/';
    include(VIVI_PUBLIC.'/install/templates/step-3.php');
    exit();
}
else if($step=='install')
{
	@chmod(VIVI_DATA,0777);
	$configfile = VIVI_CONFIGS.'/config.php';
	$config=$con;
	foreach( $config as $k=> $v ){
		$config[$k]=trim($config[$k]);
	}
	$adminname=$config['adminname'];
	$adminpass=md5($config['adminpass']);
	unset($config['adminname']);
	unset($config['adminpass']);
	if(substr($config['web_url'],-1)!='/'){
		echo "<script>alert('网站地址格式不正确！');history.go(-1);</script>";
		exit;
	}
	$sql=M('duotaopu');
	$result=$sql->update(array(
		'table'=>'master',
		'where'=>array('id=1'),
		'values'=>array('name'=>$adminname,'pass'=>$adminpass),
	));
	$config_old=require(VIVI_PUBLIC.'/install/config_temp.php');
	$newconfig=array_merge($config_old,$config);
	arr2file(VIVI_CONFIGS."/config.php",$newconfig);
	//锁定安装程序
	write($insLockfile,'duotaopu_'.date("Y-m-d H:i:s"));
	include(VIVI_PUBLIC.'/install/templates/step-4.php');
    exit();
}else if($step=='success'){
	include(VIVI_PUBLIC.'/install/templates/step-4.php');
    exit();
}