<?php
/********************************************************************
 *                          DirCMS会员类
 *                        -------------------
 *   begin                : 2010-5-12
 *   update               : 2010/9/6
 *   copyright            : (C) 2009 DirCMS
 *   email                : uuturn@126.com
 *
 *   $Id: member.class.php,v 1.0.0 2010-5-12 M Exp $
 ********************************************************************/
class member
{
	public $msg;
	public $_userid=0;
	public $activateurl='';
	private $db;
	private $table;

	function __construct()
	{
		global $db;
		$this->db=$db;
		$this->table=DB_PRE.'member';
		$this->cache_table=DB_PRE.'member_cache';
		member_cache();
	}

	function member()
    {
    	$this->__construct();
    }

	function get($userid, $fields = '*')
	{
		$userid = intval($userid);
		return $this->db->fetch_one("SELECT {$fields} FROM `{$this->cache_table}` WHERE `{$this->cache_table}`.`id`=".$userid);
	}

	function get_userid($username)
	{
		if(!is_username($username)) return false;
		$sql = "SELECT id FROM `$this->cache_table` WHERE `$this->cache_table`.`username`='$username' LIMIT 1";
		$user = $this->db->fetch_one($sql);
		return $user['id'];
	}

	function get_by_touserid($touserid,$fields='*') // UC整合时使用
	{
		$touserid = intval($touserid);
		$sql = "SELECT {$fields} FROM `$this->cache_table` WHERE `$this->cache_table`.`touserid`=$touserid LIMIT 1";
		return $this->db->fetch_one($sql);
	}

	function delete_member($id) // 删除会员
	{
		$r=$this->db->fetch_one("SELECT `$this->cache_table`.`modelid` FROM `$this->cache_table` WHERE `$this->cache_table`.`id`=$id LIMIT 1");
		if(!$r || !$id)return false;
		$modelid=$r['modelid'];
		$r=cache_read('cache_membermodel'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
		$db->query("DELETE ".DB_PRE."member,".DB_PRE.$r['name']." FROM ".DB_PRE."member,".DB_PRE.$r['name']." WHERE ".DB_PRE."member.id=".DB_PRE.$r['name'].".userid AND ".DB_PRE."member.id=".intval($id),true);
		$db->mysql_delete(DB_PRE."member_cache",intval($id));
		return true;
	}

	function register($modelid,$username,$password,$email,$invitionaluser=0)
	{

		/************* 设置注册变量 ****************/

		global $DIRCMS,$member,$_groupid;
		$info=array(); // 清除$info数组
		$pwd=$password;
		$modelid=intval($modelid);
		$r=cache_read('cache_membermodel'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
		if(!$r || !$modelid)
		{
			$this->msg = '该会员模型不存在!';
			return false;
		}
		if(!$r['register_enabled'])
		{
			$this->msg = '网站管理员已禁止注册'.$r['modelname'].'!';
			return false;
		}
		$info['template']=$r['template']; /////设置会员默认模板
		if(!$this->is_username($username))
		{
			$this->msg = '用户名由1-30位中文，数字，字母，下划线组成!';
			return false;
		}
		if(!$this->is_email($email))
		{
			$this->msg = '用户邮箱不可用或者该邮箱已经被注册!';
			return false;
		}

		if(!is_pwd($password))
		{
			$this->msg = '用户密码由3-30位数字，字母，下划线组成!';
			return false;
		}
		if($this->get_userid($username))
		{
			$this->msg = '用户名'.$username.'已经被注册!';
			return false;
		}

		if($invitionaluser && !INVITIONAL)
		{
			$this->msg = '管理员已关闭邀请注册!';
			return false;
		}

		if(!SAME_IP_ENABLED)
		{
			$rip=$this->db->fetch_one("SELECT `$this->cache_table`.`regtime` FROM `$this->cache_table` WHERE `$this->cache_table`.`loginip`='".IP."' LIMIT 0,1");
			if($rip && strtotime(date('Y-m-d',$rip['regtime']))==strtotime(date('Y-m-d',TIME)))
			{
				$this->msg = '同IP当天不能重复注册!';
				return false;
			}
		}
		$info['modelid']=$modelid;
		$info['username']=$username;
		$info['password']=$this->password($password);
		$info['email']=$email;
		$info['vipstart']=$info['vipto']=$info['regtime']=TIME; 
		$info['logintime']=TIME; 
		$info['movetime']=TIME; 
		$info['loginip']=IP;
		$info['groupid']=(AUDIT_TYPE=='1' || $_groupid==1)?4:3; //设置用户组
		$info['ispassed']=(AUDIT_TYPE=='1' || $_groupid==1)?1:(AUDIT_TYPE=='2'?0:99); // MAIL_TYPE=1：$ispassed=1 审核通过  MAIL_TYPE=2：$ispassed=0 后台审核 MAIL_TYPE=3：$ispassed=99 邮箱激活
		if($r['register_enabled']==2) ///////////会员组特别设置
		{
			$info['groupid']=3;
			$info['ispassed']=0;
		}
		$info['gradeid']=10;
		$info['template']='company';
		/************* 注册动作 ****************/
		if($this->db->insert(DB_PRE.'member',$info,false))
		{	
			$userid=$this->db->last_insert_id();
			if($this->db->query("INSERT INTO `".DB_PRE.$r['name']."`(`userid`) VALUES(".$userid.")",true))
			{
				if($invitionaluser && INVITIONAL) // 邀请注册的话则给相应会员以积分
				{
					$this->db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`point`=`".DB_PRE."member`.`point`+".intval(INVITIONAL)." WHERE `".DB_PRE."member`.`id`=".intval($invitionaluser));
				}
				if(AUDIT_TYPE=='3' && $_groupid!=1) //  发送激活邮件
				{
					require_once(DIRCMS_ROOT.'include/email.class.php');
					$sendemail=new email();
					$sendemail->send($email, $DIRCMS['site_name'].'会员注册账号激活','请单击以下链接进行账号激活：'.$DIRCMS['site_url'].'member/index.php?file=login&action=activate&verify='.md5(AUTH_KEY.$userid.$username.$info['password'].$modelid).'&id='.$userid,MAIL_USER);
					$this->activateurl=$DIRCMS['site_url'].'member/index.php?file=login&action=activate&verify='.md5(AUTH_KEY.$userid.$username.$info['password'].$modelid).'&id='.$userid;
				}
				$spaceurl=SITE_URL.'space/index.php?id='.$userid;
				$companyurl=SITE_URL.'company/company_'.$userid.'.html';
				$this->db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`spaceurl`='{$spaceurl}',`".DB_PRE."member`.`companyurl`='{$companyurl}' WHERE `".DB_PRE."member`.`id`={$userid}",true);
				member_cache(true);
				return $userid;
			}
			else
			{
				$this->db->query("DELETE FROM `".DB_PRE."member` WHERE `".DB_PRE."member`.`id`=".intval($userid),true);
				member_cache(true);
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	function add($info)
	{
		$r=cache_read('cache_membermodel'.$info['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/');
		if($this->db->insert(DB_PRE.'member',$info,false))
		{	
			$info['userid']=$this->db->last_insert_id();
			if($this->db->insert(DB_PRE.$r['name'],$info,true))
			{
				$this->db->insert(DB_PRE.'member_cache',$info,false);
			}
		}
		return $info['userid'];
	}

	function set($userid, $data = array())
	{
		$userid = intval($userid);
		if ($userid < 1) return false;
		$this->db->update($this->cache_table, $data, "id=$userid");
		$this->db->update($this->table, $data, "id=$userid");
		return true;
	}

	function edit_password_username($username, $password)
	{
		$password = $this->password($password);
		$sql = "UPDATE `$this->cache_table` c, `$this->table` m SET c.`password`='$password', m.`password`='$password' WHERE c.username=m.username AND c.username='$username'";
		return $this->db->query($sql);
	}

	function login($username,$passord,$cookietime=0)
	{
		if(!$this->is_username($username))
		{
			$this->msg = '用户名不可用!';
			return false;
		}
		$userid=$this->get_userid($username);
		$r=$this->get($userid,'*');
		if(!$r)
		{
			$this->msg='用户名不存在!';
			return false;
		}
		$md5_password=$this->password($passord);
		if($md5_password!=$r['password'])
		{
			$this->msg='用户密码不正确!';
			return false;
		}
		$cache_group=cache_read('membergroup'.$userid.'.cache.php',DIRCMS_ROOT.'data/cache/'); 
		if($r['islocked']==1)
		{	
			$this->msg='您的账号已经被禁用!';
			return false;
		}
		if($r['ispassed']==0)
		{	
			$this->msg='您的账号尚未通过审核!';
			return false;
		}
		if($r['groupid']==3)
		{	
			$this->msg='您的账号尚未通过审核!';
			return false;
		}
		if($r['ispassed']==99)
		{	
			$this->msg='您的账号尚未激活，请登录您的注册邮箱激活!';
			return false;
		}
		if($cache_group['isdisabled']==1)
		{
			$this->msg='您的账号已经被禁用!';
			return false;
		}
		$this->_userid=$r['id'];
		if(!$cookietime)$get_cookietime=get_cookie('cookietime');
		$_cookietime=$cookietime?intval($cookietime):($get_cookietime?$get_cookietime:0);
		$cookietime=$_cookietime?TIME + $_cookietime:0;
		$dircms_auth_key=md5(AUTH_KEY.$_SERVER['HTTP_USER_AGENT']);
		$dircms_auth=dircms_auth($this->_userid."\t".$md5_password,'ENCODE',$dircms_auth_key);
		set_cookie('auth',$dircms_auth,$cookietime);
		set_cookie('cookietime',$_cookietime, $cookietime);
		$logintimes=$r['logintimes']+1;
		$this->db->update($this->table,array('logintime'=>TIME,'loginip'=>IP,'logintimes'=>$logintimes),'id='.intval($this->_userid));
		if(intval($_vipto)>=TIME)
		{
			$this->db->update($this->table,array('isvip'=>0,'gradeid'=>10),'id='.intval($this->_userid));
		}
		member_cache(true);
		return $r;
	}

	function editpassword($userid,$newpassword)
	{
		if(!is_pwd($newpassword))return false;
		if($this->db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`password`=".$this->password($newpassword)." WHERE `".DB_PRE."member`.`id`=".intval($userid),true))
		{
			$this->db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`password`=".$this->password($newpassword)." WHERE `".DB_PRE."member_cache`.`id`=".intval($userid),true);
			return true;	
		}
		return false;
	}

	function logout() ///////////// 退出
	{
		set_cookie('auth', '');
		set_cookie('username', '');
		unset($_SESSION);
		session_destroy();
		return true;
	}

	function _exists($field, $value)
	{
		return $this->db->fetch_one("SELECT id FROM `$this->cache_table` WHERE `$this->cache_table`.`$field`='$value' LIMIT 0, 1");
	}

	function password($pwd)
	{
		return md5(md5(PASSWORD_KEY).PASSWORD_KEY.md5($pwd));
	}

	function is_username($username)
	{
		return (in_array($username,explode('|',FORBIDDEN_NAME)) || !is_username($username))?false:true;
	}

	function is_email($email)
	{
		return (($this->_exists('email',$email)&&!SAME_EMAIL_ENABLED) || !is_email($email))?false:true;
	}
}
?>