<?php
/*
 *		Powered by YiCms 
 *
 *		mailto:yixiao@yicms.cn
 *
 */
class TicketAction extends Action {
	public function ticketAjax(){
		if(empty($_COOKIE['ticketId'])){
			$ticket = M('News')->where('state=0')->order('id ASC')->find();
			setcookie('ticketId',$ticket['id'],$_SERVER['REQUEST_TIME']+100000000);
		}else{
			$id = $_COOKIE['ticketId'];
			$ticket = M('News')->where("state=0 AND id > $id")->order('id ASC')->find();
			if(!empty($ticket)) setcookie('ticketId',$ticket['id'],$_SERVER['REQUEST_TIME']+100000000);
		}
		if(empty($ticket)){
			$echo = C('ticketmsg');
		}else{	
			if(!empty($ticket['tag'])) $ticket['tag'] = explode(' ',$ticket['tag']);
			$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'];
		$data = M('News');
		if(!empty($vote)){
			if($vote<0) $data->where("id = $id")->setInc('no');
			else $data->where("id = $id")->setInc('yes');
			$data->where("id = $id")->setField('time',time());
			$list =$data->field('id,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').'/article/'.$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();
	}

}
?>