|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/chrmorandi/yii2-ldap for the canonical source repository |
|
4
|
|
|
* @package yii2-ldap |
|
5
|
|
|
* @author Christopher Mota <[email protected]> |
|
6
|
|
|
* @license MIT License - view the LICENSE file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace chrmorandi\ldap; |
|
10
|
|
|
|
|
11
|
|
|
use yii\base\Component; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @property resource $resource |
|
15
|
|
|
* @property boolean $bount |
|
16
|
|
|
* @property int $errNo Error number of the last command |
|
17
|
|
|
* @property string $lastError Error message of the last command |
|
18
|
|
|
* |
|
19
|
|
|
* @author Christopher Mota <[email protected]> |
|
20
|
|
|
* @since 1.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Connection extends Component |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* LDAP protocol string. |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
const PROTOCOL = 'ldap://'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* LDAP port number. |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
const PORT = '389'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @event Event an event that is triggered after a DB connection is established |
|
38
|
|
|
*/ |
|
39
|
|
|
const EVENT_AFTER_OPEN = 'afterOpen'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string the LDAP base dn. |
|
43
|
|
|
*/ |
|
44
|
|
|
public $baseDn; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* https://msdn.microsoft.com/en-us/library/ms677913(v=vs.85).aspx |
|
48
|
|
|
* @var bool the integer to instruct the LDAP connection whether or not to follow referrals. |
|
49
|
|
|
*/ |
|
50
|
|
|
public $followReferrals = false; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string The LDAP port to use when connecting to the domain controllers. |
|
54
|
|
|
*/ |
|
55
|
|
|
public $port = self::PORT; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var bool Determines whether or not to use TLS with the current LDAP connection. |
|
59
|
|
|
*/ |
|
60
|
|
|
public $useTLS = false; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var array the domain controllers to connect to. |
|
64
|
|
|
*/ |
|
65
|
|
|
public $dc = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var string the LDAP account suffix. |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $accountSuffix; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var string the LDAP account prefix. |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $accountPrefix; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string the username for establishing LDAP connection. Defaults to `null` meaning no username to use. |
|
79
|
|
|
*/ |
|
80
|
|
|
public $username; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var string the password for establishing DB connection. Defaults to `null` meaning no password to use. |
|
84
|
|
|
*/ |
|
85
|
|
|
public $password; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var bool stores the bool whether or not the current connection is bound. |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $_bound = false; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @var resource|false |
|
94
|
|
|
*/ |
|
95
|
|
|
protected $resource; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Connects and Binds to the Domain Controller with a administrator credentials. |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function open($anonymous = false) |
|
102
|
|
|
{ |
|
103
|
|
|
// Connect to the LDAP server. |
|
104
|
|
|
$this->connect($this->dc, $this->port); |
|
105
|
|
|
|
|
106
|
|
|
if ($anonymous) { |
|
107
|
|
|
$this->_bound = ldap_bind($this->resource); |
|
108
|
|
|
} else { |
|
109
|
|
|
$this->_bound = ldap_bind($this->resource, $this->username, $this->password); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Connection. |
|
115
|
|
|
* @param string|array $hostname |
|
116
|
|
|
* @param type $port |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function connect($hostname = [], $port = '389') |
|
120
|
|
|
{ |
|
121
|
|
|
if (is_array($hostname)) { |
|
122
|
|
|
$hostname = self::PROTOCOL.implode(' '.self::PROTOCOL, $hostname); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->resource = ldap_connect($hostname, $port); |
|
126
|
|
|
|
|
127
|
|
|
// Set the LDAP options. |
|
128
|
|
|
$this->setOption(LDAP_OPT_PROTOCOL_VERSION, 3); |
|
129
|
|
|
$this->setOption(LDAP_OPT_REFERRALS, $this->followReferrals); |
|
130
|
|
|
if ($this->useTLS ) { |
|
131
|
|
|
$this->startTLS(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->trigger(self::EVENT_AFTER_OPEN); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Closes the current connection. |
|
139
|
|
|
* |
|
140
|
|
|
* @return boolean |
|
141
|
|
|
*/ |
|
142
|
|
|
public function close() |
|
143
|
|
|
{ |
|
144
|
|
|
if (is_resource($this->resource)) { |
|
145
|
|
|
ldap_close($this->resource); |
|
146
|
|
|
} |
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Execute ldap functions like. |
|
152
|
|
|
* |
|
153
|
|
|
* http://php.net/manual/en/ref.ldap.php |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $function php LDAP function |
|
156
|
|
|
* @param array $params params for execute ldap function |
|
157
|
|
|
* @return bool|DataReader |
|
158
|
|
|
*/ |
|
159
|
|
|
public function execute($function, $params) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->open(); |
|
162
|
|
|
|
|
163
|
|
|
$result = call_user_func($function, $this->resource, ...$params); |
|
164
|
|
|
|
|
165
|
|
|
if (is_resource($result)) { |
|
166
|
|
|
return new DataReader($this, $result); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $result; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Returns true/false if the current connection is bound. |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getBound() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->_bound; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the current resource of connection. |
|
183
|
|
|
* @return resource |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getResource() |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->resource; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Sorts an AD search result by the specified attribute. |
|
192
|
|
|
* @param resource $result |
|
193
|
|
|
* @param string $attribute |
|
194
|
|
|
* @return bool |
|
195
|
|
|
*/ |
|
196
|
|
|
public function sort($result, $attribute) |
|
197
|
|
|
{ |
|
198
|
|
|
return ldap_sort($this->resource, $result, $attribute); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Adds an entry to the current connection. |
|
203
|
|
|
* @param string $dn |
|
204
|
|
|
* @param array $entry |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
|
|
public function add($dn, array $entry) |
|
208
|
|
|
{ |
|
209
|
|
|
return ldap_add($this->resource, $dn, $entry); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Deletes an entry on the current connection. |
|
214
|
|
|
* @param string $dn |
|
215
|
|
|
* @return bool |
|
216
|
|
|
*/ |
|
217
|
|
|
public function delete($dn) |
|
218
|
|
|
{ |
|
219
|
|
|
return ldap_delete($this->resource, $dn); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Modify the name of an entry on the current connection. |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $dn |
|
226
|
|
|
* @param string $newRdn |
|
227
|
|
|
* @param string $newParent |
|
228
|
|
|
* @param bool $deleteOldRdn |
|
229
|
|
|
* @return bool |
|
230
|
|
|
*/ |
|
231
|
|
|
public function rename($dn, $newRdn, $newParent, $deleteOldRdn = false) |
|
232
|
|
|
{ |
|
233
|
|
|
return ldap_rename($this->resource, $dn, $newRdn, $newParent, $deleteOldRdn); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Modifies an existing entry on the |
|
238
|
|
|
* current connection. |
|
239
|
|
|
* @param string $dn |
|
240
|
|
|
* @param array $entry |
|
241
|
|
|
* @return bool |
|
242
|
|
|
*/ |
|
243
|
|
|
public function modify($dn, array $entry) |
|
244
|
|
|
{ |
|
245
|
|
|
return ldap_modify($this->resource, $dn, $entry); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Batch modifies an existing entry on the current connection. |
|
250
|
|
|
* @param string $dn |
|
251
|
|
|
* @param array $values |
|
252
|
|
|
* @return mixed |
|
253
|
|
|
*/ |
|
254
|
|
|
public function modifyBatch($dn, array $values) |
|
255
|
|
|
{ |
|
256
|
|
|
return ldap_modify_batch($this->resource, $dn, $values); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Add attribute values to current attributes. |
|
261
|
|
|
* @param string $dn |
|
262
|
|
|
* @param array $entry |
|
263
|
|
|
* @return boolean |
|
264
|
|
|
*/ |
|
265
|
|
|
public function modAdd($dn, array $entry) |
|
266
|
|
|
{ |
|
267
|
|
|
return ldap_mod_add($this->resource, $dn, $entry); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Replaces attribute values with new ones. |
|
272
|
|
|
* @param string $dn |
|
273
|
|
|
* @param array $entry |
|
274
|
|
|
* @return boolean |
|
275
|
|
|
*/ |
|
276
|
|
|
public function modReplace($dn, array $entry) |
|
277
|
|
|
{ |
|
278
|
|
|
return ldap_mod_replace($this->resource, $dn, $entry); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Delete attribute values from current attributes. |
|
283
|
|
|
* @param string $dn |
|
284
|
|
|
* @param array $entry |
|
285
|
|
|
* @return boolean |
|
286
|
|
|
*/ |
|
287
|
|
|
public function modDelete($dn, array $entry) |
|
288
|
|
|
{ |
|
289
|
|
|
return ldap_mod_del($this->resource, $dn, $entry); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Retrieve the entries from a search result. |
|
294
|
|
|
* @param resource $searchResult |
|
295
|
|
|
* @return array|boolean |
|
296
|
|
|
*/ |
|
297
|
|
|
public function getEntries($searchResult) |
|
298
|
|
|
{ |
|
299
|
|
|
return ldap_get_entries($this->resource, $searchResult); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Returns the number of entries from a search result. |
|
304
|
|
|
* @param resource $searchResult |
|
305
|
|
|
* @return int |
|
306
|
|
|
*/ |
|
307
|
|
|
public function countEntries($searchResult) |
|
308
|
|
|
{ |
|
309
|
|
|
return ldap_count_entries($this->resource, $searchResult); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Retrieves the first entry from a search result. |
|
314
|
|
|
* @param resource $searchResult |
|
315
|
|
|
* @return resource |
|
316
|
|
|
*/ |
|
317
|
|
|
public function getFirstEntry($searchResult) |
|
318
|
|
|
{ |
|
319
|
|
|
return ldap_first_entry($this->resource, $searchResult); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Retrieves the next entry from a search result. |
|
324
|
|
|
* @param $entry |
|
325
|
|
|
* @return resource |
|
326
|
|
|
*/ |
|
327
|
|
|
public function getNextEntry($entry) |
|
328
|
|
|
{ |
|
329
|
|
|
return ldap_next_entry($this->resource, $entry); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Retrieves the ldap entry's attributes. |
|
334
|
|
|
* @param $entry |
|
335
|
|
|
* @return mixed |
|
336
|
|
|
*/ |
|
337
|
|
|
public function getAttributes($entry) |
|
338
|
|
|
{ |
|
339
|
|
|
return ldap_get_attributes($this->resource, $entry); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Sets an option on the current connection. |
|
344
|
|
|
* @param int $option |
|
345
|
|
|
* @param mixed $value |
|
346
|
|
|
* @return boolean |
|
347
|
|
|
*/ |
|
348
|
|
|
public function setOption($option, $value) |
|
349
|
|
|
{ |
|
350
|
|
|
return ldap_set_option($this->resource, $option, $value); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Starts a connection using TLS. |
|
355
|
|
|
* @return bool |
|
356
|
|
|
*/ |
|
357
|
|
|
public function startTLS() |
|
358
|
|
|
{ |
|
359
|
|
|
return ldap_start_tls($this->resource); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* Retrieve the last error on the current connection. |
|
364
|
|
|
* @return string |
|
365
|
|
|
*/ |
|
366
|
|
|
public function getLastError() |
|
367
|
|
|
{ |
|
368
|
|
|
return ldap_error($this->resource); |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* Returns the number of the last error on the current connection. |
|
373
|
|
|
* @return int |
|
374
|
|
|
*/ |
|
375
|
|
|
public function getErrNo() |
|
376
|
|
|
{ |
|
377
|
|
|
return ldap_errno($this->resource); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Returns the error string of the specified error number. |
|
382
|
|
|
* @param int $number |
|
383
|
|
|
* @return string |
|
384
|
|
|
*/ |
|
385
|
|
|
public function err2Str($number) |
|
386
|
|
|
{ |
|
387
|
|
|
return ldap_err2str($number); |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|