| 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__ |