<?php
require('ucCheck.php');

Area::CheckIsOutSubmit();	//检测是否外部提交

$userSysArr = Cache::PhpFile('userSys');



switch ($mudi){
	case 'rev':
		rev();
		break;

	case 'revPageNum':
		RevPageNum();
		break;

}

$DB->Close();





// 修改
function rev(){
	global $DB,$userSysArr;

	$backURL	= OT::PostStr('backURL');
	$revType	= OT::PostStr('revType');

	// 获取数据
	switch ($revType){
		case 'info':
			$realname_is	= OT::PostInt('realname_is');
			$sex_is			= OT::PostInt('sex_is');
			$address_is		= OT::PostInt('address_is');
			$postCode_is	= OT::PostInt('postCode_is');
			$weixin_is		= OT::PostInt('weixin_is');
			$alipay_is		= OT::PostInt('alipay_is');
			$qq_is			= OT::PostInt('qq_is');
			$ww_is			= OT::PostInt('ww_is');
			$web_is			= OT::PostInt('web_is');
			$note_is		= OT::PostInt('note_is');
			$realname	= OT::PostRegExpStr('realname','sql');
			$sex		= OT::PostReplaceStr('sex','input');
			$address	= OT::PostReplaceStr('address','input');
			$postCode	= OT::PostReplaceStr('postCode','input');
			$weixin		= OT::PostReplaceStr('weixin','input');
			$alipay		= OT::PostReplaceStr('alipay','input');
			$qq			= str_replace('，',',',OT::PostReplaceStr('qq','input'));
			$ww			= OT::PostReplaceStr('ww','input');
			$web		= OT::PostReplaceStr('web','input');
			$note		= OT::PostReplaceStr('note','textarea');
			$pageNum	= OT::PostInt('pageNum');
				if ($pageNum > 100){ $pageNum = 100; }
			$pageWapNum	= OT::PostInt('pageWapNum');
				if ($pageWapNum > 100){ $pageWapNum = 100; }

			$itemTotal = $itemSucc = 0;
			if (strpos($userSysArr['US_userFieldStr'],'|昵称|') !== false){		// $realname_is == 1
				$itemTotal ++;
				if (strlen($realname) > 0){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|昵称必填|') !== false){
					if ($realname == ''){ JS::AlertBackEnd('昵称不能为空.'); }
				}
			}
			if (strpos($userSysArr['US_userFieldStr'],'|性别|') !== false){		// $sex_is == 1
				$itemTotal ++;
				if (strlen($sex) > 0){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|性别必填|') !== false){
					if ($sex == ''){ JS::AlertBackEnd('请选择性别.'); }
				}
			}
			if (strpos($userSysArr['US_userFieldStr'],'|收货地址|') !== false){	// $address_is == 1
				$itemTotal ++;
				if (strlen($address) > 3){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|收货地址必填|') !== false){
					if ($address == ''){ JS::AlertBackEnd('收货地址不能为空.'); }
				}
			}
			if (strpos($userSysArr['US_userFieldStr'],'|邮编|') !== false){		// $postCode_is == 1
				$itemTotal ++;
				if (strlen($postCode) > 3){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|邮编必填|') !== false){
					if ($postCode == ''){ JS::AlertBackEnd('邮编不能为空.'); }
				}
			}
			if (strpos($userSysArr['US_userFieldStr'],'|微信|') !== false){		// $weixin_is == 1
				$itemTotal ++;
				if (strlen($weixin) > 3){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|微信必填|') !== false){
					if ($weixin == ''){ JS::AlertBackEnd('微信不能为空.'); }
				}
			}
			if (strpos($userSysArr['US_userFieldStr'],'|支付宝|') !== false){	// $alipay_is == 1
				$itemTotal ++;
				if (strlen($alipay) > 3){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|支付宝必填|') !== false){
					if ($alipay == ''){ JS::AlertBackEnd('支付宝不能为空.'); }
				}
			}
			if (strpos($userSysArr['US_userFieldStr'],'|QQ|') !== false){		// $qq_is == 1
				$itemTotal ++;
				if (strlen($qq) > 4){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|QQ必填|') !== false){
					if ($qq == ''){ JS::AlertBackEnd('QQ不能为空.'); }
				}
			}
			if (strpos($userSysArr['US_userFieldStr'],'|旺旺|') !== false){		// $ww_is == 1
				$itemTotal ++;
				if (strlen($ww) > 3){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|旺旺必填|') !== false){
					if ($ww == ''){ JS::AlertBackEnd('旺旺不能为空.'); }
				}
			}
			if (strpos($userSysArr['US_userFieldStr'],'|个人主页|') !== false){	// $web_is == 1
				$itemTotal ++;
				if (strlen($web) > 7){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|个人主页必填|') !== false){
					if ($web == ''){ JS::AlertBackEnd('个人主页不能为空.'); }
				}
			}
			if (strpos($userSysArr['US_userFieldStr'],'|备注|') !== false){		// $note_is == 1
				$itemTotal ++;
				if (strlen($note) > 3){ $itemSucc ++; }
				if (strpos($userSysArr['US_userFieldStr'],'|备注必填|') !== false){
					if ($note == ''){ JS::AlertBackEnd('备注不能为空.'); }
				}
			}
			break;

		case 'username':
			$newUsername	= OT::PostStr('newUsername');
			$newUsername2	= OT::PostStr('newUsername2');
			if ($newUsername == ''){
				JS::AlertBackEnd('新用户名不能为空.');
			}
			if ($newUsername != $newUsername2){
				JS::AlertBackEnd('两次用户名不一致.');
			}
			break;

		case 'password':
			$userpwdOld		= OT::PostStr('userpwdOld');
			$userpwd		= OT::PostStr('userpwd');
			$userpwd2		= OT::PostStr('userpwd2');
			if ($userpwdOld == '' || $userpwd == ''){
				JS::AlertBackEnd('密码不能为空.');
			}
			if ($userpwd != $userpwd2){
				JS::AlertBackEnd('两次密码不一致.');
			}
			break;

		case 'mail':
			$isAuthMail		= OT::PostInt('isAuthMail');
			$mailOldCode	= OT::PostStr('mailOldCode');
			$mail			= OT::PostStr('mail');
			$mailCode		= OT::PostStr('mailCode');
			$mailPwd		= OT::PostStr('mailPwd');
			if (! Is::Mail($mail)){
				JS::AlertBackEnd('邮箱格式错误（'. $mail .'）.');
			}
			break;

		case 'phone':
			$isAuthPhone	= OT::PostInt('isAuthPhone');
			$phoneOldCode	= OT::PostStr('phoneOldCode');
			$phone			= OT::PostStr('phone');
			$phoneCode		= OT::PostStr('phoneCode');
			$phonePwd		= OT::PostStr('phonePwd');
			if (! Is::Phone($phone)){
				JS::AlertBackEnd('手机号格式错误（'. $phone .'）.');
			}
			break;

		case 'question':
			$answerOld	= OT::PostStr('answerOld');
			$question	= OT::PostStr('question');
			$answer		= OT::PostStr('answer');
			if ($question != Str::Filter($question,'sql')){
				JS::AlertBackEnd('密保问题含特殊符号。');
			}
			if (strlen($question)<2 || strlen($question)>50){
				JS::AlertBackEnd('密保问题长度不在2~50字节范围内。');
			}
			break;

		case 'shiming':
			$cardType	= OT::PostStr('cardType');
			$cardUser	= OT::PostStr('cardUser');
			$cardID		= OT::PostStr('cardID');
			$cardImg	= OT::PostRegExpStr('cardImg','abcnum+url');
			$cardDealType = OT::PostStr('cardDealType');
			if ($cardType == '' || $cardUser == '' || $cardID == '' || $cardDealType == ''){
				JS::AlertBackEnd('实名认证 类型、姓名/公司名、身份证号/税号 都不能为空。');
			}
			if ($cardDealType == 'weixin'){
				JS::AlertBackEnd('【微信】扫码实名无需提交，通过点击【获取二维码】按钮获取二维码图，用微信扫码实名认证。');
			}elseif ($cardDealType == 'alipay'){
				JS::AlertBackEnd('【支付宝】扫码实名无需提交，通过点击【获取二维码】按钮获取二维码图，用支付宝扫码实名认证。');
			}
			break;

		case 'app':
			$payImgOld		= OT::PostRegExpStr('payImgOld','abcnum+url');
			$dashangImg1Old	= OT::PostRegExpStr('dashangImg1Old','abcnum+url');
			$dashangImg2Old	= OT::PostRegExpStr('dashangImg2Old','abcnum+url');
			$dashangImg3Old	= OT::PostRegExpStr('dashangImg3Old','abcnum+url');
			$payImg			= OT::PostRegExpStr('payImg','abcnum+url');
			$payName		= OT::PostRegExpStr('payName','sql');
			$dashangImg1	= OT::PostRegExpStr('dashangImg1','abcnum+url');
			$dashangImg2	= OT::PostRegExpStr('dashangImg2','abcnum+url');
			$dashangImg3	= OT::PostRegExpStr('dashangImg3','abcnum+url');
			$face			= OT::PostInt('face');
			if (strlen($dashangImg1 . $dashangImg2 . $dashangImg3) < 5 && $face == 0){
				JS::AlertBackEnd('内容不能都为空，或者没有修改项。');
			}
			break;

		default :
			JS::AlertBackEnd('操作不明确.');
			break;

	}


	// 获取用户信息
	$userInfoStr	= Users::Get();
	$userArr		= explode("\t",$userInfoStr);
		if (count($userArr)>=4){ $userID = intval($userArr[0]); }

	$addiFieldStr='';
	if (AppDashang::Jud()){ $addiFieldStr=',UE_dashangImg1,UE_dashangImg2,UE_dashangImg3'; }

	$urow = $DB->GetRow('select UE_ID,UE_regType,UE_apiStr,UE_authStr,UE_cardState,UE_username,UE_userpwd,UE_userKey,UE_mail,UE_question,UE_answer,UE_answerKey,UE_realname,UE_sex,UE_address,UE_postCode,UE_phone,UE_qq,UE_web,UE_note,UE_faceMode,UE_face,UE_score1,UE_score2,UE_score3'. $addiFieldStr .' from '. OT_dbPref .'users where UE_ID='. $userID);
		$UE_ID			= $urow['UE_ID'];
		$UE_regType		= $urow['UE_regType'];
		$UE_apiStr		= $urow['UE_apiStr'];
		$UE_authStr		= $urow['UE_authStr'] .'';
		$UE_cardState	= $urow['UE_cardState'];
		$UE_username	= $urow['UE_username'];
		$UE_userpwd		= $urow['UE_userpwd'];
		$UE_userKey		= $urow['UE_userKey'];
		$UE_mail		= $urow['UE_mail'];
		$UE_phone		= $urow['UE_phone'];
		$UE_question	= $urow['UE_question'];
		$UE_answer		= $urow['UE_answer'];
		$UE_answerKey	= $urow['UE_answerKey'];
		$UE_realname	= $urow['UE_realname'];
		$scoreStr = $urow['UE_score1'] .'|'. $urow['UE_score2'] .'|'. $urow['UE_score3'];

	// 数据处理
	$alertAddi = '';
	$typeCN = '';
	$record = array();
	switch ($revType){
		case 'info':
			$typeCN = '修改资料';
			if ($realname_is == 1 && strpos($userSysArr['US_userFieldStr'],'|昵称|') !== false){
				$record['UE_realname']	= $realname;
			}
			if ($sex_is == 1 && strpos($userSysArr['US_userFieldStr'],'|性别|') !== false){
				$record['UE_sex']		= $sex;
			}
			if ($address_is == 1 && strpos($userSysArr['US_userFieldStr'],'|收货地址|') !== false){
				$record['UE_address']	= $address;
			}
			if ($postCode_is == 1 && strpos($userSysArr['US_userFieldStr'],'|邮编|') !== false){
				$record['UE_postCode']	= $postCode;
			}
			if ($weixin_is == 1 && strpos($userSysArr['US_userFieldStr'],'|微信|') !== false){
				$record['UE_weixin']	= $weixin;
			}
			if ($alipay_is == 1 && strpos($userSysArr['US_userFieldStr'],'|支付宝|') !== false){
				$record['UE_alipay']	= $alipay;
			}
			if ($qq_is == 1 && strpos($userSysArr['US_userFieldStr'],'|QQ|') !== false){
				$record['UE_qq']		= $qq;
			}
			if ($ww_is == 1 && strpos($userSysArr['US_userFieldStr'],'|旺旺|') !== false){
				$record['UE_ww']		= $ww;
			}
			if ($web_is == 1 && strpos($userSysArr['US_userFieldStr'],'|个人主页|') !== false){
				$record['UE_web']		= $web;
			}
			if ($note_is == 1 && strpos($userSysArr['US_userFieldStr'],'|备注|') !== false){
				$record['UE_note']		= $note;
			}
			$record['UE_pageNum']		= $pageNum;
			// $record['UE_pageWapNum']	= $pageWapNum;
			if ($itemTotal == $itemSucc){
				if (strpos($UE_authStr,'|完善资料|') === false){
					$record['UE_authStr']	= $UE_authStr .'|完善资料|';
				}
			}else{
				if (strpos($UE_authStr,'|完善资料|') !== false){
					$record['UE_authStr']	= str_replace('|完善资料|','',$UE_authStr);
				}
			}
			$alertAddi = '，完善资料'. $itemSucc .'/'. $itemTotal;
			break;

		case 'username':
			$typeCN = '修改用户名';
			Users::CheckUsername('check',$newUsername);

			if ($userSysArr['US_regBadWord'] != ''){
				$newUsername = strtolower($newUsername);
				$badWordArr = explode('|',strtolower($userSysArr['US_regBadWord']));
				foreach ($badWordArr as $str){
					if (strlen($str)>0 && strpos($newUsername,$str)!==false){
						JS::AlertBackEnd('用户名中含禁止注册关键词（'. $str .'）');
					}
				}
			}
			$record['UE_username']	= $newUsername;
			break;

		case 'password':
			$typeCN = '修改密码';
			if (strpos($UE_apiStr,'|web:::') !== false && strpos($userSysArr['US_userFieldStr'],'|密码直接修改|') === false){
				if (md5(md5($userpwdOld) . $UE_userKey) != $UE_userpwd){
					JS::AlertBackEnd('原密码错误！');
				}
			}else{
				$record['UE_apiStr']	= '|web:::1|'. $UE_apiStr;
			}
			$newUserKey = OT::RndChar(5);
			$newUserpwd = md5(md5($userpwd) . $newUserKey);
			$record['UE_userpwd']	= $newUserpwd;
			$record['UE_userKey']	= $newUserKey;
			break;

		case 'mail':
			$typeCN = '修改邮箱';
			if ($userSysArr['US_isAuthMail'] == 1 && AppMail::Jud()){
				if (strpos($UE_authStr,'|邮箱|') === false && $isAuthMail == 1){
					$resArr = AppMail::CheckMailCode('rev', $mail, $mailCode, $userID);
					if (! $resArr['res']){
						JS::AlertBackEnd($resArr['note']);
					}
				}elseif (strpos($UE_authStr,'|邮箱|') !== false && $userSysArr['US_isLockMail'] == 1){
					JS::AlertBackEnd('邮箱验证通过后禁止用户修改，如要修改请联系网站管理员处理。');
				}else{
					$resArr = AppMail::CheckMailCode('check', $UE_mail, $mailOldCode, $userID);
					if (! $resArr['res']){
						JS::AlertBackEnd($resArr['note']);
					}

					$resArr = AppMail::CheckMailCode('rev', $mail, $mailCode, $userID);
					if (! $resArr['res']){
						JS::AlertBackEnd($resArr['note']);
					}
				}
				if (strpos($UE_authStr,'|邮箱|') === false){
					$record['UE_authStr'] = $UE_authStr .'|邮箱|';
				}
			}else{
				if (strpos($UE_authStr,'|邮箱|') !== false){
					$record['UE_authStr'] = str_replace('|邮箱|', '', $UE_authStr);
				}
			}
			if (md5(md5($mailPwd) . $UE_userKey) != $UE_userpwd){
				JS::AlertBackEnd('登录密码错误！');
			}
			$record['UE_mail']	= $mail;
			break;

		case 'phone':
			$typeCN = '修改手机号';
			if ($userSysArr['US_isAuthPhone'] == 1 && AppPhone::Jud()){
				if (strpos($UE_authStr,'|手机|') === false && $isAuthPhone == 1){
					$resArr = AppPhone::CheckPhoneCode('rev', $phone, $phoneCode, $userID);
					if (! $resArr['res']){
						JS::AlertBackEnd($resArr['note']);
					}
				}elseif (strpos($UE_authStr,'|手机|') !== false && $userSysArr['US_isLockPhone'] == 1){
					JS::AlertBackEnd('手机号验证通过后禁止用户修改，如要修改请联系网站管理员处理。');
				}else{
					$resArr = AppPhone::CheckPhoneCode('check', $UE_phone, $phoneOldCode, $userID);
					if (! $resArr['res']){
						JS::AlertBackEnd($resArr['note']);
					}

					$resArr = AppPhone::CheckPhoneCode('rev', $phone, $phoneCode, $userID);
					if (! $resArr['res']){
						JS::AlertBackEnd($resArr['note']);
					}
				}
				if (strpos($UE_authStr,'|手机|') === false){
					$record['UE_authStr'] = $UE_authStr .'|手机|';
				}
			}else{
				if (strpos($UE_authStr,'|手机|') !== false){
					$record['UE_authStr'] = str_replace('|手机|', '', $UE_authStr);
				}
			}
			if (md5(md5($phonePwd) . $UE_userKey) != $UE_userpwd){
				JS::AlertBackEnd('登录密码错误！');
			}
			$record['UE_phone']	= $phone;
			break;

		case 'question':
			$typeCN = '修改密码保护';
			$answerOld	= md5(md5($answerOld) . $UE_answerKey);
			if (strlen($UE_question)>0 && $answerOld!=$UE_answer){
				JS::AlertBackEnd('旧答案错误！');
			}

			$newAnswerKey = OT::RndChar(5);
			$record['UE_question']		= $question;
			$record['UE_answer']		= md5(md5($answer) . $newAnswerKey);
			$record['UE_answerKey']		= $newAnswerKey;
			break;

		case 'shiming':
			$typeCN = '实名认证';
			if ($UE_cardState == 1){
				JS::AlertBackEnd('实名认证已审核通过，无需再提交。');
			}elseif ($UE_cardState == 2){
				JS::AlertBackEnd('实名认证正在审核中，请耐心等待，如需加急请联系管理员。');
			}

			if (strpos($userSysArr['US_shimingTypeStr'],'[idCardOnly]') !== false){
				$chkRow = $DB->GetRow('select UE_cardState from '. OT_dbPref .'users where UE_cardState in (1,11,21) and UE_cardID='. $DB->ForStr($cardID) .' limit 1');
				if ($chkRow){
					// 1手动审核通过  11微信实名  21支付宝实名 
					JS::AlertBackEnd('该【'. $cardType .'】信息已被其他账号实名认证过，如有问题，请联系网站管理员给你处理。');
				}
				unset($chkRow);
			}

			$record['UE_cardType']	= $cardType;
			$record['UE_cardUser']	= $cardUser;
			$record['UE_cardID']	= $cardID;
			$record['UE_cardImg']	= $cardImg;
			$record['UE_cardState']	= 2;
			if (strpos($UE_authStr,'|实名审核|') === false){
				$record['UE_authStr'] = $UE_authStr .'|实名审核|';
			}
			break;

		case 'app':
			$typeCN = '修改其他（插件）';
			/* $dashangImg1Old = $dashangImg2Old = $dashangImg3Old = '';
			if (AppDashang::Jud()){
				$dashangImg1Old = $urow['UE_dashangImg1'];
				$dashangImg2Old = $urow['UE_dashangImg2'];
				$dashangImg3Old = $urow['UE_dashangImg3'];
			} */
			if (AppPayCode::Jud()){
				$record['UE_payImg']	= $payImg;
				$record['UE_payName']	= $payName;
			}
			if (strlen($dashangImg1 . $dashangImg2 . $dashangImg3) >= 5){
				$record['UE_dashangImg1']	= $dashangImg1;
				$record['UE_dashangImg2']	= $dashangImg2;
				$record['UE_dashangImg3']	= $dashangImg3;
			}
			if ($face > 0){
				$record['UE_faceMode']	= 3;
				$record['UE_face']		= $face;
			}
			break;

	}
	$UE_userpwd		= null;
	$UE_userKey		= null;
	$UE_answer		= null;
	$UE_answerKey	= null;

	$judRes = $DB->UpdateParam('users', $record, 'UE_ID='. $userID);
		if (! $judRes){
			JS::AlertBackEnd('更新数据失败，请重试下.');
		}

	if ($revType == 'shiming'){
		Users::AddLog($UE_ID, $UE_username, '提交实名认证【'. $cardUser .'】，状态[待审核]', '类型：'. $cardType .'；姓名：'. $cardUser .'；身份证号：'. $cardID .'');
	}else{
		Users::AddLog($UE_ID, $UE_username, $typeCN . $alertAddi);
	}

	if ($revType == 'info'){
		Users::SetCookies('usercall',$realname);

	}elseif ($revType == 'username'){
		Users::SetCookies('username',$newUsername);

	}elseif ($revType == 'password'){
		Users::Update($UE_ID,$UE_username,$newUserpwd,$UE_mail,$UE_realname,$scoreStr);

	}elseif ($revType == 'shiming'){
		AreaApp::MessageSend('userShiming', '『审核』会员“'. $UE_username .'”提交实名认证【'. $cardUser .'】，状态[待审核]，需要管理员审核。[WAP端]【'. TimeDate::Get() .'】');

	}elseif ($revType == 'app'){
		/* if (strlen($dashangImg1Old) > 5 && Is::HttpUrl($dashangImg1Old) == false && $dashangImg1Old != $dashangImg1){
			File::Del(OT_ROOT . UsersFileDir . $dashangImg1Old);
			File::Del(OT_ROOT . UsersFileDir .'thumb_'. $dashangImg1Old);
		}
		if (strlen($dashangImg2Old) > 5 && Is::HttpUrl($dashangImg2Old) == false && $dashangImg2Old != $dashangImg2){
			File::Del(OT_ROOT . UsersFileDir . $dashangImg2Old);
			File::Del(OT_ROOT . UsersFileDir .'thumb_'. $dashangImg2Old);
		}
		if (strlen($dashangImg3Old) > 5 && Is::HttpUrl($dashangImg3Old) == false && $dashangImg3Old != $dashangImg3){
			File::Del(OT_ROOT . UsersFileDir . $dashangImg3Old);
			File::Del(OT_ROOT . UsersFileDir .'thumb_'. $dashangImg3Old);
		} */

	}

	if ($revType == 'question' && $UE_question != ''){
		$alertMode = '建立密保';
	}else{
		$alertMode = '修改';
	}

	JS::AlertHrefEnd($alertMode .'成功'. $alertAddi .'.','usersCenter.php?mudi=revInfo&revType='. $revType);
}


// 修改每页条数
function RevPageNum(){
	global $DB;

	$pageNum	= OT::GetInt('pageNum');
	$backUrl	= OT::GetStr('backUrl');
		if ($pageNum < 10){ $pageNum = 10; }
		elseif ($pageNum > 100){ $pageNum = 100; }
		if (strlen($backUrl) < 5){
			JS::AlertBackEnd('回调地址不能为空.');
		}

	$userRow = Users::Open('get','','',$judUserErr);
	if ((! $userRow) || $judUserErr != ''){
		JS::AlertBackEnd('会员未登录，请先登录再操作.');
	}

	$judRes = $DB->UpdateParam('users', array('UE_pageNum' => $pageNum), 'UE_ID='. $userRow['UE_ID']);
	JS::HrefEnd($backUrl);
}

?>