From 66056f994f8a878dee00c9e21bf211cc05c76430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Fri, 7 Jun 2013 22:38:48 +0200 Subject: [PATCH] MDL-39096 fix obfuscate_text() utf-8 compatibility --- lib/tests/weblib_test.php | 16 ++++++++++++++++ lib/weblib.php | 13 +++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/tests/weblib_test.php b/lib/tests/weblib_test.php index 74362a4a64e..8ba4a4d5255 100644 --- a/lib/tests/weblib_test.php +++ b/lib/tests/weblib_test.php @@ -106,6 +106,22 @@ class web_testcase extends advanced_testcase { format_text_email('習習',FORMAT_HTML)); } + function test_obfuscate_email() { + $email = 'some.user@example.com'; + $obfuscated = obfuscate_email($email); + $this->assertNotSame($email, $obfuscated); + $back = textlib::entities_to_utf8(urldecode($email), true); + $this->assertSame($email, $back); + } + + function test_obfuscate_text() { + $text = 'Žluťoučký koníček 32131'; + $obfuscated = obfuscate_text($text); + $this->assertNotSame($text, $obfuscated); + $back = textlib::entities_to_utf8($obfuscated, true); + $this->assertSame($text, $back); + } + function test_highlight() { $this->assertEquals(highlight('good', 'This is good'), 'This is good'); $this->assertEquals(highlight('SpaN', 'span'), 'span'); diff --git a/lib/weblib.php b/lib/weblib.php index 1985b7ec7f7..dd9e8ba8a28 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2577,19 +2577,20 @@ function redirect($url, $message='', $delay=-1) { function obfuscate_text($plaintext) { $i=0; - $length = strlen($plaintext); + $length = textlib::strlen($plaintext); $obfuscated=''; $prev_obfuscated = false; while ($i < $length) { - $c = ord($plaintext{$i}); - $numerical = ($c >= ord('0')) && ($c <= ord('9')); + $char = textlib::substr($plaintext, $i, 1); + $ord = textlib::utf8ord($char); + $numerical = ($ord >= ord('0')) && ($ord <= ord('9')); if ($prev_obfuscated and $numerical ) { - $obfuscated.='&#'.ord($plaintext{$i}).';'; + $obfuscated.='&#'.$ord.';'; } else if (rand(0,2)) { - $obfuscated.='&#'.ord($plaintext{$i}).';'; + $obfuscated.='&#'.$ord.';'; $prev_obfuscated = true; } else { - $obfuscated.=$plaintext{$i}; + $obfuscated.=$char; $prev_obfuscated = false; } $i++;