<?php
define('THIS_SCRIPT', 'myfile');
require_once 'global.php';
require_once INC_PATH.'dv_code.php';
$lang = load_lang($lang, 'usermanager' );
$stats = $lang['FileHeaderTitle'];
$arrNavMenu = array(
	0=>array('linktitle'=>$lang['HeaderTitle1'],'linkurl'=>'usermanager.php')
	,1=>array('linktitle'=>$stats)
);
LoadStyleJs();
$groupsetting[48] = 1;
PageLoad();
LoadFooter();

function PageLoad(){
	global $userid,$lang,$Updepth,$star,$redcolor,$_DCACHE;
	if( $userid <= 0 ){
		showmsg($lang['MustLogin']);
		exit;
	}
	$Updepth = $lang['tpl.file.strings0'];
	if( !is_numeric($star) ){
		$star = 1;
	} else{
		$star = intval($star);
	}
	$redcolor = $_DCACHE['stylecss'][19];
	require_once printout('usermanager.navmenu');
	switch($GLOBALS['action']){
		case 'edit':
			edit();
			break;
		case 'fsave':
			filesave();
			break;
		case 'fadd':
			addnew();
			break;
		case 'fsnew':
			savenew();
			break;
		case 'fdel':
			fdel();
			break;
		case 'alldel':
			alldel();
			break;
		default:
			MainFunc();
			break;
	}
}//end function PageLoad

function MainFunc(){
	global $db,$dv,$fsetting,$groupsetting,$userid,$lang,$Updepth,$star,$redcolor,$membername,$action,$userinfo,$_DCACHE;

	$iMagicFace =  explode('|',$userinfo['userface']);
	if(!empty($iMagicFace[1])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[1])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	elseif(!empty($iMagicFace[0])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[0])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	else{
		$UserFace = "<img src=\"".$userinfo['userface']."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";			
	}
	$TableWidth =& $_DCACHE['stylecss'][1];
	$color =& $_DCACHE['stylecss'][19];
	$user_Article =& $userinfo['userpost'];
	$user_Group =& $userinfo['userclass'];
	$user_Face_left = $UserFace;
	$user_Title =& $userinfo['usertitle'];
	$user_Wealth =& $userinfo['userwealth'];
	$user_EP =& $userinfo['userep'];
	$user_CP =& $userinfo['usercp'];
	$user_Money =& $userinfo['usermoney'];
	$user_Ticket =& $userinfo['userticket'];
	$user_IsBest =& $userinfo['userisbest'];
	$user_AddDate =& $userinfo['joindate'];
	$user_Logins =& $userinfo['userlogins'];
	$username =& $userinfo['username'];
	$msg_newincept = intval($userinfo['uermsg']);

	$Mylist = explode('||',$lang['tpl.file.strings4']);
	$stype = $GLOBALS['Stype'];
	$totalrec = $TopicCount = filenum('');
	$F_typenames = explode('||',$lang['Dvbbs.lanstr5']);
	if( !is_numeric($stype) ){
		$sname = $lang['tpl.file.strings3'];
		$searchql = '';
		$TopicCount = filenum('');
	} else{
		if( $stype >= 0 && $stype <= 4 ){
			$sname = $F_typenames[intval($stype)];
			$searchsql = " f_type={$stype} and ";
		} else{
			$sname = $lang['tpl.file.strings3'];
			$searchsql = '';
		}
		$TopicCount = filenum(intval($stype));
	}
	$CurrentPage = intval($GLOBALS['page']);
	$PageNum = intval($fsetting['otherpagesize']);
	if ($PageNum < 1) {
		$PageNum = 20;
	}
	if( $CurrentPage < 1 ) $CurrentPage = 1;
	$Pcount = ( $totalrec <= 0 ? 0 : ceil( (float)$totalrec / (float)$PageNum ) );
	if( $CurrentPage > $Pcount ) $CurrentPage = $Pcount;
	$page_count = 0;
	if( $totalrec > 0 ){
		if( $query = $db->query("SELECT f_id,f_announceid,f_boardid,f_userid,f_username,f_filename,f_filetype,f_type,f_filesize,f_readme,f_downnum,f_viewnum,f_downuser,f_flag,f_addtime,f_viewname FROM {$dv}upfile WHERE {$searchsql} f_userid={$userid} ORDER BY f_id DESC",array('absolutePage'=>$CurrentPage,'pageSize'=>$PageNum)) ){
			while( $_dbResults[] = $db->fetch_array($query) ) $page_count++;
			$db->free_result($query);
			unset($_dbResults[count($_dbResults)-1]);
		}
	}
	$username = $membername;
	require printout('myfile');
}//end function MainFunc

function addnew(){
	global $lang,$action,$userinfo,$_DCACHE;

	$iMagicFace =  explode('|',$userinfo['userface']);
	if(!empty($iMagicFace[1])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[1])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	elseif(!empty($iMagicFace[0])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[0])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	else{
		$UserFace = "<img src=\"".$userinfo['userface']."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";			
	}
	$TableWidth =& $_DCACHE['stylecss'][1];
	$color =& $_DCACHE['stylecss'][19];
	$user_Article =& $userinfo['userpost'];
	$user_Group =& $userinfo['userclass'];
	$user_Face_left = $UserFace;
	$user_Title =& $userinfo['usertitle'];
	$user_Wealth =& $userinfo['userwealth'];
	$user_EP =& $userinfo['userep'];
	$user_CP =& $userinfo['usercp'];
	$user_Money =& $userinfo['usermoney'];
	$user_Ticket =& $userinfo['userticket'];
	$user_IsBest =& $userinfo['userisbest'];
	$user_AddDate =& $userinfo['joindate'];
	$user_Logins =& $userinfo['userlogins'];
	$username =& $userinfo['username'];
	$msg_newincept = intval($userinfo['uermsg']);

	$iupload = explode('||',$lang['Dvbbs.lanstr5']);
	require printout('myfile');
}//end function addnew

function savenew(){
	global $_SERVER,$_POST,$lang,$dv,$db,$membername,$userid,$fsetting,$groupsetting;
	if( intval($groupsetting[48]) == 0 ){
		showmsg($lang['error.file.strings28']);
		exit;
	}
	$F_Readme = cross_site_html_proc(trim($_POST['F_Readme']));
	$fileurl = str_filter_xss(trim($_POST['fileurl']));
	$filetype = trim($_POST['filetype']);
	$F_Flag = trim($_POST['Fflag']);
	if( $fileurl == '' || is_null($fileurl) ){
		showmsg($lang['tpl.file.strings15']);
		exit;
	} else{
		if( Dv_StrLength($fileurl) > 250 ){
			showmsg($lang['tpl.file.strings16']);
			exit;
		}
	}
	if( $F_Readme == '' || is_null($F_Readme) ){
		showmsg($lang['error.file.strings83']);
		exit;
	}
	if( Dv_StrLength($F_Readme) > 250 ){
		showmsg($lang['tpl.file.strings12']);
		exit;
	}
	if( !is_numeric($F_Flag) ){
		showmsg($lang['error.file.strings34']);
		exit;
	}
	if( strpos($fileurl,'/') === false || strpos($fileurl,'://') === false || strpos($fileurl,'.') === false ){
		showmsg($lang['tpl.file.strings17']);
		exit;
	} else{
		$filename = explode('/',$fileurl);
		$fileExt = strtolower($filename[count($filename)-1]);
	}
	$fileExt_a = explode('.',$fileExt);
	$fileExt = strtolower($fileExt_a[count($fileExt_a)-1]);
	if( $fileExt == 'php' || $fileExt == 'phtml' || $fileExt == 'php4' || $fileExt == 'php3' ){
		showmsg($lang['tpl.file.strings18']);
		exit;
	}
	if( preg_match('/(gif)|(jpg)|(jpeg)|(bmp)|(png)/i',$fileExt) ){
		$F_Type = 1;
	} elseif( preg_match('/(swf)|(swi)/i',$fileExt) ){
		$F_Type = 2;
	} elseif( preg_match('/(mid)|(wav)|(mp3)|(rmi)|(cda)/i',$fileExt) ){
		$F_Type = 3;
	} elseif( preg_match('/(avi)|(wov)|(asf)|(mpg)|(mpeg)|(ra)|(ram)/i',$fileExt) ){
		$F_Type = 4;
	} else{
		$F_Type = 0;
	}
	$boardid = 0;
	$db->query("INSERT INTO {$dv}upfile(f_boardid,f_userid,f_username,f_filename,f_filetype,f_type,f_readme,f_flag,f_addtime) VALUES( {$boardid} ,{$userid} ,'{$membername}' ,'".str_replace('|','',htmlspecialchars($fileurl))."' ,'".str_replace('.','',htmlspecialchars($fileExt))."' ,'".htmlspecialchars($F_Type)."' ,'".htmlspecialchars($F_Readme)."' ,'".htmlspecialchars($F_Flag)."' ,'".TIME_NOW."' )");
	include_once INC_PATH.'BoardCache.class.php';
	Create_forum_show();//EDIT 2007-04-20
	showmsg($lang['tpl.file.strings19'],1,$_SERVER['HTTP_REFERER']);
}//end function savenew

function edit(){
	global $db,$dv,$fsetting,$groupsetting,$userid,$lang,$Updepth,$star,$redcolor,$action,$userinfo,$_DCACHE;
	global $boardmaster,$superboardmaster,$master,$vipuser,$cls_issearch;

	$iMagicFace =  explode('|',$userinfo['userface']);
	if(!empty($iMagicFace[1])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[1])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	elseif(!empty($iMagicFace[0])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[0])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	else{
		$UserFace = "<img src=\"".$userinfo['userface']."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";			
	}
	$TableWidth =& $_DCACHE['stylecss'][1];
	$color =& $_DCACHE['stylecss'][19];
	$user_Article =& $userinfo['userpost'];
	$user_Group =& $userinfo['userclass'];
	$user_Face_left = $UserFace;
	$user_Title =& $userinfo['usertitle'];
	$user_Wealth =& $userinfo['userwealth'];
	$user_EP =& $userinfo['userep'];
	$user_CP =& $userinfo['usercp'];
	$user_Money =& $userinfo['usermoney'];
	$user_Ticket =& $userinfo['userticket'];
	$user_IsBest =& $userinfo['userisbest'];
	$user_AddDate =& $userinfo['joindate'];
	$user_Logins =& $userinfo['userlogins'];
	$username =& $userinfo['username'];
	$msg_newincept = intval($userinfo['uermsg']);

	$myurl = false;
	$editid = trim($GLOBALS['editid']);
	if( !is_numeric($editid) || is_null($editid) ){
		showmsg($lang['error.file.strings34']);
		exit;
	}
	if( $query = $db->query("SELECT f_id,f_announceid,f_boardid,f_userid,f_username,f_filename,f_filetype,f_type,f_filesize,f_readme,f_downnum,f_viewnum,f_downuser,f_flag,f_addtime,f_viewname FROM {$dv}upfile WHERE f_id={$editid}",array('pageSize'=>1)) ){
		if( $result = $db->fetch_array($query) ){
			$db->free_result($query);
		}
	}
	if( !empty($result) && is_array($result) ){
		require printout('myfile');
	} else{
		showmsg($lang['error.file.strings34']);
		exit;
	}
}//end function edit

function filesave(){
	global $_SERVER,$lang,$_POST,$dv,$db,$userid,$membername,$groupsetting;
	if( $groupsetting[48] == 0 ){
		showmsg($lang['error.file.strings28']);
		exit;
	}
	$F_BoardID = trim($_POST['F_BoardID']);
	$F_AnnounceID = trim($_POST['F_AnnounceID']);
	$F_Readme = cross_site_html_proc(trim($_POST['F_Readme']));
	$saveid = trim($_POST['saveid']);
	$Fflag = trim($_POST['Fflag']);
	if( !is_numeric($Fflag) || !is_numeric($F_BoardID) ){
		showmsg($lang['error.file.strings32']);
		exit;
	}
	if( !is_numeric($saveid) ){
		showmsg($lang['error.file.strings34']);
		exit;
	}
	if( $F_Readme == '' || is_null($F_Readme) ){
		showmsg($lang['error.file.strings83']);
		exit;
	}
	if( Dv_StrLength($F_Readme) > 250 ){
		showmsg($lang['tpl.file.strings0']);
		exit;
	}
	$fileurl = str_filter_xss(trim($_POST['fileurl']));
	if( $fileurl != '' ){
		if( strpos($fileurl,'/') === false || strpos($fileurl,'://') === false || strpos($fileurl,'.') === false ){
			showmsg($lang['tpl.file.strings13']);
			exit;
		}
		$db->query("UPDATE {$dv}upfile SET f_filename='{$fileurl}' ,f_readme='{$F_Readme}' ,f_flag={$Fflag} WHERE f_id={$saveid}");
	} else{
		$db->query("UPDATE {$dv}upfile SET f_boardid={$F_BoardID} ,f_announceid='{$F_AnnounceID}' ,f_readme='{$F_Readme}' ,f_flag={$Fflag} WHERE f_id={$saveid}");
	}
	showmsg($lang['tpl.file.strings14'],1,$_SERVER['HTTP_REFERER']);
}//end function filesave

function fdel(){
	global $_SERVER,$lang,$_POST,$dv,$db,$userid,$membername,$groupsetting;
	global $master,$superboardmaster,$boardmaster;
	if( $groupsetting[48] == 0 ){
		showmsg($lang['error.file.strings28']);
		exit;
	}
	if(is_numeric($_GET['delid'])){
		$_POST['delid'][]=$_GET['delid'];
	}
	elseif(is_array($_GET['delid'])){
		if(!is_array($_POST['delid'])){
			$_POST['delid']=&$_GET['delid'];
		}
		else{
			$_POST['delid']=array_merge($_POST['delid'],$_GET['delid']);
		}
	}
	if( !is_array($_POST['delid']) ){
		showmsg($lang['error.file.strings35_42']);
		exit;
	}
	$delid= intval($_POST['delid']);
	$delIDList = implode(',',$delid);
	if( !checkIDList($delIDList) ){
		showmsg($lang['error.file.strings35_42']);
		exit;
	}
	if( $master || $superboardmaster || $boardmaster ){
		$db->query("DELETE FROM {$dv}upfile WHERE f_id IN({$delIDList})");
	} else{
		$db->query("DELETE FROM {$dv}upfile WHERE f_flag<>2 AND f_userid={$userid} AND f_id IN({$delIDList})");
	}
	showmsg($lang['tpl.file.strings20'],1,$_SERVER['HTTP_REFERER']);
}//end function fdel

function alldel(){
	global $_SERVER,$lang,$_POST,$dv,$db,$userid,$membername,$groupsetting;
	if( $groupsetting[48] == 0 ){
		showmsg($lang['error.file.strings28']);
		exit;
	}
	$db->query("DELETE FROM {$dv}upfile WHERE f_flag=1 AND f_userid={$userid}");
	showmsg($lang['tpl.file.strings21'],1,$_SERVER['HTTP_REFERER']);
}//end function alldel

function filenum($types){
	global $dv,$db,$userid;
	if( is_numeric($types) ){
		if( $types >= 0 && $types <= 4 ){
			$stype = ' AND f_type='.intval($types).' ';
		} else{
			$stype = '';
		}
	} else{
		$stype = '';
	}
	return intval($db->scalar("SELECT COUNT(f_id) FROM {$dv}upfile WHERE f_userid={$userid} {$stype}"));
}//end function filenum
?>