This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
forked from pkp/emailLogger
/
EmailLoggerPlugin.inc.php
284 lines (244 loc) · 7.12 KB
/
EmailLoggerPlugin.inc.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
<?php
/**
* @file EmailLoggerPlugin.inc.php
*
* Copyright (c) 2013 Simon Fraser University Library
* Copyright (c) 2003-2013 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @package plugins.generic.emailLogger
* @class EmailLoggerPlugin
* @brief Plugin that will log all emails sent by the system.
* The main purpose is to be used by tests to verify whether emails were sent or not.
* When enabled, will prevent any email from going out.
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class EmailLoggerPlugin extends GenericPlugin {
/** @var $_notification PKPNotification */
private $_notification;
/** @var $emailLogEntryDao EmailLogDAO */
protected $emailLogEntryDao;
//
// Implement methods from PKPPlugin.
//
/**
* @copydoc LazyLoadPlugin::register()
*/
function register($category, $path) {
$success = parent::register($category, $path);
if ($success) {
if ($this->getEnabled()) {
HookRegistry::register('Mail::send', array($this, 'mailSendCallback'));
HookRegistry::register('PKPNotificationOperationManager::sendNotificationEmail', array($this, 'recordNotificationDetails'));
}
}
return $success;
}
/**
* @copydoc LazyLoadPlugin::getName()
*/
function getName() {
return 'emailloggerplugin';
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.emailLogger.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.emailLogger.description');
}
/**
* @copydoc Plugin::isSitePlugin()
*/
function isSitePlugin() {
return true;
}
/**
* PKPNotificationOperationManager::sendNotificationMail() callback
* to store notification details to be later used in log entry.
* @param $hookName string
* @args array
* @return boolean
*/
function recordNotificationDetails($hookName, $args) {
$notification = current($args);
$this->_notification = $notification;
return false;
}
/**
* Mail send callback that logs all emails sent by the system.
* @param $hookName string
* @param $args array
* @return boolean
*/
function mailSendCallback($hookName, $args) {
$mail = current($args);
$request = Application::getRequest();
// Don't send the email, avoid log entry duplication.
$returner = true;
if ($mail instanceOf SubmissionMailTemplate) {
$mail->log($request);
return $returner;
}
// This is an email that is not logged
// by default. We will use the plugin
// settings to record this log, so we make sure we
// don't mess up with the system's log and also that
// we can keep this plugin simple.
$log = $this->getSetting(CONTEXT_ID_NONE, 'emailLog');
if (!is_array($log)) $log = array();
$notificationType = null;
if ($this->_notification) {
$notificationType = $this->_notification->getType();
$this->_notification = null;
}
$log[] = array(
'dateSent' => time(),
'notification_type' => $notificationType,
'from' => $mail->getFrom(),
'recipients' => $mail->getRecipients(),
'subject' => $mail->getSubject(),
'body' => $mail->getBody()
);
$this->updateSetting(CONTEXT_ID_NONE, 'emailLog', $log, 'object');
return $returner;
}
//
// Public email log interface.
//
/**
* Check if a log entry exists.
* @param $notificationType int The type of the notification
* that's the origin of the email.
* @param $recipientEmail string
* @param $body string|null Part of the body string, if
* null, body will not be checked.
* @return boolean
*/
function exists($notificationType = null, $recipientEmail = null, $body = null) {
$pluginSettingsDao = DAORegistry::getDAO('PluginSettingsDAO');
// Force the cache object and file refresh.
$pluginSettingsDao->getPluginSettings(CONTEXT_ID_NONE, $this->getName());
$log = $this->getSetting(CONTEXT_ID_NONE, 'emailLog');
$returner = false;
if ($log) $returner = true;
if ($returner) {
foreach ($log as $entry) {
$returner = true; // Consider every entry as a possible match.
if ($returner && $notificationType) {
if ($entry['notification_type'] != $notificationType) {
$returner = false;
}
}
if ($returner && $recipientEmail) {
$recipientEmails = $this->_getRecipientEmails($entry['recipients']);
if (!in_array($recipientEmail, $recipientEmails)) {
$returner = false;
}
}
if ($returner && $body) {
if (strpos($entry['body'], $body) === false) {
$returner = false;
}
}
if ($returner) break; // Already found a matching entry.
}
}
return $returner;
}
/**
* Check the presence of an email log by assoc.
* @param $assocType int
* @param $assocId int
* @param $recipientEmail string
* @param $eventType int
* @param $body string|null Part of the body string, if
* null, body will not be checked.
* @return boolean
*/
function existsByAssoc($assocType, $assocId, $recipientEmail = null, $eventType = null, $body = null) {
switch ($assocType) {
case ASSOC_TYPE_SUBMISSION_FILE:
$this->emailLogEntryDao = DAORegistry::getDAO('MonographFileEmailLogDAO');
break;
case ASSOC_TYPE_SUBMISSION:
$this->emailLogEntryDao = DAORegistry::getDAO('SubmissionEmailLogDAO');
break;
default:
return false;
}
$entries = $this->getLogByAssoc($assocType, $assocId);
$returner = false;
if ($entries) $returner = true;
if ($entries) {
foreach ($entries as $entry) {
$returner = true;
if ($recipientEmail) {
$recipientEmails = $this->_getRecipientEmails($entry->getRecipients());
if (!in_array($recipientEmail, $recipientEmails)) {
$returner = false;
}
}
if ($eventType) {
if ($entry->getEventType() != $eventType) {
$returner = false;
}
}
if ($body) {
if (strpos($entry->getBody(), $body) === false) {
$returner = false;
}
}
if ($returner) break;
}
}
return $returner;
}
//
// Protected methods.
//
/**
* Get email log entries by assoc, using the current log entry DAO.
* If none is set, return empty array.
* @param $assocType int
* @param $assocId int
* @return array Filled with EmailLogEntry objects or empty.
*/
protected function getLogByAssoc($assocType, $assocId) {
$entries = array();
if ($this->emailLogEntryDao) {
$entryFactory = $this->emailLogEntryDao->getByAssoc($assocType, $assocId);
$entries = $entryFactory->toArray();
}
return $entries;
}
//
// Private helper methods.
//
/**
* Get all recipient emails from the
* passed recipient data.
* @param $recipients array
* @return array
*/
private function _getRecipientEmails($recipients) {
if (is_string($recipients)) {
// Email and user name are mixed in string.
import('lib.pkp.classes.mail.MailTemplate');
$mail = new Mail();
$recipientsList = array();
$recipients = array($recipients);
$recipients = $mail->processAddresses($recipientsList, $recipients);
}
$recipientEmails = array();
foreach ($recipients as $recipient) {
$recipientEmails[] = $recipient['email'];
}
return $recipientEmails;
}
}