File: ldif.php

package info (click to toggle)
turba2 2.3.4%2Bdebian0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,844 kB
  • ctags: 2,910
  • sloc: php: 12,341; xml: 1,851; sql: 520; makefile: 101; sh: 27; perl: 17
file content (310 lines) | stat: -rw-r--r-- 12,629 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
<?php
/**
 * Horde_Data implementation for LDAP Data Interchange Format (LDIF).
 *
 * $Horde: turba/lib/Data/ldif.php,v 1.2.2.5 2009/09/18 14:23:22 jan Exp $
 *
 * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Rita Selsky <ritaselsky@gmail.com>
 * @since   Horde 3.2
 * @package Horde_Data
 */
class Horde_Data_ldif extends Horde_Data {

    var $_extension = 'ldif';

    var $_contentType = 'text/ldif';

    /**
     * Useful Mozilla address book attribute names.
     *
     * @private
     * @var array
     */
    var $_mozillaAttr = array('cn', 'givenName', 'sn', 'mail', 'mozillaNickname',
                             'homeStreet', 'mozillaHomeStreet2', 'mozillaHomeLocalityName',
                             'mozillaHomeState', 'mozillaHomePostalCode',
                             'mozillaHomeCountryName', 'street',
                             'mozillaWorkStreet2', 'l', 'st', 'postalCode',
                             'c', 'homePhone', 'telephoneNumber', 'mobile',
                             'fax', 'title', 'company', 'description', 'mozillaWorkUrl',
                              'department', 'mozillaNickname');

    /**
     * Useful Turba address book attribute names.
     *
     * @private
     * @var array
     */
    var $_turbaAttr = array('name', 'firstname', 'lastname', 'email', 'alias',
                            'homeAddress', 'homeStreet', 'homeCity',
                            'homeProvince', 'homePostalCode', 'homeCountry',
                            'workAddress', 'workStreet', 'workCity', 'workProvince',
                            'workPostalCode', 'workCountry',
                            'homePhone', 'workPhone', 'cellPhone',
                            'fax', 'title', 'company', 'notes', 'website',
                            'department', 'nickname');
    /**
     * Turba address book attribute names and the corresponding Mozilla name.
     *
     * @private
     * @var array
     */
    var $_turbaMozillaMap = array('name' => 'cn',
                                  'firstname' => 'givenName',
                                  'lastname' => 'sn',
                                  'email' => 'mail',
                                  'alias' => 'mozillaNickname',
                                  'homePhone' => 'homePhone',
                                  'workPhone' => 'telephoneNumber',
                                  'cellPhone' => 'mobile',
                                  'fax' => 'fax',
                                  'title' => 'title',
                                  'company' => 'company',
                                  'notes' => 'description',
                                  'homeAddress' => 'homeStreet',
                                  'homeStreet' => 'mozillaHomeStreet2',
                                  'homeCity' => 'mozillaHomeLocalityName',
                                  'homeProvince' => 'mozillaHomeState',
                                  'homePostalCode' => 'mozillaHomePostalCode',
                                  'homeCountry' => 'mozillaHomeCountryName',
                                  'workAddress' => 'street',
                                  'workStreet' => 'mozillaWorkStreet2',
                                  'workCity' => 'l',
                                  'workProvince' => 'st',
                                  'workPostalCode' => 'postalCode',
                                  'workCountry' => 'c',
                                  'website' => 'mozillaWorkUrl',
                                  'department' => 'department',
                                  'nickname' => 'mozillaNickname');

    /**
     * Check if a string is safe according to RFC 2849, or if it needs to be
     * base64 encoded.
     *
     * @private
     *
     * @param string $str  The string to check.
     *
     * @return boolean  True if the string is safe, false otherwise.
     */
    function _is_safe_string($str)
    {
        /*  SAFE-CHAR         = %x01-09 / %x0B-0C / %x0E-7F
         *                     ; any value <= 127 decimal except NUL, LF,
         *                     ; and CR
         *
         *  SAFE-INIT-CHAR    = %x01-09 / %x0B-0C / %x0E-1F /
         *                     %x21-39 / %x3B / %x3D-7F
         *                     ; any value <= 127 except NUL, LF, CR,
         *                     ; SPACE, colon (":", ASCII 58 decimal)
         *                     ; and less-than ("<" , ASCII 60 decimal) */
        if (!strlen($str)) {
            return true;
        }
        if ($str[0] == ' ' || $str[0] == ':' || $str[0] == '<') {
            return false;
        }
        for ($i = 0; $i < strlen($str); ++$i) {
            if (ord($str[$i]) > 127 || $str[$i] == NULL || $str[$i] == "\n" ||
                $str[$i] == "\r") {
                return false;
            }
        }

        return true;
    }

    function importData($contents, $header = false)
    {
        $data = array();
        $records = preg_split('/(\r?\n){2}/', $contents);
        foreach ($records as $record) {
            if (trim($record) == '') {
                /* Ignore empty records */
                continue;
            }
            /* one key:value pair per line */
            $lines = preg_split('/\r?\n/', $record);
            $hash = array();
            foreach ($lines as $line) {
                list($key, $delimiter, $value) = preg_split('/(:[:<]?) */', $line, 2, PREG_SPLIT_DELIM_CAPTURE);
                if (in_array($key, $this->_mozillaAttr)) {
                    $hash[$key] = ($delimiter == '::' ? base64_decode($value) : $value);
                }
            }
            $data[] = $hash;
        }

        return $data;
    }

    /**
     * Builds a LDIF file from a given data structure and triggers its download.
     * It DOES NOT exit the current script but only outputs the correct headers
     * and data.
     *
     * @param string $filename  The name of the file to be downloaded.
     * @param array $data       A two-dimensional array containing the data
     *                          set.
     * @param boolean $header   If true, the rows of $data are associative
     *                          arrays with field names as their keys.
     */
    function exportFile($filename, $data, $header = false)
    {
        $export = $this->exportData($data, $header);
        $GLOBALS['browser']->downloadHeaders($filename, 'text/ldif', false, strlen($export));
        echo $export;
    }

    /**
     * Builds a LDIF file from a given data structure and returns it as a
     * string.
     *
     * @param array $data      A two-dimensional array containing the data set.
     * @param boolean $header  If true, the rows of $data are associative
     *                         arrays with field names as their keys.
     *
     * @return string  The LDIF data.
     */
    function exportData($data, $header = false)
    {
        if (!is_array($data) || !count($data)) {
            return '';
        }
        $export = '';
        $mozillaTurbaMap = array_flip($this->_turbaMozillaMap) ;
        foreach ($data as $row) {
            $recordData = '';
            foreach ($this->_mozillaAttr as $value) {
                if (isset($row[$mozillaTurbaMap[$value]])) {
                    // Base64 encode each value as necessary and store it.
                    // Store cn and mail separately for use in record dn
                    if (!$this->_is_safe_string($row[$mozillaTurbaMap[$value]])) {
                        $recordData .= $value . ':: ' . base64_encode($row[$mozillaTurbaMap[$value]]) . "\n";
                    } else {
                        $recordData .= $value . ': ' . $row[$mozillaTurbaMap[$value]] . "\n";
                    }
                }
            }

            $dn = 'cn=' . $row[$mozillaTurbaMap['cn']] . ',mail=' . $row[$mozillaTurbaMap['mail']];
            if (!$this->_is_safe_string($dn)) {
                $export .= 'dn:: ' . base64_encode($dn) . "\n";
            } else {
                $export .= 'dn: ' . $dn . "\n";
            }

            $export .= "objectclass: top\n"
                . "objectclass: person\n"
                . "objectclass: organizationalPerson\n"
                . "objectclass: inetOrgPerson\n"
                . "objectclass: mozillaAbPersonAlpha\n"
                . $recordData . "modifytimestamp: 0Z\n\n";
        }

        return $export;
    }

    /**
     * Takes all necessary actions for the given import step, parameters and
     * form values and returns the next necessary step.
     *
     * @param integer $action  The current step. One of the IMPORT_* constants.
     * @param array $param     An associative array containing needed
     *                         parameters for the current step.
     *
     * @return mixed  Either the next step as an integer constant or imported
     *                data set after the final step.
     */
    function nextStep($action, $param = array())
    {
        switch ($action) {
        case IMPORT_FILE:
            $next_step = parent::nextStep($action, $param);
            if (is_a($next_step, 'PEAR_Error')) {
                return $next_step;
            }

            $_SESSION['import_data']['data'] = $this->importFile($_FILES['import_file']['tmp_name']);
            $data = array();
            foreach ($_SESSION['import_data']['data'] as $record) {
                $turbaHash = array();
                foreach ($this->_turbaAttr as $value) {
                    switch ($value) {
                    case 'homeAddress':
                        // These are the keys we're interested in.
                        $keys = array('homeStreet', 'mozillaHomeStreet2',
                                      'mozillaHomeLocalityName', 'mozillaHomeState',
                                      'mozillaHomePostalCode', 'mozillaHomeCountryName');

                        // Grab all of them that exist in $record.
                        $values = array();
                        foreach ($keys as $key) {
                            if (isset($record[$key])) {
                                $values = $record[$key];
                            }
                        }

                        // Special handling for State if both State
                        // and Locality Name are set.
                        if (isset($values['mozillaHomeLocalityName'])
                            && isset($values['mozillaHomeState'])) {
                            $values['mozillaHomeLocalityName'] .= ', ' . $values['mozillaHomeState'];
                            unset($values['mozillaHomeState']);
                        }

                        if ($values) {
                            $turbaHash[$value] = implode("\n", $values);
                        }
                        break;

                    case 'workAddress':
                        // These are the keys we're interested in.
                        $keys = array('street', 'mozillaWorkStreet2', 'l',
                                      'st', 'postalCode', 'c');

                        // Grab all of them that exist in $record.
                        $values = array();
                        foreach ($keys as $key) {
                            if (isset($record[$key])) {
                                $values = $record[$key];
                            }
                        }

                        // Special handling for "st" if both "st" and
                        // "l" are set.
                        if (isset($values['l']) && isset($values['st'])) {
                            $values['l'] .= ', ' . $values['st'];
                            unset($values['st']);
                        }

                        if ($values) {
                            $turbaHash[$value] = implode("\n", $values);
                        }
                        break;

                    default:
                        if (isset($record[$this->_turbaMozillaMap[$value]])) {
                            $turbaHash[$value] = $record[$this->_turbaMozillaMap[$value]];
                        }
                        break;
                    }
                }

                $data[] = $turbaHash;
            }

            unset($_SESSION['import_data']['data']);
            return $data;

        default:
            return parent::nextStep($action, $param);
        }
    }

}