Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/08/2014 02:14:29 PM (12 years ago)
Author:
boonebgorges
Message:

Better detection for false positives in meta SQL filters.

Our wrappers for WP's _metadata() functions require some filtering of the SQL
string (to change a column name and, in the case of xprofile, to add an
'object_type' clause). Our str_replace() logic is too generous, creating the
possibility of matching quoted text, as when the meta value contains the string
'WHERE'.

This changeset modifies the filters so that quoted content is swapped out with
placeholders before we run our search-and-replace.

See #5919.
Props tometzky for feedback.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-filters.php

    r9001 r9073  
    604604 */
    605605function bp_filter_metaid_column_name( $q ) {
    606         return str_replace( 'meta_id', 'id', $q );
     606        /*
     607         * Replace quoted content with __QUOTE__ to avoid false positives.
     608         * This regular expression will match nested quotes.
     609         */
     610        $quoted_regex = "/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'/s";
     611        preg_match_all( $quoted_regex, $q, $quoted_matches );
     612        $q = preg_replace( $quoted_regex, '__QUOTE__', $q );
     613
     614        $q = str_replace( 'meta_id', 'id', $q );
     615
     616        // Put quoted content back into the string.
     617        if ( ! empty( $quoted_matches[0] ) ) {
     618                for ( $i = 0; $i < count( $quoted_matches[0] ); $i++ ) {
     619                        $quote_pos = strpos( $q, '__QUOTE__' );
     620                        $q = substr_replace( $q, $quoted_matches[0][ $i ], $quote_pos, 9 );
     621                }
     622        }
     623
     624        return $q;
    607625}
    608626
Note: See TracChangeset for help on using the changeset viewer.