Commit e1a37bf1 authored by Till Brehm's avatar Till Brehm

Initial commit for CSV mailbox import tool.

parent a1a3f26f
Author: Till Brehm, ISPConfig UG
https://git.ispconfig.org/u/tbrehm
ISPConfig version: 3.0.5
This addon is a CSV style importer for Email accounts. It creates mailboxes based
on an account list in CSV format. Email domains get added automatically when they
do not exist.
Installation
--------------------
Download the addon to your server and run the install.sh script to install it.
Usage
--------------------
The tool installs itself into the tools menu in ISPConfig. An example CSV file
that shows the required format for the Mailbox import is part of the addon.
#!/bin/bash
mv -f mailbox_import.php /usr/local/ispconfig/interface/web/tools/
mv -f mailbox_import.htm /usr/local/ispconfig/interface/web/tools/templates/
mv -f mailbox_import.menu.php /usr/local/ispconfig/interface/web/tools/lib/menu.d/
chown ispconfig:ispconfig /usr/local/ispconfig/interface/web/tools/mailbox_import.php
chown ispconfig:ispconfig /usr/local/ispconfig/interface/web/tools/templates/mailbox_import.htm
chown ispconfig:ispconfig /usr/local/ispconfig/interface/web/tools/lib/menu.d/mailbox_import.menu.php
\ No newline at end of file
<h2><tmpl_var name="list_head_txt"></h2>
<p><tmpl_var name="list_desc_txt"></p>
<div class="panel panel_language_import">
<div class="pnl_formsarea">
<fieldset class="inlineLabels"><legend>Mailbox Import</legend>
<div class="ctrlHolder">
<label for="lng_select">Mailbox list file</label>
<input name="file" id="file" size="30" type="file" class="fileUpload" />
</div>
<div class="ctrlHolder">
<p class="label">Client</p>
<div class="multiField">
<select name="groupid">{tmpl_var name='clientlist'}</select>
</div>
</div>
<div class="ctrlHolder">
<p class="label">Test only</p>
<div class="multiField">
<input id="test" type="checkbox" value="1" name="test" checked/> (run a test without creating the accounts)
</div>
</div>
</fieldset>
<tmpl_if name="has_emails">
<div id="OKMsg">
<table style="width:100%">
<tr>
<td style="background-color:#cccccc">Name</td>
<td style="background-color:#cccccc">Email</td>
<td style="background-color:#cccccc">Password</td>
<td style="background-color:#cccccc">Quota</td>
<td style="background-color:#cccccc">Status</td>
</tr>
<tmpl_loop name="email_list">
<tr>
<td><tmpl_var name="name"></td>
<td><tmpl_var name="email"></td>
<td><tmpl_var name="password"></td>
<td><tmpl_var name="quota"> MB</td>
<td><tmpl_var name="status"></td>
</tr>
</tmpl_loop>
</table>
</div>
</tmpl_if>
<input type="hidden" name="id" value="{tmpl_var name='id'}">
<div class="buttonHolder buttons">
<button class="positive iconstxt icoPositive" type="button" value="Import" onClick="submitUploadForm('pageForm','tools/mailbox_import.php');"><span>Import</span></button>
<button class="negative iconstxt icoNegative" type="button" value="Cancel" onClick="loadContent('tools/index.php');"><span>Cancel</span></button>
</div>
</div>
</div>
<?php
// Menu
if($app->auth->is_admin()) {
$items = array();
$items[] = array( 'title' => 'Mailbox import',
'target' => 'content',
'link' => 'tools/mailbox_import.php');
$module['nav'][] = array( 'title' => 'Import',
'open' => 1,
'items' => $items);
unset($items);
}
?>
\ No newline at end of file
<?php
/*
Copyright (c) 2015, Till Brehm, ISPConfig UG
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of ISPConfig nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
require_once('../../lib/config.inc.php');
require_once('../../lib/app.inc.php');
//* Check permissions for module
$app->auth->check_module_permissions('admin');
//* This is only allowed for administrators
if(!$app->auth->is_admin()) die('only allowed for administrators.');
$app->uses('tpl');
$app->tpl->newTemplate('form.tpl.htm');
$app->tpl->setInclude('content_tpl', 'templates/mailbox_import.htm');
$msg = '';
$error = '';
if(isset($_FILES['file']['name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
$lines = file($_FILES['file']['tmp_name']);
unset($lines[0]);
$email_list = array();
$sys_groupid = intval($_POST['groupid']);
//$server_id = 1;
$sys_user = $app->db->queryOneRecord("SELECT * FROM sys_user WHERE default_group = $sys_groupid");
$sys_userid = $sys_user['userid'];
// get the default mailserver of the client
$tmp = $app->db->queryOneRecord('SELECT default_mailserver FROM sys_group,client WHERE sys_group.client_id = client.client_id and sys_group.groupid = '.$sys_groupid);
$server_id = intval($tmp['default_mailserver']);
unset($tmp);
if($server_id == 0) die('Invalid server ID');
foreach($lines as $line) {
$parts = explode(';',$line);
// login name; full name; mail address;password;mailbox quota
$login_name = $app->db->quote(trim($parts[0]));
$name = $app->db->quote(trim($parts[1]));
$email = strtolower(trim($parts[2]));
$password = $app->db->quote(trim($parts[3]));
$quota = intval($parts[4]);
$email_parts = $parts = explode('@',$email);
$user = $app->db->quote($email_parts[0]);
$domain = $app->db->quote($email_parts[1]);
// Check if email domain exists
$domain_rec = $app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = '$domain'");
if($_POST['test'] != 1 && !is_array($domain_rec)) {
$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `domain`, `active`)
VALUES($sys_userid, $sys_groupid, 'riud', 'riud', '', $server_id, '$domain', 'y')";
$app->db->datalogInsert('mail_domain', $insert_data, 'domain_id');
}
// encrypt the password
$salt="$1$";
$base64_alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
for ($n=0;$n<8;$n++) {
//$salt.=chr(mt_rand(64,126));
$salt.=$base64_alphabet[mt_rand(0,63)];
}
$salt.="$";
$password_enc = crypt($password,$salt);
$rec = $app->db->queryOneRecord("SELECT count(mailuser_id) as number FROM mail_user WHERE email = '".$user.'@'.$domain."'");
if($rec['number'] > 0) {
$status = 'Duplicate';
} else {
$status = 'OK';
if($_POST['test'] == 1) $status = 'Test: OK';
}
$tmp = array();
$tmp['name'] = $name;
$tmp['email'] = $user.'@'.$domain;
$tmp['quota'] = $quota;
$tmp['password'] = $password;
$tmp['status'] = $status;
$email_list[] = $tmp;
if($_POST['test'] != 1 && $status = 'OK') {
$app->uses('getconf');
$mail_config = $app->getconf->get_server_config($server_id,'mail');
$maildir = str_replace("[domain]",$domain,$mail_config["maildir_path"]);
$maildir = str_replace("[localpart]",strtolower($user),$maildir);
$homedir = $mail_config["homedir_path"];
$uid = $mail_config["mailuser_uid"];
$gid = $mail_config["mailuser_gid"];
$quota_bytes = $quota*1024*1024;
$insert_data = "(sys_userid,sys_groupid,sys_perm_user,sys_perm_group,sys_perm_other,server_id,
email,`password`,name,uid,gid,maildir,quota,homedir,postfix,access)
VALUES ('$sys_userid','$sys_groupid','riud','riud','','$server_id','".$user.'@'.$domain."','$password_enc',
'$name','$uid','$gid','$maildir','$quota_bytes','$homedir','y','n')";
//die('INSERT INTO mail_user '.$insert_data);
$app->db->datalogInsert('mail_user', $insert_data, 'mailuser_id');
}
}
$app->tpl->setLoop('email_list',$email_list);
$app->tpl->setVar('has_emails',1);
}
$app->tpl->setVar('msg',$msg);
$app->tpl->setVar('error',$error);
// Set the client list
$clients = $app->db->queryAllRecords("SELECT groupid,name FROM sys_group WHERE 1");
$clientlist = '';
foreach($clients as $c) {
$clientlist .= "<option value='$c[groupid]'>$c[name]</option>";
}
$app->tpl->setVar('clientlist',$clientlist);
//* load language file
/*
$lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_mailbox_import.lng';
include($lng_file);
$app->tpl->setVar($wb);
*/
$app->tpl_defaults();
$app->tpl->pparse();
?>
\ No newline at end of file
login name; full name; mail address;password;mailbox quota
johnlogin; John Doe; john@johnsdomain.tld;12345;28
janelogin; Jane Doe; jane@janesdomain.tld;12345;6
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment