| <?php |
| |
| /******************************/ |
| /* Portable Contact Form */ |
| /* -------------------------- */ |
| /* By: Nick George */ |
| /* Last Updated: 12/30/2008 */ |
| /* -------------------------- */ |
| /******************************/ |
| |
| // 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 = ""; |
| private $emailsubject = ""; |
| private $successmessage = ""; |
| private $style = ""; |
| private $verify_style = ""; |
| |
| // 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) |
| // |
| // $types - Array |
| // ---> Usage: array("Artwork", "General", "etc") |
| |
| public function __construct($email, $subject, $types, $successmessage, $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->types = $types; |
| $this->style = $style; |
| $this->verify_style = $verify_image_style; |
| } |
| public function showForm() |
| { |
| if(empty($this->types)) |
| return "Need selection types."; |
| |
| if(!isset($_POST["subContact"])) |
| $this->Form(); |
| else |
| { |
| $errors = $this->validateInput($_POST["txtName"], $_POST["txtSubject"], $_POST["txtEmail"], $_POST["txtMessage"], $_POST["txtImageVal"]); |
| if(count($errors) > 0) |
| $this->Form($errors); |
| else |
| return ($this->sendMail($_POST["txtName"], $_POST["txtEmail"], $_POST["txtSubject"], $_POST["selType"], $_POST["txtMessage"])) ? $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>"; |
| } |
| |
| $this->text .= " |
| <form method=\"post\"> |
| <table class=\"".$this->style."\"> |
| <tr> |
| <td>Name:</td> |
| <td><input type=\"text\" name=\"txtName\" value=\"".@$_POST["txtName"]."\" /></td> |
| </tr> |
| <tr> |
| <td>Subject:</td> |
| <td><input type=\"text\" name=\"txtSubject\" value=\"".@$_POST["txtSubject"]."\" /></td> |
| </tr> |
| <tr> |
| <td>E-Mail:</td> |
| <td><input type=\"text\" name=\"txtEmail\" value=\"".@$_POST["txtEmail"]."\" /></td> |
| </tr> |
| <tr> |
| <td>Type of Question:</td> |
| <td> |
| <select name=\"selType\"> |
| "; |
| foreach($this->types as $type) |
| $this->text .= "<option value=\"".$type."\">".$type."</option>"; |
| |
| $this->text .= " |
| </select> |
| </td> |
| </tr> |
| <tr> |
| <td>Message:</td> |
| <td><textarea name=\"txtMessage\" cols=\"20\" rows=\"5\">".@$_POST["txtMessage"]."</textarea></td> |
| </tr> |
| <tr> |
| <td> |
| <img src=\"VerifyImage.php\" class=\"".$this->verify_image_style."\"> |
| </td> |
| <td> |
| <input type=\"text\" name=\"txtImageVal\" /> |
| </td> |
| </tr> |
| <tr> |
| <td colspan=\"2\" align=\"center\"> |
| Please type in the characters from the image into the box for verification! |
| </td> |
| </tr> |
| <tr> |
| <td colspan=\"2\" align=\"center\"> |
| <input type=\"submit\" name=\"subContact\" value=\"Submit\" /> |
| </td> |
| </tr> |
| </table> |
| </form> |
| "; |
| |
| return $this->text; |
| } |
| |
| private function validateInput($name, $subject, $email, $message, $validateimage) |
| { |
| $val = new Validation; |
| $messages = array(); |
| |
| // Start validation |
| |
| if(!$val->Name(trim($name))) |
| $messages[] = "Name Invalid"; |
| |
| if(!$val->Text(trim($subject))) |
| $messages[] = "Subject Invalid"; |
| |
| if(!$val->Email(trim($email))) |
| $messages[] = "E-Mail Invalid"; |
| |
| if(!$val->TextBox(trim(stripslashes($message)))) |
| $messages[] = "Message Invalid"; |
| |
| if(strtolower(trim($validateimage)) != strtolower($_SESSION["string_pass"])) |
| $messages[] = "Verification Image Invalid"; |
| |
| return $messages; |
| } |
| |
| private function sendMail($name, $email, $subject, $type, $message) |
| { |
| $headers = "From: ".$name." <".$email."> \r\n" . |
| "Reply-To: ".$email." \r\n" . |
| "X-Mailer: PHP/". phpversion(); |
| |
| $body = "Someone sent you an e-mail from your site:\n\n" . |
| "Name: ".$name."\r". |
| "Subject: ".$subject."\r". |
| "E-Mail: ".$email."\r". |
| "Type: ".$type."\r". |
| "Message: ".stripslashes($message); |
| |
| 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); |
| } |
| } |
| |
| ?> |