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

filter: Statementstmt 0.0% 0 / 310 Branchbran 0.0% 0 / 118 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 508 CC 60 SCAR 45.7
CC
60
Cov
0%
check_field
272.0
replace_control
132.0
is_ok
110.0
CRAP 96.5
1  
2 # -----------------------------------------------------------------------------
3 #
4 # Nes by Skriptke
5 # Copyright 2009 - 2010 Enrique F. Castañón
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 # forms_plugin.pm
20 #
21 # -----------------------------------------------------------------------------
22  
23  
24 package forms_plugin;
25  
26 use strict;
27  
28 {
29  
30 package nes_forms_plugin;
31 use vars qw(@ISA);
32 @ISA = qw( nes_interpret );
33  
34 sub new {
35 my $class = shift;
36 my ($out) = @_;
37 my $self = $class->SUPER::new($out);
38  
39 $self->{'plugin'} = nes_plugin->new( 'forms_plugin', 'forms_plugin', $self );
40 $self->{'captcha_name'} = '';
41  
42 $self->{'tag_form'} = 'form';
43
44 foreach my $tag ( keys %{ $self->{'container'}->{'content_obj'}->{'tags'} } ) {
45 $self->{'tags'}{$tag} = $self->{'container'}->{'content_obj'}->{'tags'}{$tag};
46 }
47  
48 return $self;
49 }
50  
51 sub replace_block {
52 my $self = shift;
53 my ( $block, $space1, $space2 ) = @_;
54 my ( $tag, $params, $code ) = $block =~ /$self->{'block_plugin'}/;
55 my $out;
56  
57 if ( $tag && $tag =~ /^$self->{'tag_form'}$/ ) {
58  
59 $out = $self->replace_form( $code, $self->param_block($params) );
60  
61 } else {
62  
63 $block =~ s/(^\s*)($self->{'pre_start'})/$1$self->{'tag_start'}/g;
64 $block =~ s/($self->{'pre_end'})(\s*$)/$self->{'tag_end'}$2/g;
65  
66 return $block;
67 }
68  
69 $out .= $space1;
70 return $out;
71 }
72  
73 sub replace_form {
74 my $self = shift;
75 my ( $code, @param ) = @_;
76  
77 $self->{'name'} = shift @param;
78 $self->{'auto_submit'} = 1 if $self->{'auto_submit'} ne '0';
79  
80 $self->{'form'}{ $self->{'name'} } = nes_forms->new( $code, $self->{'name'}, @param );
81  
82 return $self->{'form'}{ $self->{'name'} }->out();
83 }
84
85 }
86  
87 {
88  
89 package nes_forms;
90 use vars qw(@ISA);
91 @ISA = qw( nes_interpret );
92  
93 sub new {
94 my $class = shift;
95 my ( $out, $name, $auto_submit, $captcha_name, $captcha_last, $expire, $expire_last, $location, $attempts, $attempts_for_captcha ) = @_;
96 my $self = $class->SUPER::new();
97  
98 foreach my $tag ( keys %{ $self->{'container'}->{'content_obj'}->{'tags'} } ) {
99 $self->{'tags'}{$tag} = $self->{'container'}->{'content_obj'}->{'tags'}{$tag};
100 }
101  
102 $self->{'plugin'} = nes_plugin->get_obj('forms_plugin');
103 $self->{'plugin'}->add_obj( $name, $self );
104
105 $self->{'attempts_for_captcha'} = $attempts_for_captcha;
106  
107 $self->{'tag_obfuscated'} = 'obfuscated';
108 $self->{'tag_form_check'} = 'check';
109  
110 $self->{'out'} = $out;
111 $self->{'name'} = $name;
112 $self->{'auto_submit'} = 0;
113 $self->{'auto_submit'} = 1 if $auto_submit ne '0';
114 $self->{'captcha_name'} = $captcha_name;
115 $self->{'captcha_last'} = $captcha_last;
116 $self->{'location'} = $location;
117
118 $attempts = $self->{'CFG'}{'forms_plugin_max_attempts'} if !$attempts;
119 ( $self->{'max_attempts'}, $self->{'max_time'} ) = split( '/', $attempts );
120  
121 $self->{'expire_tmp'} = utl::expires_time( $self->{'CFG'}{'forms_plugin_expire_attempts'} );
122 $self->{'form_start_name'} = $self->{'CFG'}{'forms_plugin_start'} . '_' . $self->{'name'};
123 $self->{'form_finish_name'} = $self->{'CFG'}{'forms_plugin_finish'} . '_' . $self->{'name'};
124 $self->{'form_start'} = $self->{'query'}->get( $self->{'form_start_name'} );
125 $self->{'form_finish'} = $self->{'query'}->get( $self->{'form_finish_name'} );
126 $self->{'form_start_field'} = '<input type="hidden" name="' . $self->{'form_start_name'} . '" value="' . $self->{'name'} . '" />';
127 $self->{'form_finish_field'} = '<input type="hidden" name="' . $self->{'form_finish_name'} . '" value="' . $self->{'name'} . '" />';
128 $self->{'auto_submit_field'} = '';
129 $self->{'auto_submit_field'} = 'document.' . $self->{'name'} . '.submit();';
130  
131 $self->{'tmp'} = nes_tmp->new( $self->{'CFG'}{'forms_plugin_suffix'}, $self->{'name'} );
132  
133 $self->{'form_is_start'} = 0;
134 $self->{'form_is_start'} = 1 if $self->{'form_start'} eq $self->{'name'};
135 $self->{'form_is_finish'} = 0;
136 $self->{'form_is_finish'} = 1 if $self->{'form_finish'} eq $self->{'name'};
137  
138 $self->{'expire'} = $expire || $self->{'CFG'}{'forms_plugin_expire'};
139 $self->{'expire_last'} = $expire_last || $self->{'CFG'}{'forms_plugin_expire_last'};
140
141 if ( $self->{'form_is_start'} && !$self->{'form_finish'} ) {
142 $self->{'expire'} = $self->{'expire_last'};
143 }
144  
145 $self->{'is_ok'} = 0;
146  
147 $self->replace_form();
148  
149 return $self;
150 }
151  
152 sub replace_form {
153 my $self = shift;
154  
155 if ( $self->{'form_is_start'} ) {
156 $self->load();
157 $self->{'tmp'}->save( time . ':' ) if !$self->{'form_is_finish'};
158 }
159
160 $self->get_attempts;
161  
162 if ( $self->{'attempts'} < $self->{'attempts_for_captcha'}+1 && $self->{'attempts_for_captcha'} ) {
163 $self->{'auto_submit'} = 1;
164 $self->{'captcha_name'} = '';
165 $self->{'captcha_last'} = '';
166 }
167  
168 $self->replace_check();
169 $self->replace_obfuscated();
170 $self->replace_init_form();
171 $self->replace_control();
172 $self->save() if $self->verify();
173  
174 }
175  
176 sub get_attempts {
177 my $self = shift;
178  
179 my @data = $self->{'tmp'}->load();
180 my ( $first_attempt, $used_key ) = split( ':', $data[0] );
181 my $attempts = $#data;
182 $first_attempt =~ s/:.*//;
183 $self->{'used_key'} = $used_key;
184  
185 if ( $first_attempt ) {
186 if ( time - $first_attempt > $self->{'expire_tmp'} ) {
187 $self->{'tmp'}->clear();
188 $attempts = -1;
189 $first_attempt = time;
190 $self->{'used_key'} = '';
191 }
192 }
193
194 if ( $attempts > $self->{'max_attempts'} ) {
195 $self->{'tmp'}->clear() if time - $first_attempt > ( $self->{'max_time'} * 60 );
196 }
197
198 $self->{'attempts'} = $attempts + 1;
199 $self->{'attempts'} = 0 if $attempts < 0;
200 $self->{'plugin'}->add_env( 'forms_plugin', $self->{'name'}, 'attempts', $self->{'attempts'} );
201  
202 return;
203 }
204  
205 sub captcha_check {
206 my $self = shift;
207  
208 my $captcha = nes_plugin->get( 'captcha_plugin', $self->{'captcha_name'} );
209 return (1) if $captcha->{'is_ok'};
210  
211 return (0);
212 }
213  
214 sub replace_check {
215 my $self = shift;
216  
217 if ( $self->{'form_is_start'} ) {
218 $self->{'out'} =~
219 s/($self->{'pre_start'}\s*$self->{'tag_plugin'}\s*$self->{'tag_form_check'}\s*(\([^\(]*\)|[^\s]*)\s*$self->{'pre_end'})/$self->check_field($2)/gei;
220 } else {
221 $self->{'out'} =~ s/($self->{'pre_start'}\s*$self->{'tag_plugin'}\s*$self->{'tag_form_check'}\s*(\([^\(]*\)|[^\s]*)\s*$self->{'pre_end'})//gi;
222 }
223  
224 return;
225 }
226  
227 sub check_field {
228 my $self = shift;
229 my ($params) = @_;
230 my @param = $self->param_block($params);
231  
232 undef $self->{'error_check'};
233  
234 foreach my $this (@param) {
235 my ( $field, $option ) = split( /\s*:\s*/, $this );
236 my ( $min, $max, $type ) = split( ',', $option );
237 my $this_field = $self->{'query'}->get($field);
238 my $error;
239 if ($min) {
240 $error .= 'min, ' if length($this_field) < $min;
241 }
242 if ($max) {
243 $error .= 'max, ' if length($this_field) > $max;
244 }
245 if ( $type eq 'num' ) {
246 $error .= 'num, ' if $this_field =~ /\D+/;
247 }
248 if ( $type eq 'email' ) {
249  
250 if ($this_field) { # sólo da error si el campo no esta vacío, podía ser opcional
251 $error .= 'email, ' if $this_field !~ /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
252 }
253  
254 }
255 if ( $type =~ /^\/(.*)\/(.*)$/ ) {
256 my $regex = "(?x$2)$1";
257  
258 if ($this_field) { # sólo da error si el campo no esta vacío, podía ser opcional
259 $error .= 'regular expression' if $this_field !~ /$regex/;
260 }
261  
262 }
263 $self->{'error_check'} = 1 if $error;
264  
265 $self->{'plugin'}->add_error( 'forms_plugin', $self->{'name'}, 'field_' . $field, $error ) if $error;
266 $self->{'plugin'}->add_error( 'forms_plugin', $self->{'name'}, 'field', $error ) if $error;
267  
268 }
269  
270 return;
271 }
272  
273 sub replace_obfuscated {
274 my $self = shift;
275
276 # $self->{'out'} =~ s/$self->{'pre_start'}\s*$self->{'tag_plugin'}\s*$self->{'tag_obfuscated'}\s*(.+?)\s*$self->{'pre_end'}/$self->{'obfuscated'}{$1} = $self->get_key( 5 + int rand 4 )/egi;
277 # return;
278
279 my $obfuscated_tag = qr{(?six)
280 \s*$self->{'pre_start'}\s*
281 $self->{'tag_plugin'}
282 \s*
283 $self->{'tag_obfuscated'} # tag del plugin
284 \s*
285 (\([^\(\)]+\)|[^\(\)]\S+) # parametros
286 \s*
287 $self->{'pre_end'}\s*
288 };
289
290 $self->{'out'} =~ s/$obfuscated_tag/$self->obfuscated($self->param_block($1))/ge;
291
292 return;
293 }
294
295 sub obfuscated {
296 my $self = shift;
297 my ( $field ) = @_;
298  
299 return $self->{'obfuscated'}{$field} = $self->get_key( 5 + int rand 4 );
300 }
301
302 sub replace_init_form {
303 my $self = shift;
304  
305 # siempre, indica que se ha iniciado el formulario
306 $self->{'out'} =~ s/(\<\/form\>)/$self->{'form_start_field'}$1/;
307  
308 return;
309 }
310  
311 sub replace_control {
312 my $self = shift;
313  
314 # errores en check
315 return if $self->{'error_check'};
316  
317 if ( $self->{'captcha_name'} ) {
318 if ( $self->{'captcha_last'} ) {
319 $self->{'form_is_finish'} = 0 if !$self->captcha_check();
320 } else {
321 return if !$self->captcha_check();
322 }
323 }
324  
325 # desactivar los campos en el segundo paso.
326 if ( $self->{'form_is_start'} && !$self->{'form_is_finish'} && !$self->{'error_check'} ) {
327  
328 # es posible que falten tag a desactivar
329 $self->{'out'} =~ s/<input /<input readonly=\"readonly\" /gi;
330 $self->{'out'} =~ s/<textarea /<textarea readonly=\"readonly\" /gi;
331 $self->{'out'} =~ s/<select /<input readonly=\"readonly\" /gi;
332  
333 # excluimos submit y captcha
334 $self->{'out'} =~ s/<input readonly=\"readonly\" (.*type\s*=\s*\"?hidden\"?)/<input $1/gi;
335 $self->{'out'} =~ s/<input readonly=\"readonly\" (.*type\s*=\s*\"?submit\"?)/<input $1/gi;
336 $self->{'out'} =~ s/<input readonly=\"readonly\" (.*name\s*=\s*\"?$self->{'CFG'}{'captcha_plugin_start'}_$self->{'captcha_name'}\"?)/<input $1/gi if $self->{'captcha_name'};
337 $self->{'out'} =~ s/<input readonly=\"readonly\" (.*name\s*=\s*\"?$self->{'captcha_name'}\"?)/<input $1/gi if $self->{'captcha_name'};
338 $self->{'out'} =~ s/<input readonly=\"readonly\" (.*this_is_captcha_field)/<input $1/gi;
339 }
340  
341 $self->{'auto_submit_field'} = '' if !$self->{'auto_submit'};
342 if ( $self->{'form_is_start'} && !$self->{'form_is_finish'} ) {
343 $self->{'last_step'} = 1;
344 $self->{'plugin'}->add_env( 'forms_plugin', $self->{'name'}, 'last_step', '1' );
345 $self->{'key_name'} = $self->get_key( 9 + int rand 9 );
346 $self->{'key_value'} = $self->get_key( 9 + int rand 9 );
347 my $form_key = '<input type="hidden" name="' . $self->{'key_name'} . '" value="' . $self->{'key_value'} . '" />';
348 my $js_code = $form_key;
349 my $js_var = $self->get_key( 9 + int rand 9 );
350 $js_code =
351 '<script>' . $js_var . ' = unescape(\'' . utl::escape($js_code) . '\');document.write(' . $js_var . ');' . $self->{'auto_submit_field'} . '</script>';
352 $self->{'out'} =~ s/(\<\/form\>)/$self->{'form_finish_field'}$js_code\n$1/;
353 }
354  
355 return;
356 }
357  
358 sub out {
359 my $self = shift;
360  
361 return $self->{'out'};
362  
363 }
364  
365 sub is_ok {
366 my $self = shift;
367  
368 $self->{'fatal_error'} = 0;
369  
370 # no se ha terminado de llenar el formulario
371 $self->{'last_error'} = 'no form start';
372 $self->{'fatal_error'} = 0;
373 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'no form start' );
374 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '' );
375 return 0 if !$self->{'form_is_start'};
376  
377 # maximo de intentos
378 $self->{'last_error'} = "5 max attempts, wait $self->{'max_time'} minutes";
379 $self->{'fatal_error'} = 5;
380 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '5' );
381 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, "max attempts, wait $self->{'max_time'} minutes" );
382 return 0 if $self->{'attempts'} > $self->{'max_attempts'};
383  
384 # no existe la cookie
385 $self->{'last_error'} = 'no cookie';
386 $self->{'fatal_error'} = 1;
387 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '1' );
388 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'no cookie o expire' );
389 return 0 if !$self->{'cookie'};
390  
391 # la cookie ha expirado, expiración interna, "posible" manipulación de la cookie
392 $self->{'last_error'} = 'cookie expired, posible manipulate cookie';
393 $self->{'fatal_error'} = 2;
394 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '2' );
395 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'cookie expired, posible manipulate cookie' );
396 return 0 if $self->{'expired'};
397  
398 # no se ha terminado de llenar el formulario
399 $self->{'last_error'} = 'no form finish';
400 $self->{'fatal_error'} = 0;
401 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'no form finish' );
402 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '' );
403 return 0 if !$self->{'form_is_finish'};
404  
405 # hash no coninciden, seguramente la cookie ha sido manipulada
406 $self->{'last_error'} = 'posible manipulate cookie data';
407 $self->{'fatal_error'} = 3;
408 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '3' );
409 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'posible manipulate cookie data' );
410 return 0 if $self->{'generated_hash'} ne $self->{'load_hash'};
411  
412 # no se ha definido la "key", tal vez no tenga javascript habilitado
413 $self->{'last_error'} = 'no javascript key, javascript enabled?';
414 $self->{'fatal_error'} = 4;
415 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '4' );
416 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'no javascript key, javascript enabled?' );
417 return 0 if !$self->{'query'}->get( $self->{'key_name'} );
418  
419 # se intenta usar una cookie válida ya usada
420 $self->{'last_error'} = "already been used";
421 $self->{'fatal_error'} = 6;
422 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '6' );
423 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, "already been used" );
424 return 0 if $self->{'used_key'} eq $self->{'key_value'};
425  
426 # javascript está habilitado, posible manipulación del formulario
427 $self->{'last_error'} = 'no javascript key value, posible manipulate form';
428 $self->{'fatal_error'} = 7;
429 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '7' );
430 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'no javascript key value, posible manipulate form' );
431 return 0 if $self->{'key_value'} ne $self->{'query'}->get( $self->{'key_name'} );
432  
433 $self->{'last_error'} = '';
434 $self->{'fatal_error'} = 0;
435 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, 0 );
436 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'ok' );
437 # my $data = time . ':' . $self->{'key_value'};
438 # $self->{'tmp'}->clear($data);
439  
440 $self->{'is_ok'} = 1;
441  
442 return 1;
443 }
444  
445 sub verify {
446 my $self = shift;
447  
448 # no se ha comenzado el formulario
449 return 1 if !$self->{'form_is_start'};
450  
451 $self->{'fatal_error'} = 1;
452 $self->{'last_error'} = '1 no cookie o expire';
453 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '1' );
454 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'no cookie o expire' );
455 return 0 if !$self->{'cookie'};
456  
457 $self->{'fatal_error'} = 2;
458 $self->{'last_error'} = '2 manipulate expire';
459 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '2' );
460 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'manipulate expire' );
461 return 0 if $self->{'expired'};
462  
463 if ( $self->{'form_is_finish'} ) {
464 if ( $self->{'generated_hash'} ne $self->{'load_hash'} ) {
465 $self->{'fatal_error'} = 3;
466 $self->{'last_error'} = '3 manipulate data';
467 $self->{'plugin'}->add_fatal_error( 'forms_plugin', $self->{'name'}, '3' );
468 $self->{'plugin'}->add_last_error( 'forms_plugin', $self->{'name'}, 'manipulate data' );
469 return 0;
470 }
471 }
472  
473 return 1;
474 }
475  
476 sub load {
477 my $self = shift;
478  
479 use Digest::MD5 qw(md5_hex);
480 $self->{'cookie'} = $self->{'cookies'}->get( $self->{'name'} );
481  
482 my $pass;
483 my $time;
484 my $hash;
485 my %obfuscated;
486  
487 my @lines = split( /\n/, $self->{'cookie'} );
488 my $first_line = shift @lines;
489 ( $self->{'key_name'}, $self->{'key_value'} ) = split( '=', $first_line );
490  
491 foreach my $line (@lines) {
492  
493 my ( $var, $value ) = split( '=', $line );
494  
495 if ( $var eq $self->{'form_start_name'} . '_time' ) {
496 $time = $value;
497 next;
498 }
499 if ( $var eq $self->{'form_start_name'} . '_pass' ) {
500 $hash = $value;
501 next;
502 }
503 $obfuscated{$var} = $value;
504 $pass .= $self->{'query'}->{'q'}{$var};
505 }
506 $self->{'generated_hash'} = md5_hex($pass);
507 $self->{'load_hash'} = $hash;
508 $self->{'expired'} = ( ( $time + utl::expires_time( $self->{'expire'} ) ) < time );
509  
510 foreach my $key ( keys %obfuscated ) {
511 my $key_value = $self->{'query'}->get($key);
512 my $field = $obfuscated{$key};
513 $self->{'query'}->set( $field, $key_value );
514 }
515  
516 $self->{'top_container'}->init_nes_env();
517  
518 return;
519 }
520  
521 sub save {
522 my $self = shift;
523  
524 use Digest::MD5 qw(md5_hex);
525 my $pass;
526 my %obfuscated = %{ $self->{'obfuscated'} };
527 $self->{'data'} = '';
528 $self->{'data'} .= $self->{'key_name'} . '=' . $self->{'key_value'};
529 $self->{'data'} .= "\n";
530  
531 foreach my $key ( keys %obfuscated ) {
532 $self->{'data'} .= $obfuscated{$key} . '=' . $key . "\n";
533 $pass .= $self->{'query'}->{'q'}{$key};
534 }
535 $self->{'save_hash'} = md5_hex($pass);
536  
537 # controla la expiración de la cookie
538 $self->{'data'} .= $self->{'form_start_name'} . '_time' . '=' . time;
539 $self->{'data'} .= "\n";
540 $self->{'data'} .= $self->{'form_start_name'} . '_pass' . '=' . $self->{'save_hash'};
541  
542 $self->{'cookies'}->create( $self->{'name'}, $self->{'data'}, $self->{'expire'} );
543  
544 }
545  
546 }
547  
548 # don't forget to return a true value from the file
549 1;
550