From e68280064ea55528359d6738d9942062b3d7d554 Mon Sep 17 00:00:00 2001
From: Steven Wittens <steven@10.no-reply.drupal.org>
Date: Thu, 3 Mar 2005 20:06:42 +0000
Subject: [PATCH] - #18319: Move encoding conversion out of
 drupal_xml_parser_create() so it can be used by modules.

---
 includes/common.inc | 43 +++++++++++++++++++++++++++++--------------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index e63271779ea9..ce95baa39930 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -1643,20 +1643,7 @@ function drupal_xml_parser_create(&$data) {
   // Requires the iconv, GNU recode or mbstring PHP extension.
   $php_supported = array('utf-8', 'iso-8859-1', 'us-ascii');
   if (!in_array(strtolower($encoding), $php_supported)) {
-    if (function_exists('iconv')) {
-      $out = @iconv($encoding, 'utf-8', $data);
-    }
-    else if (function_exists('mb_convert_encoding')) {
-      $out = @mb_convert_encoding($data, 'utf-8', $encoding);
-    }
-    else if (function_exists('recode_string')) {
-      $out = @recode_string($encoding . '..utf-8', $data);
-    }
-    else {
-      watchdog('php', t("Unsupported XML encoding '%s'. Please install iconv, GNU recode or mbstring for PHP.", $encoding), WATCHDOG_ERROR);
-      return 0;
-    }
-
+    $out = drupal_convert_to_utf8($data, $encoding);
     if ($out !== false) {
       $data = $out;
       $encoding = 'utf-8';
@@ -1672,6 +1659,34 @@ function drupal_xml_parser_create(&$data) {
   return $xml_parser;
 }
 
+/**
+ * Convert data to UTF-8
+ *
+ * @param $data
+ *   The data to be converted.
+ * @param $encoding
+ *   The encoding that the data is in
+ * @return
+ *   Converted data or FALSE.
+ */
+function drupal_convert_to_utf8($data, $encoding) {
+  if (function_exists('iconv')) {
+    $out = @iconv($encoding, 'utf-8', $data);
+  }
+  else if (function_exists('mb_convert_encoding')) {
+    $out = @mb_convert_encoding($data, 'utf-8', $encoding);
+  }
+  else if (function_exists('recode_string')) {
+    $out = @recode_string($encoding . '..utf-8', $data);
+  }
+  else {
+    watchdog('php', t("Unsupported encoding '%s'. Please install iconv, GNU recode or mbstring for PHP.", $encoding), WATCHDOG_ERROR);
+    return FALSE;
+  }
+
+  return $out;
+}
+
 /**
  * Truncate a UTF-8-encoded string safely.
  *
-- 
GitLab