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