Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5835572

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 e679a1c commit 5835572

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
@@ -346,7 +346,12 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
346346
$lastoffset = 0;
347347
$i = 1;
348348
if( !( is_null( $arr_parms ) || empty( $arr_parms ) ) ) {
349-
while( preg_match( '/(\?)/', $p_query, $matches, PREG_OFFSET_CAPTURE, $lastoffset ) ) {
349+
while( preg_match( '/\?/', $p_query, $matches, PREG_OFFSET_CAPTURE, $lastoffset ) ) {
350+
$matches = $matches[0];
351+
# Realign the offset returned by preg_match as it is byte-based,
352+
# which causes issues with UTF-8 characters in the query string
353+
# (e.g. from custom fields names)
354+
$matches[1] = utf8_strlen( substr( $p_query, 0, $matches[1]), mb_internal_encoding() );
350355
if( $i <= count( $arr_parms ) ) {
351356
if( is_null( $arr_parms[$i - 1] ) ) {
352357
$replace = 'NULL';
@@ -370,10 +375,10 @@ function db_query_bound( $p_query, $arr_parms = null, $p_limit = -1, $p_offset =
370375
echo( "Invalid argument type passed to query_bound(): $i" );
371376
exit( 1 );
372377
}
373-
$p_query = utf8_substr( $p_query, 0, $matches[1][1] ) . $replace . utf8_substr( $p_query, $matches[1][1] + utf8_strlen( $matches[1][0] ) );
374-
$lastoffset = $matches[1][1] + utf8_strlen( $replace );
378+
$p_query = utf8_substr( $p_query, 0, $matches[1] ) . $replace . utf8_substr( $p_query, $matches[1] + utf8_strlen( $matches[0] ) );
379+
$lastoffset = $matches[1] + utf8_strlen( $replace );
375380
} else {
376-
$lastoffset = $matches[1][1] + 1;
381+
$lastoffset = $matches[1] + 1;
377382
}
378383
$i++;
379384
}

0 commit comments

Comments
 (0)
Please sign in to comment.