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