#!/usr/bin/perl

$frag = "/www/cgi-bin/mailform.txt";

$domain = $ENV{'SERVER_NAME'};
$root_dir = $ENV{'SERVER_NAME'};
$root_dir =~ s/\.com|\.net|\.org//i;
$root_dir =~ s/www.//i;
$docroot = $root_dir;
$root_dir = "/www/htdocs/".$root_dir;

$uid = (stat($root_dir))[4];
$owner = (getpwuid($uid))[0];

$header = "<HTML><HEAD><TITLE>Planetmind Form Processing Script</TITLE>
</HEAD>
<BODY background=http://planetmind.net/images/hunabku_bkg3.gif>
<IMG SRC=http://planetmind.net/images/planetwomanlogo.gif WIDTH=212 HEIGHT=54><p>";

# Parse Form Contents

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);

   # Un-Webify plus signs and %-encoding
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/<!--(.|\n)*-->//g;

   $FORM{$name} = $value;
}

$recipient = $FORM{"to_email"};
($scriptname,$domain) = split(/\@/,$FORM{'to_email'});
$scriptname = "formmail_".$scriptname.".cgi";


# branch...

if (!$FORM{'submit'}) {

 &return_form;
 exit;
}



# open file

open(FILE, "$frag") || print "notopen $frag\n";
@fragment = <FILE>;
close(FILE);

# open new script to write

open(FILE, ">$scriptname") || print "no $scriptname\n";

print FILE qq!#\!/usr/bin/perl

\$mailprog = '/usr/lib/sendmail -i -t';
\@referers = ('206.168.82.69','planetmind.net','$domain');
\@valid_ENV = ('REMOTE_HOST','REMOTE_ADDR','REMOTE_USER','HTTP_USER_AGENT');

!;

print FILE '@recipients = (\'^'.$recipient.'\');
';


foreach $line(@fragment) {
  print FILE $line;
}
close(FILE);

$ex = `chmod 755 $scriptname`;


# return response

print "Content-type: text/html\n\n";
print $header;
print "<font size=+1>You new script has been installed. The complete form tag you should use in your script is:<P>\n";

print "<I><BLOCKQUOTE>&lt;FORM METHOD=\"POST\" ACTION=\"http://".$ENV{'SERVER_NAME'}."/".$docroot."cgi/$scriptname\"&gt;</BLOCKQUOTE></I></font><P>\n";
print "Form submissions will be emailled to <B>$FORM{'to_email'}</B>.<P>\n";
print "An example appears below. More info on using the script and creating the form can be found <A HREF=\"http://www.planetmind.net/form_instr.html\">HERE</A>.<p>\n";
print "<P>[<A HREF=\"/admin/index.html\">back to Admin Page</A>]<P><HR><P>\n";
print qq! <h3>Form Example</h3>
You can change anything in the <I>NAME=</I> to suit your needs. If you use the "redirect" hidden field, you have to create the thanks.html page. Just copy and paste it into your page.<P>
<hr><BR><PRE>
&lt;FORM METHOD="POST" ACTION="http://$ENV{'SERVER_NAME'}/$docroot!;
print qq!cgi/$scriptname"&gt;<P>
Name: &lt;INPUT NAME="Name" TYPE="text" SIZE=32,1&gt;&lt;BR&gt;
Address: &lt;INPUT NAME="Address" TYPE="text" SIZE=32,1&gt;&lt;BR&gt;
City: &lt;INPUT NAME="City" TYPE="text" SIZE=32,1&gt;&lt;BR&gt;
State: &lt;INPUT NAME="Address" TYPE="text" SIZE=2,1&gt; Zip: &lt;INPUT NAME="Zip" TYPE="text" SIZE=12,1&gt;&lt;BR&gt;
Email: &lt;INPUT NAME="email" TYPE="text" SIZE=32,1&gt;&lt;BR&gt;
Tell us about yourself:  &lt;BR&gt;
&lt;TEXTAREA NAME="Comments" ROWS=4 COLS=60 WRAP=soft&gt;&lt;/TEXTAREA&gt;

&lt;INPUT NAME="redirect" TYPE="hidden" VALUE="http://yourdomain.com/thanks.html"&gt;
&lt;INPUT NAME="recipient" TYPE="hidden" VALUE="$recipient"&gt;
&lt;INPUT NAME="subject" TYPE="hidden" VALUE="Form Submission"&gt;
<P>
&lt;INPUT NAME="submit" TYPE="submit" VALUE="Submit"&gt;
&lt;/form&gt;
</pre>
<br><hr>
!;
print "</body></html>";

exit;


sub return_form {
print "Content-type: text/html\n\n";
print qq!
<HTML>
<HEAD>
<TITLE>Planetmind Form Processing Script</TITLE>
</HEAD><BODY BGCOLOR="#91B58C" background="images/hunabku_bkg3.gif">

<H1>Planetmind Form Processing Script Installer</H1>
This form will install a form processing script that will send an email with the submitted form's contents to 
the email address you specify. <P>


There is a <A HREF="http://planetmind.net/form_instr.html">list of form fields</A> you can 
use and how to implement them. 

<P>
Some of the possible uses of this script are:
<P>
1) You want to have a form that will be mailed to you, but aren't sure how to 
   write the CGI script for it.<BR>
2) You are the webmaster of your site and want to allow users to use forms, 
   but not to have their own cgi-bin directories, which can cause 
   security risks to your system.  You can set this script up and then 
   allow all users to run off of it.
3) Want to have one script to parse all of your html forms and mail them 
   to you.<P>

<HR><P>
<FORM ACTION="formmail_maker.cgi" METHOD="POST">
Enter the email address you want the subitted form's data to go to: <INPUT NAME="to_email" TYPE="text" SIZE=48,1><BR>
<INPUT NAME="submit" TYPE="submit" VALUE="Create Script"> 

</FORM>

<BR><BR><BR><BR><HR><P>

</body></html>
!;


}
