-
Notifications
You must be signed in to change notification settings - Fork 105
/
notification.php
429 lines (371 loc) · 14.6 KB
/
notification.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
424
425
426
427
428
429
<?php
namespace Helper;
class Notification extends \Prefab
{
public const QPRINT_MAXL = 75;
/**
* Convert a 8 bit string to a quoted-printable string
*
* Modified to add =2E instead of the leading double dot, see GH #238
*
* @link http://php.net/manual/en/function.quoted-printable-encode.php#115840
*
* @param string $str
* @return string
*/
public function quotePrintEncode($str)
{
$lp = 0;
$ret = '';
$hex = "0123456789ABCDEF";
$length = strlen($str);
$str_index = 0;
while ($length--) {
if ((($c = $str[$str_index++]) == "\015") && ($str[$str_index] == "\012") && $length > 0) {
$ret .= "\015";
$ret .= $str[$str_index++];
$length--;
$lp = 0;
} else {
if (
ctype_cntrl($c)
|| (ord($c) == 0x7f)
|| (ord($c) & 0x80)
|| ($c == '=')
|| (($c == ' ') && ($str[$str_index] == "\015"))
) {
if (($lp += 3) > self::QPRINT_MAXL) {
$ret .= '=';
$ret .= "\015";
$ret .= "\012";
$lp = 3;
}
$ret .= '=';
$ret .= $hex[ord($c) >> 4];
$ret .= $hex[ord($c) & 0xf];
} else {
if ((++$lp) > self::QPRINT_MAXL) {
$ret .= '=';
$ret .= "\015";
$ret .= "\012";
$lp = 1;
}
$ret .= $c;
if ($lp == 1 && $c == '.') {
$ret = substr($ret, 0, strlen($ret) - 1);
$ret .= '=2E';
$lp++;
}
}
}
}
return $ret;
}
/**
* Send an email with the UTF-8 character set
* @param string $to
* @param string $subject
* @param string $body The HTML body part
* @param string $text The plaintext body part (optional)
* @return bool
*/
public function utf8mail($to, $subject, $body, $text = null)
{
$f3 = \Base::instance();
// Add basic headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: ' . $f3->get("mail.from") . "\r\n";
// Build multipart message if necessary
if ($text) {
// Generate message breaking hash
$hash = md5(date("r"));
$headers .= "Content-Type: multipart/alternative; boundary=\"$hash\"\r\n";
// Normalize line endings
$body = str_replace("\r\n", "\n", $body);
$body = str_replace("\n", "\r\n", $body);
$text = str_replace("\r\n", "\n", $text);
$text = str_replace("\n", "\r\n", $text);
// Encode content
$body = $this->quotePrintEncode($body);
$text = $this->quotePrintEncode($text);
// Build final message
$msg = "--$hash\r\n";
$msg .= "Content-Type: text/plain; charset=utf-8\r\n";
$msg .= "Content-Transfer-Encoding: quoted-printable\r\n";
$msg .= "\r\n" . $text . "\r\n";
$msg .= "--$hash\r\n";
$msg .= "Content-Type: text/html; charset=utf-8\r\n";
$msg .= "Content-Transfer-Encoding: quoted-printable\r\n";
$msg .= "\r\n" . $body . "\r\n";
$msg .= "--$hash--\r\n";
$body = $msg;
} else {
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
}
return mail($to, $subject, $body, $headers);
}
/**
* Send an email to watchers with the comment body
* @param int $issue_id
* @param int $comment_id
*/
public function issue_comment($issue_id, $comment_id)
{
$f3 = \Base::instance();
if ($f3->get("mail.from")) {
$log = new \Log("mail.log");
// Get issue and comment data
$issue = new \Model\Issue();
$issue->load($issue_id);
$comment = new \Model\Issue\Comment\Detail();
$comment->load($comment_id);
// Get issue parent if set
if ($issue->parent_id) {
$parent = new \Model\Issue();
$parent->load($issue->parent_id);
$f3->set("parent", $parent);
}
// Get recipient list and remove current user
$recipients = $this->_issue_watchers($issue_id);
$recipients = array_diff($recipients, [$comment->user_email]);
// Render message body
$f3->set("issue", $issue);
$f3->set("comment", $comment);
$f3->set("previewText", $comment->text);
$text = $this->_render("notification/comment.txt");
$body = $this->_render("notification/comment.html");
$subject = "[#{$issue->id}] - New comment on {$issue->name}";
// Send to recipients
foreach ($recipients as $recipient) {
$this->utf8mail($recipient, $subject, $body, $text);
$log->write("Sent comment notification to: " . $recipient);
}
}
}
/**
* Send an email to watchers detailing the updated fields
* @param int $issue_id
* @param int $update_id
*/
public function issue_update($issue_id, $update_id)
{
$f3 = \Base::instance();
if ($f3->get("mail.from")) {
$log = new \Log("mail.log");
// Get issue and update data
$issue = new \Model\Issue();
$issue->load($issue_id);
$f3->set("issue", $issue);
$update = new \Model\Custom("issue_update_detail");
$update->load($update_id);
// Get issue parent if set
if ($issue->parent_id) {
$parent = new \Model\Issue();
$parent->load($issue->parent_id);
$f3->set("parent", $parent);
}
// Avoid errors from bad calls
if (!$issue->id || !$update->id) {
return false;
}
$changes = new \Model\Issue\Update\Field();
$f3->set("changes", $changes->find(["issue_update_id = ?", $update->id]));
// Get recipient list and remove update user
$recipients = $this->_issue_watchers($issue_id);
$recipients = array_diff($recipients, [$update->user_email]);
// Render message body
$f3->set("issue", $issue);
$f3->set("update", $update);
$text = $this->_render("notification/update.txt");
$body = $this->_render("notification/update.html");
$changes->load(["issue_update_id = ? AND `field` = 'closed_date' AND old_value = '' and new_value != ''", $update->id]);
if ($changes && $changes->id) {
$subject = "[#{$issue->id}] - {$issue->name} closed";
} else {
$subject = "[#{$issue->id}] - {$issue->name} updated";
}
// Send to recipients
foreach ($recipients as $recipient) {
$this->utf8mail($recipient, $subject, $body, $text);
$log->write("Sent update notification to: " . $recipient);
}
}
}
/**
* Send an email to watchers detailing the updated fields
* @param int $issue_id
*/
public function issue_create($issue_id)
{
$f3 = \Base::instance();
$log = new \Log("mail.log");
if ($f3->get("mail.from")) {
$log = new \Log("mail.log");
// Get issue and update data
$issue = new \Model\Issue\Detail();
$issue->load($issue_id);
$f3->set("issue", $issue);
// Get issue parent if set
if ($issue->parent_id) {
$parent = new \Model\Issue();
$parent->load($issue->parent_id);
$f3->set("parent", $parent);
}
// Get recipient list, conditionally removing the author
$recipients = $this->_issue_watchers($issue_id);
$user = new \Model\User();
$user->load($issue->author_id);
if ($user->option('disable_self_notifications')) {
$recipients = array_diff($recipients, [$user->email]);
}
// Render message body
$f3->set("issue", $issue);
$text = $this->_render("notification/new.txt");
$body = $this->_render("notification/new.html");
$subject = "[#{$issue->id}] - {$issue->name} created by {$issue->author_name}";
// Send to recipients
foreach ($recipients as $recipient) {
$this->utf8mail($recipient, $subject, $body, $text);
$log->write("Sent create notification to: " . $recipient);
}
}
}
/**
* Send an email to watchers with the file info
* @param int $issue_id
* @param int $file_id
*/
public function issue_file($issue_id, $file_id)
{
$f3 = \Base::instance();
if ($f3->get("mail.from")) {
$log = new \Log("mail.log");
// Get issue and comment data
$issue = new \Model\Issue();
$issue->load($issue_id);
$file = new \Model\Issue\File\Detail();
$file->load($file_id);
// This should catch a bug I can't currently find the source of. --Alan
if ($file->issue_id != $issue->id) {
return;
}
// Get issue parent if set
if ($issue->parent_id) {
$parent = new \Model\Issue();
$parent->load($issue->parent_id);
$f3->set("parent", $parent);
}
// Get recipient list and remove current user
$recipients = $this->_issue_watchers($issue_id);
$recipients = array_diff($recipients, [$file->user_email]);
// Render message body
$f3->set("issue", $issue);
$f3->set("file", $file);
$f3->set("previewText", $file->filename);
$text = $this->_render("notification/file.txt");
$body = $this->_render("notification/file.html");
$subject = "[#{$issue->id}] - {$file->user_name} attached a file to {$issue->name}";
// Send to recipients
foreach ($recipients as $recipient) {
$this->utf8mail($recipient, $subject, $body, $text);
$log->write("Sent file notification to: " . $recipient);
}
}
}
/**
* Send a user a password reset email
* @param int $user_id
* @param string $token
*/
public function user_reset($user_id, $token)
{
$f3 = \Base::instance();
if ($f3->get("mail.from")) {
$user = new \Model\User();
$user->load($user_id);
if (!$user->id) {
throw new \Exception("User does not exist.");
}
// Render message body
$f3->set("token", $token);
$text = $this->_render("notification/user_reset.txt");
$body = $this->_render("notification/user_reset.html");
// Send email to user
$subject = "Reset your password - " . $f3->get("site.name");
$this->utf8mail($user->email, $subject, $body, $text);
}
}
/**
* Send a user an email listing the issues due today and any overdue issues
* @param \Model\User $user
* @param array $due
* @param array $overdue
* @return bool
*/
public function user_due_issues(\Model\User $user, array $due, array $overdue)
{
$f3 = \Base::instance();
if ($f3->get("mail.from")) {
$f3->set("due", $due);
$f3->set("overdue", $overdue);
$preview = count($due) . " issues due today";
if ($overdue) {
$preview .= ", " . count($overdue) . " overdue issues";
}
$f3->set("previewText", $preview);
$subject = "Due Today - " . $f3->get("site.name");
$text = $this->_render("notification/user_due_issues.txt");
$body = $this->_render("notification/user_due_issues.html");
return $this->utf8mail($user->email, $subject, $body, $text);
}
return false;
}
/**
* Get array of email addresses of all watchers on an issue
* @param int $issue_id
* @return array
*/
protected function _issue_watchers($issue_id)
{
$db = \Base::instance()->get("db.instance");
$recipients = [];
// Add issue author and owner
$result = $db->exec("SELECT u.email FROM issue i INNER JOIN `user` u on i.author_id = u.id WHERE u.deleted_date IS NULL AND i.id = ?", $issue_id);
if (!empty($result[0]["email"])) {
$recipients[] = $result[0]["email"];
}
$result = $db->exec("SELECT u.email FROM issue i INNER JOIN `user` u on i.owner_id = u.id WHERE u.deleted_date IS NULL AND i.id = ?", $issue_id);
if (!empty($result[0]["email"])) {
$recipients[] = $result[0]["email"];
}
// Add whole group
$result = $db->exec("SELECT u.role, u.id FROM issue i INNER JOIN `user` u on i.owner_id = u.id WHERE u.deleted_date IS NULL AND i.id = ?", $issue_id);
if ($result && $result[0]["role"] == 'group') {
$group_users = $db->exec("SELECT g.user_email FROM user_group_user g WHERE g.deleted_date IS NULL AND g.group_id = ?", $result[0]["id"]);
foreach ($group_users as $group_user) {
if (!empty($group_user["user_email"])) {
$recipients[] = $group_user["user_email"];
}
}
}
// Add watchers
$watchers = $db->exec("SELECT u.email FROM issue_watcher w INNER JOIN `user` u ON w.user_id = u.id WHERE u.deleted_date IS NULL AND issue_id = ?", $issue_id);
foreach ($watchers as $watcher) {
$recipients[] = $watcher["email"];
}
// Remove duplicate users
return array_unique($recipients);
}
/**
* Render a view and return the result
* @param string $file
* @param string $mime
* @param array $hive
* @param integer $ttl
* @return string
*/
protected function _render($file, $mime = "text/html", array $hive = null, $ttl = 0)
{
return \Helper\View::instance()->render($file, $mime, $hive, $ttl);
}
}