| 1 | package CGI::Application::Plugin::Apache2::Request; | ||
| 2 | use strict; | ||
| 3 | use Apache2::Request; | ||
| 4 | our @ISA = qw(Apache2::Request); | ||
| 5 | use Apache2::Upload; | ||
| 6 | use Apache2::Cookie; | ||
| 7 | use Apache2::URI; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my ($class, @args) = @_; | ||
| 11 | return bless { | ||
| 12 | r => Apache2::Request->new(@args), | ||
| 13 | __cap_params => {}, | ||
| 14 | __cap_deleted => 0, | ||
| 15 | }, $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | # we need to implement our own params because Apache2::Request no | ||
| 19 | # longer allows you to maniu | ||
| 20 | sub param { | ||
| 21 | my ($self, @args) = @_; | ||
| 22 | |||
| 23 | # if we don't have anything in %PARAMS, then populate it from args | ||
| 24 | my %params = %{$self->{__cap_params}}; | ||
| 25 | unless( %params or $self->{__cap_deleted} ) { | ||
| 26 | foreach my $k ($self->SUPER::param()) { | ||
| 27 | my @values = $self->SUPER::param($k); | ||
| 28 | $params{$k} = @values > 1 ? \@values : $values[0]; | ||
| 29 | } | ||
| 30 | $self->{__cap_params} = \%params; | ||
| 31 | } | ||
| 32 | |||
| 33 | # if we just want the value of a param | ||
| 34 | if(@args > 1) { | ||
| 35 | $params{$args[0]} = $args[1]; | ||
| 36 | return $self->args->{$args[0]} = $args[1]; | ||
| 37 | } elsif( @args ) { | ||
| 38 | my @result = ref $params{$args[0]} ? @{$params{$args[0]}} : $params{$args[0]}; | ||
| 39 | return wantarray ? @result : $result[0]; | ||
| 40 | } else { | ||
| 41 | return keys %params; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | =pod | ||
| 46 | |||
| 47 | =head1 NAME | ||
| 48 | |||
| 49 | CGI::Application::Plugin::Apache::Request | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | This package is just a wrapper around L<Apache::Request> / | ||
| 54 | L<Apache2::Request> to add L<CGI.pm|CGI> compatible methods. The interface | ||
| 55 | and usage is exactly the same as L<CGI.pm|CGI> for the methods provided. | ||
| 56 | |||
| 57 | =over 8 | ||
| 58 | |||
| 59 | =item delete() | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub delete { | ||
| 64 | my ($self, @args) = @_; | ||
| 65 | delete $self->{__cap_params}->{$_} foreach (@args); | ||
| 66 | $self->{__cap_deleted} = 1; | ||
| 67 | } | ||
| 68 | |||
| 69 | =item delete_all() | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub delete_all { | ||
| 74 | my $self = shift; | ||
| 75 | $self->{__cap_params} = {}; | ||
| 76 | $self->{__cap_deleted} = 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | =item cookie() | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub cookie { | ||
| 84 | my ($self, @args) = @_; | ||
| 85 | if($#args == 0) { | ||
| 86 | # if we just have a name of a cookie then retrieve the value of the cookie | ||
| 87 | my $cookies = Apache2::Cookie->fetch(); | ||
| 88 | if( $cookies && $cookies->{$args[0]} ) { | ||
| 89 | return $cookies->{$args[0]}->value; | ||
| 90 | } else { | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | } else { | ||
| 94 | # else we have several values so try and create a new cookie | ||
| 95 | return Apache2::Cookie->new($self, @args); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | =item Dump() | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub Dump { | ||
| 104 | my $self = shift; | ||
| 105 | my($param,$value,@result); | ||
| 106 | return '<ul></ul>' unless $self->param; | ||
| 107 | push(@result,"<ul>"); | ||
| 108 | foreach $param ($self->param) { | ||
| 109 | my $name = $self->escapeHTML($param); | ||
| 110 | push(@result,"<li><strong>$name</strong></li>"); | ||
| 111 | push(@result,"<ul>"); | ||
| 112 | foreach $value ($self->param($param)) { | ||
| 113 | $value = $self->escapeHTML($value); | ||
| 114 | push(@result,"<li>$value</li>"); | ||
| 115 | } | ||
| 116 | push(@result,"</ul>"); | ||
| 117 | } | ||
| 118 | push(@result,"</ul>"); | ||
| 119 | return join("\n",@result); | ||
| 120 | } | ||
| 121 | |||
| 122 | =item Vars() | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub Vars { | ||
| 127 | my $self = shift; | ||
| 128 | my %vars = %{$self->{__cap_params}}; | ||
| 129 | |||
| 130 | return wantarray ? %vars : \%vars; | ||
| 131 | } | ||
| 132 | |||
| 133 | =item escapeHTML() | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | sub escapeHTML { | ||
| 138 | my ($self, $value) = @_; | ||
| 139 | require HTML::GenerateUtil; | ||
| 140 | $value = HTML::GenerateUtil::escape_html($value, | ||
| 141 | ( | ||
| 142 | $HTML::GenerateUtil::EH_LFTOBR | ||
| 143 | | $HTML::GenerateUtil::EH_SPTONBSP | ||
| 144 | | $HTML::GenerateUtil::EH_LEAVEKNOWN | ||
| 145 | ) | ||
| 146 | ); | ||
| 147 | return $value; | ||
| 148 | } | ||
| 149 | |||
| 150 | =item upload() | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub upload { | ||
| 155 | my ($self, $file) = @_; | ||
| 156 | # if they want a specific one, then lets give them the file handle | ||
| 157 | if( $file ) { | ||
| 158 | my $upload = $self->SUPER::upload($file); | ||
| 159 | if( $upload ) { | ||
| 160 | return $upload->fh(); | ||
| 161 | } else { | ||
| 162 | return; | ||
| 163 | } | ||
| 164 | # else they want them all | ||
| 165 | } else { | ||
| 166 | my @files = $self->SUPER::upload(); | ||
| 167 | @files = map { $self->SUPER::upload($_)->fh() } @files; | ||
| 168 | return @files; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; | ||
| 173 | |||
| 174 | __END__ |