<?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_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'){
		require_once XCONVERT_ROOT.'./data/'.$child.'/extcredits_utf8.php';
	}else {
		require_once XCONVERT_ROOT.'./data/'.$child.'/extcredits_gbk.php';
	}

	$extcredits = addslashes(serialize($extcredits));
	$xc->db['discuz']->query("UPDATE {$discuz_tablepre}common_setting SET svalue = '$extcredits' WHERE skey = 'extcredits'");	//基本积分

	ctspecialgroup();
	addbbcode(); //增加/修改 bbcode

	$dv_versql = "SELECT Forum_Version FROM {$source_tablepre}Setup";
	$rs = $xc->db['source']->execute($dv_versql);
	!$rs->EOF && $dv_ver = $rs->fields[Forum_Version]->value;
	$rs->close();
}

$dv_ver = isset($dv_ver) ? $dv_ver : $_GET['dv_ver'];
$sql = "SELECT usergroupid FROM {$source_tablepre}usergroups WHERE parentgid =2";
$rs = $xc->db['source']->execute($sql);
$fieldarray = array('usergroupid');
$specialgids = $g = array();

while (!$rs->EOF) {
	foreach($fieldarray AS $field) {
		$g[$field] = $rs->fields[$field]->value;
	}
	$specialgids[] = $g['usergroupid'];
	$rs->movenext();
}
$rs->close();

$sql = "SELECT * FROM {$source_tablepre}user WHERE (userid BETWEEN $start AND $end)";
$rs = $xc->db['source']->execute($sql);
$fieldarray = array('userid', 'username', 'userpassword', 'useremail', 'userpost', 'usertopic', 'usersign', 'usersex', 'userface', 'userwidth', 'userheight', 'userim', 'joindate', 'lastlogin', 'userlogins', 'userviews', 'lockuser', 'userclass', 'usergroup', 'userwealth', 'userep', 'usercp', 'userpower', 'userdel', 'userisbest', 'usertitle', 'userbirthday', 'userquesion', 'useranswer', 'userlastip', 'userphoto', 'userfav', 'userinfo', 'usersetting', 'usergroupid', 'titlepic', 'userhidden', 'usermsg', 'ischallenge', 'usermobile', 'truepassword', 'usertoday', 'userisava', 'useravasetting', 'usermoney', 'userticket', 'followmsgid', 'vip_startime', 'vip_endtime', 'passport', 'fav_boards');
while (!$rs->EOF) {
	foreach($fieldarray AS $field) {
		$user[$field] = $rs->fields[$field]->value;
	}

	$uid = $user['userid'];
	$username = trim($user['username']);

	if(!$username || $username != htmlspecialchars(daddslashes($username))) {
		reportlog("非法用户名 <b><font color='red'>$username</font></b> 不能被转换，uid = $uid ；");
	} elseif(strlen($username) > 15) {
		reportlog("用户名 <b><font color='orange'>$username</font></b> 长度大于 15，不能被转换，uid = $uid ；");
	} elseif(getuid($username)) {
			reportlog("重复用户名 <b><font color='blue'>$username</font></b> 不能被转换，uid = $uid ；");
	} else {
		$user = daddslashes($user);

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

		//uc memberfields
		$blacklist		=	'';

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

		if(in_array($user['usergroupid'], array('1', '2', '3'))) {
			$adminid = $groupid = $user['usergroupid']; //管理组
		} elseif($user['lockuser'] == 2) {//屏蔽
			$adminid	=	-1;
			$groupid	=	4; //禁止发言
		} elseif($user['lockuser'] == 1) {//锁定
			$adminid	=	-1;
			$groupid	=	5; //禁止访问
		}elseif(in_array($user['usergroupid'], $specialgids)) { //上面已经查询出gids
			$adminid	=	-1;
			$groupid	=	$user['usergroupid'] + 15; //特殊用户组
		} else {
			$adminid	=	0;
			$groupid	=	10; //新手上路
		}

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

		//pre_common_member_count
		//$uid				=	'';
		$extcredits1	=	$user['userep'];		//经验值
		$extcredits2	=	$user['userpower'];		//威望
		$extcredits3	=	$user['userwealth'];	//金钱
		$extcredits4	=	$user['usercp'];		//魅力
		$extcredits5	=	$user['userticket'];	//点券
		$extcredits6	=	$user['usermoney'];		//金币
		$extcredits7	=	0;
		$extcredits8	=	0;
		$friends		=	0;
		$posts			=	isset($user['userpost']) ? $user['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			=	'';

		//转换头像
		$biasArray = explode('|', $user['userface']);
		$user['userface'] = count($biasArray) == 2 ? $biasArray[1] : $user['userface'];
		if($user['userface'] && $user['userface'] != 'http://') {
			$user['userface'] = trim($user['userface']);
			if(substr($user['userface'], 0, 7) == 'http://') {
				$avatar = '';
			} else {
				if(strtolower(substr($user['userface'], 0, 16)) == 'images/userface/') {
					$avatar = '';
				} elseif(strtolower(substr($user['userface'], 0, 11)) == 'uploadface/') {
					$avatar = 'customavatars/data/attachment/forum/dvbbsface/'.substr($user['userface'], 11);
				}
			}
			//$avatarwidth = $user['userwidth'] > 0 && $user['userwidth'] <= 120 ? $user['userwidth'] : 83;
			//$avatarheight = $user['userheight'] > 0 && $user['userheight'] <= 120 ? $user['userheight'] : 94;
		} else {
			$avatar = '';
			//$avatarwidth = 0;
			//$avatarheight = 0;
		}

		$sightml		=	parsesign($user['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			=	$user['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('|||',$user['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			=	$user['userlastip'];
		$lastvisit		=	$user['lastlogin'] ? timetounix($user['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");
		}
	}
	$converted = 1;
	$totalrows ++;
	$rs->movenext();
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($user['userid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);

}
$rs->close();
$end < $maxid ? $converted = 1 : '';
if($converted || $end < $maxid) {
	showmessage("正在处理第 $start - $end 行数据", 'index.php?action=convert&step='.$step.'&start='.($end + 1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&maxid='.$maxid.'&dv_ver='.$dv_ver);
} else {
	altertable('members', 'uid');
	altertable('memberfields', 'uid');
	altertable_uc('members', 'uid');
	altertable_uc('memberfields', 'uid');
}
?>