Commit 14b85c78 authored by tbrehm's avatar tbrehm
Browse files

Added mail account import tool.

parent 0aa4efbc
<?php
/*
Copyright (c) 2012, Till Brehm, projektfarm Gmbh, 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,validate_dns');
$app->tpl->newTemplate('form.tpl.htm');
$app->tpl->setInclude('content_tpl', 'templates/import_ispconfig.htm');
$msg = '';
$error = '';
//* load language file
$lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_import_ispconfig.lng';
include($lng_file);
$app->tpl->setVar($wb);
if(isset($_POST['connected'])) {
$connected = intval($_POST['connected']);
if($connected == 0) {
//* Try to connect to remote server
if(empty($_POST['remote_server'])) $error .= 'Remote Server is empty.</br>';
if(empty($_POST['remote_user'])) $error .= 'Remote User is empty.</br>';
if(empty($_POST['remote_password'])) $error .= 'Remote Password is empty.</br>';
if($error == '') {
try {
$client = new SoapClient(null, array('location' => $_POST['remote_server'],
'uri' => $_POST['remote_server'].'/index.php',
'trace' => 1,
'exceptions' => 1));
if($remote_session_id = $client->login($_POST['remote_user'],$_POST['remote_password'])) {
$connected = 1;
$msg .= 'Successfully connected to remote server.';
}
} catch (SoapFault $e) {
//echo $client->__getLastResponse();
$error .= $e->getMessage();
$connected = 0;
}
}
}
if($connected == 1) {
//* Fill the client select field
$sql = "SELECT groupid, name FROM sys_group WHERE client_id > 0 ORDER BY name";
$clients = $app->db->queryAllRecords($sql);
$client_select = "";
if(is_array($clients)) {
foreach( $clients as $client) {
$selected = @($client['groupid'] == $_POST['client_group_id'])?'SELECTED':'';
$client_select .= "<option value='$client[groupid]' $selected>$client[name]</option>\r\n";
}
}
$app->tpl->setVar("client_group_id",$client_select);
try {
$client = new SoapClient(null, array('location' => $_POST['remote_server'],
'uri' => $_POST['remote_server'].'/index.php',
'trace' => 1,
'exceptions' => 1));
if(!isset($remote_session_id)) $remote_session_id = $_POST['remote_session_id'];
//* Get all email domains
$mail_domains = $client->mail_domain_get($remote_session_id, array('active' => 'y'));
$mail_domain_select = '<option value="">-- select domain --</option>';
if(is_array($mail_domains)) {
foreach( $mail_domains as $mail_domain) {
$selected = @($mail_domain['domain'] == $_POST['mail_domain'])?'SELECTED':'';
$mail_domain_select .= "<option value='$mail_domain[domain]' $selected>$mail_domain[domain]</option>\r\n";
}
}
$app->tpl->setVar("mail_domain",$mail_domain_select);
//* Do the import
if($_POST['mail_domain'] != '') start_domain_import($_POST['mail_domain']);
} catch (SoapFault $e) {
//echo $client->__getLastResponse();
$error .= $e->getMessage();
$connected = 0;
}
}
}
$app->tpl->setVar('remote_server',$_POST['remote_server']);
$app->tpl->setVar('remote_user',$_POST['remote_user']);
$app->tpl->setVar('remote_password',$_POST['remote_password']);
$app->tpl->setVar('connected',$connected);
$app->tpl->setVar('remote_session_id',$remote_session_id);
$app->tpl->setVar('msg',$msg);
$app->tpl->setVar('error',$error);
$app->tpl_defaults();
$app->tpl->pparse();
###########################################################
function start_domain_import($mail_domain) {
global $app, $conf, $client, $msg, $error, $remote_session_id;
//* Get the user and groupid for the new records
$sys_groupid = intval($_POST['client_group_id']);
$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $sys_groupid");
$sys_userid = intval($tmp['userid']);
unset($tmp);
if($sys_groupid == 0) $error .= 'Inavlid groupid<br />';
if($sys_userid == 0) $error .= 'Inavlid Userid<br />';
//* get the mail domain record
$mail_domain_rec = $client->mail_domain_get($remote_session_id, array('domain' => $mail_domain));
if(is_array($mail_domain_rec)) {
$mail_domain_rec = $mail_domain_rec[0];
$tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM mail_domain WHERE domain = '".$app->db->quote($mail_domain)."'");
if($tmp['number'] > 0) $error .= 'Domain '.$mail_domain.' exists already in local database.<br />';
unset($tmp);
//* Change the record owner and remove the index field
$mail_domain_rec['sys_userid'] = $sys_userid;
$mail_domain_rec['sys_groupid'] = $sys_groupid;
unset($mail_domain_rec['domain_id']);
//* Insert domain if no error occurred
if($error == '') {
$app->db->datalogInsert('mail_domain', $mail_domain_rec, 'domain_id');
$msg .= "Imported mail domain ".$mail_domain_rec['domain']."<br />";
} else {
return false;
}
//* Import mailboxes
if(isset($_POST['import_mailbox']) && $_POST['import_mailbox'] == 1) {
$mail_users = $client->mail_user_get($remote_session_id, array('email' => '%@'.$mail_domain));
if(is_array($mail_users)) {
foreach($mail_users as $mail_user) {
$tmp = $app->db->queryOneRecord("SELECT count(mailuser_id) as number FROM mail_user WHERE email = '".$app->db->quote($mail_user['email'])."'");
if($tmp['number'] == 0) {
//* Prepare record
$mail_user['sys_userid'] = $sys_userid;
$mail_user['sys_groupid'] = $sys_groupid;
$remote_mailuser_id = $mail_user['mailuser_id'];
unset($mail_user['mailuser_id']);
if(!isset($_POST['import_user_filter'])) $mail_user['custom_mailfilter'] = '';
//* Insert record in DB
$local_mailuser_id = $app->db->datalogInsert('mail_user', $mail_user, 'mailuser_id');
$msg .= "Imported mailbox ".$mail_user['email']."<br />";
//* Import mail user filters
if(isset($_POST['import_user_filter']) && $_POST['import_user_filter'] == 1 && $local_mailuser_id > 0) {
$mail_user_filters = $client->mail_user_filter_get($remote_session_id, array('mailuser_id' => $remote_mailuser_id));
if(is_array($mail_user_filters)) {
foreach($mail_user_filters as $mail_user_filter) {
$mail_user_filter['sys_userid'] = $sys_userid;
$mail_user_filter['sys_groupid'] = $sys_groupid;
$mail_user_filter['mailuser_id'] = $local_mailuser_id;
unset($mail_user_filter['filter_id']);
//* Insert record in DB
$app->db->datalogInsert('mail_user_filter', $mail_user_filter, 'filter_id');
$msg .= "Imported mailbox filter ".$mail_user['email'].": ".$mail_user_filter['rulename']."<br />";
}
}
}
} else {
$error .= "Mailbox ".$mail_user['email']." exists in local database. Skipped import of mailbox<br />";
}
}
}
}
//* Import email aliases
if(isset($_POST['import_alias']) && $_POST['import_alias'] == 1) {
$mail_aliases = $client->mail_alias_get($remote_session_id, array('type' => 'alias', 'destination' => '%@'.$mail_domain));
if(is_array($mail_aliases)) {
foreach($mail_aliases as $mail_alias) {
$tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE `type` = 'alias' AND source = '".$app->db->quote($mail_alias['source'])."' AND destination = '".$app->db->quote($mail_alias['destination'])."'");
if($tmp['number'] == 0) {
$mail_alias['sys_userid'] = $sys_userid;
$mail_alias['sys_groupid'] = $sys_groupid;
unset($mail_alias['forwarding_id']);
$app->db->datalogInsert('mail_forwarding', $mail_alias, 'forwarding_id');
$msg .= "Imported email alias ".$mail_alias['source']."<br />";
} else {
$error .= "Email alias ".$mail_alias['source']." exists in local database. Skipped import.<br />";
}
}
}
}
//* Import domain aliases
if(isset($_POST['import_aliasdomain']) && $_POST['import_aliasdomain'] == 1) {
$mail_aliases = $client->mail_alias_get($remote_session_id, array('type' => 'aliasdomain', 'destination' => '@'.$mail_domain));
if(is_array($mail_aliases)) {
foreach($mail_aliases as $mail_alias) {
$tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE `type` = 'aliasdomain' AND source = '".$app->db->quote($mail_alias['source'])."' AND destination = '".$app->db->quote($mail_alias['destination'])."'");
if($tmp['number'] == 0) {
$mail_alias['sys_userid'] = $sys_userid;
$mail_alias['sys_groupid'] = $sys_groupid;
unset($mail_alias['forwarding_id']);
$app->db->datalogInsert('mail_forwarding', $mail_alias, 'forwarding_id');
$msg .= "Imported email aliasdomain ".$mail_alias['source']."<br />";
} else {
$error .= "Email aliasdomain ".$mail_alias['source']." exists in local database. Skipped import.<br />";
}
}
}
}
//* Import email forward
if(isset($_POST['import_forward']) && $_POST['import_forward'] == 1) {
$mail_forwards = $client->mail_forward_get($remote_session_id, array('type' => 'forward', 'source' => '%@'.$mail_domain));
if(is_array($mail_forwards)) {
foreach($mail_forwards as $mail_forward) {
$tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE `type` = 'forward' AND source = '".$app->db->quote($mail_forward['source'])."' AND destination = '".$app->db->quote($mail_forward['destination'])."'");
if($tmp['number'] == 0) {
$mail_forward['sys_userid'] = $sys_userid;
$mail_forward['sys_groupid'] = $sys_groupid;
unset($mail_forward['forwarding_id']);
$app->db->datalogInsert('mail_forwarding', $mail_forward, 'forwarding_id');
$msg .= "Imported email forward ".$mail_forward['source']."<br />";
} else {
$error .= "Email forward ".$mail_forward['source']." exists in local database. Skipped import.<br />";
}
}
}
}
//* Import spamfilter
if(isset($_POST['import_spamfilter']) && $_POST['import_spamfilter'] == 1) {
$mail_spamfilters = $client->mail_spamfilter_user_get($remote_session_id, array('email' => '%@'.$mail_domain));
if(is_array($mail_spamfilters)) {
foreach($mail_spamfilters as $mail_spamfilter) {
$tmp = $app->db->queryOneRecord("SELECT count(id) as number FROM spamfilter_users WHERE email = '".$app->db->quote($mail_spamfilter['email'])."'");
if($tmp['number'] == 0) {
$mail_spamfilter['sys_userid'] = $sys_userid;
$mail_spamfilter['sys_groupid'] = $sys_groupid;
unset($mail_spamfilter['id']);
$app->db->datalogInsert('spamfilter_users', $mail_spamfilter, 'id');
$msg .= "Imported spamfilter user ".$mail_spamfilter['email']."<br />";
} else {
$error .= "Spamfilter user ".$mail_spamfilter['email']." exists in local database. Skipped import.<br />";
}
}
}
}
}
}
?>
\ No newline at end of file
......@@ -6,4 +6,5 @@ $wb['Password and Language'] = 'Password and Language';
$wb['ispconfig_tools_note'] = 'This module allows you to change the password and language and to start a resync of the DNS records.';
$wb['Sync Tools'] = 'Sync Tools';
$wb['Resync'] = 'Resync';
$wb['Import'] = 'Import';
?>
\ No newline at end of file
<?php
$wb['head_txt'] = 'Import email configuration from ISPConfig 3';
$wb['legend_txt'] = 'Remote server connection details';
$wb['legend2_txt'] = 'Import email domain';
$wb['resync_sites_txt'] = 'Resync Websites';
$wb['resync_ftp_txt'] = 'Resync FTP users';
$wb['resync_shell_txt'] = 'Resync shell users';
$wb['resync_cron_txt'] = 'Resync cronjobs';
$wb['resync_db_txt'] = 'Resync clientdb config';
$wb['resync_mailbox_txt'] = 'Resync Mailboxes';
$wb['resync_dns_txt'] = 'Resync DNS records';
$wb['btn_start_txt'] = 'Start Import';
$wb['btn_connect_txt'] = 'Connect to remote server';
$wb['btn_cancel_txt'] = 'Cancel';
$wb['client_group_id_txt'] = 'Local client';
$wb['mail_domain_txt'] = 'Remote email domain';
$wb['import_mailbox_txt'] = 'Import mailbox';
$wb['import_aliasdomain_txt'] = 'Import alias domain';
$wb['import_alias_txt'] = 'Import email alias';
$wb['import_forward_txt'] = 'Import forward';
$wb['import_user_filter_txt'] = 'Import user filter';
$wb['import_spamfilter_txt'] = 'Import spamfilter';
?>
\ No newline at end of file
......@@ -5,7 +5,7 @@ $wb['resync_sites_txt'] = 'Resync Websites';
$wb['resync_ftp_txt'] = 'Resync FTP users';
$wb['resync_shell_txt'] = 'Resync shell users';
$wb['resync_cron_txt'] = 'Resync cronjobs';
$wb['resync_db_txt'] = 'Resync database config';
$wb['resync_db_txt'] = 'Resync clientdb config';
$wb['resync_mailbox_txt'] = 'Resync Mailboxes';
$wb['resync_dns_txt'] = 'Resync DNS records';
$wb['btn_start_txt'] = 'Start';
......
<?php
// Menu
if($app->auth->is_admin()) {
$items = array();
$items[] = array( 'title' => 'ISPConfig 3',
'target' => 'content',
'link' => 'tools/import_ispconfig.php');
$module['nav'][] = array( 'title' => 'Import',
'open' => 1,
'items' => $items);
unset($items);
}
?>
\ No newline at end of file
<h2>{tmpl_var name="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>{tmpl_var name="legend_txt"}</legend>
<div class="ctrlHolder">
<p class="label">Remote API URL</p>
<div class="multiField">
<input id="remote_server" type="text" value="{tmpl_var name='remote_server'}" name="remote_server" /> (e.g. https://www.example.com:8080/remote/ )
</div>
</div>
<div class="ctrlHolder">
<p class="label">Remote User</p>
<div class="multiField">
<input id="remote_user" type="text" value="{tmpl_var name='remote_user'}" name="remote_user" />
</div>
</div>
<div class="ctrlHolder">
<p class="label">Remote password</p>
<div class="multiField">
<input id="remote_password" type="text" value="{tmpl_var name='remote_password'}" name="remote_password" />
</div>
</div>
</fieldset>
<tmpl_if name="connected">
<div class="pnl_formsarea">
<fieldset class="inlineLabels"><legend>{tmpl_var name="legend2_txt"}</legend>
<div class="ctrlHolder">
<label for="mail_domain">{tmpl_var name='mail_domain_txt'}</label>
<select name="mail_domain" id="mail_domain" class="selectInput">
{tmpl_var name='mail_domain'}
</select>
</div>
<div class="ctrlHolder">
<label for="client_group_id">{tmpl_var name='client_group_id_txt'}</label>
<select name="client_group_id" id="client_group_id" class="selectInput">
{tmpl_var name='client_group_id'}
</select>
</div>
<div class="ctrlHolder">
<p class="label">{tmpl_var name="import_mailbox_txt"}</p>
<div class="multiField">
<input id="import_mailbox" type="checkbox" value="1" name="import_mailbox" checked="checked" />
</div>
</div>
<div class="ctrlHolder">
<p class="label">{tmpl_var name="import_user_filter_txt"}</p>
<div class="multiField">
<input id="import_user_filter" type="checkbox" value="1" name="import_user_filter" checked="checked" />
</div>
</div>
<div class="ctrlHolder">
<p class="label">{tmpl_var name="import_spamfilter_txt"}</p>
<div class="multiField">
<input id="import_spamfilter" type="checkbox" value="1" name="import_spamfilter" checked="checked" />
</div>
</div>
<div class="ctrlHolder">
<p class="label">{tmpl_var name="import_alias_txt"}</p>
<div class="multiField">
<input id="import_alias" type="checkbox" value="1" name="import_alias" checked="checked" />
</div>
</div>
<div class="ctrlHolder">
<p class="label">{tmpl_var name="import_forward_txt"}</p>
<div class="multiField">
<input id="import_forward" type="checkbox" value="1" name="import_forward" checked="checked" />
</div>
</div>
<div class="ctrlHolder">
<p class="label">{tmpl_var name="import_aliasdomain_txt"}</p>
<div class="multiField">
<input id="import_aliasdomain" type="checkbox" value="1" name="import_aliasdomain" checked="checked" />
</div>
</div>
</fieldset>
</tmpl_if>
<tmpl_if name="msg">
<div id="OKMsg"><p><tmpl_var name="msg"></p></div>
</tmpl_if>
<tmpl_if name="error">
<div id="errorMsg"><h3>ERROR</h3><ol><tmpl_var name="error"></ol></div>
</tmpl_if>
<input type="hidden" name="remote_session_id" value="{tmpl_var name='remote_session_id'}">
<input type="hidden" name="connected" value="{tmpl_var name='connected'}">
<tmpl_if name="connected">
<div class="buttonHolder buttons">
<button class="positive iconstxt icoPositive" type="button" value="Import" onClick="submitForm('pageForm','tools/import_ispconfig.php');"><span>{tmpl_var name="btn_start_txt"}</span></button>
<button class="negative iconstxt icoNegative" type="button" value="Cancel" onClick="loadContent('tools/index.php');"><span>{tmpl_var name="btn_cancel_txt"}</span></button>
</div>
<tmpl_else>
<div class="buttonHolder buttons">
<button class="positive iconstxt icoPositive" type="button" value="Import" onClick="submitForm('pageForm','tools/import_ispconfig.php');"><span>{tmpl_var name="btn_connect_txt"}</span></button>
<button class="negative iconstxt icoNegative" type="button" value="Cancel" onClick="loadContent('tools/index.php');"><span>{tmpl_var name="btn_cancel_txt"}</span></button>
</div>
</tmpl_if>
</div>
</div>
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