Till Brehm
2015-06-03 5af0cfd99a13fda9afad3380b0c50a3428acd299
commit | author | age
a93288 1 <?php
M 2 /*
3 Copyright (c) 2008, Till Brehm, projektfarm Gmbh
4 Plesk(r) Importer (c) 2012, Marius Cramer, pixcept KG
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 global $app, $conf;
32
7fe908 33 require_once '../../lib/config.inc.php';
a93288 34
7fe908 35 require_once '../../lib/app.inc.php';
a93288 36
M 37 /**
38  *
39  * @param db $exdb
7fe908 40  * @return array
a93288 41  */
M 42
7fe908 43
MC 44 function read_limit_data($exdb) {
45     $limits = array();
46     // Limits
47     $limit_data = $exdb->queryAllRecords("SELECT l.id, l.limit_name, l.value FROM Limits as l");
48     foreach($limit_data as $entry) {
49         if(array_key_exists($entry['id'], $limits) == false) $limits[$entry['id']] = array();
50         $limits[$entry['id']][$entry['limit_name']] = $entry['value'];
51
52         // limits that are there:
53         /*
a93288 54         disk_space
M 55         disk_space_soft
56         expiration
57         max_box
58         max_db
59         max_dom_aliases
60         max_maillists
61         max_mn
62         max_site
63         max_site_builder
64         max_subdom
65         max_subftp_users
66         max_traffic
67         max_traffic_soft
68         max_unity_mobile_sites
69         max_webapps
70         max_wu
71         mbox_quota
72         */
7fe908 73     }
MC 74
75     return $limits;
a93288 76 }
7fe908 77
a93288 78
M 79 /**
80  *
81  * @param array $limits
82  * @param int $id
83  * @param string $limit
84  * @param mixed $default
7fe908 85  * @return mixed
a93288 86  */
M 87 function get_limit($limits, $id, $limit, $default = false) {
7fe908 88     $ret = $default;
MC 89     if(isset($limits[$id][$limit])) $ret = $limits[$id][$limit];
90
91     return $ret;
a93288 92 }
M 93
94 function get_option($options, $option, $default = false) {
7fe908 95     $ret = $default;
MC 96     if(isset($options[$option])) $ret = $options[$option];
97
98     return $ret;
a93288 99 }
M 100
101 function add_dot($string) {
7fe908 102     if(strlen($string) > 0 && substr($string, -1, 1) !== '.') $string .= '.';
MC 103     return $string;
a93288 104 }
M 105
106 function byte_to_mbyte($byte) {
7fe908 107     if($byte <= 0) return $byte; // limit = -1 -> unlimited
MC 108     return round($byte / (1024*1024));
a93288 109 }
M 110
111 function yes_no($num, $reverse = false) {
7fe908 112     return ($num == 1 && !$reverse) || ($num != 1 && $reverse) ? 'y' : 'n';
a93288 113 }
M 114
115 // taken from the web_domain_edit.php
7fe908 116 function id_hash($id, $levels) {
MC 117     $hash = "" . $id % 10 ;
118     $id /= 10 ;
119     $levels -- ;
120     while ( $levels > 0 ) {
121         $hash .= "/" . $id % 10 ;
122         $id /= 10 ;
123         $levels-- ;
124     }
125     return $hash;
a93288 126 }
M 127
128 /* TODO: document root rewrite on ftp account and other home directories */
129
130 //* Check permissions for module
131 $app->auth->check_module_permissions('admin');
132
133 //* This is only allowed for administrators
134 if(!$app->auth->is_admin()) die('only allowed for administrators.');
135
b0191f 136 $app->uses('tpl,getconf');
a93288 137 $app->load('importer');
M 138
139 $app->tpl->newTemplate('form.tpl.htm');
140 $app->tpl->setInclude('content_tpl', 'templates/import_plesk.htm');
141 $msg = '';
142 $error = '';
143
144 // Start migrating plesk data
145 if(isset($_POST['start']) && $_POST['start'] == 1) {
7fe908 146
5af0cf 147     //* CSRF Check
TB 148     $app->auth->csrf_token_check();
149     
a93288 150     //* Set variable sin template
7fe908 151     $app->tpl->setVar('dbhost', $_POST['dbhost']);
MC 152     $app->tpl->setVar('dbname', $_POST['dbname']);
153     $app->tpl->setVar('dbuser', $_POST['dbuser']);
154     $app->tpl->setVar('dbpassword', $_POST['dbpassword']);
155     $app->tpl->setVar('webcontent', $_POST['webcontent']);
156     $app->tpl->setVar('mailcontent', $_POST['mailcontent']);
157
a93288 158     //* Establish connection to external database
M 159     $msg .= 'Connecting to external database...<br />';
7fe908 160
a93288 161     //* Backup DB login details
M 162     /*$conf_bak['db_host'] = $conf['db_host'];
163     $conf_bak['db_database'] = $conf['db_database'];
164     $conf_bak['db_user'] = $conf['db_user'];
165     $conf_bak['db_password'] = $conf['db_password'];*/
7fe908 166
a93288 167     //* Set external Login details
M 168     $conf['imp_db_host'] = $_POST['dbhost'];
169     $conf['imp_db_database'] = $_POST['dbname'];
170     $conf['imp_db_user'] = $_POST['dbuser'];
171     $conf['imp_db_password'] = $_POST['dbpassword'];
7fe908 172     $conf['imp_db_charset'] = $conf['db_charset'];
MC 173     $conf['imp_db_new_link'] = $conf['db_new_link'];
174     $conf['imp_db_client_flags'] = $conf['db_client_flags'];
175
a93288 176     //* create new db object
M 177     $exdb = new db('imp');
7fe908 178
MC 179     $msg .= 'db object created...<br />';
180
181     $importer = new importer();
182     $session_id = 'ISPC3'; // set dummy session id for remoting lib
183     $msg .= 'importer object created...<br />';
184
185     // import on server
a93288 186     $server_id = 1;
7fe908 187
a93288 188     //* Connect to DB
M 189     if($exdb !== false) {
7fe908 190         $msg .= 'Connecting to external database done...<br />';
MC 191
192         $limits = read_limit_data($exdb);
193
194         $msg .= 'read all limit data...<br />';
195
196         // param_id -> cl_params table - not needed for import
197         // tpye = admin, reseller, client
198         $admins = $exdb->queryAllRecords("SELECT c.id, c.parent_id, c.type, c.cr_date, c.cname, c.pname, c.login, c.account_id, a.password, a.type as `pwtype`, c.status, c.phone, c.fax, c.email, c.address, c.city, c.state, c.pcode, c.country, c.locale, c.limits_id, c.params_id, c.perm_id, c.pool_id, c.logo_id, c.tmpl_id, c.guid, c.overuse, c.vendor_id, c.external_id FROM clients as c LEFT JOIN accounts as a ON (a.id = c.account_id) WHERE c.type = 'admin' ORDER BY c.parent_id, c.id");
199         $resellers = $exdb->queryAllRecords("SELECT c.id, c.parent_id, c.type, c.cr_date, c.cname, c.pname, c.login, c.account_id, a.password, a.type as `pwtype`, c.status, c.phone, c.fax, c.email, c.address, c.city, c.state, c.pcode, c.country, c.locale, c.limits_id, c.params_id, c.perm_id, c.pool_id, c.logo_id, c.tmpl_id, c.guid, c.overuse, c.vendor_id, c.external_id FROM clients as c LEFT JOIN accounts as a ON (a.id = c.account_id) WHERE c.type = 'reseller' ORDER BY c.parent_id, c.id");
200         $clients  = $exdb->queryAllRecords("SELECT c.id, c.parent_id, c.type, c.cr_date, c.cname, c.pname, c.login, c.account_id, a.password, a.type as `pwtype`, c.status, c.phone, c.fax, c.email, c.address, c.city, c.state, c.pcode, c.country, c.locale, c.limits_id, c.params_id, c.perm_id, c.pool_id, c.logo_id, c.tmpl_id, c.guid, c.overuse, c.vendor_id, c.external_id FROM clients as c LEFT JOIN accounts as a ON (a.id = c.account_id) WHERE c.type = 'client' ORDER BY c.parent_id, c.id");
201
202         $users = array_merge($admins, $resellers, $clients);
203         $msg .= 'read all users (' . count($users) . ')...<br />';
204
205
206         $plesk_ispc_ids = array(); // array with key = plesk id, value = ispc id
207
208         $phpopts = array('no', 'fast-cgi', 'cgi', 'mod', 'suphp', 'php-fpm');
209
210         // import admins / resellers
211         for($i = 0; $i < count($users); $i++) {
212             $entry = $users[$i];
213
214             $old_client = $importer->client_get_by_username($session_id, $entry['login']);
215             if($old_client) {
216                 if($old_client['client_id'] == 0) {
217                     $entry['login'] = 'psa_' . $entry['login'];
218                     $old_client = $importer->client_get_by_username($session_id, $entry['login']);
219                     if($old_client) {
220                         $msg .= $entry['login'] . ' existed, updating id ' . $old_client['client_id'] . '<br />';
221                     }
222                 } else {
223                     $msg .= $entry['login'] . ' existed, updating id ' . $old_client['client_id'] . '<br />';
224                 }
225             }
226             $params = array(
227                 'company_name' => $entry['cname'],
228                 'contact_name' => $entry['pname'],
229                 //'customer_no' => '',
230                 'username' => $entry['login'],
231                 'password' => $entry['password'],
232                 'language' => substr($entry['locale'], 0, 2), // plesk stores as de-DE or en-US
233                 //'usertheme' => '',
234                 'street' => $entry['address'],
235                 'zip' => $entry['pcode'],
236                 'city' => $entry['city'],
237                 'state' => $entry['state'],
238                 'country' => $entry['country'],
239                 'telephone' => $entry['phone'],
240                 //'mobile' => $entry[''],
241                 'fax' => $entry['fax'],
242                 'email' => $entry['email'],
243                 //'internet' => $entry[''],
244                 //'icq' => $entry[''],
245                 //'vat_id' => $entry[''],
246                 //'company_id' => $entry[''],
247                 //'bank_account_number' => $entry[''],
248                 //'bank_code' => $entry[''],
249                 //'bank_name' => $entry[''],
250                 //'bank_account_iban' => $entry[''],
251                 //'bank_account_swift' => $entry[''],
252                 'notes' => 'imported from Plesk id ' . $entry['id'],
253                 //'template_master' => $entry[''],
254                 //'template_additional' => $entry[''],
255                 //'default_mailserver' => $entry[''],
256                 'limit_maildomain' => get_limit($limits, $entry['id'], 'max_site', -1),
257                 'limit_mailbox' => get_limit($limits, $entry['id'], 'max_box', -1),
258                 'limit_mailalias' => get_limit($limits, $entry['id'], 'max_mn', -1),
259                 'limit_mailaliasdomain' => get_limit($limits, $entry['id'], 'max_dom_aliases', -1),
260                 'limit_mailmailinglist' => get_limit($limits, $entry['id'], 'max_maillists', -1),
261                 'limit_mailforward' => get_limit($limits, $entry['id'], 'max_mn', -1),
262                 'limit_mailcatchall' => 1,
263                 'limit_mailrouting' => 0,
264                 'limit_mailfilter' => 0,
265                 'limit_fetchmail' => 0,
266                 'limit_mailquota' => get_limit($limits, $entry['id'], 'mbox_quota', -1),
267                 'limit_spamfilter_wblist' => 0,
268                 'limit_spamfilter_user' => 0,
269                 'limit_spamfilter_policy' => 0,
270                 //'default_webserver' => '',
271                 'limit_web_domain' => get_limit($limits, $entry['id'], 'max_site', -1),
272                 'limit_web_quota' => intval(get_limit($limits, $entry['id'], 'disk_space', -1)),
273                 'web_php_options' => implode(',', $phpopts),
274                 'limit_web_aliasdomain' => get_limit($limits, $entry['id'], 'max_dom_aliases', -1),
275                 'limit_web_subdomain' => get_limit($limits, $entry['id'], 'max_subdom', -1),
276                 'limit_ftp_user' => (string)($app->functions->intval(get_limit($limits, $entry['id'], 'max_subftp_users', -2)) + 1),
277                 'limit_shell_user' => 0,
278                 'ssh_chroot' => 'no,jailkit',
279                 'limit_webdav_user' => get_limit($limits, $entry['id'], 'max_wu', 0),
280                 //'default_dnsserver' => '',
281                 'limit_dns_zone' => -1,
282                 'limit_dns_slave_zone' => -1,
283                 'limit_dns_record' => -1,
284                 'limit_client' => ($entry['type'] == 'client' ? 0 : -1),
285                 //'default_dbserver' => '',
286                 'limit_database' => get_limit($limits, $entry['id'], 'max_db', -1),
287                 'limit_cron' => 0,
288                 'limit_cron_type' => 'url',
289                 'limit_cron_frequency' => '5',
290                 'limit_traffic_quota' => intval(get_limit($limits, $entry['id'], 'max_traffic', -1)),
291                 'limit_openvz_vm' => 0,
292                 'limit_openvz_vm_template_id' => ''
293             );
294             $reseller_id = 0;
295             if($entry['parent_id'] != 0) {
296                 if(array_key_exists($entry['parent_id'], $plesk_ispc_ids)) {
297                     $reseller_id = $plesk_ispc_ids[$entry['parent_id']];
298                 }
299             }
300
301             if($old_client) {
302                 $new_id = $old_client['client_id'];
303                 $ok = $importer->client_update($session_id, $old_client['client_id'], $reseller_id, $params);
304                 if($ok === false) {
305
306                 }
307             } else {
308                 $new_id = $importer->client_add($session_id, $reseller_id, $params);
309             }
310             if($new_id === false) {
311                 //something went wrong here...
312                 $msg .= "Client " . $entry['id'] . " (" . $entry['pname'] . ") could not be inserted/updated.<br />";
313                 $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
314             } else {
315                 $msg .= "Client " . $entry['id'] . " (" . $entry['pname'] . ") inserted/updated.<br />";
316             }
317
318             $plesk_ispc_ids[$entry['id']] = $new_id;
319         }
320         unset($users);
321         unset($clients);
322         unset($resellers);
323         unset($admins);
324
325         $web_config = $app->getconf->get_server_config($server_id, 'web');
326
327         $domains = $exdb->queryAllRecords("SELECT d.id, d.cr_date, d.name, d.displayName, d.dns_zone_id, d.status, d.htype, d.real_size, d.cl_id, d.limits_id, d.params_id, d.guid, d.overuse, d.gl_filter, d.vendor_id, d.webspace_id, d.webspace_status, d.permissions_id, d.external_id FROM domains as d");
328         $dom_ftp_users = array();
329         $domain_ids = array();
330         $domain_roots = array();
331         $domain_owners = array();
332         $dns_domain_ids = array();
333         $maildomain_ids = array();
334         foreach($domains as $entry) {
335             $res = $exdb->query("SELECT d.dom_id, d.param, d.val FROM dom_param as d WHERE d.dom_id = '" . $entry['id'] . "'");
336             $options = array();
337             while($opt = $exdb->nextRecord()) {
338                 $options[$opt['param']] = $opt['val'];
339             }
340
341             /* TODO: options that might be used later:
a93288 342              * OveruseBlock true/false
M 343              * OveruseNotify true/false
344              * OveruseSuspend true/false
345              * wu_script true/false (webusers allowed to use scripts?)
346              * webmail string (webmailer used - horde)
347              */
7fe908 348
MC 349             $redir_type = '';
350             $redir_path = '';
351
352             if($entry['htype'] === 'std_fwd') {
353                 // redirection
354                 $redir = $exdb->queryOneRecord("SELECT f.dom_id, f.ip_address_id, f.redirect FROM forwarding as f WHERE f.dom_id = '" . $entry['id'] . "'");
355                 $redir_type = 'R,L';
356                 $redir_path = $redir['redirect'];
357             } elseif($entry['htype'] === 'vrt_hst') {
358                 // default virtual hosting (vhost)
359             } else {
360                 /* TODO: unknown type */
361             }
362
363             $hosting = $exdb->queryOneRecord("SELECT h.dom_id, h.sys_user_id, h.ip_address_id, h.real_traffic, h.fp, h.fp_ssl, h.fp_enable, h.fp_adm, h.fp_pass, h.ssi, h.php, h.cgi, h.perl, h.python, h.fastcgi, h.miva, h.coldfusion, h.asp, h.asp_dot_net, h.ssl, h.webstat, h.same_ssl, h.traffic_bandwidth, h.max_connection, h.php_handler_type, h.www_root, h.maintenance_mode, h.certificate_id, s.login, s.account_id, s.home, s.shell, s.quota, s.mapped_to, a.password, a.type as `pwtype` FROM hosting as h LEFT JOIN sys_users as s ON (s.id = h.sys_user_id) LEFT JOIN accounts as a ON (s.account_id = a.id) WHERE h.dom_id = '" . $entry['id'] . "'");
364             if($hosting['sys_user_id']) {
365                 $dom_ftp_users[] = array('id' => 0,
366                     'dom_id' => $hosting['dom_id'],
367                     'sys_user_id' => $hosting['sys_user_id'],
368                     'login' => $hosting['login'],
369                     'account_id' => $hosting['account_id'],
370                     'home' => $hosting['home'],
371                     'shell' => $hosting['shell'],
372                     'quota' => $hosting['quota'],
373                     'mapped_to' => $hosting['mapped_to'],
374                     'password' => $hosting['password'],
375                     'pwtype' => $hosting['pwtype']
376                 );
377             }
378
379             $phpmode = 'no';
380             if(get_option($hosting, 'php', 'false') === 'true') {
381                 $mode = get_option($hosting, 'php_handler_type', 'module');
382                 if($mode === 'module') $phpmode = 'mod';
383                 else $phpmode = 'fast-cgi';
384                 /* TODO: what other options could be in "php_handler_type"? */
385             }
386
387             /* TODO: plesk offers some more options:
a93288 388              * sys_user_id -> owner of files?
M 389              * ip_address_id - needed?
390              * fp - frontpage extensions
391              * miva - ?
392              * coldfusion
393              * asp
394              * asp_dot_net
395              * traffic_bandwidth
396              * max_connections
397              */
7fe908 398             $params = array(
MC 399                 'server_id' => $server_id,
400                 'ip_address' => '*',
401                 //'ipv6_address' => '',
402                 'domain' => $entry['name'],
403                 'type' => 'vhost', // can be vhost or alias
404                 'parent_domain_id' => '', // only if alias
405                 'vhost_type' => 'name', // or ip (-based)
406                 'hd_quota' => byte_to_mbyte(get_limit($limits, $entry['id'], 'disk_space', -1)),
407                 'traffic_quota' => byte_to_mbyte(get_limit($limits, $entry['id'], 'max_traffic', -1)),
408                 'cgi' => yes_no(get_option($hosting, 'cgi', 'false') === 'true' ? 1 : 0),
409                 'ssi' => yes_no(get_option($hosting, 'ssi', 'false') === 'true' ? 1 : 0),
410                 'suexec' => yes_no(1), // does plesk use this?!
411                 'errordocs' => get_option($options, 'apacheErrorDocs', 'false') === 'true' ? 1 : 0,
412                 'subdomain' => 'www', // plesk always uses this option
413                 'ssl' => yes_no(get_option($hosting, 'ssl', 'false') === 'true' ? 1 : 0),
414                 'php' => $phpmode,
415                 'fastcgi_php_version' => '', // plesk has no different php versions
416                 'ruby' => yes_no(0), // plesk has no ruby support
417                 'python' => yes_no(get_option($hosting, 'python', 'false') === 'true' ? 1 : 0),
418                 'active' => yes_no(($entry['status'] == 0 && get_option($hosting, 'maintenance_mode', 'false') !== 'true') ? 1 : 0),
419                 'redirect_type' => $redir_type,
420                 'redirect_path' => $redir_path,
421                 'seo_redirect' => '',
422                 'ssl_state' => $entry[''],
423                 'ssl_locality' => $entry[''],
424                 'ssl_organisation' => $entry[''],
425                 'ssl_organisation_unit' => $entry[''],
426                 'ssl_country' => $entry[''],
427                 'ssl_domain' => $entry[''],
428                 'ssl_request' => $entry[''],
429                 'ssl_cert' => $entry[''],
430                 'ssl_bundle' => $entry[''],
431                 'ssl_action' => $entry[''],
432                 'stats_password' => '',
433                 'stats_type' => get_option($hosting, 'webstat', 'webalizer') === 'awstats' ? 'awstats' : 'webalizer',
434                 'backup_interval' => 'none',
435                 'backup_copies' => 1,
436                 'allow_override' => 'All',
437                 'pm_process_idle_timeout' => 10,
438                 'pm_max_requests' => 0
439             );
a93288 440
7fe908 441             // find already inserted domain
MC 442             $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain = '" . $entry['name'] . "'");
443             if(!$old_domain) $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE CONCAT(subdomain, '.', domain) = '" . $entry['name'] . "'");
444             if($old_domain) {
445                 $new_id = $old_domain['domain_id'];
446                 $msg .= "Found domain with id " . $new_id . ", updating it.<br />";
447                 $params = array_merge($old_domain, $params);
448                 $ok = $importer->sites_web_domain_update($session_id, $plesk_ispc_ids[$entry['cl_id']], $new_id, $params);
449                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
450             } else {
451                 $new_id = $importer->sites_web_domain_add($session_id, $plesk_ispc_ids[$entry['cl_id']], $params, true); // read only...
452             }
a93288 453
7fe908 454             $domain_ids[$entry['id']] = $new_id;
MC 455             $domain_roots[$entry['id']] = $entry['www_root'];
456             $domain_owners[$entry['id']] = $entry['cl_id'];
457             $dns_domain_ids[$entry['dns_zone_id']] = $entry['id'];
458
459             if($new_id === false) {
460                 //something went wrong here...
461                 $msg .= "Domain " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
462                 $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
463             } else {
464                 $msg .= "Domain " . $entry['id'] . " (" . $entry['name'] . ") inserted -> " . $new_id . ".<br />";
465             }
466
467             // add domain to mail domains too
468             $params = array(
469                 'server_id' => $server_id,
470                 'domain' => $entry['name'],
471                 'active' => yes_no(($entry['status'] == 0 ? 1 : 0))
472             );
473             $old_domain = $app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = '" . $entry['name'] . "'");
474             if($old_domain) {
475                 $new_id = $old_domain['domain_id'];
476                 $params = array_merge($old_domain, $params);
477                 $msg .= "Found maildomain with id " . $new_id . ", updating it.<br />";
478                 $ok = $importer->mail_domain_update($session_id, $plesk_ispc_ids[$entry['cl_id']], $new_id, $params);
479                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
480             } else {
481                 $msg .= "Inserting new maildomain " . $entry['name'] . ".<br />";
482                 $new_id = $importer->mail_domain_add($session_id, $plesk_ispc_ids[$entry['cl_id']], $params);
483             }
484
485             $maildomain_ids[$entry['id']] = $new_id;
486             if($new_id === false) {
487                 //something went wrong here...
488                 $msg .= "Maildomain (" . $entry['name'] . ") could not be inserted.<br />";
489                 $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
490             } else {
491                 $msg .= "Maildomain " . $new_id . " (" . $entry['name'] . ") inserted.<br />";
492             }
493
494         }
495
496         $domain_aliases = $exdb->queryAllRecords("SELECT da.id, da.name, da.displayName, da.dns, da.mail, da.web, da.dom_id, da.status FROM domainaliases as da");
497         foreach($domain_aliases as $entry) {
498             $params = array(
499                 'server_id' => $server_id,
500                 'domain' => $entry['name'],
501                 'type' => 'alias',
502                 'parent_domain_id' => $domain_ids[$entry['dom_id']],
503                 'redirect_type' => '',
504                 'redirect_path' => '',
505                 'subdomain' => 'www',
506                 'active' => yes_no(($entry['status'] == 0 && $entry['web'] === 'true') ? 1 : 0)
507             );
508
509             $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain = '" . $entry['name'] . "'");
510             if(!$old_domain) $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE CONCAT(subdomain, '.', domain) = '" . $entry['name'] . "'");
511             if($old_domain) {
512                 $new_id = $old_domain['domain_id'];
513                 $params = array_merge($old_domain, $params);
514                 $msg .= "Found domain with id " . $new_id . ", updating it.<br />";
515                 $ok = $importer->sites_web_aliasdomain_update($session_id, $plesk_ispc_ids[$domain_owners[$entry['dom_id']]], $new_id, $params);
516                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
517             } else {
518                 $new_id = $importer->sites_web_aliasdomain_add($session_id, $plesk_ispc_ids[$domain_owners[$entry['dom_id']]], $params);
519             }
520
521             if($new_id === false) {
522                 //something went wrong here...
523                 $msg .= "Aliasdomain " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
524                 $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
525             } else {
526                 $msg .= "Aliasdomain " . $entry['id'] . " (" . $entry['name'] . ") inserted.<br />";
527             }
528
529             // add alias to mail domains, too
530             $params = array(
531                 'server_id' => $server_id,
532                 'domain' => $entry['name'],
533                 'active' => yes_no(($entry['status'] == 0 && $entry['mail'] === 'true') ? 1 : 0)
534             );
535
536             $old_domain = $app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = '" . $entry['name'] . "'");
537             if($old_domain) {
538                 $new_id = $old_domain['domain_id'];
539                 $params = array_merge($old_domain, $params);
540                 $msg .= "Found mail domain with id " . $new_id . ", updating it.<br />";
541                 $ok = $importer->sites_web_aliasdomain_update($session_id, $plesk_ispc_ids[$domain_owners[$entry['dom_id']]], $new_id, $params);
542                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
543             } else {
544                 $new_id = $importer->mail_domain_add($session_id, $plesk_ispc_ids[$domain_owners[$entry['dom_id']]], $params);
545             }
546
547             $maildomain_ids[$entry['id']] = $new_id;
548             if($new_id === false) {
549                 //something went wrong here...
550                 $msg .= "Aliasmaildomain " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
551                 $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
552             } else {
553                 $msg .= "Aliasmaildomain " . $entry['id'] . " (" . $entry['name'] . ") inserted.<br />";
554             }
555         }
556
557         // subdomains in plesk are real vhosts, so we have to treat them as vhostsubdomains
558         $subdomains = $exdb->queryAllRecords("SELECT d.id, d.dom_id, d.name, d.displayName, d.sys_user_id, d.ssi, d.php, d.cgi, d.perl, d.python, d.fastcgi, d.miva, d.coldfusion, d.asp, d.asp_dot_net, d.ssl, d.same_ssl, d.php_handler_type, d.www_root, d.maintenance_mode, d.certificate_id FROM subdomains as d");
559         $subdomain_ids = array();
560         $subdomain_roots = array();
561         $subdomain_owners = array();
562         foreach($subdomains as $entry) {
563             $res = $exdb->query("SELECT d.dom_id, d.param, d.val FROM dom_param as d WHERE d.dom_id = '" . $entry['dom_id'] . "'");
564             $options = array();
565             while($opt = $exdb->nextRecord()) {
566                 $options[$opt['param']] = $opt['val'];
567             }
568
569             $parent_domain = $exdb->queryOneRecord("SELECT d.id, d.cl_id, d.name FROM domains as d WHERE d.id = '" . $entry['dom_id'] . "'");
570
571             /* TODO: options that might be used later:
a93288 572              * OveruseBlock true/false
M 573              * OveruseNotify true/false
574              * OveruseSuspend true/false
575              * wu_script true/false (webusers allowed to use scripts?)
576              * webmail string (webmailer used - horde)
577              */
7fe908 578
MC 579             $redir_type = '';
580             $redir_path = '';
581
582             if($entry['htype'] === 'std_fwd') {
583                 // redirection
584                 $redir = $exdb->queryOneRecord("SELECT f.dom_id, f.ip_address_id, f.redirect FROM forwarding as f WHERE f.dom_id = '" . $entry['id'] . "'");
585                 $redir_type = 'R,L';
586                 $redir_path = $redir['redirect'];
587             } elseif($entry['htype'] === 'vrt_hst') {
588                 // default virtual hosting (vhost)
589             } else {
590                 /* TODO: unknown type */
591             }
592
593             $hosting = $exdb->queryOneRecord("SELECT h.dom_id, h.sys_user_id, h.ip_address_id, h.real_traffic, h.fp, h.fp_ssl, h.fp_enable, h.fp_adm, h.fp_pass, h.ssi, h.php, h.cgi, h.perl, h.python, h.fastcgi, h.miva, h.coldfusion, h.asp, h.asp_dot_net, h.ssl, h.webstat, h.same_ssl, h.traffic_bandwidth, h.max_connection, h.php_handler_type, h.www_root, h.maintenance_mode, h.certificate_id FROM hosting as h WHERE h.dom_id = '" . $entry['dom_id'] . "'");
594             $hosting = array_merge($hosting, $entry); //settings from subdomain override parent settings
595
596             $phpmode = 'no';
597             if(get_option($hosting, 'php', 'false') === 'true') {
598                 $mode = get_option($hosting, 'php_handler_type', 'module');
599                 if($mode === 'module') $phpmode = 'mod';
600                 else $phpmode = 'fast-cgi';
601                 /* TODO: what other options could be in "php_handler_type"? */
602             }
603             /* TODO: plesk offers some more options:
a93288 604              * sys_user_id -> owner of files?
M 605              * ip_address_id - needed?
606              * fp - frontpage extensions
607              * miva - ?
608              * coldfusion
609              * asp
610              * asp_dot_net
611              * traffic_bandwidth
612              * max_connections
613              */
614
7fe908 615             $params = array(
MC 616                 'server_id' => $server_id,
617                 'ip_address' => '*',
618                 //'ipv6_address' => '',
619                 'domain' => $entry['name'] . '.' . $parent_domain['name'],
620                 'web_folder' => $entry['www_root'],
621                 'type' => 'vhostsubdomain', // can be vhost or alias
622                 'parent_domain_id' => $domain_ids[$entry['dom_id']],
623                 'vhost_type' => 'name', // or ip (-based)
624                 'hd_quota' => byte_to_mbyte(get_limit($limits, $entry['dom_id'], 'disk_space', -1)),
625                 'traffic_quota' => byte_to_mbyte(get_limit($limits, $entry['dom_id'], 'max_traffic', -1)),
626                 'cgi' => yes_no(get_option($hosting, 'cgi', 'false') === 'true' ? 1 : 0),
627                 'ssi' => yes_no(get_option($hosting, 'ssi', 'false') === 'true' ? 1 : 0),
628                 'suexec' => yes_no(1), // does plesk use this?!
629                 'errordocs' => get_option($options, 'apacheErrorDocs', 'false') === 'true' ? 1 : 0,
630                 'subdomain' => '', // plesk always uses this option
631                 'ssl' => yes_no(get_option($hosting, 'ssl', 'false') === 'true' ? 1 : 0),
632                 'php' => $phpmode,
633                 'fastcgi_php_version' => '', // plesk has no different php versions
634                 'ruby' => yes_no(0), // plesk has no ruby support
635                 'python' => yes_no(get_option($hosting, 'python', 'false') === 'true' ? 1 : 0),
636                 'active' => yes_no(($entry['status'] == 0 && get_option($hosting, 'maintenance_mode', 'false') !== 'true') ? 1 : 0),
637                 'redirect_type' => $redir_type,
638                 'redirect_path' => $redir_path,
639                 'seo_redirect' => '',
640                 'ssl_state' => $entry[''],
641                 'ssl_locality' => $entry[''],
642                 'ssl_organisation' => $entry[''],
643                 'ssl_organisation_unit' => $entry[''],
644                 'ssl_country' => $entry[''],
645                 'ssl_domain' => $entry[''],
646                 'ssl_request' => $entry[''],
647                 'ssl_cert' => $entry[''],
648                 'ssl_bundle' => $entry[''],
649                 'ssl_action' => $entry[''],
650                 'stats_password' => '',
651                 'stats_type' => get_option($hosting, 'webstat', 'webalizer') === 'awstats' ? 'awstats' : 'webalizer',
652                 'backup_interval' => 'none',
653                 'backup_copies' => 1,
654                 'allow_override' => 'All',
655                 'pm_process_idle_timeout' => 10,
656                 'pm_max_requests' => 0
657             );
658
659             $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain = '" . $entry['name'] . '.' . $parent_domain['name'] . "'");
660             if(!$old_domain) $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE CONCAT(subdomain, '.', domain) = '" . $entry['name'] . "'");
661             if($old_domain) {
662                 $new_id = $old_domain['domain_id'];
663                 $params = array_merge($old_domain, $params);
664                 $msg .= "Found domain with id " . $new_id . ", updating it.<br />";
665                 $ok = $importer->sites_web_vhost_subdomain_update($session_id, $plesk_ispc_ids[$parent_domain['cl_id']], $new_id, $params);
666                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
667             } else {
668                 $new_id = $importer->sites_web_vhost_subdomain_add($session_id, $plesk_ispc_ids[$parent_domain['cl_id']], $params, true); // read only...
669             }
670
671             $subdomain_ids[$entry['id']] = $new_id;
672             $subdomain_roots[$entry['id']] = $entry['www_root'];
673             $subdomain_owners[$entry['id']] = $entry['cl_id'];
674             if($new_id === false) {
675                 //something went wrong here...
676                 $msg .= "Subdomain " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
677                 $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
678             } else {
679                 $msg .= "Subdomain " . $entry['id'] . " (" . $entry['name'] . ") inserted.<br />";
680             }
681         }
682
683         // dns have to be done AFTER domains due to missing client info
684         /*
a93288 685         $dns_zone_ids = array();
M 686         $dns_zone_serials = array();
687         $dns_zones = $exdb->queryAllRecords("SELECT d.id, d.name, d.displayName, d.status, d.email, d.type, d.ttl, d.ttl_unit, d.refresh, d.refresh_unit, d.retry, d.retry_unit, d.expire, d.expire_unit, d.minimum, d.minimum_unit, d.serial_format, d.serial FROM dns_zone as d");
688         foreach($dns_zones as $entry) {
689             $ns = $exdb->queryOneRecord("SELECT d.id, d.val FROM dns_recs as d WHERE d.dns_zone_id = '" . $entry['id'] . "' AND d.type = 'NS'");
690             if(!$ns) $ns = array('id' => 0, 'val' => 'ns.' . $entry['name']);
7fe908 691
a93288 692             $dom_id = $dns_domain_ids[$entry['id']];
M 693             $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
694             if(!$client_id) $client_id = 0;
7fe908 695
a93288 696             $params = array(
M 697                             'server_id' => $server_id,
698                             'origin' => add_dot($entry['name']), // what to put here?
699                             'ns' => add_dot($ns['val']), // what to put here?
700                             'mbox' => str_replace('@', '.', add_dot($entry['email'])),
701                             'serial' => $entry['serial'],
702                             'refresh' => $entry['refresh'],
703                             'retry' => $entry['retry'],
704                             'expire' => $entry['expire'],
705                             'minimum' => $entry['minimum'],
706                             'ttl' => $entry['ttl'],
707                             'xfer' => '',
708                             'also_notify' => '',
709                             'update_acl' => '',
710                             'active' => yes_no(($entry['status'] == 0 ? 1 : 0))
711                             );
7fe908 712
a93288 713             $old_dns = $app->db->queryOneRecord("SELECT id FROM dns_soa WHERE origin = '" . add_dot($entry['name']) . "'");
M 714             if($old_dns) $old_id = $old_dns['id'];
715             if($old_id) {
716                 $new_id = $old_id;
717                 $ok = $importer->dns_zone_update($session_id, $client_id, $old_id, $params);
b0191f 718                 /if($ok === false) {
a93288 719                 //    $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") could not be updated.<br />";
M 720                 //    $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
721                 //} else {
722                     $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") updated.<br />";
723                 //}
724             } else {
725                 $new_id = $importer->dns_zone_add($session_id, $client_id, $params);
726                 if($new_id === false) {
727                     //something went wrong here...
728                     $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
729                     $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
730                 } else {
731                     $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") inserted.<br />";
732                 }
733             }
734             $dns_zone_ids[$entry['id']] = $new_id;
735             $dns_zone_serials[$entry['id']] = $entry['serial'];
736         }
737         unset($dns_zones);
b0191f 738         */
7fe908 739         /* types:
a93288 740          * PTR, NS, A, CNAME, MX, TXT, AAAA
b0191f 741          *//*
a93288 742         $dns_records = $exdb->queryAllRecords("SELECT d.id, d.dns_zone_id, d.type, d.displayHost, d.host, d.displayVal, d.val, d.opt, d.time_stamp FROM dns_recs as d");
M 743         foreach($dns_records as $entry) {
744             $dns_id = (array_key_exists($entry['dns_zone_id'], $dns_zone_ids) ? $dns_zone_ids[$entry['dns_zone_id']] : 0);
745             if(!$dns_id) {
746                 // entry for missing dns zone...?
747                 continue;
748             }
7fe908 749
a93288 750             $dom_id = $dns_domain_ids[$entry['dns_zone_id']];
M 751             $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
752             if(!$client_id) $client_id = 0;
7fe908 753
a93288 754             $params = array(
M 755                         'server_id' => $server_id,
756                         'zone' => $dns_id,
757                         'name' => add_dot($entry['host']),
758                         'type' => $entry['type'],
759                         'data' => $entry['val'],
760                         //'ttl' => '',
761                         'active' => yes_no(1),
762                         'stamp' => $entry['time_stamp'],
763                         //'serial' => $dns_zone_serials[$entry['id']]
764                         );
7fe908 765
MC 766
a93288 767             $record = $app->db->queryOneRecord("SELECT id FROM dns_rr WHERE zone = '" . $dns_zone_ids[$entry['dns_zone_id']] . "' AND name = '" . add_dot($entry['host']) . "' AND type = '" . $entry['type'] . "'");
M 768             $old_id = 0;
769             if($record) {
770                 $old_id = $record['id'];
771             }
7fe908 772
a93288 773             $new_id = false;
M 774             if($entry['type'] === 'MX') {
775                 $params['aux'] = $entry['opt'];
776                 if($old_id) {
777                     $ok = $importer->dns_mx_update($session_id, $client_id, $old_id, $params);
b0191f 778                     if($ok !== false) $new_id = $old_id;
a93288 779                 } else {
M 780                     $new_id = $importer->dns_mx_add($session_id, $client_id, $params);
781                 }
782             } elseif($entry['type'] === 'PTR') {
783                 if($old_id) {
784                     $ok = $importer->dns_ptr_update($session_id, $client_id, $old_id, $params);
b0191f 785                     if($ok !== false) $new_id = $old_id;
a93288 786                 } else {
M 787                     $new_id = $importer->dns_ptr_add($session_id, $client_id, $params);
788                 }
789             } elseif($entry['type'] === 'A') {
790                 if($old_id) {
791                     $ok = $importer->dns_a_update($session_id, $client_id, $old_id, $params);
b0191f 792                     if($ok !== false) $new_id = $old_id;
a93288 793                 } else {
M 794                     $new_id = $importer->dns_a_add($session_id, $client_id, $params);
795                 }
796             } elseif($entry['type'] === 'AAAA') {
797                 if($old_id) {
798                     $ok = $importer->dns_aaaa_update($session_id, $client_id, $old_id, $params);
b0191f 799                     if($ok !== false) $new_id = $old_id;
a93288 800                 } else {
M 801                     $new_id = $importer->dns_aaaa_add($session_id, $client_id, $params);
802                 }
803             } elseif($entry['type'] === 'TXT') {
804                 if($old_id) {
805                     $ok = $importer->dns_txt_update($session_id, $client_id, $old_id, $params);
b0191f 806                     if($ok !== false) $new_id = $old_id;
a93288 807                 } else {
M 808                     $new_id = $importer->dns_txt_add($session_id, $client_id, $params);
809                 }
810             } elseif($entry['type'] === 'CNAME') {
811                 if($old_id) {
812                     $ok = $importer->dns_cname_update($session_id, $client_id, $old_id, $params);
b0191f 813                     if($ok !== false) $new_id = $old_id;
a93288 814                 } else {
M 815                     $new_id = $importer->dns_cname_add($session_id, $client_id, $params);
816                 }
817             } elseif($entry['type'] === 'NS') {
818                 if($old_id) {
819                     $ok = $importer->dns_ns_update($session_id, $client_id, $old_id, $params);
b0191f 820                     if($ok !== false) $new_id = $old_id;
a93288 821                 } else {
M 822                     $new_id = $importer->dns_ns_add($session_id, $client_id, $params);
823                 }
824             }
825             if($new_id === false) {
826                 //something went wrong here...
827                 $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted/updated.<br />";
b0191f 828                 $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />" . var_export($params, true) . '<br />';
a93288 829             } else {
M 830                 $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") inserted/updated.<br />";
831             }
7fe908 832
a93288 833         }
M 834         unset($dns_records);
b0191f 835         */
7fe908 836
MC 837         $folder_ids = array();
838         /* web_folder creation*/
839         $protected_dirs = $exdb->queryAllRecords("SELECT `id`, `non_ssl`, `ssl`, `cgi_bin`, `realm`, `path`, `dom_id` FROM protected_dirs");
840         foreach($protected_dirs as $entry) {
841             if($entry['path'] == 'plesk-stat') continue;
842
843             $params = array('server_id' => $server_id,
844                 'parent_domain_id' => $domain_ids[$entry['dom_id']],
845                 'path' => $entry['path'],
846                 'active' => 'y');
847             $folder_id = 0;
848             $check = $app->db->queryOneRecord('SELECT * FROM `web_folder` WHERE `parent_domain_id` = \'' . $domain_ids[$entry['dom_id']] . '\' AND `path` = \'' . $app->db->quote($entry['path']) . '\'');
849             if($check) {
850                 $ok = $importer->sites_web_folder_update($session_id, $client_id, $check['web_folder_id'], array_merge($check, $params));
851                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
852                 $folder_id = $check['web_folder_id'];
853                 $msg .= 'Updated HTTP AUTH folder (' . $folder_id . '): ' . $entry['path'] . '<br />';
854             } else {
855                 $folder_id = $importer->sites_web_folder_add($session_id, $client_id, $params);
856                 $msg .= 'Created HTTP AUTH folder (' . $folder_id . '): ' . $entry['path'] . '<br />';
857                 if(!$folder_id) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />" . var_export($params, true) . '<br />';
858             }
859
860             $folder_ids[$entry['id']] = $folder_id;
861         }
862
863         $pd_users = $exdb->queryAllRecords("SELECT u.id, u.login, u.account_id, u.pd_id, a.password FROM pd_users as u INNER JOIN accounts as a ON (a.id = u.account_id)");
864         foreach($protected_dirs as $entry) {
865             $params = array('server_id' => $server_id,
866                 'web_folder_id' => $folder_ids[$entry['pd_id']],
867                 'username' => $entry['login'],
868                 'password' => $entry['password'],
869                 'active' => 'y');
870
871             $check = $app->db->queryOneRecord('SELECT * FROM `web_folder_user` WHERE `web_folder_id` = ' . intval($folder_ids[$entry['pd_id']]) . ' AND `username` = \'' . $entry['login'] . '\'');
872             if($check) {
873                 $ok = $importer->sites_web_folder_user_update($session_id, $client_id, $check['web_folder_user_id'], array_merge($check, $params));
874                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
875                 $msg .= 'Updated HTTP AUTH folder user  (' . $fu_id . '): ' . $entry['login'] . '<br />';
876             } else {
877                 $fu_id = $importer->sites_web_folder_user_add($session_id, $client_id, $params);
878                 $msg .= 'Created HTTP AUTH folder user  (' . $fu_id . '): ' . $entry['login'] . '<br />';
879                 if(!$fu_id) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />" . var_export($params, true) . '<br />';
880             }
881         }
882
883         /*$web_users = $exdb->queryAllRecords("SELECT id, dom_id, sys_user_id, ssi, php, cgi, perl, python, fastcgi, asp, asp_dot_net FROM web_users");
a93288 884         foreach($web_users as $entry) {
7fe908 885             $params =
a93288 886         }
M 887         */
888
7fe908 889
MC 890         $ftp_users = $exdb->queryAllRecords("SELECT f.id, f.dom_id, f.sys_user_id, s.login, s.account_id, s.home, s.shell, s.quota, s.mapped_to, a.password, a.type as `pwtype` FROM ftp_users as f INNER JOIN sys_users as s ON (s.id = f.sys_user_id) INNER JOIN accounts as a ON (a.id = s.account_id)");
891         $ftp_users = array_merge($ftp_users, $dom_ftp_users);
892         foreach($ftp_users as $entry) {
893             $parent_domain = $exdb->queryOneRecord("SELECT d.id, d.cl_id, d.name FROM domains as d WHERE d.id = '" . $entry['dom_id'] . "'");
894
895             $ispc_dom_id = $domain_ids[$entry['dom_id']];
896             $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
897             if(!$client_id) $client_id = 0;
898
899             $document_root = str_replace("[website_id]", $ispc_dom_id, $web_config["website_path"]);
900             $document_root = str_replace("[website_idhash_1]", id_hash($ispc_dom_id, 1), $document_root);
901             $document_root = str_replace("[website_idhash_2]", id_hash($ispc_dom_id, 1), $document_root);
902             $document_root = str_replace("[website_idhash_3]", id_hash($ispc_dom_id, 1), $document_root);
903             $document_root = str_replace("[website_idhash_4]", id_hash($ispc_dom_id, 1), $document_root);
904
905             // Set the values for document_root, system_user and system_group
906             $system_user = 'web'.$ispc_dom_id;
907             $system_group = 'client'.$client_id;
908             $document_root = str_replace("[client_id]", $client_id, $document_root);
909             $document_root = str_replace("[client_idhash_1]", id_hash($client_id, 1), $document_root);
910             $document_root = str_replace("[client_idhash_2]", id_hash($client_id, 2), $document_root);
911             $document_root = str_replace("[client_idhash_3]", id_hash($client_id, 3), $document_root);
912             $document_root = str_replace("[client_idhash_4]", id_hash($client_id, 4), $document_root);
913
914             $uid = $system_user;
915             $gid = $system_group;
916
917             $sys_grp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = '" . $client_id . "'");
918             if(!$sys_grp) $sys_grp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = 0");
919
920             if(!$sys_grp) $sys_groupid = 1;
921             else $sys_groupid = $sys_grp['groupid'];
922
923             $params = array(
924                 'server_id' => $server_id,
925                 'parent_domain_id' => $domain_ids[$entry['dom_id']],
926                 'username' => $entry['login'],
927                 'password' => $entry['password'],
928                 'quota_size' => byte_to_mbyte(($entry['quota'] == 0 ? -1 : $entry['quota'])),
929                 'active' => yes_no(1),
930                 'uid' => $uid,
931                 'gid' => $gid,
932                 'dir' => $document_root . (substr($document_root, -1) !== '/' ? '/' : ''),
933                 'sys_groupid' => $sys_groupid
934                 //'quota_files' => $entry[''],
935                 //'ul_ratio' => $entry[''],
936                 //'dl_ratio' => $entry[''],
937                 //'ul_bandwidth' => $entry[''],
938                 //'dl_bandwidth' => $entry['']
939             );
940             $new_id = false;
941             $old_ftp = $app->db->queryOneRecord("SELECT ftp_user_id, parent_domain_id FROM ftp_user WHERE username = '" . $entry['login'] ."'");
942             if($old_ftp) {
943                 if($old_ftp['parent_domain_id'] != $domain_ids[$entry['dom_id']]) {
944                     $msg .= "FTP Account conflicts with other domain!<br />";
945                 } else {
946                     $new_id = $old_ftp['ftp_user_id'];
947                     $ok = $importer->sites_ftp_user_update($session_id, $client_id, $new_id, $params);
948                     if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
949                 }
950             } else {
951                 $new_id = $importer->sites_ftp_user_add($session_id, $client_id, $params);
952             }
953             if($new_id === false) {
954                 //something went wrong here...
955                 $msg .= "FTP " . $entry['id'] . " (" . $entry['login'] . ") could not be inserted.<br />";
956                 $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
957                 $msg .= "Params: " . var_export($params, true) . "<br />";
958             } else {
959                 $msg .= "FTP Account " . $entry['id'] . " (" . $entry['login'] . ") inserted.<br />";
960             }
961         }
962
963         $mail_config = $app->getconf->get_server_config($server_id, 'mail');
964
965         $mail_addresses = $exdb->queryAllRecords("SELECT m.id, m.mail_name, m.perm_id, m.postbox, m.account_id, m.redirect, m.redir_addr, m.mail_group, m.autoresponder, m.spamfilter, m.virusfilter, m.mbox_quota, m.dom_id, m.userId, a.password, a.type as `pwtype` FROM mail as m LEFT JOIN accounts as a ON (a.id = m.account_id) ");
966         $mail_ids = array();
967         foreach($mail_addresses as $entry) {
968
969             $parent_domain = $exdb->queryOneRecord("SELECT d.id, d.cl_id, d.name FROM domains as d WHERE d.id = '" . $entry['dom_id'] . "'");
970             if(!$parent_domain) {
971                 $msg .= "Could not insert/update mail address " . $entry['mail_name'] . " as domain is missing.<br />";
972                 continue;
973             }
974
975             /* postbox true/false
a93288 976              * mail_group true/false
M 977              * spamfilter true/false
978              */
979
980
7fe908 981             $has_responder = false;
MC 982             if($entry['autoresponder'] === 'true') {
983                 $responder = $exdb->queryOneRecord("SELECT id, mn_id, resp_name, keystr, key_where, subject, reply_to, content_type, charset, text, resp_on, ans_freq, mem_limit FROM mail_resp WHERE mn_id = '" . $entry['id'] . "'");
984                 if($responder) $has_responder = true;
985             }
a93288 986
7fe908 987             $maildir = str_replace("[domain]", $parent_domain["name"], $mail_config["maildir_path"]);
MC 988             $maildir = str_replace("[localpart]", strtolower($entry["mail_name"]), $maildir);
989
990
991             $params = array(
992                 'server_id' => $server_id,
993                 'email' => $entry['mail_name'] . "@" . $parent_domain['name'],
994                 'login' => strtolower($entry['mail_name'] . "@" . $parent_domain['name']),
995                 'password' => $entry['password'],
996                 'name' => $entry[''],
997                 'quota' => ($entry['mbox_quota'] == -1 ? 0 : $entry['mbox_quota']), // in bytes!
998                 'cc' => $entry['redir_addr'],
999                 'maildir' => $maildir,
1000                 'homedir' => $mail_config["homedir_path"],
1001                 'uid' => $mail_config["mailuser_uid"],
1002                 'gid' => $mail_config["mailuser_gid"],
1003                 'postfix' => yes_no(1),
1004                 'disableimap' => yes_no(0),
1005                 'disablepop3' => yes_no(0),
1006                 'autoresponder_subject' => ($has_responder ? $responder['subject'] : ''),
1007                 'autoresponder_text' => ($has_responder ? $responder['text'] : ''),
1008                 'autoresponder' => yes_no($has_responder ? 1 : 0),
1009                 'autoresponder_start_date' => ($has_responder && $responder['resp_on'] === 'true' ? strftime('%Y-%m-%d', time()) : strftime('%Y-%m-%d', time() - (3600*24))),
1010                 'autoresponder_end_date' => ($has_responder && $responder['resp_on'] === 'true' ? strftime('%Y-%m-%d', time() + (3600*24*365)) : strftime('%Y-%m-%d', time())),
1011                 'move_junk' => yes_no(0)
1012             );
1013             $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
1014
1015             // if this is no postbox we do not need to create a mailuser
1016             if($entry['postbox'] !== 'false') {
1017                 $old_mail = $app->db->queryOneRecord("SELECT mailuser_id FROM mail_user WHERE email = '" . $entry['mail_name'] . "@" . $parent_domain['name'] . "'");
1018                 if($old_mail) {
1019                     $new_id = $old_mail['mailuser_id'];
1020                     $ok = $importer->mail_user_update($session_id, $client_id, $new_id, $params);
1021                     if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1022                 } else {
1023                     $new_id = $importer->mail_user_add($session_id, $client_id, $params);
1024                 }
1025
1026                 if($new_id === false) {
1027                     //something went wrong here...
1028                     $msg .= "Mail" . $entry['id'] . " (" . $entry['mail_name'] . "@" . $parent_domain['name'] . ") could not be inserted/updated.<br />";
1029                     $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1030                 } else {
1031                     $msg .= "Mail " . $entry['id'] . " (" . $entry['mail_name'] . "@" . $parent_domain['name'] . ") inserted/updated.<br />";
1032                 }
1033                 $mail_ids[$entry['id']] = $new_id;
1034             }
1035
1036             // select all redirs for this address
1037             $mail_redir = $exdb->queryAllRecords("SELECT id, mn_id, address FROM mail_redir WHERE mn_id = '" . $entry['id'] . "'");
1038             foreach($mail_redir as $redir) {
1039                 $params = array(
1040                     'server_id' => $server_id,
1041                     'source' => $entry['mail_name'] . "@" . $parent_domain['name'],
1042                     'destination' => $redir['address'],
1043                     'type' => 'forward', // or forward
1044                     'active' => yes_no(1)
1045                 );
1046
1047                 $old_mail = $app->db->queryOneRecord("SELECT forwarding_id FROM mail_forwarding WHERE source = '" . $entry['mail_name'] . "@" . $parent_domain['name'] . "' AND destination = '" . $redir['address'] . "'");
1048                 if($old_mail) {
1049                     $new_id = $old_mail['forwarding_id'];
1050                     $ok = $importer->mail_forward_update($session_id, $client_id, $new_id, $params);
1051                     if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1052                 } else {
1053                     $new_id = $importer->mail_forward_add($session_id, $client_id, $params);
1054                 }
1055
1056                 if($new_id === false) {
1057                     //something went wrong here...
1058                     $msg .= "Mail redirect " . $entry['id'] . " (" . $entry['mail_name'] . "@" . $parent_domain['name'] . " to " . $redir['address'] . ") could not be inserted/updated.<br />";
1059                     $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1060                 } else {
1061                     $msg .= "Mail redirect " . $entry['id'] . " (" . $entry['mail_name'] . "@" . $parent_domain['name'] . " to " . $redir['address'] . ") inserted/updated.<br />";
1062                 }
1063             }
1064             unset($mail_redir);
1065         }
1066         unset($mail_addresses);
1067
1068         $mail_aliases = $exdb->queryAllRecords("SELECT a.id, a.mn_id, a.alias, m.dom_id, m.mail_name FROM mail_aliases as a INNER JOIN mail as m ON (m.id = a.mn_id)");
1069         foreach($mail_aliases as $entry) {
1070
1071             $parent_domain = $exdb->queryOneRecord("SELECT d.id, d.cl_id, d.name FROM domains as d WHERE d.id = '" . $entry['dom_id'] . "'");
1072             if(!$parent_domain) {
1073                 $msg .= "Could not insert/update mail alias " . $entry['alias'] . " as domain is missing.<br />";
1074                 continue;
1075             }
1076
1077             $params = array(
1078                 'server_id' => $server_id,
1079                 'source' => $entry['alias'] . "@" . $parent_domain['name'],
1080                 'destination' => $entry['mail_name'] . "@" . $parent_domain['name'],
1081                 'type' => 'alias', // or forward
1082                 'active' => yes_no(1)
1083             );
1084             $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
1085
1086             $old_mail = $app->db->queryOneRecord("SELECT forwarding_id FROM mail_forwarding WHERE source = '" . $entry['alias'] . "@" . $parent_domain['name'] . "' AND destination = '" . $entry['mail_name'] . "@" . $parent_domain['name'] . "'");
1087             if($old_mail) {
1088                 $new_id = $old_mail['forwarding_id'];
1089                 $ok = $importer->mail_alias_update($session_id, $client_id, $new_id, $params);
1090                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1091             } else {
1092                 $new_id = $importer->mail_alias_add($session_id, $client_id, $params);
1093             }
1094
1095             if($new_id === false) {
1096                 //something went wrong here...
1097                 $msg .= "Mail alias " . $entry['id'] . " (" . $entry['alias'] . "@" . $parent_domain['name'] . ") could not be inserted/updated.<br />";
1098                 $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1099             } else {
1100                 $msg .= "Mail alias " . $entry['id'] . " (" . $entry['alias'] . "@" . $parent_domain['name'] . ") inserted/updated.<br />";
1101             }
1102         }
1103         unset($mail_aliases);
1104
1105         //spamfilter // preferences = true/false, username = email address, can be *@*
1106         //id, username, preferences
1107
1108         //spamfilter_preferences
1109         //prefid, spamfilter_id, preference, value
1110
1111
1112
1113         //$client_traffic = $exdb->queryAllRecords("SELECT t.cl_id, t.date, t.http_in, t.http_out, t.ftp_in, t.ftp_out, t.smtp_in, t.smtp_out, t.pop3_imap_in, t.pop3_imap_out FROM ClientsTraffic as t");
1114
1115         $db_userids = array();
1116
1117         $db_users  = $exdb->queryAllRecords("SELECT u.id, u.login, u.account_id, u.db_id, a.password, a.type as `pwtype` FROM db_users as u LEFT JOIN accounts as a ON (a.id = u.account_id)");
1118         foreach($db_users as $db_user) {
1119             // database user
1120             $params = array('server_id' => $server_id,
1121                 'database_user' => $db_user['login'],
1122                 'database_password' => $db_user['password']);
1123             $check = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user` = \'' . $app->db->quote($db_user['login']) . '\'');
1124             $db_user_id = 0;
1125             if($check) {
1126                 $ok = $importer->sites_database_user_update($session_id, $client_id, $check['database_user_id'], array_merge($check, $params));
1127                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1128                 $db_user_id = $check['database_user_id'];
1129             } else {
1130                 $db_user_id = $importer->sites_database_user_add($session_id, $client_id, $params);
1131             }
1132
1133             if(!isset($db_userids[$db_user['db_id']])) $db_userids[$db_user['db_id']] = $db_user_id;
1134             $msg .= 'Created / updated database user: ' . $db_user['login'] . '<br />';
1135         }
1136
1137         $databases  = $exdb->queryAllRecords("SELECT d.id, d.name, d.type, d.dom_id, d.db_server_id, d.default_user_id FROM `data_bases` as d");
1138         foreach($databases as $database) {
1139             $params = array('server_id' => $server_id,
1140                 'parent_domain_id' => $domain_ids[$database['dom_id']],
1141                 'type' => 'mysql',
1142                 'database_name' => $database['name'],
1143                 'database_user_id' => $db_userids[$database['id']],
1144                 'database_ro_user_id' => 0,
1145                 'database_charset' => 'utf8',
1146                 'remote_access' => 'n',
1147                 'active' => 'y',
1148                 'remote_ips' => '');
1149
1150             $check = $app->db->queryOneRecord('SELECT * FROM `web_database` WHERE `database_name` = \'' . $app->db->quote($database['name']) . '\'');
1151             if($check) {
1152                 $ok = $importer->sites_database_update($session_id, $client_id, $check['database_id'], array_merge($check, $params));
1153                 if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1154             } else {
1155                 $importer->sites_database_add($session_id, $client_id, $params);
1156             }
1157
1158             $msg .= 'Created / updated database: ' . $database['name'] . '<br />';
1159         }
1160
1161         // do we need table disk_usage for import? i think we don't
1162
1163         // name is domain name, displayName is including "Umlaute"
1164         //$anon_ftp = $exdb->queryAllRecords("SELECT f.id, f.dom_id, f.max_conn, f.bandwidth, f.incoming, f.incoming_readable, f.incoming_subdirs, f.status, f.quota, f.display_login, f.login_text FROM anon_ftp as f");
1165
1166
1167         //DomainServices
1168         //id, dom_id, type, status, parameters_id, ipCollectionId
1169
1170         //DomainsTraffic
1171         //dom_id, date, http_in, http_out, ftp_in, ftp_out, smtp_in, smtp_out, pop3_imap_in, pop3_imap_out
1172
1173
1174         //IP_Adresses
1175         //id, ip_address, mask, iface, ssl_certificate_id, default_domain_id, ftps, main, status
1176
1177         //ip_pool
1178         //id, ip_address_id, type
1179
1180         /* TODO:
a93288 1181                 */
7fe908 1182         //misc // needed? global settings
MC 1183         //param, val
1184
1185         //Permissions
1186         //id, permission, value
1187
1188         //smb_users // pass is base64 encoded plaintext
1189         //id, login, password, contactName, email, companyName, phone, fax, address, city, state, zip, country, creationDate, isBuiltIn, roleId, uuid, isLocked, authCookie, sessionId, externalId, ownerId, isDomainAdmin, additionalInfo, imNumber, imType, isLegacyUser
1190
1191         /* TODO:
a93288 1192         sys_users // mapped_to = parent_id
M 1193         id, login, account_id, home, shell, quota, mapped_to
7fe908 1194
a93288 1195          */
M 1196     } else {
7fe908 1197         $msg .= 'Connecting to external database failed!<br />';
MC 1198         $msg .= $exdb->connect_error;
1199         $msg .= substr($exdb->errorMessage, 0, 25);
1200
a93288 1201         $error .= $exdb->errorMessage;
M 1202     }
7fe908 1203
a93288 1204     //* restore db login details
M 1205     /*$conf['db_host'] = $conf_bak['db_host'];
1206     $conf['db_database'] = $conf_bak['db_database'];
1207     $conf['db_user'] = $conf_bak['db_user'];
1208     $conf['db_password'] = $conf_bak['db_password'];*/
7fe908 1209
a93288 1210 }
M 1211
7fe908 1212 $app->tpl->setVar('msg', $msg);
MC 1213 $app->tpl->setVar('error', $error);
a93288 1214
5af0cf 1215 //* SET csrf token
TB 1216 $csrf_token = $app->auth->csrf_token_get('plesk_import');
1217 $app->tpl->setVar('_csrf_id',$csrf_token['csrf_id']);
1218 $app->tpl->setVar('_csrf_key',$csrf_token['csrf_key']);
a93288 1219
M 1220 $app->tpl_defaults();
1221 $app->tpl->pparse();
1222
1223
7fe908 1224 ?>