File: Tree.php

package info (click to toggle)
phpldapadmin 1.1.0.5-6%2Blenny2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,008 kB
  • ctags: 3,949
  • sloc: php: 17,735; xml: 1,532; sh: 388; makefile: 46
file content (206 lines) | stat: -rw-r--r-- 6,141 bytes parent folder | download
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
<?php
// $Header: /cvsroot/phpldapadmin/phpldapadmin/lib/Tree.php,v 1.2.2.2 2008/01/27 07:23:43 wurley Exp $

/**
 * @package phpLDAPadmin
 * @author The phpLDAPadmin development team
 * @author Xavier Bruyet
 *
 * Abstract class which represents the LDAP tree view ; the draw() method
 * must be implemented by subclasses
 * @see HTMLTree PLMTree
 */
abstract class Tree {
	// list of entries in the tree view cache
	// array : dn -> Entry
	protected $entries = array();

	// list of entries which are not visible in the tree view
	// array : dn -> (true|false)
	protected $misses = array();

	// ldap server id represented by this tree
	protected $server_id = -1;

	protected function __construct($server_id) {
		$this->server_id = $server_id;
	}

	static public function getInstance($server_id) {
		$tree = get_cached_item($server_id,'tree');
		if (!$tree) {
			$ldapserver = $_SESSION[APPCONFIG]->ldapservers->Instance($server_id);
			if (!$ldapserver) return null;

			$treeclass = $_SESSION[APPCONFIG]->GetValue('appearance','tree');
			eval('$tree = new '.$treeclass.'($server_id);');

			foreach ($ldapserver->getBaseDN() as $baseDn)
				if ($baseDn)
					$tree->addEntry($baseDn);

			set_cached_item($server_id,'tree','null',$tree);
		}
		return $tree;
	}

	public function getLdapServer() {
		return $_SESSION[APPCONFIG]->ldapservers->Instance($this->server_id);
	}

	/**
	 * This function will take the DN, convert it to lowercase and strip unnessary
	 * commas. This result will be used as the index for the tree object.
	 * Any display of a DN should use the object->dn entry, not the index.
	 * The reason we need to do this is because:
	 * uid=User A,ou=People,c=AU and
	 * uid=User B, ou=PeOpLe, c=au
	 * are infact in the same branch, but PLA will show them inconsistently.
	 */
	public function indexDN($dn) {
		$index = strtolower(join(',',pla_explode_dn($dn)));

		if (DEBUG_ENABLED)
			debug_log('Entered with (%s), Result (%s)',1,__FILE__,__LINE__,__METHOD__,$dn,$index);

		return $index;
	}

	/**
	 * Add an entry in the tree view ; the entry is added in the
	 * children array of its parent
	 *
	 * The added entry is created using the factory class defined
	 * in $_SESSION[APPCONFIG]->custom->appearance['entry_factory']
	 *
	 * @param $dn the dn of the entry to create
	 */
	public function addEntry($dn) {
		if (DEBUG_ENABLED)
			debug_log('Entered with (%s)',1,__FILE__,__LINE__,__METHOD__,$dn);

		$ldapserver = $_SESSION[APPCONFIG]->ldapservers->Instance($this->server_id);

		# We need to convert the DN to lower case, to avoid any case problems and strip any unnessary spaces after commas.
		$dnlower = $this->indexDN($dn);

		# If the parent entry is not in the tree, we add it
		$bases = $ldapserver->getBaseDN();

		if (! $bases)
			$bases = array('');
		elseif (! is_array($bases))
			$bases = array($bases);

		if (DEBUG_ENABLED)
			debug_log('Got BaseDNs (%s)',64,__FILE__,__LINE__,__METHOD__,$bases);

		$parent_entry = null;
		if (! in_array_ignore_case($dn,$bases)) {
			$parent_dn = get_container($dn);

			if (DEBUG_ENABLED)
				debug_log('Parent DNs (%s)',64,__FILE__,__LINE__,__METHOD__,$parent_dn);

			if ($parent_dn) {
				$parent_entry = $this->getEntry($parent_dn);
				if (! $parent_entry) {
					$this->addEntry($parent_dn);
					$parent_entry = $this->getEntry($parent_dn);
				}
			} else {
				if (DEBUG_ENABLED)
					debug_log('NO parent, entry (%s) ignored.',64,__FILE__,__LINE__,__METHOD__,$dn);
			}
		}

		if (isset($this->entries[$dnlower]))
			unset($this->entries[$dnlower]);

		# If this DN is in our miss list, we can remove it now.
		if (isset($this->misses[$dnlower]))
			unset($this->misses[$dnlower]);

		$entryfactoryclass = $_SESSION[APPCONFIG]->GetValue('appearance','entry_factory');
		eval('$entry_factory = new '.$entryfactoryclass.'();');
		if (DEBUG_ENABLED)
			debug_log('New ENTRY (%s) for (%s).',64,__FILE__,__LINE__,__METHOD__,$dnlower,$dn);
		$this->entries[$dnlower] = $entry_factory->newEditingEntry($dn);

		$this->entries[$dnlower]->setTree($ldapserver->server_id);
		if ($ldapserver->isReadOnly())
			$this->entries[$dnlower]->setReadOnly();

		# Update this DN's parent's children list as well.
		if ($parent_entry)
			$parent_entry->addChild($dn);

		if (DEBUG_ENABLED)
			debug_log('Leaving (%s)',1,__FILE__,__LINE__,__METHOD__,$dn);
	}

	/**
	 * Delete an entry from the tree view ; the entry is deleted from the
	 * children array of its parent
	 */
	public function delEntry($dn) {
		if (DEBUG_ENABLED)
			debug_log('Entered with (%s)',1,__FILE__,__LINE__,__METHOD__,$dn);

		$dnlower = $this->indexDN($dn);
		if (isset($this->entries[$dnlower])) unset($this->entries[$dnlower]);

		# Delete entry from parent's children as well.
		$parent_dn = get_container($dn);
		$parent_entry = $this->getEntry($parent_dn);
		if ($parent_entry) $parent_entry->delChild($dn);

		# Might be worthwhile adding it to our miss list, while we are here.
		$this->misses[$dnlower] = true;
	}

	public function renameEntry($oldDn, $newDn) {
		if (DEBUG_ENABLED)
			debug_log('Entered with (%s,%s)',1,__FILE__,__LINE__,__METHOD__,$oldDn,$newDn);

		$olddnlower = $this->indexDN($oldDn);
		$newdnlower = $this->indexDN($newDn);

		$this->entries[$newdnlower] = $this->entries[$olddnlower];
		unset($this->entries[$olddnlower]);
		$this->entries[$newdnlower]->rename($newDn);

		# Might be worthwhile adding it to our miss list, while we are here.
		$this->misses[$olddnlower] = true;
		if (isset($this->misses[$newdnlower])) unset($this->misses[$newdnlower]);

		# Update the parent's children
		$parent_dn = get_container($newDn);
		$parent_entry = $this->getEntry($parent_dn);
		if ($parent_entry) {
			$parent_entry->delChild($oldDn);
			$parent_entry->addChild($newDn);
		}
	}

	public function getEntry($dn) {
		$dnlower = $this->indexDN($dn);

		if (isset($this->entries[$dnlower]))
			return $this->entries[$dnlower];
		else
			return null;
	}

	public function isMissed($dn) {
		$dnlower = $this->indexDN($dn);

		return isset($this->misses[$dnlower]) && $this->misses[$dnlower];
	}

	/**
	 * Displays the LDAP tree
	 */
	abstract public function draw();
}
?>