3 namespace Adldap\Connections
;
5 use Adldap\AdldapException
;
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
;
19 * The active LDAP connection.
23 protected $connection;
26 * Stores the bool whether or not
27 * the current connection is bound.
31 protected $bound = false;
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
42 protected $useSSL = false;
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
52 protected $useTLS = false;
57 public function isUsingSSL()
65 public function isUsingTLS()
73 public function isBound()
81 public function canChangePasswords()
83 return $this->isUsingSSL() ||
$this->isUsingTLS();
89 public function ssl($enabled = true)
91 $this->useSSL
= $enabled;
99 public function tls($enabled = true)
101 $this->useTLS
= $enabled;
109 public function getConnection()
111 return $this->connection
;
117 public function getEntries($searchResults)
119 return ldap_get_entries($this->getConnection(), $searchResults);
125 public function getFirstEntry($searchResults)
127 return ldap_first_entry($this->getConnection(), $searchResults);
133 public function getNextEntry($entry)
135 return ldap_next_entry($this->getConnection(), $entry);
141 public function getAttributes($entry)
143 return ldap_get_attributes($this->getConnection(), $entry);
149 public function countEntries($searchResults)
151 return ldap_count_entries($this->getConnection(), $searchResults);
157 public function compare($dn, $attribute, $value)
159 return ldap_compare($this->getConnection(), $dn, $attribute, $value);
165 public function getLastError()
167 return ldap_error($this->getConnection());
173 public function getValuesLen($entry, $attribute)
175 return ldap_get_values_len($this->getConnection(), $entry, $attribute);
181 public function setOption($option, $value)
183 return ldap_set_option($this->getConnection(), $option, $value);
189 public function setOptions(array $options = [])
191 foreach ($options as $option => $value) {
192 $this->setOption($option, $value);
199 public function setRebindCallback(callable
$callback)
201 return ldap_set_rebind_proc($this->getConnection(), $callback);
207 public function startTLS()
209 return ldap_start_tls($this->getConnection());
215 public function connect($hosts = [], $port = '389')
217 $connections = $this->getConnectionString($hosts, $this->getProtocol(), $port);
219 return $this->connection
= ldap_connect($connections);
225 public function close()
227 $connection = $this->getConnection();
229 return is_resource($connection) ?
ldap_close($connection) : false;
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);
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);
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);
259 public function bind($username, $password, $sasl = false)
261 if ($this->isUsingTLS()) {
266 return $this->bound
= ldap_sasl_bind($this->getConnection(), null, null, 'GSSAPI');
269 return $this->bound
= ldap_bind($this->getConnection(), $username, $password);
275 public function add($dn, array $entry)
277 return ldap_add($this->getConnection(), $dn, $entry);
283 public function delete($dn)
285 return ldap_delete($this->getConnection(), $dn);
291 public function rename($dn, $newRdn, $newParent, $deleteOldRdn = false)
293 return ldap_rename($this->getConnection(), $dn, $newRdn, $newParent, $deleteOldRdn);
299 public function modify($dn, array $entry)
301 return ldap_modify($this->getConnection(), $dn, $entry);
307 public function modifyBatch($dn, array $values)
309 return ldap_modify_batch($this->getConnection(), $dn, $values);
315 public function modAdd($dn, array $entry)
317 return ldap_mod_add($this->getConnection(), $dn, $entry);
323 public function modReplace($dn, array $entry)
325 return ldap_mod_replace($this->getConnection(), $dn, $entry);
331 public function modDelete($dn, array $entry)
333 return ldap_mod_del($this->getConnection(), $dn, $entry);
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.'
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.'
367 public function errNo()
369 return ldap_errno($this->getConnection());
375 public function getExtendedError()
377 return $this->getDiagnosticMessage();
383 public function getExtendedErrorHex()
385 if (preg_match("/(?<=data\s).*?(?=\,)/", $this->getExtendedError(), $code)) {
393 public function getExtendedErrorCode()
395 return $this->extractDiagnosticCode($this->getExtendedError());
401 public function err2Str($number)
403 return ldap_err2str($number);
409 public function getDiagnosticMessage()
411 ldap_get_option($this->getConnection(), LDAP_OPT_ERROR_STRING
, $diagnosticMessage);
413 return $diagnosticMessage;
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.
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
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}";
454 return implode(' ', $hosts);