3 namespace BookStack\Access;
7 * An object-orientated thin abstraction wrapper for common PHP LDAP functions.
8 * Allows the standard LDAP functions to be mocked for testing.
13 * Connect to an LDAP server.
15 * @return resource|\LDAP\Connection|false
17 public function connect(string $hostName)
19 return ldap_connect($hostName);
23 * Set the value of an LDAP option for the given connection.
25 * @param resource|\LDAP\Connection|null $ldapConnection
27 public function setOption($ldapConnection, int $option, mixed $value): bool
29 return ldap_set_option($ldapConnection, $option, $value);
33 * Start TLS on the given LDAP connection.
35 public function startTls($ldapConnection): bool
37 return ldap_start_tls($ldapConnection);
41 * Set the version number for the given LDAP connection.
43 * @param resource|\LDAP\Connection $ldapConnection
45 public function setVersion($ldapConnection, int $version): bool
47 return $this->setOption($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $version);
51 * Search LDAP tree using the provided filter.
53 * @param resource|\LDAP\Connection $ldapConnection
55 * @return \LDAP\Result|array|false
57 public function search($ldapConnection, string $baseDn, string $filter, array $attributes = [])
59 return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
63 * Read an entry from the LDAP tree.
65 * @param resource|\Ldap\Connection $ldapConnection
67 * @return \LDAP\Result|array|false
69 public function read($ldapConnection, string $baseDn, string $filter, array $attributes = [])
71 return ldap_read($ldapConnection, $baseDn, $filter, $attributes);
75 * Get entries from an LDAP search result.
77 * @param resource|\LDAP\Connection $ldapConnection
78 * @param resource|\LDAP\Result $ldapSearchResult
80 public function getEntries($ldapConnection, $ldapSearchResult): array|false
82 return ldap_get_entries($ldapConnection, $ldapSearchResult);
86 * Search and get entries immediately.
88 * @param resource|\LDAP\Connection $ldapConnection
90 public function searchAndGetEntries($ldapConnection, string $baseDn, string $filter, array $attributes = []): array|false
92 $search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
94 return $this->getEntries($ldapConnection, $search);
98 * Bind to LDAP directory.
100 * @param resource|\LDAP\Connection $ldapConnection
102 public function bind($ldapConnection, ?string $bindRdn = null, ?string $bindPassword = null): bool
104 return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
108 * Explode an LDAP dn string into an array of components.
110 public function explodeDn(string $dn, int $withAttrib): array|false
112 return ldap_explode_dn($dn, $withAttrib);
116 * Escape a string for use in an LDAP filter.
118 public function escape(string $value, string $ignore = '', int $flags = 0): string
120 return ldap_escape($value, $ignore, $flags);