File: ds_ldap_pla.php

package info (click to toggle)
phpldapadmin 1.2.6.7-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,528 kB
  • sloc: php: 17,684; javascript: 5,299; xml: 1,498; sh: 379; python: 148; makefile: 23
file content (687 lines) | stat: -rw-r--r-- 21,857 bytes parent folder | download | duplicates (3)
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
<?php
/**
 * Classes and functions for communication of Data Stores
 *
 * @author The phpLDAPadmin development team
 * @package phpLDAPadmin
 */

/**
 * This abstract class provides variables and methods for LDAP datastores for use by PLA.
 *
 * @package phpLDAPadmin
 * @subpackage DataStore
 */
class ldap_pla extends ldap {
	function __construct($index) {
		parent::__construct($index);

		$this->default->appearance['pla_password_hash'] = array(
			'desc'=>'Default HASH to use for passwords',
			'default'=>'md5');

		$this->default->appearance['show_create'] = array(
			'desc'=>'Whether to show the "Create new Entry here" in the tree browser',
			'default'=>true);

		$this->default->appearance['open_tree'] = array(
			'desc'=>'Whether to initially open each tree',
			'default'=>false);

		$this->default->appearance['show_authz'] = array(
			'desc'=>'Enable display of authorization ID as login',
			'default'=>false);

		$this->default->login['fallback_dn'] = array(
			'desc'=>'If the attribute base login fails, see if a DN was entered',
			'default'=>false);

		$this->default->query['disable_default'] = array(
			'desc'=>'Configuration to disable the default query template',
			'default'=>false);

		$this->default->query['custom_only'] = array(
			'desc'=>'Configuration to force the usage of custom query templates',
			'default'=>false);

		$this->default->server['branch_rename'] = array(
			'desc'=>'Enable renaming of branches',
			'default'=>false);

		$this->default->server['custom_attrs'] = array(
			'desc'=>'Custom operational attributes to be treated as regular attributes',
			'default'=>array(''));

		$this->default->server['custom_sys_attrs'] = array(
			'desc'=>'Custom operational attributes to be treated as internal attributes',
			'default'=>array('+'));

		$this->default->server['jpeg_attributes'] = array(
			'desc'=>'Additional attributes to treat as Jpeg Attributes',
			'default'=>array());

		# This was added in case the LDAP server doesnt provide them with a base +,* query.
		$this->default->server['root_dse_attributes'] = array(
			'desc'=>'RootDSE attributes for use when displaying server info',
			'default'=>array(
				'namingContexts',
				'subschemaSubentry',
				'altServer',
				'supportedExtension',
				'supportedControl',
				'supportedSASLMechanisms',
				'supportedLDAPVersion',
				'currentTime',
				'dsServiceName',
				'defaultNamingContext',
				'schemaNamingContext',
				'configurationNamingContext',
				'rootDomainNamingContext',
				'supportedLDAPPolicies',
				'highestCommittedUSN',
				'dnsHostName',
				'ldapServiceName',
				'serverName',
				'supportedCapabilities',
				'changeLog',
				'tlsAvailableCipherSuites',
				'tlsImplementationVersion',
				'supportedSASLMechanisms',
				'dsaVersion',
				'myAccessPoint',
				'dseType',
				'+',
				'*'
			));

		$this->default->server['force_may'] = array(
			'desc'=>'Force server MUST attributes as MAY attributes',
			'default'=>array(
			));

		# Settings for auto_number
		$this->default->auto_number['enable'] = array(
			'desc'=>'Enable the AUTO UID feature',
			'default'=>true);

		$this->default->auto_number['mechanism'] = array(
			'desc'=>'Mechanism to use to search for automatic numbers',
			'default'=>'search');

		$this->default->auto_number['search_base'] = array(
			'desc'=>'Base DN to use for search mechanisms',
			'default'=>null);

		$this->default->auto_number['min'] = array(
			'desc'=>'Minimum number to start with',
			'default'=>array('uidNumber'=>1000,'gidNumber'=>500));

		$this->default->auto_number['dn'] = array(
			'desc'=>'DN to use when evaluating numbers',
			'default'=>null);

		$this->default->auto_number['pass'] = array(
			'desc'=>'Password for DN to use when evaluating numbers',
			'default'=>null);

		$this->default->unique['attrs'] = array(
			'desc'=>'Attributes to check for uniqueness before allowing updates',
			'default'=>array('mail','uid','uidNumber'));

		$this->default->unique['dn'] = array(
			'desc'=>'DN to use when evaluating attribute uniqueness',
			'default'=>null);

		$this->default->unique['pass'] = array(
			'desc'=>'Password for DN to use when evaluating attribute uniqueness',
			'default'=>null);
	}

	public function __get($key) {
		switch ($key) {
			case 'name':
				return $this->getValue('server','name');

			default:
				system_message(array(
					'title'=>_('Unknown request for Object value.'),
					'body'=>sprintf(_('Attempt to obtain value %s from %s'),$key,get_class($this)),
					'type'=>'error'));
		}
	}

	/**
	 * Gets whether the admin has configured phpLDAPadmin to show the "Create New" link in the tree viewer.
	 * <code>
	 *	$servers->setValue('appearance','show_create',true|false);
	 * </code>
	 * If NOT set, then default to show the Create New item.
	 * If IS set, then return the value (it should be true or false).
	 *
	 * The entry creation command must be available.
	 * <code>
	 *	$config->custom->commands['script'] = array('create' => true);
	 * </code>
	 *
	 * @return boolean true if the feature is enabled and false otherwise.
	 */
	function isShowCreateEnabled() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		if (! $_SESSION[APPCONFIG]->isCommandAvailable('script','create'))
			return false;
		else
			return $this->getValue('appearance','show_create');
	}

	/**
	 * Fetch whether the user has configured a certain server login to be non anonymous
	 *
	 * <code>
	 *	$servers->setValue('login','anon_bind',true|false);
	 * </code>
	 *
	 * @return boolean
	 */
	public function isAnonBindAllowed() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		# If only_login_allowed_dns is set, then we cant have anonymous.
		if (count($this->getValue('login','allowed_dns')) > 0)
			$return = false;
		else
			$return = $this->getValue('login','anon_bind');

		if (DEBUG_ENABLED)
			debug_log('Returning (%s)',17,0,__FILE__,__LINE__,__METHOD__,$return);

		return $return;
	}

	/**
	 * Returns true if the user has configured the specified server to enable branch (non-leaf) renames.
	 *
	 * This is configured in config.php thus:
	 * <code>
	 *	$servers->setValue('server','branch_rename',true|false);
	 * </code>
	 *
	 * @return boolean
	 */
	function isBranchRenameEnabled() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

 		return $this->getValue('server','branch_rename');
	}

	/**
	 * Determines if an attribute's value can contain multiple lines. Attributes that fall
	 * in this multi-line category may be configured in config.php. Hence, this function
	 * accesses the global variable $_SESSION[APPCONFIG]->custom->appearance['multi_line_attributes'];
	 *
	 * Usage example:
	 * <code>
	 *	if ($ldapserver->isMultiLineAttr('postalAddress'))
	 *		echo '<textarea name="postalAddress"></textarea>';
	 *	else
	 *		echo '<input name="postalAddress" type="text">';
	 * </code>
	 *
	 * @param string The name of the attribute of interested (case insensivite)
	 * @param string (optional) The current value of the attribute (speeds up the process by searching for carriage returns already in the attribute value)
	 * @return boolean
	 */
	function isMultiLineAttr($attr_name,$val=null) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		# Set default return
		$return = false;

		# First, check the optional val param for a \n or a \r
		if (! is_null($val) && (strpos($val,"\n") || strpos($val,"\r")))
			$return = true;

		# Next, compare strictly by name first
		else
			foreach ($_SESSION[APPCONFIG]->getValue('appearance','multi_line_attributes') as $multi_line_attr_name)
				if (strcasecmp($multi_line_attr_name,$attr_name) == 0) {
					$return = true;
					break;
				}

		# If unfound, compare by syntax OID
		if (! $return) {
			$sattr = $this->getSchemaAttribute($attr_name);

			if ($sattr) {
				$syntax_oid = $sattr->getSyntaxOID();

				if ($syntax_oid)
					foreach ($_SESSION[APPCONFIG]->getValue('appearance','multi_line_syntax_oids') as $multi_line_syntax_oid)
						if ($multi_line_syntax_oid == $syntax_oid) {
							$return = true;
							break;
						}
			}
		}

		if (DEBUG_ENABLED)
			debug_log('Returning (%s)',17,0,__FILE__,__LINE__,__METHOD__,$return);

		return $return;
	}

	/**
	 * Returns true if the specified attribute is configured according to
	 * the test enabled in config.php
	 *
	 * @param string The name of the attribute to test.
	 * @param array The attributes to test against.
	 * @param dn A DN that is exempt from these tests.
	 * @return boolean
	 */
	private function isAttrTest($attr,$attrs,$except_dn) {
		$attr = trim($attr);
		if (! trim($attr) || ! count($attrs))
			return false;

		# Is the user excluded?
		if ($except_dn && $this->userIsMember($this->getLogin(),$except_dn))
			return false;

		foreach ($attrs as $attr_name)
			if (strcasecmp($attr,trim($attr_name)) == 0)
				return true;

		return false;
	}

	/**
	 * Returns true if the specified attribute is configured as read only
	 * in config.php.
	 * Attributes are configured as read-only in config.php thus:
	 * <code>
	 *	$config->custom->appearance['readonly_attrs'] = array('objectClass');
	 * </code>
	 *
	 * @param string The name of the attribute to test.
	 * @return boolean
	 */
	public function isAttrReadOnly($attr) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$attrs = $_SESSION[APPCONFIG]->getValue('appearance','readonly_attrs');
		$except_dn = $_SESSION[APPCONFIG]->getValue('appearance','readonly_attrs_exempt');

		return $this->isAttrTest($attr,$attrs,$except_dn);
	}

	/**
	 * Returns true if the specified attribute is configured as hidden
	 * in config.php.
	 * Attributes are configured as hidden in config.php thus:
	 * <code>
	 *	$config->custom->appearance['hide_attrs'] = array('objectClass');
	 * </code>
	 *
	 * @param string The name of the attribute to test.
	 * @return boolean
	 */
	public function isAttrHidden($attr) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$attrs = $_SESSION[APPCONFIG]->getValue('appearance','hide_attrs');
		$except_dn = $_SESSION[APPCONFIG]->getValue('appearance','hide_attrs_exempt');

		return $this->isAttrTest($attr,$attrs,$except_dn);
	}

	/**
	 * Add objects
	 */
	public function add($dn,$entry_array,$method=null) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		foreach ($entry_array as $attr => $val)
			$entry_array[$attr] = dn_unescape($val);

		$result = false;

		# Check our unique attributes.
		if (! $this->checkUniqueAttrs($dn,$entry_array))
			return false;

		if (run_hook('pre_entry_create',array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attrs'=>$entry_array))) {
			$result = @ldap_add($this->connect($method),dn_escape($dn),$entry_array);

			if ($result) {
				# Update the tree
				$tree = get_cached_item($this->index,'tree');

				# If we created the base, delete it, then add it back
				if (get_request('create_base'))
					$tree->delEntry($dn);

				$tree->addEntry($dn);

				set_cached_item($this->index,'tree','null',$tree);

				run_hook('post_entry_create',array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attrs'=>$entry_array));

			} else {
				system_message(array(
					'title'=>_('Could not add the object to the LDAP server.'),
					'body'=>ldap_error_msg($this->getErrorMessage(null),$this->getErrorNum(null)),
					'type'=>'error'));
			}
		}

		return $result;
	}

	/**
	 * Delete objects
	 */
	public function delete($dn,$method=null) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$result = false;

		if (run_hook('pre_entry_delete',array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn))) {
			$result = @ldap_delete($this->connect($method),dn_escape($dn));

			if ($result) {
				# Update the tree
				$tree = get_cached_item($this->index,'tree');
				$tree->delEntry($dn);

				set_cached_item($this->index,'tree','null',$tree);

				run_hook('post_entry_delete',array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn));
			}
		}

		return $result;
	}

	/**
	 * Rename objects
	 */
	public function rename($dn,$new_rdn,$container,$deleteoldrdn,$method=null) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$result = false;

		if (run_hook('pre_entry_rename',array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'rdn'=>$new_rdn,'container'=>$container))) {
			$result = @ldap_rename($this->connect($method),$dn,$new_rdn,$container,$deleteoldrdn);

			if ($result) {
				# Update the tree
				$tree = get_cached_item($this->index,'tree');
				$newdn = sprintf('%s,%s',$new_rdn,$container);
				$tree->renameEntry($dn,$newdn);

				set_cached_item($this->index,'tree','null',$tree);

				run_hook('post_entry_rename',array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'rdn'=>$new_rdn,'container'=>$container));
			}
		}

		return $result;
	}

	/**
	 * Modify objects
	 */
	public function modify($dn,$attrs,$method=null) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		# Check our unique attributes.
		if (! $this->checkUniqueAttrs($dn,$attrs))
			return false;

		$result = false;
		$summary = array();
		$current_attrs = $this->getDNAttrValues($dn,$method,LDAP_DEREF_NEVER,array('*'));

		# Go through our attributes and call our hooks for each attribute changing its value
		foreach ($attrs as $attr => $values) {
			# For new attributes
			if (count($values) && ! isset($current_attrs[$attr])) {
				if (! run_hook('pre_attr_add',
					array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attr'=>$attr,'newvalue'=>$values))) {

					unset($attrs[$attr]);
					system_message(array(
						'title'=>_('Attribute not added'),
						'body'=>sprintf('%s (<b>%s</b>)',_('Hook pre_attr_add prevented attribute from being added'),$attr),
						'type'=>'warn'));

				} else
					$summary['add'][$attr]['new'] = $values;

			# For modify attributes
			} elseif (count($values)) {
				if (! run_hook('pre_attr_modify',
					array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attr'=>$attr,'oldvalue'=>$current_attrs[$attr],'newvalue'=>$values))) {

					unset($attrs[$attr]);
					system_message(array(
						'title'=>_('Attribute not modified'),
						'body'=>sprintf('%s (<b>%s</b>)',_('Hook pre_attr_modify prevented attribute from being modified'),$attr),
						'type'=>'warn'));

				} else {
					$summary['modify'][$attr]['new'] = $values;
					$summary['modify'][$attr]['old'] = $current_attrs[$attr];
				}

			# For delete attributes
			} else {
				if (! run_hook('pre_attr_delete',
					array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attr'=>$attr,'oldvalue'=>$current_attrs[$attr]))) {

					unset($attrs[$attr]);
					system_message(array(
						'title'=>_('Attribute not deleted'),
						'body'=>sprintf('%s (<b>%s</b>)',_('Hook pre_attr_delete prevented attribute from being deleted'),$attr),
						'type'=>'warn'));

				} else
					$summary['delete'][$attr]['old'] = $current_attrs[$attr];
			}
		}

		if (! count($attrs))
			return false;

		if (run_hook('pre_entry_modify',array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attrs'=>$attrs))) {
			$result = @ldap_modify($this->connect($method),$dn,$attrs);

			if ($result) {
				run_hook('post_entry_modify',array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attrs'=>$attrs));

				foreach (array('add','modify','delete') as $mode)
					if (isset($summary[$mode]))
						foreach ($summary[$mode] as $attr => $values)
							switch ($mode) {
								case 'add':
									run_hook(sprintf('post_attr_%s',$mode),
										array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attr'=>$attr,'newvalue'=>$values['new']));
									break;

								case 'modify':
									run_hook(sprintf('post_attr_%s',$mode),
										array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attr'=>$attr,'oldvalue'=>$values['old'],'newvalue'=>$values['new']));
									break;

								case 'delete':
									run_hook(sprintf('post_attr_%s',$mode),
										array('server_id'=>$this->index,'method'=>$method,'dn'=>$dn,'attr'=>$attr,'oldvalue'=>$values['old']));
									break;

								default:
									debug_dump_backtrace(sprintf('Unkown mode %s',$mode),1);
							}
			} else {
				system_message(array(
					'title'=>_('Could not perform ldap_modify operation.'),
					'body'=>ldap_error_msg($this->getErrorMessage($method),$this->getErrorNum($method)),
					'type'=>'error'));
			}
		}

		return $result;
	}

	/**
	 * Returns true if the specified attribute is configured as unique
	 * in config.php.
	 * Attributes are configured as hidden in config.php thus:
	 * <code>
	 *	$servers->setValue('unique','attrs',array('mail','uid','uidNumber'));
	 * </code>
	 *
	 * @param string $attr The name of the attribute to test.
	 * @return boolean
	 */
	public function isAttrUnique($attr) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		# Should this attribute value be unique
		if (in_array_ignore_case($attr,$this->getValue('unique','attrs')))
			return true;
		else
			return false;
	}

	/**
	 * This function will check whether the value for an attribute being changed
	 * is already assigned to another DN.
	 *
	 * Returns the bad value, or null if all values are OK
	 *
	 * @param dn DN that is being changed
	 * @param string Attribute being changed
	 * @param string|array New values for the attribute
	 */
	public function checkUniqueAttrs($dn,$attrs) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		# If none of the attributes are defined unique, we'll return immediately;
		if (! $checkattrs = array_intersect(arrayLower($this->getValue('unique','attrs')),array_keys(array_change_key_case($attrs))))
			return true;

		# Check see and use our alternate uid_dn and password if we have it.
		if (! $this->login($this->getValue('unique','dn'),$this->getValue('unique','pass'),'unique')) {
			system_message(array(
				'title'=>_('UNIQUE invalid login/password'),
				'body'=>sprintf('%s (<b>%s</b>)',_('Unable to connect to LDAP server with the unique login/password, please check your configuration.'),
					$this->getName()),
				'type'=>'warn'));

			return false;
		}

		$query = array();

		# Build our search filter to double check each attribute.
		$query['filter'] = '(|';
		foreach ($checkattrs as $attr) {
			if (!is_array($attrs[$attr])) {
				$val = $attrs[$attr];
				$query['filter'] .= sprintf('(%s=%s)',$attr,$val);
				continue;
			}
			foreach ($attrs[$attr] as $val)
				if ($val)
					$query['filter'] .= sprintf('(%s=%s)',$attr,$val);
		}
		$query['filter'] .= ')';

		$query['attrs'] = $checkattrs;

		# Search through our bases and see if we have match
		foreach ($this->getBaseDN() as $base) {
			$query['base'] = $base;

			# Do the search
			$results = $this->query($query,'unique');

			# If we have a match.
			if (count($results))
				foreach ($results as $values)
					# If one of the attributes is owned to somebody else, then we may as well die here.
					if ($values['dn'] != $dn) {
						$href = sprintf('cmd.php?cmd=query_engine&server_id=%s&filter=%s&scope=sub&query=none&format=list&search=true',$this->index,$query['filter']);

						system_message(array(
							'title'=>_('Attribute value would not be unique'),
							'body'=>sprintf('%s (<b><a href="%s">%s</a></b>)',
								_('This update has been or will be cancelled, it would result in an attribute value not being unique. You might like to search the LDAP server for the offending entry.'),
								htmlspecialchars($href),
								_('Search')),
							'type'=>'warn'));

						return false;
					}
		}

		# If we get here, then it must be OK?
		return true;
	}

	/**
	 * Check if the session timeout has occured for this LDAP server.
	 */
	public function isSessionValid() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',17,0,__FILE__,__LINE__,__METHOD__,$fargs);

		# If inactiveTime() returns a value, we need to check that it has not expired.
		if (is_null($this->inactivityTime()) || ! $this->isLoggedIn())
			return true;

		# If session has expired
		if ((isset($_SESSION['ACTIVITY'][$this->getIndex()])) && ($_SESSION['ACTIVITY'][$this->getIndex()] < time())) {
			$this->logout();
			unset($_SESSION['ACTIVITY'][$this->getIndex()]);

			return false;
		}

		$_SESSION['ACTIVITY'][$this->getIndex()] = $this->inactivityTime();
		return true;
	}

	/**
	 * Return login, or authorization ID if show_authz enabled
	 */
	public function displayLogin($method=null) {
		// check for whoami function, added in 7.2
		if ($this->getValue('appearance', 'show_authz') && function_exists('ldap_exop_whoami')) {
			$result = @ldap_exop_whoami($this->connect($method));
			if ($result) // strip any dn: or u: prefix
				$result = preg_replace('/^(u|dn):/i', '', $result);
			else // fall back to login on error
				$result = $this->getLogin($method);
			return $result;
		}
		else {
			return $this->getLogin($method);
		}
	}
}
?>