#! /usr/bin/perl

##############################################################
# Name: x-link.pl                                            #
# Created: Mar. 98 by Knut A. Mork                           #
# Last changed: 7. Apr 1998 by Knut A. Mork                  #
# Takes the input from the link-page and sends a mail        #
# to webmaster                                               #
##############################################################


# Variables

$path       = "/home/almash/admin/x-link/";
$suggestion = "base_list";
$no_url     = 0;
$mailadr    = "almashriq\@hiof.no";

#-------------------------------------------------------------

# Standard procedure... :-)

print "Content-type: text/html\n\n";
$ct = $ENV{"CONTENT_TYPE"};
$cl = $ENV{"CONTENT_LENGTH"};

if( $ct ne "application/x-www-form-urlencoded")
{
    printf "I dont understand content-type: %s\n", $ct;
    exit 1;
}
read( STDIN, $qs, $cl);
@qs = split(/&/, $qs);

foreach $i (0 .. $#qs)
{
    $qs[$i] =~ s/\+/ /g;
    $qs =~ s/%(..)/pack("c",hex($1))/ge;
    ($name, $value) = split(/=/,$qs[$i],2);
    $qs{$name} = $value;
}

# convert (some) special caracters (and to ensure security!)

foreach $q (keys %qs)
{
    $qs{$q} =~ s/%2C/,/g;
    $qs{$q} =~ s/%21/!/g;
    $qs{$q} =~ s/%28/\(/g;
    $qs{$q} =~ s/%29/\)/g;
    $qs{$q} =~ s/%2F/\//g;
    $qs{$q} =~ s/%3A/:/g;
}

#-------------------------------------------------------------

# Sjekk om bruker har skrevet noe i de to foerste feltene, ellers send feil-melding til brukeren
foreach $name (keys(%qs))
{
    if( $name eq "URL" && $qs{$name} eq "")
    {
	$no_url = 1;
    }
}

# Skriver en ok-side til brukeren
print "<html>\n<head>\n<title>Adding external links<\/title>\n<\/head>\n";
print "<body bgcolor=\"#ffffff\" background=\"\/sys\/ornament-back-small.gif\">\n";
print "<center>\n<table width=528 cols=1 border=0 cellpadding=5>\n";
print "<tr valign=top>\n<td colspan=2>\n";
print "<a href=\"\/index.html\"><img src=\"\/sys\/almashriq-pan-head.gif\" border=0><\/a>\n";
print "<tr valign=top>\n<td colspan=2>\n<h3>External links<\/h3>\n<p>";
if( ! $no_url)
{
    print "Operation succeded. Your link has been sent to webmaster\n";
}
else
{
    print "Operation cancelled. You have to type in an url!\n";
    print "<p>\n<a href=\"link.html\">Try again<\/a>";
}
print "<p>\n<a href=\"\/index.html\"><img src=\"\/sys\/almashriq-bottom-line.gif\" border=0><\/a>\n<\/table>\n<\/center>\n<\/body>\n<\/html>";

#-------------------------------------------------------------

# Sender mail med dataene til almash

if( ! $no_url)
{
    $mail = "| /usr/sbin/sendmail $mailadr";
    open (MAIL, $mail);
    print (MAIL "From: x-link\@edmund2\.hiof\.no\r\n");
    print (MAIL "Subject: --Request for adding an external link--\r\n");
    print (MAIL "\n");
    foreach $name (keys(%qs))
    {
	print MAIL "$name: $qs{$name}\n";
    }
    print MAIL "\nFILE: \n\n";
    
# Henter inn forslag til hvor linken skal plasseres
    open (SUGG, "$path$suggestion");
    while(<SUGG>)
    {
	if( !($_ =~ /^\#/ or $_ =~ /^\s/))
	{
	    print MAIL "$_";
	}
    }
    close (SUGG);
    
    close (MAIL);
}
