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 protected string $hostName;
22 public function __construct(string $hostName, int $port)
24 $this->hostName = $hostName;
26 $this->connection = $this->connect();
30 * Start a connection to an LDAP server.
31 * Does not actually call out to the external server until an action is performed.
35 protected function connect()
37 return ldap_connect($this->hostName, $this->port);
41 * Set the value of a LDAP option for the current connection.
45 public function setOption(int $option, $value): bool
47 return ldap_set_option($this->connection, $option, $value);
51 * Start TLS for this LDAP connection.
53 public function startTls(): bool
55 return ldap_start_tls($this->connection);
59 * Set the version number for this ldap connection.
61 public function setVersion(int $version): bool
63 return $this->setOption(LDAP_OPT_PROTOCOL_VERSION, $version);
67 * Search LDAP tree using the provided filter.
71 public function search(string $baseDn, string $filter, array $attributes = null)
73 return ldap_search($this->connection, $baseDn, $filter, $attributes);
77 * Get entries from an ldap search result.
79 * @param resource $ldapSearchResult
82 public function getEntries($ldapSearchResult)
84 return ldap_get_entries($this->connection, $ldapSearchResult);
88 * Search and get entries immediately.
92 public function searchAndGetEntries(string $baseDn, string $filter, array $attributes = null)
94 $search = $this->search($baseDn, $filter, $attributes);
96 return $this->getEntries($search);
100 * Bind to LDAP directory.
102 * @throws ErrorException
104 public function bind(string $bindRdn = null, string $bindPassword = null): bool
106 return ldap_bind($this->connection, $bindRdn, $bindPassword);
110 * Explode a LDAP dn string into an array of components.
112 * @return array|false
114 public static function explodeDn(string $dn, int $withAttrib)
116 return ldap_explode_dn($dn, $withAttrib);
120 * Escape a string for use in an LDAP filter.
122 public static function escape(string $value, string $ignore = '', int $flags = 0): string
124 return ldap_escape($value, $ignore, $flags);
128 * Set a non-connection-specific LDAP option.
129 * @param mixed $value
131 public static function setGlobalOption(int $option, $value): bool
133 return ldap_set_option(null, $option, $value);