blib/lib/Dancer2/Plugin/Captcha.pm

filter: Statementstmt 100% 20 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 27 / 27 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Dancer2::Plugin::Captcha;
2  
3 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 our $VERSION = qv('v1.0.0');
6 our $AUTHORITY = 'cpan:MANWAR';
7  
8 =head1 NAME
9  
10 Dancer2::Plugin::Captcha - Dancer2 add-on for CAPTCHA.
11  
12 =head1 VERSION
13  
14 Version v1.0.0
15  
16 =cut
17  
18 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use GD::SecurityImage;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 DESCRIPTION
26  
27 A very simple plugin for L<Dancer2> to process CAPTCHA.I needed this for my other
28 project (in-progress) available on L<github|https://github.com/Manwar/Dancer2-Cookbook>.
29  
30 The core functionality of the plugin is supported by L<GD::SecurityImage>.
31  
32 =head1 SYNOPSIS
33  
34 =head2 Setting up the application configuration.
35  
36 The plugin expect a session engine configured for it to perform its task,
37  
38 session: Simple
39  
40 plugins:
41 Captcha:
42 new:
43 width: 160
44 height: 175
45 lines: 5
46 gd_font: 'giant'
47 create: [ 'normal', 'default' ]
48 out:
49 force: 'png'
50 particle: [ 100 ]
51  
52 =head2 Setting up the application route handler.
53  
54 get '/get_captcha' => sub {
55 return generate_captcha();
56 };
57  
58 post '/validate_captcha' => sub {
59 return "Invalid captcha code."
60 unless (is_valid_captcha(request->params->{captcha}));
61  
62 remove_captcha;
63 };
64  
65 =head2 Setting up the application template.
66  
67 <form method="POST" action="/validate_captcha">
68 <label>Enter the Captcha</label>
69 <input type="text" name="captcha" style="width: 120px;">
70 <img id="img1" src="<% request.uri_base %>/get_captcha" style="margin-bottom:20px;">
71 <button type="submit">Submit</button>
72 </form>
73  
74 =head1 CONFIGURATION
75  
76 The plugin can be configured in the application configuration file as below:
77  
78 plugins:
79 Captcha:
80 new:
81 create:
82 particle:
83 out:
84  
85 =head2 new
86  
87 The following keys can be assigned to method 'new':
88  
89 +------------+--------------------------------------------------------------+
90 | Key | Description |
91 +------------+--------------------------------------------------------------+
92 | width | The width of the image (in pixels). |
93 | height | The height of the image (in pixels). |
94 | ptsize | The point size of the ttf character. |
95 | lines | The number of lines in the background of the image. |
96 | font | The absolute path to your TrueType font file. |
97 | gd_font | The possible value are 'small', 'large', 'mediumbold', |
98 | | 'tiny' and 'giant'. |
99 | bgcolor | The background color of the image. |
100 | send_ctobg | If has a true value, the random security code will be |
101 | | displayed in the background and the lines will pass over it. |
102 | frame | If has a true value, a frame will be added around the image. |
103 | scramble | If set, the characters will be scrambled. |
104 | angle | Sets the angle (0-360) for scrambled/normal characters. |
105 | thickness | Sets the line drawing width. |
106 | rndmax | The minimum length if the random string. Default is 6. |
107 | rnd_data | Default character set used to create the random string is |
108 | | [0..9]. |
109 +------------+--------------------------------------------------------------+
110  
111 See L<GD::SecurityImage/new> for more details.
112  
113 =head2 create
114  
115 The data should be in the format as below for method 'create':
116  
117 [ $method, $style, $text_color, $line_color ]
118  
119 The key C<$method> and C<$style> are mandatory and the rest all are optionals.
120  
121 The key C<$method> can have one of the following values:
122  
123 normal or ttf
124  
125 The key C<$style> can have one of the following values:
126  
127 +---------+-----------------------------------------------------------------+
128 | Key | Description |
129 +---------+-----------------------------------------------------------------+
130 | default | The default style. Draws horizontal, vertial and angular lines. |
131 | rect | Draws horizontal and vertical lines. |
132 | box | Draws two filled rectangles. |
133 | circle | Draws circles. |
134 | ellipse | Draws ellipses. |
135 | ec | Draws both ellipses and circles. |
136 | blank | Draws nothing. |
137 +---------+-----------------------------------------------------------------+
138  
139 =head2 out
140  
141 The following keys can be assigned to method 'out':
142  
143 +----------+----------------------------------------------------------------+
144 | Key | Description |
145 +----------+----------------------------------------------------------------+
146 | force | Can have one of the formats 'jpeg' or 'png' or 'gif'. |
147 | compress | Can be between 1 and 100. |
148 +----------+----------------------------------------------------------------+
149  
150 =head2 particle
151  
152 The data should be in the format as below for method 'particle':
153  
154 [ $density, $maximum_dots ]
155  
156 The default value for C<$density> is dependent on the image's height & width. The
157 greater value of height and width is taken and multiplied by 20 for defaults.
158  
159 The key C<$maximum_dots> defines the maximum number of dots near the default dot.
160 The default value is 1. If you set it to 4, the selected pixel and 3 other pixels
161 near it will be used and colored.
162  
163 =head1 METHODS
164  
165 =head2 generate_captcha(\%params, $id)
166  
167 It returns captcha image as per the given parameters and C<$id> is the captcha
168 ID (optional, default value is 'default'). If the key 'random' is not defined
169 then the default character sets [0..9] will be used.
170  
171 get '/get_captcha' => sub {
172 return generate_captcha({
173 new => {
174 width => 500,
175 height => 75,
176 lines => 5,
177 gd_font => 'giant',
178 },
179 particle => [ 100 ],
180 out => { force => 'png' },
181 random => <your_randomly_generated_string>,
182 });
183 };
184  
185 =cut
186  
187 register generate_captcha => sub {
188 my ($dsl, $init, $id) = @_;
189  
190 $init = {} unless defined $init;
191 $id = 'default' unless defined $id;
192  
193 my $conf = plugin_setting();
194  
195 foreach my $key (qw(new out)) {
196 $conf->{$key} //= {};
197 $init->{$key} //= {};
198 $init->{$key} = { %{$conf->{$key}}, %{$init->{$key}} };
199 }
200  
201 foreach my $key (qw(create particle)) {
202 $conf->{$key} //= [];
203 $init->{$key} ||= $conf->{$key};
204 }
205  
206 $dsl->engine('session')
207 or die "ERROR: Session engine required for the plugin ".__PACKAGE__.".\n";
208  
209 my $image = GD::SecurityImage->new(%{$init->{new}});
210 $image->random($init->{random});
211 $image->create(@{$init->{create}});
212 $image->particle(@{$init->{particle}});
213 my ($captcha, $mime_type, $random_number) = $image->out(%{$init->{out}});
214  
215 $dsl->_save_captcha($id, 'string' => $random_number);
216 my $set_header = $dsl->can('response_header') ? 'response_header' : 'header';
217 $dsl->$set_header('Pragma' => 'no-cache');
218 $dsl->$set_header('Cache-Control' => 'no-cache');
219 $dsl->content_type($mime_type);
220  
221 return $captcha;
222 };
223  
224 =head2 is_valid_captcha($input, $id)
225  
226 The C<$input> is the captcha code entered by the user and C<$id> is the captcha
227 ID (optional, default value is 'default'). It returns 0 or 1 depending on whether
228 the captcha matches or not.
229  
230 post '/validate_captcha' => sub {
231 return "Invalid captcha code."
232 unless (is_valid_captcha(request->params->{captcha}));
233  
234 remove_captcha;
235 };
236  
237 =cut
238  
239 register is_valid_captcha => sub {
240 my ($dsl, $input, $id) = @_;
241  
242 $id = 'default' unless defined $id;
243 my $captcha = $dsl->_get_captcha($id, 'string');
244 ((defined $input) && (defined $captcha) && ($input eq $captcha))
245 ?
246 (return 1)
247 :
248 (return 0);
249 };
250  
251 =head2 remove_captcha($id)
252  
253 The C<$id> is the captcha ID (optional, default value is 'default').
254 It removes the captcha from the session.
255  
256 =cut
257  
258 register remove_captcha => sub {
259 my ($dsl, $id) = @_;
260  
261 $id = 'default' unless defined $id;
262  
263 my $captcha = $dsl->session('captcha');
264 return unless defined $captcha;
265 return unless ((exists $captcha->{$id}) && (exists $captcha->{$id}{'string'}));
266  
267 $captcha->{$id}{'string'} = undef;
268 $dsl->session('captcha' => $captcha);
269 };
270  
271 register_plugin;
272  
273 #
274 #
275 # PRIVATE METHODS
276  
277 sub _save_captcha {
278 my ($dsl, $id, $key, $value) = @_;
279  
280 die "ERROR: Missing captcha ID.\n" unless defined $id;
281 die "ERROR: Missing captcha key.\n" unless defined $key;
282 die "ERROR: Missing captcha value.\n" unless defined $value;
283  
284 my $captcha = $dsl->app->session->read('captcha') || {};
285 $captcha->{$id} ||= {};
286 $captcha->{$id}{$key} = $value;
287 $dsl->app->session->write('captcha' => $captcha);
288 }
289  
290 sub _get_captcha {
291 my ($dsl, $id, $key) = @_;
292  
293 die "ERROR: Missing captcha ID.\n" unless defined $id;
294 die "ERROR: Missing captcha key.\n" unless defined $key;
295  
296 my $captcha = $dsl->app->session->read('captcha');
297 return unless defined $captcha;
298  
299 return $captcha->{$id}{$key};
300 }
301  
302 =head1 AUTHOR
303  
304 Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
305  
306 =head1 REPOSITORY
307  
308 L<https://github.com/manwar/Dancer2-Plugin-Captcha>
309  
310 =head1 ACKNOWLEDGEMENTS
311  
312 Inspired by the package L<Dancer::Plugin::Captcha::SecurityImage> (Alessandro Ranellucci <aar@cpan.org>).
313  
314 =head1 SEE ALSO
315  
316 L<Dancer::Plugin::Captcha::SecurityImage>, L<GD::SecurityImage>
317  
318 =head1 BUGS
319  
320 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/Dancer-Plugin-Captcha/issues>.
321 I will be notified and then you'll automatically be notified of progress on your
322 bug as I make changes.
323  
324 =head1 SUPPORT
325  
326 You can find documentation for this module with the perldoc command.
327  
328 perldoc Dancer2::Plugin::Captcha
329  
330 You can also look for information at:
331  
332 =over 4
333  
334 =item * BUG Report
335  
336 L<https://github.com/manwar/Dancer-Plugin-Captcha/issues>
337  
338 =item * Search MetaCPAN
339  
340 L<https://metacpan.org/dist/Dancer-Plugin-Captcha/>
341  
342 =back
343  
344 =head1 LICENSE AND COPYRIGHT
345  
346 Copyright (C) 2015 - 2026 Mohammad Sajid Anwar.
347  
348 This program is free software; you can redistribute it and/or modify it under
349 the terms of the Artistic License (2.0).
350  
351 =cut
352  
353 1; # End of Dancer2-Plugin-Captcha
↑ Summary