Skip to content

Commit

Permalink
Fix filters with date custom fields on PHP 8
Browse files Browse the repository at this point in the history
In PHP < 8.0, comparing a string to 0 evaluates to true when the string
is empty, but starting with 8.0 it returns false [1].

As a result, when BugFilterQuery::build_prop_custom_fields() checks the
filter (`$t_field[0] == CUSTOM_FIELD_DATE_ANY`, line 1470), it fails on
PHP 8.0+.

To fix this behavior, we ensure that a date custom fields's filter type
is always an int:

- When processing it in filter_gpc_get(), use gpc_get_int() instead of
  gpc_get_string() to retrieve the filter's type
- When validating a filter, add special handling for date custom fields
  to ensure that the values (control, timestamp 1 & 2) are treated as
  integers.

Fixes #35291

[1]: https://wiki.php.net/rfc/string_to_number_comparison
  • Loading branch information
dregad committed Feb 23, 2025
1 parent ef2dd09 commit db610bc
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions core/filter_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,24 @@ function filter_ensure_valid_filter( array $p_filter_arr ) {
$p_filter_arr['custom_fields'][$t_cfid],
);
}

$t_checked_array = array();
foreach( $p_filter_arr['custom_fields'][$t_cfid] as $t_filter_value ) {
$t_filter_value = stripslashes( $t_filter_value );
if( ( $t_filter_value === 'any' ) || ( $t_filter_value === '[any]' ) ) {
$t_filter_value = META_FILTER_ANY;

# Special handling for date custom fields which have a special array format
$t_def = custom_field_get_definition( $t_cfid );
if( $t_def['type'] == CUSTOM_FIELD_TYPE_DATE ) {
# All array elements should be numeric
# - 0 is one of the CUSTOM_FIELD_DATE_xxx constants
# - 1 & 2 are unix timestamps
$t_checked_array = array_map( 'intval', $p_filter_arr['custom_fields'][$t_cfid] );
} else {
foreach( $p_filter_arr['custom_fields'][$t_cfid] as $t_filter_value ) {
$t_filter_value = stripslashes( $t_filter_value );
if( ( $t_filter_value === 'any' ) || ( $t_filter_value === '[any]' ) ) {
$t_filter_value = META_FILTER_ANY;
}
$t_checked_array[] = $t_filter_value;
}
$t_checked_array[] = $t_filter_value;
}
$p_filter_arr['custom_fields'][$t_cfid] = $t_checked_array;
}
Expand Down Expand Up @@ -2152,7 +2163,7 @@ function filter_gpc_get( array $p_filter = null ) {
$f_custom_fields_data[$t_cfid] = array();

# Get date control property
$t_control = gpc_get_string( 'custom_field_' . $t_cfid . '_control', null );
$t_control = gpc_get_int( 'custom_field_' . $t_cfid . '_control', null );
$f_custom_fields_data[$t_cfid][0] = $t_control;

$t_one_day = 86399;
Expand Down

0 comments on commit db610bc

Please sign in to comment.