<?php
class IndexAction extends Action {
	public function _empty($name){
		$this->all($name);
	}
	public function checkUser(){
		if(empty($_SESSION['uid'])) $this->redirect('/User/Index/login');
	}
	public function all($name){
		$USER_PUBLIC = C('USER_PUBLIC');
		$this->assign('USER_PUBLIC',$USER_PUBLIC);
		$YICMS_PUBLIC = C('YICMS_PUBLIC');
		$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
		$data = M('About');
		$about = $data->find();
		$this->assign('about',$about);
		if($name=='index'){
			$uid = (int)$_GET['uid'];
			if(empty($uid)) $uid = $_SESSION['uid'];
			if(empty($uid)) $this->error('要么登录,要么只能访问别人的!');
			$profile = M('User')->where("uid = $uid")->find();
			if(empty($profile)) $this->error('该用户不存在!');
			if($profile['state'] == '0') $this->error('该用户已被禁止登录,');
			$profile['face'] = C('YICMS_PUBLIC').'/face/f_'.$profile['face'];
			$this->assign('profile',$profile);				
			$news = M('News')->where("uid = $uid")->order('time DESC')->limit(10)->select();	
			$this->assign('news',$news);
			$config = M('Config')->find();
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='fans'){
			$uid = (int)$_GET['uid'];
			if(empty($uid)) $uid = $_SESSION['uid'];
			if(empty($uid)) $this->error('要么登录,要么只能访问别人的!');
			$profile = M('User')->where("uid = $uid")->find();
			if(empty($profile)) $this->error('该用户不存在!');
			if($profile['state'] == '0') $this->error('该用户已被禁止登录,');
			$profile['face'] = C('YICMS_PUBLIC').'/face/f_'.$profile['face'];
			$this->assign('profile',$profile);		
			import("@.ORG.Page");
			$count = M('Fans')->where("toid = $uid")->count('id');	
			$p     = new Page($count, 10);					
			$fans = M('Fans')->where("toid = $uid")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
			$this->assign('fans',$fans);
			$p->setConfig('header','个粉丝');
			$p->setConfig('prev','上一页');
			$p->setConfig('next','下一页');
			$p->setConfig('first','<<');
			$p->setConfig('last','>>');
			$page = $p->show ();
			$this->assign("page", $page );
			$config = M('Config')->find();
			if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='look'){
			$uid = (int)$_GET['uid'];
			if(empty($uid)) $uid = $_SESSION['uid'];
			if(empty($uid)) $this->error('要么登录,要么只能访问别人的!');
			$profile = M('User')->where("uid = $uid")->find();
			if(empty($profile)) $this->error('该用户不存在!');
			if($profile['state'] == '0') $this->error('该用户已被禁止登录,');
			$profile['face'] = C('YICMS_PUBLIC').'/face/f_'.$profile['face'];
			$this->assign('profile',$profile);			
			import("@.ORG.Page");
			$count = M('Fans')->where("fromid = $uid")->count('id');	
			$p     = new Page($count, 10);			
			$look = M('Fans')->where("fromid = $uid")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
			$this->assign('look',$look);
			$p->setConfig('header','个关注');
			$p->setConfig('prev','上一页');
			$p->setConfig('next','下一页');
			$p->setConfig('first','<<');
			$p->setConfig('last','>>');
			$page = $p->show ();
			$this->assign("page",$page);
			$config = M('Config')->find();
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='articles'){
			$uid = (int)$_GET['uid'];
			if(empty($uid)) $uid = $_SESSION['uid'];
			if(empty($uid)) $this->error('要么登录,要么只能访问别人的!');
			$profile = M('User')->where("uid = $uid")->find();
			if(empty($profile)) $this->error('该用户不存在!');
			if($profile['state'] == '0') $this->error('该用户已被禁止登录,');
			$profile['face'] = C('YICMS_PUBLIC').'/face/f_'.$profile['face'];
			$this->assign('profile',$profile);		
			import("@.ORG.Page");
			$count = M('News')->where("uid = $uid")->count('id');	
			$p     = new Page($count, 10);			
			$articles = M('News')->where("uid = $uid")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
			$this->assign('articles',$articles);
			$p->setConfig('header','条文章');
			$p->setConfig('prev','上一页');
			$p->setConfig('next','下一页');
			$p->setConfig('first','<<');
			$p->setConfig('last','>>');
			$page = $p->show ();
			$this->assign("page", $page );
			$config = M('Config')->find();
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);		
			$this->display();
		}elseif($name=='comments'){
			$uid = (int)$_GET['uid'];
			if(empty($uid)) $uid = $_SESSION['uid'];
			if(empty($uid)) $this->error('要么登录,要么只能访问别人的!');
			$profile = M('User')->where("uid = $uid")->find();
			if(empty($profile)) $this->error('该用户不存在!');
			if($profile['state'] == '0') $this->error('该用户已被禁止登录,');
			$profile['face'] = C('YICMS_PUBLIC').'/face/f_'.$profile['face'];
			$this->assign('profile',$profile);		
			import("@.ORG.Page");
			$count = M('Reply')->where("arid = $uid")->count('id');	
			$p     = new Page($count, 10);			
			$comments = M('Reply')->where("arid = $uid")->order('artime DESC')->limit($p->firstRow.','.$p->listRows)->select();
			$this->assign('comments',$comments);
			$p->setConfig('header','条文章');
			$p->setConfig('prev','上一页');
			$p->setConfig('next','下一页');
			$p->setConfig('first','<<');
			$p->setConfig('last','>>');
			$page = $p->show ();
			$this->assign("page", $page );
			$config = M('Config')->find();
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='edit'){
			$this->checkUser();
			$profile = $_SESSION;
			$this->assign('profile',$profile);
			$config = M('Config')->find();
			if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='editpass'){
			$this->checkUser();
			$profile = $_SESSION;
			$this->assign('profile',$profile);
			$config = M('Config')->find();
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='login'){
			if(!empty($_SESSION['uid'])){
				$this->redirect('/User/Index/index');
			}
			$config = M('Config')->find();
			$config['title'] = '登录-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='entercode'){
			$config = M('Config')->find();
			$config['title'] = '请输入验证码-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='reg'){
			$code = trim($_POST['code']);
			if(C('getcode') && empty($code)) $this->redirect('/User/Index/entercode');
			if(C('getcode') && !empty($code)){
				$list = M('Code')->where("code = '$code'")->find();
				if(empty($list)) $this->error('邀请码错误!');
				$this->assign('code',$list['code']);
			}
			if(!empty($_SESSION['uid'])){
				$this->assign('jumpUrl',U('User/Index/index'));
				$this->error('已登录,不能继续注册!');
			}
			$config = M('Config')->find();
			$config['title'] = '注册会员-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}elseif($name=='search'){
			$this->checkUser();
			$config = M('Config')->find();
			$config['title'] = '会员搜索-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$account = htmlspecialchars(trim($_POST['account']));
			$userlist = M('User')->field('face,uname,uid')->where("uid = $account OR uname like '%$account%'")->select();
			$count = M('User')->field('face,uname,uid')->where("uid = $account OR uname like '%$account%'")->count('uid');
			$this->assign('count',$count);
			$this->assign('userlist',$userlist);
			$this->assign('account',$account);
			$this->display();
		}elseif($name=="msg"){
			$this->checkUser();
			$profile = $_SESSION;
			$this->assign('profile',$profile);
			if(empty($_GET['what'])) $_GET['what'] = 'in';
			$uid = $_SESSION['uid'];
			if($_GET['what']=='in'){
				import("@.ORG.Page");
				$count = M('User_msg')->where("to_uid = $uid")->count('id');	
				$p     = new Page($count, 10);			
				$msg = M('User_msg')->where("to_uid = $uid")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
				$this->assign('msg',$msg);
				$page = $p->show ();
				$this->assign("page", $page);
			}elseif($_GET['what']=='out'){
				import("@.ORG.Page");
				$count = M('User_msg')->where("from_uid = $uid")->count('id');	
				$p     = new Page($count, 10);			
				$msg = M('User_msg')->where("from_uid = $uid")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
				$this->assign('msg',$msg);
				$page = $p->show ();
				$this->assign("page", $page);
			}elseif($_GET['what']=='new'){
				$new = true;
			}else{
				$this->error('您输入的网址不存在!');
			}
			$this->display();
		}elseif($name=='code'){
			$this->checkUser();
			$profile = $_SESSION;
			$this->assign('profile',$profile);
			$uid = $profile['uid'];
			$code = M('Code')->where("uid = $uid")->order('time DESC')->select();
			$this->assign('code',$code);
			$config = M('Config')->find();
			if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
			$config['title'] = $profile['uname'].'-会员中心-'.$config['title'];
			$this->assign('config',$config);
			$this->display();
		}else{
			$this->display();
		}
	}
	public function codeAdd(){
		$uid = $_SESSION['uid'];
		$time = $_SERVER['REQUEST_TIME'];
		$year = date('Y',$time);
		$month = date('m',$time);
		$day = date('d',$time);
		$time = mktime(0,0,0,$month,$day,$year);
		$code = M('Code')->where("uid = $uid AND time>$time")->find();
		if(!empty($code)){
			$this->error('您今日已经申请了邀请码!');
		}else{
			$data['uid'] = $_SESSION['uid'];
			$data['time'] = $_SERVER['REQUEST_TIME'];
			$max = C('codeNum');
			if($max=='0') $this->error('管理员关闭了此功能!'); 
			for($i=0;$i<$max;$i++){
				$data['code'] = uniqid();
				M('Code')->add($data);
			}
			$this->success('申请成功!赶快邀请好友吧!^_^');
		}
	}
	public function checkMsg(){
		$to_uname = htmlspecialchars($_POST['to_uname']);
		$list = M('User')->field('uid,uname')->where("uname = '$to_uname'")->find();
		if(empty($list)) $this->error('写给的用户不存在!请重新填写.');
		$data['from_uid']   = $_SESSION['uid'];
		$data['from_uname'] = $_SESSION['uname'];
		$data['time']       = $_SERVER['REQUEST_TIME'];
		$data['to_uid']     = $list['uid'];
		$data['to_uname']   = $list['uname'];
		$data['msg']        = htmlspecialchars($_POST['msg']);
		if(M('User_msg')->add($data)) $this->success('写信成功!^_^');
		else $this->error('抱歉.写信失败!可以尝试再写一次!');
	}
	public function checkEditpass(){
		$uid = (int)$_SESSION['uid'];
		$uname = $_SESSION['uname'];
		$user = M('User')->where("uid = $uid AND uname = '$uname'")->find();
		$old_password = htmlspecialchars(trim($_POST['old_password']));
		if(md5($old_password) != $user['password']) $this->error('当前密码错误!');
		$password = htmlspecialchars(trim($_POST['password']));
		if(empty($password)) $this->error('请填写新密码!');
		if(strlen($password)<6) $this->error('新密码须大于6位!');
		$repassword = htmlspecialchars(trim($_POST['repassword']));
		if($password != $repassword) $this->error('两次新密码不一致!');
		$data['uid'] = $uid;
		$data['password'] = md5($password);
		if(M('User')->save($data)) $this->success('密码修改成功!');
		else $this->error('密码修改失败!');
	}
	public function checkLogin(){
		if($_COOKIE['error']=='3') $this->error('请15分钟再试!'); 
		if(empty($_POST['account']))  $this->error('用户名或邮箱必须填写！');
		if(empty($_POST['password'])) $this->error('密码必须填写！');
		$account = trim($_POST['account']);
		$password = md5(trim($_POST['password']));
		$user = M('User')->where("`uname` = '$account' OR `email` = '$account'")->find();
		if(is_array($user)){
			if($user['password'] == $password){
				$regemail = C('regemail');
				if($regemail && !$user['checkmail']){
					$this->assign('waitSecond',15);
					$this->error('请验证邮件后再行登录!您的邮箱地址是: <font color="black">'.$user['email'].'</font><br/>');
				}
				if($user['state'] == '0') $this->error('您已被禁止登录,');
				unset($user['password']);
				$_SESSION = array_merge($user,$_SESSION);
				$_SESSION['lasttime'] = switchtime($user['lasttime']);
				$_SESSION['lastip'] = long2ip($user['lastip']);
				$_SESSION['uid'] = $user['uid'];
				$_SESSION['face'] = C('YICMS_PUBLIC').'/face/f_'.$user['face'];
				//保存登录信息
				$upUser = M('User');
				$time = $_SERVER['REQUEST_TIME'];
				$ip = ip2long(get_client_ip());
				$data = array();
				$data['uid']	=	$user['uid'];
				$data['lasttime']	=	$time;
				$data['lastip']	= $ip;
				$upUser->save($data);
				$this->assign('jumpUrl',U('User/Index/index'));
				$this->success('登录成功！现在将跳转用户中心!');
			}else{
				$error = $_COOKIE['error'];
				setcookie('error',$error+1,$_SERVER['REQUEST_TIME']+900);
				$this->error('密码错误!');
			}
		}else{
			$error = $_COOKIE['error'];
			setcookie('error',$error+1,$_SERVER['REQUEST_TIME']+900);
			$this->error('用户名或邮箱不存在!');
		}
	}
	public function checkRegForm(){
		$uname = htmlspecialchars($_POST['uname']);
		$email = htmlspecialchars($_POST['email']);
		$password = htmlspecialchars($_POST['password']);
		$repassword = htmlspecialchars($_POST['repassword']);
		if(strlen($password) < 6) exit('-1'); 
		
		if($password != $repassword ) exit('-2');
		if(strlen($uname) < 2 ) exit('-3');
		if(!preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i",$_POST['email'])) exit('-4');
		$list = M('User')->where("uname = '$uname'")->find();
		if(!empty($list)) exit('-5');
		$list = M('User')->where("email = '$email'")->find();
		if(!empty($list)) exit('-6');
		echo 1;
	}
	public function checkReg(){
		$code = trim($_POST['code']);
		if(!empty($code)) M('Code')->where("code = '$code'")->delete();
		$User = D('User'); 
		$_POST['state'] = '1';
		$_POST['face'] = 'noface.png';
		//压入数据库
		if ($vo = $User->create()) {
			if($voo = $User->add()){
				$regemail = C('regemail');
				if($regemail){
					import("@.ORG.Smtp");
					$smtpserver = C('smtpserver'); 
					$port = C('smtpport');
					$smtpuser = C('smtpuser');
					$smtppwd = C('smtppwd');
					$mailtype = "TXT";
					$sender = C('smtpuser');
					$smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender); 
					$to = $vo['email']; 
					$subject = iconv('UTF-8','gb2312',C('regtitle'));
					$code = C('URL').U('User/Index/checkEmail?uid='.$voo.'&code='.md5($voo.$vo['email'].$vo['uname']));
					$regcontent = C('regcontent');
					$regcontent = str_replace('{username}',$vo['uname'],$regcontent);
					$regcontent = str_replace('{time}',switchtime($_SERVER['REQUEST_TIME']),$regcontent);
					$regcontent = str_replace('{code}',$code,$regcontent);
					$body = iconv('UTF-8','gb2312',$regcontent);
					$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);
					
					$this->assign('waitSecond',15);
					$this->error('请访问你的邮箱 '.$vo['email'].' 验证邮箱后登录!<br/>');
				}
				//注册后自动登录
				$_SESSION = $vo;
				$_SESSION['uid'] = $voo;
				$_SESSION['lasttime'] = switchtime($vo['lasttime']);
				$_SESSION['lastip'] = getip($vo['lastip']);
				$_SESSION['face'] = C('YICMS_PUBLIC').'/face/f_'.$vo['face'];
				unset($_SESSION ['password']);
				$this->assign('jumpUrl',U('User/Index/index'));
				$this->success('用户注册成功！现在将返回主页!'); 
				
			} else { 
				$this->error('抱歉!注册出现错误！您可以重新注册试试 - -!'); 
			}
		}else {
			$this->error($User->getError()); 
		}
	}
	public function checkEmail(){
		$uid = (int)$_GET['uid'];
		$user = M('User')->field('uid,email,uname')->where("uid = $uid")->find();
		if(empty($user)){
			$this->error('用户不存在!');
		}else{
			$code = $_GET['code'];
			if(md5($user['uid'].$user['email'].$user['uname']) == $code){
				$data['uid'] = $user['uid'];
				$data['checkmail'] = '1';
				M('User')->save($data);
				$this->assign('jumpUrl',U('User/Index/login'));
				$this->success('验证成功!现在请返回登录页!');
			}else{
				$this->error('验证失败!');
			}
		}
	}
	public function checkEdit(){
		if(empty($_POST['email']) && empty($_FILES['face']['name'])) $this->error('请选择一项值!');
		if (!preg_match("/^[\w-]+@([a-zA-Z0-9-]+\.){1,3}[a-zA-Z]+$/",$_POST['email'])) $this->error('邮件格式错误!');
		unset($_POST['__hash__']);
		if($_FILES['face']['error']!='4'){
			import("@.ORG.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize =20971520;
			$upload->allowExts = explode(',','jpg,gif,png,jpeg');
			$upload->savePath ='Public/face/'; 
			$upload->thumb=true;
			$upload->imageClassPath = '@.ORG.Image';
			$upload->thumbPrefix = 'f_';
			$upload->thumbMaxWidth  = 30;
			$upload->thumbMaxHeight = 30;
			$upload->saveRule = uniqid;
			$upload->thumbRemoveOrigin = true;
			if(!$upload->upload()){
				$this->error($upload->getErrorMsg());
			}else{
				$uploadList = $upload->getUploadFileInfo();
				$_POST['face'] = $uploadList[0]['savename'];
				$_SESSION['face'] = C('YICMS_PUBLIC').'/face/f_'.$_POST['face'];
			}
		}
		$_POST['uid'] = $_SESSION['uid'];
		$data = M('User');
		if(!$data->save($_POST)) $this->error('修改失败！');
		$_SESSION['email'] = $_POST['email'];
		$this->success('修改成功!');
	}
	public function logout(){
		session_destroy();
		$this->assign('jumpUrl','/');
		$this->success('退出成功！现在将返回主页!'); 
	}
	public function checkFetchForm(){
		$uname = htmlspecialchars($_POST['uname']);
		$email = htmlspecialchars($_POST['email']);
		$list = M('User')->field('email')->where("uname = '$uname'")->find();
		if(empty($list)) exit('-1');
		if($list['email'] != $email) exit('-2');
		echo '1';
	}
	public function checkFetch(){
		$uname = htmlspecialchars($_POST['uname']);
		$email = htmlspecialchars($_POST['email']);
		$list = M('User')->field('uid,password,email')->where("uname = '$uname' AND email = '$email'")->find();
		if(empty($list)){
			$this->error('用户名或邮箱错误..');
		}else{
			import("@.ORG.Smtp");
			$smtpserver = C('smtpserver'); 
			$port = C('smtpport');
			$smtpuser = C('smtpuser');
			$smtppwd = C('smtppwd');
			$mailtype = "TXT";
			$sender = C('smtpuser');
			$smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender); 
			$to = $list['email']; 
			$subject = iconv('UTF-8','gb2312',C('fetchtitle'));
			$code = C('URL').U('User/Index/checkFetchPass?uid='.$list['uid'].'&code='.md5($list['uid'].$list['password'].$list['email']));
			$fetchcontent = C('fetchcontent');
			$fetchcontent = str_replace('{username}',$uname,$fetchcontent);
			$fetchcontent = str_replace('{time}',switchtime($_SERVER['REQUEST_TIME']),$fetchcontent);
			$fetchcontent = str_replace('{code}',$code,$fetchcontent);
			$body = iconv('UTF-8','gb2312',$fetchcontent);
			$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);
			$this->assign('waitSecond',15);
			$this->success('请访问你的邮箱 '.$list['email'].' 验证邮箱后登录!<br/>');
		}
	}
	public function checkFetchPass(){
		$uid = (int)$_GET['uid'];
		$user = M('User')->field('uid,email,uname,password')->where("uid = $uid")->find();
		if(empty($user)){
			$this->error('用户不存在!');
		}else{
			$code = $_GET['code'];
			if(md5($user['uid'].$user['password'].$user['email']) == $code){
				$this->assign('uname',$user['uname']);
				$USER_PUBLIC = C('USER_PUBLIC');
				$this->assign('USER_PUBLIC',$USER_PUBLIC);
				$YICMS_PUBLIC = C('YICMS_PUBLIC');
				$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
				$config = M('Config')->find();
				$config['title'] = '找回密码-'.$config['title'];
				$this->assign('config',$config);
				$this->display();
			}else{
				$this->error('已失效!');
			}
		}
	}
	public function editFetchPass(){
		$password = trim($_POST['password']);
		if(empty($password)) $this->error('密码不能为空!');
		if(strlen($password)<6) $this->error('密码不能少于6位!');
		$password = md5(trim($_POST['password']));
		$repassword = md5(trim($_POST['repassword']));
		if($password != $repassword || empty($password)) $this->error('两次密码不一致!');
		$uid = (int)$_POST['uid'];
		$user = M('User')->field('uid,email,uname,password')->where("uid = $uid")->find();
		if(empty($user)){
			$this->error('用户不存在!');
		}else{
			$code = $_POST['code'];
			if(md5($user['uid'].$user['password'].$user['email']) == $code){
				$list['uid'] = $uid;
				$list['password'] = $password;
				M('User')->data($list)->save();
				$this->assign('jumpUrl',U('Index/login'));
				$this->success('恭喜修改成功!现在前去登录!');
			}else{
				$this->error('已失效!');
			}
		}
	}
	public function verify(){
		import("@.ORG.Image"); 
		Image::buildImageVerify(4,1,'gif',53,25); 
	}
}
?>
