]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/Ldap.php
Added testing coverage for tag index
[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      * @return resource
15      */
16     public function connect(string $hostName, int $port)
17     {
18         return ldap_connect($hostName, $port);
19     }
20
21     /**
22      * Set the value of a LDAP option for the given connection.
23      *
24      * @param resource $ldapConnection
25      * @param mixed    $value
26      */
27     public function setOption($ldapConnection, int $option, $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 $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   $ldapConnection
54      * @param string     $baseDn
55      * @param string     $filter
56      * @param array|null $attributes
57      *
58      * @return resource
59      */
60     public function search($ldapConnection, $baseDn, $filter, array $attributes = null)
61     {
62         return ldap_search($ldapConnection, $baseDn, $filter, $attributes);
63     }
64
65     /**
66      * Get entries from an ldap search result.
67      *
68      * @param resource $ldapConnection
69      * @param resource $ldapSearchResult
70      *
71      * @return array
72      */
73     public function getEntries($ldapConnection, $ldapSearchResult)
74     {
75         return ldap_get_entries($ldapConnection, $ldapSearchResult);
76     }
77
78     /**
79      * Search and get entries immediately.
80      *
81      * @param resource   $ldapConnection
82      * @param string     $baseDn
83      * @param string     $filter
84      * @param array|null $attributes
85      *
86      * @return resource
87      */
88     public function searchAndGetEntries($ldapConnection, $baseDn, $filter, array $attributes = null)
89     {
90         $search = $this->search($ldapConnection, $baseDn, $filter, $attributes);
91
92         return $this->getEntries($ldapConnection, $search);
93     }
94
95     /**
96      * Bind to LDAP directory.
97      *
98      * @param resource $ldapConnection
99      * @param string   $bindRdn
100      * @param string   $bindPassword
101      *
102      * @return bool
103      */
104     public function bind($ldapConnection, $bindRdn = null, $bindPassword = null)
105     {
106         return ldap_bind($ldapConnection, $bindRdn, $bindPassword);
107     }
108
109     /**
110      * Explode a LDAP dn string into an array of components.
111      *
112      * @param string $dn
113      * @param int    $withAttrib
114      *
115      * @return array
116      */
117     public function explodeDn(string $dn, int $withAttrib)
118     {
119         return ldap_explode_dn($dn, $withAttrib);
120     }
121
122     /**
123      * Escape a string for use in an LDAP filter.
124      *
125      * @param string $value
126      * @param string $ignore
127      * @param int    $flags
128      *
129      * @return string
130      */
131     public function escape(string $value, string $ignore = '', int $flags = 0)
132     {
133         return ldap_escape($value, $ignore, $flags);
134     }
135 }