<?php

/**
 *      [Dzz!] (C)2010-2011 dzz.cc
 *      This is NOT a freeware, use is subject to license terms
 *
 * ============================================================================
 * 版权所有 2010-2011 Dzz!Desktop，并保留所有权利。
 * 网站地址: http://dzz.cc；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式、任何目的的再发布。
 * ============================================================================
 */
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
$refer=dreferer();
dzz_cpheader();

if($operation=='delete'){
	
	require_once  DISCUZ_ROOT.'./dzz/comment/function_comment.php';
	//cpmsg(DISCUZ_ROOT.'./dzz/comment/function_comment.php');
	$detail=$_POST['detail'];
	if($detail){
		$cids=$_POST['delete'];
		dzz_deletecomments($cids);
	}else{
		$cids=authcode($_POST['cids'],'DECODE');
		dzz_deletecomments(explode(',',$cids));
	}
	cpmsg($lang['do_success'],$_POST['refer'],'succeed');
}elseif($operation=='search'){
	
	$gets=array();
	$gets['detail'] = !empty($_GET['authorid']) ? true : $_GET['detail'];
	$gets['idtype'] = $_GET['idtype'];
	$gets['id'] = $_GET['id'];
	$gets['author'] = $_GET['author'];
	$gets['authorid'] = $_GET['authorid'];
	$gets['uid ']= $_GET['uid'];
	$gets['message'] = $_GET['message'];
	$gets['ip'] = $_GET['ip'];
	$gets['users'] = $_GET['users'];
	$gets['starttime'] = $_GET['starttime'];
	$gets['endtime'] = $_GET['endtime'];
	$gets['starttime'] = !preg_match("/^(0|\d{4}\-\d{1,2}\-\d{1,2})$/", $gets['starttime']) ? dgmdate(TIMESTAMP - 86400 * 7, 'Y-n-j') : $gets['starttime'];
	$gets['endtime'] = $_G['adminid'] == 3 || !preg_match("/^(0|\d{4}\-\d{1,2}\-\d{1,2})$/", $gets['endtime']) ? dgmdate(TIMESTAMP, 'Y-n-j') : $gets['endtime'];
	$gets['searchsubmit'] = $_GET['searchsubmit'];
	$gets['perpage'] = intval($_GET['perpage']);
	$gets['cids'] = $_GET['cids'];
	$gets['action']=$action;
	$gets['operation']=$operation;
	$page = max(1, $_GET['page']);
	$gets['searchsubmit']=$_G['searchsubmit']?true:false;
	$theurl=ADMINSCRIPT."?".url_implode($gets);
	if(submitcheck('searchsubmit', 1)) {
		$gets['searchsubmit']=true;
		$theurl=ADMINSCRIPT."?".url_implode($gets);
		$comments = $commentcount = '0';
		$sql = $error = '';
		$author = trim($author);

		if($gets['id'] !='') {
			$sql .=" AND c.id IN ('".str_replace(',', '\',\'', str_replace(' ', '', $gets['id']))."')";
		}

		if($gets['author'] != '') {
			$authorids = array();
			$query = DB::query("SELECT uid FROM ".DB::table('common_member')." WHERE username IN ('".str_replace(',', '\',\'', str_replace(' ', '', $gets['author']))."')");
			while($arr = DB::fetch($query)) {
				$authorids[] = intval($arr['uid']);
			}
			$authorid = ($authorid ? $authorid.',' : '').implode(',',$authorids);
		}

		$authorid = trim($authorid, ', ');
		if($gets['authorid'] != '') {
			$sql .= " AND c.authorid IN ('".str_replace(',', '\',\'', str_replace(' ', '', $gets['authorid']))."')";
		}

		if($gets['idtype'] != '') {
			$sql .= " AND c.idtype='$gets[idtype]'";
		}

		if($gets['starttime'] != '') {
			$starttime = strtotime($gets['starttime']);
			$sql .= " AND c.dateline>'$starttime'";
		}

		if($_G['adminid'] == 1 && $gets['endtime'] != dgmdate(TIMESTAMP, 'Y-n-j')) {
			if($gets['endtime'] != '') {
				$endtime = strtotime($gets['endtime']);
				$sql .= " AND c.dateline<'$endtime'";
			}
		} else {
			$endtime = TIMESTAMP;
		}

		if(($_G['adminid'] == 2 && $endtime - $starttime > 86400 * 16) || ($_G['adminid'] == 3 && $endtime - $starttime > 86400 * 8)) {
			$error = dzzlang('admin/template','mod_range_illegal');
		}

		$uid = trim($gets['uid'], ', ');
		if($uid !='') {
			$sql .=" AND c.uid IN ('".str_replace(',', '\',\'', str_replace(' ', '', $uid))."')";
		}

		if($gets['message'] != '') {
			$sqlmessage = '';
			$or = '';
			$message = explode(',', str_replace(' ', '', $gets['message']));

			for($i = 0; $i < count($message); $i++) {
				if(preg_match("/\{(\d+)\}/", $message[$i])) {
					$message[$i] = preg_replace("/\\\{(\d+)\\\}/", ".{0,\\1}", preg_quote($message[$i], '/'));
					$message .= " $or c.message REGEXP '".$message[$i]."'";
				} else {
					$sqlmessage .= " $or c.message LIKE '%".$message[$i]."%'";
				}
				$or = 'OR';
			}
			$sql .= " AND ($sqlmessage)";
		}

		if($gets['ip'] != '') {
			$sql .= " AND c.ip LIKE '".str_replace('*', '%', $gets['ip'])."'";
		}

		if(!$error) {
			if(($commentcount = DB::result_first("SELECT count(*) FROM ".DB::table('dzz_comment')." c WHERE 1 $sql"))) {
				if($gets['detail']) {
					$_GET['perpage'] = intval($_GET['perpage']) < 1 ? 20 : intval($_GET['perpage']);
					$perpage =  $_GET['perpage'];
					$query = DB::query("SELECT c.cid, c.uid, c.message, c.author,c.authorid, c.idtype, c.id, c.ip, c.dateline FROM ".DB::table('dzz_comment')." c  WHERE 1 $sql ORDER BY c.dateline DESC LIMIT ".(($page - 1) * $perpage).",{$perpage}");

					$comments = array();

					while($comment = DB::fetch($query)) {
						$comment['dateline'] = dgmdate($comment['dateline']);
						$comment['message']=stripslashes($comment['message']);
						switch($comment['idtype']) {
							case 'picid':
								
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=image&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								
								break;
							case 'appid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=app&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
							case 'lid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=link&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
							case 'qid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=attach&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
							case 'vid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=video&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
							case 'mid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=music&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
						}
						
						$comment['idtype']=dzzlang('admin/template','comment_idtype_'.$comment['idtype']);
						$comments[]=$comment;
					}
					$multi = multi($commentcount, $perpage, $page, $theurl);
					
				} else {
					$query = DB::query("SELECT c.cid FROM ".DB::table('dzz_comment')." c WHERE 1 $sql");
					while($comment = DB::fetch($query)) {
						$cids .= ','.$comment['cid'];
					}
					$cids=authcode($cids, 'ENCODE');
				}
			} else{
				$error = dzzlang('admin/template','comment_post_nonexistence');
			}
		}
	}
	
}else{
	$page = max(1, $_GET['page']);
	$perpage=20;
				if($commentcount = DB::result_first("SELECT count(*) FROM ".DB::table('dzz_comment')." c WHERE c.dateline>".($_G[timestamp]-60*60*24*7))) {	
					$query = DB::query("SELECT c.cid, c.uid, c.message, c.author,c.authorid, c.idtype, c.id, c.ip, c.dateline FROM ".DB::table('dzz_comment')." c  WHERE c.dateline>($_G[timestamp]-60*60*24*7) ORDER BY c.dateline DESC LIMIT ".(($page - 1) * $perpage).",{$perpage}");

					$comments = array();

					while($comment = DB::fetch($query)) {
						$comment['dateline'] = dgmdate($comment['dateline']);
						$comment['message']=stripslashes($comment['message']);
						switch($comment['idtype']) {
							case 'picid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=image&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
							case 'appid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=app&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
							case 'lid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=link&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
							case 'qid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=attach&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
							case 'vid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=video&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
							case 'mid':
								$comment['address'] = "<a href=\"dzz.php?mod=system&op=detail&type=music&oid=$comment[id]\" target=\"_blank\">$comment[message]</a>";
								break;
						}
						
						$comment['idtype']=dzzlang('admin/template','comment_idtype_'.$comment['idtype']);
						$comments[]=$comment;
					}
				}
			$multi = multi($commentcount, $perpage, $page, ADMINSCRIPT.'?action=comment');
}

include dzztemplate('admin/dzz_comment');
dzzfooter();


?>