From 6ae2b318ec2bacbf87773242c9f5861eb73a2675 Mon Sep 17 00:00:00 2001 From: hj-collab <101234934+hj-collab@users.noreply.github.com> Date: Wed, 31 May 2023 12:49:19 +0530 Subject: [PATCH 1/5] Merged 105 - Modern MySQL compatibility https://github.com/Automattic/HyperDB/pull/105 --- db.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/db.php b/db.php index f1a7e01..0fb2381 100644 --- a/db.php +++ b/db.php @@ -435,6 +435,12 @@ public function db_connect( $query = '' ) { if ( ! $this->ex_mysql_select_db( DB_NAME, $this->dbh ) ) { return $this->log_and_bail( 'We were unable to select the database' ); } + + // Set the SQL mode. + if ( is_callable( array( $this, 'set_sql_mode' ) ) ) { + $this->set_sql_mode(); + } + if ( ! empty( $this->charset ) ) { $collation_query = "SET NAMES '$this->charset'"; if ( ! empty( $this->collate ) ) { @@ -781,8 +787,13 @@ public function db_connect( $query = '' ) { $this->set_charset( $this->dbhs[ $dbhname ], $charset, $collate ); - $this->dbh = $this->dbhs[ $dbhname ]; // needed by $wpdb->_real_escape() - + $this->dbh = $this->dbhs[ $dbhname ]; // needed by $wpdb->_real_escape() and `$wpdb->set_sql_mode()` + + // Set the SQL mode. + if ( is_callable( array( $this, 'set_sql_mode' ) ) ) { + $this->set_sql_mode(); + } + $this->last_used_server = compact( 'host', 'user', 'name', 'read', 'write' ); $this->used_servers[ $dbhname ] = $this->last_used_server; From 58183c5e7496e1ebf5dc5bda54319038b0481a94 Mon Sep 17 00:00:00 2001 From: hj-collab <101234934+hj-collab@users.noreply.github.com> Date: Wed, 31 May 2023 12:50:50 +0530 Subject: [PATCH 2/5] Merged 56 - PHP8 warnings https://github.com/Automattic/HyperDB/pull/56 --- db.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/db.php b/db.php index 0fb2381..5d2ec65 100644 --- a/db.php +++ b/db.php @@ -602,6 +602,8 @@ public function db_connect( $query = '' ) { // Overlay $server if it was extracted from a callback if ( isset( $server ) && is_array( $server ) ) { extract( $server, EXTR_OVERWRITE ); + } else { + $server = null; } // Split again in case $server had host:port From 90b719309d67f8087bf48ee86c795db4755b7ae1 Mon Sep 17 00:00:00 2001 From: hj-collab <101234934+hj-collab@users.noreply.github.com> Date: Wed, 31 May 2023 12:54:23 +0530 Subject: [PATCH 3/5] Merged 112 - is_write_query() https://github.com/Automattic/HyperDB/pull/112 --- db.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/db.php b/db.php index 5d2ec65..110d7d0 100644 --- a/db.php +++ b/db.php @@ -341,7 +341,10 @@ public function get_table_from_query( $q ) { public function is_write_query( $q ) { // Quick and dirty: only SELECT statements are considered read-only. $q = ltrim( $q, "\r\n\t (" ); - return ! preg_match( '/^(?:SELECT|SHOW|DESCRIBE|DESC|EXPLAIN)\s/i', $q ); + return ( + ! preg_match( '/^(?:SELECT|SHOW|DESCRIBE|DESC|EXPLAIN)\s/i', $q ) + || preg_match( '/(\sFOR UPDATE)/i', $q ) + ); } /** From 9ccbf40e656cfd4c6d483160240ff425a04f572a Mon Sep 17 00:00:00 2001 From: hj-collab <101234934+hj-collab@users.noreply.github.com> Date: Wed, 31 May 2023 12:56:25 +0530 Subject: [PATCH 4/5] Merged 130 - Flush the query results variables following strip_invalid_text_from_query() https://github.com/Automattic/HyperDB/pull/130 --- db.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db.php b/db.php index 110d7d0..a22ba26 100644 --- a/db.php +++ b/db.php @@ -950,6 +950,9 @@ public function query( $query ) { // If we're writing to the database, make sure the query will write safely. if ( $this->check_current_query && method_exists( $this, 'check_ascii' ) && ! $this->check_ascii( $query ) ) { $stripped_query = $this->strip_invalid_text_from_query( $query ); + // strip_invalid_text_from_query() can perform queries, so we need + // to flush again, just to make sure everything is clear. + $this->flush(); if ( $stripped_query !== $query ) { $this->insert_id = 0; $this->last_error = 'Invalid query'; From 3629f8461536946952b8433b9a2b8cc53f3a2b44 Mon Sep 17 00:00:00 2001 From: hj-collab <101234934+hj-collab@users.noreply.github.com> Date: Wed, 31 May 2023 12:57:55 +0530 Subject: [PATCH 5/5] Merged 129 - Add a connect timeout explicitly to hyperdb https://github.com/Automattic/HyperDB/pull/129 --- db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db.php b/db.php index a22ba26..daa4445 100644 --- a/db.php +++ b/db.php @@ -1507,7 +1507,7 @@ public function ex_mysql_connect( $db_host, $db_user, $db_password, $persistent if ( $persistent ) { $db_host = "p:{$db_host}"; } - + mysqli_options( $dbh, MYSQLI_OPT_CONNECT_TIMEOUT, $this->ex_mysql_connect_timeout() ); $retval = mysqli_real_connect( $dbh, $db_host, $db_user, $db_password, null, $port, $socket, $client_flags ); if ( ! $retval || $dbh->connect_errno ) {