<?php
/*
pre_common_member
pre_common_member_count
pre_common_member_field_forum
pre_common_member_field_home
pre_common_member_profile
pre_common_member_status
*/

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');
	validid('userid', 'user');

	//在 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("添加用户头像字段失败，转换完成后，所有用户头像将被置为系统默认头像");
		}
	}

	if($discuz_charset == 'utf8'){
		include './data/'.$child.'/exts_utf8.php';
	} else {
		include './data/'.$child.'/exts_gbk.php';
	}
	$extcredits = addslashes(serialize($extcreditsarray));
	$xc->db['discuz']->query("UPDATE {$discuz_tablepre}common_setting SET svalue='$extcredits' WHERE skey='extcredits'");//基本积分
	ctspecialgroup(); //特殊用户组
	addbbcode(); //增加&修改 bbcode
}

$query = $xc->db['source']->query("SELECT usergroupid FROM {$source_tablepre}UserGroups WHERE parentgid =2");
$specialgids = array();
while ($g = $xc->db['source']->fetch_array($query)) {
	$specialgids[] = $g['usergroupid'];
}//特殊组gids
$query = $xc->db['source']->query("SELECT max(UserID) FROM {$source_tablepre}user");
$maxid = $xc->db['source']->fetch_array($query);
$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}user WHERE (UserID BETWEEN $start AND $end)") or dexit("检索数据表 '{$source_tablepre}user' 错误！<br>请将用户表 '{$source_tablepre}user' 字段类型为 nText 的改为 Text 类型。", 'mssql');

while ($dvuser = $xc->db['source']->fetch_array($query)) {

	$dvuser =	array_change_key_case($dvuser);
	$uid = $dvuser['userid'];
	$username = trim($dvuser['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 {
		$dvuser = daddslashes($dvuser);

		//uc members
		$ucpw		=	convertucpw($dvuser['userpassword']);
		$password	=	$ucpw['password'];
		$email		=	cutstr($dvuser['email'], 32);
		$myid		=	'';
		$myidkey	=	'';
		$regip		=	$dvuser['userlastip'];
		$regdate	=	timetounix($dvuser['joindate']);
		$lastloginip	=	$dvuser['userlastip'];
		$lastlogintime	=	timetounix($dvuser['joindate']);
		$salt		=	$ucpw['salt'];
		$secques	=    '';

		//uc memberfields
		$blacklist		=	'';

		//pre_common_member
		//$uid				=	'';
		//$email				=	'';
		//$username			=	'';
		//$password			=	'';
		$status				=	0;
		$emailstatus		=	0;
		$avatarstatus		=	0;
		$videophotostatus	=	0;

		if(in_array($dvuser['usergroupid'], array('1', '2', '3'))) {
			$adminid = $groupid = $dvuser['usergroupid'];
		} elseif($dvuser['lockuser'] == 2) {
			$adminid	=	-1;
			$groupid	=	4;
		} elseif($dvuser['lockuser'] == 1) {
			$adminid	=	-1;
			$groupid	=	5;
		}elseif(in_array($dvuser['usergroupid'], $specialgids)) {
			$adminid	=	-1;
			$groupid	=	$dvuser['usergroupid'] + 15;
		} else {
    		$adminid	=	0;
    		$groupid	=	10;
		}

		$groupexpiry	=	0;
		$extgroupids	=	'';
		$regdate		=	timetounix($dvuser['joindate'] ? $dvuser['joindate'] : ($dvuser['adddate'] ? $dvuser['adddate'] : $timestamp));
		$credits		=	$dvuser['userep'];
		$notifysound	=	0;
		$timeoffset		=	'9999';
		$newpm			=	0;
		$newprompt		=	0;
		$accessmasks	=	0;
		$allowadmincp	=	0;

		//pre_common_member_count
		//$uid				=	'';
		$extcredits1	=	$dvuser['userep'];
		$extcredits2	=	$dvuser['userpower'];
		$extcredits3	=	$dvuser['userwealth'];
		$extcredits4	=	$dvuser['usercp'];
		$extcredits5	=	$dvuser['userticket'];
		$extcredits6	=	$dvuser['usermoney'];
		$extcredits7	=	0;
		$extcredits8	=	0;
		$friends		=	0;
		$posts			=	isset($dvuser['userpost']) ? $dvuser['userpost'] : 0;
		$threads		=	0;
		$digestposts	=	0;
		$doings			=	0;
		$blogs			=	0;
		$albums			=	0;
		$sharings		=	0;
		$attachsize		=	0;
		$views			=	0;

		//pre_common_member_field_forum
		//$uid			=	'';
		$publishfeed	=	0;
		$customshow		=	26;
		$customstatus	=	'';
		$medals			=	@strip_tags($dvuser['usersign']);

		//转换头像
		$biasArray = explode('|', $dvuser['userface']);
		$dvuser['userface'] = count($biasArray) == 2 ? $biasArray[1] : $dvuser['userface'];
		if($dvuser['userface'] && $dvuser['userface'] != 'http://') {
			$dvuser['userface'] = trim($dvuser['userface']);
			if(substr($dvuser['userface'], 0, 7) == 'http://') {	//远程图片转为 空
				$avatar = '';
			} else {
				if(strtolower(substr($dvuser['userface'], 0, 16)) == 'images/userface/') {	//系统默认头像转为 空
					$avatar = '';
				} elseif(strtolower(substr($dvuser['userface'], 0, 11)) == 'uploadface/') {	//上传的图片转换头像
					$avatar = 'customavatars/data/attachment/forum/dvbbsface/'.substr($dvuser['userface'], 11);
				}
			}
		} else {
			$avatar = '';
		}

		$sightml		=	@strip_tags($dvuser['usersign']);
		$groupterms		=	'';
		$authstr		=	'';
		$groups			=	'';
		$attentiongroup	=	'';

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

		//pre_common_member_profile
		//$uid				=	'';
		$gender				=	$dvuser['usersex']==1 ? 1 : 2;
		$birthyear			=	0;
		$birthmonth			=	0;
		$birthday			=	0;
		$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				=	'';

		$userfinfo	=	explode('|||',$dvuser['userim']);
		$icq				=	'';
		$qq					=	parseqqicq($userfinfo[1]);
		$yahoo				=	'';
		$msn				=	$userfinfo[3] ? htmlspecialchars($userfinfo[3]) : '';
		$taobao				=	'';
		$site				=	parsesite($userfinfo[0]);
		$bio				=	'';
		$interest			=	'';
		$field1				=	'';
		$field2				=	'';
		$field3				=	'';
		$field4				=	'';
		$field5				=	'';
		$field6				=	'';
		$field7				=	'';
		$field8				=	'';

		//pre_common_member_status
		//$uid				=	'';
		//$regip			=	'';
		$lastip				=	$dvuser['userlastip'];
		$lastvisit			=	$dvuser['lastlogin'] ? timetounix($dvuser['lastlogin']) : $regdate;
		$lastactivity		=	$lastvisit;
		$lastpost			=	$lastvisit;
		$lastsendmail		=	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','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');
		$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','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($dvuser['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 {
										$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_members WHERE uid='$uid' LIMIT 1;");
										$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_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 {
									$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_members WHERE uid='$uid' LIMIT 1;");
									$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_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 {
								$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_members WHERE uid='$uid' LIMIT 1;");
								$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_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 {
							$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_members WHERE uid='$uid' LIMIT 1;");
							$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_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 {
						$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_members WHERE uid='$uid' LIMIT 1;");
						$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_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 {
					$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_members WHERE uid='$uid' LIMIT 1;");
					$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_memberfields WHERE uid='$uid' LIMIT 1;");
					
					reportlog("转换X用户信息主表出错 uid=$uid, username=$username");
				}
			} else {
				$db['uc']->query("DELETE FROM {$discuz_tablepre}uc_members WHERE uid='$uid' LIMIT 1;");
				
				reportlog("转换 UCenter 用户资料表出错 uid=$uid, username=$username");
			}
		} else {
			reportlog("转换 UCenter 用户主表出错 uid=$uid, username=$username");
		}
	}
	$totalrows ++;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($dvuser['UserID']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
if( $end < $maxid ) {
	$converted = 1;
}
?>