| 1 |
<?php
|
| 2 |
// $Id: checkmail.module,v 1.3.4.1 2008/08/26 02:09:36 oadaeh Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* The main file for Checkmail, which displays statistical information about a mailbox.
|
| 7 |
*
|
| 8 |
* This module generates a page and/or a block, which displays statistical
|
| 9 |
* information about an e-mail inbox, including the number of messages and
|
| 10 |
* size of the mailbox.
|
| 11 |
*
|
| 12 |
* @author Stefan Nagtegaal <Stefan at: Sempre-Crescendo.nl>
|
| 13 |
* @author Kristjan Jansen
|
| 14 |
* @author David Kent Norman <deekayen at: deekayen {dot} net>
|
| 15 |
* @author Jason Flatt http://drupal.org/user/4649
|
| 16 |
*/
|
| 17 |
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_help().
|
| 21 |
*/
|
| 22 |
function checkmail_help($section) {
|
| 23 |
switch ($section) {
|
| 24 |
case 'admin/help#checkmail':
|
| 25 |
$help_text = t('Displays statistical information about an e-mail inbox, including the number of messages and size of the mailbox.');
|
| 26 |
break;
|
| 27 |
}
|
| 28 |
|
| 29 |
return $help_text;
|
| 30 |
} // End of checkmail_help().
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Implementation of hook_perm().
|
| 36 |
*/
|
| 37 |
function checkmail_perm() {
|
| 38 |
return array('access checkmail', 'administer checkmail');
|
| 39 |
} // End of checkmail_perm().
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_menu().
|
| 45 |
*/
|
| 46 |
function checkmail_menu($may_cache) {
|
| 47 |
$items = array();
|
| 48 |
|
| 49 |
if ($may_cache) {
|
| 50 |
$items[] = array(
|
| 51 |
'path' => 'admin/settings/checkmail',
|
| 52 |
'title' => t('Checkmail'),
|
| 53 |
'access' => user_access('administer checkmail'),
|
| 54 |
'callback' => 'drupal_get_form',
|
| 55 |
'callback arguments' => 'checkmail_admin_settings',
|
| 56 |
'description' => t('Configure the settings for the mailbox to check.'),
|
| 57 |
);
|
| 58 |
$items[] = array(
|
| 59 |
'path' => 'checkmail',
|
| 60 |
'title' => t('Check e-mail'),
|
| 61 |
'access' => user_access('access checkmail'),
|
| 62 |
'callback' => 'checkmail_page',
|
| 63 |
'description' => t('Display the mailbox information.'),
|
| 64 |
);
|
| 65 |
}
|
| 66 |
|
| 67 |
return $items;
|
| 68 |
} // End of checkmail_menu().
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Configuration options for setting mail server address, mail server port,
|
| 74 |
* username, and password.
|
| 75 |
*
|
| 76 |
* @return
|
| 77 |
* The generated form.
|
| 78 |
*/
|
| 79 |
function checkmail_admin_settings() {
|
| 80 |
$form['checkmail_general_settings'] = array(
|
| 81 |
'#type' => 'fieldset',
|
| 82 |
'#title' => t('General settings'),
|
| 83 |
);
|
| 84 |
$form['checkmail_general_settings']['checkmail_number_of_messages'] = array(
|
| 85 |
'#type' => 'checkbox',
|
| 86 |
'#title' => t("Show the number of messages in the inbox."),
|
| 87 |
'#default_value' => variable_get('checkmail_number_of_messages', 1),
|
| 88 |
'#description' => t("Checking this will include the total number of messages in the inbox as part of the display."),
|
| 89 |
);
|
| 90 |
$form['checkmail_general_settings']['checkmail_number_of_recent_messages'] = array(
|
| 91 |
'#type' => 'checkbox',
|
| 92 |
'#title' => t("Show the number of recent (new) messages in the inbox."),
|
| 93 |
'#default_value' => variable_get('checkmail_number_of_recent_messages', 1),
|
| 94 |
'#description' => t("Checking this will include the total number of <em>recent (new)</em> messages in the inbox as part of the display. NOTE: This only works for IMAP servers."),
|
| 95 |
);
|
| 96 |
$form['checkmail_general_settings']['checkmail_number_of_unread_messages'] = array(
|
| 97 |
'#type' => 'checkbox',
|
| 98 |
'#title' => t("Show the number of unread messages in the inbox."),
|
| 99 |
'#default_value' => variable_get('checkmail_number_of_unread_messages', 1),
|
| 100 |
'#description' => t("Checking this will include the total number of <em>unread</em> messages in the inbox as part of the display. NOTE: This only works for IMAP servers."),
|
| 101 |
);
|
| 102 |
$form['checkmail_general_settings']['checkmail_size_of_mailbox'] = array(
|
| 103 |
'#type' => 'checkbox',
|
| 104 |
'#title' => t("Show the total size of the mailbox."),
|
| 105 |
'#default_value' => variable_get('checkmail_size_of_mailbox', 1),
|
| 106 |
'#description' => t("Checking this will include the total size of the mailbox as part of the display."),
|
| 107 |
);
|
| 108 |
|
| 109 |
$form['checkmail_server_settings'] = array(
|
| 110 |
'#type' => 'fieldset',
|
| 111 |
'#title' => t('Server settings'),
|
| 112 |
);
|
| 113 |
// Collect basic server information.
|
| 114 |
$form['checkmail_server_settings']['checkmail_server_type'] = array(
|
| 115 |
'#type' => 'select',
|
| 116 |
'#title' => t('E-mail server type'),
|
| 117 |
'#default_value' => variable_get('checkmail_server_type', 0),
|
| 118 |
'#options' => array('imap' => t('IMAP'), 'pop3' => t('POP3')),
|
| 119 |
);
|
| 120 |
$form['checkmail_server_settings']['checkmail_server_address'] = array(
|
| 121 |
'#type' => 'textfield',
|
| 122 |
'#title' => t('E-mail server name'),
|
| 123 |
'#default_value' => variable_get('checkmail_server_address', ''),
|
| 124 |
'#description' => t("Fill in your e-mail server's name (for example, mail.example.com)."),
|
| 125 |
'#required' => TRUE,
|
| 126 |
);
|
| 127 |
$form['checkmail_server_settings']['checkmail_server_port'] = array(
|
| 128 |
'#type' => 'textfield',
|
| 129 |
'#title' => t('E-mail server port'),
|
| 130 |
'#default_value' => variable_get('checkmail_server_port', ''),
|
| 131 |
'#size' => 10,
|
| 132 |
'#maxlength' => 10,
|
| 133 |
'#description' => t("Fill in your e-mail server's port number. For POP3 servers, the default is 110. For IMAP servers, the default is 143. If you are using a secure connection with either SSL or TLS, the default for POP3 is and for IMAP is , but check with your system administer for the correct number for your mail server."),
|
| 134 |
'#required' => TRUE,
|
| 135 |
);
|
| 136 |
// Collect advanced server information.
|
| 137 |
$form['checkmail_server_settings']['checkmail_secure_log_in'] = array(
|
| 138 |
'#type' => 'checkbox',
|
| 139 |
'#title' => t('Secure login.'),
|
| 140 |
'#default_value' => variable_get('checkmail_secure_log_in', 0),
|
| 141 |
'#description' => t('Check this box to make a secure connection to the mail server.'),
|
| 142 |
);
|
| 143 |
$form['checkmail_server_settings']['checkmail_validate_cert'] = array(
|
| 144 |
'#type' => 'checkbox',
|
| 145 |
'#title' => t('Validate certificate.'),
|
| 146 |
'#default_value' => variable_get('checkmail_validate_cert', 0),
|
| 147 |
'#description' => t('Check this box to validate the certificate, when using a secure connection.'),
|
| 148 |
);
|
| 149 |
$form['checkmail_server_settings']['checkmail_use_ssl'] = array(
|
| 150 |
'#type' => 'checkbox',
|
| 151 |
'#title' => t('Encrypt session using SSL.'),
|
| 152 |
'#default_value' => variable_get('checkmail_use_ssl', 0),
|
| 153 |
'#description' => t('Check this box to use SSL when connecting to the server.'),
|
| 154 |
);
|
| 155 |
$form['checkmail_server_settings']['checkmail_use_tls'] = array(
|
| 156 |
'#type' => 'checkbox',
|
| 157 |
'#title' => t('Encrypt session using TLS.'),
|
| 158 |
'#default_value' => variable_get('checkmail_use_tls', 0),
|
| 159 |
'#description' => t('Check this box to use TLS when connecting to the server.'),
|
| 160 |
);
|
| 161 |
|
| 162 |
// Collect user information.
|
| 163 |
$form['checkmail_authentication_settings'] = array(
|
| 164 |
'#type' => 'fieldset',
|
| 165 |
'#title' => t('Authentication settings'),
|
| 166 |
);
|
| 167 |
$options = array(
|
| 168 |
'user_account' => t("Use the user's account e-mail address and password for checking e-mail. (Existing users will have to re-enter their password.)"),
|
| 169 |
'user_fields' => t('Use additional fields in the user account to collect the login ID and password from each user.'),
|
| 170 |
// 'module_settings' => t("Use the login ID and password collected for another module, specified in the 'Module settings' fieldset below. (Depending on how the data is stored, existing users may have to re-enter their password.)"),
|
| 171 |
'admin_fields' => t("Use the form fields in the 'Log in settings' fieldset below for the login information. (All users allowed access will see the same information."),
|
| 172 |
);
|
| 173 |
$form['checkmail_authentication_settings']['checkmail_account_info'] = array(
|
| 174 |
'#type' => 'radios',
|
| 175 |
'#options' => $options,
|
| 176 |
'#default_value' => variable_get('checkmail_account_info', 'user_account'),
|
| 177 |
);
|
| 178 |
|
| 179 |
// $form['checkmail_authentication_settings']['checkmail_option_three'] = array(
|
| 180 |
// '#type' => 'fieldset',
|
| 181 |
// '#title' => t('Module settings'),
|
| 182 |
// '#collapsible' => TRUE,
|
| 183 |
// '#collapsed' => TRUE,
|
| 184 |
// );
|
| 185 |
|
| 186 |
$form['checkmail_authentication_settings']['checkmail_option_four'] = array(
|
| 187 |
'#type' => 'fieldset',
|
| 188 |
'#title' => t('Log in settings'),
|
| 189 |
'#collapsible' => TRUE,
|
| 190 |
'#collapsed' => TRUE,
|
| 191 |
);
|
| 192 |
$form['checkmail_authentication_settings']['checkmail_option_four']['checkmail_login_id'] = array(
|
| 193 |
'#type' => 'textfield',
|
| 194 |
'#title' => t('E-mail server log in ID'),
|
| 195 |
'#default_value' => variable_get('checkmail_login_id', ''),
|
| 196 |
);
|
| 197 |
$form['checkmail_authentication_settings']['checkmail_option_four']['checkmail_login_password'] = array(
|
| 198 |
'#type' => 'password_confirm',
|
| 199 |
'#default_value' => variable_get('checkmail_login_password', ''),
|
| 200 |
'#description' => t('NOTE: The stored password is not encrypted.'),
|
| 201 |
);
|
| 202 |
|
| 203 |
return system_settings_form($form);
|
| 204 |
} // End of checkmail_admin_settings().
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
/**
|
| 209 |
* Provides extra validation for the administrative settings form.
|
| 210 |
*
|
| 211 |
* @param form_id
|
| 212 |
* A unique string identifying the form.
|
| 213 |
* @param form_values
|
| 214 |
* The contents of the form fields.
|
| 215 |
*/
|
| 216 |
function checkmail_admin_settings_validate($form_id, $form_values) {
|
| 217 |
if ($form_values['checkmail_account_info'] == 'admin_fields') {
|
| 218 |
if ($form_values['checkmail_login_id'] == '') {
|
| 219 |
form_set_error('checkmail_login_id', t('You must provide a log in ID.'));
|
| 220 |
}
|
| 221 |
if ($form_values['checkmail_login_password'] == '') {
|
| 222 |
form_set_error('checkmail_login_password', t('You must provide a password.'));
|
| 223 |
}
|
| 224 |
}
|
| 225 |
} // End of checkmail_admin_settings_validate().
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
/**
|
| 230 |
* Implementation of hook_user().
|
| 231 |
*
|
| 232 |
* @TODO: change this module to be on a per user basis.
|
| 233 |
*/
|
| 234 |
function checkmail_user($op, &$edit, &$account, $category = NULL) {
|
| 235 |
switch ($op) {
|
| 236 |
case 'form':
|
| 237 |
if ($category == 'account' && user_access('access checkmail') && variable_get('checkmail_account_info', '') == 'user_fields') {
|
| 238 |
$form['checkmail_authentication_settings'] = array(
|
| 239 |
'#type' => 'fieldset',
|
| 240 |
'#title' => t('Checkmail authentication settings'),
|
| 241 |
'#collapsible' => TRUE,
|
| 242 |
'#weight' => 1,
|
| 243 |
);
|
| 244 |
$form['checkmail_authentication_settings']['checkmail_username'] = array(
|
| 245 |
'#type' => 'textfield',
|
| 246 |
'#title' => t('E-mail account log in ID'),
|
| 247 |
'#default_value' => $account->checkmail_username,
|
| 248 |
'#description' => t('The login id for your e-mail account.'),
|
| 249 |
'#required' => TRUE,
|
| 250 |
);
|
| 251 |
$form['checkmail_authentication_settings']['checkmail_passsword'] = array(
|
| 252 |
'#type' => 'password_confirm',
|
| 253 |
'#description' => t('The password for your e-mail account. NOTE: The password is stored unencrypted in the database.'),
|
| 254 |
);
|
| 255 |
}
|
| 256 |
return $form;
|
| 257 |
break;
|
| 258 |
|
| 259 |
case 'insert':
|
| 260 |
if (variable_get('checkmail_account_info', '') == 'user_account') {
|
| 261 |
$edit['checkmail_username'] = $edit['mail'];
|
| 262 |
$edit['checkmail_passsword'] = $edit['pass'];
|
| 263 |
}
|
| 264 |
break;
|
| 265 |
|
| 266 |
case 'register':
|
| 267 |
if (user_access('access checkmail') && !variable_get('checkmail_account_info', 1)) {
|
| 268 |
$form['checkmail_email_settings'] = array(
|
| 269 |
'#type' => 'fieldset',
|
| 270 |
'#title' => t('Basic webmail account settings'),
|
| 271 |
'#collapsible' => TRUE,
|
| 272 |
'#weight' => 1,
|
| 273 |
);
|
| 274 |
$form['checkmail_email_settings']['checkmail_username'] = array(
|
| 275 |
'#type' => 'textfield',
|
| 276 |
'#title' => t('E-mail account login'),
|
| 277 |
'#default_value' => $account->checkmail_username,
|
| 278 |
'#description' => t('The login id for your e-mail account.'),
|
| 279 |
'#required' => TRUE,
|
| 280 |
);
|
| 281 |
$form['checkmail_email_settings']['checkmail_passsword'] = array(
|
| 282 |
'#type' => 'password_confirm',
|
| 283 |
'#description' => t('The password for your e-mail account. NOTE: The password is stored unencrypted in the database.'),
|
| 284 |
);
|
| 285 |
}
|
| 286 |
return $form;
|
| 287 |
break;
|
| 288 |
|
| 289 |
case 'update':
|
| 290 |
if ($category == 'account' && variable_get('checkmail_account_info', '') == 'user_account') {
|
| 291 |
if ($edit['mail'] != $account->mail) {
|
| 292 |
$edit['checkmail_username'] = $edit['mail'];
|
| 293 |
}
|
| 294 |
else {
|
| 295 |
$edit['checkmail_username'] = $account->mail;
|
| 296 |
}
|
| 297 |
|
| 298 |
if ($edit['pass'] != '') {
|
| 299 |
$edit['checkmail_passsword'] = $edit['pass'];
|
| 300 |
}
|
| 301 |
else {
|
| 302 |
$edit['checkmail_passsword'] = $account->checkmail_passsword;
|
| 303 |
}
|
| 304 |
}
|
| 305 |
break;
|
| 306 |
}
|
| 307 |
} // End of checkmail_user().
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
/**
|
| 312 |
* Implementation of hook_block().
|
| 313 |
*/
|
| 314 |
function checkmail_block($op = 'list', $delta = 0, $edit = array()) {
|
| 315 |
$block = $blocks = array();
|
| 316 |
switch ($op) {
|
| 317 |
case 'list':
|
| 318 |
if (user_access('administer checkmail')) {
|
| 319 |
$blocks[0]['info'] = t('Check e-mail');
|
| 320 |
return $blocks;
|
| 321 |
}
|
| 322 |
break;
|
| 323 |
|
| 324 |
case 'configure':
|
| 325 |
/**
|
| 326 |
* @TODO: Add options for display of text and quick access to seeing the e-mails.
|
| 327 |
*/
|
| 328 |
if (user_access('administer checkmail')) {
|
| 329 |
$cache_options = array('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'));
|
| 330 |
$form['block_cache'] = array(
|
| 331 |
'#type' => 'select',
|
| 332 |
'#title' => t('Cache'),
|
| 333 |
'#default_value' => variable_get('checkmail_block_cache', 60),
|
| 334 |
'#options' => $cache_options,
|
| 335 |
'#description' => t('Cache the mail results for this amount of time before querying the server again.'),
|
| 336 |
'#required' => FALSE,
|
| 337 |
);
|
| 338 |
return $form;
|
| 339 |
}
|
| 340 |
break;
|
| 341 |
|
| 342 |
case 'save':
|
| 343 |
if (user_access('administer checkmail')) {
|
| 344 |
if ($delta == 0) {
|
| 345 |
variable_set('checkmail_block_subject', $edit['block_subject']);
|
| 346 |
variable_set('checkmail_block_cache', $edit['block_cache']);
|
| 347 |
}
|
| 348 |
}
|
| 349 |
break;
|
| 350 |
|
| 351 |
case 'view': default:
|
| 352 |
switch ($delta) {
|
| 353 |
case 0:
|
| 354 |
$time = time();
|
| 355 |
$cache = variable_get('checkmail_block_cache', 60);
|
| 356 |
$last_check = variable_get('checkmail_checked_time', $time);
|
| 357 |
|
| 358 |
if ((!$cache || $time == $last_check) || ($cache > 0 && $time > $last_check + $cache)) {
|
| 359 |
$mail = _checkmail();
|
| 360 |
}
|
| 361 |
else {
|
| 362 |
$mail = array();
|
| 363 |
$mail['status'] = 'success';
|
| 364 |
$mail['messages'] = variable_get('checkmail_cache_num', t('cache not set!'));
|
| 365 |
$mail['size'] = variable_get('checkmail_cache_mem', t('cache not set!'));
|
| 366 |
$mail['recent'] = variable_get('checkmail_cache_recent', t('cache not set!'));
|
| 367 |
$mail['unread'] = variable_get('checkmail_cache_unread', t('cache not set!'));
|
| 368 |
}
|
| 369 |
|
| 370 |
if ($mail['status'] == 'success') {
|
| 371 |
if (variable_get('checkmail_size_of_mailbox', 1)) {
|
| 372 |
if (variable_get('checkmail_number_of_messages', 1)) {
|
| 373 |
if (variable_get('checkmail_number_of_unread_messages', 1)) {
|
| 374 |
// print everything
|
| 375 |
$block['content'] = t('There are !mailsum !e-mail (!unread unread), totaling !mailmem KB.', array('!mailsum' => $mail['messages'], '!e-mail' => $mail['messages'] == 1 ? t('e-mail') : t('e-mails'), '!unread' => $mail['unread'], '!mailmem' => sprintf("%01.2f", $mail['size'] ? $mail['size']/1024 : $mail['size'])));
|
| 376 |
}
|
| 377 |
else {
|
| 378 |
// print messages and size
|
| 379 |
$block['content'] = t('There are !mailsum !e-mail, totaling !mailmem KB.', array('!mailsum' => $mail['messages'], '!e-mail' => $mail['messages'] == 1 ? t('e-mail') : t('e-mails'), '!mailmem' => sprintf("%01.2f", $mail['size'] ? $mail['size']/1024 : $mail['size'])));
|
| 380 |
}
|
| 381 |
}
|
| 382 |
else {
|
| 383 |
if (variable_get('checkmail_number_of_unread_messages', 1)) {
|
| 384 |
// print unread and size
|
| 385 |
$block['content'] = t('There are !unread unread !e-mail. The size of the mailbox is !mailmem KB.', array('!unread' => $mail['unread'], '!e-mail' => $mail['messages'] == 1 ? t('e-mail') : t('e-mails'), '!mailmem' => sprintf("%01.2f", $mail['size'] ? $mail['size']/1024 : $mail['size'])));
|
| 386 |
}
|
| 387 |
else {
|
| 388 |
// print size only
|
| 389 |
$block['content'] = t('The size of the mailbox is !mailmem KB.', array('!mailmem' => sprintf("%01.2f", $mail['size'] ? $mail['size']/1024 : $mail['size'])));
|
| 390 |
}
|
| 391 |
}
|
| 392 |
}
|
| 393 |
elseif (variable_get('checkmail_number_of_messages', 1)) {
|
| 394 |
if (variable_get('checkmail_number_of_unread_messages', 1)) {
|
| 395 |
// print messages and unread
|
| 396 |
$block['content'] = t('There are !mailsum !e-mail (!unread unread).', array('!mailsum' => $mail['messages'], '!e-mail' => $mail['messages'] == 1 ? t('e-mail') : t('e-mails'), '!unread' => $mail['unread']));
|
| 397 |
}
|
| 398 |
else {
|
| 399 |
// print messages only
|
| 400 |
$block['content'] = t('There are !mailsum !e-mail.', array('!mailsum' => $mail['messages'], '!e-mail' => $mail['messages'] == 1 ? t('e-mail') : t('e-mails')));
|
| 401 |
}
|
| 402 |
}
|
| 403 |
elseif (variable_get('checkmail_number_of_unread_messages', 1)) {
|
| 404 |
// print unread only
|
| 405 |
$block['content'] = t('There are !unread unread !e-mail.', array('!unread' => $mail['unread'], '!e-mail' => $mail['messages'] == 1 ? t('e-mail') : t('e-mails')));
|
| 406 |
}
|
| 407 |
else {
|
| 408 |
// print nothing
|
| 409 |
$block['content'] = 'No information was selected to display.';
|
| 410 |
}
|
| 411 |
}
|
| 412 |
else {
|
| 413 |
$block['content'] = t('There was a problem checking the mailbox. Please check the logs for errors, correct them and try again.');
|
| 414 |
}
|
| 415 |
|
| 416 |
break;
|
| 417 |
}
|
| 418 |
|
| 419 |
return $block;
|
| 420 |
}
|
| 421 |
} // End of checkmail_block().
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
/**
|
| 426 |
* Generates the output when the menu link is clicked
|
| 427 |
*/
|
| 428 |
function checkmail_page() {
|
| 429 |
$mail = _checkmail();
|
| 430 |
if ($mail['status'] == 'success') {
|
| 431 |
if (variable_get('checkmail_number_of_messages', 1) && variable_get('checkmail_number_of_unread_messages', 1)) {
|
| 432 |
$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']));
|
| 433 |
}
|
| 434 |
elseif (variable_get('checkmail_number_of_messages', 1) && !variable_get('checkmail_number_of_unread_messages', 1)) {
|
| 435 |
$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')));
|
| 436 |
}
|
| 437 |
if (variable_get('checkmail_size_of_mailbox', 1)) {
|
| 438 |
$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>';
|
| 439 |
}
|
| 440 |
}
|
| 441 |
else {
|
| 442 |
$output = '<p>'. t('There was a problem checking the mailbox. Please check the logs for errors, correct them and try again.') .'</p>';
|
| 443 |
}
|
| 444 |
|
| 445 |
echo theme('page', $output);
|
| 446 |
} // End of checkmail_page().
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
/**
|
| 451 |
* Check the server and set the cache.
|
| 452 |
*
|
| 453 |
* @return
|
| 454 |
* An associated array in the following format:
|
| 455 |
* Array(
|
| 456 |
* 'status' => 'failure', // This will be either 'failure' or
|
| 457 |
* // 'success'.
|
| 458 |
* 'server_type' => $server_type,
|
| 459 |
* 'login_id' => $login_id,
|
| 460 |
* 'messages' => 0, // The total number of messages.
|
| 461 |
* 'recent' => 0, // The number of recent (new) messages
|
| 462 |
* // (IMAP only).
|
| 463 |
* 'unread' => 0, // The number of unread messages
|
| 464 |
* // (IMAP only).
|
| 465 |
* 'size' => 0 // The total size of the mailbox.
|
| 466 |
* )
|
| 467 |
*/
|
| 468 |
function _checkmail() {
|
| 469 |
// Initialize the server variables.
|
| 470 |
$server_type = variable_get('checkmail_server_type', 'pop3');
|
| 471 |
$server_address = variable_get('checkmail_server_address', '');
|
| 472 |
$server_port = (int)variable_get('checkmail_server_port', 110);
|
| 473 |
|
| 474 |
// Initialize the user variables.
|
| 475 |
$auth_method = variable_get('checkmail_account_info', '');
|
| 476 |
if ($auth_method == 'user_account' || $auth_method == 'user_fields') {
|
| 477 |
global $user;
|
| 478 |
$login_id = $user->checkmail_username;
|
| 479 |
$login_password = $user->checkmail_passsword;
|
| 480 |
}
|
| 481 |
elseif ($auth_method == 'admin_fields') {
|
| 482 |
$login_id = variable_get('checkmail_login_id', '');
|
| 483 |
$login_password = variable_get('checkmail_login_password', '');
|
| 484 |
}
|
| 485 |
|
| 486 |
// Set the initial return value.
|
| 487 |
$ret_val = array('status' => 'failure', 'server_type' => $server_type, 'login_id' => $login_id, 'messages' => 0, 'recent' => 0, 'unread' => 0, 'size' => 0);
|
| 488 |
|
| 489 |
// Error checking.
|
| 490 |
if (empty($server_address)) {
|
| 491 |
watchdog('checkmail', 'The e-mail server address is not configured in the administrative settings.', WATCHDOG_ERROR);
|
| 492 |
return $ret_val;
|
| 493 |
}
|
| 494 |
if (empty($login_id)) {
|
| 495 |
$error_message = t('There is no login ID configured for user !user.', array('!user' => $user->name));
|
| 496 |
watchdog('checkmail', $error_message, WATCHDOG_ERROR);
|
| 497 |
return $ret_val;
|
| 498 |
}
|
| 499 |
|
| 500 |
switch ($server_type) {
|
| 501 |
case 'pop3':
|
| 502 |
$mailserver = fsockopen($server_address, $server_port, $errno, $errstr, 30);
|
| 503 |
if (!$mailserver) {
|
| 504 |
$error_message = t('Cannot connect to mailserver: !server. Connection error: !errno: !errstr.', array('!server' => $server_address, '!errno' => $errno, '!errstr' => $errstr));
|
| 505 |
watchdog('checkmail', $error_message, WATCHDOG_ERROR);
|
| 506 |
}
|
| 507 |
else {
|
| 508 |
$buffer = fgets($mailserver, 512);
|
| 509 |
|
| 510 |
$buffer = "USER $login_id\n";
|
| 511 |
fputs($mailserver, $buffer);
|
| 512 |
$buffer = fgets($mailserver, 512);
|
| 513 |
|
| 514 |
$buffer = "PASS $login_password\n";
|
| 515 |
fputs($mailserver, $buffer);
|
| 516 |
|
| 517 |
$buffer = fgets($mailserver, 512);
|
| 518 |
|
| 519 |
if (substr($buffer, 0, 4) == '-ERR') {
|
| 520 |
$error_message = t('Invalid password or username for checking !account', array('!account' => $login_id));
|
| 521 |
watchdog('checkmail', $error_message, WATCHDOG_ERROR);
|
| 522 |
fclose($mailserver);
|
| 523 |
}
|
| 524 |
else {
|
| 525 |
$buffer = "STAT\n";
|
| 526 |
fputs($mailserver, $buffer);
|
| 527 |
$buffer = fgets($mailserver, 512);
|
| 528 |
|
| 529 |
// Debugging code:
|
| 530 |
//drupal_set_message('$buffer = "'. $buffer .'"');
|
| 531 |
|
| 532 |
$mailsum = (int)substr($buffer, 4, 2);
|
| 533 |
$mailmem = (int)substr($buffer, 6, drupal_strlen($buffer) - 6);
|
| 534 |
|
| 535 |
$buffer = "QUIT\n";
|
| 536 |
fputs($mailserver, $buffer);
|
| 537 |
$buffer = fgets($mailserver, 512);
|
| 538 |
|
| 539 |
fclose($mailserver);
|
| 540 |
|
| 541 |
variable_set('checkmail_checked_time', time());
|
| 542 |
variable_set('checkmail_cache_num', $mailsum);
|
| 543 |
variable_set('checkmail_cache_mem', $mailmem);
|
| 544 |
|
| 545 |
$ret_val['status'] = 'success';
|
| 546 |
$ret_val['messages'] = $mailsum;
|
| 547 |
$ret_val['size'] = $mailmem;
|
| 548 |
}
|
| 549 |
}
|
| 550 |
|
| 551 |
break;
|
| 552 |
|
| 553 |
case 'imap':
|
| 554 |
// Prepare the flags for connecting to the server with.
|
| 555 |
$imap_flags = '';
|
| 556 |
if (variable_get('checkmail_secure_log_in', 0) == 1) {
|
| 557 |
$imap_flags .= '/secure';
|
| 558 |
}
|
| 559 |
if (variable_get('checkmail_validate_cert', 0) == 1) {
|
| 560 |
$imap_flags .= '/validate-cert';
|
| 561 |
}
|
| 562 |
else {
|
| 563 |
$imap_flags .= '/novalidate-cert';
|
| 564 |
}
|
| 565 |
if (variable_get('checkmail_use_ssl', 0) == 1) {
|
| 566 |
$imap_flags .= '/ssl';
|
| 567 |
}
|
| 568 |
if (variable_get('checkmail_use_tls', 0) == 1) {
|
| 569 |
$imap_flags .= '/tls';
|
| 570 |
}
|
| 571 |
else {
|
| 572 |
$imap_flags .= '/notls';
|
| 573 |
}
|
| 574 |
|
| 575 |
$server_string = '{'. $server_address .':'. $server_port . $imap_flags .'}';
|
| 576 |
|
| 577 |
// Debugging code:
|
| 578 |
//drupal_set_message('Server settings: $server_address = "'. $server_address .'"; $server_port = "'. $server_port .'"; $login_id = "'. $login_id .'"; $login_password = "'. $login_password .'"; $imap_flags = "'. $imap_flags .'"; $server_string = "'. $server_string .'"');
|
| 579 |
|
| 580 |
if (($imap_resource = imap_open($server_string, $login_id, $login_password, 0)) == FALSE) {
|
| 581 |
$error_message = t('There was an error accessing the remote server: ') . imap_last_error();
|
| 582 |
watchdog('checkmail', $error_message, WATCHDOG_ERROR);
|
| 583 |
return $ret_val;
|
| 584 |
}
|
| 585 |
|
| 586 |
$imap_status = imap_mailboxmsginfo($imap_resource);
|
| 587 |
imap_close($imap_resource);
|
| 588 |
|
| 589 |
variable_set('checkmail_checked_time', time());
|
| 590 |
variable_set('checkmail_cache_num', $imap_status->Nmsgs);
|
| 591 |
variable_set('checkmail_cache_recent', $imap_status->Recent);
|
| 592 |
variable_set('checkmail_cache_unread', $imap_status->Unread);
|
| 593 |
variable_set('checkmail_cache_mem', $imap_status->Size);
|
| 594 |
|
| 595 |
$ret_val['status'] = 'success';
|
| 596 |
$ret_val['messages'] = $imap_status->Nmsgs;
|
| 597 |
$ret_val['recent'] = $imap_status->Recent;
|
| 598 |
$ret_val['unread'] = $imap_status->Unread;
|
| 599 |
$ret_val['size'] = $imap_status->Size;
|
| 600 |
break;
|
| 601 |
|
| 602 |
default:
|
| 603 |
$error_message = t('An invalid server type was specified when calling the _checkmail() function: "!server_type"', array('!server_type' => $server_type));
|
| 604 |
watchdog('checkmail', $error_message, WATCHDOG_ERROR);
|
| 605 |
break;
|
| 606 |
}
|
| 607 |
|
| 608 |
return $ret_val;
|
| 609 |
} // End of _checkmail().
|