<?php
error_reporting(0);

@set_time_limit(600);

$INSLANG = array();

header('Content-type: text/html; charset=utf-8');

include_once './install/language.php';

if(is_file('./data/install.lock')){
	die($INSLANG['delete_install_lock_to_install']);
}

if(isset($_GET['phpinfo'])){
	phpinfo();exit;
}

$COOKIE = @include './data/install.php';

function _cookie(){
	global $COOKIE;
	$fp = fopen('./data/install.php', 'w');
	fputs($fp, "<?php\r\nreturn ". var_export($COOKIE, true) .';');
	fclose($fp);
}

require_once './install/install.func.php';
$log = '';
if(!isset($_POST['step'])){
	$step='agreement';
}else{

$step = $_POST['step'];

switch($step){

case 'start':
	//<<<<<<<<<<<<<<<<<<<<<<start >>>>>>>>>>>>>>>>>>>>
	//环境探测
@mkdir('./data');
@mkdir('./data/log');
@copy('./inc/index.html', './data/index.html');
@copy('./inc/index.html', './index.html');
@copy('./inc/csft.conf.php', './data/csft.conf.php');
is_file('./#.php') or @copy('./##.php', './#.php');
	
	/* define('PHP168_PATH', str_replace(array('\\\\', '\\'), array('/', '/'), dirname(__FILE__)) .'/');
	define('PHP_SELF', $_SERVER['SCRIPT_NAME']);
	
	$document_root = isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT']) ? $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] : (isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '');
	$document_root = $document_root ?
		str_replace(array('\\\\', '\\'), '/', $_SERVER['DOCUMENT_ROOT']) :
		str_replace(PHP_SELF, '', str_replace(array('\\\\', '\\'), '/', $_SERVER['SCRIPT_FILENAME']));
	$tmp = preg_replace('?'. $document_root .'?i', '', PHP168_PATH);
	define('P8_ROOT', substr($tmp, 0, 1) == '/' ? $tmp : '/'.$tmp); //相对于http服务器的web绝对路径
	$path = substr(P8_ROOT, 0, -1); */
	

	$scriptName=basename($_SERVER['SCRIPT_FILENAME']);
	if(basename($_SERVER['SCRIPT_NAME'])===$scriptName)
		$_scriptUrl=$_SERVER['SCRIPT_NAME'];
	else if(basename($_SERVER['PHP_SELF'])===$scriptName)
		$_scriptUrl=$_SERVER['PHP_SELF'];
	else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME'])===$scriptName)
		$_scriptUrl=$_SERVER['ORIG_SCRIPT_NAME'];
	else if(($pos=strpos($_SERVER['PHP_SELF'],'/'.$scriptName))!==false)
		$_scriptUrl=substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$scriptName;
	else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT'])===0)
		$_scriptUrl=str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
	else
		exit('error');
	
	$base_root = rtrim(dirname($_scriptUrl),'\\/').'/';

	$siteurl = 'http://'.$_SERVER['HTTP_HOST'].$base_root;

	$fp = fopen('./data/pathinfo.php', 'w'); fputs($fp, "<?php\r\necho \$_SERVER['REQUEST_URI'];"); fclose($fp);
	$path_info = preg_match('#pathinfo\.php/pathinfo#', file_get_contents($siteurl.'data/pathinfo.php/pathinfo'));

	//必须支持index.php/1/2/3这种访问方式
	$php_version = version_compare(phpversion(), '5.2.2') != -1;	//php 5.2.4以上
	$iconv = function_exists('iconv') ? 'iconv' : '';
	$mb = function_exists('mb_convert_encoding') ? 'mb_convert_encoding' : '';
	$conv = $iconv || $mb;
	//必须有其中一个编码转换函数
	$mysql = function_exists('mysql_connect');	//必须支持MYSQL
	$memcache = class_exists('Memcache');		//memcache可选
	$gd = function_exists('imagecreate');		//GD必须支持
	$url_fopen = ini_get('allow_url_fopen');	//远程文件
	$ssl = extension_loaded('openssl');			//ssl
	$curl = function_exists('curl_init');		//curl
	$zo = 1;//function_exists('extension_loaded') && extension_loaded('Zend Optimizer');
	$fp = fopen('./data/ssi.html', 'w'); fputs($fp, 'ssi'); fclose($fp);
	$fp = fopen('./data/ssi.shtml', 'w'); fputs($fp, '<!--#include virtual="ssi.html"-->'); fclose($fp);
	$ssi = file_get_contents($siteurl.'data/ssi.shtml') == 'ssi';
	//$ssi = $ssi_test['body'] == 'ssi' ? true : false;
	$ssi = true;
	$upload_max_filesize = ini_get('upload_max_filesize');
	
	@unlink('./index.html');
	
	$fp = @fopen('./index.html', 'w');
	@fputs($fp, '请进入网站后台静态首页');
	@fclose($fp);
	@chmod('./#.php', 0666);
	@chmod('./index.html', 0666); 
	
	$root_writable = is_writable('./#.php') && is_writable('./index.html');			//根目录#.php,index.html必须可写
	$attachment_writable = is_writable('./attachment/');			//上传目录必须可写
	$data_writable = is_writable('./data/');	//data目录必须可写
	$js_writable = is_writable('./js/');		//js目录必须可写

	$installable = $php_version && $path_info && $mysql && $gd && $zo && $url_fopen && $root_writable && $data_writable && $attachment_writable && $js_writable;
	$installable = true;
	$COOKIE = array();
	
	$COOKIE['p8_installable'] = $installable;
	
	_cookie();

	
break;

case 'step1':
		//<<<<<<<<<<<<<<< step 1 >>>>>>>>>>>>>>>>>>>>>>>>>>
break;



case 'step2':
	//<<<<<<<<<<<<<<< step 2 >>>>>>>>>>>>>>>>>>>>>>>>>>

	!empty($COOKIE['p8_installable']) or die($INSLANG['environment_not_matched']);

	//初始化数据库配置

	$table_prefix = $_POST['table_prefix'];

	$core_table_prefix = $_POST['core_table_prefix'];

	$page_charset = $_POST['page_charset'];
	
	$COOKIE['db_table_prefix'] = $table_prefix;
	$COOKIE['core_table_prefix'] = $core_table_prefix;
	$COOKIE['page_charset'] = $page_charset;
	
	_cookie();

	$db_host = $_POST['db_host'];
	$db_port = $_POST['db_port'];
	$db_user = $_POST['db_user'];
	$db_password = $_POST['db_password'];
	$db_name = $_POST['db_name'];
	$db_charset = $_POST['db_charset'];



	$conv_func = function_exists('iconv') ? 'iconv' : (function_exists('mb_convert_encoding') ? 'mb_convert_encoding' : '');

	require_once 'inc/mysql.class.php';

	$db = new P8_mysql(
		$db_host,
		$db_user,
		$db_password,
		$db_name,
		$db_charset,
		$db_port,
		0
	);

	if($db->connect() == -1){
		die('不能连接数据库');
	}

	$default_charset = "";
	if($db->version() > '4.1'){
		//检查支不支持当前字符集
		$db->query("SET NAMES $db_charset");

		$default_charset = "DEFAULT CHARSET=$db_charset";

		if($db->error()){
			die($db->error());
		}
	}


	$db->query("CREATE DATABASE IF NOT EXISTS `$db_name` $default_charset");

	if($db->error()){
		die($db->error());
	}

	if(!$db->select_db($db_name)){
		die($INSLANG['can_not_use_or_create_database']);
	}
	
	@unlink('./data/core/core.php');


	//检查是否安装过
	$check = $db->fetch_all("SHOW TABLES LIKE '{$table_prefix}config'");
	if(!empty($check) && !$_POST['cover']){

		//###############################################
		//是选择是否要覆盖安装
		//如果覆盖跳回step 2,不覆盖跳到step 6
		//include 'install/cover.html';
		$step="cover";
		break;
		//###############################################

	}










	//创建目录
	@mkdir('./data/');
	@mkdir('./data/core/');
	@mkdir('./data/core/plugin');
	@mkdir('./data/tmp/');
	
	
	//取得相对路径
	define('PHP168_PATH', str_replace(array('\\\\', '\\'), array('/', '/'), dirname(__FILE__)) .'/');
	define('PHP_SELF', $_SERVER['SCRIPT_NAME']);

	/* $document_root = isset($_SERVER['DOCUMENT_ROOT']) ?
		str_replace(array('\\\\', '\\'), '/', $_SERVER['DOCUMENT_ROOT']) :
		str_replace(PHP_SELF, '', str_replace(array('\\\\', '\\'), '/', $_SERVER['SCRIPT_FILENAME']));
	$tmp = preg_replace('?'. $document_root .'?i', '', PHP168_PATH);
	define('P8_ROOT', substr($tmp, 0, 1) == '/' ? $tmp : '/'.$tmp); //相对于http服务器的web绝对路径
	$path = substr(P8_ROOT, 0, -1); */
	$scriptName=basename($_SERVER['SCRIPT_FILENAME']);
	if(basename($_SERVER['SCRIPT_NAME'])===$scriptName)
		$_scriptUrl=$_SERVER['SCRIPT_NAME'];
	else if(basename($_SERVER['PHP_SELF'])===$scriptName)
		$_scriptUrl=$_SERVER['PHP_SELF'];
	else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME'])===$scriptName)
		$_scriptUrl=$_SERVER['ORIG_SCRIPT_NAME'];
	else if(($pos=strpos($_SERVER['PHP_SELF'],'/'.$scriptName))!==false)
		$_scriptUrl=substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$scriptName;
	else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT'])===0)
		$_scriptUrl=str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
	else
		exit('error');
	$path = rtrim(dirname($_scriptUrl),'\\/');
	
	//取得相对路径
	
	$mysql_connect_types = "'mysql_connect_types' => array(
		'mysql' => 'P8_mysql',
		". (function_exists('mysqli_connect') ? "'mysqli' => 'P8_mysqli'" : '') ."
	),";
	
	$db_master = "
	/*DB_master*/
	'DB_master' => array(
		'host' => '$db_host',
		'port' => '$db_port',
		'user' => '$db_user',
		'password' => '$db_password',
		'db' => '$db_name',
		'pconnect' => 0
	),
	/*DB_master*/";
	
	$config = "
	'mysql_connect_type' => 'mysql',
	$mysql_connect_types
	'mysql_charset' => '$db_charset',
	'core_table_prefix' => '$core_table_prefix',
	'encode_conv_func' => '$conv_func',
	'page_charset' => '$page_charset',
";
	
	$info = file_get_contents('./#.php');
	$info = preg_replace(
		'#/\*for_config\*/[\s\S]*?/\*for_config\*/#', 
		"/*for_config*/\r\n". $config ."\t/*for_config*/",
		$info
	);
	$fp = fopen('./#.php', 'w');
	fputs($fp, $info);
	fclose($fp);
	
	$config = "
	$db_master
	$config
	'table_prefix' => '$table_prefix',
	";
	
	$fp = fopen('./data/config.php', 'w');
	fputs($fp, "<?php\r\nreturn array(\r\n$config\r\n);");
	fclose($fp);
	
	
	
	
	

	if(empty($core_table_prefix)){
		//处理SQL
		$sql = file_get_contents('./install/core_table.php');
		$sql = get_install_sql($db, $sql, $table_prefix, true);
		
		function _encode($from, $to, $string){
			$from = strtoupper($from);
			$to = strtoupper($to);
			if($from == $to) return $string;
			
			if(function_exists('mb_convert_encoding')){
				if(!is_array($string)) return mb_convert_encoding($string, $to, $from);
				
				foreach($string as $k => $v)
				$string[$k] = _encode($from, $to, $v);
				return $string;
			}else if(function_exists('iconv')){
				if(!is_array($string)) return iconv($from, $to .'//IGNORE', $string);
				
				foreach($string as $k => $v)
				$string[$k] = _encode($from, $to, $v);
				return $string;
			}
		}

		//处理SQL
		$error = array();
		foreach($sql as $v){
			if($db->error()) $error[] = $db->errno() .':'. $db->error() ."\t". $v;
			$db->query($db_charset != 'gbk' ? _encode('gbk', $page_charset, $v) : $v);
		}

		if($error)
		die(print_r($error));
	}


	//<<<<<<<<<<<<<<<< step 2 >>>>>>>>>>>>>>>>>>>>>>
break;






















case 'step3':
	//<<<<<<<<<<<<<<<< step 3 >>>>>>>>>>>>>>>>>>>>>>
	//创建配置,菜单
	!empty($COOKIE['p8_installable']) or die($INSLANG['environment_not_matched']);

	require_once './inc/init.php';
	require_once './admin/inc/menu.class.php';
	require_once './admin/inc/navigation_menu.class.php';
	require_once './modules/member/inc/menu.class.php';
	error_reporting(0);
	
	if($DB_master->connect() === -1){
		die($INSLANG['can_not_connect_to_database']);
	}
	
	$domain = ip2long($_SERVER['SERVER_NAME']) || strpos($_SERVER['SERVER_NAME'], '.') === false ? '' : $_SERVER['SERVER_NAME'];
	
	$base_domain = substr_count($domain, '.') > 1 ? substr($domain, strpos($domain, '.') +1) : $domain;
	//### 初始化一些默认的配置 ###
	$core->set_config(array(
		'debug' => 0,
		//ssi
		'ssi' => file_get_contents($_POST['url'] .'/data/ssi.shtml') == 'ssi' ? true : false,
		//访问URL
		'url' => $_POST['url'],
		'murl' => $_POST['url'].'/m',
		//网站名称
		'site_name' => 'PHP168',
		//表前缀
		'table_prefix' => $COOKIE['db_table_prefix'],
		//页面编码
		'page_charset' => $COOKIE['page_charset'],
		//编码函数
		'encode_conv_func' => function_exists('iconv') ? 'iconv' : (function_exists('mb_convert_encoding') ? 'mb_convert_encoding' : ''),
		'cookie' => array(
			'prefix' => rand_str(4) .'_',
			'path' => '/',
			'domain' => $base_domain
		),
		'base_domain' => $base_domain,
		//当前模板
		'template' => S_version()? S_version() : 'media',
		'mobile_template' => 'mobile/default',
		//会员中心模板
		'member_template' => 'member/default',
		//模板目录
		'template_dir' => 'template/',
		//后台入口
		'admin_controller' => 'admin.php',
		//通信密码
		'p8_key' => unique_id(),
		//核心表名前缀
		'core_table_prefix' => $COOKIE['core_table_prefix'],
		//仅使用核心的角色
		'use_core_roles_only' => 1,
		//全站使用核心导航
		'CoreMenu'=> 1,
		//附件目录
		'attachment' => array(
			//附件地址
			'path' => 'attachment',
			//出入库处理
			'confuse' => 1,
			//远程附件地址
			'remote' => array(
				'server' => array(
					1 => $_POST['url'] .'/attachment'
				),
				'current' => 0
			)
		),
		//暂定CMS的首页为核心首页
		'index_system' => 'cms',
		//最后一次标签缓存更新时间
		'last_label_cache' => P8_TIME,
		//最后一次更新权限时间
		'last_acl_cache' => P8_TIME,
		//最后一次更新积分缓存时间
		'last_credit_cache' => P8_TIME,
		//管理员登录需要验证码
		//'admin_login_with_captcha' => 1,
		//安全码
		//'admin_login_code' => 'php168',
		'lang' => 'zh-cn',
		//需要跨域同步SESSION的域名
		'session_cross_domains' => array(
			/*
			'domain1.com' => 'http://www.domain1.com/sync_session.php'
			*/
		),
		//默认个人主页模板
		'homepage_template' => 'homepage/default',
		//sphinx索引前缀
		'sphinx_prefix' => '',
		'memcache' => array(
			'enabled' => 0,
			'host' => 'localhost',
			'port' => '11211',
			'pconnect' => 1,
			'prefix' => rand_str(4) .'_',
			'server' => array(
				'localhost:11211' => array(
					'host' => 'localhost',
					'port' => 11211
				)
			)
		),
		//CC攻击
		'cc_num' => 20,
		'cc_time' => 30,
		'site_open' => 1,
		//版本
		'version' => S_version(),
		//build
		'build' => S_build(),
		'serial_number' => $_POST['serial_number']
	));
	
	rm(CACHE_PATH .'core/acl/');
	md(CACHE_PATH .'core/acl/', true);
	
	//系统,模块缓存
	require_once './inc/cache.func.php';
	cache_system_module();
	
	//后台菜单
	$menus = @include './install/data/'.(S_version()? S_version() : 'media').'/admin_menus.php';
	$menus = to_installed_charset($menus);
	insert_admin_menu($menus);

	//会员菜单
	$menus = @include './install/member_menus.php';
	$menus = to_installed_charset($menus);
	insert_member_menu($menus);

	//导航菜单
	$S_version = S_version()? S_version() : 'media';
	$menus = @include './install/data/'.(S_version()? S_version() : 'media').'/navigation.php';
	$menus = to_installed_charset($menus);
	insert_navigation_menu($menus);
	
	header('Content-type: text/html; charset=utf-8');
	
	$systems = convert_encode($core->CONFIG['page_charset'], 'gbk', $core->list_systems(true));
	$modules = convert_encode($core->CONFIG['page_charset'], 'gbk', $core->list_modules(true));
	$plugins = convert_encode($core->CONFIG['page_charset'], 'gbk', $core->list_plugins(true));
	include './install/install_order.php';//调入必须安装的选项
	//cache_all();
	
	//<<<<<<<<<<<<<<<< step 3 >>>>>>>>>>>>>>>>>>>>>>
	break;














case 'step4':

//<<<<<<<<<<<<<<<<<<<< step 4 >>>>>>>>>>
	require_once './inc/init.php';
	error_reporting(0);
	header('Content-type: text/html; charset=utf-8');
	$batch_install = true;
	
	//进行角色配置
	$role = &$core->load_module('role');
	$controller = &$core->controller($role);

	//添加角色组
	$S_version = S_version()? S_version() : 'media';
	if($role_groups = to_installed_charset(include "./install/data/$S_version/role_groups.php")){
		$_config = array();

		foreach($role_groups as $name => $v){
			$_config[$name] = $controller->add_group($v);
		}

		$core->set_config($_config);
	}

	//添加角色
	if($roles = to_installed_charset(include "./install/data/$S_version/roles.php")){
		$_config = array();

		foreach($roles as $name => $v){
			$_config[$name] = $controller->add($v);
		}

		$core->set_config($_config);
	}
	
	//权限
	if($acls = @include './install/acls.php'){
		foreach($acls as $rid => $acl){
			$controller->set_acl($core, $rid, $acl);
		}
	}
	
	
	$systems = convert_encode($core->CONFIG['page_charset'], 'utf-8', $core->list_systems(true));
	$modules = convert_encode($core->CONFIG['page_charset'], 'utf-8', $core->list_modules(true));
	$plugins = convert_encode($core->CONFIG['page_charset'], 'utf-8', $core->list_plugins(true));
	include './install/install_order.php';//调入必须安装的选项
break;















	
case 'step5':

	//<<<<<<<<<<<<<<<<<<<< step 5 >>>>>>>>>>
	!empty($COOKIE['p8_installable']) or die($INSLANG['environment_not_matched']);
	require_once './inc/init.php';
	error_reporting(0);
	header('Content-type: text/html; charset=utf-8');
	
	$systems = isset($_POST['system']) ? implode(',',$_POST['system']) :'';
	$modules = isset($_POST['system']) ? implode(',',$_POST['module']) :'';
	$plugins = isset($_POST['plugin']) ? implode(',',$_POST['plugin']) : '';
	$log = $systems.','.$modules.','.$plugins;
	$COOKIE['chose_install']=$log;
	$fp = fopen('./data/chose_install.php', 'w');
	$data = var_export(explode(",",$log),true);
	fputs($fp, "<?php\r\nreturn $data;");
	fclose($fp);
	_cookie();
break;


case 'step52':
	require_once './inc/init.php';
	error_reporting(0);
	header('Content-type: text/html; charset=utf-8');
	$batch_install = true;

	
	$modules = !empty($_POST['modules']) ? explode(',',$_POST['modules']) : array();
	$plugins = !empty($_POST['plugins']) ? explode(',',$_POST['plugins']) : array();
	$systems = !empty($_POST['systems']) ? explode(',', $_POST['systems']) : array();
	
	$all = array_merge($modules, $plugins, $systems);
	
	$msg = '';
	if($modules){
	//安装系统模块
		$this_system = &$core;
		$msg = $module = array_shift($modules);
		$this_module = &$core->load_module($module);
		include PHP168_PATH .'install/install_module.php';
	}else if($plugins){
	//安装选择的插件
		$msg = $plugin = array_shift($plugins);
		$this_plugin = &$core->load_plugin($plugin);
		require PHP168_PATH .'install/install_plugin.php';
	}else if($systems || $_POST['system']){
	//安装选择的系统
		if(!$_POST['module']){
			$msg = $_sys_ = array_shift($systems);
			$this_system = &$core->load_system($_sys_);
			include PHP168_PATH .'install/install_system.php';
			//调用安装顺序
			$install_order = include $this_system->path .'modules/install_order.php';
	
		}else if($_POST['module']){
		//安装系统中的模块
			$_sys_ = $_POST['system'];
			$install_order = explode(',',$_POST['module']);
			
			$this_system = &$core->load_system($_sys_);
			$core->systems[$this_system->name]['installed'] = true;
			$module = array_shift($install_order);
			$this_module = &$this_system->load_module($module);
			include PHP168_PATH .'install/install_module.php';
			$this_module->set_config(array(
				'template' => S_version()? S_version() : 'media'
			));
		}
		
		if($_sys_ && $install_order){
			$msg = $_sys_.($module? ' => '.$module : '');
			$date = '['.date('h:i:s').'] ';
			echo "<script type='text/javascript'>";
			if(!$_POST['module'])echo "parent.$('#systems').val('".($systems? implode(',',$systems) : ''). "');";
			echo "parent.$('#system').val('".$_sys_. "');";
			echo "parent.$('#module').val('".($install_order? implode(',',$install_order) : ''). "');";
			if($install_order)echo "parent.postcheck('".$date.$msg."','".$date.$_sys_.'=>'.$install_order[0]."');";
			echo "parent.formsubmit('form3');";
			echo "</script>";
			exit;
		}else{
			$date = '['.date('h:i:s').'] ';
			echo "<script type='text/javascript'>";
			echo "parent.formsubmit('form2');";
			echo "</script>";
			exit;
		}
		
		//释放内存
		$core->unload($_sys_);
	}

if($msg){
	$date = '['.date('h:i:s').'] ';
	echo "<script type='text/javascript'>";
	echo "parent.$('#systems').val('".($systems? implode(',',$systems) : ''). "');";
	echo "parent.$('#plugins').val('".($plugins? implode(',',$plugins) : ''). "');";
	echo "parent.$('#modules').val('".($modules? implode(',',$modules) : ''). "');";
	echo "parent.postcheck('".$date.$msg."','".$date.$all[1]."');";
	echo "parent.formsubmit('form2');";
	echo "</script>";
	exit;

}	
	
	//系统,模块缓存
	require_once './inc/cache.func.php';
	//cache_all();
	
	echo "<script>";
	echo "parent.document.getElementById('logbox').style.display='none';";
	echo "parent.document.getElementById('nextbox').style.display='';";
	echo "parent.document.getElementById('admin').focus();";
	echo "</script>";
	exit;
	//<<<<<<<<<<<<<<<<<<<<end step 5 >>>>>>>>>>

break;






case 'step6':
	//<<<<<<<<<<<<<<<< step 6>>>>>>>>>>>>>>>>>>>>>>

	!empty($COOKIE['p8_installable']) or die($INSLANG['environment_not_matched']);
	require_once './inc/init.php';
	error_reporting(0);
	header('Content-type: text/html; charset=utf-8');

	$this_system = &$core;

	//添加积分
	if($credits = to_installed_charset(include './install/credits.php')){
		$credit = &$core->load_module('credit');
		$controller = &$core->controller($credit);
		$_config = array();
		foreach($credits as $name => $v){
			$_config[$name] = $controller->add($v);
		}

		$core->set_config($_config);

		//每天登录一次可获得5个积分,一天只能获得3次
		$rule = array(
			'credit_id' => $core->CONFIG['credit_common'],
			'system' => 'core',
			'module' => 'member',
			'action' => 'login',
			'credit' => 5,
			'times' => 3,
			'interval' => 86400
		);
		$controller->add_rule($rule);
		$credit->cache();
	}
	
	
	$member = &$core->load_module('member');
	//设置一些默认的充值金额
	$member->set_config(array(
		'recharge' => array(
			'credit_type' => $core->CONFIG['credit_gold'],
			'quantity' => array(
				2 => 2,
				5 => 5,
				10 => 10,
				30 => 33,
				50 => 55,
				100 => 110
			)
		)
	));

	
	$core->get_cache('role');
	$role = &$core->load_module('role');
	foreach($core->roles as $v){
		$role->set_menu_privilege($v['id']);
	}
	
	//角色上传限制{
	$uploader = &$core->load_module('uploader');
	//开启核心的上传功能
	$this_system = &$core;
	unset($this_module);
	require $uploader->path .'inc/enables.php';
	require $uploader->path .'inc/init_system.php';
	
	$max = ini_get('upload_max_filesize') * 1024 * 1024;
	
	$config = $core->get_config('core', 'uploader');
	$filter = array (
		'jpg' => 2048000,
		'gif' => 2048000,
		'png' => 2048000,
		'zip' => 5120000,
		'rar' => 5120000,
		'swf' => 5120000,
		'rmvb' => $max,
		'rm' => $max,
		'avi' => $max,
		'wmv' => $max,
		'flv' => $max,
		'world' => $max,
		'csv' => $max,
		'xls' => $max,
		'mp3' => $max
	);
	
	$role_filters = empty($config['role_filters']) ? array() : $config['role_filters'];
	
	$role_filters[$core->CONFIG['member_role']] = array(
		'enabled' => true,
		'filter' => array(
			'jpg' => 20480000,
			'gif' => 20480000,
			'rar' => 51200000
		)
	);

	$role_filters[$core->CONFIG['administrator_role']] = array(
		'enabled' => true,
		'filter' => array(
			'jpg' => 5120000,
			'gif' => 5120000,
			'png' => 5120000,
			'zip' => 5120000,
			'rar' => 5120000,
			'swf' => 5120000,
			
			'rmvb' => $max,
			'rm' => $max,
			'avi' => $max,
			'wmv' => $max,
			'flv' => $max,
		)
	);
	$role_filters[$core->CONFIG['member_role']] = array(
		'enabled' => true,
		'filter' => array(
			'jpg' => 2560000,
			'gif' => 2560000,
			'png' => 2560000,
			'zip' => 2560000,
			'rar' => 2560000,
			'swf' => 2560000,
		)
	);
	$uploader->set_config(array(
		'filter' => $filter,
		'role_filters' => $role_filters,
		'core_table_prefix' => empty($COOKIE['_core_table_prefix']) ? '' : $COOKIE['_core_table_prefix']
	));
	//角色上传限制}
	//添加创始人
	$member = &$core->load_module('member');
	
	$member->register(
		$_POST['admin'],
		$_POST['password'],
		$_POST['email'],
		array(
			'role_id' => $core->CONFIG['administrator_role'],
			'role_gid' => $core->CONFIG['administrator_role_group'],
			'is_founder' => 1,
			'is_admin' => 1,
		)
	);

	//登录
	$member->login($_POST['admin'], $_POST['password']);
	
	$role = &$core->load_module('role');
	//复制权限
	$role->copy_acl($core->CONFIG['member_role'], array($core->CONFIG['normal_company_role']));
	
	//把#多余的数据删掉
	$info = file_get_contents('./#.php');
	$info = preg_replace("/\/\/delete--[\s\S]+\/\/delete--/", '', $info);
	$fp = fopen('./#.php', 'w');
	fputs($fp, $info);
	fclose($fp);
	//$S_version = S_version()? S_version() : 'media';
	$S_version = 'school707';
	if(!is_dir("install/data/$S_version/fill_data")){
		
		@unlink('./data/ssi.html');
		@unlink('./data/ssi.shtml');
		@unlink('./data/install.php');
		
		//锁定二次安装
		write_file('./data/install.lock', '');
		//unlink('./utf8ize.php');
		//安装完成
		
		require_once 'inc/cache.func.php';
		
		//更所有缓存
		cache_all();
		
		$config = var_export(array(
			'DB_master' => $core->CONFIG['DB_master'],
			'memcache' => $core->CONFIG['memcache'],
			'table_prefix' => $core->CONFIG['table_prefix']
		), true);
		$fp = fopen('./data/config.php', 'w');
		fputs($fp, "<?php\r\nreturn $config;");
		fclose($fp);
		
		if(!is_dir('.svn')){
			@unlink('./install.php');
			@unlink('./utf8ize.php');
		}
		$step = 'finish';
	}
//<<<<<<<<<<<<<<<< step 6>>>>>>>>>>>>>>>>>>>>>>
break;



case 'step7':
//<<<<<<<<<<<<<<<< step 7>>>>>>>>>>>>>>>>>>>>>>
require_once './inc/init.php';
error_reporting(0);
header('Content-type: text/html; charset=utf-8');
	
	if($_POST['input']){
		//取出己安装的模块
		$chose_install = $COOKIE['chose_install'];
		$chose_install = explode(",",$chose_install);
		if(empty($chose_install)){
			$chose_install = include './data/chose_install.php';
		}
		$chose_install[]='post_sql';
		//$S_version = S_version()? S_version() : 'media';
		$S_version = 'school707';
		foreach($chose_install as $detail){
			if(!file_exists("install/data/$S_version/fill_data/$detail.php"))continue;
			//按模块导入填充数据
			$sql = get_install_sql(
				$core->DB_master,
				@file_get_contents("install/data/$S_version/fill_data/$detail.php"),
				$core->TABLE_,
				false
			);
			
			foreach($sql as $v){
				$core->DB_master->query($v);
			}
		
		
		}
		
	}
	
	@unlink('./data/ssi.html');
	@unlink('./data/ssi.shtml');
	@unlink('./data/install.php');
	@unlink('./data/chose_install.php');
	//锁定二次安装
	write_file('./data/install.lock', ' ');
	write_file('./data/installcache.lock', ' ');
	//unlink('./utf8ize.php');
	@unlink('./index.html');
	//安装完成
	
	require_once 'inc/cache.func.php';
	
	$config = var_export(array(
		'DB_master' => $core->CONFIG['DB_master'],
		'memcache' => $core->CONFIG['memcache'],
		'table_prefix' => $core->CONFIG['table_prefix']
	), true);
	$fp = fopen('./data/config.php', 'w');
	fputs($fp, "<?php\r\nreturn $config;");
	fclose($fp);
	
	if(!is_dir('.svn')){
		@unlink('./install.php');
		@unlink('./utf8ize.php');
	}
	//更所有缓存
	//cache_all();
	$step = 'finish';
//<<<<<<<<<<<<<<<< step 7>>>>>>>>>>>>>>>>>>>>>>
break;
	
}










}
require_once 'install/install.html';
