[icinga-checkins] icinga.org: icinga-web/master: Added additional utf8 detection and encoding to simpledataprovider ( fixes #2940)

git at icinga.org git at icinga.org
Wed Oct 10 12:06:23 CEST 2012


Module: icinga-web
Branch: master
Commit: 6219670790892a58a540d48cc99605095cbc54d7
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=6219670790892a58a540d48cc99605095cbc54d7

Author: Jannis Mosshammer <jannis.mosshammer at netways.de>
Date:   Wed Oct 10 12:05:15 2012 +0200

Added additional utf8 detection and encoding to simpledataprovider (fixes #2940)

---

 .../AppKit/lib/util/AppKitArrayUtil.class.php      |   27 +++++++++++++
 .../AppKit/lib/util/AppKitStringUtil.class.php     |   40 ++++++++++++++++++++
 .../ApiSimpleDataProviderSuccessView.class.php     |    5 +-
 3 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/app/modules/AppKit/lib/util/AppKitArrayUtil.class.php b/app/modules/AppKit/lib/util/AppKitArrayUtil.class.php
old mode 100644
new mode 100755
index d01a0fe..f058aca
--- a/app/modules/AppKit/lib/util/AppKitArrayUtil.class.php
+++ b/app/modules/AppKit/lib/util/AppKitArrayUtil.class.php
@@ -283,6 +283,33 @@ class AppKitArrayUtil {
     public static function matchAgainstStringList($list, $match, $split_char=',') {
         return in_array($match, self::trimSplit($list, $split_char));
     }
+    
+    /**
+     * Converts an array containing ISO-8859-1 string to utf-8
+     * 
+     * @param array $obj
+     */
+    public static function toUTF8_recursive(array &$obj) {
+        foreach($obj as $field=>&$value) {
+            if(is_string($value) && !AppKitStringUtil::isUTF8($value))
+                $value = utf8_encode($value);
+            else if(is_array($value))
+                AppKitArrayUtil::toUTF8_recursive($value);
+        }
+    }
+    /**
+     * Converts an array containing ISO-8859-1 string to utf-8
+     * 
+     * @param array $obj
+     */
+    public static function toISO_recursive(array &$obj) {
+        foreach($obj as $field=>&$value) {
+            if(is_string($value))
+                $value = utf8_decode($value);
+            else if(is_array($value))
+                AppKitArrayUtil::toISO_recursive($value);
+        }
+    }
 }
 
 class AppKitArrayUtilException extends AppKitException {}
diff --git a/app/modules/AppKit/lib/util/AppKitStringUtil.class.php b/app/modules/AppKit/lib/util/AppKitStringUtil.class.php
old mode 100644
new mode 100755
index 637ac70..c0f8a4f
--- a/app/modules/AppKit/lib/util/AppKitStringUtil.class.php
+++ b/app/modules/AppKit/lib/util/AppKitStringUtil.class.php
@@ -123,5 +123,45 @@ class AppKitStringUtil {
     public static function detectUrl($url) {
         return preg_match('@^[a-z]{3,8}://.+$@', $url);
     }
+    
+    /**
+    * Checks to see if a string is utf8 encoded.
+    *
+    * NOTE: This function checks for 5-Byte sequences, UTF8
+    *       has Bytes Sequences with a maximum length of 4.
+    *
+    * @author bmorel at ssi dot fr (modified)
+    * @since 1.2.1
+    *
+    * @param string $str The string to be checked
+    * @return bool True if $str fits a UTF-8 model, false otherwise.
+     * 
+     * Taken from wordpress: http://core.svn.wordpress.org/trunk/wp-includes/formatting.php
+    */
+    public static function isUTF8($str) {
+        $length = strlen($str);
+        for ($i = 0; $i < $length; $i++) {
+            $c = ord($str[$i]);
+            if ($c < 0x80)
+                $n = 0;# 0bbbbbbb
+            elseif (($c & 0xE0) == 0xC0)
+                $n = 1;# 110bbbbb
+            elseif (($c & 0xF0) == 0xE0)
+                $n = 2;# 1110bbbb
+            elseif (($c & 0xF8) == 0xF0)
+                $n = 3;# 11110bbb
+            elseif (($c & 0xFC) == 0xF8)
+                $n = 4;# 111110bb
+            elseif (($c & 0xFE) == 0xFC)
+                $n = 5;# 1111110b
+            else
+                return false;# Does not match any model
+            for ($j = 0; $j < $n; $j++) { # n bytes matching 10bbbbbb follow ?
+                if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
+                    return false;
+            }
+        }
+        return true;
+    }
 
 }
\ No newline at end of file
diff --git a/app/modules/Web/views/Icinga/ApiSimpleDataProviderSuccessView.class.php b/app/modules/Web/views/Icinga/ApiSimpleDataProviderSuccessView.class.php
old mode 100644
new mode 100755
index d44004b..9f4782b
--- a/app/modules/Web/views/Icinga/ApiSimpleDataProviderSuccessView.class.php
+++ b/app/modules/Web/views/Icinga/ApiSimpleDataProviderSuccessView.class.php
@@ -30,7 +30,7 @@ class Web_Icinga_ApiSimpleDataProviderSuccessView extends IcingaWebBaseView {
 
         $this->setAttribute('_title', 'IcingaApiSimpleDataProvider');
     }
-
+    
     public function executeJson(AgaviRequestDataHolder $rd) {
 
         // init
@@ -55,7 +55,8 @@ class Web_Icinga_ApiSimpleDataProviderSuccessView extends IcingaWebBaseView {
         if (($template = $model->getTemplateCode()) !== false) {
             $jsonData['result']['template'] = $template;
         }
-
+        
+        AppKitArrayUtil::toUTF8_recursive($jsonData);
         $jsonDataEnc = json_encode($jsonData);
 
         return $jsonDataEnc;





More information about the icinga-checkins mailing list