-
Notifications
You must be signed in to change notification settings - Fork 17
/
data.php
428 lines (387 loc) · 14.1 KB
/
data.php
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/**
* Turba data.php.
*
* Copyright 2001-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (ASL). If you
* did not receive this file, see http://www.horde.org/licenses/apache.
*
* @author Jan Schneider <[email protected]>
*/
/**
* Remove empty attributes from attributes array.
*
* @param mixed $val Value from attributes array.
*
* @return boolean Boolean used by array_filter.
*/
function _emptyAttributeFilter($var)
{
if (!is_array($var)) {
return ($var != '');
}
foreach ($var as $v) {
if ($v == '') {
return false;
}
}
return true;
}
require_once __DIR__ . '/lib/Application.php';
$app_ob = Horde_Registry::appInit('turba');
if (!$conf['menu']['import_export']) {
require TURBA_BASE . '/index.php';
exit;
}
/* If there are absolutely no valid sources, abort. */
if (!$cfgSources) {
$notification->push(_("No Address Books are currently available. Import and Export is disabled."), 'horde.error');
$page_output->header();
$notification->notify(array('listeners' => 'status'));
$page_output->footer();
exit;
}
/* Importable file types. */
$file_types = array(
'csv' => _("CSV"),
'tsv' => _("TSV"),
'vcard' => _("vCard"),
'mulberry' => _("Mulberry Address Book"),
'pine' => _("Pine Address Book"),
'ldif' => _("LDIF Address Book")
);
/* Templates for the different import steps. */
$templates = array(
Horde_Data::IMPORT_FILE => array(TURBA_TEMPLATES . '/data/export.inc'),
Horde_Data::IMPORT_CSV => array($registry->get('templates', 'horde') . '/data/csvinfo.inc'),
Horde_Data::IMPORT_TSV => array($registry->get('templates', 'horde') . '/data/tsvinfo.inc'),
Horde_Data::IMPORT_MAPPED => array($registry->get('templates', 'horde') . '/data/csvmap.inc'),
Horde_Data::IMPORT_DATETIME => array($registry->get('templates', 'horde') . '/data/datemap.inc')
);
/* Initial values. */
$vars = $injector->getInstance('Horde_Variables');
$import_step = $vars->get('import_step', 0) + 1;
$next_step = Horde_Data::IMPORT_FILE;
$app_fields = $bad_charset = $time_fields = array();
$error = false;
$import_mapping = array(
'e-mail' => 'email',
'homeaddress' => 'homeAddress',
'businessaddress' => 'workAddress',
'homephone' => 'homePhone',
'businessphone' => 'workPhone',
'mobilephone' => 'cellPhone',
'businessfax' => 'fax',
'jobtitle' => 'title',
'internetfreebusy' => 'freebusyUrl',
// Turba groups
'uid' => '__uid',
'members' => '__members',
// Entourage on MacOS
'Dept' => 'department',
'Work Street Address' => 'workStreet',
'Work City' => 'workCity',
'Work State' => 'workProvince',
'Work Zip' => 'workPostalCode',
'Work Country/Region' => 'workCountry',
'Home Street Address' => 'homeStreet',
'Home City' => 'homeCity',
'Home State' => 'homeProvince',
'Home Zip' => 'homePostalCode',
'Home Country/Region' => 'homeCountry',
'Work Fax' => 'workFax',
'Work Phone 1' => 'workPhone',
'Home Phone 1' => 'homePhone',
'Instant Messaging 1' => 'imaddress',
// Thunderbird
'Primary Email' => 'email',
'Fax Number' => 'fax',
'Pager Number' => 'pager',
'Mobile Number' => 'Mobile Phone',
'Home Address' => 'homeStreet',
'Home ZipCode' => 'homePostalCode',
'Work Address' => 'workStreet',
'Work ZipCode' => 'workPostalCode',
'Work Country' => 'workCountry',
'Work Phone' => 'workPhone',
'Organization' => 'company',
'Web Page 1' => 'website',
);
$param = array(
'time_fields' => $time_fields,
'file_types' => $file_types,
'import_mapping' => array_merge(
$app_ob->getOutlookMapping(),
$import_mapping
)
);
if (in_array($vars->import_format, array('mulberry', 'pine'))) {
$vars->import_format = 'tsv';
}
if ($vars->actionID != 'select') {
array_unshift($templates[Horde_Data::IMPORT_FILE], TURBA_TEMPLATES . '/data/import.inc');
}
$data = null;
if ($vars->import_format) {
switch ($vars->import_format) {
case 'ldif':
$driver = 'Turba_Data_Ldif';
break;
case 'csv':
$param['check_charset'] = true;
// Fall-through
default:
$driver = $vars->import_format;
break;
}
try {
$data = $injector->getInstance('Horde_Core_Factory_Data')->create(
$driver,
array(
'cleanup' => array($app_ob, 'cleanupData')
)
);
} catch (Horde_Exception $e) {
$notification->push(_("This file format is not supported."), 'horde.error');
$next_step = Horde_Data::IMPORT_FILE;
}
}
/* Loop through the action handlers. */
switch ($vars->actionID) {
case Horde_Data::IMPORT_FILE:
try {
$driver = $injector->getInstance('Turba_Factory_Driver')->create($vars->dest);
} catch (Horde_Exception $e) {
$notification->push($e, 'horde.error');
$error = true;
break;
}
if (Turba::hasMaxContacts($driver, true)) {
$error = true;
} else {
$data->storage->set('target', $vars->dest);
$data->storage->set('purge', $vars->purge);
}
break;
case Horde_Data::IMPORT_MAPPED:
case Horde_Data::IMPORT_DATETIME:
foreach ($cfgSources[$data->storage->get('target')]['map'] as $field => $null) {
if (substr($field, 0, 2) != '__' && !is_array($null)) {
switch ($attributes[$field]['type']) {
case 'monthyear':
case 'monthdayyear':
$time_fields[$field] = 'date';
break;
case 'time':
$time_fields[$field] = 'time';
break;
}
}
}
$param['time_fields'] = $time_fields;
break;
}
if (!$error && $data) {
try {
try {
$next_step = $data->nextStep($vars->actionID, $param);
/* Raise warnings if some exist. */
if (method_exists($data, 'warnings')) {
$warnings = $data->warnings();
if (count($warnings)) {
foreach ($warnings as $warning) {
$notification->push($warning, 'horde.warning');
}
$notification->push(_("The import can be finished despite the warnings."), 'horde.message');
}
}
} catch (Horde_Data_Exception_Charset $e) {
if ($e->badCharset != 'UTF-8') {
$bad_charset[] = $e->badCharset;
throw $e;
}
$param['charset'] = 'windows-1252';
try {
$next_step = $data->nextStep($vars->actionID, $param);
} catch (Horde_Data_Exception_Charset $e) {
$bad_charset = array('UTF-8', 'windows-1252');
throw $e;
}
}
} catch (Horde_Data_Exception $e) {
$notification->push($e, 'horde.error');
$next_step = $data->cleanup();
}
}
/* We have a final result set. */
if (is_array($next_step)) {
/* Create a Turba storage instance. */
$dest = $data->storage->get('target');
try {
$driver = $injector->getInstance('Turba_Factory_Driver')->create($dest);
} catch (Horde_Exception $e) {
$notification->push($e, 'horde.error');
$driver = null;
}
if (!count($next_step)) {
$notification->push(sprintf(_("The %s file didn't contain any contacts."), $file_types[$data->storage->get('format')]), 'horde.error');
} elseif ($driver) {
/* Purge old address book if requested. */
if ($data->storage->get('purge')) {
try {
$driver->deleteAll();
$notification->push(_("Address book successfully purged."), 'horde.success');
} catch (Turba_Exception $e) {
$notification->push(sprintf(_("The address book could not be purged: %s"), $e->getMessage()), 'horde.error');
}
}
$error = false;
$imported = 0;
$contact_groups = array();
foreach ($next_step as $row) {
if ($row instanceof Horde_Icalendar_Vcard) {
$row = $driver->toHash($row);
} else {
// Check for contact groups, and defer processing until all
// other contacts are added.
if (!empty($row['__members'])) {
$contact_groups[] = $row;
continue;
}
}
/* Don't search for empty attributes. */
try {
$result = $driver->search(array_filter($row, '_emptyAttributeFilter'));
} catch (Turba_Exception $e) {
$notification->push($e, 'horde.error');
$error = true;
break;
}
if (count($result)) {
$result->reset();
$object = $result->next();
$notification->push(sprintf(_("\"%s\" already exists and was not imported."),
$object->getValue('name')), 'horde.message');
} else {
/* Check for, and validate, any email fields */
foreach (array_keys($row) as $field) {
if ($attributes[$field]['type'] == 'email') {
$allow_multi = is_array($attributes[$field]['params']) &&
!empty($attributes[$field]['params']['allow_multi']);
$rfc822 = $injector->getInstance('Horde_Mail_Rfc822');
try {
$row[$field] = strval($rfc822->parseAddressList($row[$field], array(
'limit' => $allow_multi ? 0 : 1
)));
} catch (Horde_Mail_Exception $e) {
$row[$field] = '';
}
}
}
$row['__owner'] = $driver->getContactOwner();
try {
$driver->add($row);
$imported++;
} catch (Turba_Exception $e) {
$notification->push(sprintf(_("There was an error importing the data: %s"), $e->getMessage()), 'horde.error');
$error = true;
break;
}
}
}
// Now attempt to create Turba group objects.
foreach ($contact_groups as $group) {
$attributes = $group;
unset($attributes['__members']);
$group_obj = new Turba_Object_Group($driver, $attributes);
foreach (explode(',', $group['__members']) as $uid) {
$results = $driver->search(array('__uid' => $uid));
if (count($results->objects)) {
$object = array_pop($results->objects);
$group_obj->addMember($object->getValue('__key'), $object->getSource());
}
}
// We don't actually use the group object to save to storage since
// it's not an existing group. We use it so it's responsible for
// properly formatting the __members data, which we pull out and
// place in the attributes array.
$attributes['__members'] = $group_obj->getValue('__members');
$attributes['__type'] = 'group';
$driver->add($attributes);
}
if (!$error && $imported) {
$notification->push(sprintf(_("%s file successfully imported."),
$file_types[$data->storage->get('format')]), 'horde.success');
}
}
$next_step = $data->cleanup();
}
switch ($next_step) {
case Horde_Data::IMPORT_MAPPED:
case Horde_Data::IMPORT_DATETIME:
foreach ($cfgSources[$data->storage->get('target')]['map'] as $field => $null) {
if ((substr($field, 0, 2) != '__' && !is_array($null)) || ($field == '__uid' || $field == '__members')) {
if ($field == '__uid') {
$app_fields['__uid'] = _("UID");
} elseif ($field == '__members') {
$app_fields['__members'] = _("Contact list members");
} else {
$app_fields[$field] = $attributes[$field]['label'];
}
}
}
break;
}
$page_output->header(array(
'title' => _("Import/Export Address Books")
));
$notification->notify(array('listeners' => 'status'));
$default_source = $prefs->getValue('default_dir');
if ($next_step == Horde_Data::IMPORT_FILE) {
/* Build the directory sources select widget. */
$unique_source = '';
$source_options = array();
foreach (Turba::getAddressBooks() as $key => $entry) {
if (!empty($entry['export'])) {
$source_options[] = '<option value="' . htmlspecialchars($key) . '">' .
htmlspecialchars($entry['title']) . "</option>\n";
$unique_source = $key;
}
}
/* Build the directory destination select widget. */
$unique_dest = '';
$dest_options = array();
$hasWriteable = false;
foreach (Turba::getAddressBooks(Horde_Perms::EDIT) as $key => $entry) {
$selected = ($key == $default_source) ? ' selected="selected"' : '';
$dest_options[] = '<option value="' . htmlspecialchars($key) . '" ' . $selected . '>' .
htmlspecialchars($entry['title']) . "</option>\n";
$unique_dest = $key;
$hasWriteable = true;
}
if (!$hasWriteable) {
array_shift($templates[$next_step]);
}
/* Build the charset options. */
$charsets = array();
if (!empty($bad_charset)) {
$charsets = $registry->nlsconfig->encodings_sort;
foreach ($registry->nlsconfig->charsets as $charset) {
if (!isset($charsets[$charset]) &&
!in_array($charset, $bad_charset)) {
$charsets[$charset] = $charset;
}
}
$my_charset = $GLOBALS['registry']->getLanguageCharset();
}
}
// @todo: These variables are needed for stuff that is in *Horde*, not Turba.
// That's not correct.
if ($data) {
$storage = $data->storage;
}
foreach ($templates[$next_step] as $template) {
require $template;
}
$page_output->footer();