#!/usr/local/bin/perl # $Id: admin.pl,v 1.1.1.1 2000/06/15 19:56:30 kayos Exp $ BEGIN { #-------------------------------------------------------------------- # configuration # if you are having errors, make sure the first line of this program # has the correct path to the correct version of perl. Also, # try uncommenting the below line and making sure it is the path # to the directory containing this program, data directory, and # modules # #$base_dir = "/path/to/cgi-bin/template/"; #---------------------------------- # try to determine what directory the script is in # if(! $base_dir) { $base_dir = $0; $base_dir =~ s/\\/\//g; #substitute NT \ for / if(rindex($base_dir,"/") > 0) { $base_dir = substr($base_dir,0,rindex($base_dir,"/")); } else { $base_dir = '.'; } } $base_dir =~ s/\/$//; chdir($base_dir) or die "Cannot change directory to $base_dir: $!"; } #-------------------------------------------------------------------- # modules and pragmas # pragmas # standard modules use CGI; use CGI::Carp qw(fatalsToBrowser); use Data::Dumper; # non-standard modules use Parse::ePerl; # declare global vars, each can be called from other packages like: # $main::base_dir or $main::cgi->url() # use vars qw($base_dir $cgi), # config vars qw($url $redirect $action), # state vars qw($config); #-------------------------------------------------------------------- # configuration my $configfile = "config.pl"; my %config_vars = ( default => { output => 'output/default.tmpl', input => 'input/default.tmpl' } ); if( -f $configfile && ! do $configfile ) { if( $! ) { croak <<" EOF"; $! This program can't find or access $configfile in $base_dir. You might have to explicitly set \$base_dir at the top of $0 and make sure that $configfile is in that directory. EOF } elsif( $@ ) { croak <<" EOF"; $@ There are errors in $configfile. Check over the file carefully and correct any syntax errors. EOF } } for my $var ( keys %config_vars ) { if(! defined $config->{$var}) { $config->{$var} = $config_vars{$var}; } } #-------------------------------------------------------------------- # main: initialize, branch to handler, cleanup local $|=1; # turn off buffering for STDOUT # grab and parse CGI params and environment vars # my $cgi = new CGI; # what's my url? # $url = $cgi->url(); # if a called action isn't a function that returns a page, then # do the action, then redirect # $redirect = $cgi->param('redirect') || $cgi->referer() || $cgi->url(); $cgi->delete('redirect'); # if the only parameter is an action, then you can, optionally, pass just # the action word as a query_string like "http://url?logout" instead # of "http://url?action=logout" # $action = $cgi->param('action') || $cgi->param('keywords') || ""; $cgi->delete('action'); $cgi->delete('keywords'); # branch to handler functions # $_ = $action; if ( /edit/i && $cgi->param('file') ) { edit(); } elsif ( /save/i && $cgi->param('file') ) { save(); } elsif ( $cgi->param('file') ) { load(); } else { list(); } exit; #-------------------------------------------------------------------- # handler functions sub list { # give them a choice of files to edit print $cgi->header(); print <<" EOF";
EOF for my $file ( keys %{$config} ) { if( $file ne 'default' ) { my $description = $config->{$file}{'name'} || $config->{'default'}{'name'}; print <<" EOF"; EOF } } print <<" EOF";
$description
EOF } sub edit { local $::file; $file = $cgi->param('file'); # load the input template my $input; { local $/; my $inputfile = $config->{$file}{'input'} || $config->{'default'}{'input'}; open(INPUT,$inputfile) or error("Can't open $inputfile: $!"); $input = ; close(INPUT); } # load the template data my $data; { local $/; my $datafile = $config->{$file}{'file'} || $config->{'default'}{'file'}; if( open(DATA,$datafile) ) { $data = ; close(DATA); } else { $data = ''; } } my $result; my $error; Parse::ePerl::Translate({ Script => $input, Result => \$result, }) or error( "Couldn't parse the template." ); Parse::ePerl::Evaluate({ Script => 'no strict; '.$data.$result, Result => \$result, Error => \$error }) or error( $error ); print $cgi->header(); print $result; } sub save { my $file = $cgi->param('file'); $cgi->delete('file'); my $filename = $config->{$file}{'file'} || $config->{'default'}{'file'}; open(FILE,">$filename") or error($!); for my $field ( $cgi->param() ) { print FILE Data::Dumper->Dump([$cgi->param($field)],[$field]); print FILE "\n"; } print FILE "1;\n"; close(FILE); confirmation(); } sub load { my $file = $cgi->param('file'); # load the output template my $output; { local $/; my $outputfile = $config->{$file}{'output'} || $config->{'default'}{'output'}; open(OUTPUT,$outputfile) or error("Can't open $outputfile: $!"); $output = ; close(OUTPUT); } # load the template data my $data; { local $/; my $datafile = $config->{$file}{'file'} || $config->{'default'}{'file'}; if( open(DATA,$datafile) ) { $data = ; close(DATA); } else { $data = ''; } } my $result; my $error; Parse::ePerl::Translate({ Script => $output, Result => \$result, }) or error( $result ); Parse::ePerl::Evaluate({ Script => 'no strict;'.$data.$result, Result => \$result, Error => \$error }) or error( $error ); print $cgi->header(); print $result; } #-------------------------------------------------------------------- sub confirmation { print $cgi->header(); print <<" EOF";

The file was successfully saved!

Click here to edit more files

EOF } sub encode { my $body = shift; $body =~ s/(\015\012|\015|\012)/\n/gs; $body =~ s/\n\n/

/gs; $body =~ s/\n/
/gs; $body =~ s/
/
\n/gis; $body =~ s/

/\n

/gis; return $body; } sub decode { my $body = shift; $body =~ s/(\015|\012)//gs; $body =~ s/
/\n/gis; $body =~ s/

/\n\n/gis; return $body; } sub error { my $error = shift; die $error; }