<?php
if($xc->config['info']['convert_type'] == 'file') {
	if(!isset($filenum) && !is_file(XCONVERT_ROOT.'./cache/sql/filenum.php')) {
		$filenum = array();
		$files = show_dir(XCONVERT_ROOT.'./cache/sql/');
		if(is_array($files)) {
			foreach($files as $dir) {
				if(is_dir($dir)) {
					$dir_files = @show_dir($dir.'/');
					if(is_array($dir_files) && !empty($dir_files)) {
						$dir_key = substr($dir, strrpos($dir, '/') + 1);
						$filenum[$dir_key] = count($dir_files);
					}
				}
			}
		}
		$starttime = time();
		file_put_contents(XCONVERT_ROOT.'./cache/sql/filenum.php', "<?php\n\$filenum=".var_export($filenum, TRUE).";\n\$starttime=".$starttime.';');
	}
	require_once XCONVERT_ROOT.'./cache/sql/filenum.php';

	$curprocess = $curprocess ? $curprocess : '';

	if($curprocess == '') {
		foreach($filenum as $k => $v) {
			$curprocess = $k;
			break;
		}
	}

	$fileleft = countthedir(XCONVERT_ROOT.'./cache/sql/'.$curprocess.'/');
	if($fileleft < 1) {
		$curprocess = '';
		foreach($filenum as $key => $value) { 
			if($fileleft = countthedir(XCONVERT_ROOT.'./cache/sql/'.$key.'/')) {
				$curprocess = $key;
				break;
			}
		}
	}
	
	if($curprocess != '' && function_exists("exec")) {
		$filedone = $filenum[$curprocess] - $fileleft;
		if($filedone == 0) {
			if($xc->config['info']['systype'] == '1') {
				$shell = '';
				$shell .= '#! /bin/bash'."\r\n";
				$shell .= "sqlfiledir=\"".XCONVERT_ROOT.'cache/sql/'.$curprocess."/\"\r\n";
				$shell .= "mysqldir=\"".$xc->config['info']['mysqldir']."\"\r\n";
				$shell .= "for i in \$(ls \$sqlfiledir)"."\r\n";
				$shell .= "do"."\r\n";
				$shell .= "echo \"开始导入 : \" \$i"."\r\n";
				$shell .= "\$mysqldir/mysql -u ".$xc->config['db']['discuz_dbuser']." -p".$xc->config['db']['discuz_dbpw']." --default-character-set=".$xc->config['db']['discuz_charset']." ".$xc->config['db']['discuz_dbname']." < \$sqlfiledir/\$i"."\r\n";
				$shell .= "rm -f \$sqlfiledir/\$i"."\r\n";
				$shell .= "done"."\r\n";
				$shell_suffix = '.sh';
			} else {
				$shell = '@echo off'."\r\n";
				$shell .= 'set sqlfiledir='.XCONVERT_ROOT.'cache\\sql\\'.$curprocess."\r\n";
				$shell .= '(echo %time:~0,-3%)>>start.txt'."\r\n";
				$shell .= 'set sqlid=1'."\r\n";
				$shell .= 'for /l %%a in (1,1,'.$filenum[$curprocess].') do ('."\r\n";
				$shell .= 'echo 开始导入 %sqlfiledir%\%%a.sql'."\r\n";
				$shell .= $xc->config['info']['mysqldir'].'\mysql.exe -u '.$xc->config['db']['discuz_dbuser'].' -p'.$xc->config['db']['discuz_dbpw'].' '." --default-character-set=".$xc->config['db']['discuz_charset']." ".$xc->config['db']['discuz_dbname'].' < %sqlfiledir%\%%a.sql'."\r\n";
				$shell .= 'del %sqlfiledir%\%%a.sql'."\r\n";
				$shell .= ')'."\r\n";
				$shell_suffix = '.bat';
			}
			file_put_contents(XCONVERT_ROOT.'./cache/exec/'.$curprocess.$shell_suffix, $shell);
			$timetodo = "<iframe style=\"display:none\"src=\"".'http://'.''.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?mod=exec&amp;curprocess=$curprocess\"></iframe>";
		}
		
		list($rday, $rhour, $rmin, $rsec) = remaintime(time() - $starttime);
		$stime = gmdate('Y-m-d H:i:s', $starttime + 3600* 8);
		$etime = gmdate('Y-m-d H:i:s',time() + 3600* 8);
		$timetodo .= "导出 sql 完成，正在为您导入数据库.";
		$timetodo .= "<br><br>升级开始时间：<strong>$stime</strong>, 升级程序已经执行了 <strong>$rday</strong>天 <strong>$rhour</strong>小时 <strong>$rmin</strong>分 <strong>$rsec</strong>秒";
		$timetodo .= "<br><br>目前正在执行导入 $next_table 的 sql 程序( $sql_done / $sql_total ) <strong>$prg</strong>，转换过程中需要多次跳转，请勿关闭浏览器。";
		$show_url = "index.php?mod=import&curprocess=$curprocess";
		$timetodo .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
		$timetodo .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
		$show_message = $timetodo;
	} elseif(!function_exists("exec")) {
		$show_message = "请确认 exec 函数是否被禁止：打开 PHP 的配置文件 php.ini ，找到 disable_functions ，检查是否存在限制";
	} else {
		$show_message = "进程标识丢失，请重新操作";
	}
	include_once template('import');
}

function show_dir($server_dir) {
	if($handle = opendir($server_dir)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
				$files[] = $server_dir . $file;
			}
		}
		closedir($handle);
	}
	return $files;
}

function countthedir($dir) {
	$num = 0;
	if(is_dir($dir)) {
		$files = show_dir($dir);
		if(is_array($files)) {
			foreach($files as $file) {
				$num ++;
			}
			return $num;
		}
	} else {
		return false;
	}
}

function save_process_main($prg = '') {
	global $process;
	if(defined('PROGRAM_TYPE')) {
		$prg = empty($prg) ? $GLOBALS['prg'] : $prg;
		$process[PROGRAM_TYPE][$prg] = 1;
	}
	save_process('main', $process);
}

function remaintime($time) {
	$seconds 	= $time % 60;
	$minutes 	= $time % 3600 / 60;
	$hours 		= $time % 86400 / 3600;
	$days 		= $time / 86400;
	return array((int)$days, (int)$hours, (int)$minutes, (int)$seconds);
}
?>