| 1 |
<?php
|
| 2 |
|
| 3 |
/* $Id: mailserver.module,v 1.1.2.1 2008/02/04 12:58:28 tbarregren Exp $
|
| 4 |
*
|
| 5 |
* Copyright (C) 2008 Thomas Barregren.
|
| 6 |
*
|
| 7 |
* This program is free software; you can redistribute it and/or modify
|
| 8 |
* it under the terms of the GNU General Public License as published by
|
| 9 |
* the Free Software Foundation; either version 2 of the License, or
|
| 10 |
* (at your option) any later version.
|
| 11 |
*
|
| 12 |
* This program is distributed in the hope that it will be useful,
|
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 15 |
* GNU General Public License for more details.
|
| 16 |
*
|
| 17 |
* You should have received a copy of the GNU General Public License along
|
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc.,
|
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
| 20 |
*/
|
| 21 |
|
| 22 |
|
| 23 |
/**
|
| 24 |
* @file
|
| 25 |
* MailServer - a Drupal module that automatically creates, updates and removes
|
| 26 |
* e-mail accounts for users with an e-mail domain that the module is
|
| 27 |
* configured to manage.
|
| 28 |
*
|
| 29 |
* The e-mail accounts must be on a mail server with Postfix and MySQL
|
| 30 |
* authentication as described in Christoph Haas' HOWTO: "ISP-style Email
|
| 31 |
* Server with Debian-Etch and Postfix" at
|
| 32 |
*
|
| 33 |
* http://workaround.org/articles/ispmail-etch/
|
| 34 |
*
|
| 35 |
* The HOWTO is written for Debian Etch, but it can also be used for Ubuntu
|
| 36 |
* Gutsy Gibbon.
|
| 37 |
*
|
| 38 |
* Author:
|
| 39 |
* Thomas Barregren <http://drupal.org/user/16678>.
|
| 40 |
*
|
| 41 |
* Sponsors:
|
| 42 |
* Norrlandsflyg <http://www.norrlandsflyg.se/>.
|
| 43 |
* Webbredaktören <http://www.webbredaktoren.se/>.
|
| 44 |
*/
|
| 45 |
|
| 46 |
|
| 47 |
/*****************************************************************************
|
| 48 |
* PERMISSION HOOK
|
| 49 |
*****************************************************************************/
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Implementation of hook_perm().
|
| 53 |
*/
|
| 54 |
function mailserver_perm() {
|
| 55 |
return array('administer mailserver');
|
| 56 |
}
|
| 57 |
|
| 58 |
|
| 59 |
/*****************************************************************************
|
| 60 |
* MENU HOOK
|
| 61 |
*****************************************************************************/
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implementation of hook_menu().
|
| 65 |
*/
|
| 66 |
function mailserver_menu($may_cache) {
|
| 67 |
$items = array();
|
| 68 |
if (!$may_cache) {
|
| 69 |
$items[] = array(
|
| 70 |
'path' => 'admin/settings/mailserver',
|
| 71 |
'type' => MENU_NORMAL_ITEM,
|
| 72 |
'title' => t('MailServer'),
|
| 73 |
'callback' => 'drupal_get_form',
|
| 74 |
'callback arguments' => array('mailserver_form_settings'),
|
| 75 |
'access' => user_access('administer mailserver'),
|
| 76 |
);
|
| 77 |
}
|
| 78 |
return $items;
|
| 79 |
}
|
| 80 |
|
| 81 |
|
| 82 |
/*****************************************************************************
|
| 83 |
* USER HOOK
|
| 84 |
*****************************************************************************/
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Implementation of hook_user().
|
| 88 |
*/
|
| 89 |
function mailserver_user($op, &$edit, &$account, $category = null) {
|
| 90 |
static $accepted = array('insert', 'update', 'delete');
|
| 91 |
$args = array(&$edit, &$account, $category);
|
| 92 |
return _mailserver_dispatcher(__FUNCTION__, $op, $args, $accepted);
|
| 93 |
}
|
| 94 |
|
| 95 |
function _mailserver_user_insert_hook(&$edit, &$account) {
|
| 96 |
list($user, $domain) = mailserver_get_mail_components($edit['mail']);
|
| 97 |
if ($doman_id = mailserver_domain_get_id($domain)) {
|
| 98 |
mailserver_user_insert($account->uid, $doman_id, $user, $edit['pass']);
|
| 99 |
_mailserver_log('MailServer', 'The e-mail account %address has been created.', array('%address' => $edit['mail']), WATCHDOG_NOTICE);
|
| 100 |
}
|
| 101 |
}
|
| 102 |
|
| 103 |
function _mailserver_user_update_hook(&$edit, &$account) {
|
| 104 |
if ($edit['mail'] != $account->mail) {
|
| 105 |
_mailserver_user_delete_hook(&$edit, &$account);
|
| 106 |
_mailserver_user_insert_hook(&$edit, &$account);
|
| 107 |
}
|
| 108 |
elseif ($edit['pass']) {
|
| 109 |
mailserver_user_update_password($account->uid, $edit['pass']);
|
| 110 |
_mailserver_log('MailServer', 'The password for the e-mail account %address has been changed.', array('%address' => $account->mail), WATCHDOG_NOTICE);
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
function _mailserver_user_delete_hook(&$edit, &$account) {
|
| 115 |
mailserver_user_delete($account->uid);
|
| 116 |
_mailserver_log('MailServer', 'The e-mail account %address has been deleted.', array('%address' => $account->mail), WATCHDOG_NOTICE);
|
| 117 |
}
|
| 118 |
|
| 119 |
|
| 120 |
/*****************************************************************************
|
| 121 |
* SETTINGS
|
| 122 |
*****************************************************************************/
|
| 123 |
|
| 124 |
function mailserver_form_settings($form_values = null) {
|
| 125 |
|
| 126 |
if ($form_values) {
|
| 127 |
$form = mailserver_form_settings_confirm($form_values);
|
| 128 |
unset($form['#redirect']);
|
| 129 |
}
|
| 130 |
else {
|
| 131 |
$form = mailserver_form_settings_page($form);
|
| 132 |
$form['#redirect'] = false;
|
| 133 |
}
|
| 134 |
|
| 135 |
$form['#multistep'] = true;
|
| 136 |
|
| 137 |
return $form;
|
| 138 |
|
| 139 |
}
|
| 140 |
|
| 141 |
function mailserver_form_settings_page() {
|
| 142 |
|
| 143 |
$form['domains'] = array(
|
| 144 |
'#type' => 'textarea',
|
| 145 |
'#title' => 'Managed domains',
|
| 146 |
'#description' => t('For each mail domain to be manged by the MailServer module, add a line with the domain name, e.g. <em>example.com</em>.'),
|
| 147 |
'#default_value' => implode("\n", mailserver_domain_get_all()),
|
| 148 |
);
|
| 149 |
|
| 150 |
$form['submit'] = array(
|
| 151 |
'#type' => 'submit',
|
| 152 |
'#value' => t('Update domains'),
|
| 153 |
);
|
| 154 |
|
| 155 |
return $form;
|
| 156 |
|
| 157 |
}
|
| 158 |
|
| 159 |
function mailserver_form_settings_confirm($form_values) {
|
| 160 |
|
| 161 |
// Partition the old domains and the new domains into sets of domains to
|
| 162 |
// keep, insert and delete.
|
| 163 |
$new_domains = mailserver_get_lines($form_values['domains']);
|
| 164 |
$old_domains = mailserver_domain_get_all();
|
| 165 |
$keep_domains = array_intersect($new_domains, $old_domains);
|
| 166 |
$insert_domains = array_diff($new_domains, $keep_domains);
|
| 167 |
$delete_domains = array_diff($old_domains, $keep_domains);
|
| 168 |
|
| 169 |
// Store the domains to be inserted and delete for later retrieval by the
|
| 170 |
// submit function.
|
| 171 |
$form['domains'] = array(
|
| 172 |
'#type' => 'value',
|
| 173 |
'#value' => array(
|
| 174 |
'insert' => $insert_domains,
|
| 175 |
'delete' => $delete_domains,
|
| 176 |
),
|
| 177 |
);
|
| 178 |
|
| 179 |
// Build message.
|
| 180 |
if ($keep_domains) {
|
| 181 |
$form['keep_message'] = array(
|
| 182 |
'#value' => '<h3>'. t('Keep') ."</h3>\n<p>". t('Following domains are unaffected:') .'</p>'. theme('item_list', $keep_domains),
|
| 183 |
);
|
| 184 |
}
|
| 185 |
if ($insert_domains) {
|
| 186 |
$form['insert_message'] = array(
|
| 187 |
'#value' => '<h3>'. t('Create') ."</h3>\n<p>". t('Following domains will be added:') .'</p>'. theme('item_list', $insert_domains),
|
| 188 |
);
|
| 189 |
}
|
| 190 |
if ($delete_domains) {
|
| 191 |
$form['delete_message'] = array(
|
| 192 |
'#value' => '<h3>'. t('Delete') ."</h3>\n<p>". t('<strong>WARNING: Following domains will be <em>deleted</em> along with all its e-mail accounts</strong>:') .'</p>'. theme('item_list', $delete_domains),
|
| 193 |
);
|
| 194 |
}
|
| 195 |
|
| 196 |
// Build the confirmation form.
|
| 197 |
$form = confirm_form($form, t('Confirmation'), 'admin/settings/mailserver', t('Would you like to continue?'), t('Continue'), t('Cancel'));
|
| 198 |
|
| 199 |
return $form;
|
| 200 |
|
| 201 |
}
|
| 202 |
|
| 203 |
function mailserver_form_settings_submit($form_id, $form_values) {
|
| 204 |
if (!$form_values['confirm']) return;
|
| 205 |
if ($insert_domains = $form_values['domains']['insert']) {
|
| 206 |
mailserver_domain_insert($insert_domains);
|
| 207 |
}
|
| 208 |
if ($delete_domains = $form_values['domains']['delete']) {
|
| 209 |
mailserver_domain_delete($delete_domains);
|
| 210 |
}
|
| 211 |
$var = array(
|
| 212 |
'!created' => format_plural(count($insert_domains), '1 e-mail domain', '@count e-mail domains'),
|
| 213 |
'!deleted' => format_plural(count($delete_domains), '1 e-mail domain', '@count e-mail domains'),
|
| 214 |
);
|
| 215 |
_mailserver_log('MailServer', '!created has been created and !deleted has been deleted.', $var, WATCHDOG_NOTICE);
|
| 216 |
}
|
| 217 |
|
| 218 |
|
| 219 |
/*****************************************************************************
|
| 220 |
* DOMAINS
|
| 221 |
*****************************************************************************/
|
| 222 |
|
| 223 |
function mailserver_domain_get_all() {
|
| 224 |
return _mailserver_db_get_domains();
|
| 225 |
}
|
| 226 |
|
| 227 |
function mailserver_domain_get_id($domain) {
|
| 228 |
return _mailserver_db_get_domain_id($domain);
|
| 229 |
}
|
| 230 |
|
| 231 |
function mailserver_domain_insert($domains) {
|
| 232 |
_mailserver_db_insert_domains($domains);
|
| 233 |
_mailserver_db_insert_users($domains);
|
| 234 |
}
|
| 235 |
|
| 236 |
function mailserver_domain_delete($domains) {
|
| 237 |
_mailserver_db_delete_domains($domains);
|
| 238 |
}
|
| 239 |
|
| 240 |
|
| 241 |
/*****************************************************************************
|
| 242 |
* USERS
|
| 243 |
*****************************************************************************/
|
| 244 |
|
| 245 |
function mailserver_user_insert($user_id, $domain_id, $user, $password) {
|
| 246 |
_mailserver_db_insert_user($user_id, $domain_id, $user, $password);
|
| 247 |
}
|
| 248 |
|
| 249 |
function mailserver_user_delete($user_id) {
|
| 250 |
_mailserver_db_delete_user($user_id);
|
| 251 |
}
|
| 252 |
|
| 253 |
function mailserver_user_update_password($user_id, $password) {
|
| 254 |
_mailserver_db_update_user($user_id, $password);
|
| 255 |
}
|
| 256 |
|
| 257 |
|
| 258 |
/*****************************************************************************
|
| 259 |
* DATABASE
|
| 260 |
*****************************************************************************/
|
| 261 |
|
| 262 |
function _mailserver_db_get_domains() {
|
| 263 |
$domains = array();
|
| 264 |
if ($db_result = _mailserver_db_query(__FUNCTION__)) {
|
| 265 |
while ($domain = db_fetch_object($db_result)) {
|
| 266 |
$domains[$domain->id] = $domain->name;
|
| 267 |
}
|
| 268 |
}
|
| 269 |
return $domains;
|
| 270 |
}
|
| 271 |
|
| 272 |
function _mailserver_db_get_domain_id($domain) {
|
| 273 |
if ($db_result = _mailserver_db_query(__FUNCTION__, $domain)) {
|
| 274 |
if ($domain = db_fetch_object($db_result)) {
|
| 275 |
return $domain->id;
|
| 276 |
}
|
| 277 |
}
|
| 278 |
}
|
| 279 |
|
| 280 |
function _mailserver_db_insert_domains($domains) {
|
| 281 |
_mailserver_db_query(__FUNCTION__, $domains);
|
| 282 |
}
|
| 283 |
|
| 284 |
function _mailserver_db_insert_users($domains) {
|
| 285 |
_mailserver_db_query(__FUNCTION__, $domains);
|
| 286 |
}
|
| 287 |
|
| 288 |
function _mailserver_db_delete_domains($domains) {
|
| 289 |
_mailserver_db_query(__FUNCTION__, $domains);
|
| 290 |
}
|
| 291 |
|
| 292 |
function _mailserver_db_insert_user($user_id, $domain_id, $user, $password) {
|
| 293 |
_mailserver_db_query(__FUNCTION__, $user_id, $domain_id, $user, $password);
|
| 294 |
}
|
| 295 |
|
| 296 |
function _mailserver_db_delete_user($user_id) {
|
| 297 |
_mailserver_db_query(__FUNCTION__, $user_id);
|
| 298 |
}
|
| 299 |
|
| 300 |
function _mailserver_db_update_user($user_id, $password) {
|
| 301 |
_mailserver_db_query(__FUNCTION__, $password, $user_id);
|
| 302 |
}
|
| 303 |
|
| 304 |
|
| 305 |
/*****************************************************************************
|
| 306 |
* HELPERS
|
| 307 |
*****************************************************************************/
|
| 308 |
|
| 309 |
/**
|
| 310 |
* Dispatch the call of the hook $hook to appropriate handler.
|
| 311 |
*/
|
| 312 |
function _mailserver_dispatcher($hook, $op, $args, &$accepted = null) {
|
| 313 |
if ($accepted !== null && !in_array($op, $accepted)) return;
|
| 314 |
$function = '_'. $hook .'_'. $op .'_hook';
|
| 315 |
if ($accepted === null && !function_exists($function)) return;
|
| 316 |
return call_user_func_array($function, $args);
|
| 317 |
}
|
| 318 |
|
| 319 |
/**
|
| 320 |
* Similar to watchdog() in D6, but also output the message on screen.
|
| 321 |
*/
|
| 322 |
function _mailserver_log($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = null) {
|
| 323 |
$message = t($message, $variables);
|
| 324 |
watchdog($type, $message, $variables, $severity, $link);
|
| 325 |
drupal_set_message($message, $severity < WATCHDOG_WARNING ? 'status' : 'error');
|
| 326 |
}
|
| 327 |
|
| 328 |
/**
|
| 329 |
* Returns an array with the name and domain of a e-mail address $mail.
|
| 330 |
*/
|
| 331 |
function mailserver_get_mail_components($mail) {
|
| 332 |
$pos = strpos($mail, '@');
|
| 333 |
if ($pos === false) return;
|
| 334 |
return array(substr($mail, 0, $pos), substr($mail, $pos + 1));
|
| 335 |
}
|
| 336 |
|
| 337 |
/**
|
| 338 |
* Returns an array with the lines in the string $string.
|
| 339 |
*/
|
| 340 |
function mailserver_get_lines($string) {
|
| 341 |
$lines = preg_split('/(\n|\r\n?)/', $string);
|
| 342 |
foreach ($lines as $key => &$line) {
|
| 343 |
$line = trim($line);
|
| 344 |
if (!$line) {
|
| 345 |
unset($lines[$key]);
|
| 346 |
}
|
| 347 |
}
|
| 348 |
return $lines;
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
* Returns a string with $token repeated $n times and $glue inserted inbetween.
|
| 353 |
*/
|
| 354 |
function mailserver_token_string($n, $token, $glue = '') {
|
| 355 |
$str = str_repeat($token . $glue, $n);
|
| 356 |
$str = substr($str, 0, -strlen($glue));
|
| 357 |
return $str;
|
| 358 |
}
|
| 359 |
|
| 360 |
/**
|
| 361 |
* Returns the result of the database specific query named $query_name.
|
| 362 |
*/
|
| 363 |
function _mailserver_db_query($query_name) {
|
| 364 |
|
| 365 |
static $included = false;
|
| 366 |
// Include the database specific file with queries.
|
| 367 |
if (!$included) {
|
| 368 |
global $db_type;
|
| 369 |
$db = $db_type == 'mysqli' ? 'mysql' : $db_type;
|
| 370 |
$db_file = "mailserver.$db.inc";
|
| 371 |
if (!is_file(drupal_get_path('module', 'mailserver') ."/$db_file")) {
|
| 372 |
_mailserver_log('MailServer', 'No support for %db.', array('%db' => $db_type), WATCHDOG_CRITICAL);
|
| 373 |
return false;
|
| 374 |
}
|
| 375 |
include_once $db_file;
|
| 376 |
$included = true;
|
| 377 |
}
|
| 378 |
|
| 379 |
// Get the arguments.
|
| 380 |
$args = func_get_args();
|
| 381 |
array_shift($args);
|
| 382 |
if (isset($args[0]) and is_array($args[0])) {
|
| 383 |
$args = $args[0];
|
| 384 |
}
|
| 385 |
|
| 386 |
// Get the query, and new arguments, if any.
|
| 387 |
$query_function = $query_name .'_query';
|
| 388 |
if (!function_exists($query_function)) {
|
| 389 |
_mailserver_log('MailServer', "Can't find the %query for %db", array('%query' => $query_name, '%db' => $db_type), WATCHDOG_ERROR);
|
| 390 |
return false;
|
| 391 |
}
|
| 392 |
$query = $query_function($args);
|
| 393 |
|
| 394 |
// Build the query.
|
| 395 |
$query = db_prefix_tables($query);
|
| 396 |
_db_query_callback($args, true);
|
| 397 |
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
|
| 398 |
|
| 399 |
// Do the database query, and return the status.
|
| 400 |
return _db_query($query);
|
| 401 |
|
| 402 |
}
|
| 403 |
|