-
Notifications
You must be signed in to change notification settings - Fork 1
/
notify.php
181 lines (152 loc) · 4.66 KB
/
notify.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
<?php
use dokuwiki\Extension\AuthPlugin;
use dokuwiki\plugin\structpublish\meta\Assignments;
use dokuwiki\plugin\structpublish\meta\Constants;
use dokuwiki\plugin\structpublish\meta\Revision;
/**
* Notification helper
*/
class helper_plugin_structpublish_notify extends DokuWiki_Plugin
{
/** @var helper_plugin_structpublish_db */
protected $dbHelper;
public function __construct()
{
$this->dbHelper = plugin_load('helper', 'structpublish_db');
}
/**
* If activated, send emails on configured status changes.
*
* @param string $action
* @param Revision $newRevision
* @return void
* @throws Exception
*/
public function sendEmails($action, $newRevision)
{
if (!$this->triggerNotification($action)) {
return;
}
// get assignees from DB
$assignments = Assignments::getInstance();
$assignees = $assignments->getPageAssignments($newRevision->getId(), false);
// get recipients for the next workflow step
$nextAction = Constants::workflowSteps($action)['nextAction'];
if (is_null($nextAction)) {
return;
}
if (empty($assignees[$nextAction])) {
msg($this->getLang('email_error_norecipients'), -1);
return;
}
// flatten the array and split into single user or group items
$assignees = implode(',', array_values($assignees[$nextAction]));
$assignees = explode(',', $assignees);
// get recipient emails
$recipients = $this->resolveRecipients($assignees);
// prepare mail text
$mailText = $this->prepareMailText($newRevision->getStatus());
$this->sendMail(implode(',', $recipients), $mailText);
}
/**
* @param string $recipients Comma separated list of emails
* @param string $mailText
* @return void
*/
public function sendMail($recipients, $mailText)
{
$mailer = new Mailer();
$mailer->bcc($recipients);
$subject = $this->getLang('email_subject');
$mailer->subject($subject);
$mailer->setBody($mailText);
$mailer->send();
}
/**
* Processes an array of (comma separated) recipients
* and returns an array of emails
* with user groups resolved to individual users
*
* @param array $recipients
* @return array
* @throws Exception
*/
public function resolveRecipients($recipients)
{
$resolved = [];
$recipients = array_unique($recipients);
foreach ($recipients as $recipient) {
$recipient = trim($recipient);
if ($recipient[0] === '@') {
$this->resolveGroup($resolved, $recipient);
} elseif (strpos($recipient, '@') === false) {
$this->resolveUser($resolved, $recipient);
} else {
$resolved[] = $recipient;
}
}
return $resolved;
}
/**
* @param array $resolved
* @param string $recipient
* @return void
* @throws Exception
*/
protected function resolveGroup(&$resolved, $recipient)
{
/** @var AuthPlugin $auth */
global $auth;
if (!$auth->canDo('getUsers')) {
throw new \Exception('Auth cannot fetch users by group.');
}
$users = $auth->retrieveUsers(0, 0, ['grps' => substr($recipient, 1)]);
foreach ($users as $user) {
$resolved[] = $user['mail'];
}
}
/**
* @param array $resolved
* @param string $recipient
* @return void
*/
protected function resolveUser(&$resolved, $recipient)
{
/** @var AuthPlugin $auth */
global $auth;
$user = $auth->getUserData($recipient);
if ($user) {
$resolved[] = $user['mail'];
}
}
/**
* Check configuration to see if a notification should be triggered.
*
* @return bool
*/
private function triggerNotification($action)
{
if (!$this->getConf('email_enable')) {
return false;
}
$actions = array_map('trim', explode(',', $this->getConf('email_status')));
return in_array($action, $actions);
}
/**
* @return string
*/
protected function prepareMailText($status)
{
global $ID;
$mailtext = file_get_contents($this->localFN('mail'));
$vars = [
'PAGE' => $ID,
'URL' => wl($ID, '', true),
'STATUS_CURRENT' => $status,
];
foreach ($vars as $var => $val) {
$mailtext = str_replace('@' . $var . '@', $val, $mailtext);
}
return $mailtext;
}
}