Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: multiPageSearch funcation added for Pagination while searchi… #9

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,100 @@ public function search(
return $this->createCollection($iterator, $collectionClass);
}



/**
* An LDAP search routine for finding information and returning paginated results
* https://stackoverflow.com/questions/16892693/zf2-ldap-pagination
*
* Options can be either passed as single parameters according to the
* method signature or as an array with one or more of the following keys
* - filter
* - baseDn
* - scope
* - attributes
* - sort
* - collectionClass
* - sizelimit
* - timelimit
*
* @param string|Filter\AbstractFilter|array $filter
* @param string|Dn|null $basedn
* @param int $scope
* @param array $attributes
* @param string|null $sort
* @param string|null $collectionClass
* @param integer $timelimit
* @param integer $pageSize
* @return Array
* @throws Exception\LdapException
*/
public function multiPageSearch(
$filter, $basedn = null, $scope, array $attributes = array(), $sort = null,
$collectionClass = null, $timelimit = 0, $pageSize = 10000
)
{
if (is_array($filter)) {
$options = array_change_key_case($filter, CASE_LOWER);
foreach ($options as $key => $value) {
switch ($key) {
case 'filter':
case 'basedn':
case 'scope':
case 'sort':
$$key = $value;
break;
case 'attributes':
if (is_array($value)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when $value is not an array?

$attributes = $value;
}
break;
case 'collectionclass':
$collectionClass = $value;
break;
case 'sizelimit':
case 'timelimit':
$$key = (int) $value;
break;
}
}
}

if ($basedn === null) {
$basedn = $this->getBaseDn();
} elseif ($basedn instanceof Dn) {
$basedn = $basedn->toString();
}

if ($filter instanceof Filter\AbstractFilter) {
$filter = $filter->toString();
}

$resource = $this->getResource();
ErrorHandler::start(E_WARNING);
$cookie = '';
$results = [];
do {
ldap_control_paged_result($resource, $pageSize, true, $cookie);

$result = ldap_search($resource, $basedn, $filter,
$attributes
);

foreach (ldap_get_entries($resource, $result) as $item){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see an issue here as ldap_get_entries returns a rather awkward array containing an entry count followed by a numerically indext list of entries. So for each iteration in the do...while we will have an entry count pushed to the results array. I doubt that is what you had in mind...

array_push($results, (array)$item);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This array_push causes the values of the count entries to be added multiple times as array.

So you weill have some arbitrary array entries in the result that contain [$pagesize] ...

}

ldap_control_paged_result_response($resource, $result, $cookie);
} while ($cookie);
ErrorHandler::stop();

if (count($results) == 0) {
Copy link
Member

@samsonasik samsonasik May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use truthy check:

Suggested change
if (count($results) == 0) {
if (! $results) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather see this test here:

Suggested change
if (count($results) == 0) {
if ($results === []) {

This checks for an untouched $results. As soon as something is in that array we have to assume something worked correctly.

throw new Exception\LdapException($this, 'searching: ' . $filter);
}
return $results;
}

/**
* Extension point for collection creation
*
Expand Down