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