line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Captcha; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
24640
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3343
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use GD::SecurityImage; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = 0.02; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub register { |
13
|
|
|
|
|
|
|
my ($self, $app, $conf) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
die ref($self), ": need session_name\n" |
16
|
|
|
|
|
|
|
unless $conf->{session_name}; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $session_name = $conf->{session_name}; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $captcha_string = sub { |
21
|
|
|
|
|
|
|
return shift->session->{ $session_name }; |
22
|
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$app->helper( |
25
|
|
|
|
|
|
|
create_captcha => sub { |
26
|
|
|
|
|
|
|
my $self = shift; |
27
|
|
|
|
|
|
|
my $image = GD::SecurityImage->new( %{ $conf->{new} } ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$image->random(); |
30
|
|
|
|
|
|
|
$image->create( @{ $conf->{create} } ); |
31
|
|
|
|
|
|
|
$image->particle( @{ $conf->{particle} } ); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my ( $image_data, $mime_type, $random_string ) = $image->out( %{ $conf->{out} } ); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$self->session->{ $session_name } = $random_string; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
return $image_data; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
$app->helper( |
42
|
|
|
|
|
|
|
validate_captcha => sub { |
43
|
|
|
|
|
|
|
my ( $self, $string, $case_sens ) = @_; |
44
|
|
|
|
|
|
|
return $case_sens |
45
|
|
|
|
|
|
|
? $string eq &{$captcha_string} |
46
|
|
|
|
|
|
|
: uc($string) eq uc(&{$captcha_string}) |
47
|
|
|
|
|
|
|
; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
1; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 NAME |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Mojolicious::Plugin::Captcha - create and validate captcha for Mojolicious framework |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 VERSION |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
0.02 |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 SYNOPSIS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Mojolicious |
65
|
|
|
|
|
|
|
$self->plugin( |
66
|
|
|
|
|
|
|
'captcha', |
67
|
|
|
|
|
|
|
{ |
68
|
|
|
|
|
|
|
session_name => 'captcha_string', |
69
|
|
|
|
|
|
|
out => {force => 'jpeg'}, |
70
|
|
|
|
|
|
|
particle => [0,0], |
71
|
|
|
|
|
|
|
create => [qw/normal rect/], |
72
|
|
|
|
|
|
|
new { |
73
|
|
|
|
|
|
|
rnd_data => [0...9, 'A'...'Z'], |
74
|
|
|
|
|
|
|
width => 80, |
75
|
|
|
|
|
|
|
height => 30, |
76
|
|
|
|
|
|
|
lines => 7, |
77
|
|
|
|
|
|
|
gd_font => 'giant', |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
package MyApp::MyController; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub captcha { |
85
|
|
|
|
|
|
|
my $self = shift; |
86
|
|
|
|
|
|
|
$self->render( data => $self->create_captcha ); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub some_post : Local { |
90
|
|
|
|
|
|
|
my ($self, $c) = @_; |
91
|
|
|
|
|
|
|
if ($self->validate_captcha($c->req->param('captcha')){ |
92
|
|
|
|
|
|
|
.. |
93
|
|
|
|
|
|
|
} else { |
94
|
|
|
|
|
|
|
.. |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 DESCRIPTION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This plugin create and validate Captcha, using L |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 METHODS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 create_captcha |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Create Captcha image and output it. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 validate_captcha |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Validate captcha string |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Accept optional second parameter to switch comparator case sensitivity (default is off, i.e. comparator make case insensivity comparing) |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# case sensitivity comparing |
115
|
|
|
|
|
|
|
$self->validate_captcha($self->param('captcha'), 1); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 CONFIGURATION |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=over 4 |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item session_name |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
The keyword for storing captcha string |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=item new |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item create |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=item particle |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item out |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
These parameters are passed to each GD::Security's method. Please see L for details. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=back |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 SUPPORT |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=over 4 |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item * Repository |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
L |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=back |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 SEE ALSO |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
L, L, L |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Copyright 2014 zar. All right reserved. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |