| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Connection; |
| 4 |
|
| 5 |
/** |
| 6 |
* This class extends WodPress class wpdb to enable remote connections and connections to local databases. |
| 7 |
*/ |
| 8 |
class WPDADB_WPDB extends \wpdb { |
| 9 |
/** |
| 10 |
* Indicator: ON = use SSL |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
private $ssl; |
| 15 |
|
| 16 |
/** |
| 17 |
* SSL key |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $ssl_key; |
| 22 |
|
| 23 |
/** |
| 24 |
* SSL certificate |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $ssl_cert; |
| 29 |
|
| 30 |
/** |
| 31 |
* SSL CA |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $ssl_ca; |
| 36 |
|
| 37 |
/** |
| 38 |
* SSL path |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
private $ssl_path; |
| 43 |
|
| 44 |
/** |
| 45 |
* SSL cipher |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
private $ssl_cipher; |
| 50 |
|
| 51 |
/** |
| 52 |
* Overwrite WPDADB_WPDB constructor |
| 53 |
* |
| 54 |
* Save schema_name |
| 55 |
* |
| 56 |
* @param string $dbuser Username (original wpdb parameter). |
| 57 |
* @param string $dbpassword Password (original wpdb parameter). |
| 58 |
* @param string $dbname Database schema name (original wpdb parameter). |
| 59 |
* @param string $dbhost Host name (original wpdb parameter). |
| 60 |
* @param string $ssl SSL on|off. |
| 61 |
* @param string $ssl_key SSL key. |
| 62 |
* @param string $ssl_cert SSL certificate. |
| 63 |
* @param string $ssl_ca SSL CA. |
| 64 |
* @param string $ssl_path SSL path. |
| 65 |
* @param string $ssl_cipher SSL cipher. |
| 66 |
*/ |
| 67 |
public function __construct( |
| 68 |
$dbuser, |
| 69 |
$dbpassword, |
| 70 |
$dbname, |
| 71 |
$dbhost, |
| 72 |
// Original WordPress wpdb parameters |
| 73 |
$ssl = 'OFF', |
| 74 |
$ssl_key = null, |
| 75 |
$ssl_cert = null, |
| 76 |
$ssl_ca = null, |
| 77 |
$ssl_path = null, |
| 78 |
$ssl_cipher = null |
| 79 |
) { |
| 80 |
$this->ssl = $ssl; |
| 81 |
$this->ssl_key = $ssl_key; |
| 82 |
$this->ssl_cert = $ssl_cert; |
| 83 |
$this->ssl_ca = $ssl_ca; |
| 84 |
$this->ssl_path = $ssl_path; |
| 85 |
$this->ssl_cipher = $ssl_cipher; |
| 86 |
if ( '' === trim( (string) $this->ssl_path ) ) { |
| 87 |
$this->ssl_path = null; |
| 88 |
} |
| 89 |
if ( '' === trim( (string) $this->ssl_cipher ) ) { |
| 90 |
$this->ssl_cipher = null; |
| 91 |
} |
| 92 |
parent::__construct( |
| 93 |
$dbuser, |
| 94 |
$dbpassword, |
| 95 |
$dbname, |
| 96 |
$dbhost |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Is connection established? |
| 102 |
* |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
public function is_connected() { |
| 106 |
return 0 === $this->dbh->connect_errno; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Overwrite method |
| 111 |
* |
| 112 |
* This just adds a call to private method @ssl() |
| 113 |
* |
| 114 |
* @param bool $allow_bail Allow bail. |
| 115 |
* |
| 116 |
* @return bool |
| 117 |
*/ |
| 118 |
public function db_connect( $allow_bail = false ) { |
| 119 |
// phpcs:disable -- skip all mysqli messages |
| 120 |
$this->is_mysql = true; |
| 121 |
$client_flags = ( defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0 ); |
| 122 |
/* |
| 123 |
* Set the MySQLi error reporting off because WordPress handles its own. |
| 124 |
* This is due to the default value change from `MYSQLI_REPORT_OFF` |
| 125 |
* to `MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT` in PHP 8.1. |
| 126 |
*/ |
| 127 |
mysqli_report( MYSQLI_REPORT_OFF ); |
| 128 |
$this->dbh = mysqli_init(); |
| 129 |
$host = $this->dbhost; |
| 130 |
$port = null; |
| 131 |
$socket = null; |
| 132 |
$is_ipv6 = false; |
| 133 |
$host_data = $this->parse_db_host( $this->dbhost ); |
| 134 |
if ( $host_data ) { |
| 135 |
list( $host, $port, $socket, $is_ipv6 ) = $host_data; |
| 136 |
} |
| 137 |
/* |
| 138 |
* If using the `mysqlnd` library, the IPv6 address needs to be enclosed |
| 139 |
* in square brackets, whereas it doesn't while using the `libmysqlclient` library. |
| 140 |
* @see https://bugs.php.net/bug.php?id=67563 |
| 141 |
*/ |
| 142 |
if ( $is_ipv6 && extension_loaded( 'mysqlnd' ) ) { |
| 143 |
$host = "[{$host}]"; |
| 144 |
} |
| 145 |
if ( 'on' === $this->ssl ) { |
| 146 |
mysqli_ssl_set( |
| 147 |
$this->dbh, |
| 148 |
$this->ssl_key, |
| 149 |
$this->ssl_cert, |
| 150 |
$this->ssl_ca, |
| 151 |
$this->ssl_path, |
| 152 |
$this->ssl_cipher |
| 153 |
); |
| 154 |
} |
| 155 |
mysqli_report( MYSQLI_REPORT_STRICT ); |
| 156 |
try { |
| 157 |
if ( WP_DEBUG ) { |
| 158 |
if ( !mysqli_real_connect( |
| 159 |
$this->dbh, |
| 160 |
$host, |
| 161 |
$this->dbuser, |
| 162 |
$this->dbpassword, |
| 163 |
null, |
| 164 |
$port, |
| 165 |
$socket, |
| 166 |
$client_flags |
| 167 |
) ) { |
| 168 |
mysqli_report( MYSQLI_REPORT_OFF ); |
| 169 |
return false; |
| 170 |
} |
| 171 |
} else { |
| 172 |
if ( !@mysqli_real_connect( |
| 173 |
$this->dbh, |
| 174 |
$host, |
| 175 |
$this->dbuser, |
| 176 |
$this->dbpassword, |
| 177 |
null, |
| 178 |
$port, |
| 179 |
$socket, |
| 180 |
$client_flags |
| 181 |
) ) { |
| 182 |
mysqli_report( MYSQLI_REPORT_OFF ); |
| 183 |
return false; |
| 184 |
} |
| 185 |
} |
| 186 |
} catch ( \mysqli_sql_exception $e ) { |
| 187 |
mysqli_report( MYSQLI_REPORT_OFF ); |
| 188 |
return false; |
| 189 |
} |
| 190 |
mysqli_report( MYSQLI_REPORT_OFF ); |
| 191 |
if ( $this->dbh->connect_errno ) { |
| 192 |
$this->dbh = null; |
| 193 |
/* |
| 194 |
* It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if: |
| 195 |
* - We haven't previously connected, and |
| 196 |
* - WP_USE_EXT_MYSQL isn't set to false, and |
| 197 |
* - ext/mysql is loaded. |
| 198 |
*/ |
| 199 |
$attempt_fallback = true; |
| 200 |
if ( $this->has_connected ) { |
| 201 |
$attempt_fallback = false; |
| 202 |
} elseif ( defined( 'WP_USE_EXT_MYSQL' ) && !WP_USE_EXT_MYSQL ) { |
| 203 |
$attempt_fallback = false; |
| 204 |
} elseif ( !function_exists( 'mysql_connect' ) ) { |
| 205 |
$attempt_fallback = false; |
| 206 |
} |
| 207 |
if ( $attempt_fallback ) { |
| 208 |
$this->use_mysqli = false; |
| 209 |
return $this->db_connect( $allow_bail ); |
| 210 |
} |
| 211 |
} |
| 212 |
if ( !$this->dbh && $allow_bail ) { |
| 213 |
wp_load_translations_early(); |
| 214 |
// Load custom DB error template, if present. |
| 215 |
if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) { |
| 216 |
require_once WP_CONTENT_DIR . '/db-error.php'; |
| 217 |
die; |
| 218 |
} |
| 219 |
$message = '<h1>' . __( 'Error establishing a database connection', 'wp-data-access' ) . "</h1>\n"; |
| 220 |
$message .= '<p>' . sprintf( |
| 221 |
/* translators: 1: wp-config.php, 2: Database host. */ |
| 222 |
__( '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.', 'wp-data-access' ), |
| 223 |
'<code>wp-config.php</code>', |
| 224 |
'<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
| 225 |
) . "</p>\n"; |
| 226 |
$message .= "<ul>\n"; |
| 227 |
$message .= '<li>' . __( 'Are you sure you have the correct username and password?', 'wp-data-access' ) . "</li>\n"; |
| 228 |
$message .= '<li>' . __( 'Are you sure you have typed the correct hostname?', 'wp-data-access' ) . "</li>\n"; |
| 229 |
$message .= '<li>' . __( 'Are you sure the database server is running?', 'wp-data-access' ) . "</li>\n"; |
| 230 |
$message .= "</ul>\n"; |
| 231 |
$message .= '<p>' . sprintf( |
| 232 |
/* translators: %s: Support forums URL. */ |
| 233 |
__( '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>.', 'wp-data-access' ), |
| 234 |
__( 'https://wordpress.org/support/forums/', 'wp-data-access' ) |
| 235 |
) . "</p>\n"; |
| 236 |
$this->bail( $message, 'db_connect_fail' ); |
| 237 |
// phpcs:enable |
| 238 |
return false; |
| 239 |
} elseif ( $this->dbh ) { |
| 240 |
if ( !$this->has_connected ) { |
| 241 |
$this->init_charset(); |
| 242 |
} |
| 243 |
$this->has_connected = true; |
| 244 |
$this->set_charset( $this->dbh ); |
| 245 |
$this->ready = true; |
| 246 |
$this->set_sql_mode(); |
| 247 |
$this->select( $this->dbname, $this->dbh ); |
| 248 |
// phpcs:enable |
| 249 |
return true; |
| 250 |
} |
| 251 |
// phpcs:enable |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Get connection error no |
| 257 |
* |
| 258 |
* @return mixed |
| 259 |
*/ |
| 260 |
public function get_connect_errno() { |
| 261 |
return $this->dbh->connect_errno; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Get connection error message |
| 266 |
* |
| 267 |
* @return mixed |
| 268 |
*/ |
| 269 |
public function get_connect_error() { |
| 270 |
return $this->dbh->connect_error; |
| 271 |
} |
| 272 |
|
| 273 |
public function wpda_open_cursor( $query ) { |
| 274 |
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { |
| 275 |
$this->timer_start(); |
| 276 |
} |
| 277 |
if ( !empty( $this->dbh ) && $this->use_mysqli ) { |
| 278 |
// phpcs:disable |
| 279 |
$this->result = mysqli_query( $this->dbh, $query ); |
| 280 |
// phpcs:enable |
| 281 |
} |
| 282 |
$this->num_queries++; |
| 283 |
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { |
| 284 |
$this->log_query( |
| 285 |
$query, |
| 286 |
$this->timer_stop(), |
| 287 |
$this->get_caller(), |
| 288 |
$this->time_start, |
| 289 |
array() |
| 290 |
); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
public function wpda_fetch_from_cursor() { |
| 295 |
if ( $this->use_mysqli && 'mysqli_result' === get_class( $this->result ) ) { |
| 296 |
// phpcs:disable |
| 297 |
return mysqli_fetch_object( $this->result ); |
| 298 |
// phpcs:enable |
| 299 |
} |
| 300 |
return null; |
| 301 |
} |
| 302 |
|
| 303 |
} |
| 304 |
|