]> BookStack Code Mirror - bookstack/blob - app/Access/Ldap.php
Vectors: Added command to regenerate for all
[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 \LDAP\Result|array|false
56      */
57     public function search($ldapConnection, string $baseDn, string $filter, array $attributes = [])
58     {
59         return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
60     }
61
62     /**
63      * Read an entry from the LDAP tree.
64      *
65      * @param resource|\Ldap\Connection $ldapConnection
66      *
67      * @return \LDAP\Result|array|false
68      */
69     public function read($ldapConnection, string $baseDn, string $filter, array $attributes = [])
70     {
71         return ldap_read($ldapConnection, $baseDn, $filter, $attributes);
72     }
73
74     /**
75      * Get entries from an LDAP search result.
76      *
77      * @param resource|\LDAP\Connection $ldapConnection
78      * @param resource|\LDAP\Result $ldapSearchResult
79      */
80     public function getEntries($ldapConnection, $ldapSearchResult): array|false
81     {
82         return ldap_get_entries($ldapConnection, $ldapSearchResult);
83     }
84
85     /**
86      * Search and get entries immediately.
87      *
88      * @param resource|\LDAP\Connection   $ldapConnection
89      */
90     public function searchAndGetEntries($ldapConnection, string $baseDn, string $filter, array $attributes = []): array|false
91     {
92         $search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
93
94         return $this->getEntries($ldapConnection, $search);
95     }
96
97     /**
98      * Bind to LDAP directory.
99      *
100      * @param resource|\LDAP\Connection $ldapConnection
101      */
102     public function bind($ldapConnection, ?string $bindRdn = null, ?string $bindPassword = null): bool
103     {
104         return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
105     }
106
107     /**
108      * Explode an LDAP dn string into an array of components.
109      */
110     public function explodeDn(string $dn, int $withAttrib): array|false
111     {
112         return ldap_explode_dn($dn, $withAttrib);
113     }
114
115     /**
116      * Escape a string for use in an LDAP filter.
117      */
118     public function escape(string $value, string $ignore = '', int $flags = 0): string
119     {
120         return ldap_escape($value, $ignore, $flags);
121     }
122 }