LdapConnection: Don't utilize ldap_control_paged_result() on PHP 7.3+

https://www.php.net/manual/en/migration74.deprecated.php#migration74.deprecated.ldap
This commit is contained in:
Johannes Meyer 2019-12-04 09:33:13 +01:00
parent e93fbdb802
commit 7bc1893a24

View File

@ -889,13 +889,17 @@ class LdapConnection implements Selectable, Inspectable
} }
} }
$legacyControlHandling = version_compare(PHP_VERSION, '7.3.0') < 0;
$count = 0; $count = 0;
$cookie = ''; $cookie = '';
$entries = array(); $entries = array();
do { do {
if ($legacyControlHandling) {
// Do not request the pagination control as a critical extension, as we want the // Do not request the pagination control as a critical extension, as we want the
// server to return results even if the paged search request cannot be satisfied // server to return results even if the paged search request cannot be satisfied
ldap_control_paged_result($ds, $pageSize, false, $cookie); ldap_control_paged_result($ds, $pageSize, false, $cookie);
}
if ($serverSorting && $query->hasOrder()) { if ($serverSorting && $query->hasOrder()) {
ldap_set_option($ds, LDAP_OPT_SERVER_CONTROLS, array( ldap_set_option($ds, LDAP_OPT_SERVER_CONTROLS, array(
@ -910,7 +914,10 @@ class LdapConnection implements Selectable, Inspectable
$query, $query,
array_values($fields), array_values($fields),
0, 0,
($serverSorting || ! $query->hasOrder()) && $limit ? $offset + $limit : 0 ($serverSorting || ! $query->hasOrder()) && $limit ? $offset + $limit : 0,
0,
LDAP_DEREF_NEVER,
$legacyControlHandling ? null : $pageSize
); );
if ($results === false) { if ($results === false) {
if (ldap_errno($ds) === self::LDAP_NO_SUCH_OBJECT) { if (ldap_errno($ds) === self::LDAP_NO_SUCH_OBJECT) {
@ -976,7 +983,7 @@ class LdapConnection implements Selectable, Inspectable
&& ($entry = ldap_next_entry($ds, $entry)) && ($entry = ldap_next_entry($ds, $entry))
); );
if (false === @ldap_control_paged_result_response($ds, $results, $cookie)) { if ($legacyControlHandling && false === @ldap_control_paged_result_response($ds, $results, $cookie)) {
// If the page size is greater than or equal to the sizeLimit value, the server should ignore the // If the page size is greater than or equal to the sizeLimit value, the server should ignore the
// control as the request can be satisfied in a single page: https://www.ietf.org/rfc/rfc2696.txt // control as the request can be satisfied in a single page: https://www.ietf.org/rfc/rfc2696.txt
// This applies no matter whether paged search requests are permitted or not. You're done once you // This applies no matter whether paged search requests are permitted or not. You're done once you
@ -993,7 +1000,7 @@ class LdapConnection implements Selectable, Inspectable
ldap_free_result($results); ldap_free_result($results);
} while ($cookie && (! $serverSorting || $limit === 0 || count($entries) < $limit)); } while ($cookie && (! $serverSorting || $limit === 0 || count($entries) < $limit));
if ($cookie) { if ($legacyControlHandling && $cookie) {
// A sequence of paged search requests is abandoned by the client sending a search request containing a // A sequence of paged search requests is abandoned by the client sending a search request containing a
// pagedResultsControl with the size set to zero (0) and the cookie set to the last cookie returned by // pagedResultsControl with the size set to zero (0) and the cookie set to the last cookie returned by
// the server: https://www.ietf.org/rfc/rfc2696.txt // the server: https://www.ietf.org/rfc/rfc2696.txt
@ -1219,6 +1226,7 @@ class LdapConnection implements Selectable, Inspectable
* @param int $sizelimit Enables you to limit the count of entries fetched * @param int $sizelimit Enables you to limit the count of entries fetched
* @param int $timelimit Sets the number of seconds how long is spend on the search * @param int $timelimit Sets the number of seconds how long is spend on the search
* @param int $deref * @param int $deref
* @param int $pageSize The page size to request (Only supported with PHP v7.3+)
* *
* @return resource|bool A search result identifier or false on error * @return resource|bool A search result identifier or false on error
* *
@ -1230,7 +1238,8 @@ class LdapConnection implements Selectable, Inspectable
$attrsonly = 0, $attrsonly = 0,
$sizelimit = 0, $sizelimit = 0,
$timelimit = 0, $timelimit = 0,
$deref = LDAP_DEREF_NEVER $deref = LDAP_DEREF_NEVER,
$pageSize = null
) { ) {
$queryString = (string) $query; $queryString = (string) $query;
$baseDn = $query->getBase() ?: $this->getDn(); $baseDn = $query->getBase() ?: $this->getDn();
@ -1285,6 +1294,20 @@ class LdapConnection implements Selectable, Inspectable
throw new LogicException('LDAP scope %s not supported by ldapSearch', $scope); throw new LogicException('LDAP scope %s not supported by ldapSearch', $scope);
} }
$serverctrls = [];
if ($pageSize !== null) {
$serverctrls[] = [
'oid' => LDAP_CONTROL_PAGEDRESULTS,
// Do not request the pagination control as a critical extension, as we want the
// server to return results even if the paged search request cannot be satisfied
'iscritical' => false,
'value' => [
'size' => $pageSize,
'cookie' => ''
]
];
}
return @$function( return @$function(
$this->getConnection(), $this->getConnection(),
$baseDn, $baseDn,
@ -1293,7 +1316,8 @@ class LdapConnection implements Selectable, Inspectable
$attrsonly, $attrsonly,
$sizelimit, $sizelimit,
$timelimit, $timelimit,
$deref $deref,
$serverctrls
); );
} }