Permalink
Browse files

Merge branch 'MDL-33353_22' of git://github.com/stronk7/moodle into M…

…OODLE_22_STABLE
  • Loading branch information...
2 parents 9331c1e + b71e34d commit 6a27fc8b341d77ca488c34a834ec0cacd7d40623 Sam Hemelryk committed Jun 5, 2012
Showing with 40 additions and 2 deletions.
  1. +38 −1 lib/moodlelib.php
  2. +2 −1 lib/simpletest/testmoodlelib.php
View
@@ -1127,7 +1127,44 @@ function fix_utf8($value) {
// shortcut
return $value;
}
- return iconv('UTF-8', 'UTF-8//IGNORE', $value);
+
+ // Note: This is a partial backport of MDL-32586 and MDL-33007 to stable branches.
+ // Lower error reporting because glibc throws bogus notices.
+ $olderror = error_reporting();
+ if ($olderror & E_NOTICE) {
+ error_reporting($olderror ^ E_NOTICE);
+ }
+
+ // Detect buggy iconv implementations borking results.
+ static $buggyiconv = null;
+ if ($buggyiconv === null) {
+ $buggyiconv = (!function_exists('iconv') or iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'') !== '100€');
+ }
+
+ if ($buggyiconv) {
+ if (function_exists('mb_convert_encoding')) {
+ // Fallback to mbstring if available.
+ $subst = mb_substitute_character();
+ mb_substitute_character('');
+ $result = mb_convert_encoding($value, 'utf-8', 'utf-8');
+ mb_substitute_character($subst);
+
+ } else {
+ // Return unmodified text, mbstring not available.
+ $result = $value;
+ }
+
+ } else {
+ // Working iconv, use it normally (with PHP notices disabled)
+ $result = iconv('UTF-8', 'UTF-8//IGNORE', $value);
+ }
+
+ // Back to original reporting level
+ if ($olderror & E_NOTICE) {
+ error_reporting($olderror);
+ }
+
+ return $result;
} else if (is_array($value)) {
foreach ($value as $k=>$v) {
@@ -311,6 +311,7 @@ function test_fix_utf8() {
$this->assertidentical(1, fix_utf8(1));
$this->assertidentical(1.1, fix_utf8(1.1));
$this->assertidentical(true, fix_utf8(true));
+ $this->assertidentical('abc', fix_utf8('abc'));
$this->assertidentical('', fix_utf8(''));
$array = array('do', 're', 'mi');
$this->assertidentical($array, fix_utf8($array));
@@ -323,7 +324,7 @@ function test_fix_utf8() {
$this->assertidentical("žlutý koníček přeskočil potůček \n\t\r\0", fix_utf8("žlutý koníček přeskočil potůček \n\t\r\0"));
// invalid utf8 string
- $this->assertidentical('aaabbb', fix_utf8('aaa'.chr(130).'bbb'));
+ $this->assertidentical('', fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.');
}
function test_optional_param() {

0 comments on commit 6a27fc8

Please sign in to comment.