Aleksander Machniak
2013-12-04 18b40c1a3214518764e99f69b581bd7c90426091
program/steps/addressbook/import.inc
@@ -30,7 +30,7 @@
  $attrib += array('id' => "rcmImportForm");
  $writable_books = $RCMAIL->get_address_sources(true);
  $writable_books = $RCMAIL->get_address_sources(true, true);
  $upload = new html_inputfield(array(
    'type' => 'file',
@@ -40,31 +40,46 @@
    'multiple' => 'multiple',
  ));
  $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . $upload->show());
  $table = new html_table(array('cols' => 2));
  // addressbook selector
  if (count($writable_books) > 1) {
    $select = new html_select(array('name' => '_target', 'id' => 'rcmimporttarget'));
    $select = new html_select(array('name' => '_target', 'id' => 'rcmimporttarget', 'is_escaped' => true));
    foreach ($writable_books as $book)
        $select->add($book['name'], $book['id']);
    $form .= html::p(null, html::label('rcmimporttarget', rcube_label('importtarget'))
        . $select->show($target));
    $table->add('title', html::label('rcmimporttarget', rcube_label('importtarget')));
    $table->add(null, $select->show($target));
  }
  else {
    $abook = new html_hiddenfield(array('name' => '_target', 'value' => key($writable_books)));
    $form .= $abook->show();
  }
  // selector for group import options
  if (count($writable_books) >= 1 || $writable_books[0]->groups) {
    $select = new html_select(array('name' => '_groups', 'id' => 'rcmimportgroups', 'is_escaped' => true));
    $select->add(rcube_label('none'), '0');
    $select->add(rcube_label('importgroupsall'), '1');
    $select->add(rcube_label('importgroupsexisting'), '2');
    $table->add('title', html::label('rcmimportgroups', rcube_label('importgroups')));
    $table->add(null, $select->show(get_input_value('_groups', RCUBE_INPUT_GPC)));
  }
  // checkbox to replace the entire address book
  $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
  $form .= html::p(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)) .
    html::label('rcmimportreplace', rcube_label('importreplace')));
  $table->add('title', html::label('rcmimportreplace', rcube_label('importreplace')));
  $table->add(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)));
  $form .= $table->show(array('id' => null) + $attrib);
  $OUTPUT->set_env('writable_source', !empty($writable_books));
  $OUTPUT->add_label('selectimportfile','importwait');
  $OUTPUT->add_gui_object('importform', $attrib['id']);
  $out = html::p(null, Q(rcube_label('importtext'), 'show'));
  $out = html::p(null, Q(rcube_label('importdesc'), 'show'));
  $out .= $OUTPUT->form_tag(array(
      'action' => $RCMAIL->url('import'),
@@ -88,7 +103,7 @@
  $content = html::p(null, rcube_label(array(
      'name' => 'importconfirm',
      'nr' => $IMORT_STATS->inserted,
      'nr' => $IMPORT_STATS->inserted,
      'vars' => $vars,
    )) . ($IMPORT_STATS->names ? ':' : '.'));
@@ -98,7 +113,7 @@
  if ($IMPORT_STATS->skipped) {
      $content .= html::p(null, rcube_label(array(
          'name' => 'importconfirmskipped',
          'nr' => $IMORT_STATS->skipped,
          'nr' => $IMPORT_STATS->skipped,
          'vars' => $vars,
        )) . ':');
      $content .= html::p('em', join(', ', array_map('Q', $IMPORT_STATS->skipped_names)));
@@ -134,18 +149,49 @@
}
/**
 * Returns the matching group id. If group doesn't exist, it'll be created if allowed.
 */
function rcmail_import_group_id($group_name, $CONTACTS, $create, &$import_groups)
{
    $group_id = 0;
    foreach ($import_groups as $key => $group) {
        if (strtolower($group['name']) == strtolower($group_name)) {
            $group_id = $group['ID'];
            break;
        }
    }
    // create a new group
    if (!$group_id && $create) {
        $new_group = $CONTACTS->create_group($group_name);
        if (!$new_group['ID'])
            $new_group['ID'] = $new_group['id'];
        $import_groups[] = $new_group;
        $group_id = $new_group['ID'];
    }
    return $group_id;
}
/** The import process **/
$importstep = 'rcmail_import_form';
if (is_array($_FILES['_file'])) {
    $replace  = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
    $target   = get_input_value('_target', RCUBE_INPUT_GPC);
    $replace      = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
    $target       = get_input_value('_target', RCUBE_INPUT_GPC);
    $with_groups  = intval(get_input_value('_groups', RCUBE_INPUT_GPC));
    $vcards       = array();
    $upload_error = null;
    $CONTACTS = $RCMAIL->get_address_book($target, true);
    if (!$CONTACTS->groups) {
        $with_groups = false;
    }
    if ($CONTACTS->readonly) {
        $OUTPUT->show_message('addresswriterror', 'error');
@@ -159,11 +205,22 @@
                $upload_error = $err;
            }
            else {
                $file_content = file_get_contents($filepath);
                // let rcube_vcard do the hard work :-)
                $vcard_o = new rcube_vcard();
                $vcard_o->extend_fieldmap($CONTACTS->vcard_map);
                $v_list = $vcard_o->import($file_content);
                $v_list = $vcard_o->import(file_get_contents($filepath));
                if (!empty($v_list)) {
                    $vcards = array_merge($vcards, $v_list);
                    continue;
                }
                // no vCards found, try CSV
                $csv = new rcube_csv2vcard($_SESSION['language']);
                $csv->import($file_content);
                $v_list = $csv->export();
                if (!empty($v_list)) {
                    $vcards = array_merge($vcards, $v_list);
@@ -181,7 +238,7 @@
            $OUTPUT->show_message('fileuploaderror', 'error');
        }
        else {
            $OUTPUT->show_message('importerror', 'error');
            $OUTPUT->show_message('importformaterror', 'error');
        }
    }
    else {
@@ -189,32 +246,49 @@
        $IMPORT_STATS->names = array();
        $IMPORT_STATS->skipped_names = array();
        $IMPORT_STATS->count = count($vcards);
        $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
        $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->invalid = $IMPORT_STATS->errors = 0;
        if ($replace) {
            $CONTACTS->delete_all();
            $CONTACTS->delete_all($CONTACTS->groups && $with_groups < 2);
        }
        if ($with_groups) {
            $import_groups = $CONTACTS->list_groups();
        }
        foreach ($vcards as $vcard) {
            $email    = $vcard->email[0];
            $a_record = $vcard->get_assoc();
            // skip entries without an e-mail address or invalid
            if (empty($email) || !$CONTACTS->validate($a_record, true)) {
                $IMPORT_STATS->nomail++;
            // Generate contact's display name (must be before validation), the same we do in save.inc
            if (empty($a_record['name'])) {
                $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true);
                // Reset it if equals to email address (from compose_display_name())
                if ($a_record['name'] == $a_record['email'][0]) {
                    $a_record['name'] = '';
                }
            }
            // skip invalid (incomplete) entries
            if (!$CONTACTS->validate($a_record, true)) {
                $IMPORT_STATS->invalid++;
                continue;
            }
            // We're using UTF8 internally
            $email = $vcard->email[0];
            $email = rcube_idn_to_utf8($email);
            if (!$replace && $email) {
            if (!$replace) {
                $existing = null;
                // compare e-mail address
                $existing = $CONTACTS->search('email', $email, 1, false);
                if (!$existing->count && $vcard->displayname) {  // compare display name
                if ($email) {
                    $existing = $CONTACTS->search('email', $email, 1, false);
                }
                // compare display name if email not found
                if ((!$existing || !$existing->count) && $vcard->displayname) {
                    $existing = $CONTACTS->search('name', $vcard->displayname, 1, false);
                }
                if ($existing->count) {
                if ($existing && $existing->count) {
                    $IMPORT_STATS->skipped++;
                    $IMPORT_STATS->skipped_names[] = $vcard->displayname ? $vcard->displayname : $email;
                    continue;
@@ -234,8 +308,17 @@
                $success = $plugin['result'];
            if ($success) {
                // assign groups for this contact (if enabled)
                if ($with_groups && !empty($a_record['groups'])) {
                    foreach (explode(',', $a_record['groups'][0]) as $group_name) {
                        if ($group_id = rcmail_import_group_id($group_name, $CONTACTS, $with_groups == 1, $import_groups)) {
                            $CONTACTS->add_to_group($group_id, $success);
                        }
                    }
                }
                $IMPORT_STATS->inserted++;
                $IMPORT_STATS->names[] = $vcard->displayname ? $vcard->displayname : $email;
                $IMPORT_STATS->names[] = $a_record['name'] ? $a_record['name'] : $email;
            }
            else {
                $IMPORT_STATS->errors++;