Node.php
29 KB
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Ldap
* @subpackage Node
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Node.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* @see Zend_Ldap
*/
require_once 'Zend/Ldap.php';
/**
* @see Zend_Ldap_Node_Abstract
*/
require_once 'Zend/Ldap/Node/Abstract.php';
/**
* Zend_Ldap_Node provides an object oriented view into a LDAP node.
*
* @category Zend
* @package Zend_Ldap
* @subpackage Node
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Ldap_Node extends Zend_Ldap_Node_Abstract implements Iterator, RecursiveIterator
{
/**
* Holds the node's new DN if node is renamed.
*
* @var Zend_Ldap_Dn
*/
protected $_newDn;
/**
* Holds the node's orginal attributes (as loaded).
*
* @var array
*/
protected $_originalData;
/**
* This node will be added
*
* @var boolean
*/
protected $_new;
/**
* This node will be deleted
*
* @var boolean
*/
protected $_delete;
/**
* Holds the connection to the LDAP server if in connected mode.
*
* @var Zend_Ldap
*/
protected $_ldap;
/**
* Holds an array of the current node's children.
*
* @var array
*/
protected $_children;
/**
* Controls iteration status
*
* @var boolean
*/
private $_iteratorRewind = false;
/**
* Constructor.
*
* Constructor is protected to enforce the use of factory methods.
*
* @param Zend_Ldap_Dn $dn
* @param array $data
* @param boolean $fromDataSource
* @param Zend_Ldap $ldap
* @throws Zend_Ldap_Exception
*/
protected function __construct(Zend_Ldap_Dn $dn, array $data, $fromDataSource, Zend_Ldap $ldap = null)
{
parent::__construct($dn, $data, $fromDataSource);
if ($ldap !== null) $this->attachLdap($ldap);
else $this->detachLdap();
}
/**
* Serialization callback
*
* Only DN and attributes will be serialized.
*
* @return array
*/
public function __sleep()
{
return array('_dn', '_currentData', '_newDn', '_originalData',
'_new', '_delete', '_children');
}
/**
* Deserialization callback
*
* Enforces a detached node.
*
* @return null
*/
public function __wakeup()
{
$this->detachLdap();
}
/**
* Gets the current LDAP connection.
*
* @return Zend_Ldap
* @throws Zend_Ldap_Exception
*/
public function getLdap()
{
if ($this->_ldap === null) {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, 'No LDAP connection specified.', Zend_Ldap_Exception::LDAP_OTHER);
}
else return $this->_ldap;
}
/**
* Attach node to an LDAP connection
*
* This is an offline method.
*
* @uses Zend_Ldap_Dn::isChildOf()
* @param Zend_Ldap $ldap
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function attachLdap(Zend_Ldap $ldap)
{
if (!Zend_Ldap_Dn::isChildOf($this->_getDn(), $ldap->getBaseDn())) {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, 'LDAP connection is not responsible for given node.',
Zend_Ldap_Exception::LDAP_OTHER);
}
if ($ldap !== $this->_ldap) {
$this->_ldap = $ldap;
if (is_array($this->_children)) {
foreach ($this->_children as $child) {
$child->attachLdap($ldap);
}
}
}
return $this;
}
/**
* Detach node from LDAP connection
*
* This is an offline method.
*
* @return Zend_Ldap_Node Provides a fluid interface
*/
public function detachLdap()
{
$this->_ldap = null;
if (is_array($this->_children)) {
foreach ($this->_children as $child) {
$child->detachLdap();
}
}
return $this;
}
/**
* Checks if the current node is attached to a LDAP server.
*
* This is an offline method.
*
* @return boolean
*/
public function isAttached()
{
return ($this->_ldap !== null);
}
/**
* @param array $data
* @param boolean $fromDataSource
* @throws Zend_Ldap_Exception
*/
protected function _loadData(array $data, $fromDataSource)
{
parent::_loadData($data, $fromDataSource);
if ($fromDataSource === true) {
$this->_originalData = $data;
} else {
$this->_originalData = array();
}
$this->_children = null;
$this->_markAsNew(($fromDataSource === true) ? false : true);
$this->_markAsToBeDeleted(false);
}
/**
* Factory method to create a new detached Zend_Ldap_Node for a given DN.
*
* @param string|array|Zend_Ldap_Dn $dn
* @param array $objectClass
* @return Zend_Ldap_Node
* @throws Zend_Ldap_Exception
*/
public static function create($dn, array $objectClass = array())
{
if (is_string($dn) || is_array($dn)) {
$dn = Zend_Ldap_Dn::factory($dn);
} else if ($dn instanceof Zend_Ldap_Dn) {
$dn = clone $dn;
} else {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, '$dn is of a wrong data type.');
}
$new = new self($dn, array(), false, null);
$new->_ensureRdnAttributeValues();
$new->setAttribute('objectClass', $objectClass);
return $new;
}
/**
* Factory method to create an attached Zend_Ldap_Node for a given DN.
*
* @param string|array|Zend_Ldap_Dn $dn
* @param Zend_Ldap $ldap
* @return Zend_Ldap_Node|null
* @throws Zend_Ldap_Exception
*/
public static function fromLdap($dn, Zend_Ldap $ldap)
{
if (is_string($dn) || is_array($dn)) {
$dn = Zend_Ldap_Dn::factory($dn);
} else if ($dn instanceof Zend_Ldap_Dn) {
$dn = clone $dn;
} else {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, '$dn is of a wrong data type.');
}
$data = $ldap->getEntry($dn, array('*', '+'), true);
if ($data === null) {
return null;
}
$entry = new self($dn, $data, true, $ldap);
return $entry;
}
/**
* Factory method to create a detached Zend_Ldap_Node from array data.
*
* @param array $data
* @param boolean $fromDataSource
* @return Zend_Ldap_Node
* @throws Zend_Ldap_Exception
*/
public static function fromArray(array $data, $fromDataSource = false)
{
if (!array_key_exists('dn', $data)) {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, '\'dn\' key is missing in array.');
}
if (is_string($data['dn']) || is_array($data['dn'])) {
$dn = Zend_Ldap_Dn::factory($data['dn']);
} else if ($data['dn'] instanceof Zend_Ldap_Dn) {
$dn = clone $data['dn'];
} else {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, '\'dn\' key is of a wrong data type.');
}
$fromDataSource = ($fromDataSource === true) ? true : false;
$new = new self($dn, $data, $fromDataSource, null);
$new->_ensureRdnAttributeValues();
return $new;
}
/**
* Ensures that teh RDN attributes are correctly set.
*
* @return void
*/
protected function _ensureRdnAttributeValues()
{
foreach ($this->getRdnArray() as $key => $value) {
Zend_Ldap_Attribute::setAttribute($this->_currentData, $key, $value, false);
}
}
/**
* Marks this node as new.
*
* Node will be added (instead of updated) on calling update() if $new is true.
*
* @param boolean $new
*/
protected function _markAsNew($new)
{
$this->_new = ($new === false) ? false : true;
}
/**
* Tells if the node is consiedered as new (not present on the server)
*
* Please note, that this doesn't tell you if the node is present on the server.
* Use {@link exits()} to see if a node is already there.
*
* @return boolean
*/
public function isNew()
{
return $this->_new;
}
/**
* Marks this node as to be deleted.
*
* Node will be deleted on calling update() if $delete is true.
*
* @param boolean $delete
*/
protected function _markAsToBeDeleted($delete)
{
$this->_delete = ($delete === true) ? true : false;
}
/**
* Is this node going to be deleted once update() is called?
*
* @return boolean
*/
public function willBeDeleted()
{
return $this->_delete;
}
/**
* Marks this node as to be deleted
*
* Node will be deleted on calling update() if $delete is true.
*
* @return Zend_Ldap_Node Provides a fluid interface
*/
public function delete()
{
$this->_markAsToBeDeleted(true);
return $this;
}
/**
* Is this node going to be moved once update() is called?
*
* @return boolean
*/
public function willBeMoved()
{
if ($this->isNew() || $this->willBeDeleted()) {
return false;
} else if ($this->_newDn !== null) {
return ($this->_dn != $this->_newDn);
} else {
return false;
}
}
/**
* Sends all pending changes to the LDAP server
*
* @param Zend_Ldap $ldap
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function update(Zend_Ldap $ldap = null)
{
if ($ldap !== null) {
$this->attachLdap($ldap);
}
$ldap = $this->getLdap();
if (!($ldap instanceof Zend_Ldap)) {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, 'No LDAP connection available');
}
if ($this->willBeDeleted()) {
if ($ldap->exists($this->_dn)) {
$ldap->delete($this->_dn);
}
return $this;
}
if ($this->isNew()) {
$data = $this->getData();
$ldap->add($this->_getDn(), $data);
$this->_loadData($data, true);
return $this;
}
$changedData = $this->getChangedData();
if ($this->willBeMoved()) {
$recursive = $this->hasChildren();
$ldap->rename($this->_dn, $this->_newDn, $recursive, false);
foreach ($this->_newDn->getRdn() as $key => $value) {
if (array_key_exists($key, $changedData)) {
unset($changedData[$key]);
}
}
$this->_dn = $this->_newDn;
$this->_newDn = null;
}
if (count($changedData) > 0) {
$ldap->update($this->_getDn(), $changedData);
}
$this->_originalData = $this->_currentData;
return $this;
}
/**
* Gets the DN of the current node as a Zend_Ldap_Dn.
*
* This is an offline method.
*
* @return Zend_Ldap_Dn
*/
protected function _getDn()
{
return ($this->_newDn === null) ? parent::_getDn() : $this->_newDn;
}
/**
* Gets the current DN of the current node as a Zend_Ldap_Dn.
* The method returns a clone of the node's DN to prohibit modification.
*
* This is an offline method.
*
* @return Zend_Ldap_Dn
*/
public function getCurrentDn()
{
$dn = clone parent::_getDn();
return $dn;
}
/**
* Sets the new DN for this node
*
* This is an offline method.
*
* @param Zend_Ldap_Dn|string|array $newDn
* @throws Zend_Ldap_Exception
* @return Zend_Ldap_Node Provides a fluid interface
*/
public function setDn($newDn)
{
if ($newDn instanceof Zend_Ldap_Dn) {
$this->_newDn = clone $newDn;
} else {
$this->_newDn = Zend_Ldap_Dn::factory($newDn);
}
$this->_ensureRdnAttributeValues();
return $this;
}
/**
* {@see setDn()}
*
* This is an offline method.
*
* @param Zend_Ldap_Dn|string|array $newDn
* @throws Zend_Ldap_Exception
* @return Zend_Ldap_Node Provides a fluid interface
*/
public function move($newDn)
{
return $this->setDn($newDn);
}
/**
* {@see setDn()}
*
* This is an offline method.
*
* @param Zend_Ldap_Dn|string|array $newDn
* @throws Zend_Ldap_Exception
* @return Zend_Ldap_Node Provides a fluid interface
*/
public function rename($newDn)
{
return $this->setDn($newDn);
}
/**
* Sets the objectClass.
*
* This is an offline method.
*
* @param array|string $value
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function setObjectClass($value)
{
$this->setAttribute('objectClass', $value);
return $this;
}
/**
* Appends to the objectClass.
*
* This is an offline method.
*
* @param array|string $value
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function appendObjectClass($value)
{
$this->appendToAttribute('objectClass', $value);
return $this;
}
/**
* Returns a LDIF representation of the current node
*
* @param array $options Additional options used during encoding
* @return string
*/
public function toLdif(array $options = array())
{
$attributes = array_merge(array('dn' => $this->getDnString()), $this->getData(false));
/**
* Zend_Ldap_Ldif_Encoder
*/
require_once 'Zend/Ldap/Ldif/Encoder.php';
return Zend_Ldap_Ldif_Encoder::encode($attributes, $options);
}
/**
* Gets changed node data.
*
* The array contains all changed attributes.
* This format can be used in {@link Zend_Ldap::add()} and {@link Zend_Ldap::update()}.
*
* This is an offline method.
*
* @return array
*/
public function getChangedData()
{
$changed = array();
foreach ($this->_currentData as $key => $value) {
if (!array_key_exists($key, $this->_originalData) && !empty($value)) {
$changed[$key] = $value;
} else if ($this->_originalData[$key] !== $this->_currentData[$key]) {
$changed[$key] = $value;
}
}
return $changed;
}
/**
* Returns all changes made.
*
* This is an offline method.
*
* @return array
*/
public function getChanges()
{
$changes = array(
'add' => array(),
'delete' => array(),
'replace' => array());
foreach ($this->_currentData as $key => $value) {
if (!array_key_exists($key, $this->_originalData) && !empty($value)) {
$changes['add'][$key] = $value;
} else if (count($this->_originalData[$key]) === 0 && !empty($value)) {
$changes['add'][$key] = $value;
} else if ($this->_originalData[$key] !== $this->_currentData[$key]) {
if (empty($value)) {
$changes['delete'][$key] = $value;
} else {
$changes['replace'][$key] = $value;
}
}
}
return $changes;
}
/**
* Sets a LDAP attribute.
*
* This is an offline method.
*
* @param string $name
* @param mixed $value
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function setAttribute($name, $value)
{
$this->_setAttribute($name, $value, false);
return $this;
}
/**
* Appends to a LDAP attribute.
*
* This is an offline method.
*
* @param string $name
* @param mixed $value
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function appendToAttribute($name, $value)
{
$this->_setAttribute($name, $value, true);
return $this;
}
/**
* Checks if the attribute can be set and sets it accordingly.
*
* @param string $name
* @param mixed $value
* @param boolean $append
* @throws Zend_Ldap_Exception
*/
protected function _setAttribute($name, $value, $append)
{
$this->_assertChangeableAttribute($name);
Zend_Ldap_Attribute::setAttribute($this->_currentData, $name, $value, $append);
}
/**
* Sets a LDAP date/time attribute.
*
* This is an offline method.
*
* @param string $name
* @param integer|array $value
* @param boolean $utc
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function setDateTimeAttribute($name, $value, $utc = false)
{
$this->_setDateTimeAttribute($name, $value, $utc, false);
return $this;
}
/**
* Appends to a LDAP date/time attribute.
*
* This is an offline method.
*
* @param string $name
* @param integer|array $value
* @param boolean $utc
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function appendToDateTimeAttribute($name, $value, $utc = false)
{
$this->_setDateTimeAttribute($name, $value, $utc, true);
return $this;
}
/**
* Checks if the attribute can be set and sets it accordingly.
*
* @param string $name
* @param integer|array $value
* @param boolean $utc
* @param boolean $append
* @throws Zend_Ldap_Exception
*/
protected function _setDateTimeAttribute($name, $value, $utc, $append)
{
$this->_assertChangeableAttribute($name);
Zend_Ldap_Attribute::setDateTimeAttribute($this->_currentData, $name, $value, $utc, $append);
}
/**
* Sets a LDAP password.
*
* @param string $password
* @param string $hashType
* @param string $attribName
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function setPasswordAttribute($password, $hashType = Zend_Ldap_Attribute::PASSWORD_HASH_MD5,
$attribName = 'userPassword')
{
$this->_assertChangeableAttribute($attribName);
Zend_Ldap_Attribute::setPassword($this->_currentData, $password, $hashType, $attribName);
return $this;
}
/**
* Deletes a LDAP attribute.
*
* This method deletes the attribute.
*
* This is an offline method.
*
* @param string $name
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function deleteAttribute($name)
{
if ($this->existsAttribute($name, true)) {
$this->_setAttribute($name, null, false);
}
return $this;
}
/**
* Removes duplicate values from a LDAP attribute
*
* @param string $attribName
* @return void
*/
public function removeDuplicatesFromAttribute($attribName)
{
Zend_Ldap_Attribute::removeDuplicatesFromAttribute($this->_currentData, $attribName);
}
/**
* Remove given values from a LDAP attribute
*
* @param string $attribName
* @param mixed|array $value
* @return void
*/
public function removeFromAttribute($attribName, $value)
{
Zend_Ldap_Attribute::removeFromAttribute($this->_currentData, $attribName, $value);
}
/**
* @param string $name
* @return boolean
* @throws Zend_Ldap_Exception
*/
protected function _assertChangeableAttribute($name)
{
$name = strtolower($name);
$rdn = $this->getRdnArray(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
if ($name == 'dn') {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, 'DN cannot be changed.');
}
else if (array_key_exists($name, $rdn)) {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, 'Cannot change attribute because it\'s part of the RDN');
} else if (in_array($name, self::$_systemAttributes)) {
/**
* @see Zend_Ldap_Exception
*/
require_once 'Zend/Ldap/Exception.php';
throw new Zend_Ldap_Exception(null, 'Cannot change attribute because it\'s read-only');
}
else return true;
}
/**
* Sets a LDAP attribute.
*
* This is an offline method.
*
* @param string $name
* @param mixed $value
* @return null
* @throws Zend_Ldap_Exception
*/
public function __set($name, $value)
{
$this->setAttribute($name, $value);
}
/**
* Deletes a LDAP attribute.
*
* This method deletes the attribute.
*
* This is an offline method.
*
* @param string $name
* @return null
* @throws Zend_Ldap_Exception
*/
public function __unset($name)
{
$this->deleteAttribute($name);
}
/**
* Sets a LDAP attribute.
* Implements ArrayAccess.
*
* This is an offline method.
*
* @param string $name
* @param mixed $value
* @return null
* @throws Zend_Ldap_Exception
*/
public function offsetSet($name, $value)
{
$this->setAttribute($name, $value);
}
/**
* Deletes a LDAP attribute.
* Implements ArrayAccess.
*
* This method deletes the attribute.
*
* This is an offline method.
*
* @param string $name
* @return null
* @throws Zend_Ldap_Exception
*/
public function offsetUnset($name)
{
$this->deleteAttribute($name);
}
/**
* Check if node exists on LDAP.
*
* This is an online method.
*
* @param Zend_Ldap $ldap
* @return boolean
* @throws Zend_Ldap_Exception
*/
public function exists(Zend_Ldap $ldap = null)
{
if ($ldap !== null) {
$this->attachLdap($ldap);
}
$ldap = $this->getLdap();
return $ldap->exists($this->_getDn());
}
/**
* Reload node attributes from LDAP.
*
* This is an online method.
*
* @param Zend_Ldap $ldap
* @return Zend_Ldap_Node Provides a fluid interface
* @throws Zend_Ldap_Exception
*/
public function reload(Zend_Ldap $ldap = null)
{
if ($ldap !== null) {
$this->attachLdap($ldap);
}
$ldap = $this->getLdap();
parent::reload($ldap);
return $this;
}
/**
* Search current subtree with given options.
*
* This is an online method.
*
* @param string|Zend_Ldap_Filter_Abstract $filter
* @param integer $scope
* @param string $sort
* @return Zend_Ldap_Node_Collection
* @throws Zend_Ldap_Exception
*/
public function searchSubtree($filter, $scope = Zend_Ldap::SEARCH_SCOPE_SUB, $sort = null)
{
/**
* @see Zend_Ldap_Node_Collection
*/
require_once 'Zend/Ldap/Node/Collection.php';
return $this->getLdap()->search($filter, $this->_getDn(), $scope, array('*', '+'), $sort,
'Zend_Ldap_Node_Collection');
}
/**
* Count items in current subtree found by given filter.
*
* This is an online method.
*
* @param string|Zend_Ldap_Filter_Abstract $filter
* @param integer $scope
* @return integer
* @throws Zend_Ldap_Exception
*/
public function countSubtree($filter, $scope = Zend_Ldap::SEARCH_SCOPE_SUB)
{
return $this->getLdap()->count($filter, $this->_getDn(), $scope);
}
/**
* Count children of current node.
*
* This is an online method.
*
* @return integer
* @throws Zend_Ldap_Exception
*/
public function countChildren()
{
return $this->countSubtree('(objectClass=*)', Zend_Ldap::SEARCH_SCOPE_ONE);
}
/**
* Gets children of current node.
*
* This is an online method.
*
* @param string|Zend_Ldap_Filter_Abstract $filter
* @param string $sort
* @return Zend_Ldap_Node_Collection
* @throws Zend_Ldap_Exception
*/
public function searchChildren($filter, $sort = null)
{
return $this->searchSubtree($filter, Zend_Ldap::SEARCH_SCOPE_ONE, $sort);
}
/**
* Checks if current node has children.
* Returns whether the current element has children.
*
* Can be used offline but returns false if children have not been retrieved yet.
*
* @return boolean
* @throws Zend_Ldap_Exception
*/
public function hasChildren()
{
if (!is_array($this->_children)) {
if ($this->isAttached()) {
return ($this->countChildren() > 0);
} else {
return false;
}
} else {
return (count($this->_children) > 0);
}
}
/**
* Returns the children for the current node.
*
* Can be used offline but returns an empty array if children have not been retrieved yet.
*
* @return Zend_Ldap_Node_ChildrenIterator
* @throws Zend_Ldap_Exception
*/
public function getChildren()
{
if (!is_array($this->_children)) {
$this->_children = array();
if ($this->isAttached()) {
$children = $this->searchChildren('(objectClass=*)', null);
foreach ($children as $child) {
$this->_children[$child->getRdnString(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER)] = $child;
}
}
}
/**
* @see Zend_Ldap_Node_ChildrenIterator
*/
require_once 'Zend/Ldap/Node/ChildrenIterator.php';
return new Zend_Ldap_Node_ChildrenIterator($this->_children);
}
/**
* Returns the parent of the current node.
*
* @param Zend_Ldap $ldap
* @return Zend_Ldap_Node
* @throws Zend_Ldap_Exception
*/
public function getParent(Zend_Ldap $ldap = null)
{
if ($ldap !== null) {
$this->attachLdap($ldap);
}
$ldap = $this->getLdap();
$parentDn = $this->_getDn()->getParentDn(1);
return self::fromLdap($parentDn, $ldap);
}
/**
* Return the current attribute.
* Implements Iterator
*
* @return array
*/
public function current()
{
return $this;
}
/**
* Return the attribute name.
* Implements Iterator
*
* @return string
*/
public function key()
{
return $this->getRdnString();
}
/**
* Move forward to next attribute.
* Implements Iterator
*/
public function next()
{
$this->_iteratorRewind = false;
}
/**
* Rewind the Iterator to the first attribute.
* Implements Iterator
*/
public function rewind()
{
$this->_iteratorRewind = true;
}
/**
* Check if there is a current attribute
* after calls to rewind() or next().
* Implements Iterator
*
* @return boolean
*/
public function valid()
{
return $this->_iteratorRewind;
}
}