| 1 | |||
| 2 | package ASP4::Server; | ||
| 3 | |||
| 4 | 9 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 9 | ▶ | use warnings 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 9 | ▶ | use ASP4::HTTPContext; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 9 | ▶ | use encoding 'utf8'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | use Mail::Sendmail; | ||
| 9 | |||
| 10 | sub new | ||
| 11 | { | ||
| 12 | return bless { }, shift; | ||
| 13 | }# end new() | ||
| 14 | |||
| 15 | |||
| 16 | sub context { ASP4::HTTPContext->current } | ||
| 17 | |||
| 18 | |||
| 19 | sub URLEncode | ||
| 20 | { | ||
| 21 | ASP4::HTTPContext->current->cgi->escape( $_[1] ); | ||
| 22 | }# end URLEncode() | ||
| 23 | |||
| 24 | |||
| 25 | sub URLDecode | ||
| 26 | { | ||
| 27 | ASP4::HTTPContext->current->cgi->unescape( $_[1] ); | ||
| 28 | }# end URLDecode() | ||
| 29 | |||
| 30 | |||
| 31 | sub HTMLEncode | ||
| 32 | { | ||
| 33 | my ($s, $str) = @_; | ||
| 34 | no warnings 'uninitialized'; | ||
| 35 | $str =~ s/&/&/g; | ||
| 36 | $str =~ s/</</g; | ||
| 37 | $str =~ s/>/>/g; | ||
| 38 | $str =~ s/"/"/g; | ||
| 39 | $str =~ s/'/'/g; | ||
| 40 | return $str; | ||
| 41 | }# end HTMLEncode() | ||
| 42 | |||
| 43 | |||
| 44 | sub HTMLDecode | ||
| 45 | { | ||
| 46 | my ($s, $str) = @_; | ||
| 47 | no warnings 'uninitialized'; | ||
| 48 | $str =~ s/</</g; | ||
| 49 | $str =~ s/>/>/g; | ||
| 50 | $str =~ s/"/"/g; | ||
| 51 | $str =~ s/&/&/g; | ||
| 52 | $str =~ s/'/'/g; | ||
| 53 | return $str; | ||
| 54 | }# end HTMLDecode() | ||
| 55 | |||
| 56 | |||
| 57 | sub MapPath | ||
| 58 | { | ||
| 59 | my ($s, $path) = @_; | ||
| 60 | |||
| 61 | return unless defined($path); | ||
| 62 | |||
| 63 | ASP4::HTTPContext->current->config->web->www_root . $path; | ||
| 64 | }# end MapPath() | ||
| 65 | |||
| 66 | |||
| 67 | sub Mail | ||
| 68 | { | ||
| 69 | my $s = shift; | ||
| 70 | |||
| 71 | Mail::Sendmail::sendmail( @_ ); | ||
| 72 | }# end Mail() | ||
| 73 | |||
| 74 | |||
| 75 | sub RegisterCleanup | ||
| 76 | { | ||
| 77 | my ($s, $sub, @args) = @_; | ||
| 78 | |||
| 79 | $s->context->r->pool->cleanup_register( $sub, \@args ); | ||
| 80 | }# end RegisterCleanup() | ||
| 81 | |||
| 82 | 1;# return true: | ||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | =head1 NAME | ||
| 87 | |||
| 88 | ASP4::Server - Utility Methods | ||
| 89 | |||
| 90 | =head1 SYNOPSIS | ||
| 91 | |||
| 92 | # Get the full disk path to /contact/form.asp: | ||
| 93 | $Server->MapPath("/contact/form.asp"); | ||
| 94 | |||
| 95 | # Email someone: | ||
| 96 | $Server->Mail( | ||
| 97 | To => 'jim@bob.com', | ||
| 98 | From => 'Joe Jangles <joe@jangles.net>', | ||
| 99 | Subject => 'Test Email', | ||
| 100 | Message => "Hello There!", | ||
| 101 | ); | ||
| 102 | |||
| 103 | # Avoid XSS: | ||
| 104 | <input type="text" name="foo" value="<%= $Server->HTMLEncode( $Form->{foo} ) %>" /> | ||
| 105 | |||
| 106 | # Proper URLs: | ||
| 107 | <a href="foo.asp?bar=<%= $Server->URLEncode($Form->{bar}) %>">Click</a> | ||
| 108 | |||
| 109 | =head1 DESCRIPTION | ||
| 110 | |||
| 111 | The C<$Server> object provides some utility methods that don't really fit anywhere | ||
| 112 | else, but are still important. | ||
| 113 | |||
| 114 | =head1 PUBLIC METHODS | ||
| 115 | |||
| 116 | =head2 HTMLEncode( $str ) | ||
| 117 | |||
| 118 | Performs a simple string substitution to sanitize C<$str> for inclusion on HTML pages. | ||
| 119 | |||
| 120 | Removes the threat of cross-site-scripting (XSS). | ||
| 121 | |||
| 122 | Eg: | ||
| 123 | |||
| 124 | <tag/> | ||
| 125 | |||
| 126 | Becomes: | ||
| 127 | |||
| 128 | <tag/> | ||
| 129 | |||
| 130 | =head2 HTMLDecode( $str ) | ||
| 131 | |||
| 132 | Does exactly the reverse of HTMLEncode. | ||
| 133 | |||
| 134 | Eg: | ||
| 135 | |||
| 136 | <tag/> | ||
| 137 | |||
| 138 | Becomes: | ||
| 139 | |||
| 140 | <tag/> | ||
| 141 | |||
| 142 | =head2 URLEncode( $str ) | ||
| 143 | |||
| 144 | Converts a string for use within a URL. | ||
| 145 | |||
| 146 | eg: | ||
| 147 | |||
| 148 | test@test.com | ||
| 149 | |||
| 150 | becomes: | ||
| 151 | |||
| 152 | test%40test.com | ||
| 153 | |||
| 154 | =head2 URLDecode( $str ) | ||
| 155 | |||
| 156 | Converts a url-encoded string to a normal string. | ||
| 157 | |||
| 158 | eg: | ||
| 159 | |||
| 160 | test%40test.com | ||
| 161 | |||
| 162 | becomes: | ||
| 163 | |||
| 164 | test@test.com | ||
| 165 | |||
| 166 | =head2 MapPath( $file ) | ||
| 167 | |||
| 168 | Converts a relative path to a full disk path. | ||
| 169 | |||
| 170 | eg: | ||
| 171 | |||
| 172 | /contact/form.asp | ||
| 173 | |||
| 174 | becomes: | ||
| 175 | |||
| 176 | /var/www/mysite.com/htdocs/contact/form.asp | ||
| 177 | |||
| 178 | =head2 Mail( %args ) | ||
| 179 | |||
| 180 | Sends email - uses L<Mail::Sendmail>'s C<sendmail(...)> function. | ||
| 181 | |||
| 182 | =head2 RegisterCleanup( \&code, @args ) | ||
| 183 | |||
| 184 | The supplied coderef will be executed with its arguments as the request enters | ||
| 185 | its Cleanup phase. | ||
| 186 | |||
| 187 | See L<http://perl.apache.org/docs/2.0/user/handlers/http.html#PerlCleanupHandler> for details. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||