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 933356710f0..cd43addb126 100644
--- a/lib/weblib.php
+++ b/lib/weblib.php
@@ -2594,19 +2594,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++;