#file:MyApache/EmailObfuscate.pm #-------------------------------- # # This filter finds instances of an email address in HTML files # and replaces them with an ISO-obfuscated version. # # Note: you will need to replace the dummy "me@here.com" # email address (and the obfuscated version) with your own. # package MyApache::EmailObfuscate; use strict; use warnings; use Apache::Filter (); use Apache::RequestRec (); use APR::Table (); use Apache::Const -compile => qw(OK); use constant BUFF_LEN => 1024; sub handler { my $f = shift; my $one_line_string; # f->ctx is a way of passing arguments from one invocation # of this handler to the next. Pretty much any perl # data structure can be passed this way. Here we're going # to be passing the $leftover string, which will have the # leftover string that was a truncated line at the end of # our 1024 byte buffer. # We will unset the content_length, becaseu we are changing # it below. But we only need to unset it once for this page. # unless ($f->ctx) { $f->r->headers_out->unset('Content-Length'); } my $email_string = 'me@here.com'; my $leftover = $f->ctx; while ($f->read(my $buffer, BUFF_LEN)) { $buffer = $leftover . $buffer if defined $leftover; $leftover = undef; while ($buffer =~ /([^\r\n]*)([\r\n]*)/g) { # This will find all the \n-terminated lines $leftover = $1, last unless $2; $one_line_string = $1; $one_line_string =~ s/(.*)me\@here\.com/$1${email_string}/s; if ($2) { $f->print($one_line_string, $2); } else { $f->print($one_line_string); } } } # Done with loop. Now if this is the end of the file then we just output # the rest, otherwise we send the leftover bit to the next invocation. if ($f->seen_eos) { $f->print(scalar $leftover) if defined $leftover; } else { $f->ctx($leftover) if defined $leftover; } return Apache::OK; } 1;