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