<?php
//2、转换会员数据

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');
	validid('uid', 'users');

	//勋章
	$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');
}

$query = $xc->db['source']->query("SELECT u.*, uf.*, g.groupid, g.radminid, g.grouptitle FROM {$source_tablepre}users u LEFT JOIN {$source_tablepre}userfields uf ON uf.uid=u.uid LEFT JOIN {$source_tablepre}usergroups g ON g.groupid=u.groupid WHERE u.uid >= $start AND u.uid < $start + $rpp") or die("检索数据表 '{$source_tablepre}users' 和 '{$source_tablepre}userfields' 错误！请将表 '{$source_tablepre}users' 和 '{$source_tablepre}userfields' 字段类型为 nText 的改为 Text 类型。");

while ($m = $xc->db['source']->fetch_assoc($query)) {
	$m	= daddslashes(array_change_key_case($m));
	$uid = $m['uid'];
	if(empty($uid)){
		continue;
	}
	$username = trim(stripslashes($m['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($m['password']);
		$password		= $ucpw['password'];
		$email			= $m['email'];
		$myid			= '';
		$myidkey		= '';
		$regip			= $m['regip'];
		$regdate		= timetounix($m['joindate']);
		$lastloginip	= $m['lastip'];
		$lastlogintime	= timetounix($m['lastvisit']);
		$salt			= $ucpw['salt'];
		$secques		= $m['secques'];

		//uc memberfields
		$blacklist		= '';

		//pre_common_member
		$status			=	0;
		$emailstatus	=	0;
		$avatarstatus	=	0;
		$videophotostatus	=	0;
		$adminid		= $m['adminid'];
		$groupid		= $m['groupid'];
		$groupexpiry	= $m['groupexpiry'];
		$extgroupids	= $m['extgroupids'];
		$credits		= $m['credits'];
		$notifysound	=	0;
		$timeoffset		= 9999;
		$newpm			=	$m['newpm'];
		$newprompt		=	0;
		$accessmasks	=	0;
		$allowadmincp	=	0;

		//pre_common_member_count
		$extcredits1	= $m['extcredits1'];
		$extcredits2	= $m['extcredits2'];
		$extcredits3	= $m['extcredits3'];
		$extcredits4	= $m['extcredits4'];
		$extcredits5	= $m['extcredits5'];
		$extcredits6	= $m['extcredits6'];
		$extcredits7	= $m['extcredits7'];
		$extcredits8	= $m['extcredits8'];
		$friends			=	0;
		$oltime			= $m['oltime'];

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

		//pre_common_member_field_forum
		$publishfeed	=	0;
		$customshow		=	26;
		$customstatus	= $m['customstatus'];
		$medals		=	str_replace(',', "\t", $m['medals']);
		$sightml		= $m['sightml'];
		$groupterms		= 'a:0:{}';
		$authstr		= $m['authstr'];
		$groups			=	'';
		$attentiongroup	=	'';

		//pre_common_member_field_home
		$videophoto		=	'';
		$spacename		=	'';
		$spacedescription	=	'';
		$domain			=	'';
		$addsize		=	0;
		$addfriend		=	0;
		$theme			=	'';
		$spacecss		=	'';
		$blockposition	=	'';
		$recentnote		=	'';
		$spacenote		=	'';
		$privacy		=	'';
		$feedfriend		=	'';
		$acceptemail	=	'';
		
		//pre_common_member_profile
		$realname		= $m['realname'];
		$gender			= $m['gender'];
		$birthyear		= gexplode($m['bday'], 0);
		$birthmonth		= gexplode($m['bday'], 1);
		$birthday		= gexplode($m['bday'], 2);
		$constellation	= '';
		$zodiac			= '';
		$telephone		= $m['phone'];
		$mobile			= $m['mobile'];
		$idcardtype		= '';
		$idcard			=	$m['idcard'];
		$address		=	'';
		$zipcode		=	'';
		$nationality	=	'';
		$birthprovince	=	'';
		$birthcity		=	'';
		$resideprovince	=	'';
		$residecity		=	'';
		$residedist		=	'';
		$residecommunity	=	'';
		$residesuite	=	'';
		$graduateschool	=	'';
		$company		=	'';
		$education		=	'';
		$occupation		=	'';
		$position		=	'';
		$revenue		=	'';
		$affectivestatus=	'';
		$lookingfor		=	'';
		$bloodtype		=	'';
		$height			=	'';
		$weight			=	'';
		$alipay			=	'';
		$icq			=	$m['icq'];
		$qq				=	$m['qq'];
		$yahoo			=	$m['yahoo'];
		$msn			=	$m['msn'];
		$taobao			=	'';
		$site			=	$m['website'];
		$bio			=	$m['bio'];
		$interest		=	'';
		$field1			=	'';
		$field2			=	'';
		$field3			=	'';
		$field4			=	'';
		$field5			=	'';
		$field6			=	'';
		$field7			=	'';
		$field8			=	'';

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

		$fields1	= array('uid', 'username', 'password', 'email', 'myid', 'myidkey', 'regip', 'regdate', 'lastloginip', 'lastlogintime', 'salt','secques');
		$query1		= getinsertsql("{$discuz_tablepre}ucenter_members", $fields1);

		$fields2 	= array('uid', 'blacklist');
		$query2  	= getinsertsql("{$discuz_tablepre}ucenter_memberfields", $fields2);

		$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);

		$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);

		$fields5 	= array('uid','publishfeed','customshow','customstatus','medals','sightml','groupterms','authstr','groups','attentiongroup');
		$query5 	= getinsertsql("{$discuz_tablepre}common_member_field_forum", $fields5);

		$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);

		$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);

		$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($query1)) {
			if ($xc->db['discuz']->query($query2)) {
				$password = strtolower($pwmember['password']);
				if ($xc->db['discuz']->query($query3)) {
					if ($xc->db['discuz']->query($query4)) {
						if($xc->db['discuz']->query($query5)) {
							if($xc->db['discuz']->query($query6)) {
								if($xc->db['discuz']->query($query7)) {
									if($xc->db['discuz']->query($query8)) {
										$convertedrows ++;
									} else {
										$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");
									}
								} else {
									$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");
								}
							} else {
								$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");
							}
						} else {
							$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");
						}
					} else {
						$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");
					}
				} else {
					$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");
				}
			} else {
				$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}ucenter_members WHERE uid='$uid' LIMIT 1;");

				reportlog("转换 UCenter 用户资料表出错 uid=$uid, username=$username");
			}
		} else {
			reportlog("转换 UCenter 用户主表出错 uid=$uid, username=$username");
		}
	}
	$converted = 1;
	$totalrows ++;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($m['uid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}

?>