<?php
/*
用户资料
pre_ucenter_members
pre_ucenter_memberfields
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');
	//在 pre_common_member 数据库中增加一个字段 avatar：
	$co_query	=	$xc->db['discuz']->query("show columns from {$discuz_tablepre}common_member like 'avatar'");
	$avatar_arr =	$xc->db['discuz']->fetch_array($co_query);
	if(!$avatar_arr['Field'] == 'avatar') {
		if(!$xc->db['discuz']->query("ALTER TABLE {$discuz_tablepre}common_member ADD avatar VARCHAR(255) NOT NULL DEFAULT '' COMMENT '仅为转换头像而添加的字段，头像转换结束后，此字段可删除'")) {
			reportlog("添加用户头像字段失败，转换完成后，所有用户头像将被置为系统默认头像");
		}
	}

	addmedals();

	ctspecialgroup("{$source_tablepre}usergroups", 'common_usergroup');		//转特殊用户组
	$sqlfilenum = 0;
	$extrastr = '';
}
$sqlfilenum ++;
$extrastr = '&sqlfilenum='.$sqlfilenum;

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

if($start < $maxid){
	$converted = 1;
}
$query = $xc->db['source']->query("SELECT m.*, m.uid as muid, md.*, g.gid, g.gptype, g.grouptitle FROM {$source_tablepre}members m LEFT JOIN {$source_tablepre}memberdata md ON md.uid=m.uid LEFT JOIN {$source_tablepre}usergroups g ON g.gid=m.groupid WHERE m.uid>=$start AND m.uid<$start+$rpp") or dexit();
while($pwmember = $xc->db['source']->fetch_array($query)) {
	$pwmember =	daddslashes(array_change_key_case($pwmember));

	$uid = $pwmember['muid'];
	if(empty($uid)){
		continue;
	}
	$username = trim(stripslashes($pwmember['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($pwmember['password']);
		$password		=	$ucpw['password'];
		$email			=	cutstr($pwmember['email'], 32);
		$myid			=	'';
		$myidkey		=	'';

		$t_r			=	gexplode($pwmember['onlineip'], 0, '|');
		$regip			=	substr($t_r, -1) == '*' ? trim(substr($t_r, 0, -1)) : $t_r;

		$regdate		=	$pwmember['regdate'];
		$lastloginip	=	$regip;

		$t_lt	=	gexplode($pwmember['onlineip'], 1, '|');
		$lastlogintime	=	$t_lt ? $t_lt : ($pwmember['lastvisit'] ? $pwmember['lastvisit'] : $timestamp);

		$salt			=	$ucpw['salt'];
		$secques        =    '';

		//uc memberfields
		$blacklist		=	'';

		//pre_common_member
		//标记头像
		$avatarinfo	= explode('|', $pwmember['icon']);
		($avatarinfo[1] !=2 && $avatarinfo[1] != 3) && $avatarinfo[1] = 1;		//1：系统头像  2：远程图片  3：上传头像
		if($avatarinfo[1] == 2 || $avatarinfo[1] == 3) {
			//customavatars仅作为一个标志位
			$avatar	=	$avatarinfo[1] == 2 ? $avatarinfo[0] : 'customavatars/data/attachment/forum/pw/upload/'.$avatarinfo[0];
		} elseif($avatarinfo[1] == 1 && $avatarinfo[0]) {
			$avatar	=	'images/data/avatar/face/'.$avatarinfo[0];
		} else {
			$avatar	=	'';
		}

		$status				=	0;
		$emailstatus		=	$pwmember['yz'] == 1 ? 1 : 0;
		$avatarstatus		=	0;
		$videophotostatus	=	0;
		$adminid			=	$pwmember['groupid'];
		$groupid			=	$pwmember['memberid'];
		if($pwmember['groupid'] == 3) {
			$adminid	=	$groupid = 1;
		} elseif($pwmember['groupid'] == 4) {
			$adminid	=	$groupid = 2;
		} elseif($pwmember['groupid'] == 5) {
			$adminid	=	$groupid = 3;
		} elseif($pwmember['groupid'] == 6) {
			$adminid	=	-1;
			$groupid	=	4;
		} elseif($pwmember['groupid'] == 7) {
			$adminid	=	0;
			$groupid	=	8;
		} elseif($pwmember['gptype'] == 'special' || $pwmember['gptype'] == 'system') {
			$adminid	=	0;
			$groupid	=	$pwmember['groupid'] + 9;
		} else {
			$adminid	=	0;
			$groupid	=	$pwmember['memberid'] + 9;
		}
		$groupexpiry	=	0;
		$extgroupids	=	'';
		$credits		=	(isset($pwmember['rvrc']) && $pwmember['rvrc']) ? floor($pwmember['rvrc']/10) : 0;		//威望
		$notifysound	=	0;
		$timeoffset		=	empty($pwmember['timedf']) ? '8': $pwmember['timedf'];
		$newpm			=	$pwmember['newpm'];
		$newprompt		=	0;
		$accessmasks	=	0;
		$allowadmincp	=	$adminid == 1 ? 1 : 0;

		//pre_common_member_count
		$extcredits1		=	(isset($pwmember['rvrc']) && $pwmember['rvrc']) ? floor($pwmember['rvrc']/10) : 0;
		$extcredits2		=	$pwmember['money'];
		$extcredits3		=	$pwmember['credit'];
		$extcredits4		=	$pwmember['currency'];
		$extcredits5		=	0;
		$extcredits6		=	0;
		$extcredits7		=	0;
		$extcredits8		=	0;
		$friends			=	0;
		$oltime				=	round($pwmember['onlinetime']/3600);
		//..._count 中广播统计	add 2012-04-13 18:09:16
		$feeds 			= 	$xc->db['source']->result_first("SELECT COUNT(*) FROM {$source_tablepre}weibo_content WHERE uid='$uid'");
		$follower		=	$pwmember['fans'];
		$following		=	$pwmember['follows'];
		$newfollower		=	$pwmember['newfans'];


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

		//pre_common_member_field_forum
		$publishfeed		=	0;
		$customshow			=	26;
		$customstatus		=	$pwmember['honor'] ? addslashes(cutstr(htmlspecialchars(trim(@strip_tags($pwmember['honor']))), 30)) : '';

		//勋章
		$medalcomma = $medals = $mdvar = '';
		if ($pwmember['medals']) {
			$medalarray	=	explode(',', $pwmember['medals']);
			foreach($medalarray as $mdvar) {
				$medals	.=	$medalcomma.(intval($mdvar) + 10);
				$medalcomma	= "\t";
				$medalvalue[] = '('.$uid.','.(intval($mdvar) + 10).')';
			}
		}
		if($medalvalue) {
			$xc->db['discuz']->query("INSERT INTO {$discuz_tablepre}common_member_medal VALUES ".implode(',', $medalvalue));
		}

		$sightml			=	parsesign($pwmember['signature']);
		$groupterms			=	'';
		$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			=	$pwmember['realname'];
		$gender				=	$pwmember['gender'];
		$birthyear			=	gexplode($pwmember['bday'], 0);
		$birthmonth			=	gexplode($pwmember['bday'], 1);
		$birthday			=	gexplode($pwmember['bday'], 2);
		$constellation		=	'';
		$zodiac				=	'';
		$telephone			=	'';
		$mobile				=	$pwmember['authmobile'];
		$idcardtype			=	'';
		$idcard				=	'';
		$address			=	$pwmember['location'];
		$zipcode			=	'';
		$nationality		=	'';
		$birthprovince		=	'';
		$birthcity			=	'';
		$resideprovince		=	'';
		$residecity			=	'';
		$residedist			=	'';
		$residecommunity	=	'';
		$residesuite		=	'';
		$graduateschool		=	'';
		$company			=	'';
		$education			=	'';
		$occupation			=	'';
		$position			=	'';
		$revenue			=	'';
		$affectivestatus	=	'';
		$lookingfor			=	'';
		$bloodtype			=	'';
		$height				=	'';
		$weight				=	'';
		$alipay				=	'';
		$icq				=	$pwmember['icq'];
		$qq					=	$pwmember['oicq'];
		$yahoo				=	$pwmember['yahoo'];
		$msn				=	$pwmember['msn'];
		$taobao				=	$pwmember['aliww'];
		$site				=	$pwmember['site'];
		$bio				=	$pwmember['introduce'];
		$interest			=	'';
		$field1				=	'';
		$field2				=	'';
		$field3				=	'';
		$field4				=	'';
		$field5				=	'';
		$field6				=	'';
		$field7				=	'';
		$field8				=	'';

		//pre_common_member_status
		//$uid				=	'';
		//$regip			=	'';
		$lastip				=	$lastloginip;
		$lastvisit			=	$lastlogintime;
		$lastactivity		=	$lastlogintime;
		$lastpost			=	$pwmember['lastpost'];
		$lastsendmail		=	0;
		$pokes				=	0;
		$pendingfriends		=	0;
		$invisible			=	0;
		$buyercredit		=	0;
		$sellercredit		=	0;

		//pre_common_onlinetime
		$thismonth			=	0;
		$total				=	round($pwmember['onlinetime']/60);
		$lastupdate			=	time();
		$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','avatar');		//avatar字段为转换头像增加的字段
		$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','feeds', 'follower', 'following');
		$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);

		$fields9 	= array('uid','thismonth','total','lastupdate');
		$query9		= getinsertsql("{$discuz_tablepre}common_onlinetime", $fields9);

		if($xc->config['info']['convert_type'] == 'file') {
			writesql('ucenter_members', $sqlfilenum, $query1);
			writesql('ucenter_memberfields', $sqlfilenum, $query2);
			writesql('common_member', $sqlfilenum, $query3);
			writesql('common_member_count', $sqlfilenum, $query4);
			writesql('common_member_field_forum', $sqlfilenum, $query5);
			writesql('common_member_field_home', $sqlfilenum, $query6);
			writesql('common_member_profile', $sqlfilenum, $query7);
			writesql('common_member_status', $sqlfilenum, $query8);
			writesql('common_onlinetime', $sqlfilenum, $query9);
		} else {
			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)) {
											$xc->db['discuz']->query($query9);
											$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='.($pwmember['uid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
?>
