1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?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);
       }
}
 
?>