<?php
require_once(dirname(__FILE__)."/../inc/common.php");
require_once(dirname(__FILE__)."/include/inc.function.php");
//运行脚本超时设置
@set_time_limit(3600);
/*以下配置好导入目标数据库设置*/
$CONFIG['dbhost'] 	= 'datas.mdb'; 	//数据库文件名或者数据库主机
$CONFIG['dbuser'] 	= '';    			//数据库用户名
$CONFIG['dbpw'] 	= '';  					//数据库密码
$CONFIG['dbname'] 	= 'dbname'; 		//数据库名称
$CONFIG['tablepre'] = 'm_';  			//原数据表前缀
$CONFIG['database'] = 'access';  		//数据库类型支持Access,MSSQL
$CONFIG['pconnect'] = '0';  				//是否使用持久连接
$CONFIG['dbcharset']= 'gbk'; 				//数据库连接字符集
$CONFIG['charset'] 	= 'gbk'; 				//网站字符集
require_once(dirname(__FILE__)."/include/db_".$CONFIG['database'].".class.php");
$classname = 'db_'.$CONFIG['database'];
$dbs = new $classname();
@$dbs->connect($CONFIG['dbhost'], $CONFIG['dbuser'], $CONFIG['dbpw'], $CONFIG['dbname'], $pconnect);
$scriptname		=	'index.php'; //运行脚本文件
$djcms_ver			=	'DjCms3.0正式版'; //当前DjCms版本号
$source_ver		=	'MAXCMS4.0ACCESS+MSSQL'; //脚本说明
$action				=	array('1'=>'导入分类数据','2'=>'专辑数据','3'=>'影片数据','4'=>'新闻数据','5'=>'评论数据','6'=>'友情链接','7'=>'评论数据','8'=>'留言数据','9'=>'广告数据','10'=>'自定义标签');
$djcms_startitems = '100000'; // 这里的数字是您的MAXCMS2.8里面最大的影片ID数,请认真填写.
$djcms_startitems_double = $djcms_startitems * 2;
$rpp					=	'200'; // 每次需要转换的数据量，如果您的服务器比较慢，请将这个值调小
$reportfile		=	'report.htm'; // 用来记录转换结果分析报告，该文件所在目录须具有可写权限
$timestamp		=	time();
$start				=	isset($_GET['start']) && $_GET['start'] > 1 ? $_GET['start'] : 1;
$limit_start	=	$start - 1;
$end					=	$start + $rpp - 1;
$stay					=	isset($_GET['stay']) ? $_GET['stay'] : 0;
$converted		=	0;
$totalrows		=	isset($_GET['totalrows']) ? $_GET['totalrows'] : 0;
$convertedrows=	isset($_GET['convertedrows']) ? $_GET['convertedrows'] : 0;
$step					=	isset($_GET['step']) ? $_GET['step'] : 0;
$steps				=	count($action);
$actionnow		=	isset($action[$step]) ? $action[$step] : '结束';
$maxid				=	isset($_GET['maxid']) ? $_GET['maxid'] : 0;
$tableid			=	isset($_GET['tableid']) ? $_GET['tableid'] : 1;
$debug				= 300;
echo djcmsheader();

if(!$step) {
	if(@mysql_get_server_info() > '4.1') {
		if(!in_array('gbk', array('latin1', 'gbk'))) {
			die("请在 common.inc.php 中正确设置  DjCms3.0正式版 的字符集！");
			exit();
		}
	}
	$validversion		=	"　MAXCMS4.0 => DjCms3.0正式版";
	$environment		=	"
　配置好 PHP + MySQL 的 web 环境；";
	$statements		=	"本程序只对数据作转换，不会对原来的 $source_ver 数据造成破坏，如果担心 $source_ver 数据丢失，请先作备份，我们不对任何此类型的数据丢失负责；";

	$validdata		=	"影片分类 专辑数据 影片数据 友情链接 评论数据 留言数据 广告数据 自定义标签";
	$invaliddata		=	"影片标题长度超出 60 字节的部分将被截掉；";
	$beforeconvert		=	"
　1.请确认您已经成功安装了 DjCms3.0正式版;并PHP支持MSSQL数据库连接；<br>
　<font color=\"red\">2.请修改 $scriptname 文件，仔细配置转换程序需要的参数，其中，\$rpp 变量需要根据转换程序所在环境的优劣进行合理设置；</font><br>
　<font color=\"green\">3.$scriptname 文件通过 web 可以访问，在浏览器中运行 $scriptname 准备转换；</font><br>
　4.由于转换过程非常消耗资源，所以建议您尽量在服务器空闲期间进行。
	";
	$converting		=	"
　1.正常情况下，转换过程是全自动的，您无需干预，请静静等待出现转换完成的提示；<br>
　<font color=\"red\">2.如果转换过程中程序终止运行，请根据出错提示信息进行调整，然后刷新页面继续进行转换；</font><br>
　3.转换所需时间取决于您的数据量大小、数据结构的标准与否、转换程序所在平台的性能、网络状况等因素。
	";
?>
<table border="0" width="100%"><tr><td style="line-height:150%">
<b>声明</b>：<br><?=$statements?><br><br>
<b>本程序适用版本</b>：<br><?=$validversion?><br><br>
<b>本程序所需环境</b>：<br><?=$environment?><br><br>
<b>本程序可以转换的数据</b>：<br><?=$validdata?><br><br>
<b>本程序不能或无法完整转换的数据</b>：<br><?=$invaliddata?><br><br>
<b>转换前</b>：<br><?=$beforeconvert?><br><br>
<b>转换中</b>：<br><?=$converting?><br><br>
<b>完全转换：</b><br>
<ul><li>[<a href="<?=$scriptname?>?step=1"><b>开始完全转换</b></a>]</li></ul>
<?php
echo "<b>分步单独转换：</b>\r\n<ul>";
foreach($action AS $astep=>$aaction) echo "<li>单独[<a href='".$scriptname."?step=".$astep."&stay=1'>$aaction</a>]</li>\r\n";
echo "</ul>\r\n";

}else if($step == 1) {	// 导入分类数据
	if ($start <= 1) {
		truncatetable('type');
		validid('m_id', 'type');
	}
	$categories = array();
	$r = $dbs->query("SELECT * FROM ".$CONFIG['tablepre']."type WHERE m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $categories[] = $rs;
	foreach($categories as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$typearr = array(
		    'm_id' => $m_id,
	        'm_name' => $m_name,
	        'm_enname' => $m_enname,
	        'm_sort' => $m_sort,
	        'm_template' => $m_template,
            'm_upid' => $m_upid,
	        'm_type' => $m_type,
	        'm_subtemplate' => $m_subtemplate,
	    );
		if(DB::insert('type', $typearr)){
			$convertedrows ++;
		} else {
			reportlog("插入分类数据出错");
		}
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('分类');
		stay_redirect();
	}
}else  if($step == 2) {
	if ($start <= 1) {
		truncatetable('topic');
		validid('m_id', 'topic');
	}
	$categories = array();
	$r = $dbs->query("SELECT * FROM ".$CONFIG['tablepre']."topic WHERE m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $categories[] = $rs;
	foreach($categories as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$typearr = array(
		    'm_id' => $m_id,
	        'm_name' => $m_name,
	        'm_enname' => $m_enname,
	        'm_sort' => $m_sort,
	        'm_template' => $m_template,
            'm_pic' => $m_pic,
	        'm_des' => $m_des,
	    );
		if(DB::insert('topic', $typearr)){
			$convertedrows ++;
		} else {
			reportlog("插入专辑数据出错");
		}
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('专辑');
		stay_redirect();
	}
}else if($step == 3) {
	if ($start <= 1) {
		truncatetable('data');
		validid('m_id', 'data');
	}
	$datas = array();
	$dessql = ",m_des";
	if($CONFIG['database'] == 'mssql') $dessql = ",CAST(m_des AS TEXT) as m_des";
	$r = $dbs->query("SELECT * FROM ".$CONFIG['tablepre']."data WHERE m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $datas[] = $rs;
	unset($rs);
	foreach($datas as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$m_addtime = ereg_replace("[^0-9]","",acctimetounixtime($m_addtime));
		if(empty($m_addtime)) $m_addtime = time();
		$m_datetime = ereg_replace("[^0-9]","",acctimetounixtime($m_datetime));
		if(empty($m_datetime)) $m_datetime = time();
		$feedarr = array(
		    'm_id' => $m_id,
			'm_name' => $m_name,
			'm_type' => $m_type,
			'm_state' => $m_state,
			'm_pic' => $m_pic,
			'm_hit' => $m_hit,
			'm_digg' => $m_digg,
			'm_actor' => $m_actor,
			'm_note' => $m_note,
			'm_des' => $m_des,
			'm_topic' => $m_topic,
			'm_color' => $m_color,
			'm_commend' => $m_commend,
			'm_wrong' => $m_wrong,
			'm_addtime' => $m_addtime,
			'm_publishyear' => $m_publishyear,
			'm_publisharea' => $m_publisharea,
			'm_playdata' => $m_playdata,
			'm_letter' => msubstr($m_enname,1),
			'm_keyword' => $m_keyword,
			'm_lang' => $m_lang,
			'm_dayhit' => $m_dayhit,
			'm_weekhit' => $m_weekhit,
			'm_monthhit' => $m_monthhit,
			'm_director' => $m_director,
			'm_enname' => $m_enname,
			'm_datetime' => $m_datetime,
			'm_recycle' => $m_recycle,
			'm_score' => $m_score,
		);
		if(DB::insert('data', $feedarr)){
			$convertedrows ++;
		} else {
			reportlog("插入影片数据数据出错");
		}
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('影片数据');
		stay_redirect();
	}
}else if($step == 4) {
	if ($start <= 1) {
		truncatetable('news');
		validid('m_id', 'news');
	}
	$datas = array();
	$dessql = ",m_content";
	if($CONFIG['database'] == 'mssql') $dessql = ",CAST(m_content AS TEXT) as m_content";
	$r = $dbs->query("SELECT * FROM ".$CONFIG['tablepre']."news WHERE m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $datas[] = $rs;
	unset($rs);
	foreach($datas as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$m_addtime = ereg_replace("[^0-9]","",acctimetounixtime($m_addtime));
		if(empty($m_addtime)) $m_addtime = time();
		$m_datetime = ereg_replace("[^0-9]","",acctimetounixtime($m_datetime));
		if(empty($m_datetime)) $m_datetime = time();
		$feedarr = array(
		    'm_id' => $m_id,
			'm_type' => $m_type,
			'm_vid' => $m_vid,
			'm_title' => $m_title,
			'm_author' => $m_author,
			'm_color' => $m_color,
			'm_pic' => $m_pic,
			'm_from' => $m_from,
			'm_keyword' => $m_keyword,
			'm_outline' => $m_outline,
			'm_content' => $m_content,
			'm_commend' => $m_commend,
			'm_hit' => $m_hit,
			'm_dayhit' => $m_dayhit,
			'm_weekhit' => $m_weekhit,
			'm_monthhit' => $m_monthhit,
			'm_note' => $m_note,
			'm_digg' => $m_digg,
			'm_tread' => $m_tread,
			'm_entitle' => $m_entitle,
			'm_letter' => $m_letter,
			'm_addtime' => $m_addtime,
			'm_datetime' => $m_datetime,
			'm_recycle' => $m_recycle,
			'm_score' => $m_score,
		);
		if(DB::insert('news', $feedarr)){
			$convertedrows ++;
		} else {
			reportlog("插入新闻数据数据出错");
		}
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('新闻数据');
		stay_redirect();
	}
}else if($step == 5) {
	if ($start <= 1) {
		truncatetable('review');
		validid('m_id', 'review');
	}
	$datas = array();
	$dessql = ",m_content";
	if($CONFIG['database'] == 'mssql') $dessql = ",CAST(m_content AS TEXT) as m_content";
	$r = $dbs->query("SELECT * FROM ".$CONFIG['tablepre']."review WHERE m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $datas[] = $rs;
	unset($rs);
	foreach($datas as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$m_addtime = ereg_replace("[^0-9]","",acctimetounixtime($m_addtime));
		if(empty($m_addtime)) $m_addtime = time();
		$feedarr = array(
		    'm_id' => $m_id,
			'm_author' => $m_author,
			'm_type' => $m_type,
			'm_videoid' => $m_videoid,
			'm_content' => $m_content,
			'm_ip' => $m_ip,
			'm_addtime' => $m_addtime,
			'm_reply' => $m_reply,
			'm_agree' => $m_agree,
			'm_anti' => $m_anti,
			'm_pic' => $m_pic,
			'm_vote' => $m_vote,
			'm_check' => $m_check,
		);
		DB::insert('review', $feedarr);
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('评论数据');
		stay_redirect();
	}
}else if($step == 6) {
	if ($start <= 1) {
		truncatetable('link');
		validid('m_id', 'link');
	}
	$links = array();
	$dessql = ",m_des";
	if($CONFIG['database'] == 'mssql') $dessql = ",CAST(m_des AS TEXT) as m_des";
	$r = $dbs->query("SELECT m_id,m_name,m_type,m_pic,m_url,m_sort$dessql FROM ".$CONFIG['tablepre']."link WHERE m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $links[] = $rs;
	foreach($links as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$feedarr = array(
		    'm_id' => $m_id,
			'm_name' => $m_name,
			'm_type' => $m_type,
			'm_pic' => $m_pic,
			'm_url' => $m_url,
			'm_des' => $m_des,
			'm_sort' => $m_sort,
		);
		if(DB::insert('link', $feedarr)) {
			$convertedrows ++;
		} else {
			reportlog("插入友情链接出错");
		}
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('友情链接');
		stay_redirect();
	}
}else if($step == 7) {
	if ($start <= 1) {
		truncatetable('info');
		validid('m_id', 'info' ,"m_type='comment'");
	}
	$comments = array();
	$Contentsql = ",m_content";
	if($CONFIG['database'] == 'mssql') $Contentsql = ",CAST(m_content AS TEXT) as m_content";
	$r = $dbs->query("SELECT m_id,m_videoid,m_author,m_ip,m_addtime $Contentsql FROM ".$CONFIG['tablepre']."info WHERE m_type='comment' and m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $comments[] = $rs;
	foreach($comments as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$m_addtime = ereg_replace("[^0-9]","",acctimetounixtime($m_addtime));
		$feedarr = array(
		    'm_id' => $m_id,
			'm_videoid' => $m_videoid,
			'm_author' => $m_author,
			'm_ip' => $m_ip,
			'm_content' => $m_content,
			'm_addtime' => $m_addtime,
		);
		if(DB::insert('info', $feedarr)) {
			$convertedrows ++;
		} else {
			reportlog("插入评论数据出错");
		}
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('评论');
		stay_redirect();
	}
}else if($step == 8) {
	if ($start <= 1) {
		truncatetable('leaveword',$dsql);
		validid('m_id', 'leaveword');
	}
	$guestbooks = array();
	$Contentsql = ",m_content";
	if($CONFIG['database'] == 'mssql') $Contentsql = ",CAST(m_content AS TEXT) as m_content";
	$r = $dbs->query("SELECT m_id,m_author,m_ip,m_addtime $Contentsql FROM ".$CONFIG['tablepre']."leaveword WHERE m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $guestbooks[] = $rs;
	foreach($guestbooks as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$m_addtime = ereg_replace("[^0-9]","",acctimetounixtime($m_addtime));
		$feedarr = array(
		    'm_id' => $m_id,
			'm_author' => $m_author,
			'm_ip' => $m_ip,
			'm_content' => $m_content,
			'm_addtime' => $m_addtime,
		);
		if(DB::insert('leaveword', $feedarr)) {
			$convertedrows ++;
		} else {
			reportlog("插入留言数据出错");
		}
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('留言');
		stay_redirect();
	}
}else if($step == 9) {
	if ($start <= 1) {
		truncatetable('ads',$dsql);
		validid('m_id', 'ads');
	}
	$myads = array();
	$Contentsql = ",m_content";
	if($CONFIG['database'] == 'mssql') $Contentsql = ",CAST(m_content AS TEXT) as m_content";
	$r = $dbs->query("SELECT m_id,m_name,m_enname,m_des,m_addtime $Contentsql FROM ".$CONFIG['tablepre']."ads WHERE m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $myads[] = $rs;
	foreach($myads as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$m_addtime = ereg_replace("[^0-9]","",acctimetounixtime($m_addtime));
		$feedarr = array(
		    'm_id' => $m_id,
			'm_name' => $m_name,
			'm_enname' => $m_enname,
			'm_des' => $m_des,
			'm_content' => $m_content,
			'm_addtime' => $m_addtime,
		);
		if(DB::insert('ads', $feedarr)) {
			$convertedrows ++;
		} else {
			reportlog("插入广告数据出错");
		}
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('广告');
		stay_redirect();
	}
}else if($step == 10) {
	if ($start <= 1) {
		truncatetable('selflabel',$dsql);
		validid('m_id', 'selflabel');
	}
	$mytags = array();
	$Contentsql = ",m_content";
	if($CONFIG['database'] == 'mssql') $Contentsql = ",CAST(m_content AS TEXT) as m_content";
	$r = $dbs->query("SELECT m_id,m_name,m_des,m_addtime $Contentsql FROM ".$CONFIG['tablepre']."selflabel WHERE m_id BETWEEN $start AND $end");
	while($rs = $dbs->fetch_array($r)) $mytags[] = $rs;
	foreach($mytags as $key => $row){
		foreach($row as $k => $v) $$k = new_addslashes($v);
		$m_content=str_replace('search.asp','search.php',str_replace('{maxcms:sitepath}','{djcms:sitepath}',$m_content));
		$m_addtime = ereg_replace("[^0-9]","",acctimetounixtime($m_addtime));
		$feedarr = array(
		    'm_id' => $m_id,
			'm_name' => $m_name,
			'm_content' => $m_content,
			'm_des' => $m_des,
			'm_addtime' => $m_addtime,
		);
		if(DB::insert('selflabel', $feedarr)) {
			$convertedrows ++;
		} else {
			reportlog("插入自定义标签数据出错 m_id = $m_id");
		}
		$converted = 1;
		$totalrows ++;
	}
	if($converted || $end < $maxid) {
		continue_redirect();
	} else {
		report('自定义标签');
		stay_redirect();
	}
}else if($step > 10){ // 结束转换
	echo $stay ? '' : "<br><center><b><span style='font-size:16px'>$source_ver => $djcms_ver 转换完毕！&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"./$reportfile\" target=\"_blank\"><u>查看转换结果分析报告 $reportfile</u></a></span></b></center><br>\r\n";
	echo "		<b>重新转换：</b>\r\n";
	echo "		<ul><li>[<a href='".$scriptname."'>返回到程序首页</a>]</li>\r\n";
	echo "		<li>[<a href='".$scriptname."?step=1'><b>重新开始完全转换</b></a>]</li></ul>\r\n";
	echo "		<b>分步单独转换：</b>\r\n<ul>";
	foreach($action AS $astep=>$aaction) echo "		<li>单独[<a href='".$scriptname."?step=".$astep."&stay=1'>$aaction</a>]</li>\r\n";
	echo "		</ul>\r\n";
}
$dbs->close();
echo djcmsfooter();
?>