#!/usr/bin/perl5

my $TITLE = "Send message";
my $BGCOLOR = "#FFFFFF";
my $TEXT_COLOR_NORMAL = "#000000";
my $REQUIRED_TEXT_COLOR = "#000000";
my $BACKGROUND_URL = "";
my $CGI_DIR = "/cgi-bin";
my $SENDMAIL = "/usr/sbin/sendmail";
## my $SENDMAIL = "/usr/lib/sendmail";
my $IMAGE_DIR = "http://research.cirp.org/images";
## my $CC_EMAIL = "harrisond\@telus.net";
my $CC_EMAIL = "";
my $MODULE = "feedbk1.cgi";

&get_form_data;
if (!$FORM_DATA{'mail'}) {
        &print_header;
        &print_html_form;
} else {
        $error = 0;
        &print_header;        
        &check_form_data;
        if (!$error) {
                &send_mail;
                &reply_to_user;
        }       
}
exit(0);

######## THE SUBROUTINES ########

########
#
#sub get_form_data
#
#       Gets the information submitted, decodes it, and puts it
#       into the $FORM_DATA associative array.
#
#########
sub get_form_data {
        
$request_method = $ENV{'REQUEST_METHOD'};
if ($request_method eq "GET") {
        $form_info = $ENV{'QUERY_STRING'};
} else {
        $size_of_form_information = $ENV{'CONTENT_LENGTH'};
        read (STDIN, $form_info, $size_of_form_information);
}
@key_value_pairs = split (/&/, $form_info);
foreach $key_value (@key_value_pairs) {
        ($key, $value) = split (/=/, $key_value);
        $value =~ tr/+/ /;
        $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
        if (defined($FORM_DATA{$key})) {
                $FORM_DATA{$key} = join (", ", $FORM_DATA{$key}, $value);
        } else {
                $FORM_DATA{$key} = $value;
        }
}

} #get_form_data

########
#
#sub print_header
#
#       Prints out HTML page headers. 
#
########
sub print_header {

print ("Content-type: text/html\n\n");

print <<__END_OF_HTML_CODE__;

\<?xml version=\"1.0\" encoding=\"iso-8859-1\"?\>

\<\!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"\>
\<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\"\>

\n<head>\n<title>$TITLE</title>

<link rel=\"StyleSheet\" href=\"http://research.cirp.org/style1.css\" type=\"text/css\" />

<SCRIPT LANGUAGE="JavaScript">
<!-- 

function getSysteminfo() {
var x = navigator;
document.mailForm.browserName.value = x.appName;
document.mailForm.browserVersion.value = x.appVersion;
document.mailForm.systemLanguage.value = x.systemLanguage;
document.mailForm.platform.value = navigator.platform;
document.mailForm.referrer.value = document.referrer;

var h = screen.height;
var w = screen.width;
var s = w + "x" + h;
document.mailForm.screenSize.value = s;
}


//-->
</SCRIPT>


</head>
<body onLoad="getSysteminfo();">
__END_OF_HTML_CODE__

}#print_header

########
#
#sub print_html_form
#
#       This subroutine prints out the HTML form the user fills in to
#       send an email message.
#
#########
sub print_html_form {


print <<__END_OF_HTML_CODE__;

<div class="container-main">

<form name="mailForm" action="/cgi-bin/feedbk1.cgi" method="post">
<INPUT TYPE="hidden" NAME="browserName" value="Unknown">
<INPUT TYPE="hidden" NAME="browserVersion" value="Unknown">
<INPUT TYPE="hidden" NAME="systemLanguage" value="Unknown">
<INPUT TYPE="hidden" NAME="screenSize" value="Unknown">
<INPUT TYPE="hidden" NAME="platform" value="Unknown">
<INPUT TYPE="hidden" NAME="referrer" value="Unknown">

<table border="0" cellpadding="0" cellspacing="0" class="table-main">
  <tr style="height: 0">
    <td style="width: 110px"></td>
    <td style="width:  30px"></td>
    <td style="width: 190px"></td>
    <td style="width: 170px"></td>
  </tr>
  <tr style="height: 1.5em">
    <td colspan="4" class="masthead1">Ridged Band</td>
  </tr>
  <tr style="height: 1.2em">
    <td colspan="4" class="masthead2">SPECIALIZED TISSUE OF THE PENIS</td>
  </tr>
  <tr style="height: 1.2em">
    <td colspan="4"></td>
  </tr>
  <tr>
    <td rowspan="3" valign="top">
<table border="0" cellpadding="0" cellspacing="2" class="table-nav-left">
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/index-e.html">Home</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/abstr1.html">Abstract</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/anat1.html">Anatomy</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/func1.html">Function</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/pix1.html">Pictures</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/faq1.html">FAQ</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/news1.html">Newsletter</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/cgi-bin/feedbk2.cgi">Questionnaire</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left-selected">Feedback</td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/links1.html">Links</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/cgi-bin/feedbk1-f.cgi">Français</a></td>
        </tr>
      </table>
    </td>
    <td></td>
    <td colspan="2" class="bodytext" valign="top">

<p style="font-weight: bold; margin: 0 0 1.5em 0">We welcome your comments or questions.</p> 

    </td> 
  </tr>

  <tr>
    <td></td>
    <td class="bodytext">
Your email address:<br /><input type="text" name="users_email" id="users_email" value="" />
    </td>
    <td class="bodytext">
Your name (optional):<br /><input type="text" name="users_name" />
    </td> 
  </tr>

  <tr>
    <td></td>
    <td colspan="2" class="bodytext">

<p style="margin: 1em 0 0 0">Please enter your comments below:</p>
<textarea name="message" rows="6" cols="40"></textarea>
<br /><input type="submit" value="   Send it   " style="margin: 0.5em 0 1em 0" /><input type="hidden" name="mail" value="1" />


    </td> 
  </tr>

  <tr style="height: 0.5em">
    <td colspan="4"></td>
  </tr>
  <tr class="hr-bottom">
    <td colspan="4"></td>
  </tr>
  <tr style="height: 0.5em">
    <td colspan="4"></td>
  </tr>
  <tr>
    <td colspan="4" style="text-align: left; padding-left: 0.5em"><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" style="height: 31px; width: 88px; border: none" /></a></td>
  </tr>
</table>
</form>
</div>

<script type="text/javascript">
<!--- Hide
document.form["mailForm"].users_email.focus();
// -->
</script>

</body>
</html>



__END_OF_HTML_CODE__

} #print_html_form

########
#
#sub check_form_data
#
#       This subroutine checks the information submitted via the
#       html form to make sure it has all the required information.
#
########
sub check_form_data {

$FORM_DATA{"users_email"} = &escape_input($FORM_DATA{"users_email"}) ;
$FORM_DATA{"users_name"} = &escape_input($FORM_DATA{"users_name"}) ;
$FORM_DATA{"message"} = &escape_input($FORM_DATA{"message"}) ;


#The array which holds all the error code numbers, and the error message
#associated with that code number.
my      %error_codes = (
                "1"     =>    "Email address is blank.",
                "2"     =>    "Email address must contain \@.",
                "3"     =>    "Comment field is blank.",
                "4"     =>    "Comment field must not contain urls.",
                "5"     =>    "Referring Web page is blank.",
                "6"     =>    "Unknown referring Web page.");

if ($FORM_DATA{'users_email'} !~ /\w/) {
        $error = 1;
} elsif ($FORM_DATA{'users_email'} !~ /@/) {
        $error = 2;
} elsif ($FORM_DATA{'message'} !~ /\w/) {
        $error = 3;
} elsif ($FORM_DATA{'message'} =~ /href/) {
        $error = 4;
} elsif ($FORM_DATA{'referrer'} !~ /\w/) {
        $error = 5;
} elsif ($FORM_DATA{'referrer'} =~ /Unknown/) {
        $error = 6;
}

if ($error) {

print <<__END_OF_HTML_CODE__;


<div class="container-main">
<table border="0" cellpadding="0" cellspacing="0" class="table-main">
  <tr style="height: 1.5em">
    <td colspan="4" class="masthead1">Ridged Band</td>
  </tr>
  <tr style="height: 1.2em">
    <td colspan="4" class="masthead2">SPECIALIZED TISSUE OF THE PENIS</td>
  </tr>
  <tr style="height: 2.5em">
    <td colspan="3"></td>
  </tr>
  <tr style="height: 1.2em">
    <td colspan="3" class="bodytext"><p style="margin-left: 1em">The form you submitted contained the following error:</p><p style="font-weight: bold; color: #ff0000; margin-left: 2em">$error_codes{$error}</p><p style="margin-left: 1em">Please click on your browser&#8217;s <span class="i">Back</span> button to correct your input.</p></td>
  </tr>
  <tr style="height: 2em">
    <td colspan="3"></td>
  </tr>
  <tr class="hr-bottom">
    <td colspan="3"></td>
  </tr>
  <tr style="height: 0.5em">
    <td colspan="3"></td>
  </tr>
  <tr>
    <td colspan="3" style="text-align: left; padding-left: 0.5em"><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" style="height: 31px; width: 88px; border: none" /></a></td>
  </tr>
</table>

</div>

</body>
</html>

__END_OF_HTML_CODE__

}

if ($FORM_DATA{'cc'} ne "") {
        $CC_EMAIL = $FORM_DATA{'cc'};
}

} #check_form_data

########
#
#sub send_mail
#
#       This subroutine writes and sends the email message.
#
########
sub send_mail {

open (MESSAGE, "| $SENDMAIL -t -oi ");
print MESSAGE <<__END_OF_MESSAGE__;
To: no_circ\@sbcglobal.net
From: research\@cirp.org
Subject: RB comments
Cc: harrisond\@telus.net

__END_OF_MESSAGE__
print MESSAGE ("Respondent: $FORM_DATA{'users_email'}\n");
print MESSAGE ("Name: $FORM_DATA{'users_name'}\n\n");
print MESSAGE ("Message:\n");
print MESSAGE ("$FORM_DATA{'message'}\n\n");
print MESSAGE ("###\n");
print MESSAGE ("SYSTEM INFORMATION\n");
print MESSAGE ("Browser: $FORM_DATA{'browserName'}\n");
print MESSAGE ("Version: $FORM_DATA{'browserVersion'}\n");
print MESSAGE ("Screen Resolution: $FORM_DATA{'screenSize'}\n");
print MESSAGE ("Operating System: $FORM_DATA{'platform'}\n");
print MESSAGE ("System Language: $FORM_DATA{'systemLanguage'}\n");
print MESSAGE ("Referrer: $FORM_DATA{'referrer'}\n");
close (MESSAGE);


} #send_mail

########
#
#sub reply_to_user
#
#       Provided everything submitted from the form checks out ok,
#       this subroutine returns an acknowledgement to the user.
#
########
sub reply_to_user {

print <<__END_OF_HTML_CODE__;

<div class="container-main">
<table border="0" cellpadding="0" cellspacing="0" class="table-main">
  <tr style="height: 0">
    <td width="110px"></td>
    <td width=" 50px"></td>
    <td width="440px"></td>
  </tr>
  <tr style="height: 1.5em">
    <td colspan="4" class="masthead1">Ridged Band</td>
  </tr>
  <tr style="height: 1.2em">
    <td colspan="4" class="masthead2">SPECIALIZED TISSUE OF THE PENIS</td>
  </tr>
  <tr style="height: 1.2em">
    <td colspan="3"></td>
  </tr>
  <tr>
    <td valign="top">
<table border="0" cellpadding="0" cellspacing="2" class="table-nav-left">
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/index-e.html">Home</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/abstr1.html">Abstract</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/anat1.html">Anatomy</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/func1.html">Function</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/pix1.html">Pictures</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/faq1.html">FAQ</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/news1.html">Newsletter</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/cgi-bin/feedbk2.cgi">Questionnaire</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left-selected">Feedback</td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/links1.html">Links</a></td>
        </tr>
        <tr style="height: 1em"> 
          <td class="nav-left"><a class="nav-left" href="http://research.cirp.org/cgi-bin/feedbk1-f.cgi">Français</a></td>
        </tr>
      </table>
    </td>
    <td></td>
    <td colspan="2" class="bodytext" valign="middle"><p class="b">Thank you...</p><p class="b">...your message has been sent.</p></td>
  </tr>
  <tr style="height: 0.5em">
    <td colspan="3"></td>
  </tr>
  <tr class="hr-bottom">
    <td colspan="3"></td>
  </tr>
  <tr style="height: 0.5em">
    <td colspan="3"></td>
  </tr>
  <tr>
    <td colspan="3" style="text-align: left; padding-left: 0.5em"><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" style="height: 31px; width: 88px; border: none" /></a></td>
  </tr>
</table>


__END_OF_HTML_CODE__


print ("</BODY>\n</HTML>\n");

} #reply_to_user


sub escape_input { 

local ($stringin) = @_;
$_ = $stringin;
s/&/&amp;/g;
s/</\&#60;/g;
s/>/\&#62;/g;
s/"/&#34;/g;
s/\\\\/&#92;/g;
s/\\\{/&#123;/g;
s/\\\}/&#125;/g;
s/\\,/&#44;/g;
s/\(/&#40;/g;
s/\)/&#41;/g;
s/\[/&#91;/g;
s/\]/&#93;/g;
s/\*/&#42;/g;
s/\+/&#43;/g;
s/\|/&#124;/g;
s/'/&#39;/g;
s/\r\n/\n/g;
s/\r/\n/g;
s/\n/ <BR>/g;

return $_; }


#FILE: $MODULE

