File Coverage

blib/lib/Dancer/Plugin/Captcha/SecurityImage.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Dancer::Plugin::Captcha::SecurityImage;
2 1     1   8937 use strict;
  1         1  
  1         27  
3 1     1   5 use warnings;
  1         1  
  1         34  
4              
5             our $VERSION = '0.10';
6              
7 1     1   4 use Carp;
  1         4  
  1         57  
8 1     1   11512 use Dancer ':syntax';
  1         377755  
  1         6  
9 1     1   1326 use Dancer::Plugin;
  1         1353  
  1         79  
10 1     1   167273 use GD::SecurityImage;
  0            
  0            
11              
12             my $conf = plugin_setting;
13              
14             my $session_engine;
15              
16             register create_captcha => sub (;$$) {
17             my $init = pop || {};
18             $init->{$_} ||= $conf->{$_} || {} for qw(new out);
19             $init->{$_} ||= $conf->{$_} || [] for qw(create particle);
20             my $captcha_id = @_ ? shift : 'default';
21            
22             $session_engine ||= engine 'session'
23             or croak __PACKAGE__ . " error: there is no session engine configured. "
24             . "You need a session engine to be able to use this plugin";
25            
26             my $captcha = GD::SecurityImage->new(%{$init->{new}});
27             $captcha->random($init->{random});
28             $captcha->create(@{$init->{create}});
29             $captcha->particle(@{$init->{particle}});
30             my ($image_data, $mime_type, $random_number) = $captcha->out(%{$init->{out}});
31            
32             _captcha_data($captcha_id, 'string' => $random_number);
33            
34             header 'Pragma' => 'no-cache';
35             header 'Cache-Control' => 'no-cache';
36             content_type $mime_type;
37             return $image_data;
38             };
39              
40             register validate_captcha => sub ($;$) {
41             my $user_input = pop;
42             my $captcha_id = @_ ? shift : 'default';
43            
44             my $string = _captcha_data($captcha_id, 'string');
45             return $user_input && $string && $user_input eq $string;
46             };
47              
48             register clear_captcha => sub (;$) {
49             my $captcha_id = shift || 'default';
50             _captcha_data($captcha_id, 'string' => undef);
51             };
52              
53             sub _captcha_data {
54             my ($captcha_id, $key, $val) = @_;
55             my $session_data = session('captcha') || {};
56             $session_data->{$captcha_id} ||= {};
57             if ($val) {
58             $session_data->{$captcha_id}{$key} = $val;
59             session 'captcha' => $session_data;
60             }
61             return $session_data->{$captcha_id}{$key};
62             }
63              
64             register_plugin;
65              
66             1;
67             __END__