Skip to content

Commit

Permalink
Improve detection of db host (#91)
Browse files Browse the repository at this point in the history
* Improve detection of db host

* more tweaks for better db handling

* bump version
  • Loading branch information
tsteur authored Nov 17, 2019
1 parent 51aa816 commit b72db01
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 29 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/libs/Zend/Db/Adapter/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -355,7 +357,7 @@ protected function _connect()
$this->_config['password'],
$this->_config['dbname'],
$port,
$socket = null,
$socket,
$enable_ssl ? $flags : null
);

Expand Down
43 changes: 35 additions & 8 deletions classes/WpMatomo/Admin/SystemReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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(),
Expand Down Expand Up @@ -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
Expand Down
26 changes: 15 additions & 11 deletions classes/WpMatomo/Db/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 );
}
}

Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
32 changes: 24 additions & 8 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion matomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b72db01

Please sign in to comment.