File: Entry.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 (307 lines) | stat: -rw-r--r-- 7,375 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
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
<?php
// $Header: /cvsroot/phpldapadmin/phpldapadmin/lib/Entry.php,v 1.2.2.3 2008/01/27 07:23:43 wurley Exp $

/**
 * @package phpLDAPadmin
 * @author The phpLDAPadmin development team
 * @author Xavier Bruyet
 *
 * Represent a tree node
 */
abstract class Entry {
	protected $dn;

	# the server_id to which the entry belongs
	protected $server_id;

	# is the entry a leaf ?
	private $leaf;

	# is the node open ?
	private $open;

	# array of dn
	private $children;

	# allow to test if addChild() is called by readChildren()
	private $reading_children;

	# is the size of children limited ?
	private $size_limited;

	# is the entry modifiable ?
	private $readonly;

	# an icon file path
	protected $icon;

	protected $properties;

	public function __construct($dn) {
		$this->dn = $dn;
		$this->leaf = false;
		$this->open = false;
		$this->children = array();
		$this->reading_children = false;
		$this->size_limited = true;
		$this->readonly = false;
		$this->icon = '';
		$this->properties = array();
	}

	public function getDn() {
		return $this->dn;
	}

	public function getRdn() {
		return get_rdn($this->getDn(), 0, true);
	}

	public function getRdnAttributeName() {
		$attr = '';
		if ($this->dn) {
			$i = strpos($this->dn, '=');
			if ($i !== false) $attr = substr($this->dn, 0, $i);
		}
		return $attr;
	}

	public function setTree($index) {
		$this->server_id = $index;
	}

	private function readChildren($nolimit=false) {
		if (DEBUG_ENABLED)
			debug_log('Entered with ()',1,__FILE__,__LINE__,__METHOD__);

		$ldapserver = (isset($this->server_id) ? $_SESSION[APPCONFIG]->ldapservers->Instance($this->server_id) : null);
		if (DEBUG_ENABLED)
			debug_log('LdapServer (%s)',1,__FILE__,__LINE__,__METHOD__, $ldapserver ? $ldapserver->server_id : -1);

		$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','view');
		$ldap['children'] = $ldapserver->getContainerContents($this->getDn(),$ldap['child_limit'],$ldap['filter'],$ldap['deref']);

		if (DEBUG_ENABLED)
			debug_log('Children of (%s) are (%s)',64,__FILE__,__LINE__,__METHOD__,$this->getDn(),$ldap['children']);

		if (isset($this->server_id)) {
			$this->reading_children = true;
			$tree = get_cached_item($ldapserver->server_id,'tree');

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

				if (! $tree->getEntry($dn))
					$tree->addEntry($dn);
			}
			set_cached_item($ldapserver->server_id,'tree','null',$tree);
			usort($this->children,'pla_compare_dns');
			$this->reading_children = false;
		}

		if (count($this->children) == $ldap['child_limit'])
			$this->size_limited = true;
		else
			$this->size_limited = false;

		if (DEBUG_ENABLED)
			debug_log('Entered with (), Returning ()',1,__FILE__,__LINE__,__METHOD__);
	}

	/**
	 * Returns null if the children have never be defined
	 * or an array of the dn of the children
	 */
	public function getChildren() {
		if (! $this->children)
			$this->readChildren();

		return $this->children;
	}

	public function getChildrenNumber() {
		if (! $this->children)
			$this->readChildren();

		if ($this->children)
			return count($this->children);
		else
			return 0;
	}

	/**
	 * Called by Tree::addEntry() only
	 */
	public function addChild($dn) {
		if (DEBUG_ENABLED)
			debug_log('Entered with (%s)',1,__FILE__,__LINE__,__METHOD__,$dn);

		if (! $this->children) {
			if (DEBUG_ENABLED)
				debug_log('this->children is FALSE',64,__FILE__,__LINE__,__METHOD__);

			if (! $this->reading_children) {
				if (DEBUG_ENABLED)
					debug_log('this->reading_children is FALSE',64,__FILE__,__LINE__,__METHOD__,$dn);

				$this->readChildren();
			}else {
				$this->children = array();
			}
		}

		$index = array_search($dn,$this->children);
		if (DEBUG_ENABLED)
			debug_log('array_search of (%s) in (%s) returned (%s)',64,__FILE__,__LINE__,__METHOD__,$dn,$this->children,$index);

		if ($index === false) {
			$this->children[] = $dn;
			if (! $this->reading_children) usort($this->children,'pla_compare_dns');
		}

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

	/**
	 * Called by Tree::delEntry() only
	 */
	public function delChild($dn) {
		if (DEBUG_ENABLED)
			debug_log('Entered with (%s)',1,__FILE__,__LINE__,__METHOD__,$dn);

		if ($this->children) {
			# If the parent hasnt been opened in the tree, then there wont be any children.
			$index = array_search($dn,$this->children);
			if ($index !== false) unset($this->children[$index]);
		}
	}

	public function rename($newDn) {
		if (DEBUG_ENABLED)
			debug_log('Entered with (%s)',1,__FILE__,__LINE__,__METHOD__,$newDn);
		$this->dn = $newDn;
	}

	public function isOpened() {
		return $this->open;
	}

	public function close() {
		if (DEBUG_ENABLED)
			debug_log('Entered with ()',1,__FILE__,__LINE__,__METHOD__);

		$this->open = false;
	}

	/**
	 * Opens the node ; the children of the node must have been defined
	 */
	public function open() {
		if (DEBUG_ENABLED)
			debug_log('Entered with ()',1,__FILE__,__LINE__,__METHOD__);

		$this->open = true;

		if ($this->isSizeLimited()) {
			$this->readChildren(true);
		}
	}

	public function setLeaf($is_leaf) {
		$this->leaf = $is_leaf;
	}

	public function isLeaf() {
		return $this->leaf;
	}

	public function isReadOnly() {
		return $this->readonly;
	}

	public function setReadOnly() {
		$this->readonly = true;
	}

	public function setReadWrite() {
		$this->readonly = false;
	}

	/**
	 * Returns the path of the icon file used to represent this node ;
	 * returns the result of get_icon() function
	 */
	public function getIcon($ldapserver) {
		if ($this->icon) return $this->icon;
		else return get_icon($ldapserver,$this->dn);
	}

	public function isSizeLimited() {
		return $this->size_limited;
	}

	public function setProperty($name, $value) {
		$this->properties[$name] = $value;
	}

	public function delProperty($name) {
		if ($this->hasProperty($name)) unset($this->properties[$name]);
	}

	public function hasProperty($name) {
		return isset($this->properties[$name]);
	}

	public function getProperty($name) {
		if ($this->hasProperty($name)) return $this->properties[$name];
		else return null;
	}

	public function getTemplateName() {
		if (isset($this->selected_template))
			return $this->selected_template;
		else
			return '';
	}

	public function getTemplateTitle() {
		if (isset($this->selected_template['title']))
			return $this->templates[$this->selected_template]['title'];
		else
			return _('No Template');
	}

	/**
	 * Visit the entry and its attributes
	 *
	 * The visitor must implement these methods :
	 * - visit<Entry>Start($entry)
	 * - visit<Entry>End($entry)
	 * where <Entry> is the entry class name.
	 */
	public function accept($visitor) {
		$visitor->visit('Start', $this);
		$attrs = $this->getAttributes();
		foreach ($attrs as $attribute) {
			$attribute->accept($visitor);
		}
		$visitor->visit('End', $this);
	}

	public function getAttribute($name) {
		foreach ($this->getAttributes() as $attr) {
			if ($attr->getName() == $name) return $attr;
		}
		return null;
	}

	/**
	 * Return an array of Attribute objects
	 */
	abstract public function getAttributes();
}
?>