http://qs321.pair.com?node_id=163292
Category: CGI Programming
Author/Contact Info Matthew Musgrove
muskrat@mindless.com
Description:

Okay, this was created as a custom script. It is designed to read lines from a data file and "auto-submit" the data in the line to a cold fusion app. It works as advertised when run as a CGI script.

When run as from the command line, the data cannot be sent to the cold fusion application. You will see the line read, and the extracted fields. It will cycle through until there is no more data.

You may now use, modify, distribute, trash and generally do whatever you want with this software. My apologies to the company that paid for the last version of this. (Wow! Was it really almost two years ago?) You will not be reimbursed as it worked as promised and saved untold thousands of man-hours.

Versions prior to 3.0 used here documents to produce the HTML.

Updated: new version! 2002-05-08 12:30 (GMT -0600)

Updated: fixed email address 2002-05-07 13:21 (GMT -0600)

#!/usr/bin/perl

# Autosubmit version 3.3
# Last updated: 2002-05-07 22:25 (GMT -0600)
# Copyright, 2000 - present, Matthew Musgrove, muskrat@mindless.com

use strict;
use warnings;
use File::Copy;
use CGI;
use Text::ParseWords;

my $online;
$online = 1 if (defined $ENV{"REMOTE_ADDR"});            # assumes tha
+t the REMOTE_ADDR environment variable isn't set unless run as a CGI
our $q = new CGI;                        # so I'm lazy...
my $datapath = ".";                        # data path
my $filename = "data.txt";                                   # filenam
+e of data file
my $datafile = "$datapath/$filename";                # full file path 
+of data file
my $backup = "$datafile.bak";                    # full file path of b
+ackup file
my $header = "header.html";                    # header file
my $footer = "footer.html";                    # footer file
#my $action = "http://servername/path/to/cfm/enter_action.cfm";    # w
+here to submit the form, in this case a cold fusion app
my $action = "test.cgi";    # where to submit the form, in this case a
+ test script that spits out what you submit
my $FLock = 0;                                          # use file loc
+king?
my $timeout = 10000;                        # time (sec*1000) before s
+tarting next submission
my $delimit = '\|';                        # datafile delimiter, in th
+is case the pipe symbol

copy($datafile, $backup) if (! -e $backup);
my @data = ReadFile($datafile, $FLock);                # Read the data
+ from the file and return it sorted
my $size = @data;                        # Size of the array?
my $line;                            # One line from the array
if ($size > 0) {                        # At least one line in array
    @data = RemoveBlanks(@data);                # Return the non-blank
+ lines
    ($line, @data) = GetLine(@data);            # Get the last line an
+d the shorter array
    UpdateFile($datafile, $FLock, @data);                # Update the 
+datafile with the new data array
    CreateForm($header, $footer, $line, $delimit, $timeout, $action); 
+# create the frameset and turn it loose!
    exec("perl $0");                    # execute myself! using perl f
+or those systems that don't know what to do with the file
} else {                            # Cleanup
    unlink $header;                        # delete the header file, n
+o error checking because it's not critical
    unlink $footer;                        # delete the footer file, d
+itto
    if ($online) {
        print $q->header,
            $q->start_html('Job Finished'),
            $q->p,
                "Back to the ",
                $q->a({-href=>'index.html'}, 'index'),
                ".",
            $q->end_html();
    } else {
        print "Job Finished.\n";
        unlink $datafile;                    # delete the original (no
+w empty)
        move($backup, $datafile);                # Restore the origina
+l
    }
}

sub ReadFile{
    my ($datafile, $FLock) = @_;
    open(DATA, "<$datafile") or die "Couldn't open $datafile for readi
+ng: $!\n";
    flock (DATA, 2) if ($FLock);
    my @data = <DATA>;
    close(DATA);
    return sort @data;
}

sub RemoveBlanks{
    my @data;
    foreach (@_){
        push(@data, $_) unless /^\s+$/;
    }
    return @data;
}

sub GetLine{
    my (@data) = @_;
    my $line = pop(@data);
    return $line, @data;
}

sub UpdateFile{
    my ($datafile, $FLock, @data) = @_;
    open(DATA, ">$datafile") || die "Unable to update the datafile, st
+opped $!";
    flock(DATA, 2) if ($FLock);
    print DATA @data;
    close(DATA);
}

sub CreateForm{
# Create the header and footer files, then the frameset to contain the
+m.
    my ($header, $footer, $line, $delimit, $timeout, $action) = @_;
    CreateHeader($header, $line, $timeout);
    CreateFooter($footer, $line, $delimit, $action);
    print     $q->header(-expires=>"now"),
        "<html><head><meta name='pragma' content='no-cache' /><title>A
+utosubmit</title></head>",
        $q->frameset({-rows=>"20%,80%"},
            $q->frame({-name=>"header", -src=>"$header"}),
            $q->frame({-name=>"footer", -src=>"$footer"})
        ),
        "</html>" if ($online);
    
}

sub CreateHeader{
# Create the header file.  It uses javascript to restart this web app.
    my ($header, $line, $timeout) = @_;
    if ($online) {
        open(HTML, ">$header");
        print HTML $q->start_html(
                -script=>{-language=>'JavaScript',
                                 -code=>'function DoIt(){    document.
+auto.submit();        }'},
                            -title=>'AutoSubmit',
                            -head=>$q->meta({-name => 'pragma',
                                        -content    => 'no-cache'}),
                             -onLoad=>"setTimeout('DoIt()', $timeout)"
+, ),
            $q->start_form({-action=>"autosubmit.cgi",
                -method=>"post",
                -name=>"auto",
                -target=>"_parent"}),
            $q->endform(),
            $q->p("Line to process: $line"),
            $q->p($q->big('Wait for the page that says "Job Finished."
+')),
            $q->end_html();
        close(HTML);
    } else {
        print "Line to process: $line\n";
    }
}

sub CreateFooter {
# Create the footer.  It uses javascript to autosubmit the form to the
+ cold fusion app.
    my ($footer, $line, $delimit, $action) = @_;
    my ($ID, $title, $cat, $desc) = parse_line($delimit, 0, $line);  #
+ parses the line and puts the section into variables
    if ($online) {
        open(HTML, ">$footer");
        print HTML $q->start_html(
                -script=>{-language=>'JavaScript',
                                 -code=>'function DoIt(){    document.
+EnterForm.submit();        }'},
                            -title=>'AutoSubmit',
                        -head=>$q->meta({-name => 'pragma',
                    -content    => 'no-cache'}),
                            -onLoad=>"DoIt()", ),
            $q->start_form({-action=>"$action",
                -method=>"post",
                -name=>"EnterForm"}),
            $q->table(),
                $q->Tr(
                    $q->td($q->b('File ID')),
                    $q->td($q->textfield(-name=>"file_id", -value=>$ID
+, -maxlength=>11)),
                ),
                $q->Tr(
                    $q->td($q->b('Title')),
                    $q->td($q->textfield(-name=>"title", -value=>$titl
+e, -size=>50)),
                ),
                $q->Tr(
                    $q->td($q->b('Category')),
                    $q->td($q->textfield(-name=>"category1", -value=>$
+cat, -size=>50)),
                ),
                $q->Tr(
                    $q->td($q->b('Description')),
                    $q->td($q->textarea(-name=>"description", -cols=>"
+20", -rows=>"3", -default=>$desc)),
                ),
                $q->Tr(
                    $q->td($q->b('Convert to')),
                    $q->td($q->radio_group(-name=>'Convert', -values=>
+['HTML','PDF','No'], -default=>'No', -linebreak=>'true')),
                ),
                $q->Tr(
                    $q->td($q->b('Private')),
                    $q->td($q->checkbox(-name=>'chkPrivate', -label=>'
+Check here if this is a private item')),
                ),
                $q->Tr(
                    $q->td($q->b('Site ID')),
                    $q->td($q->checkbox_group(-name=>'site_id', -value
+=>['Department', 'Practice Group', 'Office'], -linebreak=>'true')),
                ),
            $q->submit,
            $q->br,
            $q->end_form(),
            $q->end_table(),
            $q->end_html();
        close(HTML);
    } else {
        print "ID = $ID\nTitle = $title\nCategory = $cat\nDescription 
+= $desc\n";
    }
}