composer package updates
[openemr.git] / vendor / adldap2 / adldap2 / src / Connections / Ldap.php
blobc758b3084eab9492b8bb2cb3e340998659d44b20
1 <?php
3 namespace Adldap\Connections;
5 use Adldap\AdldapException;
7 /**
8 * Class Ldap
10 * A class that abstracts PHP's LDAP functions and stores the bound connection.
12 * @package Adldap\Connections
14 class Ldap implements ConnectionInterface
16 use LdapFunctionSupportTrait;
18 /**
19 * The active LDAP connection.
21 * @var resource
23 protected $connection;
25 /**
26 * Stores the bool whether or not
27 * the current connection is bound.
29 * @var bool
31 protected $bound = false;
33 /**
34 * Stores the bool to tell the connection
35 * whether or not to use SSL.
37 * To use SSL, your server must support LDAP over SSL.
38 * http://adldap.sourceforge.net/wiki/doku.php?id=ldap_over_ssl
40 * @var bool
42 protected $useSSL = false;
44 /**
45 * Stores the bool to tell the connection
46 * whether or not to use TLS.
48 * If you wish to use TLS you should ensure that $useSSL is set to false and vice-versa
50 * @var bool
52 protected $useTLS = false;
54 /**
55 * {@inheritdoc}
57 public function isUsingSSL()
59 return $this->useSSL;
62 /**
63 * {@inheritdoc}
65 public function isUsingTLS()
67 return $this->useTLS;
70 /**
71 * {@inheritdoc}
73 public function isBound()
75 return $this->bound;
78 /**
79 * {@inheritdoc}
81 public function canChangePasswords()
83 return $this->isUsingSSL() || $this->isUsingTLS();
86 /**
87 * {@inheritdoc}
89 public function ssl($enabled = true)
91 $this->useSSL = $enabled;
93 return $this;
96 /**
97 * {@inheritdoc}
99 public function tls($enabled = true)
101 $this->useTLS = $enabled;
103 return $this;
107 * {@inheritdoc}
109 public function getConnection()
111 return $this->connection;
115 * {@inheritdoc}
117 public function getEntries($searchResults)
119 return ldap_get_entries($this->getConnection(), $searchResults);
123 * {@inheritdoc}
125 public function getFirstEntry($searchResults)
127 return ldap_first_entry($this->getConnection(), $searchResults);
131 * {@inheritdoc}
133 public function getNextEntry($entry)
135 return ldap_next_entry($this->getConnection(), $entry);
139 * {@inheritdoc}
141 public function getAttributes($entry)
143 return ldap_get_attributes($this->getConnection(), $entry);
147 * {@inheritdoc}
149 public function countEntries($searchResults)
151 return ldap_count_entries($this->getConnection(), $searchResults);
155 * {@inheritdoc}
157 public function compare($dn, $attribute, $value)
159 return ldap_compare($this->getConnection(), $dn, $attribute, $value);
163 * {@inheritdoc}
165 public function getLastError()
167 return ldap_error($this->getConnection());
171 * {@inheritdoc}
173 public function getValuesLen($entry, $attribute)
175 return ldap_get_values_len($this->getConnection(), $entry, $attribute);
179 * {@inheritdoc}
181 public function setOption($option, $value)
183 return ldap_set_option($this->getConnection(), $option, $value);
187 * {@inheritdoc}
189 public function setOptions(array $options = [])
191 foreach ($options as $option => $value) {
192 $this->setOption($option, $value);
197 * {@inheritdoc}
199 public function setRebindCallback(callable $callback)
201 return ldap_set_rebind_proc($this->getConnection(), $callback);
205 * {@inheritdoc}
207 public function startTLS()
209 return ldap_start_tls($this->getConnection());
213 * {@inheritdoc}
215 public function connect($hosts = [], $port = '389')
217 $connections = $this->getConnectionString($hosts, $this->getProtocol(), $port);
219 return $this->connection = ldap_connect($connections);
223 * {@inheritdoc}
225 public function close()
227 $connection = $this->getConnection();
229 return is_resource($connection) ? ldap_close($connection) : false;
233 * {@inheritdoc}
235 public function search($dn, $filter, array $fields, $onlyAttributes = false, $size = 0, $time = 0)
237 return ldap_search($this->getConnection(), $dn, $filter, $fields, $onlyAttributes, $size, $time);
241 * {@inheritdoc}
243 public function listing($dn, $filter, array $fields, $onlyAttributes = false, $size = 0, $time = 0)
245 return ldap_list($this->getConnection(), $dn, $filter, $fields, $onlyAttributes, $size, $time);
249 * {@inheritdoc}
251 public function read($dn, $filter, array $fields, $onlyAttributes = false, $size = 0, $time = 0)
253 return ldap_read($this->getConnection(), $dn, $filter, $fields, $onlyAttributes, $size, $time);
257 * {@inheritdoc}
259 public function bind($username, $password, $sasl = false)
261 if ($this->isUsingTLS()) {
262 $this->startTLS();
265 if ($sasl) {
266 return $this->bound = ldap_sasl_bind($this->getConnection(), null, null, 'GSSAPI');
269 return $this->bound = ldap_bind($this->getConnection(), $username, $password);
273 * {@inheritdoc}
275 public function add($dn, array $entry)
277 return ldap_add($this->getConnection(), $dn, $entry);
281 * {@inheritdoc}
283 public function delete($dn)
285 return ldap_delete($this->getConnection(), $dn);
289 * {@inheritdoc}
291 public function rename($dn, $newRdn, $newParent, $deleteOldRdn = false)
293 return ldap_rename($this->getConnection(), $dn, $newRdn, $newParent, $deleteOldRdn);
297 * {@inheritdoc}
299 public function modify($dn, array $entry)
301 return ldap_modify($this->getConnection(), $dn, $entry);
305 * {@inheritdoc}
307 public function modifyBatch($dn, array $values)
309 return ldap_modify_batch($this->getConnection(), $dn, $values);
313 * {@inheritdoc}
315 public function modAdd($dn, array $entry)
317 return ldap_mod_add($this->getConnection(), $dn, $entry);
321 * {@inheritdoc}
323 public function modReplace($dn, array $entry)
325 return ldap_mod_replace($this->getConnection(), $dn, $entry);
329 * {@inheritdoc}
331 public function modDelete($dn, array $entry)
333 return ldap_mod_del($this->getConnection(), $dn, $entry);
337 * {@inheritdoc}
339 public function controlPagedResult($pageSize = 1000, $isCritical = false, $cookie = '')
341 if ($this->isPagingSupported()) {
342 return ldap_control_paged_result($this->getConnection(), $pageSize, $isCritical, $cookie);
345 throw new AdldapException(
346 'LDAP Pagination is not supported on your current PHP installation.'
351 * {@inheritdoc}
353 public function controlPagedResultResponse($result, &$cookie)
355 if ($this->isPagingSupported()) {
356 return ldap_control_paged_result_response($this->getConnection(), $result, $cookie);
359 throw new AdldapException(
360 'LDAP Pagination is not supported on your current PHP installation.'
365 * {@inheritdoc}
367 public function errNo()
369 return ldap_errno($this->getConnection());
373 * {@inheritdoc}
375 public function getExtendedError()
377 return $this->getDiagnosticMessage();
381 * {@inheritdoc}
383 public function getExtendedErrorHex()
385 if (preg_match("/(?<=data\s).*?(?=\,)/", $this->getExtendedError(), $code)) {
386 return $code[0];
391 * {@inheritdoc}
393 public function getExtendedErrorCode()
395 return $this->extractDiagnosticCode($this->getExtendedError());
399 * {@inheritdoc}
401 public function err2Str($number)
403 return ldap_err2str($number);
407 * {@inheritdoc}
409 public function getDiagnosticMessage()
411 ldap_get_option($this->getConnection(), LDAP_OPT_ERROR_STRING, $diagnosticMessage);
413 return $diagnosticMessage;
417 * {@inheritdoc}
419 public function extractDiagnosticCode($message)
421 preg_match('/^([\da-fA-F]+):/', $message, $matches);
423 return isset($matches[1]) ? $matches[1] : false;
427 * Returns the LDAP protocol to utilize for the current connection.
429 * @return string
431 public function getProtocol()
433 return $this->isUsingSSL() ? $this::PROTOCOL_SSL : $this::PROTOCOL;
437 * Generates an LDAP connection string for each host given.
439 * @param string|array $hosts
440 * @param string $protocol
441 * @param string $port
443 * @return string
445 protected function getConnectionString($hosts = [], $protocol, $port)
447 // Normalize hosts into an array.
448 $hosts = is_array($hosts) ? $hosts : [$hosts];
450 $hosts = array_map(function ($host) use ($protocol, $port) {
451 return "{$protocol}{$host}:{$port}";
452 }, $hosts);
454 return implode(' ', $hosts);