<?php
require_once XCONVERT_ROOT.'./data/'.$xc->config['info']['childtype'].'/'.$xc->config['info']['child'].'/config.php';
if(file_exists(XCONVERT_ROOT.'./data/'.$xc->config['info']['childtype'].'/'.$xc->config['info']['child'].'/function.php')) {
	require_once XCONVERT_ROOT.'./data/'.$xc->config['info']['childtype'].'/'.$xc->config['info']['child'].'/function.php';
}

if($isstop == 0) {
	if(!isset($step)) {
		$processarr = $tmpsteparr = array();
		$lockdir = dir(XCONVERT_ROOT.'./cache/lock/');
		while($entry = $lockdir->read()) {
			if(is_file(XCONVERT_ROOT.'./cache/lock/'.$entry) && substr($entry, 0, 9) == 'progress_') {
				echo "abcdef = ".XCONVERT_ROOT.'./cache/lock/'.$entry."<br>";
				preg_match_all("/progress_(\d+?)\.txt/is", $entry, $tmpsteparr);
				$processarr[] = $tmpsteparr['1']['0'];
			}
		}
		$lockdir->close();
		rsort($processarr);
		$step = $processarr['0'] ? $processarr['0'] : 1;
		file_put_contents(XCONVERT_ROOT."./cache/lock/fhb0421.txt", var_export($processarr,true)."\r\n", FILE_APPEND);
	}
	
	//====================
	if($gap == 1/* && $stay == 1*/) {
		if(file_exists(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt")) {
			$contents = file_get_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt");
			$tmparr = $parameters = array();
			preg_match_all("/&(.+?)=([\d|a-z|,]+)?/is", $contents, $tmparr);
			foreach($tmparr[1] as $k => $v) {
				$parameters[$v] = $tmparr[2][$k];
			}
			extract($parameters);
		}
		$gapmsg = dgmdate($timestamp, 'Y-m-d H:i:s')."\t";
		$gapmsg .= 'index.php?mod=dealdata&step='.$step.'&start='.($end + 1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows."\n";
		runlog("jumpinterrupt", $errmsg);
		sendmail($xc->config['info']['toemail'], "XConvert Jump Interrupt Message", $message);
	}
	//====================
	$steps = count($operations);
	if($step == $steps + 1) {
		showmessage($xc->language['convert_ready_stats'], 'index.php?mod=stats');
	}

	$discuz_tablepre = $xc->config['db']['discuz_dbname'].'.'.$xc->config['db']['discuz_tablepre'];
	$uc_tablepre = $xc->config['db']['uc_dbname'].'.'.$xc->config['db']['uc_tablepre'];
	$uch_tablepre = $xc->config['db']['uch_dbname'].'.'.$xc->config['db']['uch_tablepre'];
	$config_type = $xc->config['db']['discuz_type'];
	if($xc->config['db']['source_dbtype'] == 'access') {
		$source_tablepre = $xc->config['db']['source_tablepre'];
	} elseif($xc->config['db']['source_dbtype'] == 'mssql') {
		if(PHP_VERSION >= '4.2.0') {
			@ini_set('mssql.datetimeconvert', 0);
		}
		@ini_set('mssql.textlimit', 2147483647);
		@ini_set('mssql.textsize', 2147483647);
		$source_tablepre = ($xc->config['db']['source_dbowner'] ? $xc->config['db']['source_dbowner'].'.' : '').$xc->config['db']['source_tablepre'];
	} elseif($xc->config['db']['source_dbtype'] == 'mysql') {
		$source_tablepre = $xc->config['db']['source_dbname'].'.'.$xc->config['db']['source_tablepre'];
	} elseif($xc->config['db']['source_dbtype'] == 'oracle') {
		$source_tablepre = $xc->config['db']['source_tablepre'];
	}

	if(PHP_VERSION > '5.1') {
		@date_default_timezone_set('Etc/GMT'.($xc->config['db']['source_timeoffset'] > 0 ? '-' : '+').(abs($xc->config['db']['source_timeoffset'])));
	}

	$rpp = $xc->config['info']['rpp'];
	$step = isset($step) ? $step : 1;
	$start = isset($start) && $start > 1 ? $start : 1;
	$end = $start + $rpp - 1;
	$stay = isset($stay) ? $stay : 0;
	$converted = 0;
	$convertedrows = isset($convertedrows) ? $convertedrows : 0;
	$totalrows = isset($totalrows) ? $totalrows : 0;
	$operation = isset($operations[$step]) ? $operations[$step] : $xc->language['convert_end'];
	$extrastr = isset($extrastr) ? $extrastr : '';
	$sqlfilenum = isset($sqlfilenum) ? $sqlfilenum : 0;

	if($start <= 1) {
		startreport();
	}

	require_once XCONVERT_ROOT.'./data/'.$xc->config['info']['childtype'].'/'.$xc->config['info']['child'].'/step_'.$step.'.php';

	if($converted) {
		if($multitables) {
			if($nexttableid > 0) {
				$show_message = '<p>'.$xc->language['convert_atstep'].$step.$xc->language['convert_slash'].$steps.$xc->language['convert_step'].$operation.'</p>'.$xc->language['convert_convert'].$nextid.$xc->language['convert_splittable'].$xc->language['convert_date_end'].$xc->language['convert_ready'].$nexttableid.$xc->language['convert_splittable'];
				$show_url = 'index.php?mod=dealdata&step='.$step.'&start='.($end + 1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&tableid='.$nextid.'&maxtableid='.$maxtableid.'&sqlfilenum='.$sqlfilenum.'&posttables='.$posttables;
			} else {
				$show_message = '<p>'.$xc->language['convert_atstep'].$step.$xc->language['convert_slash'].$steps.$xc->language['convert_step'].$operation.'</p>'.$xc->language['convert_steping'].$nowtableid.$xc->language['convert_splittable'].$start.$xc->language['convert_horizontal'].$end.$xc->language['convert_step_date'];
				$show_url = 'index.php?mod=dealdata&step='.$step.'&start='.($end + 1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&tableid='.$tableid.'&maxtableid='.$maxtableid.'&sqlfilenum='.$sqlfilenum.'&posttables='.$posttables;
			}
		} else {
			$show_message = '<p>'.$xc->language['convert_atstep'].$step.$xc->language['convert_slash'].$steps.$xc->language['convert_step'].$operation.'</p>'.$xc->language['convert_steping'].$start.$xc->language['convert_horizontal'].$end.$xc->language['convert_step_date'];
			$show_url = 'index.php?mod=dealdata&step='.$step.'&start='.($end + 1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
		}
		if(!file_exists(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt")){ //为了兼容where查询条件中没有使用limit
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $show_url);
		}
		$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
		$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=dealdata&stay=".$stay."&isstop=1&step=".$step."\" style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";

	} else {
		if(file_exists(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt")) {
			unlink(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt");
		}
		report();
		$nextstep = $step + 1;
		if ($stay == 0) {
			if($xc->config['info']['convert_type'] == 'file') {
				$nexturl = $nextstep <= $steps ? 'index.php?mod=dealdata&step='.$nextstep.$extrastr : 'index.php?mod=import';
				$nextoperation = isset($operations[$nextstep]) ? $xc->language['convert_convert'].$operations[$nextstep].$xc->language['convert_date'] : $xc->language['convert_import'];
			} else {
				$nexturl = $nextstep <= $steps ? 'index.php?mod=dealdata&step='.$nextstep.$extrastr : 'index.php?mod=stats';
				$nextoperation = isset($operations[$nextstep]) ? $xc->language['convert_convert'].$operations[$nextstep].$xc->language['convert_date'] : $xc->language['convert_stats'];
			}
			$show_message = $xc->language['convert_ready'].$nextoperation.$xc->language['convert_date'].'......<br>';
			$show_url = $nexturl;
			$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
			$show_message .= $nextstep <= $steps ? "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>" : "<script>setTimeout(\"jumpstats('".$show_url."');\", 1000);</script>";
			$show_message .= "<script>function jumpstats(url) {window.parent.location = url;}</script>";
		} else {
			
			if($xc->config['info']['convert_type'] == 'file') {
				$nexturl = 'index.php?mod=import';
				$nextoperation = $xc->language['convert_import'];
				$show_message = $xc->language['convert_ready'].$nextoperation.$xc->language['convert_date'].'......<br>';
				$show_url = $nexturl;
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_message .= "<script>window.parent.location = \"$nexturl\";</script>";
			} else {
				$nexturl = $nextstep <= $steps ? 'index.php?mod=dealdata&step='.$nextstep.'&stay=1' : 'index.php?mod=stats';
				$nextoperation = isset($operations[$nextstep]) ? $operations[$nextstep] : $xc->language['convert_stats'];
				$show_message = $xc->language['convert_convert'].$operations[$nextstep-1].$xc->language['convert_date_end'].'&nbsp;&nbsp;&nbsp;&nbsp;
				<p><a href="'.($nextstep <= $steps ? $nexturl.'">' : '#" onclick="jumpstats(\''.$nexturl.'\')">').$xc->language['convert_next_step'].$nextoperation.$xc->language['convert_click'].'</a><a style="margin-left:20px;" href="index.php?mod=report&step='.$step.'" target="_blank">'.$xc->language['report_viewdetail'].'</a></p>';
				$show_url = $nexturl;
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_message .= "<script>function jumpstats(url) {window.parent.location = url;}</script>";
			}
		}
	}
} else {
	$operation = isset($operations[$step]) ? $operations[$step] : $xc->language['convert_end'];
	$show_message = $xc->language['convert_have_stoped']."<br><br><div align=\"right\">[<a href=\"index.php?mod=dealdata&stay=".$stay."&step=".$step."\" style=\"color:red\">".$xc->language['convert_start_again']."</a>]</div>";
	$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
}
include_once template('dealdata');
?>
