<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace user\controller;
use \temmoku\user;
use \temmoku\db;
class myprofile extends user{
	/*修改我的资料*/
	public function edit_my_profile(){
		if('post'===$_POST['step']){
			hook_listen('user_edit_my_profile_begin');
			$user=db::select('*')->from(jab.'user')->where("uid= ".C('mydb.uid'))->row();
			//先进行判断是否是更换密码
			if($_POST['new_password']){
				if($_POST['new_password'] != $_POST['new_password1']){$this->err('重复密码不正确');}
				if(!$_POST['oldpassword']){$this->err('必须输入老密码，才可以更换新密码');}
				if(C('MYDB.password') != md5(MD5($_POST['oldpassword']).C('MYDB.rand'))){$this->err('你的老密码不正确');}
				$_POST['new_password']=md5(MD5($_POST['new_password']).C('MYDB.rand'));
			}
			//头像判断
			if($_POST['thumb'] && !is_url($_POST['thumb'])){
				$path=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$_POST['thumb'];
				if(is_file($path)){
					if($_POST['thumb'] !=$user['thumb']){
						if($user['thumb']){
							$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$user['thumb'];
							del_file($oldthumb);
						}
	    				$_POST['thumb']=UploadFiles_MOVE($_POST['thumb'],'icon');
					}
				}
			}elseif($user['thumb']){
				$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$user['thumb'];
				del_file($oldthumb);
			}
			//将性别整数化
			//正则匹配QQ号
			if($_POST['qq']){
				if(is_qq($_POST['qq'])==false){
					$this->err('你输入的qq不合法，请检查后重新输入');
				}
			}
			//正则匹配手机号
			if($_POST['phone_number']){
				if(is_phone($_POST['phone_number'])==false){
					$this->err('你输入的手机号码不合法，请检查后重新输入');
				}
			}
			//正则匹配EMAIL
			if($_POST['email']){
				if(is_email($_POST['email'])==false){
					$this->err('你输入的Emai不合法，请检查后重新输入');
				}
			}
			//正则匹配身份证号码
			/* if($_POST['id_number']){
				if(is_id_number($_POST['id_number'])===false){
					$this->err('你输入的身份证号码不合法');
				}
			} */
			if(!$_POST['new_password']){
				$_POST['new_password']=C('MYDB.password');
			}
			$_POST['sex']=intval($_POST['sex']);
			$arr=[
				'password'=>$_POST['new_password'],
				'thumb'=>$_POST['thumb'],
				'qq'=>$_POST['qq'],
				'mobile'=>$_POST['mobile'],
				'address'=>$_POST['address'],
				'sex'=>$_POST['sex'],
				'email'=>$_POST['email'],
				'real_name'=>$_POST['real_name'],
				'id_number'=>$_POST['id_number'],
			];
			if(C('MYDB.mobile_rz_state')){
				unset($arr['mobile']);
			}
			if(C('MYDB.email_rz_state')){
				unset($arr['email']);
			}
			if(C('MYDB.id_rz_state')==1){
				unset($arr['real_name'],$arr['id_number']);
			}
			db::update(jab.'user')->cols($arr)->where('uid='.C('MYDB.uid'))->query();
			hook_listen('user_edit_my_profile_end');
			$this->Success('修改成功');
		}
		if(DEVICE != 'pc'){
			$this->assign('webname','我的资料');
		}
		$this->display(tpl('myprofile/edit_my_profile'));
	}
	/*身份认证*/
	public function certification(){
		$this->display();
	}
}
?>
