3 namespace BookStack\Auth\Access\Ldap;
8 * An object-orientated wrapper for core ldap functions,
9 * holding an internal connection instance.
14 * The core ldap connection resource.
17 protected $connection;
19 public function __construct(string $hostName, int $port)
21 $this->connection = $this->connect($hostName, $port);
25 * Connect to an LDAP server.
29 protected function connect(string $hostName, int $port)
31 return ldap_connect($hostName, $port);
35 * Set the value of a LDAP option for the current connection.
39 public function setOption(int $option, $value): bool
41 return ldap_set_option($this->connection, $option, $value);
45 * Start TLS for this LDAP connection.
47 public function startTls(): bool
49 return ldap_start_tls($this->connection);
53 * Set the version number for this ldap connection.
55 public function setVersion(int $version): bool
57 return $this->setOption(LDAP_OPT_PROTOCOL_VERSION, $version);
61 * Search LDAP tree using the provided filter.
65 public function search(string $baseDn, string $filter, array $attributes = null)
67 return ldap_search($this->connection, $baseDn, $filter, $attributes);
71 * Get entries from an ldap search result.
73 * @param resource $ldapSearchResult
76 public function getEntries($ldapSearchResult)
78 return ldap_get_entries($this->connection, $ldapSearchResult);
82 * Search and get entries immediately.
86 public function searchAndGetEntries(string $baseDn, string $filter, array $attributes = null)
88 $search = $this->search($baseDn, $filter, $attributes);
90 return $this->getEntries($search);
94 * Bind to LDAP directory.
96 * @throws ErrorException
98 public function bind(string $bindRdn = null, string $bindPassword = null): bool
100 return ldap_bind($this->connection, $bindRdn, $bindPassword);
104 * Explode a LDAP dn string into an array of components.
106 * @return array|false
108 public static function explodeDn(string $dn, int $withAttrib)
110 return ldap_explode_dn($dn, $withAttrib);
114 * Escape a string for use in an LDAP filter.
116 public static function escape(string $value, string $ignore = '', int $flags = 0): string
118 return ldap_escape($value, $ignore, $flags);
122 * Set a non-connection-specific LDAP option.
123 * @param mixed $value
125 public static function setGlobalOption(int $option, $value): bool
127 return ldap_set_option(null, $option, $value);