]> BookStack Code Mirror - bookstack/blob - app/Access/Ldap.php
Comments: Fixed failing tests due to unset template variable
[bookstack] / app / Access / Ldap.php
1 <?php
2
3 namespace BookStack\Access;
4
5 /**
6  * Class Ldap
7  * An object-orientated thin abstraction wrapper for common PHP LDAP functions.
8  * Allows the standard LDAP functions to be mocked for testing.
9  */
10 class Ldap
11 {
12     /**
13      * Connect to an LDAP server.
14      *
15      * @return resource|\LDAP\Connection|false
16      */
17     public function connect(string $hostName)
18     {
19         return ldap_connect($hostName);
20     }
21
22     /**
23      * Set the value of an LDAP option for the given connection.
24      *
25      * @param resource|\LDAP\Connection|null $ldapConnection
26      */
27     public function setOption($ldapConnection, int $option, mixed $value): bool
28     {
29         return ldap_set_option($ldapConnection, $option, $value);
30     }
31
32     /**
33      * Start TLS on the given LDAP connection.
34      */
35     public function startTls($ldapConnection): bool
36     {
37         return ldap_start_tls($ldapConnection);
38     }
39
40     /**
41      * Set the version number for the given LDAP connection.
42      *
43      * @param resource|\LDAP\Connection $ldapConnection
44      */
45     public function setVersion($ldapConnection, int $version): bool
46     {
47         return $this->setOption($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $version);
48     }
49
50     /**
51      * Search LDAP tree using the provided filter.
52      *
53      * @param resource|\LDAP\Connection   $ldapConnection
54      *
55      * @return resource|\LDAP\Result
56      */
57     public function search($ldapConnection, string $baseDn, string $filter, array $attributes = null)
58     {
59         return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
60     }
61
62     /**
63      * Get entries from an LDAP search result.
64      *
65      * @param resource|\LDAP\Connection $ldapConnection
66      * @param resource|\LDAP\Result $ldapSearchResult
67      */
68     public function getEntries($ldapConnection, $ldapSearchResult): array|false
69     {
70         return ldap_get_entries($ldapConnection, $ldapSearchResult);
71     }
72
73     /**
74      * Search and get entries immediately.
75      *
76      * @param resource|\LDAP\Connection   $ldapConnection
77      */
78     public function searchAndGetEntries($ldapConnection, string $baseDn, string $filter, array $attributes = null): array|false
79     {
80         $search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
81
82         return $this->getEntries($ldapConnection, $search);
83     }
84
85     /**
86      * Bind to LDAP directory.
87      *
88      * @param resource|\LDAP\Connection $ldapConnection
89      */
90     public function bind($ldapConnection, string $bindRdn = null, string $bindPassword = null): bool
91     {
92         return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
93     }
94
95     /**
96      * Explode an LDAP dn string into an array of components.
97      */
98     public function explodeDn(string $dn, int $withAttrib): array|false
99     {
100         return ldap_explode_dn($dn, $withAttrib);
101     }
102
103     /**
104      * Escape a string for use in an LDAP filter.
105      */
106     public function escape(string $value, string $ignore = '', int $flags = 0): string
107     {
108         return ldap_escape($value, $ignore, $flags);
109     }
110 }