예제 #1
0
function convert_disc_client()
{
    set_time_limit(3600);
    ini_set('default_socket_timeout', 360);
    global $sugar_config;
    require_once 'vendor/nusoap//nusoap.php';
    $errors = array();
    $server_url = "http://";
    $user_name = "";
    $admin_name = "";
    $password = "";
    $oc_install = false;
    if (empty($sugar_config['unique_key'])) {
        $sugar_config['unique_key'] = create_guid();
    }
    if (isset($_SESSION['oc_install']) && $_SESSION['oc_install'] == true) {
        $oc_install = true;
    }
    // run options are: convert, sync
    // default behavior of this page
    $run = "convert";
    if (!$oc_install) {
        if (isset($_REQUEST['run'])) {
            $run = $_REQUEST['run'];
        }
        if ($run == "convert") {
            if (isset($_REQUEST['server_url'])) {
                $server_url = $_REQUEST['server_url'];
                if ($server_url == "") {
                    $errors[] = "Server URL cannot be empty.";
                }
            }
        } else {
            if ($run == "sync") {
                $server_url = $sugar_config['sync_site_url'];
            }
        }
        if (isset($_REQUEST['user_name'])) {
            $user_name = $_REQUEST['user_name'];
            if ($user_name == "") {
                $errors[] = "User Name cannot be empty.";
            }
        }
        if (isset($_REQUEST['password'])) {
            if ($_REQUEST['password'] == "") {
                $errors[] = "Password cannot be empty.";
            } else {
                $password = $_REQUEST['password'];
            }
        }
    } else {
        //this is an offline client install
        if (isset($_SESSION['oc_run'])) {
            $run = $_SESSION['oc_run'];
        }
        if ($run == "convert") {
            if (isset($_SESSION['oc_server_url'])) {
                $server_url = $_SESSION['oc_server_url'];
                if ($server_url == "") {
                    $errors[] = "Server URL cannot be empty.";
                }
            }
        } else {
            if ($run == "sync") {
                $server_url = $sugar_config['sync_site_url'];
            }
        }
        if (isset($_SESSION['oc_username'])) {
            $user_name = $_SESSION['oc_username'];
            if ($user_name == "") {
                $errors[] = "User Name cannot be empty.";
            }
        }
        if (isset($_SESSION['oc_password'])) {
            if ($_SESSION['oc_password'] == "") {
                $errors[] = "Password cannot be empty.";
            } else {
                $password = $_SESSION['oc_password'];
            }
        }
    }
    //end check for offline client install
    if (!isset($_SESSION['is_oc_conversion']) || $_SESSION['is_oc_conversion'] == false) {
        $password = md5($password);
    }
    $sugar_config['oc_username'] = $user_name;
    $sugar_config['oc_password'] = $password;
    $sugar_config['oc_converted'] = false;
    $sugar_config['disc_client'] = true;
    if (isset($_SESSION['install_method'])) {
        $sugar_config['install_method'] = $_SESSION['install_method'];
    }
    if ((isset($_REQUEST['submitted']) || $oc_install) && sizeof($errors) == 0) {
        if (empty($server_url) || $server_url == 'http://') {
            $errors[] = "Server URL is required";
        } else {
            $sugar_config['sync_site_url'] = $server_url;
            $soapclient = new nusoapclient("{$server_url}/soap.php");
            $soapclient->response_timeout = 360;
            if ($soapclient->call('is_loopback', array())) {
                $errors[] = "Server and Client must be on separate machines with unique ip addresses";
            }
            $result = $soapclient->call('get_sugar_flavor', array());
            global $sugar_flavor, $sugar_version;
            if ($result != $sugar_flavor) {
                $errors[] = "Server and Client must both be running the same flavor of Sugar.";
            }
            if (!$soapclient->call('offline_client_available', array())) {
                $errors[] = "No licenses available for offline client";
            }
            $result = $soapclient->call('login', array('user_auth' => array('user_name' => $user_name, 'password' => $password, 'version' => '.01'), 'application_name' => 'Disconnected Client Setup'));
            if ($soapclient->error_str) {
                $errors[] = "Login failed with error: " . $soapclient->response;
            }
            if ($result['error']['number'] != 0) {
                $errors[] = "Login failed with error: " . $result['error']['name'] . ' ' . $result['error']['description'];
            }
            $session = $result['id'];
        }
        $errorString = "";
        if (!empty($errors)) {
            foreach ($errors as $error) {
                $errorString .= $error . "<br>";
            }
        }
        if ($session && empty($errors)) {
            if ($run == "convert") {
                // register this client/user with server
                // update local config.php file
                $install_method = 'web';
                if (isset($sugar_config['install_method'])) {
                    $install_method = $sugar_config['install_method'];
                }
                //attempt to obtain the system_id from the server
                //php_uname('n') will only work on a windows system
                $machine_name = php_uname('n');
                $soapclient->setHeaders('sugar_version=' . $sugar_version);
                $result = $soapclient->call('get_unique_system_id', array('session' => $session, 'unique_key' => $sugar_config['unique_key'], 'system_name' => $machine_name, 'install_method' => $install_method));
                if ($soapclient->error_str) {
                    $errors[] = "Unable to obtain unique system id from server: " . $soapclient->error_str;
                } else {
                    if ($result['error']['number'] != 0) {
                        $errors[] = $result['error']['description'];
                    } else {
                        $admin = BeanFactory::getBean('Administration');
                        $system_id = $result['id'];
                        if (!isset($system_id)) {
                            $system_id = 1;
                        }
                        $admin->saveSetting('system', 'system_id', $system_id);
                    }
                }
            }
            // data sync triggers
            if (empty($errors)) {
                require_once "modules/Sync/SyncHelper.php";
                sync_users($soapclient, $session, true, true);
                $sugar_config['oc_converted'] = true;
                echo 'Updating Local Information<br>';
                //echo 'Done - will auto logout in <div id="seconds_left">10</div> seconds<script> function logout_countdown(left){document.getElementById("seconds_left").innerHTML = left; if(left == 0){document.location.href = "index.php?module=Users&action=Logout";}else{left--; setTimeout("logout_countdown("+ left+")", 1000)}};setTimeout("logout_countdown(10)", 1000)</script>';
                // done with soap calls
                $result = $soapclient->call('logout', array('session' => $session));
                ksort($sugar_config);
                if (!write_array_to_file("sugar_config", $sugar_config, "config.php")) {
                    return;
                }
            }
        }
    }
    $errorString = "";
    foreach ($errors as $error) {
        $errorString .= $error . "<br>";
    }
    return $errorString;
}