2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
namespace BookStack\Access;
|
2016-01-16 07:21:47 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Ldap
|
|
|
|
* An object-orientated thin abstraction wrapper for common PHP LDAP functions.
|
|
|
|
* Allows the standard LDAP functions to be mocked for testing.
|
|
|
|
*/
|
|
|
|
class Ldap
|
|
|
|
{
|
|
|
|
/**
|
2021-11-06 08:32:01 +08:00
|
|
|
* Connect to an LDAP server.
|
2021-11-07 06:00:33 +08:00
|
|
|
*
|
2023-05-30 20:10:05 +08:00
|
|
|
* @return resource|\LDAP\Connection|false
|
2016-01-16 07:21:47 +08:00
|
|
|
*/
|
2023-05-30 20:10:05 +08:00
|
|
|
public function connect(string $hostName)
|
2016-01-16 07:21:47 +08:00
|
|
|
{
|
2023-05-30 20:10:05 +08:00
|
|
|
return ldap_connect($hostName);
|
2016-01-16 07:21:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-30 20:10:05 +08:00
|
|
|
* Set the value of an LDAP option for the given connection.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2023-05-30 20:10:05 +08:00
|
|
|
* @param resource|\LDAP\Connection|null $ldapConnection
|
2016-01-16 07:21:47 +08:00
|
|
|
*/
|
2023-05-30 20:10:05 +08:00
|
|
|
public function setOption($ldapConnection, int $option, mixed $value): bool
|
2016-01-16 07:21:47 +08:00
|
|
|
{
|
|
|
|
return ldap_set_option($ldapConnection, $option, $value);
|
|
|
|
}
|
|
|
|
|
2021-02-08 04:00:04 +08:00
|
|
|
/**
|
|
|
|
* Start TLS on the given LDAP connection.
|
|
|
|
*/
|
|
|
|
public function startTls($ldapConnection): bool
|
|
|
|
{
|
|
|
|
return ldap_start_tls($ldapConnection);
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:38:07 +08:00
|
|
|
/**
|
2023-05-30 20:10:05 +08:00
|
|
|
* Set the version number for the given LDAP connection.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2023-05-30 20:10:05 +08:00
|
|
|
* @param resource|\LDAP\Connection $ldapConnection
|
2016-05-02 18:38:07 +08:00
|
|
|
*/
|
2021-11-06 08:32:01 +08:00
|
|
|
public function setVersion($ldapConnection, int $version): bool
|
2016-05-02 18:38:07 +08:00
|
|
|
{
|
|
|
|
return $this->setOption($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $version);
|
|
|
|
}
|
|
|
|
|
2016-01-16 07:21:47 +08:00
|
|
|
/**
|
|
|
|
* Search LDAP tree using the provided filter.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2023-05-30 20:10:05 +08:00
|
|
|
* @param resource|\LDAP\Connection $ldapConnection
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2023-05-30 20:10:05 +08:00
|
|
|
* @return resource|\LDAP\Result
|
2016-01-16 07:21:47 +08:00
|
|
|
*/
|
2023-05-30 20:10:05 +08:00
|
|
|
public function search($ldapConnection, string $baseDn, string $filter, array $attributes = null)
|
2016-01-16 07:21:47 +08:00
|
|
|
{
|
|
|
|
return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-30 20:10:05 +08:00
|
|
|
* Get entries from an LDAP search result.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2023-05-30 20:10:05 +08:00
|
|
|
* @param resource|\LDAP\Connection $ldapConnection
|
|
|
|
* @param resource|\LDAP\Result $ldapSearchResult
|
2016-01-16 07:21:47 +08:00
|
|
|
*/
|
2023-05-30 20:10:05 +08:00
|
|
|
public function getEntries($ldapConnection, $ldapSearchResult): array|false
|
2016-01-16 07:21:47 +08:00
|
|
|
{
|
|
|
|
return ldap_get_entries($ldapConnection, $ldapSearchResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search and get entries immediately.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2023-05-30 20:10:05 +08:00
|
|
|
* @param resource|\LDAP\Connection $ldapConnection
|
2016-01-16 07:21:47 +08:00
|
|
|
*/
|
2023-05-30 20:10:05 +08:00
|
|
|
public function searchAndGetEntries($ldapConnection, string $baseDn, string $filter, array $attributes = null): array|false
|
2016-01-16 07:21:47 +08:00
|
|
|
{
|
|
|
|
$search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2016-01-16 07:21:47 +08:00
|
|
|
return $this->getEntries($ldapConnection, $search);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind to LDAP directory.
|
2021-06-26 23:23:15 +08:00
|
|
|
*
|
2023-05-30 20:10:05 +08:00
|
|
|
* @param resource|\LDAP\Connection $ldapConnection
|
2016-01-16 07:21:47 +08:00
|
|
|
*/
|
2023-05-30 20:10:05 +08:00
|
|
|
public function bind($ldapConnection, string $bindRdn = null, string $bindPassword = null): bool
|
2016-01-16 07:21:47 +08:00
|
|
|
{
|
|
|
|
return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
|
|
|
|
}
|
2018-12-21 04:04:09 +08:00
|
|
|
|
|
|
|
/**
|
2023-05-30 20:10:05 +08:00
|
|
|
* Explode an LDAP dn string into an array of components.
|
2018-12-21 04:04:09 +08:00
|
|
|
*/
|
2023-05-30 20:10:05 +08:00
|
|
|
public function explodeDn(string $dn, int $withAttrib): array|false
|
2018-12-21 04:04:09 +08:00
|
|
|
{
|
|
|
|
return ldap_explode_dn($dn, $withAttrib);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape a string for use in an LDAP filter.
|
|
|
|
*/
|
2023-05-30 20:10:05 +08:00
|
|
|
public function escape(string $value, string $ignore = '', int $flags = 0): string
|
2018-12-21 04:04:09 +08:00
|
|
|
{
|
|
|
|
return ldap_escape($value, $ignore, $flags);
|
|
|
|
}
|
2016-11-15 18:10:12 +08:00
|
|
|
}
|