Some fixes for the LDAP handling of Kolab_Server. It was still broken
authorGunnar Wrobel <p@rdus.de>
Sun, 29 Mar 2009 21:08:46 +0000 (21:08 +0000)
committerGunnar Wrobel <p@rdus.de>
Sun, 29 Mar 2009 21:08:46 +0000 (21:08 +0000)
in some places because of the switch to Net_LDAP2.

framework/Kolab_Server/lib/Horde/Kolab/Server/ldap.php
framework/Kolab_Server/lib/Horde/Kolab/Server/test.php

index ea9f760..d009829 100644 (file)
@@ -63,7 +63,7 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server
         $base_config = array('host'           => 'localhost',
                              'port'           => 389,
                              'version'        => 3,
-                             'starttls'       => true,
+                             'starttls'       => false,
                              'uid'            => '',
                              'pass'           => '',
                              'basedn'         => '',
@@ -79,7 +79,10 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server
         $config['binddn'] = $config['uid'];
         $config['bindpw'] = $config['pass'];
 
-        $this->_ldap = new Net_LDAP2($config);
+        $this->_ldap = Net_LDAP2::connect($config);
+        if (is_a($this->_ldap, 'PEAR_Error')) {
+            throw new Horde_Kolab_Server_Exception($this->_ldap);
+        }
 
         parent::__construct($params);
     }
@@ -98,20 +101,16 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server
      */
     public function read($dn, $attrs = null)
     {
-        $params = array('scope' => 'one');
+        $params = array('scope' => 'base');
         if (!empty($attrs)) {
-            $params['attributes'] = $attr;
+            $params['attributes'] = $attrs;
         }
 
-        $result = $this->search(null, $params, $dn);
-        if (empty($result) || !($result instanceOf Net_LDAP2_Search)) {
-            throw new Horde_Kolab_Server_Exception(_("Empty or invalid result!"));
+        $data = $this->search(null, $params, $dn);
+        if (empty($data)) {
+            throw new Horde_Kolab_Server_Exception(_("Empty result!"));
         }            
 
-        $data = $result->as_struct();
-        if (is_a($data, 'PEAR_Error')) {
-            throw new Horde_Kolab_Server_Exception($data);
-        }
         if (!isset($data[$dn])) {
             throw new Horde_Kolab_Server_Exception(sprintf(_("No result found for %s"),
                                                            $dn));
@@ -418,7 +417,7 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server
         if (is_a($result, 'PEAR_Error')) {
             throw new Horde_Kolab_Server_Exception($result->getMessage());
         }
-        return $result;
+        return $result->as_struct();
     }
 
     /**
@@ -533,10 +532,7 @@ class Horde_Kolab_Server_ldap extends Horde_Kolab_Server
         if (empty($result)) {
             return false;
         }
-        $dns = array();
-        foreach ($result as $entry) {
-            $dns[] = $entry['dn'];
-        }
+        $dns = array_keys($result);
 
         switch ($restrict) {
         case self::RESULT_STRICT:
index 86cb55f..eabf557 100644 (file)
@@ -664,4 +664,42 @@ class Horde_Kolab_Server_test extends Horde_Kolab_Server_ldap
         return $this->_error;
     }
 
+    /**
+     * Identify the DN of the first result entry.
+     *
+     * @todo Check if this could be reintegrated with the code in the LDAP handler
+     *       again.
+     *
+     * @param array $result   The LDAP search result.
+     * @param int   $restrict A Horde_Kolab_Server::RESULT_* result restriction.
+     *
+     * @return boolean|string|array The DN(s) or false if there was no result.
+     *
+     * @throws Horde_Kolab_Server_Exception If the number of results did not
+     *                                      meet the expectations.
+     */
+    protected function dnFromResult($result,
+                                    $restrict = Horde_Kolab_Server::RESULT_SINGLE)
+    {
+        if (empty($result)) {
+            return false;
+        }
+        $dns = array();
+        foreach ($result as $entry) {
+            $dns[] = $entry['dn'];
+        }
+
+        switch ($restrict) {
+        case self::RESULT_STRICT:
+            if (count($dns) > 1) {
+                throw new Horde_Kolab_Server_Exception(sprintf(_("Found %s results when expecting only one!"),
+                                                               $count));
+            }
+        case self::RESULT_SINGLE:
+            return $dns[0];
+        case self::RESULT_MANY:
+            return $dns;
+        }
+    }
+
 }