blib/lib/ASP4x/Captcha/Imager.pm

filter: Statementstmt 86.6% 52 / 60 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 9 / 9 Podpod 33.3% 1 / 3 totaltotal 75.8% 69 / 91 CC 11 SCAR 17.7
CC
11
Cov
80%
run
11.0
generate_pair
4.0
BEGIN
1.0
CRAP 5.9
↑ Summary
1  
2 package ASP4x::Captcha::Imager;
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base 'ASP4::FormHandler';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use vars __PACKAGE__->VARS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '0.003';
12  
13  
14 sub run
15 2 ▶{
Subroutine run: called CC=8 SCAR=24.0
Pod documented
16 2 my ($s, $context) = @_;
17
18 2 my ($word, $key) = $s->generate_pair( $context );
19
20 2 $Session->{asp4captcha} = { lc($word) => $key };
21 2 $word = join ' ', split //, $word;
22
23 my $img = Imager->new(
24 2 ▶ xsize => eval { $Config->system->settings->captcha_width } || 140,
Condition50%1/2
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_height } } || 70
l!l
20
MC/DC0%0/1
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_height } } || 70
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_height } }
Truth table50%1/2
A
A= eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_height } }
Aresult
11
00
25 2 ▶ ysize => eval { $Config->system->settings->captcha_height } || 70
Condition50%1/2
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_width } } || 140
l!l
20
MC/DC0%0/1
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_width } } || 140
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_width } }
Truth table50%1/2
A
A= eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_width } }
Aresult
11
00
26 );
27  
28 $img->box(
29 filled => 1,
30 2 ▶ color => eval { $Config->system->settings->captcha_bg_color } || 'white'
Condition50%1/2
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_bg_color } } || 'white'
l!l
20
MC/DC0%0/1
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_bg_color } } || 'white'
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_bg_color } }
Truth table50%1/2
A
A= eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_bg_color } }
Aresult
11
00
31 );
32  
33 2 my $font = Imager::Font->new( $s->font );
34  
35 2 my @colors = qw(
36 A9A9A9 878787 656565 808080
37 CACACA EFEFEF DEDEDE CDCDCD
38 BABABA A9A9A9 878787 656565
39 434343 212121 EFEFEF DEDEDE
40 CDCDCD BABABA CCCCCC AAAAAA
41 );
42
43 # Add the word to the image, but make it hard to read:
44 2 my @chars = split //, $word;
45 2 my $charWidth = ( $img->getwidth * 0.8 ) / scalar(@chars);
46 2 my $charHeight = sprintf("%d", $img->getheight * 0.7);
47
48 2 for( 1..10 )
49 {
50 2 for my $idx ( 0..@chars - 1 )
51 {
52 2 for( 1..int(rand() * @colors) )
53 {
54 21 push @colors, shift(@colors);
55 }
56 2 my $color = $colors[0];
57 2 $font->align(
58 halign => 'center',
59 valign => 'center',
60 string => $chars[$idx],
61 size => $charHeight,
62 image => $img,
63 color => $color,
64 'x' => ( $idx * $charWidth ) + int(rand() * 8) + $charWidth,
65 'y' => ($img->getheight / 2) + int(rand() * 8) - 4,
66 );
67 }# end for()
68 0 $img->filter( type => 'gaussian', stddev => 1 );
69 }# end for()
70
71 # Render the image as PNG:
72 0 my $str = "";
73 0 ▶ $img->write(type=>'png', data => \$str)
Branch: 1 branch
BranchTrueFalseTotal
$img->write("type", "png", "data", \$str) or die $img->errstr 0 0 0
74 or die $img->errstr;
75 0 $Response->Expires( -30 );
76 0 $Response->AddHeader( pragma => 'no-cache' );
77 0 $Response->SetHeader('content-type' => 'image/png');
78 0 $Response->ContentType( 'image/png' );
79 0 $Response->Write( $str );
80 }# end run()
81  
82  
83 sub generate_pair
84 2 ▶{
Subroutine generate_pair: called CC=4 SCAR=13.9
Pod undocumented
85 2 my ($s, $context) = @_;
86  
87 2 ▶ my $len = eval { $Config->system->settings->captcha_length } || 4;
Condition50%1/2
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_length } } || 4
l!l
20
MC/DC0%0/1
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_length } } || 4
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_length } }
Truth table50%1/2
A
A= eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_length } }
Aresult
11
00
88 2 my $chars = join '', ( 'A'..'H', 'J'..'N', 'P'..'Z', 1..9 );
89  
90 2 my $word = '';
91 2 ▶ while( length($word) < $len )
Branch: 1 branch
BranchTrueFalseTotal
if (length $word < $len) 8 2 10
92 {
93 8 $word .= substr($chars, int(rand()*length($chars)), 1);
94 }# end while()
95
96 2 ▶ my $key = md5_hex( lc($word) . ( eval { $Config->system->settings->captcha_key } || '' ) );
Condition50%1/2
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_key } } || ''
l!l
20
MC/DC0%0/1
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_key } } || ''
eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_key } }
Truth table50%1/2
A
A= eval { do { $ASP4x::Captcha::Imager::Config->system->settings->captcha_key } }
Aresult
11
00
97
98 2 return ( $word, $key );
99 }# end generate_pair()
100  
101  
102 sub font
103 2 ▶{
Subroutine font: called CC=1 SCAR=0.0
Pod undocumented
104 2 my $s = shift;
105
106 2 return ( file => $Config->system->settings->captcha_font );
107 }# end font()
108  
109 3 1;# return true:
110  
111 =pod
112  
113 =head1 NAME
114  
115 ASP4x::Captcha::Imager - Imager-based CAPTCHA for your ASP4 web application.
116  
117 =head1 SYNOPSIS
118  
119 =head2 In Your asp4-config.conf
120  
121 {
122 ...
123 "system": {
124 ...
125 "settings": {
126 ...
127 "captcha_key": "Some random string of any length",
128 "captcha_font": "@ServerRoot@/etc/LiberationSans-Regular.ttf",
129 "captcha_width": 140,
130 "captcha_height": 40,
131 "captcha_bg_color": "FFFFFF",
132 "captcha_length": 4
133 ...
134 }
135 }
136 }
137  
138 =head2 In a handler
139  
140 Simply subclass C<ASP4x::Captcha::Imager> as shown below:
141  
142 package dev::captcha;
143  
144 use strict;
145 use warnings 'all';
146 use base 'ASP4x::Captcha::Imager';
147 use vars __PACKAGE__->VARS;
148  
149 1;# return true:
150  
151 =head2 In your ASP Script:
152  
153 Render the Captcha image:
154  
155 <html>
156 <head>
157 <style type="text/css">
158 LABEL {
159 display: block;
160 width: 265px;
161 text-align: right;
162 float: left;
163 padding-right: 5px;
164 }
165
166 IMG {
167 border: dotted 1px #AAA;
168 }
169 </style>
170 </head>
171 <body>
172 <form action="/handlers/dev.validate" method="post">
173 <p>
174 <label>Enter the code you see below:</label>
175 <input type="text" name="security_code" />
176 </p>
177 <p>
178 <label>&nbsp;</label>
179 <img id="captcha" src="/handlers/dev.captcha?r=<%= rand() %>" alt="Security Code" />
180 <a href="" onclick="document.getElementById('captcha').src = '/handlers/dev.captcha?r=' + Math.random(); return false">
181 (Click for a new Image)
182 </a>
183 </p>
184 <p>
185 <label>&nbsp;</label>
186 <input type="submit" value="Submit" />
187 </p>
188 </form>
189 </body>
190 </html>
191  
192 =head2 Validate the Captcha
193  
194 package dev::validate;
195  
196 use strict;
197 use warnings 'all';
198 use base 'ASP4::FormHandler';
199 use vars __PACKAGE__->VARS;
200  
201 sub run
202 {
203 my ($s, $context) = @_;
204
205 my $secret = $Config->system->settings->captcha_key;
206 my $code = lc($Form->{security_code});
207
208 # It should exist in the session and have the correct value:
209 if( exists($Session->{asp4captcha}->{$code}) )
210 {
211 # Ding ding ding ding ding!
212 $Response->Write("CORRECT");
213 }
214 else
215 {
216 # Bzzzzzzzzzzt: WRONG!
217 $Response->Write("WRONG");
218 }# end if()
219 }# end run()
220  
221 1;# return true:
222  
223 =head1 DESCRIPTION
224  
225 "CAPTCHA" is the little security image containing a hard-to-read code that you may
226 have seen on some websites. They are common on sign-up forms and email forms. The
227 idea is that a bot or script can't read the image and can't guess the code.
228  
229 C<ASP4x::Captcha::Imager> uses L<Imager> to generate an image of a random string
230 of numbers and letters.
231  
232 =head2 What Does the Captcha Image Look Like?
233  
234 You can see an example in the example/example.png file included with this distribution.
235  
236 =head2 Recommendations and Considerations
237  
238 =over 4
239  
240 =item * Shorter Captchas are probably good enough.
241  
242 Unless you've got yourself the next Facebook, you could probably get away with
243 4 characters in your Captcha. Long captchas will just annoy humans.
244  
245 =item * Where to use Captcha
246  
247 Any form that might be attacked by a script including registration forms, email forms, etc.
248 is a good candidate for a Captcha. Since it's so easy to use Captchas there really
249 isn't any reason not to use them anywhere you think B<might> benefit. If you keep
250 the Captcha length short (see the first point in this list) then the humans won't
251 be too bothered by them and may actually be pleased with your consideration of their privacy.
252  
253 =back
254  
255 =head2 What About Fonts?
256  
257 Because Linux systems tend to put fonts in several different places, I recommend
258 copying the font file (*.ttf) into the C<etc/> folder of your website and referencing it
259 (just like you see in the C<t/> folder of this distribution and in the SYNOPSIS example above.
260  
261 Mono-space fonts are recommended over variable-width fonts. So, "Courier New" is
262 recommended over Verdana.
263  
264 =head1 SEE ALSO
265  
266 L<ASP4>, L<Imager>
267  
268 =head1 PREREQUISITES
269  
270 L<Imager>, L<ASP4>, L<Digest::MD5>
271  
272 =head1 AUTHOR
273  
274 John Drago <jdrago_999@yahoo.com>
275  
276 =head1 LICENSE
277  
278 This software is B<Free> software and may be used and redistributed under the
279 same terms as any version of Perl itself.
280  
281 =cut
282  
↑ Summary