Skip to content

Commit

Permalink
Merge branch 'w24_MDL-39096_m25_obfuscate' of git://github.com/skodak…
Browse files Browse the repository at this point in the history
…/moodle into MOODLE_25_STABLE
  • Loading branch information
danpoltawski committed Jun 10, 2013
2 parents 9d1a909 + 06de509 commit 7f9f034
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 16 additions & 0 deletions lib/tests/weblib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ function test_format_text_email() {
format_text_email('習習',FORMAT_HTML));
}

function test_obfuscate_email() {
$email = '[email protected]';
$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 <span class="highlight">good</span>');
$this->assertEquals(highlight('SpaN', 'span'), '<span class="highlight">span</span>');
Expand Down
13 changes: 7 additions & 6 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2594,19 +2594,20 @@ function obfuscate_email($email) {
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++;
Expand Down

0 comments on commit 7f9f034

Please sign in to comment.