Skip to content
import_plesk.php 73.2 KiB
Newer Older
mcramer's avatar
mcramer committed
            }
        }
        
        /*$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'] . "'");
            
            $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,
                            'dir' => $document_root . (substr($document_root, -1) !== '/' ? '/' : ''),
                            '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'];
                    $ok = $importer->sites_ftp_user_update($session_id, $client_id, $new_id, $params);
                    if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
                }
            } 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
                            'quota' => ($entry['mbox_quota'] == -1 ? 0 : $entry['mbox_quota']), // in bytes!
                            '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'];
                    $ok = $importer->mail_user_update($session_id, $client_id, $new_id, $params);
                    if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
                } 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 />";
                }
                $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'];
                    $ok = $importer->mail_forward_update($session_id, $client_id, $new_id, $params);
                    if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
                } 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'];
                $ok = $importer->mail_alias_update($session_id, $client_id, $new_id, $params);
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
            } 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
        $db_userids = array();
        
        $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)");
mcramer's avatar
mcramer committed
        foreach($db_users as $db_user) {
            // database user
            $params = array('server_id' => $server_id,
                            'database_user' => $db_user['login'],
                            'database_password' => $db_user['password']);
            $check = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user` = \'' . $app->db->quote($db_user['login']) . '\'');
            $db_user_id = 0;
            if($check) {
                $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
                $db_user_id = $check['database_user_id'];
            } else {
                $db_user_id = $importer->sites_database_user_add($session_id, $client_id, $params);
mcramer's avatar
mcramer committed
            }
            
            if(!isset($db_userids[$db_user['db_id']])) $db_userids[$db_user['db_id']] = $db_user_id;
            $msg .= 'Created / updated database user: ' . $db_user['login'] . '<br />';
        $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");
mcramer's avatar
mcramer committed
        foreach($databases as $database) {
            $params = array('server_id' => $server_id,
                            'parent_domain_id' => $domain_ids[$database['dom_id']],
                            'type' => 'mysql',
                            'database_name' => $database['name'],
                            'database_user_id' => $db_userids[$database['id']],
                            'database_ro_user_id' => 0,
                            'database_charset' => 'utf8',
                            'remote_access' => 'n',
                            'active' => 'y',
                            'remote_ips' => '');
            
            $check = $app->db->queryOneRecord('SELECT * FROM `web_database` WHERE `database_name` = \'' . $app->db->quote($database['name']) . '\'');
            if($check) {
                $ok = $importer->sites_database_update($session_id, $client_id, $check['database_id'], array_merge($check, $params));
                if($ok === false) $msg .= "&nbsp; Error: " . $importer->getFault() . "<br />";
mcramer's avatar
mcramer committed
            } else {
                $importer->sites_database_add($session_id, $client_id, $params);
            }
            
            $msg .= 'Created / updated database: ' . $database['name'] . '<br />';
mcramer's avatar
mcramer committed
         }
        
        // do we need table disk_usage for import? i think we don't
        
        // name is domain name, displayName is including "Umlaute"
        //$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");
        
        
        //DomainServices
        //id, dom_id, type, status, parameters_id, ipCollectionId
        
        //DomainsTraffic
        //dom_id, date, http_in, http_out, ftp_in, ftp_out, smtp_in, smtp_out, pop3_imap_in, pop3_imap_out
        
        
        //IP_Adresses
        //id, ip_address, mask, iface, ssl_certificate_id, default_domain_id, ftps, main, status
        
        //ip_pool
        //id, ip_address_id, type

        /* TODO:
                */
        //misc // needed? global settings
        //param, val
        
        //Permissions
        //id, permission, value
        
        //smb_users // pass is base64 encoded plaintext
        //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
        
        /* TODO: 
        sys_users // mapped_to = parent_id
        id, login, account_id, home, shell, quota, mapped_to
        
         */
	} else {
        $msg .= 'Connecting to external database failed!<br />';
        $msg .= $exdb->connect_error;
        $msg .= substr($exdb->errorMessage, 0, 25);
	
		$error .= $exdb->errorMessage;
	}
	
	//* restore db login details
	/*$conf['db_host'] = $conf_bak['db_host'];
	$conf['db_database'] = $conf_bak['db_database'];
	$conf['db_user'] = $conf_bak['db_user'];
	$conf['db_password'] = $conf_bak['db_password'];*/
	
}

$app->tpl->setVar('msg',$msg);
$app->tpl->setVar('error',$error);


$app->tpl_defaults();
$app->tpl->pparse();


?>