<?php
#=============================================================#
#                                                             #
#           Powered by Dvbbs.PHP 2.0.0                        #
#           function  DownLoad 效益                           #
#           author    IvanSky                                 #
#           date      2007-9-3                                #
#                                                             #
#=============================================================#

define('THIS_SCRIPT', 'forum_DownLoad_benefit');
include_once 'global.php';
$lang = load_lang($lang,'forum_DownLoad_benefit');
checklogin();
header("content-Type: text/html; charset=".$charset);
head();

if ( strpos($sysinfo['flag'],'50,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}

PageLoad();
footer();
function Pageload(){
	global $do_action,$dv,$db,$lang,$action,$arrDownLoad_benefit,$selected_id,$GetArrDB;
	global $setDB_Close,$setDB_id;
	$action= $do_action;
	require_once INC_PATH.'BoardCache.class.php';
	$currcachefile =CACHE_PATH.'Forum_DownLoad_benefit_File.php';

	if (file_exists($currcachefile))
	{
		require_once $currcachefile;
		$arrDownLoad_benefit = $Forum_DownLoad_benefit['DownLoad_benefit'];
	}else{
		@chmod($currcachefile,0777);
		putcachefile($contents, $currcachefile);
	}
	readme($lang['forum.info1'],$lang['str.info9'],$lang['nav']);
	switch ($do_action) {
		case "save":
			ad_save();
			break;
		case "edit":
			ad_edit();
			break;
		case "del":
			ad_del();
			break;
		case "startclose":
			ad_startclose();
			break;
	}

include printout('forum_DownLoad_benefit');
}
//开启关闭
function ad_startclose(){
	global $lang,$setDB_Close,$setDB_id,$arrDownLoad_benefit;
	if ( is_numeric($setDB_Close) && is_numeric($setDB_id) )
	{
		StartClose_Forum_DownLoad_benefit();
		show_msg($lang['forum.info1'],$lang['succeed.info1'],1,'','',$lang['url'],$lang['comback']);
		exit;
	}
}

function ad_save(){
	
	global $dv,$db,$lang,$do_action,$arrDownLoad_benefit,$selected_id,$strDB_File,$strDB_File_type,$DB_groupid;
	

	ad_check ();
	$file=$_FILES['DB_File'];
	$size=$_POST['MAX_FILE_SIZE'];
	if ( $file[error] == 0 and !empty($file[tmp_name]))
	{
		$strDB_File=uploadrule($file,$size);
		$strDB_File_type=strtolower(substr(strrchr($strDB_File,'.'),1));
	}

	Create_Forum_DownLoad_benefit();
	show_msg($lang['forum.info1'],$lang['succeed.info2'],1,'','',$lang['url'],$lang['comback']);
	exit;

}
function ad_edit(){
	global $dv,$lang,$do_action,$arrDownLoad_benefit,$selected_id,$GetArrDB,$strDB_File,$strDB_File_type,$DB_groupid;
	
	$DB_id= !empty($_POST['selected_id']) ? $_POST['selected_id'] : $_GET['selected_id'] ;
	$selected_id = $DB_id[0];

	if ( $_POST[self_action] != 'saveedit' )
	{
		eval('$DBurl = "'.$lang['DBurl'].'";');
		if (empty($DB_id[0])){
			show_msg($lang['forum.info1'],$lang['err.info2'],0,'','',$DBurl,$lang['comback']);
			exit;
		}
		$GetArrDB = $arrDownLoad_benefit[$DB_id[0]];
	}
	else
	{
		ad_check ();
		eval('$DBurl = "'.$lang['DBurl'].'";');
		$file=$_FILES['DB_File'];
		$size=$_POST['MAX_FILE_SIZE'];

		if ( $file[error] == 0 and !empty($file[tmp_name]))
		{
			$strDB_File=uploadrule($file,$size);
			$strDB_File_type=strtolower(substr(strrchr($strDB_File,'.'),1));
		}else{
			$strDB_File=$_POST['oldDB_File'];
			$strDB_File_type=strtolower(substr(strrchr($_POST['oldDB_File'],'.'),1));

		}		
		Create_Forum_DownLoad_benefit();
		show_msg($lang['forum.info1'],$lang['succeed.info3'],1,'','',$DBurl,$lang['comback']);
		exit;
	
	}
}

function ad_del(){
	global $dv,$lang,$do_action,$arrDownLoad_benefit,$selected_id,$GetArrDB,$strDB_File,$strDB_File_type,$strDB_id,$arrDB_del_id;
	
	$arrDB_del_id= !empty($_POST['selected_id']) ? $_POST['selected_id'] : $_GET['selected_id'] ;
	
	if ( count($arrDB_del_id) > 0 )
	{
		Create_Forum_DownLoad_benefit();
		show_msg($lang['forum.info1'],$lang['succeed.info4'],1,'','',$lang['url'],$lang['comback']);
		exit;
	}
	else
	{
		show_msg($lang['forum.info1'],$lang['err.info3'],0,'','',$lang['url'],$lang['comback']);
		exit;
	}

}
function str_r($s){
	$s = str_replace('&amp;','&',$s);
	$s = str_replace('&nbsp;',' ',$s);
	$s = str_replace('"','&quot;',$s);
	$s = str_replace("'",'&#39;',$s);
	$s = str_replace("<","&lt;",$s);
	$s = str_replace(">","&gt;",$s);
	$s = str_replace("\t","&nbsp; &nbsp; ",$s);
	$s = str_replace("\r","",$s);
	$s = str_replace("  ","&nbsp; ",$s);
	return $s;
}

function ad_check ()
{
	global $dv,$lang, $DB_groupid, $DB_boardid, $DB_type, $DB_File, $DB_width, $DB_height
		, $DB_second, $DB_starttime, $DB_endtime, $DB_close, $DB_name, $DB_linkurl, $DB_HTML;
	$DB_boardid=  intval(trim($_POST['DB_boardid']));
	$DB_type=  intval(trim($_POST['DB_type']));
	$DB_File=     trim($DB_File);
	$DB_width=    intval(trim($_POST['DB_width']));
	$DB_height=   intval(trim($_POST['DB_height']));
	$DB_second=   intval(trim($_POST['DB_second']));
	$DB_starttime=trim($_POST['DB_starttime']);
	$DB_endtime=  trim($_POST['DB_endtime']);
	$DB_close=    intval(trim($_POST['DB_close']));
	$DB_name=    trim($_POST['DB_name']);
	$DB_linkurl=    trim($_POST['DB_linkurl']);
	$DB_groupid=    trim($_POST['groupid']);
	$DB_HTML=    $_POST['DB_HTML'];


	if ($DB_boardid == 0){
		show_msg($lang['forum.info1'],$lang['err.info4'],0,'','',$lang['url'],$lang['comback']);
		exit;
	}
	if (empty($DB_width)){
		show_msg($lang['forum.info1'],$lang['err.info5'],0,'','',$lang['url'],$lang['comback']);
		exit;
	}
	if (empty($DB_height)){
		show_msg($lang['forum.info1'],$lang['err.info6'],0,'','',$lang['url'],$lang['comback']);
		exit;
	}
	if (empty($DB_second)){
		show_msg($lang['forum.info1'],$lang['err.info7'],0,'','',$lang['url'],$lang['comback']);
		exit;
	}
	if (isDate($DB_starttime) === FALSE){
		show_msg($lang['forum.info1'],$lang['err.info1'],0,'','',$lang['url'],$lang['comback']);
		exit;
	}

	if (isDate($DB_endtime) === FALSE){
		show_msg($lang['forum.info1'],$lang['err.info1'],0,'','',$lang['url'],$lang['comback']);
		exit;
	}

	if ($DB_starttime > $DB_endtime){
		show_msg($lang['forum.info1'],$lang['err.info8'],0,'','',$lang['url'],$lang['comback']);
		exit;
	}

}

function uploadrule($file,$size) {//导入规则文件;
	global $lang,$file_list,$forum_setting,$oldDB_File,$fsetting;

	$uploaddir=$forum_setting[76];
	$upDownLoadbenefit = 'DownLoad_benefit/';
	$inportrule=array();

	require_once INC_PATH.'upload.functions.php';
	$fsetting['forumuploadname']="../".$uploaddir;

	$updirsettings = initUploadDirectories(array('init_space_updestdir'=>false));
	
	if (false === (realpath("../".$uploaddir.$upDownLoadbenefit))) {
		$GetErrurl=str_replace('{$uploaddir}{$upDownLoadbenefit}', $uploaddir.$upDownLoadbenefit,$lang['str.add.info176']);
		show_msg($lang['forum.info1'],$GetErrurl,0,'','',$lang['url'],$lang['cmdback']);
		exit;			
	}
	$inportrule=$file;
	$uploadruledir=$updirsettings[uptopdir].'/'.$upDownLoadbenefit;

	if(empty($size)){
		show_msg($lang['forum.info1'],$lang['str.add.info172'],0,'','',$lang['url'],$lang['cmdback']);
		exit;		
	}
	else{

		$name_str=date("YmdHis");
		$filebase=strtolower(strrchr($inportrule['name'],'.'));

		if( $filebase <> '.gif' &&  $filebase <> '.jpg' &&  $filebase <> '.swf'){
			show_msg($lang['forum.info1'],$lang['str.add.info172'],0,'','',$lang['url'],$lang['cmdback']);
			exit;			
		}
		else{
			
			$attachstr=$uploadruledir.$name_str.$filebase;
			@chmod($uploadruledir,0777);
			$inportrule['tmp_name']=str_replace('\\\\', '\\', $inportrule['tmp_name']);
			if (@move_uploaded_file($inportrule['tmp_name'],$attachstr)){
				@chmod($attachstr,0777);				
				if (!empty($oldDB_File))
					@unlink('..'.$oldDB_File);
				return $uploaddir.$upDownLoadbenefit.$name_str.$filebase;
			}
			else{
				show_msg($lang['forum.info1'],$lang['str.add.info171'],0,'','',$lang['url'],$lang['cmdback']);
				exit;
			}
			@unlink($inportrule);
		}
	}
	
}
?>