Till Brehm
2015-06-03 5af0cfd99a13fda9afad3380b0c50a3428acd299
commit | author | age
e94a9f 1 <?php
T 2 /*
3 Copyright (c) 2012, Till Brehm, projektfarm Gmbh, ISPConfig UG
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
8
9     * Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice,
12       this list of conditions and the following disclaimer in the documentation
13       and/or other materials provided with the distribution.
14     * Neither the name of ISPConfig nor the names of its contributors
15       may be used to endorse or promote products derived from this software without
16       specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
7fe908 30 require_once '../../lib/config.inc.php';
MC 31 require_once '../../lib/app.inc.php';
e94a9f 32
T 33 //* Check permissions for module
34 $app->auth->check_module_permissions('admin');
35
36 //* This is only allowed for administrators
37 if(!$app->auth->is_admin()) die('only allowed for administrators.');
38
39 $app->uses('tpl,validate_dns');
40
41 $app->tpl->newTemplate('form.tpl.htm');
42 $app->tpl->setInclude('content_tpl', 'templates/import_ispconfig.htm');
43 $msg = '';
44 $error = '';
45
46 //* load language file
47 $lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_import_ispconfig.lng';
7fe908 48 include $lng_file;
e94a9f 49 $app->tpl->setVar($wb);
T 50
51 if(isset($_POST['connected'])) {
5af0cf 52     
TB 53     //* CSRF Check
54     $app->auth->csrf_token_check();
55     
65ea2e 56     $connected = $app->functions->intval($_POST['connected']);
e94a9f 57     if($connected == 0) {
T 58
59         //* Try to connect to remote server
60         if(empty($_POST['remote_server'])) $error .= 'Remote Server is empty.</br>';
61         if(empty($_POST['remote_user'])) $error .= 'Remote User is empty.</br>';
62         if(empty($_POST['remote_password'])) $error .= 'Remote Password is empty.</br>';
63
64         if($error == '') {
65             try {
66                 $client = new SoapClient(null, array('location' => $_POST['remote_server'],
7fe908 67                         'uri'      => $_POST['remote_server'].'/index.php',
MC 68                         'trace' => 1,
69                         'exceptions' => 1));
70
71                 if($remote_session_id = $client->login($_POST['remote_user'], $_POST['remote_password'])) {
e94a9f 72                     $connected = 1;
T 73                     $msg .= 'Successfully connected to remote server.';
74                 }
75             } catch (SoapFault $e) {
76                 //echo $client->__getLastResponse();
77                 $error .= $e->getMessage();
78                 $connected = 0;
79             }
80         }
81     }
7fe908 82
e94a9f 83     if($connected == 1) {
7fe908 84
e94a9f 85         //* Fill the client select field
T 86         $sql = "SELECT groupid, name FROM sys_group WHERE client_id > 0 ORDER BY name";
87         $clients = $app->db->queryAllRecords($sql);
88         $client_select = "";
89         if(is_array($clients)) {
90             foreach( $clients as $client) {
91                 $selected = @($client['groupid'] == $_POST['client_group_id'])?'SELECTED':'';
92                 $client_select .= "<option value='$client[groupid]' $selected>$client[name]</option>\r\n";
93             }
94         }
7fe908 95         $app->tpl->setVar("client_group_id", $client_select);
MC 96
97
e94a9f 98         try {
T 99             $client = new SoapClient(null, array('location' => $_POST['remote_server'],
7fe908 100                     'uri'      => $_POST['remote_server'].'/index.php',
MC 101                     'trace' => 1,
102                     'exceptions' => 1));
103
104             if(!isset($remote_session_id)) $remote_session_id = $_POST['remote_session_id'];
105
106             //* Get all email domains
107             $mail_domains = $client->mail_domain_get($remote_session_id, array('active' => 'y'));
108             $mail_domain_select = '<option value="">-- select domain --</option>';
109             if(is_array($mail_domains)) {
110                 foreach( $mail_domains as $mail_domain) {
111                     $selected = @($mail_domain['domain'] == $_POST['mail_domain'])?'SELECTED':'';
112                     $mail_domain_select .= "<option value='$mail_domain[domain]' $selected>$mail_domain[domain]</option>\r\n";
113                 }
e94a9f 114             }
7fe908 115             $app->tpl->setVar("mail_domain", $mail_domain_select);
MC 116
117             //* Do the import
118             if($_POST['mail_domain'] != '') start_domain_import($_POST['mail_domain']);
119
120
121
e94a9f 122         } catch (SoapFault $e) {
T 123             //echo $client->__getLastResponse();
124             $error .= $e->getMessage();
125             $connected = 0;
126         }
7fe908 127
e94a9f 128     }
7fe908 129
e94a9f 130 }
T 131
7fe908 132 $app->tpl->setVar('remote_server', $_POST['remote_server']);
MC 133 $app->tpl->setVar('remote_user', $_POST['remote_user']);
134 $app->tpl->setVar('remote_password', $_POST['remote_password']);
135 $app->tpl->setVar('connected', $connected);
136 $app->tpl->setVar('remote_session_id', $remote_session_id);
137 $app->tpl->setVar('msg', $msg);
138 $app->tpl->setVar('error', $error);
e94a9f 139
5af0cf 140 //* SET csrf token
TB 141 $csrf_token = $app->auth->csrf_token_get('ispconfig_import');
142 $app->tpl->setVar('_csrf_id',$csrf_token['csrf_id']);
143 $app->tpl->setVar('_csrf_key',$csrf_token['csrf_key']);
144
e94a9f 145 $app->tpl_defaults();
T 146 $app->tpl->pparse();
147
7fe908 148 //##########################################################
e94a9f 149
T 150 function start_domain_import($mail_domain) {
151     global $app, $conf, $client, $msg, $error, $remote_session_id;
7fe908 152
e94a9f 153     //* Get the user and groupid for the new records
65ea2e 154     $sys_groupid = $app->functions->intval($_POST['client_group_id']);
e94a9f 155     $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $sys_groupid");
65ea2e 156     $sys_userid = $app->functions->intval($tmp['userid']);
e94a9f 157     unset($tmp);
T 158     if($sys_groupid == 0) $error .= 'Inavlid groupid<br />';
159     if($sys_userid == 0) $error .= 'Inavlid Userid<br />';
7fe908 160
3f478f 161     //* Get the mail server ID
531b04 162     $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE mail_server = 1 and mirror_server_id = 0 LIMIT 0,1");
3f478f 163     $server_id = intval($tmp['server_id']);
T 164     unset($tmp);
165     if($server_id == 0) $server_id = 1;
7fe908 166
e94a9f 167     //* get the mail domain record
T 168     $mail_domain_rec = $client->mail_domain_get($remote_session_id, array('domain' => $mail_domain));
169     if(is_array($mail_domain_rec)) {
170         $mail_domain_rec = $mail_domain_rec[0];
171         $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM mail_domain WHERE domain = '".$app->db->quote($mail_domain)."'");
172         if($tmp['number'] > 0) $error .= 'Domain '.$mail_domain.' exists already in local database.<br />';
173         unset($tmp);
7fe908 174
e94a9f 175         //* Change the record owner and remove the index field
T 176         $mail_domain_rec['sys_userid'] = $sys_userid;
177         $mail_domain_rec['sys_groupid'] = $sys_groupid;
3f478f 178         $mail_domain_rec['server_id'] = $server_id;
e94a9f 179         unset($mail_domain_rec['domain_id']);
7fe908 180
e94a9f 181         //* Insert domain if no error occurred
T 182         if($error == '') {
183             $app->db->datalogInsert('mail_domain', $mail_domain_rec, 'domain_id');
184             $msg .= "Imported mail domain ".$mail_domain_rec['domain']."<br />";
185         } else {
186             return false;
187         }
7fe908 188
e94a9f 189         //* Import mailboxes
T 190         if(isset($_POST['import_mailbox']) && $_POST['import_mailbox'] == 1) {
191             $mail_users = $client->mail_user_get($remote_session_id, array('email' => '%@'.$mail_domain));
192             if(is_array($mail_users)) {
193                 foreach($mail_users as $mail_user) {
194                     $tmp = $app->db->queryOneRecord("SELECT count(mailuser_id) as number FROM mail_user WHERE email = '".$app->db->quote($mail_user['email'])."'");
195                     if($tmp['number'] == 0) {
7fe908 196
e94a9f 197                         //* Prepare record
T 198                         $mail_user['sys_userid'] = $sys_userid;
199                         $mail_user['sys_groupid'] = $sys_groupid;
3f478f 200                         $mail_user['server_id'] = $server_id;
e94a9f 201                         $remote_mailuser_id = $mail_user['mailuser_id'];
T 202                         unset($mail_user['mailuser_id']);
203                         if(!isset($_POST['import_user_filter'])) $mail_user['custom_mailfilter'] = '';
7fe908 204
e94a9f 205                         //* Insert record in DB
T 206                         $local_mailuser_id = $app->db->datalogInsert('mail_user', $mail_user, 'mailuser_id');
207                         $msg .= "Imported mailbox ".$mail_user['email']."<br />";
7fe908 208
e94a9f 209                         //* Import mail user filters
T 210                         if(isset($_POST['import_user_filter']) && $_POST['import_user_filter'] == 1 && $local_mailuser_id > 0) {
7fe908 211
e94a9f 212                             $mail_user_filters = $client->mail_user_filter_get($remote_session_id, array('mailuser_id' => $remote_mailuser_id));
7fe908 213
e94a9f 214                             if(is_array($mail_user_filters)) {
T 215                                 foreach($mail_user_filters as $mail_user_filter) {
216                                     $mail_user_filter['sys_userid'] = $sys_userid;
217                                     $mail_user_filter['sys_groupid'] = $sys_groupid;
218                                     $mail_user_filter['mailuser_id'] = $local_mailuser_id;
3f478f 219                                     $mail_user_filter['server_id'] = $server_id;
e94a9f 220                                     unset($mail_user_filter['filter_id']);
7fe908 221
e94a9f 222                                     //* Insert record in DB
T 223                                     $app->db->datalogInsert('mail_user_filter', $mail_user_filter, 'filter_id');
224                                     $msg .= "Imported mailbox filter ".$mail_user['email'].": ".$mail_user_filter['rulename']."<br />";
225                                 }
226                             }
227                         }
228                     } else {
229                         $error .= "Mailbox ".$mail_user['email']." exists in local database. Skipped import of mailbox<br />";
230                     }
7fe908 231
e94a9f 232                 }
T 233             }
234         }
7fe908 235
e94a9f 236         //* Import email aliases
T 237         if(isset($_POST['import_alias']) && $_POST['import_alias'] == 1) {
238             $mail_aliases = $client->mail_alias_get($remote_session_id, array('type' => 'alias', 'destination' => '%@'.$mail_domain));
239             if(is_array($mail_aliases)) {
240                 foreach($mail_aliases as $mail_alias) {
241                     $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'])."'");
242                     if($tmp['number'] == 0) {
243                         $mail_alias['sys_userid'] = $sys_userid;
244                         $mail_alias['sys_groupid'] = $sys_groupid;
3f478f 245                         $mail_alias['server_id'] = $server_id;
e94a9f 246                         unset($mail_alias['forwarding_id']);
T 247                         $app->db->datalogInsert('mail_forwarding', $mail_alias, 'forwarding_id');
248                         $msg .= "Imported email alias ".$mail_alias['source']."<br />";
249                     } else {
250                         $error .= "Email alias ".$mail_alias['source']." exists in local database. Skipped import.<br />";
251                     }
7fe908 252
e94a9f 253                 }
T 254             }
255         }
7fe908 256
e94a9f 257         //* Import domain aliases
T 258         if(isset($_POST['import_aliasdomain']) && $_POST['import_aliasdomain'] == 1) {
259             $mail_aliases = $client->mail_alias_get($remote_session_id, array('type' => 'aliasdomain', 'destination' => '@'.$mail_domain));
260             if(is_array($mail_aliases)) {
261                 foreach($mail_aliases as $mail_alias) {
262                     $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'])."'");
263                     if($tmp['number'] == 0) {
264                         $mail_alias['sys_userid'] = $sys_userid;
265                         $mail_alias['sys_groupid'] = $sys_groupid;
3f478f 266                         $mail_alias['server_id'] = $server_id;
e94a9f 267                         unset($mail_alias['forwarding_id']);
T 268                         $app->db->datalogInsert('mail_forwarding', $mail_alias, 'forwarding_id');
269                         $msg .= "Imported email aliasdomain ".$mail_alias['source']."<br />";
270                     } else {
271                         $error .= "Email aliasdomain ".$mail_alias['source']." exists in local database. Skipped import.<br />";
272                     }
7fe908 273
e94a9f 274                 }
T 275             }
276         }
7fe908 277
e94a9f 278         //* Import email forward
T 279         if(isset($_POST['import_forward']) && $_POST['import_forward'] == 1) {
280             $mail_forwards = $client->mail_forward_get($remote_session_id, array('type' => 'forward', 'source' => '%@'.$mail_domain));
281             if(is_array($mail_forwards)) {
282                 foreach($mail_forwards as $mail_forward) {
283                     $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'])."'");
284                     if($tmp['number'] == 0) {
285                         $mail_forward['sys_userid'] = $sys_userid;
286                         $mail_forward['sys_groupid'] = $sys_groupid;
3f478f 287                         $mail_forward['server_id'] = $server_id;
e94a9f 288                         unset($mail_forward['forwarding_id']);
T 289                         $app->db->datalogInsert('mail_forwarding', $mail_forward, 'forwarding_id');
290                         $msg .= "Imported email forward ".$mail_forward['source']."<br />";
291                     } else {
292                         $error .= "Email forward ".$mail_forward['source']." exists in local database. Skipped import.<br />";
293                     }
7fe908 294
e94a9f 295                 }
T 296             }
297         }
7fe908 298
e94a9f 299         //* Import spamfilter
T 300         if(isset($_POST['import_spamfilter']) && $_POST['import_spamfilter'] == 1) {
301             $mail_spamfilters = $client->mail_spamfilter_user_get($remote_session_id, array('email' => '%@'.$mail_domain));
302             if(is_array($mail_spamfilters)) {
303                 foreach($mail_spamfilters as $mail_spamfilter) {
304                     $tmp = $app->db->queryOneRecord("SELECT count(id) as number FROM spamfilter_users WHERE email = '".$app->db->quote($mail_spamfilter['email'])."'");
305                     if($tmp['number'] == 0) {
306                         $mail_spamfilter['sys_userid'] = $sys_userid;
307                         $mail_spamfilter['sys_groupid'] = $sys_groupid;
3f478f 308                         $mail_spamfilter['server_id'] = $server_id;
e94a9f 309                         unset($mail_spamfilter['id']);
T 310                         $app->db->datalogInsert('spamfilter_users', $mail_spamfilter, 'id');
311                         $msg .= "Imported spamfilter user ".$mail_spamfilter['email']."<br />";
312                     } else {
313                         $error .= "Spamfilter user ".$mail_spamfilter['email']." exists in local database. Skipped import.<br />";
314                     }
7fe908 315
e94a9f 316                 }
T 317             }
318         }
319
320     }
7fe908 321
e94a9f 322 }
T 323
324
7fe908 325 ?>