Gossamer Forum
Home : Products : Links 2.0 : Customization :

Add.cgi create duplicate record

Quote Reply
Add.cgi create duplicate record
After some modification i make on my links installation i have a strange behaviour on Add.cgi.
When you press submit button more than one record is inserted in validate.db.

Most of the time are 2 records, with different ID but the same data, rarely 3 records or 1.

Any Help?
Quote Reply
Re: [gamluca] Add.cgi create duplicate record In reply to
what kind of changes did you make? Please explain and attach the files here if posible.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Add.cgi create duplicate record In reply to
Very Few modification, here the code, with red the modification i make:

#!/usr/local/bin/perl
# -------------
# Links
# -------------
# Links Manager
#
# File: add.cgi
# Description: Adds a record marked unvalidated to the database and
# optionally emails someone.
# Author: Alex Krohn
# Email: alex@gossamer-threads.com
# Web:
http://www.gossamer-threads.com/
# Version: 2.01
#
# (c) 1998 Gossamer Threads Inc.
#
# This script is not freeware! Please read the README for full details # on registration and terms of use.
# =====================================================================
#
# Setup Notes:
# Make sure the require statement below points to the config file.
# Required Librariers
# --------------------------------------------------------
eval {
($0 =~ m,(.*)/[^/]+,) && unshift (@INC, "$1"); # Get the script location: UNIX /
($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1"); # Get the script location: Windows \
require "admin/links.cfg"; # Change this to full path to links.cfg if you have problems.
require "$db_lib_path/db_utils.pl";
require "$db_lib_path/links.def";
require "admin/check-page.pl";
$build_use_templates ?
require "$db_lib_path/site_html_templates.pl" :
require "$db_lib_path/site_html.pl"; }; if ($@) {
print "Content-type: text/plain\n\n";
print "Error including libraries: $@\n";
print "Make sure they exist, permissions are set properly, and paths are set correctly.";
exit;
}
# ========================================================
eval { &main; }; # Trap any fatal errors so the program hopefully
if ($@) { &cgierr("fatal error: $@"); } # never produces that nasty 500 server error page.
exit; # There are only two exit calls in the script, here and in in &cgierr.
sub main {
# --------------------------------------------------------
local (%in) = &parse_form;
# We are processing the form.
if (keys %in != 0) {
&process_form;
exit;
}
# 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 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") {
if ($mod_rewrite eq 1) {
## Modifica l'url per il rewriteRule
@cats = split(/\//,$in{$db_cols[$db_category]});
my $newurl = "";
for ($count = $#cats; $count >= 0; $count--) {
if ($count ne 0) {
$newurl .= $cats[$count] . "-";
} else {
$newurl .= $cats[$count]; }
}
$linkurl = &urlencode($newurl) . ".htm";
} else {
# First let's get the name, number of links, and last modified date...
$linkurl = "$build_root_url/" . &urlencode($in{$db_cols[$db_category]}) . "/";
}
# 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;
# Adding the Previous CAT variable to the template name $lastcat
@total = split(/\|/,&join_encode(%in));
@temp_lc = &build_clean(split(/,/,@total[4]));
$c = @temp_lc;
$lastcat = @temp_lc[$c-1];
# Checking Reciprocal Link Presence
$no_reciprocal = 0;
if ($add_check_recipr eq 1) {
$recipr = &check_page(@total[14]);
if ($recipr eq 0) {
$no_reciprocal = 1;
}
}
# Send the visitor to the success page.
&site_html_add_success;
}
else {
&site_html_add_failure($status);
exit;
}
}
sub send_email {
# --------------------------------------------------------
# Sends an email to the admin, letting him know that there is # a new link waiting to be validated. No error checking as we don't # want users to see the informative &cgierr output.
# Check to make sure that there is an admin email address defined.
$db_admin_email or &cgierr("Admin Email Address Not Defined in config file!");
my $to = $db_admin_email;
my $from = $in{$db_cols[$db_contact_email]};
my $subject = "Addition to Database: $in{'Title'}\n";
my $msg = qq|
The following link is awaiting validation:
Title: $in{'Title'}
URL: $in{'URL'}
Category: $in{'Category'}
Description: $in{'Description'}
Contact Name: $in{'Contact Name'}
Contact Email: $in{'Contact Email'}

Remote Host: $ENV{'REMOTE_HOST'}
Referer: $ENV{'HTTP_REFERER'}

To validate, please go to:
$db_script_url

Sincerely,
Links Manager.
|;
# Then mail it away!
require "$db_lib_path/Mailer.pm";
my $mailer = new Mailer ( { smtp => $db_smtp_server,
sendmail => $db_mail_path,
from => $from,
subject => $subject,
to => $to,
msg => $msg,
log => $db_mailer_log
} ) or return;
$mailer->send or return;
}

Quote Reply
Re: [gamluca] Add.cgi create duplicate record In reply to
I don't see anything there that should be causing multiple entries. I believe the problem lies elsewhere. Backup your changes and restore add.cgi to the original to see if the problem persists.

Make sure you aren't hitting the submit button twice or refreshing the "success" page.

Also I want to point out that you are using the wrong the logical operators. While it might not matter in some cases, using the wrong operator can cause unexpected results.

example:

print ('01' == 1 ? 'yes' : 'no' ); #prints "yes"
print ('01' eq 1 ? 'yes' : 'no' ); #prints "no"

see the "perlop" man page for a full explaination of the different operators.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Add.cgi create duplicate record In reply to
do you mean here:

if ($recipr eq 0)

Anyway the problem was present even before the red modification.

I restore a default add.cgi with no modification and i still receive 2 submission.
I didn't click 2 times end even i don't refresh the page !
Quote Reply
Re: [gamluca] Add.cgi create duplicate record In reply to
Quote:
do you mean here:

Yes, and elsewhere in your code.

Quote:
Anyway the problem was present even before the red modification.

Good. We've ruled out your recent changes to add.cgi as the problem. Are there any other changes or was this the only one?

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Add.cgi create duplicate record In reply to
Hi Fuzzy, as i told you
Quote:

I restore a default add.cgi with no modification and i still receive 2 submission.


i replace the modified add.cgi with the standard one and i receive the same problem.
The script have a lot of modification ,but i think that in the submit process only the add.cgi is called !?!
no ?
Quote Reply
Re: [gamluca] Add.cgi create duplicate record In reply to
It doesn't look like it in the code you posted, but are you using the add-confirm mod?


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Add.cgi create duplicate record In reply to
i don'y use the confirm mod, but i add come field in the links database:

Quote:

%db_def = (
ID => [0, 'numer', 5, 8, 1, '', ''],
Title => [1, 'alpha', 40, 75, 1, '', ''],
URL => [2, 'alpha', 40, 75, 1, 'http://', '^http'],
Date => [3, 'date', 15, 15, 1, \&get_date, ''],
Category => [4, 'alpha', 0, 150, 1, '', ''],
Description => [5, 'alpha', '40x3', 500, 0, '', ''],
'Contact Name' => [6, 'alpha', 40, 75, 1, 'a', ''],
'Contact Email' => [7, 'alpha', 40, 75, 1, '', '.+@.+\..+'],
Hits => [8, 'numer', 10, 10, 1, '0', '\d+'],
isNew => [9, 'alpha', 0, 5, 0, 'No', ''],
isPopular => [10, 'alpha', 0, 5, 0, 'No', ''],
Rating => [11, 'numer', 10, 10, 1, 0, '^[\d\.]+$'],
Votes => [12, 'numer', 10, 10, 1, 0, '^\d+$'],
ReceiveMail => [13, 'alpha', 10, 10, 1, 'Yes', 'No|Yes'],
ReciprURL => [14, 'alpha', 40, 75, 1, '', '^((ht|f)tp(s?))\://([0-9a-zA-Z\-]+\.)+[a-zA-Z]
PremiumListing => [15, 'alpha', 10, 10, 0, 'No', 'No|Yes'],
PremiumImage => [16, 'alpha', 40, 75, 0, '', ''],
PartnerListing => [17, 'alpha', 10, 10, 0, 'No', 'No|Yes']
);

I add the red line just 1 week before and the problem was already present.
Quote Reply
Re: [gamluca] Add.cgi create duplicate record In reply to
Problem Solved,

In add.html i use :

<form action="<%db_cgi_url%>/search.cgi">

instead of

<form action="<%db_cgi_url%>/search.cgi" method="GET">

This was the reason.
Thanks.
Quote Reply
Re: [gamluca] Add.cgi create duplicate record In reply to
Not sure why that would cause a problem, but if it fixed it, great! Smile

The code you posted has an error, too:

ReciprURL => [14, 'alpha', 40, 75, 1, '', '^((ht|f)tp(s?))\://([0-9a-zA-Z\-]+\.)+[a-zA-Z]
PremiumListing => [15, 'alpha', 10, 10, 0, 'No', 'No|Yes'],

ReciprURL => [14, 'alpha', 40, 75, 1, '', '^((ht|f)tp(s?))\://([0-9a-zA-Z\-]+\.)+[a-zA-Z]

It's missing: '],


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] Add.cgi create duplicate record In reply to
There is not error, i just past only part of the complete line:

ReciprURL => [14, 'alpha', 40, 75, 1, '', '^((ht|f)tp(s?))\://([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(/\S*)?$'],

The problem with double records is not solved, yesterday i try to make some submission and seems work, but tonight i receive many duplicate records again, i really don't know where to investigate anymore !
Quote Reply
Re: [gamluca] Add.cgi create duplicate record In reply to
Can you confirm whether you get duplicates when directly adding a link from the admin panel?

Quote:
The problem with double records is not solved, yesterday i try to make some submission and seems work, but tonight i receive many duplicate records again, i really don't know where to investigate anymore !

So when you test adding links from add.cgi, you received no duplicates? But user submitted links do? I'm begining to think you are being spammed.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Add.cgi create duplicate record In reply to
if i add a link from the admin.cgi everithing is ok.
the duplicate record happen only if i use the add.cgi, i'm not spammed because even if I tray to add a link i receive duplicate record, some times even triple.

Very Strange !!!!
I'm still investigating but i really donět know where to look for, the same problem happens with an original add.cgi !!!