<?php
class AjaxAction extends CommonAction{
    public function checkok(){
        load('extend');
		$id=H($_POST['id']);
		$content=H($_POST['content']);
		if(noOKUserName($content))
		{
			echo "禁止注册的用户名或者个人主页名称";exit;
		}
		if($id=="username")	
		{
			if((getStringLen($content)>5)&&(getStringLen($content)<20))
			{
				$user=M('user');
				$data=array();
				$data['userid']=$content;
				$rs=FALSE;
				$rs=$user->where($data)->find();
				if($rs)
					echo "<img src='/".getDadengPath(2).APP_PATH."/images/error.jpg' />用户名".$content."已经存在";
				else
					echo "<img src='/".getDadengPath(2).APP_PATH."/images/ok.jpg' />";
			}
			else
				echo "<img src='/".getDadengPath(2).APP_PATH."/images/error.jpg' />用户名长度错误";
		}
		else
		{
			if(preg_match("/^[0-9a-zA-Z]{6,20}$/",$content))
			{
				$user=M('user');
				$data=array();
				$data['homepage']=$content;
				$rs=FALSE;
				$rs=$user->where($data)->find();
				if($rs)
					echo "<img src='/".getDadengPath(2).APP_PATH."/images/error.jpg' />个人主页".$content."已经存在";
				else
					echo "<img src='/".getDadengPath(2).APP_PATH."/images/ok.jpg' />";
			}
			else
				echo "<img src='/".getDadengPath(2).APP_PATH."/images/error.jpg' />请不要输入特殊字符";
		}
    }
	public function send()
	{
		parent::notlogin();
		checkUserStatus();
		load('extend');
		$content=trim(filterSpecial(H($_POST['content'])));
		$userid=getUserID();
		$gid=$_POST['gid'];
		if(!checkId32($gid))
		{
			echo '错误ID';
			exit;
		}
		//判断用户发言时间限制
		getSendTime();
		//
		if(empty($content))
			echo "内容不能为空";
		else
		{
			$s1=stripos($content,"#");
			$subject="";
			$tagid=0;
			if($s1!==FALSE)
			{
				$s2=stripos($content,"#",1);
				$subject=substr($content,($s1+1),$s2-($s1+1));
				$content=substr($content,($s2+1),strlen($content)-$s2);
			}
			if($subject=="请在这里输入话题名称")
			{
				echo "请更改话题的名称";
				exit;
			}
			$data=array();
			if(!empty($subject))
			{
				$sub=M("topic_tag");
				$subrs=$sub->where("title='".$subject."'")->find();
				if($subrs)
				{
					$tagid=$subrs['id'];
				}
				else
				{
					$sdata=array();
					$sdata['title']=$subject;
					$sdata['create_time']=time();
					$sdata['create_user']=$userid;
					$tagid=$sub->add($sdata);	
				}
			}
			$status=getCheckHttp($content)?0:1;
			$data['title']=replaceNoTag($content);
			$data['userid']=$userid;
			$data['time']=time();
			$data['groupid']=$gid;
			$data['parentid']=0;
			$data['ding']=0;
			$data['cai']=0;
			$data['pass']=0;
			$data['tagid']=$tagid;
			$data['status']=$status;
			$topic=M('topic');
			$rs=$topic->add($data);
			if($rs)
			{
				//增加积分
				if($status==1)
					addUserScore($userid,1,"发布微博id=".$rs);
				//
				echo '9999';
			}	
			else
				echo '0000';
		}
	}
	public function replay()
	{
		parent::notlogin();
		checkUserStatus();
		load('extend');
		$topid=0;
		$content=trim(filterSpecial(H($_POST['content'])));
		$topid=checkId32($_POST['topid']);
		$gid=checkId32($_POST['gid']);
		if($topid==0)
		{
			echo '发生错误';
			exit;
		}
		if($gid==0)
		{
			echo '发生错误';
			exit;
		}
		$userid=getUserID();
		//判断用户发言时间限制
		getSendTime();
		//
		if(empty($content))
			echo "内容不能为空";
		else
		{	
			$status=getCheckHttp($content)?0:1;		
			$data=array();
			$data['title']=replaceNoTag($content);
			$data['userid']=$userid;
			$data['time']=time();
			$data['groupid']=$gid;
			$data['parentid']=$topid;
			$data['ding']=0;
			$data['cai']=0;
			$data['pass']=0;
			$data['status']=$status;
			$topic=M('topic');
			$rs=$topic->add($data);
			if($rs)
			{
				if($status==1)
					addUserScore($userid,2,"回复微博id=".$topid);
				echo "<font color='green'>我的回复</font>:".$content;
			}
			else
				echo '回复失败';
		}	
	}
	function ding()
	{
		$id=$_POST['id'];
		$num=$_POST['num'];
		$typeid=$_POST['typeid'];
		if($num==0)
			$num=1;
		if(empty($id)||empty($typeid)||empty($num))
		{
			echo '不能为空';
			exit;
		}
		else
		{
			if($typeid!=3)
			{
				parent::notlogin();
			}
				$id=checkId32($id);
				$num=checkId32($num);
				$typeid=checkId32($typeid);
				if(($id)&&($num)&&($typeid))
				{
					$top=M('topic');
					$rs=$top->find($id);
					if($rs)
					{
						$data=array();
						if($typeid==1)
							$data['ding']=$rs['ding']+1;
						else if($typeid==2)
							$data['cai']=$rs['cai']+1;
						else
							$data['pass']=$rs['pass']+1;
						$crs=$top->where('id='.$id)->save($data);
						if($crs)
							echo '9999';
						else
							echo '发生错误';
					 }
					 else
						echo '读取数据失败';
	
				}
				else
					echo '必须是数字';	
		}
	}
	public function attention()
	{
		parent::notlogin();
		$obj=filterSpecial($_POST['obj']);
		if($obj==getSession())
		{
			echo '不能关注自己';
			exit;
		}
		$u=M('user');
		$data=array();
		$data['id']=$obj;
		$rs=$u->where($data)->find();
		if(!$rs)
		{
			echo '用户不存在';
			exit;
		}
		$attention=M('attention');
		$da=array();
		$da['userid']=getUserID();
		$da['objuser']=$obj;
		$ok=$attention->where($da)->count();
		if($ok)
		{
			echo '你已经关注了此用户';
			exit;
		}
		$da['time']=time();
		$ars=$attention->add($da);
		if($ars)
		{
			echo '您成功的关注了此用户';	
		}
		else
			echo '发生错误';
	}
	public function userinfo()
	{
		load('extend');
		parent::notlogin();
		$nkname=filterSpecial(H($_POST['nkname']));
		$sex=$_POST['sex'];
		$content=filterSpecial(H($_POST['content']));
		if(empty($nkname)||empty($sex)||empty($content))
		{
			echo '请输入所有信息';exit;
		}
		if(getStringLen($nkname)>10)
		{
			echo '昵称必须少于11个字符';exit;
		}
		if(($sex==0)||($sex!=1&&$sex!=2))
		{
			echo '性别信息错误'.$sex;exit;
		}
		if(getStringLen($content)>140)
		{
			echo '自我介绍文字必须少于141个字符！';exit;
		}
		$user=M('user');
		$data=array();
		$data['id']=getUserID();
		$rs=$user->find(getUserID());
		$data['sex']=$sex;
		$data['memo']=$content;
		if(empty($rs['nickname']))
		{
			$data['nickname']=$nkname;
		}
		$ok=$user->save($data);
		if($ok)
			echo '个人信息保存成功';
		else
			echo '保存失败';
	}
	public function userpwd()
	{
		parent::notlogin();
		$pwd=$_POST['passwd'];
		$repwd=$_POST['repwd'];
		if(empty($pwd)||empty($repwd))
		{
			echo '请输入密码';exit;
		}
		load('extend');
		$pwd=H($pwd);
		$repwd=H($repwd);
		if($pwd!=$repwd)
		{
			echo '两次密码必须一致！';exit;
		}
		$user=M('user');
		$data=array();
		$data['id']=getUserID();
		$data['passwd']=md5($pwd);
		$rs=$user->save($data);
		if($rs)
			echo '修改成功,您的密码是:'.$pwd.',请牢记！';
		else
			echo '保存失败';
	}
	public function mail()
	{
		parent::notlogin();
		$mail=$_POST['mail'];
		if(ismail($mail))
		{
			$user=M('user');
			$data=array();
			$data['id']=getUserID();
			$data['mail']=$mail;
			$rs=$user->save($data);
			if($rs)
				echo '邮件绑定成功';
			else
				echo '保存失败';
		}
		else
			echo 'e-mail格式错误';
	}
	public function getinvite()
	{
		parent::notlogin();
		$code=uniqid().mt_rand(100,999);
		$in=M('invite');
		$data['userid']=getUserID();
		$data['time']=time();
		$data['code']=$code;
		$data['is_use']=0;
		if($in->add($data))
		{
			//扣除积分
			$site=F('site');
			$usescore=$site['is_invite_free'];
			$user=M('user');
			$rs=$user->find(getUserID());
			if($rs['score']-$usescore>0)
			{
				execUserScore(getUserID(),$usescore,'邀请用户注册,扣除积分'.$usescore.'，邀请码：'.$code);
				echo '您的邀请码是：'.$site['domain'].U('public/reg?invitecode='.$code);	
			}
			else
			{
				echo '您的积分不够'.$usescore;
			}
		}
		else
			echo "发生错误";
	}
}
?>