| <?php |
| |
| /******************************/ |
| /* Portable Contact Form */ |
| /* -------------------------- */ |
| /* By: Nick George */ |
| /* Created: 12/30/2008 */ |
| /* Last Updated: 08/14/2009 */ |
| /* -------------------------- */ |
| /******************************/ |
| |
| // Requirements: |
| // ********************* |
| // In order to use this script, |
| // you must be able to use: |
| // 1. PHP GD Image Functions |
| // 2. Be able to use sessions |
| |
| class ContactForm |
| { |
| private $text = ""; |
| private $emailaddress = ""; // email address to send the form to |
| private $emailsubject = ""; // subject of the email |
| private $person_name = ""; // Name of the person sending the form - this will be automatically filled out |
| private $person_email = ""; // Email of the person sending the form - this will be automatically filled out |
| private $successmessage = ""; // message shown when the email/form has been successfully sent/submitted |
| private $verify = false; // Turn the verification image on or off. Default: off |
| private $display_type = 1; // display type: |
| // 1 => Long - one row per input |
| // Ex: Name: [_________] |
| // E-Mail: [_________] |
| // |
| // 2 => Narrow - Name above input |
| // Ex: Name: |
| // [__________] |
| // E-Mail: |
| // [__________] |
| private $style = ""; // style for the whole form element |
| private $verify_style = ""; // style for the verification image |
| private $inputs = array(); // [input_name] => [name, input_name type, validation_type, [..type specific options..], before-text, after-text] |
| // types: |
| // 1 => Text |
| // 2 => Textbox |
| // 3 => Drop Down List |
| // 4 => Submit Button |
| // validation types: |
| // 1 => regular text |
| // 2 => paragraph text |
| // 3 => name |
| // 4 => email |
| private $ddlOptions = array(); // Drop Down List Options |
| |
| // Variables: |
| // ------------------------- |
| // $email - String |
| // Description: Your E-Mail address. This is where the details of the form will be sent to |
| // ---> Usage: "me@mysite.com" |
| // |
| // $subject - String |
| // Description: The subject line of the e-mail being to you. This can help when trying to sort through your e-mails |
| // ---> Usage: "Contact Form Submission" |
| // |
| // $successmessage - String |
| // Description: The text that will be displayed after form is successfully completed |
| // ---> Usage: "Thank you for completing out form, we will get back to you shortly." |
| // |
| // $style - String |
| // ---> Usage: "myClass" |
| // ---> * Can be null (empty) |
| // |
| // $verify_image_style - String |
| // ---> Usage: "ValidateBox" |
| // ---> * Can be null (empty) |
| public function __construct($email, $subject, $successmessage, $display_type, $style = null, $verify_image_style = null) |
| { |
| // Must have this enabled in order to use validation image |
| session_start(); |
| |
| // Set variables |
| $this->emailaddress = $email; |
| $this->emailsubject = $subject; |
| $this->successmessage = $successmessage; |
| $this->display_type = $display_type; |
| $this->style = $style; |
| $this->verify_style = $verify_image_style; |
| } |
| |
| // Variables: |
| // ------------------------- |
| // $name - String |
| // Description: The name attached to input. |
| // ---> Usage: "Your Name" |
| // |
| // $input_name - String |
| // Description: The name of the input. |
| // ---> Usage: "txtName" |
| // ---> This would be the input for the name of the person submitting the form |
| // |
| // $type - Integer (single digit) |
| // Description: Validation requirements. Please select from the list below. |
| // 1 => Regular text (ie. description, phone number, or any other type of sentence-like text) |
| // 3 => Name (someone's name) |
| // 4 => E-Mail |
| // ---> Usage: 1 |
| // |
| // $attributes - String |
| // Description: Attributes you would like to add to the input tag |
| // ---> Usage: "style=\"border: 1px solid black;\"" |
| // ---> "onclick=\"javascript:doSomething()\" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| // ---> Note: Make sure your styling is correct and ended with semi-colons (;) |
| // |
| // $before_text - String |
| // Description - Any type of text or HTML code you want before the input |
| // ---> Usage: "<div id=\"myMarkup\">" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| // |
| // $after_text - String |
| // Description - Any type of text or HTML code you want after the input |
| // ---> Usage: "</div>" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| public function addTextInput($name, $input_name, $type, $attributes = "", $before_text = "", $after_text = "") |
| { |
| $this->inputs[$input_name] = array($name, 1, $type, $attributes, $before_text, $after_text); |
| } |
| |
| // Variables: |
| // ------------------------- |
| // $name - String |
| // Description: The name attached to input. |
| // ---> Usage: "Your Name" |
| // |
| // $input_name - String |
| // Description: The name of the input. |
| // ---> Usage: "txtName" |
| // ---> This would be the input for the name of the person submitting the form |
| // |
| // $cols - Integer |
| // Description: How many columns wide do you want the textbox to be. |
| // ---> Usage: 30 |
| // |
| // $rows - Integer |
| // Description: How many rows tall do you want the textbox to be. |
| // ---> Usage: 10 |
| // |
| // $attributes - String |
| // Description: Attributes you would like to add to the input tag |
| // ---> Usage: "style=\"border: 1px solid black;\"" |
| // ---> "onclick=\"javascript:doSomething()\" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| // ---> Note: Make sure your styling is correct and ended with semi-colons (;) |
| // |
| // $before_text - String |
| // Description - Any type of text or HTML code you want before the input |
| // ---> Usage: "<div id=\"myMarkup\">" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| // |
| // $after_text - String |
| // Description - Any type of text or HTML code you want after the input |
| // ---> Usage: "</div>" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| public function addTextBoxInput($name, $input_name, $cols, $rows, $attributes = "", $before_text = "", $after_text = "") |
| { |
| $this->inputs[$input_name] = array($name, 2, 2, $cols, $rows, $attributes, $before_text, $after_text); |
| } |
| |
| // Variables: |
| // ------------------------- |
| // $name - String |
| // Description: The name attached to input. |
| // ---> Usage: "Your Name" |
| // |
| // $input_name - String |
| // Description: The name of the input. |
| // ---> Usage: "ddlQuestion" |
| // ---> This would be the drop down list of submission types |
| // |
| // $attributes - String |
| // Description: Attributes you would like to add to the input tag |
| // ---> Usage: "style=\"border: 1px solid black;\"" |
| // ---> "onclick=\"javascript:doSomething()\" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| // ---> Note: Make sure your styling is correct and ended with semi-colons (;) |
| // |
| // $before_text - String |
| // Description - Any type of text or HTML code you want before the input |
| // ---> Usage: "<div id=\"myMarkup\">" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| // |
| // $after_text - String |
| // Description - Any type of text or HTML code you want after the input |
| // ---> Usage: "</div>" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| public function addDropDownList($name, $input_name, $attributes = "", $before_text = "", $after_text = "") |
| { |
| $this->inputs[$input_name] = array($name, 3, 1, $attributes, $before_text, $after_text); |
| } |
| |
| // Variables: |
| // ------------------------- |
| // $list_name - String |
| // Description: The name of the drop down list you would like to add options to. |
| // ---> Usage: "ddlQuestion" |
| // |
| // $option_text - String |
| // Description: The text you would like to appear for the option in the drop down list |
| // ---> Usage: "Technical Support" |
| // |
| // $option_value - String |
| // Description: The value of the text you would like to use. |
| // ---> Usage: "ts" |
| public function addDropDownListOption($list_name, $option_text, $option_value) |
| { |
| $this->ddlOptions[$list_name][] = array($option_text, $option_value); |
| } |
| |
| // Variables: |
| // ------------------------- |
| // $name - String |
| // Description: The name attached to input. |
| // ---> Usage: "Submit Form" |
| // |
| // $input_name - String |
| // Description: The name of the input. |
| // ---> Usage: "subForm" |
| // |
| // $attributes - String |
| // Description: Attributes you would like to add to the input tag |
| // ---> Usage: "style=\"border: 1px solid black;\"" |
| // ---> "onclick=\"javascript:doSomething()\" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| // ---> Note: Make sure your styling is correct and ended with semi-colons (;) |
| // |
| // $before_text - String |
| // Description - Any type of text or HTML code you want before the input |
| // ---> Usage: "<div id=\"myMarkup\">" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| // |
| // $after_text - String |
| // Description - Any type of text or HTML code you want after the input |
| // ---> Usage: "</div>" |
| // ---> Note: Quotations (") have to be escaped by a backslash in order to be used correctly without errors |
| public function addSubmitButton($name, $input_name, $attributes = "", $before_text = "", $after_text = "") |
| { |
| $this->inputs[$input_name] = array($name, 4, 1, $attributes, $before_text, $after_text); |
| } |
| |
| // Description: turns the verification image on |
| public function addVerifyImage() |
| { |
| $this->verify = true; |
| } |
| |
| public function showForm() |
| { |
| // Check to see if form has been submitted, if not, post the form |
| if(count($_POST) <= 0) |
| return $this->Form(); |
| // Form submitted, validate and send out email if everything checks out |
| else |
| { |
| $val = new Validation(); |
| $messages = array(); |
| $inputs = array(); // array for email inputs. This is a simple name:value pair array |
| |
| foreach($_POST as $name => $value) |
| { |
| // check to make sure key exists, if not, do not validate or include in email |
| if(array_key_exists($name, $this->inputs)) |
| { |
| // validate |
| switch($this->inputs[$name][2]) |
| { |
| // Regular Text |
| case 1: |
| if(!$val->Text(trim($value))) |
| $messages[] = $this->inputs[$name][0]." is invalid"; |
| break; |
| |
| // Paragraph Text |
| case 2: |
| if(!$val->TextBox(trim(stripslashes($value)))) |
| $messages[] = $this->inputs[$name][0]." is invalid"; |
| break; |
| |
| // Name |
| case 3: |
| if(!$val->Name(trim($value))) |
| $messages[] = $this->inputs[$name][0]." is invalid"; |
| else |
| $this->person_name = $value; |
| break; |
| |
| // E-Mail |
| case 4: |
| if(!$val->Email(trim($value))) |
| $messages[] = $this->inputs[$name][0]." is invalid"; |
| else |
| $this->person_email = $value; |
| break; |
| } |
| $inputs[$name] = trim(stripslashes($value)); |
| } |
| } |
| |
| if($this->verify) |
| { |
| if(strtolower(trim($_POST["txtImageVal"])) != strtolower($_SESSION["string_pass"])) |
| $messages[] = "Verification Image Invalid"; |
| } |
| |
| if(count($messages) > 0) |
| $this->Form($messages); |
| else |
| return ($this->sendMail($inputs)) ? $this->successmessage : "We could not complete the request at this time. Please try again later."; |
| } |
| |
| return $this->text; |
| } |
| |
| private function Form($errors = null) |
| { |
| if(is_array($errors)) |
| { |
| $this->text = "There were errors with your submission:<ol>"; |
| |
| foreach($errors as $i) |
| { |
| $this->text .= "<li>".$i."</li>"; |
| } |
| |
| $this->text .= "</ol>"; |
| } |
| |
| $submits = (String)""; // submit buttons text - will add after all other inputs and verification image |
| // Setup Table |
| $this->text .= "<form method=\"post\" action=\"\"> |
| <table class=\"".$this->style."\">"; |
| |
| // Iterate through each input and based on the type, output it to the page |
| foreach($this->inputs as $input_name => $input) |
| { |
| switch($input[1]) |
| { |
| // Text |
| case 1: |
| $this->text .= ($this->display_type === 1) ? |
| "<tr><td>".$input[0]."</td><td>".$input[4]."<input type=\"text\" name=\"".$input_name."\" value=\"".@$_POST[$input_name]."\" ".$input[3]." />".$input[5]."</td></tr>" : |
| "<tr><td>".$input[0]."</td></tr><tr><td>".$input[4]."<input type=\"text\" name=\"".$input_name."\" value=\"".@$_POST[$input_name]."\" ".$input[3]." />".$input[5]."</td></tr>"; |
| break; |
| |
| // Textbox |
| case 2: |
| $this->text .= ($this->display_type === 1) ? |
| "<tr><td>".$input[0]."</td><td>".$input[6]."<textarea name=\"".$input_name."\" cols=\"".$input[3]."\" rows=\"".$input[4]."\" ".$input[5]." />".@$_POST[$input_name]."</textarea>".$input[7]."</td></tr>" : |
| "<tr><td>".$input[0]."</td></tr><tr><td>".$input[6]."<textarea name=\"".$input_name."\" cols=\"".$input[3]."\" rows=\"".$input[4]."\" ".$input[5].">".@$_POST[$input_name]."</textarea>".$input[7]."</td></tr>"; |
| break; |
| |
| // Drop Down List |
| case 3: |
| if($this->display_type === 1) |
| { |
| $this->text .= "<tr><td>".$input[0]."</td><td>".$input[4]."<select name=\"".$input_name."\">"; |
| |
| foreach($this->ddlOptions[$input_name] as $option) |
| { |
| $this->text .= "<option value=\"".$option[1]."\""; |
| if(@$_POST[$input_name] == $option[1]) |
| $this->text .= " SELECTED"; |
| $this->text .= ">".$option[0]."</option>"; |
| } |
| $this->text .= "</select>".$input[5]."</td></tr>"; |
| } |
| else |
| { |
| $this->text .= "<tr><td>".$input[0]."</td></tr><tr><td>".$input[4]."<select name=\"".$input_name."\">"; |
| |
| foreach($this->ddlOptions[$input_name] as $option) |
| { |
| $this->text .= "<option value=\"".$option[1]."\""; |
| if(@$_POST[$input_name] == $option[1]) |
| $this->text .= " SELECTED"; |
| $this->text .= ">".$option[0]."</option>"; |
| } |
| $this->text .= "</select>".$input[5]."</td></tr>"; |
| } |
| break; |
| |
| // Submit button |
| case 4: |
| $submits .= ($this->display_type === 1) ? |
| "<tr><td colspan=\"2\">".$input[4]."<input type=\"submit\" name=\"".$input_name."\" value=\"".$input[0]."\" ".$input[3]." />".$input[5]."</td></tr>" : |
| "<tr><td>".$input[4]."<input type=\"submit\" name=\"".$input_name."\" value=\"".$input[0]."\" ".$input[3]." />".$input[5]."</td></tr>"; |
| break; |
| } |
| } |
| |
| // Add the verification image if it is set to active |
| if($this->verify) |
| $this->text .= ($this->display_type === 1) ? |
| "<tr><td><img src=\"VerifyImage.php\" class=\"".$this->verify_style."\"></td><td><input type=\"text\" name=\"txtImageVal\" /></td></tr>" : |
| "<tr><td><img src=\"VerifyImage.php\" class=\"".$this->verify_style."\"></td></tr><tr><td>Enter Text: <input type=\"text\" name=\"txtImageVal\" /></td></tr>"; |
| |
| // Add the submit button(s) and close the table and form |
| $this->text .= $submits."</table></form>"; |
| |
| return $this->text; |
| } |
| |
| private function sendMail($vars) |
| { |
| $headers = "From: ".$this->person_name." <".$this->person_email."> \r\n" . |
| "X-Mailer: PHP/". phpversion(); |
| |
| $body = "Someone sent you an e-mail from your site:\n\n"; |
| |
| foreach($vars as $name => $value) |
| { |
| if($this->inputs[$name][1] != 4) |
| $body .= $this->inputs[$name][0].": ".$value."\n\n"; |
| } |
| |
| return (mail($this->emailaddress, $this->emailsubject, $body, $headers)) ? true : false; |
| } |
| } |
| |
| class Validation |
| { |
| private function RegChecker($string, $regex, $EmptyValid) |
| { |
| if(strlen($string) <= 0) |
| return (!$EmptyValid) ? false : true; |
| |
| return (ereg($regex, $string)) ? true : false; |
| } |
| |
| public function Name($string, $EmptyValid = false) |
| { |
| $regex = "^[A-Za-z][A-Za-z0-9 ]*$"; |
| return self::RegChecker($string, $regex, $EmptyValid); |
| } |
| |
| public function Email($string, $EmptyValid = false) |
| { |
| $regex = "^([0-9a-zA-Z]([-._]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,4})$"; |
| return self::RegChecker($string, $regex, $EmptyValid); |
| } |
| |
| public function Text($string, $EmptyValid = false) |
| { |
| $regex = "^[a-zA-Z0-9][\'\,\.\!\/\(\)\#\? a-zA-Z0-9\%\&\*\+\$\#\-]+$"; |
| return self::RegChecker($string, $regex, $EmptyValid); |
| } |
| |
| public function TextBox($string, $EmptyValid = false) |
| { |
| $regex = "^[a-zA-Z0-9( );:\"',!\(\)\/\\#%&-_@\+\^\$\.\?\*\\\n\\\r]+$"; |
| return self::RegChecker($string, $regex, $EmptyValid); |
| } |
| } |
| |
| ?> |