#!/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 that 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"; # filename of data file my $datafile = "$datapath/$filename"; # full file path of data file my $backup = "$datafile.bak"; # full file path of backup file my $header = "header.html"; # header file my $footer = "footer.html"; # footer file #my $action = "http://servername/path/to/cfm/enter_action.cfm"; # where 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 locking? my $timeout = 10000; # time (sec*1000) before starting next submission my $delimit = '\|'; # datafile delimiter, in this 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 and 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 for those systems that don't know what to do with the file } else { # Cleanup unlink $header; # delete the header file, no error checking because it's not critical unlink $footer; # delete the footer file, ditto 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 (now empty) move($backup, $datafile); # Restore the original } } sub ReadFile{ my ($datafile, $FLock) = @_; open(DATA, "<$datafile") or die "Couldn't open $datafile for reading: $!\n"; flock (DATA, 2) if ($FLock); my @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, stopped $!"; flock(DATA, 2) if ($FLock); print DATA @data; close(DATA); } sub CreateForm{ # Create the header and footer files, then the frameset to contain them. my ($header, $footer, $line, $delimit, $timeout, $action) = @_; CreateHeader($header, $line, $timeout); CreateFooter($footer, $line, $delimit, $action); print $q->header(-expires=>"now"), "Autosubmit", $q->frameset({-rows=>"20%,80%"}, $q->frame({-name=>"header", -src=>"$header"}), $q->frame({-name=>"footer", -src=>"$footer"}) ), "" 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=>$title, -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"; } }