<?
function db_save_config($db_host,$db_port=3306,$db_user,$db_pass,$db_name,$db_pre,&$setup_arr)
{
	global $glo_lang;
	$conn		=	@mysql_connect($db_host.':'.$db_port,$db_user,$db_pass);
	global $_INSTALL_LANG;
	if(!$conn){
		$setup_arr['connect']['err']		=	$_INSTALL_LANG['connect_sql_err'];
		return;
	}
	if(!mysql_select_db($db_name)){
		$setup_arr['exist_db']['err']		=	$_INSTALL_LANG['exist_db_err'];
		return;
	}
	mysql_query('set names utf8');
	$db_sql	=	file_get_contents('db_data/db_configuration_'.$glo_lang.'.sql');	
	$db_sql	=	str_replace('{$db_pre}',$db_pre,$db_sql);
	$db_sql_arr	=	explode(";\n",$db_sql);
	foreach($db_sql_arr as $db_sql_val){
		$db_sql_val	=	trim($db_sql_val);
		if(!$db_sql_val){
			continue;
		}
		mysql_query($db_sql_val);
		if(mysql_error()){
			$setup_arr['dump_sql']['err']		=	$_INSTALL_LANG['dump_sql_err'];
			return ;
		}
	}
	$install_time	=	time();
	global $glo_site_version;
	$config_str	=	<<<html
<?
	\$glo_db_host			=	'{$db_host}';
	\$glo_db_port			=	'{$db_port}';
	\$glo_db_name			=	'{$db_name}';
	\$glo_db_user			=	'{$db_user}';
	\$glo_db_pass			=	'{$db_pass}';
	\$glo_db_pre			=	'{$db_pre}';
	
	\$glo_install_time		=	{$install_time};
	\$glo_site_version		=	'{$glo_site_version}';
html;
	if(!@file_put_contents(DATA_DIR.'config.php',$config_str)){
			$setup_arr['dump_sql']['err']		=	$_INSTALL_LANG['write_config_err'];	
			return;	
	}
}


function save_admin_config($admin_name,$admin_pass,$admin_email,&$setup_arr)
{
	global $_INSTALL_LANG;
	if(!file_exists(DATA_DIR.'config.php')){
		$setup_arr['setup_config']['err']	=	$_INSTALL_LANG['configuration_file_can_be_found'];
		return;
	}else{
		require_once(DATA_DIR.'config.php');
		@mysql_connect($glo_db_host.':'.$glo_db_port,$glo_db_user,$glo_db_pass);
		mysql_select_db($glo_db_name);
		mysql_query("set names utf8");		
		$sql		=		"insert into {$glo_db_pre}site_config set name='admin_name',value='".mysql_escape_string($admin_name)."',update_time='".time()."' ON DUPLICATE KEY UPDATE  value='".mysql_escape_string($admin_name)."' ";
		mysql_query($sql);
		if(mysql_error()){
			$setup_arr['set_admin_name']['err']		=	$_INSTALL_LANG['admin_account_setting_failure'];
			return;
		}	
		$sql		=		"insert into {$glo_db_pre}site_config set name='admin_pass',value='".mysql_escape_string(encrypt_admin_pass($admin_pass))."',update_time='".time()."' ON DUPLICATE KEY UPDATE  value='".mysql_escape_string(encrypt_admin_pass($admin_pass))."' ";
		mysql_query($sql);
		if(mysql_error()){
			$setup_arr['set_admin_name']['err']		=	$_INSTALL_LANG['admin_password_setting_failure'];
			return;
		}
		$sql		=		"insert into {$glo_db_pre}site_config set name='admin_email',value='".mysql_escape_string($admin_email)."',update_time='".time()."' ON DUPLICATE KEY UPDATE  value= '".mysql_escape_string($admin_email)."' ";
		mysql_query($sql);
		if(mysql_error()){
			$setup_arr['set_admin_name']['err']		=	$_INSTALL_LANG['admin_mailbox_setting_failure'];
			return;
		}	
	}
}

function install_test_data($test_data,&$setup_arr)
{
	global $_INSTALL_LANG;
	if(!file_exists(DATA_DIR.'config.php')){
		$setup_arr['install_test_data']['err']	=	$_INSTALL_LANG['configuration_file_can_be_found'];
		return;
	}else{
		require_once(DATA_DIR.'config.php');	
		@mysql_connect($glo_db_host.':'.$glo_db_port,$glo_db_user,$glo_db_pass);
		mysql_select_db($glo_db_name);
		mysql_query("set names utf8");	
	}
//	foreach($test_data as $val){
		$sql_file	=	ROOT_PATH.'install/db_data/test_data/'.basename($test_data).'/data.sql';					
		$db_sql	=	file_get_contents($sql_file);	
		$db_sql	=	str_replace('{$db_pre}',$glo_db_pre,$db_sql);
		$db_sql_arr	=	explode(";\n",$db_sql);
		foreach($db_sql_arr as $db_sql_val){
			$db_sql_val	=	trim($db_sql_val);
			if(!$db_sql_val){
				continue;
			}
			mysql_query($db_sql_val);
			if(mysql_error()){
				$setup_arr['install_test_data']['err']		=	$_INSTALL_LANG['tested_data_input_failure'];
				return ;
			}
		}	
	//}
}

function install_site_type($site_type,&$setup_arr)
{
	global $_INSTALL_LANG;
	if(!file_exists(DATA_DIR.'config.php')){
		$setup_arr['install_site_type']['err']	=	$_INSTALL_LANG['configuration_file_can_be_found'];
		return;
	}else{
		require_once(DATA_DIR.'config.php');	
		@mysql_connect($glo_db_host.':'.$glo_db_port,$glo_db_user,$glo_db_pass);
		mysql_select_db($glo_db_name);
		mysql_query("set names utf8");	
	}	
	$sitetype       =       $site_type=='only'?'only':'';	
	mysql_query(" INSERT INTO  {$glo_db_pre}site_config SET value='".$sitetype."', name='site_type' ON  DUPLICATE KEY UPDATE value='".$sitetype."'");	
	if(mysql_error()){
		$setup_arr['install_site_type']['err']	=	$_INSTALL_LANG['configuration_failure_of_site_cateogry_info'];
	}
	return;
}

function install_site_tpl($site_tpl,&$setup_arr)
{
	global $_INSTALL_LANG;
	if(!file_exists(DATA_DIR.'config.php')){
		$setup_arr['install_site_type']['err']	=	$_INSTALL_LANG['configuration_file_can_be_found'];
		return;
	}else{
		require_once(DATA_DIR.'config.php');	
		require_once(ROOT_PATH.'includes/database.php');
		$db_obj	 =	 new Database(TRUE);
		if (!$db_obj->connect($glo_db_host, $glo_db_user, $glo_db_pass, $glo_db_name)){
   	  die("Not Connect!");
		}
		$db_obj->query('set names utf8');	
		$db_obj->db_pre	=	$glo_db_pre;
	}	
	require_once(ROOT_PATH.'includes/glo_template_extension.php');
	$tpl_ex				=		new template_ex($db_obj,$site_tpl);
	if(!$tpl_ex->install_tpl()){
		$setup_arr['install_site_tpl']['err']		=	$_INSTALL_LANG['template_installation_failure'];
	}	
	return;	
}

function install_pad_data_input($pad_link='',$pad_file='',&$setup_arr,$pad_name)
{
	global $_INSTALL_LANG;
	if(!$pad_link&&!$pad_file){
		$setup_arr['install_pad_data_input']['err']		=	str_replace('$1',"<b>".$pad_name."</b>",$_INSTALL_LANG['pad_import_failure']);
		return; 
	}
	if(!file_exists(DATA_DIR.'config.php')){
		$setup_arr['install_pad_data_input']['err']	=	$_INSTALL_LANG['configuration_file_can_be_found'];
		return;
	}else{
		require_once(DATA_DIR.'config.php');	
		require_once(ROOT_PATH.'includes/database.php');
		$db_obj	 =	 new Database(TRUE);
		if (!$db_obj->connect($glo_db_host, $glo_db_user, $glo_db_pass, $glo_db_name)){
   	  die("Not Connect!");
		}
		$db_obj->query('set names utf8');	
		$db_obj->db_pre	=	$glo_db_pre;
		$GLOBALS['db_obj']	=	$db_obj;
	}	
	if($pad_link){
		$pad_str		=	@file_get_contents($pad_link);
		$pad_data	=	$pad_link;
	}else{
		$pad_str		=	file_get_contents($pad_file['tmp_name']);
		$pad_data	=	$pad_file;
	}
	require_once(ROOT_PATH.'admin/includes/action.class.php');
	require_once(ROOT_PATH.'admin/includes/soft.class.php');
	$software_handle_obj		=		new soft;
	if(!($software_name_version	=		$software_handle_obj->get_pad_data($pad_str,$pad_data,1))){
		$setup_arr['install_pad_data_input']['err']		=	$software_handle_obj->message_info;
	}
	return;	
}
