Index: branches/release-0.6/program/include/rcube_ldap.php
===================================================================
--- branches/release-0.6/program/include/rcube_ldap.php	(revision 5213)
+++ branches/release-0.6/program/include/rcube_ldap.php	(revision 5235)
@@ -6,4 +6,5 @@
  | This file is part of the Roundcube Webmail client                     |
  | Copyright (C) 2006-2011, The Roundcube Dev Team                       |
+ | Copyright (C) 2011, Kolab Systems AG                                  |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
@@ -14,4 +15,5 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  |         Andreas Dick <andudi (at) gmx (dot) ch>                       |
+ |         Aleksander Machniak <machniak@kolabsys.com>                   |
  +-----------------------------------------------------------------------+
 
@@ -186,5 +188,5 @@
                 else
                   $d = $this->mail_domain;
-                
+
                 $dc = 'dc='.strtr($d, array('.' => ',dc=')); // hierarchal domain string
 
@@ -455,5 +457,5 @@
                 foreach ($entries as $entry)
                 {
-                    if ($this->group_members[base64_encode($entry['dn'])])
+                    if ($this->group_members[self::dn_encode($entry['dn'])])
                     {
                         $members[] = $entry;
@@ -615,5 +617,5 @@
         if ($this->conn && $dn)
         {
-            $dn = base64_decode($dn);
+            $dn = self::dn_decode($dn);
 
             $this->_debug("C: Read [dn: $dn] [(objectclass=*)]");
@@ -714,9 +716,11 @@
         $this->_debug("S: OK");
 
+        $dn = self::dn_encode($dn);
+
         // add new contact to the selected group
         if ($this->groups)
-            $this->add_to_group($this->group_id, base64_encode($dn));
-
-        return base64_encode($dn);
+            $this->add_to_group($this->group_id, $dn);
+
+        return $dn;
     }
 
@@ -739,5 +743,5 @@
         $replacedata = array();
         $deletedata = array();
-        
+
         // flatten composite fields in $record
         if (is_array($record['address'])) {
@@ -777,5 +781,5 @@
         } // end foreach
 
-        $dn = base64_decode($id);
+        $dn = self::dn_decode($id);
 
         // Update the entry as required.
@@ -834,15 +838,19 @@
             $this->_debug("S: OK");
 
+            $dn    = self::dn_encode($dn);
+            $newdn = self::dn_encode($newdn);
+
             // change the group membership of the contact
             if ($this->groups)
             {
-                $group_ids = $this->get_record_groups(base64_encode($dn));
+                $group_ids = $this->get_record_groups($dn);
                 foreach ($group_ids as $group_id)
                 {
-                    $this->remove_from_group($group_id, base64_encode($dn));
-                    $this->add_to_group($group_id, base64_encode($newdn));
+                    $this->remove_from_group($group_id, $dn);
+                    $this->add_to_group($group_id, $newdn);
                 }
             }
-            return base64_encode($newdn);
+
+            return $newdn;
         }
 
@@ -867,5 +875,5 @@
 
         foreach ($ids as $id) {
-            $dn = base64_decode($id);
+            $dn = self::dn_decode($id);
             $this->_debug("C: Delete [dn: $dn]");
             // Delete the record.
@@ -879,10 +887,9 @@
 
             // remove contact from all groups where he was member
-            if ($this->groups)
-            {
-                $group_ids = $this->get_record_groups(base64_encode($dn));
-                foreach ($group_ids as $group_id)
-                {
-                    $this->remove_from_group($group_id, base64_encode($dn));
+            if ($this->groups) {
+                $dn = self::dn_encode($dn);
+                $group_ids = $this->get_record_groups($dn);
+                foreach ($group_ids as $group_id) {
+                    $this->remove_from_group($group_id, $dn);
                 }
             }
@@ -968,5 +975,5 @@
 
         if ($rec['dn'])
-            $out[$this->primary_key] = base64_encode($rec['dn']);
+            $out[$this->primary_key] = self::dn_encode($rec['dn']);
 
         foreach ($this->fieldmap as $rf => $lf)
@@ -1068,5 +1075,5 @@
             {
                 if (!empty($cache_members[$i]))
-                    $members[base64_encode($cache_members[$i])] = 1;
+                    $members[self::dn_encode($cache_members[$i])] = 1;
             }
             $this->group_members = $members;
@@ -1127,5 +1134,5 @@
             if (!$search || strstr(strtolower($group_name), strtolower($search)))
             {
-                $group_id = base64_encode($group_name);
+                $group_id = self::dn_encode($group_name);
                 $groups[$group_id]['ID'] = $group_id;
                 $groups[$group_id]['name'] = $group_name;
@@ -1153,5 +1160,5 @@
         $base_dn = $this->groups_base_dn;
         $new_dn = "cn=$group_name,$base_dn";
-        $new_gid = base64_encode($group_name);
+        $new_gid = self::dn_encode($group_name);
 
         $new_entry = array(
@@ -1223,5 +1230,5 @@
         $old_dn = "cn=$group_name,$base_dn";
         $new_rdn = "cn=$new_name";
-        $new_gid = base64_encode($new_name);
+        $new_gid = self::dn_encode($new_name);
 
         $this->_debug("C: Rename [dn: $old_dn] [dn: $new_rdn]");
@@ -1258,5 +1265,5 @@
         $new_attrs = array();
         foreach (explode(",", $contact_ids) as $id)
-            $new_attrs['member'][] = base64_decode($id);
+            $new_attrs['member'][] = self::dn_decode($id);
 
         $this->_debug("C: Add [dn: $group_dn]: ".print_r($new_attrs, true));
@@ -1293,5 +1300,5 @@
         $del_attrs = array();
         foreach (explode(",", $contact_ids) as $id)
-            $del_attrs['member'][] = base64_decode($id);
+            $del_attrs['member'][] = self::dn_decode($id);
 
         $this->_debug("C: Delete [dn: $group_dn]: ".print_r($del_attrs, true));
@@ -1324,5 +1331,5 @@
 
         $base_dn = $this->groups_base_dn;
-        $contact_dn = base64_decode($contact_id);
+        $contact_dn = self::dn_decode($contact_id);
         $filter = strtr("(member=$contact_dn)", array('\\' => '\\\\'));
 
@@ -1343,5 +1350,5 @@
         {
             $group_name = $ldap_data[$i]['cn'][0];
-            $group_id = base64_encode($group_name);
+            $group_id = self::dn_encode($group_name);
             $groups[$group_id] = $group_id;
         }
@@ -1402,5 +1409,5 @@
      * create ber encoding for sort control
      *
-     * @pararm array List of cols to sort by
+     * @param array List of cols to sort by
      * @return string BER encoded option value
      */
@@ -1456,5 +1463,6 @@
      * Returns ascii string encoded in hex
      */
-    private static function _string2hex($str) {
+    private static function _string2hex($str)
+    {
         $hex = '';
         for ($i=0; $i < strlen($str); $i++)
@@ -1463,3 +1471,29 @@
     }
 
+    /**
+     * HTML-safe DN string encoding
+     *
+     * @param string $str DN string
+     *
+     * @return string Encoded HTML identifier string
+     */
+    static function dn_encode($str)
+    {
+        // @TODO: to make output string shorter we could probably
+        //        remove dc=* items from it
+        return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
+    }
+
+    /**
+     * Decodes DN string encoded with _dn_encode()
+     *
+     * @param string $str Encoded HTML identifier string
+     *
+     * @return string DN string
+     */
+    static function dn_decode($str)
+    {
+        $str = str_pad(strtr($str, '-_', '+/'), strlen($str) % 4, '=', STR_PAD_RIGHT);
+        return base64_decode($str);
+    }
 }
