<?php
# MetInfo Enterprise Content Management System 
# Copyright (C) MetInfo Co.,Ltd (http://www.metinfo.cn). All rights reserved. 

defined('IN_MET') or exit('No permission');

load::mod_class('user/class/userweb');

class profile extends userweb {

	protected $paraclass;
	protected $paralist;
	
	public function __construct() {
		global $_M;
		parent::__construct();
		$this->paraclass = load::sys_class('para', 'new');
		$this->paralist  = $this->paraclass->get_para_list(10);
	}	
	
	
	/*未激活账户重发邮件验证*/
	public function dovalid_email() {
		global $_M;	
		$valid = load::mod_class('user/class/valid','new');
		if ($valid->get_email($_M['user']['username'])) {
			echo $_M['word']['emailsuc'];
		} else { 
			echo $_M['word']['emailfail'];
		}
	}
	
	/*基本信息*/
	
	public function doindex() {
		global $_M;	
		if(!$_M['user']['valid']){
			$valid = $_M['config']['met_member_vecan'] == 1?'valid_email':'valid_admin';
			require_once $this->template('tem/'.$valid);
		}else{
			require_once $this->template('tem/profile_index');
		}
	}
	
	public function doinfosave() {
		global $_M;	
		$infos = $this->paraclass->form_para($_M['form'],10);
		$this->paraclass->update_para($_M['user']['id'], $infos, 10);
		$this->userclass->modify_head($_M['user']['id'], $_M['form']['head']);
		okinfo($_M['url']['profile'], $_M['word']['modifysuc']);
	}
	
	/*帐号安全*/
	public function dosafety() {
		global $_M;	
		if($_M['user']['email']){
			$emailtxt = $_M['user']['email'];
			$emailbut = $_M['word']['modify'];
			$emailclass = 'emailedit';
		}else{
			$emailtxt = $_M['word']['notbound'];
			$emailbut = $_M['word']['binding'];
			$emailclass = 'emailadd';
		}
		if($_M['user']['tel']){
			$teltxt = $_M['user']['tel'];
			$telbut = $_M['word']['modify'];
			$telclass = 'teledit';
		}else{
			$teltxt = $_M['word']['notbound'];
			$telbut = $_M['word']['binding'];
			$telclass = 'teladd';
		}
		if($_M['config']['met_member_vecan']==1&&$_M['user']['email']&&$_M['user']['email']==$_M['user']['username']){
			$emailbut = $_M['word']['accnotmodify'];
			$disabled = 'disabled';
		}
		require_once $this->template('tem/profile_safety');
	}
	
	/*邮箱绑定与修改*/
	public function doemailedit() {
		global $_M;
		if($_M['form']['p']){
			$auth = load::sys_class('auth', 'new');
			$email = $auth->decode($_M['form']['p']);
			if($email&&$email==$_M['user']['email']){
				if($_M['form']['email']){
					$valid = load::mod_class('user/class/valid','new');
					if ($valid->get_email($_M['form']['email'],'emailadd')) {
						okinfo($_M['url']['profile_safety'], $_M['word']['emailsuclink']);
					} else { 
						okinfo($_M['url']['profile_safety'], $_M['word']['emailfail']);
					}
				}else{
					require_once $this->template('tem/profile_emailedit');
				}
			}else{
				okinfo($_M['url']['profile_safety'], $_M['word']['emailvildtips2']);
			}
		}else{
			$valid = load::mod_class('user/class/valid','new');
			if ($valid->get_email($_M['user']['email'],'emailedit')) {
				echo $_M['word']['emailsuclink'];
			} else { 
				echo $_M['word']['emailfail'];
			}
		}
	}
	public function doemailok() {
		global $_M;
		$valid = true;
		if($this->userclass->get_user_by_email($_M['form']['email'])){
			$valid = false;
		}
		echo json_encode(array(
			'valid' => $valid
		));
	}
	public function dosafety_emailadd() {
		global $_M;
		if($_M['form']['p']){
			$auth = load::sys_class('auth', 'new');
			$email = $auth->decode($_M['form']['p']);
			if($email){
				if($this->userclass->editor_uesr_email($_M['user']['id'], $email)){
					okinfo($_M['url']['profile_safety'], $_M['word']['bindingok']);
				}else{
					okinfo($_M['url']['profile_safety'], $_M['word']['opfail']);
				}
			}else{
				okinfo($_M['url']['profile_safety'], $_M['word']['emailvildtips2']);
			}
		}else{
			if($this->userclass->get_user_by_email($_M['form']['email'])){
				die;
			}
			$valid = load::mod_class('user/class/valid','new');
			if ($valid->get_email($_M['form']['email'],'emailadd')) {
				okinfo($_M['url']['profile_safety'], $_M['word']['emailsuclink']);
			} else { 
				okinfo($_M['url']['profile_safety'], $_M['word']['emailfail']);
			}
		}
	}
	
	/*密码修改*/
	public function dopasssave() {
		global $_M;	
		if(md5($_M['form']['oldpassword'])==$_M['user']['password']){
			if($this->userclass->editor_uesr_password($_M['user']['id'],$_M['form']['password'])){
				okinfo($_M['url']['profile_safety'], $_M['word']['modifypasswordsuc']);
			}else{
				okinfo($_M['url']['profile_safety'], $_M['word']['opfail']);
			}
		}else{
			okinfo($_M['url']['profile_safety'], $_M['word']['lodpasswordfail']);
		}
	}
	
	/*手机绑定与修改*/
	public function dosafety_teledit() {
		global $_M;
		if($_M['form']['code']){
			$session = load::sys_class('session', 'new');
			if($_M['form']['code']!=$session->get("phonecode")){
				echo $_M['word']['membercode'];
				die;
			}
			if(time()>$session->get("phonetime")){
				echo $_M['word']['codetimeout'];
				die;
			}
			$session->del('phonecode');
			$session->del('phonetime');
			$session->del('phonetel');
			echo 'SUCCESS';
		}else{
			$valid = load::mod_class('user/class/valid','new');
			if ($valid->get_tel($_M['user']['tel'])) {
				echo 'SUCCESS';  
			} else {
				echo $_M['word']['Sendfrequent'];  
			}
		}
	}
	public function dosafety_teladd() {
		global $_M;
		$session = load::sys_class('session', 'new');
		if($_M['form']['code']!=$session->get("phonecode")){
			okinfo($_M['url']['profile_safety'], $_M['word']['membercode']);
		}
		if(time()>$session->get("phonetime")){
			okinfo($_M['url']['profile_safety'], $_M['word']['codetimeout']);
		}
		if($_M['form']['tel']!=$session->get("phonetel")){
			okinfo($_M['url']['profile_safety'], $_M['word']['telcheckfail']);
		}
		$session->del('phonecode');
		$session->del('phonetime');
		$session->del('phonetel');
		
		if($this->userclass->editor_uesr_tel($_M['user']['id'], $_M['form']['tel'])){
			okinfo($_M['url']['profile_safety'], $_M['word']['bindingok']);
		}else{
			okinfo($_M['url']['profile_safety'], $_M['word']['opfail']);
		}
		
	}
	public function dosafety_telvalid() {
		global $_M;
		if($this->userclass->get_user_by_tel($_M['form']['tel'])){
			echo $_M['word']['teluse'];
			die;
		}
		$valid = load::mod_class('user/class/valid','new');
		if ($valid->get_tel($_M['form']['tel'])) {
			echo 'SUCCESS';  
		} else {
			echo $_M['word']['Sendfrequent'];  
		}
	}
	public function dosafety_telok() {
		global $_M;
		$valid = true;
		if($this->userclass->get_user_by_tel($_M['form']['tel'])){
			$valid = false;
		}
		echo json_encode(array(
			'valid' => $valid
		));
	}
}

# This program is an open source system, commercial use, please consciously to purchase commercial license.
# Copyright (C) MetInfo Co., Ltd. (http://www.metinfo.cn). All rights reserved.
?>