Hi,
I have a handler - we talked about it a few weeks ago, Templating Redux -
that mostly works. It takes any page request, and wraps it in a template
file while also adding some cookie-defined components to the template. One
of the problems it is having is when I try to insert a cgi script as
anything other than the requested page (as a toolbar for example), the
server tries to execute the content file, which is usually an html
file. If the content is a perl script, and the components (toolbar, etc)
are static, everything works peachily.
The wrapper follows. Code comments that could lead to my improvement as a
(mod_)perl programmer are always most welcome. If you need more
information, please ask.
thanks everyone!
Todd
package Carescout::Wrapper;
use strict;
use Apache::Constants qw(:common DONE);
use Apache::Log ();
use Apache::Request;
use FileHandle;
sub handler {
my ( $r ) = shift;
my ( $log ) = $r->log;
$log->error("Wrapper: Inside Wrapper.");
# this is just setting up a few directories to find the components in
my ( $template_directory ) = "/www/dbg/templates/";
my ( $components_directory ) = '/www/dbg/components/';
my ( $r_ary ) = &get_values();
my ( $template ) = $template_directory.@$r_ary->[0];
my ( $error ) = $components_directory.'error/'.'standard';
# components order is page top, toolbar, page bottom, content
my ( @components ) = ($components_directory.'head/'.@$r_ary->[1],
$components_directory.'tool/'.@$r_ary->[2],$components_directory.'feet/'.@$r_ary->[3],
$r->filename);
$r->send_http_header;
if ( -e $template ) {
my ( $template_fh ) = FileHandle->new();
open( $template_fh, "<$template" ) or die "Failed to open template
$temp
late: $!";
my ( $line );
while ( $line = $template_fh->getline) {
if ( $line =~ /(.*)<\[(.*)\]>(.*)/o ) {
my ( $before ) = $1;
my ( $component ) = $2;
my ( $after ) = $3;
my ( $name ) = $error;
print $before;
$name = $components[0] if $component eq 'head';
$name = $components[1] if $component eq 'tool';
$name = $components[2] if $component eq 'foot';
$name = $components[3] if $component eq 'cont';
my ( $title ) = &get_title( $r, $components[3] ) if
$component =
~ /^title/o;
my ( $meta ) = &get_meta( $r, $components[3] ) if
$component =~
/meta/o;
if ( $title ) {
print $title;
} elsif ( $meta ) {
if ( -e $meta ) {
my $fh = FileHandle->new();
open($fh, "<$meta" ) or die "Failed to open meta
file $m
eta: $!";
$r->send_fd($fh);
close($fh);
} else {
print $meta unless $meta == 1;
}
} else {
&print_component($r,$name);
}
print $after;
} else {
chop;
$r->print( $line );
}
}
close($template_fh );
}
$log->error("Wrapper: Exiting Wrapper.");
return DONE;
}
sub get_meta {
my ( $r ) = shift;
my ( $file ) = shift;
my ( $meta );
if ( -e $file ) {
my ( $fh ) = FileHandle->new();
open($fh, "<$file" ) or die "Failed to open meta file $file: $!";
my ( $line );
while ( $line = $fh->getline) {
if ( $line =~ /<\[meta:(.*)\]>/o ) {
$meta = $1;
last;
}
last if $meta;
}
close($fh);
} else {
$meta = 0;
}
return $meta || 1;
}
sub get_title {
my ( $r ) = shift;
my ( $file ) = shift;
my ( $title );
if ( -e $file ) {
my ( $fh ) = FileHandle->new();
open($fh, "<$file" ) or die "Failed to open content file $file: $!";
my ( $line );
while ( $line = $fh->getline) {
if ( $line =~ /<\[title:(.*)\]>/o ) {
$title = $1;
last;
}
last if $title;
}
close($fh);
} else {
$title = "File Not Found.";
}
return $title;
}
sub print_component {
my ($r) = shift;
my ($file) = shift;
my ( $log ) = $r->log;
if ( -e $file ) {
if($file =~ /\.(?:cgi|pl)$/o) {
$log->error("Wrapper: $file is a script");
my $apr = Apache::Request->new($r);
my $status = $apr->parse;
unless ($status == OK) {
$apr->custom_response($status,$apr->notes("error-notes"));
return $status;
}
my @params = $apr->param;
my ( $args );
if ( @params ) {
for ( @params ) {
$args .= $_."=".$apr->param($_)."&";
}
}
my ( $uri ) = $r->uri;
$uri = $uri .'?'.$args if $args;
my $subr = $r->lookup_uri($uri);
$subr->handler('cgi-script');
$subr->run();
} else {
$log->error("Wrapper: $file static");
my $fh = FileHandle->new();
open($fh, "<$file" ) or die "Failed to open content file
$file: $!";
$r->send_fd($fh);
close($fh);
}
} else { print "404 File Not Found. : $file\n"; }
}
sub get_values {
use CGI::Cookie;
my ( %cookies, %hash, @ary );
%cookies = fetch CGI::Cookie;
eval { my ( %hash ) = %cookies->{'preferences'}->value; };
if ( ! $@ ) {
@ary = ( %hash->{'temp'}, %hash->{'head'}, %hash->{'tool'},
%hash->{'foo
t'} );
} else {
# Order of elements is template, header, toolbar, footer
@ary = ( 'carescout', 'carescout', 'carescout.pl', 'carescout' );
}
return \@ary;
}
1;
__END__
A template looks like this:
<html>
<head>
<title><[title]></title>
<[meta]>
</head>
<body bgcolor="#FFFFFF">
<table border="0" cellpadding="0" cellspacing="0" width="630">
<tr><td colspan="3"><[head]></td></tr>
<tr>
<td valign="top" width="145"><[tool]></td><td valign="top" width="24"></td>
<td valign="top" width="461" align="left"><[cont]></td></tr>
</table>
<table border="0"><tr><td align="center"><[foot]></td></tr></table>
</body>
</html>