From 78a1961d0c6835453e180e67e86117a371282874 Mon Sep 17 00:00:00 2001 From: gggeek Date: Mon, 12 Dec 2022 11:22:09 +0000 Subject: [PATCH] prefer not to use utf8_encode and utf8_decode --- src/Encoder.php | 8 ++++---- src/Helper/Charset.php | 1 + src/Request.php | 9 ++++----- src/Server.php | 8 ++++---- tests/0CharsetTest.php | 6 ++++-- tests/2MessageTest.php | 7 ++++--- tests/5ServerTest.php | 3 ++- 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Encoder.php b/src/Encoder.php index 9b1d2cea..e7a6bc0c 100644 --- a/src/Encoder.php +++ b/src/Encoder.php @@ -304,11 +304,11 @@ class Encoder // The following code might be better for mb_string enabled installs, but makes the lib about 200% slower... //if (!is_valid_charset($valEncoding, array('UTF-8')) if (!in_array($valEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($xmlVal)) { - if ($valEncoding == 'ISO-8859-1') { - $xmlVal = utf8_encode($xmlVal); + if (extension_loaded('mbstring')) { + $xmlVal = mb_convert_encoding($xmlVal, 'UTF-8', $valEncoding); } else { - if (extension_loaded('mbstring')) { - $xmlVal = mb_convert_encoding($xmlVal, 'UTF-8', $valEncoding); + if ($valEncoding == 'ISO-8859-1') { + $xmlVal = utf8_encode($xmlVal); } else { $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of xml text: ' . $valEncoding); } diff --git a/src/Helper/Charset.php b/src/Helper/Charset.php index 45cb3d34..820d85b6 100644 --- a/src/Helper/Charset.php +++ b/src/Helper/Charset.php @@ -235,6 +235,7 @@ class Charset case 'ISO-8859-1_UTF-8': $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + /// @todo if on php >= 8.2, prefer using mbstring or iconv $escapedData = utf8_encode($escapedData); break; diff --git a/src/Request.php b/src/Request.php index 7eb124ff..c946bdac 100644 --- a/src/Request.php +++ b/src/Request.php @@ -309,12 +309,11 @@ class Request // The following code might be better for mb_string enabled installs, but makes the lib about 200% slower... //if (!is_valid_charset($respEncoding, array('UTF-8'))) if (!in_array($respEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($data)) { - if ($respEncoding == 'ISO-8859-1') { - $data = utf8_encode($data); + if (extension_loaded('mbstring')) { + $data = mb_convert_encoding($data, 'UTF-8', $respEncoding); } else { - - if (extension_loaded('mbstring')) { - $data = mb_convert_encoding($data, 'UTF-8', $respEncoding); + if ($respEncoding == 'ISO-8859-1') { + $data = utf8_encode($data); } else { $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received response: ' . $respEncoding); } diff --git a/src/Server.php b/src/Server.php index 6c24b0a2..587fa412 100644 --- a/src/Server.php +++ b/src/Server.php @@ -558,11 +558,11 @@ class Server // makes the lib about 200% slower... //if (!is_valid_charset($reqEncoding, array('UTF-8'))) if (!in_array($reqEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($data)) { - if ($reqEncoding == 'ISO-8859-1') { - $data = utf8_encode($data); + if (extension_loaded('mbstring')) { + $data = mb_convert_encoding($data, 'UTF-8', $reqEncoding); } else { - if (extension_loaded('mbstring')) { - $data = mb_convert_encoding($data, 'UTF-8', $reqEncoding); + if ($reqEncoding == 'ISO-8859-1') { + $data = utf8_encode($data); } else { $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $reqEncoding); } diff --git a/tests/0CharsetTest.php b/tests/0CharsetTest.php index a5789118..7e5c006a 100644 --- a/tests/0CharsetTest.php +++ b/tests/0CharsetTest.php @@ -67,7 +67,8 @@ class CharsetTest extends PhpXmlRpc_PolyfillTestCase mb_detect_encoding($this->latinString, 'ISO-8859-1, UTF-8, WINDOWS-1251, ASCII', true), 'Setup latinString is not ISO-8859-1 encoded...' );*/ - $string = utf8_encode($this->latinString); + // the warning suppression is due to utf8_encode being deprecated in php 8.2 + $string = @utf8_encode($this->latinString); $encoded = $this->utf8ToLatin1($string); $this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $this->latinString), $encoded); } @@ -76,7 +77,8 @@ class CharsetTest extends PhpXmlRpc_PolyfillTestCase { $string = 'a.b.c.Ã¥.ä.ö.€.'; $encoded = $this->utf8ToLatin1($string); - $this->assertEquals(utf8_decode('a.b.c.Ã¥.ä.ö.€.'), $encoded); + // the warning suppression is due to utf8_decode being deprecated in php 8.2 + $this->assertEquals(@utf8_decode('a.b.c.Ã¥.ä.ö.€.'), $encoded); } public function testUtf8ToLatin1Runes() diff --git a/tests/2MessageTest.php b/tests/2MessageTest.php index bb7461a9..4f76804c 100644 --- a/tests/2MessageTest.php +++ b/tests/2MessageTest.php @@ -145,7 +145,8 @@ class MessageTests extends PhpXmlRpc_PolyfillTestCase public function testUnicodeInErrorString() { - $response = utf8_encode( + // the warning suppression is due to utf8_decode being deprecated in php 8.2 + $response = @utf8_encode( ' @@ -413,7 +414,7 @@ and there they were.postid7414222newMsg('dummy'); $f = "HTTP/1.1 200 OK\r\nContent-type: text/xml; charset=UTF-8\r\n\r\n" . 'userid311127 -dateCreated20011126T09:17:52content' . utf8_encode($string) . 'postid7414222 +dateCreated20011126T09:17:52content' . @utf8_encode($string) . 'postid7414222 '; $r = $s->parseResponse($f, false, 'phpvals'); $v = $r->value(); @@ -421,7 +422,7 @@ and there they were.postid7414222assertEquals($string, $v); $f = 'userid311127 -dateCreated20011126T09:17:52content' . utf8_encode($string) . 'postid7414222 +dateCreated20011126T09:17:52content' . @utf8_encode($string) . 'postid7414222 '; $r = $s->parseResponse($f, false, 'phpvals'); $v = $r->value(); diff --git a/tests/5ServerTest.php b/tests/5ServerTest.php index f91b41aa..cf164efd 100644 --- a/tests/5ServerTest.php +++ b/tests/5ServerTest.php @@ -283,7 +283,8 @@ class ServerTest extends PhpXmlRpc_PolyfillTestCase $this->markTestSkipped('Miss mbstring extension to test exotic charsets'); return; } - $sendString = utf8_decode('élève'); + // the warning suppression is due to utf8_decode being deprecated in php 8.2 + $sendString = @utf8_decode('élève'); $str = ' examples.stringecho -- 2.47.0