| 1 |
package HTML::FormHandler::Field::Captcha; |
| 2 |
# ABSTRACT: captcha field with GD::SecurityImage |
| 3 |
$HTML::FormHandler::Field::Captcha::VERSION = '0.410002'; |
| 4 |
use Moose; |
| 5 |
extends 'HTML::FormHandler::Field'; |
| 6 |
use HTTP::Date; |
| 7 |
|
| 8 |
|
| 9 |
has 'height' => ( isa => 'Int', is => 'rw', default => '20' ); |
| 10 |
has 'width' => ( isa => 'Int', is => 'rw', default => '80' ); |
| 11 |
has 'scramble' => ( isa => 'Int', is => 'rw', default => '0' ); |
| 12 |
has 'lines' => ( isa => 'Int', is => 'rw', default => '2' ); |
| 13 |
has 'gd_font' => ( isa => 'Str', is => 'rw', default => 'Large' ); |
| 14 |
has 'image' => ( is => 'rw' ); |
| 15 |
has '+wrapper_class' => ( default => 'captcha' ); |
| 16 |
has '+widget' => ( default => 'Captcha' ); |
| 17 |
|
| 18 |
has '+noupdate' => ( default => 1 ); |
| 19 |
|
| 20 |
our $class_messages = { |
| 21 |
'captcha_verify_failed' => 'Verification incorrect. Try again.', |
| 22 |
}; |
| 23 |
|
| 24 |
sub get_class_messages { |
| 25 |
my $self = shift; |
| 26 |
return { |
| 27 |
%{ $self->next::method }, |
| 28 |
%$class_messages, |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
sub get_default_value { |
| 33 |
my $self = shift; |
| 34 |
|
| 35 |
my $captcha = $self->form->get_captcha; |
| 36 |
# setting the widget after the field is instantiated |
| 37 |
# doesn't actually work. The Captcha widget checks for |
| 38 |
# this setting though. |
| 39 |
if ($captcha) { |
| 40 |
if ( $captcha->{validated} ) { |
| 41 |
$self->required(0); |
| 42 |
$self->widget('NoRender'); |
| 43 |
} |
| 44 |
else { |
| 45 |
$self->required(1); |
| 46 |
$self->widget('Captcha'); |
| 47 |
$self->image( $captcha->{image} ); |
| 48 |
} |
| 49 |
} |
| 50 |
else { |
| 51 |
$self->required(1); |
| 52 |
$self->widget('Captcha'); |
| 53 |
$self->gen_captcha; |
| 54 |
} |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
sub validate { |
| 59 |
my $self = shift; |
| 60 |
|
| 61 |
my $captcha = $self->form->get_captcha; |
| 62 |
unless ( $captcha->{rnd} eq $self->value ) { |
| 63 |
$self->add_error($self->get_message('captcha_verify_failed')); |
| 64 |
$self->gen_captcha; |
| 65 |
} |
| 66 |
else { |
| 67 |
$captcha->{validated} = 1; |
| 68 |
} |
| 69 |
return !$self->has_errors; |
| 70 |
} |
| 71 |
|
| 72 |
sub fif { } |
| 73 |
|
| 74 |
sub gen_captcha { |
| 75 |
my $self = shift; |
| 76 |
|
| 77 |
require GD::SecurityImage; |
| 78 |
my ( $image, $type, $rnd ) = GD::SecurityImage->new( |
| 79 |
height => $self->height, |
| 80 |
width => $self->width, |
| 81 |
scramble => $self->scramble, |
| 82 |
lines => $self->lines, |
| 83 |
gd_font => $self->gd_font, |
| 84 |
)->random->create->out; |
| 85 |
my $captcha = { |
| 86 |
image => $image, |
| 87 |
type => $type, |
| 88 |
rnd => $rnd, |
| 89 |
validated => 0, |
| 90 |
}; |
| 91 |
$self->image($image); |
| 92 |
$self->form->set_captcha($captcha); |
| 93 |
} |
| 94 |
|
| 95 |
__PACKAGE__->meta->make_immutable; |
| 96 |
use namespace::autoclean; |
| 97 |
1; |
| 98 |
|
| 99 |
__END__ |
| 100 |
|
| 101 |
=pod |
| 102 |
|
| 103 |
=encoding UTF-8 |
| 104 |
|
| 105 |
=head1 NAME |
| 106 |
|
| 107 |
HTML::FormHandler::Field::Captcha - captcha field with GD::SecurityImage |
| 108 |
|
| 109 |
=head1 VERSION |
| 110 |
|
| 111 |
version 0.410002 |
| 112 |
|
| 113 |
=head1 SYNOPSIS |
| 114 |
|
| 115 |
A Captcha class using GD::SecurityImage. |
| 116 |
Requires that three methods be available from a form object: |
| 117 |
|
| 118 |
$self->form->get_captcha; |
| 119 |
$self->form->set_captcha; |
| 120 |
|
| 121 |
Using Catalyst and the Catalyst session plugin this field can be used |
| 122 |
in a form by using L<HTML::FormHandler::TraitFor::Captcha>. |
| 123 |
|
| 124 |
package MyApp::Form::Post; |
| 125 |
use HTML::FormHandler::Moose; |
| 126 |
with 'HTML::FormHandler::TraitFor::Captcha'; |
| 127 |
|
| 128 |
You can set the following attributes on the 'captcha' field: |
| 129 |
|
| 130 |
height, width, scramble, lines, gd_font |
| 131 |
|
| 132 |
Example: |
| 133 |
|
| 134 |
has 'captcha' => ( height => '24', width => '70' ); |
| 135 |
|
| 136 |
=head1 AUTHOR |
| 137 |
|
| 138 |
FormHandler Contributors - see HTML::FormHandler |
| 139 |
|
| 140 |
=head1 COPYRIGHT AND LICENSE |
| 141 |
|
| 142 |
This software is copyright (c) 2017 by Gerda Shank. |
| 143 |
|
| 144 |
This is free software; you can redistribute it and/or modify it under |
| 145 |
the same terms as the Perl 5 programming language system itself. |
| 146 |
|
| 147 |
=cut |