Perl - Form handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    Perl - Form handler

    Dear all,

    It been more than 3 days I am trying to debug this program, I
    interpret it using activePerl and it is giving (perl -wc
    code_process.pl ) no error syntax but when I put it online, change to
    the appropriate mode and test the html form it gave me an 500 internel
    server error.

    I really need it to be done soon, I would not post here before I test
    it as much as I can,

    In case you would like to check the html too, please let me know.

    regards,
    John

    #!/usr/local/bin/perl

    $forward_url_en g = 'www.anything.c om/eng/formsent.html';
    $forward_url_fr a = 'www.anything.c om/fra/formsentf.html' ;

    $htmlbody="<bod y bgcolor=\"#F2F6 E9\" text=\"#000000\ " link=\"#8B2833\ "
    vlink=\"#000000 \" alink=\"#FF0000 \" leftmargin=\"0\ " topmargin=\"0\"
    rightmargin=\"0 \" bottommargin=\" 0\" marginwidth=\"0 \"
    marginheight=\" 0\">";

    # Parse Form Contents
    &parse_form;

    # Generate ID
    &generate_id ;

    # Return HTML Page
    &return_html ;

    # Send E-Mail
    &send_mail;

    #============== =============== === Parse Form
    #============== =============== ========

    sub parse_form {

    # Get the input
    read(STDIN, $buffer, $ENV{'CONTENT_L ENGTH'});
    # Split the name-value pairs
    @pairs = split(/&/, $buffer);

    foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/<!--(.|\n)*-->//g;

    if ($name eq 'required') {@required = split(/,/,$value);}
    elsif ($name eq 'env_report') {@env_report = split(/,/,$value);}
    else {
    if ($FORM{$name} && ($value)) {$FORM{$name} = "$FORM{$nam e},
    $value";
    }
    elsif ($value) {$FORM{$name} = $value;
    }
    }
    }
    }

    #============== =============== === Generate ID
    =============== =============== =======

    sub generate_id
    {
    $lower=100;
    $upper=999;
    $random = int(rand( $upper-$lower+1 ) ) + $lower;
    @f = (localtime)[3..5]; # grabs day/month/year values
    $DD = ($f[1] + 1);
    if ($DD < 10) {$DD = 0 . $DD};
    $MM = $f[0];
    if ($MM < 10) {$MM = 0 . $MM};
    $ID = $DD . "-" . $MM . "-" . substr(($f[2] + 1900),2,2) . "-" .
    $random;
    }



    #============== =============== === Return HTML
    =============== =============== =======

    sub return_html {
    print "Content-type: text/html\n\n";
    print "<HTML>";
    print "<HEAD>";
    if ($FORM{'formlan g'} eq 'English') {print "<META
    HTTP-EQUIV=\"Refresh \"
    CONTENT=\"0; URL=$forward_ur l_eng\">\n";}
    elsif ($FORM{'formlan g'} eq 'French') {print "<META
    HTTP-EQUIV=\"Refresh \"
    CONTENT=\"0; URL=$forward_ur l_fra\">\n";}

    print "</HEAD>";
    print "$htmlbody\ n ";
    print "</body>\n</html>";
    print "</HTML>";
    }


    #============== =============== =========== Send mail
    #============== =============== =============== ===

    sub send_mail {

    # ==== Next three lines for Unix
    $mail_path = "/usr/sbin";
    $mail_prog = "sendmail";
    $mail_temp_path = "/tmp";


    # ====== Send mail to ILS ======

    local($mail_fil e);
    do {$mail_file=int (rand(99999999) ) . ".txt";} until !(-e
    $mail_file);

    $mail_command = "$mail_path/$mail_prog -F \"$FORM{'email' }\"
    $FORM{'recipien t'}
    < $mail_temp_path/$mail_file";
    open (MAIL,">$mail_t emp_path/$mail_file") || die "CANT OPEN
    $mail_temp_path/$mail_file\n";

    $mail_command = "$mail_path$mai l_prog -CGI -s\"Applicatio n Form
    ($ID)\"
    -f$FORM{'email'} < $mail_temp_path \\$mail_file";
    open (MAIL,">$mail_t emp_path/$mail_file") || die "CANT OPEN
    $mail_temp_path/$mail_file\n";

    &mail_to_ILS ; # Compose mail to be sent to ILS

    close (MAIL);
    system ("$mail_command ");
    unlink($mail_fi le);

    #====== Send mail to Applicant ======

    local($mail_fil e);
    do {$mail_file=int (rand(99999999) ) . ".txt";} until !(-e
    $mail_file);
    open (MAIL,">$mail_t emp_path/$mail_file") || die "CANT OPEN
    $mail_temp_path/$mail_file\n";


    if ($FORM{'formlan g'} eq 'English')
    {
    $mail_command = "$mail_path/$mail_prog -F \"$FORM{'recipi ent'}\"
    $FORM{'email'}
    < $mail_temp_path/$mail_file";
    # $mail_command = "$mail_path$mai l_prog -CGI -s\"
    Application Form\" -f$FORM{'recipie nt'} $FORM{'email'} <
    $mail_temp_path \\$mail_file";
    &mail_to_applic ant_eng; # Compose mail to be sent to applicant
    (English)
    }

    elsif ($FORM{'formlan g'} eq 'French')
    {
    $mail_command = "$mail_path/$mail_prog -F \"$FORM{'recipi ent'}\"
    $FORM{'email'}
    < $mail_temp_path/$mail_file";
    # $mail_command = "$mail_path$mai l_prog -CGI -s\"Inscriptio n \"
    -f$FORM{'recipie nt'} $FORM{'email'} < $mail_temp_path \\$mail_file";
    &mail_to_applic ant_fra; # Compose mail to be sent to applicant
    (French)
    }

    close (MAIL);
    system ("$mail_command ");
    unlink($mail_fi le);
    }

    #============== =============== =========== Mail to ILS
    #============== =============== =============== ===

    sub mail_to_ILS {
    print MAIL "Reply-To: $FORM{'email'}\ n";
    print MAIL "Subject: Application Form ($ID)\n";
    print MAIL "From: $FORM{'email'}\ n";
    print MAIL "Applicatio n Form, ID: ($ID)\n";
    print MAIL "--------------------------------------------\n";
    print MAIL "Language: $FORM{'formlang '}\n";
    print MAIL "--------------------------------------------------------------\n";
    print MAIL "1. Personal Information:\n" ;
    print MAIL " Family name(s): $FORM{'lname'}\ n";
    print MAIL " Given name(s): $FORM{'fname'}\ n";
    print MAIL " Employee Number: $FORM{'num_empl oye'}\n";
    print MAIL " Other Contact Information:\n" ;
    if ($FORM{'phoneho me'} ne '') {print MAIL " Telephone numbers
    (Home):
    $FORM{'phonehom e'}\n";};
    if ($FORM{'phonewo rk'} ne '') {print MAIL " Telephone numbers
    (Work):
    $FORM{'phonewor k'}\n";};
    print MAIL " E-mail: $FORM{'email'}\ n";
    print MAIL "--------------------------------------------------------------\n";
    print MAIL "2. I am a member of:\n";
    print MAIL " The faculty of : $FORM{'faculte' }\n";
    if ($FORM{'faculte '} ne '') {print MAIL " $FORM{'faculte' }\n";};
    print MAIL " Departement: $FORM{'departem ent'}\n";
    if ($FORM{'departe ment'} ne '') {print MAIL "
    $FORM{'departem ent'}\n";};
    print MAIL " Unit: $FORM{'unite'}\ n";
    if ($FORM{'unit'} ne '') {print MAIL " $FORM{'unite'}\ n";};
    print MAIL " I am a : $FORM{'job'}\n" ;
    print MAIL " My status is : $FORM{'status'} \n";
    #if ($FORM{'statut-autre'} ne '') {print MAIL " Year:
    $FORM{'statut-autre'}\n";};
    print MAIL "--------------------------------------------------------------\n";
    print MAIL "3. I wish to enroll in the following course(s) and
    session(s):\n";

    if ($FORM{'a1'} ne '') {print MAIL " $FORM{'a1'}\n"; };
    if ($FORM{'a2'} ne '') {print MAIL " $FORM{'a2'}\n"; };
    if ($FORM{'a3'} ne '') {print MAIL " $FORM{'a3'}\n"; };
    if ($FORM{'a4'} ne '') {print MAIL " $FORM{'a4'}\n"; };
    if ($FORM{'a5'} ne '') {print MAIL " $FORM{'a5'}\n"; };
    if ($FORM{'a6'} ne '') {print MAIL " $FORM{'a6'}\n"; };

    if ($FORM{'h1'} ne '') {print MAIL " $FORM{'h1'}\n"; };
    if ($FORM{'h2'} ne '') {print MAIL " $FORM{'h2'}\n"; };
    if ($FORM{'h3'} ne '') {print MAIL " $FORM{'h3'}\n"; };
    if ($FORM{'h4'} ne '') {print MAIL " $FORM{'h4'}\n"; };
    if ($FORM{'h5'} ne '') {print MAIL " $FORM{'h5'}\n"; };

    if ($FORM{'i1'} ne '') {print MAIL " $FORM{'i1'}\n"; };
    if ($FORM{'i2'} ne '') {print MAIL " $FORM{'i2'}\n"; };
    if ($FORM{'i3'} ne '') {print MAIL " $FORM{'i3'}\n"; };

    print MAIL "--------------------------------------------------------------\n";
    print MAIL "4. How do you intend to be classified?\n";
    print MAIL " Answer: $FORM{'test'}\n ";
    #if ($FORM{'date_te st'} ne '') {print MAIL " Year:
    $FORM{'date_tes t'}\n";};
    #if ($FORM{'resulta t_test'} ne '') {print MAIL " Year:
    $FORM{'resultat _test'}\n";};
    print MAIL "--------------------------------------------------------------\n";
    }

    #============== =============== =========== Mail to Applicant (English)
    #============== =============== =============== ===
    sub mail_to_applica nt_eng {
    print MAIL "Reply-To: $FORM{'recipien t'}\n";
    print MAIL "Subject: Application Form\n";
    print MAIL "From: $FORM{'recipien t'}\n";
    print MAIL "To: $FORM{'email'}\ n";
    print MAIL "Dear $FORM{'fname'} $FORM{'lname'}, \n\n";
    print MAIL "Thank you for choosing the
    Institute.\n\n" ;
    print MAIL "Your reference number is \"$ID\". Please use this number
    when you write
    to us ";
    print MAIL "You indicated your intention to enroll in the following
    course(s) and session(s):\n";

    if ($FORM{'a1'} ne '') {print MAIL " $FORM{'a1'}\n"; };
    if ($FORM{'a2'} ne '') {print MAIL " $FORM{'a2'}\n"; };
    if ($FORM{'a3'} ne '') {print MAIL " $FORM{'a3'}\n"; };
    if ($FORM{'a4'} ne '') {print MAIL " $FORM{'a4'}\n"; };
    if ($FORM{'a5'} ne '') {print MAIL " $FORM{'a5'}\n"; };
    if ($FORM{'a6'} ne '') {print MAIL " $FORM{'a6'}\n"; };

    if ($FORM{'h1'} ne '') {print MAIL " $FORM{'h1'}\n"; };
    if ($FORM{'h2'} ne '') {print MAIL " $FORM{'h2'}\n"; };
    if ($FORM{'h3'} ne '') {print MAIL " $FORM{'h3'}\n"; };
    if ($FORM{'h4'} ne '') {print MAIL " $FORM{'h4'}\n"; };
    if ($FORM{'h5'} ne '') {print MAIL " $FORM{'h5'}\n"; };

    if ($FORM{'i1'} ne '') {print MAIL " $FORM{'i1'}\n"; };
    if ($FORM{'i2'} ne '') {print MAIL " $FORM{'i2'}\n"; };
    if ($FORM{'i3'} ne '') {print MAIL " $FORM{'i3'}\n"; };

    print MAIL "We look forward to your arrival and wish you an enjoyable
    and rewarding
    experience .\n\n";
    print MAIL "We invite you to read our Privacy Policy at: ";

    print MAIL "We would be pleased to answer any questions you
    have.\n\n";

    }

    #============== =============== =========== Mail to Applicant (French)
    #============== =============== =============== ===
    sub mail_to_applica nt_fra {
    print MAIL "Reply-To: $FORM{'recipien t'}\n";
    print MAIL "Subject: Inscription\n";
    print MAIL "From: $FORM{'recipien t'}\n";
    print MAIL "To: $FORM{'email'}\ n";
    print MAIL "Bonjour $FORM{'fname'} $FORM{'lname'}, \n\n";
    print MAIL "Nous vous remercions d'avoir choisi
    ..\n\n";
    print MAIL "Le numéro de référence qui vous a été attribué est le
    \"$ID\". Nous vous
    serions reconnaissants de ";
    print MAIL "bien vouloir rappeler ce numéro sur toute
    correspondance. \n\n";
    print MAIL "Vous avez indiqué votre intention de poursuivre dans les
    cours suivants:\n";

    if ($FORM{'a1'} ne '') {print MAIL " $FORM{'a1'}\n"; };
    if ($FORM{'a2'} ne '') {print MAIL " $FORM{'a2'}\n"; };
    if ($FORM{'a3'} ne '') {print MAIL " $FORM{'a3'}\n"; };
    if ($FORM{'a4'} ne '') {print MAIL " $FORM{'a4'}\n"; };
    if ($FORM{'a5'} ne '') {print MAIL " $FORM{'a5'}\n"; };
    if ($FORM{'a6'} ne '') {print MAIL " $FORM{'a6'}\n"; };

    if ($FORM{'h1'} ne '') {print MAIL " $FORM{'h1'}\n"; };
    if ($FORM{'h2'} ne '') {print MAIL " $FORM{'h2'}\n"; };
    if ($FORM{'h3'} ne '') {print MAIL " $FORM{'h3'}\n"; };
    if ($FORM{'h4'} ne '') {print MAIL " $FORM{'h4'}\n"; };
    if ($FORM{'h5'} ne '') {print MAIL " $FORM{'h5'}\n"; };

    if ($FORM{'i1'} ne '') {print MAIL " $FORM{'i1'}\n"; };
    if ($FORM{'i2'} ne '') {print MAIL " $FORM{'i2'}\n"; };
    if ($FORM{'i3'} ne '') {print MAIL " $FORM{'i3'}\n"; };

    print MAIL "Nous nous réjouissons de bientôt pouvoir vous accueillir
    et nous
    espérons que votre séjour parmi nous ";
    print MAIL "sera à la fois une expérience agréable et
    enrichissante.\ n\n";
    print MAIL "Nous vous invitons à prendre connaissance de notre
    Politique de
    protection de la confidentialité à : ";
    print MAIL "merci\n\n" ;
    print MAIL "Nous nous ferons un plaisir de répondre à toute question
    que vous
    pourriez vouloir nous poser.\n\n";
    print MAIL "Nous vous prions d'agréer nos salutations
    distinguées.\n\ n";

    }
  • Gunnar Hjalmarsson

    #2
    Re: Perl - Form handler

    John wrote:[color=blue]
    > It been more than 3 days I am trying to debug this program, I
    > interpret it using activePerl and it is giving (perl -wc
    > code_process.pl ) no error syntax but when I put it online, change
    > to the appropriate mode and test the html form it gave me an 500
    > internel server error.[/color]

    What does the server's error log tell you? If you can't access it, you
    can add this line in the beginning of the script so as to have fatal
    errors printed to the screen:

    use CGI::Carp 'fatalsToBrowse r';
    [color=blue]
    > In case you would like to check the html too, please let me know.[/color]

    To be honest, I don't want to dig into the details in that program,
    since it's old and not very good according to today's standards.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    Comment

    • Jim Gibson

      #3
      Re: Perl - Form handler

      In article <46532e21.04080 60520.1089c958@ posting.google. com>, John
      <johngalil@hotm ail.com> wrote:
      [color=blue]
      > Dear all,
      >
      > It been more than 3 days I am trying to debug this program, I
      > interpret it using activePerl and it is giving (perl -wc
      > code_process.pl ) no error syntax but when I put it online, change to
      > the appropriate mode and test the html form it gave me an 500 internel
      > server error.
      >
      > I really need it to be done soon, I would not post here before I test
      > it as much as I can,
      >
      > In case you would like to check the html too, please let me know.[/color]

      I can give you the standard advice for these types of problems:

      1. Run the CGI on the command-line (but you have already done this).
      2. Look at the server error logs; what do they say?
      3. Use the CGI module; don't try to parse form input yourself.
      4. Put "use CGI::Carp 'fatalsToBrowse r';" in your program (after doing
      3)
      5. Minimize your program to attempt to isolate the problem and post
      that minimal program, what it is doing, what the server logs say, if
      anything, and what you expect it to be doing that it is not.

      FYI: this newsgroup is defunct; try comp.lang.perl. misc in the future.

      Comment

      • John

        #4
        Re: Perl - Form handler

        thank you Gunnar Hjalmarsson, I will keep searching to solve the bug.

        regards,
        John.


        Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message news:<QbMQc.732 8$dx3.43370@new sb.telia.net>.. .[color=blue]
        > John wrote:[color=green]
        > > It been more than 3 days I am trying to debug this program, I
        > > interpret it using activePerl and it is giving (perl -wc
        > > code_process.pl ) no error syntax but when I put it online, change
        > > to the appropriate mode and test the html form it gave me an 500
        > > internel server error.[/color]
        >
        > What does the server's error log tell you? If you can't access it, you
        > can add this line in the beginning of the script so as to have fatal
        > errors printed to the screen:
        >
        > use CGI::Carp 'fatalsToBrowse r';
        >[color=green]
        > > In case you would like to check the html too, please let me know.[/color]
        >
        > To be honest, I don't want to dig into the details in that program,
        > since it's old and not very good according to today's standards.[/color]

        Comment

        Working...