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