|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Plugin name: Secure DB Connection |
| 4 | +Plugin URI: http://wordpress.org/plugins/secure-db-connection/ |
| 5 | +Description: Sets SSL keys and certs for encrypted database connections |
| 6 | +Author: Xiao Yu |
| 7 | +Author URI: http://xyu.io/ |
| 8 | +Version: 1.0 |
| 9 | +*/ |
| 10 | + |
| 11 | +class wpdb_ssl extends wpdb { |
| 12 | + |
| 13 | + /** |
| 14 | + * Connect to and select database. |
| 15 | + * |
| 16 | + * If $allow_bail is false, the lack of database connection will need |
| 17 | + * to be handled manually. |
| 18 | + * |
| 19 | + * @since 3.0.0 |
| 20 | + * @since 3.9.0 $allow_bail parameter added. |
| 21 | + * |
| 22 | + * @param bool $allow_bail Optional. Allows the function to bail. Default true. |
| 23 | + * @return bool True with a successful connection, false on failure. |
| 24 | + */ |
| 25 | + public function db_connect( $allow_bail = true ) { |
| 26 | + $this->is_mysql = true; |
| 27 | + |
| 28 | + /* |
| 29 | + * Deprecated in 3.9+ when using MySQLi. No equivalent |
| 30 | + * $new_link parameter exists for mysqli_* functions. |
| 31 | + */ |
| 32 | + $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; |
| 33 | + $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; |
| 34 | + |
| 35 | + if ( $this->use_mysqli ) { |
| 36 | + $this->dbh = mysqli_init(); |
| 37 | + |
| 38 | + // mysqli_real_connect doesn't support the host param including a port or socket |
| 39 | + // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file. |
| 40 | + $port = null; |
| 41 | + $socket = null; |
| 42 | + $host = $this->dbhost; |
| 43 | + $port_or_socket = strstr( $host, ':' ); |
| 44 | + if ( ! empty( $port_or_socket ) ) { |
| 45 | + $host = substr( $host, 0, strpos( $host, ':' ) ); |
| 46 | + $port_or_socket = substr( $port_or_socket, 1 ); |
| 47 | + if ( 0 !== strpos( $port_or_socket, '/' ) ) { |
| 48 | + $port = intval( $port_or_socket ); |
| 49 | + $maybe_socket = strstr( $port_or_socket, ':' ); |
| 50 | + if ( ! empty( $maybe_socket ) ) { |
| 51 | + $socket = substr( $maybe_socket, 1 ); |
| 52 | + } |
| 53 | + } else { |
| 54 | + $socket = $port_or_socket; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Set SSL certs if we want to use secure DB connections |
| 59 | + $ssl_opts = array( |
| 60 | + 'KEY' => ( defined( 'MYSQL_SSL_KEY' ) && is_file( MYSQL_SSL_KEY ) ) ? MYSQL_SSL_KEY : null, |
| 61 | + 'CERT' => ( defined( 'MYSQL_SSL_CERT' ) && is_file( MYSQL_SSL_CERT ) ) ? MYSQL_SSL_CERT : null, |
| 62 | + 'CA' => ( defined( 'MYSQL_SSL_CA' ) && is_file( MYSQL_SSL_CA ) ) ? MYSQL_SSL_CA : null, |
| 63 | + 'CA_PATH' => ( defined( 'MYSQL_SSL_CA_PATH' ) && is_dir ( MYSQL_SSL_CA_PATH ) ) ? MYSQL_SSL_CA_PATH : null, |
| 64 | + 'CIPHER' => ( defined( 'MYSQL_SSL_CIPHER' ) && !empty ( MYSQL_SSL_CIPHER ) ) ? MYSQL_SSL_CIPHER : null, |
| 65 | + ); |
| 66 | + $ssl_opts_set = false; |
| 67 | + foreach ( $ssl_opts as $ssl_opt_val ) { |
| 68 | + if ( !is_null( $ssl_opt_val ) ) { |
| 69 | + $ssl_opts_set = true; |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + if ( $ssl_opts_set ) { |
| 74 | + mysqli_ssl_set( |
| 75 | + $this->dbh, |
| 76 | + $ssl_opts[ 'KEY' ], |
| 77 | + $ssl_opts[ 'CERT' ], |
| 78 | + $ssl_opts[ 'CA' ], |
| 79 | + $ssl_opts[ 'CA_PATH' ], |
| 80 | + $ssl_opts[ 'CIPHER' ] |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + if ( WP_DEBUG ) { |
| 85 | + mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); |
| 86 | + } else { |
| 87 | + @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); |
| 88 | + } |
| 89 | + |
| 90 | + if ( $this->dbh->connect_errno ) { |
| 91 | + $this->dbh = null; |
| 92 | + |
| 93 | + /* It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if: |
| 94 | + * - We haven't previously connected, and |
| 95 | + * - WP_USE_EXT_MYSQL isn't set to false, and |
| 96 | + * - ext/mysql is loaded. |
| 97 | + */ |
| 98 | + $attempt_fallback = true; |
| 99 | + |
| 100 | + if ( $this->has_connected ) { |
| 101 | + $attempt_fallback = false; |
| 102 | + } elseif ( defined( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) { |
| 103 | + $attempt_fallback = false; |
| 104 | + } elseif ( ! function_exists( 'mysql_connect' ) ) { |
| 105 | + $attempt_fallback = false; |
| 106 | + } |
| 107 | + |
| 108 | + if ( $attempt_fallback ) { |
| 109 | + $this->use_mysqli = false; |
| 110 | + return $this->db_connect( $allow_bail ); |
| 111 | + } |
| 112 | + } |
| 113 | + } else { |
| 114 | + if ( WP_DEBUG ) { |
| 115 | + $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
| 116 | + } else { |
| 117 | + $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if ( ! $this->dbh && $allow_bail ) { |
| 122 | + wp_load_translations_early(); |
| 123 | + |
| 124 | + // Load custom DB error template, if present. |
| 125 | + if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) { |
| 126 | + require_once( WP_CONTENT_DIR . '/db-error.php' ); |
| 127 | + die(); |
| 128 | + } |
| 129 | + |
| 130 | + $message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n"; |
| 131 | + |
| 132 | + $message .= '<p>' . sprintf( |
| 133 | + /* translators: 1: wp-config.php. 2: database host */ |
| 134 | + __( 'This either means that the username and password information in your %1$s file is incorrect or we can’t contact the database server at %2$s. This could mean your host’s database server is down.' ), |
| 135 | + '<code>wp-config.php</code>', |
| 136 | + '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
| 137 | + ) . "</p>\n"; |
| 138 | + |
| 139 | + $message .= "<ul>\n"; |
| 140 | + $message .= '<li>' . __( 'Are you sure you have the correct username and password?' ) . "</li>\n"; |
| 141 | + $message .= '<li>' . __( 'Are you sure that you have typed the correct hostname?' ) . "</li>\n"; |
| 142 | + $message .= '<li>' . __( 'Are you sure that the database server is running?' ) . "</li>\n"; |
| 143 | + $message .= "</ul>\n"; |
| 144 | + |
| 145 | + $message .= '<p>' . sprintf( |
| 146 | + /* translators: %s: support forums URL */ |
| 147 | + __( 'If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ), |
| 148 | + __( 'https://wordpress.org/support/' ) |
| 149 | + ) . "</p>\n"; |
| 150 | + |
| 151 | + $this->bail( $message, 'db_connect_fail' ); |
| 152 | + |
| 153 | + return false; |
| 154 | + } elseif ( $this->dbh ) { |
| 155 | + if ( ! $this->has_connected ) { |
| 156 | + $this->init_charset(); |
| 157 | + } |
| 158 | + |
| 159 | + $this->has_connected = true; |
| 160 | + |
| 161 | + $this->set_charset( $this->dbh ); |
| 162 | + |
| 163 | + $this->ready = true; |
| 164 | + $this->set_sql_mode(); |
| 165 | + $this->select( $this->dbname, $this->dbh ); |
| 166 | + |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | +} |
| 174 | + |
| 175 | +$wpdb = new wpdb_ssl( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ); |
0 commit comments