| 1 | #!/usr/bin/perl -w | ||
| 2 | # | ||
| 3 | # | ||
| 4 | # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org> | ||
| 5 | # Copyright (c) 2000-2002 Mitel Networks Corporation | ||
| 6 | # This software is distributed under the same licenses as Perl itself; | ||
| 7 | # see the file COPYING for details. | ||
| 8 | |||
| 9 | # | ||
| 10 | # $Id: Events.pm,v 1.13 2003/02/05 17:18:34 anoncvs_gtkglext Exp $ | ||
| 11 | # | ||
| 12 | |||
| 13 | package CGI::FormMagick; | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use Carp; | ||
| 17 | |||
| 18 | =pod | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | CGI::FormMagick::Events -- pre/post form/page event handlers | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | use CGI::FormMagick; | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | =head2 $fm->form_pre_event() | ||
| 31 | |||
| 32 | performs the PRE-EVENT (if any) for the form. Usually used to do | ||
| 33 | setup for the application. | ||
| 34 | |||
| 35 | this is the routine where we call some routine that will | ||
| 36 | give us default data for the form, or otherwise | ||
| 37 | do things that need doing before the form is submitted. | ||
| 38 | |||
| 39 | =for testing | ||
| 40 | TODO: { | ||
| 41 | local $TODO = "Write tests for Event.pm!"; | ||
| 42 | ok(0, "nothing here yet"); | ||
| 43 | } | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub form_pre_event { | ||
| 48 | my ($self) = @_; | ||
| 49 | |||
| 50 | $self->debug_msg("This is the form pre event"); | ||
| 51 | |||
| 52 | # find out what the form pre_event action is. | ||
| 53 | my $pre_form_routine = $self->{xml}->{'pre-event'} || return; | ||
| 54 | |||
| 55 | $self->do_external_routine($pre_form_routine); | ||
| 56 | } | ||
| 57 | |||
| 58 | =pod | ||
| 59 | |||
| 60 | =head2 $fm->form_post_event() | ||
| 61 | |||
| 62 | performs validation and runs the POST-EVENT (if any) otherwise just | ||
| 63 | prints out the data that the user input | ||
| 64 | |||
| 65 | Note: we need to validate EVERY ONE of the form inputs to make | ||
| 66 | sure malicious attacks don't happen. See also "SECURITY | ||
| 67 | CONSIDERATIONS" in the perldoc for how to get around this :-/ | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub form_post_event { | ||
| 72 | my ($self) = @_; | ||
| 73 | $self->debug_msg("This is the form post event"); | ||
| 74 | if ($self->errors()) { | ||
| 75 | $self->debug_msg("Looks like we've got some errors"); | ||
| 76 | #print "<h2>", localise("Validation errors"), "</h2>\n"; | ||
| 77 | #print "<p>", localise("These validation errors are probably evidence of an attempt to circumvent the data validation on this application. Please start over again."), "</p>"; | ||
| 78 | $self->list_error_messages(); | ||
| 79 | } else { | ||
| 80 | $self->debug_msg("Validation successful."); | ||
| 81 | |||
| 82 | # find out what the form post_event action is. | ||
| 83 | my $post_form_routine = $self->{xml}->{'post-event'}; | ||
| 84 | |||
| 85 | unless ($self->do_external_routine($post_form_routine)) { | ||
| 86 | |||
| 87 | # default form post-event -- print out user data | ||
| 88 | print "<p>", localise("The following data was submitted"), "</p>\n"; | ||
| 89 | |||
| 90 | print "<ul>\n"; | ||
| 91 | my @params = $self->{cgi}->param; | ||
| 92 | foreach my $param (@params) { | ||
| 93 | my $value = $self->{cgi}->param($param); | ||
| 94 | print "<li>$param: $value\n"; | ||
| 95 | } | ||
| 96 | print "</ul>\n"; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | =pod | ||
| 102 | |||
| 103 | =head2 $fm->page_pre_event() | ||
| 104 | |||
| 105 | Performs the PAGE PRE-EVENT (if any). | ||
| 106 | |||
| 107 | XXX NEEDS TESTS | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub page_pre_event { | ||
| 112 | my ($self) = @_; | ||
| 113 | $self->debug_msg("This is the page pre-event."); | ||
| 114 | if (my $pre_page_routine = $self->page->{'pre-event'}) { | ||
| 115 | $self->debug_msg("The pre-routine is $pre_page_routine"); | ||
| 116 | $self->do_external_routine($pre_page_routine); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | =pod | ||
| 121 | |||
| 122 | =head2 $fm->page_post_event() | ||
| 123 | |||
| 124 | Performs the PAGE POST-EVENT (if any). | ||
| 125 | |||
| 126 | XXX NEEDS TESTS | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub page_post_event { | ||
| 131 | my ($self) = @_; | ||
| 132 | $self->debug_msg("This is the page post-event."); | ||
| 133 | if (my $post_page_routine = $self->page->{'post-event'}) { | ||
| 134 | $self->debug_msg("The post-routine is $post_page_routine"); | ||
| 135 | $self->do_external_routine($post_page_routine); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | return "FALSE"; # true value | ||
| 141 | |||
| 142 | =pod | ||
| 143 | |||
| 144 | =head1 SEE ALSO | ||
| 145 | |||
| 146 | CGI::FormMagick | ||
| 147 | |||
| 148 | =cut |