/
HandlesConnection.php
263 lines (231 loc) · 5.84 KB
/
HandlesConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace LdapRecord;
use Closure;
use ErrorException;
use Exception;
trait HandlesConnection
{
/**
* The LDAP host that is currently connected.
*
* @var string|null
*/
protected $host;
/**
* The LDAP connection resource.
*
* @var resource|null
*/
protected $connection;
/**
* The bound status of the connection.
*
* @var bool
*/
protected $bound = false;
/**
* Whether the connection must be bound over SSL.
*
* @var bool
*/
protected $useSSL = false;
/**
* Whether the connection must be bound over TLS.
*
* @var bool
*/
protected $useTLS = false;
/**
* @inheritdoc
*/
public function isUsingSSL()
{
return $this->useSSL;
}
/**
* @inheritdoc
*/
public function isUsingTLS()
{
return $this->useTLS;
}
/**
* @inheritdoc
*/
public function isBound()
{
return $this->bound;
}
/**
* @inheritdoc
*/
public function isConnected()
{
return ! is_null($this->connection);
}
/**
* @inheritdoc
*/
public function canChangePasswords()
{
return $this->isUsingSSL() || $this->isUsingTLS();
}
/**
* @inheritdoc
*/
public function ssl($enabled = true)
{
$this->useSSL = $enabled;
return $this;
}
/**
* @inheritdoc
*/
public function tls($enabled = true)
{
$this->useTLS = $enabled;
return $this;
}
/**
* @inheritdoc
*/
public function setOptions(array $options = [])
{
foreach ($options as $option => $value) {
$this->setOption($option, $value);
}
}
/**
* @inheritdoc
*/
public function getHost()
{
return $this->host;
}
/**
* @inheritdoc
*/
public function getConnection()
{
return $this->connection;
}
/**
* @inheritdoc
*/
public function getProtocol()
{
return $this->isUsingSSL() ? LdapInterface::PROTOCOL_SSL : LdapInterface::PROTOCOL;
}
/**
* @inheritdoc
*/
public function getExtendedError()
{
return $this->getDiagnosticMessage();
}
/**
* Convert warnings to exceptions for the given operation.
*
* @param Closure $operation
*
* @return mixed
*
* @throws LdapRecordException
*/
protected function executeFailableOperation(Closure $operation)
{
// If some older versions of PHP, errors are reported instead of throwing
// exceptions, which could be a significant detriment to our application.
// Here, we will enforce these operations to throw exceptions instead.
set_error_handler(function (int $severity, string $message, string $file, int $line): bool {
if (! $this->shouldBypassError($message)) {
throw new ErrorException($message, $severity, $severity, $file, $line);
}
return true;
});
try {
if (($result = $operation()) !== false) {
return $result;
}
// If the failed query operation was a based on a query being executed
// -- such as a search, read, or listing, then we can safely return
// the failed response here and prevent throwning an exception.
if ($this->shouldBypassFailure($method = debug_backtrace()[1]['function'])) {
return $result;
}
throw new Exception("LDAP operation [$method] failed.");
} catch (ErrorException $e) {
throw LdapRecordException::withDetailedError($e, $this->getDetailedError());
} finally {
restore_error_handler();
}
}
/**
* Determine if the failed operation should be bypassed.
*
* @param string $method
*
* @return bool
*/
protected function shouldBypassFailure($method)
{
return in_array($method, ['search', 'read', 'listing']);
}
/**
* Determine if the error should be bypassed.
*
* @param string $error
*
* @return bool
*/
protected function shouldBypassError($error)
{
return $this->causedByPaginationSupport($error) || $this->causedBySizeLimit($error) || $this->causedByNoSuchObject($error);
}
/**
* Determine if the current PHP version supports server controls.
*
* @deprecated since v2.5.0
*
* @return bool
*/
public function supportsServerControlsInMethods()
{
return version_compare(PHP_VERSION, '7.3.0') >= 0;
}
/**
* Generates an LDAP connection string for each host given.
*
* @param string|array $hosts
* @param string $port
*
* @return string
*/
protected function makeConnectionUris($hosts, $port)
{
// If an attempt to connect via SSL protocol is being performed,
// and we are still using the default port, we will swap it
// for the default SSL port, for developer convenience.
if ($this->isUsingSSL() && $port == LdapInterface::PORT) {
$port = LdapInterface::PORT_SSL;
}
// The blank space here is intentional. PHP's LDAP extension
// requires additional hosts to be seperated by a blank
// space, so that it can parse each individually.
return implode(' ', $this->assembleHostUris($hosts, $port));
}
/**
* Assemble the host URI strings.
*
* @param array|string $hosts
* @param string $port
*
* @return array
*/
protected function assembleHostUris($hosts, $port)
{
return array_map(function ($host) use ($port) {
return "{$this->getProtocol()}{$host}:{$port}";
}, (array) $hosts);
}
}