| 1 | # Apache::ErrorControl | ||
| 2 | # | ||
| 3 | # description: Apache Error Templating Engine | ||
| 4 | # | ||
| 5 | # author: DJ <dj@boxen.net> | ||
| 6 | # | ||
| 7 | # $Id: ErrorControl.pm,v 1.26 2004/09/09 04:02:26 dj Exp $ | ||
| 8 | |||
| 9 | package Apache::ErrorControl; | ||
| 10 | |||
| 11 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | # BEGIN BLOCK {{{ | ||
| 15 | BEGIN { | ||
| 16 | ## Modules | ||
| 17 | 1 | ▶ | use HTML::Template::Set; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 0 | ▶ | use Apache::Constants qw(:common); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | use Apache::File (); | ||
| 20 | use Apache::Request; | ||
| 21 | use Class::Date; | ||
| 22 | use MIME::Entity; | ||
| 23 | |||
| 24 | ## Constants | ||
| 25 | use constant TRUE => 1; | ||
| 26 | use constant FALSE => 0; | ||
| 27 | |||
| 28 | ## Variables | ||
| 29 | use vars (qw($VERSION)); | ||
| 30 | |||
| 31 | $VERSION = do {my @r=(q$Revision: 1.26 $=~/\d+/g); sprintf "%d."."%03d"x$#r,@r}; | ||
| 32 | } | ||
| 33 | # }}} | ||
| 34 | |||
| 35 | |||
| 36 | # Handler Function {{{ | ||
| 37 | sub handler { | ||
| 38 | my $r = shift; | ||
| 39 | my $self = bless({}, __PACKAGE__); | ||
| 40 | |||
| 41 | # Set Apache::ErrorControl as a subprocess_env/prev->subprocess_env/%ENV | ||
| 42 | $r->subprocess_env('APACHE_ERROR_CONTROL', $VERSION); | ||
| 43 | if ($r->prev()) { | ||
| 44 | $r->prev()->subprocess_env('APACHE_ERROR_CONTROL', $VERSION); | ||
| 45 | } | ||
| 46 | $ENV{'APACHE_ERROR_CONTROL'} = $VERSION; | ||
| 47 | |||
| 48 | |||
| 49 | # Define Variables {{{ | ||
| 50 | my $file = $r->filename; | ||
| 51 | my $c = $r->connection; | ||
| 52 | my $s = $r->server; | ||
| 53 | |||
| 54 | # check for test mode/hardcoded mode | ||
| 55 | if ($r->uri() =~ /\/(\d+)[\/]?$/) { | ||
| 56 | $self->{error_code} = $1; | ||
| 57 | } else { | ||
| 58 | unless ($r->prev()) { | ||
| 59 | # return FORBIDDEN if the handler is being accessed directly | ||
| 60 | return FORBIDDEN; | ||
| 61 | } else { | ||
| 62 | # return FORBIDDEN if the handler for some reason has OK for the | ||
| 63 | # prev->status | ||
| 64 | $self->{error_code} = $r->prev()->status(); | ||
| 65 | |||
| 66 | return FORBIDDEN if ($self->{error_code} == OK); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | $self->{document_root} = $r->document_root(); | ||
| 71 | |||
| 72 | unless (exists $self->{error_code} and $self->{error_code}) { | ||
| 73 | die "Unable to find Error Code, very odd!\n"; | ||
| 74 | } | ||
| 75 | |||
| 76 | $self->{template_dir} = $r->dir_config("TemplateDir"); | ||
| 77 | $self->{default_template} = $r->dir_config("DefaultTemplate"); | ||
| 78 | |||
| 79 | my $template = $self->find_error_template(); | ||
| 80 | |||
| 81 | unless (defined $template and -f $template) { | ||
| 82 | die "Unable to find DefaultTemplate or derrived template!\n"; | ||
| 83 | } | ||
| 84 | |||
| 85 | # try and derrive the MTA Program and setup the email_on hash | ||
| 86 | my $MTA_Prog; | ||
| 87 | my %email_on = (); | ||
| 88 | my $disable_email = $r->dir_config("DisableEmail"); | ||
| 89 | unless ($disable_email) { | ||
| 90 | $MTA_Prog = $r->dir_config("MTA"); | ||
| 91 | if ($MTA_Prog) { | ||
| 92 | # bah, how else do i check -f when its got a -t or something? | ||
| 93 | my ($path) = split(/\s+/, $MTA_Prog); | ||
| 94 | unless (defined $path and -f $path) { | ||
| 95 | undef($MTA_Prog); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | unless ($MTA_Prog) { | ||
| 99 | if (-f "/var/qmail/bin/qmail-inject") { | ||
| 100 | $MTA_Prog = "/var/qmail/bin/qmail-inject"; | ||
| 101 | } elsif (-f "/usr/sbin/sendmail") { | ||
| 102 | $MTA_Prog = "/usr/sbin/sendmail -t"; | ||
| 103 | } elsif (-f "/usr/lib/sendmail") { | ||
| 104 | $MTA_Prog = "/usr/lib/sendmail -t"; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | # build the email_on hash, defaulting to 500 | ||
| 108 | my $have_email_on = FALSE; | ||
| 109 | my @email_on = $r->dir_config()->get("EmailOn"); | ||
| 110 | if (@email_on) { | ||
| 111 | foreach my $ec (@email_on) { | ||
| 112 | next unless (defined $ec and $ec); | ||
| 113 | $email_on{$ec} = TRUE; | ||
| 114 | $have_email_on = TRUE; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | unless ($have_email_on) { | ||
| 118 | # default to email_on Internal Server Error's | ||
| 119 | $email_on{'500'} = TRUE; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | my $date_format = $r->dir_config("DateFormat"); | ||
| 124 | # }}} | ||
| 125 | |||
| 126 | |||
| 127 | # Load Template {{{ | ||
| 128 | my %tmpl_args = (); | ||
| 129 | if ($self->{template_dir}) { | ||
| 130 | $tmpl_args{path} = $self->{template_dir}; | ||
| 131 | } | ||
| 132 | |||
| 133 | my $tmpl = new HTML::Template::Set( | ||
| 134 | %tmpl_args, | ||
| 135 | filename => $template, | ||
| 136 | cache => TRUE, | ||
| 137 | # debug => TRUE, | ||
| 138 | associate_env => TRUE | ||
| 139 | ); | ||
| 140 | # }}} | ||
| 141 | |||
| 142 | |||
| 143 | # Setup Template Params {{{ | ||
| 144 | |||
| 145 | # build a hash of the params, so we dont try and set anything that doesnt | ||
| 146 | # exist in the template | ||
| 147 | my %params; | ||
| 148 | map { $params{$_} = TRUE } $tmpl->param(); | ||
| 149 | |||
| 150 | # build a list of emails to send emails to, unless DisableEmail is turned on, | ||
| 151 | # there isnt an EmailOn for this error code or the MTA_Prog is not defined. | ||
| 152 | my (@email) = (); | ||
| 153 | unless ($disable_email) { | ||
| 154 | if (exists $email_on{$self->{error_code}} and $MTA_Prog) { | ||
| 155 | my @email_to = $r->dir_config()->get('EmailTo'); | ||
| 156 | if (@email_to) { | ||
| 157 | # push the emails specified in EmailTo | ||
| 158 | foreach my $email (@email_to) { | ||
| 159 | next unless ($email); | ||
| 160 | push(@email, $email); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | my $email_server_admin = $r->dir_config('EmailServerAdmin'); | ||
| 164 | if ($email_server_admin) { | ||
| 165 | # default to adding the server_admin to the @email if there are no | ||
| 166 | # EmailOn's or EmailServerAdmin is on | ||
| 167 | my $server_admin = $s->server_admin(); | ||
| 168 | unless (grep(/^\Q$server_admin\E$/, @email)) { | ||
| 169 | push(@email, $server_admin); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | if (exists $params{'webmaster_email'}) { | ||
| 173 | # if a webmaster_email is specified add it to the @email, unless it | ||
| 174 | # already exists in the array | ||
| 175 | my $webmaster_email = $tmpl->param('webmaster_email'); | ||
| 176 | |||
| 177 | # lets not add the webmaster_email if its in EmailTo | ||
| 178 | unless (grep(/^\Q$webmaster_email\E$/, @email)) { | ||
| 179 | push(@email, $webmaster_email); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | my $notes = ($r->prev()) ? $r->prev()->notes() : undef; | ||
| 186 | |||
| 187 | # set the current error_code's TMPL_IF on (if the TMPL_IF exists) | ||
| 188 | # i.e. <TMPL_IF NAME="404"> | ||
| 189 | if (exists $params{$self->{error_code}}) { | ||
| 190 | $tmpl->param( $self->{error_code} => TRUE ); | ||
| 191 | } elsif (exists $params{'unknown_error'}) { | ||
| 192 | $tmpl->param( unknown_error => TRUE ); | ||
| 193 | } | ||
| 194 | # set the error_code TMPL_VAR | ||
| 195 | # i.e. <TMPL_VAR NAME="error_code"> (which is substituted with 404) | ||
| 196 | if (exists $params{error_code}) { | ||
| 197 | $tmpl->param( error_code => $self->{error_code} ); | ||
| 198 | } | ||
| 199 | # set the error_note if its defined | ||
| 200 | if (exists $params{'error_notes'} | ||
| 201 | and $notes and exists $notes->{'error-notes'}) { | ||
| 202 | $tmpl->param( error_notes => $notes->{'error-notes'} ); | ||
| 203 | } | ||
| 204 | |||
| 205 | # load the 'date_format' from the template if its set | ||
| 206 | if (exists $params{date_format}) { | ||
| 207 | $date_format = $tmpl->param('date_format'); | ||
| 208 | } | ||
| 209 | |||
| 210 | # make the date string (formatted or default) | ||
| 211 | my $formatted_date; | ||
| 212 | if (exists $params{date}) { | ||
| 213 | my $date = new Class::Date(time); | ||
| 214 | if ($date_format) { | ||
| 215 | $formatted_date = $date->strftime($date_format); | ||
| 216 | } else { | ||
| 217 | $formatted_date = $date->string(); | ||
| 218 | } | ||
| 219 | $tmpl->param( date => $formatted_date ); | ||
| 220 | } | ||
| 221 | |||
| 222 | # build the 'requestor' TMPL_VAR (made up from the remote host/ip) | ||
| 223 | my $requestor; | ||
| 224 | my $remote_host = $c->remote_host(); | ||
| 225 | my $remote_ip = $c->remote_ip(); | ||
| 226 | if ($remote_host and $remote_ip) { | ||
| 227 | $requestor = $remote_host. ' ('. $remote_ip. ')'; | ||
| 228 | } elsif ($remote_ip) { | ||
| 229 | $requestor = $remote_ip; | ||
| 230 | } else { | ||
| 231 | $requestor = 'unknown'; | ||
| 232 | } | ||
| 233 | |||
| 234 | # use the r->user in the requestor if its available | ||
| 235 | if ($c->user()) { | ||
| 236 | $requestor = $c->user(). ' ('. $requestor. ')'; | ||
| 237 | } | ||
| 238 | |||
| 239 | if (exists $params{requestor}) { | ||
| 240 | $tmpl->param( requestor => $requestor ); | ||
| 241 | } | ||
| 242 | |||
| 243 | # build the 'base_url' TMPL_VAR (made up from the server_name etc) | ||
| 244 | my $base_url; | ||
| 245 | if (exists $ENV{'HTTPS'} and $ENV{'HTTPS'}) { | ||
| 246 | $base_url = 'https://'; | ||
| 247 | } else { | ||
| 248 | $base_url = 'http://'; | ||
| 249 | } | ||
| 250 | $base_url .= $s->server_hostname(); | ||
| 251 | |||
| 252 | if (exists $params{base_url}) { | ||
| 253 | $tmpl->param( base_url => $base_url ); | ||
| 254 | } | ||
| 255 | |||
| 256 | # build the 'request_url' TMPL_VAR (made up from the base_urlm | ||
| 257 | # the url and the args) | ||
| 258 | my $request_url = $base_url; | ||
| 259 | if ($r->prev()) { | ||
| 260 | $request_url .= $r->prev()->uri(); | ||
| 261 | if ($r->prev()->args()) { | ||
| 262 | $request_url .= '?'. $r->prev()->args(); | ||
| 263 | } | ||
| 264 | } else { | ||
| 265 | $request_url .= $r->uri(); | ||
| 266 | if ($r->args()) { | ||
| 267 | $request_url .= '?'. $r->args(); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | if (exists $params{request_url}) { | ||
| 272 | $tmpl->param( request_url => $request_url ); | ||
| 273 | } | ||
| 274 | # }}} | ||
| 275 | |||
| 276 | |||
| 277 | # Send Email For Internal Server Error {{{ | ||
| 278 | if (@email > 0) { | ||
| 279 | # build our email | ||
| 280 | my $mobj = MIME::Entity->build( | ||
| 281 | 'From' => 'Apache::ErrorControl <apache-errorcontrol@'. | ||
| 282 | $s->server_hostname(). '>', | ||
| 283 | 'Subject' => 'Error '. $self->{error_code}. ' on '. | ||
| 284 | $s->server_hostname(), | ||
| 285 | 'Type' => 'multipart/mixed', | ||
| 286 | 'X-Mailer' => 'Apache::ErrorControl' | ||
| 287 | ); | ||
| 288 | |||
| 289 | my $body = 'Time: '. $formatted_date. "\n". | ||
| 290 | 'Requested URL: '. $request_url. "\n". | ||
| 291 | 'Requested By: '. $requestor. "\n\n". | ||
| 292 | "--------------------\n". | ||
| 293 | "Apache::ErrorControl\n\n"; | ||
| 294 | |||
| 295 | $mobj->attach( | ||
| 296 | Data => $body, | ||
| 297 | Type => 'text/plain' | ||
| 298 | ); | ||
| 299 | |||
| 300 | # Construct Included Debug | ||
| 301 | my %content; | ||
| 302 | my ($headers_in, $headers_out, $err_headers_out, $subprocess_env); | ||
| 303 | if ($r->prev()) { | ||
| 304 | %content = $r->prev()->content(); | ||
| 305 | $headers_in = $r->prev()->headers_in(); | ||
| 306 | $headers_out = $r->prev()->headers_out(); | ||
| 307 | $err_headers_out = $r->prev()->err_headers_out(); | ||
| 308 | $subprocess_env = $r->prev()->subprocess_env(); | ||
| 309 | } | ||
| 310 | # NB: retrieval of POST data will only work on status 204, 304, 400, 408, | ||
| 311 | # 411, 413, 414, 500, 501, 503 - hey one of them is 500 so im happy :D :D | ||
| 312 | |||
| 313 | my %files = ( | ||
| 314 | headers_in => $headers_in, | ||
| 315 | headers_out => $headers_out, | ||
| 316 | err_headers_out => $err_headers_out, | ||
| 317 | notes => $notes, | ||
| 318 | subprocess_env => $subprocess_env, | ||
| 319 | post_data => \%content, | ||
| 320 | env => \%ENV | ||
| 321 | ); | ||
| 322 | |||
| 323 | foreach my $file (keys %files) { | ||
| 324 | my $string = $self->apache_table_to_string($files{$file}); | ||
| 325 | if ($string) { | ||
| 326 | $mobj->attach( | ||
| 327 | Data => $string, | ||
| 328 | Filename => $file. '.txt', | ||
| 329 | Type => 'text/plain', | ||
| 330 | Encoding => 'base64' | ||
| 331 | ); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | foreach my $email_address (@email) { | ||
| 336 | $mobj->head()->replace('To', $email_address); | ||
| 337 | |||
| 338 | open(MTAHANDLE,"|$MTA_Prog") | ||
| 339 | or die "Failed to open MTA: ". $MTA_Prog. ": $!\n"; | ||
| 340 | $mobj->print(\*MTAHANDLE); | ||
| 341 | close(MTAHANDLE); | ||
| 342 | } | ||
| 343 | } | ||
| 344 | # }}} | ||
| 345 | |||
| 346 | |||
| 347 | # Send Headers {{{ | ||
| 348 | $r->content_type('text/html; charset=ISO-8859-1'); | ||
| 349 | $r->send_http_header; | ||
| 350 | # }}} | ||
| 351 | |||
| 352 | |||
| 353 | # Send Template {{{ | ||
| 354 | $r->print($tmpl->output()); | ||
| 355 | # }}} | ||
| 356 | |||
| 357 | return; | ||
| 358 | } | ||
| 359 | # }}} | ||
| 360 | |||
| 361 | |||
| 362 | # Apache Table To String Function {{{ | ||
| 363 | sub apache_table_to_string { | ||
| 364 | my ($self, $table) = @_; | ||
| 365 | |||
| 366 | return unless ($table); | ||
| 367 | |||
| 368 | my @string = (); | ||
| 369 | |||
| 370 | while(my($key,$val) = each %$table) { | ||
| 371 | next unless ($key); | ||
| 372 | my $string = sprintf("%-15s", $key); | ||
| 373 | $string .= $val if ($val); | ||
| 374 | push(@string, $string); | ||
| 375 | } | ||
| 376 | |||
| 377 | return join("\n", @string); | ||
| 378 | } | ||
| 379 | # }}} | ||
| 380 | |||
| 381 | |||
| 382 | # Find Error Template Function {{{ | ||
| 383 | # im not sure why I chose to allow so many paths/filenames but I think its | ||
| 384 | # better to be flexiable. | ||
| 385 | sub find_error_template { | ||
| 386 | my ($self) = @_; | ||
| 387 | |||
| 388 | my $error_code = $self->{error_code} || undef; | ||
| 389 | |||
| 390 | my @paths; | ||
| 391 | if ($self->{document_root}) { | ||
| 392 | push(@paths, $self->{document_root}); | ||
| 393 | } | ||
| 394 | if ($self->{template_dir}) { | ||
| 395 | push(@paths, $self->{template_dir}); | ||
| 396 | } | ||
| 397 | |||
| 398 | foreach my $path (@paths) { | ||
| 399 | if (defined $error_code) { | ||
| 400 | if (-f $path. '/'. $error_code) { | ||
| 401 | return $path. '/'. $error_code; | ||
| 402 | } elsif (-f $path. '/'. $error_code. '.html') { | ||
| 403 | return $path. '/'. $error_code. '.html'; | ||
| 404 | } elsif (-f $path. '/'. $error_code. '.tmpl') { | ||
| 405 | return $path. '/'. $error_code. '.tmpl'; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | if (-f $path. '/allerrors') { | ||
| 409 | return $path. '/allerrors'; | ||
| 410 | } elsif (-f $path. '/allerrors.html') { | ||
| 411 | return $path. '/allerrors.html'; | ||
| 412 | } elsif (-f $path. '/allerrors.tmpl') { | ||
| 413 | return $path. '/allerrors.tmpl'; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | if (exists $self->{default_template} and $self->{default_template}) { | ||
| 418 | if (-f $self->{default_template}) { | ||
| 419 | return $self->{default_template}; | ||
| 420 | } elsif (-f $self->{template_dir}. '/'. $self->{default_template}) { | ||
| 421 | return $self->{template_dir}. '/'. $self->{default_template}; | ||
| 422 | } elsif (-f $self->{document_root}. '/'. $self->{default_template}) { | ||
| 423 | return $self->{document_root}. '/'. $self->{default_template}; | ||
| 424 | } | ||
| 425 | } | ||
| 426 | } | ||
| 427 | # }}} | ||
| 428 | |||
| 429 | 1; | ||
| 430 | |||
| 431 | END { } | ||
| 432 | |||
| 433 | __END__ | ||