blib/lib/PEF/Front/Captcha.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 161 CC 10 SCAR 26.1
CC
10
Cov
0%
initialize
42.0
make_captcha
12.0
check_code
12.0
CRAP 13.5
1 package PEF::Front::Captcha;
2  
3 use strict;
4 use warnings;
5 use Carp;
6  
7 use PEF::Front::Config;
8 use Digest::SHA qw{sha1_hex};
9 use Storable;
10 use MLDBM::Sync;
11 use MLDBM qw(MLDBM::Sync::SDBM_File Storable);
12 use Fcntl qw(:DEFAULT :flock);
13  
14 our $cfg_captcha_db;
15 our $cfg_www_static_captchas_dir;
16 our $cfg_captcha_image_class;
17 our $initialized = 0;
18  
19 sub initialize {
20 $cfg_captcha_db = cfg_captcha_db();
21 $cfg_www_static_captchas_dir = cfg_www_static_captchas_dir();
22 $cfg_captcha_image_class = cfg_captcha_image_class();
23 no strict 'refs';
24 for ($cfg_captcha_db, $cfg_www_static_captchas_dir) {
25 die "$_ must be directory " unless -d $_;
26 die "directory $_ must be writable" unless -w $_;
27 $_ .= "/" unless substr ($_, -1, 1) eq '/';
28 }
29 $cfg_captcha_image_class = "PEF::Front::SecureCaptcha"
30 if not $cfg_captcha_image_class
31 or not %{$cfg_captcha_image_class . "::"}
32 or not $cfg_captcha_image_class->can("generate_image");
33 eval "use $cfg_captcha_image_class";
34 croak "error loading captcha image class $cfg_captcha_image_class: $@" if $@;
35 $initialized = 1;
36 }
37  
38 sub _random {
39 my $max = $_[0];
40 open (my $rf, "<", "/dev/urandom") or die $!;
41 binmode $rf;
42 my $cu;
43 sysread ($rf, $cu, 8);
44 close ($rf);
45 my $l = unpack "Q", $cu;
46 return $l % $max;
47 }
48  
49 sub generate_code {
50 my ($size, $expire) = @_;
51 my $str = '';
52 my $symbols = cfg_captcha_symbols();
53 my $num = @$symbols;
54 $str .= $symbols->[_random($num)] for (1 .. $size);
55 my %dbm;
56 tie (%dbm, 'MLDBM::Sync', "${cfg_captcha_db}captcha.dbm", O_CREAT | O_RDWR, 0666) or die "$!";
57 my $sha1 = sha1_hex(lc ($str) . cfg_captcha_secret());
58 $dbm{$sha1} = time + $expire;
59 return ($str, $sha1);
60 }
61  
62 sub make_captcha {
63 my $req = $_[0];
64 initialize if not $initialized;
65 my ($str, $sha1) = generate_code($req->{size}, cfg_captcha_expire_sec());
66 my $image_init = cfg_captcha_image_init();
67 $image_init = {} if not $image_init or 'HASH' ne ref $image_init;
68 no strict 'refs';
69 &{"${cfg_captcha_image_class}::generate_image"}(
70 width => $req->{width},
71 height => $req->{height},
72 size => $req->{size},
73 str => $str,
74 code => $sha1,
75 out_folder => $cfg_www_static_captchas_dir,
76 font => cfg_captcha_font(),
77 %$image_init,
78 );
79 return {result => "OK", code => $sha1};
80 }
81  
82 sub check_code {
83 my ($code, $sha1sum) = @_;
84 my $sha1 = sha1_hex(lc ($code) . cfg_captcha_secret());
85 my %dbm;
86 my $sync_obj = tie (%dbm, 'MLDBM::Sync', "${cfg_captcha_db}captcha.dbm", O_CREAT | O_RDWR, 0666) or die "$!";
87 $sync_obj->Lock;
88 for my $cc (keys %dbm) {
89 if ($dbm{$cc} < time) {
90 unlink "${cfg_www_static_captchas_dir}$cc.jpg";
91 delete $dbm{$cc};
92 }
93 }
94 my $passed = 0;
95 if (exists $dbm{$sha1sum} && $sha1sum eq $sha1) {
96 delete $dbm{$sha1sum};
97 $passed = 1;
98 }
99 $sync_obj->UnLock;
100 return $passed;
101 }
102  
103 sub check_captcha {
104 my ($input, $md5sum) = @_;
105 return check_code($input, $md5sum) == 1;
106 }
107  
108 1;