Till Brehm
2015-06-03 5af0cfd99a13fda9afad3380b0c50a3428acd299
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?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'])) {
    
    //* CSRF Check
    $app->auth->csrf_token_check();
    
    $connected = $app->functions->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);
 
//* SET csrf token
$csrf_token = $app->auth->csrf_token_get('ispconfig_import');
$app->tpl->setVar('_csrf_id',$csrf_token['csrf_id']);
$app->tpl->setVar('_csrf_key',$csrf_token['csrf_key']);
 
$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 = $app->functions->intval($_POST['client_group_id']);
    $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $sys_groupid");
    $sys_userid = $app->functions->intval($tmp['userid']);
    unset($tmp);
    if($sys_groupid == 0) $error .= 'Inavlid groupid<br />';
    if($sys_userid == 0) $error .= 'Inavlid Userid<br />';
 
    //* Get the mail server ID
    $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE mail_server = 1 and mirror_server_id = 0 LIMIT 0,1");
    $server_id = intval($tmp['server_id']);
    unset($tmp);
    if($server_id == 0) $server_id = 1;
 
    //* 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;
        $mail_domain_rec['server_id'] = $server_id;
        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;
                        $mail_user['server_id'] = $server_id;
                        $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;
                                    $mail_user_filter['server_id'] = $server_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;
                        $mail_alias['server_id'] = $server_id;
                        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;
                        $mail_alias['server_id'] = $server_id;
                        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;
                        $mail_forward['server_id'] = $server_id;
                        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;
                        $mail_spamfilter['server_id'] = $server_id;
                        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 />";
                    }
 
                }
            }
        }
 
    }
 
}
 
 
?>