#[BOFN]###############################################################################
#
#
#Pagenews - a free script to publish news on websites
#Copyright (C) 2004,2005,2006,2007,2008 Philipp Kindt
#
#This file is part of Pagenews.
#
# 	 This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#[EOFN]###############################################################################


#this package cares about uploading media and showing/deleting uploaded media...  
package dnmedia_upload;
use POSIX;

$image_dir = "images";
$others_dir = 	"other";

#########################################################################################################
#receives uploaded files and saves them either to the "images" or the "others" - location, depending on their file ending
#filename in $_[0], content in $_[1]
#returns file destination
#it needs a ref on an array with destinations of files that were allredy written in $_[2]
sub write_uploaded_file{
	my $filename = $_[0];
	#Internet Explorer Bugfix:
	#IE shows strange behaviour when sending filenames:
	#it deliveres the whole local path on the client's machine.
	#we just cut off the path	
	if($filename =~ m/\\/){
		my @parts = split(/\\/,$filename);
		$filename = $parts[$#parts];
	}
	
	(my $waste, my $extension) = split(/\./,$filename);	
	donstdlib::check_filename($filename,"subdir");
	my @allowed_extensions = split(/\ /,$dnmain::settings->{'allowed_upload_extensions'});
	my $allowed = 0;	
	foreach my $allowed_extension (@allowed_extensions){
		if($extension =~ m/^$allowed_extension$/i){
			$allowed = 1;
		} 
	}
	if($dnmain::settings->{'allowed_upload_extensions'} eq ""){
		$allowed = 1;
	}
	if($allowed == 0){
		donstdlib::error("dnmedia_upload_not_allowed",$extension,donstdlib::array_to_string($_[2],", "));
	}
	my $file_dest = $dnmain::settings->{'media_path'}."/".$dnmain::in{'username'}."/".get_subdir($extension)."/".$filename;
	if(donstdlib::make_sure_that_exists($file_dest,"check") == 1){
		donstdlib::error("dnmedia_upload_file_exists",$file_dest,donstdlib::array_to_string($_[2],", "));
	}
	if(length($_[1])/1024 > $dnmain::settings->{'max_upload_size'}){
		if($dnmain::settings->{'max_upload_size'} ne ""){
		donstdlib::error("dnmedia_upload_to_big",$file_dest, length($_[1])/1024,$dnmain::settings->{'max_upload_size'},donstdlib::array_to_string($_[2],", "));					
		}
	}
	
	donstdlib::write_file($file_dest,$_[1],"binmode");
	return $file_dest;
}
#########################################################################################################
#returns right path determined by the filename extension
#needs filename extension in $_[0]

sub get_subdir{
	my $extension = $_[0];
	my @images = split(/\ /,$dnmain::settings->{'image_extensions'});
	my $is_img = 0;	
	foreach my $image (@images){
		if($extension =~ m/^$image$/i){
			$is_img = 1;
		} 
	}
	if($is_img == 1){
		return $image_dir;
	}else{
		return $others_dir;	
	}
	
}

