<?php
defined('PP_PATH') or exit;
class comment_control extends base_control{
	public $_cfg = array();
	public $_var = array();

	public function index() {
		$_GET['cid'] = (int)R('cid');
		$_GET['id'] = (int)R('id');
		$this->_var = $this->category->get_cache($_GET['cid']);
		empty($this->_var) && core::error404();
		
		$mid = (int)$this->_var['mid'];
		
		
		
		//$this->_cfg = $this->runtime->xget();
		$this->cms_content->table = 'cms_'.$this->_var['table'];
		$_show = $this->cms_content->read($_GET['id']);
		if(empty($_show['cid']) || $_show['cid'] != $_GET['cid']) core::error404();
		
		$this->_cfg['titles'] = $_show['title'];
		$this->_cfg['seo_keywords'] = empty($_show['seo_keywords']) ? $_show['title'] : $_show['seo_keywords'];
		$this->_cfg['seo_description'] = empty($_show['seo_description']) ? $_show['title']: $_show['seo_description'];
		$this->assign('pp', $this->_cfg);
		$this->assign('pp_var', $this->_var);

		$GLOBALS['run'] = &$this;
		$GLOBALS['_show'] = &$_show;
		$_ENV['_theme'] = &$this->_cfg['theme'];
		$this->display('comment.htm');
	}
	public function post() {
		//$_cfg = $this->runtime->xget();
		!empty($this->_cfg['dis_comment']) && $this->message(0, '已关闭全站评论！');
		
		//验证码
		$code = R('code','P');
		if( $code == '' ){
			E(1, '验证码不能为空！');
		}else{
			if( md5($code) != session::get('commentverify')){
				E(1, '验证码不正确！');
			}
		}
		
		//会员登录才能评论
		if( $this->_cfg['user_comment'] && !$this->_uid ){
			E(1, '登录后才能评论！');
		}
		
		
		$cid = (int) R('cid', 'P');
		$id = (int) R('id', 'P');
		$content = htmlspecialchars(trim(R('content', 'P')));
		$author = htmlspecialchars(trim(R('author', 'P')));
		$ip = ip2long(ip());
		if(empty($cid) || empty($id)) E(1, '参数不完整！');
		empty($author) && E(1, '昵称不能为空！');
		strlen($author)>20 && E(1, '昵称太长了！');
		empty($content) && E(1, '评论内容不能为空！');
		strlen($content)>1000 && E(1, '评论内容太长了！');

		$cates = $this->category->get_cache($cid);
		empty($cates) && E(1, '分类ID不正确！');
		$this->cms_content->table = 'cms_'.$cates['table'];
		$data = $this->cms_content->read($id);
		$data['iscomment'] && E(1, '不允许发表评论！');

		$status = $this->_cfg['comment_status'] ;
		$mid = $this->category->get_mid_by_cid($cid);
		
		//敏感词过滤
		$comment_filter = isset($this->_cfg['comment_filter']) ? $this->_cfg['comment_filter']:'';
		if( $comment_filter ){
			$comment_filter = str_replace(PHP_EOL, '', $comment_filter);
			$comment_filter_arr = explode(',', $comment_filter);
			foreach ($comment_filter_arr as $v){
				$content = str_replace($v, '**', $content);
			}
		}

		$maxid = $this->cms_comment->create(array(
			'id' => $id,
			'mid' => $mid,
			'uid' => $this->_uid,
			'author' => $author,
			'content' => $content,
			'ip' => $ip,
			'dateline' => $_ENV['_time'],
			'status' => $status,
		));
		if(!$maxid) {
			E(1, '写入评论表出错！');
		}
		$msg = '发表评论成功！';
		if($this->_cfg['comment_status'] < 1){ //如果不需要审核，内容的评论数量+1
			$data['comments']++;
			if(!$this->cms_content->update($data)){
				E(1, '写入内容表出错！');
			}
		}else{
			$msg = '发表评论成功！审核后显示！';
		}
		
		//登录用户评论，用户的评论数+1
		if($this->_uid){
			$user = $this->user->read($this->_uid);
			$user['comments'] += 1;
			$this->user->update($user);
		}
		E(0, $msg);
	}
	public function json() {
		$cid = (int)R('cid');
		$id = (int)R('id');

		$commentid = (int)R('commentid');
		$orderway = isset($_GET['orderway']) && $_GET['orderway'] == 1 ? 1 : -1;
		$pagenum = empty($_GET['pagenum']) ? 20 : max(1, (int)$_GET['pagenum']);
		$dateformat = empty($_GET['dateformat']) ? 'Y-m-d H:i' : base64_decode($_GET['dateformat']);
		$humandate = isset($_GET['humandate']) ? ($_GET['humandate'] == 1 ? 1 : 0) : 1;

		if(empty($cid) || empty($id) || empty($commentid)) $this->message(0, '参数不完整！');

		$cates = $this->category->get_cache($cid);
		empty($cates) && $this->message(0, '分类ID不正确！');

		// 获取评论列表
		$key = $orderway == 1 ? '>' : '<';
		$where = array('id' => $id, 'mid'=>$cates['mid'], 'status'=>0, 'commentid' => array($key => $commentid));
		
		$ret = array();
		$ret['list_arr'] = $this->cms_comment->find_fetch($where, array('commentid' => $orderway), 0, $pagenum);
		foreach($ret['list_arr'] as &$v) {
			$this->cms_comment->format($v, $dateformat, $humandate);
		}

		$end_arr = end($ret['list_arr']);
		$commentid = $end_arr['commentid'];
		$orderway = max(0, $orderway);
		$dateformat = base64_encode($dateformat);

		$ret['next_url'] = $this->_cfg['webdir']."index.php?comment-json-cid-$cid-id-$id-commentid-$commentid-orderway-$orderway-pagenum-$pagenum-dateformat-$dateformat-humandate-$humandate-status-0-ajax-1";
		$ret['isnext'] = count($ret['list_arr']) < $pagenum ? 0 : 1;

		echo json_encode($ret);
		exit;
	}
}
