From: Jan Schneider Date: Thu, 11 Mar 2010 10:53:45 +0000 (+0100) Subject: Add parameter to enforce charset conversion and use it to enforce UTF-8 X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=14139ecaf77a83c4e720e3b9b31e0e5e2be5110b;p=horde.git Add parameter to enforce charset conversion and use it to enforce UTF-8 encoded data for JSON serialization. This makes convertToUtf8() obsolete. --- diff --git a/framework/Serialize/lib/Horde/Serialize.php b/framework/Serialize/lib/Horde/Serialize.php index be7a40ae5..29872bd11 100644 --- a/framework/Serialize/lib/Horde/Serialize.php +++ b/framework/Serialize/lib/Horde/Serialize.php @@ -250,7 +250,7 @@ class Horde_Serialize // $params = Source character set case self::JSON: - if (!empty($params) && is_string($data)) { + if (!empty($params)) { $data = Horde_String::convertCharset($data, $params, 'UTF-8'); } $tmp = json_encode($data); @@ -260,7 +260,7 @@ class Horde_Serialize * data. */ if (function_exists('json_last_error') && (json_last_error() == 5)) { - $data = json_encode(Horde_String::convertToUtf8($data)); + $data = json_encode(Horde_String::convertCharset($data, $params, 'UTF-8', true)); } else { $data = $tmp; } diff --git a/framework/Util/lib/Horde/String.php b/framework/Util/lib/Horde/String.php index e926fc30a..d21e361a8 100644 --- a/framework/Util/lib/Horde/String.php +++ b/framework/Util/lib/Horde/String.php @@ -67,7 +67,7 @@ class Horde_String * * @return mixed The converted input data. */ - static public function convertCharset($input, $from, $to = null) + static public function convertCharset($input, $from, $to = null, $force = false) { /* Don't bother converting numbers. */ if (is_numeric($input)) { @@ -80,12 +80,12 @@ class Horde_String } /* If the from and to character sets are identical, return now. */ - if ($from == $to) { + if (!$force && $from == $to) { return $input; } $from = self::lower($from); $to = self::lower($to); - if ($from == $to) { + if (!$force && $from == $to) { return $input; } @@ -93,7 +93,7 @@ class Horde_String $tmp = array(); reset($input); while (list($key, $val) = each($input)) { - $tmp[self::_convertCharset($key, $from, $to)] = self::convertCharset($val, $from, $to); + $tmp[self::_convertCharset($key, $from, $to)] = self::convertCharset($val, $from, $to, $force); } return $tmp; } @@ -107,9 +107,10 @@ class Horde_String Horde::logMessage('Called convertCharset() on a PEAR_Error object. ' . print_r($input, true), __FILE__, __LINE__, PEAR_LOG_DEBUG); return ''; } + $input = Horde_Util::cloneObject($input); $vars = get_object_vars($input); while (list($key, $val) = each($vars)) { - $input->$key = self::convertCharset($val, $from, $to); + $input->$key = self::convertCharset($val, $from, $to, $force); } return $input; } @@ -140,12 +141,12 @@ class Horde_String !Horde_Util::extensionExists('iconv') || !Horde_Util::extensionExists('mbstring'))) { if (($to == 'utf-8') && - in_array($from, array('iso-8859-1', 'us-ascii'))) { + in_array($from, array('iso-8859-1', 'us-ascii', 'utf-8'))) { return utf8_encode($input); } if (($from == 'utf-8') && - in_array($to, array('iso-8859-1', 'us-ascii'))) { + in_array($to, array('iso-8859-1', 'us-ascii', 'utf-8'))) { return utf8_decode($input); } } @@ -737,32 +738,4 @@ class Horde_String return $charset; } - /** - * Convert a variable to UTF-8. Recursively handles inner variables. - * - * @param mixed $in The variable to convert. - * - * @return mixed The converted variable. - */ - static public function convertToUtf8($in) - { - if (is_string($in)) { - if (Horde_Util::extensionExists('xml')) { - $in = utf8_encode($in); - } - } elseif (is_array($in)) { - reset($in); - while (list($key, $val) = each($in)) { - $in[$key] = self::convertToUtf8($in[$key]); - } - } elseif (is_object($in)) { - $in = self::cloneObject($in); - foreach (get_object_vars($in) as $key => $val) { - $in->$key = self::convertToUtf8($in->$key); - } - } - - return $in; - } - }