From fade884e4191c0be52f527499f4bc929571cf4dd Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Mon, 19 May 2014 14:06:41 +0200 Subject: [PATCH] Make sure db prefix / suffix is consistently applied Modifies the db_get_table() and plugin_table() functions to ensure that we build a correct table name based on the user's config settings, e.g. - trim leading/trailing whitespace - do not add underscores if already present Fixes #17355, regression issue introduced by commit 5a84e9db76162e378b9a1b0dea129d1f7ce127e9 where plugin_table() no longer inserts an underscore after $g_db_table_plugin_prefix --- core/database_api.php | 18 +++++++++++------- core/plugin_api.php | 13 +++++-------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/core/database_api.php b/core/database_api.php index 5bbeb6cbc4..619a2c28be 100644 --- a/core/database_api.php +++ b/core/database_api.php @@ -959,16 +959,20 @@ function db_get_table( $p_name ) { $t_table = $p_name; } - $t_prefix = config_get_global( 'db_table_prefix' ); - $t_suffix = config_get_global( 'db_table_suffix' ); - - if( $t_prefix ) { - $t_table = $t_prefix . '_' . $t_table; + # Determine table prefix including trailing '_' + $t_prefix = trim( config_get_global( 'db_table_prefix' ) ); + if( !empty( $t_prefix ) && '_' != substr( $t_prefix, -1 ) ) { + $t_prefix .= '_'; + } + # Determine table suffix including leading '_' + $t_suffix = trim( config_get_global( 'db_table_suffix' ) ); + if( !empty( $t_suffix ) && '_' != substr( $t_suffix, 0, 1 ) ) { + $t_suffix = '_' . $t_suffix; } - $t_table .= $t_suffix; + # Physical table name + $t_table = $t_prefix . $t_table . $t_suffix; db_check_identifier_size( $t_table ); - return $t_table; } diff --git a/core/plugin_api.php b/core/plugin_api.php index 93ce1de11e..530092108b 100644 --- a/core/plugin_api.php +++ b/core/plugin_api.php @@ -233,16 +233,13 @@ function plugin_table( $p_name, $p_basename = null ) { $t_current = $p_basename; } - $t_table_name = config_get_global( 'db_table_prefix' ); - if( !empty( $t_table_name ) ) { - $t_table_name .= '_'; + # Determine plugin table prefix including trailing '_' + $t_prefix = trim( config_get_global( 'db_table_plugin_prefix' ) ); + if( !empty( $t_prefix ) && '_' != substr( $t_prefix, -1 ) ) { + $t_prefix .= '_'; } - $t_table_name .= - config_get_global( 'db_table_plugin_prefix' ) . - $t_current . '_' . $p_name . - config_get_global( 'db_table_suffix' ); - return $t_table_name; + return db_get_table( $t_prefix . $t_current . '_' . $p_name ); } /**