示例#1
0
    /**
     * Function checks the database connection
     * @param <String> $db_type
     * @param <String> $db_hostname
     * @param <String> $db_username
     * @param <String> $db_password
     * @param <String> $db_name
     * @param <String> $create_db
     * @param <String> $create_utf8_db
     * @param <String> $root_user
     * @param <String> $root_password
     * @return <Array>
     */
    public static function checkDbConnection($db_type, $db_hostname, $db_username, $db_password, $db_name, $create_db = false, $create_utf8_db = true, $root_user = '', $root_password = '')
    {
        $dbCheckResult = array();
        $db_type_status = false;
        // is there a db type?
        $db_server_status = false;
        // does the db server connection exist?
        $db_creation_failed = false;
        // did we try to create a database and fail?
        $db_exist_status = false;
        // does the database exist?
        $db_utf8_support = false;
        // does the database support utf8?
        //Checking for database connection parameters
        if ($db_type) {
            $conn =& NewADOConnection($db_type);
            $db_type_status = true;
            if (@$conn->Connect($db_hostname, $db_username, $db_password)) {
                $db_server_status = true;
                $serverInfo = $conn->ServerInfo();
                if (self::isMySQL($db_type)) {
                    $mysql_server_version = self::getMySQLVersion($serverInfo);
                }
                if ($create_db) {
                    // drop the current database if it exists
                    $dropdb_conn =& NewADOConnection($db_type);
                    if (@$dropdb_conn->Connect($db_hostname, $root_user, $root_password, $db_name)) {
                        $query = "DROP DATABASE " . $db_name;
                        $dropdb_conn->Execute($query);
                        $dropdb_conn->Close();
                    }
                    // create the new database
                    $db_creation_failed = true;
                    $createdb_conn =& NewADOConnection($db_type);
                    if (@$createdb_conn->Connect($db_hostname, $root_user, $root_password)) {
                        $query = "CREATE DATABASE " . $db_name;
                        if ($create_utf8_db == 'true') {
                            if (self::isMySQL($db_type)) {
                                $query .= " DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci";
                            }
                            $db_utf8_support = true;
                        }
                        if ($createdb_conn->Execute($query)) {
                            $db_creation_failed = false;
                        }
                        $createdb_conn->Close();
                    }
                }
                if (@$conn->Connect($db_hostname, $db_username, $db_password, $db_name)) {
                    $db_exist_status = true;
                    if (!$db_utf8_support) {
                        $db_utf8_support = Vtiger_Util_Helper::checkDbUTF8Support($conn);
                    }
                }
                $conn->Close();
            }
        }
        $dbCheckResult['db_utf8_support'] = $db_utf8_support;
        $error_msg = '';
        $error_msg_info = '';
        if (!$db_type_status || !$db_server_status) {
            $error_msg = getTranslatedString('ERR_DATABASE_CONNECTION_FAILED', 'Install') . '. ' . getTranslatedString('ERR_INVALID_MYSQL_PARAMETERS', 'Install');
            $error_msg_info = getTranslatedString('MSG_LIST_REASONS', 'Install') . ':<br>
					-  ' . getTranslatedString('MSG_DB_PARAMETERS_INVALID', 'Install') . '
					-  ' . getTranslatedString('MSG_DB_USER_NOT_AUTHORIZED', 'Install');
        } elseif (self::isMySQL($db_type) && $mysql_server_version < 4.1) {
            $error_msg = $mysql_server_version . ' -> ' . getTranslatedString('ERR_INVALID_MYSQL_VERSION', 'Install');
        } elseif ($db_creation_failed) {
            $error_msg = getTranslatedString('ERR_UNABLE_CREATE_DATABASE', 'Install') . ' ' . $db_name;
            $error_msg_info = getTranslatedString('MSG_DB_ROOT_USER_NOT_AUTHORIZED', 'Install');
        } elseif (!$db_exist_status) {
            $error_msg = $db_name . ' -> ' . getTranslatedString('ERR_DB_NOT_FOUND', 'Install');
        } else {
            $dbCheckResult['flag'] = true;
            return $dbCheckResult;
        }
        $dbCheckResult['flag'] = false;
        $dbCheckResult['error_msg'] = $error_msg;
        $dbCheckResult['error_msg_info'] = $error_msg_info;
        return $dbCheckResult;
    }