<?php
if(!defined('DEDEINC'))
{
	exit("Request Error!");
}

//检查用户名的合法性
function CheckUserID($uid,$msgtitle='用户名',$ckhas=true)
{
	global $cfg_mb_notallow,$cfg_mb_idmin,$cfg_md_idurl,$cfg_soft_lang,$dsql;
	if($cfg_mb_notallow != '')
	{
		$nas = explode(',',$cfg_mb_notallow);
		if(in_array($uid,$nas))
		{
			return $msgtitle.'为系统禁止的标识！';
		}
	}
	if($cfg_md_idurl=='Y' && eregi("[^a-z0-9]",$uid))
	{
		return $msgtitle.'必须由英文字母或数字组成！';
	}

	if($cfg_soft_lang=='utf-8')
	{
		$ck_uid = utf82gb($uid);
	}
	else
	{
		$ck_uid = $uid;
	}

	for($i=0;isset($ck_uid[$i]);$i++)
	{
			if(ord($ck_uid[$i]) > 0x80)
			{
				if(isset($ck_uid[$i+1]) && ord($ck_uid[$i+1])>0x40)
				{
					$i++;
				}
				else
				{
					return $msgtitle.'可能含有乱码，建议你改用英文字母和数字组合！';
				}
			}
			else
			{
				if(eregi("[^0-9a-z@\.-]",$ck_uid[$i]))
				{
					return $msgtitle.'不能含有 [@]、[.]、[-]以外的特殊符号！';
				}
			}
	}
	if($ckhas)
	{
		$row = $dsql->GetOne("Select * From `#@__member` where userid like '$uid' ");
		if(is_array($row)) return $msgtitle."已经存在！";
	}
	return 'ok';
}


//网站会员登录类
class MemberLogin
{
	var $M_ID;
	var $M_LoginID;
	var $M_MbType;
	var $M_Uprank;
	var $M_Money;
	var $M_Scores;
	var $M_UserName;
	var $M_Rank;
	var $M_LoginTime;
	var $M_KeepTime;
	var $M_Upmoney;
	var $M_Spacesta;
	var $fields;
	var $isAdmin;

	var $M_Honor = '';

	//php5构造函数
	function __construct($kptime = -1)
	{
		global $dsql;
		if($kptime==-1)
		{
			$this->M_KeepTime = 3600 * 24 * 7;
		}
		else
		{
			$this->M_KeepTime = $kptime;
		}
		$this->M_ID = $this->GetNum(GetCookie("DedeUserID"));
		$this->M_LoginTime = GetCookie("DedeLoginTime");
		$this->fields = array();
		$this->isAdmin = false;
		if(empty($this->M_ID))
		{
			$this->ResetUser();
		}
		else
		{
			$this->M_ID = intval($this->M_ID);
			$this->fields = $dsql->GetOne("Select * From `#@__member` where mid='{$this->M_ID}' ");
			if(is_array($this->fields))
			{
				//间隔一小时更新一次用户登录时间
				if(time() - $this->M_LoginTime > 3600)
				{
					$dsql->ExecuteNoneQuery("update `#@__member` set logintime='".time()."',loginip='".GetIP()."' where mid='".$this->fields['mid']."';");
					PutCookie("DedeLoginTime",time(),$this->M_KeepTime);
				}
				$this->M_LoginID = $this->fields['userid'];
				$this->M_MbType = $this->fields['mtype'];
				$this->M_Uprank = $this->fields['uprank'];
				$this->M_Money = $this->fields['money'];
				$this->M_UserName = $this->fields['uname'];
				$this->M_Upmoney = $this->fields['upmoney'];
				$this->M_Scores = $this->fields['scores'];
				$this->M_Rank = $this->fields['rank'];
				$this->M_Spacesta = $this->fields['spacesta'];
				$sql = "Select titles From #@__scores where integral<={$this->fields['scores']} order by integral desc";
				$scrow = $dsql->GetOne($sql);
				$this->fields['honor'] = $scrow['titles'];
				$this->M_Honor = $this->fields['honor'];
				if($this->fields['matt']==10)
				{
					$this->isAdmin = true;
				}
			}
			else
			{
				$this->ResetUser();
			}
		}
	}

	function MemberLogin($kptime = -1)
	{
		$this->__construct($kptime);
	}

	//退出cookie的会话
	function ExitCookie()
	{
		$this->ResetUser();
	}

	//验证用户是否已经登录
	function IsLogin()
	{
		if($this->M_ID > 0) return true;
		else return false;
	}

	//检测用户上传空间
	function GetUserSpace()
	{
		global $dsql;
		$uid = $this->M_ID;
		$row = $dsql->GetOne("select sum(filesize) as fs From `#@__uploads` where mid='$uid'; ");
		return $row['fs'];
	}

	function CheckUserSpace()
	{
		global $cfg_mb_max;
		$uid = $this->M_ID;
		$hasuse = $this->GetUserSpace();
		$maxSize = $cfg_mb_max * 1024 * 1024;
		if($hasuse >= $maxSize)
		{
			ShowMsg('你的空间已满，不允许上传新文件！','-1');
			exit();
		}
	}

	//更新用户信息统计表
	function UpdateUserTj($field,$uptype='add')
	{
		global $dsql;
		$mid = $this->M_ID;
		$arr = $dsql->GetOne("Select * `#@__member_tj` where mid='$mid' ");
		if(!is_array($arr))
		{
			$arr = array('article'=>0,'album'=>0,'archives'=>0,'homecount'=>0,'pagecount'=>0,'feedback'=>0,'friend'=>0,'stow'=>0);
		}
		extract($arr);
		if(isset($$field))
		{
			if($uptype=='add')
			{
				$$field++;
			}
			else if($$field > 0)
			{
				$$field--;
			}
		}
		$inquery = "INSERT INTO `#@__member_tj` (`mid`,`article`,`album`,`archives`,`homecount`,`pagecount`,`feedback`,`friend`,`stow`)
               VALUES ('$mid','$article','$album','$archives','$homecount','$pagecount','$feedback','$friend','$stow'); ";
		$dsql->ExecuteNoneQuery("Delete From `#@__member_tj` where mid='$mid' ");
		$dsql->ExecuteNoneQuery($inquery);
	}

	//重置用户信息
	function ResetUser()
	{
		$this->fields = '';
		$this->M_ID = 0;
		$this->M_LoginID = '';
		$this->M_Rank = 0;
		$this->M_Uprank = 0;
		$this->M_Money = 0;
		$this->M_UserName = "";
		$this->M_LoginTime = 0;
		$this->M_MbType = '';
		$this->M_Scores = 0;
		$this->M_Spacesta = -2;
		DropCookie('DedeUserID');
		DropCookie('DedeLoginTime');
	}

	//获取整数值
	function GetNum($fnum){
		$fnum = ereg_replace("[^0-9\.]",'',$fnum);
		return $fnum;
	}

	//用户登录
	function CheckUser(&$loginuser,$loginpwd)
	{
		global $dsql;

		//检测用户名的合法性
		$rs = CheckUserID($loginuser,'用户名',false);

		//用户名不正确时返回验证错误，原登录名通过引用返回错误提示信息
		if($rs!='ok')
		{
			$loginuser = $rs;
			return '0';
		}

		//matt=10 是管理员关连的前台帐号，为了安全起见，这个帐号只能从后台登录，不能直接从前台登录
		$row = $dsql->GetOne("Select mid,matt,pwd From `#@__member` where userid like '$loginuser' ");
		if(is_array($row))
		{
			if($row['pwd'] != md5($loginpwd))
			{
				return -1;
			}
			else
			{
				//管理员帐号不允许从前台登录
				if($row['matt']==10)
				{
					return -2;
				}
				else
				{
					$this->PutLoginInfo($row['mid']);
					return 1;
				}
			}
		}
		else
		{
			return 0;
		}
	}

	//保存用户cookie
	function PutLoginInfo($uid)
	{
		$this->M_ID = $uid;
		$this->M_LoginTime = time();
		if($this->M_KeepTime > 0)
		{
			PutCookie('DedeUserID',$uid,$this->M_KeepTime);
			PutCookie('DedeLoginTime',$this->M_LoginTime,$this->M_KeepTime);
		}
		else
		{
			PutCookie('DedeUserID',$uid);
			PutCookie('DedeLoginTime',$this->M_LoginTime);
		}
	}

	//获得会员目前的状态
	function GetSta($dsql)
	{
		$sta = '';
		if($this->M_Rank==0)
		{
			$sta .= "你目前的身份是：普通会员";
		}
		else
		{
			$row = $dsql->GetOne("Select membername From `#@__arcrank` where rank='".$this->M_Rank."'");
			$sta .= "你目前的身份是：".$row['membername'];
		}
		if($this->M_Uprank>0)
		{
			$row = $dsql->GetOne("Select membername From `#@__arcrank` where rank='".$this->M_Uprank."'");
			$mname = $row['membername'];
			$sta .= " 正在申请升级为：$mname ";
		}
		$sta .= " 拥有金币：{$this->M_Money} 个，积分：{$this->M_Scores} 分";
		if($this->M_Upmoney>0)
		{
			$sta .= "，正在申请 ".$this->M_Upmoney." 个金币";
		}
		$sta .= "。";
		return $sta;
	}
}
?>