From b72db01b984fa50b85a41f1167deb7bc896a8dad Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Mon, 18 Nov 2019 09:51:46 +1300 Subject: [PATCH] Improve detection of db host (#91) * Improve detection of db host * more tweaks for better db handling * bump version --- CHANGELOG.md | 5 +++ app/libs/Zend/Db/Adapter/Mysqli.php | 4 ++- classes/WpMatomo/Admin/SystemReport.php | 43 ++++++++++++++++++++----- classes/WpMatomo/Db/WordPress.php | 26 ++++++++------- config/config.php | 32 +++++++++++++----- matomo.php | 2 +- 6 files changed, 83 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c46caa118..6840fbb08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +0.2.7 +- Improved system report +- Check for incompatible plugins +- Log SQL query when error occurs + 0.2.6 - Improvements for multi site tracking in network mode - Show default tracking code when tracking is disabled diff --git a/app/libs/Zend/Db/Adapter/Mysqli.php b/app/libs/Zend/Db/Adapter/Mysqli.php index e94ec26b3..2260a5a07 100644 --- a/app/libs/Zend/Db/Adapter/Mysqli.php +++ b/app/libs/Zend/Db/Adapter/Mysqli.php @@ -346,6 +346,8 @@ protected function _connect() } } + $socket = !empty($this->_config['unix_socket']) ? $this->_config['unix_socket'] : null; + // Suppress connection warnings here. // Throw an exception instead. $_isConnected = @mysqli_real_connect( @@ -355,7 +357,7 @@ protected function _connect() $this->_config['password'], $this->_config['dbname'], $port, - $socket = null, + $socket, $enable_ssl ? $flags : null ); diff --git a/classes/WpMatomo/Admin/SystemReport.php b/classes/WpMatomo/Admin/SystemReport.php index 59c7087df..6d57f592d 100644 --- a/classes/WpMatomo/Admin/SystemReport.php +++ b/classes/WpMatomo/Admin/SystemReport.php @@ -392,8 +392,6 @@ private function add_diagnostic_results( $rows, $results ) { } private function get_wordpress_info() { - global $wpdb; - $is_multi_site = is_multisite(); $num_blogs = 1; $is_network_enabled = false; @@ -439,8 +437,8 @@ private function get_wordpress_info() { 'value' => defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON, ); $rows[] = array( - 'name' => 'DB Prefix', - 'value' => $wpdb->prefix, + 'name' => 'Force SSL Admin', + 'value' => defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN, ); return $rows; @@ -449,10 +447,18 @@ private function get_wordpress_info() { private function get_server_info() { $rows = array(); - $rows[] = array( - 'name' => 'Server Info', - 'value' => $_SERVER['SERVER_SOFTWARE'], - ); + if ( ! empty( $_SERVER['SERVER_SOFTWARE'] ) ) { + $rows[] = array( + 'name' => 'Server Info', + 'value' => $_SERVER['SERVER_SOFTWARE'], + ); + } + if ( PHP_OS ) { + $rows[] = array( + 'name' => 'PHP OS', + 'value' => PHP_OS, + ); + } $rows[] = array( 'name' => 'PHP Version', 'value' => phpversion(), @@ -525,6 +531,27 @@ private function get_db_info() { 'comment' => '', ); + $rows[] = array( + 'name' => 'DB Prefix', + 'value' => $wpdb->prefix, + ); + + if ( method_exists( $wpdb, 'parse_db_host' ) ) { + $host_data = $wpdb->parse_db_host( DB_HOST ); + if ( $host_data ) { + list( $host, $port, $socket, $is_ipv6 ) = $host_data; + } + + $rows[] = array( + 'name' => 'Uses Socket', + 'value' => ! empty( $socket ), + ); + $rows[] = array( + 'name' => 'Uses IPv6', + 'value' => ! empty( $is_ipv6 ), + ); + } + $grants = $this->get_db_grants(); // we only show these grants for security reasons as only they are needed and we don't need to know any other ones diff --git a/classes/WpMatomo/Db/WordPress.php b/classes/WpMatomo/Db/WordPress.php index 313e89e87..6e1a79f81 100644 --- a/classes/WpMatomo/Db/WordPress.php +++ b/classes/WpMatomo/Db/WordPress.php @@ -164,7 +164,7 @@ public function query( $sql, $bind = array() ) { $result = $wpdb->query( $prepare ); - $this->after_execute_query( $wpdb ); + $this->after_execute_query( $wpdb, $sql ); } return new WordPressDbStatement( $this, $sql, $result ); @@ -176,7 +176,7 @@ public function exec( $sqlQuery ) { $this->before_execute_query( $wpdb, $sqlQuery ); $exec = $wpdb->query( $sqlQuery ); - $this->after_execute_query( $wpdb ); + $this->after_execute_query( $wpdb, $sqlQuery ); return $exec; } @@ -193,7 +193,7 @@ public function fetchCol( $sql, $bind = array() ) { $col = $wpdb->get_col( $prepare ); - $this->after_execute_query( $wpdb ); + $this->after_execute_query( $wpdb, $sql ); return $col; } @@ -206,7 +206,7 @@ public function fetchAssoc( $sql, $bind = array() ) { $assoc = $wpdb->get_results( $prepare, ARRAY_A ); - $this->after_execute_query( $wpdb ); + $this->after_execute_query( $wpdb, $sql ); return $assoc; } @@ -263,14 +263,18 @@ private function before_execute_query( $wpdb, $sql ) { * * @throws \Zend_Db_Statement_Exception */ - private function after_execute_query( $wpdb ) { + private function after_execute_query( $wpdb, $sql ) { if ( isset( $this->old_suppress_errors_value ) ) { $wpdb->suppress_errors( $this->old_suppress_errors_value ); $this->old_suppress_errors_value = null; } if ( $wpdb->last_error ) { - throw new \Zend_Db_Statement_Exception( 'WP DB Error: ' . $wpdb->last_error ); + $message = 'WP DB Error: ' . $wpdb->last_error; + if ( $sql ) { + $message .= ' SQL: ' . $sql; + } + throw new \Zend_Db_Statement_Exception( $message ); } } @@ -282,7 +286,7 @@ public function fetchAll( $sql, $bind = array(), $fetchMode = null ) { $results = $wpdb->get_results( $prepare, ARRAY_A ); - $this->after_execute_query( $wpdb ); + $this->after_execute_query( $wpdb, $sql ); return $results; } @@ -295,7 +299,7 @@ public function fetchOne( $sql, $bind = array() ) { $value = $wpdb->get_var( $prepare ); - $this->after_execute_query( $wpdb ); + $this->after_execute_query( $wpdb, $sql ); if ( $value === null ) { return false; // make sure to behave same way as matomo @@ -312,7 +316,7 @@ public function fetchRow( $sql, $bind = array(), $fetchMode = null ) { $row = $wpdb->get_row( $prepare, ARRAY_A ); - $this->after_execute_query( $wpdb ); + $this->after_execute_query( $wpdb, $sql ); return $row; } @@ -324,7 +328,7 @@ public function insert( $table, array $bind ) { $insert = $wpdb->insert( $table, $bind ); - $this->after_execute_query( $wpdb ); + $this->after_execute_query( $wpdb, '' ); return $insert; } @@ -345,7 +349,7 @@ public function update( $table, array $bind, $where = '' ) { $update = $wpdb->query( $prepared ); - $this->after_execute_query( $wpdb ); + $this->after_execute_query( $wpdb, '' ); return $update; } diff --git a/config/config.php b/config/config.php index 9065c87b7..54660e3e3 100644 --- a/config/config.php +++ b/config/config.php @@ -43,13 +43,26 @@ \Piwik\Plugins\TagManager\TagManager::$enableAutoContainerCreation = false; - // in case DB credentials change in wordpress, we need to apply these changes here as well on demand - $hostParts = explode(':', DB_HOST); - $host = $hostParts[0]; - if (count($hostParts) === 2 && is_numeric($hostParts[1])) { - $port = $hostParts[1]; - } else { - $port = 3306; + $socket = ''; + $host_data = null; + if (method_exists($wpdb, 'parse_db_host')) { + // WP 4.9+ + $host_data = $wpdb->parse_db_host( DB_HOST ); + if ($host_data) { + list( $host, $port, $socket, $is_ipv6 ) = $host_data; + } + } + + if (!$host_data) { + // WP 4.8 and older + // in case DB credentials change in wordpress, we need to apply these changes here as well on demand + $hostParts = explode(':', DB_HOST); + $host = $hostParts[0]; + if (count($hostParts) === 2 && is_numeric($hostParts[1])) { + $port = $hostParts[1]; + } else { + $port = 3306; + } } if (defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN) { @@ -60,11 +73,14 @@ } $paths = new Paths(); - if (file_exists( $paths->get_config_ini_path())) { + if ( file_exists( $paths->get_config_ini_path() ) ) { // we overwrite DB on demand only once installed... otherwise Matomo may think it is installed already $database = $previous->database; $database['host'] = $host; $database['port'] = $port; + if (!empty($socket)) { + $database['unix_socket'] = $socket; + } $database['username'] = DB_USER; $database['password'] = DB_PASSWORD; $database['dbname'] = DB_NAME; diff --git a/matomo.php b/matomo.php index ee7c7245a..47c304bf3 100644 --- a/matomo.php +++ b/matomo.php @@ -4,7 +4,7 @@ * Description: Most powerful web analytics for WordPress giving you 100% data ownership and privacy protection * Author: Matomo * Author URI: https://matomo.org - * Version: 0.2.6 + * Version: 0.2.7 * Domain Path: /languages * WC requires at least: 2.4.0 * WC tested up to: 3.2.6