#!/usr/bin/perl require 5.001; use strict; use Fcntl; use CGI; require "./cgi-lib.pl"; #----------------------------------------- # TODO: Replace with your own paths use constant UPLOAD_DIR => " TODO: Your Path"; use constant DEST_DIR => " TODO: Your Path"; use constant INDEX_FILE => " TODO: Your Path"; # TODO #----------------------------------------- my $q = new CGI; #----------------------------------------- MAIN: { print $q->header("text/html"); print "\n"; print "\n"; print "Map Index Update\n"; #----------------------------------------------------------- # TODO: Your URL print "\n"; # TODO: Your URL #----------------------------------------------------------- print "\n"; print "\n"; my ($name, $value, $action, $target, $src, $dest); foreach $name ($q->param) { foreach $value ($q->param($name)) { $action = $value; $target = $name; } } # Setup File Names $src = INDEX_FILE; $dest = DEST_DIR."maps.tmp"; # Create new tmp file from Maps.html - doing the indicated action if ($action eq 'DEL') { print "

Deleting selected entry...

\n"; UpdateIndexDelete ($src, $dest, $target); } else { print "

Saving selected entry...

\n"; UpdateIndexSave($src, $dest, $target); } print "

Done.

\n"; print "

Resetting Map Index...

\n"; # Remove Maps.html index my $delCommand = "rm -f ".$src; my $mvCommand = "mv ".$dest." ".$src; # remove old index my $sysret = system ("$delCommand"); if ($sysret) { &CgiError("While removing existing map index, system returned:$sysret. Errors: $!.\n"); } # mv temp file to index file $sysret = system ("$mvCommand"); if ($sysret) { &CgiError("While creating new map index, system returned:$sysret. Errors: $!.\n"); } print "

Done.

\n"; print "

Returning to Map Index....

\n"; print $q->end_html; } #------------------------------------------- # UpdateIndexDelete: removes the entry from # the maps.html index file #------------------------------------------- sub UpdateIndexDelete() { my ($src, $dest, $id) = @_; my $inputLine = ""; # open index file open (INDXFILE, "$src") or &CgiDie("Error: Unable to open temporary file for index update: $src: $!\n"); # open temp file sysopen (TMPOUT, $dest, O_RDWR|O_EXCL|O_CREAT ) or &CgiDie ("Error: Unable to open temporary file for index update: $dest: $!\n"); # read from index, write to temp while ($inputLine = ) { # get to comment if ($inputLine !~ m// ) { print TMPOUT $inputLine; } else { # Remove the whole entry by skipping it $inputLine = ; while ($inputLine !~ m##) { $inputLine = ; } } # continue / finish read from index, write to temp }; # close index file close (INDXFILE); # close temp file close (TMPOUT); #Remove the actual files my $delFile = DEST_DIR.$id; my $delCommand = "rm -f ".$delFile."*"; my $sysret = system ("$delCommand"); if ($sysret) { &CgiError("While removing existing map index, system returned:$sysret. Errors: $!.\n"); } } #------------------------------------------- # UpdateIndexSave: updates the maps.html index # removing the input tags #------------------------------------------- sub UpdateIndexSave() { my ($src, $dest, $id) = @_; my $inputLine = ""; # open index file open (INDXFILE, "$src") or &CgiDie("Error: Unable to open temporary file for index update: $src: $!\n"); # open temp file sysopen (TMPOUT, $dest, O_RDWR|O_EXCL|O_CREAT, 0777) or &CgiDie ("Error: Unable to open temporary file for index update: $dest: $!\n"); # read from index, write to temp while ($inputLine = ) { # get to comment if ($inputLine !~ m// ) { print TMPOUT $inputLine; } else { # Just remove the Input Tags while ($inputLine !~ m/; } } # continue / finish read from index, write to temp }; # close index file close (INDXFILE); # close temp file close (TMPOUT); }