-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_capture.pl
286 lines (250 loc) · 7.77 KB
/
get_capture.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/perl
use Image::Magick;
use CGI::Carp qw/fatalsToBrowser/;
use CGI::Fast qw(:standard);
use strict;
#use DBI;
use lib 'lib';
use appdbh;
while (new CGI::Fast) {
# do './manager/connect';
# use vars qw($DBname $DBhost $DBuser $DBpassword);
# my $dbh=DBI->connect("DBI:mysql:$DBname:$DBhost",$DBuser,$DBpassword, , { RaiseError => 1 }) || die($!);
my $dbh = appdbh->new;
# óáèðàåì çàïèñè, ñòàðøå 1-ãî ÷àñà:
my ($sec,$min,$hour,$day,$mon,$year)=(localtime(time-3600))[0..5];
$year+=1900; $mon++;
$dbh->do("DELETE from capture WHERE registered<'$year-$mon-$day $hour:$min:$sec'");
#if(0){
# print "Content-type: text/html\n\nopt_string: $opt->{params};"; exit;
#}
my $action=param('action');
my $domain=$ENV{SERVER_NAME};
$domain=~s/^www\.//;
my $sth=$dbh->prepare("SELECT project_id from domain WHERE domain=?");
$sth->execute($domain);
my $project_id=$sth->fetchrow();
unless($project_id){
print "Content-type: text/html\n\n";
print "project_id not found";
exit;
}
my $opt=&get_options({
dbh=>$dbh,
project_id=>$project_id
});
my $qs=$ENV{QUERY_STRING};
$qs=~s/action=.+?&?//;
if($action eq 'refrash'){
# åñëè äåëàåòñÿ refrash, íóæíî ïðîâåðèòü, ãåíåðèðîâàëñÿ ëè êîä ðàíåå:
my $str_key=param('str_key');
my $sth=$dbh->prepare("SELECT count(*) from capture where str_key=?");
$sth->execute($str_key);
if($sth->fetchrow()){
#$dbh->do("DELETE from capture where str_key='$str_key'");
}
else{
print "Content-type: text/html; charset=windows-1251\n\n";
print "ïîïûòêà ïîäìåíû?";
exit;
}
}
if($action eq 'out_key' || $action eq 'refrash'){
print "Content-type: text/html\n\n";
# ïîëó÷àåì ñëó÷àéíóþ ñòðîêó è êëþ÷ äëÿ íå¸
my $str;
my $str_res;
if($opt->{method}==1){ # âûâîä ïðèìåðà
$opt->{chars_count}=2 unless($opt->{chars_count});
$str=$str_res=&gen_example($opt->{chars_count});
eval('$str_res=('.$str.')');
$str.='=';
}
else{ # âûâîä öèôð
$opt->{chars_count}=5 unless($opt->{chars_count});
$str=$str_res=&gen_str($opt->{chars_count});
}
my $sth=$dbh->prepare('select md5(?)');
$sth->execute($str_res);
my $str_key=$sth->fetchrow();
#open F, '>./temp/tst_cap';
#print F "str: $str\nstr_res: $str_res\nstr_key: $str_key";
#close F;
# ñîõðàíÿåì èíôîðìàöèþ î ñòðîêè è êëþ÷å â ÁÄ
$sth=$dbh->prepare("INSERT INTO capture(str,str_key,registered,project_id) values(?,?,now(),?)");
$sth->execute($str,$str_key,$project_id);
# ãåíåðèðóåì êëþ÷ äëÿ cpan'à ñ êàï÷åé (âåäü íà ñòðàíèöå êàï÷ ìîæåò áûòü íåñêîëüêî)
#print "Content-type: text/html\n\n"; print "qs: $qs"; exit;
if($action eq 'refrash'){
my $cpt_id=param('cpt_id');
print qq{<input type='hidden' name='capture_key' value='$str_key'><a href="#" onclick="javascript: document.getElementById('$cpt_id').innerHTML=loadDocAsync('/get_capture.pl?action=refrash&cpt_id=$cpt_id&str_key=$str_key&$qs&$opt->{params}');return false"><img class="capture" align='absmiddle' src='/get_capture.pl?action=out_capture&key=$str_key&$qs&$opt->{params}'></a>};
}
else{
my $cpt_p=param('cpt_id');
$cpt_p='cpt' unless($cpt_p);
my $cpt_id=$cpt_p.&gen_str(5);
my $img_and_hidden=qq{<input type='hidden' name='capture_key' value='$str_key'><a href="#" onclick="javascript: document.getElementById('$cpt_id').innerHTML=loadDocAsync('/get_capture.pl?action=refrash&cpt_id=$cpt_id&str_key=$str_key&$qs&$opt->{params}');return false"><img class="capture" align='absmiddle' src='/get_capture.pl?action=out_capture&key=$str_key&cpt_id=$cpt_id&$qs&$opt->{params}'></a>};
print qq{<span id='$cpt_id'>$img_and_hidden</span>};
}
#$sth=$dbh->prepare("INSERT INTO capture(str,str_key) VALUES()");
}
elsif($action eq 'out_capture'){
my $key=param('key');
my $sth=$dbh->prepare("SELECT str from capture where project_id=? and str_key=?");
$sth->execute($project_id,$key);
my $str=$sth->fetchrow();
&out_capture($str,$opt);
}
}
sub gen_str{
my $count=shift;
my $a=q{123456789abcdefghijklmnopqrstuvwxyz};
my $str='';
foreach my $k (1..$count){
$str.=substr($a,int(rand(length($a))),1)
}
return $str;
}
sub gen_example{
my $count=shift;
# my $a=q{1234567890};
my $a=q{123456789};
my $x1='';
foreach my $k (1..$count){
$x1.=substr($a,int(rand(length($a))),1)
}
my $x2='';
foreach my $k (1..$count){
$x2.=substr($a,int(rand(length($a))),1)
}
# do {
# $x1 = int(rand(10));
# $x2 = int(rand(10));
# } until($x1 ne '0' || $x2 ne '0');
return qq{$x1+$x2};
}
sub gen_num {
my $in = @_;
my $n = q{123456789};
my $result = '';
foreach my $k (1..$in){
$result .= substr($a,int(rand(length($a))),1);
}
return $result;
}
sub out_capture{
my $cap_string = shift;
my $opt=shift;
#my $font = 'times.ttf';
my $font = q{./lib/capcha.ttf};
#my $pointsize = 70;
#my $path = './';
my $image = new Image::Magick;
# 1. Ñîçäà¸ì ïîëå 300x100 áåëîãî öâåòà.
$image->Set(size => ($opt->{width}*3).'x'.($opt->{height}*2));
#$opt->{background}='rgba(255, 255, 255, 100)';
$image->ReadImage('xc:'.$opt->{background});
# 2. Ïå÷àòàåì ÷åðíûì ñ àíòèàëèàñèíãîì
$image->Set(
type => 'TrueColor',
antialias => 'True',
fill => $opt->{color},
# ñòðîêó STRING øðèôòîì $font ðàçìåðîì $pointsize
font => $font,
pointsize => $opt->{fontsize},
);
$image->Draw(
primitive => 'text',
points => '20,75', # îðèåíòàöèÿ ñòðîêè òåêñòà âíóòðè êàðòèíêè
text => $cap_string, # ÷òî ïå÷àòàåì
);
# 3. Ïîäâèíóòü öåíòð âëåâî íà 100 òî÷åê +ñëó÷àéíàÿ ôëóêòóàöèÿ
$image->Extent(
#geometry => '400x120', # ìåíÿåì ðàçìåð êàðòèíêè
geometry => '400x120', # ìåíÿåì ðàçìåð êàðòèíêè
);
$image->Roll(
#x => 101+int(rand(4)),
x => 101#+int(rand(4)),
);
# 4. Ïåðâûé swirl íà ñëó÷àéíûé óãîë (îò 37 äî 51)
$image->Swirl(
degrees => (rand($opt->{deg2_to}))+$opt->{deg2_from} #37,
);
# 5. Ïîäâèíóòü öåíòð âïðàâî íà 200 òî÷åê, òîæå ñî ñëó÷àéíîé ôëóêòóàöèåé
$image->Extent(
#geometry => '600x140', # ìåíÿåì ðàçìåð êàðòèíêè
geometry => '600x140', # ìåíÿåì ðàçìåð êàðòèíêè
);
$image->Roll(
#x => 3-int(rand(4)),
x => 3#-int(rand(4)),
);
# 6. Âòîðîé ïîâîðîò (îò 20 äî 35)
$image->Swirl(
degrees => int(rand($opt->{deg1_to}))+$opt->{deg1_from},
);
# 7. Îêîí÷àòåëüíàÿ îáðàáîòêà è âûâîä
$image->Crop('230x80+110+7');
$image->Resize($opt->{width}.'x'.$opt->{height});
#$filename = $path . $filename;
#$filename .= '.png';
#open(IMAGE,'>',$filename) or die $!;
#$image->Write(file=>\*IMAGE, filename=>$filename);
#close(IMAGE);
print "Content-type: image/png\n\n";
binmode STDOUT;
$image->Write('png:-');
#return $filename;
}
sub get_options{
my $par=shift;
my $opt={
width=>'150',
height=>'50',
deg1_from=>0,
deg1_to=>30,
deg2_from=>0,
deg2_to=>30,
background=>'#ffffff',
color=>'#000000',
fontsize=>'60',
method=>'0', # 0 -- ïðîâåðî÷íàÿ ñòðîêà ; 1 -- ïðèìåð
# params=>'',
};
my $domain = $ENV{HTTP_HOST};
$domain=~s/^www\.//;
#print "Content-type: text/html\n\n$domain"; exit;
# ñ÷èòûâàåì ïàðàìåòðû
my $sth=$par->{dbh}->prepare(q{
SELECT cs.*
FROM project p, domain d, capture_setting cs
WHERE p.project_id = d.project_id and d.template_id=cs.template_id and d.domain = ?
});
$sth->execute($domain);
my $opt_tmp=$sth->fetchrow_hashref();
#print "Content-type: text/html\n\n$par->{project_id}<br>";
foreach my $attr (keys(%{$opt_tmp})){
# print "a: $attr<br>";
my $v=$opt_tmp->{$attr};
my $p=param($attr);
if($p=~m/^[a-zA-Z0-9#]+$/){ # ïðîâåðÿåì ïàðàìåòð ÷åðåç http
# print "X";
if($p=~m/^[a-f0-9]{6}$/i && ($attr eq 'color' || $attr eq 'background')){
$p='#'.$p;
}
$opt->{$attr}=$p;
print "value: $p";
}
elsif($v=~m/^[a-zA-Z0-9#]+$/){ # ïðîâåðÿåì ïàðàìåòð èç ÁÄ
#$opt->{params}.=qq{&$attr=$v};
if($v=~m/^[a-f0-9]{6}$/i && ($attr eq 'color' || $attr eq 'background')){
$v='#'.$v;
}
$opt->{$attr}=$v;
}
}
#use Data::Dumper;
#print Dumper($opt); exit;
return $opt;
}