1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of the Contact Form plugin for Moodle - https://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Contact Form is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Contact Form is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Contact Form. If not, see <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* This plugin for Moodle is used to send emails through a web form.
|
|
|
19 |
*
|
|
|
20 |
* @package local_contact
|
|
|
21 |
* @copyright 2016-2024 TNG Consulting Inc. - www.tngconsulting.ca
|
|
|
22 |
* @author Michael Milette
|
|
|
23 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* local_contact class. Handles processing of information submitted from a web form.
|
|
|
28 |
* @copyright 2016-2024 TNG Consulting Inc. - www.tngconsulting.ca
|
|
|
29 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class local_contact {
|
|
|
32 |
/**
|
|
|
33 |
* The name of the sender for the message.
|
|
|
34 |
*
|
|
|
35 |
* @var string
|
|
|
36 |
*/
|
|
|
37 |
public $fromname;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* The email address of the sender for the message.
|
|
|
41 |
*
|
|
|
42 |
* @var string
|
|
|
43 |
*/
|
|
|
44 |
public $fromemail;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* True if the information submitted is considered to have been sent from a spambot.
|
|
|
48 |
*
|
|
|
49 |
* @var bool
|
|
|
50 |
*/
|
|
|
51 |
public $isspambot;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Error message in case there are any issues.
|
|
|
55 |
*
|
|
|
56 |
* @var string
|
|
|
57 |
*/
|
|
|
58 |
public $errmsg;
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Class constructor. Receives and validates information received through a
|
|
|
62 |
* web form submission.
|
|
|
63 |
*
|
|
|
64 |
* @return boolean True if the information received passes our spambot detection. False if it fails.
|
|
|
65 |
*/
|
|
|
66 |
public function __construct() {
|
|
|
67 |
global $CFG;
|
|
|
68 |
|
|
|
69 |
if (isloggedin() && !isguestuser()) {
|
|
|
70 |
// If logged-in as non guest, use their registered fullname and email address.
|
|
|
71 |
global $USER;
|
|
|
72 |
$this->fromname = get_string('fullnamedisplay', null, $USER);
|
|
|
73 |
$this->fromemail = $USER->email;
|
|
|
74 |
|
|
|
75 |
// Insert name and email address at first position in $_POST array.
|
|
|
76 |
if (!empty($_POST['email'])) {
|
|
|
77 |
unset($_POST['email']);
|
|
|
78 |
}
|
|
|
79 |
if (!empty($_POST['name'])) {
|
|
|
80 |
unset($_POST['name']);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$_POST = array_merge(['email' => $this->fromemail], $_POST);
|
|
|
84 |
$_POST = array_merge(['name' => $this->fromname], $_POST);
|
|
|
85 |
} else {
|
|
|
86 |
// If not logged-in as a user or logged in a guest, the name and email fields are required.
|
|
|
87 |
if (empty($this->fromname = trim(optional_param(get_string('field-name', 'local_contact'), '', PARAM_TEXT)))) {
|
|
|
88 |
$this->fromname = required_param('name', PARAM_TEXT);
|
|
|
89 |
}
|
|
|
90 |
if (empty($this->fromemail = trim(optional_param(get_string('field-email', 'local_contact'), '', PARAM_EMAIL)))) {
|
|
|
91 |
$this->fromemail = required_param('email', PARAM_TEXT);
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
$this->fromname = trim($this->fromname ?? '');
|
|
|
95 |
$this->fromemail = trim($this->fromemail ?? '');
|
|
|
96 |
|
|
|
97 |
$this->isspambot = false;
|
|
|
98 |
$this->errmsg = '';
|
|
|
99 |
|
|
|
100 |
if ($CFG->branch >= 32) {
|
|
|
101 |
// As of Moodle 3.2, $CFG->emailonlyfromnoreplyaddress has been deprecated.
|
|
|
102 |
$CFG->emailonlyfromnoreplyaddress = !empty($CFG->noreplyaddress);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
// Did someone forget to configure Moodle properly?
|
|
|
106 |
|
|
|
107 |
// Validate Moodle's no-reply email address.
|
|
|
108 |
if (!empty($CFG->emailonlyfromnoreplyaddress)) {
|
|
|
109 |
if (
|
|
|
110 |
!$this->isspambot
|
|
|
111 |
&& !empty($CFG->emailonlyfromnoreplyaddress)
|
|
|
112 |
&& $this->isspambot = !validate_email($CFG->noreplyaddress)
|
|
|
113 |
) {
|
|
|
114 |
$this->errmsg = 'Moodle no-reply email address is invalid.';
|
|
|
115 |
if ($CFG->branch >= 32) {
|
|
|
116 |
$this->errmsg .= ' (<a href="../../admin/settings.php?section=outgoingmailconfig">change</a>)';
|
|
|
117 |
} else {
|
|
|
118 |
$this->errmsg .= ' (<a href="../../admin/settings.php?section=messagesettingemail">change</a>)';
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
// Use primary administrators name and email address if support name and email are not defined.
|
|
|
124 |
$primaryadmin = get_admin();
|
|
|
125 |
$CFG->supportemail = empty($CFG->supportemail) ? $primaryadmin->email : $CFG->supportemail;
|
|
|
126 |
$CFG->supportname = empty($CFG->supportname) ? fullname($primaryadmin, true) : $CFG->supportname;
|
|
|
127 |
|
|
|
128 |
// Validate Moodle's support email address.
|
|
|
129 |
if (!$this->isspambot && $this->isspambot = !validate_email($CFG->supportemail)) {
|
|
|
130 |
$this->errmsg = 'Moodle support email address is invalid.';
|
|
|
131 |
$this->errmsg .= ' (<a href="../../admin/settings.php?section=supportcontact">change</a>)';
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// START: Spambot detection.
|
|
|
135 |
|
|
|
136 |
// File attachments not supported.
|
|
|
137 |
$supportattachments = !empty(get_config('local_contact', 'attachment'));
|
|
|
138 |
if (!$supportattachments && !$this->isspambot && $this->isspambot = !empty($_FILES)) {
|
|
|
139 |
$this->errmsg = 'File attachments not enabled.';
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
// Validate submit button.
|
|
|
143 |
if (!$this->isspambot && $this->isspambot = !isset($_POST['submit'])) {
|
|
|
144 |
$this->errmsg = 'Missing submit button.';
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
// Limit maximum number of form $_POST fields to 1024.
|
|
|
148 |
if (!$this->isspambot) {
|
|
|
149 |
$postsize = @count($_POST);
|
|
|
150 |
if ($this->isspambot = ($postsize > 1024)) {
|
|
|
151 |
$this->errmsg = 'Form cannot contain more than 1024 fields.';
|
|
|
152 |
} else if ($this->isspambot = ($postsize == 0)) {
|
|
|
153 |
$this->errmsg = 'Form must be submitted using POST method.';
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// Limit maximum size of allowed form $_POST submission to 256 KB.
|
|
|
158 |
if (!$this->isspambot) {
|
|
|
159 |
$postsize = (int) @$_SERVER['CONTENT_LENGTH'];
|
|
|
160 |
if ($this->isspambot = ($postsize > 262144)) {
|
|
|
161 |
$this->errmsg = 'Form cannot contain more than 256 KB of data.';
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
// Validate if "sesskey" field contains the correct value.
|
|
|
166 |
if (!$this->isspambot && $this->isspambot = (optional_param('sesskey', '3.1415', PARAM_RAW) != sesskey())) {
|
|
|
167 |
$this->errmsg = '"sesskey" field is missing or contains an incorrect value.';
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
// Validate referrer URL.
|
|
|
171 |
if (!$this->isspambot && $this->isspambot = !isset($_SERVER['HTTP_REFERER'])) {
|
|
|
172 |
$this->errmsg = 'Missing referrer.';
|
|
|
173 |
}
|
|
|
174 |
if (!$this->isspambot && $this->isspambot = (stripos($_SERVER['HTTP_REFERER'], $CFG->wwwroot) != 0)) {
|
|
|
175 |
$this->errmsg = 'Unknown referrer - must come from this site: ' . $CFG->wwwroot;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
// Validate sender's email address.
|
|
|
179 |
if (!$this->isspambot && $this->isspambot = !validate_email($this->fromemail)) {
|
|
|
180 |
$this->errmsg = 'Unknown sender - invalid email address or the form field name is incorrect.';
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
// Validate sender's name.
|
|
|
184 |
if (!$this->isspambot && $this->isspambot = empty($this->fromname)) {
|
|
|
185 |
$this->errmsg = 'Missing sender - invalid name or the form field name is incorrect';
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
// Validate against email address whitelist and blacklist.
|
|
|
189 |
$skipdomaintest = false;
|
|
|
190 |
// TODO: MDL-0 - Create a plugin setting for this list.
|
|
|
191 |
$whitelist = ''; // Future code: $config->whitelistemails .
|
|
|
192 |
$whitelist = ',' . $whitelist . ',';
|
|
|
193 |
// TODO: MDL-0 - Create a plugin blacklistemails setting.
|
|
|
194 |
$blacklist = ''; // Future code: $config->blacklistemails .
|
|
|
195 |
$blacklist = ',' . $blacklist . ',';
|
|
|
196 |
if (!$this->isspambot && stripos($whitelist, ',' . $this->fromemail . ',') != false) {
|
|
|
197 |
$skipdomaintest = true; // Skip the upcoming domain test.
|
|
|
198 |
} else {
|
|
|
199 |
if (
|
|
|
200 |
!$this->isspambot
|
|
|
201 |
&& $blacklist != ',,'
|
|
|
202 |
&& $this->isspambot = ($blacklist == '*' || stripos($blacklist, ',' . $this->fromemail . ',') == false)
|
|
|
203 |
) {
|
|
|
204 |
// Nice try. We know who you are.
|
|
|
205 |
$this->errmsg = 'Bad sender - Email address is blacklisted.';
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
// Validate against domain whitelist and blacklist... except for the nice people.
|
|
|
210 |
if (!$skipdomaintest && !$this->isspambot) {
|
|
|
211 |
// TODO: MDL-0 - Create a plugin whitelistdomains setting.
|
|
|
212 |
$whitelist = ''; // Future code: $config->whitelistdomains .
|
|
|
213 |
$whitelist = ',' . $whitelist . ',';
|
|
|
214 |
$domain = substr(strrchr($this->fromemail, '@'), 1);
|
|
|
215 |
|
|
|
216 |
if (stripos($whitelist, ',' . $domain . ',') != false) {
|
|
|
217 |
// Ya, you check out. This email domain is gold here!
|
|
|
218 |
$blacklist = '';
|
|
|
219 |
} else {
|
|
|
220 |
// TODO: MDL-0 - Create a plugin blacklistdomains setting.
|
|
|
221 |
$blacklist = 'example.com,example.net,sample.com,test.com,specified.com'; // Future code:$config->blacklistdomains .
|
|
|
222 |
$blacklist = ',' . $blacklist . ',';
|
|
|
223 |
if (
|
|
|
224 |
$blacklist != ',,'
|
|
|
225 |
&& $this->isspambot = ($blacklist == '*'
|
|
|
226 |
|| stripos($blacklist, ',' . $domain . ',') != false)
|
|
|
227 |
) {
|
|
|
228 |
// Naughty naughty. We know all about your kind.
|
|
|
229 |
$this->errmsg = 'Bad sender - Email domain is blacklisted.';
|
|
|
230 |
}
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
// TODO: MDL-0 - Test IP address against blacklist.
|
|
|
235 |
|
|
|
236 |
// END: Spambot detection... Wait, got some photo ID on you? ;-) .
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Creates a user info object based on provided parameters.
|
|
|
241 |
*
|
|
|
242 |
* @param string $email email address.
|
|
|
243 |
* @param string $name (optional) Plain text real name.
|
|
|
244 |
* @param int $id (optional) Moodle user ID.
|
|
|
245 |
*
|
|
|
246 |
* @return object Moodle userinfo.
|
|
|
247 |
*/
|
|
|
248 |
private function makeemailuser($email, $name = '', $id = -99) {
|
|
|
249 |
$emailuser = new stdClass();
|
|
|
250 |
$emailuser->email = trim(filter_var($email, FILTER_SANITIZE_EMAIL) ?? '');
|
|
|
251 |
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
|
252 |
$emailuser->email = '';
|
|
|
253 |
}
|
|
|
254 |
$emailuser->firstname = format_text($name, FORMAT_PLAIN, ['trusted' => false]);
|
|
|
255 |
$emailuser->lastname = '';
|
|
|
256 |
$emailuser->maildisplay = true;
|
|
|
257 |
$emailuser->mailformat = 1; // 0 (zero) text-only emails, 1 (one) for HTML emails.
|
|
|
258 |
$emailuser->id = $id;
|
|
|
259 |
$emailuser->firstnamephonetic = '';
|
|
|
260 |
$emailuser->lastnamephonetic = '';
|
|
|
261 |
$emailuser->middlename = '';
|
|
|
262 |
$emailuser->alternatename = '';
|
|
|
263 |
$emailuser->username = '';
|
|
|
264 |
return $emailuser;
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
/**
|
|
|
268 |
* Send email message and optionally autorespond.
|
|
|
269 |
*
|
|
|
270 |
* @param string $email Recipient's Email address.
|
|
|
271 |
* @param string $name Recipient's real name in plain text.
|
|
|
272 |
* @param boolean $sendconfirmationemail Set to true to also send an autorespond confirmation email back to user (TODO).
|
|
|
273 |
*
|
|
|
274 |
* @return boolean $status - True if message was successfully sent, false if not.
|
|
|
275 |
*/
|
|
|
276 |
public function sendmessage($email, $name, $sendconfirmationemail = false) {
|
|
|
277 |
global $USER, $CFG, $SITE;
|
|
|
278 |
|
|
|
279 |
$systemcontext = context_system::instance();
|
|
|
280 |
|
|
|
281 |
// Create the sender from the submitted name and email address.
|
|
|
282 |
$from = $this->makeemailuser($this->fromemail, $this->fromname);
|
|
|
283 |
|
|
|
284 |
// Create the recipient.
|
|
|
285 |
$to = $this->makeemailuser($email, $name);
|
|
|
286 |
|
|
|
287 |
// Create the Subject for message.
|
|
|
288 |
$subject = '';
|
|
|
289 |
if (empty(get_config('local_contact', 'nosubjectsitename'))) { // Not checked.
|
|
|
290 |
// Include site name in subject field.
|
|
|
291 |
$subject .= '[' . format_string($SITE->shortname, true, ['escape' => false, 'context' => $systemcontext]) . '] ';
|
|
|
292 |
}
|
|
|
293 |
$subject .= optional_param(
|
|
|
294 |
get_string('field-subject', 'local_contact'),
|
|
|
295 |
get_string('defaultsubject', 'local_contact'),
|
|
|
296 |
PARAM_TEXT
|
|
|
297 |
);
|
|
|
298 |
|
|
|
299 |
// Build the body of the email using user-entered information.
|
|
|
300 |
|
|
|
301 |
// Note: Name of message field is defined in the language pack.
|
|
|
302 |
$fieldmessage = get_string('field-message', 'local_contact');
|
|
|
303 |
|
|
|
304 |
$htmlmessage = '';
|
|
|
305 |
|
|
|
306 |
/**
|
|
|
307 |
* Callback function for array_filter.
|
|
|
308 |
*
|
|
|
309 |
* @param string $string Text to be chekced.
|
|
|
310 |
* @return boolean true if string is not empty, otherwise false.
|
|
|
311 |
*/
|
|
|
312 |
function filterempty($string) {
|
|
|
313 |
$string = trim($string ?? '');
|
|
|
314 |
return ($string !== null && $string !== false && $string !== '');
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
foreach ($_POST as $key => $value) {
|
|
|
318 |
// Only process key conforming to valid form field ID/Name token specifications.
|
|
|
319 |
if (preg_match('/^[A-Za-z][A-Za-z0-9_:\.-]*/', $key)) {
|
|
|
320 |
if (is_array($value)) {
|
|
|
321 |
// Join array of values. Example: <select multiple>.
|
|
|
322 |
$value = array_filter($value, "filterempty");
|
|
|
323 |
$value = join(', ', $value);
|
|
|
324 |
}
|
|
|
325 |
$value = (!empty($value) ? trim($value) : '');
|
|
|
326 |
// Exclude fields we don't want in the message and empty fields.
|
|
|
327 |
if (!in_array($key, ['sesskey', 'submit']) && $value != '') {
|
|
|
328 |
// Apply minor formatting of the key by replacing underscores with spaces.
|
|
|
329 |
$key = str_replace('_', ' ', $key);
|
|
|
330 |
// Make custom alterations.
|
|
|
331 |
switch ($key) {
|
|
|
332 |
case 'message':
|
|
|
333 |
// Message field - use translated value from language file.
|
|
|
334 |
$key = $fieldmessage;
|
|
|
335 |
// Continue checking for more issues to fix.
|
|
|
336 |
case strpos($value, "\n") !== false:
|
|
|
337 |
// Field contains linefeeds.
|
|
|
338 |
case $fieldmessage: // Message field.
|
|
|
339 |
// Strip out excessive empty lines.
|
|
|
340 |
$value = preg_replace('/\n(\s*\n){2,}/', "\n\n", $value);
|
|
|
341 |
// Sanitize the text.
|
|
|
342 |
$value = format_text($value, FORMAT_PLAIN, ['trusted' => false]);
|
|
|
343 |
// Add to email message.
|
|
|
344 |
$htmlmessage .= '<p><strong>' . ucfirst($key) . ' :</strong></p><p>' . $value . '</p>';
|
|
|
345 |
break;
|
|
|
346 |
// Don't include the following fields in the body of the message.
|
|
|
347 |
case 'recipient':
|
|
|
348 |
// Recipient field.
|
|
|
349 |
case 'recaptcha challenge field':
|
|
|
350 |
// ReCAPTCHA related field.
|
|
|
351 |
case 'recaptcha response field':
|
|
|
352 |
// ReCAPTCHA related field.
|
|
|
353 |
case 'g-recaptcha-response':
|
|
|
354 |
// ReCAPTCHA related field.
|
|
|
355 |
break;
|
|
|
356 |
// Use language translations for the labels of the following fields.
|
|
|
357 |
case 'name':
|
|
|
358 |
// Name field.
|
|
|
359 |
case 'email':
|
|
|
360 |
// Email field.
|
|
|
361 |
case 'subject':
|
|
|
362 |
// Subject field.
|
|
|
363 |
$key = get_string('field-' . $key, 'local_contact');
|
|
|
364 |
// Continue processing.
|
|
|
365 |
default:
|
|
|
366 |
// All other fields.
|
|
|
367 |
// Sanitize the text.
|
|
|
368 |
$value = format_text($value, FORMAT_PLAIN, ['trusted' => false]);
|
|
|
369 |
if (filter_var($value, FILTER_VALIDATE_URL)) {
|
|
|
370 |
// Convert URL into clickable link.
|
|
|
371 |
$value = '<a href="' . $value . '">' . $value . '</a>';
|
|
|
372 |
}
|
|
|
373 |
// Add to email message.
|
|
|
374 |
$htmlmessage .= '<strong>' . ucfirst($key) . ' :</strong> ' . $value . '<br>' . PHP_EOL;
|
|
|
375 |
}
|
|
|
376 |
}
|
|
|
377 |
}
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
$attachname = '';
|
|
|
381 |
$attachpath = '';
|
|
|
382 |
// If support for an attachement is enabled.
|
|
|
383 |
$supportattachments = !empty(get_config('local_contact', 'attachment'));
|
|
|
384 |
if ($supportattachments) {
|
|
|
385 |
// Take the first file as the attachment.
|
|
|
386 |
foreach ($_FILES as $value) {
|
|
|
387 |
$attachname = $value['name'];
|
|
|
388 |
$path = $CFG->tempdir . '/local_contact/';
|
|
|
389 |
if (!is_dir($path)) {
|
|
|
390 |
mkdir($path); // Create temp directory if it does not exist.
|
|
|
391 |
}
|
|
|
392 |
$attachpath = tempnam($path, 'attachment_');
|
|
|
393 |
move_uploaded_file($value['tmp_name'], $attachpath);
|
|
|
394 |
break;
|
|
|
395 |
}
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
// Sanitize user agent and referer.
|
|
|
399 |
$httpuseragent = format_text($_SERVER['HTTP_USER_AGENT'], FORMAT_PLAIN, ['trusted' => false]);
|
|
|
400 |
$httpreferer = format_text($_SERVER['HTTP_REFERER'], FORMAT_PLAIN, ['trusted' => false]);
|
|
|
401 |
|
|
|
402 |
// Prepare arrays to handle substitution of embedded tags in the footer.
|
|
|
403 |
$tags = [
|
|
|
404 |
'[fromname]',
|
|
|
405 |
'[fromemail]',
|
|
|
406 |
'[supportname]',
|
|
|
407 |
'[supportemail]',
|
|
|
408 |
'[lang]',
|
|
|
409 |
'[userip]',
|
|
|
410 |
'[userstatus]',
|
|
|
411 |
'[sitefullname]',
|
|
|
412 |
'[siteshortname]',
|
|
|
413 |
'[siteurl]',
|
|
|
414 |
'[http_user_agent]',
|
|
|
415 |
'[http_referer]',
|
|
|
416 |
];
|
|
|
417 |
$info = [
|
|
|
418 |
$from->firstname,
|
|
|
419 |
$from->email,
|
|
|
420 |
$CFG->supportname,
|
|
|
421 |
$CFG->supportemail,
|
|
|
422 |
current_language(),
|
|
|
423 |
getremoteaddr(),
|
|
|
424 |
$this->moodleuserstatus($from->email),
|
|
|
425 |
format_text($SITE->fullname, FORMAT_HTML, ['context' => $systemcontext, 'escape' => false]) . ': ',
|
|
|
426 |
format_text($SITE->shortname, FORMAT_HTML, ['context' => $systemcontext, 'escape' => false]),
|
|
|
427 |
$CFG->wwwroot,
|
|
|
428 |
$httpuseragent,
|
|
|
429 |
$httpreferer,
|
|
|
430 |
];
|
|
|
431 |
|
|
|
432 |
// Create the footer - Add some system information.
|
|
|
433 |
$footmessage = get_string('extrainfo', 'local_contact');
|
|
|
434 |
$footmessage = format_text($footmessage, FORMAT_HTML, ['trusted' => true, 'noclean' => true, 'para' => false]);
|
|
|
435 |
$htmlmessage .= str_replace($tags, $info, $footmessage);
|
|
|
436 |
|
|
|
437 |
// Override "from" email address if one was specified in the plugin's settings.
|
|
|
438 |
$noreplyaddress = $CFG->noreplyaddress;
|
|
|
439 |
if (!empty($customfrom = get_config('local_contact', 'senderaddress'))) {
|
|
|
440 |
$CFG->noreplyaddress = $customfrom;
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
// Send email message to recipient and set replyto to the sender's email address and name.
|
|
|
444 |
if (empty(get_config('local_contact', 'noreplyto'))) { // Not checked.
|
|
|
445 |
$status = email_to_user(
|
|
|
446 |
$to,
|
|
|
447 |
$from,
|
|
|
448 |
$subject,
|
|
|
449 |
html_to_text($htmlmessage),
|
|
|
450 |
$htmlmessage,
|
|
|
451 |
$attachpath,
|
|
|
452 |
$attachname,
|
|
|
453 |
true,
|
|
|
454 |
$from->email,
|
|
|
455 |
$from->firstname
|
|
|
456 |
);
|
|
|
457 |
} else { // Checked.
|
|
|
458 |
$status = email_to_user($to, $from, $subject, html_to_text($htmlmessage), $htmlmessage, $attachpath, $attachname, true);
|
|
|
459 |
}
|
|
|
460 |
$CFG->noreplyaddress = $noreplyaddress;
|
|
|
461 |
|
|
|
462 |
// If successful and a confirmation email is desired, send it the original sender.
|
|
|
463 |
if ($status && $sendconfirmationemail) {
|
|
|
464 |
// Substitute embedded tags for some information.
|
|
|
465 |
$htmlmessage = str_replace($tags, $info, get_string('confirmationemail', 'local_contact'));
|
|
|
466 |
$htmlmessage = format_text($htmlmessage, FORMAT_HTML, ['trusted' => true, 'noclean' => true, 'para' => false]);
|
|
|
467 |
|
|
|
468 |
$replyname = empty($CFG->emailonlyfromnoreplyaddress) ? $CFG->supportname : get_string('noreplyname');
|
|
|
469 |
$replyemail = empty($CFG->emailonlyfromnoreplyaddress) ? $CFG->supportemail : $CFG->noreplyaddress;
|
|
|
470 |
$to = $this->makeemailuser($replyemail, $replyname);
|
|
|
471 |
|
|
|
472 |
// Send confirmation email message to the sender.
|
|
|
473 |
email_to_user($from, $to, $subject, html_to_text($htmlmessage), $htmlmessage, '', '', true);
|
|
|
474 |
}
|
|
|
475 |
return $status;
|
|
|
476 |
}
|
|
|
477 |
|
|
|
478 |
/**
|
|
|
479 |
* Builds a one line status report on the user. Uses their Moodle info, if
|
|
|
480 |
* logged in, or their email address to look up the information if they are
|
|
|
481 |
* not.
|
|
|
482 |
*
|
|
|
483 |
* @param string $emailaddress Plain text email address.
|
|
|
484 |
*
|
|
|
485 |
* @return string Contains what we know about the Moodle user including whether they are logged in or out.
|
|
|
486 |
*/
|
|
|
487 |
private function moodleuserstatus($emailaddress) {
|
|
|
488 |
if (isloggedin() && !isguestuser()) {
|
|
|
489 |
global $USER;
|
|
|
490 |
$info = get_string('fullnamedisplay', null, $USER) . ' / ' . $USER->email . ' (' . $USER->username .
|
|
|
491 |
' / ' . get_string('eventuserloggedin', 'auth') . ')';
|
|
|
492 |
} else {
|
|
|
493 |
global $DB;
|
|
|
494 |
$usercount = $DB->count_records('user', ['email' => $emailaddress, 'deleted' => 0]);
|
|
|
495 |
switch ($usercount) {
|
|
|
496 |
case 0: // We don't know this email address.
|
|
|
497 |
$info = get_string('emailnotfound');
|
|
|
498 |
break;
|
|
|
499 |
case 1: // We found exactly one match.
|
|
|
500 |
$user = get_complete_user_data('email', $emailaddress);
|
|
|
501 |
$extrainfo = '';
|
|
|
502 |
|
|
|
503 |
// Is user locked out?
|
|
|
504 |
if ($lockedout = get_user_preferences('login_lockout', 0, $user)) {
|
|
|
505 |
$extrainfo .= ' / ' . get_string('lockedout', 'local_contact');
|
|
|
506 |
}
|
|
|
507 |
|
|
|
508 |
// Has user responded to confirmation email?
|
|
|
509 |
if (empty($user->confirmed)) {
|
|
|
510 |
$extrainfo .= ' / ' . get_string('notconfirmed', 'local_contact');
|
|
|
511 |
}
|
|
|
512 |
|
|
|
513 |
$info = get_string('fullnamedisplay', null, $user) . ' / ' . $user->email . ' (' . $user->username .
|
|
|
514 |
' / ' . get_string('eventuserloggedout') . $extrainfo . ')';
|
|
|
515 |
break;
|
|
|
516 |
default: // We found multiple users with this email address.
|
|
|
517 |
$info = get_string('duplicateemailaddresses', 'local_contact');
|
|
|
518 |
}
|
|
|
519 |
}
|
|
|
520 |
return $info;
|
|
|
521 |
}
|
|
|
522 |
}
|