JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
contact: implement honeypot instead of visible robot field
[wfpl-cms.git] / contact.php
1 <?php
2
3 # This form requires wfpl. See: http://sametwice.com/wfpl
4
5 # SETUP
6
7 # To send results by e-mail, all you have to do is set your e-mail address here:
8 $GLOBALS['contact_to'] = 'fixme@example.com';
9 $GLOBALS['contact_from'] = 'noreply@example.com';
10 $GLOBALS['contact_cc'] = '';
11 $GLOBALS['contact_subject'] = '';
12
13
14 require_once(__DIR__.'/'.'inc/wfpl/format.php');
15 require_once(__DIR__.'/'.'inc/wfpl/email.php');
16
17 # generate a new random 16-character string
18 function contact_new_field_key() {
19         $character_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
20         $id = "                ";
21
22         # PHP 4.2.0 and up seed the random number generator for you.
23         # Lets hope that it seeds with something harder to guess than the clock.
24         for($i = 0; $i < 16; ++$i) {
25                 $id{$i} = $character_set{mt_rand(0, 61)};
26         }
27
28         return $id;
29 }
30
31 function contact_get_fields() {
32         $data = array();
33
34
35         $data['name'] = format_oneline(_REQUEST_cut('name'));
36         $data['comments'] = format_unix(_REQUEST_cut('comments'));
37
38         $fields = _REQUEST_cut('fields');
39         if (preg_match('/^[a-zA-Z0-9]{32}$/', $fields)) {
40                 $data['robot'] = format_oneline(_REQUEST_cut(substr($fields, 0, 16)));
41                 $data['email'] = format_email(_REQUEST_cut(substr($fields, 16)));
42         }
43
44         return $data;
45 }
46
47
48 function contact_main() {
49         return contact_main_form();
50 }
51
52 function contact_main_form() {
53         if (isset($_POST['name'])) {
54                 $data = contact_get_fields();
55                 $host = this_host();
56
57                 # gj robot, you did it ;)
58                 if ($data['robot'] !== '') {
59                         return './contact_thanks';
60                 }
61
62                 if (!$data['name'] && !$data['email'] && !$data['comments']) {
63                         // message("you didn't fill anything out")
64                 } elseif (!$data['email']) {
65                         message("Error: Please fill out the email field.");
66                 } else {
67                         $error = false;
68                         if ($data['robot'] === '' && $GLOBALS['contact_to'] != 'fixme@example.com') {
69                                 $to = $GLOBALS['contact_to'];
70                                 if ($GLOBALS['contact_from'] === '') {
71                                         $from = "$host/contact <noreply@$host>";
72                                 } else {
73                                         $from = $GLOBALS['contact_from'];
74                                 }
75                                 $reply_to = $to;
76                                 if (isset($data['email']) and valid_email($data['email'])) {
77                                         $reply_to = $data['email'];
78                                         if ($data['name'] and preg_match('/^[a-zA-Z0-9_\'. -]*$/', $data['name']) !== false) {
79                                                 $reply_to = "$data[name] <$reply_to>";
80                                         }
81                                 }
82                                 if ($GLOBALS['contact_subject'] === '') {
83                                         $subject = "Your message via $host/contact";
84                                 } else {
85                                         $subject = $GLOBALS['contact_subject'];
86                                 }
87                                 $email_template = new tem();
88                                 $email_template->load('contact.email.txt');
89                                 $email_template->sets($data);
90                                 $email_template->set('$host', $host);
91                                 $message = $email_template->run();
92                                 $cc = $GLOBALS['contact_cc'];
93                                 $bcc = '';
94                                 if (email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
95                                         message('Due to an internal error, your message could not be sent. Please try again later.');
96                                         $error = true;
97                                 } else {
98                                         message('Message sent');
99                                 }
100                         }
101                         if ($error !== true) {
102                                 # FIXME create this page or change this to go elsewhere
103                                 return './contact_thanks';
104                         }
105                 }
106                 # otherwise, we display the form again. We've got the form field
107                 # values in $data and will put those back in the filds below. You
108                 # should add some message asking people to fix their entry in
109                 # whatever way you require.
110         } else {
111                 # form not submitted, you can set default values like so:
112                 #$data = array('name' => 'Yes');
113                 $data = array();
114         }
115
116         $data['robot_field'] = contact_new_field_key();
117         $data['email_field'] = contact_new_field_key();
118
119         tem_set('form', $data);
120 }