Beispiel #1
0
 public function testisValidDBName()
 {
     //valid value
     $expected = true;
     $actual = isValidDBName('suitecrmtest', 'mysql');
     $this->assertSame($expected, $actual);
     //invalid value
     $expected = false;
     $actual = isValidDBName('suite/crm.test', 'mysql');
     $this->assertSame($expected, $actual);
 }
 public function testInvalidMSSQLNameReturnsFalse()
 {
     $this->assertFalse(isValidDBName('622sugarCRM', 'mssql'));
     $this->assertFalse(isValidDBName('sugar crm', 'mssql'));
     $this->assertFalse(isValidDBName('#sugarCRM_ver6', 'mssql'));
 }
function checkDBSettings($silent = false)
{
    installLog("Begin DB Check Process *************");
    if (function_exists('mysqli_connect')) {
        $_SESSION['mysql_type'] = 'mysqli';
    }
    if (function_exists('sqlsrv_connect')) {
        $_SESSION['mssql_type'] = 'sqlsrv';
    }
    global $mod_strings;
    $errors = array();
    copyInputsIntoSession();
    installLog("Checking DB Name is valid.");
    $_SESSION['setup_db_database_name'] = trim($_SESSION['setup_db_database_name']);
    if ($_SESSION['setup_db_database_name'] == '') {
        $errors['ERR_DB_NAME'] = $mod_strings['ERR_DB_NAME'];
        installLog("ERROR::  {$errors['ERR_DB_NAME']}");
    }
    if (!isValidDBName($_SESSION['setup_db_database_name'], $_SESSION['setup_db_type'])) {
        $errIdx = 'ERR_DB_' . strtoupper($_SESSION['setup_db_type']) . '_DB_NAME_INVALID';
        $errors[$errIdx] = $mod_strings[$errIdx];
        installLog("ERROR::  {$errors[$errIdx]}");
    }
    if ($_SESSION['setup_db_type'] != 'oci8') {
        if (trim($_SESSION['setup_db_host_name']) == '') {
            $errors['ERR_DB_HOSTNAME'] = $mod_strings['ERR_DB_HOSTNAME'];
            installLog("ERROR::  {$errors['ERR_DB_HOSTNAME']}");
        }
    }
    //check to see that password and retype are same, if needed
    if (isset($_SESSION['dbUSRData']) && !empty($_SESSION['dbUSRData']) && ($_SESSION['dbUSRData'] == 'create' || $_SESSION['dbUSRData'] == 'provide')) {
        if ($_SESSION['setup_db_sugarsales_password'] != $_SESSION['setup_db_sugarsales_password_retype']) {
            $errors['ERR_DBCONF_PASSWORD_MISMATCH'] = $mod_strings['ERR_DBCONF_PASSWORD_MISMATCH'];
            installLog("ERROR::  {$errors['ERR_DBCONF_PASSWORD_MISMATCH']}");
        }
    }
    // bail if the basic info isn't valid
    if (count($errors) > 0) {
        installLog("Basic form info is INVALID, exit Process.");
        return printErrors($errors);
    } else {
        installLog("Basic form info is valid, continuing Process.");
    }
    // test the account that will talk to the db if we're not creating it
    if ($_SESSION['setup_db_sugarsales_user'] != '' && !$_SESSION['setup_db_create_sugarsales_user']) {
        if ($_SESSION['setup_db_type'] == 'mysql') {
            installLog("testing with mysql");
            if (isset($_SESSION['mysql_type']) && $_SESSION['mysql_type'] == 'mysqli') {
                installLog("MySQLI library detected");
            }
            if (isset($_SESSION['mysql_type'])) {
                $host_name = getHostPortFromString($_SESSION['setup_db_host_name']);
                if (empty($host_name)) {
                    $link = @mysqli_connect($_SESSION['setup_db_host_name'], $_SESSION['setup_db_sugarsales_user'], $_SESSION['setup_db_sugarsales_password']);
                } else {
                    $link = @mysqli_connect($host_name[0], $_SESSION['setup_db_sugarsales_user'], $_SESSION['setup_db_sugarsales_password'], null, $host_name[1]);
                }
            } else {
                $link = @mysql_connect($_SESSION['setup_db_host_name'], $_SESSION['setup_db_sugarsales_user'], $_SESSION['setup_db_sugarsales_password']);
            }
            if (!$link) {
                installLog("Could not make Connection using  host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_sugarsales_user']}");
                if (isset($_SESSION['mysql_type'])) {
                    $errno = mysqli_connect_errno();
                    $error = mysqli_connect_error();
                } else {
                    $errno = mysql_errno();
                    $error = mysql_error();
                }
                $errors['ERR_DB_LOGIN_FAILURE'] = $mod_strings['ERR_DB_LOGIN_FAILURE_MYSQL'];
                installLog("ERROR::  {$errors['ERR_DB_LOGIN_FAILURE']}");
            } else {
                installLog("Connection made using  host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_sugarsales_user']}");
                if (isset($_SESSION['mysql_type'])) {
                    mysqli_close($link);
                } else {
                    mysql_close($link);
                }
            }
        } elseif ($_SESSION['setup_db_type'] == 'mssql') {
            installLog("testing with mssql");
            $connect_host = "";
            $_SESSION['setup_db_host_instance'] = trim($_SESSION['setup_db_host_instance']);
            if (empty($_SESSION['setup_db_host_instance'])) {
                $connect_host = $_SESSION['setup_db_host_name'];
            } else {
                $connect_host = $_SESSION['setup_db_host_name'] . "\\" . $_SESSION['setup_db_host_instance'];
            }
            if (isset($_SESSION['mssql_type'])) {
                $connect_params = array("UID" => $_SESSION['setup_db_sugarsales_user'], "PWD" => $_SESSION['setup_db_sugarsales_password'], "MultipleActiveResultSets" => false);
                $link = sqlsrv_connect($connect_host, $connect_params);
            } else {
                $link = @mssql_connect($connect_host, $_SESSION['setup_db_sugarsales_user'], $_SESSION['setup_db_sugarsales_password']);
            }
            if (!$link) {
                $errors['ERR_DB_LOGIN_FAILURE'] = $mod_strings['ERR_DB_LOGIN_FAILURE_MSSQL'];
                installLog("ERROR::  {$errors['ERR_DB_LOGIN_FAILURE']}");
            } else {
                installLog("Connection made using  host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_sugarsales_user']}");
                if (isset($_SESSION['mssql_type'])) {
                    sqlsrv_close($link);
                } else {
                    mssql_close($link);
                }
            }
            // Bug 29855 - Check to see if given db name is valid
            //                if (preg_match("/^[0-9#@]+|[\"\'\*\/\\?\:\\<\>\-\ \&\!\(\)\[\]\{\}\;\,\.\`\~\|\\\\]+/i", $_SESSION['setup_db_database_name']) ) {
            //                    $errors['ERR_DB_MSSQL_DB_NAME'] = $mod_strings['ERR_DB_MSSQL_DB_NAME_INVALID'];
            //                    installLog("ERROR::  {$errors['ERR_DB_MSSQL_DB_NAME']}");
            //                }
            //                Moved to isValidDBName function in db_utils.php
        } elseif ($_SESSION['setup_db_type'] == 'oci8') {
        }
    }
    // privileged account tests
    if ($_SESSION['setup_db_admin_user_name'] == '') {
        $errors['ERR_DB_PRIV_USER'] = $mod_strings['ERR_DB_PRIV_USER'];
        installLog("ERROR:: {$errors['ERR_DB_PRIV_USER']}");
    } else {
        installLog("Testing priviliged account...");
        if ($_SESSION['setup_db_type'] == 'mysql') {
            if (isset($_SESSION['mysql_type'])) {
                $host_name = getHostPortFromString($_SESSION['setup_db_host_name']);
                if (empty($host_name)) {
                    $link = @mysqli_connect($_SESSION['setup_db_host_name'], $_SESSION['setup_db_admin_user_name'], $_SESSION['setup_db_admin_password']);
                } else {
                    $link = @mysqli_connect($host_name[0], $_SESSION['setup_db_admin_user_name'], $_SESSION['setup_db_admin_password'], null, $host_name[1]);
                }
            } else {
                $link = @mysql_connect($_SESSION['setup_db_host_name'], $_SESSION['setup_db_admin_user_name'], $_SESSION['setup_db_admin_password']);
            }
            if ($link) {
                installLog("Connection made for Privileged admin account using  host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_admin_user_name']}");
                // database admin credentials are valid--can continue check on stuff
                if (isset($_SESSION['mysql_type'])) {
                    $db_selected = @mysqli_select_db($link, $_SESSION['setup_db_database_name']);
                } else {
                    $db_selected = @mysql_select_db($_SESSION['setup_db_database_name'], $link);
                }
                if ($silent == false && $db_selected && $_SESSION['setup_db_create_database'] && (!isset($_SESSION['setup_db_drop_tables']) || !$_SESSION['setup_db_drop_tables'])) {
                    $errStr = $mod_strings['ERR_DB_EXISTS_PROCEED'];
                    $errors['ERR_DB_EXISTS_PROCEED'] = $errStr;
                    installLog("ERROR:: {$errors['ERR_DB_EXISTS_PROCEED']}");
                } else {
                    if (!$db_selected && !$_SESSION['setup_db_create_database']) {
                        $errors['ERR_DB_EXISTS_NOT'] = $mod_strings['ERR_DB_EXISTS_NOT'];
                        installLog("ERROR:: {$errors['ERR_DB_EXISTS_NOT']}");
                    }
                }
                // test for upgrade and inform user about the upgrade wizard
                if ($db_selected) {
                    installLog("DB Selected, will reuse {$_SESSION['setup_db_database_name']}");
                    if (isset($_SESSION['mysql_type'])) {
                        $config_query = "SHOW TABLES LIKE 'config'";
                        $config_result = mysqli_query($link, $config_query);
                        $config_table_exists = mysqli_num_rows($config_result) == 1;
                        mysqli_free_result($config_result);
                        include 'sugar_version.php';
                        if (!$_SESSION['setup_db_drop_tables'] && $config_table_exists) {
                            $query = "SELECT COUNT(*) FROM config WHERE category='info' AND name='sugar_version' AND VALUE LIKE '{$sugar_db_version}'";
                            $result = mysqli_query($link, $query);
                            $row = mysqli_fetch_row($result);
                            if ($row[0] != 1 && $silent == false) {
                                $errors['ERR_DB_EXISTS_WITH_CONFIG'] = $mod_strings['ERR_DB_EXISTS_WITH_CONFIG'];
                                installLog("ERROR:: {$errors['ERR_DB_EXISTS_WITH_CONFIG']}");
                            }
                            mysqli_free_result($result);
                        }
                    } else {
                        $config_query = "SHOW TABLES LIKE 'config'";
                        $config_result = mysql_query($config_query, $link);
                        $config_table_exists = mysql_num_rows($config_result) == 1;
                        mysql_free_result($config_result);
                        include 'sugar_version.php';
                        if (!$_SESSION['setup_db_drop_tables'] && $config_table_exists) {
                            $query = "SELECT COUNT(*) FROM config WHERE category='info' AND name='sugar_version' AND VALUE LIKE '{$sugar_db_version}'";
                            $result = mysql_query($query, $link);
                            $row = mysql_fetch_row($result);
                            if ($row[0] != 1 && $silent == false) {
                                $errors['ERR_DB_EXISTS_WITH_CONFIG'] = $mod_strings['ERR_DB_EXISTS_WITH_CONFIG'];
                                installLog("ERROR:: {$errors['ERR_DB_EXISTS_WITH_CONFIG']}");
                            }
                            mysql_free_result($result);
                        }
                    }
                } else {
                    installLog("DB not selected, will create {$_SESSION['setup_db_database_name']}");
                }
                // check for existing SugarCRM database user if create flag is set,
                //user name has been given, and database has been selected (reusing db, not creating new one)
                if ($_SESSION['setup_db_create_sugarsales_user'] && $_SESSION['setup_db_sugarsales_user'] != '' && $db_selected) {
                    if (isset($_SESSION['mysql_type'])) {
                        $mysqli_db_selected = mysqli_select_db($link, 'mysql');
                        $user = $_SESSION['setup_db_sugarsales_user'];
                        $query = "select count(*) from user where User ='******'";
                        $result = mysqli_query($link, $query);
                        if (!$result) {
                            $errno = mysqli_connect_errno();
                            $error = mysqli_connect_error();
                            $errors['ERR_DB_ADMIN'] = $mod_strings['ERR_DB_ADMIN'] . $errno . ": {$error}).";
                            installLog("ERROR:: {$errors['ERR_DB_ADMIN']}");
                        } else {
                            $row = mysqli_fetch_row($result);
                            if ($row[0] == 1) {
                                $errors['ERR_DB_USER_EXISTS'] = $mod_strings['ERR_DB_USER_EXISTS'];
                                installLog("ERROR:: {$errors['ERR_DB_USER_EXISTS']}");
                            }
                            mysqli_free_result($result);
                        }
                    } else {
                        $mysql_db_selected = mysql_select_db('mysql', $link);
                        $user = $_SESSION['setup_db_sugarsales_user'];
                        $query = "select count(*) from user where User ='******'";
                        $result = mysql_query($query, $link);
                        if (!$result) {
                            $errno = mysql_errno();
                            $error = mysql_error();
                            $errors['ERR_DB_ADMIN'] = $mod_strings['ERR_DB_ADMIN'] . $errno . ": {$error}).";
                            installLog("ERROR:: {$errors['ERR_DB_ADMIN']}");
                        } else {
                            $row = mysql_fetch_row($result);
                            if ($row[0] == 1) {
                                $errors['ERR_DB_USER_EXISTS'] = $mod_strings['ERR_DB_USER_EXISTS'];
                                installLog("ERROR:: {$errors['ERR_DB_USER_EXISTS']}");
                                //do not throw errors, reuse existing user
                                //$_SESSION['setup_db_create_sugarsales_user'] = 0;
                            }
                            mysql_free_result($result);
                        }
                    }
                }
                // check mysql minimum version requirement
                $db_version = getMysqlVersion($link);
                if (version_compare($db_version, '4.1.2') < 0) {
                    $errors['ERR_DB_MYSQL_VERSION1'] = $mod_strings['ERR_DB_MYSQL_VERSION1'] . $db_version . $mod_strings['ERR_DB_MYSQL_VERSION2'];
                    installLog("ERROR:: {$errors['ERR_DB_MYSQL_VERSION1']}");
                } else {
                    installLog("Passed DB Version check, version is {$db_version}");
                }
                if (isset($_SESSION['mysql_type'])) {
                    mysqli_close($link);
                } else {
                    mysql_close($link);
                }
            } else {
                // dblink was bad
                if (isset($_SESSION['mysql_type'])) {
                    $errno = mysqli_connect_errno();
                    $error = mysqli_connect_error();
                } else {
                    $errno = mysql_errno();
                    $error = mysql_error();
                }
                $errors['ERR_DB_ADMIN'] = $mod_strings['ERR_DB_ADMIN'] . $errno . ": {$error}).";
                installLog("ERROR:: {$errors['ERR_DB_ADMIN']}");
            }
        } else {
            if ($_SESSION['setup_db_type'] == 'mssql') {
                installLog("Testing priviliged account...");
                $connect_host = "";
                $_SESSION['setup_db_host_instance'] = trim($_SESSION['setup_db_host_instance']);
                if (empty($_SESSION['setup_db_host_instance'])) {
                    $connect_host = $_SESSION['setup_db_host_name'];
                } else {
                    $connect_host = $_SESSION['setup_db_host_name'] . "\\" . $_SESSION['setup_db_host_instance'];
                }
                if (isset($_SESSION['mssql_type'])) {
                    $connect_params = array("UID" => $_SESSION['setup_db_sugarsales_user'], "PWD" => $_SESSION['setup_db_sugarsales_password'], "MultipleActiveResultSets" => false);
                    $link = sqlsrv_connect($connect_host, $connect_params);
                } else {
                    $link = @mssql_connect($connect_host, $_SESSION['setup_db_admin_user_name'], $_SESSION['setup_db_admin_password']);
                }
                if ($link) {
                    installLog("Connection made for Privileged admin account using  host: {$_SESSION['setup_db_host_name']}, usr: {$_SESSION['setup_db_admin_user_name']}");
                    // database admin credentials are valid--can continue check on stuff
                    $tbl_exists_qry = "SELECT name FROM master..sysdatabases WHERE name = N'{$_SESSION['setup_db_database_name']}'";
                    if (isset($_SESSION['mssql_type'])) {
                        $res = sqlsrv_query($link, $tbl_exists_qry);
                    } else {
                        $res = mssql_query($tbl_exists_qry);
                    }
                    $db_exists = false;
                    if (isset($_SESSION['mssql_type']) && sqlsrv_fetch($res) == 1) {
                        $db_exists = true;
                        installLog("DB Exists and selected, will reuse {$_SESSION['setup_db_database_name']}");
                    } elseif (!isset($_SESSION['mssql_type']) && mssql_num_rows($res) == 1) {
                        $db_exists = true;
                        installLog("DB Exists and selected, will reuse {$_SESSION['setup_db_database_name']}");
                    } else {
                        installLog("No DB Selected, will create {$_SESSION['setup_db_database_name']}");
                    }
                    if ($silent == false && $db_exists && $_SESSION['setup_db_create_database'] && (!isset($_SESSION['setup_db_drop_tables']) || !$_SESSION['setup_db_drop_tables'])) {
                        $errStr = $mod_strings['ERR_DB_EXISTS_PROCEED'];
                        $errors['ERR_DB_EXISTS_PROCEED'] = $errStr;
                        installLog("ERROR:: {$errors['ERR_DB_EXISTS_PROCEED']}");
                    } else {
                        if (!$db_exists && !$_SESSION['setup_db_create_database']) {
                            $errors['ERR_DB_EXISTS_NOT'] = $mod_strings['ERR_DB_EXISTS_NOT'];
                            installLog("ERROR:: {$errors['ERR_DB_EXISTS_NOT']}");
                        }
                    }
                    // check for existing SugarCRM database user if create flag is set,
                    //user name has been given, and database has been selected (reusing db, not creating new one)
                    if ($_SESSION['setup_db_create_sugarsales_user'] && $_SESSION['setup_db_sugarsales_user'] != '') {
                        if (isset($_SESSION['mssql_type'])) {
                            $mssql_db_selected = (bool) sqlsrv_query($link, 'USE master');
                            $user = $_SESSION['setup_db_sugarsales_user'];
                            $query = "select count(*) from sys.sql_logins where name ='{$user}'";
                            $result = sqlsrv_query($link, $query);
                            if (!$result) {
                                $errors['ERR_DB_ADMIN'] = $mod_strings['ERR_DB_ADMIN'];
                                installLog("ERROR:: {$errors['ERR_DB_ADMIN']}");
                            } else {
                                $row = sqlsrv_fetch_array($result);
                                if ($row[0] == 1) {
                                    $errors['ERR_DB_USER_EXISTS'] = $mod_strings['ERR_DB_USER_EXISTS'];
                                    installLog("ERROR:: {$errors['ERR_DB_USER_EXISTS']}");
                                }
                                sqlsrv_free_stmt($result);
                            }
                        } else {
                            $mssql_db_selected = mssql_select_db('master', $link);
                            $user = $_SESSION['setup_db_sugarsales_user'];
                            $query = "select count(*) from sys.sql_logins where name ='{$user}'";
                            $result = mssql_query($query, $link);
                            if (!$result) {
                                $errors['ERR_DB_ADMIN'] = $mod_strings['ERR_DB_ADMIN'];
                                installLog("ERROR:: {$errors['ERR_DB_ADMIN']}");
                            } else {
                                $row = mssql_fetch_row($result);
                                if ($row[0] == 1) {
                                    $errors['ERR_DB_USER_EXISTS'] = $mod_strings['ERR_DB_USER_EXISTS'];
                                    installLog("ERROR:: {$errors['ERR_DB_USER_EXISTS']}");
                                }
                                mssql_free_result($result);
                            }
                        }
                    }
                    if (isset($_SESSION['mssql_type'])) {
                        sqlsrv_close($link);
                    } else {
                        mssql_close($link);
                    }
                } else {
                    // dblink was bad
                    $errors['ERR_DB_ADMIN_MSSQL'] = $mod_strings['ERR_DB_ADMIN_MSSQL'] . $connect_host;
                    installLog("ERROR:: {$errors['ERR_DB_ADMIN_MSSQL']}");
                }
            } else {
                if ($_SESSION['setup_db_type'] == 'oci8') {
                }
            }
        }
    }
    // end of privileged user tests
    if ($silent) {
        return $errors;
    } else {
        printErrors($errors);
    }
    installLog("End DB Check Process *************");
}