public function search($base = null, $filter = null, $params = array())
{
if (is_null($base)) {
$base = $this->_config['basedn'];
}
if ($base instanceof Horde_Ldap_Entry) {
/* Fetch DN of entry, making searchbase relative to the entry. */
$base = $base->dn();
}
if (is_null($filter)) {
$filter = $this->_config['filter'];
}
if ($filter instanceof Horde_Ldap_Filter) {
/* Convert Horde_Ldap_Filter to string representation. */
$filter = (string) $filter;
}
/* Setting search parameters. */
$sizelimit = isset($params['sizelimit']) ? $params['sizelimit'] : 0;
$timelimit = isset($params['timelimit']) ? $params['timelimit'] : 0;
$attrsonly = isset($params['attrsonly']) ? $params['attrsonly'] : 0;
$attributes = isset($params['attributes']) ? $params['attributes'] : array();
/* Ensure $attributes to be an array in case only one attribute name
* was given as string. */
if (!is_array($attributes)) {
$attributes = array($attributes);
}
/* Reorganize the $attributes array index keys sometimes there are
* problems with not consecutive indexes. */
$attributes = array_values($attributes);
/* Scoping makes searches faster! */
$scope = isset($params['scope']) ? $params['scope'] : $this->_config['scope'];
switch ($scope) {
case 'one':
$search_function = 'ldap_list';
break;
case 'base':
$search_function = 'ldap_read';
break;
default:
$search_function = 'ldap_search';
}
/* Continue attempting the search operation until we get a success or a
* definitive failure. */
while (true) {
$link = $this->getLink();
$search = @call_user_func($search_function, $link, $base, $filter, $attributes, $attrsonly, $sizelimit, $timelimit);
if ($errno = @ldap_errno($link)) {
$err = $this->errorName($errno);
if ($err == 'LDAP_NO_SUCH_OBJECT' || $err == 'LDAP_SIZELIMIT_EXCEEDED') {
return new Horde_Ldap_Search($search, $this, $attributes);
}
if ($err == 'LDAP_FILTER_ERROR') {
/* Bad search filter. */
throw new Horde_Ldap_Exception(ldap_err2str($errno) . ' ($filter)', $errno);
}
if ($err == 'LDAP_OPERATIONS_ERROR' && $this->_config['auto_reconnect']) {
$this->_link = false;
$this->_reconnect();
} else {
$msg = "\nParameters:\nBase: {$base}\nFilter: {$filter}\nScope: {$scope}";
throw new Horde_Ldap_Exception(ldap_err2str($errno) . $msg, $errno);
}
} else {
return new Horde_Ldap_Search($search, $this, $attributes);
}
}
}