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