1: <?php
2: /**
3: * Getting the rootDSE entry of a LDAP server.
4: *
5: * Copyright 2009 Jan Wagner
6: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
7: *
8: * @category Horde
9: * @package Ldap
10: * @author Jan Wagner <wagner@netsols.de>
11: * @author Jan Schneider <jan@horde.org>
12: * @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0
13: */
14: class Horde_Ldap_RootDse
15: {
16: /**
17: * @var object Horde_Ldap_Entry
18: */
19: protected $_entry;
20:
21: /**
22: * Constructor.
23: *
24: * Fetches a RootDSE object from an LDAP connection.
25: *
26: * @param Horde_Ldap $ldap Directory from which the RootDSE should be
27: * fetched.
28: * @param array $attrs Array of attributes to search for.
29: *
30: * @throws Horde_Ldap_Exception
31: */
32: public function __construct(Horde_Ldap $ldap, $attrs = null)
33: {
34: if (is_array($attrs) && count($attrs)) {
35: $attributes = $attrs;
36: } else {
37: $attributes = array('vendorName',
38: 'vendorVersion',
39: 'namingContexts',
40: 'altServer',
41: 'supportedExtension',
42: 'supportedControl',
43: 'supportedSASLMechanisms',
44: 'supportedLDAPVersion',
45: 'subschemaSubentry');
46: }
47: $referral = $ldap->getOption('LDAP_OPT_REFERRALS');
48: $ldap->setOption('LDAP_OPT_REFERRALS', false);
49: try {
50: $result = $ldap->search('', '(objectClass=*)',
51: array('attributes' => $attributes,
52: 'scope' => 'base'));
53: } catch (Horde_Ldap_Exception $e) {
54: $ldap->setOption('LDAP_OPT_REFERRALS', $referral);
55: throw $e;
56: }
57: $ldap->setOption('LDAP_OPT_REFERRALS', $referral);
58: $entry = $result->shiftEntry();
59: if (!$entry) {
60: throw new Horde_Ldap_Exception('Could not fetch RootDSE entry');
61: }
62: $this->_entry = $entry;
63: }
64:
65: /**
66: * Returns the requested attribute value.
67: *
68: * @see Horde_Ldap_Entry::getValue()
69: *
70: * @param string $attr Attribute name.
71: * @param array $options Array of options.
72: *
73: * @return string|array Attribute value(s).
74: * @throws Horde_Ldap_Exception
75: */
76: public function getValue($attr, $options = '')
77: {
78: return $this->_entry->getValue($attr, $options);
79: }
80:
81: /**
82: * Determines if the extension is supported.
83: *
84: * @param array $oids Array of OIDs to check.
85: *
86: * @return boolean
87: */
88: public function supportedExtension($oids)
89: {
90: return $this->checkAttr($oids, 'supportedExtension');
91: }
92:
93: /**
94: * Determines if the version is supported.
95: *
96: * @param array $versions Versions to check.
97: *
98: * @return boolean
99: */
100: public function supportedVersion($versions)
101: {
102: return $this->checkAttr($versions, 'supportedLDAPVersion');
103: }
104:
105: /**
106: * Determines if the control is supported.
107: *
108: * @param array $oids Control OIDs to check.
109: *
110: * @return boolean
111: */
112: public function supportedControl($oids)
113: {
114: return $this->checkAttr($oids, 'supportedControl');
115: }
116:
117: /**
118: * Determines if the sasl mechanism is supported.
119: *
120: * @param array $mechlist SASL mechanisms to check.
121: *
122: * @return boolean
123: */
124: public function supportedSASLMechanism($mechlist)
125: {
126: return $this->checkAttr($mechlist, 'supportedSASLMechanisms');
127: }
128:
129: /**
130: * Checks for existance of value in attribute.
131: *
132: * @param array $values Values to check.
133: * @param string $attr Attribute name.
134: *
135: * @return boolean
136: */
137: protected function checkAttr($values, $attr)
138: {
139: if (!is_array($values)) {
140: $values = array($values);
141: }
142:
143: foreach ($values as $value) {
144: if (!in_array($value, $this->get_value($attr, 'all'))) {
145: return false;
146: }
147: }
148:
149: return true;
150: }
151: }
152: