-
Notifications
You must be signed in to change notification settings - Fork 1
/
LDAP.php
888 lines (805 loc) · 31 KB
/
LDAP.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Tarjej Huse |
// | Jan Wagner <wagner@netsols.de> |
// +----------------------------------------------------------------------+
//
// $Id$
require_once('PEAR.php');
require_once('LDAP/Entry.php');
require_once('LDAP/Search.php');
/**
* Net_LDAP - manipulate LDAP servers the right way!
*
* @author Tarjei Huse
* @author Jan Wagner
* @version $Revision$
* @package Net_LDAP
*/
class Net_LDAP extends PEAR
{
/**
* Class configuration array
*
* dn = the DN to bind as.
* host = the ldap host to connect to
* password = no explanation needed
* base = ldap base
* port = the server port
* tls = when set, ldap_start_tls() is run after connecting.
* version = ldap version (defaults to v 3)
* filter = default search filter
* scope = default search scope
*
* @access private
* @var array
*/
var $_config = array('dn' => '',
'host' => 'localhost',
'password' => '',
'tls' => false,
'base' => '',
'port' => 389,
'version' => 3,
'filter' => '(objectClass=*)',
'scope' => 'sub');
/**
* LDAP resource link.
*
* @access private
* @var resource
*/
var $_link;
/**
* Net_LDAP_Schema object
*
* @access private
* @var object Net_LDAP_Schema
*/
var $_schema = null;
/**
* Cache for attribute encoding checks
*
* @access private
* @var array Hash with attribute names as key and boolean value
* to determine whether they should be utf8 encoded or not.
*/
var $_schemaAttrs = array();
/**
* Net_LDAP constructor
*
* Sets the config array
*
* @access protected
* @param array Configuration array
* @return void
* @see $_config
*/
function Net_LDAP($_config = array())
{
foreach ($_config as $k => $v) {
$this->_config[$k] = $v;
}
}
/**
* Creates the initial ldap-object
*
* Static function that returns either an error object or the new Net_LDAP object.
* Something like a factory. Takes a config array with the needed parameters.
*
* @access public
* @param array Configuration array
* @return mixed object Net_LDAP_Error or Net_LDAP
* @see $_config
*/
function &connect($config = array())
{
@$obj = & new Net_LDAP($config);
$err = $obj->bind();
if (Net_LDAP::isError($err)) {
return $err;
}
return $obj;
}
/**
* Bind to the ldap-server
*
* The function may be used if you do not create the object using Net_LDAP::connect.
*
* @access public
* @param array Configuration array
* @return mixed Net_LDAP_Error or true
* @see $_config
*/
function bind($config = array())
{
foreach ($config as $k => $v) {
$this->_config[$k] = $v;
}
if ($this->_config['host']) {
$this->_link = @ldap_connect($this->_config['host'], $this->_config['port']);
} else {
return $this->raiseError("Host not defined in config. {$this->_config['host']}");
}
if (!$this->_link) {
// there is no good errorcode for this one! I chose 52.
return $this->raiseError("Could not connect to server. ldap_connect failed.",52 );
}
// You must set the version and start tls BEFORE binding!
if ($this->_config['version'] != 2 && Net_LDAP::isError($msg = $this->setLDAPVersion())) {
return $msg;
}
if ($this->_config['tls'] && Net_LDAP::isError($msg = $this->startTLS())) {
return $msg;
}
if (isset($this->_config['dn']) && isset($this->_config['password'])) {
$bind = @ldap_bind($this->_link, $this->_config['dn'], $this->_config['password']);
} else {
$bind = @ldap_bind($this->_link);
}
if (!$bind) {
return $this->raiseError("Bind failed " . @ldap_error($this->_link), @ldap_errno($this->_link));
}
return true;
}
/**
* Starts an encrypted session
*
* @access public
* @return mixed True or Net_LDAP_Error
*/
function startTLS()
{
if (!@ldap_start_tls($this->_link)) {
return $this->raiseError("TLS not started. Error:" . @ldap_error($this->_link), @ldap_errno($this->_link));
}
return true;
}
/**
* alias function of startTLS() for perl-ldap interface
*
* @see startTLS()
*/
function start_tls()
{
$args = func_get_args();
return call_user_func_array(array( &$this, 'startTLS' ), $args);
}
/**
* Close LDAP connection.
*
* Closes the connection. Use this when the session is over.
*
* @return void
*/
function done()
{
$this->_Net_LDAP();
}
/**
* Destructor
*
* @access private
*/
function _Net_LDAP()
{
@ldap_close($this->_link);
}
/**
* Add a new entryobject to a directory.
*
* Use add to add a new Net_LDAP_Entry object to the directory.
*
* @param object Net_LDAP_Entry
* @return mixed Net_LDAP_Error or true
*/
function add($entry)
{
if (@ldap_add($this->_link, $entry->dn(), $entry->attributes())) {
return true;
} else {
return $this->raiseError("Could not add entry " . $entry->dn() . " " . ldap_error(),
ldap_errno($this->_link));
}
}
/**
* Delete an entry from the directory
*
* The object may either be a string representing the dn or a Net_LDAP_Entry object.
* The param array may contain a boolean value named recursive. When set, all subentries
* of the Entry will be deleted as well
*
* @access public
* @param mixed string or Net_LDAP_Entry
* @param array
* @return mixed Net_LDAP_Error or true
*/
function delete($dn, $param = array())
{
if (is_object($dn) && get_class($dn) == 'net_ldap_entry') {
$dn = $dn->dn();
} else {
if (!is_string($dn)) {
// this is what the server would say: invalid_dn_syntax.
return $this->raiseError("$dn not a string nor an entryobject!",34);
}
}
if ($param['recursive'] ) {
$searchresult = @ldap_list($this->_link, $dn, '(objectClass=*)', array());
if ($searchresult) {
$entries = @ldap_get_entries($this->_link, $searchresult);
for ($i=0; $i<$entries['count']; $i++) {
$result = $this->delete($entries[$i]['dn'], array('recursive' => true));
if (!$result) {
$errno = @ldap_errno($this->_link);
return $this->raiseMessage ("Net_LDAP::delete: " . $this->errorMessage($errno), $errno);
}
if(PEAR::isError($result)){
return $result;
}
}
}
} else {
if (!@ldap_delete($this->_link, $dn)) {
$error = ldap_errno($this->_link );
if ($error == 66) {
/* entry has subentries */
return $this->raiseError('Net_LDAP::delete: Cound not delete entry ' . $dn .
' because of subentries. Use the recursive param to delete them.');
} else {
return $this->raiseError("Net_LDAP::delete: Could not delete entry " . $dn ." because: ".
$this->errorMessage($error), $error);
}
}
}
return true;
}
/**
* Modify an ldapentry
*
* This is taken from the perlpod of net::ldap, and explains things quite nicely.
* modify ( DN, OPTIONS )
* Modify the contents of DN on the server. DN May be a
* string or a Net::LDAP::Entry object.
*
* dn This option is here for compatibility only, and
* may be removed in future. Previous releases did
* not take the DN argument which replaces this
* option.
*
* add The add option should be a reference to a HASH.
* The values of the HASH are the attributes to add,
* and the values may be a string or a reference to a
* list of values.
*
* delete
* A reference to an ARRAY of attributes to delete.
* TODO: This does not support deleting one or two values yet - use
* replace.
*
* replace
* The <replace> option takes a argument in the same
* form as add, but will cause any existing
* attributes with the same name to be replaced. If
* the value for any attribute in the årray is a ref
* erence to an empty string the all instances of the
* attribute will be deleted.
*
* changes
* This is an alternative to add, delete and replace
* where the whole operation can be given in a single
* argument. The argument should be a array
*
* Values in the ARRAY are used in pairs, the first
* is the operation add, delete or replace and the
* second is a reference to an ARRAY of attribute
* values.
*
* The attribute value list is also used in pairs.
* The first value in each pair is the attribute name
* and the second is a reference to a list of values.
*
* Example:
* $ldap->modify ( $dn, array (changes => array(
* 'delete' => array('faxNumber' => ''),
* 'add' => array('sn' => 'Barr'),
* 'replace' => array(email => 'tarjei@nu.no'))));
*
* @access public
* @param string
* @param array
* @return mixed Net_LDAP_Error or true
*/
function modify($dn , $params = array())
{
if (is_object($dn)) {
$dn = $dn->dn();
}
// since $params['dn'] is not used in net::ldap now:
if (isset($params['dn'])) {
return $this->raiseError("This feature will not be implemented!");
}
if (isset($params['changes'])) {
if (isset($params['changes']['add']) &&
!@ldap_modify($this->_link, $dn, $params['changes']['add'])) {
return $this->raiseError("Net_LDAP::modify: $dn not modified because:" . ldap_error($this->_link),
ldap_errno($this->_link));
}
if (isset($params['changes']['replace']) &&
!@ldap_modify($this->_link, $dn, $params['changes']['replace'])) {
return $this->raiseError("Net_LDAP::modify: replace change didn't work: " . ldap_error($this->_link),
ldap_errno($this->_link));
}
if (isset($params['changes']['delete']) &&
!@ldap_mod_del($this->_link, $dn, $params['changes']['delete'])) {
return $this->raiseError("Net_LDAP::modify:delete did not work" . ldap_error($this->_link),
ldap_errno($this->_link));
}
}
if (isset($params['add']) && !@ldap_add($this->_link, $dn, $params['add'])) {
return $this->raiseError(ldap_error($this->_link), ldap_errno($this->_link));
}
if (isset($params['replace']) && !@ldap_modify($this->_link, $dn, $params['replace'])) {
return $this->raiseError(ldap_error($this->_link), ldap_errno($this->_link));
}
if (isset($params['delete'])) {
// since you delete an attribute by making it empty:
foreach ($params['delete'] as $k) {
$params['delete'][$k] = '';
}
if (!@ldap_modify($this->_link, $dn, $params['delete'])) {
return $this->raiseError(ldap_error($this->_link), ldap_errno($this->_link));
}
}
// everything went fine :)
return true;
}
/**
* Run a ldap query
*
* Search is used to query the ldap-database.
* $base and $filter may be ommitted. BaseDN and default filter will then be used.
* Params may contain:
*
* scope: The scope which will be used for searching
* base - Just one entry
* sub - The whole tree
* one - Immediately below $base
* sizelimit: Limit the number of entries returned (default: 0),
* timelimit: Limit the time spent for searching (default: 0),
* attrsonly: If true, the search will only return the attribute names, NO values
* attributes: Array of attribute names, which the entry should contain. It is good practice
* to limit this to just the ones you need, so by default this function does not
* return any attributes at all.
* [NOT IMPLEMENTED]
* deref: By default aliases are dereferenced to locate the base object for the search, but not when
* searching subordinates of the base object. This may be changed by specifying one of the
* following values:
*
* never - Do not dereference aliases in searching or in locating the base object of the search.
* search - Dereference aliases in subordinates of the base object in searching, but not in
* locating the base object of the search.
* find
* always
*
* @access public
* @param string LDAP searchbase
* @param string LDAP search filter
* @param array Array of options
* @return object mixed Net_LDAP_Search or Net_LDAP_Error
*/
function search($base = null, $filter = null, $params = array())
{
if (is_null($base)) {
$base = $this->_config['base'];
}
if (is_null($filter)) {
$filter = $this->_config['filter'];
}
/* setting searchparameters */
(isset($params['sizelimit'])) ? $sizelimit = $params['sizelimit'] : $sizelimit = 0;
(isset($params['timelimit'])) ? $timelimit = $params['timelimit'] : $timelimit = 0;
(isset($params['attrsonly'])) ? $attrsonly = $params['attrsonly'] : $attrsonly = 0;
(isset($params['attributes'])) ? $attributes = $params['attributes'] : $attributes = array('');
if (!is_array($attributes)) {
$this->raiseError("The param attributes must be an array!");
}
/* scoping makes searches faster! */
$scope = (isset($params['scope']) ? $params['scope'] : $this->_config['scope']);
switch ($scope) {
case 'one':
$search_function = 'ldap_list';
break;
case 'base':
$search_function = 'ldap_read';
break;
default:
$search_function = 'ldap_search';
}
$search = @call_user_func($search_function,
$this->_link,
$base,
$filter,
$attributes,
$attrsonly,
$sizelimit,
$timelimit);
if ($err = ldap_errno($this->_link)) {
if ($err == 32) {
// Errorcode 32 = no such object, i.e. a nullresult.
return $obj = & new Net_LDAP_Search (& $search, $this->_link);
// Errorcode 4 = sizelimit exeeded. this will be handled better in time...
//} elseif ($err == 4) {
// return $obj = & new Net_LDAP_Search ($search, $this->_link);
} elseif ($err == 87) {
// bad search filter
return $this->raiseError($this->errorMessage($err) . "($filter)", $err);
} else {
$msg = "\nParameters:\nBase: $base\nFilter: $filter\nScope: $scope";
return $this->raiseError($this->errorMessage($err) . $msg, $err);
}
} else {
@$obj = & new Net_LDAP_Search($search, $this->_link);
return $obj;
}
}
/**
* Get the LDAP_PROTOCOL_VERSION that is used on the connection.
*
* A lot of ldap functionality is defined by what protocol version the ldap server speaks.
* This might be 2 or 3.
*
* @return int
*/
function getLDAPVersion()
{
if($this->_link) {
@ldap_get_option( $this->_link, LDAP_OPT_PROTOCOL_VERSION, $version);
} else {
$version = $this->_config['version'];
}
return $version;
}
/**
* Set the LDAP_PROTOCOL_VERSION that is used on the connection.
*
* @param int Version to set
* @return mixed Net_LDAP_Error or TRUE
*/
function setLDAPVersion($version = 0)
{
if (!$version) {
$version = $this->_config['version'];
}
if (!$this->_link) {
return $this->raiseError('No valid LDAP link');
}
if (!@ldap_set_option($this->_link, LDAP_OPT_PROTOCOL_VERSION, $version)) {
return $this->raiseError("Could not set LDAP version to $version " .
ldap_error($this->_link), ldap_errno($this->_link));
}
return true;
}
/**
* Get the Net_LDAP version.
*
* Not yet supported. For now, raises an error.
*
* @return object Net_LDAP_Error
*/
function getVersion ()
{
return $this->raiseError("This function is not yet supported by Net_LDAP. If you want to find the LDAP Protocolversion use getLDAPVersion()");
}
/**
* Tell if a dn already exists
*
* @param string
* @return boolean
*/
function dnExists($dn)
{
$dns = explode(",",$dn);
$filter = array_shift($dns);
$base= implode($dns,',');
//$base = $dn;
//$filter = '(objectclass=*)';
$result = @ldap_list($this->_link, $base, $filter, array(), 1, 1);
if (ldap_errno($this->_link) == 32) {
return false;
}
if (ldap_errno($this->_link) != 0) {
$this->raiseError(ldap_error($this->_link), ldap_errno($this->_link));
}
if (@ldap_count_entries($this->_link, $result)) {
return true;
}
return false;
}
/**
* Get a specific entry based on the dn
*
* @param string dn
* @return mixed Net_LDAP_Entry or false
*/
function &getEntry($dn)
{
$base = $dn;
$filter = '(objectclass=*)';
$result = @ldap_list($this->_link, $base, $filter, array(), 1, 1);
if (ldap_count_entries($result) > 0) {
$elink = @ldap_first_entry($this->_link, $this->result);
return new Net_LDAP_Entry($this->_link,
@ldap_get_dn($this->_link, $elink),
@ldap_get_attributes($this->_link, $elink));
}
return false;
}
/**
* Returns the string for an ldap errorcode.
*
* Made to be able to make better errorhandling
* Function based on DB::errorMessage()
* Tip: The best description of the errorcodes is found here: http://www.directory-info.com/LDAP/LDAPErrorCodes.html
*
* @param int Error code
* @return string The errorstring for the error.
*/
function errorMessage($errorcode)
{
$errorMessages = array(
0x00 => "LDAP_SUCCESS",
0x01 => "LDAP_OPERATIONS_ERROR",
0x02 => "LDAP_PROTOCOL_ERROR",
0x03 => "LDAP_TIMELIMIT_EXCEEDED",
0x04 => "LDAP_SIZELIMIT_EXCEEDED",
0x05 => "LDAP_COMPARE_FALSE",
0x06 => "LDAP_COMPARE_TRUE",
0x07 => "LDAP_AUTH_METHOD_NOT_SUPPORTED",
0x08 => "LDAP_STRONG_AUTH_REQUIRED",
0x09 => "LDAP_PARTIAL_RESULTS",
0x0a => "LDAP_REFERRAL",
0x0b => "LDAP_ADMINLIMIT_EXCEEDED",
0x0c => "LDAP_UNAVAILABLE_CRITICAL_EXTENSION",
0x0d => "LDAP_CONFIDENTIALITY_REQUIRED",
0x0e => "LDAP_SASL_BIND_INPROGRESS",
0x10 => "LDAP_NO_SUCH_ATTRIBUTE",
0x11 => "LDAP_UNDEFINED_TYPE",
0x12 => "LDAP_INAPPROPRIATE_MATCHING",
0x13 => "LDAP_CONSTRAINT_VIOLATION",
0x14 => "LDAP_TYPE_OR_VALUE_EXISTS",
0x15 => "LDAP_INVALID_SYNTAX",
0x20 => "LDAP_NO_SUCH_OBJECT",
0x21 => "LDAP_ALIAS_PROBLEM",
0x22 => "LDAP_INVALID_DN_SYNTAX",
0x23 => "LDAP_IS_LEAF",
0x24 => "LDAP_ALIAS_DEREF_PROBLEM",
0x30 => "LDAP_INAPPROPRIATE_AUTH",
0x31 => "LDAP_INVALID_CREDENTIALS",
0x32 => "LDAP_INSUFFICIENT_ACCESS",
0x33 => "LDAP_BUSY",
0x34 => "LDAP_UNAVAILABLE",
0x35 => "LDAP_UNWILLING_TO_PERFORM",
0x36 => "LDAP_LOOP_DETECT",
0x3C => "LDAP_SORT_CONTROL_MISSING",
0x3D => "LDAP_INDEX_RANGE_ERROR",
0x40 => "LDAP_NAMING_VIOLATION",
0x41 => "LDAP_OBJECT_CLASS_VIOLATION",
0x42 => "LDAP_NOT_ALLOWED_ON_NONLEAF",
0x43 => "LDAP_NOT_ALLOWED_ON_RDN",
0x44 => "LDAP_ALREADY_EXISTS",
0x45 => "LDAP_NO_OBJECT_CLASS_MODS",
0x46 => "LDAP_RESULTS_TOO_LARGE",
0x47 => "LDAP_AFFECTS_MULTIPLE_DSAS",
0x50 => "LDAP_OTHER",
0x51 => "LDAP_SERVER_DOWN",
0x52 => "LDAP_LOCAL_ERROR",
0x53 => "LDAP_ENCODING_ERROR",
0x54 => "LDAP_DECODING_ERROR",
0x55 => "LDAP_TIMEOUT",
0x56 => "LDAP_AUTH_UNKNOWN",
0x57 => "LDAP_FILTER_ERROR",
0x58 => "LDAP_USER_CANCELLED",
0x59 => "LDAP_PARAM_ERROR",
0x5a => "LDAP_NO_MEMORY",
0x5b => "LDAP_CONNECT_ERROR",
0x5c => "LDAP_NOT_SUPPORTED",
0x5d => "LDAP_CONTROL_NOT_FOUND",
0x5e => "LDAP_NO_RESULTS_RETURNED",
0x5f => "LDAP_MORE_RESULTS_TO_RETURN",
0x60 => "LDAP_CLIENT_LOOP",
0x61 => "LDAP_REFERRAL_LIMIT_EXCEEDED"
);
return isset($errorMessages[$errorcode]) ? $errorMessages[$errorcode] : $errorMessages[LDAP_ERROR];
}
/**
* gets a root dse object
*
* @access public
* @author Jan Wagner <wagner@netsols.de>
* @param array Array of attributes to search for
* @return object mixed Net_LDAP_Error or Net_LDAP_RootDSE
*/
function &rootDse($attrs = null)
{
require_once('Net/LDAP/RootDSE.php');
if (is_array($attrs) && count($attrs) > 0 ) {
$attributes = $attrs;
} else {
$attributes = array('namingContexts',
'altServer',
'supportedExtension',
'supportedControl',
'supportedSASLMechanisms',
'supportedLDAPVersion',
'subschemaSubentry' );
}
$result = $this->search('', '(objectClass=*)', array('attributes' => $attributes, 'scope' => 'base'));
if (Net_LDAP::isError($result)) return $result;
$entry = $result->shift_entry();
if (false === $entry) return $this->raiseError('Could not fetch RootDSE entry');
return new Net_LDAP_RootDSE($entry);
}
/**
* alias function of rootDse() for perl-ldap interface
*
* @access public
* @see rootDse()
*/
function &root_dse()
{
$args = func_get_args();
return call_user_func_array(array(&$this, 'rootDse'), $args);
}
/**
* get a schema object
*
* @access public
* @author Jan Wagner <wagner@netsols.de>
* @param string Subschema entry dn
* @return object mixed Net_LDAP_Schema or Net_LDAP_Error
*/
function &schema($dn = null)
{
require_once('Net/LDAP/Schema.php');
$schema = & new Net_LDAP_Schema();
if (is_null($dn)) {
// get the subschema entry via root dse
$dse = $this->rootDSE(array('subschemaSubentry'));
if (false == Net_LDAP::isError($dse)) {
$base = $dse->getValue('subschemaSubentry', 'single');
if (!Net_LDAP::isError($base)) {
$dn = $base;
}
}
}
if (is_null($dn)) {
$dn = 'cn=Subschema';
}
// fetch the subschema entry
$result = $this->search($dn, '(objectClass=*)',
array('attributes' => array_values($schema->types), 'scope' => 'base'));
if (Net_LDAP::isError($result)) {
return $result;
}
$entry = $result->shift_entry();
if (false === $entry) {
return $this->raiseError('Could not fetch Subschema entry');
}
$schema->parse($entry);
return $schema;
}
/**
* Encodes given attributes to UTF8 if needed
*
* This function takes attributes in an array and then checks against the schema if they need
* UTF8 encoding. If that is so, they will be encoded. An encoded array will be returned and
* can be used for adding or modifying.
*
* @access public
* @param array Array of attributes
* @return array Array of UTF8 encoded attributes
*/
function utf8Encode($attributes)
{
return $this->_utf8($attributes, 'utf8_encode');
}
/**
* Decodes the given attribute values
*
* @access public
* @param array Array of attributes
* @return array Array with decoded attribute values
*/
function utf8Decode($attributes)
{
return $this->_utf8($attributes, 'utf8_decode');
}
/**
* Encodes or decodes attribute values if needed
*
* @access private
* @param array Array of attributes
* @param array Function to apply to attribute values
* @return array Array of attributes with function applied to values
*/
function _utf8($attributes, $function)
{
if (!$this->_schema) {
$this->_schema = $this->schema();
}
if (!$this->_link || Net_LDAP::isError($this->_schema) || !function_exists($function)) {
return $attributes;
}
if (is_array($attributes) && count($attributes) > 0) {
foreach( $attributes as $k => $v ) {
if (!isset($this->_schemaAttrs[$k])) {
$attr = $this->_schema->get('attribute', $k);
if (Net_LDAP::isError($attr)) {
continue;
}
if (false !== strpos($attr['syntax'], '1.3.6.1.4.1.1466.115.121.1.15')) {
$encode = true;
} else {
$encode = false;
}
$this->_schemaAttrs[$k] = $encode;
} else {
$encode = $this->_schemaAttrs[$k];
}
if ($encode) {
if (is_array($v)) {
foreach ($v as $ak => $av) {
$v[$ak] = call_user_func($function, $av );
}
} else {
$v = call_user_func($function, $v);
}
}
$attributes[$k] = $v;
}
}
return $attributes;
}
}
/**
* Net_LDAP_Error implements a class for reporting portable LDAP error messages.
*
* @package Net_LDAP
*/
class Net_LDAP_Error extends PEAR_Error
{
/**
* Net_LDAP_Error constructor.
*
* @param mixed Net_LDAP error code, or string with error message.
* @param integer what "error mode" to operate in
* @param integer what error level to use for $mode & PEAR_ERROR_TRIGGER
* @param mixed additional debug info, such as the last query
* @access public
* @see PEAR_Error
*/
function Net_LDAP_Error($code = DB_ERROR, $mode = PEAR_ERROR_RETURN,
$level = E_USER_NOTICE, $debuginfo = null)
{
if (is_int($code)) {
$this->PEAR_Error('Net_LDAP_Error: ' . Net_LDAP::errorMessage($code), $code, $mode, $level, $debuginfo);
} else {
$this->PEAR_Error("Net_LDAP_Error: $code", LDAP_ERROR, $mode, $level, $debuginfo);
}
}
}
?>