Make WordPress Core


Ignore:
Timestamp:
04/20/2015 05:41:37 AM (10 years ago)
Author:
pento
Message:

Clean up some edge cases in sanitize_sql_orderby().

Props vortfu, dd32.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r32161 r32164  
    13631363
    13641364/**
    1365  * Ensures a string is a valid SQL order by clause.
    1366  *
    1367  * Accepts one or more columns, with or without ASC/DESC, and also accepts
    1368  * RAND().
     1365 * Ensures a string is a valid SQL 'order by' clause.
     1366 *
     1367 * Accepts one or more columns, with or without a sort order (ASC / DESC).
     1368 * e.g. 'column_1', 'column_1, column_2', 'column_1 ASC, column_2 DESC' etc.
     1369 *
     1370 * Also accepts 'RAND()'.
    13691371 *
    13701372 * @since 2.5.1
    13711373 *
    1372  * @param string $orderby Order by string to be checked.
    1373  * @return false|string Returns the order by clause if it is a match, false otherwise.
    1374  */
    1375 function sanitize_sql_orderby( $orderby ){
    1376     preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches);
    1377     if ( !$obmatches )
    1378         return false;
    1379     return $orderby;
     1374 * @param string $orderby Order by clause to be validated.
     1375 * @return string|bool Returns $orderby if valid, false otherwise.
     1376 */
     1377function sanitize_sql_orderby( $orderby ) {
     1378    if ( preg_match( '/^\s*(([a-z0-9_]+|`[a-z0-9_]+`)(\s+(ASC|DESC))?\s*(,\s*(?=[a-z0-9_`])|$))+$/i', $orderby ) || preg_match( '/^\s*RAND\(\s*\)\s*$/i', $orderby ) ) {
     1379        return $orderby;
     1380    }
     1381    return false;
    13801382}
    13811383
Note: See TracChangeset for help on using the changeset viewer.