8000 [1813] Fix errors when URL parameters like "filter[status]" aren't strings by pedro-mendonca · Pull Request #1814 · GlotPress/GlotPress · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[1813] Fix errors when URL parameters like "filter[status]" aren't strings #1814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 27, 2024

Conversation

pedro-mendonca
Copy link
Member
@pedro-mendonca pedro-mendonca commented Mar 15, 2024

Problem

If the translations filter in the URL parameter sends an array, for example ...?filters[status][0]=current&filters[status][1]=fuzzy, the gp_array_get( $filters, 'status', 'current_or_waiting_or_fuzzy_or_untranslated' ) will return an array instead of a string.
This errors on subsequent explode( '_or_', $selected_status ).

Solution

If the statuses set on URL get parameters are not the predictable ones, fallback to the current_or_waiting_or_fuzzy_or_untranslated an empty string.

Testing Instructions

  1. Open a translation set.
  2. Change the default ...?filters[status]=current_or_fuzzy URL parameters to ...?filters[status][0]=current&filters[status][1]=fuzzy
  3. See the errors.

Fixes #1813

@pedro-mendonca pedro-mendonca requested a review from amieiro March 15, 2024 11:42
@amieiro
Copy link
Member
amieiro commented Mar 21, 2024

I am testing this PR in 2 installations, one with PHP7.4 and another with PHP8.0. In PHP7.4 I get warnings, but in PHP8.0 I get
fatal errors.

I am using this GET string to test this PR:

?filters%5Bstatus%5D=untranslated&filters%5Bstatus%5D%5Bwaiting%5D=fc7e06db

I suggest one new check, to resolve some notices I get with PHP7.4:

PHP Notice:  Array to string conversion in wp-content/plugins/GlotPress/gp-templates/translations.php on line 135
PHP Notice:  Array to string conversion in wp-content/plugins/GlotPress/gp-templates/translations.php on line 149
PHP Notice:  Array to string conversion in wp-content/plugins/GlotPress/gp-templates/translations.php on line 163
PHP Notice:  Array to string conversion in wp-content/plugins/GlotPress/gp-templates/translations.php on line 177
PHP Notice:  Array to string conversion in wp-content/plugins/GlotPress/gp-templates/translations.php on line 191
PHP Notice:  Array to string conversion in wp-content/plugins/GlotPress/gp-templates/translations.php on line 207
PHP Notice:  Array to string conversion in wp-content/plugins/GlotPress/gp-templates/translations.php on line 221

In the gp-templates/translations.php we need to filter the values in the $filters_values_only that are not strings. I suggest to change

		$filters_values_only = array_filter( $filters );

to

		$filters_values_only = array_map(function($value) {
			return is_string($value) ? $value : '';
		}, $filters_values_only);

@pedro-mendonca
Copy link
Member Author

The problem is more vast, unfortunately not only the key $filters[status] has this problem.
The same problem can easily occur with both $filters array and $sort since gp_get( 'filters' ) and gp_get( 'sort' ) in translations.php route.

To fix it, I propose sanitize it immediatly after the gp_get().

@pedro-mendonca pedro-mendonca changed the title [1813] Fix errors when URL parameter "filter[status]" isn't a string [1813] Fix errors when URL parameters like "filter[status]" asn't strings Mar 22, 2024
@pedro-mendonca pedro-mendonca changed the title [1813] Fix errors when URL parameters like "filter[status]" asn't strings [1813] Fix errors when URL parameters like "filter[status]" aren't strings Mar 22, 2024
@pedro-mendonca
Copy link
Member Author

In 1e294de I've updated the check for the int values sent through gp_get() with is_numeric().

@amieiro amieiro merged commit b100e73 into GlotPress:develop Mar 27, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cascading warnings from input on array functions
2 participants
0