<?php
class IndexAction extends Action {
	public function _empty($name){
		$this->all($name);
	}
	public function all($name){
		$HOME_PUBLIC = C('HOME_PUBLIC');
		$this->assign('HOME_PUBLIC',$HOME_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'){
			$data = M('Config');
			$config = $data->find();
			if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
			$this->assign('config',$config);
			$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
			$this->assign('tagList',$tagList);
			$this->display();
		}elseif($name=='page'){
			$id = (int)$_GET['id'];
			$data = M('Page');
			$page = $data->where("id = $id")->find();
			$this->assign('page',$page);
			$data = M('Config');
			$config = $data->find();
			if(empty($page['title'])) $config['title'] = $page['name'].'-'.$config['title'];
			else $config['title'] = $page['title'];
			if(!empty($page['keywords'])) $config['keywords'] = $page['keywords'];
			if(!empty($page['description'])) $config['description'] = $page['description'];
			$this->assign('config',$config);
			$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
			$this->assign('tagList',$tagList);
			$this->display();
		}elseif($name=='nlist'){
			if($_GET['id']){
				$id = (int)$_GET['id'];
				$nlist = M('Nav')->field('id,name,title,keywords,description')->where("id = $id")->find();
			}
			if(empty($nlist['name'])) $nlist['name'] = '新闻列表';
			$this->assign('list',$nlist);
			$data = M('Config');
			$config = $data->find();
			if($_GET['p']) $p = (int)$_GET['p'];
			if(empty($nlist['title'])){
				if($p) $config['title'] = $nlist['name'].'-第'.$p.'页-'.$config['title'];
				else $config['title'] = $nlist['name'].'-'.$config['title'];
			}else{
				$config['title'] = $nlist['title'].'-'.$config['title'];
			}
			if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
			if(!empty($nlist['keywords'])) $config['keywords'] = $nlist['keywords'];
			if(!empty($nlist['description'])) $config['description'] = $nlist['description'];
			$this->assign('config',$config);
			$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
			$this->assign('tagList',$tagList);
			$this->display();
		}elseif($name=='news'){
			$id = (int)$_GET['id'];
			M('News')->where("id = $id")->setInc('click',1);
			$news = M()->Table(array(C('DB_PREFIX').'news'=>'news',C('DB_PREFIX').'user'=>'user'))->where("news.id = $id AND news.state = 1 AND news.uid = user.uid")->find();
			$news['spic'] = C('URL').'/Public/news/s_'.$news['pic'];
			$news['mpic'] = C('URL').'/Public/news/m_'.$news['pic'];
			if(C('ISHTML')){
				$pid = $news['pid'];
				$nav = M('Nav')->where("id = $pid")->find();
				$nav['dir'] = rtrim($nav['dir'],'/');
				$conname = str_replace('{id}',$news['id'],$nav['conname']);
				$conname = str_replace('{Y}',date('Y',$news['time']),$conname);
				$news['url']= C('URL').'/'.$nav['dir'].'/'.$conname;
			}else{
				$news['url']= C('URL').'/index.php/Index/news/id/'.$news['id'];
			}
			$this->assign('news',$news);
			$data = M('Config');
			$config = $data->find();
			$config['title'] = $news['title'].'-'.$config['title'];
			if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
			$config['keywords'] = $news['title'].'-'.$config['keywords'];
			if(empty($news['video']) || empty($news['pic'])) $config['description'] = msubstr(strip_tags($news['content']),0,120,'utf-8',false);
			else $config['description'] = $news['title'].'-'.$config['description'];
			$this->assign('config',$config);
			$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
			$this->assign('tagList',$tagList);
			$this->display();
		}elseif($name=="reply"){
			$data = M('Config');
			$config = $data->find();
			$config['title'] = '在线留言-'.$config['title'];
			if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
			$this->assign('config',$config);
			$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
			$this->assign('tagList',$tagList);
			$this->display();
		}elseif($name=="search"){
			$wd = htmlspecialchars(trim($_POST['wd']));
			if(strlen($wd)<2) $this->error('您得写两个字吧!空荡荡的来,恐怕也会空空如也的回去!<br/>');
			$sql = 'INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|SELECT|OR|AND|WHERE';
			if (preg_match('/^(' . $sql . ')/i',$wd)) $this->error('请换些词再来搜索.!');
			$config = M('Config')->find();
			$config['title'] = $wd.'-糗事搜索-'.$config['title'];
			if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
			$this->assign('config',$config);
			
			$time = C('searchtime');
			if(!empty($_COOKIE['searchtime'])) $this->error('两次搜索时间不能少于'.$time.'秒钟.');
			import("@.ORG.Page");
			$count = M('News')->where("state = 1 AND title like '%$wd%'")->count('id');
			$p     = new Page ($count,20);
			$list = M('News')->where("state = 1 AND title like '%$wd%'")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
			$p->setConfig('theme','%totalRow% %header% %first% %prePage% %linkPage% %nextPage% %end%');
			$page = $p->show();
			if(!empty($list)) setcookie('searchtime',md5(rand(1,19999)),($_SERVER['REQUEST_TIME'])+$time);
			foreach($list as $key=>$value){
				if(C('ISHTML')){
					$pid = $value['pid'];
					$nav = M('Nav')->where("id = $pid")->find();
					$nav['dir'] = rtrim($nav['dir'],'/');
					$conname = str_replace('{id}',$value['id'],$nav['conname']);
					$conname = str_replace('{Y}',date('Y',$news['time']),$conname);
					$list[$key]['url']= C('URL').'/'.$nav['dir'].'/'.$conname;
				}else{
					$list[$key]['url']= C('URL').'/index.php/Index/news/id/'.$value['id'];
				}
				$list[$key]['spic'] = C('URL').'/Public/news/s_'.$value['pic'];
				$list[$key]['mpic'] = C('URL').'/Public/news/m_'.$value['pic'];
				if(!empty($value['tag'])) $list[$key]['tag'] = explode(' ',$value['tag']);
			}
			$this->assign('list',$list);
			$this->assign('page',$page);
			$this->assign('count',$count);
			$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
			$this->assign('tagList',$tagList);
			$this->display();
		}else{
			$data = M('Config');
			$config = $data->find();
			$this->assign('config',$config);
			$this->display();
		}
	}
	public function day(){
		$HOME_PUBLIC = C('HOME_PUBLIC');
		$this->assign('HOME_PUBLIC',$HOME_PUBLIC);
		$YICMS_PUBLIC = C('YICMS_PUBLIC');
		$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
		$data = M('About');
		$about = $data->find();
		$this->assign('about',$about);
		$config = M('Config')->find();
		if($_GET['p']){
			$p = (int)$_GET['p'];
			$config['title'] = '24小时内-第'.$p.'页'.$config['title'];
		}else{
			$config['title'] = '24小时内'.$config['title'];
		}
		if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
		$this->assign('config',$config);
		import("@.ORG.Page");
		$time = $_SERVER['REQUEST_TIME'] - 24*60*60;
		$count = M('News')->where("state = 1 AND time > $time")->count('id');
		$p     = new Page ($count,20);
		$list = M()->Table(array(C('DB_PREFIX').'news'=>'news',C('DB_PREFIX').'user'=>'user'))->where("news.time > $time AND news.state = 1 AND news.uid = user.uid")->order('news.time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		$p->setConfig('theme','%totalRow% %header% %first% %prePage% %linkPage% %nextPage% %end%');
		$page = $p->show();
		foreach($list as $key=>$value){
			if(C('ISHTML')){
				$pid = $value['pid'];
				$nav = M('Nav')->where("id = $pid")->find();
				$nav['dir'] = rtrim($nav['dir'],'/');
				$conname = str_replace('{id}',$value['id'],$nav['conname']);
				$conname = str_replace('{Y}',date('Y',$news['time']),$conname);
				$list[$key]['url']= C('URL').'/'.$nav['dir'].'/'.$conname;
			}else{
				$list[$key]['url']= C('URL').'/index.php/Index/news/id/'.$value['id'];
			}
			$list[$key]['spic'] = C('URL').'/Public/news/s_'.$value['pic'];
			$list[$key]['mpic'] = C('URL').'/Public/news/m_'.$value['pic'];
			if(!empty($value['tag'])) $list[$key]['tag'] = explode(' ',$value['tag']);
		}
		$this->assign('list',$list);
		$this->assign('page',$page);
		$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
		$this->assign('tagList',$tagList);
		$this->display();
	}
	public function week(){
		$HOME_PUBLIC = C('HOME_PUBLIC');
		$this->assign('HOME_PUBLIC',$HOME_PUBLIC);
		$YICMS_PUBLIC = C('YICMS_PUBLIC');
		$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
		$data = M('About');
		$about = $data->find();
		$this->assign('about',$about);
		$config = M('Config')->find();
		if($_GET['p']){
			$p = (int)$_GET['p'];
			$config['title'] = '最新一周-第'.$p.'页-'.$config['title'];
		}else{
			$config['title'] = '最新一周-'.$config['title'];
		}
		if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
		$this->assign('config',$config);
		import("@.ORG.Page");
		$time = $_SERVER['REQUEST_TIME'] - 7*24*60*60;
		$count = M('News')->where("state = 1 AND time > $time")->count('id');
		$p     = new Page ($count,20);
		$list = M()->Table(array(C('DB_PREFIX').'news'=>'news',C('DB_PREFIX').'user'=>'user'))->where("news.time > $time AND news.state = 1 AND news.uid = user.uid")->order('news.time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		$p->setConfig('theme','%totalRow% %header% %first% %prePage% %linkPage% %nextPage% %end%');
		$page = $p->show();
		foreach($list as $key=>$value){
			if(C('ISHTML')){
				$pid = $value['pid'];
				$nav = M('Nav')->where("id = $pid")->find();
				$nav['dir'] = rtrim($nav['dir'],'/');
				$conname = str_replace('{id}',$value['id'],$nav['conname']);
				$conname = str_replace('{Y}',date('Y',$news['time']),$conname);
				$list[$key]['url']= C('URL').'/'.$nav['dir'].'/'.$conname;
			}else{
				$list[$key]['url']= C('URL').'/index.php/Index/news/id/'.$value['id'];
			}
			if(!empty($value['tag'])) $list[$key]['tag'] = explode(' ',$value['tag']);
			$list[$key]['spic'] = C('URL').'/Public/news/s_'.$value['pic'];
			$list[$key]['mpic'] = C('URL').'/Public/news/m_'.$value['pic'];
		}
		$this->assign('list',$list);
		$this->assign('page',$page);
		$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
		$this->assign('tagList',$tagList);
		$this->display();
	}
	public function month(){
		$HOME_PUBLIC = C('HOME_PUBLIC');
		$this->assign('HOME_PUBLIC',$HOME_PUBLIC);
		$YICMS_PUBLIC = C('YICMS_PUBLIC');
		$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
		$data = M('About');
		$about = $data->find();
		$this->assign('about',$about);
		$config = M('Config')->find();
		if($_GET['p']){
			$p = (int)$_GET['p'];
			$config['title'] = '最新一个月-第'.$p.'页-'.$config['title'];
		}else{
			$config['title'] = '最新一个月-'.$config['title'];
		}
		if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
		$this->assign('config',$config);
		import("@.ORG.Page");
		$time = $_SERVER['REQUEST_TIME'] - 365*24*60*60;
		$count = M('News')->where("state = 1 AND time > $time")->count('id');
		$p     = new Page ($count,20);
		$list = M()->Table(array(C('DB_PREFIX').'news'=>'news',C('DB_PREFIX').'user'=>'user'))->where("news.time > $time AND news.state = 1 AND news.uid = user.uid")->order('news.time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		$p->setConfig('theme','%totalRow% %header% %first% %prePage% %linkPage% %nextPage% %end%');
		$page = $p->show();
		foreach($list as $key=>$value){
			if(C('ISHTML')){
				$pid = $value['pid'];
				$nav = M('Nav')->where("id = $pid")->find();
				$nav['dir'] = rtrim($nav['dir'],'/');
				$conname = str_replace('{id}',$value['id'],$nav['conname']);
				$conname = str_replace('{Y}',date('Y',$news['time']),$conname);
				$list[$key]['url']= C('URL').'/'.$nav['dir'].'/'.$conname;
			}else{
				$list[$key]['url']= C('URL').'/index.php/Index/news/id/'.$value['id'];
			}
			$list[$key]['spic'] = C('URL').'/Public/news/s_'.$value['pic'];
			$list[$key]['mpic'] = C('URL').'/Public/news/m_'.$value['pic'];
			if(!empty($value['tag'])) $list[$key]['tag'] = explode(' ',$value['tag']);
		}
		$this->assign('list',$list);
		$this->assign('page',$page);
		$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
		$this->assign('tagList',$tagList);
		$this->display();
	}
	public function best(){
		$HOME_PUBLIC = C('HOME_PUBLIC');
		$this->assign('HOME_PUBLIC',$HOME_PUBLIC);
		$YICMS_PUBLIC = C('YICMS_PUBLIC');
		$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
		$data = M('About');
		$about = $data->find();
		$this->assign('about',$about);
		$config = M('Config')->find();
		if($_GET['p']){
			$p = (int)$_GET['p'];
			$config['title'] = '排行榜-第'.$p.'页';
		}else{
			$config['title'] = '排行榜';
		}
		$this->assign('config',$config);
		import("@.ORG.Page");
		$time = $_SERVER['REQUEST_TIME'] - 30*24*60*60;
		$count = M('News')->where("state = 1 AND time > $time")->count('id');
		$p     = new Page ($count,20);
		$list = M()->Table(array(C('DB_PREFIX').'news'=>'news',C('DB_PREFIX').'user'=>'user'))->where("news.time > $time AND news.state = 1 AND news.uid = user.uid")->order('news.yes DESC,news.time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		$p->setConfig('theme','%totalRow% %header% %first% %prePage% %linkPage% %nextPage% %end%');
		$page = $p->show();
		foreach($list as $key=>$value){
			if(C('ISHTML')){
				$pid = $value['pid'];
				$nav = M('Nav')->where("id = $pid")->find();
				$nav['dir'] = rtrim($nav['dir'],'/');
				$conname = str_replace('{id}',$value['id'],$nav['conname']);
				$conname = str_replace('{Y}',date('Y',$news['time']),$conname);
				$list[$key]['url']= C('URL').'/'.$nav['dir'].'/'.$conname;
			}else{
				$list[$key]['url']= C('URL').'/index.php/Index/news/id/'.$value['id'];
			}
			$list[$key]['spic'] = C('URL').'/Public/news/s_'.$value['pic'];
			$list[$key]['mpic'] = C('URL').'/Public/news/m_'.$value['pic'];
			if(!empty($value['tag'])) $list[$key]['tag'] = explode(' ',$value['tag']);
		}
		$this->assign('list',$list);
		$this->assign('page',$page);
		$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
		$this->assign('tagList',$tagList);
		$this->display();
	}
	public function checkVote2(){
		$id = (int)$_POST['id'];
		$kind = (int)$_POST['kind'];
		if(in_array($id,explode('%',$_COOKIE['vote2Id']))){
			echo '-1-';
		}else{
			if($kind<0){
				M('News')->where("id = $id")->setInc('no');
				$vote = explode(',',$_COOKIE['vote2Id']);
				$vote[] = $id;
				$vote2Id = implode('%',$vote);
				setcookie('vote2Id',$vote2Id,$_SERVER['REQUEST_TIME']+100000000);
				echo '-1';
			}else{
				M('News')->where("id = $id")->setInc('yes');
				$vote = explode(',',$_COOKIE['vote2Id']);
				$vote[] = $id;
				$vote2Id = implode('%',$vote);
				setcookie('vote2Id',$vote2Id,$_SERVER['REQUEST_TIME']+100000000);
				echo '+1';
			}
		}
	}
	public function showReply(){
		$id = (int)$_POST['id'];
		$reply = M()->Table(array(C('DB_PREFIX').'reply'=>'reply',C('DB_PREFIX').'user'=>'user'))->where("reply.pid = $id AND reply.state = 1 AND reply.arid = user.uid")->order('reply.artime ASC')->select();
		$echo = '<div class="comments" id="reply-'.$id.'">';
		$echo.= '<div>';
		$i = 1;
		foreach($reply as $key=>$value){
			$echo.= '<div class="comment-block">';
			$echo.= '<div class="clearfix">';
			$echo.= '<div class="avatar">';
			$echo.= '<img src="'.C('YICMS_PUBLIC').'/face/f_'.$value['face'].'"/>';
			$echo.= '</div>';
			$echo.= '<div class="reply">';
			$echo.= '<span class="userlogin"><a href="'.U('User/Index/index?uid='.$value['arid']).'">'.$value['uname'].'</a></span>';
			$echo.= '<span class="body">'.$value['arcontent'].'</span>';
			$echo.= '</div>';
			$echo.= '<div class="report"><span title="'.date('Y-m-d H:i:s',$value['artime']).'">'.$i.'</span></div>';
			$echo.= '</div>';
			$echo.= '</div>';
			$i++;
		}
		$echo.= '</div>';
		$echo.= '<div class="input-block">';
		if(empty($_SESSION['uid'])){
			$echo.= '<div class="logout login-block"><a href="javascript:showLoginForm()">登录</a>后才能发表评论</div>';
		}else{
			$echo.= '<form method="post" action="'.U('Index/checkReply').'">';
			$echo.= '<textarea class="comment_input" id="arcontent-'.$id.'" name="arcontent">';
			$echo.= '请不要发表与本内容无关的评论，您有了账号就是有身份的人了，我们可认识您。';
			$echo.= '</textarea>';
			$echo.= '<div class="row">';
			$echo.= '<input type="hidden" name="hidden-'.$id.'"/>';
			$echo.= '<button type="button" id="reply_submit" onclick="checkReply2('.$id.')">发表</button>';
			$echo.= '<label for="comment_anonymous"><input id="anonymous-'.$id.'" type="checkbox" value="1" name="anonymous"/> 匿名评论</label>';
			$echo.= '</form>';
			$echo.= '</div>';
		}
		$echo.= '</div>';
		$echo.= '</div>';
		echo $echo;
	}
	public function checkReply2(){
		$_POST['arid'] = empty($_POST['anonymous']) ? $_SESSION['uid'] : '1';
		$_POST['arname'] = empty($_POST['anonymous']) ? $_SESSION['uname'] : '匿名';
		$_POST['arcontent'] =  htmlspecialchars($_POST['arcontent']);
		$_POST['artime'] = $_SERVER['REQUEST_TIME'];
		$_POST['pid'] = (int)$_POST['pid'];
		$_POST['ip'] = ip2long(get_client_ip());
		$_POST['state'] = '1';
		if(empty($_POST['arcontent']) || $_POST['arcontent'] =='请不要发表与本内容无关的评论，您有了账号就是有身份的人了，我们可认识您。'){
			echo '0';
		}else if(M('Reply')->add($_POST)){
			$uid = $_POST['arid'];
			M('User')->where("uid = $uid")->setInc('comments');
			$id = $_POST['pid'];
			M('News')->where("id = $id")->setInc('reply');
			echo '1';
		}else{
			echo '-1';
		}	
	}
	public function tagcloud(){
		$HOME_PUBLIC = C('HOME_PUBLIC');
		$this->assign('HOME_PUBLIC',$HOME_PUBLIC);
		$YICMS_PUBLIC = C('YICMS_PUBLIC');
		$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
		$data = M('About');
		$about = $data->find();
		$this->assign('about',$about);
		import("@.ORG.Page");
		$count = M('News')->where("state = 1 AND tag IS NOT NULL")->count('id');
		$p     = new Page ($count,20);
		$news  = M('News')->where("state = 1 AND tag IS NOT NULL")->order('time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		$p->setConfig('theme','%first% %prePage% %linkPage% %nextPage% %end%');
		foreach($news as $value){
			$tag    = explode(' ',$value['tag']);
			$list[] = $tag[0];
			if(!empty($tag[1])) $list[] = $tag[1];
			if(!empty($tag[2])) $list[] = $tag[2];
		}
		$page = $p->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$data = M('Config');
		$config = $data->find();
		$config['title'] = '标签聚合-'.$config['title'];
		if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
		$this->assign('config',$config);
		$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
		$this->assign('tagList',$tagList);
		$this->display();
	}
	public function tag(){
		$HOME_PUBLIC = C('HOME_PUBLIC');
		$this->assign('HOME_PUBLIC',$HOME_PUBLIC);
		$YICMS_PUBLIC = C('YICMS_PUBLIC');
		$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
		$data = M('About');
		$about = $data->find();
		$this->assign('about',$about);
		$name = $_REQUEST['name'];
		if($name !== iconv('UTF-8','UTF-8',iconv('UTF-8','UTF-8',$name))) $name =  iconv('GBK','UTF-8',$name);
		import("@.ORG.Page");
		$count = M('News')->where("state = 1 AND title like '%$name%'")->count('id');
		$p     = new Page ($count,20);
		$list = M()->Table(array(C('DB_PREFIX').'news'=>'news',C('DB_PREFIX').'user'=>'user'))->where("news.state = 1 AND news.uid = user.uid AND news.title like '%$name%'")->order('news.time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		$p->setConfig('theme','%first% %prePage% %linkPage% %nextPage% %end%');
		$page = $p->show();
		if(is_array($list)){
			foreach($list as $key=>$value){
				if(C('ISHTML')){
					$pid = $value['pid'];
					$nav = M('Nav')->where("id = $pid")->find();
					$nav['dir'] = rtrim($nav['dir'],'/');
					$conname = str_replace('{id}',$value['id'],$nav['conname']);
					$conname = str_replace('{Y}',date('Y',$news['time']),$conname);
					$list[$key]['url']= C('URL').'/'.$nav['dir'].'/'.$conname;
				}else{
					$list[$key]['url']= C('URL').'/index.php/Index/news/id/'.$value['id'];
				}
				$list[$key]['spic'] = C('URL').'/Public/news/s_'.$value['pic'];
				$list[$key]['mpic'] = C('URL').'/Public/news/m_'.$value['pic'];
				if(!empty($value['tag'])) $list[$key]['tag'] = explode(' ',$value['tag']);
			}
			$this->assign('list',$list);
			$this->assign('page',$page);
			$this->assign('count',$count);
		}else{
			$this->assign('count',0);
		}
		$this->assign('name',$name);
		$data = M('Config');
		$config = $data->find();
		$config['title'] = '标签聚合-'.$config['title'];
		if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
		$this->assign('config',$config);
		$tagList = M('Tag')->order('taxis ASC,id DESC')->select();
		$this->assign('tagList',$tagList);
		$this->display();
	}
	//检测回复
	public function checkReply(){
		if(C('reply')) $this->error('管理员未开启此功能!');
		if(!C('replyuser') && empty($_SESSION['uid'])) $this->error('请登录后再操作!'); //需要登录后才能回复.
		if(C('userreply') && !empty($_SESSION['uid'])) $_POST['state'] = '1';  //会员回复不需要审核
		if(C('regreply')) $_POST['state'] = '1';		//游客回复不需要审核.若不需要,则会员也不需要审核.
		if($_SESSION['verify'] != md5($_POST['verify']))  $this->error('验证码错误!');
		if(empty($_POST['arqq']) && empty($_POST['arphone']))  $this->error('电话或手机您总得写一个吧!!');
		$_POST['arname'] = htmlspecialchars($_POST['arname']);
		$_POST['artime'] = $_SERVER['REQUEST_TIME'];
		$_POST['ip'] = ip2long($_SERVER['REMOTE_ADDR']);
		$_POST['arcontent'] = htmlspecialchars($_POST['arcontent']);
		$data = D('Reply');
		if ($data->create()) {
			if(!$data->add($_POST)) $this->error("提交失败!= =");
			$this->success("提交成功!^_^");
		}else{
			$this->error($data->getError());
		}
	}
	public function checkForm(){
		if(md5($_POST['verify']) != $_SESSION['verify']) exit('-1');
		$title = trim($_POST['title']);
		if(empty($title)) exit('-2');
		$content = trim($_POST['content']);
		if(empty($content) && empty($_POST['pic']) && empty($_POST['video'])) exit('-3');
		if(count(explode(' ',trim($_POST['tag'])))>3) exit('-4');
		if(!empty($_POST['pic']) && !empty($_POST['video'])) exit('-5');
		if(!empty($_POST['video']) && !file_get_contents($_POST['video'])) exit('-6');
		if(!empty($_POST['email']) && !preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i",$_POST['email'])) exit('-7');
	}
	
	public function checkAdd(){
		//获取用户UID
		if(empty($_SESSION['uid']) || $_POST['anonymous']) $_POST['uid'] = '1';
		else $_POST['uid'] = $_SESSION['uid'];
		//获取用户名
		if(empty($_SESSION['uname']) || $_POST['anonymous']) $_POST['author'] = '匿名发布';
		else $_POST['author'] = $_SESSION['uname'];
		//时间
		$_POST['time'] = $_SERVER['REQUEST_TIME'];
		$_POST['tag'] =  htmlspecialchars(trim($_POST['tag']));
		$_POST['title'] =  htmlspecialchars(trim($_POST['title']));
		if(!empty($_SESSION['uid'])){
			$uid = $_SESSION['uid'];
			$user = M('User')->field('email')->where("uid = $uid")->find();
			$_POST['email'] = $user['email'];
		}	
		$_POST['content'] =  trim($_POST['content']);
		if($_FILES['pic']['error'] != '4'){
			import("@.ORG.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize =20971520;
			$upload->allowExts = explode(',','jpg,gif,png,jpeg');
			$upload->savePath ='Public/news/';
			if($_FILES['pic']['type']!='image/gif'){
				$upload->thumb=true;
				$upload->imageClassPath = '@.ORG.Image';
				$upload->thumbPrefix = 'm_,s_';
				$upload->thumbMaxWidth  = C('newsmwidth').','.C('newsmwidth');
				$upload->thumbMaxHeight = C('newsmheight').','.C('newsmheight');
				$upload->thumbRemoveOrigin = true;
			}
			$upload->saveRule = uniqid;
			if(!$upload->upload()){
				$this->error($upload->getErrorMsg());
			}else{
				$uploadList = $upload->getUploadFileInfo();
				$_POST['pic'] = $uploadList[0]['savename'];
				if($_FILES['pic']['type']!='image/gif'){
					import("@.ORG.Image");
					Image::watermark($uploadList[0]['savepath'].'m_'.$uploadList[0]['savename']);
					Image::watermark($uploadList[0]['savepath'].'s_'.$uploadList[0]['savename']);
				}else{
					copy($uploadList[0]['savepath'].$uploadList[0]['savename'],$uploadList[0]['savepath'].'m_'.$uploadList[0]['savename']);
					copy($uploadList[0]['savepath'].$uploadList[0]['savename'],$uploadList[0]['savepath'].'s_'.$uploadList[0]['savename']);
					unlink($uploadList[0]['savepath'].$uploadList[0]['savename']);
				}	
			}
			$_POST['pid'] = 3;
		}elseif(!empty($_POST['video'])){
			$_POST['video'] =  htmlspecialchars(trim($_POST['video']));
			$_POST['pid'] = 4;
		}else{
			$_POST['pid'] = 2;
		}
		if(!M('News')->add($_POST)) $this->error('发布失败!您可以重新试试.');
		if(empty($_SESSION['uid']) || !empty($_POST['anonymous'])) $uid='1';
		else $uid = $_SESSION['uid'];
		M('User')->where("uid = $uid")->setInc('articles');
		$_SESSION['articles'] = $_SESSION['articles'] + 1 ; 
		$this->success('发布成功!已加入审核队列.');
	}
	public function ticketAjax(){
		if(empty($_COOKIE['ticketId'])){
			$ticket = M('News')->where('state=0')->find();
			setcookie('ticketId',$ticket['id'],$_SERVER['REQUEST_TIME']+100000000);
		}else{
			$id = $_COOKIE['ticketId'];
			$ticket = M('News')->where("state=0 AND id > $id")->find();
			if(!empty($ticket)) setcookie('ticketId',$ticket['id'],$_SERVER['REQUEST_TIME']+100000000);
		}
		if(empty($ticket)){
			$echo = '---暂时没有新的糗事!---';
		}else{	
			if(!empty($ticket['tag'])) $ticket['tag'] = explode(' ',$ticket['tag']);
			$echo = '<span id="title">【标题】'.$ticket['title'].'</span><br/>';
			$echo.= '<span id="content">【内容】'.$ticket['content'].'</span><br/>';
			if(!empty($ticket['pic'])) $echo.= '<div id="pic"><img src="'.C('YICMS_PUBLIC').'/news/s_'.$ticket['pic'].'"/></div>';
			if(!empty($ticket['video'])) $echo.= '<div><embed src="'.$ticket['video'].'" type="application/x-shockwave-flash" width="300" height="200" quality="high" allowfullscreen="true"/></div>';
			if(!empty($ticket['tag'])){
				$echo.= '<div style="margin:20px 0 0 0" id="tag_line"><b style="color:green">标签</b>';
				foreach($ticket['tag'] as $value){
					$echo.= '&nbsp;&nbsp;'.$value;
				}
				$echo.= '</div>';
			}
		}	
		echo $echo;
	}
	public function checkTicket(){
		$vote = (int)$_POST['vote'];
		$id = $_COOKIE['ticketId'];
		if(!empty($vote)){
			if($vote<0){
				M('News')->where("id = $id")->setInc('no');
			}else{
				M('News')->where("id = $id")->setInc('yes');
			}
			$data = M('News');
			$list = $data->field('id,author,email,yes,no')->where("id = $id")->find();
			$yes = C('netpass');
			$no = C('netnopass');
			if($list['yes']>=$yes){
				$data->where("id = $id")->setField('state','1');
				$passemail = C('passemail');
				if(!empty($list['email']) && $passemail){
					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('smtpsubject'));
					$smtpbody = str_replace('{username}',$list['author'],C('smtpbody'));
					$smtpbody = str_replace('{url}',C('URL').U('Home/Index/news?id='.$list['id']),$smtpbody);
					$smtpbody = str_replace('{time}',switchtime($_SERVER['REQUEST_TIME']),$smtpbody);
					$body = iconv('UTF-8','gb2312',$smtpbody);
					$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);
				}
			}
			if($list['no']>=$no)	$data->where("id = $id")->delete();
		}
		$this->ticketAjax();
	}
	public function log(){
		echo 'document.write(\'';
		if(empty($_SESSION['uid'])){
			echo '<div class="logout">';
			echo '<a href="'.U('User/Index/login').'">登录</a> ';
			echo '<a href="'.U('User/Index/reg').'">注册</a>';
			echo '</div>';
		}else{
			echo '<div class="login" style="display:block">';
			echo '<a href="'.U('User/Index/index').'" class="username">'.$_SESSION['uname'].'</a> ';
			echo '<a href="'.U('User/Index/logout').'" class="exit">退出</a> ';
			echo '<a href="'.U('User/Index/msg').'" class="message" id="tip-message" title="小纸条"><span class="bti"></span></a> ';
			echo '<a href="'.U('User/Index/code').'" class="code" id="tip-code" title="邀请码"><span class="bti"></span></a>';
			echo '</div>';
		}
		echo '\')';
	}
	public function suggest(){
		$list['content'] = htmlspecialchars(trim($_POST['content']));
		if(empty($list['content'])){
			$this->error('请填写建议内容^_^');
		}else{
			$list['time'] = $_SERVER['REQUEST_TIME'];
			$list['ip'] = ip2long($_SERVER['REMOTE_ADDR']);
			M('Suggest')->add($list);
			$this->success('感谢您的建议!我们将及时改进!');
		}
	}
	public function history(){
		$HOME_PUBLIC = C('HOME_PUBLIC');
		$this->assign('HOME_PUBLIC',$HOME_PUBLIC);
		$YICMS_PUBLIC = C('YICMS_PUBLIC');
		$this->assign('YICMS_PUBLIC',$YICMS_PUBLIC);
		$data = M('About');
		$about = $data->find();
		$this->assign('about',$about);
		$config = M('Config')->find();
		if($_GET['p']){
			$p = (int)$_GET['p'];
			$config['title'] = '24小时内-第'.$p.'页'.$config['title'];
		}else{
			$config['title'] = '24小时内'.$config['title'];
		}
		if(stripos($config['title'],'')===false) $config['title'] = $config['title'].'';
		$this->assign('config',$config);
		/*获取时间,如果没有传time参数则取当天*/
		$time = (int)$_GET['time'];
		if(empty($time)) $time = date('Y').date('m').date('d');
		$year = substr($time,0,4);
		$month = substr($time,4,2);
		$day = substr($time,6,2);
		$startTime = mktime(0,0,0,$month,$day,$year);
		$stopTime  = mktime(23,59,59,$month,$day,$year);
		/*通过时间数据库取数据*/
		import("@.ORG.Page");
		$count = M('News')->where("state = 1 AND time>'$startTime' AND time<'$stopTime'")->count('id');
		$p     = new Page ($count,20);
		$list = M()->Table(array(C('DB_PREFIX').'news'=>'news',C('DB_PREFIX').'user'=>'user'))->where("news.state = 1 AND news.uid = user.uid AND news.time>'$startTime' AND news.time<'$stopTime'")->order('news.time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		$p->setConfig('theme','%first% %prePage% %linkPage% %nextPage% %end%');
		$page = $p->show();
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('page',$page);
		$this->assign('year',$year);
		$this->assign('month',$month);
		$this->assign('day',$day);
		/*输出日历*/
		//echo $startTime.'<br/>';
		//echo $stopTime;
		import("@.ORG.Calendar");
		$cal = new Calendar($time);
		$getMonth = $cal->getMonth();
		$this->assign('getMonth',$getMonth);
		$getDay = $cal->getDay($startTime);
		$this->assign('getDay',$getDay);
		$this->display();
	}
	public function verify(){
		import("@.ORG.Image");
		Image::buildImageVerify(4,1,'gif',53,25); 
	}
}
?>
