You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Carlos Ramirez <cr...@gte.net> on 2000/07/13 07:07:08 UTC

[New Module] Apache::Motd

I wrote an Apache Perl Module that I think can be of use to others
who maintain webservers and once in a while have to notify web users
with an urgent message. This module is currently being on the servers
I maintain.

The proposed module name is Apache::Motd (Message of the Day). It
basically mimics the functionality of the motd on UNIX systems.  If this
feature already exist, then I guess we can ignore this one, otherwise
I can upload to CPAN.

This module allows you to display a message before the visitor enters
your site without the need to modify webpages or web application
code/logic. The message can be a "Server Going Down in So many hours", 
"Terms of Use", "News Flashes",etc. Once the visitor gets the message, 
a cookie is set and the visitor is redirected to their original URI 
after N seconds. 

Comments/Suggestions??

Thanks,

-Carlos

Here is the code and POD.


package Apache::Motd;

use strict;
use vars qw($VERSION);

$VERSION = '0.01';

use Apache;
use Apache::Cookie;
use Apache::Constants qw(:common);

sub handler {
    my $r    = shift;
    my $uri  = $r->uri;
    my $cn   = $r->dir_config('CookieName')     || 'seenMOTD';
    my $file = $r->dir_config('MessageFile')    || 0;
    my $exp  = $r->dir_config('ExpireCookie')   || '+1d';
    my $sec  = $r->dir_config('RedirectInSecs') || 10;

    unless ($file) {
       $r->log_error("Apache::Motd::Error : No Message File
Specified!");
       return SERVER_ERROR;
    }

    return OK unless $r->is_initial_req;

    ## Lookup cookie state
    my $cookies = Apache::Cookie->new($r)->parse;
    if (my $c   = $cookies->{$cn}) {
        my $cv  = $c->value;

        return OK if $cv;
    }

    ## Apparently this is your first visit
    ## Prepare cookie information
    my $cookie = Apache::Cookie->new($r,
                         -name => $cn,-value => '1',-expires => $exp );

    $cookie->bake;

    ## Print message $file or return custom server error
    unless (open MSG,$file) {
       $r->log_error("Apache::Motd::Error : Unable to load message:
$file");
       return SERVER_ERROR;
    }

    ## Slurp message $file
    my $msg = "";
    {
      local $/;
      $msg = <MSG>;
    }
    close MSG;

    $msg =~ s/<VAR_URI>/$uri/g;
    $msg =~ s/<VAR_REDIRECT>/$sec/;

    $r->send_http_header('text/html');
    $r->print($msg);

    return DONE;
}

1;
__END__

=head1 NAME

      Apache::Motd - Mimic motd (Message of the Day) on a webserver

=head1 SYNOPSIS

      httpd.conf: (in Server, <Location>, <Directory>, <Files>)

      PerlPostHeaderParserHandler Apache::Motd
      PerlSetVar      MessageFile /path/to/message/file

 ---optional (if you are satisified with the defaults)---

      PerlSetVar      RedirectInSecs N
      PerlSetVar      CookieName
      PerlSetVar      ExpireCookie


=head1 DESCRIPTION

      Apache::Motd allows you to force/display a message before the
      visitor enters your site without the need to modify webpages or
      web application code/logic. The message can be a "Server Going 
      Down in So many hours", "Terms of Use", "News Flashes",etc. 
      Once the visitor gets the message, a cookie is set and the visitor 
      is redirected to their original URI after N seconds.

      All variables except MessageFile are optional.

      RedirectInSecs    - the number of secs to wait before
                          redirecting to original URI 
                          [Default: 10]
      CookieName        - the name of the cookie, value is set to 1
                          [Default: seenMOTD]
      ExpireCookie      - exipration time of cookie 
                          [Default: +1d i.e. one day]
      MessageFile       - filesystem path to the file** containing the 
                          message

      **file: the file *should* be an html file with the following
        requirements.
      1. The message file should include a meta tag that handles the
         redirect like so:
         <a meta http-equiv="Refresh"
content="<VAR_REDIRECT>;url=<URI>">

         where the <VAR_REDIRECT> will be replaced by the value of
         the variable RedirectInSecs and <VAR_URI> with the original
URI.
         Ommiting the meta tag will prevent the user from being
redirected.

         <VAR_URI> can also be used as  a "click here to prceed" link,
if 
         the user chooses not to wait.
         Example: <a href="<URI>">click to proceed</a>

         See sample provided: motd.txt


=head1 REQUIREMENTS

        Apache::Cookie
        But I can (might) illiminate this requirement if (when) I
        implement my own parse incoming header and look for CookieName
routine.

=head1 SEE ALSO

        perl(1), mod_perl(1), Apache(3)

=head1 AUTHOR

        Carlos Ramirez <ca...@quantumfx.com>

=head1 COPYRIGHT

        Copyright 2000 Carlos Ramirez - all rights reserved.

        This library is free software; you can redistribute it and/or
        modify it under the same terms as Perl itself.

 =cut