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