lib/api lib/kolab_api_service.php lib/kolab_utils.php

Aleksander Machniak machniak at kolabsys.com
Thu Oct 9 16:52:06 CEST 2014


 lib/api/kolab_api_service_form_value.php |    3 --
 lib/kolab_api_service.php                |    5 +---
 lib/kolab_utils.php                      |   38 +++++++++++++++++++++++++++----
 3 files changed, 37 insertions(+), 9 deletions(-)

New commits:
commit 596aae5d7fa89a39057d7f96541f63848265bfe7
Author: Aleksander Machniak <machniak at kolabsys.com>
Date:   Thu Oct 9 16:51:00 2014 +0200

    Fix bug where user OU was not properly selected in user form if OU's DN contains non-ascii characters (#3744)

diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php
index 18867a4..9a9bff2 100644
--- a/lib/api/kolab_api_service_form_value.php
+++ b/lib/api/kolab_api_service_form_value.php
@@ -1028,10 +1028,9 @@ class kolab_api_service_form_value extends kolab_api_service
                 $subjects = $subjects->entries(true);
                 $subject = array_shift($subjects);
                 $subject_dn = key($subject);
-                $subject_dn_components = ldap_explode_dn($subject_dn, 0);
+                $subject_dn_components = kolab_utils::explode_dn($subject_dn);
 
                 if ($subject_dn_components) {
-                    unset($subject_dn_components['count']);
                     array_shift($subject_dn_components);
                     $default = strtolower(implode(',', $subject_dn_components));
                 }
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 9c55763..0049b8f 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -535,9 +535,8 @@ abstract class kolab_api_service
 
         // add organizational unit to the result
         if (empty($attrs['ou']) && isset($attributes['ou'])) {
-            $dn = ldap_explode_dn($dn, 0);
-            // pop the count and rdn
-            unset($dn['count']);
+            $dn = kolab_utils::explode_dn($dn);
+            // pop the rdn
             unset($dn[0]);
             $attrs['ou'] = implode(',', $dn);
         }
diff --git a/lib/kolab_utils.php b/lib/kolab_utils.php
index 9f064e3..e2602af 100644
--- a/lib/kolab_utils.php
+++ b/lib/kolab_utils.php
@@ -163,17 +163,47 @@ class kolab_utils
      */
     public static function dn2ufn($dn)
     {
-        $name = ldap_dn2ufn($dn);
+        return self::decode_dn(ldap_dn2ufn($dn));
+    }
+
+    /**
+     * Unicode-aware ldap_explode_dn() wrapper
+     *
+     * @param string $dn LDAP DN string
+     *
+     * @return array Exploded DN (uses unicode encoding)
+     */
+    public static function explode_dn($dn)
+    {
+        $result = ldap_explode_dn($dn, 0);
+
+        // get rid of count
+        unset($result['count']);
+
+        $result = array_map(array('kolab_utils', 'decode_dn'), $result);
+
+        return $result;
+    }
+
+    /**
+     * Decode \XX sequences into characters
+     *
+     * @param string $str String to decode
+     *
+     * @return string Decoded string
+     */
+    public static function decode_dn($str)
+    {
         $pos  = 0;
 
         // example: "\C3\A4" => "ä"
-        while (preg_match('/\\\\[0-9a-fA-F]{2}/', $name, $matches, PREG_OFFSET_CAPTURE, $pos)) {
+        while (preg_match('/\\\\[0-9a-fA-F]{2}/', $str, $matches, PREG_OFFSET_CAPTURE, $pos)) {
             $char = chr(hexdec(substr($matches[0][0], 1)));
             $pos  = $matches[0][1];
-            $name = substr_replace($name, $char, $pos, 3);
+            $str  = substr_replace($str, $char, $pos, 3);
             $pos += 1;
         }
 
-        return $name;
+        return $str;
     }
 }




More information about the commits mailing list