| 1 | |||
| 2 | ################################################################################### | ||
| 3 | # | ||
| 4 | # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de | ||
| 5 | # Embperl - Copyright (c) 2008-2015 Gerald Richter | ||
| 6 | # Embperl - Copyright (c) 2015-2023 actevy.io | ||
| 7 | # | ||
| 8 | # You may distribute under the terms of either the GNU General Public | ||
| 9 | # License or the Artistic License, as specified in the Perl README file. | ||
| 10 | # | ||
| 11 | # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR | ||
| 12 | # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | ||
| 13 | # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| 14 | # | ||
| 15 | ################################################################################### | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | package Embperl::App ; | ||
| 20 | |||
| 21 | use strict ; | ||
| 22 | use vars qw{%Recipes} ; | ||
| 23 | |||
| 24 | no warnings "uninitialized" ; | ||
| 25 | |||
| 26 | # --------------------------------------------------------------------------------- | ||
| 27 | # | ||
| 28 | # Get/create named recipe | ||
| 29 | # | ||
| 30 | # --------------------------------------------------------------------------------- | ||
| 31 | |||
| 32 | |||
| 33 | sub get_recipe | ||
| 34 | |||
| 35 | { | ||
| 36 | my ($self, $r, $name) = @_ ; | ||
| 37 | |||
| 38 | $name ||= 'Embperl' ; | ||
| 39 | my @names = split (/\s/, $name) ; | ||
| 40 | |||
| 41 | foreach my $recipe (@names) | ||
| 42 | { | ||
| 43 | my $mod ; | ||
| 44 | $recipe =~ /([a-zA-Z0-9_:]*)/ ; | ||
| 45 | $recipe = $1 ; | ||
| 46 | if (!($mod = $Recipes{$recipe})) | ||
| 47 | { | ||
| 48 | $mod = ($name =~ /::/)?$recipe:'Embperl::Recipe::'. $recipe ; | ||
| 49 | if (!defined (&{$mod . '::get_recipe'})) | ||
| 50 | { | ||
| 51 | eval "require $mod" ; | ||
| 52 | if ($@) | ||
| 53 | { | ||
| 54 | warn $@ ; | ||
| 55 | return undef ; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | $Recipes{$recipe} = $mod ; | ||
| 59 | } | ||
| 60 | print Embperl::LOG "[$$] Use Recipe $recipe\n" if ($r -> component -> config -> debug) ; | ||
| 61 | my $obj = $mod -> get_recipe ($r, $recipe) ; | ||
| 62 | return $obj if ($obj) ; | ||
| 63 | } | ||
| 64 | |||
| 65 | return undef ; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | # --------------------------------------------------------------------------------- | ||
| 70 | # | ||
| 71 | # send error page | ||
| 72 | # | ||
| 73 | # --------------------------------------------------------------------------------- | ||
| 74 | |||
| 75 | |||
| 76 | sub send_error_page | ||
| 77 | |||
| 78 | { | ||
| 79 | my ($self, $r) = @_ ; | ||
| 80 | |||
| 81 | local $SIG{__WARN__} = 'Default' ; | ||
| 82 | my $virtlog = '' ; # $r -> VirtLogURI || '' ; | ||
| 83 | my $logfilepos = $r -> log_file_start_pos ; | ||
| 84 | my $url = '' ; # $Embperl::dbgLogLink?"<A HREF=\"$virtlog\?$logfilepos\&$$\">Logfile</A>":'' ; | ||
| 85 | my $req_rec = $r -> apache_req ; | ||
| 86 | my $status = $req_rec?$req_rec -> status:0 ; | ||
| 87 | my $err ; | ||
| 88 | my $cnt = 0 ; | ||
| 89 | local $Embperl::escmode = 0 ; | ||
| 90 | my $time = localtime ; | ||
| 91 | my $mail = $req_rec -> server -> server_admin if (defined ($req_rec)) ; | ||
| 92 | $mail ||= '' ; | ||
| 93 | $req_rec -> content_type('text/html') if (defined ($req_rec)) ; | ||
| 94 | |||
| 95 | # don't use method call to avoid trouble with overloading | ||
| 96 | Embperl::Req::output ($r,"<HTML><HEAD><TITLE>Embperl Error</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\">\r\n$url") ; | ||
| 97 | if ($status == 403) | ||
| 98 | { | ||
| 99 | Embperl::Req::output ($r,"<H1>Forbidden</H1>\r\n") ; | ||
| 100 | } | ||
| 101 | elsif ($status == 404) | ||
| 102 | { | ||
| 103 | Embperl::Req::output ($r,"<H1>Not Found</H1>\r\n") ; | ||
| 104 | } | ||
| 105 | else | ||
| 106 | { | ||
| 107 | Embperl::Req::output ($r,"<H1>Internal Server Error</H1>\r\n") ; | ||
| 108 | } | ||
| 109 | Embperl::Req::output ($r,"The server encountered an internal error or misconfiguration and was unable to complete your request.<P>\r\n") ; | ||
| 110 | Embperl::Req::output ($r,"Please contact the server administrator, $mail and inform them of the time the error occurred, and anything you might have done that may have caused the error.<P><P>\r\n") ; | ||
| 111 | |||
| 112 | my $errors = $r -> errors ; | ||
| 113 | if ($virtlog ne '' && $Embperl::dbgLogLink) | ||
| 114 | { | ||
| 115 | foreach $err (@$errors) | ||
| 116 | { | ||
| 117 | Embperl::Req::output ($r,"<A HREF=\"$virtlog?$logfilepos&$$#E$cnt\">") ; #<tt>") ; | ||
| 118 | $Embperl::escmode = 3 ; | ||
| 119 | $err =~ s|\\|\\\\|g; | ||
| 120 | $err =~ s|\n|\n\\<br\\>\\ \\ \\ \\ |g; | ||
| 121 | $err =~ s|(Line [0-9]*:)|$1\\</a\\>|; | ||
| 122 | Embperl::Req::output ($r,$err) ; | ||
| 123 | $Embperl::escmode = 0 ; | ||
| 124 | Embperl::Req::output ($r,"<p>\r\n") ; | ||
| 125 | #Embperl::Req::output ($r,"</tt><p>\r\n") ; | ||
| 126 | $cnt++ ; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | else | ||
| 130 | { | ||
| 131 | $Embperl::escmode = 3 ; | ||
| 132 | Embperl::Req::output ($r,"\\<table cellspacing='2' cellpadding='5'\\>\r\n") ; | ||
| 133 | foreach $err (@$errors) | ||
| 134 | { | ||
| 135 | $err =~ s|\\|\\\\|g; | ||
| 136 | $err =~ s|\n|\n\\<br\\>\\ \\ \\ \\ |g; | ||
| 137 | Embperl::Req::output ($r,"\\<tr bgcolor='#eeeeee'\\>\\<td\\>\r\n\\<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --\\>\r\n") ; | ||
| 138 | Embperl::Req::output ($r,"$err\r\n") ; | ||
| 139 | Embperl::Req::output ($r,"\\<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --\\>\r\n\\</td\\>\\</tr\\>\r\n") ; | ||
| 140 | #Embperl::Req::output ($r,"\\<tt\\>$err\\</tt\\>\\<p\\>\r\n") ; | ||
| 141 | $cnt++ ; | ||
| 142 | } | ||
| 143 | Embperl::Req::output ($r,"\\</table\\>\r\n\\<br\\>\n\r") ; | ||
| 144 | $Embperl::escmode = 0 ; | ||
| 145 | } | ||
| 146 | my $server = $ENV{SERVER_SOFTWARE} || 'Offline' ; | ||
| 147 | |||
| 148 | Embperl::Req::output ($r,$server . ($server =~ /Embperl/?'':" Embperl $Embperl::VERSION") . " [$time]<P>\r\n") ; | ||
| 149 | Embperl::Req::output ($r,"</BODY></HTML>\r\n\r\n") ; | ||
| 150 | } | ||
| 151 | |||
| 152 | # --------------------------------------------------------------------------------- | ||
| 153 | # | ||
| 154 | # mail errors | ||
| 155 | # | ||
| 156 | # --------------------------------------------------------------------------------- | ||
| 157 | |||
| 158 | |||
| 159 | sub mail_errors | ||
| 160 | |||
| 161 | { | ||
| 162 | my ($self, $r) = @_ ; | ||
| 163 | |||
| 164 | local $SIG{__WARN__} = 'Default' ; | ||
| 165 | |||
| 166 | my $to = $self -> config -> mail_errors_to ; | ||
| 167 | return undef if (!$to) ; | ||
| 168 | |||
| 169 | $r -> log ("[$$]ERR: Mail errors to $to\n") ; | ||
| 170 | |||
| 171 | my $time = localtime ; | ||
| 172 | |||
| 173 | require Net::SMTP ; | ||
| 174 | |||
| 175 | my $mailhost = $self -> config -> mailhost || 'localhost' ; | ||
| 176 | my $smtp = Net::SMTP->new($mailhost, Debug => $self -> config -> maildebug) or die "Cannot connect to mailhost $mailhost" ; | ||
| 177 | $smtp->mail("Embperl\@$ENV{SERVER_NAME}"); | ||
| 178 | $smtp->to($to); | ||
| 179 | my $ok = $smtp->data(); | ||
| 180 | $ok and $ok = $smtp->datasend("To: $to\r\n"); | ||
| 181 | $ok and $ok = $smtp->datasend("Subject: ERROR in Embperl page " . $r -> param -> uri . " on $ENV{HTTP_HOST}\r\n"); | ||
| 182 | $ok and $ok = $smtp->datasend("\r\n"); | ||
| 183 | |||
| 184 | $ok and $ok = $smtp->datasend("ERROR in Embperl page $ENV{HTTP_HOST}$ENV{SCRIPT_NAME}\r\n"); | ||
| 185 | $ok and $ok = $smtp->datasend("\r\n"); | ||
| 186 | |||
| 187 | $ok and $ok = $smtp->datasend("-------\r\n"); | ||
| 188 | $ok and $ok = $smtp->datasend("Errors:\r\n"); | ||
| 189 | $ok and $ok = $smtp->datasend("-------\r\n"); | ||
| 190 | my $errors = $r -> errors ; | ||
| 191 | my $err ; | ||
| 192 | |||
| 193 | foreach $err (@$errors) | ||
| 194 | { | ||
| 195 | $ok and $ok = $smtp->datasend("$err\r\n"); | ||
| 196 | } | ||
| 197 | |||
| 198 | $ok and $ok = $smtp->datasend("-----------\r\n"); | ||
| 199 | $ok and $ok = $smtp->datasend("Formfields:\r\n"); | ||
| 200 | $ok and $ok = $smtp->datasend("-----------\r\n"); | ||
| 201 | |||
| 202 | my $ffld = $r -> thread -> form_array ; | ||
| 203 | my $fdat = $r -> thread -> form_hash ; | ||
| 204 | my $k ; | ||
| 205 | my $v ; | ||
| 206 | |||
| 207 | foreach $k (@$ffld) | ||
| 208 | { | ||
| 209 | $v = $fdat->{$k} ; | ||
| 210 | $ok and $ok = $smtp->datasend("$k\t= \"$v\" \n" ); | ||
| 211 | } | ||
| 212 | $ok and $ok = $smtp->datasend("-------------\r\n"); | ||
| 213 | $ok and $ok = $smtp->datasend("Environment:\r\n"); | ||
| 214 | $ok and $ok = $smtp->datasend("-------------\r\n"); | ||
| 215 | |||
| 216 | my $env = $r -> thread -> env_hash ; | ||
| 217 | |||
| 218 | foreach $k (sort keys %$env) | ||
| 219 | { | ||
| 220 | $v = $env -> {$k} ; | ||
| 221 | $ok and $ok = $smtp->datasend("$k\t= \"$v\" \n" ); | ||
| 222 | } | ||
| 223 | |||
| 224 | my $server = $ENV{SERVER_SOFTWARE} || 'Offline' ; | ||
| 225 | |||
| 226 | $ok and $ok = $smtp->datasend("-------------\r\n"); | ||
| 227 | $ok and $ok = $smtp->datasend("$server Embperl $Embperl::VERSION [$time]\r\n") ; | ||
| 228 | |||
| 229 | $ok and $ok = $smtp->dataend() ; | ||
| 230 | $smtp->quit; | ||
| 231 | |||
| 232 | return $ok ; | ||
| 233 | } | ||
| 234 | |||
| 235 | # --------------------------------------------------------------------------------- | ||
| 236 | # | ||
| 237 | # MailFormTo | ||
| 238 | # | ||
| 239 | # --------------------------------------------------------------------------------- | ||
| 240 | |||
| 241 | |||
| 242 | sub mail_form_to | ||
| 243 | |||
| 244 | { | ||
| 245 | my ($self, $to, $subject, $returnfield) = @_ ; | ||
| 246 | my $v ; | ||
| 247 | my $k ; | ||
| 248 | my $ok ; | ||
| 249 | my $smtp ; | ||
| 250 | my $ret ; | ||
| 251 | my $r = $self -> curr_req ; | ||
| 252 | my $fdat = $r -> thread -> form_hash ; | ||
| 253 | |||
| 254 | $ret = $fdat -> {$returnfield} ; | ||
| 255 | |||
| 256 | require Net::SMTP ; | ||
| 257 | |||
| 258 | $smtp = Net::SMTP->new($self -> config -> mailhost || 'localhost', | ||
| 259 | Debug => $self -> config -> maildebug || 0, | ||
| 260 | $self -> config -> mailhelo?(Hello => $self -> config -> mailhelo):()) | ||
| 261 | or die "Cannot connect to mailhost" ; | ||
| 262 | |||
| 263 | $smtp->mail($self -> config -> mailfrom || ("WWW-Server\@" . ($ENV{SERVER_NAME} || 'localhost'))); | ||
| 264 | $smtp->to($to); | ||
| 265 | $ok = $smtp->data(); | ||
| 266 | $ok = $smtp->datasend("Reply-To: $ret\n") if ($ok && $ret) ; | ||
| 267 | $ok and $ok = $smtp->datasend("To: $to\n"); | ||
| 268 | $ok and $ok = $smtp->datasend("Subject: $subject\n"); | ||
| 269 | $ok and $ok = $smtp->datasend("\n"); | ||
| 270 | foreach $k (@{$r -> thread -> form_array}) | ||
| 271 | { | ||
| 272 | $v = $fdat->{$k} ; | ||
| 273 | if (defined ($v) && $v ne '') | ||
| 274 | { | ||
| 275 | $ok and $ok = $smtp->datasend("$k\t= $v \n" ); | ||
| 276 | } | ||
| 277 | } | ||
| 278 | $ok and $ok = $smtp->datasend("\nClient\t= $ENV{REMOTE_HOST} ($ENV{REMOTE_ADDR})\n\n" ); | ||
| 279 | $ok and $ok = $smtp->dataend() ; | ||
| 280 | $smtp->quit; | ||
| 281 | |||
| 282 | $? = $ok?0:1 ; | ||
| 283 | |||
| 284 | return $ok ; | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | 1; | ||
| 291 | |||
| 292 | |||
| 293 | __END__ |