import_plesk.php 79.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
<?php
/*
Copyright (c) 2008, Till Brehm, projektfarm Gmbh
Plesk(r) Importer (c) 2012, Marius Cramer, pixcept KG
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.
*/

global $app, $conf;

require_once('../../lib/config.inc.php');

require_once('../../lib/app.inc.php');

/**
 *
 * @param db $exdb
 * @return array 
 */
function read_limit_data($exdb) {
    $limits = array();
    // Limits
    $limit_data = $exdb->queryAllRecords("SELECT l.id, l.limit_name, l.value FROM Limits as l");
    foreach($limit_data as $entry) {
        if(array_key_exists($entry['id'], $limits) == false) $limits[$entry['id']] = array();
        $limits[$entry['id']][$entry['limit_name']] = $entry['value'];

        // limits that are there:
        /*
        disk_space
        disk_space_soft
        expiration
        max_box
        max_db
        max_dom_aliases
        max_maillists
        max_mn
        max_site
        max_site_builder
        max_subdom
        max_subftp_users
        max_traffic
        max_traffic_soft
        max_unity_mobile_sites
        max_webapps
        max_wu
        mbox_quota
        */
    }
    
    return $limits;
}

/**
 *
 * @param array $limits
 * @param int $id
 * @param string $limit
 * @param mixed $default
 * @return mixed 
 */
function get_limit($limits, $id, $limit, $default = false) {
    $ret = $default;
    if(isset($limits[$id][$limit])) $ret = $limits[$id][$limit];
    
    return $ret;
}

function get_option($options, $option, $default = false) {
    $ret = $default;
    if(isset($options[$option])) $ret = $options[$option];
    
    return $ret;
}

function add_dot($string) {
    if(strlen($string) > 0 && substr($string, -1, 1) !== '.') $string .= '.';
    return $string;
}

function byte_to_mbyte($byte) {
    if($byte <= 0) return $byte; // limit = -1 -> unlimited
    return round($byte / (1024*1024));
}

function yes_no($num, $reverse = false) {
    return (($num == 1 && !$reverse) || ($num != 1 && $reverse) ? 'y' : 'n');
}

// taken from the web_domain_edit.php
function id_hash($id,$levels) {
    $hash = "" . $id % 10 ;
    $id /= 10 ;
    $levels -- ;
    while ( $levels > 0 ) {
        $hash .= "/" . $id % 10 ;
        $id /= 10 ;
        $levels-- ;
    }
    return $hash;
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
$COMMANDS = 'unset HISTFILE
MYSERVER="192.168.1.10"
MYSQL_EXPORT_USER="root"
MYSQL_EXPORT_PASS=""
MYSQL_IMPORT_USER="root"
MYSQL_IMPORT_PASS=""
';

function add_command($cmd) {
    global $COMMANDS;
    
    $COMMANDS .= $cmd . "\n";
}


140 141 142 143 144 145 146 147
/* TODO: document root rewrite on ftp account and other home directories */

//* 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.');

148
$app->uses('tpl,getconf');
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
$app->load('importer');

$app->tpl->newTemplate('form.tpl.htm');
$app->tpl->setInclude('content_tpl', 'templates/import_plesk.htm');
$msg = '';
$error = '';

// Start migrating plesk data
if(isset($_POST['start']) && $_POST['start'] == 1) {
	
	//* Set variable sin template
	$app->tpl->setVar('dbhost',$_POST['dbhost']);
	$app->tpl->setVar('dbname',$_POST['dbname']);
	$app->tpl->setVar('dbuser',$_POST['dbuser']);
	$app->tpl->setVar('dbpassword',$_POST['dbpassword']);
	$app->tpl->setVar('webcontent',$_POST['webcontent']);
	$app->tpl->setVar('mailcontent',$_POST['mailcontent']);
	
	//* Establish connection to external database
	$msg .= 'Connecting to external database...<br />';
	
	//* Backup DB login details
	/*$conf_bak['db_host'] = $conf['db_host'];
	$conf_bak['db_database'] = $conf['db_database'];
	$conf_bak['db_user'] = $conf['db_user'];
	$conf_bak['db_password'] = $conf['db_password'];*/
	
	//* Set external Login details
	$conf['imp_db_host'] = $_POST['dbhost'];
	$conf['imp_db_database'] = $_POST['dbname'];
	$conf['imp_db_user'] = $_POST['dbuser'];
	$conf['imp_db_password'] = $_POST['dbpassword'];
    $conf['imp_db_charset'] = $conf['db_charset'];
    $conf['imp_db_new_link'] = $conf['db_new_link'];
    $conf['imp_db_client_flags'] = $conf['db_client_flags'];
	
	//* create new db object
	$exdb = new db('imp');
    
    $msg .= 'db object created...<br />';
    
    $importer = new importer();
    $session_id = 'ISPC3'; // set dummy session id for remoting lib
    $msg .= 'importer object created...<br />';
	
    // import on server
	$server_id = 1;
	
	//* Connect to DB
	if($exdb !== false) {
        $msg .= 'Connecting to external database done...<br />';
	
        $limits = read_limit_data($exdb);
        
        $msg .= 'read all limit data...<br />';
	
        // param_id -> cl_params table - not needed for import
        // tpye = admin, reseller, client
        $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");
        $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");
        $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");
        
        $users = array_merge($admins, $resellers, $clients);
        $msg .= 'read all users (' . count($users) . ')...<br />';
	
        
        $plesk_ispc_ids = array(); // array with key = plesk id, value = ispc id
        
        $phpopts = array('no', 'fast-cgi', 'cgi', 'mod', 'suphp', 'php-fpm');
        
        // import admins / resellers
        for($i = 0; $i < count($users); $i++) {
            $entry = $users[$i];
            
            $old_client = $importer->client_get_by_username($session_id, $entry['login']);
            if($old_client) {
                if($old_client['client_id'] == 0) {
                    $entry['login'] = 'psa_' . $entry['login'];
                    $old_client = $importer->client_get_by_username($session_id, $entry['login']);
                    if($old_client) {
                        $msg .= $entry['login'] . ' existed, updating id ' . $old_client['client_id'] . '<br />';
                    }
                } else {
                    $msg .= $entry['login'] . ' existed, updating id ' . $old_client['client_id'] . '<br />';                    
                }
            }
            $params = array(
                            'company_name' => $entry['cname'],
                            'contact_name' => $entry['pname'],
238
                            'customer_no' => 'Plesk' . $entry['id'],
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
                            'username' => $entry['login'],
                            'password' => $entry['password'],
                            'language' => substr($entry['locale'], 0, 2), // plesk stores as de-DE or en-US
                            //'usertheme' => '',
                            'street' => $entry['address'],
                            'zip' => $entry['pcode'],
                            'city' => $entry['city'],
                            'state' => $entry['state'],
                            'country' => $entry['country'],
                            'telephone' => $entry['phone'],
                            //'mobile' => $entry[''],
                            'fax' => $entry['fax'],
                            'email' => $entry['email'],
                            //'internet' => $entry[''],
                            //'icq' => $entry[''],
                            //'vat_id' => $entry[''],
                            //'company_id' => $entry[''],
                            //'bank_account_number' => $entry[''],
                            //'bank_code' => $entry[''],
                            //'bank_name' => $entry[''],
                            //'bank_account_iban' => $entry[''],
                            //'bank_account_swift' => $entry[''],
                            'notes' => 'imported from Plesk id ' . $entry['id'],
                            //'template_master' => $entry[''],
                            //'template_additional' => $entry[''],
                            //'default_mailserver' => $entry[''],
                            'limit_maildomain' => get_limit($limits, $entry['id'], 'max_site', -1),
                            'limit_mailbox' => get_limit($limits, $entry['id'], 'max_box', -1),
                            'limit_mailalias' => get_limit($limits, $entry['id'], 'max_mn', -1),
                            'limit_mailaliasdomain' => get_limit($limits, $entry['id'], 'max_dom_aliases', -1),
                            'limit_mailmailinglist' => get_limit($limits, $entry['id'], 'max_maillists', -1),
                            'limit_mailforward' => get_limit($limits, $entry['id'], 'max_mn', -1),
                            'limit_mailcatchall' => 1,
                            'limit_mailrouting' => 0,
                            'limit_mailfilter' => 0,
                            'limit_fetchmail' => 0,
                            'limit_mailquota' => get_limit($limits, $entry['id'], 'mbox_quota', -1),
                            'limit_spamfilter_wblist' => 0,
                            'limit_spamfilter_user' => 0,
                            'limit_spamfilter_policy' => 0,
                            //'default_webserver' => '',
                            'limit_web_domain' => get_limit($limits, $entry['id'], 'max_site', -1),
281
                            'limit_web_quota' => intval(get_limit($limits, $entry['id'], 'disk_space', -1)),
282 283 284
                            'web_php_options' => implode(',', $phpopts),
                            'limit_web_aliasdomain' => get_limit($limits, $entry['id'], 'max_dom_aliases', -1),
                            'limit_web_subdomain' => get_limit($limits, $entry['id'], 'max_subdom', -1),
285
                            'limit_ftp_user' => (string)($app->functions->intval(get_limit($limits, $entry['id'], 'max_subftp_users', -2)) + 1),
286 287 288 289 290 291 292 293 294 295 296 297 298
                            'limit_shell_user' => 0,
                            'ssh_chroot' => 'no,jailkit',
                            'limit_webdav_user' => get_limit($limits, $entry['id'], 'max_wu', 0),
                            //'default_dnsserver' => '',
                            'limit_dns_zone' => -1,
                            'limit_dns_slave_zone' => -1,
                            'limit_dns_record' => -1,
                            'limit_client' => ($entry['type'] == 'client' ? 0 : -1),
                            //'default_dbserver' => '',
                            'limit_database' => get_limit($limits, $entry['id'], 'max_db', -1),
                            'limit_cron' => 0,
                            'limit_cron_type' => 'url',
                            'limit_cron_frequency' => '5',
299
                            'limit_traffic_quota' => intval(get_limit($limits, $entry['id'], 'max_traffic', -1)),
300 301 302 303 304 305 306 307 308 309 310 311
                            'limit_openvz_vm' => 0,
                            'limit_openvz_vm_template_id' => ''
                            );
            $reseller_id = 0;
            if($entry['parent_id'] != 0) {
                if(array_key_exists($entry['parent_id'], $plesk_ispc_ids)) {
                    $reseller_id = $plesk_ispc_ids[$entry['parent_id']];
                }
            }
            
            if($old_client) {
                $new_id = $old_client['client_id'];
312
                $ok = $importer->client_update($session_id, $old_client['client_id'], $reseller_id, array_merge($old_client, $params));
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
                if($ok === false) {
                    
                }
            } else {
                $new_id = $importer->client_add($session_id, $reseller_id, $params);
            }
            if($new_id === false) {
                //something went wrong here...
                $msg .= "Client " . $entry['id'] . " (" . $entry['pname'] . ") could not be inserted/updated.<br />";
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } else {
                $msg .= "Client " . $entry['id'] . " (" . $entry['pname'] . ") inserted/updated.<br />";
            }
            
            $plesk_ispc_ids[$entry['id']] = $new_id;
        }
        unset($users);
        unset($clients);
        unset($resellers);
        unset($admins);
        
        $web_config = $app->getconf->get_server_config($server_id,'web');
        
mcramer's avatar
Update  
mcramer committed
336
        $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 WHERE d.parentDomainId = 0");
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        $dom_ftp_users = array();
        $domain_ids = array();
        $domain_roots = array();
        $domain_owners = array();
        $dns_domain_ids = array();
        $maildomain_ids = array();
        foreach($domains as $entry) {
            $res = $exdb->query("SELECT d.dom_id, d.param, d.val FROM dom_param as d WHERE d.dom_id = '" . $entry['id'] . "'");
            $options = array();
            while($opt = $exdb->nextRecord()) {
                $options[$opt['param']] = $opt['val'];
            }
            
            /* TODO: options that might be used later:
             * OveruseBlock true/false
             * OveruseNotify true/false
             * OveruseSuspend true/false
             * wu_script true/false (webusers allowed to use scripts?)
             * webmail string (webmailer used - horde)
             */
            
            $redir_type = '';
            $redir_path = '';
            
            if($entry['htype'] === 'std_fwd') {
                // redirection
                $redir = $exdb->queryOneRecord("SELECT f.dom_id, f.ip_address_id, f.redirect FROM forwarding as f WHERE f.dom_id = '" . $entry['id'] . "'");
                $redir_type = 'R,L';
                $redir_path = $redir['redirect'];
            } elseif($entry['htype'] === 'vrt_hst') {
                // default virtual hosting (vhost)
            } else {
                /* TODO: unknown type */
            }
            
372
            $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'] . "'");
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
            if($hosting['sys_user_id']) {
                $dom_ftp_users[] = array('id' => 0,
                                         'dom_id' => $hosting['dom_id'],
                                         'sys_user_id' => $hosting['sys_user_id'],
                                         'login' => $hosting['login'],
                                         'account_id' => $hosting['account_id'],
                                         'home' => $hosting['home'],
                                         'shell' => $hosting['shell'],
                                         'quota' => $hosting['quota'],
                                         'mapped_to' => $hosting['mapped_to'],
                                         'password' => $hosting['password'],
                                         'pwtype' => $hosting['pwtype']
                                        );
            }
            
            $phpmode = 'no';
            if(get_option($hosting, 'php', 'false') === 'true') {
                $mode = get_option($hosting, 'php_handler_type', 'module');
                if($mode === 'module') $phpmode = 'mod';
                else $phpmode = 'fast-cgi';
                /* TODO: what other options could be in "php_handler_type"? */
            }
            
            /* TODO: plesk offers some more options:
             * sys_user_id -> owner of files?
             * ip_address_id - needed?
             * fp - frontpage extensions
             * miva - ?
             * coldfusion
             * asp
             * asp_dot_net
             * traffic_bandwidth
             * max_connections
             */
            $params = array(
                            'server_id' => $server_id,
409
                            'ip_address' => '*',
410 411 412 413 414 415 416 417 418
                            //'ipv6_address' => '',
                            'domain' => $entry['name'],
                            'type' => 'vhost', // can be vhost or alias
                            'parent_domain_id' => '', // only if alias
                            'vhost_type' => 'name', // or ip (-based)
                            'hd_quota' => byte_to_mbyte(get_limit($limits, $entry['id'], 'disk_space', -1)),
                            'traffic_quota' => byte_to_mbyte(get_limit($limits, $entry['id'], 'max_traffic', -1)),
                            'cgi' => yes_no(get_option($hosting, 'cgi', 'false') === 'true' ? 1 : 0),
                            'ssi' => yes_no(get_option($hosting, 'ssi', 'false') === 'true' ? 1 : 0),
419
                            'suexec' => yes_no(1), // does plesk use this?!
420 421 422 423
                            'errordocs' => get_option($options, 'apacheErrorDocs', 'false') === 'true' ? 1 : 0,
                            'subdomain' => 'www', // plesk always uses this option
                            'ssl' => yes_no(get_option($hosting, 'ssl', 'false') === 'true' ? 1 : 0),
                            'php' => $phpmode,
424
                            'fastcgi_php_version' => '', // plesk has no different php versions
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
                            'ruby' => yes_no(0), // plesk has no ruby support
                            'python' => yes_no(get_option($hosting, 'python', 'false') === 'true' ? 1 : 0),
                            'active' => yes_no(($entry['status'] == 0 && get_option($hosting, 'maintenance_mode', 'false') !== 'true') ? 1 : 0),
                            'redirect_type' => $redir_type,
                            'redirect_path' => $redir_path,
                            'seo_redirect' => '',
                            'ssl_state' => $entry[''],
                            'ssl_locality' => $entry[''],
                            'ssl_organisation' => $entry[''],
                            'ssl_organisation_unit' => $entry[''],
                            'ssl_country' => $entry[''],
                            'ssl_domain' => $entry[''],
                            'ssl_request' => $entry[''],
                            'ssl_cert' => $entry[''],
                            'ssl_bundle' => $entry[''],
                            'ssl_action' => $entry[''],
                            'stats_password' => '',
                            'stats_type' => get_option($hosting, 'webstat', 'webalizer') === 'awstats' ? 'awstats' : 'webalizer',
                            'backup_interval' => 'none',
                            'backup_copies' => 1,
                            'allow_override' => 'All',
                            'pm_process_idle_timeout' => 10,
                            'pm_max_requests' => 0
                            );
            
            // find already inserted domain
            $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain = '" . $entry['name'] . "'");
452
            if(!$old_domain) $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE CONCAT(subdomain, '.', domain) = '" . $entry['name'] . "'");
453 454 455 456 457
            if($old_domain) {
                $new_id = $old_domain['domain_id'];
                $msg .= "Found domain with id " . $new_id . ", updating it.<br />";
                $params = array_merge($old_domain, $params);
                $ok = $importer->sites_web_domain_update($session_id, $plesk_ispc_ids[$entry['cl_id']], $new_id, $params);
458
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
459 460 461 462 463 464 465 466 467 468 469 470 471 472
            } else {
                $new_id = $importer->sites_web_domain_add($session_id, $plesk_ispc_ids[$entry['cl_id']], $params, true); // read only...
            }
            
            $domain_ids[$entry['id']] = $new_id;
            $domain_roots[$entry['id']] = $entry['www_root'];
            $domain_owners[$entry['id']] = $entry['cl_id'];
            $dns_domain_ids[$entry['dns_zone_id']] = $entry['id'];
            
            if($new_id === false) {
                //something went wrong here...
                $msg .= "Domain " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } else {
473
                $msg .= "Domain " . $entry['id'] . " (" . $entry['name'] . ") inserted -> " . $new_id . ".<br />";
474 475 476 477 478 479 480 481 482 483
                
                $cmd_data = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = '" . $new_id . "'");
                $path = $cmd_data['document_root'];
                add_command('chattr -i ' . escapeshellarg($path));
                add_command('if [[ -f ' . $path . '/web/index.html ]] ; then rm ' . $path . '/web/index.html ; fi');
                add_command('rsync -av --modify-window 10 --progress -e ssh root@${MYSERVER}:' . $hosting['www_root'] . '/ ' . $path . '/web/');
                add_command('chown -R ' . $cmd_data['system_user'] . ':' . $cmd_data['system_group'] . ' ' . escapeshellarg($path));
                add_command('grep ' . escapeshellarg($hosting['www_root']) . ' ' . $path . '/web -r -l | xargs replace ' . escapeshellarg($hosting['www_root']) . ' ' . escapeshellarg($path . '/web') . ' --');
                add_command('chown -R root:root ' . escapeshellarg($path . '/log') . ' ' . escapeshellarg($path . '/ssl') . ' ' . escapeshellarg($path . '/web/stats'));
                add_command('chattr +i ' . escapeshellarg($path));
484 485 486 487 488 489 490 491 492 493 494 495 496 497
            }
            
            // add domain to mail domains too
            $params = array(
                            'server_id' => $server_id,
                            'domain' => $entry['name'],
                            'active' => yes_no(($entry['status'] == 0 ? 1 : 0))
                            );
            $old_domain = $app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = '" . $entry['name'] . "'");
            if($old_domain) {
                $new_id = $old_domain['domain_id'];
                $params = array_merge($old_domain, $params);
                $msg .= "Found maildomain with id " . $new_id . ", updating it.<br />";
                $ok = $importer->mail_domain_update($session_id, $plesk_ispc_ids[$entry['cl_id']], $new_id, $params);
498
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
499
            } else {
500
                $msg .= "Inserting new maildomain " . $entry['name'] . ".<br />";
501 502 503 504 505 506
                $new_id = $importer->mail_domain_add($session_id, $plesk_ispc_ids[$entry['cl_id']], $params);
            }
            
            $maildomain_ids[$entry['id']] = $new_id;
            if($new_id === false) {
                //something went wrong here...
507
                $msg .= "Maildomain (" . $entry['name'] . ") could not be inserted.<br />";
508 509
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } else {
510
                $msg .= "Maildomain " . $new_id . " (" . $entry['name'] . ") inserted.<br />";
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
            }

        }
        
        $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");
        foreach($domain_aliases as $entry) {
            $params = array(
                            'server_id' => $server_id,
                            'domain' => $entry['name'],
                            'type' => 'alias',
                            'parent_domain_id' => $domain_ids[$entry['dom_id']],
                            'redirect_type' => '',
                            'redirect_path' => '',
                            'subdomain' => 'www',
                            'active' => yes_no(($entry['status'] == 0 && $entry['web'] === 'true') ? 1 : 0)
                            );
            
            $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain = '" . $entry['name'] . "'");
529
            if(!$old_domain) $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE CONCAT(subdomain, '.', domain) = '" . $entry['name'] . "'");
530 531 532 533 534
            if($old_domain) {
                $new_id = $old_domain['domain_id'];
                $params = array_merge($old_domain, $params);
                $msg .= "Found domain with id " . $new_id . ", updating it.<br />";
                $ok = $importer->sites_web_aliasdomain_update($session_id, $plesk_ispc_ids[$domain_owners[$entry['dom_id']]], $new_id, $params);
535
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
            } else {
                $new_id = $importer->sites_web_aliasdomain_add($session_id, $plesk_ispc_ids[$domain_owners[$entry['dom_id']]], $params);
            }
            
            if($new_id === false) {
                //something went wrong here...
                $msg .= "Aliasdomain " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } else {
                $msg .= "Aliasdomain " . $entry['id'] . " (" . $entry['name'] . ") inserted.<br />";
            }
            
            // add alias to mail domains, too
            $params = array(
                        'server_id' => $server_id,
                        'domain' => $entry['name'],
                        'active' => yes_no(($entry['status'] == 0 && $entry['mail'] === 'true') ? 1 : 0)
                        );

            $old_domain = $app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = '" . $entry['name'] . "'");
            if($old_domain) {
                $new_id = $old_domain['domain_id'];
                $params = array_merge($old_domain, $params);
                $msg .= "Found mail domain with id " . $new_id . ", updating it.<br />";
560
                $ok = $importer->mail_domain_update($session_id, $plesk_ispc_ids[$domain_owners[$entry['dom_id']]], $new_id, $params);
561
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
            } else {
                $new_id = $importer->mail_domain_add($session_id, $plesk_ispc_ids[$domain_owners[$entry['dom_id']]], $params);
            }
            
            $maildomain_ids[$entry['id']] = $new_id;
            if($new_id === false) {
                //something went wrong here...
                $msg .= "Aliasmaildomain " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } else {
                $msg .= "Aliasmaildomain " . $entry['id'] . " (" . $entry['name'] . ") inserted.<br />";
            }
        }
        
        $subdomain_ids = array();
        $subdomain_roots = array();
        $subdomain_owners = array();
mcramer's avatar
Update  
mcramer committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
        
        $subdomains = $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, d.parentDomainId FROM domains as d WHERE d.parentDomainId != 0");
        foreach($subdomains as $entry) {
            $res = $exdb->query("SELECT d.dom_id, d.param, d.val FROM dom_param as d WHERE d.dom_id = '" . $entry['id'] . "'");
            $options = array();
            while($opt = $exdb->nextRecord()) {
                $options[$opt['param']] = $opt['val'];
            }
            
            $parent_domain = $exdb->queryOneRecord("SELECT d.id, d.cl_id, d.name FROM domains as d WHERE d.id = '" . $entry['parentDomainId'] . "'");
            $redir_type = '';
            $redir_path = '';
            
            if($entry['htype'] === 'std_fwd') {
                // redirection
                $redir = $exdb->queryOneRecord("SELECT f.dom_id, f.ip_address_id, f.redirect FROM forwarding as f WHERE f.dom_id = '" . $entry['id'] . "'");
                $redir_type = 'R,L';
                $redir_path = $redir['redirect'];
            } elseif($entry['htype'] === 'vrt_hst') {
                // default virtual hosting (vhost)
            } else {
                /* TODO: unknown type */
            }
            
            $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'] . "'");
            if($hosting['sys_user_id']) {
                $dom_ftp_users[] = array('id' => 0,
                                         'dom_id' => $hosting['dom_id'],
                                         'sys_user_id' => $hosting['sys_user_id'],
                                         'login' => $hosting['login'],
                                         'account_id' => $hosting['account_id'],
                                         'home' => $hosting['home'],
                                         'shell' => $hosting['shell'],
                                         'quota' => $hosting['quota'],
                                         'mapped_to' => $hosting['mapped_to'],
                                         'password' => $hosting['password'],
                                         'pwtype' => $hosting['pwtype']
                                        );
            }
            
            $phpmode = 'no';
            if(get_option($hosting, 'php', 'false') === 'true') {
                $mode = get_option($hosting, 'php_handler_type', 'module');
                if($mode === 'module') $phpmode = 'mod';
                else $phpmode = 'fast-cgi';
                /* TODO: what other options could be in "php_handler_type"? */
            }
            /* TODO: plesk offers some more options:
             * sys_user_id -> owner of files?
             * ip_address_id - needed?
             * fp - frontpage extensions
             * miva - ?
             * coldfusion
             * asp
             * asp_dot_net
             * traffic_bandwidth
             * max_connections
             */
            
638 639 640 641 642
            $web_folder = $hosting['www_root'];
            $web_folder = preg_replace('/^\/(var|srv)\/www\/(vhosts\/)?[^\/]+\/(.*)\/httpdocs.*/', '$3', $web_folder);
            
            //if(substr($web_folder, 0, 1) === '/') $web_folder = substr($web_folder, 1);
            //if(substr($web_folder, -1, 1) === '/') $web_folder = substr($web_folder, 0, -1);
mcramer's avatar
Update  
mcramer committed
643 644 645 646
            $params = array(
                            'server_id' => $server_id,
                            'ip_address' => '*',
                            //'ipv6_address' => '',
647 648
                            'domain' => $entry['name'],
                            'web_folder' => $web_folder,
mcramer's avatar
Update  
mcramer committed
649
                            'type' => 'vhostsubdomain', // can be vhost or alias
650
                            'parent_domain_id' => $domain_ids[$entry['parentDomainId']],
mcramer's avatar
Update  
mcramer committed
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
                            'vhost_type' => 'name', // or ip (-based)
                            'hd_quota' => byte_to_mbyte(get_limit($limits, $entry['dom_id'], 'disk_space', -1)),
                            'traffic_quota' => byte_to_mbyte(get_limit($limits, $entry['dom_id'], 'max_traffic', -1)),
                            'cgi' => yes_no(get_option($hosting, 'cgi', 'false') === 'true' ? 1 : 0),
                            'ssi' => yes_no(get_option($hosting, 'ssi', 'false') === 'true' ? 1 : 0),
                            'suexec' => yes_no(1), // does plesk use this?!
                            'errordocs' => get_option($options, 'apacheErrorDocs', 'false') === 'true' ? 1 : 0,
                            'subdomain' => '', // plesk always uses this option
                            'ssl' => yes_no(get_option($hosting, 'ssl', 'false') === 'true' ? 1 : 0),
                            'php' => $phpmode,
                            'fastcgi_php_version' => '', // plesk has no different php versions
                            'ruby' => yes_no(0), // plesk has no ruby support
                            'python' => yes_no(get_option($hosting, 'python', 'false') === 'true' ? 1 : 0),
                            'active' => yes_no(($entry['status'] == 0 && get_option($hosting, 'maintenance_mode', 'false') !== 'true') ? 1 : 0),
                            'redirect_type' => $redir_type,
                            'redirect_path' => $redir_path,
                            'seo_redirect' => '',
                            'ssl_state' => $entry[''],
                            'ssl_locality' => $entry[''],
                            'ssl_organisation' => $entry[''],
                            'ssl_organisation_unit' => $entry[''],
                            'ssl_country' => $entry[''],
                            'ssl_domain' => $entry[''],
                            'ssl_request' => $entry[''],
                            'ssl_cert' => $entry[''],
                            'ssl_bundle' => $entry[''],
                            'ssl_action' => $entry[''],
                            'stats_password' => '',
                            'stats_type' => get_option($hosting, 'webstat', 'webalizer') === 'awstats' ? 'awstats' : 'webalizer',
                            'backup_interval' => 'none',
                            'backup_copies' => 1,
                            'allow_override' => 'All',
                            'pm_process_idle_timeout' => 10,
                            'pm_max_requests' => 0
                            );

            $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain = '" . $entry['name'] . "'");
            if(!$old_domain) $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE CONCAT(subdomain, '.', domain) = '" . $entry['name'] . "'");
            if($old_domain) {
                $new_id = $old_domain['domain_id'];
                $params = array_merge($old_domain, $params);
692
                $msg .= "Found domain " . $entry['name'] . " with id " . $new_id . ", updating it.<br />";
mcramer's avatar
Update  
mcramer committed
693 694 695 696 697 698 699
                $ok = $importer->sites_web_vhost_subdomain_update($session_id, $plesk_ispc_ids[$parent_domain['cl_id']], $new_id, $params);
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } else {
                $new_id = $importer->sites_web_vhost_subdomain_add($session_id, $plesk_ispc_ids[$parent_domain['cl_id']], $params, true); // read only...
            }
            
            $subdomain_ids[$entry['id']] = $new_id;
700
            $subdomain_roots[$entry['id']] = $hosting['www_root'];
mcramer's avatar
Update  
mcramer committed
701 702 703
            $subdomain_owners[$entry['id']] = $entry['cl_id'];
            if($new_id === false) {
                //something went wrong here...
704
                $msg .= "Subdomain " . $entry['id'] . " (" . $entry['name'] . ") with folder \"" . $web_folder . "\" could not be inserted.<br />";
mcramer's avatar
Update  
mcramer committed
705 706 707
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } else {
                $msg .= "Subdomain " . $entry['id'] . " (" . $entry['name'] . ") inserted.<br />";
708 709 710 711 712 713 714 715 716 717 718
                
                $cmd_data = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = '" . $new_id . "'");
                $path = $cmd_data['document_root'];
                add_command('chattr -i ' . escapeshellarg($path));
                add_command('if [[ -f ' . $path . '/' . $web_folder . '/index.html ]] ; then rm ' . $path . '/' . $web_folder . '/index.html ; fi');
                add_command('rsync -av --modify-window 10 --progress -e ssh root@${MYSERVER}:' . $hosting['www_root'] . '/ ' . $path . '/' . $web_folder . '/');
                add_command('chown -R ' . $cmd_data['system_user'] . ':' . $cmd_data['system_group'] . ' ' . escapeshellarg($path));
                add_command('grep ' . escapeshellarg($hosting['www_root']) . ' ' . $path . '/web -r -l | xargs replace ' . escapeshellarg($hosting['www_root']) . ' ' . escapeshellarg($path . '/web') . ' --');
                add_command('chown -R root:root ' . escapeshellarg($path . '/log') . ' ' . escapeshellarg($path . '/ssl') . ' ' . escapeshellarg($path . '/web/stats'));
                add_command('chattr +i ' . escapeshellarg($path));
                
mcramer's avatar
Update  
mcramer committed
719
            }
720
            $domain_ids[$entry['id']] = $new_id;
mcramer's avatar
Update  
mcramer committed
721 722 723 724
        }
        
        // subdomains in plesk are real vhosts, so we have to treat them as vhostsubdomains
        $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");
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
        foreach($subdomains as $entry) {
            $res = $exdb->query("SELECT d.dom_id, d.param, d.val FROM dom_param as d WHERE d.dom_id = '" . $entry['dom_id'] . "'");
            $options = array();
            while($opt = $exdb->nextRecord()) {
                $options[$opt['param']] = $opt['val'];
            }
            
            $parent_domain = $exdb->queryOneRecord("SELECT d.id, d.cl_id, d.name FROM domains as d WHERE d.id = '" . $entry['dom_id'] . "'");
            
            /* TODO: options that might be used later:
             * OveruseBlock true/false
             * OveruseNotify true/false
             * OveruseSuspend true/false
             * wu_script true/false (webusers allowed to use scripts?)
             * webmail string (webmailer used - horde)
             */
            
            $redir_type = '';
            $redir_path = '';
            
            if($entry['htype'] === 'std_fwd') {
                // redirection
                $redir = $exdb->queryOneRecord("SELECT f.dom_id, f.ip_address_id, f.redirect FROM forwarding as f WHERE f.dom_id = '" . $entry['id'] . "'");
                $redir_type = 'R,L';
                $redir_path = $redir['redirect'];
            } elseif($entry['htype'] === 'vrt_hst') {
                // default virtual hosting (vhost)
            } else {
                /* TODO: unknown type */
            }
            
756
            $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'] . "'");
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
            $hosting = array_merge($hosting, $entry); //settings from subdomain override parent settings
            
            $phpmode = 'no';
            if(get_option($hosting, 'php', 'false') === 'true') {
                $mode = get_option($hosting, 'php_handler_type', 'module');
                if($mode === 'module') $phpmode = 'mod';
                else $phpmode = 'fast-cgi';
                /* TODO: what other options could be in "php_handler_type"? */
            }
            /* TODO: plesk offers some more options:
             * sys_user_id -> owner of files?
             * ip_address_id - needed?
             * fp - frontpage extensions
             * miva - ?
             * coldfusion
             * asp
             * asp_dot_net
             * traffic_bandwidth
             * max_connections
             */
            
778 779 780
            $web_folder = $entry['www_root'];
            $web_folder = preg_replace('/^\/(var|srv)\/www\/(vhosts\/)?[^\/]+\/(.*)\/httpdocs.*/', '$3', $web_folder);

781 782
            $params = array(
                            'server_id' => $server_id,
783
                            'ip_address' => '*',
784 785
                            //'ipv6_address' => '',
                            'domain' => $entry['name'] . '.' . $parent_domain['name'],
786
                            'web_folder' => $web_folder,
787
                            'type' => 'vhostsubdomain', // can be vhost or alias
mcramer's avatar
mcramer committed
788
                            'parent_domain_id' => $domain_ids[$entry['dom_id']],
789 790 791 792 793
                            'vhost_type' => 'name', // or ip (-based)
                            'hd_quota' => byte_to_mbyte(get_limit($limits, $entry['dom_id'], 'disk_space', -1)),
                            'traffic_quota' => byte_to_mbyte(get_limit($limits, $entry['dom_id'], 'max_traffic', -1)),
                            'cgi' => yes_no(get_option($hosting, 'cgi', 'false') === 'true' ? 1 : 0),
                            'ssi' => yes_no(get_option($hosting, 'ssi', 'false') === 'true' ? 1 : 0),
794
                            'suexec' => yes_no(1), // does plesk use this?!
795
                            'errordocs' => get_option($options, 'apacheErrorDocs', 'false') === 'true' ? 1 : 0,
796
                            'subdomain' => '', // plesk always uses this option
797 798
                            'ssl' => yes_no(get_option($hosting, 'ssl', 'false') === 'true' ? 1 : 0),
                            'php' => $phpmode,
799
                            'fastcgi_php_version' => '', // plesk has no different php versions
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
                            'ruby' => yes_no(0), // plesk has no ruby support
                            'python' => yes_no(get_option($hosting, 'python', 'false') === 'true' ? 1 : 0),
                            'active' => yes_no(($entry['status'] == 0 && get_option($hosting, 'maintenance_mode', 'false') !== 'true') ? 1 : 0),
                            'redirect_type' => $redir_type,
                            'redirect_path' => $redir_path,
                            'seo_redirect' => '',
                            'ssl_state' => $entry[''],
                            'ssl_locality' => $entry[''],
                            'ssl_organisation' => $entry[''],
                            'ssl_organisation_unit' => $entry[''],
                            'ssl_country' => $entry[''],
                            'ssl_domain' => $entry[''],
                            'ssl_request' => $entry[''],
                            'ssl_cert' => $entry[''],
                            'ssl_bundle' => $entry[''],
                            'ssl_action' => $entry[''],
                            'stats_password' => '',
                            'stats_type' => get_option($hosting, 'webstat', 'webalizer') === 'awstats' ? 'awstats' : 'webalizer',
                            'backup_interval' => 'none',
                            'backup_copies' => 1,
                            'allow_override' => 'All',
                            'pm_process_idle_timeout' => 10,
                            'pm_max_requests' => 0
                            );

            $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain = '" . $entry['name'] . '.' . $parent_domain['name'] . "'");
826
            if(!$old_domain) $old_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE CONCAT(subdomain, '.', domain) = '" . $entry['name'] . "'");
827 828 829 830
            if($old_domain) {
                $new_id = $old_domain['domain_id'];
                $params = array_merge($old_domain, $params);
                $msg .= "Found domain with id " . $new_id . ", updating it.<br />";
mcramer's avatar
mcramer committed
831
                $ok = $importer->sites_web_vhost_subdomain_update($session_id, $plesk_ispc_ids[$parent_domain['cl_id']], $new_id, $params);
832
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
833
            } else {
mcramer's avatar
mcramer committed
834
                $new_id = $importer->sites_web_vhost_subdomain_add($session_id, $plesk_ispc_ids[$parent_domain['cl_id']], $params, true); // read only...
835 836 837 838 839 840 841 842 843 844 845
            }
            
            $subdomain_ids[$entry['id']] = $new_id;
            $subdomain_roots[$entry['id']] = $entry['www_root'];
            $subdomain_owners[$entry['id']] = $entry['cl_id'];
            if($new_id === false) {
                //something went wrong here...
                $msg .= "Subdomain " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } else {
                $msg .= "Subdomain " . $entry['id'] . " (" . $entry['name'] . ") inserted.<br />";
846 847 848 849 850 851 852 853 854
                
                $cmd_data = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = '" . $new_id . "'");
                $path = $cmd_data['document_root'];
                add_command('chattr -i ' . escapeshellarg($path));
                add_command('if [[ -f ' . $path . '/' . $web_folder . '/index.html ]] ; then rm ' . $path . '/' . $web_folder . '/index.html ; fi');
                add_command('rsync -av --modify-window 10 --progress -e ssh root@${MYSERVER}:' . $entry['www_root'] . '/ ' . $path . '/' . $web_folder . '/');
                add_command('chown -R ' . $cmd_data['system_user'] . ':' . $cmd_data['system_group'] . ' ' . escapeshellarg($path));
                add_command('chown -R root:root ' . escapeshellarg($path . '/log') . ' ' . escapeshellarg($path . '/ssl') . ' ' . escapeshellarg($path . '/web/stats'));
                add_command('chattr +i ' . escapeshellarg($path));
855 856 857 858
            }
        }
        
        // dns have to be done AFTER domains due to missing client info
859
        /*
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
        $dns_zone_ids = array();
        $dns_zone_serials = array();
        $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");
        foreach($dns_zones as $entry) {
            $ns = $exdb->queryOneRecord("SELECT d.id, d.val FROM dns_recs as d WHERE d.dns_zone_id = '" . $entry['id'] . "' AND d.type = 'NS'");
            if(!$ns) $ns = array('id' => 0, 'val' => 'ns.' . $entry['name']);
            
            $dom_id = $dns_domain_ids[$entry['id']];
            $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
            if(!$client_id) $client_id = 0;
            
            $params = array(
                            'server_id' => $server_id,
                            'origin' => add_dot($entry['name']), // what to put here?
                            'ns' => add_dot($ns['val']), // what to put here?
                            'mbox' => str_replace('@', '.', add_dot($entry['email'])),
                            'serial' => $entry['serial'],
                            'refresh' => $entry['refresh'],
                            'retry' => $entry['retry'],
                            'expire' => $entry['expire'],
                            'minimum' => $entry['minimum'],
                            'ttl' => $entry['ttl'],
                            'xfer' => '',
                            'also_notify' => '',
                            'update_acl' => '',
                            'active' => yes_no(($entry['status'] == 0 ? 1 : 0))
                            );
            
            $old_dns = $app->db->queryOneRecord("SELECT id FROM dns_soa WHERE origin = '" . add_dot($entry['name']) . "'");
            if($old_dns) $old_id = $old_dns['id'];
            if($old_id) {
                $new_id = $old_id;
                $ok = $importer->dns_zone_update($session_id, $client_id, $old_id, $params);
893
                /if($ok === false) {
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
                //    $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") could not be updated.<br />";
                //    $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
                //} else {
                    $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") updated.<br />";
                //}
            } else {
                $new_id = $importer->dns_zone_add($session_id, $client_id, $params);
                if($new_id === false) {
                    //something went wrong here...
                    $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted.<br />";
                    $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
                } else {
                    $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") inserted.<br />";
                }
            }
            $dns_zone_ids[$entry['id']] = $new_id;
            $dns_zone_serials[$entry['id']] = $entry['serial'];
        }
        unset($dns_zones);
913
        */
914 915
        /* types:
         * PTR, NS, A, CNAME, MX, TXT, AAAA
916
         *//*
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
        $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");
        foreach($dns_records as $entry) {
            $dns_id = (array_key_exists($entry['dns_zone_id'], $dns_zone_ids) ? $dns_zone_ids[$entry['dns_zone_id']] : 0);
            if(!$dns_id) {
                // entry for missing dns zone...?
                continue;
            }
            
            $dom_id = $dns_domain_ids[$entry['dns_zone_id']];
            $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
            if(!$client_id) $client_id = 0;
            
            $params = array(
                        'server_id' => $server_id,
                        'zone' => $dns_id,
                        'name' => add_dot($entry['host']),
                        'type' => $entry['type'],
                        'data' => $entry['val'],
                        //'ttl' => '',
                        'active' => yes_no(1),
                        'stamp' => $entry['time_stamp'],
                        //'serial' => $dns_zone_serials[$entry['id']]
                        );
            
            
            $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'] . "'");
            $old_id = 0;
            if($record) {
                $old_id = $record['id'];
            }
            
            $new_id = false;
            if($entry['type'] === 'MX') {
                $params['aux'] = $entry['opt'];
                if($old_id) {
                    $ok = $importer->dns_mx_update($session_id, $client_id, $old_id, $params);
953
                    if($ok !== false) $new_id = $old_id;
954 955 956 957 958 959
                } else {
                    $new_id = $importer->dns_mx_add($session_id, $client_id, $params);
                }
            } elseif($entry['type'] === 'PTR') {
                if($old_id) {
                    $ok = $importer->dns_ptr_update($session_id, $client_id, $old_id, $params);
960
                    if($ok !== false) $new_id = $old_id;
961 962 963 964 965 966
                } else {
                    $new_id = $importer->dns_ptr_add($session_id, $client_id, $params);
                }
            } elseif($entry['type'] === 'A') {
                if($old_id) {
                    $ok = $importer->dns_a_update($session_id, $client_id, $old_id, $params);
967
                    if($ok !== false) $new_id = $old_id;
968 969 970 971 972 973
                } else {
                    $new_id = $importer->dns_a_add($session_id, $client_id, $params);
                }
            } elseif($entry['type'] === 'AAAA') {
                if($old_id) {
                    $ok = $importer->dns_aaaa_update($session_id, $client_id, $old_id, $params);
974
                    if($ok !== false) $new_id = $old_id;
975 976 977 978 979 980
                } else {
                    $new_id = $importer->dns_aaaa_add($session_id, $client_id, $params);
                }
            } elseif($entry['type'] === 'TXT') {
                if($old_id) {
                    $ok = $importer->dns_txt_update($session_id, $client_id, $old_id, $params);
981
                    if($ok !== false) $new_id = $old_id;
982 983 984 985 986 987
                } else {
                    $new_id = $importer->dns_txt_add($session_id, $client_id, $params);
                }
            } elseif($entry['type'] === 'CNAME') {
                if($old_id) {
                    $ok = $importer->dns_cname_update($session_id, $client_id, $old_id, $params);
988
                    if($ok !== false) $new_id = $old_id;
989 990 991 992 993 994
                } else {
                    $new_id = $importer->dns_cname_add($session_id, $client_id, $params);
                }
            } elseif($entry['type'] === 'NS') {
                if($old_id) {
                    $ok = $importer->dns_ns_update($session_id, $client_id, $old_id, $params);
995
                    if($ok !== false) $new_id = $old_id;
996 997 998 999 1000 1001 1002
                } else {
                    $new_id = $importer->dns_ns_add($session_id, $client_id, $params);
                }
            }
            if($new_id === false) {
                //something went wrong here...
                $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") could not be inserted/updated.<br />";
1003
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />" . var_export($params, true) . '<br />';
1004 1005 1006 1007 1008 1009
            } else {
                $msg .= "DNS " . $entry['id'] . " (" . $entry['name'] . ") inserted/updated.<br />";
            }
            
        }
        unset($dns_records);
1010
        */
1011
        
mcramer's avatar
mcramer committed
1012 1013
        $folder_ids = array();
        /* web_folder creation*/
1014
        $protected_dirs = $exdb->queryAllRecords("SELECT `id`, `non_ssl`, `ssl`, `cgi_bin`, `realm`, `path`, `dom_id` FROM protected_dirs");
1015
        foreach($protected_dirs as $entry) {
1016 1017
            if($entry['path'] == 'plesk-stat') continue;
            
mcramer's avatar
mcramer committed
1018 1019 1020 1021
            $params = array('server_id' => $server_id,
                            'parent_domain_id' => $domain_ids[$entry['dom_id']],
                            'path' => $entry['path'],
                            'active' => 'y');
1022 1023 1024
            
            $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
            
mcramer's avatar
mcramer committed
1025
            $folder_id = 0;
1026
            $check = $app->db->queryOneRecord('SELECT * FROM `web_folder` WHERE `parent_domain_id` = \'' . $domain_ids[$entry['dom_id']] . '\' AND `path` = \'' . $app->db->quote($entry['path']) . '\'');
mcramer's avatar
mcramer committed
1027
            if($check) {
1028 1029
                $ok = $importer->sites_web_folder_update($session_id, $client_id, $check['web_folder_id'], array_merge($check, $params));
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
mcramer's avatar
mcramer committed
1030
                $folder_id = $check['web_folder_id'];
1031
                $msg .= 'Updated HTTP AUTH folder (' . $folder_id . '): ' . $entry['path'] . '<br />';
mcramer's avatar
mcramer committed
1032 1033
            } else {
                $folder_id = $importer->sites_web_folder_add($session_id, $client_id, $params);
1034 1035
                $msg .= 'Created HTTP AUTH folder (' . $folder_id . '): ' . $entry['path'] . '<br />';
                if(!$folder_id) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />" . var_export($params, true) . '<br />';
mcramer's avatar
mcramer committed
1036
            }
1037
            
mcramer's avatar
mcramer committed
1038
            $folder_ids[$entry['id']] = $folder_id;
1039 1040
        }
        
1041 1042
        $pd_users = $exdb->queryAllRecords("SELECT u.id, u.login, u.account_id, u.pd_id, a.password, d.dom_id FROM pd_users as u INNER JOIN protected_dirs as d ON (d.id = u.pd_id) INNER JOIN accounts as a ON (a.id = u.account_id)");
        foreach($pd_users as $entry) {
mcramer's avatar
mcramer committed
1043
            $params = array('server_id' => $server_id,
1044
                            'web_folder_id' => $folder_ids[$entry['pd_id']],
mcramer's avatar
mcramer committed
1045 1046 1047
                            'username' => $entry['login'],
                            'password' => $entry['password'],
                            'active' => 'y');
1048 1049 1050 1051 1052
            if($entry['login'] == '' || !isset($folder_ids[$entry['pd_id']])) {
                $msg .= 'Skipping Folder user because of missing data.<br />';
                continue;
            }
            $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
1053
            
1054
            $check = $app->db->queryOneRecord('SELECT * FROM `web_folder_user` WHERE `web_folder_id` = ' . intval($folder_ids[$entry['pd_id']]) . ' AND `username` = \'' . $entry['login'] . '\'');
mcramer's avatar
mcramer committed
1055
            if($check) {
1056 1057 1058
                $ok = $importer->sites_web_folder_user_update($session_id, $client_id, $check['web_folder_user_id'], array_merge($check, $params));
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
                $msg .= 'Updated HTTP AUTH folder user  (' . $fu_id . '): ' . $entry['login'] . '<br />';
mcramer's avatar
mcramer committed
1059
            } else {
1060 1061 1062
                $fu_id = $importer->sites_web_folder_user_add($session_id, $client_id, $params);
                $msg .= 'Created HTTP AUTH folder user  (' . $fu_id . '): ' . $entry['login'] . '<br />';
                if(!$fu_id) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />" . var_export($params, true) . '<br />';
mcramer's avatar
mcramer committed
1063
            }
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
        }
        
        /*$web_users = $exdb->queryAllRecords("SELECT id, dom_id, sys_user_id, ssi, php, cgi, perl, python, fastcgi, asp, asp_dot_net FROM web_users");
        foreach($web_users as $entry) {
            $params = 
        }
        */
        
        
        $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)");
        $ftp_users = array_merge($ftp_users, $dom_ftp_users);
        foreach($ftp_users as $entry) {
            $parent_domain = $exdb->queryOneRecord("SELECT d.id, d.cl_id, d.name FROM domains as d WHERE d.id = '" . $entry['dom_id'] . "'");
1077
            if(!$entry['id']) continue;
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
            $ispc_dom_id = $domain_ids[$entry['dom_id']];
            $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
            if(!$client_id) $client_id = 0;
            
            $document_root = str_replace("[website_id]",$ispc_dom_id,$web_config["website_path"]);
            $document_root = str_replace("[website_idhash_1]",id_hash($ispc_dom_id,1),$document_root);
            $document_root = str_replace("[website_idhash_2]",id_hash($ispc_dom_id,1),$document_root);
            $document_root = str_replace("[website_idhash_3]",id_hash($ispc_dom_id,1),$document_root);
            $document_root = str_replace("[website_idhash_4]",id_hash($ispc_dom_id,1),$document_root);

            // Set the values for document_root, system_user and system_group
            $system_user = 'web'.$ispc_dom_id;
            $system_group = 'client'.$client_id;
            $document_root = str_replace("[client_id]",$client_id,$document_root);
            $document_root = str_replace("[client_idhash_1]",id_hash($client_id,1),$document_root);
            $document_root = str_replace("[client_idhash_2]",id_hash($client_id,2),$document_root);
            $document_root = str_replace("[client_idhash_3]",id_hash($client_id,3),$document_root);
            $document_root = str_replace("[client_idhash_4]",id_hash($client_id,4),$document_root);
            
            $uid = $system_user;
            $gid = $system_group;
            
            $sys_grp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = '" . $client_id . "'");
            if(!$sys_grp) $sys_grp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = 0");
            
            if(!$sys_grp) $sys_groupid = 1;
            else $sys_groupid = $sys_grp['groupid'];
            
            $params = array(
                            'server_id' => $server_id,
                            'parent_domain_id' => $domain_ids[$entry['dom_id']],
                            'username' => $entry['login'],
                            'password' => $entry['password'],
                            'quota_size' => byte_to_mbyte(($entry['quota'] == 0 ? -1 : $entry['quota'])),
                            'active' => yes_no(1),
                            'uid' => $uid,
                            'gid' => $gid,
1115
                            'dir' => $document_root . (substr($document_root, -1) !== '/' ? '/' : ''),
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
                            'sys_groupid' => $sys_groupid
                            //'quota_files' => $entry[''],
                            //'ul_ratio' => $entry[''],
                            //'dl_ratio' => $entry[''],
                            //'ul_bandwidth' => $entry[''],
                            //'dl_bandwidth' => $entry['']
                            );
            $new_id = false;
            $old_ftp = $app->db->queryOneRecord("SELECT ftp_user_id, parent_domain_id FROM ftp_user WHERE username = '" . $entry['login'] ."'");
            if($old_ftp) {
                if($old_ftp['parent_domain_id'] != $domain_ids[$entry['dom_id']]) {
                    $msg .= "FTP Account conflicts with other domain!<br />";
                } else {
                    $new_id = $old_ftp['ftp_user_id'];
1130
                    $ok = $importer->sites_ftp_user_update($session_id, $client_id, $new_id, array_merge($old_ftp, $params));
1131
                    if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
                }
            } else {
                $new_id = $importer->sites_ftp_user_add($session_id, $client_id, $params);
            }
            if($new_id === false) {
                //something went wrong here...
                $msg .= "FTP " . $entry['id'] . " (" . $entry['login'] . ") could not be inserted.<br />";
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
                $msg .= "Params: " . var_export($params, true) . "<br />";
            } else {
                $msg .= "FTP Account " . $entry['id'] . " (" . $entry['login'] . ") inserted.<br />";
            }
        }
        
        $mail_config = $app->getconf->get_server_config($server_id, 'mail');
        
        $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) ");
        $mail_ids = array();
        foreach($mail_addresses as $entry) {
            
            $parent_domain = $exdb->queryOneRecord("SELECT d.id, d.cl_id, d.name FROM domains as d WHERE d.id = '" . $entry['dom_id'] . "'");
            if(!$parent_domain) {
                $msg .= "Could not insert/update mail address " . $entry['mail_name'] . " as domain is missing.<br />";
                continue;
            }
            
            /* postbox true/false
             * mail_group true/false
             * spamfilter true/false
             */
            
            
            $has_responder = false;
            if($entry['autoresponder'] === 'true') {
                $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'] . "'");
                if($responder) $has_responder = true;
            }
            
            $maildir = str_replace("[domain]",$parent_domain["name"],$mail_config["maildir_path"]);
			$maildir = str_replace("[localpart]",strtolower($entry["mail_name"]),$maildir);
            
            
            $params = array(
                            'server_id' => $server_id,
                            'email' => $entry['mail_name'] . "@" . $parent_domain['name'],
                            'login' => strtolower($entry['mail_name'] . "@" . $parent_domain['name']),
                            'password' => $entry['password'],
                            'name' => $entry[''],
mcramer's avatar
mcramer committed
1180
                            'quota' => ($entry['mbox_quota'] == -1 ? 0 : $entry['mbox_quota']), // in bytes!
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
                            'cc' => $entry['redir_addr'],
                            'maildir' => $maildir,
                            'homedir' => $mail_config["homedir_path"],
                            'uid' => $mail_config["mailuser_uid"],
                            'gid' => $mail_config["mailuser_gid"],
                            'postfix' => yes_no(1),
                            'disableimap' => yes_no(0),
                            'disablepop3' => yes_no(0),
                            'autoresponder_subject' => ($has_responder ? $responder['subject'] : ''),
                            'autoresponder_text' => ($has_responder ? $responder['text'] : ''),
                            'autoresponder' => yes_no($has_responder ? 1 : 0),
                            'autoresponder_start_date' => ($has_responder && $responder['resp_on'] === 'true' ? strftime('%Y-%m-%d', time()) : strftime('%Y-%m-%d', time() - (3600*24))),
                            'autoresponder_end_date' => ($has_responder && $responder['resp_on'] === 'true' ? strftime('%Y-%m-%d', time() + (3600*24*365)) : strftime('%Y-%m-%d', time())),
                            'move_junk' => yes_no(0)
                            );
            $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
            
            // if this is no postbox we do not need to create a mailuser
            if($entry['postbox'] !== 'false') {
                $old_mail = $app->db->queryOneRecord("SELECT mailuser_id FROM mail_user WHERE email = '" . $entry['mail_name'] . "@" . $parent_domain['name'] . "'");
                if($old_mail) {
                    $new_id = $old_mail['mailuser_id'];
1203
                    $ok = $importer->mail_user_update($session_id, $client_id, $new_id, array_merge($old_mail, $params));
1204
                    if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
                } else {
                    $new_id = $importer->mail_user_add($session_id, $client_id, $params);
                }

                if($new_id === false) {
                    //something went wrong here...
                    $msg .= "Mail" . $entry['id'] . " (" . $entry['mail_name'] . "@" . $parent_domain['name'] . ") could not be inserted/updated.<br />";
                    $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
                } else {
                    $msg .= "Mail " . $entry['id'] . " (" . $entry['mail_name'] . "@" . $parent_domain['name'] . ") inserted/updated.<br />";
1215 1216 1217 1218 1219 1220 1221
                    
                    add_command('rsync -av --delete-after --modify-window 10 --progress -e ssh root@${MYSERVER}:/var/qmail/mailnames/' . $parent_domain['name'] . '/' . strtolower($entry['mail_name']) . '/Maildir/ ' . $maildir . '/Maildir/');
                    add_command('chown -R vmail:vmail ' . $maildir);
                    add_command('chmod 744 ' . $maildir . '/Maildir/subscriptions');
                    add_command('chmod 600 ' . $maildir . '/Maildir/dovecot-*');
                    add_command('chmod 700 ' . $maildir . '/Maildir/cur ' . $maildir . '/Maildir/new ' . $maildir . '/Maildir/tmp');
                    add_command('chmod 600 ' . $maildir . '/Maildir/cur/* ' . $maildir . '/Maildir/new/* ' . $maildir . '/Maildir/tmp/*');
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
                }
                $mail_ids[$entry['id']] = $new_id;
            }
            
            // select all redirs for this address
            $mail_redir = $exdb->queryAllRecords("SELECT id, mn_id, address FROM mail_redir WHERE mn_id = '" . $entry['id'] . "'");
            foreach($mail_redir as $redir) {
                $params = array(
                                'server_id' => $server_id,
                                'source' => $entry['mail_name'] . "@" . $parent_domain['name'],
                                'destination' => $redir['address'],
                                'type' => 'forward', // or forward
                                'active' => yes_no(1)
                                );
                
                $old_mail = $app->db->queryOneRecord("SELECT forwarding_id FROM mail_forwarding WHERE source = '" . $entry['mail_name'] . "@" . $parent_domain['name'] . "' AND destination = '" . $redir['address'] . "'");
                if($old_mail) {
                    $new_id = $old_mail['forwarding_id'];
1240
                    $ok = $importer->mail_forward_update($session_id, $client_id, $new_id, array_merge($old_mail, $params));
1241
                    if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
                } else {
                    $new_id = $importer->mail_forward_add($session_id, $client_id, $params);
                }

                if($new_id === false) {
                    //something went wrong here...
                    $msg .= "Mail redirect " . $entry['id'] . " (" . $entry['mail_name'] . "@" . $parent_domain['name'] . " to " . $redir['address'] . ") could not be inserted/updated.<br />";
                    $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
                } else {
                    $msg .= "Mail redirect " . $entry['id'] . " (" . $entry['mail_name'] . "@" . $parent_domain['name'] . " to " . $redir['address'] . ") inserted/updated.<br />";
                }
            }
            unset($mail_redir);
        }
        unset($mail_addresses);
        
        $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)");
        foreach($mail_aliases as $entry) {
            
            $parent_domain = $exdb->queryOneRecord("SELECT d.id, d.cl_id, d.name FROM domains as d WHERE d.id = '" . $entry['dom_id'] . "'");
            if(!$parent_domain) {
                $msg .= "Could not insert/update mail alias " . $entry['alias'] . " as domain is missing.<br />";
                continue;
            }
            
            $params = array(
                            'server_id' => $server_id,
                            'source' => $entry['alias'] . "@" . $parent_domain['name'],
                            'destination' => $entry['mail_name'] . "@" . $parent_domain['name'],
                            'type' => 'alias', // or forward
                            'active' => yes_no(1)
                            );
            $client_id = $plesk_ispc_ids[$domain_owners[$entry['dom_id']]];
        
            $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'] . "'");
            if($old_mail) {
                $new_id = $old_mail['forwarding_id'];
1279
                $ok = $importer->mail_alias_update($session_id, $client_id, $new_id, array_merge($old_mail, $params));
1280
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
            } else {
                $new_id = $importer->mail_alias_add($session_id, $client_id, $params);
            }
            
            if($new_id === false) {
                //something went wrong here...
                $msg .= "Mail alias " . $entry['id'] . " (" . $entry['alias'] . "@" . $parent_domain['name'] . ") could not be inserted/updated.<br />";
                $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } else {
                $msg .= "Mail alias " . $entry['id'] . " (" . $entry['alias'] . "@" . $parent_domain['name'] . ") inserted/updated.<br />";
            }
        }
        unset($mail_aliases);
        
        //spamfilter // preferences = true/false, username = email address, can be *@*
        //id, username, preferences
        
        //spamfilter_preferences
        //prefid, spamfilter_id, preference, value
        
        
        
        //$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");
        
mcramer's avatar
mcramer committed
1305 1306
        $db_userids = array();
        
1307
        $db_users  = $exdb->queryAllRecords("SELECT u.id, u.login, u.account_id, u.db_id, a.password, a.type as `pwtype`, d.dom_id FROM db_users as u INNER JOIN data_bases as d ON (d.id = u.db_id) LEFT JOIN accounts as a ON (a.id = u.account_id)");
mcramer's avatar
mcramer committed
1308 1309 1310 1311 1312
        foreach($db_users as $db_user) {
            // database user
            $params = array('server_id' => $server_id,
                            'database_user' => $db_user['login'],
                            'database_password' => $db_user['password']);
1313 1314 1315
            
            $client_id = $plesk_ispc_ids[$domain_owners[$db_user['dom_id']]];

mcramer's avatar
mcramer committed
1316 1317 1318
            $check = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user` = \'' . $app->db->quote($db_user['login']) . '\'');
            $db_user_id = 0;
            if($check) {
1319 1320
                $ok = $importer->sites_database_user_update($session_id, $client_id, $check['database_user_id'], array_merge($check, $params));
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
mcramer's avatar
mcramer committed
1321 1322
                $db_user_id = $check['database_user_id'];
            } else {
1323
                $db_user_id = $importer->sites_database_user_add($session_id, $client_id, $params);
mcramer's avatar
mcramer committed
1324 1325 1326
            }
            
            if(!isset($db_userids[$db_user['db_id']])) $db_userids[$db_user['db_id']] = $db_user_id;
1327
            $msg .= 'Created / updated database user: ' . $db_user['login'] . '<br />';
mcramer's avatar
mcramer committed
1328
        }
1329 1330
         
        add_command('# DATABASES');
mcramer's avatar
mcramer committed
1331