forked from ulsdevteam/simple_ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleLdapSchema.class.php
468 lines (414 loc) · 12.1 KB
/
SimpleLdapSchema.class.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
<?php
/**
* @file
* Class to represent an LDAP server schema.
*/
/**
* Simple LDAP Schema class.
*/
class SimpleLdapSchema {
protected $dn;
protected $schema;
protected $server;
protected $attributes = array(
'attributeTypes',
'dITContentRules',
'dITStructureRules',
'matchingRules',
'matchingRuleUse',
'nameForms',
'objectClasses',
'ldapSyntaxes',
);
/**
* Constructor.
*/
public function __construct(SimpleLdapServer $server) {
$this->server = $server;
// Fallback for broken servers.
$this->dn = 'cn=Subschema';
// Load the subschema DN from the server.
try {
if (isset($this->server->rootdse['subschemasubentry'])) {
$this->dn = $this->server->rootdse['subschemasubentry'][0];
}
} catch (SimpleLdapException $e) {}
}
/**
* Magic __get function.
*
* @param string $name
* Name of the variable to get.
*
* @return mixed
* Returns the value of the requested variable, if allowed.
*/
public function __get($name) {
switch ($name) {
case 'dn':
case 'attributes':
return $this->$name;
case 'entry':
case 'subentry':
$this->load();
return $this->schema;
}
}
/**
* Magic __set function.
*
* @param string $name
* The name of the attribute to set.
* @param mixed $value
* The value to assigned to the given attribute.
*/
public function __set($name, $value) {
// The schema is read-only, just return.
return;
}
/**
* Returns whether the given item exists.
*
* @param string $attribute
* Name of the schema attribute type to check.
* @param string $name
* Name or OID of a single entry to check. If NULL, then this function will
* return whether or not the given attribute type is empty.
*
* @return boolean
* TRUE if the item exists, FALSE otherwise.
*
* @throw SimpleLdapException
*/
public function exists($attribute, $name = NULL) {
// Make sure the schema for the requested type is loaded.
$this->load($attribute);
// Check to see if the requested schema entry exists.
$attribute = drupal_strtolower($attribute);
if (isset($this->schema[$attribute])) {
if ($name === NULL) {
return (count($this->schema[$attribute]) > 0);
}
else {
if (isset($this->schema[$attribute][drupal_strtolower($name)])) {
// An attribute of the given name exists.
return TRUE;
}
else {
// Search for an alias or OID.
foreach ($this->schema[$attribute] as $attr) {
foreach ($attr['aliases'] as $alias) {
if (drupal_strtolower($alias) == drupal_strtolower($name)) {
return TRUE;
}
}
if ($attr['oid'] == $name) {
return TRUE;
}
}
}
}
}
return FALSE;
}
/**
* Fetches entries of the given type.
*
* @param string $attribute
* Name of the schema attribute type to return.
* @param string $name
* If specified, a single entry with this name or OID is returned.
*
* @return array
* The requested attribute list or entry.
*
* @throw SimpleLdapException
*/
public function get($attribute, $name = NULL) {
if ($this->exists($attribute, $name)) {
$attribute = drupal_strtolower($attribute);
if ($name === NULL) {
return $this->schema[$attribute];
}
else {
$name = drupal_strtolower($name);
if (isset($this->schema[$attribute][$name])) {
// Return a named attribute.
return $this->schema[$attribute][$name];
}
else {
// Search for an alias or OID.
foreach ($this->schema[$attribute] as $attr) {
foreach ($attr['aliases'] as $alias) {
if (drupal_strtolower($alias) == drupal_strtolower($name)) {
return $attr;
}
}
if ($attr['oid'] == $name) {
return $attr;
}
}
}
}
}
throw new SimpleLdapException('The requested entry does not exist: ' . $attribute . ', ' . $name);
}
/**
* Return a list of attributes defined for the objectclass.
*
* @param string $objectclass
* The objectclass to query for attributes.
* @param boolean $recursive
* If TRUE, the attributes of the parent objectclasses will also be
* retrieved.
*
* @return array
* A list of the MAY/MUST attributes.
*
* @throw SimpleLdapException
*/
public function attributes($objectclass, $recursive = FALSE) {
$may = $this->may($objectclass, $recursive);
if (!is_array($may)) {
$may = array();
}
$must = $this->must($objectclass, $recursive);
if (!is_array($must)) {
$must = array();
}
$attributes = array_merge($may, $must);
return $attributes;
}
/**
* Return a list of attributes specified as MAY for the objectclass.
*
* @param string $objectclass
* The objectclass to query for attributes.
* @param boolean $recursive
* If TRUE, the attributes of the parent objectclasses will also be
* retrieved.
*
* @return array
* A list of the MAY attributes.
*
* @throw SimpleLdapException
*/
public function may($objectclass, $recursive = FALSE) {
$oc = $this->get('objectclasses', $objectclass);
$may = array();
if (isset($oc['may'])) {
$may = $oc['may'];
}
if ($recursive && isset($oc['sup'])) {
foreach ($oc['sup'] as $sup) {
$may = array_merge($may, $this->may($sup, TRUE));
}
}
return $may;
}
/**
* Return a list of attributes specified as MUST for the objectclass.
*
* @param string $objectclass
* The objectclass to query for attributes.
* @param boolean $recursive
* If TRUE, the attributes of the parent objectclasses will also be
* retrieved.
*
* @return array
* A list of the MUST attributes.
*
* @throw SimpleLdapException
*/
public function must($objectclass, $recursive = FALSE) {
$oc = $this->get('objectclasses', $objectclass);
$must = array();
if (isset($oc['must'])) {
$must = $oc['must'];
}
if ($recursive && isset($oc['sup'])) {
foreach ($oc['sup'] as $sup) {
$must = array_merge($must, $this->must($sup, TRUE));
}
}
return $must;
}
/**
* Returns the objectclass's superclass.
*/
public function superclass($objectclass, $recursive = FALSE) {
if ($oc = $this->get('objectclasses', $objectclass)) {
$superclass = array();
if (isset($oc['sup'])) {
$superclass = $oc['sup'];
if ($recursive) {
foreach ($oc['sup'] as $sup) {
$superclass = array_merge($superclass, $this->superclass($sup, TRUE));
}
}
}
return $superclass;
}
return FALSE;
}
/**
* Load the schema.
*
* Schema parsing can be slow, so only the attributes that are specified, and
* are not already cached, are loaded.
*
* @param array $attributes
* A list of attributes to load. If not specified, all attributes are
* loaded.
*
* @throw SimpleLdapException
*/
protected function load($attributes = NULL) {
// If no attributes are specified, default to all attributes.
if ($attributes === NULL) {
$attributes = $this->attributes;
}
// Make sure $attributes is an array.
if (!is_array($attributes)) {
$attributes = array($attributes);
}
// Determine which attributes need to be loaded.
$load = array();
foreach ($attributes as $attribute) {
$attribute = drupal_strtolower($attribute);
if (!isset($this->schema[$attribute])) {
$load[] = $attribute;
}
}
// Load the attributes.
if (!empty($load)) {
$result = SimpleLdap::clean($this->server->search($this->dn, 'objectclass=*', 'base', $load));
// Parse the schema.
foreach ($load as $attribute) {
$attribute = drupal_strtolower($attribute);
$this->schema[$attribute] = array();
// Get the values for each attribute.
if (isset($result[$this->dn][$attribute])) {
foreach ($result[$this->dn][$attribute] as $value) {
$parsed = $this->parse($value);
$this->schema[$attribute][drupal_strtolower($parsed['name'])] = $parsed;
}
}
}
}
}
/**
* Parse a schema value into a usable array.
*
* @link
* http://pear.php.net/package/Net_LDAP2/
*
* @license
* http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
*/
protected function parse($value) {
// Tokens that have no associated value.
$novalue = array(
'single-value',
'obsolete',
'collective',
'no-user-modification',
'abstract',
'structural',
'auxiliary',
);
// Tokens that can have multiple values.
$multivalue = array('must', 'may', 'sup');
// Initialization.
$schema_entry = array('aliases' => array());
// Get an array of tokens.
$tokens = $this->tokenize($value);
// Remove left paren.
if ($tokens[0] == '(') {
array_shift($tokens);
}
// Remove right paren.
if ($tokens[count($tokens) - 1] == ')') {
array_pop($tokens);
}
// The first token is the OID.
$schema_entry['oid'] = array_shift($tokens);
// Loop through the tokens until there are none left.
while (count($tokens) > 0) {
$token = drupal_strtolower(array_shift($tokens));
if (in_array($token, $novalue)) {
// Single value token.
$schema_entry[$token] = 1;
}
else {
// This one follows a string or a list if it is multivalued.
if (($schema_entry[$token] = array_shift($tokens)) == '(') {
// This creates the list of values and cycles through the tokens until
// the end of the list is reached ')'.
$schema_entry[$token] = array();
while ($tmp = array_shift($tokens)) {
if ($tmp == ')') {
break;
}
if ($tmp != '$') {
array_push($schema_entry[$token], $tmp);
}
}
}
// Create an array if the value should be multivalued but was not.
if (in_array($token, $multivalue) && !is_array($schema_entry[$token])) {
$schema_entry[$token] = array($schema_entry[$token]);
}
}
}
// Get the max length from syntax.
if (key_exists('syntax', $schema_entry)) {
if (preg_match('/{(\d+)}/', $schema_entry['syntax'], $matches)) {
$schema_entry['max_length'] = $matches[1];
}
}
// Force a name.
if (empty($schema_entry['name'])) {
$schema_entry['name'] = $schema_entry['oid'];
}
// Make one name the default and put the others into aliases.
if (is_array($schema_entry['name'])) {
$aliases = $schema_entry['name'];
$schema_entry['name'] = array_shift($aliases);
$schema_entry['aliases'] = $aliases;
}
return $schema_entry;
}
/**
* Tokenizes the given value into an array of tokens.
*
* @link
* http://pear.php.net/package/Net_LDAP2/
*
* @license
* http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
*/
protected function tokenize($value) {
$tokens = array();
$matches = array();
// This one is taken from perl-lap, modified for php.
$pattern = "/\s* (?:([()]) | ([^'\s()]+) | '((?:[^']+|'[^\s)])*)') \s*/x";
// This one matches one big pattern wherin only one of the three subpatterns
// matched. We are interested in the subpatterns that matched. If it matched
// its value will be non-empty and so it is a token. Tokens may be round
// brackets, a string, or a string enclosed by "'".
preg_match_all($pattern, $value, $matches);
// Loop through all tokens (full pattern match).
for ($i = 0; $i < count($matches[0]); $i++) {
// Loop through each sub-pattern.
for ($j = 1; $j < 4; $j++) {
// Pattern match in this sub-pattern.
$token = trim($matches[$j][$i]);
if (!empty($token)) {
$tokens[$i] = $token;
}
}
}
return $tokens;
}
}