1 |
<?php |
2 |
/* |
3 |
* $Horde: turba/data.php,v 1.12.2.10 2002/05/27 17:05:20 jan Exp $ |
4 |
* |
5 |
* Copyright 2001-2002 Jan Schneider <jan@horde.org> |
6 |
* |
7 |
* See the enclosed file COPYING for license information (GPL). If you |
8 |
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html. |
9 |
*/ |
10 |
|
11 |
define('TURBA_BASE', dirname(__FILE__)); |
12 |
require_once TURBA_BASE . '/lib/base.php'; |
13 |
require_once TURBA_BASE . '/lib/Source.php'; |
14 |
require_once HORDE_BASE . '/lib/Data.php'; |
15 |
require TURBA_BASE . '/config/attributes.php'; |
16 |
|
17 |
if (!$conf['menu']['import_export']) { |
18 |
header('Location: ' . Horde::applicationUrl('index.php', true)); |
19 |
exit; |
20 |
} |
21 |
|
22 |
$error = false; |
23 |
$errors_import = array(); |
24 |
$actionID = Horde::getFormData('actionID', NO_ACTION); |
25 |
|
26 |
if ($actionID == HORDE_IMPORT) { |
27 |
|
28 |
$importID = Horde::getFormData('importID'); |
29 |
$dates = array(); |
30 |
|
31 |
if ($importID == IMPORT_MAPPED) { |
32 |
|
33 |
include_once HORDE_BASE . '/lib/SessionCache.php'; |
34 |
$cache = new SessionCache(); |
35 |
$cacheID = Horde::getFormData('cacheID'); |
36 |
|
37 |
if (!isset($cacheID) || !(list($importData, $dest) = $cache->getObject($cacheID))) { |
38 |
Horde::raiseMessage(_("The data got lost."), HORDE_ERROR); |
39 |
$error = true; |
40 |
} else { |
41 |
$import_type = Horde::getFormData('import_type'); |
42 |
$dataKeys = Horde::getFormData('dataKeys', ''); |
43 |
$appKeys = Horde::getFormData('appKeys', ''); |
44 |
if (empty($dataKeys) || empty($appKeys)) { |
45 |
Horde::raiseMessage(_("You didn't map any fields from the imported file to the corresponding fields in Turba."), HORDE_ERROR); |
46 |
$error = true; |
47 |
} else { |
48 |
$dataKeys = explode("\t", $dataKeys); |
49 |
$appKeys = explode("\t", $appKeys); |
50 |
$map = array(); |
51 |
foreach ($appKeys as $key => $app) { |
52 |
$map[$app] = $dataKeys[$key]; |
53 |
if ($attributes[$app]['type'] == 'date' || |
54 |
$attributes[$app]['type'] == 'time' || |
55 |
$attributes[$app]['type'] == 'datetime') { |
56 |
$dates[$dataKeys[$key]]['type'] = $attributes[$app]['type']; |
57 |
for ($i = 0; $i < min(count($importData), 10); $i++) { |
58 |
if ($importData[$i][$dataKeys[$key]] != '') { |
59 |
$dates[$dataKeys[$key]]['values'][] = $importData[$i][$dataKeys[$key]]; |
60 |
} |
61 |
} |
62 |
} |
63 |
} |
64 |
|
65 |
if (count($dates) > 0) { |
66 |
$cacheID = $cache->putObject(array($importData, $dest, $map)); |
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
} elseif ($importID == IMPORT_DATETIME) { |
72 |
|
73 |
include_once HORDE_BASE . '/lib/SessionCache.php'; |
74 |
$cache = new SessionCache(); |
75 |
$cacheID = Horde::getFormData('cacheID'); |
76 |
|
77 |
if (!isset($cacheID) || !($tmp = $cache->getObject($cacheID))) { |
78 |
Horde::raiseMessage(_("The data got lost"), HORDE_ERROR); |
79 |
$error = true; |
80 |
} else { |
81 |
$importData = $tmp[0]; |
82 |
$dest = $tmp[1]; |
83 |
$map = $tmp[2]; |
84 |
$delimiter = Horde::getFormData('delimiter'); |
85 |
$format = Horde::getFormData('format'); |
86 |
} |
87 |
|
88 |
} |
89 |
|
90 |
|
91 |
if ($importID == IMPORT_MAPPED || $importID == IMPORT_DATETIME) { |
92 |
|
93 |
if (!$error && count($dates) == 0) { |
94 |
|
95 |
$driver = &Turba_Source::singleton($dest, $cfgSources[$dest]); |
96 |
if (PEAR::isError($driver)) { |
97 |
Horde::raiseMessage(_("Failed to connect to the specified directory."), HORDE_ERROR); |
98 |
$error = true; |
99 |
} else { |
100 |
foreach ($importData as $row) { |
101 |
$hash = array(); |
102 |
reset($cfgSources[$dest]['map']); |
103 |
while (list($key, ) = each($cfgSources[$dest]['map'])) { |
104 |
// determine if appKey contains expression |
105 |
if ( isset($map[$key]) && substr($map[$key], 0, 6) == 'expr: ') { |
106 |
// patch "$row[$map[$key]]" |
107 |
$val = evaluateExpression(substr($map[$key], 6), $row); |
108 |
$row[$map[$key]] = $val; |
109 |
} |
110 |
if (isset($map[$key]) && !empty($row[$map[$key]])) { |
111 |
if ($attributes[$key]['type'] == 'date' || |
112 |
$attributes[$key]['type'] == 'time' || |
113 |
$attributes[$key]['type'] == 'datetime') { |
114 |
$dt_arr = Data::mapDate($row[$map[$key]], $attributes[$key]['type'], $delimiter[$map[$key]], $format[$map[$key]]); |
115 |
} |
116 |
if ($attributes[$key]['type'] == 'date' && |
117 |
$dt_arr['mday'] != 0 && |
118 |
$dt_arr['month'] != 0 && |
119 |
$dt_arr['year'] != 0) { |
120 |
$hash[$key] = mktime(0, 0, 0, $dt_arr['month'], $dt_arr['mday'], $dt_arr['year']); |
121 |
} elseif ($attributes[$key]['type'] == 'time') { |
122 |
$hash[$key] = mktime($dt_arr['hour'], $dt_arr['min'], $dt_arr['sec'], 0, 0, 0); |
123 |
} elseif ($attributes[$key]['type'] == 'datetime') { |
124 |
$hash[$key] = mktime($dt_arr['hour'], $dt_arr['min'], $dt_arr['sec'], $dt_arr['month'], $dt_arr['mday'], $dt_arr['year']); |
125 |
} else { |
126 |
$hash[$key] = $row[$map[$key]]; |
127 |
} |
128 |
} |
129 |
} |
130 |
$hash['__owner'] = Auth::getAuth(); |
131 |
|
132 |
$result = $driver->addObject($hash); |
133 |
// $driver->addObject may return a PEAR-Error-Object |
134 |
if ( is_object($result) && get_class($result) == 'pear_error' ) { |
135 |
$error_row = array( |
136 |
'error_source' => 'PEAR', |
137 |
'message' => $result->message, |
138 |
'sourcedata_dump' => showvar($row), |
139 |
'mapping_dump' => showvar($map), |
140 |
'importhash_dump' => showvar($hash), |
141 |
); |
142 |
array_push($errors_import, $error_row); |
143 |
$error = true; |
144 |
} |
145 |
|
146 |
# if ($result == true) { |
147 |
# $error = false; |
148 |
# } |
149 |
} |
150 |
} |
151 |
|
152 |
if ($error) { |
153 |
$error_idx = 0; |
154 |
?> |
155 |
<script language="javascript"> |
156 |
function toggleDetails(obj) { |
157 |
sel = obj.style.getAttribute('display'); |
158 |
if (sel == 'none') { |
159 |
obj.style.setAttribute('display', 'inline'); |
160 |
} else { |
161 |
obj.style.setAttribute('display', 'none'); |
162 |
} |
163 |
} |
164 |
</script> |
165 |
<? |
166 |
$msg_append = ''; |
167 |
foreach ($errors_import as $error_item) { |
168 |
$msg_append .= $error_item['message'] . " |
169 |
<a href=\"javascript:toggleDetails(document.all.details$error_idx);\">details</a> |
170 |
<span id=\"details$error_idx\" style=\"display:none; font-size:10px;\"> |
171 |
<pre><blockquote> |
172 |
sourcedata:<br>$error_item[sourcedata_dump]<br> |
173 |
mapping:<br>$error_item[mapping_dump]<br> |
174 |
import-hash:<br>$error_item[importhash_dump]<br> |
175 |
</pre></blockquote></span><hr>"; |
176 |
$error_idx++; |
177 |
} |
178 |
Horde::raiseMessage(_("There was an error importing the data.<hr>" . $msg_append), HORDE_ERROR); |
179 |
} else { |
180 |
$import_type = Horde::getFormData('import_type'); |
181 |
switch ($import_type) { |
182 |
case IMPORT_CSV: |
183 |
$type = "CSV"; |
184 |
break; |
185 |
case IMPORT_OUTLOOK: |
186 |
$type = "Outlook"; |
187 |
break; |
188 |
} |
189 |
Horde::raiseMessage(sprintf(_("%s file successfully imported"), $type), HORDE_SUCCESS); |
190 |
} |
191 |
|
192 |
} |
193 |
|
194 |
} else { |
195 |
|
196 |
if (!isset($HTTP_POST_FILES['import_file']['size']) || |
197 |
!isset($HTTP_POST_FILES['import_file']['tmp_name'])) { |
198 |
Horde::raiseMessage(_("There was a problem with the file upload. The file may have been larger than the maximum allowed size."), HORDE_ERROR); |
199 |
$error = true; |
200 |
} elseif (!is_uploaded_file($HTTP_POST_FILES['import_file']['tmp_name'])) { |
201 |
Horde::raiseMessage(_("Illegal or malformed form data."), HORDE_ERROR); |
202 |
$error = true; |
203 |
} elseif ($HTTP_POST_FILES['import_file']['size'] > 0) { |
204 |
_fileCleanup($HTTP_POST_FILES['import_file']['tmp_name']); |
205 |
|
206 |
$header = null; |
207 |
switch ($importID) { |
208 |
|
209 |
case IMPORT_OUTLOOK: |
210 |
$header = true; |
211 |
|
212 |
case IMPORT_CSV: |
213 |
$csv = new Data_csv(); |
214 |
if (!isset($header)) { |
215 |
$header = Horde::getFormData('header'); |
216 |
} |
217 |
$importData = $csv->importFile($HTTP_POST_FILES['import_file']['tmp_name'], $header, ','); |
218 |
|
219 |
if (!isset($importData) || !is_array($importData)) { |
220 |
Horde::raiseMessage(_("There was an error importing the uploaded file"), HORDE_ERROR); |
221 |
$error = true; |
222 |
} else { |
223 |
$dest = Horde::getFormData('dest'); |
224 |
include_once HORDE_BASE . '/lib/SessionCache.php'; |
225 |
$cache = new SessionCache(); |
226 |
$cacheID = $cache->putObject(array($importData, $dest)); |
227 |
$actionUrl = Horde::applicationUrl('data.php'); |
228 |
|
229 |
$appFields = array(); |
230 |
reset($cfgSources[$dest]['map']); |
231 |
while (list($key, ) = each($cfgSources[$dest]['map'])) { |
232 |
if (substr($key, 0, 2) != '__') { |
233 |
$appFields[$key] = $attributes[$key]['desc']; |
234 |
} |
235 |
} |
236 |
} |
237 |
|
238 |
break; |
239 |
|
240 |
case IMPORT_VCARD: |
241 |
$vcf = new Data_rfc2425(); |
242 |
$data = $vcf->importFile($HTTP_POST_FILES['import_file']['tmp_name']); |
243 |
$dest = Horde::getFormData('dest'); |
244 |
$driver = &Turba_Source::singleton($dest, $cfgSources[$dest]); |
245 |
if (PEAR::isError($driver)) { |
246 |
Horde::raiseMessage(_("Failed to connect to the specified directory."), HORDE_ERROR); |
247 |
$error = true; |
248 |
} else { |
249 |
$dest = $cfgSources[$dest]['map']; |
250 |
foreach ($data as $object) { |
251 |
if ($object['type'] == 'VCARD') { |
252 |
$hash = array(); |
253 |
foreach ($object['params'] as $item) { |
254 |
|
255 |
switch ($item['name']) { |
256 |
|
257 |
case 'FN': |
258 |
$hash['name'] = $vcf->read($item); |
259 |
break; |
260 |
|
261 |
case 'NICKNAME': |
262 |
$hash['alias'] = $vcf->read($item); |
263 |
break; |
264 |
|
265 |
// We use LABEL but should probably use ADR. |
266 |
case 'LABEL': |
267 |
if (isset($item['params']['HOME'])) { |
268 |
$hash['homeAddress'] = $vcf->read($item); |
269 |
} elseif (isset($item['params']['WORK'])) { |
270 |
$hash['workAddress'] = $vcf->read($item); |
271 |
} else { |
272 |
$hash['workAddress'] = $vcf->read($item); |
273 |
} |
274 |
break; |
275 |
|
276 |
case 'TEL': |
277 |
if (isset($item['params']['VOICE'])) { |
278 |
if (isset($item['params']['HOME'])) { |
279 |
$hash['homePhone'] = $vcf->read($item); |
280 |
} elseif (isset($item['params']['WORK'])) { |
281 |
$hash['workPhone'] = $vcf->read($item); |
282 |
} elseif (isset($item['params']['CELL'])) { |
283 |
$hash['cellPhone'] = $vcf->read($item); |
284 |
} |
285 |
} elseif (isset($item['params']['FAX'])) { |
286 |
$hash['fax'] = $vcf->read($item); |
287 |
} |
288 |
break; |
289 |
|
290 |
case 'EMAIL': |
291 |
if (isset($item['params']['PREF']) || !isset($hash['email'])) { |
292 |
$hash['email'] = $vcf->read($item); |
293 |
} |
294 |
break; |
295 |
|
296 |
case 'TITLE': |
297 |
$hash['title'] = $vcf->read($item); |
298 |
break; |
299 |
|
300 |
case 'ORG': |
301 |
$units = array(); |
302 |
for ($i = 0; $i < count($item['values']); $i++) { |
303 |
$units[] = $vcf->read($item, $i); |
304 |
} |
305 |
$hash['company'] = implode(', ', $units); |
306 |
break; |
307 |
|
308 |
case 'NOTE': |
309 |
$hash['notes'] = $vcf->read($item); |
310 |
break; |
311 |
|
312 |
} |
313 |
|
314 |
} |
315 |
$hash['__owner'] = Auth::getAuth(); |
316 |
if (!$driver->addObject($hash)) { |
317 |
$error = true; |
318 |
} |
319 |
} |
320 |
} |
321 |
} |
322 |
|
323 |
if ($error) { |
324 |
Horde::raiseMessage(_("There was an error importing the data."), HORDE_ERROR); |
325 |
} else { |
326 |
Horde::raiseMessage(sprintf(_("%s file successfully imported"), 'vCard'), HORDE_SUCCESS); |
327 |
} |
328 |
|
329 |
break; |
330 |
|
331 |
} |
332 |
|
333 |
} else { |
334 |
Horde::raiseMessage(_("The file contained no data."), HORDE_ERROR); |
335 |
$error = true; |
336 |
} |
337 |
} |
338 |
|
339 |
} elseif ($actionID == HORDE_EXPORT) { |
340 |
$exportID = Horde::getFormData('exportID'); |
341 |
|
342 |
switch ($exportID) { |
343 |
|
344 |
case EXPORT_CSV: |
345 |
$source = Horde::getFormData('source'); |
346 |
if (!isset($source) && isset($cfgSources) && is_array($cfgSources) && count($cfgSources) > 0) { |
347 |
reset($cfgSources); |
348 |
$source = key($cfgSources); |
349 |
} |
350 |
|
351 |
$driver = &Turba_Source::singleton($source, $cfgSources[$source]); |
352 |
if (PEAR::isError($driver)) { |
353 |
Horde::raiseMessage(_("Failed to connect to the specified directory."), HORDE_ERROR); |
354 |
$error = true; |
355 |
} else { |
356 |
if (get_class($addresses = $driver->search(array())) != 'turba_list') { |
357 |
Horde::raiseMessage(sprintf(_("Failed to search the directory: %s"), $driver->error()), HORDE_ERROR); |
358 |
$error = true; |
359 |
} else { |
360 |
$data = array(); |
361 |
while ($ob = $addresses->next()) { |
362 |
$tmp = $ob->getAttributes(); |
363 |
$row = array(); |
364 |
foreach ($tmp as $key => $attribute) { |
365 |
if (substr($key, 0, 2) != '__') { |
366 |
$row[$key] = $attribute; |
367 |
} |
368 |
} |
369 |
$data[] = $row; |
370 |
} |
371 |
|
372 |
$csv = new Data_csv(); |
373 |
$csv->exportFile('turba.csv', $data, true); |
374 |
exit; |
375 |
} |
376 |
} |
377 |
break; |
378 |
|
379 |
} |
380 |
} |
381 |
|
382 |
$title = _("Import/Export Addressbooks"); |
383 |
$js_onLoad = null; |
384 |
require TURBA_TEMPLATES . '/common-header.inc'; |
385 |
require TURBA_BASE . '/menu.php'; |
386 |
require TURBA_BASE . '/status.php'; |
387 |
|
388 |
if ($actionID == NO_ACTION || $error) { |
389 |
/* Build the directory sources select widget. */ |
390 |
$source_options = ''; |
391 |
foreach ($cfgSources as $key => $entry) { |
392 |
if (!empty($entry['export'])) { |
393 |
$source_options .= "<option value=\"$key\">"; |
394 |
$source_options .= $entry['title'] . "</option>\n"; |
395 |
} |
396 |
} |
397 |
|
398 |
/* Build the directory destination select widget. */ |
399 |
$dest_options = ''; |
400 |
$hasWriteable = false; |
401 |
foreach ($cfgSources as $key => $entry) { |
402 |
if (empty($entry['readonly']) || (isset($entry['admin']) && in_array(Auth::getAuth(), $entry['admin']))) { |
403 |
$dest_options .= "<option value=\"$key\">"; |
404 |
$dest_options .= $entry['title'] . "</option>\n"; |
405 |
$hasWriteable = true; |
406 |
} |
407 |
} |
408 |
|
409 |
if ($hasWriteable) { |
410 |
include TURBA_TEMPLATES . '/data/import.inc'; |
411 |
} |
412 |
include TURBA_TEMPLATES . '/data/export.inc'; |
413 |
} else { |
414 |
switch ($importID) { |
415 |
|
416 |
case IMPORT_OUTLOOK: |
417 |
case IMPORT_CSV: |
418 |
include $registry->getParam('templates', 'horde') . '/data/csvmap.inc'; |
419 |
break; |
420 |
|
421 |
case IMPORT_MAPPED: |
422 |
if (count($dates) > 0) { |
423 |
$actionUrl = Horde::applicationUrl('data.php'); |
424 |
include $registry->getParam('templates', 'horde') . '/data/datemap.inc'; |
425 |
} |
426 |
break; |
427 |
|
428 |
} |
429 |
} |
430 |
|
431 |
$registry->shutdown(); |
432 |
require TURBA_TEMPLATES . '/common-footer.inc'; |
433 |
|
434 |
|
435 |
// dumps a variable |
436 |
function showvar($var, $string = '$var') |
437 |
{ |
438 |
$out = ""; |
439 |
// TRUE, FALSE, NULL and empty arrays: return this value |
440 |
if($var === true) { |
441 |
$out .= htmlentities($string . ' = TRUE;') . "\n"; |
442 |
} else if($var === false) { |
443 |
$out .= htmlentities($string . ' = FALSE;') . "\n"; |
444 |
} else if($var === null) { |
445 |
$out .= htmlentities($string . ' = NULL;') . "\n"; |
446 |
} else if($var === array()) { |
447 |
$out .= htmlentities($string . ' = array();') . "\n"; |
448 |
// array or object - foreach element of $var |
449 |
} else if ( is_array( $var ) || is_object($var) ) { |
450 |
foreach($var as $k=>$v) { |
451 |
// Format the string which stands next to the ' = '. |
452 |
// [] for arrays and -> for objects |
453 |
if(is_array($var)) { |
454 |
if(is_string($k)) { |
455 |
$k = "'" . str_replace('\"', '"', |
456 |
addslashes($k)) . "'"; |
457 |
} |
458 |
$new_string = $string . '[' . $k . ']'; |
459 |
} else if(is_object($var)) { |
460 |
$new_string = $string . '->' . $k; |
461 |
} |
462 |
// dive |
463 |
$out .= showvar($v, $new_string); |
464 |
} |
465 |
// not object, not array |
466 |
} else { |
467 |
// Format as a string if it is one |
468 |
if(is_string($var)) { |
469 |
$var = "'" . str_replace('\"', '"', |
470 |
addslashes($var)) . "'"; |
471 |
} |
472 |
$out .= htmlentities($string . ' = ' . $var) . ";\n"; |
473 |
} |
474 |
return $out; |
475 |
} |
476 |
|
477 |
|
478 |
// evaluates field-expression |
479 |
function evaluateExpression($expr, $row) { |
480 |
$expr = str_replace('+', '.', $expr); |
481 |
preg_match_all("/{.+?}/", $expr, $matches); |
482 |
foreach ($matches[0] as $match_item) { |
483 |
$key = substr($match_item, 1, -1); |
484 |
$replacement = "''"; |
485 |
if (isset($row[$key])) { |
486 |
$replacement = "'" . $row[$key] . "'"; |
487 |
} |
488 |
$expr = str_replace($match_item, $replacement, $expr); |
489 |
} |
490 |
$expr_evalcmd = 'return ' . $expr . ';'; |
491 |
$expr_evalres = eval($expr_evalcmd); |
492 |
return $expr_evalres; |
493 |
} |
494 |
|
495 |
?> |