<?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'){
	
	$detail=$_POST['detail'];
	if($detail){
		$picids=$_POST['delete'];
		dzz_deleteimages($picids);
	}else{
		$picids=authcode($_POST['picids'],'DECODE');
		dzz_deleteimages(explode(',',$picids));
	}
	cpmsg($lang['do_success'],$_POST['refer'],'succeed');
}elseif($operation=='search'){
	
	$gets=array();
	$gets['detail'] = $_GET['detail']?$_GET['detail']:$_POST['detail'];
	$gets['username'] = $_GET['username']?$_GET['username']:$_POST['username'];
	$gets['picid']= $_GET['picid']?$_GET['picid']:$_POST['picid'];
	$gets['ip'] =$_GET['ip']?$_GET['ip']:$_POST['ip'];
	$gets['starttime'] =$_GET['starttime']?$_GET['starttime']:$_POST['starttime']; 
	$gets['endtime'] = $_GET['endtime']?$_GET['endtime']:$_POST['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['search'] = $_GET['search']?$_GET['search']:$_POST['search'];
	$gets['perpage'] = intval($_GET['perpage']?$_GET['perpage']:$_POST['perpage']);
	$gets['orderby'] =$_GET['orderby']?$_GET['orderby']:$_POST['orderby'];
	$gets['orderasc'] =$_GET['orderasc']?$_GET['orderasc']:$_POST['orderasc'];
	$gets['title'] =$_GET['title']?$_GET['title']:$_POST['title'];
	$gets['action']=$action;
	$gets['operation']=$operation;
	$page = max(1, $_GET['page']);
	$theurl=ADMINSCRIPT."?".url_implode($gets);
	if(submitcheck('searchsubmit', 1)) {
		$gets['search']=true;
	}
	$theurl=ADMINSCRIPT."?".url_implode($gets);
	$comments = $commentcount = '0';
	$sql = $error = $ordersql='';
	$username = trim($gets['username'], ', ');
	if($username!='') {
		$sql .= " and username IN ('".str_replace(',', '\',\'', str_replace(' ', '', $username))."')";
	}

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

	if($_G['adminid'] == 1 && $gets['endtime'] != dgmdate(TIMESTAMP, 'Y-n-j')) {
		if($gets['endtime'] != '') {
			$endtime = strtotime($gets['endtime']);
			$sql .= " AND 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');
	}

	$picid = trim($gets['picid'], ', ');
	if($picid !='') {
		$sql .=" AND picid IN ('".str_replace(',', '\',\'', str_replace(' ', '', $picid))."')";
	}
	if($gets['ip'] != '') {
		$sql .= " AND ip LIKE '".str_replace('*', '%', $gets['ip'])."'";
	}
	if($gets['title'] != '') {
		$sql .= " AND title LIKE '%$gets[title]%'";
	}
	if($gets['orderby'] != '') {
		$sql .= " order by $gets[orderby] ".strtoupper($gets['orderasc']);
	}else{
		$sql .= " order by dateline ".strtoupper($gets['orderasc']);
	}
	
	
	if(!$error) {
		if(($count = DB::result_first("SELECT count(*) FROM ".DB::table('dzz_image')."  WHERE 1 $sql"))) {
			if($gets['detail']) {
				$gets['perpage'] = intval($gets['perpage']) < 1 ? 20 : intval($gets['perpage']);
				$perpage =  $gets['perpage'];
				$query = DB::query("SELECT * FROM ".DB::table('dzz_image')."   WHERE 1 $sql  LIMIT ".(($page - 1) * $perpage).",{$perpage}");

				$list = array();

				while($value = DB::fetch($query)) {
					$value['dateline'] = dgmdate($value['dateline']);
					$value['imgsrc']=$_G['setting']['attachurl'].$value['filepath'];
					$value['pic']=$_G['setting']['attachurl'].($value['thumb']?$value['filepath'].'.thumb.jpg':$value['filepath']);
					$list[]=$value;
				}
				$multi = multi($count, $perpage, $page, $theurl);
				
			} else {
				$query = DB::query("SELECT picid FROM ".DB::table('dzz_image')."  WHERE 1 $sql");
				while($value = DB::fetch($query)) {
					$picids .= ','.$value['picid'];
				}
				$picids=authcode($picids, 'ENCODE');
			}
		} else{
			$error = dzzlang('admin/template','image_post_nonexistence');
		}
	}
	
	
	
}else{
	$page = max(1, $_GET['page']);
	$perpage=20;
				if($count = DB::result_first("SELECT count(*) FROM ".DB::table('dzz_image')." WHERE dateline>".($_G[timestamp]-60*60*24*7))) {	
					$query = DB::query("SELECT * FROM ".DB::table('dzz_image')."   WHERE dateline>($_G[timestamp]-60*60*24*7) ORDER BY dateline DESC LIMIT ".(($page - 1) * $perpage).",{$perpage}");

					$list = array();

					while($value = DB::fetch($query)) {
						$value['dateline'] = dgmdate($value['dateline']);
						$value['imgsrc']=$_G['setting']['attachurl'].$value['filepath'];
						$value['pic']=$_G['setting']['attachurl'].($value['thumb']?$value['filepath'].'.thumb.jpg':$value['filepath']);
						$list[]=$value;
					}
				}
			$multi = multi($count, $perpage, $page, ADMINSCRIPT.'?action=image');
}

include dzztemplate('admin/dzz_image');
dzzfooter();
function dzz_deleteimages($picids){
	global $_G;
	if(!$picids) return ;
	
	$query=DB::query("select picid,uid from ".DB::table('dzz_image'). " where picid IN (".dimplode($picids).")");
	while($value=DB::fetch($query)){
		dzz_delete_source($value['picid'],$value['uid'],'image');
	}
}

?>