<?php

/**
 *      
 */


if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
$do=trim($_GET['do']);
$refer=dreferer();
if($do=='buy'){
	if(!$sysconfig['spacebuy']) dzzshowmessage(dzzlang('space/template','space_buy_close'));
	if(submitcheck('buysubmit')){
		
		$pid=intval($_POST['pid']);
		$time=intval($_POST['time']);
		$product=dstripslashes(DB::fetch_first("select * from ".DB::table('dzz_space_product')."  where pid='{$pid}'"));
			
			if($time<=0){
				dzzshowmessage(dzzlang('space/template','buy_num_not_empty'),$refer);
			}
			if($product['price']>0){
				//判断用户密码；
				loaducenter();
				$ucresult = uc_user_login($_POST['username'], $_POST['password']);
				list($tmp['uid']) = daddslashes($ucresult);
				if($tmp['uid'] <= 0) {
					dzzshowmessage(dzzlang('space/template','password_error'),$refer);
				}
				//判断用户的金币是否能够购买此服务
				$money=$product['price']*$time;
				$dzzmoney=getusermoney($tmp['uid']);
				if($dzzmoney-$money<0){
					$msg=dzzlang('space/template','no_money',array('credits'=>$_G['setting']['extcredits'][$sysconfig['buycredits']]['title'],'link'=>$_G['setting']['creditstrans']==$sysconfig['buycredits']?'home.php?mod=spacecp&ac=credit&op=buy':'home.php?mod=spacecp&ac=credit&op=rule','title'=>$_G['setting']['creditstrans']==$sysconfig['buycredits']?dzzlang('space/template','add_credits'):dzzlang('space/template','credits_rule')));
					
					dzzshowmessage($msg,$refer);
				}
			}
			$setarr=array(
							'pname'=>$product['pname'],
							'uid'=>$_G['uid'],
							'danwei'=>$product['danwei'],
							'price'=>$product['price'],
							'spacesize'=>$product['spacesize'],
							'ip'=>$_G['clientip'],
							'username'=>$_G['username'],
							'dateline'=>$_G['timestamp'],
							'endtime'=>strtotime(" + $time $product[danwei]"),
							'num'=>$time,
						);
			if($rid=DB::insert('dzz_space_record',daddslashes($setarr))){
				
				if($field=DB::fetch_first("select buysize from ".DB::table('dzz_userconfig_field')." where uid='{$_G[uid]}'")){
					if(DB::update('dzz_userconfig_field',array('buysize'=>$field['buysize']+$product['spacesize']),"uid='{$uid}'")){
						updatemembercount($_G['uid'], array($sysconfig['buycredits'] => -$money), 1, 'BDC',$_G['uid']);
						dzzshowmessage('buy_success',DZZSCRIPT.'?mod=space');
						/*include template('common/header_ajax');
						echo "<script type=\"text/javascript\">";
						echo "showDialog('".dzzlang('space/template','buy_success')."','notice','',function(){location.href='".DZZSCRIPT."?mod=space';});";
						echo "</script>";
						include template('common/footer_ajax');
						exit();*/
					}else{
						DB::delete('dzz_space_record',"rid='{$rid}'");
						dzzshowmessage(dzzlang('space/template','buy_failure'),$refer);
					}	
				}else{
					if(DB::insert('dzz_userconfig_field',array('uid'=>$_G['uid'],'buysize'=>$product['spacesize']),1,1)){
						updatemembercount($_G['uid'], array($sysconfig['buycredits'] => -$money), 1, 'BDC',$_G['uid']);
						dzzshowmessage('buy_success',DZZSCRIPT.'?mod=space');
						/*include template('common/header_ajax');
						echo "<script type=\"text/javascript\">";
						echo "showDialog('".dzzlang('space/template','buy_success')."','notice','',function(){location.href='".DZZSCRIPT."?mod=space';});";
						echo "</script>";
						include template('common/footer_ajax');
						exit();	*/
					}else{
						DB::delete('dzz_space_record',"rid='{$rid}'");
						dzzshowmessage(dzzlang('space/template','buy_failure'),$refer);
					}	
				}
			}
			dzzshowmessage(dzzlang('space/template','buy_failure'),$refer);
	}else{
		//获取用户选择的主题信息
		$pid=intval($_GET['pid']);
		$product=dstripslashes(DB::fetch_first("SELECT * FROM ".DB::table('dzz_space_product')."  where pid='{$pid}'"));
		$product['spacesize']=formatsize($product['spacesize']);
		$product['description']=htmlspecialchars_decode(getstr($product['description'],0,1,0,1,1));
		$product['pname']=htmlspecialchars_decode($product['pname']);
		//获取用户的积分
		$credits=getusermoney($_G['uid']);
	}
}elseif($do=='usergroup'){
	$group_member=$group_system=$group_special=array();
	$dzzgroup=array();
	$mygroup=array();
	
	$query=DB::query("select * from ".DB::table('dzz_usergroup')." where 1");
	while($value=dstripslashes(DB::fetch($query))){
		$dzzgroup[$value['groupid']]=$value;
	}
	$query = DB::query("SELECT g.groupid, g.radminid, g.type, g.grouptitle,f.maxattachsize,f.attachextensions,f.maxspacesize FROM ".DB::table('common_usergroup')." g
					LEFT JOIN ".DB::table('common_usergroup_field')." f ON g.groupid=f.groupid ORDER BY g.creditshigher");
		while($group = dstripslashes(DB::fetch($query))) {
			
			$group['maxattachsize']=floor($group['maxattachsize']);
			if($dzzgroup[$group['groupid']]){
				$group['maxattachsize']=floor($dzzgroup[$group['groupid']]['maxattachsize']);
				$group['attachextensions']=$dzzgroup[$group['groupid']]['attachextensions'];
				$group['maxspacesize']=$dzzgroup[$group['groupid']]['maxspacesize']*1024*1024;
				$group['allowupload']=$dzzgroup[$group['groupid']]['allowupload'];
				$group['allownewfolder']=$dzzgroup[$group['groupid']]['allownewfolder'];
				$group['allownewlink']=$dzzgroup[$group['groupid']]['allownewlink'];
			}else{
				$group['allowupload']=1;
				$group['allownewfolder']=1;
				$group['allownewlink']=1;
			}
			if($group['attachextensions']=='') $group['attachextensions']=dzzlang('space/template','no_limit');
			if($group['maxattachsize']==0) $group['maxattachsize']=dzzlang('space/template','no_limit');
			else $group['maxattachsize']=formatsize($group['maxattachsize']);
			if($group['maxspacesize']==0) $group['maxspacesize']=dzzlang('space/template','no_limit');
			else $group['maxspacesize']=formatsize($group['maxspacesize']);
			
			if($group['groupid']==$space['groupid']){
				$mygroup['grouptitle']=$group['grouptitle'];
				$mygroup['maxspacesize']=$group['maxspacesize']*1024*1024;
				$mygroup['maxattachsize']=$space['maxattachsize'];
				$mygroup['attachextensions']=$space['attachextensions'];
				$mygroup['allowupload']=$space['allowupload'];
				$mygroup['allownewfolder']=$space['allownewfolder'];
				$mygroup['allownewlink']=$space['allownewlink'];
				if($mygroup['attachextensions']=='') $mygroup['attachextensions']=dzzlang('space/template','no_limit');
				if($mygroup['maxattachsize']==0) $mygroup['maxattachsize']=dzzlang('space/template','no_limit');
				else $mygroup['maxattachsize']=formatsize($mygroup['maxattachsize']);
				if($mygroup['maxspacesize']==0) $mygroup['maxspacesize']=dzzlang('space/template','no_limit');
				else $mygroup['maxspacesize']=formatsize($mygroup['maxspacesize']);
			}
			if($group['type'] == 'member') {
				$group_member[]=$group;
			}elseif($group['type'] == 'system') {
				$group_system[]=$group;
			}elseif($group['type'] == 'special' && $group['radminid'] == '0') {
				$group_special[]=$group;
			}
		}
}elseif($do=='myspace'){
	if(!$_G['uid']) showmessage('to_login', '', array(), array('showmsg' => true, 'login' => 1));
	$usesize=DB::result_first("select usesize from ".DB::table('dzz_userconfig')." where uid='{$_G[uid]}'");
	$fusesize=formatsize($usesize);
	$addsize=DB::result_first("select addsize from ".DB::table('dzz_userconfig_field')." where uid='{$_G[uid]}'");
	$groupsize=$space['groupsize'];
	if($groupsize==0){
		$fgroupsize=dzzlang('space/template','no_limit');
		$buysize=0;
	}else{
		$buysize=getBuySize($_G['uid']);
		$faddsize=formatsize($addsize);
		$fgroupsize=formatsize($groupsize);
		$fbuysize=formatsize($buysize);
	}
	$totalsize=$addsize+$buysize+$groupsize;
	if($totalsize==0 || $groupsize==0){
		$ftotalsize=dzzlang('space/template','no_limit');
	}else{
		$ftotalsize=formatsize($totalsize);
	}
	$per=($usesize/($addsize+$buysize+$groupsize)*100>98)?98:($usesize/($addsize+$buysize+$groupsize)*100);
	$mygroup=array();
	$mygroup['maxattachsize']=floor($space['maxattachsize']);
	$mygroup['attachextensions']=$space['attachextensions'];
	
	$mygroup['allowupload']=$space['allowupload'];
	$mygroup['allownewfolder']=$space['allownewfolder'];
	$mygroup['allownewlink']=$space['allownewlink'];
	if($mygroup['attachextensions']=='') $mygroup['attachextensions']=dzzlang('space/template','no_limit');
	if($mygroup['maxattachsize']==0) $mygroup['maxattachsize']=dzzlang('space/template','no_limit');
	else $mygroup['maxattachsize']=formatsize($mygroup['maxattachsize']);
	if($mygroup['maxspacesize']==0) $mygroup['maxspacesize']=dzzlang('space/template','no_limit');
	else $mygroup['maxspacesize']=formatsize($mygroup['maxspacesize']);
	
	$record=array();
		if($count=DB::result_first("select COUNT(*) from ".DB::table('dzz_space_record')." where uid='{$_G[uid]}'")){
			$query=DB::query("select * from ".DB::table('dzz_space_record')." where uid='{$_G[uid]}' order by dateline DESC");
			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']);
				$record[]=$value;
			}
		}
		$record1=array();
		if($count=DB::result_first("select COUNT(*) from ".DB::table('dzz_space_record_log')." where uid='{$_G[uid]}'")){
			$query=DB::query("select * from ".DB::table('dzz_space_record_log')." where uid='{$_G[uid]}' order by dateline DESC");
			while($value=dstripslashes(DB::fetch($query))){
				if($value['dateline']) $value['dateline']=dgmdate($value['dateline'], 'Y-n-j');
				if($value['endtime']) $value['endtime']=dgmdate($value['endtime'], 'Y-n-j');
				$value['spacesize']=formatsize($value['spacesize']);
				$value['pname']=htmlspecialchars_decode($value['pname']);
				$record1[]=$value;
			}
		}
	
}else{
	if(!$sysconfig['spacebuy']) dzzshowmessage(dzzlang('space/template','space_buy_close'));
	$list=array();
	//获取空间产品
	$query=DB::query("SELECT * FROM ".DB::table('dzz_space_product')."  where `available`>0 and (endtime='0' or (endtime>0 and endtime>$_G[timestamp] and starttime<$_G[timestamp]) or starttime='0' )  ORDER BY disp");
	while($value=dstripslashes(DB::fetch($query))){
		$value['spacesize']=formatsize($value['spacesize']);
		$value['pname']=htmlspecialchars_decode($value['pname']);
		$list[]=$value;
	}
}
function getusermoney($uid){
	global $_G,$sysconfig;
	
	$credit='extcredits'.($sysconfig['buycredits']?$sysconfig['buycredits']:2);
	$dzzmoney = DB::result_first("SELECT $credit FROM ".DB::table('common_member_count')." WHERE uid='{$uid}'");
	return $dzzmoney?$dzzmoney:0;
}
include dzztemplate('space/space_index');

?>