2
// $Header: /cvsroot/phpldapadmin/phpldapadmin/lib/EntryReader.php,v 1.2.2.3 2008/01/27 14:09:14 wurley Exp $
4
define('ENTRY_READER_CREATION_CONTEXT', '1');
5
define('ENTRY_READER_EDITING_CONTEXT', '2');
8
* @package phpLDAPadmin
9
* @author The phpLDAPadmin development team
10
* @author Xavier Bruyet
12
* Visit an entry and its attributes to initialize their values
14
class EntryReader extends Visitor {
18
public function __construct($ldapserver) {
19
$this->index = $ldapserver->server_id;
23
/**************************/
25
/**************************/
27
public function visitEntryStart($entry) {
29
debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
32
public function visitEntryEnd($entry) {
34
debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
37
/**************************/
38
/* Visit a EditingEntry */
39
/**************************/
41
public function visitDefaultEditingEntryStart($entry) {
43
debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
45
$this->context = ENTRY_READER_EDITING_CONTEXT;
46
$this->visit('Entry::Start', $entry);
49
public function visitTemplateEditingEntryStart($entry) {
51
debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
53
$this->visit('DefaultEditingEntry::Start', $entry);
55
if (isset($_REQUEST['template'])) {
56
$entry->setSelectedTemplateName(trim($_REQUEST['template']));
57
} elseif (($entry->getTemplatesCount() == 1) && !$entry->hasDefaultTemplate()) {
58
$templates = &$entry->getTemplates();
59
$template_names = array_keys($templates);
60
$entry->setSelectedTemplateName($template_names[0]);
64
/**************************/
65
/* Visit a CreatingEntry */
66
/**************************/
68
public function visitDefaultCreatingEntryStart($entry) {
70
debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
72
$this->context = ENTRY_READER_CREATION_CONTEXT;
73
$this->visit('Entry::Start', $entry);
75
if (isset($_POST['new_values']['objectClass'])) {
76
$ocs = $_POST['new_values']['objectClass'];
77
if (is_string($ocs) && (strlen($ocs) > 0)) $ocs = array($ocs);
78
elseif (!$ocs) $ocs = array();
80
foreach ($ocs as $oc) $entry->addObjectClass(trim($oc));
83
if (isset($_REQUEST['container'])) {
84
$entry->setContainer(trim($_REQUEST['container']));
88
public function visitTemplateCreatingEntryStart($entry) {
90
debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
92
$this->visit('DefaultCreatingEntry::Start', $entry);
94
if (isset($_REQUEST['template'])) {
95
$entry->setSelectedTemplateName(trim($_REQUEST['template']));
96
} elseif (($entry->getTemplatesCount() == 1) && !$entry->hasDefaultTemplate()) {
97
$templates = &$entry->getTemplates();
98
$template_names = array_keys($templates);
99
$entry->setSelectedTemplateName($template_names[0]);
103
/**************************/
104
/* Visit an Attribute */
105
/**************************/
107
public function visitAttribute($attribute) {
109
debug_log('Enter with (%s) for attribute (%s)',1,__FILE__,__LINE__,__METHOD__,$attribute,$attribute->getName());
111
$name = $attribute->getName();
113
// @todo editing objectclasses
114
if (($this->context == ENTRY_READER_CREATION_CONTEXT) && ($name == 'objectClass')) return;
116
$old_vals = $this->get('OldValues', $attribute);
117
$new_vals = $this->get('NewValues', $attribute);
119
if (isset($_POST['old_values'][$name])) {
120
$post_old_vals = $_POST['old_values'][$name];
121
if (is_string($post_old_vals) && (strlen($post_old_vals) > 0)) $post_old_vals = array($post_old_vals);
122
elseif (!$post_old_vals) $post_old_vals = array();
124
// delete last empty values
125
for ($i = count($post_old_vals)-1; $i >= 0; $i--) {
126
if (! strlen($post_old_vals[$i])) unset($post_old_vals[$i]);
130
// attribute modified by someone else ?
131
if (count($old_vals) != count($post_old_vals)) {
132
$attribute->justModified();
134
foreach ($post_old_vals as $i => $old_val) {
135
if (!isset($old_vals[$i]) || ($old_vals[$i] != $old_val)) {
136
$attribute->justModified();
143
foreach ($new_vals as $i => $new_val) {
144
// if the attribute has not been already modified by a post of a previous page
145
if (!$attribute->hasBeenModified()) {
146
// if the value has changed (added or modified/deleted)
147
if ((!isset($old_vals[$i]) && (strlen($new_val) > 0)) || (isset($old_vals[$i]) && ($old_vals[$i] != $new_val))) {
148
$new_val = $this->get('PostValue', $attribute, $i, $new_val);
152
if ((!isset($old_vals[$i]) && (strlen($new_val) > 0)) || (isset($old_vals[$i]) && ($old_vals[$i] != $new_val))) {
153
$attribute->justModified();
154
$attribute->addValue($new_val, $i);
158
// old value deletion
159
if (isset($_POST['old_values'][$name]) && !$attribute->isInternal()) {
160
for ($i = count($new_vals); $i < count($old_vals); $i++) {
161
$attribute->addValue('', $i);
165
// modified attributes
166
$modified_attrs = isset($_REQUEST['modified_attrs']) ? $_REQUEST['modified_attrs'] : false;
167
if (is_array($modified_attrs) && in_array($name, $modified_attrs)) {
168
$attribute->justModified();
172
public function getAttributeOldValues($attribute) {
173
$old_vals = $attribute->getValues();
177
public function getAttributeNewValues($attribute) {
178
$name = $attribute->getName();
180
$new_vals = isset($_POST['new_values'][$name]) ? $_POST['new_values'][$name] : null;
181
if (is_string($new_vals) && (strlen($new_vals) > 0)) $new_vals = array($new_vals);
182
elseif (!$new_vals) $new_vals = array();
184
$i = count($new_vals) - 1;
185
$j = $attribute->getValueCount();
186
while (($i >= 0) && ($i >= $j) && !$new_vals[$i]) {
187
if ($i > $j) unset($new_vals[$i]);
194
public function getAttributeRequestValue($attribute, $i, $val, $request) {
195
if ($request == $attribute->getName()) return $val;
198
$entry = $attribute->getEntry();
199
$request_attribute = ($entry ? $entry->getAttribute($request) : null);
201
if ($request_attribute) {
202
$val = $request_attribute->getValue($i);
203
} elseif (isset($_REQUEST[$request][$attribute->getName()][$i])) {
204
$val = $_REQUEST[$request][$attribute->getName()][$i];
208
pla_error(sprintf(_('Your template is missing variable (%s)'), $request));
214
public function getAttributePostValue($attribute, $i, $val) {
215
if (!$attribute->hasProperty('post')) return trim($val);
217
if (preg_match('/^=php\.(\w+)\((.*)\)$/', $attribute->getProperty('post'), $matches)) {
218
switch ($matches[1]) {
220
preg_match_all('/%(\w+)(\|.+)?(\/[lU])?%/U',$matches[2],$matchall);
221
$enc = $this->get('RequestValue', $attribute, $i, $val, $matchall[1][0]);
224
$val = password_hash($password, $enc);
227
case 'SambaPassword' :
228
$matchall = explode(',',$matches[2]);
230
# If we have no password, then dont hash nothing!
231
if (strlen($val) <= 0)
234
$sambapassword = new smbHash;
236
switch ($matchall[0]) {
237
case 'LM' : $val = $sambapassword->lmhash($val); break;
238
case 'NT' : $val = $sambapassword->nthash($val); break;
243
preg_match_all('/%(\w+)(\|.+)?(\/[lU])?%/U',$matches[2],$matchall);
244
$matchattrs = explode(',',$matches[2]);
245
$char = $matchattrs[0];
248
foreach ($matchall[1] as $joinattr) {
249
$values[] = $this->get('RequestValue', $attribute, $i, $val, $joinattr);
252
$val = implode($char, $values);
255
if (function_exists($matches[1])) {
256
$val = call_user_func($matches[1], $matches[2], $attribute, $i, $val);
258
pla_error(sprintf(_('Your template has an unknown post function (%s).'), $matches[1]));
266
/*******************************/
267
/* Visit a BinaryAttribute */
268
/*******************************/
270
public function getBinaryAttributeOldValues($attribute) {
276
* If there is binary post data, save them in
277
* $_SESSION['submitform'][$attribute_name][$key][$file_name][$file_path]
278
* with key = md5("$file_name|$file_path")
280
* return binary values
282
public function getBinaryAttributeNewValues($attribute) {
283
$name = $attribute->getName();
284
$new_vals = $this->get('Attribute::NewValues', $attribute);
288
foreach ($new_vals as $new_val) {
289
if (isset($_SESSION['submitform'][$name][$new_val])) {
291
foreach ($_SESSION['submitform'][$name][$new_val] as $filename => $file) {
292
$attribute->addFileName($filename, $i);
293
foreach ($file as $filepath => $binaries) {
294
$attribute->addFilePath($filepath, $i);
303
$new_files = isset($_FILES['new_values']['name'][$name]) ? $_FILES['new_values']['name'][$name] : null;
304
if (!$new_files) $new_files = array();
305
elseif (!is_array($new_files)) $new_files = array($new_files);
307
foreach ($new_files as $j => $file_name) {
308
$file_path = $_FILES['new_values']['tmp_name'][$name][$j];
309
if (is_uploaded_file($file_path)) {
310
$f = fopen($file_path, 'r');
311
$binary_data = fread($f, filesize($file_path));
314
$attribute->addFileName($file_name, $i);
315
$attribute->addFilePath($file_path, $i);
317
$key = md5("$file_name|$file_path");
318
$_SESSION['submitform'][$name][$key][$file_name][$file_path] = $binary_data;
319
$vals[] = $binary_data;
327
public function getBinaryAttributePostValue($attribute, $i, $val) {
331
/*********************************/
332
/* Visit a PasswordAttribute */
333
/*********************************/
335
public function getPasswordAttributePostValue($attribute, $i, $val) {
336
$name = $attribute->getName();
338
if ($attribute->hasProperty('verify') && $attribute->getProperty('verify')) {
339
$verif_val = isset($_POST['new_values_verify'][$name][$i]) ? $_POST['new_values_verify'][$name][$i] : null;
340
if (!$verif_val || ($verif_val != $val)) {
341
system_message(array(
342
'title'=>_('Checking passwords'),
343
'body'=>_('You have specified two different passwords'),
345
return $attribute->getValue($i);
349
if ($attribute->hasProperty('post')) {
350
$val = $this->get('Attribute::PostValue', $attribute, $i, $val);
352
} elseif (strlen($val) > 0) {
353
if (isset($_REQUEST['enc'][$attribute->getName()][$i]))
354
$enc = $_REQUEST['enc'][$attribute->getName()][$i];
356
$enc = get_default_hash($this->index);
358
$val = password_hash($val, $enc);
363
public function getSambaPasswordAttributePostValue($attribute, $i, $val) {
364
$name = $attribute->getName();
366
if ($attribute->hasProperty('verify') && $attribute->getProperty('verify')) {
367
$verif_val = isset($_POST['new_values_verify'][$name][$i]) ? $_POST['new_values_verify'][$name][$i] : null;
368
if (!$verif_val || ($verif_val != $val)) {
369
system_message(array(
370
'title'=>_('Checking passwords'),
371
'body'=>_('You have specified two different passwords'),
373
return $attribute->getValue($i);
377
if ($attribute->hasProperty('post')) {
378
$val = $this->get('Attribute::PostValue', $attribute, $i, $val);
379
} elseif (strlen($val) > 0) {
380
$sambapassword = new smbHash;
382
if ($name == 'sambaLMPassword')
383
$val = $sambapassword->lmhash($val);
384
elseif ($name == 'sambaNTPassword')
385
$val = $sambapassword->nthash($val);