blib/lib/POE/Component/Captcha/reCAPTCHA.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package POE::Component::Captcha::reCAPTCHA;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use POE qw(Component::Client::HTTP);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use HTTP::Request::Common;
8 use Captcha::reCAPTCHA;
9 use vars qw($VERSION);
10  
11 $VERSION = '0.02';
12  
13 use constant API_VERIFY_SERVER => 'http://api-verify.recaptcha.net';
14 use constant SERVER_ERROR => 'recaptcha-not-reachable';
15  
16 # Stolen from POE::Wheel. This is static data, shared by all
17 my $current_id = 0;
18 my %active_identifiers;
19  
20 sub _allocate_identifier {
21 while (1) {
22 last unless exists $active_identifiers{ ++$current_id };
23 }
24 return $active_identifiers{$current_id} = $current_id;
25 }
26  
27 sub _free_identifier {
28 my $id = shift;
29 delete $active_identifiers{$id};
30 }
31  
32  
33 sub spawn {
34 my $package = shift;
35 my %opts = @_;
36 $opts{lc $_} = delete $opts{$_} for keys %opts;
37 my $options = delete $opts{options};
38 my $self = bless \%opts, $package;
39 $self->{session_id} = POE::Session->create(
40 object_states => [
41 $self => { shutdown => '_shutdown',
42 check_answer => '_check_answer',
43 },
44 $self => [ qw(_start _check_answer _dispatch _http_request _http_response) ],
45 ],
46 heap => $self,
47 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
48 )->ID();
49 my $captcha = Captcha::reCAPTCHA->new();
50 $self->{_captcha} = $captcha;
51 return $self;
52 }
53  
54 sub session_id {
55 return $_[0]->{session_id};
56 }
57  
58 sub shutdown {
59 my $self = shift;
60 $poe_kernel->post( $self->{session_id}, 'shutdown' );
61 }
62  
63 sub _start {
64 my ($kernel,$self) = @_[KERNEL,OBJECT];
65 $self->{session_id} = $_[SESSION]->ID();
66 if ( $self->{alias} ) {
67 $kernel->alias_set( $self->{alias} );
68 }
69 else {
70 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
71 }
72 $self->{_httpc} = 'httpc-' . $self->{session_id};
73 POE::Component::Client::HTTP->spawn(
74 Alias => $self->{_httpc},
75 FollowRedirects => 2,
76 );
77 return;
78 }
79  
80 sub _shutdown {
81 my ($kernel,$self) = @_[KERNEL,OBJECT];
82 $kernel->alias_remove( $_ ) for $kernel->alias_list();
83 $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias};
84 $self->{_shutdown} = 1;
85 $kernel->post( $self->{_httpc}, 'shutdown' );
86 undef;
87 }
88  
89 sub get_html {
90 my $self = shift;
91 $self->{_captcha}->get_html( @_ );
92 }
93  
94 sub get_options_setter {
95 my $self = shift;
96 $self->{_captcha}->get_options_setter( @_ );
97 }
98  
99 sub check_answer {
100 my $self = shift;
101 $poe_kernel->post( $self->{session_id}, 'check_answer', @_ );
102 }
103  
104 sub _check_answer {
105 my ($kernel,$self,$state) = @_[KERNEL,OBJECT,STATE];
106 my $sender = $_[SENDER]->ID();
107 return if $self->{_shutdown};
108 my $args;
109 if ( ref( $_[ARG0] ) eq 'HASH' ) {
110 $args = { %{ $_[ARG0] } };
111 } else {
112 $args = { @_[ARG0..$#_] };
113 }
114  
115 $args->{lc $_} = delete $args->{$_} for grep { $_ !~ /^_/ } keys %{ $args };
116  
117 unless ( $args->{event} ) {
118 warn "No 'event' specified for $state";
119 return;
120 }
121  
122 croak
123 "To use reCAPTCHA you must get an API key from http://recaptcha.net/api/getkey"
124 unless $args->{privatekey};
125  
126 croak "For security reasons, you must pass the remote ip to reCAPTCHA"
127 unless $args->{remoteip};
128  
129 $args->{sender} = $sender;
130 $kernel->refcount_increment( $sender => __PACKAGE__ );
131 $kernel->yield( '_http_request', $args );
132  
133 return;
134 }
135  
136 sub _http_request {
137 my ($kernel,$self,$req) = @_[KERNEL,OBJECT,ARG0];
138
139 unless ( $req->{challenge} and $req->{response} ) {
140 $req->{is_valid} = 0;
141 $req->{error} = 'incorrect-captcha-sol';
142 $kernel->yield( '_dispatch', $req );
143 return;
144 }
145  
146 my %postargs = map {
147 ( $_ => $req->{$_} )
148 } qw(privatekey remoteip challenge response);
149  
150 my $id = _allocate_identifier();
151  
152 $kernel->post(
153 $self->{_httpc},
154 'request',
155 '_http_response',
156 POST( API_VERIFY_SERVER . '/verify', \%postargs ),
157 "$id",
158 );
159  
160 $self->{_requests}->{ $id } = $req;
161 return;
162 }
163  
164 sub _http_response {
165 my ($kernel,$self,$request_packet,$response_packet) = @_[KERNEL,OBJECT,ARG0,ARG1];
166 my $id = $request_packet->[1];
167 my $req = delete $self->{_requests}->{ $id };
168 _free_identifier( $id );
169 my $resp = $response_packet->[0];
170 if ( $resp->is_success ) {
171 my ( $answer, $message ) = split( /\n/, $resp->content, 2 );
172 if ( $answer =~ /true/ ) {
173 $req->{is_valid} = 1;
174 }
175 else {
176 chomp $message;
177 $req->{is_valid} = 0; $req->{error} = $message;
178 }
179 }
180 else {
181 $req->{is_valid} = 0; $req->{error} = SERVER_ERROR;
182 }
183  
184 $kernel->yield( '_dispatch', $req );
185 return;
186 }
187  
188 sub _dispatch {
189 my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0];
190 my $session = delete $input->{sender};
191 my $event = delete $input->{event};
192 $kernel->post( $session, $event, $input );
193 $kernel->refcount_decrement( $session => __PACKAGE__ );
194 return;
195 }
196  
197 qq[CAPTCH!];
198  
199 __END__
↑ Summary