<?php

/**
 *      [Dzz!] (C)2010-2011 dzz.cc
 *      This is NOT a freeware, use is subject to license terms
 *
 * ============================================================================
 * 版权所有 2011-2012 DzzDesktop，并保留所有权利。
 * 网站地址: http://dzz.cc；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式、任何目的的再发布。
 * ============================================================================
 */
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
$refer=dreferer();
dzz_cpheader();
if(!submitcheck('spacesubmit')) {
	
	if($operation=='config'){
		dzz_shownav($lang['menu_space'],$lang['menu_space_config']);
		$config=DB::fetch_first("select spacebuy,buycredits from ".DB::table('dzz_config')." where id='1'");
		if(!$config['buycredits']) $config['buycredits']=$_G['setting']['creditstrans']?$_G['setting']['creditstrans']:2;
	}elseif($operation=='product'){
		dzz_shownav($lang['menu_space'],$lang['menu_space_product']);
		if($_GET['do']=='edit' || $_GET['do']=='add'){
			$config=DB::fetch_first("select spacebuy,buycredits from ".DB::table('dzz_config')." where id='1'");
			if(!$config['buycredits']) $config['buycredits']=$_G['setting']['creditstrans']?$_G['setting']['creditstrans']:2;
			$product=array();
			$pid=intval($_GET['pid']);
			if($product=dstripslashes(DB::fetch_first("select * from ".DB::table("dzz_space_product")." where pid='{$pid}'"))){
				if($product['starttime']) $product['starttime']=dgmdate($product['starttime'], 'Y-n-j');
				else $product['starttime']='';
				if($product['endtime']) $product['endtime']=dgmdate($product['endtime'], 'Y-n-j');
				else $product['endtime']='';
				$product['spacesize']=intval($product['spacesize']/(1024*1024));
			}else{
				$product['spacesize']=10;
				$product['price']=10;
				$product['price']=10;
				$product['starttime']='';
				$product['endtime']='';
			}
			
		}else{
			$config=DB::fetch_first("select spacebuy,buycredits from ".DB::table('dzz_config')." where id='1'");
			if(!$config['buycredits']) $config['buycredits']=$_G['setting']['creditstrans']?$_G['setting']['creditstrans']:2;
			$list=array();
			$query=DB::query("select * from ".DB::table('dzz_space_product')." order by disp");
			while($value=dstripslashes(DB::fetch($query))){
				$value['spacesize']=formatsize($value['spacesize']);
				if($value['starttime']) $value['starttime']=dgmdate($value['starttime'], 'Y-n-j');
				if($value['endtime']) $value['endtime']=dgmdate($value['endtime'], 'Y-n-j');
				$value['pname']=htmlspecialchars_decode($value['pname']);
				$list[]=$value;
			}
		}
	}elseif($operation=='record'){
		if($_GET['do']=='edit'){
			$config=DB::fetch_first("select spacebuy,buycredits from ".DB::table('dzz_config')." where id='1'");
			if(!$config['buycredits']) $config['buycredits']=$_G['setting']['creditstrans']?$_G['setting']['creditstrans']:2;
			$rid=intval($_GET['rid']);
			if($record=dstripslashes(DB::fetch_first("select * from ".DB::table("dzz_space_record")." where rid='{$rid}'"))){
				$record['spacesize']=$record['spacesize']/(1024*1024);
				$record['endtime']=dgmdate($record['endtime']);
				$record['dateline']=dgmdate($record['dateline']);
			}else{
				cpmsg(dzzlang('admin/template','record_not_exist'),$refer,'error');
			}
		}elseif($_GET['do']=='delete'){
			$config=DB::fetch_first("select spacebuy,buycredits from ".DB::table('dzz_config')." where id='1'");
			if(!$config['buycredits']) $config['buycredits']=$_G['setting']['creditstrans']?$_G['setting']['creditstrans']:2;
			$rid=intval($_GET['rid']);
			if($record=dstripslashes(DB::fetch_first("select * from ".DB::table("dzz_space_record")." where rid='{$rid}'"))){
				$record['spacesize']=$record['spacesize']/(1024*1024);
				$record['endtime']=dgmdate($record['endtime']);
				$record['dateline']=dgmdate($record['dateline']);
			}else{
				cpmsg(dzzlang('admin/template','record_not_exist'),$refer,'error');
			}
		}else{
			dzz_shownav($lang['menu_space'],$lang['menu_space_record']);
			$config=DB::fetch_first("select spacebuy,buycredits from ".DB::table('dzz_config')." where id='1'");
			if(!$config['buycredits']) $config['buycredits']=$_G['setting']['creditstrans']?$_G['setting']['creditstrans']:2;
				$list=array();
				$page = empty($_GET['page'])?1:intval($_GET['page']);
				$day=intval($_GET['day']);
				$uid=intval($_GET['uid']);
				$username=trim($_GET['username']);
			$perpage = 10;
			$start = ($page-1)*$perpage;
			$gets = array(
					'action'=>$action,
					'operation' => $operation,
					'uid'=>$uid,
					'username'=>$username,
					'day'=>$day,
				);
			$theurl=ADMINSCRIPT."?".url_implode($gets);
			$wheresql=" where '1'";
			if($day==1){
				$wheresql.=" and endtime>'{$_G[timestamp]}'";
			}elseif($day==2){
				$wheresql.=" and endtime<'{$_G[timestamp]}'";
			}
			if($uid) $wheresql.=" and uid='{$uid}'";
			if($username) $wheresql.=" and username='{$username}'";
			if($count=DB::result_first("select COUNT(*) from ".DB::table('dzz_space_record')." $wheresql")){
				$query=DB::query("select * from ".DB::table('dzz_space_record')." $wheresql order by dateline DESC LIMIT $start,$perpage");
				while($value=dstripslashes(DB::fetch($query))){
					if($value['dateline']) $value['dateline']=dgmdate($value['dateline']);
					if($value['endtime']) $value['endtime']=dgmdate($value['endtime']);
					$value['spacesize']=formatsize($value['spacesize']);
					$value['pname']=htmlspecialchars_decode($value['pname']);
					$list[]=$value;
				}
			}
			$multi = multi($count, $perpage, $page, $theurl);
		}
	}elseif($operation=='log'){
		dzz_shownav($lang['menu_space'],$lang['menu_space_log']);
		$config=DB::fetch_first("select spacebuy,buycredits from ".DB::table('dzz_config')." where id='1'");
		if(!$config['buycredits']) $config['buycredits']=$_G['setting']['creditstrans']?$_G['setting']['creditstrans']:2;
			$list=array();
			$page = empty($_GET['page'])?1:intval($_GET['page']);
			$day=intval($_GET['day']);
			$uid=intval($_GET['uid']);
		  	$username=trim($_GET['username']);
		$perpage = 10;
		$start = ($page-1)*$perpage;
		$gets = array(
				'action'=>$action,
				'operation' => $operation,
				'uid'=>$uid,
				'username'=>$username,
				'day'=>$day,
			);
		$theurl=ADMINSCRIPT."?".url_implode($gets);
		$wheresql=" where '1'";
		if($day==1){
			$wheresql.=" and endtime>'{$_G[timestamp]}'";
		}elseif($day==2){
			$wheresql.=" and endtime<'{$_G[timestamp]}'";
		}
		if($uid) $wheresql.=" and uid='{$uid}'";
		if($username) $wheresql.=" and username='{$username}'";
		if($count=DB::result_first("select COUNT(*) from ".DB::table('dzz_space_record_log')." $wheresql")){
			$query=DB::query("select * from ".DB::table('dzz_space_record_log')." $wheresql order by dateline DESC LIMIT $start,$perpage");
			while($value=dstripslashes(DB::fetch($query))){
				if($value['dateline']) $value['dateline']=dgmdate($value['dateline']);
				if($value['endtime']) $value['endtime']=dgmdate($value['endtime']);
				$value['spacesize']=formatsize($value['spacesize']);
				$value['pname']=htmlspecialchars_decode($value['pname']);
				$list[]=$value;
			}
		}
		$multi = multi($count, $perpage, $page, $theurl);
	}
	
	include dzztemplate('admin/dzz_spacemanage');
}else{
	
	if($operation=='config'){
			$config=DB::fetch_first("select * from ".DB::table('dzz_config')." where id='1'");		
					$config['spacebuy']=intval($_POST['spacebuy']);
					$config['buycredits']=intval($_POST['buycredits']);
				DB::update('dzz_config',daddslashes($config),"id='1'");
				
				DB::insert('common_setting', array(
				'skey' => 'dzz_sysconfig',
				'svalue' => addslashes(serialize(dstripslashes($config))),
			), false, true);
			updatecache(array('setting', 'dzz_sysconfig'));
			//开启或关闭计划任务
			//添加计划任务;
			$cron=array(
						'available'=>$config['spacebuy'],
						'type'=>'user',
						'name'=>$lang['cron_title'],
						'filename'=>'cron_dzz_spacecount_daily.php',
						'weekday'=>-1,
						'day'=>-1,
						'hour'=>0,
						'minute'=>0
						);
			if($cronid=DB::result_first("select cronid from ".DB::table('common_cron')." where filename='cron_dzz_spacecount_daily.php'")){
				DB::update('common_cron',array('available'=>$config['spacebuy']),"cronid='{$cronid}'");
			}else{
				DB::insert('common_cron',daddslashes($cronid),1);
			}
			cpmsg($lang['do_success'],$refer,'succeed');		
	
	}elseif($operation=='product'){	
		if($_POST['do']=='add'||$_POST['do']=='edit' ) {
			$pid=intval($_POST['pid']);
			if(!$_POST['pname']) cpmsg($lang['product_name_empty'],$refer,'error');
			if(intval($_POST['spacesize'])<1) cpmsg($lang['product_size_empty'],$refer,'error');
			
				$setarr=array();
				$setarr['pname']=getstr($_POST['pname']);
				$setarr['price']=intval($_POST['price']);
				$setarr['spacesize']=intval($_POST['spacesize'])*1024*1024;
				$setarr['danwei']=trim($_POST['danwei']);
				$setarr['starttime']=($_POST['starttime']!='')?strtotime($_POST['starttime']):0;
				$setarr['endtime']=($_POST['endtime']!='')?strtotime($_POST['endtime']):0;
				$setarr['description']=getstr($_POST['description']);
				
			if($pid) {
				DB::update('dzz_space_product',daddslashes($setarr),"pid='{$pid}'");
			}else{
				DB::insert('dzz_space_product',daddslashes($setarr));
			}
			cpmsg($lang['do_success'], $refer, 'succeed');
		
		}else{
			
			$delete=$_POST['delete'];
			if($delete){
				DB::delete('dzz_space_product',"pid IN(".dimplode($delete).")");
			}
			$ids=$_POST['pid'];
			foreach($ids as $pid){
				if(!in_array($pid,$delete))	DB::update('dzz_space_product',array('disp'=>intval($_POST['disp'][$pid]),'available'=>intval($_POST['available'][$pid])),"pid='{$pid}'");
			}
			cpmsg($lang['do_success'], $refer, 'succeed');
		}
		
	}elseif($operation=='record'){	
		if($_POST['do']=='edit') {
			$rid=intval($_POST['rid']);
			if(!$_POST['pname']) cpmsg($lang['product_name_empty'],$refer,'error');
				$setarr=array();
				$setarr['pname']=getstr($_POST['pname']);
				$setarr['price']=intval($_POST['price']);
				$setarr['spacesize']=intval($_POST['spacesize'])*1024*1024;
				$setarr['danwei']=trim($_POST['danwei']);
				$setarr['num']=intval($_POST['num']);
				$setarr['endtime']=strtotime(trim($_POST['dateline'])." + $setarr[num] $setarr[danwei]");
				
			if(DB::update('dzz_space_record',daddslashes($setarr),"rid='{$rid}'")){
				getBuySize(intval($_POST['uid']));
			}
			cpmsg($lang['do_success'], $refer, 'succeed');
		
		}elseif($_POST['do']=='delete') {
			$rid=intval($_POST['rid']);
			DB::delete('dzz_space_record',"rid='{$rid}'");
			getBuySize(intval($_POST['uid']));
			cpmsg($lang['do_success'], $refer, 'succeed');
		}
		
	}elseif($operation=='log'){	
			$delete=$_POST['delete'];
			if($delete){
				DB::delete('dzz_space_record_log',"rid IN(".dimplode($delete).")");
			}
			cpmsg($lang['do_success'], $refer, 'succeed');
	}
}
?>