~ubuntu-branches/debian/squeeze/phpldapadmin/squeeze

« back to all changes in this revision

Viewing changes to lib/EntryReader.php

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella
  • Date: 2008-06-23 07:14:57 UTC
  • mfrom: (1.2.1 upstream) (3.1.9 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080623071457-m0yh4msp3n61crwv
Tags: 1.1.0.5-3
debian/conf/apache.conf: use php5 instead of php4. (Closes: #487617)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Header: /cvsroot/phpldapadmin/phpldapadmin/lib/EntryReader.php,v 1.2.2.3 2008/01/27 14:09:14 wurley Exp $
 
3
 
 
4
define('ENTRY_READER_CREATION_CONTEXT', '1');
 
5
define('ENTRY_READER_EDITING_CONTEXT', '2');
 
6
 
 
7
/**
 
8
 * @package phpLDAPadmin
 
9
 * @author The phpLDAPadmin development team
 
10
 * @author Xavier Bruyet
 
11
 *
 
12
 * Visit an entry and its attributes to initialize their values
 
13
 */
 
14
class EntryReader extends Visitor {
 
15
        protected $index;
 
16
        protected $context;
 
17
 
 
18
        public function __construct($ldapserver) {
 
19
                $this->index = $ldapserver->server_id;
 
20
                $this->context = 0;
 
21
        }
 
22
 
 
23
        /**************************/
 
24
        /* Visit an Entry         */
 
25
        /**************************/
 
26
 
 
27
        public function visitEntryStart($entry) {
 
28
                if (DEBUG_ENABLED)
 
29
                        debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
 
30
        }
 
31
 
 
32
        public function visitEntryEnd($entry) {
 
33
                if (DEBUG_ENABLED)
 
34
                        debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
 
35
        }
 
36
 
 
37
        /**************************/
 
38
        /* Visit a EditingEntry   */
 
39
        /**************************/
 
40
 
 
41
        public function visitDefaultEditingEntryStart($entry) {
 
42
                if (DEBUG_ENABLED)
 
43
                        debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
 
44
 
 
45
                $this->context = ENTRY_READER_EDITING_CONTEXT;
 
46
                $this->visit('Entry::Start', $entry);
 
47
        }
 
48
 
 
49
        public function visitTemplateEditingEntryStart($entry) {
 
50
                if (DEBUG_ENABLED)
 
51
                        debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
 
52
 
 
53
                $this->visit('DefaultEditingEntry::Start', $entry);
 
54
 
 
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]);
 
61
                }
 
62
        }
 
63
 
 
64
        /**************************/
 
65
        /* Visit a CreatingEntry  */
 
66
        /**************************/
 
67
 
 
68
        public function visitDefaultCreatingEntryStart($entry) {
 
69
                if (DEBUG_ENABLED)
 
70
                        debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
 
71
 
 
72
                $this->context = ENTRY_READER_CREATION_CONTEXT;
 
73
                $this->visit('Entry::Start', $entry);
 
74
 
 
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();
 
79
 
 
80
                        foreach ($ocs as $oc) $entry->addObjectClass(trim($oc));
 
81
                }
 
82
 
 
83
                if (isset($_REQUEST['container'])) {
 
84
                        $entry->setContainer(trim($_REQUEST['container']));
 
85
                }
 
86
        }
 
87
 
 
88
        public function visitTemplateCreatingEntryStart($entry) {
 
89
                if (DEBUG_ENABLED)
 
90
                        debug_log('Enter with (%s) for dn (%s)',1,__FILE__,__LINE__,__METHOD__,$entry,$entry->getDn());
 
91
 
 
92
                $this->visit('DefaultCreatingEntry::Start', $entry);
 
93
 
 
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]);
 
100
                }
 
101
        }
 
102
 
 
103
        /**************************/
 
104
        /* Visit an Attribute     */
 
105
        /**************************/
 
106
 
 
107
        public function visitAttribute($attribute) {
 
108
                if (DEBUG_ENABLED)
 
109
                        debug_log('Enter with (%s) for attribute (%s)',1,__FILE__,__LINE__,__METHOD__,$attribute,$attribute->getName());
 
110
 
 
111
                $name = $attribute->getName();
 
112
 
 
113
                // @todo editing objectclasses
 
114
                if (($this->context == ENTRY_READER_CREATION_CONTEXT) && ($name == 'objectClass')) return;
 
115
 
 
116
                $old_vals = $this->get('OldValues', $attribute);
 
117
                $new_vals = $this->get('NewValues', $attribute);
 
118
 
 
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();
 
123
 
 
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]);
 
127
                                else break;
 
128
                        }
 
129
 
 
130
                        // attribute modified by someone else ?
 
131
                        if (count($old_vals) != count($post_old_vals)) {
 
132
                                $attribute->justModified();
 
133
                        } else {
 
134
                                foreach ($post_old_vals as $i => $old_val) {
 
135
                                        if (!isset($old_vals[$i]) || ($old_vals[$i] != $old_val)) {
 
136
                                                $attribute->justModified();
 
137
                                                break;
 
138
                                        }
 
139
                                }
 
140
                        }
 
141
                }
 
142
 
 
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);
 
149
                                }
 
150
                        }
 
151
 
 
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);
 
155
                        }
 
156
                }
 
157
 
 
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);
 
162
                        }
 
163
                }
 
164
 
 
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();
 
169
                }
 
170
        }
 
171
 
 
172
        public function getAttributeOldValues($attribute) {
 
173
                $old_vals = $attribute->getValues();
 
174
                return $old_vals;
 
175
        }
 
176
 
 
177
        public function getAttributeNewValues($attribute) {
 
178
                $name = $attribute->getName();
 
179
 
 
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();
 
183
 
 
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]);
 
188
                        $i--;
 
189
                }
 
190
 
 
191
                return $new_vals;
 
192
        }
 
193
 
 
194
        public function getAttributeRequestValue($attribute, $i, $val, $request) {
 
195
                if ($request == $attribute->getName()) return $val;
 
196
 
 
197
                $val = null;
 
198
                $entry = $attribute->getEntry();
 
199
                $request_attribute = ($entry ? $entry->getAttribute($request) : null);
 
200
 
 
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];
 
205
                }
 
206
 
 
207
                if (is_null($val)) {
 
208
                        pla_error(sprintf(_('Your template is missing variable (%s)'), $request));
 
209
                }
 
210
 
 
211
                return $val;
 
212
        }
 
213
 
 
214
        public function getAttributePostValue($attribute, $i, $val) {
 
215
                if (!$attribute->hasProperty('post')) return trim($val);
 
216
 
 
217
                if (preg_match('/^=php\.(\w+)\((.*)\)$/', $attribute->getProperty('post'), $matches)) {
 
218
                        switch ($matches[1]) {
 
219
                                case 'Password' :
 
220
                                        preg_match_all('/%(\w+)(\|.+)?(\/[lU])?%/U',$matches[2],$matchall);
 
221
                                        $enc = $this->get('RequestValue', $attribute, $i, $val, $matchall[1][0]);
 
222
                                        $password = $val;
 
223
                                        if ($password) {
 
224
                                                $val = password_hash($password, $enc);
 
225
                                        }
 
226
                                        break;
 
227
                                case 'SambaPassword' :
 
228
                                        $matchall = explode(',',$matches[2]);
 
229
 
 
230
                                        # If we have no password, then dont hash nothing!
 
231
                                        if (strlen($val) <= 0)
 
232
                                                break;
 
233
 
 
234
                                        $sambapassword = new smbHash;
 
235
 
 
236
                                        switch ($matchall[0]) {
 
237
                                                case 'LM' : $val = $sambapassword->lmhash($val); break;
 
238
                                                case 'NT' : $val = $sambapassword->nthash($val); break;
 
239
                                                default : $val = '';
 
240
                                        }
 
241
                                        break;
 
242
                                case 'Join' :
 
243
                                        preg_match_all('/%(\w+)(\|.+)?(\/[lU])?%/U',$matches[2],$matchall);
 
244
                                        $matchattrs = explode(',',$matches[2]);
 
245
                                        $char = $matchattrs[0];
 
246
 
 
247
                                        $values = array();
 
248
                                        foreach ($matchall[1] as $joinattr) {
 
249
                                                $values[] = $this->get('RequestValue', $attribute, $i, $val, $joinattr);
 
250
                                        }
 
251
 
 
252
                                        $val = implode($char, $values);
 
253
                                        break;
 
254
                                default :
 
255
                                        if (function_exists($matches[1])) {
 
256
                                                $val = call_user_func($matches[1], $matches[2], $attribute, $i, $val);
 
257
                                        } else {
 
258
                                                pla_error(sprintf(_('Your template has an unknown post function (%s).'), $matches[1]));
 
259
                                        }
 
260
                        }
 
261
                }
 
262
 
 
263
                return $val;
 
264
        }
 
265
 
 
266
        /*******************************/
 
267
        /* Visit a BinaryAttribute     */
 
268
        /*******************************/
 
269
 
 
270
        public function getBinaryAttributeOldValues($attribute) {
 
271
                $old_vals = array();
 
272
                return $old_vals;
 
273
        }
 
274
 
 
275
        /**
 
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")
 
279
         *
 
280
         * return binary values
 
281
         */
 
282
        public function getBinaryAttributeNewValues($attribute) {
 
283
                $name = $attribute->getName();
 
284
                $new_vals = $this->get('Attribute::NewValues', $attribute);
 
285
 
 
286
                $i = 0;
 
287
                $vals = array();
 
288
                foreach ($new_vals as $new_val) {
 
289
                        if (isset($_SESSION['submitform'][$name][$new_val])) {
 
290
                                $bin = '';
 
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);
 
295
                                                $bin = $binaries;
 
296
                                        }
 
297
                                }
 
298
                                $vals[] = $bin;
 
299
                                $i++;
 
300
                        }
 
301
                }
 
302
 
 
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);
 
306
 
 
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));
 
312
                                fclose($f);
 
313
 
 
314
                                $attribute->addFileName($file_name, $i);
 
315
                                $attribute->addFilePath($file_path, $i);
 
316
 
 
317
                                $key = md5("$file_name|$file_path");
 
318
                                $_SESSION['submitform'][$name][$key][$file_name][$file_path] = $binary_data;
 
319
                                $vals[] = $binary_data;
 
320
                                $i++;
 
321
                        }
 
322
                }
 
323
 
 
324
                return $vals;
 
325
        }
 
326
 
 
327
        public function getBinaryAttributePostValue($attribute, $i, $val) {
 
328
                return $val;
 
329
        }
 
330
 
 
331
        /*********************************/
 
332
        /* Visit a PasswordAttribute     */
 
333
        /*********************************/
 
334
 
 
335
        public function getPasswordAttributePostValue($attribute, $i, $val) {
 
336
                $name = $attribute->getName();
 
337
 
 
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'),
 
344
                                        'type'=>'error'));
 
345
                                return $attribute->getValue($i);
 
346
                        }
 
347
                }
 
348
 
 
349
                if ($attribute->hasProperty('post')) {
 
350
                        $val = $this->get('Attribute::PostValue', $attribute, $i, $val);
 
351
 
 
352
                } elseif (strlen($val) > 0) {
 
353
                        if (isset($_REQUEST['enc'][$attribute->getName()][$i]))
 
354
                                $enc = $_REQUEST['enc'][$attribute->getName()][$i];
 
355
                        else
 
356
                                $enc = get_default_hash($this->index);
 
357
 
 
358
                        $val = password_hash($val, $enc);
 
359
                }
 
360
                return $val;
 
361
        }
 
362
 
 
363
        public function getSambaPasswordAttributePostValue($attribute, $i, $val) {
 
364
                $name = $attribute->getName();
 
365
 
 
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'),
 
372
                                        'type'=>'error'));
 
373
                                return $attribute->getValue($i);
 
374
                        }
 
375
                }
 
376
 
 
377
                if ($attribute->hasProperty('post')) {
 
378
                        $val = $this->get('Attribute::PostValue', $attribute, $i, $val);
 
379
                } elseif (strlen($val) > 0) {
 
380
                        $sambapassword = new smbHash;
 
381
 
 
382
                        if ($name == 'sambaLMPassword')
 
383
                                $val = $sambapassword->lmhash($val);
 
384
                        elseif ($name == 'sambaNTPassword')
 
385
                                $val = $sambapassword->nthash($val);
 
386
                }
 
387
                return $val;
 
388
        }
 
389
}
 
390
?>