<?php
if(!defined('MOD'))  die("access die");

$action   = $_GET['action'] ? $_GET['action'] : 'manage';
$api   = intval($_GET['api']);
$pageno   = intval($_GET['pageno']);
$pagesize = 20;
$pageno   = $_POST['pageno']?$_POST['pageno']:$pageno;
$id  = intval( $_GET['id']);
if ($pageno < 1) $pageno = 1;

if($_POST['many']){
	checkAdminReferer();
	$commentid = $_POST['commentid'];
	$aid = @array_unique($_POST['aid']);
	$url = $_SERVER['HTTP_REFERER'];
	if(is_array($commentid)){
		if($action=='is_pass'){
			$is_pass = intval($_GET['is_pass']);
			while (list($key, $val) = each($commentid)){
				qdb::Query("update `".TABLEPRE."comment` set `is_pass`='{$is_pass}'  WHERE `commentid` = '$val' LIMIT 1");
				qbase::get_object("comment", $channelid, 0)->clear($val);
			}
			qbase::get_object('event', 'comment', array('action'=>'pass', 'param'=>$commentid, 'val'=>$is_pass));
			adminLog("批量审核评论");
			qbase::showmsg("批量修改成功",'default',$url);
		}
		if($action=='del'){
			$ids = array();
			while (list($key, $val) = each($commentid)){
				$id = qdb::queryAlone("select id from `".TABLEPRE."comment`  where commentid='{$val}' ");
				if($id > 0){
					$ids[] = $id;
					qdb::Query("delete FROM `".TABLEPRE."comment` WHERE `commentid` = '$val' LIMIT 1");
					qbase::get_object("comment", $channelid, $id)->clear();
				}
			}
			qbase::get_object('event', 'comment', array('action'=>'del', 'param'=>$commentid, 'val'=>$ids));
			adminLog("批量删除评论");
			qbase::showmsg("批量删除成功",'default',$url);
		}
	}
	qbase::showmsg("param error",'default', $url);
}

if($action=='pass' || $action=='del'){
	checkAdminReferer();
	$is_pass=intval($_GET['is_pass']);
	$commentid=intval($_GET['commentid']);
	$id = qdb::queryAlone("select id from `".TABLEPRE."comment`  where commentid='{$commentid}' ");
	if($id > 0){
		if($action=='pass'){
			qdb::Query("update `".TABLEPRE."comment` set `is_pass`='{$is_pass}' where commentid='{$commentid}'");
			qbase::get_object('event', 'comment', array('action'=>$action, 'param'=>$commentid, 'val'=>$is_pass));
			adminLog("审核评论");
		}else{
			qdb::Query("delete from  `".TABLEPRE."comment`  where commentid='{$commentid}' limit 1");
			qbase::get_object('event', 'comment', array('action'=>'del', 'param'=>$commentid, 'id'=>$id));
			adminLog("删除评论");
		}
		qbase::get_object("comment", $channelid, $id)->clear();
	}
	$data = array( 1);
	echo qbase::xmlPrint(qbase::jsonencode($data));
	exit();
}elseif($action == 'manage'){
	require_once( R_P."/include/inc.ubb.php");
	if(isset($_GET['is_pass'])){
		$is_pass = intval( $_GET['is_pass']);
		switch($is_pass){
			case 2:
				$where =" and c.is_pass>=0";
			break;
			case 1:
				$where =" and c.is_pass=1";
			break;
			case 0:
				$where =" and c.is_pass=0";
			break;
			default:
				$where =" and c.is_pass >= 0";
			break;
		}
	}else{
		$is_pass=2;
	}

	if ($keyword = $_GET['keyword']){
		$keyword = trim(rawurldecode($keyword));
		switch( intval( $_GET['type'])){
			case 1:
				$where.= " and c.username = '{$keyword}' ";
			break;
			case 2:
				$where.= " and  (c.content LIKE '%".trim($keyword)."%')";
			break;
			case 3:
				$regip = trim($keyword);
				$num = substr_count($regip, "*");
				if($num == 1){
					$ip_area = getIpArea($regip);
					if($ip_area) $where .= " and (c.ip >= {$ip_area['min']} and c.ip <= {$ip_area['max']}) ";
				}else if($num == 0){
					$ip_area = ip2long($regip);
					if($ip_area) $where .= " and c.ip = '{$ip_area}' ";
				}
			break;
		}
	}
	if($id > 0){
		$info = qdb::QueryFirst("select a.id, a.subject, a.filename, a.pay, c.ciddir, c.html, c.pur_browse ,a.dateline from ".TABLEPRE."{$_MODULE[$channelselect][table_name]}_{$channelid} as a left join ".TABLEPRE."acategory as c on a.cid=c.cid where a.id='$id' limit 1");
		if (empty($info)) qbase::showmsg("request error");
		if($info['status'] == 4 ){
			$subject = "<a href=".qbase::GetHtmlDirType($channelid,$info['id'],$info['ciddir'],$info['dateline'],$info['filename'], $info['ver'], !($info['html'] || $info['pur_browse'] || $info['pay']))." target='_blank'><font color=red>".$info['subject']."</font></a>";
		}else{
			$subject = "<span title='未能发布'>" . $info['subject'] . "</span>";
		}
		$where.=" and c.id='$id'";
	}
	$total = qdb::QueryAlone("select count(*) from `".TABLEPRE."comment` as c where c.channelid='$channelid' {$where} ");
	$pages = ceil($total / $pagesize);
	if(($pageno > $pages)&&($pages>0)) $pageno = $pages;

	$offset = intval($pageno - 1) * $pagesize;
	$comments = qdb::fetchAll("select c.* from `".TABLEPRE."comment` as c 
									left join ".TABLEPRE."{$_MODULE[$channel[$channelid][channelselect]][table_name]}_{$channelid} as s on c.id=s.id 
									left join ".TABLEPRE."acategory as p on p.cid=s.cid where c.channelid=".$channelid.$where." order by c.commentid desc limit ".$offset.",".$pagesize );
	foreach($comments as &$comment)
	{
		$comment['addtime'] = qbase::formatTime('Y-m-d G:i',$comment['addtime']);
		$comment['content'] = ubb(qbase::substrs( $comment['content'], 100));
	}
	unset($comment);
}
include qbase::template($path . 'comment');
?>