Jump to content

MySQL Over SSL


buffer

Recommended Posts

I understand that the vast majority of users have their DB on localhost so SSL is pointless, but for those with external DB's it's an important feature to have. I wrote a quick hack in system/Db/Db.php, which can be seen below, and I hope Invision adds official support in the near future.

protected static function _establishConnection( $classname, $sqlCredentials, $utf8Mb4 = TRUE )
{
        // =============== Begin SSL Support Changes =============== //
        /* Initialize */
        $object = @new $classname();
        
        /* Setup SSL */
        $object->ssl_set('/path/to/client/key', '/path/to/client/cert', '/path/to/ca', NULL, NULL);
        
        /* Connect (SSL support) */
        $object->real_connect(
            $sqlCredentials['host'],
            $sqlCredentials['username'],
            $sqlCredentials['password'],
            $sqlCredentials['database'],
            $sqlCredentials['port'],
            $sqlCredentials['socket'],
            MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT
        );
        
        /* Connect (original code without SSL support) */
        /*
        $object = @new $classname(
            $sqlCredentials['host'],
            $sqlCredentials['username'],
            $sqlCredentials['password'],
            $sqlCredentials['database'],
            $sqlCredentials['port'],
            $sqlCredentials['socket']
            );
        */
        // =============== End SSL Support Changes =============== //
        
        /* If the connection failed, throw an exception */
        if( $error = mysqli_connect_error() )
        {
            $errorNumber = $object->connect_errno;
            throw new \IPS\Db\Exception( $error, $errorNumber );
        }
        
        /* Charset */
        if ( $utf8Mb4 )
        {
            if ( $object->set_charset( 'utf8mb4' ) === FALSE )
            {
                /* If setting utf8mb4 fails, then gracefully fallback to normal utf8 */
                $object->set_charset( 'utf8' );
            }
        }
        else
        {
            $object->set_charset( 'utf8' );
        }
        
        /* Strict mode */
        if ( \IPS\IN_DEV )
        {
            $object->query( "SET sql_mode='STRICT_ALL_TABLES,ONLY_FULL_GROUP_BY,ANSI_QUOTES'" );
        }
        
        /* Return */
        return $object;
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...