File: EntryReader.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 (390 lines) | stat: -rw-r--r-- 12,092 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
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
<?php
// $Header: /cvsroot/phpldapadmin/phpldapadmin/lib/EntryReader.php,v 1.2.2.3 2008/01/27 14:09:14 wurley Exp $

define('ENTRY_READER_CREATION_CONTEXT', '1');
define('ENTRY_READER_EDITING_CONTEXT', '2');

/**
 * @package phpLDAPadmin
 * @author The phpLDAPadmin development team
 * @author Xavier Bruyet
 *
 * Visit an entry and its attributes to initialize their values
 */
class EntryReader extends Visitor {
	protected $index;
	protected $context;

	public function __construct($ldapserver) {
		$this->index = $ldapserver->server_id;
		$this->context = 0;
	}

	/**************************/
	/* Visit an Entry         */
	/**************************/

	public function visitEntryStart($entry) {
		if (DEBUG_ENABLED)
			debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
	}

	public function visitEntryEnd($entry) {
		if (DEBUG_ENABLED)
			debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
	}

	/**************************/
	/* Visit a EditingEntry   */
	/**************************/

	public function visitDefaultEditingEntryStart($entry) {
		if (DEBUG_ENABLED)
			debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());

		$this->context = ENTRY_READER_EDITING_CONTEXT;
		$this->visit('Entry::Start', $entry);
	}

	public function visitTemplateEditingEntryStart($entry) {
		if (DEBUG_ENABLED)
			debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());

		$this->visit('DefaultEditingEntry::Start', $entry);

		if (isset($_REQUEST['template'])) {
			$entry->setSelectedTemplateName(trim($_REQUEST['template']));
		} elseif (($entry->getTemplatesCount() == 1) && !$entry->hasDefaultTemplate()) {
			$templates = &$entry->getTemplates();
			$template_names = array_keys($templates);
			$entry->setSelectedTemplateName($template_names[0]);
		}
	}

	/**************************/
	/* Visit a CreatingEntry  */
	/**************************/

	public function visitDefaultCreatingEntryStart($entry) {
		if (DEBUG_ENABLED)
			debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());

		$this->context = ENTRY_READER_CREATION_CONTEXT;
		$this->visit('Entry::Start', $entry);

		if (isset($_POST['new_values']['objectClass'])) {
			$ocs = $_POST['new_values']['objectClass'];
			if (is_string($ocs) && (strlen($ocs) > 0)) $ocs = array($ocs);
			elseif (!$ocs) $ocs = array();

			foreach ($ocs as $oc) $entry->addObjectClass(trim($oc));
		}

		if (isset($_REQUEST['container'])) {
			$entry->setContainer(trim($_REQUEST['container']));
		}
	}

	public function visitTemplateCreatingEntryStart($entry) {
		if (DEBUG_ENABLED)
			debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());

		$this->visit('DefaultCreatingEntry::Start', $entry);

		if (isset($_REQUEST['template'])) {
			$entry->setSelectedTemplateName(trim($_REQUEST['template']));
		} elseif (($entry->getTemplatesCount() == 1) && !$entry->hasDefaultTemplate()) {
			$templates = &$entry->getTemplates();
			$template_names = array_keys($templates);
			$entry->setSelectedTemplateName($template_names[0]);
		}
	}

	/**************************/
	/* Visit an Attribute     */
	/**************************/

	public function visitAttribute($attribute) {
		if (DEBUG_ENABLED)
			debug_log('Enter with (%s) for attribute (%s)',1,__FILE__,__LINE__,__METHOD__,$attribute,$attribute->getName());

		$name = $attribute->getName();

		// @todo editing objectclasses
		if (($this->context == ENTRY_READER_CREATION_CONTEXT) && ($name == 'objectClass')) return;

		$old_vals = $this->get('OldValues', $attribute);
		$new_vals = $this->get('NewValues', $attribute);

		if (isset($_POST['old_values'][$name])) {
			$post_old_vals = $_POST['old_values'][$name];
			if (is_string($post_old_vals) && (strlen($post_old_vals) > 0)) $post_old_vals = array($post_old_vals);
			elseif (!$post_old_vals) $post_old_vals = array();

			// delete last empty values
			for ($i = count($post_old_vals)-1; $i >= 0; $i--) {
				if (! strlen($post_old_vals[$i])) unset($post_old_vals[$i]);
				else break;
			}

			// attribute modified by someone else ?
			if (count($old_vals) != count($post_old_vals)) {
				$attribute->justModified();
			} else {
				foreach ($post_old_vals as $i => $old_val) {
					if (!isset($old_vals[$i]) || ($old_vals[$i] != $old_val)) {
						$attribute->justModified();
						break;
					}
				}
			}
		}

		foreach ($new_vals as $i => $new_val) {
			// if the attribute has not been already modified by a post of a previous page
			if (!$attribute->hasBeenModified()) {
				// if the value has changed (added or modified/deleted)
				if ((!isset($old_vals[$i]) && (strlen($new_val) > 0)) || (isset($old_vals[$i]) && ($old_vals[$i] != $new_val))) {
					$new_val = $this->get('PostValue', $attribute, $i, $new_val);
				}
			}

			if ((!isset($old_vals[$i]) && (strlen($new_val) > 0)) || (isset($old_vals[$i]) && ($old_vals[$i] != $new_val))) {
				$attribute->justModified();
				$attribute->addValue($new_val, $i);
			}
		}

		// old value deletion
		if (isset($_POST['old_values'][$name]) && !$attribute->isInternal()) {
			for ($i = count($new_vals); $i < count($old_vals); $i++) {
				$attribute->addValue('', $i);
			}
		}

		// modified attributes
		$modified_attrs = isset($_REQUEST['modified_attrs']) ? $_REQUEST['modified_attrs'] : false;
		if (is_array($modified_attrs) && in_array($name, $modified_attrs)) {
			$attribute->justModified();
		}
	}

	public function getAttributeOldValues($attribute) {
		$old_vals = $attribute->getValues();
		return $old_vals;
	}

	public function getAttributeNewValues($attribute) {
		$name = $attribute->getName();

		$new_vals = isset($_POST['new_values'][$name]) ? $_POST['new_values'][$name] : null;
		if (is_string($new_vals) && (strlen($new_vals) > 0)) $new_vals = array($new_vals);
		elseif (!$new_vals) $new_vals = array();

		$i = count($new_vals) - 1;
		$j = $attribute->getValueCount();
		while (($i >= 0) && ($i >= $j) && !$new_vals[$i]) {
			if ($i > $j) unset($new_vals[$i]);
			$i--;
		}

		return $new_vals;
	}

	public function getAttributeRequestValue($attribute, $i, $val, $request) {
		if ($request == $attribute->getName()) return $val;

		$val = null;
		$entry = $attribute->getEntry();
		$request_attribute = ($entry ? $entry->getAttribute($request) : null);

		if ($request_attribute) {
			$val = $request_attribute->getValue($i);
		} elseif (isset($_REQUEST[$request][$attribute->getName()][$i])) {
			$val = $_REQUEST[$request][$attribute->getName()][$i];
		}

		if (is_null($val)) {
			pla_error(sprintf(_('Your template is missing variable (%s)'), $request));
		}

		return $val;
	}

	public function getAttributePostValue($attribute, $i, $val) {
		if (!$attribute->hasProperty('post')) return trim($val);

		if (preg_match('/^=php\.(\w+)\((.*)\)$/', $attribute->getProperty('post'), $matches)) {
			switch ($matches[1]) {
				case 'Password' :
					preg_match_all('/%(\w+)(\|.+)?(\/[lU])?%/U',$matches[2],$matchall);
					$enc = $this->get('RequestValue', $attribute, $i, $val, $matchall[1][0]);
					$password = $val;
					if ($password) {
						$val = password_hash($password, $enc);
					}
					break;
				case 'SambaPassword' :
					$matchall = explode(',',$matches[2]);

					# If we have no password, then dont hash nothing!
					if (strlen($val) <= 0)
						break;

					$sambapassword = new smbHash;

					switch ($matchall[0]) {
						case 'LM' : $val = $sambapassword->lmhash($val); break;
						case 'NT' : $val = $sambapassword->nthash($val); break;
						default : $val = '';
					}
					break;
				case 'Join' :
					preg_match_all('/%(\w+)(\|.+)?(\/[lU])?%/U',$matches[2],$matchall);
					$matchattrs = explode(',',$matches[2]);
					$char = $matchattrs[0];

					$values = array();
					foreach ($matchall[1] as $joinattr) {
						$values[] = $this->get('RequestValue', $attribute, $i, $val, $joinattr);
					}

					$val = implode($char, $values);
					break;
				default :
					if (function_exists($matches[1])) {
						$val = call_user_func($matches[1], $matches[2], $attribute, $i, $val);
					} else {
						pla_error(sprintf(_('Your template has an unknown post function (%s).'), $matches[1]));
					}
			}
		}

		return $val;
	}

	/*******************************/
	/* Visit a BinaryAttribute     */
	/*******************************/

	public function getBinaryAttributeOldValues($attribute) {
		$old_vals = array();
		return $old_vals;
	}

	/**
	 * If there is binary post data, save them in
	 * $_SESSION['submitform'][$attribute_name][$key][$file_name][$file_path]
	 * with key = md5("$file_name|$file_path")
	 *
	 * return binary values
	 */
	public function getBinaryAttributeNewValues($attribute) {
		$name = $attribute->getName();
		$new_vals = $this->get('Attribute::NewValues', $attribute);

		$i = 0;
		$vals = array();
		foreach ($new_vals as $new_val) {
			if (isset($_SESSION['submitform'][$name][$new_val])) {
				$bin = '';
				foreach ($_SESSION['submitform'][$name][$new_val] as $filename => $file) {
					$attribute->addFileName($filename, $i);
					foreach ($file as $filepath => $binaries) {
						$attribute->addFilePath($filepath, $i);
						$bin = $binaries;
					}
				}
				$vals[] = $bin;
				$i++;
			}
		}

		$new_files = isset($_FILES['new_values']['name'][$name]) ? $_FILES['new_values']['name'][$name] : null;
		if (!$new_files) $new_files = array();
		elseif (!is_array($new_files)) $new_files = array($new_files);

		foreach ($new_files as $j => $file_name) {
			$file_path = $_FILES['new_values']['tmp_name'][$name][$j];
			if (is_uploaded_file($file_path)) {
				$f = fopen($file_path, 'r');
				$binary_data = fread($f, filesize($file_path));
				fclose($f);

				$attribute->addFileName($file_name, $i);
				$attribute->addFilePath($file_path, $i);

				$key = md5("$file_name|$file_path");
				$_SESSION['submitform'][$name][$key][$file_name][$file_path] = $binary_data;
				$vals[] = $binary_data;
				$i++;
			}
		}

		return $vals;
	}

	public function getBinaryAttributePostValue($attribute, $i, $val) {
		return $val;
	}

	/*********************************/
	/* Visit a PasswordAttribute     */
	/*********************************/

	public function getPasswordAttributePostValue($attribute, $i, $val) {
		$name = $attribute->getName();

		if ($attribute->hasProperty('verify') && $attribute->getProperty('verify')) {
			$verif_val = isset($_POST['new_values_verify'][$name][$i]) ? $_POST['new_values_verify'][$name][$i] : null;
			if (!$verif_val || ($verif_val != $val)) {
				system_message(array(
					'title'=>_('Checking passwords'),
					'body'=>_('You have specified two different passwords'),
					'type'=>'error'));
				return $attribute->getValue($i);
			}
		}

		if ($attribute->hasProperty('post')) {
			$val = $this->get('Attribute::PostValue', $attribute, $i, $val);

		} elseif (strlen($val) > 0) {
			if (isset($_REQUEST['enc'][$attribute->getName()][$i]))
				$enc = $_REQUEST['enc'][$attribute->getName()][$i];
			else
				$enc = get_default_hash($this->index);

			$val = password_hash($val, $enc);
		}
		return $val;
	}

	public function getSambaPasswordAttributePostValue($attribute, $i, $val) {
		$name = $attribute->getName();

		if ($attribute->hasProperty('verify') && $attribute->getProperty('verify')) {
			$verif_val = isset($_POST['new_values_verify'][$name][$i]) ? $_POST['new_values_verify'][$name][$i] : null;
			if (!$verif_val || ($verif_val != $val)) {
				system_message(array(
					'title'=>_('Checking passwords'),
					'body'=>_('You have specified two different passwords'),
					'type'=>'error'));
				return $attribute->getValue($i);
			}
		}

		if ($attribute->hasProperty('post')) {
			$val = $this->get('Attribute::PostValue', $attribute, $i, $val);
		} elseif (strlen($val) > 0) {
			$sambapassword = new smbHash;

			if ($name == 'sambaLMPassword')
				$val = $sambapassword->lmhash($val);
			elseif ($name == 'sambaNTPassword')
				$val = $sambapassword->nthash($val);
		}
		return $val;
	}
}
?>