File: Tree.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 (351 lines) | stat: -rw-r--r-- 10,226 bytes parent folder | download | duplicates (5)
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
<?php
/**
 * Classes and functions for the LDAP tree.
 *
 * @author The phpLDAPadmin development team
 * @package phpLDAPadmin
 */

/**
 * Abstract class which represents the LDAP tree view ; the draw() method
 * must be implemented by subclasses
 *
 * @package phpLDAPadmin
 * @subpackage Tree
 * @see HTMLTree AJAXTree
 */
abstract class Tree {
	# Server that this tree represents
	private $server_id = null;
	# List of entries in the tree view cache
	protected $entries = array();

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

	protected function __construct($server_id) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$this->server_id = $server_id;
	}

	/**
	 * Create an instance of the tree - this is used when we call this class directly
	 * Tree::getInstance($index)
	 *
	 * @return object Tree
	 */
	static public function getInstance($server_id) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$tree = get_cached_item($server_id,'tree');

		if (! $tree) {
			$server = $_SESSION[APPCONFIG]->getServer($server_id);

			if (! $server)
				return null;

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

			# If we are not logged in, just return the empty tree.
			if (is_null($server->getLogin(null)))
				return $tree;

			foreach ($server->getBaseDN(null) as $base) {
				if ($base) {
					$tree->addEntry($base);

					if ($server->getValue('appearance','open_tree')) {
						$baseEntry = $tree->getEntry($base);
						$baseEntry->open();
					}
				}
			}

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

		return $tree;
	}

	/**
	 * Get the Server ID for this tree
	 *
	 * @return int Server ID that this tree is for
	 */
	protected function getServerID() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->server_id);

		return $this->server_id;
	}

	/**
	 * Get the server Object for this tree
	 *
	 * @return object Server Object for this tree
	 */
	protected function getServer() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		return $_SESSION[APPCONFIG]->getServer($this->server_id);
	}

	/**
	 * Get the entries that are BaseDN entries.
	 *
	 * @return array Base DN entries
	 */
	public function getBaseEntries() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$return = array();

		foreach ($this->entries as $details)
			if ($details->isBaseDN() AND ((! $this->getServer()->getValue('server','hide_noaccess_base')) OR $details->isInLdap()))
				array_push($return,$details);

		return $return;
	}

	/**
	 * 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.
	 *
	 * @param dn DN to clean
	 * @return dn Lowercase clean DN
	 */
	private function indexDN($dn) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$index = strtolower(implode(',',pla_explode_dn($dn)));

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

		return $index;
	}

	/**
	 * Get a tree entry
	 *
	 * @param dn DN to retrieve
	 * @return object Tree DN object
	 */
	public function getEntry($dn) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$dnlower = $this->indexDN($dn);

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

	/**
	 * Add an entry in the tree view ; the entry is added in the
	 * children array of its parent
	 *
	 * @param dn DN to add
	 * @param string $dn the dn of the entry to create
	 */
	public function addEntry($dn) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$server = $this->getServer();
		$dnlower = $this->indexDN($dn);

		# @todo Temporarily removed, some non-ascii char DNs that do exist, fail here for some reason?
		#if (! ($server->dnExists($dn)))
		#	return;

		if (isset($this->entries[$dnlower]))
			debug_dump_backtrace('Calling add entry to an entry that ALREADY exists?',1);

		if (DEBUG_ENABLED)
			debug_log('New ENTRY (%s).',64,0,__FILE__,__LINE__,__METHOD__,$dn);

		$tree_factory = new TreeItem($server->getIndex(),$dn);
		$tree_factory->setObjectClasses($server->getDNAttrValue($dn,'objectClass'));

		if ((($isleaf = $server->getDNAttrValue($dn,'hassubordinates')) && ! strcasecmp($isleaf[0],'false')))
			$tree_factory->setLeaf();

		$this->entries[$dnlower] = $tree_factory;

		# Is this entry in a base entry?
		if (in_array_ignore_case($dn,$server->getBaseDN(null))) {
			$this->entries[$dnlower]->setBase();

		# If the parent entry is not in the tree, we add it. This routine will in itself
		# recall this method until we get to the top of the tree (the base).
		} else {
			$parent_dn = $server->getContainer($dn);

			if (DEBUG_ENABLED)
				debug_log('Parent DNs (%s)',64,0,__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);
				}

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

	/**
	 * Delete an entry from the tree view ; the entry is deleted from the
	 * children array of its parent
	 *
	 * @param dn DN to remote
	 */
	public function delEntry($dn) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$server = $this->getServer();
		$dnlower = $this->indexDN($dn);

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

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

		if ($parent_entry)
			$parent_entry->delChild($dn);
	}

	/**
	 * Rename an entry in the tree
	 *
	 * @param dn Old DN
	 * @param dn New DN
	 */
	public function renameEntry($dnOLD,$dnNEW) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$server = $this->getServer();
		$dnlowerOLD = $this->indexDN($dnOLD);
		$dnlowerNEW = $this->indexDN($dnNEW);

		$this->entries[$dnlowerNEW] = $this->entries[$dnlowerOLD];
		if ($dnlowerOLD != $dnlowerNEW)
			unset($this->entries[$dnlowerOLD]);
		$this->entries[$dnlowerNEW]->rename($dnNEW);

		# Update the parent's children
		$parentNEW = $server->getContainer($dnNEW);
		$parentOLD = $server->getContainer($dnOLD);

		$parent_entry = $this->getEntry($parentNEW);
		if ($parent_entry)
			$parent_entry->addChild($dnNEW);

		$parent_entry = $this->getEntry($parentOLD);
		if ($parent_entry)
			$parent_entry->delChild($dnOLD);
	}

	/**
	 * Read the children of a tree entry
	 *
	 * @param dn DN of the entry
	 * @param boolean LDAP Size Limit
	 */
	public function readChildren($dn,$nolimit=false) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$server = $this->getServer();
		$dnlower = $this->indexDN($dn);

		if (! isset($this->entries[$dnlower]))
			debug_dump_backtrace('Reading children on an entry that isnt set? '.$dnlower,true);

		$ldap['child_limit'] = $nolimit ? 0 : $_SESSION[APPCONFIG]->getValue('search','size_limit');
		$ldap['filter'] = $_SESSION[APPCONFIG]->getValue('appearance','tree_filter');
		$ldap['deref'] = $_SESSION[APPCONFIG]->getValue('deref','tree');

		# Perform the query to get the children.
		$ldap['children'] = $server->getContainerContents($dn,null,$ldap['child_limit'],$ldap['filter'],$ldap['deref']);

		if (! count($ldap['children'])) {
			$this->entries[$dnlower]->unsetSizeLimited();

			return;
		}

		if (DEBUG_ENABLED)
			debug_log('Children of (%s) are (%s)',64,0,__FILE__,__LINE__,__METHOD__,$dn,$ldap['children']);

		# Relax our execution time, it might take some time to load this
		if ($nolimit)
			@set_time_limit($_SESSION[APPCONFIG]->getValue('search','time_limit'));

		$this->entries[$dnlower]->readingChildren(true);

		foreach ($ldap['children'] as $child) {
			if (DEBUG_ENABLED)
				debug_log('Adding (%s)',64,0,__FILE__,__LINE__,__METHOD__,$child);

			if (! in_array($child,$this->entries[$dnlower]->getChildren()))
				$this->entries[$dnlower]->addChild($child);
		}

		$this->entries[$dnlower]->readingChildren(false);

		if (count($this->entries[$dnlower]->getChildren()) == $ldap['child_limit'])
			$this->entries[$dnlower]->setSizeLimited();
		else
			$this->entries[$dnlower]->unsetSizeLimited();
	}

	/**
	 * Return the number of children an entry has. Optionally autoread the child entry.
	 *
	 * @param dn DN of the entry
	 * @param boolean LDAP Size Limit
	 */
	protected function readChildrenNumber($dn,$nolimit=false) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',33,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$dnlower = $this->indexDN($dn);

		if (! isset($this->entries[$dnlower]))
			debug_dump_backtrace('Reading children on an entry that isnt set?',true);

		# Read the entry if we havent got it yet.
		if (! $this->entries[$dnlower]->isLeaf() && ! $this->entries[$dnlower]->getChildren())
			$this->readChildren($dn,$nolimit);

		return count($this->entries[$dnlower]->getChildren());
	}
}
?>