Skip to content
import_plesk.php 73.2 KiB
Newer Older
<?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;
}

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

$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
Loading full blame...