blib/lib/Mojolicious/Plugin/Qaptcha.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Mojolicious::Plugin::Qaptcha;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use FindBin qw'$Bin';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Util 'slurp';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use File::Basename 'dirname';
7 use File::Spec;
8 use File::ShareDir;
9  
10 our $VERSION = '0.11';
11  
12 sub register {
13 my ($self, $app, $config) = @_;
14 $app->config->{$_} = $config->{$_} for keys %$config;
15  
16 $app->config->{qaptcha_url} ||= q|/qaptcha|;
17  
18 $app->helper(qaptcha_include => \&_qaptcha_include);
19 $app->helper(qaptcha_is_unlocked => \&_is_unlocked);
20  
21 my $r = $app->routes;
22 $r->route($app->config->{qaptcha_url})->to(
23 cb => sub {
24 my $self = shift;
25 my $aResponse = {};
26 $aResponse->{error} = 0;
27  
28 if ($self->param('action') && $self->param('qaptcha_key')) {
29 $self->session('qaptcha_key', undef);
30 if ($self->param('action') eq 'qaptcha') {
31 $self->session('qaptcha_key', $self->param('qaptcha_key'));
32 }
33 else {
34 $aResponse->{error} = 1;
35 }
36 return $self->render(json => $aResponse);
37 }
38 else {
39 $aResponse->{error} = 1;
40 return $self->render(json => $aResponse);
41 }
42 }
43 );
44 $r->route('/images/bg_draggable_qaptcha.jpg')->to(
45 cb => sub {
46 my $self = shift;
47 $self->render(
48 data => slurp(&_basedir . "/bg_draggable_qaptcha.jpg"),
49 format => 'jpg'
50 );
51 }
52 );
53 $app->hook(
54 after_dispatch => sub {
55 my $c = shift;
56 $c->session('qaptcha_key', '')
57 if $c->req->url->path->to_string ne $app->config->{qaptcha_url};
58 }
59 );
60 }
61  
62 sub _qaptcha_include {
63 my $c = shift;
64 my $url_base = shift;
65  
66 my $cfg = $c->app->config;
67  
68 my $jquery = $cfg->{inbuild_jquery}
69 && $cfg->{inbuild_jquery} == 1 ? slurp(&_basedir . "/jquery.js") : '';
70 my $jquery_ui
71 = $cfg->{inbuild_jquery_ui} && $cfg->{inbuild_jquery_ui} == 1
72 ? slurp(&_basedir . "/jquery-ui.js")
73 : '';
74 my $jquery_ui_touch
75 = $cfg->{inbuild_jquery_ui_touch} && $cfg->{inbuild_jquery_ui_touch} == 1
76 ? slurp(&_basedir . "/jquery.ui.touch.js")
77 : '';
78 my $qaptcha_js = slurp(&_basedir . "/QapTcha.jquery.js");
79 my $qaptcha_css = slurp(&_basedir . "/QapTcha.jquery.css");
80  
81 $cfg->{txtLock} ||= q|Locked : form can't be submited|;
82 $cfg->{txtUnlock} ||= q|Unlocked : form can be submited|;
83 $cfg->{disabledSubmit} ||= q|false|;
84 $cfg->{autoRevert} ||= q|true|;
85 $cfg->{autoSubmit} ||= q|false|;
86  
87 require Mojo::DOM;
88 my $script = <<EOS;
89 <script type="text/javascript">
90 $jquery
91 $jquery_ui
92 $jquery_ui_touch
93 $qaptcha_js
94 \$(document).ready(function(){
95 \$('.QapTcha').QapTcha({
96 txtLock : "$cfg->{txtLock}",
97 txtUnlock : "$cfg->{txtUnlock}",
98 disabledSubmit : $cfg->{disabledSubmit},
99 PHPfile : '$cfg->{qaptcha_url}',
100 autoRevert : $cfg->{autoRevert},
101 autoSubmit : $cfg->{autoSubmit}
102 });
103 });
104 </script>
105 <style>$qaptcha_css</style>
106 EOS
107 my $dom = Mojo::DOM->new($script);
108 $dom->xml(1);
109  
110 require Mojo::ByteStream;
111 return Mojo::ByteStream->new($dom->to_string);
112 }
113  
114 sub _is_unlocked {
115 my $self = shift;
116 if ($self->session('qaptcha_key')) {
117 no warnings 'uninitialized';
118 if ($self->req->param($self->session('qaptcha_key')) eq '') {
119 return 1;
120 }
121 }
122 return 0;
123 }
124  
125 sub _basedir {
126 my $dir
127 = File::Spec->catdir(
128 dirname(__FILE__) . "/../../../jquery" //
129 File::ShareDir::dist_dir('Mojolicious-Plugin-Qaptcha'));
130 return $dir;
131 }
132  
133 =head1 NAME
134  
135 Mojolicious::Plugin::Qaptcha - jQuery QapTcha Plugin for Mojolicious
136  
137 =head1 SYNOPSIS
138  
139 # Mojolicious
140 $app->plugin('Qaptcha', {
141 inbuild_jquery => 1,
142 inbuild_jquery_ui => 1,
143 inbuild_jquery_ui_touch => 1,
144 txtLock => "LOCKED",
145 txtUnlock => "UNLOCKED",
146 disabledSubmit => "true",
147 autoRevert => "false",
148 autoSubmit => "true",
149 qaptcha_url => '/do_unlock',
150 });
151  
152 # Mojolicious::Lite
153 plugin 'Qaptcha', {
154 inbuild_jquery => 1,
155 inbuild_jquery_ui => 1,
156 inbuild_jquery_ui_touch => 1,
157 txtLock => "LOCKED",
158 txtUnlock => "UNLOCKED",
159 disabledSubmit => "true",
160 autoRevert => "false",
161 autoSubmit => "true",
162 qaptcha_url => '/do_unlock',
163 };
164  
165 and in your templates
166  
167 @@ layouts/default.html.ep
168 <!DOCTYPE html>
169 <html>
170 <head>
171 %= qaptcha_include
172 </head>
173 <body>
174 %= content;
175 </body>
176 </html>
177  
178 @@ index.html.ep
179 %= layout 'default';
180 <form method="post">
181 <fieldset>
182 <label>First Name</label> <input name="firstname" type="text"><br>
183 <label>Last Name</label> <input name="lastname" type="text">
184 <input name="submit" value="Submit form" style="margin-top:15px;" type="submit">
185 <br />
186 <!-- put a qaptcha element inside a form -->
187 <div class="QapTcha"></div>
188  
189 </fieldset>
190 </form>
191  
192 and in your controller
193  
194 # Mojolicious::Lite
195 any '/' => sub {
196 my $self = shift;
197  
198 do_something if $self->qaptcha_is_unlocked;
199  
200 $self->render('index');
201 };
202  
203 # Mojolicious
204 sub index {
205 my $self = shift;
206  
207 do_something if $self->qaptcha_is_unlocked;
208  
209 $self->render('index');
210 }
211  
212 =head1 DESCRIPTION
213  
214 L<Mojolicious::Plugin::Qaptcha> is a L<Mojolicious> plugin.
215  
216 It brings jQuery QapTcha functionality inside your html form
217 in an element with class 'QapTcha'.
218 When QapTcha is unlocked, next request has to
219 submit form. Otherwise QapTcha will be locked back.
220  
221 =head1 METHODS
222  
223 L<Mojolicious::Plugin::Qaptcha> inherits all methods from
224 L<Mojolicious::Plugin> and implements the following new ones.
225  
226 =head2 register
227  
228 $plugin->register(Mojolicious->new, $options_hash);
229  
230 Register plugin in L<Mojolicious> application.
231  
232  
233 =head2 HELPERS
234  
235 =over 4
236  
237 =item qaptcha_include
238  
239 Includes (optional configured) jquery and qaptcha javascript.
240  
241 =item qaptcha_is_unlocked
242  
243 Returns 1 if QapTcha is unlocked.
244  
245 =back
246  
247 =head2 OPTIONS
248  
249 =over 4
250  
251 =item inbuild_jquery
252  
253 If set to 1 jQuery 1.8.2 is rendered into %= qaptcha_include.
254  
255 =item inbuild_jquery_ui
256  
257 If set to 1 jQuery UI - v1.8.2 is rendered into %= qaptcha_include.
258  
259 =item inbuild_jquery_ui_touch
260  
261 If set to 1 jQuery.UI.iPad plugin is rendered into %= qaptcha_include.
262  
263 =item txtLock
264  
265 Text to display for locked QapTcha
266  
267 =item txtUnlock
268  
269 Text to display for unlocked QapTcha
270  
271 =item disabledSubmit
272  
273 Add the "disabled" attribut to the submit button
274 default: false
275  
276 =item autoRevert
277  
278 Slider returns to the init-position, when the user hasn't dragged it to end
279 default: true
280  
281 =item autoSubmit
282  
283 If true, auto-submit form when the user has dragged it to the end
284 default: false
285  
286 =item qaptcha_url
287  
288 Configurable route to unlock qaptcha
289  
290 =back
291  
292 an example is located in L<ex/qaptcha.pl|https://github.com/hrupprecht/Mojolicious-Plugin-Qaptcha/blob/master/ex/qaptcha.pl>.
293  
294 =head1 INSTALLATION
295  
296 To install this module, run the following commands:
297  
298 perl Build.PL
299 ./Build
300 ./Build test
301 ./Build install
302  
303 SUPPORT AND DOCUMENTATION
304  
305 After installing, you can find documentation for this module with the
306 perldoc command.
307  
308 perldoc Mojolicious::Plugin::Qaptcha
309  
310 You can also look for information at:
311  
312 AnnoCPAN, Annotated CPAN documentation
313 http://annocpan.org/dist/Mojolicious-Plugin-Qaptcha
314  
315 CPAN Ratings
316 http://cpanratings.perl.org/d/Mojolicious-Plugin-Qaptcha
317  
318 Search CPAN
319 http://search.cpan.org/dist/Mojolicious-Plugin-Qaptcha/
320  
321 =head1 SOURCE REPOSITORY
322  
323 L<http://github.com/hrupprecht/Mojolicious-Plugin-Qaptcha>
324  
325 =head1 AUTHOR
326  
327 Holger Rupprecht - C<Holger.Rupprecht@gmx.de>
328  
329 =head1 SEE ALSO
330  
331 L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
332  
333 =head1 COPYRIGHT AND LICENSE
334  
335 Copyright (C) 2013 by Holger Rupprecht
336  
337 This library is free software; you can redistribute it and/or modify
338 it under the same terms as Perl itself.
339  
340 =cut
341  
342 1;
↑ Summary