Lunarpages Web Hosting Forum

Scripting Languages Hosting Help => C++ / PERL / CGI Support => Topic started by: fo0hzy on July 22, 2004, 08:00:41 PM

Title: CGI upload script
Post by: fo0hzy on July 22, 2004, 08:00:41 PM
Hi. I'm trying to use Upload Lite (http://www.perlscriptsjavascripts.com/perl/upload_lite/) on my site, which is a simple upload script I want to provide for my forum members to use, for hosting pictures on the forum only.

I've been chmod'ing and editing and changing paths for about three hours now, and still can't get it right...

This is the extent of the editable user code in upload.cgi
Code: [Select]
# START USER EDITS

# absolute path to folder files will be uploaded to.
# WINDOWS users, your path would like something like : images\\uploads
# UNIX    users, your path would like something like : /home/www/images/uploads
# do not end the path with any slashes and if you're on a UNIX serv, make sure
# you CHMOD each folder in the path to 777

$dir = "/home/public_html/uploads";  
#$dir = "d:\\html\\users\\html\\images";

# absolute URL to folder files will be uploaded to
$folder = "http://www.dangerouslystupid.lunarpages.com/uploads";

# maximum file size allowed (kilo bytes)
$max = 135;

# for security reasons, enter your domain name.
# this is so uploads may only occur from your domain
# enter any part of your domain name, or leave this
# blank if you don't mind other web sites using your copy
$domain = "dangerouslystupid";

# if a file is successfully uploaded, enter a URL to redirect to.
# leave this blank to have the default message printed. If using
# this var, it must begin with http
$redirect = "";

# if you would like to be notified of uploads, enter your email address
# between the SINGLE quotes. leave this blank if you would not like to be notified
$notify = 'you@yourserver.com';

# UNIX users, if you entered a value for $notify, you must also enter your
# server's sendmail path. It usually looks something like : /usr/sbin/sendmail
$send_mail_path = "/usr/sbin/sendmail";

# WINDOWS users, if you entered a value for $notify, you must also enter your
# server's SMTP path. It usually looks something like : mail.servername.com
$smtp_path = "mail.yourserver.com";

# set to 1 if you would like all files in the directory printed to the web page
# after a successful upload (only printed if redirect is off). Set to 0 if you
# do not want filenames printed to web page
$print_contents = 1;

# allow overwrites? 1 = yes, 0 = no (0 will rename file with a number on the end, the
# highest number is the latest file)
$overwrite = 0;

# file types allowed, enter each type on a new line
# Enter the word "ALL" in uppercase, to accept all file types.
@types = qw~

txt
jpeg
jpg
gif
swf

~;

####################################################################
#    END USER EDITS
####################################################################



/cgi-bin folder permissions are set at 755
/cgi-bin/upload.cgi permissions are set at 755

Please help! I don't like the phpbb attachment mod...  :cry:

I keep getting this error (the same one every time):
Quote
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@dangerouslystupid.lunarpages.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.


Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Title: CGI upload script
Post by: Priest on July 23, 2004, 01:59:49 AM
Hello,

This path: $dir = "/home/public_html/uploads";

should look like this: $dir = "/home/cPanel username/public_html/uploads"; (insert your cPanel username at the prompted space)
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 06:50:35 AM
Quote from: StevenP
$dir = "/home/cPanel username/public_html/uploads"


What should be between cpanel & my username? Cuz that doesn't work either... thanks :wink:
Title: CGI upload script
Post by: leighsww on July 23, 2004, 10:15:35 AM
The username you use to login to cPanel, the one that LP gave you when you signed up. Forget the "CPanel" word in that path Steven gave you (he was just trying to tell you that you use your cPanel login), here this may be less ambiguous:

Code: [Select]
$dir = "/home/username/public_html/uploads";

Replace "username" with your login username.
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 03:10:58 PM
lol... d'oh :oops:

Yes that makes sense. Unfortunately it doesn't work, either... same exact error
Title: CGI upload script
Post by: leighsww on July 23, 2004, 04:17:34 PM
Okay, I see you are using a sub-domain, since you have this:

Code: [Select]
$folder = "http://www.dangerouslystupid.lunarpages.com/uploads";

Now, I would think this line then should read:

Code: [Select]
$domain = "dangerouslystupid.lunarpages.com";

It may also need the "www" version, as well.

Please check to see if you have a sub-folder called "dangerouslystupid" inside your public_html, and also check where the "uploads" folder is at (is it inside the public_html or inside the "dangerouslystupid" sub-folder? Cuz the latter will then alter the $dir config path)

Also, it seems you haven't entered any of the other configs, like:

Code: [Select]
$notify = 'you@yourserver.com';

Did you leave them unchanged only for this forum post, and it is changed in your actual script?

I am also assuming that you purposely didn't enter your entire script and are only showing us the "user config" parts (as you mentioned in your first post).  But, I have to mention, cuz we've had many times where the person's problem was cuz they were missing parts of the script but we didn't find out (cause we "assumed") until several pages of posts.  I don't know what your level of scripting knowledge is, and I don't want to offend you, but I'm just gonna tell you this just in case (to make sure you have the entire script) ... make sure that it starts with:

Code: [Select]
#!/usr/bin/perl

It might have some other stuff at the end of it like "-t" etc. but it has to start with that above line as the first line of code.

Also, make sure to compare the ending of the script with the original one you downloaded cuz many times that gets accidentally truncated when uploading your script to the server via cPanel.  This, too, has been many a time the culprit.
Title: CGI upload script
Post by: leighsww on July 23, 2004, 04:47:04 PM
Also, put a # in front of the following code, because you will be using the send_mail_path.  It should then look like this:

Code: [Select]
#$smtp_path = "mail.yourserver.com";
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 04:52:50 PM
Quote from: leighsww
Okay, I see you are using a sub-domain, since you have this:

Code: [Select]
$folder = "http://www.dangerouslystupid.lunarpages.com/uploads";

Now, I would think this line then should read:

Code: [Select]
$domain = "dangerouslystupid.lunarpages.com";

It may also need the "www" version, as well.

Please check to see if you have a sub-folder called "dangerouslystupid" inside your public_html, and also check where the "uploads" folder is at (is it inside the public_html or inside the "dangerouslystupid" sub-folder? Cuz the latter will then alter the $dir config path)

Also, it seems you haven't entered any of the other configs, like:

Code: [Select]
$notify = 'you@yourserver.com';
$smtp_path = "mail.yourserver.com";


Did you leave them unchanged only for this forum post, and it is changed in your actual script?

I don't know what your level of scripting knowledge is, and I don't want to offend you, but I'm just gonna tell you this just in case (to make sure you have the entire script) ... make sure that it starts with:


No offense taken whatsoever, I have almost no level of scripting experience! :D and I appreciate your taking the time to help me out.

I tried changing the path... tried these variations:

dangerouslystupid.lunarpages.com
dangerouslystupid.lunarpages.com/uploads
www.dangerouslystupid.lunarpages.com
www.dangerouslystupid.lunarpages.com/uploads

The uploads folder is in the public_html directory, and both that & the cgi-bin folder are chmodded 777 (which is what the script instructions call for... however, when I check the cpanel error log, it says:"error: directory is writable by others" so maybe there is a problem with the script itself. I will paste the entire script at the bottom of this reply.

I didn't enter the mail server info because I wasn't intending to make use of that function. However, if you think it is advisable to do so, I'll reconsider.

Again, thanks for your help :thumb:

Code: [Select]
<html><body bgcolor="#ffffff">#!/usr/bin/perl

# Installation Instructions
# http://www.perlscriptsjavascripts.com/perl/upload_lite/users_guide.html

# To order a custom install, please visit our "Secure order" page
# and enter the standard installation fee in the "Custom Quote" field

####################################################################
#
# Upload Lite.
# 2002, PerlscriptsJavaScripts.com
#
# Requirements: Perl5 WINDOWS NT or UNIX
# Created: Febuary , 2001
# Author: John Krinelos
# Version: 3.23
#
# Based on Upload Gold, first release : September 2001
#
# This script is free, as long as this header and any copyright messages
# remains in tact. To remove copyright messages from public web pages you
# must purchase copyright removal.
# http://www.perlscriptsjavascripts.com/copyright_fees.html
#
# Agent for copyright :
# Gene Volovich
# Law Partners,
# 140 Queen St.
# Melbourne
# Ph. +61 3 9602 2266
# gvolovich@lawpartners.com.au
# http://www.lawpartners.com.au/
#
####################################################################

# START USER EDITS

# absolute path to folder files will be uploaded to.
# WINDOWS users, your path would like something like : images\\uploads
# UNIX    users, your path would like something like : /home/www/images/uploads
# do not end the path with any slashes and if you're on a UNIX serv, make sure
# you CHMOD each folder in the path to 777

$dir = "/home/danger7/public_html/uploads"; ;  
#$dir = "d:\\html\\users\\html\\images";

# absolute URL to folder files will be uploaded to
$folder = "www.dangerouslystupid.lunarpages.com/uploads";

# maximum file size allowed (kilo bytes)
$max = 135;

# for security reasons, enter your domain name.
# this is so uploads may only occur from your domain
# enter any part of your domain name, or leave this
# blank if you don't mind other web sites using your copy
$domain = "dangerouslystupid";

# if a file is successfully uploaded, enter a URL to redirect to.
# leave this blank to have the default message printed. If using
# this var, it must begin with http
$redirect = "";

# if you would like to be notified of uploads, enter your email address
# between the SINGLE quotes. leave this blank if you would not like to be notified
$notify = 'you@yourserver.com';

# UNIX users, if you entered a value for $notify, you must also enter your
# server's sendmail path. It usually looks something like : /usr/sbin/sendmail
$send_mail_path = "/usr/sbin/sendmail";

# WINDOWS users, if you entered a value for $notify, you must also enter your
# server's SMTP path. It usually looks something like : mail.servername.com
$smtp_path = "mail.yourserver.com";

# set to 1 if you would like all files in the directory printed to the web page
# after a successful upload (only printed if redirect is off). Set to 0 if you
# do not want filenames printed to web page
$print_contents = 1;

# allow overwrites? 1 = yes, 0 = no (0 will rename file with a number on the end, the
# highest number is the latest file)
$overwrite = 0;

# file types allowed, enter each type on a new line
# Enter the word "ALL" in uppercase, to accept all file types.
@types = qw~

txt
jpeg
jpg
gif
swf

~;

####################################################################
#    END USER EDITS
####################################################################

$folder =~ s/(\/|\\)$//ig;

$OS = $^O; # operating system name
if($OS =~ /darwin/i) { $isUNIX = 1; }
elsif($OS =~ /win/i) { $isWIN = 1; }
else {$isUNIX = 1;}

if($isWIN){ $S{S} = "\\\\"; }
else { $S{S} = "/";} # seperator used in paths

$ScriptURL = "http://$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}";

unless (-d "$dir"){
mkdir ("$dir", 0777); # unless the dir exists, make it ( and chmod it on UNIX )
chmod(0777, "$dir");
}

unless (-d "$dir"){
# if there still is no dir, the path entered by the user is wrong and the upload will fail
&PrintHead; #print the header

# get the Win root
$ENV{PATH_INFO} =~ s/\//$S{S}/gi;
$ENV{PATH_TRANSLATED} =~ s/$ENV{PATH_INFO}//i;

print qq~
<table width="600">
<tr>
<td>

<font face="Arial" size="2">
<b>The path you entered is incorrect.</b> You entered : "$dir"
<p>
Your root path is (UNIX): $ENV{DOCUMENT_ROOT}
<p>
Your root path is (WINDOWS): $ENV{PATH_TRANSLATED}
<p>
Your path should contain your root path followed by a slash followed by the
destination folder's name. If you are on a WINDOWS server, each slash should
be escaped. Eg. each seperator should look like this : \\\\
<p>
Sometimes, the root returned is not the full path to your web space. In this case
you should either check with your host  or if you are using an FTP client such as
CuteFTP, change to the folder you are trying to upload to and look at the path you
have taken. You can see this just above the list of files on your server.
You must use the same path in the \$dir variable.
</font>

</td>
</tr>
</table>
~;

&PrintFoot; # print the footer
exit;
}

use CGI; # load the CGI.pm module
my $GET = new CGI; # create a new object
my @VAL = $GET->param; #get all form field names

foreach(@VAL){
$FORM{$_} = $GET->param($_); # put all fields and values in hash
}

my @files;
foreach(keys %FORM){
if($_ =~ /^FILE/){
push(@files, $_); # place the field NAME in an array
}
}

if(!$VAL[0]){
# no form fields
&PrintHead; #print the header

print qq~
<table width="760">
<tr>
<td>

<font face="Arial" size="2">
This script must be called using a form. Your form should point to this script. Your form tag must contain the following attributes :
<p>
&lt;form <font color="#FF0000">action</font>="$ScriptURL" <font color="#FF0000">method</font>="post" <font color="#FF0000">enctype</font>="multipart/form-data">
<p>
The <font color="#FF0000">method</font> must equal <font color="#FF0000">post</font> and the <font color="#FF0000">enctype</font> must equal <font color="#FF0000">multipart/form-data</font>. The <font color="#FF0000">action</font> has to point to this script (on your server). If you are reading this, copy and paste the example above. It has the correct values.
</font>

</td>
</tr>
</table>
~;

&PrintFoot; # print the footer
exit;
}

# check domain
if($domain =~ /\w+/){
if($ENV{HTTP_REFERER} !~ /$domain/i){
&PrintHead; #print the header

print qq~
<table width="600">
<tr>
<td>

<font face="Arial" size="2">
Invalid referrer.
</font>

</td>
</tr>
</table>
~;

&PrintFoot; # print the footer
exit;
}
}

my $failed; # results string = false
my $selected; # num of files selected by user

####################################################################

####################################################################

foreach (@files){
# upload each file, pass the form field NAME if it has a value
if($GET->param($_)){

# if the form field contains a file name &psjs_upload subroutine
# the file's name and path are passed to the subroutine
$returned = &psjs_upload($_);

if($returned =~ /^Success/i){
# if the $returned message begins with "Success" the upload was succssful
# remove the word "Success" and any spaces and we're left with the filename  
$returned =~ s/^Success\s+//;
push(@success, $returned);
} else {
# else if the word "success" is not returned, the message is the error encountered.
# add the error to the $failed scalar
$failed .= $returned;
}
$selected++; # increment num of files selected for uploading by user
}
}

if(!$selected){
# no files were selected by user, so nothing is returned to either variable
$failed .= qq~No files were selected for uploading~;
}

# if no error message is return ed, the upload was successful

my ($fNames, $aa, $bb, @current, @currentfiles );

if($failed){

&PrintHead;

print qq~
<table align="center" width="600">
<tr>
<td><font face="Arial" size="2">

One or more files <font color="#ff0000">failed</font> to upload. The reasons returned are:
<p>

$failed
~;

if($success[0]){
# send email if valid email was entered
if(check_email($notify)){

# enter the message you would like to receive
my $message = qq~
The following files were uploaded to your server :
~;

$folder =~ s/(\/|\\)$//ig;
foreach(@success){
$message .= qq~
$folder/$_
~;
}

if($isUNIX){
$CONFIG{mailprogram} = $send_mail_path;
# enter your e-mail name here if you like
# from e-mail, from name, to e-mail, to name, subject, body
&send_mail($notify, 'Demo Upload', $notify, 'Demo Upload', 'Upload Notification', $message);

} else {
$CONFIG{smtppath} = $smtp_path;
&send_mail_NT($notify, 'Your Name', $notify, 'Your Name', 'Upload Notification', $message);
}
}

print qq~
<p>
The following files were <font color="#ff0000">successfully</font> uploaded :
<p>
~;
foreach(@success){
print qq~
$_<p>~;
}
}

print qq~
</font></td>
</tr>
</table>
~;

&PrintFoot;

} else {
# upload was successful

# add a link to the file
$folder =~ s/(\/|\\)$//ig;

# send email if valid email was entered
if(check_email($notify)){

# enter the message you would like to receive
my $message = qq~
The following files were uploaded to your server :
~;

foreach(@success){
$message .= qq~
$folder/$_
~;
}

if($isUNIX){
$CONFIG{mailprogram} = $send_mail_path;
# enter your e-mail name here if you like
# from e-mail, from name, to e-mail, to name, subject, body
&send_mail($notify, 'Demo Upload', $notify, 'Demo Upload', 'Upload Notification', $message);

} else {
$CONFIG{smtppath} = $smtp_path;
&send_mail_NT($notify, 'Your Name', $notify, 'Your Name', 'Upload Notification', $message);
}
}

if($redirect){
# redirect user
print qq~Location: $redirect\n\n~;
} else {
# print success page

&PrintHead;

print qq~
<table align="center" width="500">
<tr>
<th><font face="Arial" size="2"><font color="#ff0000">Success</font></font></th>
</tr>
<tr>
<td><font face="Arial" size="2">The following files were successfully uploaded :
<p>
~;

foreach(@success){
print qq~
$_<p>~;
}

print qq~
</font></td>
</tr>
</table>
<br>
~;

if($print_contents){
print qq~
<table align="center" width="500">
<tr><td><font face="Arial" size="2"><b>Current files in folder</b></font></td></tr>
<tr>
<td valign="top">
<font face="Arial" size="2">
~;

opendir(DIR, "$dir");
@current = readdir(DIR);
closedir(DIR);

foreach(@current){
unless($_ eq '.' || $_ eq '..'){
push(@currentfiles, $_);
}
}

@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;

for($aa = 0; $aa <= int($#currentfiles / 2); $aa++){
print qq~
<font color="#ff0000"><b></b>
<a href="$folder/$currentfiles[$aa]" target="_blank">$currentfiles[$aa]</a></font><br>
~;
}

print qq~</font></td><td valign="top"><font face="Arial" size="2">~;

for($bb = $aa; $bb < @currentfiles; $bb++){
print qq~
<font color="#ff0000"><b></b>
<a href="$folder/$currentfiles[$bb]" target="_blank">$currentfiles[$bb]</a></font><br>
~;
}


print qq~
</font></td>
</tr>
</table>~;
}

print qq~
<br>
<center><font face="Arial" size="2">
<a href="http://www.perlscriptsjavascripts.com/?ul">&copy; PerlScriptsJavaScripts.com</a>
&nbsp; &nbsp;
<a href="http://www.perlscriptsjavascripts.com/psjs_faqs/index.html?ul">F.A.Q.</a>
&nbsp; &nbsp;
<a href="http://www.perlscriptsjavascripts.com/perl/upload_lite/users_guide.html?ul">Users Guide</a>
</font></center>
~;

&PrintFoot;

}
}

####################################################################

####################################################################

sub psjs_upload {

my ( $type_ok, $file_contents, $buffer, $destination ); # declare some vars

my $file = $GET->param($_[0]); # get the FILE name. $_[0] is the arg passed

$destination = $dir;

my $limit = $max;
$limit *= 1024; # convert limit from bytes to kilobytes

# create another instance of the $file var. This will allow the script to play
# with the new instance, without effecting the first instance. This was a major
# flaw I found in the psupload script. The author was replacing spaces in the path
# with underscores, so the script could not find a file to upload. He blammed the
# error on browser problems.
my $fileName    = $file;

# get the extension
my @file_type   = split(/\./, $fileName);
# we can assume everything after the last . found is the extension
my $file_type   = $file_type[$#file_type];

# get the file name, this removes everything up to and including the
# last slash found ( be it a forward or back slash )
$fileName =~ s/^.*(\\|\/)//;

# remove all spaces from new instance of filename var
$fileName =~ s/\s+//ig;

# check for any any non alpha numeric characters in filename (allow dots and dahses)
$fileName =~ s/\./PsJsDoT/g;
$fileName =~ s/\-/PsJsDaSh/g;
if($fileName =~ /\W/){
$fileName =~ s/\W/n/ig; # replace any bad chars with the letter "n"
}
$fileName =~ s/PsJsDoT/\./g;
$fileName =~ s/PsJsDaSh/\-/g;

# if $file_type matchs one of the types specified, make the $type_ok var true
for($b = 0; $b < @types; $b++){
if($file_type =~ /^$types[$b]$/i){
$type_ok++;
}
if($types[$b] eq "ALL"){
$type_ok++; # if ALL keyword is found, increment $type_ok var.
}
}

# if ok, check if overwrite is allowed
if($type_ok){
if(!$overwrite){ # if $overwite = 0 or flase, rename file using the checkex sub
$fileName = check_existence($destination,$fileName);
}
# create a new file on the server using the formatted ( new instance ) filename
if(open(NEW, ">$destination$S{S}$fileName")){
if($isWIN){binmode NEW;} # if it's a WIN server, switch to binary mode
# start reading users HD 1 kb at a time.
while (read($file, $buffer, 1024)){
# print each kb to the new file on the server
print NEW $buffer;
}
# close the new file on the server and we're done
close NEW;
} else {
# return the server's error message if the new file could not be created
return qq~Error: Could not open new file on server. $!~;
}

# check limit hasn't just been overshot
if(-s "$destination$S{S}$fileName" > $limit){ # -s is the file size
unlink("$destination$S{S}$fileName"); # delete it if it's over the specified limit
return qq~File exceeded limitations : $fileName~;
}
} else {
return qq~Bad file type : $file_type~;
}

# check if file has actually been uploaded, by checking the file has a size
if(-s "$destination$S{S}$fileName"){
return qq~Success $fileName~; #success
} else {
# delete the file as it has no content
unlink("$destination$S{S}$fileName");
# user probably entered an incorrect path to file
return qq~Upload failed : No data in $fileName. No size on server's copy of file.
Check the path entered.~;
}
}

####################################################################

####################################################################

sub check_existence {
# $dir,$filename,$newnum are the args passed to this sub
my ($dir,$filename,$newnum) = @_;

my (@file_type, $file_type, $exists, $bareName);
# declare some vars we will use later on in this sub always use paranthesis
# when declaring more than one var! Some novice programmers will tell you
# this is not necessary. Tell them to learn how to program.

if(!$newnum){$newnum = "0";} # new num is empty in first call, so set it to 0

# read dir and put all files in an array (list)
opendir(DIR, "$dir");
@existing_files =  readdir(DIR);
closedir(DIR);

# if the filename passed exists, set $exists to true or 1
foreach(@existing_files){
if($_ eq $filename){
$exists = 1;
}
}

# if it exists, we need to rename the file being uploaded and then recheck it to
# make sure the new name does not exist
if($exists){
$newnum++; # increment new number (add 1)

# get the extension
@file_type   = split(/\./, $filename); # split the dots and add inbetweens to a list
# put the first element in the $barename var
$bareName    = $file_type[0];
# we can assume everything after the last . found is the extension
$file_type   = $file_type[$#file_type];
# $#file_type is the last element (note the pound or hash is used)

# remove all numbers from the end of the $bareName
$bareName =~ s/\d+$//ig;

# concatenate a new name using the barename + newnum + extension
$filename = $bareName . $newnum . '.' . $file_type;

# reset $exists to 0 because the new file name is now being checked
$exists = 0;

# recall this subroutine
&check_existence($dir,$filename,$newnum);
} else {
# the $filename, whether the first or one hundreth call, now does not exist
# so return the name to be used
return ($filename);
}
}

####################################################################

####################################################################

sub send_mail {
my ($from_email, $from_name, $to_email, $to_name, $subject, $message ) = @_;

if(open(MAIL, "|$CONFIG{mailprogram} -t")) {
print MAIL "From: $from_email ($from_name)\n";
print MAIL "To: $to_email ($to_name)\n";
print MAIL "Subject: $subject\n";
print MAIL "$message\n\nSubmitter's IP Address : $ENV{REMOTE_ADDR}";
close MAIL;
return(1);
} else {
return;
}
}

####################################################################

####################################################################

sub send_mail_NT {

my ($from_email, $from_name, $to_email, $to_name, $subject, $message ) = @_;

my ($SMTP_SERVER, $WEB_SERVER, $status, $err_message);
use Socket;
    $SMTP_SERVER = "$CONFIG{smtppath}";                                

# correct format for "\n"
    local($CRLF) = "\015\012";
    local($SMTP_SERVER_PORT) = 25;
    local($AF_INET) = ($] > 5 ? AF_INET : 2);
    local($SOCK_STREAM) = ($] > 5 ? SOCK_STREAM : 1);
    local(@bad_addresses) = ();
    $, = ', ';
    $" = ', ';

    $WEB_SERVER = "$CONFIG{smtppath}\n";
    chop ($WEB_SERVER);

    local($local_address) = (gethostbyname($WEB_SERVER))[4];
    local($local_socket_address) = pack('S n a4 x8', $AF_INET, 0, $local_address);

    local($server_address) = (gethostbyname($SMTP_SERVER))[4];
    local($server_socket_address) = pack('S n a4 x8', $AF_INET, $SMTP_SERVER_PORT, $server_address);

    # Translate protocol name to corresponding number
    local($protocol) = (getprotobyname('tcp'))[2];

    # Make the socket filehandle
    if (!socket(SMTP, $AF_INET, $SOCK_STREAM, $protocol)) {
        return;
    }

# Give the socket an address
bind(SMTP, $local_socket_address);

# Connect to the server
if (!(connect(SMTP, $server_socket_address))) {
return;
}

# Set the socket to be line buffered
local($old_selected) = select(SMTP);
$| = 1;
select($old_selected);

# Set regex to handle multiple line strings
$* = 1;

    # Read first response from server (wait for .75 seconds first)
    select(undef, undef, undef, .75);
    sysread(SMTP, $_, 1024);
#print "<P>1:$_";

    print SMTP "HELO $WEB_SERVER$CRLF";
    sysread(SMTP, $_, 1024);
#print "<P>2:$_";

while (/(^|(\r?\n))[^0-9]*((\d\d\d).*)$/g) { $status = $4; $err_message = $3}
if ($status != 250) {
return;
}

print SMTP "MAIL FROM:<$from_email>$CRLF";

sysread(SMTP, $_, 1024);
#print "<P>3:$_";
if (!/[^0-9]*250/) {
return;
}

    # Tell the server where we're sending to
print SMTP "RCPT TO:<$to_email>$CRLF";
sysread(SMTP, $_, 1024);
#print "<P>4:$_";
/[^0-9]*(\d\d\d)/;

# Give the server the message header
print SMTP "DATA$CRLF";
sysread(SMTP, $_, 1024);
#print "<P>5:$_";
if (!/[^0-9]*354/) {
return;
}

$message =~ s/\n/$CRLF/ig;

print SMTP qq~From: $from_email ($from_name)$CRLF~;
print SMTP qq~To: $to_email ($to_name)$CRLF~;
if($cc){
print SMTP "CC: $cc ($cc_name)\n";
}
print SMTP qq~Subject: $subject$CRLF$CRLF~;
print SMTP qq~$message~;

print SMTP "$CRLF.$CRLF";
sysread(SMTP, $_, 1024);
#print "<P>6:$_";
if (!/[^0-9]*250/) {
return;
} else {
return(1);
}

if (!shutdown(SMTP, 2)) {
return;
    }
}

####################################################################

####################################################################

sub PrintHead {
print qq~Content-type: text/html\n\n~;
print qq~

<title>PerlScriptsJavascript.com Free upload utility</title>

~;
}

####################################################################

####################################################################

sub PrintFoot {
print qq~

~;
}

####################################################################

####################################################################

sub check_email {
my($fe_email) = $_[0];
if($fe_email) {
if(($fe_email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/) ||
($fe_email !~ /^.+@\[?(\w|[-.])+\.[a-zA-Z]{2,3}|[0-9]{1,3}\]?$/)) {
return;
} else { return(1) }
} else {
return;
}
}
</body>
</html>
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 05:00:46 PM
Also on the error log is this message (with the path to upload.cgi):

Premature end of script headers
Title: CGI upload script
Post by: TranzNDance on July 23, 2004, 05:06:09 PM
Quote from: fo0hzy
The uploads folder is in the public_html directory, and both that & the cgi-bin folder are chmodded 777 (which is what the script instructions call for... however, when I check the cpanel error log, it says:"error: directory is writable by others" so maybe there is a problem with the script itself. I will paste the entire script at the bottom of this reply.

Ah, that's the problem. The uploads folder should be chmod 755.
Title: CGI upload script
Post by: leighsww on July 23, 2004, 05:09:41 PM
Okay, the first thing in your script that I see a problem with is:

Code: [Select]
<html><body bgcolor="#ffffff">#!/usr/bin/perl

Remove everything prior to #!

This is the next thing:

Code: [Select]
</body>
   </html>


Remove that from the bottom of your code.

Quote from: fo0hzy
The uploads folder is in the public_html directory, and both that & the cgi-bin folder are chmodded 777 (which is what the script instructions call for... however, when I check the cpanel error log, it says:"error: directory is writable by others" so maybe there is a problem with the script itself.


chmod should be 755 for the cgi-bin. The "uploads" folder leave to 777, cuz it needs to be writable. Hmmm, I don't know why the instructions says to chmod the cgi-bin to 777, cuz that's not correct. Also, the user guide says something different than the script's instructions it seems --> http://www.perlscriptsjavascripts.com/perl/upload_lite/users_guide.html#inst

Quote from: fo0hzy
I didn't enter the mail server info because I wasn't intending to make use of that function. However, if you think it is advisable to do so, I'll reconsider.


You will need to enter things or leave them blank or put that # in front, cuz that's another reason you would be getting errors.

If you don't want to be notified about uploads, then delete the "you@yourdomain.com" and leave it blank but leave the quotations.  Then do the same for the "send_mail_path", but put a # in front of "smtp_path" (see my above post for how it should look for the smtp_path).

Try doing those changes and post if still errors occur.
Title: CGI upload script
Post by: leighsww on July 23, 2004, 05:10:55 PM
Quote from: TranzNDance
Ah, that's the problem. The uploads folder should be chmod 755.


Thu, his cgi-bin needs to be 755, but his "uploads" folder would need to be 777 cuz that folder needs to be written to.
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 05:19:25 PM
woohoo!! That did it, sorta!!

Quote
Success
The following files were successfully uploaded :
smileyburnout.gif

 

Current files in folder
smileyburnout.gif
 


However, clicking the link to the image give me a 'page cannot be found error', and the path is incorrect:

http://www.dangerouslystupid.lunarpages.com/cgi-bin/www.dangerouslystupid.lunarpages.com/uploads/spaceDog.gif
Title: CGI upload script
Post by: leighsww on July 23, 2004, 05:21:22 PM
Oops, made a mistake on the "send_mail_path".  You need to leave that in (DON'T blank it). Sorry, I didn't look good at what that said and all this instructions is making my eyes go batty LOL :o  . It should say:

Code: [Select]
$send_mail_path = "/usr/sbin/sendmail";

Also, one thing to consider about the "notifications" is whether it's important for you to know if people are uploading. I would think it would be.  Thus, it would be good to put your email in the $notify.

I still need the answer regarding if you have a sub-directory folder called dangerouslystupid in your public_html? We still need to establish if your $dir path is correct.
Title: CGI upload script
Post by: TranzNDance on July 23, 2004, 05:22:26 PM
Quote from: leighsww
Quote from: TranzNDance
Ah, that's the problem. The uploads folder should be chmod 755.


Thu, his cgi-bin needs to be 755, but his "uploads" folder would need to be 777 cuz that folder needs to be written to.

I'm using an upload script that uploads to a folder that is 755. :?
Title: CGI upload script
Post by: leighsww on July 23, 2004, 05:23:54 PM
Quote from: fo0hzy
However, clicking the link to the image give me a 'page cannot be found error', and the path is incorrect:

http://www.dangerouslystupid.lunarpages.com/cgi-bin/www.dangerouslystupid.lunarpages.com/uploads/spaceDog.gif


Okay, it looks like that error has the sub_domain URL after the cgi-bin directory, so when you give me the info about where that sub-domain folder is, we'll figure out the $dir path
Title: CGI upload script
Post by: leighsww on July 23, 2004, 05:25:38 PM
Quote from: TranzNDance
I'm using an upload script that uploads to a folder that is 755. :?


Okay, GREAT, he could have left it then. I didn't know that  :)
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 05:26:36 PM
Hmm... I'm not even sure what you mean? The path to cgi is /public_html/cgi-bin/ :-?

I only have a subdomain for another day or so, and the DNS stuff should kick in for my domain...
Title: CGI upload script
Post by: leighsww on July 23, 2004, 05:33:12 PM
Oh, then you will have to change the script configs once your domain kicks in cuz the sub-domain path would not be applicable.

Okay, let me see where this error is in the path:
Quote
The requested URL /cgi-bin/www.dangerouslystupid.lunarpages.com/uploads/spaceDog.gif was not found on this server.


Point me to the URL to your webpage where the form is at, as well.
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 05:35:15 PM
http://www.dangerouslystupid.lunarpages.com/upload.html


If you paste this part into your browser, you should be able to see the image (I can): www.dangerouslystupid.lunarpages.com/uploads/spaceDog.gif
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 05:37:07 PM
Quote from: leighsww
Quote from: TranzNDance
I'm using an upload script that uploads to a folder that is 755. :?


Okay, GREAT, he could have left it then. I didn't know that  :)


LOL I'll wait until it's working before I try setting it to 755 :wink:
Title: CGI upload script
Post by: leighsww on July 23, 2004, 05:38:24 PM
Yes, I can see the image.  Was that what you just uploaded via your form?

Okay, let's do a relative URL instead of an absolute URL for this line, so change this:

Code: [Select]
<form action="http://www.dangerouslystupid.lunarpages.com/cgi-bin/upload.cgi" method="post" enctype="multipart/form-data">

to this:

Code: [Select]
<form action="/cgi-bin/upload.cgi" method="post" enctype="multipart/form-data">

And see if we still get that error.
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 05:44:51 PM
Yep, I've uploaded a few images already, testing it out

Okay, did that and still get 'page not found'... but at least it still works. Maybe I should go ahead and chmod the uploads folder to 755?
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 05:45:30 PM
Oh, and the link's path is:

http://www.dangerouslystupid.lunarpages.com/cgi-bin/www.dangerouslystupid.lunarpages.com/uploads/sexyredx.jpg
Title: CGI upload script
Post by: TranzNDance on July 23, 2004, 05:49:37 PM
Quote from: fo0hzy
Maybe I should go ahead and chmod the uploads folder to 755?

It would be more secure.
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 05:54:45 PM
Okay done :D
Title: CGI upload script
Post by: leighsww on July 23, 2004, 05:55:11 PM
Quote from: fo0hzy
Yep, I've uploaded a few images already, testing it out

Okay, did that and still get 'page not found'... but at least it still works. Maybe I should go ahead and chmod the uploads folder to 755?


Yes, you can go ahead and try the chmod.

Now to find the problem for this error.  Hmmm, it might be due to the sub-domain.  I'm thinking that when your domain takes effect it'll probably work just fine. Maybe someone can spot it where I can't right now.

When your domain takes effect, remember to make the necessary changes to the $folder and  $domain configs in your script when it does.

When it does take effect, these will need to be:

Code: [Select]
$folder = "http://www.dangerouslystupid.com/uploads/";
$domain = "www.dangerouslystupid.com";
Title: CGI upload script
Post by: leighsww on July 23, 2004, 05:56:40 PM
Quote from: fo0hzy
Okay done :D


LOL, looks like 2 posts got put up before I could finish my post to you.  :)

Thu is always on the ball!  :thumb:
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 05:57:49 PM
In fact, I just got notice that the domain has resolved to the lunar pages servers... waiting for support to switch it over for me right now :thumb:

I chmodded the uploads folder to 755 and it still works fine.
Title: CGI upload script
Post by: leighsww on July 23, 2004, 06:00:22 PM
GREAT!  :yey:
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 06:01:57 PM
Thank you both for your help... you guys rock! :love:

I'll be back if something else goes wrong, but I hope not :thumb:
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 07:09:43 PM
Okay, I got the SQL settings changed after the domain resolved, and edited the upload.cgi script as you said to, and still the picture links are screwy (I even tried removing my username from the $dir path, the script told me that was a no-no):

http://www.dangerouslystupid.com/cgi-bin/www.dangerouslystupid.com/uploads/stfu3.jpg

Here is the current code:
Code: [Select]
#!/usr/bin/perl
# Installation Instructions
# http://www.perlscriptsjavascripts.com/perl/upload_lite/users_guide.html

# To order a custom install, please visit our "Secure order" page
# and enter the standard installation fee in the "Custom Quote" field

####################################################################
#
# Upload Lite.
# 2002, PerlscriptsJavaScripts.com
#
# Requirements: Perl5 WINDOWS NT or UNIX
# Created: Febuary , 2001
# Author: John Krinelos
# Version: 3.23
#
# Based on Upload Gold, first release : September 2001
#
# This script is free, as long as this header and any copyright messages
# remains in tact. To remove copyright messages from public web pages you
# must purchase copyright removal.
# http://www.perlscriptsjavascripts.com/copyright_fees.html
#
# Agent for copyright :
# Gene Volovich
# Law Partners,
# 140 Queen St.
# Melbourne
# Ph. +61 3 9602 2266
# gvolovich@lawpartners.com.au
# http://www.lawpartners.com.au/
#
####################################################################

# START USER EDITS

# absolute path to folder files will be uploaded to.
# WINDOWS users, your path would like something like : images\\uploads
# UNIX    users, your path would like something like : /home/www/images/uploads
# do not end the path with any slashes and if you're on a UNIX serv, make sure
# you CHMOD each folder in the path to 777

$dir = "/home/danger7/public_html"; ;  
#$dir = "d:\\html\\users\\html\\images";

# absolute URL to folder files will be uploaded to
$folder = "www.dangerouslystupid.com/uploads";

# maximum file size allowed (kilo bytes)
$max = 135;

# for security reasons, enter your domain name.
# this is so uploads may only occur from your domain
# enter any part of your domain name, or leave this
# blank if you don't mind other web sites using your copy
$domain = "dangerouslystupid";

# if a file is successfully uploaded, enter a URL to redirect to.
# leave this blank to have the default message printed. If using
# this var, it must begin with http
$redirect = "";

# if you would like to be notified of uploads, enter your email address
# between the SINGLE quotes. leave this blank if you would not like to be notified
$notify = '';

# UNIX users, if you entered a value for $notify, you must also enter your
# server's sendmail path. It usually looks something like : /usr/sbin/sendmail
$send_mail_path = "";

# WINDOWS users, if you entered a value for $notify, you must also enter your
# server's SMTP path. It usually looks something like : mail.servername.com
#$smtp_path = "mail.yourserver.com";

# set to 1 if you would like all files in the directory printed to the web page
# after a successful upload (only printed if redirect is off). Set to 0 if you
# do not want filenames printed to web page
$print_contents = 1;

# allow overwrites? 1 = yes, 0 = no (0 will rename file with a number on the end, the
# highest number is the latest file)
$overwrite = 0;

# file types allowed, enter each type on a new line
# Enter the word "ALL" in uppercase, to accept all file types.
@types = qw~

txt
jpeg
jpg
gif
swf

~;

####################################################################
#    END USER EDITS
####################################################################

$folder =~ s/(\/|\\)$//ig;

$OS = $^O; # operating system name
if($OS =~ /darwin/i) { $isUNIX = 1; }
elsif($OS =~ /win/i) { $isWIN = 1; }
else {$isUNIX = 1;}

if($isWIN){ $S{S} = "\\\\"; }
else { $S{S} = "/";} # seperator used in paths

$ScriptURL = "http://$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}";

unless (-d "$dir"){
mkdir ("$dir", 0777); # unless the dir exists, make it ( and chmod it on UNIX )
chmod(0777, "$dir");
}

unless (-d "$dir"){
# if there still is no dir, the path entered by the user is wrong and the upload will fail
&PrintHead; #print the header

# get the Win root
$ENV{PATH_INFO} =~ s/\//$S{S}/gi;
$ENV{PATH_TRANSLATED} =~ s/$ENV{PATH_INFO}//i;

print qq~
<table width="600">
<tr>
<td>

<font face="Arial" size="2">
<b>The path you entered is incorrect.</b> You entered : "$dir"
<p>
Your root path is (UNIX): $ENV{DOCUMENT_ROOT}
<p>
Your root path is (WINDOWS): $ENV{PATH_TRANSLATED}
<p>
Your path should contain your root path followed by a slash followed by the
destination folder's name. If you are on a WINDOWS server, each slash should
be escaped. Eg. each seperator should look like this : \\\\
<p>
Sometimes, the root returned is not the full path to your web space. In this case
you should either check with your host  or if you are using an FTP client such as
CuteFTP, change to the folder you are trying to upload to and look at the path you
have taken. You can see this just above the list of files on your server.
You must use the same path in the \$dir variable.
</font>

</td>
</tr>
</table>
~;

&PrintFoot; # print the footer
exit;
}

use CGI; # load the CGI.pm module
my $GET = new CGI; # create a new object
my @VAL = $GET->param; #get all form field names

foreach(@VAL){
$FORM{$_} = $GET->param($_); # put all fields and values in hash
}

my @files;
foreach(keys %FORM){
if($_ =~ /^FILE/){
push(@files, $_); # place the field NAME in an array
}
}

if(!$VAL[0]){
# no form fields
&PrintHead; #print the header

print qq~
<table width="760">
<tr>
<td>

<font face="Arial" size="2">
This script must be called using a form. Your form should point to this script. Your form tag must contain the following attributes :
<p>
&lt;form <font color="#FF0000">action</font>="$ScriptURL" <font color="#FF0000">method</font>="post" <font color="#FF0000">enctype</font>="multipart/form-data">
<p>
The <font color="#FF0000">method</font> must equal <font color="#FF0000">post</font> and the <font color="#FF0000">enctype</font> must equal <font color="#FF0000">multipart/form-data</font>. The <font color="#FF0000">action</font> has to point to this script (on your server). If you are reading this, copy and paste the example above. It has the correct values.
</font>

</td>
</tr>
</table>
~;

&PrintFoot; # print the footer
exit;
}

# check domain
if($domain =~ /\w+/){
if($ENV{HTTP_REFERER} !~ /$domain/i){
&PrintHead; #print the header

print qq~
<table width="600">
<tr>
<td>

<font face="Arial" size="2">
Invalid referrer.
</font>

</td>
</tr>
</table>
~;

&PrintFoot; # print the footer
exit;
}
}

my $failed; # results string = false
my $selected; # num of files selected by user

####################################################################

####################################################################

foreach (@files){
# upload each file, pass the form field NAME if it has a value
if($GET->param($_)){

# if the form field contains a file name &psjs_upload subroutine
# the file's name and path are passed to the subroutine
$returned = &psjs_upload($_);

if($returned =~ /^Success/i){
# if the $returned message begins with "Success" the upload was succssful
# remove the word "Success" and any spaces and we're left with the filename  
$returned =~ s/^Success\s+//;
push(@success, $returned);
} else {
# else if the word "success" is not returned, the message is the error encountered.
# add the error to the $failed scalar
$failed .= $returned;
}
$selected++; # increment num of files selected for uploading by user
}
}

if(!$selected){
# no files were selected by user, so nothing is returned to either variable
$failed .= qq~No files were selected for uploading~;
}

# if no error message is return ed, the upload was successful

my ($fNames, $aa, $bb, @current, @currentfiles );

if($failed){

&PrintHead;

print qq~
<table align="center" width="600">
<tr>
<td><font face="Arial" size="2">

One or more files <font color="#ff0000">failed</font> to upload. The reasons returned are:
<p>

$failed
~;

if($success[0]){
# send email if valid email was entered
if(check_email($notify)){

# enter the message you would like to receive
my $message = qq~
The following files were uploaded to your server :
~;

$folder =~ s/(\/|\\)$//ig;
foreach(@success){
$message .= qq~
$folder/$_
~;
}

if($isUNIX){
$CONFIG{mailprogram} = $send_mail_path;
# enter your e-mail name here if you like
# from e-mail, from name, to e-mail, to name, subject, body
&send_mail($notify, 'Demo Upload', $notify, 'Demo Upload', 'Upload Notification', $message);

} else {
$CONFIG{smtppath} = $smtp_path;
&send_mail_NT($notify, 'Your Name', $notify, 'Your Name', 'Upload Notification', $message);
}
}

print qq~
<p>
The following files were <font color="#ff0000">successfully</font> uploaded :
<p>
~;
foreach(@success){
print qq~
$_<p>~;
}
}

print qq~
</font></td>
</tr>
</table>
~;

&PrintFoot;

} else {
# upload was successful

# add a link to the file
$folder =~ s/(\/|\\)$//ig;

# send email if valid email was entered
if(check_email($notify)){

# enter the message you would like to receive
my $message = qq~
The following files were uploaded to your server :
~;

foreach(@success){
$message .= qq~
$folder/$_
~;
}

if($isUNIX){
$CONFIG{mailprogram} = $send_mail_path;
# enter your e-mail name here if you like
# from e-mail, from name, to e-mail, to name, subject, body
&send_mail($notify, 'Demo Upload', $notify, 'Demo Upload', 'Upload Notification', $message);

} else {
$CONFIG{smtppath} = $smtp_path;
&send_mail_NT($notify, 'Your Name', $notify, 'Your Name', 'Upload Notification', $message);
}
}

if($redirect){
# redirect user
print qq~Location: $redirect\n\n~;
} else {
# print success page

&PrintHead;

print qq~
<table align="center" width="500">
<tr>
<th><font face="Arial" size="2"><font color="#ff0000">Success</font></font></th>
</tr>
<tr>
<td><font face="Arial" size="2">The following files were successfully uploaded :
<p>
~;

foreach(@success){
print qq~
$_<p>~;
}

print qq~
</font></td>
</tr>
</table>
<br>
~;

if($print_contents){
print qq~
<table align="center" width="500">
<tr><td><font face="Arial" size="2"><b>Current files in folder</b></font></td></tr>
<tr>
<td valign="top">
<font face="Arial" size="2">
~;

opendir(DIR, "$dir");
@current = readdir(DIR);
closedir(DIR);

foreach(@current){
unless($_ eq '.' || $_ eq '..'){
push(@currentfiles, $_);
}
}

@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;

for($aa = 0; $aa <= int($#currentfiles / 2); $aa++){
print qq~
<font color="#ff0000"><b>•</b>
<a href="$folder/$currentfiles[$aa]" target="_blank">$currentfiles[$aa]</a></font><br>
~;
}

print qq~</font></td><td valign="top"><font face="Arial" size="2">~;

for($bb = $aa; $bb < @currentfiles; $bb++){
print qq~
<font color="#ff0000"><b>•</b>
<a href="$folder/$currentfiles[$bb]" target="_blank">$currentfiles[$bb]</a></font><br>
~;
}


print qq~
</font></td>
</tr>
</table>~;
}

print qq~
<br>
<center><font face="Arial" size="2">
<a href="http://www.perlscriptsjavascripts.com/?ul">&copy; PerlScriptsJavaScripts.com</a>
&nbsp; &nbsp;
<a href="http://www.perlscriptsjavascripts.com/psjs_faqs/index.html?ul">F.A.Q.</a>
&nbsp; &nbsp;
<a href="http://www.perlscriptsjavascripts.com/perl/upload_lite/users_guide.html?ul">Users Guide</a>
</font></center>
~;

&PrintFoot;

}
}

####################################################################

####################################################################

sub psjs_upload {

my ( $type_ok, $file_contents, $buffer, $destination ); # declare some vars

my $file = $GET->param($_[0]); # get the FILE name. $_[0] is the arg passed

$destination = $dir;

my $limit = $max;
$limit *= 1024; # convert limit from bytes to kilobytes

# create another instance of the $file var. This will allow the script to play
# with the new instance, without effecting the first instance. This was a major
# flaw I found in the psupload script. The author was replacing spaces in the path
# with underscores, so the script could not find a file to upload. He blammed the
# error on browser problems.
my $fileName    = $file;

# get the extension
my @file_type   = split(/\./, $fileName);
# we can assume everything after the last . found is the extension
my $file_type   = $file_type[$#file_type];

# get the file name, this removes everything up to and including the
# last slash found ( be it a forward or back slash )
$fileName =~ s/^.*(\\|\/)//;

# remove all spaces from new instance of filename var
$fileName =~ s/\s+//ig;

# check for any any non alpha numeric characters in filename (allow dots and dahses)
$fileName =~ s/\./PsJsDoT/g;
$fileName =~ s/\-/PsJsDaSh/g;
if($fileName =~ /\W/){
$fileName =~ s/\W/n/ig; # replace any bad chars with the letter "n"
}
$fileName =~ s/PsJsDoT/\./g;
$fileName =~ s/PsJsDaSh/\-/g;

# if $file_type matchs one of the types specified, make the $type_ok var true
for($b = 0; $b < @types; $b++){
if($file_type =~ /^$types[$b]$/i){
$type_ok++;
}
if($types[$b] eq "ALL"){
$type_ok++; # if ALL keyword is found, increment $type_ok var.
}
}

# if ok, check if overwrite is allowed
if($type_ok){
if(!$overwrite){ # if $overwite = 0 or flase, rename file using the checkex sub
$fileName = check_existence($destination,$fileName);
}
# create a new file on the server using the formatted ( new instance ) filename
if(open(NEW, ">$destination$S{S}$fileName")){
if($isWIN){binmode NEW;} # if it's a WIN server, switch to binary mode
# start reading users HD 1 kb at a time.
while (read($file, $buffer, 1024)){
# print each kb to the new file on the server
print NEW $buffer;
}
# close the new file on the server and we're done
close NEW;
} else {
# return the server's error message if the new file could not be created
return qq~Error: Could not open new file on server. $!~;
}

# check limit hasn't just been overshot
if(-s "$destination$S{S}$fileName" > $limit){ # -s is the file size
unlink("$destination$S{S}$fileName"); # delete it if it's over the specified limit
return qq~File exceeded limitations : $fileName~;
}
} else {
return qq~Bad file type : $file_type~;
}

# check if file has actually been uploaded, by checking the file has a size
if(-s "$destination$S{S}$fileName"){
return qq~Success $fileName~; #success
} else {
# delete the file as it has no content
unlink("$destination$S{S}$fileName");
# user probably entered an incorrect path to file
return qq~Upload failed : No data in $fileName. No size on server's copy of file.
Check the path entered.~;
}
}

####################################################################

####################################################################

sub check_existence {
# $dir,$filename,$newnum are the args passed to this sub
my ($dir,$filename,$newnum) = @_;

my (@file_type, $file_type, $exists, $bareName);
# declare some vars we will use later on in this sub always use paranthesis
# when declaring more than one var! Some novice programmers will tell you
# this is not necessary. Tell them to learn how to program.

if(!$newnum){$newnum = "0";} # new num is empty in first call, so set it to 0

# read dir and put all files in an array (list)
opendir(DIR, "$dir");
@existing_files =  readdir(DIR);
closedir(DIR);

# if the filename passed exists, set $exists to true or 1
foreach(@existing_files){
if($_ eq $filename){
$exists = 1;
}
}

# if it exists, we need to rename the file being uploaded and then recheck it to
# make sure the new name does not exist
if($exists){
$newnum++; # increment new number (add 1)

# get the extension
@file_type   = split(/\./, $filename); # split the dots and add inbetweens to a list
# put the first element in the $barename var
$bareName    = $file_type[0];
# we can assume everything after the last . found is the extension
$file_type   = $file_type[$#file_type];
# $#file_type is the last element (note the pound or hash is used)

# remove all numbers from the end of the $bareName
$bareName =~ s/\d+$//ig;

# concatenate a new name using the barename + newnum + extension
$filename = $bareName . $newnum . '.' . $file_type;

# reset $exists to 0 because the new file name is now being checked
$exists = 0;

# recall this subroutine
&check_existence($dir,$filename,$newnum);
} else {
# the $filename, whether the first or one hundreth call, now does not exist
# so return the name to be used
return ($filename);
}
}

####################################################################

####################################################################

sub send_mail {
my ($from_email, $from_name, $to_email, $to_name, $subject, $message ) = @_;

if(open(MAIL, "|$CONFIG{mailprogram} -t")) {
print MAIL "From: $from_email ($from_name)\n";
print MAIL "To: $to_email ($to_name)\n";
print MAIL "Subject: $subject\n";
print MAIL "$message\n\nSubmitter's IP Address : $ENV{REMOTE_ADDR}";
close MAIL;
return(1);
} else {
return;
}
}

####################################################################

####################################################################

sub send_mail_NT {

my ($from_email, $from_name, $to_email, $to_name, $subject, $message ) = @_;

my ($SMTP_SERVER, $WEB_SERVER, $status, $err_message);
use Socket;
    $SMTP_SERVER = "$CONFIG{smtppath}";                                

# correct format for "\n"
    local($CRLF) = "\015\012";
    local($SMTP_SERVER_PORT) = 25;
    local($AF_INET) = ($] > 5 ? AF_INET : 2);
    local($SOCK_STREAM) = ($] > 5 ? SOCK_STREAM : 1);
    local(@bad_addresses) = ();
    $, = ', ';
    $" = ', ';

    $WEB_SERVER = "$CONFIG{smtppath}\n";
    chop ($WEB_SERVER);

    local($local_address) = (gethostbyname($WEB_SERVER))[4];
    local($local_socket_address) = pack('S n a4 x8', $AF_INET, 0, $local_address);

    local($server_address) = (gethostbyname($SMTP_SERVER))[4];
    local($server_socket_address) = pack('S n a4 x8', $AF_INET, $SMTP_SERVER_PORT, $server_address);

    # Translate protocol name to corresponding number
    local($protocol) = (getprotobyname('tcp'))[2];

    # Make the socket filehandle
    if (!socket(SMTP, $AF_INET, $SOCK_STREAM, $protocol)) {
        return;
    }

# Give the socket an address
bind(SMTP, $local_socket_address);

# Connect to the server
if (!(connect(SMTP, $server_socket_address))) {
return;
}

# Set the socket to be line buffered
local($old_selected) = select(SMTP);
$| = 1;
select($old_selected);

# Set regex to handle multiple line strings
$* = 1;

    # Read first response from server (wait for .75 seconds first)
    select(undef, undef, undef, .75);
    sysread(SMTP, $_, 1024);
#print "<P>1:$_";

    print SMTP "HELO $WEB_SERVER$CRLF";
    sysread(SMTP, $_, 1024);
#print "<P>2:$_";

while (/(^|(\r?\n))[^0-9]*((\d\d\d).*)$/g) { $status = $4; $err_message = $3}
if ($status != 250) {
return;
}

print SMTP "MAIL FROM:<$from_email>$CRLF";

sysread(SMTP, $_, 1024);
#print "<P>3:$_";
if (!/[^0-9]*250/) {
return;
}

    # Tell the server where we're sending to
print SMTP "RCPT TO:<$to_email>$CRLF";
sysread(SMTP, $_, 1024);
#print "<P>4:$_";
/[^0-9]*(\d\d\d)/;

# Give the server the message header
print SMTP "DATA$CRLF";
sysread(SMTP, $_, 1024);
#print "<P>5:$_";
if (!/[^0-9]*354/) {
return;
}

$message =~ s/\n/$CRLF/ig;

print SMTP qq~From: $from_email ($from_name)$CRLF~;
print SMTP qq~To: $to_email ($to_name)$CRLF~;
if($cc){
print SMTP "CC: $cc ($cc_name)\n";
}
print SMTP qq~Subject: $subject$CRLF$CRLF~;
print SMTP qq~$message~;

print SMTP "$CRLF.$CRLF";
sysread(SMTP, $_, 1024);
#print "<P>6:$_";
if (!/[^0-9]*250/) {
return;
} else {
return(1);
}

if (!shutdown(SMTP, 2)) {
return;
    }
}

####################################################################

####################################################################

sub PrintHead {
print qq~Content-type: text/html\n\n~;
print qq~

<title>PerlScriptsJavascript.com Free upload utility</title>

~;
}

####################################################################

####################################################################

sub PrintFoot {
print qq~

~;
}

####################################################################

####################################################################

sub check_email {
my($fe_email) = $_[0];
if($fe_email) {
if(($fe_email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/) ||
($fe_email !~ /^.+@\[?(\w|[-.])+\.[a-zA-Z]{2,3}|[0-9]{1,3}\]?$/)) {
return;
} else { return(1) }
} else {
return;
}
}


The link to the script: http://www.dangerouslystupid.com/upload.html
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 07:23:34 PM
Just noticed my path was incorrect: "/home/danger7/public_html"

I changed it to "/home/danger7/public_html/uploads", but it had no effect (other than not listing html files :D)

I'm absolutely lost here...
Title: CGI upload script
Post by: TranzNDance on July 23, 2004, 07:38:14 PM
Code: [Select]
# absolute URL to folder files will be uploaded to
$folder = "www.dangerouslystupid.com/uploads";

Maybe this should be "/home/danger7/public_html/uploads" I'm not sure if that is what your latest post was referring to.

Code: [Select]
$domain = "dangerouslystupid";
should be dangerouslystupid.com
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 07:44:21 PM
Quote from: TranzNDance
[I'm not sure if that is what your latest post was referring to.


Yep it was ;)

Quote from: TranzNDance
Code: [Select]
$domain = "dangerouslystupid";
should be dangerouslystupid.com


Okay, did that and no change... still get those screwy links :/
Title: CGI upload script
Post by: leighsww on July 23, 2004, 07:55:50 PM
That $domain config is just if you want the uploads to only occur from your domain. I doubt that's causing this problem.  But, okay, let's test it out and leave that blank (leave just the quotes) and see what happens.

Also, the $folder should be:

Code: [Select]
$folder = "http://www.dangerouslystupid.com/uploads";
Title: CGI upload script
Post by: leighsww on July 23, 2004, 07:59:27 PM
Also, just in case you changed this again ... your $dir should be:

Code: [Select]
$dir="/home/danger7/public_html/uploads";
Title: CGI upload script
Post by: TranzNDance on July 23, 2004, 08:03:07 PM
Quote from: leighsww
That $domain config is just if you want the uploads to only occur from your domain.

It shouldn't effect the function on fo0hzy's site, but it's meant to prevent other people from having it run on their sites. That was the problem with some of the mail forms; there was no validation test to keep them from being run off-site by spammers. So, it could be set to blank for testing purposes, but once things work, it should be set to the domain for security.
Title: CGI upload script
Post by: leighsww on July 23, 2004, 08:06:05 PM
Quote from: TranzNDance
It shouldn't effect the function on fo0hzy's site, but it's meant to prevent other people from having it run on their sites. That was the problem with some of the mail forms; there was no validation test to keep them from being run off-site by spammers. So, it could be set to blank for testing purposes, but once things work, it should be set to the domain for security.


Yes, I'm not saying it's not good to do/have, I'm saying that it is probably not what is causing the problem he is having with this error message.   :D

By leaving it blank for testing, we can see if that's effecting the error or not.
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 08:08:19 PM
WAAAHOOOO!!! It worked!!! Should I add the $domain config, or can I just turn on hotlink protection for the uploads folder, but allow images to be posts on my domain?

Thanks again!!! :o
Title: CGI upload script
Post by: leighsww on July 23, 2004, 08:12:31 PM
Well, if it was the $domain config, then maybe we should figure out why it was causing the error.

Try entering the http:// in front of the domain.  Maybe it needs that, who knows, LOL (temperamental scripts!)  :)
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 08:18:48 PM
It worked! :yey:

So, with that, I don't need to worry about enabling hotlink protection?
Title: CGI upload script
Post by: leighsww on July 23, 2004, 08:19:53 PM
Yup. But you may want to enable Hotlinks Protection for other reasons (so people can't link your files from their website), that's up to you.

So, are you good then?  Are we done here? LOL  :love:
Title: CGI upload script
Post by: TranzNDance on July 23, 2004, 08:24:00 PM
Quote from: fo0hzy
WAAAHOOOO!!! It worked!!! Should I add the $domain config, or can I just turn on hotlink protection for the uploads folder, but allow images to be posts on my domain?

That's great! :yey:

The domain security thing has to do with the script, not the folder. Let's say I'm a lazy website owner. I come across your upload script. Without the security setting, I can call your script from one of the pages on my site and it will process it as if it were on my site. You might not mind someone doing that, but after all the work you put into it, hey, make them suffer like you did! :D
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 08:24:40 PM
I guess so! Thank you sooo much, leigh!!! :thumb:  :love:
Title: CGI upload script
Post by: TranzNDance on July 23, 2004, 08:28:11 PM
hmmm... would hotlink protection protect scripts from running elsewhere, without any other similar security mechanisms? That would be good to have another layer of security.
Title: CGI upload script
Post by: Priest on July 23, 2004, 09:01:54 PM
Not natively they wouldn't.  You could go back into the htaccess file and add the script extensions that you wanted protected.
Title: CGI upload script
Post by: fo0hzy on July 23, 2004, 09:09:47 PM
I have turned on hotlink protection & tested it on two friend's forums... the redirect image (stop stealing my sh*t!) showed up just fine ;)
Title: CGI upload script
Post by: fo0hzy on July 25, 2004, 07:20:03 PM
Then it showed up on my own forum this morning, but only for one person... everyone else got the normal forum, but one guy got the hotlink image for every single image related to the forum :/ So I disabled it for now.
Title: CGI upload script
Post by: TranzNDance on July 25, 2004, 07:24:16 PM
fo0hzy,

Do you have both versions of your domain name in the whitelist?
It should be:
http://www.yourdomain.com
http://yourdomain.com
Title: CGI upload script
Post by: fo0hzy on July 27, 2004, 02:20:36 PM
Yep they are...

Three people have complained about this issue so far. I just enabled it again, only this time I ticked the little box "Enable direct requests"... dunno if that will make a difference, but there's only so many options.

One says it works fine on his home computer, but fouls up on his work computer... the other gets just the opposite. I think it's gotta be something to do with their systems, though what it could be, I have no idea. I had them all clean out their temp files & cookies but that didn't help.

 :?
Title: CGI upload script
Post by: TranzNDance on July 27, 2004, 02:25:16 PM
Could you please post the link(s) with the problem? If it's a forum that requires a login, please provide, that, too.

I wonder if it has to do with some software blocking the referrer, which has some consequence which I can't remember right now.
Title: CGI upload script
Post by: leighsww on July 27, 2004, 02:28:37 PM
fo0hzy, post what is in your .htaccess file so we can see what's written in there.
Title: CGI upload script
Post by: fo0hzy on July 27, 2004, 02:50:51 PM
It is this forum: http://www.dangerouslystupid.com/forum/ (I will PM the username & password)


Here's the htaccess

Code: [Select]
order allow,deny
allow from all



RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://dangerouslystupid.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://dangerouslystupid.com$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.dangerouslystupid.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.dangerouslystupid.com$      [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]
Title: CGI upload script
Post by: fo0hzy on July 27, 2004, 06:19:05 PM
Okay, the guy who was having problems from his home computer finally got home & logged on... and reports no troubles! Maybe ticking that lil box that says "Enable direct requests" did the trick?

We'll see when the other two affected members log in...  I'll keep ya posted... thanks :thumb: