| 1 |
package HTML::FormHandler::TraitFor::Captcha; |
| 2 |
# ABSTRACT: generate and validate captchas |
| 3 |
$HTML::FormHandler::TraitFor::Captcha::VERSION = '0.410002'; |
| 4 |
use HTML::FormHandler::Moose::Role; |
| 5 |
use GD::SecurityImage; |
| 6 |
use HTTP::Date; |
| 7 |
|
| 8 |
requires('ctx'); |
| 9 |
|
| 10 |
has_field 'captcha' => ( type => 'Captcha', label => 'Verification' ); |
| 11 |
|
| 12 |
|
| 13 |
sub get_captcha { |
| 14 |
my $self = shift; |
| 15 |
return unless $self->ctx; |
| 16 |
my $captcha; |
| 17 |
$captcha = $self->ctx->session->{captcha}; |
| 18 |
return $captcha; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
sub set_captcha { |
| 23 |
my ( $self, $captcha ) = @_; |
| 24 |
return unless $self->ctx; |
| 25 |
$self->ctx->session( captcha => $captcha ); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
sub captcha_image_url { |
| 30 |
return '/captcha/image'; |
| 31 |
} |
| 32 |
|
| 33 |
use namespace::autoclean; |
| 34 |
1; |
| 35 |
|
| 36 |
__END__ |
| 37 |
|
| 38 |
=pod |
| 39 |
|
| 40 |
=encoding UTF-8 |
| 41 |
|
| 42 |
=head1 NAME |
| 43 |
|
| 44 |
HTML::FormHandler::TraitFor::Captcha - generate and validate captchas |
| 45 |
|
| 46 |
=head1 VERSION |
| 47 |
|
| 48 |
version 0.410002 |
| 49 |
|
| 50 |
=head1 SYNOPSIS |
| 51 |
|
| 52 |
A role to use in a form to implement a captcha field. |
| 53 |
|
| 54 |
package MyApp::Form; |
| 55 |
use HTML::FormHandler::Moose; |
| 56 |
with 'HTML::FormHandler::TraitFor::Captcha'; |
| 57 |
|
| 58 |
or |
| 59 |
|
| 60 |
my $form = MyApp::Form->new( traits => ['HTML::FormHandler::TraitFor::Captcha'], |
| 61 |
ctx => $c ); |
| 62 |
|
| 63 |
Needs a context object set in the form's 'ctx' attribute which has a session |
| 64 |
hashref in which to store a 'captcha' hashref, such as is provided by Catalyst |
| 65 |
session plugin. |
| 66 |
|
| 67 |
=head1 METHODS |
| 68 |
|
| 69 |
=head2 get_captcha |
| 70 |
|
| 71 |
Get a captcha stored in C<< $form->ctx->{session} >> |
| 72 |
|
| 73 |
=head1 set_captcha |
| 74 |
|
| 75 |
Set a captcha in C<< $self->ctx->{session} >> |
| 76 |
|
| 77 |
=head2 captcha_image_url |
| 78 |
|
| 79 |
Default is '/captcha/image'. Override in a form to change. |
| 80 |
|
| 81 |
sub captcha_image_url { '/my/image/url/' } |
| 82 |
|
| 83 |
Example of a Catalyst action to handle the image: |
| 84 |
|
| 85 |
sub image : Local { |
| 86 |
my ( $self, $c ) = @_; |
| 87 |
my $captcha = $c->session->{captcha}; |
| 88 |
$c->response->body($captcha->{image}); |
| 89 |
$c->response->content_type('image/'. $captcha->{type}); |
| 90 |
$c->res->headers->expires( time() ); |
| 91 |
$c->res->headers->header( 'Last-Modified' => HTTP::Date::time2str ); |
| 92 |
$c->res->headers->header( 'Pragma' => 'no-cache' ); |
| 93 |
$c->res->headers->header( 'Cache-Control' => 'no-cache' ); |
| 94 |
} |
| 95 |
|
| 96 |
=head1 AUTHOR |
| 97 |
|
| 98 |
FormHandler Contributors - see HTML::FormHandler |
| 99 |
|
| 100 |
=head1 COPYRIGHT AND LICENSE |
| 101 |
|
| 102 |
This software is copyright (c) 2017 by Gerda Shank. |
| 103 |
|
| 104 |
This is free software; you can redistribute it and/or modify it under |
| 105 |
the same terms as the Perl 5 programming language system itself. |
| 106 |
|
| 107 |
=cut |