1 | <?php |
||
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) |
||
112 | |||
113 | /** |
||
114 | * Connection. |
||
115 | * @param string|array $hostname |
||
116 | * @param type $port |
||
117 | * @return void |
||
118 | */ |
||
119 | public function connect($hostname = [], $port = '389') |
||
136 | |||
137 | /** |
||
138 | * Closes the current connection. |
||
139 | * |
||
140 | * @return boolean |
||
141 | */ |
||
142 | public function close() |
||
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) |
||
171 | |||
172 | /** |
||
173 | * Returns true/false if the current connection is bound. |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function getBound() |
||
180 | |||
181 | /** |
||
182 | * Get the current resource of connection. |
||
183 | * @return resource |
||
184 | */ |
||
185 | public function getResource() |
||
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) |
||
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) |
||
211 | |||
212 | /** |
||
213 | * Deletes an entry on the current connection. |
||
214 | * @param string $dn |
||
215 | * @return bool |
||
216 | */ |
||
217 | public function delete($dn) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
291 | |||
292 | /** |
||
293 | * Retrieve the entries from a search result. |
||
294 | * @param resource $searchResult |
||
295 | * @return array|boolean |
||
296 | */ |
||
297 | public function getEntries($searchResult) |
||
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) |
||
311 | |||
312 | /** |
||
313 | * Retrieves the first entry from a search result. |
||
314 | * @param resource $searchResult |
||
315 | * @return resource |
||
316 | */ |
||
317 | public function getFirstEntry($searchResult) |
||
321 | |||
322 | /** |
||
323 | * Retrieves the next entry from a search result. |
||
324 | * @param $entry |
||
325 | * @return resource |
||
326 | */ |
||
327 | public function getNextEntry($entry) |
||
331 | |||
332 | /** |
||
333 | * Retrieves the ldap entry's attributes. |
||
334 | * @param $entry |
||
335 | * @return mixed |
||
336 | */ |
||
337 | public function getAttributes($entry) |
||
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) |
||
352 | |||
353 | /** |
||
354 | * Starts a connection using TLS. |
||
355 | * @return bool |
||
356 | */ |
||
357 | public function startTLS() |
||
361 | |||
362 | /** |
||
363 | * Retrieve the last error on the current connection. |
||
364 | * @return string |
||
365 | */ |
||
366 | public function getLastError() |
||
370 | |||
371 | /** |
||
372 | * Returns the number of the last error on the current connection. |
||
373 | * @return int |
||
374 | */ |
||
375 | public function getErrNo() |
||
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) |
||
389 | } |
||
390 |