<?php
$operation = empty($operation) ? 'childconfig' : $operation;
if($operation == 'childconfig') {
	if($submitconfig) {
		require_once XCONVERT_ROOT.'./source/class/db.class.php';
		require_once XCONVERT_ROOT.'./data/'.$_POST['info_childtype'].'/'.$_POST['info_child'].'/config.php';
		$checkparameter = array("source_dbtype","source_dbhost","source_dbuser","source_dbname","source_tablepre","source_charset","discuz_dbhost","discuz_dbuser","discuz_dbname","discuz_tablepre","discuz_charset","info_rpp","info_pertask","discuz_type");
		$_POST['discuz_type'] = $xc->config['db']['discuz_type'];
		if($_POST['source_dbtype'] == 'access') { //¼æÈÝDV access°æ±¾
			$checkparameter = array("source_dbtype","source_dbhost","source_dbname","source_tablepre","source_charset","discuz_dbhost","discuz_dbuser","discuz_dbname","discuz_tablepre","discuz_charset","info_rpp","info_pertask","discuz_type");
		}
		foreach($checkparameter as $parameter) {
			if(empty($_POST[$parameter])) {
				showmessage('Parameter_lost');
				break;
			}
		}
		
		//sourcedb config checking
		if($_POST['source_dbtype'] == 'access') {
			$_POST['source_dbhost'] = str_replace('\\\\', '/', $_POST['source_dbhost']);
			if (substr($_POST['source_dbhost'], 0, 1) == '.') {
				$_POST['source_dbhost'] = str_replace('\\', '/', XCONVERT_ROOT.substr($_POST['source_dbhost'], 2));
			}
			$dblink_source = new db_access;
			$dblink_source->init(1);
			$dblink_source->connect($_POST['source_dbhost'], $_POST['source_dbname']);
		} elseif($_POST['source_dbtype'] == 'mssql') {
			$_POST['source_dbhost'] = str_replace('\\\\', '\\', $_POST['source_dbhost']);
			$source_tablepre = ($source_dbowner ? $source_dbowner.'.' : '').$source_tablepre;
			$dblink_source = new db_mssql;
			$dblink_source->init(1);
			if(PHP_VERSION >= '4.2.0') {
				@ini_set('mssql.datetimeconvert', 0);
			}
			@ini_set('mssql.textlimit', 2147483647);
			@ini_set('mssql.textsize', 2147483647);
			$dblink_source->connect($_POST['source_dbhost'], $_POST['source_dbuser'], $_POST['source_dbpw'], $_POST['source_dbname']);
		} elseif($_POST['source_dbtype'] == 'mysql') {
			$dblink_source = new db_mysql;
			$dblink_source->init('1');
			$dblink_source->connect($_POST['source_dbhost'], $_POST['source_dbuser'], $_POST['source_dbpw'], $_POST['source_dbname']);
			if($dblink_source->version() > '4.1') {
				$query_source = $dblink_source->query("SHOW FULL COLUMNS FROM ".$_POST['source_tablepre'].$checkfields['source']['table']);
			} else {
				$query_source = $dblink_source->query("SHOW COLUMNS FROM ".$_POST['source_tablepre'].$checkfields['source']['table']);
			}
			$checkcharset = '';
			while($field = @$dblink_source->fetch_array($query_source)) {
				if($field['Field'] == $checkfields['source']['field']) {
					$checkcharset = substr($field['Collation'], 0, strpos($field['Collation'], '_'));
					break;
				}
			}
			if($checkcharset != $_POST['source_charset']) {
				showmessage('Charset_error');
			}
		} elseif($_POST['source_dbtype'] == 'oracle') {
			$dblink_source = new db_oracle;
			$dblink_source->init(1);
			$dblink_source->connect($_POST['source_dbhost'], $_POST['source_dbuser'], $_POST['source_dbpw'], $_POST['source_dbname']);
		}
		
		
		//discuz config checking
		$dblink_target = new db_mysql;
		$dblink_target->init('1');
		$dblink_target->connect($_POST['discuz_dbhost'], $_POST['discuz_dbuser'], $_POST['discuz_dbpw'], $_POST['discuz_dbname']);
		if($dblink_target->version() > '4.1') {
			$query_target = $dblink_target->query("SHOW FULL COLUMNS FROM ".$_POST['discuz_tablepre'].$checkfields['target']['table']);
		} else {
			$query_target = $dblink_target->query("SHOW COLUMNS FROM ".$_POST['discuz_tablepre'].$checkfields['target']['table']);
		}
		$checkcharset = '';
		while($field = @$dblink_target->fetch_array($query_target)) {
			if($field['Field'] == $checkfields['target']['field']) {
				$checkcharset = substr($field['Collation'], 0, strpos($field['Collation'], '_'));
				break;
			}
		}
		if($checkcharset != $_POST['discuz_charset']) {
			showmessage('Charset_error');
		}
		if($xc->config['db']['check_charset']) {
			$_POST['discuz_charset'] != $_POST['source_charset'] ? showmessage('discuz_source charset error') : '';
		}
		writeconfig();
		if(!file_exists("./cache/lock/config_reset.lock")) {
			file_put_contents("./cache/lock/config_reset.lock", time()."\r\n");
		}
		$jump = 1;
		include_once template('config');
	} else {
		if(empty($childtype) || empty($child)) {
			showmessage('config_no_childselect');
		}
		require_once XCONVERT_ROOT.'./data/'.$childtype.'/'.$child.'/config.php';
		$source_dbtype = strtolower($source_dbtype);
		if (!in_array($source_dbtype, array('mysql', 'mssql', 'access', 'oracle'))) {
			showmessage('config_source_dbtype'); 
		}
		if ($source_dbtype == 'mssql'){
			if (!function_exists('mssql_connect')) {
				showmessage('config_mssql_connect');
			}
			if (!function_exists('ini_set')) {
				if (!function_exists('ini_get')) {
					showmessage('config_ini_get');
				} else {
					if(PHP_VERSION >= '4.2.0') {
						if (ini_get('mssql.datetimeconvert') != 0) {
							showmessage('config_mssql_datetimeconvert');
						}
					}
					if (ini_get('mssql.textlimit') < 2000000000) {
						showmessage('config_mssql_textlimit');
					}
					if (ini_get('mssql.textsize') < 2000000000) {
						showmessage('config_mssql_textsize');
					}
				}
			}
		}
		if ($source_dbtype == 'access') {
			if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
				showmessage('config_access');
			}
		}
		if ($source_dbtype == 'oracle') {
			if (!function_exists('oci_connect')) {
				showmessage('config_oci_connect');
			}
		}
		$is_uc = $is_uch = 0;
		
		if (strtolower(substr($child, -2)) == 'uc') {
			$is_uc = 1;
		}
		if (strtolower(substr($child, -3)) == 'uch') {
			$is_uc = 1;
			$is_uch = 1;
		}
		if (strtolower(substr($child, -1)) == 'x') {
			$is_uc = 0;
		}
		include_once template('config');
	}
} elseif($operation == 'writeconfig') {
	writeconfig("g");
	if(!file_exists("./cache/lock/config_reset.lock")) {
		file_put_contents("./cache/lock/config_reset.lock", time()."\r\n");
	}
}

?>