<?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(submitcheck('featuresubmit')) {
	if($operation=='add' || $operation=='edit'){
		$did=intval($_GET['did']);
		if($did){
			$darr=DB::fetch_first("select * from ".DB::table('dzz_icon')." where did='{$did}'");
		}
		$setarr=array(
				'domain'=>trim($_POST['domain']),
				'uid'=>$_G['uid'],
				'username'=>$_G['username'],
				'check'=>intval($_POST['check']),
				
			);
		//处理应用图标
		
		if($_FILES['iconnew']) {
			//print_r($_FILES['iconnew']);
			//cpmsg('dfdsfd');
			if($_FILES['iconnew']['tmp_name']&& $pic=upload_to_icon($_FILES['iconnew'],$darr['pic'],$setarr['domain'])){
				$setarr['pic']=$pic;
			}
		}else {
			if($_GET['iconnew'] && $_GET['iconnew']!=$_G['setting']['attachurl'].$darr['pic']){
				if($pic=image_to_icon($_GET['iconnew'],$darr['pic'],$setarr['domain'])){
					$setarr['pic']=$pic;
				}
			}
		}
		if($setarr['check']==2){
			DB::update('dzz_icon',array('check'=>1),"domain='{$setarr[domain]}' and `check`='2'");
		}
		if($darr){
			DB::update('dzz_icon',daddslashes($setarr),"did='{$did}'");
		}else{
			$setarr['dateline']=$_G['timestamp'];
			DB::insert('dzz_icon',daddslashes($setarr));
		}
		cpmsg($lang['do_success'],$_GET['refer'],'succeed');
	}elseif($operation=='check'){
		$optypeids=$_POST['optypeids'];
		if($_POST['optype']=='ignore'){
			DB::update("dzz_icon",array('check'=>0),"did IN (".dimplode($optypeids).") and `check`<2");
		}elseif($_POST['optype']=='pass'){
			$checkids=array();
			foreach($optypeids as $did){
				$darr=array();
				$darr=DB::fetch_first("select * from ".DB::table('dzz_icon')." where did='{$did}'");
				if($pic=image_to_icon($_G['setting']['attachurl'].$darr['pic'],$darr['pic'],$darr['domain'])){
					DB::update('dzz_icon',array('pic'=>$pic,'check'=>2),"did='{$did}'");
				}
			}
		}
		cpmsg($lang['do_success'],$refer,'succeed');
	}else{
			$optypeids=$_POST['optypeids'];
			if($optypeids){
				if($_POST['optype']=='default'){
					DB::update("dzz_icon",array('check'=>3),"did IN (".dimplode($optypeids).") and `check`=2");
				}elseif($_POST['optype']=='notdefault'){
					DB::update("dzz_icon",array('check'=>2),"did IN (".dimplode($optypeids).") and `check`=3");
				}elseif($_POST['optype']=='delete'){
					foreach($optypeids as $did){
						$pic=DB::result_first("select pic from ".DB::table('dzz_icon')." where did='{$did}'");
						@unlink($_G['setting']['attachdir'].$pic);
					}
					DB::delete("dzz_icon","did IN (".dimplode($optypeids).")");
				}
			}
			$ids=$_POST['ids'];
			foreach($ids as $did){
				DB::update('dzz_icon',array('disp'=>intval($_POST['disp'][$did])),"did='{$did}'");
			}
			cpmsg($lang['do_success'],$refer,'succeed');
	}
	
}elseif($operation=='add'|| $operation=='edit'){
	$did=intval($_GET['did']);
	$icon=array();
	if($did){
		$icon=DB::fetch_first("select * from ".DB::table('dzz_icon')." where did='{$did}'");
		if(strpos($icon['pic'],'http')===false){
			$icon['pic']=$_G['setting']['attachurl'].$icon['pic'];
		}
	}
}elseif($operation=='check'){
		$page = empty($_GET['page'])?1:intval($_GET['page']);
		$username=trim($_GET['username']);
		$domain=trim($_GET['domain']);
		$day=intval($_GET['day']);
		$perpage = 9;
		$start = ($page-1)*$perpage;
		$gets = array(
				'action'=>$action,
				'operation'=>$operation,
				'username' => $username,
				'day'=>$day,
				'domain'=>$domain
			);
		$theurl = ADMINSCRIPT."?".url_implode($gets);
			$time=$_G['timestamp']-$day;
			$wheresql="`check`=1";
			
			//日期选项
			if($day>0) $wheresql.=" and dateline>$time";
			//特征
			if($domain){
				$wheresql.=" and domain='{$domain}'";
			}
			//审核状态
			
			//用户选项
			if($username) $wheresql.=" and username='{$username}'";
			if($domain)	$ordersql=" ORDER BY `disp` ,`dateline` DESC";
			else $ordersql=" ORDER BY `dateline` DESC";
			//取得所有符合条件的应用列表
			$list=array();
			$count=DB::result_first("SELECT COUNT(*) FROM ".DB::table('dzz_icon')." where $wheresql  ");
			if($count){
				$query=DB::query("SELECT * FROM ".DB::table('dzz_icon')."  where $wheresql $ordersql   LIMIT $start,$perpage" );
				while($values=DB::fetch($query)){
					$values['dateline']=dgmdate($values['dateline'],'Y-m-d');
					$values['pic']=$_G['setting']['attachurl'].$values['pic'];
					$list[]=$values;
				}
			}
			$multi = multi($count, $perpage, $page, $theurl);
			
}else{
		
		$page = empty($_GET['page'])?1:intval($_GET['page']);
		$username=trim($_GET['username']);
		$domain=trim($_GET['domain']);
		$day=intval($_GET['day']);
		$check=intval($_GET['check']);
		$srctxt=trim($_GET['srctxt']);
		$perpage =9;
		$start = ($page-1)*$perpage;
		$gets = array(
				'action'=>$action,
				'operation'=>$operation,
				'username' => $username,
				'day'=>$day,
				'check'=>$check,
				'domain'=>$domain,
				'srctxt'=>$srctxt
			);
		$theurl = ADMINSCRIPT."?".url_implode($gets);
			$time=$_G['timestamp']-$day;
			$wheresql="`check`>1";
			
			//日期选项
			if($day>0) $wheresql.=" and dateline>$time";
			//特征
			if($domain){
				$wheresql.=" and domain='{$domain}'";
			}
			if($srctxt){
				$wheresql.=" and domain LIKE '%".$srctxt."%'";
			}
			//审核状态
			if($check){
			 	$wheresql.=" and `check`='{$check}'";
			}
			//用户选项
			if($username) $wheresql.=" and username='{$username}'";
			if($domain)	$ordersql=" ORDER BY `disp` ,`dateline` DESC";
			else $ordersql=" ORDER BY `dateline` DESC";

			//取得所有符合条件的应用列表
			$list=array();
			$count=DB::result_first("SELECT COUNT(*) FROM ".DB::table('dzz_icon')." where $wheresql  ");
			if($count){
				$query=DB::query("SELECT * FROM ".DB::table('dzz_icon')."  where $wheresql $ordersql   LIMIT $start,$perpage" );
				while($values=DB::fetch($query)){
					$values['dateline']=dgmdate($values['dateline'],'Y-m-d');
					if(strpos($values['pic'],'http')===false){
						$values['pic']=$_G['setting']['attachurl'].$values['pic'];
					}
					$list[]=$values;
				}
			}
			$multi = multi($count, $perpage, $page, $theurl);
}
include dzztemplate('admin/dzz_feature');

?>