<?php
defined('IN_YUNYECMS') or exit('No permission.');
core::load_class('base',false);
core::load_fun('content');
core::load_fun('tree');
class member extends base {
    public $curmodel;
	function __construct() {
	 $this->db = core::load_model('content_model');
	 parent::__construct();
	 }
	//首页
	 public function index(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr=Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
		$cfg=$this->cfg;
		$lang=$this->lang; 
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index"))
			);		
		$userid=usafestr(yunyecms_strdecode(ugetcookie("userid")));
		$member=$this->member;
		islogin();
        include tpl('index','member');
	}
	
	 public function register(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr=Lan('member_register')."-".Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
        $seo['description']="{$seostr}-{$seo['description']}";
		$breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('member_register'),'url'=>url("member/member/register"))
			);
		$token=make_rand_letternum(16);
		$_SESSION["token"]=$token;
		$tid=0;
	
		$tmem=$_REQUEST["tmember"]; 
		$tmem=yunyecms_strdecode(usafestr($tmem));
		if($tmem&&is_numeric($tmem)){
             $tuser=getmember($tmem);
			 if($tuser){
				 $tid=$tuser["id"];  
			 }else{
				 $tid=false;   
			 }
		 }
		$cfg=$this->cfg;
		$lang=$this->lang; 
        include tpl('register','member');
	}
	
	
 function register_check() {
	    $_POST=yytrim($_POST);
		 if(empty($_POST['username'])){
		     messagebox(Lan('required_username'));		
			}else{
	        $username=usafestr($_POST['username']);
		    $data['username'] = $username;
			 if(check_mobile($username)){
				$mobile=$username; 
			    $data['phone'] = $mobile;
			    $data['mobile'] = $mobile; 
			 }
		 }
	   if(empty($_POST['pwd'])){
		    messagebox(Lan('required_password'));		
		   }else{
		    $pwd=trim($_POST['pwd']);
	        $pwd=yypwd($_POST['pwd']);
		    $data['pwd'] = $pwd;
		 }
	   if(!empty($_POST['email'])){
		     $email=usafestr($_POST['email']);
			 if(!is_email($email)){
		       messagebox(Lan('email_error'));		
			 }	
		      $data['email'] = $email;
			}
	 
	 	  $token=trim($_POST["token"]);
		  if(empty($token)||$token!=$_SESSION['token']){
				messagebox(Lan('illegal_submit'));		
		  }	 
		  $cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `username`='$username' or `mobile`='$username'");
		  if($cntcheckone>=1){
				 messagebox(Lan('username_exist'));	
		   }		 
	    $regtime=time();
		$data['score'] = 0;
		$data['status'] = 1;
		$data['addtime'] = $regtime;
		$data['rnd'] = make_rand_letternum(16);
	    $groupid_default=0;
		$groupdefault=$this->db->find("select * from `#yunyecms_membergroup`  where isdefault=1");
	    if(!empty($groupdefault)){
			$groupid_default=$groupdefault["id"];
		}
		$data['groupid']=$groupid_default;
        //保存当前数据对象
          $insertid = $this->db->insert($data,'member',true);
		  usetcookie("userid",$insertid);					  
		  usetcookie("username",$username);	
		  usetcookie("groupid",$groupid_default);					  
	      if($mobile)  usetcookie("mobile",$mobile);			
		  usetcookie("loginrnd",$data['rnd']);					  
		  usetcookie("logintruetime",$regtime);
		  usetcookie("logintime",$regtime);
        if ($insertid !== false){
				$msgcontent['mod']=ROUTE_M;
				$msgcontent['act']=ROUTE_A;
				$msgcontent['ctrl']=ROUTE_C;
				$msgcontent['table']="member";
				$msgcontent['id']=$insertid;
			    $msgcontent=serialize($msgcontent);
				$msgtitle=Lan('member_register')."：".Lan('username')."-{$username}，ID-{$insertid}";
			    $remsg=savemsg($msgtitle,2,$msgcontent,$insertid);
		        messagebox(Lan('register_success'),url('member/member/index'));		
          }else {
            //失败提示
		     messagebox(Lan('register_error'),$_SERVER['HTTP_REFERER'],'error');		
		    exit;
          }
      }

       //用户注册
	  function register_check_ajax() {
			$_POST=yytrim($_POST);
			$curtime=time();
			if(!empty($_POST['regtype'])){
				$regtype=$_POST['regtype'];
				if($regtype==1){
					if(empty($_POST['mobile'])){
						output_json(array("code"=>101,"msg"=>"手机号码错误！"));				
					}else{
						$mobile=usafestr($_POST['mobile']);
						$data["username"]="ms".$curtime;
						$data['mobile'] = $mobile;
					  if(check_mobile($mobile)){
						$data['phone'] = $mobile;
					  }
				  }
				  $cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `username`='$mobile' or `mobile`='$mobile'");
				  if($cntcheckone>=1){
					  output_json(array("code"=>105,"msg"=>"注册失败：手机号码已存在！"));				
				   }
				}elseif($regtype==2){
					if(empty($_POST['email'])){ 
						output_json(array("code"=>101,"msg"=>"邮箱地址错误！"));				
					}else{
						$email=usafestr($_POST['email']);
						$data["username"]="es".$curtime;
						$data['email'] = $email;
				  }
				  $cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `email`='$email'");
				  if($cntcheckone>=1){
					output_json(array("code"=>105,"msg"=>"注册失败：邮箱地址已存在！"));				
				   }			
			   }elseif($regtype==3){
				if(empty($_POST['username'])){
					output_json(array("code"=>101,"msg"=>"用户名错误！"));				
				}else{
					$username=usafestr($_POST['username']);
					$data["username"]=$username;
			    }
				$cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `username`='$username'");
				if($cntcheckone>=1){
					output_json(array("code"=>105,"msg"=>"注册失败：手机号码已存在！"));				
				 }
			  }
			  if($regtype!=3){
						$code=trim($_POST['code']);
						if(empty($_SESSION['regcode'])){
						output_json(array("code"=>103,"msg"=>"验证码已过期，请重新获取"));				
						}
						if($code!=$_SESSION['regcode']){
						output_json(array("code"=>104,"msg"=>"注册失败：验证码错误"));				
						}
			  }

		   }else{
			output_json(array("code"=>111,"msg"=>"注册失败：注册类型不能为空！"));				
		   }

		
	    if(empty($_POST['pwd'])){
	      	output_json(array("code"=>102,"msg"=>"密码错误！"));				
		 }else{
		  $pwd=trim($_POST['pwd']);
		  $pwd=yypwd($_POST['pwd']);
		  $data['pwd'] = $pwd;
	     }
		 $token=trim($_POST["token"]);
		 if(empty($token)||$token!=$_SESSION['token']){
			    output_json(array("code"=>102,"msg"=>"注册失败：安全码不正确！"));				
		 }	 
	
		
	  $regtime=time();
	  $data['score'] = 0;
	  $data['status'] = 1;
	  $data['addtime'] = $regtime;
	  $data['regtype'] = $regtype;
	  $data['rnd'] = make_rand_letternum(16);
	  $groupid_default=0;
	  $groupdefault=$this->db->find("select * from `#yunyecms_membergroup`  where isdefault=1");
	  if(!empty($groupdefault)){
		  $groupid_default=$groupdefault["id"];
	  }
	  $data['groupid']=$groupid_default;	 
	 //保存当前数据对象
		$insertid = $this->db->insert($data,'member',true);
		usetcookie("userid",yunyecms_strencode($insertid));					  
		usetcookie("username",$data["username"]);
		usetcookie("groupid",$groupid_default);					  
		if($mobile)  usetcookie("mobile",$mobile); 			
		if($email)  usetcookie("email",$email); 			
		usetcookie("loginrnd",$data['rnd']);					  
		usetcookie("logintruetime",$regtime);
		usetcookie("logintime",$regtime);
	    if($insertid !== false){
			  $msgcontent['mod']=ROUTE_M;
			  $msgcontent['act']=ROUTE_A;
			  $msgcontent['ctrl']=ROUTE_C;
			  $msgcontent['table']="member";
			  $msgcontent['id']=$insertid;
			  $msgcontent=serialize($msgcontent);
			  $msgtitle=Lan('member_register')."：".Lan('username')."-{$mobile}，ID-{$insertid}";
			  $remsg=savemsg($msgtitle,2,$msgcontent,$insertid);
			   if($_POST['tid']&&$_POST['tid']!=$insertid) {
				     $syndata['tid'] = $_POST['tid'];
				     $this->db->update($syndata,array('id'=>$insertid),"member"); 
			   }
			  output_json(array("code"=>100,"msg"=>"注册成功！"));				
		   }else {
			  output_json(array("code"=>199,"msg"=>"注册失败：出错了，请重试"));				
	     }
	}			

	 public function login(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr=Lan('member_login')."-".Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
        $seo['description']="{$seostr}-{$seo['description']}";
		$breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('member_login'),'url'=>url("member/member/login"))
			);
		$token=make_rand_letternum(16);
		$_SESSION["token"]=$token;
		$cfg=$this->cfg;
		$lang=$this->lang; 
        include tpl('login','member');
	}
	
 function logincheck() {
	     $_POST=yytrim($_POST);
		 if(empty($_POST['username'])){
		     messagebox(Lan('required_username'));		
			}else{
	        $username=usafestr($_POST['username']);
		 }
		if(empty($_POST['pwd'])){
		    messagebox(Lan('required_password'));		
		   }else{
	        $pwd=yypwd($_POST['pwd']);
		 }
	 	  $token=trim($_POST["token"]);
		  if(empty($token)||$token!=$_SESSION['token']){
				messagebox(Lan('token_error'));		
		  }	 
		  $cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `username`='$username' or `mobile`='$username'");
		  if(empty($cntcheckone)){
			   messagebox(Lan('user_notexist'));
		   }	
		  $curmember=$this->db->find("select * from `#yunyecms_member` where  (`username`='$username' or `mobile`='$username') and pwd='$pwd'");
	      if(!empty($curmember)){
			  if($curmember["status"]){
					  $logintime=time();
					  $userid=$curmember["id"];
				      $loginrnd=make_rand_letternum(16);
			  		  $strsql2="update `#yunyecms_member` set rnd='$loginrnd'  where id=$userid ";
					  $this->db->query($strsql2);	  
					  usetcookie("userid",yunyecms_strencode($userid));					  
					  usetcookie("username",$curmember["username"]);					  
					  usetcookie("groupid",$curmember["groupid"]);
					  usetcookie("mobile",$curmember["mobile"]);					  
					  usetcookie("loginrnd",$loginrnd);					  
					  usetcookie("logintruetime",$logintime);
					  usetcookie("logintime",$logintime);
					  $strsql2="update `#yunyecms_member` set lastlogintime=".$logintime."  where id=".$userid." ";
					  $this->db->query($strsql2);	
					  messagebox(Lan('login_success'),url('member/member/index'));	 
			  }else{
                 messagebox(Lan('account_nocheck'));
			  }
		  }else{
              messagebox(Lan('password_error'));
		  }
      }

	  function logincheck_ajax() {
		$_POST=yytrim($_POST);
		if(empty($_POST['username'])){
			   output_json(array("code"=>101,"msg"=>"用户名不能为空！"));
		   }else{
		   $username=usafestr($_POST['username']);
		}
	   if(empty($_POST['pwd'])){
		   output_json(array("code"=>103,"msg"=>"密码不能为空！"));
		  }else{
		   $pwd=yypwd($_POST['pwd']);
		}
		$token=trim($_POST["token"]);
		 if(empty($token)||$token!=$_SESSION['token']){
			  output_json(array("code"=>103,"msg"=>"非法提交"));
		 }
		 if(!$this->db->GetCount("select count(*) as cnt from `#yunyecms_member` where `username`= '{$username}'  or `mobile`= '{$username}'  or `email`= '{$username}'")){
			output_json(array("code"=>102,"msg"=>"该用户不存在"));
	      }
		 $curmember=$this->db->find("select * from `#yunyecms_member` where  (`username`='$username' or `mobile`='$username'  or `email`= '{$username}' )  and pwd='$pwd'");
		 if(!empty($curmember)){
			 if($curmember["status"]){
					 $logintime=time();
					 $userid=$curmember["id"];
					 $loginrnd=make_rand_letternum(16);
					 $strsql2="update `#yunyecms_member` set rnd='$loginrnd'  where id=$userid ";
					 $this->db->query($strsql2);	  
					 usetcookie("userid",yunyecms_strencode($userid));					  
					 usetcookie("username",$curmember["username"]);					  
					 usetcookie("groupid",$curmember["groupid"]);
					 usetcookie("mobile",$curmember["mobile"]);					  
					 usetcookie("email",$curmember["email"]);	  
					 usetcookie("loginrnd",$loginrnd);					  
					 usetcookie("token",$token);					  
					 usetcookie("logintime",$logintime);
					 $strsql2="update `#yunyecms_member` set lastlogintime=".$logintime."  where id=".$userid." ";
					 $this->db->query($strsql2);	
					 output_json(array("code"=>100,"msg"=>"用户登录成功！"));
			 }else{
				output_json(array("code"=>199,"msg"=>"该用户还未通过审核！"));
			  }
		 }else{
			  output_json(array("code"=>199,"msg"=>"密码错误！"));
		     }
	   }	
   
	
	function logout(){
	  usetcookie("userid",null);					  
	  usetcookie("username",null);					  
	  usetcookie("mobile",null);					  
	  usetcookie("loginrnd",null);					  
	  usetcookie("logintruetime",null);
	  usetcookie("logintime",null);
	  usetcookie("groupid",null);
	  messagebox(Lan('memner_logout'),url('content/index/index'));	 
	}
	
	
 public function myinfo(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr=Lan('member_info')."-".Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
		$cfg=$this->cfg;
		$lang=$this->lang; 
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('member_info'),'url'=>url("member/member/myinfo"))
			);	
	    islogin();
		$userid=usafestr(yunyecms_strdecode(ugetcookie("userid")));
		$member=$this->member;
	    if(isset($_POST["yyact"])){
					  $token=trim($_POST["token"]);
					  if(empty($token)||$token!=$_SESSION['token']){
							messagebox(Lan('illegal_submit')	);	
					  }	
			          if(!empty($_POST["id"])){
						     $id=$_POST["id"];
							 if($id!=$member['id']){
							   messagebox(Lan('illegal_submit')	);		
						     }
					  }else{
							messagebox(Lan('user_notexist'));		
					  }
				      //$pic=uhtmlspecialchars(trim($_POST["pic"]));
				      $_POST=yytrim($_POST);
					  $data["name"]=usafestr($_POST["name"]);
					  if($_POST["email"]) $data["email"]=usafestr($_POST["email"]);
					  if($_POST["phone"]) $data["phone"]=usafestr($_POST["phone"]);
					  if($_POST["mobile"])$data["mobile"]=usafestr($_POST["mobile"]);
					  $data["address"]=usafestr($_POST["address"]);
					  $data["sex"]=usafestr($_POST["sex"]);
					  $data["company"]=usafestr($_POST["company"]);
					  $data["position"]=usafestr($_POST["position"]);
					   if($_POST["yyact"]=="edit"){
							   $id=$_POST["id"];
							   if(!$this->check_exist($id)){
									messagebox(Lan('user_notexist'),url('member/member/index'),"warn");
							   }
							   $retres=$this->db->update($data,"id={$id}","member");
							   if($retres){
										messagebox(Lan('member_info_ok'),url('member/member/index'),"success");
								 }else{
										messagebox(Lan('member_info_error'),url('member/member/index'),"error");
								 }
					   }		  
		  }else{
			$token=ugetcookie("loginrnd");
			$_SESSION['token']=$token;
		}
		include tpl('myinfo','member');
	 }
	
	
 public function pwd(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr=Lan('password_modify')."-".Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
		$cfg=$this->cfg;
		$lang=$this->lang; 
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('password_modify'),'url'=>url("member/member/pwd"))
			);	
	    islogin();
		$userid=usafestr(yunyecms_strdecode(ugetcookie("userid")));
		$member=$this->member;
	    if(isset($_POST["yyact"])){
					  $token=trim($_POST["token"]);
					  if(empty($token)||$token!=ugetcookie("loginrnd")){
							messagebox(Lan('illegal_submit')	);		
					  }	
			          if(!empty($_POST["id"])){
						     $id=$_POST["id"];
							 if($id!=$member['id']){
							   messagebox(Lan('illegal_submit')	);	
						     }
					  }else{
							messagebox(Lan('member_notexist'));
					  }
				      $_POST=yytrim($_POST);
					 $pwd = $_POST['pwd'];
					 $oldpwd = $_POST['oldpwd'];
					 $oldpwd=yypwd($oldpwd);	 
					 if(empty($pwd)){
						   messagebox(Lan('newpassword_empty'));
						}
					 if(empty($oldpwd)){
						   messagebox(Lan('required_oldpassword'));
						  }	
					 if($oldpwd!=$member["pwd"]){
						   messagebox(Lan('error_oldpassword'));
						  }	
					 $pwd=yypwd($pwd);	 
					   $data["pwd"]=$pwd;
					   if($_POST["yyact"]=="edit"){
							   $id=$_POST["id"];
							   if(!$this->check_exist($id)){
									messagebox(Lan('member_notexist'),url('member/member/index'),"warn");
							   }
							   $retres=$this->db->update($data,"id={$id}","member");
							   if($retres){
										messagebox(Lan('ok_password_modify'),url('member/member/index'),"success");
								 }else{
										messagebox(Lan('error_password_modify'),url('member/member/index'),"error");
								 }
					  }			  
		  }else{
			$token=ugetcookie("loginrnd");
		}
		include tpl('pwd','member');
	 }

     //找回密码
	 public function findpwd(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr="找回密码";
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
        $seo['description']="{$seostr}-{$seo['description']}";
		$breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>"找回密码",'url'=>url("member/member/findpwd"))
			);
		$token=make_rand_letternum(16);
		$_SESSION["token"]=$token;
		$cfg=$this->cfg;
		$lang=$this->lang; 
        include tpl('findpwd','member');
	}

	//快捷登录
	 public function quick_login(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr="手机/邮箱登录";
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
        $seo['description']="{$seostr}-{$seo['description']}";
		$breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>"手机登录",'url'=>url("member/member/findpwd"))
			);
		$token=make_rand_letternum(16);
		$_SESSION["token"]=$token;
		$cfg=$this->cfg;
		$lang=$this->lang; 
        include tpl('quick_login','member');
	}	
	
    //设置密码
   function setpwd() {
	      $_POST=yytrim($_POST);
		  if(!empty($_POST['regtype'])){ 
			$regtype=$_POST['regtype'];
			if($regtype==1){
				if(empty($_POST['mobile'])){
					output_json(array("code"=>101,"msg"=>"手机号码错误！"));				
				}else{
					$mobile=usafestr($_POST['mobile']);
			    }
			  $cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `username`='$mobile' or `mobile`='$mobile'");
			  if($cntcheckone<=0){
				  output_json(array("code"=>102,"msg"=>"找回密码失败：手机号码不存在！"));				
			   }
			}elseif($regtype==2){
				if(empty($_POST['email'])){ 
					output_json(array("code"=>101,"msg"=>"邮箱地址错误！"));				
				}else{
					$email=usafestr($_POST['email']);
			  }
			  $cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `email`='$email'");
			  if($cntcheckone<=0){
				output_json(array("code"=>102,"msg"=>"找回密码失败：邮箱地址不存在！"));				
			   }			
		   }
		}
	     if(empty($_POST['pwd'])){
			output_json(array("code"=>103,"msg"=>"密码不能为空！"));
		   }else{
		    $pwd=trim($_POST['pwd']);
	        $pwd=yypwd($_POST['pwd']);
		    $data['pwd'] = $pwd;
		 }
	 	  $token=trim($_POST["token"]);
		  if(empty($token)||$token!=$_SESSION['token']){
			   output_json(array("code"=>104,"msg"=>"非法提交！"));
		  } 
		 
		  $code=trim($_POST['code']);
	 	  if(empty($_SESSION['findpwdcode'])){
			output_json(array("code"=>106,"msg"=>"请输入验证码！"));
	       }
	 	  if($code!=$_SESSION['findpwdcode']){
			output_json(array("code"=>107,"msg"=>"验证码错误，请重新输入！"));
	       }	 
		   if($regtype==1){
			 $retres=$this->db->update($data,"mobile='{$mobile}'","member");
		   }elseif($regtype==2){
		 	 $retres=$this->db->update($data,"email='{$email}'","member");
		   }
	      //保存当前数据对象
            if($retres !== false){ 
			   output_json(array("code"=>100,"msg"=>"恭喜您，密码找回成功！"));
             }else {
             //失败提示
			 output_json(array("code"=>199,"msg"=>"密码找回失败！")); 
          }
      }		


	//找回密码成功
	 public function findpwdok(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr="找回密码";
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
        $seo['description']="{$seostr}-{$seo['description']}";
		$breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>"找回密码",'url'=>url("member/member/findpwd"))
			);
		$cfg=$this->cfg;
		$lang=$this->lang; 
        include tpl('findpwdok','member');
	}	

	//修改密码 
	function editpwd() {
		$_POST=yytrim($_POST);
		if(empty($_POST['mobile'])){
			echo "phonenull";
			exit;
		  }else{
			$mobile=usafestr(trim($_POST['mobile']));
		 }
	   if(empty($_POST['pwd'])){
		   echo "pwdnull";
		   exit;	
		 }else{
		  $pwd=trim($_POST['pwd']);
		  $pwd=yypwd($_POST['pwd']);
		  $data['pwd'] = $pwd;
	   }
		$token=trim($_POST["token"]);
		if(empty($token)||$token!=$_SESSION['token']){
			  // echo "token_error";
			  // exit;
		} 
		$cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `username`='$mobile' or `mobile`='$mobile'");
		if($cntcheckone<1){
			   echo "fail_phone_not_exist";
			   exit;
		 }
		$code=trim($_POST['code']);
		 if(empty($_SESSION['editpwdcode'])){
		  echo "fail_code_overtime";
		  exit;
		 }
		 if($code!=$_SESSION['editpwdcode']){
		  echo "fail_verification_code";
		  exit;
		 }	 
		$retres=$this->db->update($data,"member","username='{$mobile}'");
		//保存当前数据对象
		if($retres !== false){
			  echo "true";
			  exit;
		   }else {
		   //失败提示
		   echo "false";
		   exit;
		 }
	}		
  
	//密码修改成功页面
   public function editpwdok(){
	  $seo['title']=$this->lang["seotitle"];
	  $seo['keywords']=$this->lang["seokey"];
	  $seo['description']=$this->lang["seodesc"];
	  $seostr="找回密码";
	  $seo['title']="{$seostr}-{$seo['title']}";
	  $seo['keywords']="{$seostr}-{$seo['keywords']}";
	  $seo['description']="{$seostr}-{$seo['description']}";
	  $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
			'1'=>array('title'=>"找回密码",'url'=>url("member/member/findpwd"))
		  );
	  $cfg=$this->cfg;
	  $lang=$this->lang; 
	  include tpl('editpwdok','member');
    }	

	 //自定义表单
	public function customform() {
		$seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$cfg=$this->cfg;
		$lang=$this->lang; 
		$cat=$this->cat; 
		$seostr="{$cat['title']}-".Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>$cat['title'],'url'=>url("member/member/customform",array("catid"=>$cat['id'])))
			);	
	    islogin();
		$userid=usafestr(yunyecms_strdecode(ugetcookie("userid")));
		$member=$this->member;
		if(!empty($_REQUEST['catid'])){
		    $catid = intval(usafestr($_REQUEST['catid']));
		    $modelid=getmodelid($catid);
		}
		if(empty($modelid)){
			 messagebox(Lan('model_notexist'),"back","warn");
		}else{
			$curmodel=getmodel($modelid);
		    $tablename="m_".$curmodel['tablename'];
		 }	
		 $modelfields=$this->db->select("select * from `#yunyecms_modelfields`  where modelid={$curmodel['modelid']}  and isdisplay=1 ");
		 $pagesize=20;
		 $sqlquery="select * from `#yunyecms_{$tablename}`  ";
		 $where=" where userid={$userid}  ";
		 $sqlcnt=" select count(*) from `#yunyecms_{$tablename}` ";
		 $order=" order by `addtime` desc ";
		  if(isset($_REQUEST)){
		   if(!empty($_REQUEST["searchkey"])){
		        $searchkey=usafestr(trim($_REQUEST["searchkey"]));
		        $where=$where." and ( `title`  like '%{$searchkey}%' )";
			  }
		 }
		 $pagearr=$this->db->pagelist($sqlcnt,$sqlquery,$where,$order,$pagesize);
		 if($pagearr["count"]!=0){
			 $list=$pagearr["query"];
			 foreach($list as $key=>$var){
				  $modelarr=$this->db->find("select modelid  from `#yunyecms_category` where `id`= {$var['catid']}");
				  if($modelarr){
				     $list[$key]["modelid"]=$modelarr["modelid"];
					 if(!empty($var["userid"])) {
				         $list[$key]["user"]=$this->db->getbyid($var["userid"],"member");
					 }
				  }
			 }
			 $page=$pagearr["page"];
		 }
		include tpl('customform','member');
	  }	
	

 //自定义表单信息删除
  public function customformdelete() { 
	    $cfg=$this->cfg;
		$lang=$this->lang;
		$cat=$this->cat;
	    islogin();
		$userid=usafestr(yunyecms_strdecode(ugetcookie("userid")));
		$member=$this->member;
		if(!empty($_REQUEST['catid'])){
		    $catid = intval(usafestr($_REQUEST['catid']));
		    $modelid=getmodelid($catid);
		}
		if(empty($modelid)){
			 messagebox(Lan('model_notexist'),"back","warn");
		}else{
			$curmodel=getmodel($modelid);
		    $tablename="m_".$curmodel['tablename'];
		 }	
            $id = $_REQUEST["id"];
		    $id = intval(usafestr($_REQUEST['id']));
	         if(!is_numeric($id)){
				    messagebox(Lan('delete_error'),$_SERVER['HTTP_REFERER'],"warn");
			 }
		    if(!is_array($id)){
			  $id=compact('id');
			}
			$idarray=$id;
            if (isset($idarray)){
					   foreach($idarray as $key=>$var){
				 		if(!is_numeric($var)){
					        messagebox("错误的参数！",'back',"warn");			
					    }
				        $idarray[$key]=usafestr($var);
			           }
				 $idarray=implode(",",$idarray);
				 $retres =$this->db->delete("id in ({$idarray}) and userid={$userid} ",$tablename);
                if ($retres !== false) {
				    messagebox(Lan('delete_success'),$_SERVER['HTTP_REFERER'],"success");
                } else {
				    messagebox(Lan('delete_error'),$_SERVER['HTTP_REFERER'],"warn");
                }
            } else {
				   messagebox(Lan('delete_error'),$_SERVER['HTTP_REFERER'],"warn");
            }
    }		
	
	
	//快捷登录,如果账户不存在则新注册一个
	function quicklogin_check() {
		$_POST=yytrim($_POST);
		$curtime=time();
		if(!empty($_POST['regtype'])){ 
			$regtype=$_POST['regtype'];
			if($regtype==1){
				if(empty($_POST['mobile'])){
					output_json(array("code"=>101,"msg"=>"手机号码错误！"));				
				}else{
					  $mobile=usafestr($_POST['mobile']);
					  $data["username"]="ms".$curtime;
						$data['mobile'] = $mobile;
					  if(check_mobile($mobile)){
						$data['phone'] = $mobile;
					  }
			    }
			  $cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `username`='$mobile' or `mobile`='$mobile'");
			  if($cntcheckone<=0){
				 //output_json(array("code"=>102,"msg"=>"登录失败：手机号码不存在！"));				
			   }
			}elseif($regtype==2){
				if(empty($_POST['email'])){ 
					output_json(array("code"=>101,"msg"=>"邮箱地址错误！"));				
				}else{
					$email=usafestr($_POST['email']);
					$data['email'] = $email;
					$data["username"]="es".$curtime;
			   }  
			  $cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `email`='$email'");
			  if($cntcheckone<=0){
				//output_json(array("code"=>102,"msg"=>"登录失败：邮箱地址不存在！"));				
			   }			
		   }
		}
		 $token=trim($_POST["token"]);
		if(empty($token)||$token!=$_SESSION['token']){
			 output_json(array("code"=>104,"msg"=>"非法提交！"));
		} 
	   
		$code=trim($_POST['code']);
		 if(empty($_SESSION['logincode'])){
		  output_json(array("code"=>106,"msg"=>"请输入验证码！"));
		 }
		 if($code!=$_SESSION['logincode']){
		  output_json(array("code"=>107,"msg"=>"验证码错误，请重新输入！"));
		 }	 
		 if($regtype==1){
			$cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `username`='$mobile' or `mobile`='$mobile'");
			if($cntcheckone){
				$curmember=$this->db->find("select * from `#yunyecms_member` where  (`username`='$mobile' or `mobile`='$mobile')");
			}
		 }elseif($regtype==2){
			$cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_member` where  `email`='$email' ");
			if($cntcheckone){
				$curmember=$this->db->find("select * from `#yunyecms_member` where  `email`='$email' ");
			}
		 }
		 if($cntcheckone>=1){ 
						 if(!empty($curmember)){
						  if($curmember["status"]){
								$logintime=time();
								$userid=$curmember["id"];
								$loginrnd=make_rand_letternum(16);
								$strsql2="update `#yunyecms_member` set rnd='$loginrnd'  where id=$userid ";
								$this->db->query($strsql2);	  
								usetcookie("userid",yunyecms_strencode($userid));					  
								usetcookie("username",$curmember["username"]);					  
								usetcookie("email",$curmember["email"]);					  
								usetcookie("groupid",$curmember["groupid"]);
								usetcookie("mobile",$curmember["mobile"]);					  
								usetcookie("loginrnd",$loginrnd);					  
								usetcookie("logintruetime",$logintime);
								usetcookie("logintime",$logintime);
								$strsql2="update `#yunyecms_member` set lastlogintime=".$logintime."  where id=".$userid." ";
								$this->db->query($strsql2);	
								output_json(array("code"=>100,"msg"=>"用户登录成功！"));
						     }else{
							 output_json(array("code"=>199,"msg"=>"该用户还未通过审核！"));
						 }
					  }else{
						output_json(array("code"=>199,"msg"=>"密码错误！"));
				  }
		 }else{
		  $pwd=yypwd(make_rand_letternum(8));
		  $data['pwd'] = $pwd;
		  $regtime=time();
		  $data['score'] = 0;
		  $data['status'] = 1;
		  $data['addtime'] = $regtime;
		  $data['rnd'] = make_rand_letternum(16);
		  $groupid_default=0;
		  $groupdefault=$this->db->find("select * from `#yunyecms_membergroup`  where isdefault=1");
		  if(!empty($groupdefault)){
			  $groupid_default=$groupdefault["id"];
		  }
		  $data['groupid']=$groupid_default;	 
		   //保存当前数据对象
			$insertid = $this->db->insert($data,'member',true);
			usetcookie("userid",yunyecms_strencode($insertid));					  
			usetcookie("username",$data['username']);	
			usetcookie("groupid",$groupid_default);					  
			if($mobile)  usetcookie("mobile",$mobile);			
			if($email)  usetcookie("mobile",$email);			
			usetcookie("loginrnd",$data['rnd']);					  
			usetcookie("logintruetime",$regtime);
			usetcookie("logintime",$regtime);
			if($insertid !== false){
			  $msgcontent['mod']=ROUTE_M;
			  $msgcontent['act']=ROUTE_A;
			  $msgcontent['ctrl']=ROUTE_C;
			  $msgcontent['table']="member";
			  $msgcontent['id']=$insertid;
			  $msgcontent=serialize($msgcontent);
			  $msgtitle=Lan('member_register')."：".Lan('username')."-{$data['username']}，ID-{$insertid}";
			  $remsg=savemsg($msgtitle,2,$msgcontent,$insertid);
			  output_json(array("code"=>100,"msg"=>"用户注册并登录成功！"));
		    }else {
		      output_json(array("code"=>199,"msg"=>"登录失败！")); 
		   }
		}
	}


	//用户注册协议
	public function agreement(){
           $user_agreement=$this->cfg['user_agreement'];
		   include tpl('agreement','member');
	}

   //检测手机号码
	public function checkphone(){
		$mobile=usafestr($_POST['mobile']);
		$smstype=usafestr($_POST['smstype']);
		 if(empty($mobile)){
			  output_json(array("code"=>101,"msg"=>"手机号码为空"));	
		   }
		 if(!is_numeric($mobile)){
			  output_json(array("code"=>102,"msg"=>"手机号码错误"));	
			}
		 if($this->db->GetCount("select count(*) as cnt from `#yunyecms_member` where `mobile`= '{$mobile}'")){
			if($smstype==2||$smstype==3){
				output_json(array("code"=>100,"msg"=>"手机号码输入正确！"));
			   }else{
				output_json(array("code"=>199,"msg"=>"手机号码已存在，请重新修改"));	
			   }
		 }else{
			if($smstype==2||$smstype==3){
				output_json(array("code"=>199,"msg"=>"该手机号码已存在，请重新输入"));
			   }else{
				output_json(array("code"=>100,"msg"=>"手机号码不存在"));	
			}
		 }			 
     }	

   //检测邮箱地址
	 public function checkemail(){
		$email=usafestr($_POST['email']);
		$smstype=usafestr($_POST['smstype']);
	 	 if(empty($email)){
			  output_json(array("code"=>101,"msg"=>"邮箱地址为空"));	
		   }
		if($this->db->GetCount("select count(*) as cnt from `#yunyecms_member` where `email`= '{$email}'")){
			  if($smstype==2||$smstype==3){
				output_json(array("code"=>100,"msg"=>"邮箱地址输入正确！"));
			   }else{
				output_json(array("code"=>199,"msg"=>"邮箱地址已存在，请重新修改"));
			  }
		 }else{
			if($smstype==2||$smstype==3){
				output_json(array("code"=>199,"msg"=>"该邮箱地址已存在，请重新输入"));
			   }else{
				output_json(array("code"=>100,"msg"=>"邮箱地址不存在"));	
			}
		 }			 
     }

	 
   //检测用户名
   public function checkUname(){
	$username=usafestr($_POST['username']);
	if(empty($username)){ 
		  output_json(array("code"=>101,"msg"=>"用户名为空"));	
	   }
	if($this->db->GetCount("select count(*) as cnt from `#yunyecms_member` where `username`= '{$username}'")){
		  output_json(array("code"=>199,"msg"=>"该用户已存在，请重新输入"));
	 }else{
		 output_json(array("code"=>100,"msg"=>"用户名输入正确"));	
	 }			 
 }	

    //检测用户名
	public function checkLoginName(){
		$username=usafestr($_POST['username']);
		 if(empty($username)){ 
			  output_json(array("code"=>101,"msg"=>"用户名为空"));	
		   }
		 if($this->db->GetCount("select count(*) as cnt from `#yunyecms_member` where `username`= '{$username}'  or `mobile`= '{$username}'  or `email`= '{$username}'")){
			  output_json(array("code"=>100,"msg"=>"用户名输入正确"));
		 }else{
			 output_json(array("code"=>199,"msg"=>"该用户不存在"));	
		 }			 
	 }	
	
	private function check_exist($id) {
			 $id = trim($id);
			 if(empty($id)){
				 return false;
				 }else{
					if(!is_numeric($id)){
						  return false;
					 }
				  if ($this->db->find("select count(*) as cnt from `#yunyecms_member` where `id`= {$id}")){
					  return true;
				  }				 
			  }
		}		
   }


?>