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

$action   = $_GET['action'] ? $_GET['action'] : 'manage';
switch($action){
	case 'manage':
		$data = qdb::fetchAll("select * from  ".TABLEPRE."comment_filter order by act desc, fid asc");
	break;

	case 'add':
		$fid = intval($_GET['fid']);
		if($fid > 0){
			$_output = qdb::queryFirst("select * from ".TABLEPRE."comment_filter  where fid = '{$fid}' ");
			if(empty($_output)) die("<p class='padding'>bad request</p>");
		}
		if(isset($_POST['myFormAc'])){
			$param = array();
			$param['keyword'] = trim($_POST['keyword']);
			$param['type'] = intval($_POST['type']);
			$param['act'] = intval($_POST['act']);
			if(empty($param['keyword'])) die("<p class='padding'>需要填写关键词</p>");
			
			$num = qdb::queryAlone("select fid from ".TABLEPRE."comment_filter where keyword = '{$param['keyword']}' ");
			if($fid == 0){
				if($num > 0) die("<p class='padding'>该关键词汇已存在，不能添加</p>");
				$sql = qbase::createSql(TABLEPRE."comment_filter", $param, 'insert');
			}else{
				if($num > 0 && $num != $fid)  die("<p class='padding'>该关键词汇已存在，不能使用该词汇</p>");
				$sql = qbase::createSql(TABLEPRE."comment_filter", $param, 'update') . " where fid = '{$fid}' ";
			}
			qdb::query($sql);
			qbase::get_object('comment_verify')->delete_cache();
			adminLog("添加垃圾评论词汇");
			echo qbase::xmlprint("<p class='padding'>操作成功</p>");
			exit();
		}
	break;

	case 'load':
		if(isset($_POST['myFormAc'])){
			$param = array();
			$keyword = trim($_POST['keyword']);
			$keyword = str_replace(array("\r\n","\r","\t","，",","), "\n", $keyword);
			$param['type'] = intval($_POST['type']);
			$param['act'] = intval($_POST['act']);

			if(empty($keyword)) die("<p class='padding'>需要填写关键词</p>");
			$keyword = explode("\n", $keyword);
			foreach($keyword as $val){
				$val = str_replace(array("\r","\t","\n"),"", $val);
				$param['keyword'] = trim($val);
				if(empty($param['keyword'])) continue;
				$num = qdb::queryAlone("select fid from ".TABLEPRE."comment_filter where keyword = '{$param['keyword']}' ");
				if($num > 0) continue;
				$sql = qbase::createSql(TABLEPRE."comment_filter", $param, 'insert');
				qdb::query($sql);
			}
			qbase::get_object('comment_verify')->delete_cache();
			adminLog("批量添加垃圾评论词汇");
			echo qbase::xmlprint("<p class='padding'>操作成功</p>");
			exit();
		}
	break;

	case 'delete':
		checkAdminReferer();
		$fid = intval($_GET['fid']);
		if($fid > 0){
			qdb::query("delete from ".TABLEPRE."comment_filter  where fid = '{$fid}' ");
			qbase::get_object('comment_verify')->delete_cache();
			qbase::showmsg("操作成功", "default", $_SERVER['HTTP_REFERER']);
		}else if(!empty($_POST['ids'])){
			$ids = $_POST['ids'];
			$id = implode(',', $ids);
			qdb::query("delete from ".TABLEPRE."comment_filter  where fid IN ({$id}) ");
			qbase::get_object('comment_verify')->delete_cache();
			qbase::showmsg("操作成功", "default", $_SERVER['HTTP_REFERER']);
		}else{
			qbase::showmsg("bad request", "default", $_SERVER['HTTP_REFERER']);
		}
		adminLog("删除垃圾评论词汇");
	break;

	case 'count':
		$temp = qdb::fetchAll("select * from ".TABLEPRE."comment_spam  order by id desc limit 14 ");
		$count = qdb::queryFirst("select sum(forbidden) as a, sum(del) as b  from ".TABLEPRE."comment_spam ");
		krsort($temp);
		reset($temp);
		$data = array(
			array(),array()
		);
		$max = 0;
		$title = $data_json = array();
		foreach($temp as $key=>$val){
			$d = substr($val['dateline'], 5);
			$data[0][] = array($d, intval($val['forbidden']));
			$data[1][] = array($d, intval($val['del']));
			$max = max($max, $val['forbidden'], $val['del']);
		}
		$data_json[0] = qbase::jsonencode($data[0]);
		$data_json[1] = qbase::jsonencode($data[1]);
		//print_r($data);
	break;
}

include qbase::template($path . 'comment_filter');
?>