summaryrefslogtreecommitdiff
path: root/program/steps/addressbook/import.inc
blob: 8140a85266e9a749c9c6717380d1b84f8194c730 (plain)
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
<?php

/*
 +-----------------------------------------------------------------------+
 | program/steps/addressbook/import.inc                                  |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Import contacts from a vCard or CSV file                            |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 +-----------------------------------------------------------------------+

 $Id: $

*/

/**
 * Handler function to display the import/upload form
 */
function rcmail_import_form($attrib)
{
  global $RCMAIL, $OUTPUT;
  
  $attrib += array('id' => "rcmImportForm");
  
  $upload = new html_inputfield(array('type' => 'file', 'name' => '_file', 'id' => 'rcmimportfile', 'size' => 40));
  $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . html::br() . $upload->show());
  
  $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')));
  
  $OUTPUT->add_label('selectimportfile','importwait');
  $OUTPUT->add_gui_object('importform', $attrib['id']);
  
  $out = html::p(null, Q(rcube_label('importtext'), 'show'));
  
  $out .= $OUTPUT->form_tag(array(
      'action' => $RCMAIL->url('import'),
      'method' => 'post',
      'enctype' => 'multipart/form-data') + $attrib,
    $form);
  
  return $out;
}


/**
 * Render the confirmation page for the import process
 */
function rcmail_import_confirm($attrib)
{
  global $IMPORT_STATS;
  
  $vars = get_object_vars($IMPORT_STATS);
  $vars['names'] = join(', ', array_map('Q', $IMPORT_STATS->names));
  
  return html::p($attrib, Q(rcube_label(array(
    'name' => 'importconfirm',
    'nr' => $IMORT_STATS->inserted,
    'vars' => $vars,
  )), 'show'));
}


/**
 * Create navigation buttons for the current import step
 */
function rcmail_import_buttons($attrib)
{
  global $IMPORT_STATS, $OUTPUT;
  
  $attrib += array('type' => "input");
  unset($attrib['name']);
  
  if (is_object($IMPORT_STATS)) {
    $attrib['class'] = trim($attrib['class'] . ' mainaction');
    $out = $OUTPUT->button(array('command' => "list", 'label' => "done") + $attrib);
  }
  else {
    $out = $OUTPUT->button(array('command' => "list", 'label' => "cancel") + $attrib);
    $out .= '&nbsp;';
    $attrib['class'] = trim($attrib['class'] . ' mainaction');
    $out .= $OUTPUT->button(array('command' => "import", 'label' => "import") + $attrib);
  }
  
  return $out;
}


/** The import process **/

$importstep = 'rcmail_import_form';

if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {

  $replace = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
  $CONTACTS = $RCMAIL->get_address_book(null, true);

  // let rcube_vcard do the hard work :-)
  $vcards = rcube_vcard::import(file_get_contents($_FILES['_file']['tmp_name']));

  // no vcards detected
  if (!count($vcards)) {
    $OUTPUT->show_message('importerror', 'error');
  }
  else if ($CONTACTS->readonly) {
    $OUTPUT->show_message('addresswriterror', 'error');
  }
  else {
    $IMPORT_STATS = new stdClass;
    $IMPORT_STATS->names = array();
    $IMPORT_STATS->count = count($vcards);
    $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
    
    if ($replace)
      $CONTACTS->delete_all();
    
    foreach ($vcards as $vcard) {
      $email = $vcard->email[0];
      
      // skip entries without an e-mail address
      if (empty($email)) {
        $IMPORT_STATS->nomail++;
        continue;
      }
      
      if (!$replace) {
        // compare e-mail address
        $existing = $CONTACTS->search('email', $email, false, false);
        if (!$existing->count) {  // compare display name
          $existing = $CONTACTS->search('name', $vcard->displayname, false, false);
        }
        if ($existing->count) {
          $IMPORT_STATS->skipped++;
          continue;
        }
      }
      
      $success = $CONTACTS->insert(array(
        'name' => $vcard->displayname,
        'firstname' => $vcard->firstname,
        'surname' => $vcard->surname,
        'email' => $email,
        'vcard' => $vcard->export(),
      ));
      
      if ($success) {
        $IMPORT_STATS->inserted++;
        $IMPORT_STATS->names[] = $vcard->displayname;
      }
      else {
        $IMPORT_STATS->errors++;
      }
    }

    $importstep = 'rcmail_import_confirm';
  }
}
else if ($err = $_FILES['_file']['error']) {
  if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
    $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
  }
  else {
    $OUTPUT->show_message('fileuploaderror', 'error');
  }
}


$OUTPUT->set_pagetitle(rcube_label('importcontacts'));

$OUTPUT->add_handlers(array(
  'importstep' => $importstep,
  'importnav' => 'rcmail_import_buttons',
));

// render page
$OUTPUT->send('importcontacts');

?>