use central LDAP search and LDAP search limit

This commit is contained in:
Roland Gruber 2010-02-07 14:04:29 +00:00
parent fa9edd7963
commit 55083ce3f6
7 changed files with 75 additions and 62 deletions

View File

@ -548,7 +548,8 @@ function searchLDAPByAttribute($name, $value, $objectClass, $attributes, $scopes
} }
for ($s = 0; $s < sizeof($scopes); $s++) { for ($s = 0; $s < sizeof($scopes); $s++) {
// search LDAP // search LDAP
$sr = @ldap_search($_SESSION['ldap']->server(), escapeDN($_SESSION['config']->get_Suffix($scopes[$s])), $filter, $attributes, 0, 0, 0, LDAP_DEREF_NEVER); $sr = @ldap_search($_SESSION['ldap']->server(), escapeDN($_SESSION['config']->get_Suffix($scopes[$s])),
$filter, $attributes, 0, $_SESSION['config']->get_searchLimit(), 0, LDAP_DEREF_NEVER);
if ($sr) { if ($sr) {
$entries = ldap_get_entries($_SESSION['ldap']->server(), $sr); $entries = ldap_get_entries($_SESSION['ldap']->server(), $sr);
if ($entries) { if ($entries) {
@ -566,12 +567,14 @@ function searchLDAPByAttribute($name, $value, $objectClass, $attributes, $scopes
* @param String $filter * @param String $filter
* @param array $attributes list of attributes to return * @param array $attributes list of attributes to return
* @param array $scopes account types * @param array $scopes account types
* @return array list of found entries
*/ */
function searchLDAPByFilter($filter, $attributes, $scopes) { function searchLDAPByFilter($filter, $attributes, $scopes) {
$return = array(); $return = array();
for ($s = 0; $s < sizeof($scopes); $s++) { for ($s = 0; $s < sizeof($scopes); $s++) {
// search LDAP // search LDAP
$sr = @ldap_search($_SESSION['ldap']->server(), escapeDN($_SESSION['config']->get_Suffix($scopes[$s])), $filter, $attributes, 0, 0, 0, LDAP_DEREF_NEVER); $sr = @ldap_search($_SESSION['ldap']->server(), escapeDN($_SESSION['config']->get_Suffix($scopes[$s])),
$filter, $attributes, 0, $_SESSION['config']->get_searchLimit(), 0, LDAP_DEREF_NEVER);
if ($sr) { if ($sr) {
$entries = ldap_get_entries($_SESSION['ldap']->server(), $sr); $entries = ldap_get_entries($_SESSION['ldap']->server(), $sr);
if ($entries) { if ($entries) {
@ -583,6 +586,45 @@ function searchLDAPByFilter($filter, $attributes, $scopes) {
return $return; return $return;
} }
/**
* Runs an LDAP search.
*
* @param String $suffix LDAP suffix
* @param String $filter filter
* @param array $attributes list of attributes to return
* @return array list of found entries
*/
function searchLDAP($suffix, $filter, $attributes) {
$return = array();
$sr = @ldap_search($_SESSION['ldap']->server(), escapeDN($suffix), $filter, $attributes,
0, $_SESSION['config']->get_searchLimit(), 0, LDAP_DEREF_NEVER);
if ($sr) {
$entries = ldap_get_entries($_SESSION['ldap']->server(), $sr);
if ($entries) {
$return = cleanLDAPResult($entries);
}
@ldap_free_result($sr);
}
return $return;
}
/**
* Returns the error number of the last LDAP command.
*
* @return int error number (0 if all was ok)
*/
function getLastLDAPErrorNumber() {
return ldap_errno($_SESSION["ldap"]->server());
}
/**
* Returns the error message of the last LDAP command.
*
* @return String error message
*/
function getLastLDAPErrorMessage() {
return ldap_error($_SESSION["ldap"]->server());
}
/** /**
* Cleans the result of an LDAP search. * Cleans the result of an LDAP search.

View File

@ -146,7 +146,8 @@ class cache {
// Get Data from ldap // Get Data from ldap
$search = $this->attributes[$scope]; $search = $this->attributes[$scope];
$search[] = 'objectClass'; $search[] = 'objectClass';
$result = @ldap_search($_SESSION['ldap']->server(), escapeDN($suffix), 'objectClass=*', $search, 0, 0, 0, LDAP_DEREF_NEVER); $result = @ldap_search($_SESSION['ldap']->server(), escapeDN($suffix), 'objectClass=*',
$search, 0, $_SESSION['config']->get_searchLimit(), 0, LDAP_DEREF_NEVER);
if ($result) { if ($result) {
// Write search result in array // Write search result in array
$entry = @ldap_first_entry($_SESSION['ldap']->server(), $result); $entry = @ldap_first_entry($_SESSION['ldap']->server(), $result);

View File

@ -811,21 +811,14 @@ class lamList {
$module_filter = get_ldap_filter($this->type); // basic filter is provided by modules $module_filter = get_ldap_filter($this->type); // basic filter is provided by modules
$filter = "(&" . $module_filter . ")"; $filter = "(&" . $module_filter . ")";
$attrs = $this->attrArray; $attrs = $this->attrArray;
$sr = @ldap_search($_SESSION["ldap"]->server(), escapeDN($this->suffix), $filter, $attrs, 0, 0, 0, LDAP_DEREF_NEVER); $entries = searchLDAP($this->suffix, $filter, $attrs);
if (ldap_errno($_SESSION["ldap"]->server()) == 4) { if (getLastLDAPErrorNumber() == 4) {
StatusMessage("WARN", _("LDAP sizelimit exceeded, not all entries are shown."), _("See the manual for instructions to solve this problem.")); StatusMessage("WARN", _("LDAP sizelimit exceeded, not all entries are shown."), _("See the manual for instructions to solve this problem."));
} }
if ($sr) { elseif (getLastLDAPErrorNumber() > 0) {
$info = ldap_get_entries($_SESSION["ldap"]->server(), $sr); StatusMessage("ERROR", _("LDAP search failed! Please check your preferences."), getLastLDAPErrorMessage());
$info = cleanLDAPResult($info);
ldap_free_result($sr);
// save results
$this->entries = $info;
}
else {
$this->entries = array();
StatusMessage("ERROR", _("LDAP search failed! Please check your preferences."));
} }
$this->entries = $entries;
// generate list of possible suffixes // generate list of possible suffixes
$this->possibleSuffixes = $_SESSION['ldap']->search_units($_SESSION["config"]->get_Suffix($this->type)); $this->possibleSuffixes = $_SESSION['ldap']->search_units($_SESSION["config"]->get_Suffix($this->type));
} }

View File

@ -192,21 +192,16 @@ class fixed_ip extends baseModule {
*/ */
function load_attributes($attr) { function load_attributes($attr) {
if ($this->getAccountContainer()->dn_orig!=$_SESSION['config']->get_suffix('dhcp')) { if ($this->getAccountContainer()->dn_orig!=$_SESSION['config']->get_suffix('dhcp')) {
$sr = @ldap_search($_SESSION['ldap']->server(),$this->getAccountContainer()->dn_orig, '(objectClass=dhcpHost)', array(), 0, 0, 0, LDAP_DEREF_NEVER); $entries = searchLDAP($this->getAccountContainer()->dn_orig, '(objectClass=dhcpHost)', array('cn', 'dhcphwaddress', 'dhcpstatements'));
if ($sr) { for ($i=0; $i < sizeof($entries); $i++) {
$entries = ldap_get_entries($_SESSION['ldap']->server(), $sr); $this->fixed_ip[$i]['cn'] = $entries[$i]['cn'][0];
if ($entries) { $this->fixed_ip[$i]['mac'] = array_pop(explode(" ", $entries[$i]['dhcphwaddress'][0]));
for ($i=0; $i < $entries["count"]; $i++) { $this->fixed_ip[$i]['ip'] = array_pop(explode(" ", $entries[$i]['dhcpstatements'][0]));
$this->fixed_ip[$i]['cn'] = $entries[$i]['cn'][0];
$this->fixed_ip[$i]['mac'] = array_pop(explode(" ", $entries[$i]['dhcphwaddress'][0])); $this->orig_ips[$i]['cn'] = $entries[$i]['cn'][0];
$this->fixed_ip[$i]['ip'] = array_pop(explode(" ", $entries[$i]['dhcpstatements'][0])); $this->orig_ips[$i]['mac'] = array_pop(explode(" ", $entries[$i]['dhcphwaddress'][0]));
$this->orig_ips[$i]['ip'] = array_pop(explode(" ", $entries[$i]['dhcpstatements'][0]));
$this->orig_ips[$i]['cn'] = $entries[$i]['cn'][0]; }
$this->orig_ips[$i]['mac'] = array_pop(explode(" ", $entries[$i]['dhcphwaddress'][0]));
$this->orig_ips[$i]['ip'] = array_pop(explode(" ", $entries[$i]['dhcpstatements'][0]));
}
}
}
} }
} }

View File

@ -824,9 +824,6 @@ class kolabUser extends baseModule {
// delegates // delegates
if (in_array('kolabDelegate', $fields)) { if (in_array('kolabDelegate', $fields)) {
$delegates = array(); $delegates = array();
// $entries = searchLDAPByAttribute('mail', '*', 'inetOrgPerson', array('mail'), $this->selfServiceSettings->LDAPSuffix);
$sr = @ldap_search($_SESSION['ldapHandle'], escapeDN($this->selfServiceSettings->LDAPSuffix), '(&(objectClass=inetOrgPerson)(mail=*))', array('mail'), 0, 0, 0, LDAP_DEREF_NEVER); $sr = @ldap_search($_SESSION['ldapHandle'], escapeDN($this->selfServiceSettings->LDAPSuffix), '(&(objectClass=inetOrgPerson)(mail=*))', array('mail'), 0, 0, 0, LDAP_DEREF_NEVER);
if ($sr) { if ($sr) {
$result = ldap_get_entries($_SESSION['ldapHandle'], $sr); $result = ldap_get_entries($_SESSION['ldapHandle'], $sr);

View File

@ -134,30 +134,21 @@ class lamDHCPList extends lamList {
* @param string $attribute attribute name * @param string $attribute attribute name
*/ */
public function listPrintTableCellContent(&$entry, &$attribute) { public function listPrintTableCellContent(&$entry, &$attribute) {
// Fixed Ips // Fixed IPs
if ($attribute=="fixed_ips") { if ($attribute=="fixed_ips") {
// find all fixed addresses: // find all fixed addresses:
$ldap = $_SESSION['ldap']; $entries = searchLDAP($entry['dn'], 'objectClass=dhcpHost', array('dhcpstatements', 'dhcphwaddress', 'cn'));
$sr = @ldap_search($ldap->server(),$entry['dn'],"objectClass=dhcpHost", array('dhcpstatements', 'dhcphwaddress', 'cn'), 0, 0, 0, LDAP_DEREF_NEVER); if (sizeof($entries) > 0) {
if ($sr) {
$get = ldap_get_entries($ldap->server(),$sr);
// Now list all fixed_ips:
echo "<table border=\"0\" width=\"100%\">"; echo "<table border=\"0\" width=\"100%\">";
$this->fixed_ips = array(); for ($i = 0; $i < sizeof($entries); $i++) {
if (isset($get)) { echo "<tr>";
foreach($get AS $id=>$arr) { echo "<td width=\"25%\">".array_pop(explode(" ",$entries[$i]['dhcpstatements'][0]))."</td>";
if (is_numeric($id)) { echo "<td width=\"35%\">".array_pop(explode(" ",$entries[$i]['dhcphwaddress'][0]))."</td>";
echo "<tr>"; echo "<td width=\"40%\">".$entries[$i]['cn'][0]."</td>";
echo "<td width=\"25%\">".array_pop(explode(" ",$get[$id]['dhcpstatements'][0]))."</td>"; echo "</tr>";
echo "<td width=\"35%\">".array_pop(explode(" ",$get[$id]['dhcphwaddress'][0]))."</td>"; }
echo "<td width=\"40%\">".$get[$id]['cn'][0]."</td>"; echo "</table>";
echo "</tr>"; }
}
}
}
echo "</table>";
}
} }
// fixed ip address // fixed ip address
elseif ($attribute=="dhcpstatements") { elseif ($attribute=="dhcpstatements") {

View File

@ -244,14 +244,8 @@ if ($_POST['delete']) {
* @return interger number of childs * @return interger number of childs
*/ */
function getChildCount($dn) { function getChildCount($dn) {
$return = 0; $entries = searchLDAP(escapeDN($dn), 'objectClass=*', array('dn'));
$sr = @ldap_search($_SESSION['ldap']->server(), escapeDN($dn), 'objectClass=*', array('dn'), 0, 0, 0, LDAP_DEREF_NEVER); return (sizeof($entries) - 1);
if ($sr) {
$entries = ldap_get_entries($_SESSION['ldap']->server(), $sr);
$entries = cleanLDAPResult($entries);
$return = sizeof($entries) - 1;
}
return $return;
} }
/** /**