From 26bc46d9b671ea069fc779ecb8b4ac90323c2291 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Wed, 07 Sep 2011 02:59:48 -0400
Subject: [PATCH] - Move two entries from 0.6-rc to trunk's changelog part

---
 program/include/rcube_ldap.php |  116 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 85 insertions(+), 31 deletions(-)

diff --git a/program/include/rcube_ldap.php b/program/include/rcube_ldap.php
index 2fb25ab..699691d 100644
--- a/program/include/rcube_ldap.php
+++ b/program/include/rcube_ldap.php
@@ -181,8 +181,11 @@
                 }
 
                 // Get the pieces needed for variable replacement.
-                $fu = $RCMAIL->user->get_username();
-                list($u, $d) = explode('@', $fu);
+                if ($fu = $RCMAIL->user->get_username())
+                  list($u, $d) = explode('@', $fu);
+                else
+                  $d = $this->mail_domain;
+                
                 $dc = 'dc='.strtr($d, array('.' => ',dc=')); // hierarchal domain string
 
                 $replaces = array('%dc' => $dc, '%d' => $d, '%fu' => $fu, '%u' => $u);
@@ -431,34 +434,41 @@
         // we have a search result resource
         if ($this->ldap_result && $this->result->count > 0)
         {
+            // sorting still on the ldap server
             if ($this->sort_col && $this->prop['scope'] !== 'base' && !$this->vlv_active)
                 ldap_sort($this->conn, $this->ldap_result, $this->sort_col);
 
+            // start and end of the page
             $start_row = $this->vlv_active ? 0 : $this->result->first;
             $start_row = $subset < 0 ? $start_row + $this->page_size + $subset : $start_row;
             $last_row = $this->result->first + $this->page_size;
             $last_row = $subset != 0 ? $start_row + abs($subset) : $last_row;
 
+            // get all entries from the ldap server
             $entries = ldap_get_entries($this->conn, $this->ldap_result);
+
+            // filtering for group members
+            if ($this->groups and $this->group_id)
+            {
+                $count = 0;
+                $members = array();
+                foreach ($entries as $entry)
+                {
+                    if ($this->group_members[base64_encode($entry['dn'])])
+                    {
+                        $members[] = $entry;
+                        $count++;
+                    }
+                }
+                $entries = $members;
+                $entries['count'] = $count;
+                $this->result->count = $count;
+            }
+
+            // filter entries for this page
             for ($i = $start_row; $i < min($entries['count'], $last_row); $i++)
                 $this->result->add($this->_ldap2result($entries[$i]));
         }
-
-        // temp hack for filtering group members
-        if ($this->groups and $this->group_id)
-        {
-            $result = new rcube_result_set();
-            while ($record = $this->result->iterate())
-            {
-                if ($this->group_members[$record['ID']])
-                {
-                    $result->add($record);
-                    $result->count++;
-                }
-            }
-            $this->result = $result;
-        }
-
         return $this->result;
     }
 
@@ -728,6 +738,15 @@
         $newdata = array();
         $replacedata = array();
         $deletedata = array();
+        
+        // flatten composite fields in $record
+        if (is_array($record['address'])) {
+          foreach ($record['address'] as $i => $struct) {
+            foreach ($struct as $col => $val) {
+              $record[$col][$i] = $val;
+            }
+          }
+        }
 
         foreach ($this->fieldmap as $col => $fld) {
             $val = $save_cols[$col];
@@ -843,10 +862,10 @@
     {
         if (!is_array($ids)) {
             // Not an array, break apart the encoded DNs.
-            $dns = explode(',', $ids);
+            $ids = explode(',', $ids);
         } // end if
 
-        foreach ($dns as $id) {
+        foreach ($ids as $id) {
             $dn = base64_decode($id);
             $this->_debug("C: Delete [dn: $dn]");
             // Delete the record.
@@ -869,7 +888,7 @@
             }
         } // end foreach
 
-        return count($dns);
+        return count($ids);
     }
 
 
@@ -883,7 +902,7 @@
             $filter = $this->filter ? $this->filter : '(objectclass=*)';
             $function = $this->prop['scope'] == 'sub' ? 'ldap_search' : ($this->prop['scope'] == 'base' ? 'ldap_read' : 'ldap_list');
 
-            $this->_debug("C: Search [".$filter."]");
+            $this->_debug("C: Search [$filter]");
 
             // when using VLV, we get the total count by...
             if (!$count && $function != 'ldap_read' && $this->prop['vlv']) {
@@ -896,7 +915,7 @@
                 }
                 else  // ...or by fetching all records dn and count them
                     $this->vlv_count = $this->_exec_search(true);
-                
+
                 $this->vlv_active = $this->_vlv_set_controls();
             }
 
@@ -1081,11 +1100,13 @@
             $dc = 'dc='.strtr($d, array('.' => ',dc='));
             $replaces = array('%dc' => $dc, '%d' => $d, '%fu' => $fu, '%u' => $u);
 
-            $this->groups_base_dn = strtr($this->groups_base_dn, $replaces);;
+            $this->groups_base_dn = strtr($this->groups_base_dn, $replaces);
         }
 
         $base_dn = $this->groups_base_dn;
         $filter = $this->prop['groups']['filter'];
+
+        $this->_debug("C: Search [$filter][dn: $base_dn]");
 
         $res = ldap_search($this->conn, $base_dn, $filter, array('cn','member'));
         if ($res === false)
@@ -1094,18 +1115,23 @@
             $this->set_error(self::ERROR_SAVING, 'errorsaving');
             return array();
         }
+
         $ldap_data = ldap_get_entries($this->conn, $res);
+        $this->_debug("S: ".ldap_count_entries($this->conn, $res)." record(s)");
 
         $groups = array();
         $group_sortnames = array();
         for ($i=0; $i<$ldap_data["count"]; $i++)
         {
             $group_name = $ldap_data[$i]['cn'][0];
-            $group_id = base64_encode($group_name);
-            $groups[$group_id]['ID'] = $group_id;
-            $groups[$group_id]['name'] = $group_name;
-            $groups[$group_id]['members'] = $ldap_data[$i]['member'];
-            $group_sortnames[] = strtolower($group_name);
+            if (!$search || strstr(strtolower($group_name), strtolower($search)))
+            {
+                $group_id = base64_encode($group_name);
+                $groups[$group_id]['ID'] = $group_id;
+                $groups[$group_id]['name'] = $group_name;
+                $groups[$group_id]['members'] = $ldap_data[$i]['member'];
+                $group_sortnames[] = strtolower($group_name);
+            }
         }
         array_multisort($group_sortnames, SORT_ASC, SORT_STRING, $groups);
         $this->group_cache = $groups;
@@ -1134,6 +1160,8 @@
             'member' => '',
         );
 
+        $this->_debug("C: Add [dn: $new_dn]: ".print_r($new_entry, true));
+
         $res = ldap_add($this->conn, $new_dn, $new_entry);
         if ($res === false)
         {
@@ -1141,6 +1169,9 @@
             $this->set_error(self::ERROR_SAVING, 'errorsaving');
             return false;
         }
+
+        $this->_debug("S: OK");
+
         return array('id' => $new_gid, 'name' => $group_name);
     }
 
@@ -1157,8 +1188,10 @@
 
         $base_dn = $this->groups_base_dn;
         $group_name = $this->group_cache[$group_id]['name'];
-
         $del_dn = "cn=$group_name,$base_dn";
+
+        $this->_debug("C: Delete [dn: $del_dn]");
+
         $res = ldap_delete($this->conn, $del_dn);
         if ($res === false)
         {
@@ -1166,6 +1199,9 @@
             $this->set_error(self::ERROR_SAVING, 'errorsaving');
             return false;
         }
+
+        $this->_debug("S: OK");
+
         return true;
     }
 
@@ -1188,6 +1224,8 @@
         $new_rdn = "cn=$new_name";
         $new_gid = base64_encode($new_name);
 
+        $this->_debug("C: Rename [dn: $old_dn] [dn: $new_rdn]");
+
         $res = ldap_rename($this->conn, $old_dn, $new_rdn, NULL, TRUE);
         if ($res === false)
         {
@@ -1195,6 +1233,9 @@
             $this->set_error(self::ERROR_SAVING, 'errorsaving');
             return false;
         }
+
+        $this->_debug("S: OK");
+
         return $new_name;
     }
 
@@ -1218,6 +1259,8 @@
         foreach (explode(",", $contact_ids) as $id)
             $new_attrs['member'][] = base64_decode($id);
 
+        $this->_debug("C: Add [dn: $group_dn]: ".print_r($new_attrs, true));
+
         $res = ldap_mod_add($this->conn, $group_dn, $new_attrs);
         if ($res === false)
         {
@@ -1225,6 +1268,9 @@
             $this->set_error(self::ERROR_SAVING, 'errorsaving');
             return 0;
         }
+
+        $this->_debug("S: OK");
+
         return count($new_attrs['member']);
     }
 
@@ -1248,6 +1294,8 @@
         foreach (explode(",", $contact_ids) as $id)
             $del_attrs['member'][] = base64_decode($id);
 
+        $this->_debug("C: Delete [dn: $group_dn]: ".print_r($del_attrs, true));
+
         $res = ldap_mod_del($this->conn, $group_dn, $del_attrs);
         if ($res === false)
         {
@@ -1255,6 +1303,9 @@
             $this->set_error(self::ERROR_SAVING, 'errorsaving');
             return 0;
         }
+
+        $this->_debug("S: OK");
+
         return count($del_attrs['member']);
     }
 
@@ -1273,7 +1324,9 @@
 
         $base_dn = $this->groups_base_dn;
         $contact_dn = base64_decode($contact_id);
-        $filter = "(member=$contact_dn)";
+        $filter = strtr("(member=$contact_dn)", array('\\' => '\\\\'));
+
+        $this->_debug("C: Search [$filter][dn: $base_dn]");
 
         $res = ldap_search($this->conn, $base_dn, $filter, array('cn'));
         if ($res === false)
@@ -1283,6 +1336,7 @@
             return array();
         }
         $ldap_data = ldap_get_entries($this->conn, $res);
+        $this->_debug("S: ".ldap_count_entries($this->conn, $res)." record(s)");
 
         $groups = array();
         for ($i=0; $i<$ldap_data["count"]; $i++)

--
Gitblit v1.9.1