forked from fisharebest/webtrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.php
283 lines (249 loc) · 9.52 KB
/
message.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
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2017 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Fisharebest\Webtrees;
use Fisharebest\Webtrees\Controller\PageController;
use Symfony\Component\HttpFoundation\Request;
/** @global Tree $WT_TREE */
global $WT_TREE;
require 'includes/session.php';
$controller = new PageController;
$controller->setPageTitle(I18N::translate('webtrees message'));
// Send the message.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$to = Filter::post('to', null, '');
$from_name = Filter::post('from_name', null, '');
$from_email = Filter::post('from_email');
$subject = Filter::post('subject', null, '');
$body = Filter::post('body', null, '');
$url = Filter::postUrl('url', 'index.php');
// Only an administration can use the distribution lists.
$controller->restrictAccess(!in_array($to, ['all', 'never_logged', 'last_6mo']) || Auth::isAdmin());
$recipients = recipients($to);
// Different validation for admin/user/visitor.
$errors = !Filter::checkCsrf();
if (Auth::check()) {
$from_name = Auth::user()->getRealName();
$from_email = Auth::user()->getEmail();
} elseif ($from_name === '' || $from_email === '') {
$errors = true;
} elseif (!preg_match('/@(.+)/', $from_email, $match) || function_exists('checkdnsrr') && !checkdnsrr($match[1])) {
FlashMessages::addMessage(I18N::translate('Please enter a valid email address.'), 'danger');
$errors = true;
} elseif (preg_match('/(?!' . preg_quote(WT_BASE_URL, '/') . ')(((?:ftp|http|https):\/\/)[a-zA-Z0-9.-]+)/', $subject . $body, $match)) {
FlashMessages::addMessage(I18N::translate('You are not allowed to send messages that contain external links.') . ' ' . /* I18N: e.g. ‘You should delete the “http://” from “http://www.example.com” and try again.’ */ I18N::translate('You should delete the “%1$s” from “%2$s” and try again.', $match[2], $match[1]), 'danger');
$errors = true;
} elseif (empty($recipients)) {
$errors = true;
}
if ($errors) {
// Errors? Go back to the form.
header(
'Location: message.php' .
'?to=' . rawurlencode($to) .
'&from_name=' . rawurlencode($from_name) .
'&from_email=' . rawurlencode($from_email) .
'&subject=' . rawurlencode($subject) .
'&body=' . rawurlencode($body) .
'&url=' . rawurlencode($url)
);
} else {
// No errors. Send the message.
foreach ($recipients as $recipient) {
if (deliverMessage($WT_TREE, $from_email, $from_name, $recipient, $subject, $body, $url)) {
FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', Html::escape($to)), 'info');
} else {
FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger');
Log::addErrorLog('Unable to send a message. FROM:' . $from_email . ' TO:' . $recipient->getEmail());
}
}
header('Location: ' . $url);
}
return;
}
$to = Filter::get('to', null, '');
$from_name = Filter::get('from_name', null, '');
$from_email = Filter::get('from_email', '');
$subject = Filter::get('subject', null, '');
$body = Filter::get('body', null, '');
$url = Filter::getUrl('url', 'index.php');
// Only an administration can use the distribution lists.
$controller->restrictAccess(!in_array($to, ['all', 'never_logged', 'last_6mo']) || Auth::isAdmin());
$controller->pageHeader();
$to_names = implode(I18N::$list_separator, array_map(function(User $user) {
return $user->getRealName();
}, recipients($to)));
?>
<h2><?= I18N::translate('Send a message') ?></h2>
<form method="post">
<?= Filter::getCsrf() ?>
<input type="hidden" name="url" value="<?= Html::escape($url) ?>">
<div class="form-group row">
<div class="col-sm-3 col-form-label">
<?= I18N::translate('To') ?>
</div>
<div class="col-sm-9">
<input type="hidden" name="to" value="<?= Html::escape($to) ?>">
<div class="form-control"><?= Html::escape($to_names) ?></div>
</div>
</div>
<?php if (Auth::check()): ?>
<div class="form-group row">
<div class="col-sm-3 col-form-label">
<?= I18N::translate('From') ?>
</div>
<div class="col-sm-9">
<div class="form-control"><?= Html::escape(Auth::user()->getRealName()) ?></div>
</div>
</div>
<?php else: ?>
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="from-name">
<?= I18N::translate('Your name') ?>
</label>
<div class="col-sm-9">
<input class="form-control" id="from-name" type="text" name="from_name" value="<?= Html::escape($from_name) ?>" required>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="from-email">
<?= I18N::translate('Email address') ?>
</label>
<div class="col-sm-9">
<input class="form-control" id="from-email" type="text" name="from_email" value="<?= Html::escape($from_email) ?>" required>
</div>
</div>
<?php endif ?>
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="subject">
<?= I18N::translate('Subject') ?>
</label>
<div class="col-sm-9">
<input class="form-control" id="subject" type="text" name="subject" value="<?= Html::escape($subject) ?>" required>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="body">
<?= I18N::translate('Body') ?>
</label>
<div class="col-sm-9">
<textarea class="form-control" id="body" type="text" name="body" required><?= Html::escape($body) ?></textarea>
</div>
</div>
<div class="form-group row">
<div class="col-sm-9 push-sm-3">
<button type="submit" class="btn btn-primary">
<?= I18N::translate('Send') ?>
</button>
</div>
</div>
</form>
<?php
/**
* Convert a username (or mailing list name) into an array of recipients.
*
* @param $to
*
* @return User[]
*/
function recipients($to) {
if ($to === 'all') {
$recipients = User::all();
} elseif ($to === 'last_6mo') {
$recipients = array_filter(User::all(), function(User $user) {
return $user->getPreference('sessiontime') > 0 && WT_TIMESTAMP - $user->getPreference('sessiontime') > 60 * 60 * 24 * 30 * 6;
});
} elseif ($to === 'never_logged') {
$recipients = array_filter(User::all(), function(User $user) {
return $user->getPreference('verified_by_admin') && $user->getPreference('reg_timestamp') > $user->getPreference('sessiontime');
});
} else {
$recipients = array_filter([User::findByUserName($to)]);
}
return $recipients;
}
/**
* Add a message to a user's inbox, send it to them via email, or both.
*
* @param Tree $tree
* @param string $sender_name
* @param string $sender_email
* @param User $recipient
* @param string $subject
* @param string $body
* @param string $url
*
* @return bool
*/
function deliverMessage(Tree $tree, $sender_email, $sender_name, User $recipient, $subject, $body, $url) {
// Create a dummy user, so we can send messages from the tree.
$from = new User(
(object) [
'user_id' => null,
'user_name' => '',
'real_name' => $tree->getTitle(),
'email' => $tree->getPreference('WEBTREES_EMAIL'),
]
);
// Create a dummy user, so we can reply to visitors.
$sender = new User(
(object) [
'user_id' => null,
'user_name' => '',
'real_name' => $sender_name,
'email' => $sender_email,
]
);
$request = Request::createFromGlobals();
$success = true;
// Switch to the recipient's language
I18N::init($recipient->getPreference('language'));
// Send via the internal messaging system.
if (in_array($recipient->getPreference('contactmethod'), ['messaging', 'messaging2', 'mailto', 'none'])) {
Database::prepare("INSERT INTO `##message` (sender, ip_address, user_id, subject, body) VALUES (? ,? ,? ,? ,?)")
->execute([
Auth::check() ? Auth::user()->getEmail() : $sender_email,
$request->getClientIp(),
$recipient->getUserId(),
$subject,
View::make('emails/message-user-text', ['sender' => $sender, 'recipient' => $recipient, 'message' => $body, 'url' => $url]),
]);
}
// Send via email
if (in_array($recipient->getPreference('contactmethod'), ['messaging2', 'messaging3', 'mailto', 'none'])) {
$success = $success && Mail::send(
$from,
$recipient,
$sender,
I18N::translate('webtrees message') . ' - ' . $subject,
View::make('emails/message-user-text', ['sender' => $sender, 'recipient' => $recipient, 'message' => $body, 'url' => $url]),
View::make('emails/message-user-html', ['sender' => $sender, 'recipient' => $recipient, 'message' => $body, 'url' => $url])
);
}
I18N::init(WT_LOCALE);
// Copy the message back to the user
if (Auth::check() && in_array(Auth::user()->getPreference('contactmethod'), ['messaging', 'messaging2', 'mailto', 'none'])) {
Database::prepare(
"INSERT INTO `##message` (sender, ip_address, user_id, subject, body) VALUES (? ,? ,? ,? ,?)"
)->execute([
Auth::user()->getEmail(),
$request->getClientIp(),
$recipient->getUserId(),
$subject,
View::make('emails/message-copy-text', ['sender' => $sender, 'recipient' => $recipient, 'message' => $body, 'url' => $url])
]);
}
return $success;
}