-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.php
415 lines (313 loc) · 13.8 KB
/
form.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
<?php
/*-------------------------------------------------
Form Processor Plugin
by SemiColonWeb
---------------------------------------------------*/
/*-------------------------------------------------
PHPMailer Initialization
---------------------------------------------------*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
/*-------------------------------------------------
Receiver's Email
---------------------------------------------------*/
$toemails = array();
$toemails[] = array(
'email' => '[email protected]', // Your Email Address
'name' => 'Your Name' // Your Name
);
/*-------------------------------------------------
Sender's Email
---------------------------------------------------*/
$fromemail = array(
'email' => '[email protected]', // Company's Email Address (preferably currently used Domain Name)
'name' => 'Company Name' // Company Name
);
/*-------------------------------------------------
reCaptcha
---------------------------------------------------*/
// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = ''; // Your reCaptcha Secret
/*-------------------------------------------------
PHPMailer Initialization
---------------------------------------------------*/
$mail = new PHPMailer();
/* Add your SMTP Codes after this Line */
// End of SMTP
/*-------------------------------------------------
Form Messages
---------------------------------------------------*/
$message = array(
'success' => 'We have <strong>successfully</strong> received your Message and will get Back to you as soon as possible.',
'error' => 'Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.',
'error_bot' => 'Bot Detected! Form could not be processed! Please Try Again!',
'error_unexpected' => 'An <strong>unexpected error</strong> occured. Please Try Again later.',
'recaptcha_invalid' => 'Captcha not Validated! Please Try Again!',
'recaptcha_error' => 'Captcha not Submitted! Please Try Again.'
);
/*-------------------------------------------------
Blocked Words from Forms
---------------------------------------------------*/
$spam_keywords = array(
'viagra',
'cialis',
'levitra'
);
/*-------------------------------------------------
Form Processor
---------------------------------------------------*/
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$prefix = !empty( $_POST['prefix'] ) ? $_POST['prefix'] : '';
$submits = $_POST;
$botpassed = false;
$message_form = !empty( $submits['message'] ) ? $submits['message'] : array();
$message['success'] = !empty( $message_form['success'] ) ? $message_form['success'] : $message['success'];
$message['error'] = !empty( $message_form['error'] ) ? $message_form['error'] : $message['error'];
$message['error_bot'] = !empty( $message_form['error_bot'] ) ? $message_form['error_bot'] : $message['error_bot'];
$message['error_unexpected'] = !empty( $message_form['error_unexpected'] ) ? $message_form['error_unexpected'] : $message['error_unexpected'];
$message['recaptcha_invalid'] = !empty( $message_form['recaptcha_invalid'] ) ? $message_form['recaptcha_invalid'] : $message['recaptcha_invalid'];
$message['recaptcha_error'] = !empty( $message_form['recaptcha_error'] ) ? $message_form['recaptcha_error'] : $message['recaptcha_error'];
/*-------------------------------------------------
Bot Protection
---------------------------------------------------*/
if( isset( $submits[ $prefix . 'botcheck' ] ) ) {
$botpassed = true;
}
if( !empty( $submits[ $prefix . 'botcheck' ] ) ) {
$botpassed = false;
}
if( $botpassed == false ) {
echo '{ "alert": "error", "message": "' . $message['error_bot'] . '" }';
exit;
}
/*-------------------------------------------------
SPAM Protection
---------------------------------------------------*/
function spam_keyword_check( $submitted, $spamwords ) {
if( is_array( $submitted ) ) {
return false;
}
if( !is_array( $spamwords ) ) $spamwords = array( $spamwords );
foreach( $spamwords as $spamstring ) {
if( ( $position = stripos( $submitted, $spamstring ) ) !== false ) return $position;
}
return false;
}
foreach( $submits as $spam_submit ) {
if( spam_keyword_check( $spam_submit, $spam_keywords ) ) {
// A successful message is displayed to the submitter that makes him think that the Form has been sent so that he cannot modify the keywords to prevent SPAM
echo '{ "alert": "success", "message": "' . $message['success'] . '" }';
exit;
}
}
/*-------------------------------------------------
reCaptcha
---------------------------------------------------*/
if( isset( $submits['g-recaptcha-response'] ) ) {
$recaptcha_data = array(
'secret' => $recaptcha_secret,
'response' => $submits['g-recaptcha-response']
);
$recap_verify = curl_init();
curl_setopt( $recap_verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify" );
curl_setopt( $recap_verify, CURLOPT_POST, true );
curl_setopt( $recap_verify, CURLOPT_POSTFIELDS, http_build_query( $recaptcha_data ) );
curl_setopt( $recap_verify, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $recap_verify, CURLOPT_RETURNTRANSFER, true );
$recap_response = curl_exec( $recap_verify );
$g_response = json_decode( $recap_response );
if ( $g_response->success !== true ) {
echo '{ "alert": "error", "message": "' . $message['recaptcha_invalid'] . '" }';
exit;
}
}
$template = !empty( $submits['template'] ) ? $submits['template'] : 'html';
$html_title = !empty( $submits['html_title'] ) ? $submits['html_title'] : 'Form Response';
$forcerecap = ( !empty( $submits['force_recaptcha'] ) && $submits['force_recaptcha'] != 'false' ) ? true : false;
$replyto = !empty( $submits['replyto'] ) ? explode( ',', $submits['replyto'] ) : false;
if( $forcerecap ) {
if( !isset( $submits['g-recaptcha-response'] ) ) {
echo '{ "alert": "error", "message": "' . $message['recaptcha_error'] . '" }';
exit;
}
}
/*-------------------------------------------------
Auto-Responders
---------------------------------------------------*/
$autores = ( !empty( $submits['autoresponder'] ) && $submits['autoresponder'] != 'false' ) ? true : false;
$ar_subject = !empty( $submits['ar_subject'] ) ? $submits['ar_subject'] : 'Thanks for your Email';
$ar_title = !empty( $submits['ar_title'] ) ? $submits['ar_title'] : 'Its so good to hear from You!';
$ar_message = !empty( $submits['ar_message'] ) ? $submits['ar_message'] : 'Autoresponder Message';
preg_match_all('#\{(.*?)\}#', $ar_message, $ar_matches);
if( !empty( $ar_matches[1] ) ) {
foreach( $ar_matches[1] as $ar_key => $ar_value ) {
$ar_message = str_replace( '{' . $ar_value . '}' , $submits[ $ar_value ], $ar_message );
}
}
$ar_footer = !empty( $submits['ar_footer'] ) ? $submits['ar_footer'] : 'Copyrights © ' . date('Y') . ' <strong>SemiColonWeb</strong>. All Rights Reserved.';
$mail->Subject = !empty( $submits['subject'] ) ? $submits['subject'] : 'Form Response from your Website';
$mail->SetFrom( $fromemail['email'] , $fromemail['name'] );
if( !empty( $replyto ) ) {
if( count( $replyto ) > 1 ) {
$replyto_e = $submits[ $replyto[0] ];
$replyto_n = $submits[ $replyto[1] ];
$mail->AddReplyTo( $replyto_e , $replyto_n );
} elseif( count( $replyto ) == 1 ) {
$replyto_e = $submits[ $replyto[0] ];
$mail->AddReplyTo( $replyto_e );
}
}
foreach( $toemails as $toemail ) {
$mail->AddAddress( $toemail['email'] , $toemail['name'] );
}
$unsets = array( 'prefix', 'subject', 'replyto', 'template', 'html_title', 'message', 'autoresponder', 'ar_subject', 'ar_title', 'ar_message', 'ar_footer', $prefix . 'botcheck', 'g-recaptcha-response', 'force_recaptcha', $prefix . 'submit' );
foreach( $unsets as $unset ) {
unset( $submits[ $unset ] );
}
$fields = array();
foreach( $submits as $name => $value ) {
if( empty( $value ) ) continue;
$name = str_replace( $prefix , '', $name );
$name = mb_convert_case( $name, MB_CASE_TITLE, "UTF-8" );
if( is_array( $value ) ) {
$value = implode( ', ', $value );
}
$fields[$name] = $value;
}
$files = $_FILES;
foreach( $files as $file => $filevalue ) {
if( is_array( $filevalue['name'] ) ) {
$filecount = count( $filevalue['name'] );
for( $f = 0; $f < $filecount; $f++ ) {
if ( isset( $_FILES[ $file ] ) && $_FILES[ $file ]['error'][ $f ] == UPLOAD_ERR_OK ) {
$mail->IsHTML(true);
$mail->AddAttachment( $_FILES[ $file ]['tmp_name'][ $f ], $_FILES[ $file ]['name'][ $f ] );
}
}
} else {
if ( isset( $_FILES[ $file ] ) && $_FILES[ $file ]['error'] == UPLOAD_ERR_OK ) {
$mail->IsHTML(true);
$mail->AddAttachment( $_FILES[ $file ]['tmp_name'], $_FILES[ $file ]['name'] );
}
}
}
$response = array();
foreach( $fields as $fieldname => $fieldvalue ) {
if( $template == 'text' ) {
$response[] = $fieldname . ': ' . $fieldvalue;
} else {
$fieldname = '<tr>
<td class="hero-subheader__title" style="font-size: 16px; line-height: 24px; font-weight: bold; padding: 0 0 5px 0;" align="left">' . $fieldname . '</td>
</tr>';
$fieldvalue = '<tr>
<td class="hero-subheader__content" style="font-size: 16px; line-height: 24px; color: #777777; padding: 0 15px 30px 0;" align="left">' . $fieldvalue . '</td>
</tr>';
$response[] = $fieldname . $fieldvalue;
}
}
$referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';
$html_before = '<table class="full-width-container" border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" bgcolor="#eeeeee" style="width: 100%; height: 100%; padding: 50px 0 50px 0;">
<tr>
<td align="center" valign="top">
<!-- / 700px container -->
<table class="container" border="0" cellpadding="0" cellspacing="0" width="84%" bgcolor="#ffffff" style="width: 84%;">
<tr>
<td align="center" valign="top">
';
$html_after = '<!-- /// Footer -->
</td>
</tr>
</table>
</td>
</tr>
</table>';
if( $template == 'text' ) {
$body = implode( "<br>", $response ) . $referrer;
} else {
$html = $html_before . '<!-- / Header -->
<table class="container header" border="0" cellpadding="0" cellspacing="0" width="84%" style="width: 84%;">
<tr>
<td style="padding: 30px 0 30px 0; border-bottom: solid 1px #eeeeee; font-size: 30px; font-weight: bold; text-decoration: none; color: #000000;" align="left">
' . $html_title . '
</td>
</tr>
</table>
<!-- /// Header -->
<!-- / Hero subheader -->
<table class="container hero-subheader" border="0" cellpadding="0" cellspacing="0" width="84%" style="width: 84%; padding: 60px 0 30px 0;"">
' . implode( '', $response ) . '
</table>
<!-- / Footer -->
<table class="container" border="0" cellpadding="0" cellspacing="0" width="100%" align="center">
<tr>
<td align="center">
<table class="container" border="0" cellpadding="0" cellspacing="0" width="84%" align="center" style="border-top: 1px solid #eeeeee; width: 84%;">
<tr>
<td style="color: #d5d5d5; text-align: center; font-size: 12px; padding: 30px 0 30px 0; line-height: 22px;">' . strip_tags( $referrer ) . '</td>
</tr>
</table>
</td>
</tr>
</table>
' . $html_after;
$body = $html;
}
if( $autores && !empty( $replyto_e ) ) {
$autoresponder = new PHPMailer();
/* Add your Auto-Responder SMTP Codes after this Line */
// End of Auto-Responder SMTP
$autoresponder->SetFrom( $fromemail['email'] , $fromemail['name'] );
if( !empty( $replyto_n ) ) {
$autoresponder->AddAddress( $replyto_e , $replyto_n );
} else {
$autoresponder->AddAddress( $replyto_e );
}
$autoresponder->Subject = $ar_subject;
$ar_body = $html_before . '<!-- / Header -->
<table class="container header" border="0" cellpadding="0" cellspacing="0" width="84%" style="width: 84%;">
<tr>
<td style="padding: 30px 0 30px 0; border-bottom: solid 1px #eeeeee; font-size: 30px; font-weight: bold; text-decoration: none; color: #000000;" align="left">
' . $ar_title . '
</td>
</tr>
</table>
<!-- /// Header -->
<!-- / Hero subheader -->
<table class="container hero-subheader" border="0" cellpadding="0" cellspacing="0" width="84%" style="width: 84%; padding: 60px 0 30px 0;"">
<tr>
<td class="hero-subheader__content" style="font-size: 16px; line-height: 26px; color: #777777; padding: 0 15px 30px 0;" align="left">' . $ar_message . '</td>
</tr>
</table>
<!-- / Footer -->
<table class="container" border="0" cellpadding="0" cellspacing="0" width="100%" align="center">
<tr>
<td align="center">
<table class="container" border="0" cellpadding="0" cellspacing="0" width="84%" align="center" style="border-top: 1px solid #eeeeee; width: 84%;">
<tr>
<td style="color: #d5d5d5; text-align: center; font-size: 12px; padding: 30px 0 30px 0; line-height: 22px;">' . $ar_footer . '</td>
</tr>
</table>
</td>
</tr>
</table>
' . $html_after;
$autoresponder->MsgHTML( $ar_body );
}
$mail->MsgHTML( $body );
$mail->CharSet = "UTF-8";
$sendEmail = $mail->Send();
if( $sendEmail == true ):
if( $autores && !empty( $replyto_e ) ) {
$send_arEmail = $autoresponder->Send();
}
echo '{ "alert": "success", "message": "' . $message['success'] . '" }';
else:
echo '{ "alert": "error", "message": "' . $message['error'] . '<br><br><strong>Reason:</strong><br>' . $mail->ErrorInfo . '" }';
endif;
} else {
echo '{ "alert": "error", "message": "' . $message['error_unexpected'] . '" }';
}
?>