-
Notifications
You must be signed in to change notification settings - Fork 1
/
maillog.module
317 lines (280 loc) · 8.89 KB
/
maillog.module
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
<?php
/**
* @file
* Primary hook implementations for the Maillog module.
*/
/**
* Extensibility of maillog engines is based on the mimemail engine hooks.
*
* See mimemail_get_engines in mimemail.module with
* Please install mimemail to make the outgoing engine pluggable.
*
* Mail flow:
* * backdrop_mail -> maillog:backdrop_mail_wrapper -> maillog_mail_send
* * mimemail -> maillog_mailengine -> maillog_mail_send [-> ANY_engine]
*/
/**
* Implements hook_config_info().
*/
function maillog_config_info() {
$prefixes['maillog.settings'] = array(
'label' => t('Maillog settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_autoload_info().
*/
function maillog_autoload_info() {
return array(
'MaillogMailSystem' => 'includes/maillog.mail.inc',
'MaillogHandlerFieldMaillogLinkDelete' => 'includes/MaillogHandlerFieldMaillogLinkDelete.php',
);
}
/**
* Implements hook_permision().
*/
function maillog_permission() {
return array(
'view maillog' => array(
'title' => t('View Maillog'),
'description' => t('Allow users to view a list of recently logged mails.'),
'restrict access' => TRUE,
),
'delete maillog' => array(
'title' => t('Delete Entries from the log'),
'description' => t('Allow users to delete logged mails.'),
'restrict access' => TRUE,
),
'administer maillog' => array(
'title' => t('Administer Maillog'),
'description' => t('Allow users to change maillog seetings.'),
'restrict access' => TRUE,
),
);
}
/**
* Implements hook_menu().
*/
function maillog_menu() {
$items = array();
$items['admin/config/development/maillog'] = array(
'title' => t('Maillog settings'),
'description' => t('Configure the settings of Maillog module.'),
'page callback' => 'backdrop_get_form',
'page arguments' => array('maillog_admin_settings'),
'access arguments' => array('administer maillog'),
'type' => MENU_NORMAL_ITEM,
'file' => 'maillog.admin.inc',
);
$items['admin/reports/maillog/view/%maillog_maillog'] = array(
'title callback' => 'maillog_maillog_title',
'title arguments' => array(4),
'description' => 'View a specific Maillog record.',
'page callback' => 'maillog_maillog_page',
'page arguments' => array(4),
'access arguments' => array('view maillog'),
'type' => MENU_CALLBACK,
'file' => 'maillog.pages.inc',
);
$items['admin/reports/maillog/delete/%maillog_maillog'] = array(
'title' => 'Delete Maillog record',
'description' => 'Delete a specific Maillog record.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('maillog_maillog_delete_form', 4),
'access arguments' => array('delete maillog'),
'type' => MENU_CALLBACK,
'file' => 'maillog.pages.inc',
);
return $items;
}
/**
* Title callback for a maillog record.
*
* @param array $maillog
* A maillog record that is to be deleted with an 'id' value.
*
* @return string
* The title of a specific message.
*/
function maillog_maillog_title(array $maillog) {
return $maillog['subject'];
}
/**
* Load a maillog record.
*
* @param int $id
* The primary ID for a Maillog record.
*
* @return null|array
* An array containing the {maillog} record if found, NULL if not.
*/
function maillog_maillog_load($id) {
$result = db_query("SELECT id, header_from, header_to, header_reply_to, header_all, subject, body FROM {maillog} WHERE id=:id", array(
':id' => $id,
));
if ($result == FALSE) {
$maillog = NULL;
}
else {
$maillog = $result->fetchAssoc();
// Unserialize values.
$maillog['header_all'] = unserialize($maillog['header_all']);
}
return $maillog;
}
/**
* Delete a specific maillog record.
*
* @param int $id
* The primary ID for a maillog record.
*
* @return DatabaseStatementInterface
* The response from db_query().
*/
function maillog_maillog_delete($id) {
return db_query("DELETE FROM {maillog} WHERE id = :id", array(':id' => $id));
}
/**
* Implements hook_views_api().
*/
function maillog_views_api() {
return array(
'api' => 3,
'path' => backdrop_get_path('module', 'maillog') . '/includes/',
);
}
/**
* Implements hook_theme().
*/
function maillog_theme() {
return array(
'maillog_header_from' => array(
'variables' => array('header_from' => NULL),
),
'maillog_header_to' => array(
'variables' => array('header_to' => NULL),
),
'maillog_header_reply_to' => array(
'variables' => array('header_reply_to' => NULL),
),
'maillog_header_all' => array(
'variables' => array('header_all' => NULL),
),
'maillog_body' => array(
'variables' => array('body' => NULL),
),
'maillog' => array(
'variables' => array('maillog' => NULL),
),
);
}
/**
* Render a maillog record.
*/
function theme_maillog($variables) {
$output = theme('maillog_header_from', array('header_from' => $variables['maillog']['header_from']));
$output .= theme('maillog_header_to', array('header_to' => $variables['maillog']['header_to']));
$output .= theme('maillog_header_reply_to', array('header_reply_to' => $variables['maillog']['header_reply_to']));
$output .= theme('maillog_header_all', array('header_all' => $variables['maillog']['header_all']));
$output .= theme('maillog_body', array('body' => $variables['maillog']['body']));
return $output;
}
/**
* Render the 'From' field in the node type 'Logged Mail'.
*/
function theme_maillog_header_from($variables) {
$output = '<div class="field mail-log-header-from">';
$output .= '<div class="field-label">' . t('From') . ":</div>\n";
$output .= '<div class="field-item">' . check_plain($variables['header_from']) . "</div>\n";
$output .= "</div>\n";
return $output;
}
/**
* Render the 'To' field in the node type 'Logged Mail'.
*/
function theme_maillog_header_to($variables) {
$output = '<div class="field mail-log-header-to">' . "\n";
$output .= '<div class="field-label">' . t('To') . ":</div>\n";
$output .= '<div class="field-item">' . check_plain($variables['header_to']) . "</div>\n";
$output .= "</div>\n";
return $output;
}
/**
* Render the 'Reply-To' field in the node type 'Logged Mail'.
*/
function theme_maillog_header_reply_to($variables) {
$output = '<div class="field mail-log-header-reply-to">' . "\n";
$output .= ' <div class="field-label">' . t('Reply To') . ":</div>\n";
$output .= ' <div class="field-item">' . check_plain($variables['header_reply_to']) . "</div>\n";
$output .= "</div>\n";
return $output;
}
/**
* Render the 'Header' field in the node type 'Logged Mail'.
*/
function theme_maillog_header_all($variables) {
$output = '<div class="field mail-log-header-all">' . "\n";
$output .= ' <div class="field-label">' . t('Header') . ":</div>\n";
$output .= ' <div class="field-item">' . "\n";
foreach ($variables['header_all'] as $header_all_name => $header_all_value) {
$output .= ' <div class="mail-log-header-all-subitem">';
$output .= check_plain($header_all_name) . ': ' . check_plain($header_all_value);
$output .= "</div>\n";
}
$output .= " </div>\n";
$output .= "</div>\n";
return $output;
}
/**
* Render the 'Body' field in the node type 'Logged Mail'.
*/
function theme_maillog_body($variables) {
$output = '<div class="field mail-log-body">' . "\n";
$output .= ' <div class="field-label">' . t('Body') . ":</div>\n";
$output .= ' <div class="field-item">' . "\n";
$output .= " <pre>\n";
$output .= check_plain($variables['body']);
$output .= " </pre>\n";
$output .= " </div>\n";
$output .= "</div>\n";
return $output;
}
/**
* Implements hook_cron().
*
* Controls the size of the maillog table.
*/
function maillog_cron() {
// Cleanup the maillog table. First check if the row limit is set.
$row_limit = config_get('maillog.settings', 'maillog_row_limit');
// For row limit n, get the wid of the nth row in descending wid order.
// Counting the most recent n rows avoids issues with wid number sequences,
// e.g. auto_increment value > 1 or rows deleted directly from the table.
if ($row_limit > 0) {
$min_row = db_select('maillog', 'm')
->fields('m', array('id'))
->orderBy('id', 'DESC')
->range($row_limit - 1, 1)
->execute()->fetchField();
// Delete all table entries older than the nth row, if nth row was found.
if ($min_row) {
db_delete('maillog')
->condition('id', $min_row, '<')
->execute();
}
}
// Next check duration.
$duration = config_get('maillog.settings', 'maillog_delete_time');
if (!empty($duration) && $duration > 0) {
// Get cut off time.
$delete_time = time() - $duration;
// Delete all that are older than cut off time.
$query = db_delete('maillog')
->condition('sent_date', $delete_time, '<');
$query->execute();
watchdog('mailog', t('Deleted Mailog logs older than !time'), array('!time' => date('r', $delete_time)));
}
return;
}