package Apache::SimpleReplace;
#---------------------------------------------------------------------
#
# usage: PerlHandler Apache::SimpleReplace
# PerlSetVar TEMPLATE "/templates/templ.html"
# PerlSetVar REPLACE "|" # character or string
# defaults to "|"
# PerlSetVar Filter On # optional - will work
# within Apache::Filter
#---------------------------------------------------------------------
use 5.004;
use mod_perl 1.21;
use Apache::Constants qw( OK DECLINED SERVER_ERROR );
use Apache::File;
use Apache::Log;
use strict;
$Apache::SimpleReplace::VERSION = '0.06';
# set debug level
# 0 - messages at info or debug log levels
# 1 - verbose output at info or debug log levels
$Apache::SimpleReplace::DEBUG = 0;
sub handler {
#---------------------------------------------------------------------
# initialize request object and variables
#---------------------------------------------------------------------
my $r = shift;
my $filter = $r->dir_config('Filter') || undef;
$r->log->info("Using Apache::SimpleReplace");
# redefine $r as necessary for Apache::Filter 1.013 and above
if (lc($filter) eq 'on') {
$r->server->log->info("\tregistering handler with Apache::Filter")
if $Apache::SimpleReplace::DEBUG;
$r = $r->filter_register;
}
my $log = $r->server->log;
my $template = $r->dir_config('TEMPLATE');
my $replace = $r->dir_config('REPLACE') || "|";
#---------------------------------------------------------------------
# do some preliminary stuff...
#---------------------------------------------------------------------
unless ($r->content_type eq 'text/html') {
$log->info("\trequest is not for an html document - skipping...")
if $Apache::SimpleReplace::DEBUG;
$log->info("Exiting Apache::SimpleReplace");
return DECLINED;
}
#---------------------------------------------------------------------
# wrap the template around the requested file...
#---------------------------------------------------------------------
$log->info("\tlooking for \'$replace\' in template $template")
if $Apache::SimpleReplace::DEBUG;
# open the template handle
my $tph = Apache::File->new($template);
unless ($tph) {
$log->error("\tcannot open template! $!");
$log->info("Exiting Apache::SimpleReplace");
return SERVER_ERROR;
}
my ($rqh, $status);
# open the request handle
if ($filter) {
$log->info("\tgetting input from Apache::Filter")
if $Apache::SimpleReplace::DEBUG;
($rqh, $status) = $r->filter_input;
undef $rqh unless $status == OK; # just to be sure...
} else {
$log->info("\tgetting input from requested file")
if $Apache::SimpleReplace::DEBUG;
$rqh = Apache::File->new($r->filename);
}
unless ($rqh) {
$log->error("\tcannot open request! $!");
$log->info("Exiting Apache::SimpleReplace");
return SERVER_ERROR;
}
$r->send_http_header('text/html');
# send output
while (<$tph>) {
if (/\Q$replace/) {
$log->info("\t\'$replace\' found - replacing with request")
if $Apache::SimpleReplace::DEBUG;
my ($left, $right) = split /\Q$replace/;
print $left; # output the left side of substitution
$r->send_fd($rqh); # Apache::Filter > 1.013 overrides
# send_fd()
print $right; # ouptut the right side of substitution
}
else {
print; # print each template line
}
}
#---------------------------------------------------------------------
# wrap up...
#---------------------------------------------------------------------
$log->info("Exiting Apache::SimpleReplace");
return OK;
}
1;
__END__
=head1 NAME
Apache::SimpleReplace - a simple template framework
=head1 SYNOPSIS
httpd.conf:
the content goes here
some footers, modification dates, whatever...
httpd.conf: