0
|
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-2013, 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
|
|
23 /** The import process **/
|
|
24
|
|
25 $importstep = 'rcmail_import_form';
|
|
26
|
|
27 if (is_array($_FILES['_file'])) {
|
|
28 $replace = (bool)rcube_utils::get_input_value('_replace', rcube_utils::INPUT_GPC);
|
|
29 $target = rcube_utils::get_input_value('_target', rcube_utils::INPUT_GPC);
|
|
30 $with_groups = intval(rcube_utils::get_input_value('_groups', rcube_utils::INPUT_GPC));
|
|
31
|
|
32 $vcards = array();
|
|
33 $upload_error = null;
|
|
34
|
|
35 $CONTACTS = $RCMAIL->get_address_book($target, true);
|
|
36
|
|
37 if (!$CONTACTS->groups) {
|
|
38 $with_groups = false;
|
|
39 }
|
|
40
|
|
41 if ($CONTACTS->readonly) {
|
|
42 $OUTPUT->show_message('addresswriterror', 'error');
|
|
43 }
|
|
44 else {
|
|
45 foreach ((array)$_FILES['_file']['tmp_name'] as $i => $filepath) {
|
|
46 // Process uploaded file if there is no error
|
|
47 $err = $_FILES['_file']['error'][$i];
|
|
48
|
|
49 if ($err) {
|
|
50 $upload_error = $err;
|
|
51 }
|
|
52 else {
|
|
53 $file_content = file_get_contents($filepath);
|
|
54
|
|
55 // let rcube_vcard do the hard work :-)
|
|
56 $vcard_o = new rcube_vcard();
|
|
57 $vcard_o->extend_fieldmap($CONTACTS->vcard_map);
|
|
58 $v_list = $vcard_o->import($file_content);
|
|
59
|
|
60 if (!empty($v_list)) {
|
|
61 $vcards = array_merge($vcards, $v_list);
|
|
62 continue;
|
|
63 }
|
|
64
|
|
65 // no vCards found, try CSV
|
|
66 $csv = new rcube_csv2vcard($_SESSION['language']);
|
|
67 $csv->import($file_content);
|
|
68 $v_list = $csv->export();
|
|
69
|
|
70 if (!empty($v_list)) {
|
|
71 $vcards = array_merge($vcards, $v_list);
|
|
72 }
|
|
73 }
|
|
74 }
|
|
75 }
|
|
76
|
|
77 // no vcards detected
|
|
78 if (!count($vcards)) {
|
|
79 if ($upload_error == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
|
|
80 $size = $RCMAIL->show_bytes(rcube_utils::max_upload_size());
|
|
81 $OUTPUT->show_message('filesizeerror', 'error', array('size' => $size));
|
|
82 }
|
|
83 else if ($upload_error) {
|
|
84 $OUTPUT->show_message('fileuploaderror', 'error');
|
|
85 }
|
|
86 else {
|
|
87 $OUTPUT->show_message('importformaterror', 'error');
|
|
88 }
|
|
89 }
|
|
90 else {
|
|
91 $IMPORT_STATS = new stdClass;
|
|
92 $IMPORT_STATS->names = array();
|
|
93 $IMPORT_STATS->skipped_names = array();
|
|
94 $IMPORT_STATS->count = count($vcards);
|
|
95 $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->invalid = $IMPORT_STATS->errors = 0;
|
|
96
|
|
97 if ($replace) {
|
|
98 $CONTACTS->delete_all($CONTACTS->groups && $with_groups < 2);
|
|
99 }
|
|
100
|
|
101 if ($with_groups) {
|
|
102 $import_groups = $CONTACTS->list_groups();
|
|
103 }
|
|
104
|
|
105 foreach ($vcards as $vcard) {
|
|
106 $a_record = $vcard->get_assoc();
|
|
107
|
|
108 // Generate contact's display name (must be before validation), the same we do in save.inc
|
|
109 if (empty($a_record['name'])) {
|
|
110 $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true);
|
|
111 // Reset it if equals to email address (from compose_display_name())
|
|
112 if ($a_record['name'] == $a_record['email'][0]) {
|
|
113 $a_record['name'] = '';
|
|
114 }
|
|
115 }
|
|
116
|
|
117 // skip invalid (incomplete) entries
|
|
118 if (!$CONTACTS->validate($a_record, true)) {
|
|
119 $IMPORT_STATS->invalid++;
|
|
120 continue;
|
|
121 }
|
|
122
|
|
123 // We're using UTF8 internally
|
|
124 $email = $vcard->email[0];
|
|
125 $email = rcube_utils::idn_to_utf8($email);
|
|
126
|
|
127 if (!$replace) {
|
|
128 $existing = null;
|
|
129 // compare e-mail address
|
|
130 if ($email) {
|
|
131 $existing = $CONTACTS->search('email', $email, 1, false);
|
|
132 }
|
|
133 // compare display name if email not found
|
|
134 if ((!$existing || !$existing->count) && $vcard->displayname) {
|
|
135 $existing = $CONTACTS->search('name', $vcard->displayname, 1, false);
|
|
136 }
|
|
137 if ($existing && $existing->count) {
|
|
138 $IMPORT_STATS->skipped++;
|
|
139 $IMPORT_STATS->skipped_names[] = $vcard->displayname ?: $email;
|
|
140 continue;
|
|
141 }
|
|
142 }
|
|
143
|
|
144 $a_record['vcard'] = $vcard->export();
|
|
145
|
|
146 $plugin = $RCMAIL->plugins->exec_hook('contact_create',
|
|
147 array('record' => $a_record, 'source' => null));
|
|
148 $a_record = $plugin['record'];
|
|
149
|
|
150 // insert record and send response
|
|
151 if (!$plugin['abort'])
|
|
152 $success = $CONTACTS->insert($a_record);
|
|
153 else
|
|
154 $success = $plugin['result'];
|
|
155
|
|
156 if ($success) {
|
|
157 // assign groups for this contact (if enabled)
|
|
158 if ($with_groups && !empty($a_record['groups'])) {
|
|
159 foreach (explode(',', $a_record['groups'][0]) as $group_name) {
|
|
160 if ($group_id = rcmail_import_group_id($group_name, $CONTACTS, $with_groups == 1, $import_groups)) {
|
|
161 $CONTACTS->add_to_group($group_id, $success);
|
|
162 }
|
|
163 }
|
|
164 }
|
|
165
|
|
166 $IMPORT_STATS->inserted++;
|
|
167 $IMPORT_STATS->names[] = $a_record['name'] ?: $email;
|
|
168 }
|
|
169 else {
|
|
170 $IMPORT_STATS->errors++;
|
|
171 }
|
|
172 }
|
|
173
|
|
174 $importstep = 'rcmail_import_confirm';
|
|
175 }
|
|
176 }
|
|
177
|
|
178
|
|
179 $OUTPUT->set_pagetitle($RCMAIL->gettext('importcontacts'));
|
|
180
|
|
181 $OUTPUT->add_handlers(array(
|
|
182 'importstep' => $importstep,
|
|
183 'importnav' => 'rcmail_import_buttons',
|
|
184 ));
|
|
185
|
|
186 // render page
|
|
187 $OUTPUT->send('importcontacts');
|
|
188
|
|
189
|
|
190
|
|
191 /**
|
|
192 * Handler function to display the import/upload form
|
|
193 */
|
|
194 function rcmail_import_form($attrib)
|
|
195 {
|
|
196 global $RCMAIL, $OUTPUT;
|
|
197
|
|
198 $target = rcube_utils::get_input_value('_target', rcube_utils::INPUT_GPC);
|
|
199
|
|
200 $attrib += array('id' => "rcmImportForm");
|
|
201
|
|
202 $writable_books = $RCMAIL->get_address_sources(true, true);
|
|
203
|
|
204 $upload = new html_inputfield(array(
|
|
205 'type' => 'file',
|
|
206 'name' => '_file[]',
|
|
207 'id' => 'rcmimportfile',
|
|
208 'size' => 40,
|
|
209 'multiple' => 'multiple',
|
|
210 ));
|
|
211 $form = html::p(null, html::label('rcmimportfile', $RCMAIL->gettext('importfromfile')) . $upload->show());
|
|
212 $table = new html_table(array('cols' => 2));
|
|
213
|
|
214 // addressbook selector
|
|
215 if (count($writable_books) > 1) {
|
|
216 $select = new html_select(array('name' => '_target', 'id' => 'rcmimporttarget', 'is_escaped' => true));
|
|
217
|
|
218 foreach ($writable_books as $book) {
|
|
219 $select->add($book['name'], $book['id']);
|
|
220 }
|
|
221
|
|
222 $table->add('title', html::label('rcmimporttarget', $RCMAIL->gettext('importtarget')));
|
|
223 $table->add(null, $select->show($target));
|
|
224 }
|
|
225 else {
|
|
226 $abook = new html_hiddenfield(array('name' => '_target', 'value' => key($writable_books)));
|
|
227 $form .= $abook->show();
|
|
228 }
|
|
229
|
|
230 // selector for group import options
|
|
231 if (count($writable_books) >= 1 || $writable_books[0]->groups) {
|
|
232 $select = new html_select(array('name' => '_groups', 'id' => 'rcmimportgroups', 'is_escaped' => true));
|
|
233 $select->add($RCMAIL->gettext('none'), '0');
|
|
234 $select->add($RCMAIL->gettext('importgroupsall'), '1');
|
|
235 $select->add($RCMAIL->gettext('importgroupsexisting'), '2');
|
|
236
|
|
237 $table->add('title', html::label('rcmimportgroups', $RCMAIL->gettext('importgroups')));
|
|
238 $table->add(null, $select->show(rcube_utils::get_input_value('_groups', rcube_utils::INPUT_GPC)));
|
|
239 }
|
|
240
|
|
241 // checkbox to replace the entire address book
|
|
242 $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
|
|
243 $table->add('title', html::label('rcmimportreplace', $RCMAIL->gettext('importreplace')));
|
|
244 $table->add(null, $check_replace->show(rcube_utils::get_input_value('_replace', rcube_utils::INPUT_GPC)));
|
|
245
|
|
246 $form .= $table->show(array('id' => null) + $attrib);
|
|
247
|
|
248 $OUTPUT->set_env('writable_source', !empty($writable_books));
|
|
249 $OUTPUT->add_label('selectimportfile','importwait');
|
|
250 $OUTPUT->add_gui_object('importform', $attrib['id']);
|
|
251
|
|
252 $out = html::p(null, rcube::Q($RCMAIL->gettext('importdesc'), 'show'))
|
|
253 . $OUTPUT->form_tag(array(
|
|
254 'action' => $RCMAIL->url('import'),
|
|
255 'method' => 'post',
|
|
256 'enctype' => 'multipart/form-data') + $attrib,
|
|
257 $form);
|
|
258
|
|
259 return $out;
|
|
260 }
|
|
261
|
|
262 /**
|
|
263 * Render the confirmation page for the import process
|
|
264 */
|
|
265 function rcmail_import_confirm($attrib)
|
|
266 {
|
|
267 global $IMPORT_STATS, $RCMAIL;
|
|
268
|
|
269 $vars = get_object_vars($IMPORT_STATS);
|
|
270 $vars['names'] = $vars['skipped_names'] = '';
|
|
271
|
|
272 $content = html::p(null, $RCMAIL->gettext(array(
|
|
273 'name' => 'importconfirm',
|
|
274 'nr' => $IMPORT_STATS->inserted,
|
|
275 'vars' => $vars,
|
|
276 )) . ($IMPORT_STATS->names ? ':' : '.'));
|
|
277
|
|
278 if ($IMPORT_STATS->names) {
|
|
279 $content .= html::p('em', join(', ', array_map(array('rcube', 'Q'), $IMPORT_STATS->names)));
|
|
280 }
|
|
281
|
|
282 if ($IMPORT_STATS->skipped) {
|
|
283 $content .= html::p(null, $RCMAIL->gettext(array(
|
|
284 'name' => 'importconfirmskipped',
|
|
285 'nr' => $IMPORT_STATS->skipped,
|
|
286 'vars' => $vars,
|
|
287 )) . ':')
|
|
288 . html::p('em', join(', ', array_map(array('rcube', 'Q'), $IMPORT_STATS->skipped_names)));
|
|
289 }
|
|
290
|
|
291 return html::div($attrib, $content);
|
|
292 }
|
|
293
|
|
294 /**
|
|
295 * Create navigation buttons for the current import step
|
|
296 */
|
|
297 function rcmail_import_buttons($attrib)
|
|
298 {
|
|
299 global $IMPORT_STATS, $OUTPUT;
|
|
300
|
|
301 $target = rcube_utils::get_input_value('_target', rcube_utils::INPUT_GPC);
|
|
302
|
|
303 $attrib += array('type' => 'input');
|
|
304 unset($attrib['name']);
|
|
305
|
|
306 if (is_object($IMPORT_STATS)) {
|
|
307 $attrib['class'] = trim($attrib['class'] . ' mainaction');
|
|
308 $out = $OUTPUT->button(array('command' => 'list', 'prop' => $target, 'label' => 'done') + $attrib);
|
|
309 }
|
|
310 else {
|
|
311 $cancel = $OUTPUT->button(array('command' => 'list', 'label' => 'cancel') + $attrib);
|
|
312 $attrib['class'] = trim($attrib['class'] . ' mainaction');
|
|
313 $out = $OUTPUT->button(array('command' => 'import', 'label' => 'import') + $attrib);
|
|
314 $out .= ' ';
|
|
315 $out .= $cancel;
|
|
316 }
|
|
317
|
|
318 return $out;
|
|
319 }
|
|
320
|
|
321 /**
|
|
322 * Returns the matching group id. If group doesn't exist, it'll be created if allowed.
|
|
323 */
|
|
324 function rcmail_import_group_id($group_name, $CONTACTS, $create, &$import_groups)
|
|
325 {
|
|
326 $group_id = 0;
|
|
327 foreach ($import_groups as $group) {
|
|
328 if (strtolower($group['name']) == strtolower($group_name)) {
|
|
329 $group_id = $group['ID'];
|
|
330 break;
|
|
331 }
|
|
332 }
|
|
333
|
|
334 // create a new group
|
|
335 if (!$group_id && $create) {
|
|
336 $new_group = $CONTACTS->create_group($group_name);
|
|
337 if (!$new_group['ID'])
|
|
338 $new_group['ID'] = $new_group['id'];
|
|
339 $import_groups[] = $new_group;
|
|
340 $group_id = $new_group['ID'];
|
|
341 }
|
|
342
|
|
343 return $group_id;
|
|
344 }
|