35801
Goto Top

Variable aus URL-String übernehmen und in Zielpfad einbauen...

Hallo Leute!

Ich weiß es ist nicht gerde die feine Art andere meine Arbeit machen lassen aber ich verzweifle bald...

Ich hab untenstehende Script in PERL und hab NULL Ahnung was und wo und wie ich eine Variable welche per URL übergeben wird zum UPLOAD-DIR hinzufügen soll...

Problem:
Ich hab ein fertiges Perl-Uploadscript, dass super funktioniert!

Per URL übergebe ich die Variable "pd" mit ...&pd=projektname (zB: 122587_TestProjekt)

Nun, so weit so schlecht...

Das Script schmeißt mir alle Daten in den Ordner "uploads" der bei bedarf auch erstellt wird...

Ich möchte nur dass das script für jede Projekt einen Unterordner anlegt....
(zB: uploads/122587_TestProjekt)

Kann mir jemand sagen wo ich die Variable einbaue bzw. mir schnell die 1-2 Zeilen ändern??


GROSSES DANKE!!!


SCRIPT UPLOAD.PL
#!/usr/bin/perl -w


#**********************************************************************************************************************************
my $THIS_VERSION = "6.7";                                                    # Version of this driver  
my $start_upload = time();                                                   # Timestamp start of upload
my $end_upload = 0;                                                          # Timestamp end of upload

# Makes %ENV safer
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';  
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};  

use strict;
use lib qw(.);                              # Add current directory to list of valid paths
# use CGI::Carp 'fatalsToBrowser';          # Dump fatal errors to screen 
use CGI qw(:cgi);                           # Load the CGI.pm module
use File::Copy;                             # Module for moving uploaded files
use uu_lib;                                 # Load the uu_lib.pm module

###############################################################
# The following possible query string formats are assumed
#
# 1. ?temp_sid=some_sid_number&config_file=some_config_file_name
# 2  ?temp_sid=some_sid_number
# 3. ?cmd=about
# 4. ?cmd=debug&config_file=some_config_file_name
# 5. ?cmd=debug
###############################################################
my %query_string = parse_query_string($ENV{'QUERY_STRING'});  

# Check for tainted sid
if(exists($query_string{'temp_sid'})){  
	if($query_string{'temp_sid'} !~ m/(^[a-zA-Z0-9]{32}$)/){ &kak("<font color='red'>ERROR<\/font>: Invalid session-id<br>\n", 1, __LINE__); }  
	else{ $query_string{'temp_sid'} = $1; }  
}

# Check for tainted config file name
if(exists($query_string{'config_file'})){  
	if($query_string{'config_file'} !~ m/(^\w{5,32}$)/){ &kak("<font color='red'>ERROR<\/font>: Invalid config file name<br>\n", 1, __LINE__); }  
	else{ $query_string{'config_file'} = $1; }  
}

# Check for tainted command
if(exists($query_string{'cmd'})){  
	if($query_string{'cmd'} ne 'about' && $query_string{'cmd'} ne 'debug'){ &kak("<font color='red'>ERROR<\/font>: Invalid command<br>\n", 1, __LINE__); }  
}

# Make sure cmd or temp_sid was passed but not both
if(!exists($query_string{'cmd'}) && !exists($query_string{'temp_sid'})){ &kak("<font color='red'>ERROR<\/font>: Invalid parameters<br>\n", 1, __LINE__); }  
if(exists($query_string{'cmd'}) && exists($query_string{'temp_sid'})){ &kak("<font color='red'>ERROR<\/font>: Conflicting parameters<br>\n", 1, __LINE__); }  

#######################################################################################################
# Attempt to load the config file that was passed to the script if multi configs is enabled. If no
# config file name was passed to the script, load the default config file 'uu_default_config.pm'  
#######################################################################################################
my $config_file = 'uu_default_config';  

if(exists($query_string{'config_file'}) && $MULTI_CONFIGS_ENABLED){  
	$config_file = $query_string{'config_file'};  

	unless(eval "use $config_file"){  
		if($@){ &kak("<font color='red'>ERROR<\/font>: Failed to load config file $config_file.pm<br>\n", 1, __LINE__); }  
	}
}
elsif(exists($query_string{'config_file'}) && !$MULTI_CONFIGS_ENABLED){  
	 &kak("<font color='red'>ERROR<\/font>: Multi Config files disabled<br>\n", 1, __LINE__);  
}
else{
	unless(eval "use $config_file"){  
		if($@){ &kak("<font color='red'>ERROR<\/font>: Failed to load config file $config_file.pm<br>\n", 1, __LINE__); }  
	}
}

# Process 'about' or 'debug' command  
if(exists($query_string{'cmd'}) && $query_string{'cmd'} eq 'about'){ &kak("<u><b>UBER UPLOADER VERSION</b><\/u><br> UBER UPLOADER VERSION = <b>" . $UBER_VERSION . "<\/b><br> UU_UPLOAD = <b>" . $THIS_VERSION . "<\/b><br>\n", 1, __LINE__); }  
elsif(exists($query_string{'cmd'}) && $query_string{'cmd'} eq 'debug' && !$DEBUG_ENABLED){ &kak("<u><b>UBER UPLOADER CGI SETTINGS<\/b><\/u><br> DEBUG = <b><font color='red'>disabled</font><\/b><br>\n", 1, __LINE__); }  
elsif(exists($query_string{'cmd'}) && $query_string{'cmd'} eq 'debug' && $DEBUG_ENABLED){ &debug($main::config, $UBER_VERSION, $THIS_VERSION, $MULTI_CONFIGS_ENABLED); }  

# Declare Local variables
my $temp_sid = $query_string{'temp_sid'};                                           # Assign session-id  
my $sleep_time = 1;                                                                 # Seconds to wait before upload proceeds (for small file uploads)
my %uploaded_files = ();                                                            # Hash used to store uploaded file names, sizes and types
my $temp_dir_sid = $main::config->{'temp_dir'} . $temp_sid;                           # Append Session-id to upload temp directory  
my $flength_file = $temp_dir_sid . '/flength';                                      # Flength file is used to store the size of the upload in bytes  
my $unique_dir;                                                                     # Unique upload directory name

umask(0);
$|++;                                                                               # Force auto flush of output buffer
$SIG{HUP} = 'IGNORE';                                                               # Ignore sig hup  
local $SIG{__DIE__} = \&cleanup;                                                    # User has pressed stop during upload so deal with it
$CGI::POST_MAX = $main::config->{'max_upload'};                                     # Set the max post value  

# Create temp directory if it does not exist
if(!-d $main::config->{'temp_dir'}){ mkdir($main::config->{'temp_dir'}, 0777) or &kak("<font color='red'>ERROR</font>: Failed to mkdir $main::config->{'temp_dir'}: $!", 1, __LINE__); }  

# Create a temp directory based on Session-id
if(!-d $temp_dir_sid){ mkdir($temp_dir_sid, 0777) or &kak("<font color='red'>ERROR</font>: Failed to mkdir $temp_dir_sid: $!", 1, __LINE__); }  
else{
	&deldir($temp_dir_sid);
	mkdir($temp_dir_sid, 0777) or &kak("<font color='red'>ERROR</font>: Failed to mkdir $temp_dir_sid: $!", 1, __LINE__);  
}

# Prepare the flength file for writing
open(FLENGTH, ">" , "$flength_file") or &kak("<font color='red'>ERROR</font>: Failed to open $temp_dir_sid/flength: $!", 1, __LINE__);  

if($ENV{'CONTENT_LENGTH'} > $main::config->{'max_upload'}){  
	# If file size exceeds maximum write error to flength file and exit
	my $max_size = &format_bytes($main::config->{'max_upload'}, 99);  

	print FLENGTH "ERROR: Maximum upload size of $max_size exceeded";  
	close(FLENGTH);
	chmod 0666, $flength_file;

	&kak("<font color='red'>ERROR</font>: Maximum upload size of $max_size exceeded.<br><br>Your upload has failed.<br>", 1, __LINE__);  
	close(STDIN);
}
else{
	# Write total upload size in bytes to flength file
	print FLENGTH $ENV{'CONTENT_LENGTH'};  
	close(FLENGTH);
	chmod 0666, $flength_file;
}

# Let progress bar get some info (for small file uploads)
sleep($sleep_time);

# Tell CGI.pm to use our directory based on Session-id
if($TempFile::TMPDIRECTORY){ $TempFile::TMPDIRECTORY = $temp_dir_sid; }
elsif($CGITempFile::TMPDIRECTORY){ $CGITempFile::TMPDIRECTORY = $temp_dir_sid; }
else{ &kak("<font color='red'>ERROR</font>: Failed to assign CGI temp directory", 1, __LINE__); }  

my $query = new CGI;

####################################################################################################################
# The upload is complete at this point, so you can now access any post values. eg. $query->param("some_post_value");  
####################################################################################################################

####################################################################################################################
# IF you are modifying the upload directory with a post value, it may be done here.
#
# Note: Making modifications based on posted input may be unsafe. Make sure your posted input is safe!
#
# You must override the $main::config->{'upload_dir'} value  
# If you are linking to the file you must also override the $main::config->{'path_to_upload'} value  
#
# eg. $main::config->{'upload_dir'} .= $query->param("employee_num") . '/';  
# eg. $main::config->{'path_to_upload'} .= $query->param("employee_num") . '/';  
###################################################################################################################

# Create a unique directory inside the upload directory if config setting 'unique_upload_dir' is enabled  
if($main::config->{'unique_upload_dir'}){  
	$unique_dir = &generate_random_string($main::config->{'unique_upload_dir_length'});  
	$main::config->{'upload_dir'} .= $unique_dir . '/';  

	if($main::config->{'link_to_upload'} || $main::config->{'link_to_upload_in_email'}){ $main::config->{'path_to_upload'} .= $unique_dir . '/'; }  
}

# Create upload directory if it does not exist
if(!-d $main::config->{'upload_dir'}){ mkdir($main::config->{'upload_dir'}, 0777) or &kak("<font color='red'>ERROR</font>: Failed to mkdir $main::config->{'upload_dir'}: $!", 1, __LINE__); }  

# Start processing the uploaded files
for my $upload_key (keys %{$query->{'.tmpfiles'}}){  
	# Get the file slot name eg. 'upfile_0'  
	$query->{'.tmpfiles'}->{$upload_key}->{info}->{'Content-Disposition'} =~ / name="([^"]*)"/;  
	my $file_slot = $1;

	# Get uploaded file name
	my $file_name = param($file_slot);

	# Get the upload file handle
	my $upload_filehandle = $query->upload($file_slot);

	# Get the CGI temp file name
	my $tmp_filename = $query->tmpFileName($upload_filehandle);

	# Get the type of file being uploaded
	my $content_type = $query->uploadInfo($upload_filehandle)->{'Content-Type'};  

	# Strip extra path info from the file (IE). Note: Will likely cause problems with foreign languages like chinese
	$file_name =~ s/.*[\/\\](.*)/$1/;

	# Normalize file name  Note: Will cause problems with foreign languages like chinese
	if($main::config->{'normalize_file_names'}){ $file_name = &normalize_filename($file_name, $main::config->{'normalize_file_delimiter'}, $main::config->{'normalize_file_length'}); }  

	# Get the file extention
	my ($f_name, $file_extension) = ($file_name =~ /(.*)\.(.+)/);

	########################################################################################################
	# IF you are modifying the file name with a post value, it may be done here.
	#
	# Note: Making modifications based on posted input may be unsafe. Make sure your posted input is safe!
	#
	# eg. $file_name = $f_name . "_" . $query->param("employee_num") . $file_extension;  
	########################################################################################################

	#################################################################################################################################################
	# IF you want to filter file uploads by disallowed AND allowed extensions, change the following line to use the allow extensions config setting.
	# eg. if((-s $tmp_filename) && ($file_extension !~ m/^$main::config->{'disallow_extensions'}$/i) && ($file_extension =~ m/^$main::config->{'allow_extensions'}$/i)){  
	#################################################################################################################################################################

	# Do not process zero length files or files with illegal extensions
	if((-s $tmp_filename) && ($file_extension !~ m/^$main::config->{'disallow_extensions'}$/i)){  
		# Create a unique filename if config setting 'unique_filename' is enabled  
		if($main::config->{'unique_file_name'}){  
			my $unique_file_name = generate_random_string($main::config->{'unique_file_name_length'});  
			$unique_file_name = $unique_file_name . "." . $file_extension;  
			$file_name = $unique_file_name;
		}

		# Check for an existing file and rename if it already exists
		if(!$main::config->{'overwrite_existing_files'}){ $file_name = &rename_filename($file_name, 1, $main::config->{'upload_dir'}); }  

		my $upload_file_path = $main::config->{'upload_dir'} . $file_name;  

		# Win wants the file handle closed before transfer
		close($upload_filehandle);

		# Transfer uploaded file to final destination
		move($tmp_filename, $upload_file_path) or copy($tmp_filename, $upload_file_path) or die("Cannot move/copy from $tmp_filename to $upload_file_path: $!");  

		chmod 0666, $upload_file_path;
	}

	# Store the upload file info
	$uploaded_files{$file_slot}{'file_size'} = &get_file_size($main::config->{'upload_dir'}, $file_name);  
	$uploaded_files{$file_slot}{'file_name'} = $file_name;  
	$uploaded_files{$file_slot}{'file_type'} = $content_type;  
}

# Timestamp end of upload (includes file transfer)
$end_upload = time();

# Delete the temp directory based on session-id and everything in it
&deldir($temp_dir_sid);

# Log Upload
if($main::config->{'log_uploads'}){ &log_upload($main::config, $config_file, $start_upload, $end_upload, $temp_sid, $unique_dir, %uploaded_files); }  

# Redirect to php page if redirect enabled else display results
if($main::config->{'redirect_after_upload'}){  
	# Create param directory if it does not exist
	if(!-d $main::config->{'param_dir'}){  
		if(!mkdir($main::config->{'param_dir'}, 0777)){  
			&kak("<font color='red'>ERROR<\/font>: Failed to create param directory $main::config->{'param_dir'}. $!", 0 , __LINE__);  
			&display_results($main::config, $start_upload, $end_upload, $temp_sid, %uploaded_files);
		}
	}

	my $param_file_path = $main::config->{'param_dir'} . $temp_sid . ".xml";  

	# We are re-directing so write a sid.xml file with upload info
	if(!open PARAMS, ">", "$param_file_path"){  
		&kak("<font color='red'>ERROR<\/font>: Failed to open $param_file_path. $!", 0 , __LINE__);  
		&display_results($main::config, $start_upload, $end_upload, $temp_sid, %uploaded_files);
	}

	binmode PARAMS;
	print PARAMS "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";  
	print PARAMS "<uu_upload>\n";  
	print PARAMS "  <config>\n";  
	print PARAMS "    <config_file>$config_file<\/config_file>\n";  
	print PARAMS "    <start_upload>$start_upload<\/start_upload>\n";  
	print PARAMS "    <end_upload>$end_upload<\/end_upload>\n";  
	print PARAMS "    <remote_ip>$ENV{REMOTE_ADDR}<\/remote_ip>\n";  
	print PARAMS "    <user_agent>$ENV{HTTP_USER_AGENT}<\/user_agent>\n";  
	print PARAMS "    <temp_sid>$temp_sid<\/temp_sid>\n";  
	print PARAMS "    <upload_dir>$main::config->{'upload_dir'}<\/upload_dir>\n";  
	print PARAMS "    <link_to_upload>$main::config->{'link_to_upload'}<\/link_to_upload>\n";  

	# If we want a direct link and or an email link, we need to pass the path_to_upload setting
	if($main::config->{'link_to_upload'} || ($main::config->{'send_email_on_upload'} && $main::config->{'link_to_upload_in_email'})){  
		print PARAMS "    <path_to_upload>$main::config->{'path_to_upload'}<\/path_to_upload>\n";  
	}

	# If 'unique_upload_dir' config setting is enabled pass the info  
	if($main::config->{'unique_upload_dir'}){  
		print PARAMS "    <unique_upload_dir>$main::config->{'unique_upload_dir'}<\/unique_upload_dir>\n";  
		print PARAMS "    <unique_dir>$unique_dir<\/unique_dir>\n";  
	}

	# If email on upload, write all the email info to param file
	if($main::config->{'send_email_on_upload'}){  
		print PARAMS "    <send_email_on_upload>1<\/send_email_on_upload>\n";  
		print PARAMS "    <link_to_upload_in_email>$main::config->{'link_to_upload_in_email'}<\/link_to_upload_in_email>\n";  
		print PARAMS "    <email_subject>$main::config->{'email_subject'}<\/email_subject>\n";  
		print PARAMS "    <html_email_support>$main::config->{'html_email_support'}<\/html_email_support>\n";  
		print PARAMS "    <to_email_address>$main::config->{'to_email_address'}<\/to_email_address>\n";  
		print PARAMS "    <from_email_address>$main::config->{'from_email_address'}<\/from_email_address>\n";  
	}
	else{ print PARAMS "    <send_email_on_upload>0<\/send_email_on_upload>\n"; }  

	print PARAMS "  <\/config>\n";  
	print PARAMS "  <post>\n";  

	# Write all posted values
	my @names = $query->param;

	foreach my $key (@names){
		my $post_value = $query->param($key);

		$post_value =~ s/&/&amp;/g;
		$post_value =~ s/</&lt;/g;
		$post_value =~ s/>/&apos;/g;
		$post_value =~ s/'/&gt;/g;  
		$post_value =~ s/"/&quot;/g;  

		$key =~ s/[^a-zA-Z0-9\_\-]/_/g;

		print PARAMS "    <$key>$post_value<\/$key>\n";  
	}

	print PARAMS "  <\/post>\n";  

	# Write all file uploads

	print PARAMS "  <file>\n";  

	for my $file_slot (keys %uploaded_files){
		my $file_name = $uploaded_files{$file_slot}{'file_name'};  
		my $file_size = $uploaded_files{$file_slot}{'file_size'};  
		my $file_type = $uploaded_files{$file_slot}{'file_type'};  

		print PARAMS "    <file_upload>\n";  
		print PARAMS "      <slot>$file_slot<\/slot>\n";  
		print PARAMS "      <name>$file_name<\/name>\n";  
		print PARAMS "      <size>$file_size<\/size>\n";  
		print PARAMS "      <type>$file_type<\/type>\n";  
		print PARAMS "    <\/file_upload>\n";  
	}

	print PARAMS "  <\/file>\n";  
	print PARAMS "<\/uu_upload>\n";  

	close(PARAMS);
	chmod 0666, $param_file_path;

	# Append random string so finished page does not cache
	my $redirect_url = $main::config->{'redirect_url'} . "?rnd_id=" . &generate_random_string(8);  

	# Force 'redirect_using_location' if user does not have a javascript capable browser or using embedded_upload_results  
	if(($query->param('no_script') && $query->param('no_script') == 1) || ($query->param('embedded_upload_results') && $query->param('embedded_upload_results') == 1)){  
		$main::config->{'redirect_using_js_html'} = 0;  
		$main::config->{'redirect_using_location'} = 1;  
	}

	# Perform Redirect
	if($main::config->{'redirect_using_js_html'}){  
		if($main::config->{'pass_param_dir'}){  
			&kak("<form name='redirect' method='post' action=\"$redirect_url\"><input type='hidden' name='temp_sid' value=\"$temp_sid\"><input type='hidden' name='param_dir' value=\"$main::config->{'param_dir'}\"></form><script language='javascript' type='text/javascript'>document.redirect.submit()</script>", 1, __LINE__);  
		}
		else{
			&kak("<form name='redirect' method='post' action=\"$redirect_url\"><input type='hidden' name='temp_sid' value=\"$temp_sid\"></form><script language='javascript' type='text/javascript'>document.redirect.submit()</script>", 1, __LINE__);  
		}
	}
	else{
		# Append the temp_sid and param_dir path to the redirect url.
		if($main::config->{'pass_param_dir'}){ $redirect_url .= "&temp_sid=$temp_sid&param_dir=$main::config->{'param_dir'}"; }  
		else{ $redirect_url .= "&temp_sid=" . $temp_sid; }  

		if($main::config->{'redirect_using_html'}){  
			print "content-type:text/html; charset=utf-8\n\n";  
			print "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><meta http-equiv=\"refresh\" content=\"0; url='$redirect_url'\"></head><body></body></html>";  
		}
		elsif($main::config->{'redirect_using_js'}){  
			&kak("<script language=\"javascript\" type=\"text/javascript\">location.href='$redirect_url';</script>", 1, __LINE__);  
		}
		elsif($main::config->{'redirect_using_location'}){  
			# Uncomment next line if using Webstar V
			# print "HTTP/1.1 302 Redirection\n";  
			print "Location: $redirect_url\n\n";  
		}
	}
}
else{
	# We are not redirecting so dump upload results to screen using CGI
	&display_results($main::config, $start_upload, $end_upload, $temp_sid, %uploaded_files);
}

exit;

######################################################### START SUBROUTINES ###################################################

###########################################
# Create a log of the upload in XML format
###########################################
sub log_upload{
	my $config = shift;
	my $config_file = shift;
	my $start_upload = shift;
	my $end_upload = shift;
	my $temp_sid = shift;
	my $unique_dir = shift;
	my $uploaded_files = shift;
	my @names = $query->param;

	# Create log directory if it does not exist
	if(!-d $config->{'log_dir'}){  
		if(!mkdir($config->{'log_dir'}, 0777)){  
			&kak("Failed to create uu log directory $!", 0 , __LINE__);  
			return;
		}
	}

	my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
	$year += 1900;
	$mon++;

	my $day_dir = $config->{'log_dir'} . $year . '-' . $mon . '-' . $mday;  

	# Create day log directory if it does not exist
	if(!-d $day_dir){
		if(!mkdir($day_dir, 0777)){
			&kak("Failed to create uu day log directory $!", 0 , __LINE__);  
			return;
		}
	}

	# Log file format = temp_sid-8_character_random_string-unix_time_stamp.log
	my $random_string = &generate_random_string(8);
	my $log_file = $day_dir . '/' . $temp_sid . '-' . $random_string . '-' . time() . '.xml';  

	# Create log file
	if(open(UULOG, ">" , "$log_file")){  
		binmode UULOG;
		print UULOG "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";  
		print UULOG "<uu_upload>\n";  
		print UULOG "  <config>\n";  
		print UULOG "    <config_file>$config_file<\/config_file>\n";  
		print UULOG "    <start_upload>$start_upload<\/start_upload>\n";  
		print UULOG "    <end_upload>$end_upload<\/end_upload>\n";  
		print UULOG "    <remote_ip>$ENV{REMOTE_ADDR}<\/remote_ip>\n";  
		print UULOG "    <user_agent>$ENV{HTTP_USER_AGENT}<\/user_agent>\n";  
		print UULOG "    <temp_sid>$temp_sid<\/temp_sid>\n";  

		if($config->{'unique_upload_dir'}){ print UULOG "    <unique_dir>$unique_dir<\/unique_dir>\n"; }  

		print UULOG "  <\/config>\n";  

		# Log post values
		if($config->{'log_params'}){  
			print UULOG "  <post>\n";  

			foreach my $key (@names){
				my $post_value = $query->param($key);

				$post_value =~ s/&/&amp;/g;
				$post_value =~ s/</&lt;/g;
				$post_value =~ s/>/&apos;/g;
				$post_value =~ s/'/&gt;/g;  
				$post_value =~ s/"/&quot;/g;  

				$key =~ s/[^a-zA-Z0-9\_\-]/_/g;

				print UULOG "    <$key>$post_value<\/$key>\n";  
			}

			print UULOG "  <\/post>\n";  
		}

		print UULOG "  <file>\n";  

		# Log upload file info
		for my $file_slot (keys %uploaded_files){
			my $file_name = $uploaded_files{$file_slot}{'file_name'};  
			my $file_size = $uploaded_files{$file_slot}{'file_size'};  
			my $file_type = $uploaded_files{$file_slot}{'file_type'};  

			print UULOG "    <file_upload>\n";  
			print UULOG "      <file_slot>$file_slot<\/file_slot>\n";  
			print UULOG "      <file_name>$file_name<\/file_name>\n";  
			print UULOG "      <file_size>$file_size<\/file_size>\n";  
			print UULOG "      <file_type>$file_type<\/file_type>\n";  
			print UULOG "    <\/fileUpload>\n";  
		}

		print UULOG "  <\/file>\n";  
		print UULOG "<\/uu_upload>\n";  
		close(UULOG);
		chmod 0666, $log_file;
	}
	else{ &kak("Failed to create log file $!", 0, __LINE__); }  
}

##################################################
# Get the size of the ploaded file if it exists
##################################################
sub get_file_size{
	my $upload_dir = shift;
	my $file_name = shift;
	my $path_to_file = $upload_dir . $file_name;
	my $file_size = 0;

	if(-e $path_to_file && -f $path_to_file){ $file_size = -s $path_to_file; }

	return $file_size;
}

########################################
# Delete the temp dir based on temp_sid
########################################
sub cleanup{ &deldir($temp_dir_sid); }

####################################
# Format the upload result and exit
####################################
sub display_results{
	my $config = shift;
	my $start_upload = shift;
	my $end_upload = shift;
	my $temp_sid = shift;
	my $uploaded_files = shift;
	my ($upload_result, $email_file_list, $bg_col) = ();
	my $i = 0;
	my $end_char = "\n";  

	if($config->{'html_email_support'}){ $end_char = "<br>\n"; }  

	if(defined($query->param('embedded_upload_results')) && $query->param('embedded_upload_results') == 1){  
		$upload_result .= "<script language=\"javascript\" type=\"text/javascript\">\n";  
		$upload_result .= "  parent.document.getElementById('upload_div').style.display = \"\";\n";  
		$upload_result .= "  parent.iniFilePage();\n";  
		$upload_result .= "</script>\n";  
	}

	$upload_result .= "<table cellpadding='1' cellspacing='1' width='70%'>\n";  
	$upload_result .= "  <tr>\n";  
	$upload_result .= "    <td align='center' bgcolor='bbbbbb'>&nbsp;&nbsp;<b>UPLOADED FILE NAME</b>&nbsp;&nbsp;</td><td align='center' bgcolor='bbbbbb'>&nbsp;&nbsp;<b>UPLOADED FILE SIZE</b>&nbsp;&nbsp;</td>\n";  
	$upload_result .= "  </tr>\n";  

	for my $file_slot (keys %uploaded_files){
		my $file_name = $uploaded_files{$file_slot}{'file_name'};  
		my $file_size = $uploaded_files{$file_slot}{'file_size'};  
		my $file_type = $uploaded_files{$file_slot}{'file_type'};  

		if($i%2){ $bg_col = 'cccccc'; }  
		else{ $bg_col = 'dddddd'; }  

		if($file_size > 0){
			$file_size = &format_bytes($file_size, 99);

			if($config->{'link_to_upload'}){  
				$upload_result .= "<tr><td align='center' bgcolor='$bg_col'><a href=\"$config->{'path_to_upload'}$file_name\" TARGET=\"_blank\">$file_name</a><\/td><td align='center' bgcolor='$bg_col'>$file_size<\/td><\/tr>\n";  
			}
			else{
				$upload_result .= "<tr><td align='center' bgcolor='$bg_col'>&nbsp;$file_name&nbsp;<\/td><td align='center' bgcolor='$bg_col'>$file_size<\/td><\/tr>\n";  
			}

			if($config->{'link_to_upload_in_email'}){  
				$email_file_list .= "File Name: $config->{'path_to_upload'}$file_name     File Size: $file_size" . $end_char;  
			}
			else{
				if($config->{'unique_upload_dir'}){ $email_file_list .= "File Name: $unique_dir/$file_name     File Size: $file_size\n"; }  
				else{ $email_file_list .= "File Name: $file_name     File Size: $file_size" . $end_char; }  
			}
		}
		else{
			$upload_result .= "<tr><td align='center' bgcolor='$bg_col'>&nbsp;$file_name&nbsp;<\/td><td align='center' bgcolor='$bg_col'><font color='red'>Failed To Upload<\/font><\/td><\/tr>\n";  
			$email_file_list .= "File Name: $file_name     File Size: Failed To Upload !" . $end_char;  
		}

		$i++;
	}

	$upload_result .= "</table>\n";  
	$upload_result .= "<br>\n";  

	if($config->{'send_email_on_upload'}){ &email_upload_results($email_file_list, $config, $start_upload, $end_upload, $temp_sid); }  
	&kak($upload_result, 1, __LINE__);
}

##########################################
#  Send an email with the upload results.
##########################################
sub email_upload_results{
	my $file_list = shift;
	my $config = shift;
	my $start_upload = shift;
	my $end_upload = shift;
	my $temp_sid = shift;

	my $path_to_sendmail = "/usr/sbin/sendmail -t";  
	my $message;
	my $end_char = "\n";  
	my ($ssec, $smin, $shour, $smday, $smon, $syear, $swday, $syday, $sisdst) = localtime($start_upload);
	my ($esec, $emin, $ehour, $emday, $emon, $eyear, $ewday, $eyday, $eisdst) = localtime($end_upload);
	my @abbr = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');  

	$eyear += 1900;
	$syear += 1900;

	if(open(SENDMAIL, "|$path_to_sendmail")){  
		print SENDMAIL "From:" . $config->{'from_email_address'} . "\n";  
		print SENDMAIL "To:" . $config->{'to_email_address'} . "\n";  
		print SENDMAIL "Subject:" . $config->{'email_subject'} . "\n";  

		if($config->{'html_email_support'}){  
			print SENDMAIL 'Content-type: text/html; charset=utf-8; format=flowed' . "\r\n";  
			$end_char = "<br>\n";  
		}
		else{ print SENDMAIL 'Content-type: text/plain; charset=utf-8; format=flowed' . "\r\n"; }  

		$message .= "SID: ". $temp_sid . $end_char;  
		$message .= "\nStart Upload: " . $abbr[$smon] . " " . $smday . ", " . $syear . ", " .  $shour . ":" . $smin . ":" . $ssec . $end_char;  
		$message .= "End Upload: " . $abbr[$emon] . " " . $emday . ", " . $eyear . ", " .  $ehour . ":" . $emin . ":" . $esec . $end_char;  
		$message .= "Remote IP: " . $ENV{'REMOTE_ADDR'}  . $end_char;  
		$message .= "Browser: " . $ENV{'HTTP_USER_AGENT'} . $end_char . $end_char;  
		$message .= $file_list;

		print SENDMAIL $message;
		close(SENDMAIL);
	}
	else{ warn("Failed to open sendmail $!"); }  
}

####################################################
#  formatBytes($file_size, 99) mixed file sizes
#  formatBytes($file_size, 0) KB file sizes
#  formatBytes($file_size, 1) MB file sizes etc
####################################################
sub format_bytes{
	my $bytes = shift;
	my $byte_format = shift;
	my $byte_size = 1024;
	my $i = 0;
	my @byte_type = (" KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");  

	$bytes /= $byte_size;

	if($byte_format == 99 || $byte_format > 7){
		while($bytes > $byte_size){
			$bytes /= $byte_size;
			$i++;
		}
	}
	else{
		while($i < $byte_format){
			$bytes /= $byte_size;
			$i++;
		}
	}

	$bytes = sprintf("%1.2f", $bytes);  
	$bytes .= $byte_type[$i];

	return $bytes;
}

############################################
# Rename uploaded file if it already exists
############################################
sub rename_filename{
	my $file_name = shift;
	my $count = shift;
	my $upload_dir = shift;
	my $path_to_file = $upload_dir . $file_name;

	if(-e $path_to_file && -f $path_to_file){
		if($file_name =~ /(.*)_(\d*)\.(.*)/){
			# Already renamed so count on
			$count = $2 + 1;
			$file_name =~ s/(.*)_(\d*)\.(.*)/$1_$count\.$3/;
		}
		else{
			# Not renamed so start counting
			$file_name =~ s/(.*)\.(.*)/$1_$count\.$2/;
		}
		&rename_filename($file_name, $count, $upload_dir);
	}
	else{ return $file_name; }
}

#######################
# Normalize file name
######################
sub normalize_filename{
	my $file_name = shift;
	my $delimiter = shift;
	my $max_file_length = shift;

	$file_name =~ s/^\s+//;   # Trim left
	$file_name =~ s/\s+$//;   # Trim right

	# Check the length of the file name and cut if neseccary
	if(length($file_name) > $max_file_length){ $file_name = substr($file_name, length($file_name) - $max_file_length); }

	# Search and replace illegal file name characters
	$file_name =~ s/[^a-zA-Z0-9\_\.\-]/$delimiter/g;

	return $file_name;
}

#########################
# Generate Randon String
#########################
sub generate_random_string{
	my $length_of_randomstring = shift;
	my @chars=('a'..'z', '0'..'9');  
	my $random_string;

	for(my $i = 0; $i < $length_of_randomstring; $i++){ $random_string .= $chars[int(rand(36))]; }

	return $random_string;
}

#####################################################################
# Print config, driver settings and 'Environment Variables' to screen  
#####################################################################
sub debug{
	my $config = shift;
	my $uber_version = shift;
	my $this_version = shift;
	my $multi_configs_enabled = shift;
	my ($msg, $temp_dir_state, $upload_dir_state, $param_dir_state) = ();

	if(!-d $config->{'temp_dir'}){ $temp_dir_state = "<font color='red'>$config->{'temp_dir'}<\/font>"; }  
	else{ $temp_dir_state = "<font color='green'>$config->{'temp_dir'}<\/font>"; }  

	if(!-d $config->{'upload_dir'}){ $upload_dir_state = "<font color='red'>$config->{'upload_dir'}<\/font>"; }  
	else{ $upload_dir_state = "<font color='green'>$config->{'upload_dir'}<\/font>"; }  

	$msg .= "<u><b>UBER UPLOADER CONFIG SETTINGS<\/b><\/u><br>\n";  

	my $perlversion = $];
	my $cgiversion = $CGI::VERSION;
	my $filecopyversion = $File::Copy::VERSION;

	$msg .= "UBER UPLOADER VERSION = <b>$uber_version<\/b><br>\n";  
	$msg .= "UU_UPLOAD = <b>$this_version<\/b><br>\n";  
	$msg .= "CONFIG_FILE = <b>$config->{'config_file_name'}<\/b><br>\n";  
	$msg .= "PERL VERSION = <b>$perlversion<\/b><br>\n";  
	$msg .= "CGI.PM VERSION = <b>$cgiversion<\/b><br>\n";  
	$msg .= "FILE::COPY VERSION = <b>$filecopyversion<\/b><br>\n";  

	if($multi_configs_enabled){ $msg .= "MULTI_CONFIGS_ENABLED = <b><font color='green'>enabled</font><\/b><br>\n"; }  
	else{ $msg .= "MULTI_CONFIGS_ENABLED = <b><font color='red'>disabled</font><\/b><br>\n"; }  

	$msg .= "TEMP_DIR = <b>$temp_dir_state<\/b><br>\n";  
	$msg .= "UPLOAD_DIR = <b>$upload_dir_state<\/b><br>\n";  
	$msg .= "MAX_UPLOAD = <b>" . format_bytes($config->{'max_upload'}, 99) . "<\/b><br>\n";  
	$msg .= "GET_DATA_SPEED = <b>$config->{'get_data_speed'}<\/b><br>\n";  

	if($config->{'unique_upload_dir'}){  
		$msg .= "UNIQUE_UPLOAD_DIR = <b><font color='green'>enabled</font><\/b><br>\n";  
		$msg .= "UNIQUE_UPLOAD_DIR_LENGTH = <b>$config->{'unique_upload_dir_length'} chars<\/b><br>\n";  
	}
	else{ $msg .= "UNIQUE_UPLOAD_DIR = <b><font color='red'>disabled</font><\/b><br>\n"; }  

	if($config->{'unique_file_name'}){  
		$msg .= "UNIQUE_FILE_NAME = <b><font color='green'>enabled</font><\/b><br>\n";  
		$msg .= "UNIQUE_FILE_NAME_LENGTH = <b>$config->{'unique_file_name_length'} chars<\/b><br>\n";  
	}
	else{ $msg .= "UNIQUE_FILE_NAME = <b><font color='red'>disabled</font><\/b><br>\n"; }  

	if($config->{'overwrite_existing_files'}){ $msg .= "OVERWRITE_EXISTING_FILES = <b><font color='green'>enabled</font><\/b><br>\n"; }  
	else{ $msg .= "OVERWRITE_EXISTING_FILES = <b><font color='red'>disabled</font><\/b><br>\n"; }  

	if($config->{'normalize_file_names'}){ $msg .= "NORMALIZE_FILE_NAMES = <b><font color='green'>enabled</font><\/b><br>\n"; }  
	else{ $msg .= "NORMALIZE_FILE_NAMES = <b><font color='red'>disabled</font><\/b><br>\n"; }  

	if($config->{'normalize_file_names'}){ $msg .= "NORMALIZE_FILE_LENGTH = <b>$config->{'normalize_file_length'} chars<\/b><br>\n"; }  

	if($config->{'normalize_file_names'}){ $msg .= "NORMALIZE_FILE_DELIMITER = <b>$config->{'normalize_file_delimiter'}<\/b><br>\n"; }  

	if($config->{'link_to_upload'}){  
		$msg .= "LINK_TO_UPLOAD = <b><font color='green'>enabled</font><\/b><br>\n";  
		$msg .= "PATH_TO_UPLOAD = <a href=\"$config->{'path_to_upload'}\">$config->{'path_to_upload'}</a><br>\n";  
	}
	else{ $msg .= "LINK_TO_UPLOAD = <b><font color='red'>disabled</font><\/b><br>\n"; }  

	if($config->{'send_email_on_upload'}){  
		$msg .= "SEND_EMAIL_ON_UPLOAD = <b><font color='green'><a href=\"mailto:$config->{'to_email_address'}?subject=Uber Uploader Email Test\">enabled</a></font><\/b><br>\n";  
		$msg .= "EMAIL_SUBJECT = <b>$config->{'email_subject'}<\/b><br>\n";  

		if($config->{'html_email_support'}){ $msg .= "HTML_EMAIL_SUPPORT = <b><font color='green'>enabled</font><\/b><br>\n"; }  
		else{ $msg .= "HTML_EMAIL_SUPPORT = <b><font color='red'>disabled</font><\/b><br>\n"; }  

		if($config->{'link_to_upload_in_email'}){ $msg .= "LINK_TO_UPLOAD_IN_EMAIL = <b><font color='green'>enabled</font><\/b><br>\n"; }  
		else{ $msg .= "LINK_TO_UPLOAD_IN_EMAIL = <b><font color='red'>disabled</font><\/b><br>\n"; }  
	}
	else{ $msg .= "SEND_EMAIL_ON_UPLOAD = <b><font color='red'>disabled</font><\/b><br>\n"; }  

	if($config->{'redirect_after_upload'}){  
		$msg .= "REDIRECT_AFTER_UPLOAD = <b><font color='green'>enabled</font><\/b><br>\n";  
		$msg .= "REDIRECT_URL = <a href=\"$config->{'redirect_url'}?cmd=about\">$config->{'redirect_url'}<\/a><br>\n";  

		if(!-d $config->{'param_dir'}){ $param_dir_state = "<font color='red'>$config->{'param_dir'}<\/font>"; }  
		else{ $param_dir_state = "<font color='green'>$config->{'param_dir'}<\/font>"; }  

		$msg .= "PARAM_DIR = <b>$param_dir_state<\/b><br>\n";  

		if($config->{'pass_param_dir'}){ $msg .= "PASS_PARAM_DIR = <b><font color='green'>enabled</font><\/b><br>\n"; }  
		else{ $msg .= "PASS_PARAM_DIR = <b><font color='red'>disabled</font><\/b><br>\n"; }  

		if($config->{'redirect_using_js_html'}){ $msg .= "REDIRECT_USING_JS_HTML = <b><font color='green'>enabled</font><\/b><br>\n"; }  
		else{ $msg .= "REDIRECT_USING_JS_HTML = <b><font color='red'>disabled</font><\/b><br>\n"; }  

		if($config->{'redirect_using_html'}){ $msg .= "REDIRECT_USING_HTML = <b><font color='green'>enabled</font><\/b><br>\n"; }  
		else{ $msg .= "REDIRECT_USING_HTML = <b><font color='red'>disabled</font><\/b><br>\n"; }  

		if($config->{'redirect_using_js'}){ $msg .= "REDIRECT_USING_JS = <b><font color='green'>enabled</font><\/b><br>\n"; }  
		else{ $msg .= "REDIRECT_USING_JS = <b><font color='red'>disabled</font><\/b><br>\n"; }  

		if($config->{'redirect_using_location'}){ $msg .= "REDIRECT_USING_LOCATION = <b><font color='green'>enabled</font><\/b><br>\n"; }  
		else{ $msg .= "REDIRECT_USING_LOCATION = <b><font color='red'>disabled</font><\/b><br>\n"; }  
	}
	else{ $msg .= "REDIRECT_AFTER_UPLOAD = <b><font color='red'>disabled</font><\/b><br>\n"; }  

	if($config->{'log_uploads'}){  
		$msg .= "LOG_UPLOADS = <b><font color='green'>enabled</font><\/b><br>\n";  

		if($config->{'log_params'}){ $msg .= "LOG_PARAMS = <b><font color='green'>enabled</font><\/b><br>\n"; }  
		else{ $msg .= "LOG_PARAMS = <b><font color='red'>disabled</font><\/b><br>\n"; }  

		if(!-d $config->{'log_dir'}){ $msg .= "LOG_DIR = <b><font color='red'>$config->{'log_dir'}<\/font><\/b><br>\n"; }  
		else{ $msg .= "LOG_DIR = <b><font color='green'>$config->{'log_dir'}<\/font><\/b><br>\n"; }  

	}
	else{ $msg .= "LOG_UPLOADS = <b><font color='red'>disabled</font><\/b><br>\n"; }  

	$msg .= "<br><u><b>ENVIRONMENT VARIABLES<\/b><\/u><br>\n";  

	foreach my $key (sort keys(%ENV)){ $msg .= "$key = <b>$ENV{$key}<\/b><br>\n"; }  

	&kak($msg, 1, __LINE__);
}

Content-Key: 75425

Url: https://administrator.de/contentid/75425

Printed on: April 19, 2024 at 10:04 o'clock

Mitglied: 58502
58502 Dec 06, 2007 at 20:43:42 (UTC)
Goto Top
Reife Leistung, 942 Code Zeilen im Beitrag!

Hier fehlt ein Smiley.........
Mitglied: 35801
35801 Dec 06, 2007 at 21:05:07 (UTC)
Goto Top
Sorry....

Ich weiß leider nicht welcher Teil für die Verzeichnis-Erstellung zuständig ist...

Ich hoffe jemand hat die Muse und reißt mich aus dem Schlamessel raus...

Ach ja...
Hätt ich fast vergessen...


face-smile ... Ein Smiley... NUR für dich!
Member: Dani
Dani Dec 06, 2007 at 21:05:29 (UTC)
Goto Top
Abend,
also das ne ich mal n Perl-Script. Direkt kann ich dir leider im Moment nicht weiterhelfen, da ich das CMS o.ä. nicht komplett kenne. Aber diese Zeilen sollten dir weiterhelfen:
# Create a unique directory inside the upload directory if config setting 'unique_upload_dir' is enabled 
if($main::config->{'unique_upload_dir'}){  
	$unique_dir = &generate_random_string($main::config->{'unique_upload_dir_length'});  
	$main::config->{'upload_dir'} .= $unique_dir . '/';  

	if($main::config->{'link_to_upload'} || $main::config->{'link_to_upload_in_email'}){ $main::config->{'path_to_upload'} .= $unique_dir . '/'; }  
}
Ansonsten muss ich mir das mal in Ruhe anschauen.


Grüße
Dani
Mitglied: 35801
35801 Dec 06, 2007 at 21:09:57 (UTC)
Goto Top
Glaubst du die könnte funzen:

$main::config->{'upload_dir'} .= $unique_dir . '/' . $pd. '/'; ???


Danke,
Peter