File Coverage

blib/lib/Apache/Motd.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Apache::Motd;
2             ## FILE: Apache/Motd.pm
3             ## $Id: Motd.pm,v 1.2 2002/11/01 00:39:57 ramirezc Exp $
4              
5 1     1   656 use strict;
  1         2  
  1         37  
6 1     1   4 use vars qw($VERSION);
  1         2  
  1         52  
7 1     1   1369 use Apache;
  0            
  0            
8             use Apache::Cookie;
9             use Apache::Constants qw(:common REDIRECT);
10              
11             $VERSION = '1.00';
12              
13             sub handler {
14             my $r = shift;
15             my $uri = $r->uri;
16             my $cn = $r->dir_config('CookieName') || 'seenMOTD';
17             my $exp = $r->dir_config('ExpireCookie') || '+1d';
18             my $file = $r->dir_config('MessageFile') || 0;
19             my $cookieless = $r->dir_config('SupportCookieLess') || 1;
20             my $port = ($r->server->port eq '80') ? "" : ':'.$r->server->port;
21             my $tquery = $r->args;
22              
23             ## If the request is the part of the cookie test redirect, then
24             ## take out the ct=1 query_string value and make a note of it
25             ## by setting $ct_request
26             my $ct_request = 0;
27             if ($tquery =~ /ct=1/) {
28             $ct_request=1;
29             $tquery =~ s/ct=1//;
30             $r->args($tquery) if ($tquery =~ /=/);
31             }
32              
33             return OK unless $r->is_initial_req;
34              
35             ## MessageFile appears to be missing, pass onto next phase
36             return OK unless $file;
37             return OK unless -e $file;
38            
39             ## Look for cookie ($cn) and verify it's value
40             my $cookies = Apache::Cookie->new($r)->parse;
41             if (my $c = $cookies->{$cn}) {
42             my $cv = $c->value;
43             return OK if ($ct_request == 0 && $cv eq '1');
44             displayMotd($r);
45             return DONE;
46             }
47              
48             ## Prepare cookie information and add outgoing headers
49             my $cookie = Apache::Cookie->new($r,
50             -name => $cn,-value => '1',-expires => $exp );
51             $cookie->bake;
52              
53             ## Handle Cookieless clients
54             if ($cookieless) {
55             ## Apparently this client does not like cookies, pass it on to
56             ## next phase
57             $r->log_error("Apache::Motd::Bypassed by ".
58             $r->connection->remote_ip) if $ct_request;
59             return OK if $ct_request;
60              
61             my $host = $r->hostname;
62             my $ct_url = 'http://'.$host.$port.$uri.'?ct=1';
63             ## Test for client for cookie worthiness by redirecting client
64             ## to same $uri but along with the cookie testflag (ct=1) in the
65             ## query_string
66             $r->header_out("Location" => $ct_url);
67             return REDIRECT;
68             }
69              
70             displayMotd($r);
71             return DONE;
72             }
73              
74             sub displayMotd {
75             my $r = shift;
76             my $uri = $r->uri;
77             my $file = $r->dir_config('MessageFile');
78             my $sec = $r->dir_config('RedirectInSecs') || 10;
79              
80             ## Open motd file, otherwise server error
81             unless (open MSG,$file) {
82             $r->log_error("Apache::Motd::Error : Unable to load: $file");
83             return SERVER_ERROR;
84             }
85            
86             ## Slurp message $file into a string
87             my $msg = "";
88             {
89             local $/;
90             $msg = ;
91             }
92             close MSG;
93            
94             ## Substitute template variables
95             $msg =~ s//$uri/g;
96             $msg =~ s//$sec/g;
97              
98             ## Maintain a small logging trail
99             $r->log_error("Apache::Motd::Display URI: $uri ".$r->connection->remote_ip);
100              
101             my $headers = $r->headers_out;
102             $headers->{'Pragma'} = $headers->{'Cache-control'} = 'no-cache';
103              
104             ## straight form the mod_perl guide
105             $r->no_cache(1);
106             $r->send_http_header('text/html');
107             $r->print($msg);
108             }
109              
110             1;
111             __END__