<?php
class PublicAction extends Action{
	function _initialize(){
		header("Content-Type:text/html; charset=utf-8");
		checkCatch();
    }
	private function site($title)
	{
		$site=F('site');
		$site['title']=$title."--".$site['title'];
		$msg="<li><a href='".U('public/signup')."'>用户注册</a></li><li><a href='".U('public/login')."'>登陆系统</a></li>";
		$this->assign('site',$site);
		$this->assign('msg',$msg);
	}
    public function signup(){
		$code=$_GET['invitecode'];
		if(!empty($code))
		{
			$in=M('invite');
			$where['code']=$code;
			$rs=$in->where($where)->find();
			if($rs)
			{	
				if($rs['is_use']==1)
					$this->assign('codeinfo',"此邀请码已经使用了！");
				else
				{
					$this->assign('codeinfo',"此邀请码可用！");
					$this->assign('code',$code);	
				}
			}
			else
				$this->assign('codeinfo',"错误的邀请码");
		}
		else
		{
			$s=F('site');
			if(!$s)
			{
				checkCatch();
			}
			$this->assign('s',$s);	
		}
		$this->site('用户注册');
		$this->assign('webpath',getDadengPath(3));
        $this->display();
    }
	public function login()
	{
		$this->assign('webpath',getDadengPath(3));
		$this->site('用户登陆');
		if(Session::is_set(C('USER_AUTH_KEY')))
		{
			$this->redirect('index/index');
			exit;
		}
		$this->display();
	}
	public function checklogin()
	{
		load('extend');
		$userid=filterSpecial(H($_POST['userid']));
		$passwd=filterSpecial(H($_POST['passwd']));
		$mycode=filterSpecial(H($_POST['mycode']));
		if(empty($userid)||empty($passwd)||empty($mycode))
		{
			echo "所有信息必须输入";
			exit;
		}
		if($_SESSION['verify'] != md5($mycode)){
			echo "验证码错误";
			exit;
		}
		$user=M('user');
		$data=array();
		$data['userid']=$userid;
		$data['passwd']=md5($passwd);
		$rs=$user->where($data)->find();
		if($rs)
		{
			$updt['login_time']=time();
			$updt['login_ip']=get_client_ip();
			$user->where('id='.$rs['id'])->save($updt);
			//积分处理
			$siteconfig=F('site');
			//判断开启积分系统
			if($siteconfig['score_ok'])
			{
				$add=$siteconfig['login_score'];
				$user->setInc('score','id='.$rs['id'],$add);
				//写入积分日志
				$sclog=M('score');
				$scdata['userid']=$rs['id'];
				$scdata['types']=2;//2表示+,1表示-
				$scdata['num']=$add;
				$scdata['time']=time();
				$scdata['memo']=date("Y-m-d H:i:s")."登陆系统";
				$sclog->add($scdata);
			}
			Cookie::set('cookieid',$userid);
			Cookie::set('cookiewd',md5($passwd));
			Session::set(C('USER_AUTH_KEY'),$userid);
			echo "恭喜您登陆成功";
			exit;
		}
		else
			echo "登陆失败";
	}
	public function save()
	{
		$siteconfig=F('site');//读取缓存
		load('extend');
		$invitecode=filterSpecial(H($_POST['invitecode']));
		if(!$invitecode)
		{
			if(!$siteconfig['is_reg'])
			{
				echo "系统已经关闭注册，请联系系统管理员";
				exit;
			}	
		}
		else
		{
			$in=M('invite');
			$where['code']=$invitecode;
			$rs=$in->where($where)->find();
			if($rs)
			{
				if($rs['is_use']==1)
				{
					echo '已经使用过的邀请码';
					exit;
				}
			}
			else
			{
				echo '不存在的激活码';
				exit;
			}
		}
		$userid=filterSpecial(H($_POST['userid']));
		$homepage=filterSpecial(H($_POST['homepage']));
		$passwd=filterSpecial(H($_POST['passwd']));
		$repasswd=H($_POST['repasswd']);
		$mycode=filterSpecial(H($_POST['mycode']));
		$temp_user=$temp_home="";
		if(empty($userid)||empty($homepage)||empty($passwd)||empty($mycode))
		{
			echo "所有信息必须输入";
			exit;
		}
		if($_SESSION['verify'] != md5($mycode)){
			echo "验证码错误";
			exit;
		}
		if(noOKUserName($userid))
		{
			echo "禁止注册的用户名";exit;
		}
		if(noOKUserName($homepage))
		{
			echo "禁止注册的个人主页名称";exit;
		}
		if((str_len($userid)>5)&&(str_len($userid)<20))
		{
			$temp_user=$userid;
		}
		else
		{
			echo "用户名长度错误";
			exit;
		}
		if(preg_match("/^[0-9a-zA-Z]{6,20}$/",$homepage))
		{
			$temp_home=$homepage;
		}
		else
		{
			echo "个人主页不能含有特殊字符";
			exit;
		}
		if(!preg_match("/^[0-9a-zA-Z]{6,16}$/",$passwd))
		{
			echo "密码不能含有特殊字符，长度必须是6-16位";
			exit;
		}
		if($passwd!=$repasswd)
		{
			echo "两次密码必须一致";
			exit;
		}
		//判断注册限制用户名 执行模糊匹配
		$nouser=$siteconfig['no_user'];
		if(!empty($nouser))
		{
			$noarr=explode("|",$nouser);
			for($i=0;$i<count($noarr);$i++)
			{
				if(!empty($noarr[$i]))
				{
					if(stripos($temp_user,$noarr[$i])!==FALSE)
					{
						echo $temp_user."为系统禁止使用";
						exit;
					}
					if(stripos($temp_home,$noarr[$i])!==FALSE)
					{
						echo $temp_home."为系统禁止使用";
						exit;
					}
				}
			}
		}
		//
		$user=M('user');
		$cdata=array();
		$cdata['userid']=$temp_user;
		if($user->where($cdata)->find())
		{
			echo "该用户名已经存在,请更换一个";
			exit;
		}
		if($user->where("homepage='".$homepage."'")->find())
		{
			echo "该个人主页已经存在，请更换一个！";
			exit;
		}
		//注册IP限制
		$ip_check=$siteconfig['ip_check'];
		if($ip_check)
		{
			$ip=get_client_ip();
			$iprs=$user->where("create_ip='".$ip."'")->order('id desc')->find();
			if(time()-$iprs['create_time']<$ip_check*60)
			{
				echo "同IP".$ip_check."分钟内限制注册";exit;
			}
		}
		//
		$arr=array();
		$arr['userid']=$temp_user;
		$arr['homepage']=$temp_home;
		$arr['passwd']=md5($passwd);
		$arr['create_time']=time();
		$arr['create_ip']=get_client_ip();
		$arr['groupid']=0;
		$arr['roleid']=0;
		$arr['score']=$siteconfig['user_socre']?$siteconfig['user_socre']:0;//默认积分
		$arr['status']=$siteconfig['user_check']?0:1;//用户审核
		$ok=$user->add($arr);
		if($ok)
		{
			Session::set(C('USER_AUTH_KEY'),$temp_user);
			//销毁邀请码
			$in=M('invite');
			$in->where("code='".$invitecode."'")->setField('is_use',1);
			//
			echo '恭喜您注册成功';
		}
		else
			echo '注册失败'.$ok;
	}
	public function verify(){   
		import("ORG.Util.Image");   
		Image::buildImageVerify();   
	}
	public function logout()
	{
		Cookie::delete('cookieid');
		Cookie::delete('cookiewd');
		Session::set(C('USER_AUTH_KEY'),null);
		Session::destroy();
		$this->redirect('index/index');
	}
	public function admlogin()
	{
		$this->assign('webpath',getDadengPath(3));
		$this->site('管理登陆');
		if(!empty($_SESSION['isadmin']))
			$this->redirect('adminsys/sysinfo');
		$this->display('adminlogin');
	}
	public function check()
	{
		$pwd=filterSpecial($_POST['pwd']);
		$u=M('user');
		$rs=$u->find(getUserID());
		if($rs['passwd']!=md5($pwd))
		{
			$this->error('您输入的密码错误');
		}
		else
		{
			if($rs['roleid']!=9)
				$this->error('登陆号错误');
			else
				{
					$_SESSION['isadmin']=$rs['userid'];
					$this->redirect('adminsys/sysinfo');
				}
		}
	}
}
?>