| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Catalyst::Plugin::Captcha; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
40586
|
use strict; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
44
|
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
32
|
|
|
5
|
1
|
|
|
1
|
|
721
|
use GD::SecurityImage; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use HTTP::Date; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use MRO::Compat; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub setup { |
|
13
|
|
|
|
|
|
|
my $c = shift; |
|
14
|
|
|
|
|
|
|
my $config = $c->config->{ 'Plugin::Captcha' } |
|
15
|
|
|
|
|
|
|
|| delete $c->config->{captcha}; # Deprecated |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$config->{session_name} ||= 'captcha_string'; |
|
18
|
|
|
|
|
|
|
$config->{captcha}->{new} ||= {}; |
|
19
|
|
|
|
|
|
|
$config->{captcha}->{create} ||= []; |
|
20
|
|
|
|
|
|
|
$config->{captcha}->{particle} ||= []; |
|
21
|
|
|
|
|
|
|
$config->{captcha}->{out} ||= {}; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$c->config( 'Plugin::Captcha' => $config ); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
return $c->next::method( @_ ); |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub create_captcha { |
|
29
|
|
|
|
|
|
|
my $c = shift; |
|
30
|
|
|
|
|
|
|
my $conf = $c->config->{ 'Plugin::Captcha' }; |
|
31
|
|
|
|
|
|
|
my $image = GD::SecurityImage->new( %{ $conf->{new} } ); |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$image->random(); |
|
34
|
|
|
|
|
|
|
$image->create( @{ $conf->{create} } ); |
|
35
|
|
|
|
|
|
|
$image->particle( @{ $conf->{particle} } ); |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my ( $image_data, $mime_type, $random_string ) = |
|
38
|
|
|
|
|
|
|
$image->out( %{ $conf->{out} } ); |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$c->session->{ $conf->{session_name} } = $random_string; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$c->res->headers->expires( time() ); |
|
43
|
|
|
|
|
|
|
$c->res->headers->header( 'Last-Modified' => HTTP::Date::time2str ); |
|
44
|
|
|
|
|
|
|
$c->res->headers->header( 'Pragma' => 'no-cache' ); |
|
45
|
|
|
|
|
|
|
$c->res->headers->header( 'Cache-Control' => 'no-cache' ); |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$c->res->content_type("image/$mime_type"); |
|
48
|
|
|
|
|
|
|
$c->res->output($image_data); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub validate_captcha { |
|
52
|
|
|
|
|
|
|
my ( $c, $verify ) = @_; |
|
53
|
|
|
|
|
|
|
my $string = $c->captcha_string; |
|
54
|
|
|
|
|
|
|
return ( $verify && $string && $verify eq $string ); |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub captcha_string { |
|
58
|
|
|
|
|
|
|
my $c = shift; |
|
59
|
|
|
|
|
|
|
my $conf = $c->config->{ 'Plugin::Captcha' }; |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
return $c->session->{ $conf->{session_name} }; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub clear_captcha_string { |
|
65
|
|
|
|
|
|
|
my $c = shift; |
|
66
|
|
|
|
|
|
|
my $conf = $c->config->{ 'Plugin::Captcha' }; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
delete $c->session->{ $conf->{session_name} }; |
|
69
|
|
|
|
|
|
|
return 1; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
|
73
|
|
|
|
|
|
|
__END__ |
|
74
|
|
|
|
|
|
|
# Below is stub documentation for your module. You'd better edit it! |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 NAME |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Catalyst::Plugin::Captcha - create and validate Captcha for Catalyst |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
use Catalyst qw/Captcha/; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
MyApp->config->{ 'Plugin::Captcha' } = { |
|
85
|
|
|
|
|
|
|
session_name => 'captcha_string', |
|
86
|
|
|
|
|
|
|
new => { |
|
87
|
|
|
|
|
|
|
width => 80, |
|
88
|
|
|
|
|
|
|
height => 30, |
|
89
|
|
|
|
|
|
|
lines => 7, |
|
90
|
|
|
|
|
|
|
gd_font => 'giant', |
|
91
|
|
|
|
|
|
|
}, |
|
92
|
|
|
|
|
|
|
create => [qw/normal rect/], |
|
93
|
|
|
|
|
|
|
particle => [100], |
|
94
|
|
|
|
|
|
|
out => {force => 'jpeg'} |
|
95
|
|
|
|
|
|
|
}; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub captcha : Local { |
|
98
|
|
|
|
|
|
|
my ($self, $c) = @_; |
|
99
|
|
|
|
|
|
|
$c->create_captcha(); |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub do_post : Local { |
|
103
|
|
|
|
|
|
|
my ($self, $c) = @_; |
|
104
|
|
|
|
|
|
|
if ($c->validate_captcha($c->req->param('validate')){ |
|
105
|
|
|
|
|
|
|
.. |
|
106
|
|
|
|
|
|
|
} else { |
|
107
|
|
|
|
|
|
|
.. |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#validate with CP::FormValidator::Simple |
|
112
|
|
|
|
|
|
|
sub do_post : Local { |
|
113
|
|
|
|
|
|
|
my ($self, $c) = @_; |
|
114
|
|
|
|
|
|
|
$c->form( |
|
115
|
|
|
|
|
|
|
validate => [['EQUAL_TO',$c->captcha_string]] |
|
116
|
|
|
|
|
|
|
) |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
This plugin create, validate Captcha. |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Note: This plugin uses L<GD::SecurityImage> and requires a session plugins like L<Catalyst::Plugin::Session> |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 METHODS |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 create_captcha |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Create Captcha image and output it. |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 validate_captcha |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$c->validate_captcha($key); |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
validate key |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 captcha_string |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Return a string for validation which is stroed in session. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 clear_captcha_string |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Clear a string which is stroed in session. |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 CONFIGURATION |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=over 4 |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item session_name |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
The keyword for storing captcha string |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item new |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item create |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item particle |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item out |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
These parameters are passed to each GD::Security's method. Please see L<GD::SecurityImage> for details. |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=back |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
L<GD::SecurityImage>, L<Catalyst> |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 AUTHOR |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Masahiro Nagano E<lt>kazeburo@nomadscafe.jpE<gt> |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Copyright (C) 2006 by Masahiro Nagano |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
178
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.5 or, |
|
179
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |