import_ispconfig.php 13.4 KB
Newer Older
tbrehm's avatar
tbrehm committed
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
<?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.
*/

30
31
require_once '../../lib/config.inc.php';
require_once '../../lib/app.inc.php';
tbrehm's avatar
tbrehm committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

//* 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';
48
include $lng_file;
tbrehm's avatar
tbrehm committed
49
50
51
$app->tpl->setVar($wb);

if(isset($_POST['connected'])) {
Till Brehm's avatar
Till Brehm committed
52
53
54
55
	
	//* CSRF Check
	$app->auth->csrf_token_check();
	
56
	$connected = $app->functions->intval($_POST['connected']);
tbrehm's avatar
tbrehm committed
57
58
59
60
61
62
63
64
65
	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 {
66
67
68
69
70
71
72
73
74
75
76
77
				
				//* Allow connections to self signed SSL certs
				$context = stream_context_create(
					array(
						'ssl' => array (
						'verify_peer' => false,
						'verify_peer_name' => false,
						'allow_self_signed' => true
						)
					)
				);
				
tbrehm's avatar
tbrehm committed
78
				$client = new SoapClient(null, array('location' => $_POST['remote_server'],
79
80
						'uri'      => $_POST['remote_server'].'/index.php',
						'trace' => 1,
81
82
						'exceptions' => 1,
						'stream_context' => $context));
83
84

				if($remote_session_id = $client->login($_POST['remote_user'], $_POST['remote_password'])) {
tbrehm's avatar
tbrehm committed
85
86
87
88
89
90
91
92
93
94
					$connected = 1;
					$msg .= 'Successfully connected to remote server.';
				}
			} catch (SoapFault $e) {
				//echo $client->__getLastResponse();
				$error .= $e->getMessage();
				$connected = 0;
			}
		}
	}
95

tbrehm's avatar
tbrehm committed
96
	if($connected == 1) {
97

tbrehm's avatar
tbrehm committed
98
99
100
101
102
103
104
105
106
107
		//* 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";
			}
		}
108
109
110
		$app->tpl->setVar("client_group_id", $client_select);


tbrehm's avatar
tbrehm committed
111
		try {
Till Brehm's avatar
Till Brehm committed
112
113
114
115
116
117
118
119
120
121
122
			//* Allow connections to self signed SSL certs
			$context = stream_context_create(
				array(
					'ssl' => array (
					'verify_peer' => false,
					'verify_peer_name' => false,
					'allow_self_signed' => true
					)
				)
			);
				
tbrehm's avatar
tbrehm committed
123
			$client = new SoapClient(null, array('location' => $_POST['remote_server'],
124
125
					'uri'      => $_POST['remote_server'].'/index.php',
					'trace' => 1,
Till Brehm's avatar
Till Brehm committed
126
127
					'exceptions' => 1,
					'stream_context' => $context));
128
129
130
131
132
133
134
135
136
137
138

			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";
				}
tbrehm's avatar
tbrehm committed
139
			}
140
141
142
143
144
145
146
			$app->tpl->setVar("mail_domain", $mail_domain_select);

			//* Do the import
			if($_POST['mail_domain'] != '') start_domain_import($_POST['mail_domain']);



tbrehm's avatar
tbrehm committed
147
148
149
150
151
		} catch (SoapFault $e) {
			//echo $client->__getLastResponse();
			$error .= $e->getMessage();
			$connected = 0;
		}
152

tbrehm's avatar
tbrehm committed
153
	}
154

tbrehm's avatar
tbrehm committed
155
156
}

157
158
159
$app->tpl->setVar('remote_server', $_POST['remote_server'], true);
$app->tpl->setVar('remote_user', $_POST['remote_user'], true);
$app->tpl->setVar('remote_password', $_POST['remote_password'], true);
160
161
162
163
$app->tpl->setVar('connected', $connected);
$app->tpl->setVar('remote_session_id', $remote_session_id);
$app->tpl->setVar('msg', $msg);
$app->tpl->setVar('error', $error);
tbrehm's avatar
tbrehm committed
164

Till Brehm's avatar
Till Brehm committed
165
166
167
168
169
//* 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']);

tbrehm's avatar
tbrehm committed
170
171
172
$app->tpl_defaults();
$app->tpl->pparse();

173
//##########################################################
tbrehm's avatar
tbrehm committed
174
175
176

function start_domain_import($mail_domain) {
	global $app, $conf, $client, $msg, $error, $remote_session_id;
177

tbrehm's avatar
tbrehm committed
178
	//* Get the user and groupid for the new records
179
	$sys_groupid = $app->functions->intval($_POST['client_group_id']);
180
	$tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = ?", $sys_groupid);
181
	$sys_userid = $app->functions->intval($tmp['userid']);
tbrehm's avatar
tbrehm committed
182
183
184
	unset($tmp);
	if($sys_groupid == 0) $error .= 'Inavlid groupid<br />';
	if($sys_userid == 0) $error .= 'Inavlid Userid<br />';
185

186
	//* Get the mail server ID
187
	$tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE mail_server = 1 and mirror_server_id = 0 LIMIT 0,1");
188
189
190
	$server_id = intval($tmp['server_id']);
	unset($tmp);
	if($server_id == 0) $server_id = 1;
191

tbrehm's avatar
tbrehm committed
192
193
194
195
	//* 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];
196
		$tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM mail_domain WHERE domain = ?", $mail_domain);
tbrehm's avatar
tbrehm committed
197
198
		if($tmp['number'] > 0) $error .= 'Domain '.$mail_domain.' exists already in local database.<br />';
		unset($tmp);
199

tbrehm's avatar
tbrehm committed
200
201
202
		//* Change the record owner and remove the index field
		$mail_domain_rec['sys_userid'] = $sys_userid;
		$mail_domain_rec['sys_groupid'] = $sys_groupid;
203
		$mail_domain_rec['server_id'] = $server_id;
tbrehm's avatar
tbrehm committed
204
		unset($mail_domain_rec['domain_id']);
205

tbrehm's avatar
tbrehm committed
206
207
208
209
210
211
212
		//* 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;
		}
213

tbrehm's avatar
tbrehm committed
214
215
216
217
218
		//* 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) {
219
					$tmp = $app->db->queryOneRecord("SELECT count(mailuser_id) as number FROM mail_user WHERE email = ?", $mail_user['email']);
tbrehm's avatar
tbrehm committed
220
					if($tmp['number'] == 0) {
221

tbrehm's avatar
tbrehm committed
222
223
224
						//* Prepare record
						$mail_user['sys_userid'] = $sys_userid;
						$mail_user['sys_groupid'] = $sys_groupid;
225
						$mail_user['server_id'] = $server_id;
tbrehm's avatar
tbrehm committed
226
227
228
						$remote_mailuser_id = $mail_user['mailuser_id'];
						unset($mail_user['mailuser_id']);
						if(!isset($_POST['import_user_filter'])) $mail_user['custom_mailfilter'] = '';
229

tbrehm's avatar
tbrehm committed
230
231
232
						//* Insert record in DB
						$local_mailuser_id = $app->db->datalogInsert('mail_user', $mail_user, 'mailuser_id');
						$msg .= "Imported mailbox ".$mail_user['email']."<br />";
233

tbrehm's avatar
tbrehm committed
234
235
						//* Import mail user filters
						if(isset($_POST['import_user_filter']) && $_POST['import_user_filter'] == 1 && $local_mailuser_id > 0) {
236

tbrehm's avatar
tbrehm committed
237
							$mail_user_filters = $client->mail_user_filter_get($remote_session_id, array('mailuser_id' => $remote_mailuser_id));
238

tbrehm's avatar
tbrehm committed
239
240
241
242
243
							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;
244
									$mail_user_filter['server_id'] = $server_id;
tbrehm's avatar
tbrehm committed
245
									unset($mail_user_filter['filter_id']);
246

tbrehm's avatar
tbrehm committed
247
248
249
250
251
252
253
254
255
									//* 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 />";
					}
256

tbrehm's avatar
tbrehm committed
257
258
259
				}
			}
		}
260

tbrehm's avatar
tbrehm committed
261
262
263
264
265
		//* 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) {
266
					$tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE `type` = 'alias' AND source = ? AND destination = ?", $mail_alias['source'], $mail_alias['destination']);
tbrehm's avatar
tbrehm committed
267
268
269
					if($tmp['number'] == 0) {
						$mail_alias['sys_userid'] = $sys_userid;
						$mail_alias['sys_groupid'] = $sys_groupid;
270
						$mail_alias['server_id'] = $server_id;
tbrehm's avatar
tbrehm committed
271
272
273
274
275
276
						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 />";
					}
277

tbrehm's avatar
tbrehm committed
278
279
280
				}
			}
		}
281

tbrehm's avatar
tbrehm committed
282
283
284
285
286
		//* 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) {
287
					$tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE `type` = 'aliasdomain' AND source = ? AND destination = ?", $mail_alias['source'], $mail_alias['destination']);
tbrehm's avatar
tbrehm committed
288
289
290
					if($tmp['number'] == 0) {
						$mail_alias['sys_userid'] = $sys_userid;
						$mail_alias['sys_groupid'] = $sys_groupid;
291
						$mail_alias['server_id'] = $server_id;
tbrehm's avatar
tbrehm committed
292
293
294
295
296
297
						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 />";
					}
298

tbrehm's avatar
tbrehm committed
299
300
301
				}
			}
		}
302

tbrehm's avatar
tbrehm committed
303
304
305
306
307
		//* 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) {
308
					$tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE `type` = 'forward' AND source = ? AND destination = ?", $mail_forward['source'], $mail_forward['destination']);
tbrehm's avatar
tbrehm committed
309
310
311
					if($tmp['number'] == 0) {
						$mail_forward['sys_userid'] = $sys_userid;
						$mail_forward['sys_groupid'] = $sys_groupid;
312
						$mail_forward['server_id'] = $server_id;
tbrehm's avatar
tbrehm committed
313
314
315
316
317
318
						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 />";
					}
319

tbrehm's avatar
tbrehm committed
320
321
322
				}
			}
		}
323

tbrehm's avatar
tbrehm committed
324
325
326
327
328
		//* 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) {
329
					$tmp = $app->db->queryOneRecord("SELECT count(id) as number FROM spamfilter_users WHERE email = ?", $mail_spamfilter['email']);
tbrehm's avatar
tbrehm committed
330
331
332
					if($tmp['number'] == 0) {
						$mail_spamfilter['sys_userid'] = $sys_userid;
						$mail_spamfilter['sys_groupid'] = $sys_groupid;
333
						$mail_spamfilter['server_id'] = $server_id;
tbrehm's avatar
tbrehm committed
334
335
336
337
338
339
						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 />";
					}
340

tbrehm's avatar
tbrehm committed
341
342
343
344
345
				}
			}
		}

	}
346

tbrehm's avatar
tbrehm committed
347
348
349
}


350
?>