-
Notifications
You must be signed in to change notification settings - Fork 1
/
formmail_recaptcha.php
423 lines (364 loc) · 11.8 KB
/
formmail_recaptcha.php
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
<?php
/* Copyright (c) 2002 Eli Sand */
########################################################################
# #
# PHP FormMail v2.0 20030305 #
# #
########################################################################
#
# Settings
#
# Initialize variables
#
$auth = NULL;
$deny = NULL;
$must = NULL;
$post = NULL;
$http = NULL;
$form = NULL;
$list = NULL;
# Fix for pre PHP 4.1.x versions
#
if (!isset($_POST)) {
$_POST = &$HTTP_POST_VARS;
}
if (!isset($_SERVER)) {
$_SERVER = &$HTTP_SERVER_VARS;
}
if (!function_exists('array_key_exists')) {
function array_key_exists($key, $array) {return in_array($key, array_keys($array)) ? true : false;}
}
# Fix for magic quotes when enabled
#
if (get_magic_quotes_gpc()) {
foreach ($_POST as $key => $value) {
$_POST["$key"] = stripslashes($value);
}
}
# Detect any Windows operating system
#
if (strstr(php_uname(), 'Windows')) {
$IS_WINDOWS = TRUE;
}
# Get the referring domain name
#
$referer_domain = get_domain_referer($_SERVER['HTTP_REFERER']);
########################################################################
# #
# USER CONFIGURABLE SETTINGS #
# #
########################################################################
# Authorized email address masks that can be used as the recipient
#
$auth = "*@127.0.0.1, *@localhost";
$auth .= ",*@mychurchserver.com,*@naturecoastdesign.net,";
# Authorize all email addresses to the current domain
#
# If you want strict email account authorization, comment this out and
# the script will only authorize the masks in the list defined above.
#
$auth .= ", *@" . get_domain($_SERVER['SERVER_NAME']);
# Email address masks that will be rejected if in the email field
#
$deny = "nobody@*, anonymous@*, postmaster@*";
# Authorized domain referers
#
$auth_domains = "mychurchserver.com,naturecoastdesign.net";
# The following allow you to set some default settings
#
# These are commented out by default and when used, either override or
# append to any values. This allows you to ensure that hackers don't
# post their own values to certain fields, making you miss out on
# important data that you want to ensure is included in the email.
#
#$must['required'] = "env_report";
$must['env_report'] = "SERVER_NAME,HTTP_REFERER,REMOTE_ADDR";
#$must['redirect'] = "http://my.domain.com/ok.html";
#$must['error_redirect'] = "http://my.domain.com/error.html";
#$must['missing_fields_redirect'] = "http://my.domain.com/missing.html";
#
########################################################################
########################################################################
# #
# DO NOT EDIT ANYTHING PAST THIS POINT #
# #
########################################################################
########################################################################
#
# Functions
#
#Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)', '(\r+)', 'to:', 'cc:', 'bcc:', 'content-type:', 'mime-version:', 'multipart-mixed:', 'content-transfer-encoding:','(\t+)','(%0A+)','(%0D+)','(%08+)','(%09+)');
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
#Function to validate against any email injection attempts
function Isnumber()
{
// Break up the string:
$phone = preg_replace("/[^0-9]/", "", $_POST['phone'] );
if( is_numeric($phone) && strlen($phone) == '10')
{
return true;
}
else
{
return false;
}
}
if ($error_msg != NULL) {
echo '<p class="error">ERROR: '. nl2br($error_msg) . "</p>";
}
if ($result != NULL) {
echo '<p class="success">'. $result . "</p>";
echo $post['answer'];
}
# Trim leading and trailing white space from array values
#
function array_trim(&$value, $key) {
$value = trim($value);
}
# Return the top level domain of a hostname
#
function get_domain($string) {
if (eregi('\.?([a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]+)$', $string, $values)) {
return $values[1];
}
return NULL;
}
# Return the top level domain of a referer
#
function get_domain_referer($string) {
$referer_domain = parse_url($string);
return get_domain($referer_domain['host']);
}
# Show an error message to the user
#
function error_msg($error, $required = FALSE) {
global $post;
if (!empty($post['missing_fields_redirect']) && $required) {
header('Location: ' . $post['missing_fields_redirect']);
}
elseif (!empty($post['error_redirect'])) {
header('Location: ' . $post['error_redirect']);
}
else {
echo "<html>\r\n";
echo "\t<head>\r\n";
echo "\t\t<title>Form Error</title>\r\n";
echo "\t\t<style type=\"text/css\">* {font-family: \"Verdana\", \"Arial\", \"Helvetica\", monospace;}</style>\r\n";
echo "\t</head>\r\n";
echo "\t<body>\r\n";
echo "\t\t<p>${error}</p>\r\n\t\t<p><small>« <a href=\"javascript: history.back();\">go back</a></small></p>\r\n";
echo "\t</body>\r\n";
echo "</html>\r\n";
}
exit();
}
# Basic pattern matching on an entire array
#
function pattern_grep($input, $array) {
foreach ($array as $value) {
$value = addcslashes($value, '^.[]$()|{}\\');
$value = str_replace('*', '.*', $value);
$value = str_replace('?', '.?', $value);
$value = str_replace('+', '.+', $value);
if (eregi('^' . $value . '$', $input)) {
return TRUE;
}
}
return FALSE;
}
#
########################################################################
########################################################################
#
# Main
#
# Check to make sure the info was posted
#
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
# Check to make sure it is an acceptable referer
#
if (stristr($auth_domains,$referer_domain)) {
$post = array(
'answer' => $_POST['answer'],
'recipient' => $_POST['recipient'],
'email' => $_POST['email'],
'subject' => $_POST['subject'],
'realname' => $_POST['realname'],
'required' => $_POST['required'],
'env_report' => $_POST['env_report'],
'sort' => $_POST['sort'],
'redirect' => $_POST['redirect'],
'error_redirect' => $_POST['error_redirect'],
'missing_fields_redirect' => $_POST['missing_fields_redirect']
);
$http = array(
'REMOTE_USER' => $_SERVER['REMOTE_USER'],
'REMOTE_ADDR' => $_SERVER['REMOTE_ADDR'],
'HTTP_USER_AGENT' => $_SERVER['HTTP_USER_AGENT'],
'HTTP_REFERER' => get_domain_referer($_SERVER['HTTP_REFERER']),
'SERVER_NAME' => $_SERVER['SERVER_NAME']
);
if (isset($must['required'])) {
$post['required'] = $must['required'] . ',' . $_POST['required'];
}
if (isset($must['env_report'])) {
$post['env_report'] = $must['env_report'] . ',' . $_POST['env_report'];
}
if (isset($must['redirect'])) {
$post['redirect'] = $must['redirect'];
}
if (isset($must['error_redirect'])) {
$post['error_redirect'] = $must['error_redirect'];
}
if (isset($must['missing_fields_redirect'])) {
$post['missing_fields_redirect'] = $must['missing_fields_redirect'];
}
if (($auth = explode(',', $auth))) {
array_walk($auth, 'array_trim');
}
if (($deny = explode(',', $deny))) {
array_walk($deny, 'array_trim');
}
# Check if reCaptcha is required
#
if (!empty($post['required']) && stristr($post['required'],'answer')) {
# Check for reCaptcha response
#
if (strtolower($post['answer']) !== "green") {
error_msg("The answer wasn't entered correctly. Go back and try it again.");
}
}
# Check for missing required fields
#
if ((!empty($post['required'])) && ($list = explode(',', $post['required']))) {
$list[] = 'recipient';
$list[] = 'email';
array_walk($list, 'array_trim');
foreach ($list as $value) {
if (!empty($value) && empty($_POST["$value"])) {
error_msg("You have left a required field ($value) blank.", TRUE);
}
}
}
# Check the email addresses submitted
#
if (pattern_grep($post['email'], $deny)) {
error_msg("You have specified a banned email address.");
}
if (!eregi('^([a-zA-Z0-9\.\_\-]+)\@((([a-zA-Z0-9\-]+)\.)+([a-zA-Z]+))$', $post['email'])) {
error_msg("You have specified an invalid email address.");
}
if (!$IS_WINDOWS) {
if (!getmxrr(get_domain($post['email']), $mxhost)) {
error_msg("You have no mail exchange records for your email address.");
}
}
# Check if the recipients email address is authorized
#
if ((!empty($post['recipient'])) && ($list = explode(',', $post['recipient']))) {
array_walk($list, 'array_trim');
foreach ($list as $value) {
if (!eregi('^([a-zA-Z0-9\.\_\-]+)\@((([a-zA-Z0-9\-]+)\.)+([a-zA-Z]+))$', $value)) {
error_msg("The recipients email address is invalid.");
}
if (!pattern_grep($value, $auth)) {
error_msg("The recipients email address is unauthorized.");
}
}
}
else {
error_msg("There was an unknown error while checking the recipients email address.");
}
# Check if the posted email Is Injected
#
if( IsInjected($_POST['email']) )
{
error_msg("Bad email value!");
}
# Check if the posted phone number is a number
#
if( !Isnumber() )
{
error_msg("the phone number you entered is not vaild");
}
# Sort the fields
#
if ((!empty($post['sort'])) && ($list = explode(',', $post['sort']))) {
array_walk($list, 'array_trim');
foreach ($list as $value) {
$form["$value"] = $_POST["$value"];
}
}
else {
$form = $_POST;
}
# Create the message
#
$subject = empty($post['subject']) ? "Online form" : "Online form: " . $post['subject'];
$message = "Submitted by: " . $post['realname'] . " <" . $post['email'] . "> on " . date('l, F jS, Y @ g:i:s a (O)') . "\r\n\r\n";
$message .= "Online Form Fields\r\n";
$message .= "------------------\r\n";
foreach ($form as $key => $value) {
if (!array_key_exists($key, $post)) {
$message .= "${key}: ${value}\r\n";
}
}
if (!empty($post['env_report'])) {
if (($list = explode(',', $post['env_report']))) {
$message .= "Client Variables\r\n";
$message .= "----------------\r\n";
array_walk($list, 'array_trim');
foreach ($list as $value) {
if (array_key_exists($value, $http)) {
$message .= "${value}: " . $http["$value"] . "\r\n";
}
}
}
}
# End of message
$message .= "------------------\r\n";
# Send out the email
#
if (mail($post['recipient'], $subject, $message, "From: " . $post['email'] . "\r\nReply-To: " . $post['email'] . "\r\nX-Mailer: PHP FormMail")) {
if (!empty($post['redirect'])) {
header('Location: ' . $post['redirect']);
}
else {
echo "<html>\r\n";
echo "\t<head>\r\n";
echo "\t\t<title>Thank you</title>\r\n";
echo "\t\t<style type=\"text/css\">* {font-family: \"Verdana\", \"Arial\", \"Helvetica\", monospace;}</style>\r\n";
echo "\t</head>\r\n";
echo "\t<body>\r\n";
echo "\t\t<p>Thank you for filling out the form.</p>\r\n\t\t<p><small>« <a href=\"javascript: history.back();\">go back</a></small></p>\r\n";
echo "\t</body>\r\n";
echo "</html>\r\n";
}
}
else {
error_msg("There was an unknown error while sending email.");
}
}
else {
error_msg("This domain is unauthorized to use this program.");
}
}
else {
error_msg("Invalid request method used.");
}
#
########################################################################
?>