#!/usr/bin/perl
####################################################################################
# Links Admin Script (links.cgi) Copyright 2000 Brian Lozier (lozier@bigfoot.com). #
# Do not distribute this file without the readme.html file that was included in #
# the zip! Please read the readme.html file - it includes important installation #
# instructions as well as copyright information. #
####################################################################################
# Declare variables.
###############################################
# Links database file:
$db = "links.dat";
# Category File:
$cat_file = "categories.dat";
# Title:
$title = "Links Admin";
# Background Color:
$bgcolor = "000000";
# Text Color:
$text = "0000FF";
# Link Color:
$link = "00CCFF";
# Visited Link Color:
$vlink = "0000FF";
# Active Link Color:
$alink = "FFFFFF";
# Font Face:
$font = "Arial";
# Font Size:
$size = "2";
# Password:
$pass = "links";
# Define the data type for the browser.
###############################################
print "Content-type: text/html\n\n";
# Parse the incoming data.
###############################################
&ReadParse;
# Get the Query from the Environment Variables.
###############################################
if ($in{'action'} eq '') {
$in{'action'} = "print_main";
} # End if
# Function: print_main:
###############################################
if ($in{'action'} eq 'print_main') {
&PrintMainHeader;
print<<"END";
Please choose an option:
END
&PrintFooter;
} # End print_main
# Function: print_edit_link:
###############################################
elsif ($in{'action'} eq 'print_edit_link') {
&PrintMainHeader;
# List categories:
###############################################
open(CATS,"$cat_file") or &dienice("Could not open file: $cat_file.\n");
@cats = ;
close(CATS);
open(LINKS,"$db") or &dienice("Could not open file: $db.\n");
@links = ;
close(LINKS);
print<<"END";
Please choose which link you would like to edit.
END
&PrintFooter;
} # End print_edit_link
# Function: edit_link_2:
###############################################
elsif ($in{'action'} eq 'edit_link_2' and $in{'password'} eq $pass) {
&PrintMainHeader;
unless ($in{'edit'}) {
&dienice("You forgot to pick a link, please use your [Back] button.\n");
} # End unless
open(LINKS,"$db") or &dienice("Could not open file: $db.\n");
@links = ;
close(LINKS);
foreach $line (@links) {
chomp($line);
($link_num, $name, $category_num, $url, $description) = split(/\|/,$line);
if($in{'edit'} eq $link_num) {
$edit{'link_num'} = "$link_num";
$edit{'name'} = "$name";
$edit{'category_num'} = "$category_num";
$edit{'url'} = "$url";
$edit{'description'} = "$description";
} # End if
} # End foreach
print<<"END";
END
&PrintFooter;
} # End edit_link_2
# Function: edit_link:
###############################################
elsif ($in{'action'} eq 'edit_link' and $in{'password'} eq $pass) {
&PrintMainHeader;
unless ($in{'name'} and $in{'url'} and $in{'description'}) {
&dienice("You forgot to fill in all the fields, please use your [Back] button.\n");
} # End unless
open(LINKS,"$db") or &dienice("Could not open file: $db.\n");
@links = ;
close(LINKS);
open(LINKS,">$db") or &dienice("Could not open file: $db.\n");
flock(LINKS, 2) or &dienice("Could not lock file: $db.\n");
foreach $line (@links) {
chomp($line);
($link_num, $name, $category_num, $url, $description) = split(/\|/,$line);
if($link_num eq $in{'link_num'}) {
$new_record = join('|', $link_num, $in{'name'}, $in{'category_num'}, $in{'url'}, $in{'description'});
print LINKS "$new_record\n";
} # End if
else {
$record = join('|', $link_num, $name, $category_num, $url, $description);
print LINKS "$record\n";
} # End else
} # End foreach
close(LINKS);
print<<"END";
The link was edited successfully.
END
&PrintFooter;
} # End edit_link
# Function: print_delete_form:
###############################################
elsif ($in{'action'} eq 'print_delete_form') {
&PrintMainHeader;
# List categories:
###############################################
open(CATS,"$cat_file") or &dienice("Could not open file: $cat_file.\n");
@cats = ;
close(CATS);
open(LINKS,"$db") or &dienice("Could not open file: $db.\n");
@links = ;
close(LINKS);
print<<"END";
Please place a check next to each link you would like to delete.
END
&PrintFooter;
} # End print_delete_form
# Function: delete_link:
###############################################
elsif ($in{'action'} eq 'delete_link' and $in{'password'} eq $pass) {
&PrintMainHeader;
open(LINKS,"$db") or &dienice("Could not open file: $db.\n");
@links = ;
close(LINKS);
open(LINKS,">$db") or &dienice("Could not open file: $db.\n");
flock(LINKS, 2) or &dienice("Could not lock file: $db.\n");
foreach $instance (@links) {
chomp($instance);
($link_num, $name, $category_num, $url, $description) = split(/\|/,$instance);
unless ($in{"$link_num"} eq 'delete') {
$record = join('|', $link_num, $name, $category_num, $url, $description);
print LINKS "$record\n";
} # End unless
} # End foreach
close(LINKS);
print "Deletions successful.\n";
&PrintFooter;
} # End delete_link
# Function: print_add_cat:
###############################################
elsif ($in{'action'} eq 'print_add_cat') {
&PrintMainHeader;
print "Current Categories:\n";
# Get number of links in each category:
###############################################
open(LINKS,"$db") or &dienice("Could not open file: $db.\n");
@links = ;
close(LINKS);
foreach $line (@links) {
chomp($line);
($link_num, $name, $cat_num, $url, $description) = split(/\|/,$line);
$count{"$cat_num"}++;
} # End foreach
# List categories:
###############################################
open(CATS,"$cat_file") or &dienice("Could not open file: $cat_file.\n");
@cats = ;
close(CATS);
print "\n";
foreach $line (@cats) {
chomp($line);
($cat_num, $category, $cat_descrip) = split(/\|/,$line);
print "- $category\n";
if ($count{$cat_num}) {
if ($count{$cat_num} == 1) {
print "(1 Link)";
} # End if
else {
print "($count{$cat_num} Links)";
} # End else
} # End if
else {
print "(0 Links)";
} # End else
print "
$cat_descrip \n";
} # End foreach
print "
\n";
print<<"END";
To add a category, please fill out the form below, and then press the [Submit] button.
END
&PrintFooter;
} # End print_add_cat
# Function: add_cat:
###############################################
elsif ($in{'action'} eq 'add_cat' and $in{'password'} eq $pass) {
&PrintMainHeader;
unless ($in{'new_category'}) {
&dienice("You forgot to fill in all the fields, please try again.\n");
} # End unless
# List categories:
###############################################
open(CATS,"$cat_file") or &dienice("Could not open file: $cat_file.\n");
@cats = ;
close(CATS);
chomp($cats[0]);
($cat_num, $category, $description) = split(/\|/,$cats[0]);
if($cat_num) {
$count = ++$cat_num;
} # End if
else {
$count = 1;
} # End else
$record = join('|', $count, $in{'new_category'}, $in{'new_cat_descrip'});
open(CATS,">$cat_file") or &dienice("Could not open file: $cat_file.\n");
flock(CATS, 2) or &dienice("Could not lock file: $cat_file.\n");
print CATS "$record\n";
foreach $line (@cats) {
chomp $line;
print CATS "$line\n";
} # End foreach
close(LINKS);
print<<"END";
Category "$in{'new_category'}" has been added.
END
&PrintFooter;
} # End add_cat
# Function: print_delete_cat:
###############################################
elsif ($in{'action'} eq 'print_delete_cat') {
&PrintMainHeader;
# List categories:
###############################################
open(CATS,"$cat_file") or &dienice("Could not open file: $cat_file.\n");
@cats = ;
close(CATS);
print<<"END";
END
&PrintFooter;
} # End print_delete_cat
# Function: delete_cat:
###############################################
elsif ($in{'action'} eq 'delete_cat' and $in{'password'} eq $pass) {
&PrintMainHeader;
# Delete the Category
open(CATS,"$cat_file") or &dienice("Could not open file: $cat_file.\n");
@cats = ;
close(CATS);
open(CATS,">$cat_file") or &dienice("Could not open file: $cat_file.\n");
flock(CATS, 2) or &dienice("Could not lock file: $cat_file.\n");
foreach $line (@cats) {
chomp($line);
($cat_num, $category, $cat_descrip) = split(/\|/,$line);
unless($in{'delete'} eq $cat_num) {
$record = join('|', $cat_num, $category, $cat_descrip);
print CATS "$record\n";
} # End unless
} # End foreach
close(CATS);
# Delete the links in the category
open(LINKS,"$db") or &dienice("Could not open file: $db.\n");
@links = ;
close(LINKS);
open(LINKS,">$db") or &dienice("Could not open file: $db.\n");
flock(LINKS, 2) or &dienice("Could not lock file: $db.\n");
foreach $line (@links) {
chomp($line);
($link_num, $name, $cat_num, $url, $description) = split(/\|/,$line);
unless($in{'delete'} eq $cat_num) {
$record = join('|', $link_num, $name, $cat_num, $url, $description);
print LINKS "$record\n";
} # End unless
} # End foreach
close(LINKS);
print "Deletions Successful.";
&PrintFooter;
} # End delete_cat
#
else {
# Password Checker:
###############################################
&dienice("Invalid password, please try again.");
} # End Else
####################################################################################
# Subroutine to print main header. #
####################################################################################
sub PrintMainHeader {
print<<"END";
$title
$title
[Main Admin]
[Main Links Page]
END
} # End PrintMainHeader
####################################################################################
# Subroutine to print footer. #
####################################################################################
sub PrintFooter {
print<<"END";
Script \© Copyright 2000 Script Central. This script \+ more available at:
Script Central.
END
} # End PrintFooter
####################################################################################
# Subroutine to print an error message if the script dies. #
####################################################################################
sub dienice {
($errmsg) = @_;
print "Error\n";
print "Error
\n";
print "$errmsg\n";
print "\n";
exit;
} # End dienice
####################################################################################
# Adapted from cgi-lib.pl by S.E.Brenner@bioc.cam.ac.uk #
# Copyright 1994 Steven E. Brenner #
####################################################################################
sub ReadParse { local (*in) = @_ if @_;
local ($i, $key, $val); if ( $ENV{'REQUEST_METHOD'} eq "GET" )
{
$in = $ENV{'QUERY_STRING'};
}
elsif ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}
else
{
# Added for command line debugging
# Supply name/value form data as a command line argument
# Format: name1=value1\&name2=value2\&...
# (need to escape & for shell)
# Find the first argument that's not a switch (-)
$in = ( grep( !/^-/, @ARGV )) [0];
$in =~ s/\\&/&/g; } @in = split(/&/,$in);
foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;
# Split into key and value.
($key, $val) = split(/=/,$in[$i],2);
# splits on the first =.
# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
# Associate key and value. \0 is the multiple separator
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val; } return length($in); }