source: subversion/trunk/roundcubemail/program/steps/addressbook/import.inc @ 6067

Last change on this file since 6067 was 6067, checked in by alec, 14 months ago
  • Fix importing to LDAP addressbook when mail attribute is required by validating input data with autofix
File size: 7.5 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/import.inc                                  |
6 |                                                                       |
7 | This file is part of the Roundcube Webmail client                     |
8 | Copyright (C) 2008-2009, The Roundcube Dev Team                       |
9 |                                                                       |
10 | Licensed under the GNU General Public License version 3 or            |
11 | any later version with exceptions for skins & plugins.                |
12 | See the README file for a full license statement.                     |
13 |                                                                       |
14 | PURPOSE:                                                              |
15 |   Import contacts from a vCard or CSV file                            |
16 |                                                                       |
17 +-----------------------------------------------------------------------+
18 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19 | Author: Aleksander Machniak <machniak@kolabsys.com>                   |
20 +-----------------------------------------------------------------------+
21
22 $Id$
23
24*/
25
26/**
27 * Handler function to display the import/upload form
28 */
29function rcmail_import_form($attrib)
30{
31  global $RCMAIL, $OUTPUT;
32  $target = get_input_value('_target', RCUBE_INPUT_GPC);
33
34  $attrib += array('id' => "rcmImportForm");
35
36  $writable_books = $RCMAIL->get_address_sources(true);
37
38  $upload = new html_inputfield(array('type' => 'file', 'name' => '_file', 'id' => 'rcmimportfile', 'size' => 40));
39  $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . $upload->show());
40
41  // addressbook selector
42  if (count($writable_books) > 1) {
43    $select = new html_select(array('name' => '_target', 'id' => 'rcmimporttarget'));
44
45    foreach ($writable_books as $book)
46        $select->add($book['name'], $book['id']);
47
48    $form .= html::p(null, html::label('rcmimporttarget', rcube_label('importtarget'))
49        . $select->show($target));
50  }
51  else {
52    $abook = new html_hiddenfield(array('name' => '_target', 'value' => key($writable_books)));
53    $form .= $abook->show();
54  }
55
56  $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
57  $form .= html::p(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)) .
58    html::label('rcmimportreplace', rcube_label('importreplace')));
59
60  $OUTPUT->set_env('writable_source', !empty($writable_books));
61  $OUTPUT->add_label('selectimportfile','importwait');
62  $OUTPUT->add_gui_object('importform', $attrib['id']);
63
64  $out = html::p(null, Q(rcube_label('importtext'), 'show'));
65
66  $out .= $OUTPUT->form_tag(array(
67      'action' => $RCMAIL->url('import'),
68      'method' => 'post',
69      'enctype' => 'multipart/form-data') + $attrib,
70    $form);
71
72  return $out;
73}
74
75
76/**
77 * Render the confirmation page for the import process
78 */
79function rcmail_import_confirm($attrib)
80{
81  global $IMPORT_STATS;
82
83  $vars = get_object_vars($IMPORT_STATS);
84  $vars['names'] = $vars['skipped_names'] = '';
85
86  $content = html::p(null, rcube_label(array(
87      'name' => 'importconfirm',
88      'nr' => $IMORT_STATS->inserted,
89      'vars' => $vars,
90    )) . ($IMPORT_STATS->names ? ':' : '.'));
91
92  if ($IMPORT_STATS->names)
93    $content .= html::p('em', join(', ', array_map('Q', $IMPORT_STATS->names)));
94
95  if ($IMPORT_STATS->skipped) {
96      $content .= html::p(null, rcube_label(array(
97          'name' => 'importconfirmskipped',
98          'nr' => $IMORT_STATS->skipped,
99          'vars' => $vars,
100        )) . ':');
101      $content .= html::p('em', join(', ', array_map('Q', $IMPORT_STATS->skipped_names)));
102  }
103
104  return html::div($attrib, $content);
105}
106
107
108/**
109 * Create navigation buttons for the current import step
110 */
111function rcmail_import_buttons($attrib)
112{
113  global $IMPORT_STATS, $OUTPUT;
114  $target = get_input_value('_target', RCUBE_INPUT_GPC);
115
116  $attrib += array('type' => 'input');
117  unset($attrib['name']);
118
119  if (is_object($IMPORT_STATS)) {
120    $attrib['class'] = trim($attrib['class'] . ' mainaction');
121    $out = $OUTPUT->button(array('command' => 'list', 'prop' => $target, 'label' => 'done') + $attrib);
122  }
123  else {
124    $out = $OUTPUT->button(array('command' => 'list', 'label' => 'cancel') + $attrib);
125    $out .= '&nbsp;';
126    $attrib['class'] = trim($attrib['class'] . ' mainaction');
127    $out .= $OUTPUT->button(array('command' => 'import', 'label' => 'import') + $attrib);
128  }
129
130  return $out;
131}
132
133
134/** The import process **/
135
136$importstep = 'rcmail_import_form';
137
138if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
139  $replace = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
140  $target = get_input_value('_target', RCUBE_INPUT_GPC);
141  $CONTACTS = $RCMAIL->get_address_book($target, true);
142
143  // let rcube_vcard do the hard work :-)
144  $vcard_o = new rcube_vcard();
145  $vcard_o->extend_fieldmap($CONTACTS->vcard_map);
146
147  $vcards = $vcard_o->import(file_get_contents($_FILES['_file']['tmp_name']));
148
149  // no vcards detected
150  if (!count($vcards)) {
151    $OUTPUT->show_message('importerror', 'error');
152  }
153  else if ($CONTACTS->readonly) {
154    $OUTPUT->show_message('addresswriterror', 'error');
155  }
156  else {
157    $IMPORT_STATS = new stdClass;
158    $IMPORT_STATS->names = array();
159    $IMPORT_STATS->skipped_names = array();
160    $IMPORT_STATS->count = count($vcards);
161    $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
162
163    if ($replace)
164      $CONTACTS->delete_all();
165
166    foreach ($vcards as $vcard) {
167      $email    = $vcard->email[0];
168      $a_record = $vcard->get_assoc();
169
170      // skip entries without an e-mail address or invalid
171      if (empty($email) || !$CONTACTS->validate($a_record, true)) {
172        $IMPORT_STATS->nomail++;
173        continue;
174      }
175
176      // We're using UTF8 internally
177      $email = rcube_idn_to_utf8($email);
178
179      if (!$replace && $email) {
180        // compare e-mail address
181        $existing = $CONTACTS->search('email', $email, 1, false);
182        if (!$existing->count && $vcard->displayname) {  // compare display name
183          $existing = $CONTACTS->search('name', $vcard->displayname, 1, false);
184        }
185        if ($existing->count) {
186          $IMPORT_STATS->skipped++;
187          $IMPORT_STATS->skipped_names[] = $vcard->displayname ? $vcard->displayname : $email;
188          continue;
189        }
190      }
191
192      $a_record['vcard'] = $vcard->export();
193
194      $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => null));
195      $a_record = $plugin['record'];
196
197      // insert record and send response
198      if (!$plugin['abort'])
199        $success = $CONTACTS->insert($a_record);
200      else
201        $success = $plugin['result'];
202
203      if ($success) {
204        $IMPORT_STATS->inserted++;
205        $IMPORT_STATS->names[] = $vcard->displayname ? $vcard->displayname : $email;
206      } else {
207        $IMPORT_STATS->errors++;
208      }
209    }
210
211    $importstep = 'rcmail_import_confirm';
212  }
213}
214else if ($err = $_FILES['_file']['error']) {
215  if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
216    $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
217  } else {
218    $OUTPUT->show_message('fileuploaderror', 'error');
219  }
220}
221
222
223$OUTPUT->set_pagetitle(rcube_label('importcontacts'));
224
225$OUTPUT->add_handlers(array(
226  'importstep' => $importstep,
227  'importnav' => 'rcmail_import_buttons',
228));
229
230// render page
231$OUTPUT->send('importcontacts');
Note: See TracBrowser for help on using the repository browser.