Skip to content

Replace utf8_encode() removed in PHP 8.2#433

Merged
dregad merged 1 commit into
mantisbt-plugins:masterfrom
khushalVekariya:fix/bitbucket-php82-utf8-encode-removal
May 14, 2026
Merged

Replace utf8_encode() removed in PHP 8.2#433
dregad merged 1 commit into
mantisbt-plugins:masterfrom
khushalVekariya:fix/bitbucket-php82-utf8-encode-removal

Conversation

@khushalVekariya
Copy link
Copy Markdown
Contributor

Summary

  • utf8_encode() was deprecated in PHP 8.2 and removed in later versions, causing a fatal error when the BitBucket integration calls api_json_url() on modern PHP.
  • Replaces the call in SourceBitBucketPlugin::api_json_url() with mb_convert_encoding($data, 'UTF-8', 'ISO-8859-1'), which the PHP manual lists as the direct equivalent.
  • No behavioural change on PHP < 8.2: the conversion semantics (ISO-8859-1 → UTF-8) are identical.

Why

MantisBT plugins targeting current PHP releases were breaking when BitBucket returned non-ASCII bytes (commit messages, author names, etc.) because utf8_encode() no longer exists.

Copy link
Copy Markdown
Member

@dregad dregad left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution @khushalVekariya.

This usage of utf8_encode() is present in the code since the initial contribution of the BitBucket plugin by @lazar2606 back in 2014 (see #98). I have to admit that I never really looked at that closely until now, as I do not use BitBucket myself.

Now that I did, I must say it actually feels wrong to call utf8_encode() here, because the JSON specification states that the data should already be UTF-8.

In fact, that reminds me of a case we had in MantisBT's JSON API many years ago, where calling utf8_encode() before sending content to json_decode() caused problems with Japanese strings (see #20350.

So I believe that the correct fix would be to remove the utf8_encode() call entirely:

-               $t_json = json_decode( utf8_encode( $t_data ) );
+               $t_json = json_decode( $t_data );

Could you test on your end and confirm it works without regressions ?

@khushalVekariya
Copy link
Copy Markdown
Contributor Author

Thanks for your contribution @khushalVekariya.

This usage of utf8_encode() is present in the code since the initial contribution of the BitBucket plugin by @lazar2606 back in 2014 (see #98). I have to admit that I never really looked at that closely until now, as I do not use BitBucket myself.

Now that I did, I must say it actually feels wrong to call utf8_encode() here, because the JSON specification states that the data should already be UTF-8.

In fact, that reminds me of a case we had in MantisBT's JSON API many years ago, where calling utf8_encode() before sending content to json_decode() caused problems with Japanese strings (see #20350.

So I believe that the correct fix would be to remove the utf8_encode() call entirely:

-               $t_json = json_decode( utf8_encode( $t_data ) );
+               $t_json = json_decode( $t_data );

Could you test on your end and confirm it works without regressions ?

@dregad I don't use BitBucket myself — this PR was just aimed at fixing the PHP 8.2 compatibility violation, so I kept the behavior identical with mb_convert_encoding() as a drop-in replacement.

Your point about RFC 7159 and the MantisBT #20350 precedent makes sense, but since removing utf8_encode() is a behavioral change (not just a compat fix) and I can't test it against BitBucket, I'd suggest tracking that as a separate issue.

@dregad
Copy link
Copy Markdown
Member

dregad commented May 14, 2026

tracking that as a separate issue

Just created #434 for this.

@dregad dregad merged commit 074401f into mantisbt-plugins:master May 14, 2026
dregad added a commit that referenced this pull request May 14, 2026
Per JSON specification [1], data should already be UTF-8.

Similar code was already changed in the same way in MantisBT a few years
ago, see issue #20350 [2]

Fixes #434, follow-up on #433

[1]: https://www.rfc-editor.org/rfc/rfc7159#section-8.1
[2]: https://mantisbt.org/bugs/view.php?id=20350
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.

2 participants