blib/lib/Mojolicious/Plugin/Recaptcha.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 65 CC 6 SCAR 32.6
CC
6
Cov
0%
register
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 26.0
1 package Mojolicious::Plugin::Recaptcha;
2  
3 use strict;
4 use Mojo::ByteStream;
5 use Mojo::JSON qw/encode_json/;
6  
7 use base 'Mojolicious::Plugin';
8 our $VERSION = '0.64';
9  
10 sub register {
11 my ($self,$app,$conf) = @_;
12  
13 $conf->{'lang'} ||= 'en';
14 my $options = {
15 %$conf
16 };
17  
18 delete $options->{'private_key'};
19 delete $options->{'public_key'};
20 delete $options->{'ssl'};
21  
22 my $scheme = ($conf->{'ssl'}) ? 'https' : 'http';
23  
24 my $r_options = encode_json($options);
25 $app->renderer->add_helper(
26 recaptcha_html => sub {
27 my $self = shift;
28  
29 my $lang = shift;
30  
31 my ($error) = map { $_ ? "&error=$_" : "" } $self->stash('recaptcha_error');
32 return Mojo::ByteStream->new(<<HTML);
33 <script type="text/javascript">
34 var RecaptchaOptions = $r_options;
35 </script>
36 <script type="text/javascript"
37 src="$scheme://www.google.com/recaptcha/api/challenge?hl=$lang&k=$conf->{public_key}$error">
38 </script>
39 <noscript>
40 <iframe src="$scheme://www.google.com/recaptcha/api/noscript?hl=$lang&k=$conf->{public_key}"
41 height="300" width="500" frameborder="0"></iframe><br>
42 <textarea name="recaptcha_challenge_field" rows="3" cols="40">
43 </textarea>
44 <input type="hidden" name="recaptcha_response_field"
45 value="manual_challenge">
46 </noscript>
47 HTML
48  
49 },
50 );
51 $app->renderer->add_helper(
52 recaptcha => sub {
53 my ($self,$cb) = @_;
54  
55 my @post_data = (
56 'http://www.google.com/recaptcha/api/verify',
57 form => {
58 privatekey => $conf->{'private_key'},
59 remoteip =>
60 $self->req->headers->header('X-Real-IP')
61 ||
62 $self->tx->remote_address,
63 challenge => $self->req->param('recaptcha_challenge_field'),
64 response => $self->req->param('recaptcha_response_field')
65 }
66 );
67 my $callback = sub {
68 my $content = $_[1]->res->to_string;
69 my $result = $content =~ /true/;
70  
71 $self->stash(recaptcha_error => $content =~ m{false\s*(.*)$}si)
72 unless $result
73 ;
74 $cb->($result) if $cb;
75 return $result;
76 };
77  
78 if ($cb) {
79 $self->ua->post(
80 @post_data,
81 $callback,
82 );
83 } else {
84 my $tx = $self->ua->post(@post_data);
85  
86 return $callback->('',$tx);
87 }
88 }
89 );
90 }
91  
92 1;
93  
94 =head1 NAME
95  
96 Mojolicious::Plugin::Recaptcha - ReCaptcha plugin for Mojolicious framework
97  
98 =head1 VERSION
99  
100 0.64
101  
102 =head1 SYNOPSIS
103  
104 # Mojolicious::Lite
105 plugin recaptcha => {
106 public_key => '...',
107 private_key => '...',
108 lang => 'ru'
109 };
110  
111 # Mojolicious
112 $self->plugin(recaptcha => {
113 public_key => '...',
114 private_key => '...',
115 lang => 'ru',
116 ssl => 1, # uses https Google URLs
117 });
118  
119 # template
120 <form action="" method="post">
121 <%= recaptcha_html %>
122 <input type="submit" value="submit" name="submit" />
123 </form>
124  
125 # set reCaptcha widget language from template
126 <form action="" method="post">
127 <%= recaptcha_html 'ru' %>
128 <input type="submit" value="submit" name="submit" />
129 </form>
130  
131 # checking blocking way
132 $self->recaptcha;
133 unless ($self->stash('recaptcha_error')) {
134 # all ok
135 }
136  
137 # checking non-blocking way
138 $self->render_later;
139 $self->recaptcha(sub {
140 my $ok = shift;
141 if ($ok) {
142  
143 } else {
144 warn $self->stash('recaptcha_error');
145 }
146 # here you need call render
147 $self->render;
148 })
149  
150 =head1 Internationalisation support
151  
152 =over 4
153  
154 =item * English by default (en)
155  
156 =item * Dutch (nl)
157  
158 =item * French (fr)
159  
160 =item * German (de)
161  
162 =item * Portuguese (pt)
163  
164 =item * Russian (ru)
165  
166 =item * Spanish (es)
167  
168 =item * Turkish (tr)
169  
170 =back
171  
172 =head1 Options
173  
174 Plugin support all recaptcha options:
175 L<https://developers.google.com/recaptcha/docs/customization>
176  
177 =head1 SUPPORT
178  
179 =over 4
180  
181 =item * Repository
182  
183 L<http://github.com/konstantinov/Mojolicious-Plugin-Recaptcha>
184  
185 =back
186  
187 =head1 SEE ALSO
188  
189 L<Mojolicious>, L<Mojolicious::Plugin>, L<Mojolicious::Lite>
190  
191 =head1 THANKS
192  
193 Special thanks for help in development
194  
195 =over 2
196  
197 Alexander Voronov
198  
199 Tim Wilde
200  
201 Sangre
202  
203 Fayland Lam
204  
205 BERENYI Mihaly
206  
207 =back
208  
209 =head1 COPYRIGHT & LICENSE
210  
211 Copyright 2010-2016 Dmitry Konstantinov. All right reserved.
212  
213 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.