Skip to content

Commit 36a3446

Browse files
committedSep 9, 2011
Fix #13280: Incorrect queries logged when sorting by custom field having accented chars
The offsets returned by preg_match are byte-based, which causes issues with UTF-8 characters as the subsequent calls to utf8_substr operate on the wrong part of the string; this results in logging of invalid SQL queries when $g_show_queries_list = ON. This commit fixes the problem by realigning the offsets prior to performing the query parameter substitution. It also simplifies the code by removing parenthesis in the regexp pattern, which is not necessary since we are only matching a single element, this way there is no need to deal with a multi-dimentional array.
1 parent 94e6810 commit 36a3446

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed
 

‎core/database_api.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,12 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
322322
$lastoffset = 0;
323323
$i = 1;
324324
if( !( is_null( $arr_parms ) || empty( $arr_parms ) ) ) {
325-
while( preg_match( '/(\?)/', $p_query, $matches, PREG_OFFSET_CAPTURE, $lastoffset ) ) {
325+
while( preg_match( '/\?/', $p_query, $matches, PREG_OFFSET_CAPTURE, $lastoffset ) ) {
326+
$matches = $matches[0];
327+
# Realign the offset returned by preg_match as it is byte-based,
328+
# which causes issues with UTF-8 characters in the query string
329+
# (e.g. from custom fields names)
330+
$matches[1] = utf8_strlen( substr( $p_query, 0, $matches[1]), mb_internal_encoding() );
326331
if( $i <= count( $arr_parms ) ) {
327332
if( is_null( $arr_parms[$i - 1] ) ) {
328333
$replace = 'NULL';
@@ -346,10 +351,10 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
346351
echo( "Invalid argument type passed to query_bound(): $i" );
347352
exit( 1 );
348353
}
349-
$p_query = utf8_substr( $p_query, 0, $matches[1][1] ) . $replace . utf8_substr( $p_query, $matches[1][1] + utf8_strlen( $matches[1][0] ) );
350-
$lastoffset = $matches[1][1] + utf8_strlen( $replace );
354+
$p_query = utf8_substr( $p_query, 0, $matches[1] ) . $replace . utf8_substr( $p_query, $matches[1] + utf8_strlen( $matches[0] ) );
355+
$lastoffset = $matches[1] + utf8_strlen( $replace );
351356
} else {
352-
$lastoffset = $matches[1][1] + 1;
357+
$lastoffset = $matches[1] + 1;
353358
}
354359
$i++;
355360
}

0 commit comments

Comments
 (0)
Please sign in to comment.