-
-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathcontact.php
More file actions
407 lines (316 loc) · 9.98 KB
/
Copy pathcontact.php
File metadata and controls
407 lines (316 loc) · 9.98 KB
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
<?php
/*
* e107 website system
*
* Copyright (C) 2008-2013 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* /contact.php
*
*/
require_once(__DIR__."/class2.php");
class contact_front
{
function __construct()
{
$range = range(00,24);
$tp = e107::getParser();
$defs = array();
foreach($range as $val)
{
$inc = $tp->leadingZeros($val,2);
$legacy = 'LAN_CONTACT_'.$inc;
// $defs[$legacy] = 'LANCONTACT_'.$inc;
$defs['LANCONTACT_'.$inc] = 'LAN_CONTACT_'.$inc;
}
e107::getLanguage()->bcDefs($defs);
$this->init();
}
function init()
{
$pref = e107::pref();
$active = varset($pref['contact_visibility'], e_UC_PUBLIC);
$contactInfo = trim(SITECONTACTINFO);
$pref = e107::getPref();
if(!check_class($active) && empty($contactInfo) && empty($pref['contact_info']))
{
e107::redirect();
}
$offered = check_class($active) && isset($pref['sitecontacts']) && $pref['sitecontacts'] != e_UC_NOBODY;
if($offered && isset($_POST['send-contactus']))
{
$this->processFormSubmit();
}
$form = '';
$info = '';
if(deftrue('SITECONTACTINFO') || !empty($pref['contact_info']))
{
$info = $this->renderContactInfo();
}
if($offered)
{
$form = $this->renderContactForm();
}
elseif($active == e_UC_MEMBER && ($pref['sitecontacts'] != e_UC_NOBODY))
{
$this->renderSignupRequired();
}
if(!$LAYOUT = e107::getCoreTemplate('contact', 'layout'))
{
$LAYOUT = '{---CONTACT-INFO---} {---CONTACT-FORM---} ';
}
$LAYOUT = e107::getParser()->parseTemplate($LAYOUT, true, e107::getScBatch('contact'));
echo str_replace(
['{---CONTACT-FORM---}', '{---CONTACT-INFO---}'],
[$form, $info],
$LAYOUT
);
}
/**
* A visitor's words, every tag stripped and every brace encoded through the mail template parse.
*
* @param string $text
* @return string
*/
private function textForTheEmail($text)
{
return str_replace(array('{', '}'), array('{', '}'),
e107::getParser()->filter($text));
}
/**
* Did this submission come from a document this site rendered?
*
* A request that carries no cookie is exempt from the core check in
* class2.php, by design: it has no ambient authority to borrow, so refusing
* it protects nobody and breaks machine-to-machine callers. This form is the
* exception, because what a caller borrows here is not the visitor's
* authority but the site's own mail server, addressed to a recipient the site
* chooses. So the request has to present a cookie in every mode, and a
* browser that rendered the form always does. The header is read rather than
* $_COOKIE, which handlers add to as the request is served.
*
* Above that, the proof the site's CSRF mode asks for is required too: a
* form token where tokens are read, and Sec-Fetch-Site where the browser is
* asked instead, both of which {@see e_session::check()} settles.
*
* @return bool
*/
private function submissionIsAuthentic()
{
$session = e107::getSession();
if(empty($_SERVER['HTTP_COOKIE']) || !$session->check(false))
{
return false;
}
if(!e_session::modeUsesToken() || !deftrue('e_TOKEN'))
{
return true;
}
$token = isset($_POST['e-token']) ? $_POST['e-token'] : varset($_POST['e_token'], '');
return $session->checkFormToken($token);
}
/**
* Resolve the person a submission is addressed to.
*
* The selector that offered the visitor a choice applied the sitecontacts
* predicates, so the same predicates decide who may be named. A posted
* contact_person narrows that set; it does not replace it.
*
* @return array|false user_name and user_email, or false when nobody matches
*/
private function findRecipient()
{
/** @var contact_shortcodes $sc */
$sc = e107::getScBatch('contact');
$qb = $sc->recipientQuery();
$qb->select('user_name', 'user_email')->setMaxResults(1);
if(!empty($_POST['contact_person']))
{
$qb->where('user_id', (int) $_POST['contact_person']);
}
return $qb->fetchRow();
}
/**
* @return void
*/
private function processFormSubmit()
{
$sec_img = e107::getSecureImg();
$tp = e107::getParser();
$ns = e107::getRender();
$pref = e107::getPref();
$error = "";
$ignore = false;
if(!$this->submissionIsAuthentic())
{
message_handler("P_ALERT", LAN_CONTACT_10);
return;
}
// Contact Form Filter -----
$contact_filter = e107::pref('core', 'contact_filter', '');
if(!empty($contact_filter))
{
$tmp = explode("\n", $contact_filter);
if(!empty($tmp))
{
foreach($tmp as $filterItem)
{
if(strpos($_POST['body'], $filterItem) !== false)
{
$ignore = true;
break;
}
}
}
}
// ---------
$sender_name = $tp->toEmail($_POST['author_name'], true, 'RAWTEXT');
$sender = check_email($_POST['email_send']);
$subject = $tp->toEmail($_POST['subject'], true, 'RAWTEXT');
$body = nl2br($this->textForTheEmail($_POST['body']));
$email_copy = !empty($_POST['email_copy']) ? 1 : 0;
// Check Image-Code
if($sec_img->invalidCode(varset($_POST['rand_num'], ''), varset($_POST['code_verify'], ''), secure_image::FORM_CONTACT))
{
$error .= LAN_CONTACT_15 . "\n";
}
// Check message body.
if(strlen(trim($body)) < 15)
{
$error .= LAN_CONTACT_12 . "\n";
}
// Check subject line.
if(isset($_POST['subject']) && strlen(trim($subject)) < 2)
{
$error .= LAN_CONTACT_13 . "\n";
}
if(!strpos(trim($sender), "@"))
{
$error .= LAN_CONTACT_11 . "\n";
}
// No errors - so proceed to email the admin and the user (if selected).
if($ignore === true)
{
$ns->tablerender('', "<div class='alert alert-success'>" . LAN_CONTACT_09 . "</div>"); // ignore and leave them none the wiser.
e107::getDebug()->log("Contact form post ignored");
require_once(FOOTERF);
exit;
}
elseif(empty($error))
{
$body .= "<br /><br />
<table class='table'>
<tr>
<td>IP:</td><td>" . e107::getIPHandler()->getIP(true) . "</td></tr>";
$body .= "<tr><td>" . LAN_EMAIL . ":</td><td>" . $tp->toEmail($sender, true, 'RAWTEXT') . "</td></tr>";
if(USER)
{
$body .= "<tr><td>User:</td><td>#" . USERID . " " . USERNAME . "</td></tr>";
}
$row = $this->findRecipient();
if($row)
{
$send_to = $row['user_email'];
$send_to_name = $row['user_name'];
}
else
{
$send_to = SITEADMINEMAIL;
$send_to_name = ADMIN;
}
// ----------------------
$CONTACT_EMAIL = e107::getCoreTemplate('contact', 'email');
unset($_POST['contact_person'], $_POST['author_name'], $_POST['email_send'], $_POST['subject'], $_POST['body'], $_POST['rand_num'], $_POST['code_verify'], $_POST['send-contactus'], $_POST['e-token'], $_POST['e_token']);
if(!empty($_POST)) // support for custom fields in contact template.
{
foreach($_POST as $k => $v)
{
$body .= "<tr><td>" . $this->textForTheEmail($k) . ":</td><td>" . $this->textForTheEmail($v) . "</td></tr>";
}
}
$body .= "</table>";
if(!empty($CONTACT_EMAIL['subject']))
{
$vars = array('CONTACT_SUBJECT' => $subject, 'CONTACT_PERSON' => $send_to_name);
if(!empty($_POST)) // support for custom fields in contact template.
{
foreach($_POST as $k => $v)
{
$scKey = strtoupper($k);
$vars[$scKey] = $tp->toEmail($v, true, 'RAWTEXT');
}
}
$subject = $tp->simpleParse($CONTACT_EMAIL['subject'], $vars);
}
// -----------------------
// Send as default sender to avoid spam issues. Use 'replyto' instead.
$eml = array(
'subject' => $subject,
'sender_name' => $sender_name,
'body' => $body,
'replyto' => $sender,
'replytonames' => $sender_name,
'template' => 'default'
);
$message = e107::getEmail()->sendEmail($send_to, $send_to_name, $eml) ? LAN_CONTACT_09 : LAN_CONTACT_10;
// $message = (sendemail($send_to,"[".SITENAME."] ".$subject, $body,$send_to_name,$sender,$sender_name)) ? LANCONTACT_09 : LANCONTACT_10;
if(isset($pref['contact_emailcopy']) && $pref['contact_emailcopy'] && $email_copy == 1)
{
require_once(e_HANDLER . "mail.php");
sendemail($sender, "[" . SITENAME . "] " . $subject, $body, ADMIN, $sender, $sender_name);
}
$ns->tablerender('', "<div class='alert alert-success'>" . $message . "</div>");
}
else
{
message_handler("P_ALERT", $error);
}
}
/**
* @return string html
*/
private function renderContactInfo()
{
$contact_shortcodes = e107::getScBatch('contact');
$CONTACT_INFO = varset($GLOBALS['CONTACT_INFO']);
if(empty($CONTACT_INFO))
{
$CONTACT_INFO = e107::getCoreTemplate('contact', 'info');
}
$contact_shortcodes->wrapper('contact/info');
$text = e107::getParser()->parseTemplate($CONTACT_INFO, true, $contact_shortcodes);
return e107::getRender()->tablerender(LAN_CONTACT_01, $text, "contact-info", true);
}
private function renderContactForm()
{
$CONTACT_FORM = varset($GLOBALS['CONTACT_FORM']);
if(empty($CONTACT_FORM))
{
$CONTACT_FORM = e107::getCoreTemplate('contact', 'form'); // require_once(e_THEME."templates/contact_template.php");
}
/** @var contact_shortcodes $contact_shortcodes */
$contact_shortcodes = e107::getScBatch('contact');
$contact_shortcodes->wrapper('contact/form');
$text = $contact_shortcodes->withImagecode(
e107::getParser()->parseTemplate($CONTACT_FORM, true, $contact_shortcodes));
if(trim($text) !== '')
{
return e107::getRender()->tablerender(LAN_CONTACT_02, $text, "contact-form", true);
}
}
private function renderSignupRequired()
{
$srch = array("[", "]");
$repl = array("<a class='alert-link' href='" . e_SIGNUP . "'>", "</a>");
$message = LAN_CONTACT_16; // "You must be [registered] and signed-in to use this form.";
e107::getRender()->tablerender(LAN_CONTACT_02, "<div class='alert alert-info'>" . str_replace($srch, $repl, $message) . "</div>", "contact");
}
}
e107::lan('core','contact');
e107::title(LAN_CONTACT_00);
e107::canonical('contact');
e107::route('contact/index');
require_once(HEADERF);
new contact_front;
require_once(FOOTERF);