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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?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);
       }
}
 
?>