#!/usr/bin/perl

# Index Finger Pro v4.101
# Developed by Index Finger, Inc. - www.indexfinger.com
# Copyright 1998-99 Index Finger, Inc. - All rights reserved.

$thisprogram = "request.cgi";
$operatingsystem = "unix";

if (($operatingsystem eq "nt") || ($operatingsystem eq "other")) { 
    $ifdirectory = "D:/home/indexfinger/";
    }

$filetorequire = "$ifdirectory" . "config.cgi";
require "$filetorequire";
$filetorequire = "$ifdirectory" . "config-admin.cgi";
require "$filetorequire";
$filetorequire = "$ifdirectory" . "variables/setup.cgi";
require "$filetorequire";
$filetorequire = "$ifdirectory" . "programs.cgi";
require "$filetorequire";
$filetorequire = "$ifdirectory" . "subroutes.cgi";
require "$filetorequire";


&parsequery;
    $action = $QUERY{'action'};
    $inidchain = $QUERY{'where'};
    ($inmaingroupid,$inthisgroupid,$ingroupid) = split(/\-/,$inidchain);

&parseform;
    if ($QUERY{'where'} eq "") { $inidchain = $FORM{'where'}; }

# Begin program here.

&nonhtmlmainmenu;

$output .= "<a href=\"$directoryurl/$frontpage\">Home</a> /<p>\n";

$output .= "<b>Add A Site Request</b><p>\n";

&getcategoryinfo($inidchain);

if ($groupname ne "") {

    if ($action eq "process") {

        $inname = $FORM{'name'};
        if ($inname eq "") { $mia = "yes"; }
        $inurl = $FORM{'url'};
        if ($inurl eq "") { $mia = "yes"; }
        $indesc = $FORM{'desc'};
        if ($indesc eq "") { $mia = "yes"; }
        $inowner = $FORM{'owner'};
        if ($inowner eq "yes") {
            $inyourname = $FORM{'yourname'};
            if ($inyourname eq "") { $mia = "yes"; }
            $inyouremail = $FORM{'youremail'};
            if ($inyouremail eq "") { $mia = "yes"; }
            }

        if ($mia eq "yes") {
            $output .= "<b>Error:</b><p>\n";
            $output .= "<ul>\n";
            if ($inname eq "") { $output .= "<li>Please enter the name of the site.\n"; }
            if ($inurl eq "") { $output .= "<li>Please enter the URL for the site.\n"; }
            if ($indesc eq "") { $output .= "<li>Please enter the description for the site.\n"; }
            if ($inowner eq "yes") {
                if ($inyourname eq "") { $output .= "<li>Please enter your name.\n"; }
                if ($inyouremail eq "") { $output .= "<li>Please enter your email address.\n"; }
                }
            $output .= "</ul>\n";
            $output .= "Please use your back button to correct this information.\n";
            }

        else {
            &addtoqueue;
            if ($notifadmin eq "on") { &notification; }

            if ($inyouremail ne "") {
                $filetoappend = "$ifdirectory" . "mail/webowners.cgi";
                if (-e $filetoappend) { open(FILE,">>$filetoappend"); }
                else { open(FILE,">$filetoappend"); }
                print FILE "$inyouremail\n";
                close(FILE);
                }
 
            &gethtmlpage("variables/addasite-success.html");
            $output .= "<p>\n";
            $output .= "<a href=\"$directoryurl/$frontpage\">Click here to return to the main page</a><p>\n";
            }

        }


    else {
        &gethtmlpage("variables/addasite-form.html");
        $output =~ s/<form>/<form action=\"$adminurl\/$thisprogram?action=process&where=$inidchain\" method=\"post\">/;
        $output =~ s/\$where/$location/;
        }

    }

else { &gethtmlpage("variables/addasite-nocategory.html"); }

    &nonhtmlpoweredby;

if (-e $requesttemplate) {
    open(TEMPLATE,"$requesttemplate");
    flock(TEMPLATE,2);
    @template = <TEMPLATE>;
    close(TEMPLATE);
    foreach $line (@template) { $body = "$body$line"; }
    }


    &template;


sub addtoqueue {
    opendir (QUEUEDIR, "queue"); 
    @queuedata = readdir(QUEUEDIR);
    closedir (QUEUEDIR);

    @sortedqueuedata = sort(@queuedata);
    @sortedqueuedata = grep(/txt/,@sortedqueuedata);

    foreach $file (@sortedqueuedata) {
        ($queueid,$trash) = split (/\./,$file);
        $queueid =~ s/q//g;
        push(@newdata, $queueid);
        }

    @sortdata = sort numerically (@newdata);
    @sortdata = reverse(@sortdata);

    for (@sortdata) {
        $highest = $sortdata[0];
        }
    $highest++;

    $currenttime = time;

    $newqueueid = "q$highest";

    $filetomake = "queue/$newqueueid.txt";

    unless (-e $filetomake) {
        open(FILE,">$filetomake");
        flock(FILE,2);
        print FILE "$newqueueid|$currenttime|$location|$inidchain|$inname|$inurl|$indesc|$inyourname|$inyouremail";
        close(FILE);
        }
    }

sub notification {
    if ($servertype eq "sendmail") {
        open(MAIL, "|$mailprog -t");
        print MAIL "X-Mailer: Index Finger Pro v4.101\n";
        print MAIL "X-Mailer-Info: www.indexfinger.com\n";
        print MAIL "To: $notifemail\n";
        print MAIL "From: $directoryname <$fromemail>\n";
        print MAIL "Subject: Listing Request\n\n";
        print MAIL "\n";
        print MAIL "=====================================================================\n";
        print MAIL "$directoryname\n";
        print MAIL "$directoryurl\n\n";
        print MAIL "This site is requesting to be listed:\n\n";
        print MAIL "  Site : $inname\n";
        print MAIL "  URL  : $inurl\n";
        print MAIL "  Where: $location\n\n";
        print MAIL "Don't forget the admin center is at:\n\n";
        print MAIL "  $adminurl/admin.cgi\n\n";
        print MAIL "=====================================================================\n";
        print MAIL "Powered by Index Finger v4.101\n";
        close(MAIL);
        }
    }