| 1 | |||
| 2 | package Apache2::ASP::Server; | ||
| 3 | |||
| 4 | 23 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 23 | ▶ | use warnings 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 23 | ▶ | use Mail::Sendmail; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 23 | ▶ | use encoding 'utf8'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | |||
| 10 | #============================================================================== | ||
| 11 | sub new | ||
| 12 | { | ||
| 13 | my ($class, %args) = @_; | ||
| 14 | |||
| 15 | my $s = bless {LastError => undef}, $class; | ||
| 16 | |||
| 17 | return $s; | ||
| 18 | }# end new() | ||
| 19 | |||
| 20 | |||
| 21 | #============================================================================== | ||
| 22 | sub GetLastError | ||
| 23 | { | ||
| 24 | $_[0]->{LastError}; | ||
| 25 | }# end GetLastError() | ||
| 26 | |||
| 27 | |||
| 28 | #============================================================================== | ||
| 29 | sub context | ||
| 30 | { | ||
| 31 | $Apache2::ASP::HTTPContext::ClassName->current; | ||
| 32 | }# end context() | ||
| 33 | |||
| 34 | |||
| 35 | #============================================================================== | ||
| 36 | # Shamelessly ripped off from Apache::ASP::Server, by Joshua Chamas, | ||
| 37 | # who shamelessly ripped it off from CGI.pm, by Lincoln D. Stein. | ||
| 38 | # :) | ||
| 39 | sub URLEncode | ||
| 40 | { | ||
| 41 | my $toencode = $_[1]; | ||
| 42 | no warnings 'uninitialized'; | ||
| 43 | $toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/esg; | ||
| 44 | $toencode; | ||
| 45 | }# end URLEncode() | ||
| 46 | |||
| 47 | |||
| 48 | #============================================================================== | ||
| 49 | sub URLDecode | ||
| 50 | { | ||
| 51 | my ($s, $todecode) = @_; | ||
| 52 | return unless defined($todecode); | ||
| 53 | $todecode =~ tr/+/ /; # pluses become spaces | ||
| 54 | $todecode =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ | ||
| 55 | defined($1)? chr hex($1) : _utf8_chr(hex($2))/ge; | ||
| 56 | return $todecode; | ||
| 57 | }# end URLDecode() | ||
| 58 | |||
| 59 | |||
| 60 | #============================================================================== | ||
| 61 | sub HTMLEncode | ||
| 62 | { | ||
| 63 | my ($s, $str) = @_; | ||
| 64 | no warnings 'uninitialized'; | ||
| 65 | $str =~ s/&/&/g; | ||
| 66 | $str =~ s/</</g; | ||
| 67 | $str =~ s/>/>/g; | ||
| 68 | $str =~ s/"/"/g; | ||
| 69 | $str =~ s/'/'/g; | ||
| 70 | return $str; | ||
| 71 | }# end HTMLEncode() | ||
| 72 | |||
| 73 | |||
| 74 | #============================================================================== | ||
| 75 | sub HTMLDecode | ||
| 76 | { | ||
| 77 | my ($s, $str) = @_; | ||
| 78 | no warnings 'uninitialized'; | ||
| 79 | $str =~ s/</</g; | ||
| 80 | $str =~ s/>/>/g; | ||
| 81 | $str =~ s/"/"/g; | ||
| 82 | $str =~ s/&/&/g; | ||
| 83 | return $str; | ||
| 84 | }# end HTMLEncode() | ||
| 85 | |||
| 86 | |||
| 87 | #============================================================================== | ||
| 88 | sub MapPath | ||
| 89 | { | ||
| 90 | my ($s, $path) = @_; | ||
| 91 | |||
| 92 | return unless defined($path); | ||
| 93 | |||
| 94 | $s->context->config->web->www_root . $path; | ||
| 95 | }# end MapPath() | ||
| 96 | |||
| 97 | |||
| 98 | #============================================================================== | ||
| 99 | sub Mail | ||
| 100 | { | ||
| 101 | my ($s, %args) = @_; | ||
| 102 | |||
| 103 | # XXX: Base64-encode the content, and update the content-type to reflect that | ||
| 104 | # if content-type === 'text/html'. | ||
| 105 | # XXX: Consider updating this so that we can send attachments as well. | ||
| 106 | Mail::Sendmail::sendmail( %args ); | ||
| 107 | }# end Mail() | ||
| 108 | |||
| 109 | |||
| 110 | #============================================================================== | ||
| 111 | sub RegisterCleanup | ||
| 112 | { | ||
| 113 | my ($s, $sub, @args) = @_; | ||
| 114 | |||
| 115 | # This works both in "testing" mode and within a live mod_perl environment. | ||
| 116 | $s->context->get_prop('r')->pool->cleanup_register( $sub, \@args ); | ||
| 117 | }# end RegisterCleanup() | ||
| 118 | |||
| 119 | |||
| 120 | #============================================================================== | ||
| 121 | sub _utf8_chr | ||
| 122 | { | ||
| 123 | my ($c) = @_; | ||
| 124 | require utf8; | ||
| 125 | my $u = chr($c); | ||
| 126 | utf8::encode($u); # drop utf8 flag | ||
| 127 | return $u; | ||
| 128 | }# end _utf8_chr() | ||
| 129 | |||
| 130 | |||
| 131 | #============================================================================== | ||
| 132 | sub DESTROY | ||
| 133 | { | ||
| 134 | my $s = shift; | ||
| 135 | |||
| 136 | undef(%$s); | ||
| 137 | }# end DESTROY() | ||
| 138 | |||
| 139 | 1;# return true: | ||
| 140 | |||
| 141 | |||
| 142 | =pod | ||
| 143 | |||
| 144 | =head1 NAME | ||
| 145 | |||
| 146 | Apache2::ASP::Server - Utility methods for Apache2::ASP | ||
| 147 | |||
| 148 | =head1 SYNOPSIS | ||
| 149 | |||
| 150 | my $full_path = $Server->MapPath('/index.asp'); | ||
| 151 | |||
| 152 | $Server->URLEncode( 'user@email.com' ); | ||
| 153 | |||
| 154 | $Server->URLDecode( 'user%40email.com' ); | ||
| 155 | |||
| 156 | $Server->HTMLEncode( '<br />' ); | ||
| 157 | |||
| 158 | $Server->HTMLDecode( '<br />' ); | ||
| 159 | |||
| 160 | $Server->Mail( | ||
| 161 | To => 'user@email.com', | ||
| 162 | From => '"Friendly Name" <friendly.name@email.com>', | ||
| 163 | Subject => 'Hello World', | ||
| 164 | Message => "E Pluribus Unum.\n"x777 | ||
| 165 | ); | ||
| 166 | |||
| 167 | $Server->RegisterCleanup( sub { | ||
| 168 | my @args = @_; | ||
| 169 | ... | ||
| 170 | }, @args | ||
| 171 | ); | ||
| 172 | |||
| 173 | =head1 DESCRIPTION | ||
| 174 | |||
| 175 | The ASP Server object is historically a wrapper for a few utility functions that | ||
| 176 | don't belong anywhere else. | ||
| 177 | |||
| 178 | Keeping with that tradition, the Apache2::ASP Server object is a collection of | ||
| 179 | functions that don't belong anywhere else. | ||
| 180 | |||
| 181 | =head1 PUBLIC METHODS | ||
| 182 | |||
| 183 | =head2 URLEncode( $str ) | ||
| 184 | |||
| 185 | Converts a string into its url-encoded equivalent. This approximates to | ||
| 186 | JavaScript's C<escape()> function or L<CGI>'s C<escape()> function. | ||
| 187 | |||
| 188 | Example: | ||
| 189 | |||
| 190 | <%= $Server->URLEncode( 'user@email.com' ) %> | ||
| 191 | |||
| 192 | Returns | ||
| 193 | |||
| 194 | user%40email.com | ||
| 195 | |||
| 196 | =head2 URLDecode( $str ) | ||
| 197 | |||
| 198 | Converts a url-encoded string into its non-url-encoded equivalent. This works | ||
| 199 | the same way as JavaScript's and L<CGI>'s C<unescape()> function. | ||
| 200 | |||
| 201 | Example: | ||
| 202 | |||
| 203 | <%= $Server->URLDecode( 'user%40email.com' ) %> | ||
| 204 | |||
| 205 | Returns | ||
| 206 | |||
| 207 | user@email.com | ||
| 208 | |||
| 209 | =head2 HTMLEncode( $str ) | ||
| 210 | |||
| 211 | Safely converts <, > and & into C<<>, C<>> and C<&>, respectively. | ||
| 212 | |||
| 213 | =head2 HTMLDecode( $str ) | ||
| 214 | |||
| 215 | Converts C<<>, C<>> and C<&> into <, > and &, respectively. | ||
| 216 | |||
| 217 | =head2 MapPath( $relative_path ) | ||
| 218 | |||
| 219 | Given a relative path, C<MapPath> will return the absolute path for it, under the | ||
| 220 | document root of the current website. | ||
| 221 | |||
| 222 | For example, C</index.asp> might return C</usr/local/famicom/htdocs/index.asp> | ||
| 223 | |||
| 224 | =head2 Mail( %args ) | ||
| 225 | |||
| 226 | Sends an email message. The following arguments are required: | ||
| 227 | |||
| 228 | =over 4 | ||
| 229 | |||
| 230 | =item To | ||
| 231 | |||
| 232 | The email address the message should be sent to. | ||
| 233 | |||
| 234 | =item From | ||
| 235 | |||
| 236 | The email address the message should be sent from. | ||
| 237 | |||
| 238 | =item Subject | ||
| 239 | |||
| 240 | The subject of the email. | ||
| 241 | |||
| 242 | =item Message | ||
| 243 | |||
| 244 | The content of the body. | ||
| 245 | |||
| 246 | =back | ||
| 247 | |||
| 248 | Other arguments are passed through to L<Mail::Sendmail>. | ||
| 249 | |||
| 250 | =head2 RegisterCleanup( \&code[, @args ] ) | ||
| 251 | |||
| 252 | A wrapper around L<APR::Pool>'s C<cleanup_register> function. Pass in a coderef | ||
| 253 | and (optionally) arguments to be passed to that coderef, and it is executed during | ||
| 254 | the cleanup phase of the current request. | ||
| 255 | |||
| 256 | If we were doing vanilla mod_perl, you could achieve the same effect with this: | ||
| 257 | |||
| 258 | $r->pool->cleanup_register( sub { ... }, \@args ); | ||
| 259 | |||
| 260 | =head1 BUGS | ||
| 261 | |||
| 262 | It's possible that some bugs have found their way into this release. | ||
| 263 | |||
| 264 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports. | ||
| 265 | |||
| 266 | =head1 HOMEPAGE | ||
| 267 | |||
| 268 | Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples | ||
| 269 | of Apache2::ASP in action. | ||
| 270 | |||
| 271 | =head1 AUTHOR | ||
| 272 | |||
| 273 | John Drago <jdrago_999@yahoo.com> | ||
| 274 | |||
| 275 | =head1 COPYRIGHT | ||
| 276 | |||
| 277 | Copyright 2008 John Drago. All rights reserved. | ||
| 278 | |||
| 279 | =head1 LICENSE | ||
| 280 | |||
| 281 | This software is Free software and is licensed under the same terms as perl itself. | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||