]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/import.inc
1b9aea18a15c2689858500a8223a7af7d8e38200
[roundcube.git] / program / steps / addressbook / import.inc
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  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Import contacts from a vCard or CSV file                            |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  | Author: Aleksander Machniak <machniak@kolabsys.com>                   |
17  +-----------------------------------------------------------------------+
18
19  $Id$
20
21 */
22
23 /**
24  * Handler function to display the import/upload form
25  */
26 function rcmail_import_form($attrib)
27 {
28   global $RCMAIL, $OUTPUT;
29   $target = get_input_value('_target', RCUBE_INPUT_GPC);
30
31   $attrib += array('id' => "rcmImportForm");
32
33   $writable_books = $RCMAIL->get_address_sources(true);
34
35   $upload = new html_inputfield(array('type' => 'file', 'name' => '_file', 'id' => 'rcmimportfile', 'size' => 40));
36   $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . $upload->show());
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
53   $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
54   $form .= html::p(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)) .
55     html::label('rcmimportreplace', rcube_label('importreplace')));
56
57   $OUTPUT->set_env('writable_source', !empty($writable_books));
58   $OUTPUT->add_label('selectimportfile','importwait');
59   $OUTPUT->add_gui_object('importform', $attrib['id']);
60
61   $out = html::p(null, Q(rcube_label('importtext'), 'show'));
62
63   $out .= $OUTPUT->form_tag(array(
64       'action' => $RCMAIL->url('import'),
65       'method' => 'post',
66       'enctype' => 'multipart/form-data') + $attrib,
67     $form);
68
69   return $out;
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;
79
80   $vars = get_object_vars($IMPORT_STATS);
81   $vars['names'] = $vars['skipped_names'] = '';
82
83   $content = html::p(null, rcube_label(array(
84       'name' => 'importconfirm',
85       'nr' => $IMORT_STATS->inserted,
86       'vars' => $vars,
87     )) . ($IMPORT_STATS->names ? ':' : '.'));
88
89   if ($IMPORT_STATS->names)
90     $content .= html::p('em', join(', ', array_map('Q', $IMPORT_STATS->names)));
91
92   if ($IMPORT_STATS->skipped) {
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   }
100
101   return html::div($attrib, $content);
102 }
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;
111   $target = get_input_value('_target', RCUBE_INPUT_GPC);
112
113   $attrib += array('type' => 'input');
114   unset($attrib['name']);
115
116   if (is_object($IMPORT_STATS)) {
117     $attrib['class'] = trim($attrib['class'] . ' mainaction');
118     $out = $OUTPUT->button(array('command' => 'list', 'prop' => $target, 'label' => 'done') + $attrib);
119   }
120   else {
121     $out = $OUTPUT->button(array('command' => 'list', 'label' => 'cancel') + $attrib);
122     $out .= '&nbsp;';
123     $attrib['class'] = trim($attrib['class'] . ' mainaction');
124     $out .= $OUTPUT->button(array('command' => 'import', 'label' => 'import') + $attrib);
125   }
126
127   return $out;
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);
137   $target = get_input_value('_target', RCUBE_INPUT_GPC);
138   $CONTACTS = $RCMAIL->get_address_book($target, true);
139
140   // let rcube_vcard do the hard work :-)
141   $vcard_o = new rcube_vcard();
142   $vcard_o->extend_fieldmap($CONTACTS->vcard_map);
143
144   $vcards = $vcard_o->import(file_get_contents($_FILES['_file']['tmp_name']));
145
146   // no vcards detected
147   if (!count($vcards)) {
148     $OUTPUT->show_message('importerror', 'error');
149   }
150   else if ($CONTACTS->readonly) {
151     $OUTPUT->show_message('addresswriterror', 'error');
152   }
153   else {
154     $IMPORT_STATS = new stdClass;
155     $IMPORT_STATS->names = array();
156     $IMPORT_STATS->skipped_names = array();
157     $IMPORT_STATS->count = count($vcards);
158     $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
159
160     if ($replace)
161       $CONTACTS->delete_all();
162
163     foreach ($vcards as $vcard) {
164       $email = $vcard->email[0];
165
166       // skip entries without an e-mail address
167       if (empty($email)) {
168         $IMPORT_STATS->nomail++;
169         continue;
170       }
171
172       // We're using UTF8 internally
173       $email = rcube_idn_to_utf8($email);
174
175       if (!$replace && $email) {
176         // compare e-mail address
177         $existing = $CONTACTS->search('email', $email, false, false);
178         if (!$existing->count && $vcard->displayname) {  // compare display name
179           $existing = $CONTACTS->search('name', $vcard->displayname, false, false);
180         }
181         if ($existing->count) {
182           $IMPORT_STATS->skipped++;
183           $IMPORT_STATS->skipped_names[] = $vcard->displayname ? $vcard->displayname : $email;
184           continue;
185         }
186       }
187
188       $a_record = $vcard->get_assoc();
189       $a_record['vcard'] = $vcard->export();
190
191       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => null));
192       $a_record = $plugin['record'];
193
194       // insert record and send response
195       if (!$plugin['abort'])
196         $success = $CONTACTS->insert($a_record);
197       else
198         $success = $plugin['result'];
199
200       if ($success) {
201         $IMPORT_STATS->inserted++;
202         $IMPORT_STATS->names[] = $vcard->displayname ? $vcard->displayname : $email;
203       } else {
204         $IMPORT_STATS->errors++;
205       }
206     }
207
208     $importstep = 'rcmail_import_confirm';
209   }
210 }
211 else if ($err = $_FILES['_file']['error']) {
212   if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
213     $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
214   } else {
215     $OUTPUT->show_message('fileuploaderror', 'error');
216   }
217 }
218
219
220 $OUTPUT->set_pagetitle(rcube_label('importcontacts'));
221
222 $OUTPUT->add_handlers(array(
223   'importstep' => $importstep,
224   'importnav' => 'rcmail_import_buttons',
225 ));
226
227 // render page
228 $OUTPUT->send('importcontacts');