| 1 |
<?php
|
| 2 |
// $Id: couriermta.module,v 1.1.2.7 2008/04/15 19:44:33 oadaeh Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This is the main code file for the Courier MTA module. This module allows
|
| 7 |
* administrators and/or users to manage their Courier e-mail account(s)
|
| 8 |
* through a Drupal website.
|
| 9 |
*/
|
| 10 |
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Display help and module information.
|
| 14 |
* @param section The section of the site we're displaying help on.
|
| 15 |
* @return The help text for the section.
|
| 16 |
*/
|
| 17 |
function couriermta_help($section = '') {
|
| 18 |
$output = '';
|
| 19 |
|
| 20 |
switch ($section) {
|
| 21 |
case 'admin/settings/couriermta':
|
| 22 |
$output = t('This module gives administrators the capability of using a Drupal website to manage the user accounts on a Courier e-mail server.');
|
| 23 |
break;
|
| 24 |
}
|
| 25 |
|
| 26 |
return $output;
|
| 27 |
} // End of couriermta_help().
|
| 28 |
|
| 29 |
|
| 30 |
/**
|
| 31 |
* The valid permissions for this module.
|
| 32 |
* @return array An array of valid permissions for the module.
|
| 33 |
*/
|
| 34 |
function couriermta_perm() {
|
| 35 |
return array('administrator couriermta users');
|
| 36 |
} // End of couriermta_perm().
|
| 37 |
|
| 38 |
|
| 39 |
/**
|
| 40 |
* The stub for the menu callback.
|
| 41 |
* @param may_cache A boolean indicating whether cacheable menu items should be returned.
|
| 42 |
* @return An array of menu items.
|
| 43 |
*/
|
| 44 |
function couriermta_menu($may_cache) {
|
| 45 |
$items = array();
|
| 46 |
|
| 47 |
$items[] = array(
|
| 48 |
'path' => 'admin/settings/couriermta',
|
| 49 |
'title' => t('Courier MTA'),
|
| 50 |
'callback' => 'drupal_get_form',
|
| 51 |
'callback arguments' => array('couriermta_admin_settings'),
|
| 52 |
'access' => user_access('administer site configuration'),
|
| 53 |
'description' => t('Configure how the module saves data for the mail server.')
|
| 54 |
);
|
| 55 |
|
| 56 |
return $items;
|
| 57 |
} // End of couriermta_menu().
|
| 58 |
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Courier user administration settings form.
|
| 62 |
*/
|
| 63 |
function couriermta_admin_settings() {
|
| 64 |
$form = array();
|
| 65 |
|
| 66 |
$form['couriermta_instructions_line1'] = array(
|
| 67 |
'#value' => t('For each of the pattern fields below, if you specify an absolute value, that value will be saved in the database for each user as specified in the form field. If you enter a pattern, that pattern will be modified on a per user basis and saved in the database that way.'),
|
| 68 |
'#prefix' => '<p>',
|
| 69 |
'#suffix' => '</p>',
|
| 70 |
);
|
| 71 |
$form['couriermta_instructions_line2'] = array(
|
| 72 |
'#value' => t('For example, if you entered "/var/spool/mail" for the home form field below, that would be the value that would be saved in the database for each user. If, however, you entered "/var/spool/mail/!domain_name/!user_name" for the home form field below, the value that would be saved in the database would be based on the users\' e-mail address and would be different for every user.'),
|
| 73 |
'#prefix' => '<p>',
|
| 74 |
'#suffix' => '</p>',
|
| 75 |
);
|
| 76 |
|
| 77 |
if (module_exists('token')) {
|
| 78 |
$form['couriermta_token'] = array(
|
| 79 |
'#type' => 'fieldset',
|
| 80 |
'#title' => t('Token parsing'),
|
| 81 |
'#collapsible' => TRUE,
|
| 82 |
'#description' => t('If you have the Token module installed, you may use that module\'s tokens for any of the pattern fields below. Posible replcement tokens are listed in the "Replacement tokens" fieldset below.'),
|
| 83 |
);
|
| 84 |
$form['couriermta_token']['couriermta_use_token_module'] = array(
|
| 85 |
'#type' => 'checkbox',
|
| 86 |
'#title' => t('Use Token module.'),
|
| 87 |
'#default_value' => variable_get('couriermta_use_token_module', '0'),
|
| 88 |
'#description' => t('Check this box if you wish to use the Token module\'s parsing for the pattern fields below.'),
|
| 89 |
);
|
| 90 |
$form['couriermta_token']['couriermta_replacement_tokens'] = array(
|
| 91 |
'#type' => 'fieldset',
|
| 92 |
'#title' => t('Replacement tokens'),
|
| 93 |
'#collapsible' => TRUE,
|
| 94 |
'#collapsed' => TRUE,
|
| 95 |
'#description' => t('You may use any of the following replacements tokens for use in any of the pattern fields below.'),
|
| 96 |
);
|
| 97 |
$form['couriermta_token']['couriermta_replacement_tokens']['token_help'] = array(
|
| 98 |
'#value' => theme('token_help'),
|
| 99 |
);
|
| 100 |
}
|
| 101 |
else {
|
| 102 |
$form['couriermta_token'] = array(
|
| 103 |
'#type' => 'fieldset',
|
| 104 |
'#title' => t('Token parsing'),
|
| 105 |
'#collapsible' => TRUE,
|
| 106 |
'#description' => t('If you have the Token module installed, you may use that module\'s tokens for any of the pattern fields below.'),
|
| 107 |
);
|
| 108 |
$form['couriermta_token']['couriermta_use_token_module'] = array(
|
| 109 |
'#type' => 'checkbox',
|
| 110 |
'#title' => t('Use Token module.'),
|
| 111 |
'#disabled' => TRUE,
|
| 112 |
'#default_value' => variable_get('couriermta_use_token_module', '0'),
|
| 113 |
'#description' => t('Install and enable the Token module to enable this field.'),
|
| 114 |
);
|
| 115 |
}
|
| 116 |
|
| 117 |
$form['couriermta_email'] = array(
|
| 118 |
'#type' => 'fieldset',
|
| 119 |
'#title' => t('E-mail address'),
|
| 120 |
'#collapsible' => TRUE,
|
| 121 |
'#description' => t('Use this area to specify how the user\'s Courier e-mail address should be entered in the Courier database table. If you check the "Use the user\'s given e-mail address." checkbox, then the "E-mail pattern" field will be ignored.'),
|
| 122 |
);
|
| 123 |
$form['couriermta_email']['couriermta_email_users_address'] = array(
|
| 124 |
'#type' => 'checkbox',
|
| 125 |
'#title' => t('Use the user\'s given e-mail address.'),
|
| 126 |
'#default_value' => variable_get('couriermta_email_users_address', '1'),
|
| 127 |
);
|
| 128 |
$form['couriermta_email']['couriermta_email_pattern'] = array(
|
| 129 |
'#type' => 'textfield',
|
| 130 |
'#title' => t('E-mail pattern'),
|
| 131 |
'#default_value' => variable_get('couriermta_email_pattern', ''),
|
| 132 |
'#description' => t('The default value for the e-mail field. This field will be ignored if the "Use the user\'s given e-mail address." checkbox above is checked.'),
|
| 133 |
);
|
| 134 |
|
| 135 |
$form['couriermta_password'] = array(
|
| 136 |
'#type' => 'fieldset',
|
| 137 |
'#title' => t('Password'),
|
| 138 |
'#collapsible' => TRUE,
|
| 139 |
'#description' => t('Use this area to specify how the user\'s Courier password should be entered in the Courier database table.'),
|
| 140 |
);
|
| 141 |
$form['couriermta_password']['couriermta_password_users_password'] = array(
|
| 142 |
'#type' => 'checkbox',
|
| 143 |
'#title' => t('Use the user\'s given password.'),
|
| 144 |
'#default_value' => variable_get('couriermta_password_users_password', '1'),
|
| 145 |
);
|
| 146 |
$form['couriermta_password']['couriermta_password_type'] = array(
|
| 147 |
'#type' => 'radios',
|
| 148 |
'#title' => t('Password type to create'),
|
| 149 |
'#default_value' => variable_get('couriermta_password_type', '0'),
|
| 150 |
'#options' => array(t('Encrypted'), t('Cleartext'), t('Both')),
|
| 151 |
'#description' => t('Choose whether to create an encrypted password, a cleartext password or both.'),
|
| 152 |
);
|
| 153 |
$form['couriermta_password']['couriermta_password'] = array(
|
| 154 |
'#type' => 'textfield',
|
| 155 |
'#title' => t('Password pattern'),
|
| 156 |
'#default_value' => variable_get('couriermta_password', ''),
|
| 157 |
'#description' => t('The default value for the password field. This field will be ignored if the "Use the user\'s given password." checkbox above is checked.'),
|
| 158 |
);
|
| 159 |
|
| 160 |
$form['couriermta_name'] = array(
|
| 161 |
'#type' => 'fieldset',
|
| 162 |
'#title' => t('User name'),
|
| 163 |
'#collapsible' => TRUE,
|
| 164 |
'#description' => t('Use this area to specify how the user\'s Courier user\'s name field should be entered in the Courier database table. If you check the "Use the user\'s given name." checkbox, then the "Name pattern" field will be ignored.'),
|
| 165 |
);
|
| 166 |
$form['couriermta_name']['couriermta_users_name'] = array(
|
| 167 |
'#type' => 'checkbox',
|
| 168 |
'#title' => t('Use the user\'s given user name.'),
|
| 169 |
'#default_value' => variable_get('couriermta_users_name', '1'),
|
| 170 |
);
|
| 171 |
$form['couriermta_name']['couriermta_name_pattern'] = array(
|
| 172 |
'#type' => 'textfield',
|
| 173 |
'#title' => t('Name pattern'),
|
| 174 |
'#default_value' => variable_get('couriermta_name', ''),
|
| 175 |
'#description' => t('The default value for the name field. This field will be ignored if the "Use the user\'s given user name." checkbox above is checked.'),
|
| 176 |
);
|
| 177 |
|
| 178 |
$form['couriermta_uid'] = array(
|
| 179 |
'#type' => 'fieldset',
|
| 180 |
'#title' => t('UID'),
|
| 181 |
'#collapsible' => TRUE,
|
| 182 |
'#description' => t('Use this area to specify how the user\'s Courier UID field should be entered in the Courier database table.'),
|
| 183 |
);
|
| 184 |
$form['couriermta_uid']['couriermta_uid_pattern'] = array(
|
| 185 |
'#type' => 'textfield',
|
| 186 |
'#title' => t('UID pattern'),
|
| 187 |
'#default_value' => variable_get('couriermta_uid_pattern', '65534'),
|
| 188 |
'#description' => t('The default value for the user id field.'),
|
| 189 |
);
|
| 190 |
|
| 191 |
$form['couriermta_gid'] = array(
|
| 192 |
'#type' => 'fieldset',
|
| 193 |
'#title' => t('GID'),
|
| 194 |
'#collapsible' => TRUE,
|
| 195 |
'#description' => t('Use this area to specify how the user\'s Courier GID field should be entered in the Courier database table.'),
|
| 196 |
);
|
| 197 |
$form['couriermta_gid']['couriermta_gid_pattern'] = array(
|
| 198 |
'#type' => 'textfield',
|
| 199 |
'#title' => t('GID pattern'),
|
| 200 |
'#default_value' => variable_get('couriermta_gid_pattern', '65534'),
|
| 201 |
'#description' => t('The default value for the group id field.'),
|
| 202 |
);
|
| 203 |
|
| 204 |
$form['couriermta_home'] = array(
|
| 205 |
'#type' => 'fieldset',
|
| 206 |
'#title' => t('Home'),
|
| 207 |
'#collapsible' => TRUE,
|
| 208 |
'#description' => t('Use this area to specify how the user\'s Courier home field should be entered in the Courier database table.'),
|
| 209 |
);
|
| 210 |
$form['couriermta_home']['couriermta_home_pattern'] = array(
|
| 211 |
'#type' => 'textfield',
|
| 212 |
'#title' => t('Home pattern'),
|
| 213 |
'#default_value' => variable_get('couriermta_home_pattern', '/var/spool/mail/!domain_name/!user_name'),
|
| 214 |
'#description' => t('The default value for the home field. You may use "!domain_name" to represent the domain part of the user\'s e-mail address (the part after the @ sign) and "!user_name" to represent the user part of the user\'s e-mail address (the part before the @ sign). If you have checked the box above to use the Token module for pattern field parsing, !domain_name and !user_name will not work.'),
|
| 215 |
);
|
| 216 |
|
| 217 |
$form['couriermta_maildir'] = array(
|
| 218 |
'#type' => 'fieldset',
|
| 219 |
'#title' => t('Maildir'),
|
| 220 |
'#collapsible' => TRUE,
|
| 221 |
'#description' => t('Use this area to specify how the user\'s Courier maildir field should be entered in the Courier database table.'),
|
| 222 |
);
|
| 223 |
$form['couriermta_maildir']['couriermta_maildir_pattern'] = array(
|
| 224 |
'#type' => 'textfield',
|
| 225 |
'#title' => t('Maildir pattern'),
|
| 226 |
'#default_value' => variable_get('couriermta_maildir_pattern', '/var/spool/mail/!domain_name/!user_name'),
|
| 227 |
'#description' => t('The default value for the home field. You may use "!domain_name" to represent the domain part of the user\'s e-mail address (the part after the @ sign) and "!user_name" to represent the user part of the user\'s e-mail address (the part before the @ sign). If you have checked the box above to use the Token module for pattern field parsing, !domain_name and !user_name will not work.'),
|
| 228 |
);
|
| 229 |
|
| 230 |
$form['couriermta_defaultdelivery'] = array(
|
| 231 |
'#type' => 'fieldset',
|
| 232 |
'#title' => t('Default delivery'),
|
| 233 |
'#collapsible' => TRUE,
|
| 234 |
'#description' => t('Use this area to specify how the user\'s Courier defaultdelivery field should be entered in the Courier database table.'),
|
| 235 |
);
|
| 236 |
$form['couriermta_defaultdelivery']['couriermta_defaultdelivery_pattern'] = array(
|
| 237 |
'#type' => 'textfield',
|
| 238 |
'#title' => t('Default delivery pattern'),
|
| 239 |
'#default_value' => variable_get('couriermta_defaultdelivery_pattern', ''),
|
| 240 |
'#description' => t('The default value for the default delivery field.'),
|
| 241 |
);
|
| 242 |
|
| 243 |
$form['couriermta_quota'] = array(
|
| 244 |
'#type' => 'fieldset',
|
| 245 |
'#title' => t('Quota'),
|
| 246 |
'#collapsible' => TRUE,
|
| 247 |
'#description' => t('Use this area to specify how the user\'s Courier quota field should be entered in the Courier database table.'),
|
| 248 |
);
|
| 249 |
$form['couriermta_quota']['couriermta_quota_pattern'] = array(
|
| 250 |
'#type' => 'textfield',
|
| 251 |
'#title' => t('Quota pattern'),
|
| 252 |
'#default_value' => variable_get('couriermta_quota_pattern', ''),
|
| 253 |
'#description' => t('The default value for the quota field.'),
|
| 254 |
);
|
| 255 |
|
| 256 |
$form['couriermta_options'] = array(
|
| 257 |
'#type' => 'fieldset',
|
| 258 |
'#title' => t('Options'),
|
| 259 |
'#collapsible' => TRUE,
|
| 260 |
'#description' => t('Use this area to specify how the user\'s Courier options field should be entered in the Courier database table.'),
|
| 261 |
);
|
| 262 |
$form['couriermta_options']['couriermta_options_pattern'] = array(
|
| 263 |
'#type' => 'textfield',
|
| 264 |
'#title' => t('Options pattern'),
|
| 265 |
'#default_value' => variable_get('couriermta_options_pattern', ''),
|
| 266 |
'#description' => t('The default value for the options field.'),
|
| 267 |
);
|
| 268 |
|
| 269 |
$form['couriermta_allow_email_alias'] = array(
|
| 270 |
'#type' => 'checkbox',
|
| 271 |
'#title' => t('Allow users to specify an e-mail alias.'),
|
| 272 |
'#default_value' => variable_get('couriermta_allow_email_alias', '0'),
|
| 273 |
'#description' => t('Check this option if you want to allow the users to specify an e-mail address to forward e-mails to.'),
|
| 274 |
);
|
| 275 |
|
| 276 |
return system_settings_form($form);
|
| 277 |
} // End of couriermta_admin_settings().
|
| 278 |
|
| 279 |
|
| 280 |
/**
|
| 281 |
* Implementation of hook_user.
|
| 282 |
*/
|
| 283 |
function couriermta_user($op, &$edit, &$account, $category = NULL) {
|
| 284 |
if ($category == 'account') {
|
| 285 |
switch ($op) {
|
| 286 |
case 'delete':
|
| 287 |
// Check to see if an alias has been specified.
|
| 288 |
if (!db_result(db_query('SELECT alias FROM {couriermta_users} WHERE userid=%d', $account->uid))) {
|
| 289 |
// The account to be deleted is a mailbox e-mail account.
|
| 290 |
_couriermta_delete_mailbox_account(&$account);
|
| 291 |
}
|
| 292 |
else {
|
| 293 |
// The account to be deleted is a aliased e-mail account.
|
| 294 |
_couriermta_delete_aliased_account(&$account);
|
| 295 |
}
|
| 296 |
break;
|
| 297 |
|
| 298 |
|
| 299 |
case 'form':
|
| 300 |
if (variable_get('couriermta_allow_email_alias', '0') && $category == 'account') {
|
| 301 |
// Add an e-mail alias area.
|
| 302 |
$form['couriermta_email_alias'] = array(
|
| 303 |
'#type' => 'fieldset',
|
| 304 |
'#title' => t('E-mail alias settings'),
|
| 305 |
'#collapsible' => TRUE,
|
| 306 |
'#weight' => 1,
|
| 307 |
);
|
| 308 |
$form['couriermta_email_alias']['couriermta_email_alias_address'] = array(
|
| 309 |
'#type' => 'textfield',
|
| 310 |
'#title' => t('E-mail alias (optional)'),
|
| 311 |
'#default_value' => db_result(db_query('SELECT alias FROM {couriermta_users} WHERE userid=%d', $account->uid)),
|
| 312 |
'#description' => t('If this field is filled in with a valid e-mail address, all e-mail sent to the address entered in the "E-mail address" field above will be forwarded to this address instead. When changing from using a regular mailbox e-mail account to using an aliased e-mail account, a password must be given, as the existing password cannot be retrieved from the database and used. In order to revert back to using a regular mailbox e-mail address from an aliased one, simply clear this field.'),
|
| 313 |
);
|
| 314 |
|
| 315 |
return $form;
|
| 316 |
}
|
| 317 |
break;
|
| 318 |
|
| 319 |
|
| 320 |
case 'insert': // ** MOSTLY DONE ** //
|
| 321 |
|
| 322 |
// TODO: Check for edge case of account creation w/initial status of blocked.
|
| 323 |
|
| 324 |
// Check to see if an alias has been specified.
|
| 325 |
if ($edit['couriermta_email_alias_address'] == '') {
|
| 326 |
// No alias was specified, so we treat this account as a mailbox e-mail account.
|
| 327 |
_couriermta_insert_mailbox_account(&$edit, &$account);
|
| 328 |
}
|
| 329 |
else {
|
| 330 |
// An alias was specified, so we treat this account as a aliased e-mail account.
|
| 331 |
_couriermta_insert_aliased_account(&$edit, &$account);
|
| 332 |
}
|
| 333 |
|
| 334 |
$edit['couriermta_email_alias_address'] = NULL;
|
| 335 |
break;
|
| 336 |
|
| 337 |
|
| 338 |
case 'register':
|
| 339 |
if (variable_get('couriermta_allow_email_alias', '0')) {
|
| 340 |
// Add an e-mail alias area.
|
| 341 |
$form['couriermta_email_alias'] = array(
|
| 342 |
'#type' => 'fieldset',
|
| 343 |
'#title' => t('E-mail alias settings'),
|
| 344 |
'#collapsible' => TRUE,
|
| 345 |
'#weight' => 1,
|
| 346 |
);
|
| 347 |
$form['couriermta_email_alias']['couriermta_email_alias_address'] = array(
|
| 348 |
'#type' => 'textfield',
|
| 349 |
'#title' => t('E-mail alias (optional)'),
|
| 350 |
'#default_value' => db_result(db_query('SELECT alias FROM {couriermta_users} WHERE userid=%d', $account->uid)),
|
| 351 |
'#description' => t('If this field is filled in with a valid e-mail address, all e-mail sent to the address entered in the "E-mail address" field above will be forwarded to this address instead. When changing from using a regular mailbox e-mail account to using an aliased e-mail account, a password must be given, as the existing password cannot be retrieved from the database and used. In order to revert back to using a regular mailbox e-mail address from an aliased one, simply clear this field.'),
|
| 352 |
);
|
| 353 |
|
| 354 |
return $form;
|
| 355 |
}
|
| 356 |
break;
|
| 357 |
|
| 358 |
|
| 359 |
case 'update':
|
| 360 |
// Make sure that the user is editing the user object.
|
| 361 |
if (!empty($edit) && $category == 'account') {
|
| 362 |
$account_couriermta_email_alias_address = db_result(db_query('SELECT alias FROM {couriermta_users} WHERE userid=%d', $account->uid));
|
| 363 |
|
| 364 |
// First check to see if the account type has changed. If it has, then treat the operation as two operations: one insert & one delete.
|
| 365 |
if ($account_couriermta_email_alias_address != '' && $edit['couriermta_email_alias_address'] == '') {
|
| 366 |
// The account is currently setup to use an aliased e-mail and being changed to a mailbox account.
|
| 367 |
// Delete the user's information from the alias file.
|
| 368 |
_couriermta_delete_aliased_account(&$account);
|
| 369 |
// Insert the user's information to the couriermta_users table.
|
| 370 |
_couriermta_insert_mailbox_account(&$edit, &$account);
|
| 371 |
}
|
| 372 |
elseif ($account_couriermta_email_alias_address == '' && $edit['couriermta_email_alias_address'] != '') {
|
| 373 |
// The account is currently a mailbox account and is being changed to an aliased account.
|
| 374 |
// Delete the user's information from the couriermta_users table.
|
| 375 |
_couriermta_delete_mailbox_account(&$account);
|
| 376 |
// Insert the user's information to the alias file.
|
| 377 |
_couriermta_insert_aliased_account(&$edit, &$account);
|
| 378 |
}
|
| 379 |
|
| 380 |
elseif ($account_couriermta_email_alias_address != '' && $edit['couriermta_email_alias_address'] != '') {
|
| 381 |
// Update the user's information in the alias file and the couriermta_users table.
|
| 382 |
_couriermta_update_aliased_account(&$edit, &$account);
|
| 383 |
}
|
| 384 |
// Everything else.
|
| 385 |
else {
|
| 386 |
_couriermta_update_mailbox_account(&$edit, &$account);
|
| 387 |
}
|
| 388 |
}
|
| 389 |
break;
|
| 390 |
|
| 391 |
|
| 392 |
case 'validate':
|
| 393 |
// Verify a password is given when changing from an alias account to a mailbox account.
|
| 394 |
// Check the name and mail fields to verify the object is actually being edited.
|
| 395 |
if ($edit['name'] != '' && $edit['mail'] != '' && db_result(db_query('SELECT alias FROM {couriermta_users} WHERE userid=%d', $account->uid)) != '' && $edit['couriermta_email_alias_address'] == '' && $edit['pass'] == '') {
|
| 396 |
form_set_error('pass', 'You must specify a password when changing from using an aliased e-mail account to using a regular mailbox account.');
|
| 397 |
}
|
| 398 |
|
| 399 |
// If an alias is specified, make sure it is not the same as the e-mail address specified.
|
| 400 |
if ($edit['mail'] == $edit['couriermta_email_alias_address']) {
|
| 401 |
form_set_error('couriermta_email_alias_address', 'You may not specify an alias that is the same as your e-mail address.');
|
| 402 |
}
|
| 403 |
break;
|
| 404 |
}
|
| 405 |
}
|
| 406 |
} // End of couriermta_user().
|
| 407 |
|
| 408 |
|
| 409 |
/**
|
| 410 |
* Create a mailbox account.
|
| 411 |
*/
|
| 412 |
function _couriermta_insert_mailbox_account(&$edit, &$account) {
|
| 413 |
// Get the data.
|
| 414 |
$user_data = array();
|
| 415 |
$user_data = _couriermta_create_user_sql_data(&$edit, &$account);
|
| 416 |
|
| 417 |
// Insert the record.
|
| 418 |
switch (variable_get('couriermta_password_type', '0')) {
|
| 419 |
case 0:
|
| 420 |
// Insert encrypted passwords only.
|
| 421 |
if (!db_query('INSERT INTO {couriermta_users} (userid, email, crypt, clear, uid, gid, home, name, maildir, defaultdelivery, quota, auxoptions, alias, status) VALUES (%d, "%s", ENCRYPT("%s"), "", %d, %d, "%s", "%s", "%s", "%s", "%s", "%s", "%s", %d)', $user_data['userid'], $user_data['email'], $user_data['password'], $user_data['uid'], $user_data['gid'], $user_data['home'], $user_data['name'], $user_data['maildir'], $user_data['defaultdelivery'], $user_data['quota'], $user_data['auxoptions'], $user_data['alias'], $user_data['status'])) {
|
| 422 |
watchdog('couriermta', t('The query inserting data into the couriermta_users table did not succeed.'), WATCHDOG_ERROR);
|
| 423 |
}
|
| 424 |
break;
|
| 425 |
|
| 426 |
case 1:
|
| 427 |
// Insert cleartext passwords only.
|
| 428 |
if (!db_query('INSERT INTO {couriermta_users} (userid, email, crypt, clear, uid, gid, home, name, maildir, defaultdelivery, quota, auxoptions, alias, status) VALUES (%d, "%s", "", "%s", %d, %d, "%s", "%s", "%s", "%s", "%s", "%s", "%s", %d)', $user_data['userid'], $user_data['email'], $user_data['password'], $user_data['uid'], $user_data['gid'], $user_data['home'], $user_data['name'], $user_data['maildir'], $user_data['defaultdelivery '], $user_data['quota'], $user_data['auxoptions'], $user_data['alias'], $user_data['status'])) {
|
| 429 |
watchdog('couriermta', t('The query inserting data into the couriermta_users table did not succeed.'), WATCHDOG_ERROR);
|
| 430 |
}
|
| 431 |
break;
|
| 432 |
|
| 433 |
case 2:
|
| 434 |
// Insert both encrypted and cleartext passwords.
|
| 435 |
if (!db_query('INSERT INTO {couriermta_users} (userid, email, crypt, clear, uid, gid, home, name, maildir, defaultdelivery, quota, auxoptions, alias, status) VALUES (%d, "%s", ENCRYPT("%s"), "%s", %d, %d, "%s", "%s", "%s", "%s", "%s", "%s", "%s", %d)', $user_data['userid'], $user_data['email'], $user_data['password'], $user_data['password'], $user_data['uid'], $user_data['gid'], $user_data['home'], $user_data['name'], $user_data['maildir'], $user_data['defaultdelivery'], $user_data['quota'], $user_data['auxoptions'], $user_data['alias'], $user_data['status'])) {
|
| 436 |
watchdog('couriermta', t('The query inserting data into the couriermta_users table did not succeed.'), WATCHDOG_ERROR);
|
| 437 |
}
|
| 438 |
break;
|
| 439 |
}
|
| 440 |
|
| 441 |
// Update the insertmailbox file.
|
| 442 |
_couriermta_update_input_file('insert', 'mailbox', (empty($user_data['maildir'])) ? $user_data['home'] : $user_data['maildir'] .'
|
| 443 |
');
|
| 444 |
} // End of _couriermta_insert_mailbox_account().
|
| 445 |
|
| 446 |
|
| 447 |
/**
|
| 448 |
* Create an alias account.
|
| 449 |
*/
|
| 450 |
function _couriermta_insert_aliased_account(&$edit, &$account) {
|
| 451 |
// Get the data.
|
| 452 |
$user_data = array();
|
| 453 |
$user_data = _couriermta_create_user_sql_data(&$edit, &$account);
|
| 454 |
|
| 455 |
// Update the couriermta_users table.
|
| 456 |
if (!db_query('INSERT INTO {couriermta_users} (userid, alias, status) VALUES (%d, "%s", %d)', $user_data['userid'], $user_data['alias'], $user_data['status'])) {
|
| 457 |
watchdog('couriermta', t('The query inserting data into the couriermta_users table did not succeed.'), WATCHDOG_ERROR);
|
| 458 |
}
|
| 459 |
|
| 460 |
// Update the insertalias file.
|
| 461 |
_couriermta_update_input_file('insert', 'alias', $user_data['email'] .': '. $user_data['alias'] .'
|
| 462 |
');
|
| 463 |
} // End of _couriermta_insert_aliased_account().
|
| 464 |
|
| 465 |
|
| 466 |
/**
|
| 467 |
* Update a mailbox account.
|
| 468 |
*/
|
| 469 |
function _couriermta_update_mailbox_account(&$edit, &$account) {
|
| 470 |
// Get the data.
|
| 471 |
$user_data = array();
|
| 472 |
$user_data = _couriermta_create_user_sql_data(&$edit, &$account);
|
| 473 |
|
| 474 |
// Get the old mailbox directory before the database changes.
|
| 475 |
if ($edit['mail'] != $account->mail) {
|
| 476 |
if (!$oldmailboxdir = db_result(db_query('SELECT maildir FROM {couriermta_users} WHERE userid=%d', $account->uid))) {
|
| 477 |
$oldmailboxdir = db_result(db_query('SELECT home FROM {couriermta_users} WHERE userid=%d', $account->uid));
|
| 478 |
}
|
| 479 |
}
|
| 480 |
|
| 481 |
// Update the record.
|
| 482 |
if ($user_data['password'] == '') {
|
| 483 |
// The data does not include a password.
|
| 484 |
if (!db_query('UPDATE {couriermta_users} SET email="%s", uid=%d, gid=%d, home="%s", name="%s", maildir="%s", defaultdelivery="%s", quota="%s", auxoptions="%s", alias="%s", status=%d WHERE userid=%d', $user_data['email'], $user_data['uid'], $user_data['gid'], $user_data['home'], $user_data['name'], $user_data['maildir'], $user_data['defaultdelivery'], $user_data['quota'], $user_data['auxoptions'], $user_data['alias'], $user_data['status'], $user_data['userid'])) {
|
| 485 |
watchdog('couriermta', t('The query inserting data into the couriermta_users table did not succeed.'), WATCHDOG_ERROR);
|
| 486 |
}
|
| 487 |
}
|
| 488 |
else {
|
| 489 |
// The data includes a password.
|
| 490 |
switch (variable_get('couriermta_password_type', '0')) {
|
| 491 |
case 0:
|
| 492 |
// Insert encrypted passwords only.
|
| 493 |
if (!db_query('UPDATE {couriermta_users} SET email="%s", crypt=ENCRYPT("%s"), clear="", uid=%d, gid=%d, home="%s", name="%s", maildir="%s", defaultdelivery="%s", quota="%s", auxoptions="%s", alias="%s", status=%d WHERE userid=%d', $user_data['email'], $user_data['password'], $user_data['uid'], $user_data['gid'], $user_data['home'], $user_data['name'], $user_data['maildir'], $user_data['defaultdelivery'], $user_data['quota'], $user_data['auxoptions'], $user_data['alias'], $user_data['status'], $user_data['userid'])) {
|
| 494 |
watchdog('couriermta', t('The query inserting data into the couriermta_users table did not succeed.'), WATCHDOG_ERROR);
|
| 495 |
}
|
| 496 |
break;
|
| 497 |
|
| 498 |
case 1:
|
| 499 |
// Insert cleartext passwords only.
|
| 500 |
if (!db_query('UPDATE {couriermta_users} SET email="%s", crypt="", clear="%s", uid=%d, gid=%d, home="%s", name="%s", maildir="%s", defaultdelivery="%s", quota="%s", auxoptions="%s", alias="%s", status=%d WHERE userid=%d', $user_data['email'], $user_data['password'], $user_data['uid'], $user_data['gid'], $user_data['home'], $user_data['name'], $user_data['maildir'], $user_data['defaultdelivery'], $user_data['quota'], $user_data['auxoptions'], $user_data['alias'], $user_data['status'], $user_data['userid'])) {
|
| 501 |
watchdog('couriermta', t('The query inserting data into the couriermta_users table did not succeed.'), WATCHDOG_ERROR);
|
| 502 |
}
|
| 503 |
break;
|
| 504 |
|
| 505 |
case 2:
|
| 506 |
// Insert both encrypted and cleartext passwords.
|
| 507 |
if (!db_query('UPDATE {couriermta_users} SET email="%s", crypt=ENCRYPT("%s"), clear="%s", uid=%d, gid=%d, home="%s", name="%s", maildir="%s", defaultdelivery="%s", quota="%s", auxoptions="%s", alias="%s", status=%d WHERE userid=%d', $user_data['email'], $user_data['password'], $user_data['password'], $user_data['uid'], $user_data['gid'], $user_data['home'], $user_data['name'], $user_data['maildir'], $user_data['defaultdelivery'], $user_data['quota'], $user_data['auxoptions'], $user_data['alias'], $user_data['status'], $user_data['userid'])) {
|
| 508 |
watchdog('couriermta', t('The query inserting data into the couriermta_users table did not succeed.'), WATCHDOG_ERROR);
|
| 509 |
}
|
| 510 |
break;
|
| 511 |
}
|
| 512 |
}
|
| 513 |
|
| 514 |
if ($oldmailboxdir) {
|
| 515 |
if (!$newmailboxdir = db_result(db_query('SELECT maildir FROM {couriermta_users} WHERE userid=%d', $account->uid))) {
|
| 516 |
$newmailboxdir = db_result(db_query('SELECT home FROM {couriermta_users} WHERE userid=%d', $account->uid));
|
| 517 |
}
|
| 518 |
|
| 519 |
// Update the updatemailbox file.
|
| 520 |
_couriermta_update_input_file('update', 'mailbox', $oldmailboxdir .':'. $newmailboxdir .'
|
| 521 |
');
|
| 522 |
}
|
| 523 |
} // End of _couriermta_update_mailbox_account().
|
| 524 |
|
| 525 |
|
| 526 |
/**
|
| 527 |
* Update an alias account.
|
| 528 |
*/
|
| 529 |
function _couriermta_update_aliased_account(&$edit, &$account) {
|
| 530 |
// Get the data.
|
| 531 |
$user_data = array();
|
| 532 |
$user_data = _couriermta_create_user_sql_data(&$edit, &$account);
|
| 533 |
|
| 534 |
if (db_result(db_query('SELECT alias FROM {couriermta_users} WHERE userid=%d', $account->uid)) != $user_data['alias']) {
|
| 535 |
// Update the deletealias file.
|
| 536 |
_couriermta_update_input_file('delete', 'alias', $account->mail .': '. db_result(db_query('SELECT alias FROM {couriermta_users} WHERE userid=%d', $account->uid)) .'
|
| 537 |
');
|
| 538 |
|
| 539 |
// Update the insertalias file.
|
| 540 |
_couriermta_update_input_file('insert', 'alias', $edit['mail'] .': '. $edit['couriermta_email_alias_address'] .'
|
| 541 |
');
|
| 542 |
}
|
| 543 |
|
| 544 |
// Update the couriermta_users table.
|
| 545 |
if (!db_query('UPDATE {couriermta_users} SET alias="%s", status=%d WHERE userid=%d', $user_data['alias'], $user_data['status'], $user_data['userid'])) {
|
| 546 |
watchdog('couriermta', t('The query inserting data into the couriermta_users table did not succeed.'), WATCHDOG_ERROR);
|
| 547 |
}
|
| 548 |
} // End of _couriermta_update_aliased_account().
|
| 549 |
|
| 550 |
|
| 551 |
/**
|
| 552 |
* Delete a mailbox account.
|
| 553 |
*/
|
| 554 |
function _couriermta_delete_mailbox_account(&$account) {
|
| 555 |
if ($mailboxdir = db_result(db_query('SELECT maildir FROM {couriermta_users} WHERE userid=%d', $account->uid))) {
|
| 556 |
// Update the deletemailbox file.
|
| 557 |
_couriermta_update_input_file('delete', 'mailbox', $mailboxdir .'
|
| 558 |
');
|
| 559 |
}
|
| 560 |
else {
|
| 561 |
// Update the deletemailbox file.
|
| 562 |
_couriermta_update_input_file('delete', 'mailbox', db_result(db_query('SELECT home FROM {couriermta_users} WHERE userid=%d', $account->uid)) .'
|
| 563 |
');
|
| 564 |
}
|
| 565 |
|
| 566 |
// Update the couriermta_users table.
|
| 567 |
if (!db_query_range('DELETE FROM {couriermta_users} WHERE userid=%d AND email="%s"', $account->uid, $account->mail, 0, 1)) {
|
| 568 |
watchdog('couriermta', t('The query to remove data from the couriermta_users table was not successfully executed.'), WATCHDOG_ERROR);
|
| 569 |
}
|
| 570 |
} // End of _couriermta_delete_mailbox_account().
|
| 571 |
|
| 572 |
|
| 573 |
/**
|
| 574 |
* Delete an alias account.
|
| 575 |
*/
|
| 576 |
function _couriermta_delete_aliased_account(&$account) {
|
| 577 |
// Update the deletealias file.
|
| 578 |
_couriermta_update_input_file('delete', 'alias', $account->mail .': '. db_result(db_query('SELECT alias FROM {couriermta_users} WHERE userid=%d', $account->uid)) .'
|
| 579 |
');
|
| 580 |
|
| 581 |
// Update the couriermta_users table.
|
| 582 |
if (!db_query_range('DELETE FROM {couriermta_users} WHERE userid=%d', $account->uid, 0, 1)) {
|
| 583 |
watchdog('couriermta', t('The query to remove data from the couriermta_users table was not successfully executed.'), WATCHDOG_ERROR);
|
| 584 |
}
|
| 585 |
} // End of _couriermta_delete_aliased_account().
|
| 586 |
|
| 587 |
|
| 588 |
/**
|
| 589 |
* Creates an array with the data for an SQL string which can be used to
|
| 590 |
* insert, update or delete data in the couriermta_users table.
|
| 591 |
*/
|
| 592 |
function _couriermta_create_user_sql_data(&$edit, &$account) {
|
| 593 |
$user_data = array();
|
| 594 |
$user_data['userid'] = $account->uid;
|
| 595 |
|
| 596 |
if (module_exists('token') && variable_get('couriermta_use_token_module', '0') == 1) {
|
| 597 |
if (variable_get('couriermta_email_users_address', '1') == 1) {
|
| 598 |
$user_data['email'] = $edit['mail'];
|
| 599 |
}
|
| 600 |
else {
|
| 601 |
$user_data['email'] = token_replace(variable_get('couriermta_email_pattern', ''));
|
| 602 |
}
|
| 603 |
|
| 604 |
if (variable_get('couriermta_password_users_password', '1') == 1) {
|
| 605 |
$user_data['password'] = $edit['pass'];
|
| 606 |
}
|
| 607 |
else {
|
| 608 |
$user_data['password'] = token_replace(variable_get('couriermta_password_encrypted', ''));
|
| 609 |
}
|
| 610 |
|
| 611 |
if (variable_get('couriermta_users_name', '1') == 1) {
|
| 612 |
$user_data['name'] = $edit['name'];
|
| 613 |
}
|
| 614 |
else {
|
| 615 |
$user_data['name'] = token_replace(variable_get('couriermta_name', ''));
|
| 616 |
}
|
| 617 |
|
| 618 |
$user_data['uid'] = token_replace(variable_get('couriermta_uid_pattern', '65534'));
|
| 619 |
$user_data['gid'] = token_replace(variable_get('couriermta_gid_pattern', '65534'));
|
| 620 |
|
| 621 |
$user_data['home'] = token_replace(variable_get('couriermta_home_pattern', '/var/spool/mail/'));
|
| 622 |
$user_data['maildir'] = token_replace(variable_get('couriermta_maildir_pattern', '/var/spool/mail/'));
|
| 623 |
|
| 624 |
$user_data['defaultdelivery'] = token_replace(variable_get('couriermta_defaultdelivery_pattern', ''));
|
| 625 |
$user_data['quota'] = token_replace(variable_get('couriermta_quota_pattern', ''));
|
| 626 |
$user_data['auxoptions'] = token_replace(variable_get('couriermta_options_pattern', ''));
|
| 627 |
}
|
| 628 |
else {
|
| 629 |
if (variable_get('couriermta_email_users_address', '1') == 1) {
|
| 630 |
$user_data['email'] = $edit['mail'];
|
| 631 |
}
|
| 632 |
else {
|
| 633 |
$user_data['email'] = variable_get('couriermta_email_pattern', '');
|
| 634 |
}
|
| 635 |
|
| 636 |
if (variable_get('couriermta_password_users_password', '1') == 1) {
|
| 637 |
$user_data['password'] = $edit['pass'];
|
| 638 |
}
|
| 639 |
else {
|
| 640 |
$user_data['password'] = variable_get('couriermta_password_encrypted', '');
|
| 641 |
}
|
| 642 |
|
| 643 |
if (variable_get('couriermta_users_name', '1') == 1) {
|
| 644 |
$user_data['name'] = $edit['name'];
|
| 645 |
}
|
| 646 |
else {
|
| 647 |
$user_data['name'] = variable_get('couriermta_name', '');
|
| 648 |
}
|
| 649 |
|
| 650 |
$user_data['uid'] = variable_get('couriermta_uid_pattern', '65534');
|
| 651 |
$user_data['gid'] = variable_get('couriermta_gid_pattern', '65534');
|
| 652 |
|
| 653 |
$user_data_home = variable_get('couriermta_home_pattern', '/var/spool/mail/!domain_name/!user_name');
|
| 654 |
if (strstr ($user_data_home, '!domain_name')) {
|
| 655 |
if (strstr ($user_data_home, '!user_name')) {
|
| 656 |
$user_data_home = str_replace('!user_name', substr($edit['mail'], 0, strpos($edit['mail'], '@')), $user_data_home);
|
| 657 |
$user_data_home = str_replace('!domain_name', substr($edit['mail'], strpos($edit['mail'], '@') + 1, strlen($edit['mail'])), $user_data_home);
|
| 658 |
}
|
| 659 |
else {
|
| 660 |
$user_data_home = str_replace('!domain_name', substr($edit['mail'], strpos($edit['mail'], '@') + 1, strlen($edit['mail'])), $user_data_home);
|
| 661 |
}
|
| 662 |
}
|
| 663 |
else {
|
| 664 |
if (strstr ($user_data_home, '!user_name')) {
|
| 665 |
$user_data_home = str_replace('!user_name', substr($edit['mail'], 0, strpos($edit['mail'], '@')), $user_data_home);
|
| 666 |
}
|
| 667 |
else {
|
| 668 |
}
|
| 669 |
}
|
| 670 |
$user_data['home'] = $user_data_home;
|
| 671 |
|
| 672 |
$user_data_maildir = variable_get('couriermta_maildir_pattern', '/var/spool/mail/!domain_name/!user_name');
|
| 673 |
if (strstr ($user_data_maildir, '!domain_name')) {
|
| 674 |
if (strstr ($user_data_maildir, '!user_name')) {
|
| 675 |
$user_data_maildir = str_replace('!user_name', substr($edit['mail'], 0, strpos($edit['mail'], '@')), $user_data_maildir);
|
| 676 |
$user_data_maildir = str_replace('!domain_name', substr($edit['mail'], strpos($edit['mail'], '@') + 1, strlen($edit['mail'])), $user_data_maildir);
|
| 677 |
}
|
| 678 |
else {
|
| 679 |
$user_data_maildir = str_replace('!domain_name', substr($edit['mail'], strpos($edit['mail'], '@') + 1, strlen($edit['mail'])), $user_data_maildir);
|
| 680 |
}
|
| 681 |
}
|
| 682 |
else {
|
| 683 |
if (strstr ($user_data_maildir, '!user_name')) {
|
| 684 |
$user_data_maildir = str_replace('!user_name', substr($edit['mail'], 0, strpos($edit['mail'], '@')), $user_data_maildir);
|
| 685 |
}
|
| 686 |
}
|
| 687 |
$user_data['maildir'] = $user_data_maildir;
|
| 688 |
|
| 689 |
$user_data['defaultdelivery'] = variable_get('couriermta_defaultdelivery_pattern', '');
|
| 690 |
$user_data['quota'] = variable_get('couriermta_quota_pattern', '');
|
| 691 |
$user_data['auxoptions'] = variable_get('couriermta_options_pattern', '');
|
| 692 |
}
|
| 693 |
|
| 694 |
if (variable_get('couriermta_allow_email_alias', '0') == 1) {
|
| 695 |
$user_data['alias'] = $edit['couriermta_email_alias_address'];
|
| 696 |
}
|
| 697 |
|
| 698 |
$user_data['status'] = $edit['status'];
|
| 699 |
|
| 700 |
return $user_data;
|
| 701 |
} // End of _couriermta_create_user_sql_data().
|
| 702 |
|
| 703 |
|
| 704 |
/**
|
| 705 |
* Updates the relevant input file.
|
| 706 |
*/
|
| 707 |
function _couriermta_update_input_file($operation, $email_type, $file_contents) {
|
| 708 |
$file_name = drupal_get_path('module', 'couriermta') .'/'. $operation . $email_type;
|
| 709 |
$file_handle = fopen($file_name, 'a');
|
| 710 |
|
| 711 |
if (!$file_handle) {
|
| 712 |
watchdog('couriermta', t('The ') . $operation . $email_type . t(' file could not be opened. Check to make sure valid permissions are in place for ') . $file_name, WATCHDOG_ERROR);
|
| 713 |
}
|
| 714 |
else {
|
| 715 |
if (!fwrite($file_handle, $file_contents)) {
|
| 716 |
watchdog('couriermta', t('There was an error writing to the alias file.'), WATCHDOG_ERROR);
|
| 717 |
}
|
| 718 |
else {
|
| 719 |
watchdog('couriermta', 'Successfully wrote to '. $file_name .'.');
|
| 720 |
}
|
| 721 |
|
| 722 |
if (!fclose($file_handle)) {
|
| 723 |
watchdog('couriermta', t('There was an error closing the alias file.'), WATCHDOG_ERROR);
|
| 724 |
}
|
| 725 |
}
|
| 726 |
} // End of _couriermta_update_input_file().
|