| 1 |
<?php
|
| 2 |
|
| 3 |
/* $Id: mailserver.install,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 |
* Installer script for the MailServer – a Drupal module that automatically
|
| 26 |
* creates, updates and removes e-mail accounts for users with an e-mail domain
|
| 27 |
* that the module is configured to manage.
|
| 28 |
*
|
| 29 |
* Author:
|
| 30 |
* Thomas Barregren <http://drupal.org/user/16678>.
|
| 31 |
*/
|
| 32 |
|
| 33 |
|
| 34 |
/******************************************************************************
|
| 35 |
* MYSQL
|
| 36 |
******************************************************************************/
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Install.
|
| 40 |
*/
|
| 41 |
function mailserver_install_mysql() {
|
| 42 |
|
| 43 |
_mailserver_install_db_query(<<<SQL
|
| 44 |
CREATE TABLE IF NOT EXISTS {mailserver_virtual_domains} (
|
| 45 |
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
| 46 |
name VARCHAR(50) NOT NULL
|
| 47 |
) ENGINE = InnoDB /*!40100 DEFAULT CHARACTER SET utf8 */;
|
| 48 |
SQL
|
| 49 |
);
|
| 50 |
|
| 51 |
_mailserver_install_db_query(<<<SQL
|
| 52 |
CREATE TABLE {mailserver_virtual_users} (
|
| 53 |
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
| 54 |
domain_id INT(11) NOT NULL,
|
| 55 |
user VARCHAR(40) NOT NULL,
|
| 56 |
password VARCHAR(32) NOT NULL,
|
| 57 |
CONSTRAINT UNIQUE_EMAIL UNIQUE (domain_id, user),
|
| 58 |
FOREIGN KEY (domain_id) REFERENCES mailserver_virtual_domains(id) ON DELETE CASCADE
|
| 59 |
) ENGINE = InnoDB /*!40100 DEFAULT CHARACTER SET utf8 */;
|
| 60 |
SQL
|
| 61 |
);
|
| 62 |
|
| 63 |
_mailserver_install_db_query(<<<SQL
|
| 64 |
CREATE TABLE {mailserver_virtual_aliases} (
|
| 65 |
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
| 66 |
domain_id INT(11) NOT NULL,
|
| 67 |
source VARCHAR(40) NOT NULL,
|
| 68 |
destination VARCHAR(80) NOT NULL,
|
| 69 |
FOREIGN KEY (domain_id) REFERENCES mailserver_virtual_domains(id) ON DELETE CASCADE
|
| 70 |
) ENGINE = InnoDB /*!40100 DEFAULT CHARACTER SET utf8 */;
|
| 71 |
SQL
|
| 72 |
);
|
| 73 |
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Uninstall.
|
| 78 |
*/
|
| 79 |
function mailserver_uninstall_mysql() {
|
| 80 |
_mailserver_install_db_query('DROP TABLE {mailserver_virtual_domains}');
|
| 81 |
_mailserver_install_db_query('DROP TABLE {mailserver_virtual_users}');
|
| 82 |
_mailserver_install_db_query('DROP TABLE {mailserver_virtual_aliases}');
|
| 83 |
}
|
| 84 |
|
| 85 |
/******************************************************************************
|
| 86 |
* HOOKS
|
| 87 |
******************************************************************************/
|
| 88 |
|
| 89 |
/**
|
| 90 |
* Implements hook_install().
|
| 91 |
*/
|
| 92 |
function mailserver_install() {
|
| 93 |
_mailserver_install_dispatcher(__FUNCTION__);
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Implements hook_uninstall().
|
| 98 |
*/
|
| 99 |
function mailserver_uninstall() {
|
| 100 |
_mailserver_install_dispatcher(__FUNCTION__);
|
| 101 |
}
|
| 102 |
|
| 103 |
|
| 104 |
/******************************************************************************
|
| 105 |
* HELPERS
|
| 106 |
******************************************************************************/
|
| 107 |
|
| 108 |
/**
|
| 109 |
* Dispatch the call for $hook to the database specific implementation of it.
|
| 110 |
*/
|
| 111 |
function _mailserver_install_dispatcher($hook) {
|
| 112 |
global $db_type;
|
| 113 |
$db = $db_type === 'mysqli' ? 'mysql' : $db_type;
|
| 114 |
$op = substr($hook, strpos($hook, '_') + 1);
|
| 115 |
$function = _mailserver_install_module_name() .'_'. $op .'_'. $db;
|
| 116 |
if (function_exists($function)) {
|
| 117 |
return $function();
|
| 118 |
}
|
| 119 |
else {
|
| 120 |
$msg = t('!hook() doesn\'t support !db', array('!hook' => $hook, '!db' => $db));
|
| 121 |
drupal_set_message($msg, 'error');
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Execute a database query. Based on db_query() and update_sql().
|
| 127 |
*/
|
| 128 |
function _mailserver_install_db_query($query) {
|
| 129 |
|
| 130 |
// Get the arguments.
|
| 131 |
$args = func_get_args();
|
| 132 |
array_shift($args);
|
| 133 |
if (isset($args[0]) and is_array($args[0])) {
|
| 134 |
$args = $args[0];
|
| 135 |
}
|
| 136 |
|
| 137 |
// Build the query.
|
| 138 |
$query = db_prefix_tables($query);
|
| 139 |
_db_query_callback($args, true);
|
| 140 |
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
|
| 141 |
|
| 142 |
// Do the database query, and return the status.
|
| 143 |
$result = _db_query($query);
|
| 144 |
return array('success' => $result !== false, 'query' => check_plain($query));
|
| 145 |
|
| 146 |
}
|
| 147 |
|
| 148 |
/**
|
| 149 |
* Returns the name of this module.
|
| 150 |
*/
|
| 151 |
function _mailserver_install_module_name() {
|
| 152 |
return substr(__FUNCTION__, 1, strpos(__FUNCTION__, '_', 1) - 1);
|
| 153 |
}
|