<?php
/* $Id: formmail-php.php,v 1.0 2007-07-06 22:22:40-08 scoutt Exp $ */
// vim: expandtab sw=4 ts=4 sts=4:
########################################################################
##                                                                    ##
## ----------------------------------------------------------------   ##
## Copyright © Big Resources, Inc. All Rights Reserved.               ##
## This software must be used in accordance with its licensing        ##
## Built for use with templates from http://www.boxedart.com          ##
##                                                                    ##
## This copyright notice may not be removed.   				    ##
## ----------------------------------------------------------------   ##
########################################################################
session_start();

###########################################
## SETUP: First we need to edit a few varaibles
## so the script will work correctly.
##
## For any field that you want to be required,
## meaning you want them to have to fill it in,
## put a * in front of the name of the field
## example: <input type="text" name="*fname" value="" />
##
##
## FORM SETUP:
## Your form has to have 1 element in it and
## It has to be a hidden element. Please add
## this line to your form:
## <input type="hidden" name="send" value="1" />
##
## If you do not add it the script will not work
##
## Also it is best to name your form fileds where
## they make since. Like :
## <input type="text" name="*fname" value="" />
## 4<input type="text" name="*email" value="" />
## where fname symbolizes their first name and email
## is for there email.

###########################################
##
## Please change this to your email address
###########################################

$_email_to = "you@email.com";

###########################################
##
## Next we need to change this variable
## This controls line spacing, default is
## perfect (\r\n) but if you want more space
## between each row than add one more of 
## what you see below  eg: \r\n\r\n
##

$_lines_ = "\r\n";

############################################
##
## This sets the time between each email 
## the user is allow to send. Used for spam
## control. 1800 is 1 hour waiting time.
##
##

$_flood_time_ = 1800;

#############################################
##
## Change this varaible for the subject 
## of the email
##
##

$_subject_ = "Contact form";

##########################################
## This affects how the date is displayed,
## see http://www.php.net/date
## for more info

$today = gmdate ( "M d Y H:i:s" );

#############################################



## Main script below. Please do not edit unless you 
## change the language (text) only.
#############################################


function print_form($parray,$body) {
	global $_lines_;
        while (list($index, $sub) = each($parray)) {
            $index = ereg_replace("\*", "", $index);
			$index = ereg_replace("_", " ", $index);
            if (eregi('submit',$index)){ continue;}
            elseif (eregi('reset',$index)){ continue;}
            $body .= "$index:  ".htmlentities($sub)."$_lines_";
		}
		return $body;
}

function check_input(){
	global $flood;
    if (isset($_SESSION['FloodControl']) AND $_SESSION['FloodControl'] >= $flood){
       echo "Contact already sent";
       return false;
    }
    if(gettype($_POST)=="array") {
		while (list($index, $subarray) = each($_POST) ) {
			if(ereg("\*", $index) && (($subarray == "") || ($subarray == " "))) {
				$index = ereg_replace("\*", " ", $index);
				$index = ereg_replace("_", " ", $index);
				echo"There is a problem with your submission. The field <span style=\"color:red\">$index</span> is required, please press the back button on your browser.</div>";
				return false;

			} elseif(eregi("email", $index)){
				if(!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $subarray)){
					$index = ereg_replace("\*", " ", $index);
					echo"There is a problem with your submission. The E-mail address you provided, <span style=\"color:red\">$subarray</span>, does not appear to be valid, please press the back button on your browser and give a valid address.";
					return false;
				}
			}
		}
		return true;
	} else {
        return false;
    }
} 

$flood = time()-$_flood_time_;

if ((isset($_POST['send']) AND $_POST['send']== "1")){
    if (check_input()){
            $_SESSION['FloodControl'] = time();
            $info ="On $today, a visitor submitted '$_subject_' from the your website.{$_lines_}{$_lines_}";
            $contact_body = print_form($_POST,$info);
            $headers = "From: $_email_to<{$_email_to}>\r\n";
            // send e-mail
            mail($_email_to, $_subject_, stripslashes($contact_body), $headers);

            echo "Message sent successfully\n";
            exit;
    }

}



?>