| 1 | <?php |
|---|
| 2 | defined( 'ABSPATH' ) OR exit; |
|---|
| 3 | /** |
|---|
| 4 | * Plugin Name: MySQL(i) Database Connection |
|---|
| 5 | * Description: Database connection that uses MySQL(i). |
|---|
| 6 | * Compatibility: >= 4.3 |
|---|
| 7 | * Version: 1.3 |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | class wpdbi extends wpdb { |
|---|
| 11 | |
|---|
| 12 | private $ssl_key = null; |
|---|
| 13 | private $ssl_cert = null; |
|---|
| 14 | private $ssl_ca = null; |
|---|
| 15 | private $ssl_capath = null; |
|---|
| 16 | private $ssl_cipher = null; |
|---|
| 17 | |
|---|
| 18 | public function __construct( $dbuser, $dbpassword, $dbname, $dbhost, $ssl_key = null, $ssl_cert = null, $ssl_ca = null, $ssl_capath = null, $ssl_cipher = null ) { |
|---|
| 19 | $this->ssl_key = $ssl_key; |
|---|
| 20 | $this->ssl_cert = $ssl_cert; |
|---|
| 21 | $this->ssl_ca = $ssl_ca; |
|---|
| 22 | $this->ssl_capath = $ssl_capath; |
|---|
| 23 | $this->ssl_cipher = $ssl_cipher; |
|---|
| 24 | |
|---|
| 25 | parent::__construct($dbuser, $dbpassword, $dbname, $dbhost); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | public function db_connect( $allow_bail = true ) { |
|---|
| 29 | $this->is_mysql = true; |
|---|
| 30 | |
|---|
| 31 | /* |
|---|
| 32 | * Deprecated in 3.9+ when using MySQLi. No equivalent |
|---|
| 33 | * $new_link parameter exists for mysqli_* functions. |
|---|
| 34 | */ |
|---|
| 35 | $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; |
|---|
| 36 | $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; |
|---|
| 37 | |
|---|
| 38 | if ( $this->use_mysqli ) { |
|---|
| 39 | $this->dbh = mysqli_init(); |
|---|
| 40 | |
|---|
| 41 | $host = $this->dbhost; |
|---|
| 42 | $port = null; |
|---|
| 43 | $socket = null; |
|---|
| 44 | $is_ipv6 = false; |
|---|
| 45 | |
|---|
| 46 | $host_data = $this->parse_db_host( $this->dbhost ); |
|---|
| 47 | if ( $host_data ) { |
|---|
| 48 | list( $host, $port, $socket, $is_ipv6 ) = $host_data; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | /* |
|---|
| 52 | * If using the `mysqlnd` library, the IPv6 address needs to be enclosed |
|---|
| 53 | * in square brackets, whereas it doesn't while using the `libmysqlclient` library. |
|---|
| 54 | * @see https://bugs.php.net/bug.php?id=67563 |
|---|
| 55 | */ |
|---|
| 56 | if ( $is_ipv6 && extension_loaded( 'mysqlnd' ) ) { |
|---|
| 57 | $host = "[$host]"; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | if ( WP_DEBUG ) { |
|---|
| 61 | mysqli_ssl_set($this->dbh, $this->ssl_key, $this->ssl_cert, $this->ssl_ca, $this->ssl_capath, $this->ssl_cipher); // ADDED |
|---|
| 62 | mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); |
|---|
| 63 | } else { |
|---|
| 64 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|---|
| 65 | @mysqli_ssl_set($this->dbh, $this->ssl_key, $this->ssl_cert, $this->ssl_ca, $this->ssl_capath, $this->ssl_cipher); // ADDED |
|---|
| 66 | @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | if ( $this->dbh->connect_errno ) { |
|---|
| 70 | $this->dbh = null; |
|---|
| 71 | |
|---|
| 72 | /* |
|---|
| 73 | * It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if: |
|---|
| 74 | * - We haven't previously connected, and |
|---|
| 75 | * - WP_USE_EXT_MYSQL isn't set to false, and |
|---|
| 76 | * - ext/mysql is loaded. |
|---|
| 77 | */ |
|---|
| 78 | $attempt_fallback = true; |
|---|
| 79 | |
|---|
| 80 | if ( $this->has_connected ) { |
|---|
| 81 | $attempt_fallback = false; |
|---|
| 82 | } elseif ( defined( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) { |
|---|
| 83 | $attempt_fallback = false; |
|---|
| 84 | } elseif ( ! function_exists( 'mysql_connect' ) ) { |
|---|
| 85 | $attempt_fallback = false; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | if ( $attempt_fallback ) { |
|---|
| 89 | $this->use_mysqli = false; |
|---|
| 90 | return $this->db_connect( $allow_bail ); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | } else { |
|---|
| 94 | if ( WP_DEBUG ) { |
|---|
| 95 | $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
|---|
| 96 | } else { |
|---|
| 97 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|---|
| 98 | $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | if ( ! $this->dbh && $allow_bail ) { |
|---|
| 103 | wp_load_translations_early(); |
|---|
| 104 | |
|---|
| 105 | // Load custom DB error template, if present. |
|---|
| 106 | if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) { |
|---|
| 107 | require_once WP_CONTENT_DIR . '/db-error.php'; |
|---|
| 108 | die(); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | $message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n"; |
|---|
| 112 | |
|---|
| 113 | $message .= '<p>' . sprintf( |
|---|
| 114 | /* translators: 1: wp-config.php, 2: Database host. */ |
|---|
| 115 | __( '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.' ), |
|---|
| 116 | '<code>wp-config.php</code>', |
|---|
| 117 | '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
|---|
| 118 | ) . "</p>\n"; |
|---|
| 119 | |
|---|
| 120 | $message .= "<ul>\n"; |
|---|
| 121 | $message .= '<li>' . __( 'Are you sure you have the correct username and password?' ) . "</li>\n"; |
|---|
| 122 | $message .= '<li>' . __( 'Are you sure you have typed the correct hostname?' ) . "</li>\n"; |
|---|
| 123 | $message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; |
|---|
| 124 | $message .= "</ul>\n"; |
|---|
| 125 | |
|---|
| 126 | $message .= '<p>' . sprintf( |
|---|
| 127 | /* translators: %s: Support forums URL. */ |
|---|
| 128 | __( '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>.' ), |
|---|
| 129 | __( 'https://wordpress.org/support/forums/' ) |
|---|
| 130 | ) . "</p>\n"; |
|---|
| 131 | |
|---|
| 132 | $this->bail( $message, 'db_connect_fail' ); |
|---|
| 133 | |
|---|
| 134 | return false; |
|---|
| 135 | } elseif ( $this->dbh ) { |
|---|
| 136 | if ( ! $this->has_connected ) { |
|---|
| 137 | $this->init_charset(); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | $this->has_connected = true; |
|---|
| 141 | |
|---|
| 142 | $this->set_charset( $this->dbh ); |
|---|
| 143 | |
|---|
| 144 | $this->ready = true; |
|---|
| 145 | $this->set_sql_mode(); |
|---|
| 146 | $this->select( $this->dbname, $this->dbh ); |
|---|
| 147 | |
|---|
| 148 | return true; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | return false; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | if(!function_exists('wpdb')) |
|---|
| 156 | { |
|---|
| 157 | function wpdb(): object |
|---|
| 158 | { |
|---|
| 159 | global $wpdb; |
|---|
| 160 | |
|---|
| 161 | return $wpdb; |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | global $wpdb; |
|---|
| 166 | |
|---|
| 167 | $wpdb = new wpdbi( |
|---|
| 168 | DB_USER, |
|---|
| 169 | DB_PASSWORD, |
|---|
| 170 | DB_NAME, |
|---|
| 171 | DB_HOST, |
|---|
| 172 | ( defined( 'DB_SSL_KEY' ) ? DB_SSL_KEY : null ), |
|---|
| 173 | ( defined( 'DB_SSL_CERT' ) ? DB_SSL_CERT : null ), |
|---|
| 174 | ( defined( 'DB_SSL_CA' ) ? DB_SSL_CA : null ), |
|---|
| 175 | ( defined( 'DB_SSL_CAPATH' ) ? DB_SSL_CAPATH : null ), |
|---|
| 176 | ( defined( 'DB_SSL_CIPHER' ) ? DB_SSL_CIPHER : null ) |
|---|
| 177 | ); |
|---|