Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d332b30

Browse files
lucasabanicolas-grekas
authored andcommitted
[Ldap] Bypass the use of ldap_control_paged_result on PHP >= 7.3
1 parent b3a20e4 commit d332b30

File tree

1 file changed

+66
-13
lines changed
  • src/Symfony/Component/Ldap/Adapter/ExtLdap

1 file changed

+66
-13
lines changed

src/Symfony/Component/Ldap/Adapter/ExtLdap/Query.php

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class Query extends AbstractQuery
3030
/** @var resource[] */
3131
private $results;
3232

33+
/** @var array */
34+
private $serverctrls = [];
35+
3336
public function __construct(Connection $connection, string $dn, string $query, array $options = [])
3437
{
3538
parent::__construct($connection, $dn, $query, $options);
@@ -97,22 +100,13 @@ public function execute()
97100
$cookie = '';
98101
do {
99102
if ($pageControl) {
100-
ldap_control_paged_result($con, $pageSize, true, $cookie);
103+
$this->controlPagedResult($con, $pageSize, $cookie);
101104
}
102105
$sizeLimit = $itemsLeft;
103106
if ($pageSize > 0 && $sizeLimit >= $pageSize) {
104107
$sizeLimit = 0;
105108
}
106-
$search = @$func(
107-
$con,
108-
$this->dn,
109-
$this->query,
110-
$this->options['filter'],
111-
$this->options['attrsOnly'],
112-
$sizeLimit,
113-
$this->options['timeout'],
114-
$this->options['deref']
115-
);
109+
$search = $this->callSearchFunction($con, $func, $sizeLimit);
116110

117111
if (false === $search) {
118112
$ldapError = '';
@@ -133,7 +127,7 @@ public function execute()
133127
break;
134128
}
135129
if ($pageControl) {
136-
ldap_control_paged_result_response($con, $search, $cookie);
130+
$cookie = $this->controlPagedResultResponse($con, $search, $cookie);
137131
}
138132
} while (null !== $cookie && '' !== $cookie);
139133

@@ -180,7 +174,8 @@ public function getResources(): array
180174
private function resetPagination()
181175
{
182176
$con = $this->connection->getResource();
183-
ldap_control_paged_result($con, 0);
177+
$this->controlPagedResultResponse($con, 0, '');
178+
$this->serverctrls = [];
184179

185180
// This is a workaround for a bit of a bug in the above invocation
186181
// of ldap_control_paged_result. Instead of indicating to extldap that
@@ -203,4 +198,62 @@ private function resetPagination()
203198
ldap_set_option($con, \LDAP_OPT_SERVER_CONTROLS, $ctl);
204199
}
205200
}
201+
202+
/**
203+
* Sets LDAP pagination controls.
204+
*
205+
* @param resource $con
206+
*/
207+
private function controlPagedResult($con, int $pageSize, string $cookie): bool
208+
{
209+
if (\PHP_VERSION_ID < 70300) {
210+
return ldap_control_paged_result($con, $pageSize, true, $cookie);
211+
}
212+
$this->serverctrls = [
213+
[
214+
'oid' => \LDAP_CONTROL_PAGEDRESULTS,
215+
'isCritical' => true,
216+
'value' => [
217+
'size' => $pageSize,
218+
'cookie' => $cookie,
219+
],
220+
],
221+
];
222+
223+
return true;
224+
}
225+
226+
/**
227+
* Retrieve LDAP pagination cookie.
228+
*
229+
* @param resource $con
230+
* @param resource $result
231+
*/
232+
private function controlPagedResultResponse($con, $result, string $cookie = ''): string
233+
{
234+
if (\PHP_VERSION_ID < 70300) {
235+
ldap_control_paged_result_response($con, $result, $cookie);
236+
237+
return $cookie;
238+
}
239+
ldap_parse_result($con, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls);
240+
241+
return $controls[\LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '';
242+
}
243+
244+
/**
245+
* Calls actual LDAP search function with the prepared options and parameters.
246+
*
247+
* @param resource $con
248+
*
249+
* @return resource
250+
*/
251+
private function callSearchFunction($con, string $func, int $sizeLimit)
252+
{
253+
if (\PHP_VERSION_ID < 70300) {
254+
return @$func($con, $this->dn, $this->query, $this->options['filter'], $this->options['attrsOnly'], $sizeLimit, $this->options['timeout'], $this->options['deref']);
255+
}
256+
257+
return @$func($con, $this->dn, $this->query, $this->options['filter'], $this->options['attrsOnly'], $sizeLimit, $this->options['timeout'], $this->options['deref'], $this->serverctrls);
258+
}
206259
}

0 commit comments

Comments
 (0)