$base_config = array('host' => 'localhost',
'port' => 389,
'version' => 3,
- 'starttls' => true,
+ 'starttls' => false,
'uid' => '',
'pass' => '',
'basedn' => '',
$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);
}
*/
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));
if (is_a($result, 'PEAR_Error')) {
throw new Horde_Kolab_Server_Exception($result->getMessage());
}
- return $result;
+ return $result->as_struct();
}
/**
if (empty($result)) {
return false;
}
- $dns = array();
- foreach ($result as $entry) {
- $dns[] = $entry['dn'];
- }
+ $dns = array_keys($result);
switch ($restrict) {
case self::RESULT_STRICT:
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;
+ }
+ }
+
}