| 1 | ################################################################################ | ||
| 2 | # | ||
| 3 | # CGI.pm (CGI.pm Access for PML) | ||
| 4 | # | ||
| 5 | ################################################################################ | ||
| 6 | # | ||
| 7 | # Package | ||
| 8 | # | ||
| 9 | ################################################################################ | ||
| 10 | package PML::CGI; | ||
| 11 | ################################################################################ | ||
| 12 | # | ||
| 13 | # Includes | ||
| 14 | # | ||
| 15 | ################################################################################ | ||
| 16 | use CGI; | ||
| 17 | use strict; | ||
| 18 | ################################################################################ | ||
| 19 | # | ||
| 20 | # Global Variables and Default Settings | ||
| 21 | # | ||
| 22 | ################################################################################ | ||
| 23 | use vars qw($VERSION $DATE); | ||
| 24 | $VERSION = '0.01'; | ||
| 25 | $DATE = 'Tue Dec 7 14:19:05 1999'; | ||
| 26 | |||
| 27 | my $pkg = 'cgi::'; | ||
| 28 | my $tag; | ||
| 29 | ################################################################################ | ||
| 30 | # | ||
| 31 | # Code Start | ||
| 32 | # | ||
| 33 | ################################################################################ | ||
| 34 | foreach $tag (keys %CGI::EXPORT_TAGS) | ||
| 35 | { | ||
| 36 | foreach (@{$CGI::EXPORT_TAGS{$tag}}) | ||
| 37 | { | ||
| 38 | next if /^(:|print|header|redirect)/; | ||
| 39 | PML->register(name=>"$pkg$_", token=>\&autotoken) | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | PML->register(name=>"${pkg}url_swap", token=>\&url_swap); | ||
| 44 | PML->register(name=>"${pkg}url_http", token=>\&url_http); | ||
| 45 | PML->register(name=>"${pkg}url_https", token=>\&url_https); | ||
| 46 | PML->register(name=>"${pkg}header", token=>\&header); | ||
| 47 | PML->register(name=>"${pkg}redirect", token=>\&redirect); | ||
| 48 | ################################################################################ | ||
| 49 | # | ||
| 50 | # ==== init ==== ############################################################### | ||
| 51 | # | ||
| 52 | # Arguments: | ||
| 53 | # 1) Class | ||
| 54 | # 2) PML Object | ||
| 55 | # | ||
| 56 | # Returns: | ||
| 57 | # None | ||
| 58 | # | ||
| 59 | # Description: | ||
| 60 | # Prepares the PML object | ||
| 61 | # | ||
| 62 | ################################################################################ | ||
| 63 | sub init | ||
| 64 | { | ||
| 65 | my ($class, $self) = @_; | ||
| 66 | |||
| 67 | $self->v("${pkg}cgi", new CGI); | ||
| 68 | |||
| 69 | if ($PML::DEBUG) | ||
| 70 | { | ||
| 71 | print STDERR "PML::CGI::init called with class=$class and self=$self\n"; | ||
| 72 | } | ||
| 73 | } # <-- End init --> | ||
| 74 | ################################################################################ | ||
| 75 | # | ||
| 76 | # ==== autotoken ==== ########################################################## | ||
| 77 | # | ||
| 78 | # Arguments: | ||
| 79 | # See PML Docs for a auto_parse callback | ||
| 80 | # | ||
| 81 | # Returns: | ||
| 82 | # Whatever the CGI sub returns | ||
| 83 | # | ||
| 84 | # Description: | ||
| 85 | # Calls the correct CGI sub | ||
| 86 | # | ||
| 87 | ################################################################################ | ||
| 88 | sub autotoken | ||
| 89 | { | ||
| 90 | my ($self, $token) = @_; | ||
| 91 | my ($name, $a, $b) = @{$token->data}; | ||
| 92 | my ($code, @rv, $cgi); | ||
| 93 | |||
| 94 | $cgi = $self->v("${pkg}cgi"); | ||
| 95 | $name =~ s/^$pkg//; | ||
| 96 | $code = "\@rv = \$cgi->$name(\$self->tokens_execute(\$a))"; | ||
| 97 | eval "$code"; | ||
| 98 | |||
| 99 | return @rv if $token->context == PML->CONTEXT_LIST; | ||
| 100 | return join ' ', @rv; | ||
| 101 | } # <-- End autotoken --> | ||
| 102 | ################################################################################ | ||
| 103 | # | ||
| 104 | # ==== url_swap ==== ########################################################### | ||
| 105 | # | ||
| 106 | # Arguments: | ||
| 107 | # See PML Docs for a auto_parse callback | ||
| 108 | # | ||
| 109 | # Returns: | ||
| 110 | # A sting that contains a url | ||
| 111 | # | ||
| 112 | # Description: | ||
| 113 | # removes a script from the end of the current url and replaces it | ||
| 114 | # with the script given in the args. | ||
| 115 | # | ||
| 116 | ################################################################################ | ||
| 117 | sub url_swap | ||
| 118 | { | ||
| 119 | my ($self, $token) = @_; | ||
| 120 | my ($name, $a, $b) = @{$token->data}; | ||
| 121 | my ($url, $script, $rv, $cgi); | ||
| 122 | |||
| 123 | unless (@$a) | ||
| 124 | { | ||
| 125 | warn 'cgi::url_swap called without a url' . "\n"; | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | $cgi = $self->v("${pkg}cgi"); | ||
| 130 | $script = $self->tokens_execute($a->[0]); | ||
| 131 | $url = $cgi->url; | ||
| 132 | |||
| 133 | $url =~ s/^(.*\/).*$/$1/; | ||
| 134 | $rv = $url . $script; | ||
| 135 | |||
| 136 | return $rv; | ||
| 137 | } # <-- End url_swap --> | ||
| 138 | ################################################################################ | ||
| 139 | # | ||
| 140 | # ==== url_http ==== ########################################################### | ||
| 141 | # | ||
| 142 | # Arguments: | ||
| 143 | # See PML Docs for a auto_parse callback | ||
| 144 | # | ||
| 145 | # Returns: | ||
| 146 | # A url | ||
| 147 | # | ||
| 148 | # Description: | ||
| 149 | # A http version of the current url | ||
| 150 | # | ||
| 151 | ################################################################################ | ||
| 152 | sub url_http | ||
| 153 | { | ||
| 154 | my ($self, $token) = @_; | ||
| 155 | my ($name, $a, $b) = @{$token->data}; | ||
| 156 | my ($url, $rv, $cgi); | ||
| 157 | |||
| 158 | $cgi = $self->v("${pkg}cgi"); | ||
| 159 | $url = $cgi->url; | ||
| 160 | ($rv = $url) =~ s/^https/http/i; | ||
| 161 | |||
| 162 | return $rv; | ||
| 163 | } # <-- End url_http --> | ||
| 164 | ################################################################################ | ||
| 165 | # | ||
| 166 | # ==== url_https ==== ########################################################## | ||
| 167 | # | ||
| 168 | # Arguments: | ||
| 169 | # See PML Docs for a auto_parse callback | ||
| 170 | # | ||
| 171 | # Returns: | ||
| 172 | # A url | ||
| 173 | # | ||
| 174 | # Description: | ||
| 175 | # A https version of the current url | ||
| 176 | # | ||
| 177 | ################################################################################ | ||
| 178 | sub url_https | ||
| 179 | { | ||
| 180 | my ($self, $token) = @_; | ||
| 181 | my ($name, $a, $b) = @{$token->data}; | ||
| 182 | my ($url, $rv, $cgi); | ||
| 183 | |||
| 184 | $cgi = $self->v("${pkg}cgi"); | ||
| 185 | $url = $cgi->url; | ||
| 186 | ($rv = $url) =~ s/^http(?!s)/https/i; | ||
| 187 | |||
| 188 | return $rv; | ||
| 189 | } # <-- End url_https --> | ||
| 190 | ################################################################################ | ||
| 191 | # | ||
| 192 | # ==== header ==== ############################################################# | ||
| 193 | # | ||
| 194 | # Arguments: | ||
| 195 | # See PML Docs for a auto_parse callback | ||
| 196 | # | ||
| 197 | # Returns: | ||
| 198 | # None | ||
| 199 | # | ||
| 200 | # Description: | ||
| 201 | # sends the http header to the browser | ||
| 202 | # | ||
| 203 | ################################################################################ | ||
| 204 | sub header | ||
| 205 | { | ||
| 206 | my ($self, $token) = @_; | ||
| 207 | my ($name, $a, $b) = @{$token->data}; | ||
| 208 | my @args = $self->tokens_execute($a); | ||
| 209 | my $cgi; | ||
| 210 | |||
| 211 | $cgi = $self->v("${pkg}cgi"); | ||
| 212 | $cgi->print($cgi->header(@args)); | ||
| 213 | |||
| 214 | if ($PML::DEBUG) | ||
| 215 | { | ||
| 216 | print STDERR 'cgi::header: calling header with: ('; | ||
| 217 | print STDERR join ', ', @args; | ||
| 218 | print STDERR ")\n"; | ||
| 219 | } | ||
| 220 | |||
| 221 | return undef; | ||
| 222 | } # <-- End header --> | ||
| 223 | ################################################################################ | ||
| 224 | # | ||
| 225 | # ==== redirect ==== ########################################################### | ||
| 226 | # | ||
| 227 | # Arguments: | ||
| 228 | # See PML Docs for a auto_parse callback | ||
| 229 | # | ||
| 230 | # Returns: | ||
| 231 | # None | ||
| 232 | # | ||
| 233 | # Description: | ||
| 234 | # sends a redirect to the browser | ||
| 235 | # | ||
| 236 | ################################################################################ | ||
| 237 | sub redirect | ||
| 238 | { | ||
| 239 | my ($self, $token) = @_; | ||
| 240 | my ($name, $a, $b) = @{$token->data}; | ||
| 241 | my $cgi; | ||
| 242 | |||
| 243 | $cgi = $self->v("${pkg}cgi"); | ||
| 244 | $cgi->print($cgi->redirect($self->tokens_execute($a))); | ||
| 245 | return undef; | ||
| 246 | } # <-- End redirect --> | ||
| 247 | ################################################################################ | ||
| 248 | # END-OF-MODULE # | ||
| 249 | ################################################################################ | ||
| 250 | 1; |