Skip to content
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

Fix possible json encoding error #13823

Merged
merged 3 commits into from Dec 11, 2018
Merged

Fix possible json encoding error #13823

merged 3 commits into from Dec 11, 2018

Conversation

tsteur
Copy link
Member

@tsteur tsteur commented Dec 11, 2018

some API's in tests don't return a response cause they fail to json_encode because of Malformed UTF-8 characters, possibly incorrectly encoded. Tested this fix and it worked for me. Would maybe also need to create a general JSON class for this that always takes care of this in case we have same problem somewhere else? Not sure how good a fix it is as the input might not be utf8?

In test case there was eg /page/index.htm?q=non unicode keyword ��������";s:7:"segment";s:103:"pageUrl==http%3A%2F%2Fexample.org%2Fpage%2Findex.htm%3Fq%3Dnon+unicode+keyword+%EC%E5%F8%EA%EE%E2%FB%E5";}i and it was converted to ??????

some API's in tests don't return a response cause they fail to json_encode because of `Malformed UTF-8 characters, possibly incorrectly encoded`. Tested this fix and it worked for me. Would maybe also need to create a general JSON class for this that always takes care of this in case we have same problem somewhere else? Not sure how good a fix it is as the input might not be utf8?

In test case there was eg `/page/index.htm?q=non unicode keyword ��������";s:7:"segment";s:103:"pageUrl==http%3A%2F%2Fexample.org%2Fpage%2Findex.htm%3Fq%3Dnon+unicode+keyword+%EC%E5%F8%EA%EE%E2%FB%E5";}i` and it was converted to `??????`
@tsteur tsteur added not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. Needs Review PRs that need a code review and removed not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. labels Dec 11, 2018
@tsteur tsteur added this to the 3.8.0 milestone Dec 11, 2018
$array[$key] = self::makeArrayUtf8($value);
}
} elseif (is_string($array)) {
return mb_convert_encoding($array, 'UTF-8', 'UTF-8');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe return mb_convert_encoding($array, 'UTF-8', 'auto'); is better?

@@ -73,9 +73,30 @@ protected function renderTable($table)
// silence "Warning: json_encode(): Invalid UTF-8 sequence in argument"
$str = @json_encode($array);

if ($str === false && json_last_error() === JSON_ERROR_UTF8) {
$str = @json_encode(self::makeArrayUtf8($array));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we shouldn't silence this second function call? Just so any other issues won't be silenced.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense 👍

@tsteur
Copy link
Member Author

tsteur commented Dec 11, 2018

fyi we use slightly different version in https://github.com/matomo-org/matomo/blame/3.x-dev/core/DataTable/Renderer/Csv.php#L477

but same version for https://github.com/matomo-org/matomo/blame/3.x-dev/core/DataTable/Renderer.php#L188-L193

not sure if any is better... the one in data table renderer should be fine though I suppose

@diosmosis
Copy link
Member

Tests are passing, feel free to merge (not sure if you want to change to use the DataTable\Renderer code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review PRs that need a code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants