<?php
function stay_redirect() {
	global $action, $actionnow, $scriptname, $step, $stay, $reportfile;
	$nextstep = $step + 1;
	if($stay) {
		$actions = isset($action[$nextstep]) ? $action[$nextstep] : '结束';
		echo "$actionnow 操作完毕.".($stay == 1 ? "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"./$reportfile\" target=\"_blank\"><u>查看转换结果分析报告 $reportfile</u></a>" : '').'<br>';
		echo "<a href='".$scriptname."?step=".$nextstep."&stay=1'>如果继续下一步操作( $actions )，请点击这里！</a><br>";
		echo "<a href='$scriptname'>点击这里返回到程序首页</a>";
	} else {
		if(isset($action[$nextstep])) {
			echo '即将进入：'.$action[$nextstep].'......<br>';
		}
		$timeout = $GLOBALS['debug'] ? 5000 : 2000;
		echo "<script>\r\n";
		echo "<!--\r\n";
		echo "function redirect() {\r\n";
		echo "	window.location.replace('".$scriptname."?step=".$nextstep."');\r\n";
		echo "}\r\n";
		echo "setTimeout('redirect();', $timeout);\r\n";
		echo "-->\r\n";
		echo "</script>\r\n";
		echo "<p align=right>[<a href='$scriptname' style='color:red'>停止运行</a>]<br><br><a href=\"".$scriptname."?step=".$nextstep."\">如果您的浏览器长时间没有自动跳转，请点击这里！</a></p>";
	}
}
function continue_redirect($extra = '') {
	global $scriptname, $step, $end, $stay, $convertedrows, $totalrows, $maxid;
	$url = $scriptname."?step=".$step."&start=".($end + 1)."&stay=$stay&totalrows=$totalrows&convertedrows=$convertedrows&maxid=$maxid".$extra;
	$timeout = $GLOBALS['debug'] ? 5000 : 2000;
	echo "<script>\r\n";
	echo "<!--\r\n";
	echo "function redirect() {\r\n";
	echo "	window.location.replace('".$url."');\r\n";
	echo "}\r\n";
	echo "setTimeout('redirect();', $timeout);\r\n";
	echo "-->\r\n";
	echo "</script>\r\n";
	echo "<p align=right>[<a href='$scriptname' style='color:red'>停止运行</a>]<br><br><a href=\"".$url."\">如果您的浏览器长时间没有自动跳转，请点击这里！</a></p>";
}
function accesstimetounix($time) {
	if($time > 100000000) {
		return $time;
	}
	$time = str_replace(array(' 一月 ',' 二月 ',' 三月 ',' 四月 ',' 五月 ',' 六月 ',' 七月 ',' 八月 ',' 九月 ',' 十月 ',' 十一月 ',' 十二月 ', ' 上午 '), array('-1-','-2-','-3-','-4-','-5-','-6-','-7-','-8-','-9-','-10-','-11-','-12-', ' '), $time);
	if(strrchr($time, '下午') !== false) {
		return strtotime(str_replace(' 下午 ', ' ', $time)) + 43200;
	} else {
		return strtotime($time);
	}
}
function acctimetounixtime($time) {
        $dt_elements = @explode(" ",$time);       // 将字符串分解成2部分－日期、时间
        $date_elements = @explode("-",$dt_elements[0]);           // 分解日期
        $time_elements = @explode(":",$dt_elements[1]);           // 分解时间
        $unixtime = @mktime ($time_elements [0], $time_elements[ 1], $time_elements[ 2], $date_elements[1], $date_elements[2], $date_elements[0]);
        return $unixtime;
}
function validid($id, $table, $condition = '') {
	global $start, $end, $maxid, $rpp, $dbs, $source_name,$CONFIG;
	$sql = "SELECT MIN($id) AS minid, MAX($id) AS maxid FROM ".$CONFIG['tablepre'].$table.($condition ? " WHERE $condition" : '');
	$row = $dbs->get_one($sql);
	$start = $row['minid'];
	$maxid = $row['maxid'];
	$start = $start > 1 ? $start : 1;
	$end = $start + $rpp - 1;
}
//以下要使用到的方法函数
function altertable($table, $orderby) {
	if(!db::query("ALTER TABLE ".DB::table($table)." ORDER BY `$orderby` ASC")) reportlog('整理 '.$GLOBALS['cfg_dbprefix'].$table.' 数据表出错');
}
function truncatetable($table) {
	if(!db::query("TRUNCATE TABLE ".DB::table($table)."")) ekekeexit('清空数据表 '.$GLOBALS['cfg_dbprefix'].$table.' 出错');
}
function setnames($charset) {
	if (@mysql_get_server_info() > '4.1' && in_array(strtolower($charset), array('gbk', 'big5', 'utf8'))) {
		mysqlquery("SET NAMES '$charset';") or dexit("设置 MySQL 字符集($charset)出错，请检查配置的字符集以及 MySQL 版本是否正确！");
	}
}
function ekekeaddslashes($string) {
	if(is_array($string)) {
		foreach($string as $key => $val) $string[$key] = ekekeaddslashes($val);
	} else {
		$string = addslashes($string);
	}
	return $string;
}
function cutstr($string, $length, $dot = '') {
	global $cfg_soft_lang;
	if(strlen($string) <= $length) return $string;
	$strcut = '';
	if(strtolower($cfg_soft_lang) == 'utf8') {
		$n = $tn = $noc = 0;
		while ($n < strlen($string)) {
			$t = ord($string[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1; $n++; $noc++;
			} elseif(194 <= $t && $t <= 223) {
				$tn = 2; $n += 2; $noc += 2;
			} elseif(224 <= $t && $t < 239) {
				$tn = 3; $n += 3; $noc += 2;
			} elseif(240 <= $t && $t <= 247) {
				$tn = 4; $n += 4; $noc += 2;
			} elseif(248 <= $t && $t <= 251) {
				$tn = 5; $n += 5; $noc += 2;
			} elseif($t == 252 || $t == 253) {
				$tn = 6; $n += 6; $noc += 2;
			} else {
				$n++;
			}
			if ($noc >= $length) {
				break;
			}
		}
		if ($noc > $length) {
			$n -= $tn;
		}
		$strcut = substr($string, 0, $n);
	} else {
		for($i = 0; $i < $length - strlen($dot) - 1; $i++) {
			$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
		}
	}
	return $strcut.$dot;
}

function mysqlerror() {
	$mysqlerrno = mysql_errno();
	switch($mysqlerrno) {
		case '1005': $errmsg = "创建表失败"; break;
		case '1006': $errmsg = "创建数据库失败"; break;
		case '1007': $errmsg = "数据库已存在，创建数据库失败"; break;
		case '1008': $errmsg = "数据库不存在，删除数据库失败"; break;
		case '1016': $errmsg = "无法打开数据文件"; break;
		case '1041': $errmsg = "系统内存不足"; break;
		case '1045': $errmsg = "连接数据库失败，用户名或密码错误"; break;
		case '1046': $errmsg = "选择数据库失败，请正确配置数据库名称"; break;
		case '1044': $errmsg = "当前用户没有访问数据库的权限"; break;
		case '1048': $errmsg = "字段不能为空"; break;
		case '1049': $errmsg = "数据库不存在"; break;
		case '1050': $errmsg = "数据表已存在"; break;
		case '1051': $errmsg = "数据表不存在"; break;
		case '1054': $errmsg = "字段不存在"; break;
		case '1062': $errmsg = "字段值重复，入库失败"; break;
		case '1064': $errmsg = "可能原因：1.数据超长或类型不匹配；2.数据库记录重复"; break;
		case '1065': $errmsg = "无效的SQL语句，SQL语句为空"; break;
		case '1081': $errmsg = "不能建立Socket连接"; break;
		case '1129': $errmsg = "数据库出现异常，请重启数据库"; break;
		case '1130': $errmsg = "连接数据库失败，没有连接数据库的权限"; break;
		case '1133': $errmsg = "数据库用户不存在"; break;
		case '1141': $errmsg = "当前用户无权访问数据库"; break;
		case '1142': $errmsg = "当前用户无权访问数据表"; break;
		case '1143': $errmsg = "当前用户无权访问数据表中的字段"; break;
		case '1146': $errmsg = "数据表不存在"; break;
		case '1149': $errmsg = "SQL语句语法错误"; break;
		case '1169': $errmsg = "字段值重复，更新记录失败"; break;
		case '2003': $errmsg = "请检查数据库服务器端口设置是否正确，默认端口为 3306"; break;
		case '2005': $errmsg = "数据库服务器不存在"; break;
	}
	return $mysqlerrno ? '<br>Error '.$mysqlerrno.' : '.mysql_error().'.<br>错误 '.$mysqlerrno.'：'.$errmsg.'。<br>' : '';
}
function ekekeexit($msg = '', $sql = 'mysql') {
	$str = '<tr><td>';
	$str .= $sql == 'mysql' ? $msg.mysqlerror() : $msg;
	$str .= '<br><br><font color="blue">请根据以上提示信息进行调整，然后刷新本程序继续进行转换！</font></td></tr>';
	exit($str);
}
function reportlog($report) {
	global $reportfile;
	$fp = @fopen($reportfile, 'a');
	@fwrite($fp, $report.mysqlerror()."<br>\r\n");
	@fclose($fp);
}
function report($data) {
	global $convertedrows, $totalrows, $step;
	reportlog("\r\n<br>结果分析：共有 $data 数据 <font color='blue'>$totalrows</font> 条，转换成功 <font color='grey'>$convertedrows</font> 条，失败 <font color='gray'>".($totalrows - $convertedrows)."</font> 条，成功率：<font color='red'>".($totalrows > 0 ? $convertedrows / $totalrows * 100 : 0)."%</font>。<br>\r\n");
}
function getcururl2(){
	if(!empty($_SERVER["REQUEST_URI"])){
		$scriptName = $_SERVER["REQUEST_URI"];
		$nowurl = $scriptName;
	}else{
		$scriptName = $_SERVER["PHP_SELF"];
		if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;
		else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
	}
	return $nowurl;
}
function djcmsheader() {
	global $step, $steps, $actionnow, $source_ver, $djcms_ver, $scriptname;
	return "
<html>
<head>
<title>".(!$step ? '准备开始转换' : ($step > $steps ? '转换结束' : "第 $step/$steps 步($actionnow)"))." - $source_ver => $djcms_ver</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gbk\">
</head>
<body text=\"#000000\">
<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bgcolor=\"#FFFFFF\" align=\"center\">
  <tr>
    <td>
      <table width=\"98%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">
        <tr>
          <td class=\"install\" height=\"30\" valign=\"bottom\">
            <b><span style='font-size:14px'>$source_ver => $djcms_ver 转换程序 $scriptname</span></b></td>
        </tr>
        <tr>
          <td>
            <hr noshade align=\"center\" width=\"100%\" size=\"1\">
          </td>
        </tr>
        <tr>
          <td align=\"center\">
            <b><span style='color:#FF58C0'>本转换程序可以把 $source_ver 标准数据结构的数据转换为 $djcms_ver 的数据</span></b>
          </td>
        </tr>
        <tr>
          <td>
            <hr noshade align=\"center\" width=\"100%\" size=\"1\">
          </td>
        </tr>
        <tr>
          <td>
	";
}

function djcmsfooter() {
	return "
          </td>
        </tr>
        <tr>
          <td>
            <hr noshade align=\"center\" width=\"100%\" size=\"1\">
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<br>
</body>
</html>
	";
}
?>