<?php
define('SOFTPUBS_IN', true);
require(dirname(__FILE__) . '/includes/init.php');


require_once(ROOT_PATH.'install/lang/'.$glo_lang.'.php');


$setup	=	$_GET['setup']?$_GET['setup']:'read_agreement';
if(file_mode_info(TEMPLATE_C_DIR)!=1111){
	echo str_replace('$1','install/',$_INSTALL_LANG['set_access_777']);
	exit;
}



require_once(ROOT_PATH.'includes/glo_template.php');
$tpl_obj			=	new template;
$tpl_obj->out['glo_site_version']		=	'1.0 Beta';		
$tpl_obj->out['glo_tpl_dir']				=	str_replace(ROOT_PATH.'install/','',TEMPLATE_DIR);
$tpl_obj->out['glo_domain_name']			=	$glo_domain_name;
$tpl_obj->out['glo_domain_directory']	=	$glo_domain_directory;
switch($setup){
	case 'read_agreement':
			$tpl_obj->display('read_agreement.html');		
		break;
	case 'check_file':
				require_once(ROOT_PATH.'install/includes/env_check.php');
				$env_arr	=	get_env();
			
				require_once(ROOT_PATH.'install/includes/check_file.php');
				$check_file	=	get_check_file();
			foreach($env_arr as $val){
				if($val[2]=='no_next'){
					$tpl_obj->out['no_next']	=	'disabled=true';
					break;
				}
			}

			$tpl_obj->out['env_arr']	=	$env_arr;
			$tpl_obj->out['check_file']	=	$check_file;
			foreach($check_file as $val){
				if(!$val){
					$tpl_obj->out['no_next']	=	'disabled=true';
					break;
				}
			}
			
			$tpl_obj->display('check_file.html');		
		break;
	case 'get_database':
			if($_SERVER['REQUEST_METHOD'] != "GET"){
				exit;		
			}
			$db_host		=	$_GET['db_host'];
			$db_port		=	$_GET['db_port'];
			$db_user		=	$_GET['db_user'];
			$db_pass		=	$_GET['db_pass'];
			$conn = @mysql_connect($db_host, $db_user, $db_pass);
			if($conn){
				$res	=	mysql_query("show databases");	
				$database_arr	=	array();
				while($row_obj	=	mysql_fetch_object($res)){
					$database_arr[]	=	$row_obj->Database;
				}
			}else{
				$database_arr		=	'no connect';	
			}	
				echo json_return($database_arr);exit;
		break;
	case 'system_configure':
			require_once(ROOT_PATH.'includes/glo_template_extension.php');
			$tpl_ex				=		new template_ex(null,'default');
			$list_tpl			=		$tpl_ex->list_tpl();
			$list_tpl_tmp		=		$list_tpl['other'];
			if($list_tpl_tmp){
				array_unshift($list_tpl_tmp,$list_tpl['now']);
			}else{
				$list_tpl_tmp[]		=	$list_tpl['now'];
			}
			$list_tpl_arr		=		array();
			foreach($list_tpl_tmp as $key=>$val){
				if($val['tpl_type']=='only'){
					$list_tpl_arr['only'][]	=	$val;	
				}elseif($val['tpl_type']=='multiple'){
					$list_tpl_arr['multiple'][]	=	$val;
				}
			}
			$tpl_obj->out['list_tpl_arr']	=	$list_tpl_arr;
			$tpl_obj->display('system_configure.html');		
		break;
	case 'system_configure_save':
			if($_SERVER['REQUEST_METHOD'] != "POST"){
				exit;		
			}
			require_once(ROOT_PATH.'install/includes/db_config.php');
			$type			=	$_POST['type']?$_POST['type']:$_GET['type'];
			if($type=='install_db'){
				$db_user		=	$_POST['db_user'];
				$db_pass		=	$_POST['db_pass'];
				$db_port		=	$_POST['db_port'];
				$db_name		=	$_POST['db_name'];			
				$db_pre		=	$_POST['db_pre'];
				$db_host		=	$_POST['db_host'];

				$setup_arr	=	array(
					'connect'		=>	array('bewrite'=>$_INSTALL_LANG['connect_sql'],'err'=>''),
					'exist_db'		=>	array('bewrite'=>$_INSTALL_LANG['exist_db'],'err'=>''),
					'dump_sql'		=>	array('bewrite'=>$_INSTALL_LANG['dump_sql'],'err'=>''),
					'write_config'	=>	array('bewrite'=>$_INSTALL_LANG['write_config'],'err'=>''),
				);
				db_save_config($db_host,$db_port,$db_user,$db_pass,$db_name,$db_pre,$setup_arr);
				echo json_return($setup_arr);
				exit;		
			}elseif($type=='install_admin'){
				$setup_arr	=	array(
					'setup_config'		=>	array('bewrite'=>$_INSTALL_LANG['setup_config'],'err'=>''),
					'set_admin_name'		=>	array('bewrite'=>$_INSTALL_LANG['set_admin_name'],'err'=>''),
					'set_admin_pass'		=>	array('bewrite'=>$_INSTALL_LANG['set_admin_pass'],'err'=>''),
					'set_admin_email'		=>	array('bewrite'=>$_INSTALL_LANG['set_admin_email'],'err'=>''),
				);
				$admin_name		=	$_POST['admin_name'];
				$admin_pass		=	$_POST['admin_pass'];
				$admin_email	=	$_POST['admin_email'];
				save_admin_config($admin_name,$admin_pass,$admin_email,$setup_arr);
				echo json_return($setup_arr);
				exit;		
			}elseif($type=='install_test_data'){
				$test_data		=	$_POST['test_data'];		
				if($test_data){
					$setup_arr	=	array(
						'install_test_data'		=>	array('bewrite'=>$_INSTALL_LANG['tested_data_input'],'err'=>''),
					);	
					install_test_data($test_data,$setup_arr);
					echo json_return($setup_arr);
				}
				exit;		
			}elseif($type=='install_site_type'){
				$site_type	=	$_POST['site_type']?$_POST['site_type']:'multiple';
				$setup_arr	=	array(
						'install_site_type'		=>	array('bewrite'=>$_INSTALL_LANG['configure_site_type_information'],'err'=>''),
				);	
				install_site_type($site_type,$setup_arr);
				echo json_return($setup_arr);
				exit;
			}elseif($type=='install_site_tpl'){
				$site_tpl	=	$_POST['site_tpl']?$_POST['site_tpl']:'default';
				$setup_arr	=	array(
						'install_site_tpl'		=>	array('bewrite'=>$_INSTALL_LANG['install_template'],'err'=>''),
				);	
				install_site_tpl($site_tpl,$setup_arr);
				echo json_return($setup_arr);	
				exit;
			}elseif($type=='install_pad_data_input'){
				$pad_link	=	$_POST['pad_link'];
				$pad_file	=	$_FILES['pad_file_name'];
				if($pad_link||$pad_file){
					$pad_name	=	$pad_link?$pad_link:$pad_file['name'];
					$setup_arr	=	array(
							'install_pad_data_input'		=>	array('bewrite'=>str_replace('$1',$pad_name,$_INSTALL_LANG['receiving_pad_info_now']),'err'=>''),
					);	
					install_pad_data_input($pad_link,$pad_file,$setup_arr,$pad_name);
					echo json_return($setup_arr);
				}
				exit;	
			}	
		break;

	case 'install_ok':
			file_put_contents(DATA_DIR.'config.php',"\n\$glo_lang	=	'$glo_lang';",FILE_APPEND);	
			$tpl_obj->display('install_ok.html');		
	break;

	case 'install_ok_complete':
			$install_num		=	@file_get_contents('http://www.softpubs.com/install_num.php');
			$install_num		=	substr($install_num,0,32);
			file_put_contents(DATA_DIR.'install.lock',time());
			file_put_contents(DATA_DIR.'config.php',"\n\$glo_install_num	=	'$install_num';",FILE_APPEND);	
	break;
}
