<?php
/*
用户资料
pre_ucenter_members
pre_ucenter_membersfields
pre_common_member
pre_common_member_count
pre_common_member_field_forum
pre_common_member_field_home
pre_common_member_status
pre_common_onlinetime
pre_common_member_medal
*/

if($start <= 1) {
	truncatetable('ucenter_members');
	truncatetable('ucenter_memberfields');

	truncatetable('common_member');
	truncatetable('common_member_count');
	truncatetable('common_member_field_forum');
	truncatetable('common_member_field_home');
	truncatetable('common_member_profile');
	truncatetable('common_member_status');
	truncatetable('common_onlinetime');
	truncatetable('common_member_medal');
	truncatetable('forum_medal');

	//勋章
	$medalquery = $xc->db['source']->query("SELECT * FROM {$source_tablepre}medals");

	while($medal = $xc->db['source']->fetch_array($medalquery)) {
		$medal = daddslashes($medal);
		$xc->db['discuz']->query(
			"REPLACE INTO {$discuz_tablepre}forum_medal
			(medalid, name, available, image) 
			VALUES ('{$medal[medalid]}', '{$medal[name]}', '{$medal[available]}', '{$medal[image]}')"
		);
	}
	//用户组
	ctspecialgroup("{$source_tablepre}usergroups", 'common_usergroup');
}


$maxid = $xc->db['source']->result($xc->db['source']->query("SELECT max(uid) MAXUID FROM {$source_tablepre}users"), 0, 'MAXUID');

if($start < $maxid){
	$converted = 1;
}
$query = $xc->db['source']->query(
	"SELECT m.*, m.uid as muid, mf.*, g.groupid, g.radminid, g.grouptitle 
	FROM {$source_tablepre}users m 
	LEFT JOIN {$source_tablepre}userfields mf ON mf.uid=m.uid 
	LEFT JOIN {$source_tablepre}usergroups g ON g.groupid=m.groupid
	WHERE m.uid>=$start AND m.uid<$start+$rpp"
	) or dexit();

while($ntm = $xc->db['source']->fetch_array($query)) {
	$ntm =	daddslashes(array_change_key_case($ntm));
	$uid		=	$ntm['uid'];
	if(empty($uid)){
		continue;
	}
	$username = trim(stripslashes($ntm['username']));

	if(!$username || $username != htmlspecialchars(daddslashes($username))) {
		reportlog("非法用户名 <b><font color='red'>$username</font></b> 不能被转换，uid = $uid ；<br>\r\n");
	} elseif(strlen($username) > 15) {
		reportlog("用户名 <b><font color='orange'>$username</font></b> 长度大于 15，不能被转换，uid = $uid ；<br>\r\n");
	} elseif(getuid($username)) {
		reportlog("重复用户名 <b><font color='blue'>$username</font></b> 不能被转换，uid = $uid ；<br>\r\n");
	} else {
	
	//uc members
		$ucpw		= convertucpw($ntm['password']);
		$password	= $ucpw['password'];
		$email		= $ntm['email'];
		$myid		= '';
		$myidkey	= '';
		$regip		= $ntm['regip'];
		$regdate	= timetounix($ntm['joindate']);
		$lastloginip	= $ntm['lastip'];
		$lastlogintime	= timetounix($ntm['lastvisit']);
		$salt		= $ucpw['salt'];
		$secques	= $ntm['secques'];
		
		$fields1	= array('uid', 'username', 'password', 'email', 'myid', 'myidkey', 
					'regip', 'regdate', 'lastloginip', 'lastlogintime', 'salt','secques');
		$query1		= getinsertsql("{$discuz_tablepre}ucenter_members", $fields1);

		if(!$xc->db['discuz']->query($query1)) {
			reportlog("转换 UCenter 用户主表出错 uid=$uid, username=$username");
			continue;
		}
	//uc_memberfields
		$blacklist	= '';
		$fields2 	= array('uid', 'blacklist');
		$query2  	= getinsertsql("{$discuz_tablepre}ucenter_memberfields", $fields2);
		
		if(!$xc->db['discuz']->query($query2)) {
			
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_members WHERE uid='$uid' LIMIT 1;");
			reportlog("转换 UCenter 用户资料表出错 uid=$uid, username=$username");
			continue;
		}
	//pre_common_member
		$status		=	0;
		$emailstatus	=	0;
		$avatarstatus	=	0;
		$videophotostatus=	0;
		$adminid	=	$ntm['adminid'];
		$groupid	=	$ntm['groupid'];
		$groupexpiry	=	$ntm['groupexpiry'];
		$extgroupids	=	$ntm['extgroupids'];
		$credits	=	$ntm['credits'];
		$notifysound	=	0;
		$timeoffset	=	9999;
		$newpm		=	$ntm['newpm'];
		$newprompt	=	0;
		$accessmasks	=	0;
		$allowadmincp	=	0;

		$fields3	= array(
					'uid','email','username','password','status','emailstatus',
					'avatarstatus','videophotostatus','adminid','groupid','groupexpiry',
					'extgroupids','regdate','credits','notifysound','timeoffset','newpm',
					'newprompt','accessmasks','allowadmincp');
		$query3 	= getinsertsql("{$discuz_tablepre}common_member", $fields3);

		if(!$xc->db['discuz']->query($query3)) {
			
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_members WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_memberfields WHERE uid='$uid' LIMIT 1;");

			reportlog("转换X用户信息主表出错 uid=$uid, username=$username");
			continue;
		}
	//pre_common_member_count
		$extcredits1	=	$ntm['extcredits1'];
		$extcredits2	=	$ntm['extcredits2'];
		$extcredits3	=	$ntm['extcredits3'];
		$extcredits4	=	$ntm['extcredits4'];
		$extcredits5	=	$ntm['extcredits5'];
		$extcredits6	=	$ntm['extcredits6'];
		$extcredits7	=	$ntm['extcredits7'];
		$extcredits8	=	$ntm['extcredits8'];
		$friends	=	0;
		$oltime		=	$ntm['oltime'];

		//最后统计
		$posts		=	$ntm['posts'];
		$threads	=	0;
		$digestposts	=	$ntm['digestposts'];
		$doings		=	0;
		$blogs		=	0;
		$albums		=	0;
		$sharings	=	0;
		$attachsize	=	0;
		$views		=	0;

		$fields4 	= array(
					'uid','extcredits1','extcredits2','extcredits3','extcredits4',
					'extcredits5','extcredits6','extcredits7','extcredits8','friends',
					'posts','threads','digestposts','doings','blogs','albums',
					'sharings','attachsize','views','oltime');
		$query4 	= getinsertsql("{$discuz_tablepre}common_member_count", $fields4);
		if(!$xc->db['discuz']->query($query4)) {

			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_members WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_memberfields WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member WHERE uid='$uid' LIMIT 1;");

			reportlog("转换用户统计数据表出错 uid=$uid, username=$username");
			continue;
		}
	//pre_common_member_field_forum
		$publishfeed	=	0;
		$customshow	=	26;
		$customstatus	=	$ntm['customstatus'];
		$medals		=	str_replace(',', "\t", $ntm['medals']);
		$sightml	=	$ntm['sightml'];
		$groupterms	=	'a:0:{}';
		$authstr	=	$ntm['authstr'];
		$groups		=	'';
		$attentiongroup	=	'';

		$fields5 	= array(
					'uid','publishfeed','customshow','customstatus','medals',
					'sightml','groupterms','authstr','groups','attentiongroup');
		$query5 	= getinsertsql("{$discuz_tablepre}common_member_field_forum", $fields5);
		
		if(!$xc->db['discuz']->query($query5)) {
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_members WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_memberfields WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_count WHERE uid='$uid' LIMIT 1;");

			reportlog("转换用户论坛字段表出错 uid=$uid, username=$username");
			continue;
		}

	//pre_common_member_field_home
		$videophoto		=	'';
		$spacename		=	'';
		$spacedescription	=	'';
		$domain			=	'';
		$addsize		=	0;
		$addfriend		=	0;
		$theme			=	'';
		$spacecss		=	'';
		$blockposition		=	'';
		$recentnote		=	'';
		$spacenote		=	'';
		$privacy		=	'';
		$feedfriend		=	'';
		$acceptemail		=	'';

		$fields6 	= array(
					'uid','videophoto','spacename','spacedescription','domain','addsize',
					'addfriend','theme','spacecss','blockposition','recentnote','spacenote',
					'privacy','feedfriend','acceptemail');
		$query6 	= getinsertsql("{$discuz_tablepre}common_member_field_home", $fields6);
		if(!$xc->db['discuz']->query($query6)) {
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_members WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_memberfields WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_count WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_field_forum WHERE uid='$uid' LIMIT 1;");

			reportlog("转换用户家园字段表出错 uid=$uid, username=$username");
			continue;
		}
	//pre_common_member_profile
		$realname		=	$ntm['realname'];
		$gender			=	$m['gender'];
		$birthyear		=	gexplode($ntm['bday'], 0);
		$birthmonth		=	gexplode($ntm['bday'], 1);
		$birthday		=	gexplode($ntm['bday'], 2);
		$constellation		=	'';
		$zodiac			=	'';
		$telephone		=	$ntm['phone'];
		$mobile			=	$ntm['mobile'];
		$idcardtype		=	'';
		$idcard			=	$ntm['idcard'];
		$address		=	'';
		$zipcode		=	'';
		$nationality		=	'';
		$birthprovince		=	'';
		$birthcity		=	'';
		$resideprovince		=	'';
		$residecity		=	'';
		$residedist		=	'';
		$residecommunity	=	'';
		$residesuite		=	'';
		$graduateschool		=	'';
		$company		=	'';
		$education		=	'';
		$occupation		=	'';
		$position		=	'';
		$revenue		=	'';
		$affectivestatus	=	'';
		$lookingfor		=	'';
		$bloodtype		=	'';
		$height			=	'';
		$weight			=	'';
		$alipay			=	'';
		$icq			=	$ntm['icq'];
		$qq			=	$ntm['qq'];
		$yahoo			=	$ntm['yahoo'];
		$msn			=	$ntm['msn'];
		$taobao			=	'';
		$site			=	$ntm['website'];
		$bio			=	$ntm['bio'];
		$interest		=	'';
		$field1			=	'';
		$field2			=	'';
		$field3			=	'';
		$field4			=	'';
		$field5			=	'';
		$field6			=	'';
		$field7			=	'';
		$field8			=	'';

		$fields7 	= array(
					'uid','gender','birthyear','birthmonth','birthday','constellation',
					'zodiac','telephone','mobile','idcardtype','idcard','address','zipcode',
					'nationality','birthprovince','birthcity','resideprovince','residecity',
					'residedist','residecommunity','residesuite','graduateschool','company',
					'education','occupation','position','revenue','affectivestatus',
					'lookingfor','bloodtype','height','weight','alipay','icq','qq','yahoo',
					'msn','taobao','site','bio','interest','field1','field2','field3',
					'field4','field5','field6','field7','field8');
		$query7 	= getinsertsql("{$discuz_tablepre}common_member_profile", $fields7);

		if(!$xc->db['discuz']->query($query7)) {
		
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_members WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_memberfields WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_count WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_field_forum WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_field_home WHERE uid='$uid' LIMIT 1;");

			reportlog("转换用户自定义信息表出错 uid=$uid, username=$username");
			continue;

		}
	//pre_common_member_status
		$lastip			=	$ntm['lastip'];
		$lastvisit		=	timetounix($ntm['lastvisit']);
		$lastactivity		=	timetounix($ntm['lastactivity']);
		$lastpost		=	timetounix($ntm['lastpost']);
		$lastsendmail		=	0;
		$pokes			=	0;
		$pendingfriends		=	0;
		$invisible		=	0;
		$buyercredit		=	0;
		$sellercredit		=	0;

		$fields8 	= array(
					'uid','regip','lastip','lastvisit','lastactivity','lastpost',
					'lastsendmail','invisible','buyercredit','sellercredit');
		$query8 	= getinsertsql("{$discuz_tablepre}common_member_status", $fields8);

		if(!$xc->db['discuz']->query($query8)) {
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_members WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_memberfields WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_count WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_field_forum WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_field_home WHERE uid='$uid' LIMIT 1;");
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}common_member_profile WHERE uid='$uid' LIMIT 1;");

			reportlog("转换用户状态表出错 uid=$uid, username=$username");
			continue;
		}
		$convertedrows ++;
		$converted = 1;
		$totalrows ++;
	}
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($ntm['uid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
?>