auto/share/dist/Nes/cgi-bin/plugin/captcha_plugin.pm untested

filter: Statementstmt 0.0% 0 / 274 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 438 CC 31 SCAR 35.2
CC
31
Cov
0%
create
110.0
get_attempts
42.0
is_ok
42.0
CRAP 33.9
1  
2 # -----------------------------------------------------------------------------
3 #
4 # Nes by Skriptke
5 # Copyright 2009 - 2010 Enrique F. Castañón Barbero
6 # Licensed under the GNU GPL.
7 #
8 # CPAN:
9 # http://search.cpan.org/dist/Nes/
10 #
11 # Sample:
12 # http://nes.sourceforge.net/
13 #
14 # Repository:
15 # http://github.com/Skriptke/nes
16 #
17 # Version 1.03
18 #
19 # captcha_plugin.pm
20 #
21 # -----------------------------------------------------------------------------
22  
23 # despues de tanto parche, esto hay que volver a escribirlo.
24 package captcha_plugin;
25  
26 use strict;
27  
28 my $captcha;
29  
30 sub replace_captcha {
31 my ( $out, @param ) = @_;
32 my ( $name, $type, $digits, $noise, $size, $sig, $spc, $expire, $attempts ) = @param;
33  
34 $type = 'ascii' if !$type;
35 $captcha = nes_captcha->new( $name, $type, $digits, $noise, $size, $sig, $spc, $expire, $attempts );
36 $captcha->create;
37  
38 return $out;
39 }
40  
41 sub replace_captcha_code {
42 my ( $out, @param ) = @_;
43  
44 return $captcha->out();
45 }
46  
47 sub verify {
48 my ( $name, $type, $expire, $attempts ) = @_;
49  
50 $captcha = nes_captcha->new( $name,$type,'','','','','', $expire, $attempts );
51 $captcha->verify();
52 $captcha->{'tmp'}->clear() if $captcha->{'is_ok'};
53
54 }
55  
56  
57 {
58  
59 package nes_captcha_plugin;
60 use vars qw(@ISA);
61 @ISA = qw( nes_interpret );
62  
63 sub new {
64 my $class = shift;
65 my ($out) = @_;
66 my $self = $class->SUPER::new($out);
67
68 foreach my $tag ( keys %{ $self->{'container'}->{'content_obj'}->{'tags'} } ) {
69 $self->{'tags'}{$tag} = $self->{'container'}->{'content_obj'}->{'tags'}{$tag};
70 }
71  
72 $self->{'plugin'} = nes_plugin->new( 'captcha_plugin', 'captcha_plugin', $self );
73  
74 $self->{'tag_captcha'} = 'captcha';
75 $self->{'tag_captcha_out'} = 'captcha_code';
76 $self->{'clear_out'} = 0;
77  
78 return $self;
79 }
80  
81 sub replace_block {
82 my $self = shift;
83 my ( $block, $space1, $space2 ) = @_;
84 my ( $tag, $params, $code ) = $block =~ /$self->{'block_plugin'}/;
85 my $out;
86  
87 if ( $tag && $tag =~ /^$self->{'tag_captcha'}$/ ) {
88  
89 $out = $self->replace_captcha( $code, $self->param_block($params) );
90  
91 } else {
92  
93 $block =~ s/(^\s*)($self->{'pre_start'})/$1$self->{'tag_start'}/g;
94 $block =~ s/($self->{'pre_end'})(\s*$)/$self->{'tag_end'}$2/g;
95  
96 return $block;
97 }
98  
99 $out .= $space1;
100 return $out;
101 }
102  
103 sub replace_captcha {
104 my $self = shift;
105 my ( $out, @param ) = @_;
106 my ( $name, $type, $digits, $noise, $size, $sig, $spc, $expire, $attempts ) = @param;
107  
108 return '' if $self->{'clear_out'};
109  
110 $type = 'ascii' if !$type;
111 $self->{'captcha'}{$name} = nes_captcha->new( $name, $type, $digits, $noise, $size, $sig, $spc, $expire, $attempts );
112 $self->{'captcha'}{$name}->create;
113  
114 my $captcha_code = $self->{'captcha'}{$name}->out();
115 $out =~ s/$self->{'pre_start'}\s*$self->{'tag_plugin'}\s*$self->{'tag_captcha_out'}\s*(.+?)\s*$self->{'pre_end'}/$captcha_code/gi;
116  
117 return $out;
118 }
119  
120 }
121  
122 {
123  
124 package nes_captcha;
125 use vars qw(@ISA);
126 @ISA = qw( Nes );
127  
128 sub new {
129 my $class = shift;
130 my ( $name, $type, $digits, $noise, $size, $sig, $spc, $expire, $attempts ) = @_;
131 my $self = $class->SUPER::new();
132  
133 foreach my $tag ( keys %{ $self->{'container'}->{'content_obj'}->{'tags'} } ) {
134 $self->{'tags'}{$tag} = $self->{'container'}->{'content_obj'}->{'tags'}{$tag};
135 }
136
137 $self->{'cookie_name'} = 'cp_'.$name;
138  
139 $self->{'plugin'} = nes_plugin->get_obj('captcha_plugin');
140  
141 if ($self->{'plugin'} =~ /nes_plugin/) {
142 # antiguo método, obsoleto
143 $self->{'plugin'}->add_obj( $name, $self );
144 }
145 # nevo método
146 $self->{'plugin'} = $self->{'register'}->add_obj('captcha_plugin', $name, $self);
147  
148 $attempts = $self->{'CFG'}{'captcha_plugin_max_attempts'} if !$attempts;
149 ($self->{'max_attempts'}, $self->{'max_time'}) = split ('/',$attempts);
150  
151 $self->{'expire_tmp'} = utl::expires_time( $self->{'CFG'}{'captcha_plugin_expire_attempts'} );
152 $self->{'expire'} = $expire || $self->{'CFG'}{'captcha_plugin_expire'};
153 $self->{'captcha_start'} = $self->{'query'}->get( $self->{'CFG'}{'captcha_plugin_start'} . '_' . $name ) || '';
154 $self->{'class'} = 'nes_captcha_' . $type;
155 $self->{'captcha'} = $self->{'class'}->new( $name, $digits, $noise, $size, $sig, $spc );
156 $self->{'captcha_name'} = $name || '';
157 $self->{'tmp'} = nes_tmp->new($self->{'CFG'}{'captcha_plugin_suffix'},$self->{'captcha_name'});
158  
159 $self->{'is_ok'} = 0;
160 $self->load_captcha();
161  
162 $self->get_attempts;
163  
164 return $self;
165 }
166
167 sub create {
168 my $self = shift;
169  
170 $self->{'captcha'}->create();
171 $self->{'key_ok'} = $self->{'captcha'}->{'key_ok'};
172 $self->save_captcha();
173 $self->{'tmp'}->save(time.':') if $self->{'captcha_start'} eq $self->{'captcha_name'};
174 $self->get_attempts;
175
176 }
177
178 sub get_attempts {
179 my $self = shift;
180  
181 my @data = $self->{'tmp'}->load();
182 my ( $first_attempt, $used_key ) = split( ':', $data[0] );
183 my $attempts = $#data;
184 $first_attempt =~ s/:.*//;
185 $self->{'used_key'} = $used_key;
186  
187 if ( $first_attempt ) {
188 if ( time - $first_attempt > $self->{'expire_tmp'} ) {
189 $self->{'tmp'}->clear();
190 $attempts = -1;
191 $first_attempt = time;
192 $self->{'used_key'} = '';
193 }
194 }
195
196 if ( $attempts > $self->{'max_attempts'} ) {
197 $self->{'tmp'}->clear() if time - $first_attempt > ( $self->{'max_time'} * 60 );
198 }
199
200 $self->{'attempts'} = $attempts + 1;
201 $self->{'attempts'} = 0 if $attempts < 0;
202 $self->{'plugin'}->add_env( 'captcha_plugin', $self->{'captcha_name'}, 'attempts', $self->{'attempts'} );
203  
204 return;
205 }
206  
207 sub verify {
208 my $self = shift;
209  
210 $self->{'fatal_error'} = 0;
211  
212 # no se ha terminado de llenar el formulario
213 $self->{'last_error'} = 'no captcha start';
214 $self->{'plugin'}->add_fatal_error( 'captcha_plugin', $self->{'captcha_name'}, 0 );
215 $self->{'plugin'}->add_last_error( 'captcha_plugin', $self->{'captcha_name'}, 'no captcha start' );
216 return 0 if $self->{'captcha_start'} ne $self->{'captcha_name'};
217  
218 return $self->is_ok( $self->{'query'}->get( $self->{'captcha_name'} ) );
219 }
220  
221 sub is_ok {
222 my $self = shift;
223 my ($key_ok) = @_;
224
225 $self->{'is_ok'} = 0;
226
227 $self->{'last_error'} = 3;
228 $self->{'fatal_error'} = 3;
229 $self->{'plugin'}->add_last_error( 'captcha_plugin', $self->{'captcha_name'}, "max attempts, wait $self->{'max_time'} minutes" );
230 $self->{'plugin'}->add_fatal_error( 'captcha_plugin', $self->{'captcha_name'}, 3 );
231 return 0 if $self->{'attempts'} > $self->{'max_attempts'};
232
233 my $expire = $self->{'expire'};
234 $self->{'last_error'} = 1;
235 $self->{'fatal_error'} = 1;
236 $self->{'plugin'}->add_last_error( 'captcha_plugin', $self->{'captcha_name'}, 'expire' );
237 $self->{'plugin'}->add_fatal_error( 'captcha_plugin', $self->{'captcha_name'}, 1 );
238 return (0) if ( $self->{'time'} + utl::expires_time($expire) ) < time;
239  
240 $self->{'last_error'} = 2;
241 $self->{'fatal_error'} = 2;
242 $self->{'plugin'}->add_last_error( 'captcha_plugin', $self->{'captcha_name'}, 'no key' );
243 $self->{'plugin'}->add_fatal_error( 'captcha_plugin', $self->{'captcha_name'}, 2 );
244 return (0) if !$key_ok;
245
246 # impide que guarden una cookie válida para volver a usarla
247 $self->{'last_error'} = 4;
248 $self->{'fatal_error'} = 4;
249 $self->{'plugin'}->add_last_error( 'captcha_plugin', $self->{'captcha_name'}, 'already been used' );
250 $self->{'plugin'}->add_fatal_error( 'captcha_plugin', $self->{'captcha_name'}, 4 );
251 return (0) if $self->{'used_key'} eq $key_ok;
252  
253 if ( $key_ok eq $self->{'load_key_ok'} ) {
254 $self->{'last_error'} = 0;
255 $self->{'fatal_error'} = 0;
256 $self->{'plugin'}->add_last_error( 'captcha_plugin', $self->{'captcha_name'}, '' );
257 $self->{'plugin'}->add_fatal_error( 'captcha_plugin', $self->{'captcha_name'}, 0 );
258 $self->{'plugin'}->add_env( 'captcha_plugin', $self->{'captcha_name'}, 'is_ok', 1 );
259 $self->{'is_ok'} = 1;
260 my $data = time.':'.$key_ok;
261 $self->{'tmp'}->clear($data);
262 $self->get_attempts;
263 return (1);
264 }
265  
266 $self->{'last_error'} = 5;
267 $self->{'fatal_error'} = 5;
268 $self->{'plugin'}->add_last_error( 'captcha_plugin', $self->{'captcha_name'}, 'incorrect key' );
269 $self->{'plugin'}->add_fatal_error( 'captcha_plugin', $self->{'captcha_name'}, 5 );
270 $self->{'is_ok'} = 0;
271 return (0);
272 }
273  
274 sub save_captcha {
275 my $self = shift;
276  
277 my $key_ok = $self->{'key_ok'};
278 my $refuse1 = $self->get_key( 20 + int rand 10 );
279 $refuse1 =~ s/\://g;
280 my $refuse2 = $self->get_key( 20 + int rand 10 );
281 $refuse2 =~ s/\://g;
282 my $value = $refuse1 . ':' . $key_ok . ':' . time . ':' . $refuse2;
283  
284 my $expire = $self->{'expire'};
285  
286 $self->{'cookies'}->create( $self->{'cookie_name'}, $value, $expire );
287
288 return;
289 }
290  
291 sub load_captcha {
292 my $self = shift;
293  
294 $self->{'cookie'} = $self->{'cookies'}->get( $self->{'cookie_name'} );
295  
296 my $refuse;
297 ( $refuse, $self->{'load_key_ok'}, $self->{'time'}, $refuse ) = split( ':', $self->{'cookie'} );
298  
299 return;
300 }
301  
302 sub out {
303 my $self = shift;
304  
305 return $self->{'captcha'}->out;
306 }
307
308 }
309  
310 {
311  
312 package nes_captcha_ascii;
313 use vars qw(@ISA);
314 @ISA = qw( Nes );
315  
316 sub new {
317 my $class = shift;
318 my ( $name, $digits, $noise_level, $size, $sig_char, $space_char ) = @_;
319 my $self = $class->SUPER::new();
320  
321 $self->{'captcha_name'} = $name;
322  
323 my @sigs = ( '0', 'X', '9', '@', '&#9617;', '&#9619;', '&#9689;' );
324 my @spaces = ( ' ', '·', '.', ',', ' ', '&#732;', '&#39;', '&nbsp;', '&#8249;', '&nbsp;' );
325  
326 $noise_level = 9 if $noise_level > 9;
327 $self->{'noise_level'} = $self->{'CFG'}{'captcha_plugin_noise'} || 1;
328  
329 # $noise_level puede ser 0
330 $self->{'noise_level'} = $noise_level if defined $noise_level;
331  
332 $self->{'digits'} = $digits || $self->{'CFG'}{'captcha_plugin_digits'} || ( 5 + int rand 3 );
333 $self->{'size'} = $size || $self->{'CFG'}{'captcha_plugin_size'} || 2;
334 $self->{'sig_char'} = $sig_char || $self->{'CFG'}{'captcha_plugin_sig'} || ( $sigs[ int rand $#sigs + 1 ] );
335 $self->{'space_char'} = $space_char || $self->{'CFG'}{'captcha_plugin_spc'} || ( $spaces[ int rand $#spaces + 1 ] );
336  
337 return ($self);
338 }
339  
340 sub create {
341 my $self = shift;
342  
343 my $fonts = nes_captcha_fonts->new();
344 my @type = @{ $fonts->{'ceros'}{'font'} };
345 my $sig = $fonts->{'ceros'}{'sig'};
346  
347 my $nums;
348 for ( 1 .. $self->{'digits'} ) {
349 $nums .= int rand 10;
350 }
351  
352 $self->{'key_ok'} = $nums;
353  
354 my @lines;
355 my $noise;
356 foreach my $num ( split( '', $nums ) ) {
357 my @char = @{ $type[$num] };
358 my $n1 = int rand 9;
359 my $space = ' ' x 1;
360 $space = ' ' x ( int rand 4 ) if $self->{'noise_level'};
361 unshift( @char, pop(@char) ) if int rand 2;
362 for ( my $i = 0 ; $i < @char ; $i++ ) {
363 $lines[$i] .= $space . $char[$i] . $space;
364 }
365 }
366  
367 # add noise
368 if ( $self->{'noise_level'} ) {
369  
370 # corta una columna, dificulta que se detecte el comienzo de cada número
371 my $colums = ',';
372 for ( my $c = 0 ; $c < length( $lines[0] ) ; $c++ ) {
373 if ( !int rand 9 - $self->{'noise_level'} ) {
374 $colums .= $c . ',';
375 $c = $c + 4;
376 }
377 }
378 foreach my $line (@lines) {
379 my $colum = int rand length($line);
380 for ( my $c = 0 ; $c < length($line) ; $c++ ) {
381 for ( my $level = 0 ; $level < $self->{'noise_level'} && $level < 10 ; $level++ ) {
382 substr( $line, $c, 1, ' ' ) if $colums =~ /,$c,/;
383 substr( $line, $c, 1, $sig ) if $colums =~ /,$c,/ && !int rand 5;
384 substr( $line, $c, 1, $sig ) if !int rand 50;
385 substr( $line, $c, 1, ' ' ) if !int rand 50;
386 }
387 }
388 }
389 }
390  
391 my $line_out;
392 foreach my $line (@lines) {
393 $line =~ s/ /$self->{'space_char'}/gi;
394 $line =~ s/$sig/$self->{'sig_char'}/gi;
395 $line .= "\n";
396 $line_out .= $line;
397 }
398  
399 my $captcha_form_fstart = '<input type="hidden" name=":-:name:-:" value=":-:value:-:" />';
400 $captcha_form_fstart =~ s/:-:name:-:/$self->{'CFG'}{'captcha_plugin_start'}_$self->{'captcha_name'}/;
401 $captcha_form_fstart =~ s/:-:value:-:/$self->{'captcha_name'}/;
402 $line_out .= "\n" . $captcha_form_fstart;
403 if ( $self->{'size'} ne 'none' ) {
404 $line_out = '<pre style="font-size:' . $self->{'size'} . 'px; line-height:1.0;">' . $line_out . '<br></pre>';
405 }
406  
407 $self->{'out'} = $line_out;
408  
409 return;
410 }
411  
412 sub out {
413 my $self = shift;
414  
415 return $self->{'out'};
416 }
417  
418 }
419  
420 {
421  
422 package nes_captcha_fonts;
423 use vars qw(@ISA);
424 @ISA = qw( nes );
425  
426 sub new {
427 my $class = shift;
428 my $self = bless {}, $class;
429  
430 # con el caracter que esta escrita la fuente
431 $self->{'ceros'}{'sig'} = '0';
432 @{ $self->{'ceros'}{'font'} } = (
433 [
434 ' ',
435 ' 0000000000000 ',
436 '000000000000000',
437 '0000 0000',
438 '0000 0000',
439 '0000 0000',
440 '0000 0000',
441 '0000 0000',
442 '0000 0000',
443 '0000 0000',
444 '0000 0000',
445 '000000000000000',
446 ' 0000000000000 ',
447 ' ',
448 ' ',
449 ],
450 [
451 ' ',
452 ' 000000 ',
453 ' 0000000 ',
454 ' 0000 ',
455 ' 0000 ',
456 ' 0000 ',
457 ' 0000 ',
458 ' 0000 ',
459 ' 0000 ',
460 ' 0000 ',
461 ' 0000 ',
462 '000000000000000',
463 ' 0000000000000 ',
464 ,
465 ' ',
466 ' ',
467 ],
468 [
469 ' ',
470 ' 0000000000000 ',
471 '000000000000000',
472 ' 0000',
473 ' 0000',
474 ' 0000',
475 ' 00000000000000',
476 '00000000000000 ',
477 '0000 ',
478 '0000 ',
479 '0000 ',
480 '000000000000000',
481 ' 0000000000000 ',
482 ' ',
483 ' ',
484 ],
485 [
486 ' ',
487 ' 0000000000000 ',
488 '000000000000000',
489 ' 0000',
490 ' 0000',
491 ' 0000',
492 ' 00000000000000',
493 ' 00000000000000',
494 ' 0000',
495 ' 0000',
496 ' 0000',
497 '000000000000000',
498 ' 0000000000000 ',
499 ' ',
500 ' ',
501 ],
502 [
503 ' ',
504 ' 000 000 ',
505 '0000 0000',
506 '0000 0000',
507 '0000 0000',
508 '0000 0000',
509 '000000000000000',
510 ' 00000000000000',
511 ' 0000',
512 ' 0000',
513 ' 0000',
514 ' 0000',
515 ' 000 ',
516 ' ',
517 ' ',
518 ],
519 [
520 ' ',
521 ' 0000000000000 ',
522 '000000000000000',
523 '0000 ',
524 '0000 ',
525 '0000 ',
526 '00000000000000 ',
527 ' 00000000000000',
528 ' 0000',
529 ' 0000',
530 ' 0000',
531 '000000000000000',
532 ' 0000000000000 ',
533 ' ',
534 ' ',
535 ],
536 [
537 ' ',
538 ' 0000000000000 ',
539 '000000000000000',
540 '0000 ',
541 '0000 ',
542 '0000 ',
543 '00000000000000 ',
544 '000000000000000',
545 '0000 0000',
546 '0000 0000',
547 '0000 0000',
548 '000000000000000',
549 ' 0000000000000 ',
550 ' ',
551 ' ',
552 ],
553 [
554 ' ',
555 ' 0000000000000 ',
556 '000000000000000',
557 ' 0000',
558 ' 0000',
559 ' 0000',
560 ' 0000',
561 ' 0000',
562 ' 0000',
563 ' 0000',
564 ' 0000',
565 ' 0000',
566 ' 000 ',
567 ' ',
568 ' ',
569 ],
570 [
571 ' ',
572 ' 0000000000000 ',
573 '000000000000000',
574 '0000 0000',
575 '0000 0000',
576 '0000 0000',
577 '000000000000000',
578 '000000000000000',
579 '0000 0000',
580 '0000 0000',
581 '0000 0000',
582 '000000000000000',
583 ' 0000000000000 ',
584 ' ',
585 ' ',
586 ],
587 [
588 ' ',
589 ' 0000000000000 ',
590 '000000000000000',
591 '0000 0000',
592 '0000 0000',
593 '0000 0000',
594 '000000000000000',
595 ' 00000000000000',
596 ' 0000',
597 ' 0000',
598 ' 0000',
599 ' 0000',
600 ' 000 ',
601 ' ',
602 ' ',
603 ]
604 );
605  
606 return $self;
607 }
608  
609 }
610  
611 1;