Gossamer Forum
Home : Products : Links 2.0 : Customization :

add.cgi upload mod

(Page 1 of 2)
> >
Quote Reply
add.cgi upload mod
Every link on my site has a jpg to go with it. When people use add.cgi, I have to go to their site, take a screen shot of their game, validate their site, and then upload the jpg. I'm getting sick of this and want a mod that will allow people to upload/include a jpg with their site submission.

I searched the forum and resource center, and it doesn't look like anyone has published anything like this for Links 2.0 before. So... I am going to write the mod, however, if similar code is out there, please give me a heads up and make my life a little easier.

Thanks!

Besides myself, is there anyone out there interested in something like this?

Quote Reply
Re: add.cgi upload mod In reply to
Just a question, but why couldn't your submitters just include the url to the jpg instead of you hosting their pics on your server? Then all you need is a field for the pic.

Quote Reply
Re: add.cgi upload mod In reply to
...because then the quality of your site in under the control of the resource owners.

They may delete/move images and you'll be left with loads of dead images on your site.

Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
I would be happy to write an upload mod if you didn't want to do it.

Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
Paul, that would be very cool if you wanted to take this on. I downloaded some already written code, modified it, and tried to stick it in sub validate_record in db_utils.pl, but it had too many errors. There are three features that are important to me:

1. only accept jpg files
2. files must be < 10 kb
3. file should be renamed to the same number as the links ID (example: picture.jpg should become 256.jpg)
Quote Reply
Re: add.cgi upload mod In reply to
I'll experiment over the next day or two and let you know how I get on.

Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
I've put a few hours into this mod, and am realizing that I don't have a deep enough understanding of links/perl to get it done. I know how to get it to work with two forms, but I would like one form, and that totally changes the scenario because it would require modification to &process_form (add.cgi), &validate_record (db_utils.pl), and &parse_form (db_utils.pl). The &parse_form sub is difficult enough for me to understand, trying to work in a file upload is way over my head.

This mod is really important to me. I get so many submissions (right now via email) that I can no longer devote the time to manually enter the links, and then take screenshots.

It appears that Paul is super busy! So, can anyone else offer me some help, or write this mod (it can't be much code!) for a small fee?

THANKS! Ryan

http://www.allgamesfree.com
If anyone wants a cool way to get more people to rate links, check out my site and click one of the links (just not the Adversity one)

Quote Reply
Re: add.cgi upload mod In reply to
I've been using a separate script from links to handle uploads in a new window. Once finished, they only need to enter the filename in the field with links. The template already has the information setup...I can provide you instructions on this if you'd like...

Anyone else have an easier solution?

Perl Hopefull
Quote Reply
Re: add.cgi upload mod In reply to
Yeah, that would be cool. Tell me how you do it. The only thing that concerns me is people abusing it and just uploading tons of files.

Quote Reply
Re: add.cgi upload mod In reply to
The script is called PS Uploader and can be found at http://64.45.58.170/en/library.shtml

It's a free script and has a great tutorial on how to get it up and running quickly...you can set limits on size, type of download, and so on in the admin...

Good Luck,


Perl Hopefull
Quote Reply
Re: add.cgi upload mod In reply to
OK, I am familiar with that. You probably didn't notice that the code I posted above was taken from PSupload. I just want something to integrate with add.cgi.

If anyone knows how to do this, please help us out.

Quote Reply
Re: add.cgi upload mod In reply to
Here's a quick hint:

Code:
my ($bytesread,$buffer);
open(OUTFILE, ">$in{'File'}");
while ($bytesread=read($in{'File'},$buffer,1024)) {
print OUTFILE $buffer;
}
close (OUTFILE);
Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
Hey,

Why did you delete you post, I was just about to answer Smile!

Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
That causes a problem because now there is an extra piece of data contained in the %in hash so validate_record comes back and says that all of the required fields were left blank. How can I trick it into thinking that $in{'File'} isn't there?

(I deleted an earlier post because I forgot the ENCTYPE="multipart/form-data")
Quote Reply
Re: add.cgi upload mod In reply to
It shouldn't give an error.

Have you added File as a field in links.def?

If so make in non-required (1 to 0), if not then it won't be validated either way.

If you want either a URL or File then make URL non-required and in add.cgi add:
Code:
if (!$in{'URL'} && !$in{'File'}) {
&site_html_add_failure("Please enter a URL or a File") and return;
}
Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
1) Un-require the File field in the links.def set it to 0 rather than 1 to un-require it.

2) Then add conditional statements around the codes Paul provided:

Code:

if ($in{'File'} ne "") {
PAUL's CODES
}


Regards,

Eliot Lee
Quote Reply
Re: add.cgi upload mod In reply to
Oh right, I see what you are asking now....yep try:

Code:
if ($in{'File'}) {
my ($bytesread,$buffer);
open(OUTFILE, ">$in{'File'}");
while ($bytesread=read($in{'File'},$buffer,1024)) {
print OUTFILE $buffer;
}
close (OUTFILE);
}
Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
I can't get this sucker to work! This mod involves three sub programs &parse_form, &process_form, &validate_record. I made changes to only &process_form.

Right before

Code:
# Validate the form input..
I pasted Paul's code:
Code:
if ($in{'File'}) {
my ($bytesread,$buffer);
open(OUTFILE, ">/home/allgamesfree/public_html/images/captures/temp/$in{'File'}");
while ($bytesread=read($in{'File'},$buffer,1024)) {
print OUTFILE $buffer;
}
close (OUTFILE);
}
That code seems to do nothing now, as no file is saved. A couple of lines down, there is a
Code:
&validate_record(%in)
. It doesn't validate properly, and returns that all of the required fields have been left blank. This is because I have no file field in my links.def. I don't need one because the names of the images correspond with their link idea. For example, the images that goes with link 50 is called 50.jpg and can be inserted into a template via <%ID%>.jpg. To bypass this links.def problem, at the end of the code Paul gave me, I added:

Code:
delete $in{'File'};
I figured since there is now no "File" key in the %in hash, &validate_record would not see any problems. This is not the case.

So there are really two problems:

1. Paul's code does not write a file as it should.
2. &validate_record returns errors even though $in{'File'} no longer exists.

I made certain that my form tag includes
Code:
ENCTYPE="multipart/form-data"
, and also that the input box for upload the jpg has a name of "File" and type of "File".

I can't understand what could be wrong. Hopefully I've clearly stated the problem. Paul, Anthro, and the other guys: I appreciate your help more than you could know!

Ryan

Quote Reply
Re: add.cgi upload mod In reply to
Yeah the reason it doesn't work is because of the way add.cgi works.

You would need to bypass &parse_form if you needed to upload a file and vice versa.

You'd need something like...

Code:
if ($in{'File'}) {
&upload_file;
} else {
&process_form
}
Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
So it's impossible to add a spot for file upload to the form without a huge re-write of add.cgi? Why doesn't "delete $in{'File'}" take care of the hash so it's ok to pass through &validate_record?

Quote Reply
Re: add.cgi upload mod In reply to
I gave up on delete $in{'File'} since it doesn't seem to work. Instead I added a new field called "File" to links.def so &validate_record would hopefully accept %in and not return that fields have been left blank. No such luck, it still does.

What can I do? It can't be impossible to run a form with both text inputs and a file upload through add.cgi! Please help, I've been working really hard on this.

Thanks... Ryan

Quote Reply
Re: add.cgi upload mod In reply to
I'm not sure why you are experiencing that problem. I've added a File field before and it didn't do what you are experiencing.

All you need to do is make the file field non required and then use:

if ($in{'File'}) {
upload the file
} else {
go the normal route
}

...as I mentioned further up.

Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
I'm not understanding this:

if ($in{'File'}) {
&upload_file;
} else {
&process_form;
}

So, if there is a file, and we &upload_file, then &process_form never runs. Basically, that conditional gives us one or the other, but not both. In the system you created, did you use two forms, one for text and one for the file?

Quote Reply
Re: add.cgi upload mod In reply to
No,

In the upload sub you'd upload the file and then call the process_form sub for the rest of the processing.

Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: add.cgi upload mod In reply to
I can't understand why that should work, but I tried it anyway and got the same results. Here is my code:

Code:
sub main {
# --------------------------------------------------------
local (%in) = &parse_form;

# We are processing the form.
if (keys %in != 0) {
if ($in{'File'}) {
&upload_file;
} else {
&process_form;
}
}


# Otherwise we are displaying the form (in site_html.pl).
else {
#if ($db_single_category) {
# my %is_valid = map { $_ => 1 } &category_list;
# $ENV{'HTTP_REFERER'} =~ s,/[^/]+\.[^/]+$,,;
# $ENV{'HTTP_REFERER'} =~ m,$build_root_url/(.+?)/?$,;
# $is_valid{$1} ? &site_html_add_form ($1) : &site_html_add_form ();
#}
#else {
&site_html_add_form ();
#}
}
}

sub upload_file {
# --------------------------------------------------------
if ($in{'File'}) {
my ($bytesread,$buffer);
open(OUTFILE, ">/home/allgamesfree/public_html/images/captures/temp/$in{'File'}");
while ($bytesread=read($in{'File'},$buffer,1024)) {
print OUTFILE $buffer;
}
close (OUTFILE);
}
delete $in{'File'};
&process_form;
}

sub process_form {
# --------------------------------------------------------
my ($key, $status, $line, $output);

# Check the referer.
if (@db_referers and $ENV{'HTTP_REFERER'}) {
$found = 0;
foreach (@db_referers) {
$ENV{'HTTP_REFERER'} =~ /$_/i and $found++ and last;
}
if (!$found) {
&site_html_add_failure ("Auto submission is not allowed in this directory. Please visit the site to add your entry.");
return;
}
}

# This will set system fields like Validated to their proper values.
foreach $key (keys %add_system_fields) {
$in{$key} = $add_system_fields{$key};
}

# Set date variable to today's date.
$in{$db_cols[$db_modified]} = &get_date;

open (ID, "<$db_links_id_file_name") or &cgierr("error in process_form. unable to open id file: $db_links_id_file_name. Reason: $!");
$in{$db_key} = <ID> + 1; # Get next ID number
close ID;




# Validate the form input..
$status = &validate_record(%in);
if ($status eq "ok") {





# Update the counter.
open (ID, ">$db_links_id_file_name") or &cgierr("error in get_defaults. unable to open id file: $db_links_id_file_name. Reason: $!");
flock(ID, 2) unless (!$db_use_flock);
print ID $in{$db_key}; # update counter.
close ID; # automatically removes file lock

# Print out the validate input to a "validation database" where it is stored until
# the admin decides to add it into the real database.
open (VAL, ">>$db_valid_name") or &cgierr("error in add_record. unable to open validate file: $db_valid_name. Reason: $!");
flock(VAL, 2) unless (!$db_use_flock);
print VAL &join_encode(%in);
close VAL; # automatically removes file lock

# Send the admin an email message notifying of new addition.
&send_email;
# Send the visitor to the success page.
&site_html_add_success;
}
else {
&site_html_add_failure($status);
}
}
The result of all that is no file upload and more field cannot be left blank errors. What am I doing wrong?
> >