• Hello, I’m trying to use this plugin to replace WordPress users db with external MSSQL db.

    I installed sqlsrv and pdo extensions on my server, but can’t get this to work, so it’s time to ask the basic question – is this plugin compatible with MSSQL?

    Thank you in advance for any response.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author tbenyon

    (@tbenyon)

    Hey Bartekryt,

    I’m just going to test compatibility with it now.

    If it’s not, are you still interested in the plugin? If so I’m happy to get this working for you asap.

    Tom

    Hi Tom,
    if you change the code in login/db.php to the following it will work with the MSSQL Server. Tested on Windows with Microsoft Drivers for PHP for SQL Server (https://docs.microsoft.com/en-us/sql/connect/php/microsoft-php-driver-for-sql-server?view=sql-server-2017)

    <?php

    function exlog_get_external_db_instance_and_fields() {
    try {
    $connectionOptions = array(
    “Database” => exlog_get_option(“external_login_option_db_name”),
    “UID” => exlog_get_option(“external_login_option_db_username”),
    “PWD” => exlog_get_option(“external_login_option_db_password”),
    “APP” => “WordPressSupport”,
    “ApplicationIntent” => “ReadOnly”
    );

    $conn = sqlsrv_connect( exlog_get_option(“external_login_option_db_host”), $connectionOptions);
    if( $conn === false )
    {
    error_log(FormatErrors(sqlsrv_errors()));
    return false;
    }

    $data = array(
    “db_instance” => $conn,
    “dbstructure_table” => exlog_get_option(‘exlog_dbstructure_table’),
    “dbstructure_username” => exlog_get_option(‘exlog_dbstructure_username’),
    “dbstructure_password” => exlog_get_option(‘exlog_dbstructure_password’),
    “dbstructure_first_name” => exlog_get_option(‘exlog_dbstructure_first_name’),
    “dbstructure_last_name” => exlog_get_option(‘exlog_dbstructure_last_name’),
    “dbstructure_role” => exlog_get_option(‘exlog_dbstructure_role’),
    “dbstructure_email” => exlog_get_option(‘exlog_dbstructure_email’),
    );

    if (exlog_get_option(‘external_login_option_db_salting_method’) == ‘all’) {
    $data[‘dbstructure_salt’] = exlog_get_option(‘exlog_dbstructure_salt’);
    }

    return $data;
    }
    catch (Exception $ex)
    {
    error_log(FormatErrors($ex));
    return false;
    }
    };

    function exlog_build_wp_user_data($db_data, $userData) {
    return array(
    “username” => $userData->{$db_data[“dbstructure_username”]},
    “password” => $userData->{$db_data[“dbstructure_password”]},
    “first_name” => $userData->{$db_data[“dbstructure_first_name”]},
    “last_name” => $userData->{$db_data[“dbstructure_last_name”]},
    “role” => $userData->{$db_data[“dbstructure_role”]},
    “email” => $userData->{$db_data[“dbstructure_email”]},
    );
    }

    function exlog_auth_query($username, $password) {
    try {
    $db_data = exlog_get_external_db_instance_and_fields();

    $query_string =
    ‘SELECT *’ .
    ‘ FROM ‘ . esc_sql($db_data[“dbstructure_table”]) .
    ‘ WHERE ‘ . esc_sql($db_data[“dbstructure_username”]) . ‘=\” . esc_sql($username) . ‘\”;

    $stmt = sqlsrv_query( $db_data[“db_instance”], $query_string);
    if (sqlsrv_has_rows( $stmt ) != true)
    {
    return array(
    “valid” => false
    );
    }

    while( $userData = sqlsrv_fetch_object( $stmt))
    {
    $user_specific_salt = false;

    if (exlog_get_option(‘external_login_option_db_salting_method’) == ‘all’) {
    $user_specific_salt = $userData->{$db_data[“dbstructure_salt”]};
    }

    $valid_credentials = exlog_validate_password($password, $userData->{$db_data[“dbstructure_password”]}, $user_specific_salt);

    if ($valid_credentials) {
    $wp_user_data = exlog_build_wp_user_data($db_data, $userData);
    $wp_user_data[“valid”] = true;
    return $wp_user_data;
    }
    }
    return array(
    “valid” => false
    );
    }
    catch (Exception $ex)
    {
    error_log(FormatErrors($ex));
    return false;
    }
    }

    function exlog_test_query($limit = false) {

    $db_data = exlog_get_external_db_instance_and_fields();

    try {
    $query_string = ”;
    if ($limit && is_int($limit)) {
    $query_string .= ‘SELECT TOP ‘ . $limit . ‘ *’;
    }
    else
    {
    $query_string .= ‘SELECT *’;
    }

    $query_string .= ‘ FROM ‘ . esc_sql($db_data[“dbstructure_table”]);

    $stmt = sqlsrv_query( $db_data[“db_instance”], $query_string);
    if (sqlsrv_has_rows( $stmt ) != true)
    {
    error_log(“External Login – No rows returned from test query.”);
    return false;
    }

    $users = array();
    while( $user_data = sqlsrv_fetch_object( $stmt))
    {
    array_push($users, exlog_build_wp_user_data($db_data, $user_data));
    }
    return $users;
    }
    catch (Exception $ex)
    {
    error_log(FormatErrors($ex));
    return false;
    }
    }

    Plugin Author tbenyon

    (@tbenyon)

    Hey @fbosito,

    Thanks for the time you’ve taken on this. I’m happy to look at integrating your solution into the plugin if this would be useful.

    My only issue is I don’t have access to Windows to get this setup and tested so it’s going to take a bit more time for me to arrange something my end.

    Just wanted to let you know I appreciate your time and I’m not ignoring you and I’ll try and get round to it soon.

    Thanks,

    Tom

    Plugin Author tbenyon

    (@tbenyon)

    I’m not marking this as resolved as I clearly haven’t. Just wanted to update and say I’m struggling to gain access to a MSSQL DB to test with so I haven’t been able to do this yet.

    I will keep trying when I find time but if anyone knows where I can get access to a free test db I’d appreciate the feedback. I develop on Mac without access to a Windows machine to build one locally.

    Thanks,

    Tom

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Compatibilty with MSSQL’ is closed to new replies.