3 namespace BookStack\Auth\Access\Ldap;
8 * App provided config array.
11 protected array $config;
13 public function __construct(array $config)
15 $this->config = $config;
19 * Get a value from the config.
21 public function get(string $key)
23 return $this->config[$key] ?? null;
27 * Parse the potentially multi-value LDAP server host string and return an array of host/port detail pairs.
28 * Multiple hosts are separated with a semicolon, for example: 'ldap.example.com:8069;ldaps://ldap.example.com'
30 * @return array<array{host: string, port: int}>
32 public function getServers(): array
34 $serverStringList = explode(';', $this->get('server'));
36 return array_map(fn ($serverStr) => $this->parseSingleServerString($serverStr), $serverStringList);
40 * Parse an LDAP server string and return the host and port for a connection.
41 * Is flexible to formats such as 'ldap.example.com:8069' or 'ldaps://ldap.example.com'.
43 * @return array{host: string, port: int}
45 protected function parseSingleServerString(string $serverString): array
47 $serverNameParts = explode(':', trim($serverString));
49 // If we have a protocol just return the full string since PHP will ignore a separate port.
50 if ($serverNameParts[0] === 'ldaps' || $serverNameParts[0] === 'ldap') {
51 return ['host' => $serverString, 'port' => 389];
54 // Otherwise, extract the port out
55 $hostName = $serverNameParts[0];
56 $ldapPort = (count($serverNameParts) > 1) ? intval($serverNameParts[1]) : 389;
58 return ['host' => $hostName, 'port' => $ldapPort];