File Coverage

blib/lib/Captcha/AreYouAHuman.pm
Criterion Covered Total %
statement 23 67 34.3
branch 3 16 18.7
condition n/a
subroutine 5 8 62.5
pod 5 5 100.0
total 36 96 37.5


line stmt bran cond sub pod time code
1             package Captcha::AreYouAHuman;
2             $VERSION = 0.00003;
3              
4             # Are You a Human Perl Integration Library
5             # Copyright December 5, 2011 Are You a Human LLC
6             #
7             # Sign up for a publisher key at www.areyouahuman.com!
8             #
9             # AUTHOR:
10             # Jonathan Brown - jonathan@areyouahuman.com
11             #
12             #
13             # This program is free software; you can redistribute it and/or modify
14             # it under the terms of the GNU General Public License as published by
15             # the Free Software Foundation; either version 2 of the License, or
16             # (at your option) any later version.
17             #
18             # This program is distributed in the hope that it will be useful,
19             # but WITHOUT ANY WARRANTY; without even the implied warranty of
20             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21             # GNU General Public License for more details.
22             #
23             # You should have received a copy of the GNU General Public License along
24             # with this program; if not, write to the Free Software Foundation, Inc.,
25             # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26             #
27              
28 1     1   72625 use LWP::UserAgent;
  1         131123  
  1         33  
29 1     1   9 use URI::Escape;
  1         3  
  1         89  
30 1     1   1205 use JSON;
  1         16266  
  1         6  
31              
32             # Create a new instance;
33             # parameters are passed with key => value
34             # with parameters server, publisher_key, and scoring_key
35             sub new {
36             # make a hash-based object
37 1     1 1 333 my $class = shift;
38 1         3 my $self = {};
39 1         3 bless($self, $class);
40              
41 1         3 my %params = @_;
42              
43 1 50       4 if ($params{"server"} ne "") {
44 0         0 $self->{"server"} = $params{"server"};
45             } else {
46 1         8 $self->{"server"} = "ws.areyouahuman.com";
47             }
48              
49 1 50       4 if ($params{"publisher_key"} eq "") {
50 1         3 $self->errorLog("Called Captcha::AreYouAHuman integration without publisher_key");
51             } else {
52 0         0 $self->{"publisher_key"} = $params{"publisher_key"};
53             }
54              
55 1 50       5 if ($params{"scoring_key"} eq "") {
56 1         4 $self->errorLog("Called Captcha::AreYouAHuman integration without scoring_key");
57             } else {
58 0         0 $self->{"scoring_key"} = $params{"scoring_key"};
59             }
60              
61 1         6 return $self;
62             }
63              
64              
65             # Get the HTML that gets embedded
66             # Returns the string to be echoed out to the browser.
67             sub getPublisherHTML {
68 0     0 1 0 my $self = shift;
69              
70             # Get the variables out
71 0         0 my $server = $self->{"server"};
72 0         0 my $publisher_key = $self->{"publisher_key"};
73              
74 0         0 return "
";
77             }
78              
79             # Score the results
80             # parameters are passed with key => value
81             # with parameters client_ip and session_secret
82             #
83             # If you are using CGI.pm, call as
84             # $cgi = new CGI();
85             # my $ayah = new Captcha::AreYouAHuman;
86             # my $result = $ayah->scoreResult(
87             # 'session_secret' => $cgi->param('session_secret'),
88             # 'client_ip' => $cgi->remote_host()
89             # );
90             #
91             # Returns 0/false if failed; true if passed.
92             #
93             sub scoreResult {
94 0     0 1 0 my $self = shift;
95              
96 0         0 my %params = @_;
97              
98             # get the variables out
99 0         0 my $server = $self->{"server"};
100 0         0 my $client_ip = "";
101 0         0 my $session_secret = "";
102              
103 0 0       0 if ($params{"session_secret"} eq "") {
104 0         0 $self->errorLog("Called Captcha::AreYouAHuman::scoreResult without a session_secret");
105 0         0 return 0;
106             } else {
107 0         0 $session_secret = $params{"session_secret"};
108             }
109              
110 0 0       0 if ($params{"client_ip"} eq "") {
111 0         0 $self->errorLog("Called Captcha::AreYouAHuman::scoreResult without a client_ip");
112 0         0 return 0;
113             } else {
114 0         0 $client_ip = $params{"client_ip"};
115             }
116              
117              
118             # Make the request
119 0         0 my $ua = LWP::UserAgent->new;
120 0         0 my $req = HTTP::Request->new(POST => 'https://' . $server .
121             '/ayahwebservices/index.php/ayahwebservice/scoreGame');
122 0         0 $req->content_type('application/x-www-form-urlencoded');
123 0         0 $req->content('session_secret=' . uri_escape($session_secret) . '&client_ip=' .
124             uri_escape($client_ip));
125 0         0 my $res = $ua->request($req);
126              
127 0 0       0 if ($res->is_success) {
128             # JSON decode and evaluate result
129 0         0 my $results;
130 0         0 eval {
131 0         0 $results = decode_json($res->content);
132             };
133              
134 0 0       0 if ($@) {
135 0         0 $self->errorLog("Could not JSON decode: " . $res->content);
136 0         0 return 0;
137             } else {
138 0         0 return ($results->{'status_code'} == 1);
139             }
140             } else {
141 0         0 $self->errorLog("Error: Internal error: " . $res->status_line);
142 0         0 return 0;
143             }
144             }
145              
146             # Record the conversion
147             # parameters are passed with key => value
148             # with parameters session_secret
149             #
150             # If you are using CGI.pm, call as
151             # $cgi = new CGI();
152             # my $ayah = new Captcha::AreYouAHuman;
153             # my $result = $ayah->scoreResult(
154             # 'session_secret' => $cgi->param('session_secret')
155             # );
156             #
157             # Returns the HTML string to be inserted into the conversion page. (Hidden iframe)
158             #
159             sub recordConversion {
160 0     0 1 0 my $self = shift;
161              
162 0         0 my %params = @_;
163              
164             # get the variables out
165 0         0 my $server = $self->{"server"};
166 0         0 my $session_secret = "";
167              
168 0 0       0 if ($params{"session_secret"} eq "") {
169 0         0 $self->errorLog("Called Captcha::AreYouAHuman::scoreResult without a session_secret");
170 0         0 return "";
171             } else {
172 0         0 $session_secret = $params{"session_secret"};
173             }
174              
175 0         0 return '';
177             }
178              
179             # Error logging function; override if you don't want this making noise.
180             # Parameter: Error message
181             # Default behavior: Outputs to the STDERR
182             sub errorLog {
183 2     2 1 3 my $self = shift;
184 2         4 my $message = shift;
185              
186 2         114 print STDERR "Error: Captcha::AreYouAHuman: " . $message . "\n";
187             }
188              
189             # EOF
190             1;
191             __END__