SilverSplash/Apache2.pm untested

filter: Statementstmt 0.0% 0 / 175 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 270 CC 22 SCAR 32.4
CC
22
Cov
0%
handler
72.0
free
42.0
paid
42.0
CRAP 25.6
1 package App::SilverSplash::Apache2;
2  
3 use strict;
4 use warnings;
5  
6 use constant DEBUG => $ENV{SL_DEBUG} || 0;
7  
8 use base 'App::SilverSplash';
9  
10 use Apache2::Const -compile => qw( NOT_FOUND OK REDIRECT SERVER_ERROR DECLINED
11 AUTH_REQUIRED HTTP_SERVICE_UNAVAILABLE DONE
12 M_GET M_POST HTTP_METHOD_NOT_ALLOWED );
13  
14 use Data::Dumper qw(Dumper);
15  
16 use Apache2::Request ();
17 use Apache2::RequestRec ();
18 use Apache2::Connection ();
19 use Apache2::ConnectionUtil ();
20 use Apache2::Log ();
21 use Apache2::Response ();
22 use Apache2::RequestUtil ();
23 use Apache2::URI ();
24 use APR::Table ();
25  
26 use Config::SL ();
27 use Template ();
28 use URI::Escape ();
29 use Business::PayPal;
30  
31 our $Config = Config::SL->new;
32  
33 our $Template =
34 Template->new( INCLUDE_PATH => $Config->sl_httpd_root . '/htdocs/sl/' );
35  
36 # bypass the iphone captive portal detector
37  
38 sub iphone_check {
39 my ( $class, $r ) = @_;
40  
41 my $ua = $r->headers_in->{'user-agent'};
42 if ( defined $ua && ( substr( $ua, 0, 21 ) eq 'CaptiveNetworkSupport' ) ) {
43  
44 $r->set_handlers( PerlResponseHandler => undef );
45 return Apache2::Const::OK;
46 }
47 else {
48 return Apache2::Const::OK;
49 }
50 }
51  
52 # paypal ipn handler
53  
54 sub ipnvalidate {
55 my ( $class, $r ) = @_;
56  
57 my $req = Apache2::Request->new($r);
58  
59 # build cgi compatible request hash
60 my @param = $req->param;
61 my %q;
62 foreach my $key (@param) {
63 $q{$key} = $req->param($key);
64 }
65  
66 $r->log->debug( "request dump is " . Dumper( \%q ) ) if DEBUG;
67  
68 my $id = $q{custom};
69 my $paypal = Business::PayPal->new( id => $id );
70 my ( $txnstatus, $reason ) = $paypal->ipnvalidate( \%q );
71 unless ($txnstatus) {
72 $r->log->error("PayPal failed: $reason");
73 return Apache2::Const::SERVER_ERROR;
74 }
75  
76 # check to see if we can find a mac address for this paypal_id
77 my $found = $class->get("paypal_id|$id");
78 my ( $mac, $ip ) = split( /\|/, $found );
79 my $added = App::SilverSplash::IPTables->add_to_paid_chain( $mac, $ip );
80  
81 return Apache2::Const::OK;
82 }
83  
84 # base level request handler
85  
86 sub handler {
87 my ( $class, $r ) = @_;
88  
89 my $ip = $r->connection->remote_ip;
90 $r->log->debug("$$ new request ip $ip") if DEBUG;
91 $r->log->debug( "request: " . $r->as_string ) if DEBUG;
92  
93 # "Thou shalt not pass!" - Gandalf
94 my $mac = $class->mac_from_ip($ip);
95 unless ($mac) {
96 $r->no_cache(1);
97 $r->content_type('text/plain');
98 $r->print(
99 "Please renew your DHCP lease, or reboot your computer to get a valid DHCP lease"
100 );
101 return Apache2::Const::OK;
102 }
103  
104 return Apache2::Const::HTTP_METHOD_NOT_ALLOWED
105 if ( $r->header_only or ( $r->method_number != Apache2::Const::M_GET ) );
106  
107 return Apache2::Const::HTTP_SERVICE_UNAVAILABLE
108 unless $r->headers_in->{'user-agent'};
109  
110 my $dest_url = $r->construct_url( $r->unparsed_uri );
111  
112 # check to see if this mac has been authenticated already
113 my $authed = $class->check_auth( $mac, $ip );
114  
115 if ($authed) {
116  
117 # this is a known mac, go on to the web
118 $r->log->debug("$$ valid mac $mac, redirect to $dest_url") if DEBUG;
119 $r->headers_out->set( Location => $dest_url );
120 $r->no_cache(1);
121 return Apache2::Const::REDIRECT;
122  
123 }
124  
125 my %tmpl = (
126 perlbal => 'http://' . $class->lan_ip . ':' . $Config->sl_perlbal_port,
127 cdn_host => $Config->sl_cdn_host,
128 mac => URI::Escape::uri_escape($mac),
129 url => URI::Escape::uri_escape($dest_url),
130 cp_mac => URI::Escape::uri_escape( $class->wan_mac ),
131 provider_href => $Config->sl_account_website,
132 provider_logo => $Config->sl_account_logo,
133 );
134  
135 $r->content_type('text/html; charset=UTF-8');
136 $r->no_cache(1);
137 $r->rflush;
138  
139 my $output;
140 $Template->process( 'splash.tmpl', \%tmpl, \$output )
141 || die $Template->error;
142 $r->print($output);
143  
144 return Apache2::Const::OK;
145 }
146  
147 # authentication requests for free service
148  
149 sub free {
150 my ( $class, $r ) = @_;
151  
152 my $ip = $r->connection->remote_ip;
153 my $mac = $class->mac_from_ip($ip);
154 return Apache2::Const::NOT_FOUND unless $mac;
155  
156 my $req = Apache2::Request->new($r);
157 my $url = $req->param('url');
158  
159 # serve the page or process the request
160 if ( $r->method_number == Apache2::Const::M_GET ) {
161  
162 my %tmpl = (
163 perlbal => 'http://'
164 . $class->lan_ip . ':'
165 . $Config->sl_perlbal_port,
166 cdn_host => $Config->sl_cdn_host,
167 mac => URI::Escape::uri_escape($mac),
168 url => $url, #URI::Escape::uri_escape($url),
169 cp_mac => URI::Escape::uri_escape( $class->wan_mac ),
170 provider_href => $Config->sl_account_website,
171 provider_logo => $Config->sl_account_logo,
172 );
173  
174 my $output;
175 $Template->process( 'free.tmpl', \%tmpl, \$output )
176 || die $Template->error;
177 $r->content_type('text/html; charset=UTF-8');
178 $r->no_cache(1);
179 $r->rflush;
180 $r->print($output);
181 return Apache2::Const::OK;
182  
183 }
184 elsif ( $r->method_number == Apache2::Const::M_POST ) {
185  
186 my $terms = $req->param('terms');
187  
188 unless ($terms) {
189 $r->method_number(Apache2::Const::M_GET);
190 return $class->free($r);
191 }
192  
193 # they clicked yes, authenticate
194 my $added = App::SilverSplash::IPTables->add_to_ads_chain( $mac, $ip );
195  
196 unless ($added) {
197  
198 $r->log->error("$$ error adding mac $mac to ads chain: $@");
199 return Apache2::Const::SERVER_ERROR;
200 }
201  
202 my $location =
203 $class->make_post_url( $Config->sl_splash_href,
204 URI::Escape::uri_escape($url) );
205  
206 $r->headers_out->set( Location => $location );
207 $r->no_cache(1);
208 return Apache2::Const::REDIRECT;
209 }
210 }
211  
212 sub paid {
213 my ( $class, $r ) = @_;
214  
215 my $ip = $r->connection->remote_ip;
216 my $mac = $class->mac_from_ip($ip);
217 return Apache2::Const::NOT_FOUND unless $mac;
218  
219 my $req = Apache2::Request->new($r);
220 my $url = $req->param('url');
221  
222 # serve the page or process the request
223 if ( $r->method_number == Apache2::Const::M_GET ) {
224  
225 my $p = Business::PayPal->new;
226 my $id = $p->id;
227 my $b = $p->button(
228 'item_number' => 1,
229 'business' => $Config->sl_paypal_account,
230 'item_name' => 'airCloud WiFi Purchase',
231 'notify_url' => 'http://'
232 . $Config->sl_dmz_listen
233 . "/ipn_validate?url=$url&id=$id",
234 'return' => $Config->sl_splash_href,
235 'cancel_return' => 'http://'
236 . $class->lan_ip
237 . ":9999/paid?url=$url",
238 'amount' => '0.05',
239 'quantity' => 1,
240 'button_image' => CGI::image_button(
241 -name => 'submit',
242 -src =>
243 'http://s1.slwifi.com/images/buttons/3_hour_service.png',
244 -alt => 'Make payments with PayPal',
245 ),
246 );
247  
248 $class->set( "paypal_id|$id" => "$mac|$ip" );
249  
250 my %tmpl = (
251 perlbal => 'http://'
252 . $class->lan_ip . ':'
253 . $Config->sl_perlbal_port,
254 cdn_host => $Config->sl_cdn_host,
255 mac => URI::Escape::uri_escape($mac),
256 url => $url, #URI::Escape::uri_escape($url),
257 cp_mac => URI::Escape::uri_escape( $class->wan_mac ),
258 provider_href => $Config->sl_account_website,
259 provider_logo => $Config->sl_account_logo,
260 button => $b,
261 );
262  
263 my $output;
264 $Template->process( 'paid.tmpl', \%tmpl, \$output )
265 || die $Template->error;
266 $r->content_type('text/html; charset=UTF-8');
267 $r->no_cache(1);
268 $r->rflush;
269 $r->print($output);
270 return Apache2::Const::OK;
271  
272 }
273 elsif ( $r->method_number == Apache2::Const::M_POST ) {
274  
275 my $terms = $req->param('terms');
276  
277 unless ($terms) {
278 $r->method_number(Apache2::Const::M_GET);
279 return $class->free($r);
280 }
281  
282 # they clicked yes, authenticate
283  
284 my $added = App::SilverSplash::IPTables->add_to_paid_chain( $mac, $ip );
285  
286 unless ($added) {
287  
288 $r->log->error("$$ error adding mac $mac to paid chain: $@");
289 return Apache2::Const::SERVER_ERROR;
290 }
291 }
292 $mac = URI::Escape::uri_escape($mac);
293 $url = URI::Escape::uri_escape($url);
294  
295 # else we have an authenticated user
296 my $location = $class->make_post_url( $Config->sl_splash_href, $url );
297 $r->headers_out->set( Location => $location );
298 $r->no_cache(1);
299 return Apache2::Const::REDIRECT;
300 }
301  
302 sub make_post_url {
303 my ( $class, $splash_url, $dest_url ) = @_;
304  
305 $dest_url = URI::Escape::uri_escape($dest_url);
306 my $separator = ( $splash_url =~ m/\?/ ) ? '&' : '?';
307  
308 my $location = $splash_url . $separator . "url=$dest_url";
309  
310 return $location;
311 }
312  
313 1;
314  
315 __END__