<?php
if(!defined('R_P')) exit('Access Denied');
$id = intval($_GET['id']);
$channelid = intval( $_GET['channelid']);
if( !$id || !$channelid || empty($channel[$channelid])) qbase::purviewCheckDIe('error request');
$user_info = qbase::get_object('login')->checkSession();

if($_POST['myFormAc'] == 'update'){
	$location = W_P."index.php?ac=comment&channelid={$channelid}&id={$id}";
	$param = array();
	if($user_info['uid']){
		if(false === ($usergroup = cache::get('sys_usergroup'))){
			$usergroup = qbase::get_object("group")->cache();
		}
		if(!$usergroup[$user_info['gid']]['purview']['reply']) qbase::showmsg("出错了，你所在的用户组没有评论权限", SKIN, $location);
		$param['uid'] = $user_info['uid'];
		$param['username'] = addslashes($user_info['username']);
	}else if($_SET['comment_guest']){
		$param['uid'] = 0;
		$param['username'] = '';
	}else{
		qbase::showmsg("需要登录后才能发表评论", 'default', "user.php?ac=login&forward=" . urlencode($main_location));
	}
	if(intval($_SET['comment_code']) > 0){
		ini_get('session.auto_start') || @session_start();
		$code = strtolower(trim($_SESSION['code']));
		$verify_code = strtolower(trim($_POST['verify_code']));
		unset($_SESSION['code']);
		if(!$code || $code != $verify_code) qbase::showmsg("出错了，验证码错误", SKIN, $location);
	}
	$param['content'] = trim(htmlspecialchars(qbase::FilterString(strip_tags($_POST['content']))));
	$param['ip'] = qbase::get_ip();
	if(!$param['content'] ) qbase::purviewCheckDIe('请填写完整评论内容');
	$param['addtime'] = $_SERVER['REQUEST_TIME'];
	$param['channelid'] = $channelid;
	$param['vote'] = intval($_POST['vote']);
	$param['id'] = $id;
	$param['is_pass'] = intval($channel[$channelid]['coment_pass']) ;
	$param['type'] = 'comment';
	if($param['vote'] < 0 || $param['vote'] > 2) qbase::purviewCheckDIe('error request');
	
	require_once(R_P . "/include/class.comment_verify.php");
	$verify = new comment_verify;
	if($verify->check_verify($param['channelid'], $param['id']) > 0 || $verify->check_referer() === false) qbase::purviewCheckDIe('error request');
	$status = $verify->check_word($param['content']);

	if($status > 1){
		$verify->update_sp('del');
		qbase::purviewCheckDIe('您所提交的评论含有危险语句，已被禁止');
	}else if($status == 1){
		$param['is_pass'] = 0;
		$verify->update_sp('forbidden');
	}
	qbase::get_object("comment", $channelid, $id)->add($param);
	qbase::showmsg('谢谢您的评论', 'default', $location);
}else{
	if(AJAX === false && JSONP === false){
		$info = qbase::get_object('content', $channelid, $id)->get_item();
		if(empty($info)) qbase::showmsg('没有找到评论对象');
		if($info['nocoment']==1 || $info['c_allow'] == 0) qbase::showmsg('该内容被禁止评论', 'default', $info['url']);
		$keyword = $description = $info['subject'];
	}

	require_once( R_P."/include/inc.ubb.php");
	$pageno = intval($_GET['pageno']) > 0 ? intval($_GET['pageno']) : 1;
	$pagesize = 10 ;
	$offset = ($pageno - 1) * $pagesize;

	$data = qbase::get_object('comment', $channelid, $id, intval($_SET['comment_self_see']))->get($offset, $pagesize);
	$total = $data['total'];
	$comments = array();
	if($total > 0){
		$pager = qbase::page($total , $pageno, $pagesize, 1);
	}else{
		$pager = '';
	}

	$vote = array(
		0 => '差评',
		1 => '中评',
		2 => '好评'
	);
	
	foreach($data['data'] as $val){
		$val['ip'] = qbase::formatIp($val['ip']);
		$val['tip'] = $vote[$val['vote']];
		$comments[] = $val;
	}
	$verify = intval($_SET['comment_random_verify']) > 0 ? qbase::get_object('comment_verify')->create($channelid, $id) : '';
	if(JSONP == false){
		include qbase::template(AJAX ? $_SET['skin'] . '/comment_ajax' : $_SET['skin'] . '/comment');
	}else{
		include qbase::template($_SET['skin'] . '/comment_ajax');
		$c = ob_get_contents();
		ob_clean();
		echo qbase::xmlprint($c);
	}
}
?>