-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkmail.module
533 lines (474 loc) · 19.1 KB
/
checkmail.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/**
* @file
* The main file for Checkmail.
*
* The Checkmail module displays statistical information about a user's mailbox.
*
* This module generates a page and/or a block, which displays statistical
* information about an e-mail inbox, including the number of messages and
* size of the mailbox.
*
* @author Stefan Nagtegaal http://drupal.org/user/612
* @author Kristjan Jansen http://drupal.org/user/11
* @author David Kent Norman http://drupal.org/user/972
* @author Jason Flatt http://drupal.org/user/4649
*
* @todo create proper theming functions.
*/
/**
* Implements hook_permission().
*/
function checkmail_permission() {
return array(
'administer checkmail' => array(
'title' => t('administer checkmail'),
'description' => t('Perform administration taks and change the configuration settings for this module.'),
),
'access checkmail' => array(
'title' => t('access checkmail'),
'description' => t('Allows one to access the block and page with the mailbox stats on them.'),
),
);
}
/**
* Implements hook_menu().
*/
function checkmail_menu() {
$items = array();
$system_admin_inc_path = '/' . backdrop_get_path('module', 'system') . '/system.admin.inc';
//dpm($system_admin_inc_path, '$system_admin_inc_path');
$items['admin/config/mail'] = array(
'title' => 'Mail',
'description' => 'Settings related to email and messaging.',
'position' => 'left',
'weight' => 0,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => $system_admin_inc_path,
);
// Administrative settings
$items['admin/config/mail/checkmail'] = array(
'title' => 'Checkmail',
'access arguments' => array('administer checkmail'),
'page callback' => 'drupal_get_form',
'page arguments' => array('checkmail_admin_settings'),
'description' => 'Configure the settings for the mailbox to check.',
'file' => 'checkmail.admin.inc',
);
// The page to show the stats.
$items['checkmail'] = array(
'title' => 'Check e-mail',
'access arguments' => array('access checkmail'),
'page callback' => 'checkmail_page',
'description' => 'Display the mailbox information.',
);
return $items;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Modifies the user registration page to add the login ID and password fields.
*/
function checkmail_form_user_register_form_alter(&$form, &$form_state, $form_id) {
// If the user is allowed to access Checkmail, and the administrator has
// chosen to collect the login ID and password, then add the fields to the
// form.
$config = config('checkmail.settings');
if (user_access('access checkmail') && $config->get('checkmail_account_info') === 'user_fields') {
_checkmail_form_user_form($form);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Modifies the user profile page to add the login ID and password fields.
*/
function checkmail_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
// If this is the user's account form, they are allowed to access Checkmail,
// and the administrator has chosen to collect the login ID and
// password, then add the fields to the form.
$config = config('checkmail.settings');
if ($form['#user_category'] == 'account' && user_access('access checkmail') && $config->get('checkmail_account_info') === 'user_fields') {
_checkmail_form_user_form($form);
}
}
/**
* The form parts to add to the user forms above.
*
* @param array $form
* The form to modify.
*/
function _checkmail_form_user_form(&$form) {
$description = '';
$config = config('checkmail.settings');
if ($config->get('checkmail_use_encryption') === 0) {
$description = t('NOTE: The password is stored in the database, and it is not encrypted.');
}
$form['checkmail_authentication_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Checkmail authentication settings'),
'#collapsible' => TRUE,
'#weight' => 1,
);
$form['checkmail_authentication_settings']['checkmail_username'] = array(
'#type' => 'textfield',
'#title' => t('E-mail account log in ID'),
'#default_value' => !empty($form['#user']->data['checkmail_username']) ? $form['#user']->data['checkmail_username'] : '',
'#description' => t('The login id for your e-mail account.'),
'#required' => TRUE,
);
$form['checkmail_authentication_settings']['checkmail_passsword'] = array(
'#type' => 'password_confirm',
'#description' => t('The password for your e-mail account.') . $description,
);
}
/**
* Implements hook_user_insert().
*/
function checkmail_user_insert(&$edit, $account, $category) {
$config = config('checkmail.settings');
if ($config->get('checkmail_account_info') === 'user_fields') {
$edit['data']['checkmail_username'] = $edit['checkmail_username'];
module_load_include('inc', 'checkmail', 'checkmail.common');
$edit['data']['checkmail_passsword'] = _checkmail_encrypt_password($edit['checkmail_passsword']);
}
}
/**
* Implements hook_user_presave().
*/
function checkmail_user_presave(&$edit, $account, $category) {
$config = config('checkmail.settings');
if ($category === 'account' && user_access('access checkmail') && $config->get('checkmail_account_info') === 'user_fields') {
// If the login ID has changed, but is not empty, save it.
if (!empty($edit['checkmail_username'])) {
if (empty($account->data['checkmail_username']) || ($account->data['checkmail_username'] != $edit['checkmail_username'])) {
$edit['data']['checkmail_username'] = $edit['checkmail_username'];
}
}
// Becuase it's empty by default when the form is shown, if there is
// anything in the password field, save it.
if (!empty($edit['checkmail_passsword'])) {
module_load_include('inc', 'checkmail', 'checkmail.common');
$edit['data']['checkmail_passsword'] = _checkmail_encrypt_password($edit['checkmail_passsword']);
}
}
}
/**
* Implements hook_block_info().
*/
function checkmail_block_info() {
if (user_access('administer checkmail')) {
$blocks = array();
$blocks['mailboxstats']['info'] = t('Check e-mail');
return $blocks;
}
}
/**
* Implements hook_block_configure().
*/
function checkmail_block_configure($delta) {
// @TODO: Add options for display of text & quick access to the e-mails?
if (user_access('administer checkmail')) {
$config = config('checkmail.settings');
$cache_options = array(
0 => 'no cache',
30 => t('30 seconds'),
60 => t('1 minute'),
300 => t('5 minutes'),
600 => t('10 minutes'),
900 => t('15 minutes'),
1800 => t('30 minutes'),
3600 => t('1 hour'),
21600 => t('6 hours'),
43200 => t('12 hours'),
86400 => t('1 day'),
);
$form['block_cache'] = array(
'#type' => 'select',
'#title' => t('Cache'),
'#default_value' => $config->get('checkmail_block_cache'),
'#options' => $cache_options,
'#description' => t('Cache the mail results for this amount of time before querying the server again.'),
'#required' => FALSE,
);
return $form;
}
}
/**
* Implements hook_block_save().
*/
function checkmail_block_save($delta, $edit) {
if (user_access('administer checkmail')) {
if ($delta == 'mailboxstats') {
$config = config('checkmail.settings');
$config->set('checkmail_block_subject', $edit['block_subject']);
$config->set('checkmail_block_cache', $edit['block_cache']);
$config->save();
}
}
}
/**
* Implements hook_block_view().
*/
function checkmail_block_view($delta) {
if (user_access('access checkmail')) {
$config = config('checkmail.settings');
$block = array();
switch ($delta) {
case 0:
$mail = array();
$auth_method = $config->get('checkmail_account_info');
if ($auth_method === 'user_fields' || $auth_method === 'user_account') {
global $user;
if (empty($user->data['checkmail_username'])) {
$block['content'] = t('You have not configured your account to check your mailbox statistics. You can do that by navigating to !myaccount.', array('!myaccount' => l(t('My account'), 'user')));
break;
}
else {
$mail['login_id'] = $user->data['checkmail_username'];
}
}
elseif ($auth_method == 'admin_fields') {
$checkmail_login_id = $config->get('checkmail_login_id');
if (empty($checkmail_login_id)) {
$block['content'] = t('The system administrator has not configured the account to check the mailbox statistics.');
break;
}
else {
$mail['login_id'] = $config->get('checkmail_login_id');
}
}
$time = REQUEST_TIME;
$cache = $config->get('checkmail_block_cache');
$last_check = $config->get('checkmail_checked_time');
if ((!$cache || $time == $last_check) || ($cache > 0 && $time > $last_check + $cache)) {
$mail = _checkmail();
}
else {
$mail['status'] = 'success';
$mail['messages'] = empty($config->get('checkmail_cache_num')) ? t('cache not set!') : $config->get('checkmail_cache_num');
$mail['size'] = empty($config->get('checkmail_cache_mem')) ? t('cache not set!') : $config->get('checkmail_cache_mem');
$mail['recent'] = empty($config->get('checkmail_cache_recent')) ? t('cache not set!') : $config->get('checkmail_cache_recent');
$mail['unread'] = empty($config->get('checkmail_cache_unread')) ? t('cache not set!') : $config->get('checkmail_cache_unread') + $mail['recent'];
}
if ($mail['status'] == 'success') {
$block['content'] = t('Mailbox statistics for') . ' ' . $mail['login_id'] . ':';
$block['content'] .= '<br /><ul>';
if ($config->get('checkmail_number_of_messages')) {
$block['content'] .= '<li>' . $mail['messages'] . ' ' . t('total messages') . '</li>';
}
if ($config->get('checkmail_number_of_recent_messages')) {
$block['content'] .= '<li>' . $mail['recent'] . ' ' . t('new messages') . '</li>';
}
if ($config->get('checkmail_number_of_unread_messages')) {
$block['content'] .= '<li>' . $mail['unread'] . ' ' . t('unread messages') . '</li>';
}
if ($config->get('checkmail_size_of_mailbox')) {
$block['content'] .= '<li>' . $mail['size'] . ' ' . t('mailbox size in KB') . '</li>';
}
$block['content'] .= '</ul>';
}
elseif ($mail['status'] == 'user-uninitialized') {
$block['content'] = t('You have not configured your account to check your mailbox statistics. You can do that by navigating to !myaccount.', array('!myaccount' => l(t('My account'), 'user')));
}
elseif ($mail['status'] == 'admin-uninitialized') {
$block['content'] = t('The system administrator has not configured the account to check the mailbox statistics.');
}
else {
$block['content'] = t('There was a problem checking the mailbox. Please verify that your login credentials are correct, and then check the logs for errors, correct them and try again.');
}
break;
}
return $block;
}
}
/**
* Generates the output when the menu link is clicked.
*/
function checkmail_page() {
$mail = _checkmail();
if ($mail['status'] == 'success') {
$config = config('checkmail.settings');
if ($config->get('checkmail_number_of_messages') && $config->get('checkmail_number_of_unread_messages')) {
$output = '<p>' . t('!account has !mailsum !e-mail (!unread of them are unread).', array(
'!account' => $mail['login_id'],
'!mailsum' => $mail['messages'],
'!e-mail' => $mail['messages'] == 1 ? t('e-mail') : t('e-mails'),
'!unread' => $mail['unread'],
)
);
}
elseif ($config->get('checkmail_number_of_messages') && !$config->get('checkmail_number_of_unread_messages')) {
$output = '<p>' . t('!account has !mailsum !e-mail.', array(
'!account' => $mail['login_id'],
'!mailsum' => $mail['messages'],
'!e-mail' => $mail['messages'] == 1 ? t('e-mail') : t('e-mails'),
));
}
if ($config->get('checkmail_size_of_mailbox')) {
$output .= '<br />' . t('The total size of the inbox is !mailmem KB.', array('!mailmem' => sprintf("%01.2f", $mail['size'] ? $mail['size'] / 1024 : $mail['size']))) . '</p>';
}
}
else {
$output = '<p>' . t('There was a problem checking the mailbox. Please verify that your login credentials are correct, and then check the logs for errors, correct them and try again.') . '</p>';
}
return $output;
}
/**
* Check the server and set the cache.
*
* @return array
* An associated array with the following keys:
* - status: a string with one of 'failure', 'success', 'user-uninitialized',
* or 'admin-uninitialized'.
* - server_type: a string with the type of server being quried,
* - login_id: a string with the login ID for the mailbox.
* - messages: an integer with the total number of messages.
* - recent: an integer with the number of recent (new) messages (IMAP only).
* - unread: an integer with the number of unread messages (IMAP only).
* - size: an integer with the size of the mailbox (POP3 only).
*/
function _checkmail() {
// Initialize the server variables.
$config = config('checkmail.settings');
// if ($config->get('checkmail_use_encryption') === 1) {
$server_type = $config->get('checkmail_server_type');
$server_address = $config->get('checkmail_server_address');
$server_port = (int) $config->get('checkmail_server_port');
global $user;
// Set the initial return value.
$ret_val = array(
'status' => 'failure',
'server_type' => $server_type,
'login_id' => '',
'messages' => 0,
'recent' => 0,
'unread' => 0,
'size' => 0,
);
// Initialize the user variables.
$auth_method = $config->get('checkmail_account_info');
if ($auth_method == 'user_fields') {
// Make sure the user has filled in the proper information before attempting
// to access the acount data.
if (empty($user->data['checkmail_username'])) {
watchdog('checkmail', "User :name (account ID :uid) has not been configured for use with Checkmail.", array(':name' => $user->name, ':uid' => $user->uid), WATCHDOG_WARNING);
$ret_val['status'] = 'user-uninitialized';
return $ret_val;
}
$login_id = $user->data['checkmail_username'];
module_load_include('inc', 'checkmail', 'checkmail.common');
$login_password = _checkmail_decrypt_password($user->data['checkmail_passsword']);
}
elseif ($auth_method == 'admin_fields') {
$login_id = $config->get('checkmail_login_id');
// Make sure the admin has filled in the proper information before
// attempting to access the acount data.
if (empty($login_id)) {
watchdog('checkmail', 'The proper administrative settings have not been configured for Checkmail.', WATCHDOG_WARNING);
$ret_val['status'] = 'admin-uninitialized';
return $ret_val;
}
module_load_include('inc', 'checkmail', 'checkmail.common');
$login_password = _checkmail_decrypt_password($config->get('checkmail_login_password'));
}
$ret_val['login_id'] = $login_id;
// Error checking.
if (empty($server_address)) {
watchdog('checkmail', 'The e-mail server address is not configured in the administrative settings.', array(), WATCHDOG_ERROR);
return $ret_val;
}
if (empty($login_id)) {
watchdog('checkmail', 'There is no login ID configured for user !user.', array('!user' => $user->name), WATCHDOG_ERROR);
return $ret_val;
}
switch ($server_type) {
case 'pop3':
$mailserver = fsockopen($server_address, $server_port, $errno, $errstr, 30);
if (!$mailserver) {
watchdog('checkmail', 'Cannot connect to mailserver: !server. Connection error: !errno: !errstr.', array(
'!server' => $server_address,
'!errno' => $errno,
'!errstr' => $errstr,
), WATCHDOG_ERROR);
}
else {
$buffer = fgets($mailserver, 512);
$buffer = "USER $login_id\n";
fwrite($mailserver, $buffer);
$buffer = fgets($mailserver, 512);
$buffer = "PASS $login_password\n";
fwrite($mailserver, $buffer);
$buffer = fgets($mailserver, 512);
if (substr($buffer, 0, 4) == '-ERR') {
watchdog('checkmail', 'Invalid password or username for checking !account', array('!account' => $login_id), WATCHDOG_ERROR);
fclose($mailserver);
}
else {
$buffer = "STAT\n";
fwrite($mailserver, $buffer);
$buffer = fgets($mailserver, 512);
$mailsum = (int) substr($buffer, 4, 2);
$mailmem = (int) substr($buffer, 6, drupal_strlen($buffer) - 6);
$buffer = "QUIT\n";
fwrite($mailserver, $buffer);
$buffer = fgets($mailserver, 512);
fclose($mailserver);
$config = config('checkmail.settings');
$config->set('checkmail_checked_time', REQUEST_TIME);
$config->set('checkmail_cache_num', $mailsum);
$config->set('checkmail_cache_mem', $mailmem);
$config->save();
$ret_val['status'] = 'success';
$ret_val['messages'] = $mailsum;
$ret_val['size'] = $mailmem;
}
}
break;
case 'imap':
// Prepare the flags for connecting to the server with.
$imap_flags = '';
if ($config->get('checkmail_secure_log_in') === 1) {
$imap_flags .= '/secure';
}
if ($config->get('checkmail_validate_cert') === 1) {
$imap_flags .= '/validate-cert';
}
else {
$imap_flags .= '/novalidate-cert';
}
if ($config->get('checkmail_use_ssl') === 1) {
$imap_flags .= '/ssl';
}
if ($config->get('checkmail_use_tls') === 1) {
$imap_flags .= '/tls';
}
else {
$imap_flags .= '/notls';
}
$server_string = '{' . $server_address . ':' . $server_port . $imap_flags . '}';
if (($imap_resource = imap_open($server_string, $login_id, $login_password, 0, 0)) == FALSE) {
watchdog('checkmail', 'There was an error accessing the remote server: ' . imap_last_error(), array(), WATCHDOG_ERROR);
return $ret_val;
}
$options = SA_MESSAGES + SA_RECENT + SA_UNSEEN;
$imap_status = imap_status($imap_resource, $server_string, $options);
imap_close($imap_resource);
$config = config('checkmail.settings');
$config->set('checkmail_checked_time', REQUEST_TIME);
$config->set('checkmail_cache_num', $imap_status->messages);
$config->set('checkmail_cache_recent', $imap_status->recent);
$config->set('checkmail_cache_unread', $imap_status->unseen);
$config->save();
$ret_val['status'] = 'success';
$ret_val['messages'] = $imap_status->messages;
$ret_val['recent'] = $imap_status->recent;
$ret_val['unread'] = $imap_status->unseen;
break;
default:
watchdog('checkmail', 'An invalid server type was specified when calling the _checkmail() function: "!server_type"', array('!server_type' => $server_type), WATCHDOG_ERROR);
break;
}
return $ret_val;
}