]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/import.inc
7f979de825928042877605af7829cfd6f3ab1dc7
[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, Roundcube Dev. - Switzerland                 |
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  +-----------------------------------------------------------------------+
17
18  $Id: $
19
20 */
21
22 /**
23  * Handler function to display the import/upload form
24  */
25 function rcmail_import_form($attrib)
26 {
27   global $RCMAIL, $OUTPUT;
28   $target = get_input_value('_target', RCUBE_INPUT_GPC);
29   
30   $attrib += array('id' => "rcmImportForm");
31   
32   $abook = new html_hiddenfield(array('name' => '_target', 'value' => $target));
33   $form = $abook->show();
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')) . html::br() . $upload->show());
37   
38   $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
39   $form .= html::p(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)) .
40     html::label('rcmimportreplace', rcube_label('importreplace')));
41   
42   $OUTPUT->add_label('selectimportfile','importwait');
43   $OUTPUT->add_gui_object('importform', $attrib['id']);
44   
45   $out = html::p(null, Q(rcube_label('importtext'), 'show'));
46   
47   $out .= $OUTPUT->form_tag(array(
48       'action' => $RCMAIL->url('import'),
49       'method' => 'post',
50       'enctype' => 'multipart/form-data') + $attrib,
51     $form);
52   
53   return $out;
54 }
55
56
57 /**
58  * Render the confirmation page for the import process
59  */
60 function rcmail_import_confirm($attrib)
61 {
62   global $IMPORT_STATS;
63   
64   $vars = get_object_vars($IMPORT_STATS);
65   $vars['names'] = join(', ', array_map('Q', $IMPORT_STATS->names));
66   
67   return html::p($attrib, Q(rcube_label(array(
68     'name' => 'importconfirm',
69     'nr' => $IMORT_STATS->inserted,
70     'vars' => $vars,
71   )), 'show'));
72 }
73
74
75 /**
76  * Create navigation buttons for the current import step
77  */
78 function rcmail_import_buttons($attrib)
79 {
80   global $IMPORT_STATS, $OUTPUT;
81   $target = get_input_value('_target', RCUBE_INPUT_GPC);
82   
83   $attrib += array('type' => 'input');
84   unset($attrib['name']);
85   
86   if (is_object($IMPORT_STATS)) {
87     $attrib['class'] = trim($attrib['class'] . ' mainaction');
88     $out = $OUTPUT->button(array('command' => 'list', 'prop' => $target, 'label' => 'done') + $attrib);
89   }
90   else {
91     $out = $OUTPUT->button(array('command' => 'list', 'label' => 'cancel') + $attrib);
92     $out .= '&nbsp;';
93     $attrib['class'] = trim($attrib['class'] . ' mainaction');
94     $out .= $OUTPUT->button(array('command' => 'import', 'label' => 'import') + $attrib);
95   }
96   
97   return $out;
98 }
99
100
101 /** The import process **/
102
103 $importstep = 'rcmail_import_form';
104
105 if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
106   $replace = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
107   $target = get_input_value('_target', RCUBE_INPUT_GPC);
108   $CONTACTS = $RCMAIL->get_address_book($target, true);
109
110   // let rcube_vcard do the hard work :-)
111   $vcards = rcube_vcard::import(file_get_contents($_FILES['_file']['tmp_name']));
112
113   // no vcards detected
114   if (!count($vcards)) {
115     $OUTPUT->show_message('importerror', 'error');
116   }
117   else if ($CONTACTS->readonly) {
118     $OUTPUT->show_message('addresswriterror', 'error');
119   }
120   else {
121     $IMPORT_STATS = new stdClass;
122     $IMPORT_STATS->names = array();
123     $IMPORT_STATS->count = count($vcards);
124     $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
125     
126     if ($replace)
127       $CONTACTS->delete_all();
128     
129     foreach ($vcards as $vcard) {
130       $email = $vcard->email[0];
131       
132       // skip entries without an e-mail address
133       if (empty($email)) {
134         $IMPORT_STATS->nomail++;
135         continue;
136       }
137
138       // We're using UTF8 internally
139       $email = rcube_idn_to_utf8($email);
140       
141       if (!$replace) {
142         // compare e-mail address
143         $existing = $CONTACTS->search('email', $email, false, false);
144         if (!$existing->count) {  // compare display name
145           $existing = $CONTACTS->search('name', $vcard->displayname, false, false);
146         }
147         if ($existing->count) {
148           $IMPORT_STATS->skipped++;
149           continue;
150         }
151       }
152       
153       $a_record = array(
154         'name' => $vcard->displayname,
155         'firstname' => $vcard->firstname,
156         'surname' => $vcard->surname,
157         'email' => $email,
158         'vcard' => $vcard->export(),
159       );
160       
161       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => null));
162       $a_record = $plugin['record'];
163
164       // insert record and send response
165       if (!$plugin['abort'])
166         $success = $CONTACTS->insert($a_record);
167       else
168         $success = $plugin['result'];
169
170       if ($success) {
171         $IMPORT_STATS->inserted++;
172         $IMPORT_STATS->names[] = $vcard->displayname;
173       } else {
174         $IMPORT_STATS->errors++;
175       }
176     }
177
178     $importstep = 'rcmail_import_confirm';
179   }
180 }
181 else if ($err = $_FILES['_file']['error']) {
182   if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
183     $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
184   } else {
185     $OUTPUT->show_message('fileuploaderror', 'error');
186   }
187 }
188
189
190 $OUTPUT->set_pagetitle(rcube_label('importcontacts'));
191
192 $OUTPUT->add_handlers(array(
193   'importstep' => $importstep,
194   'importnav' => 'rcmail_import_buttons',
195 ));
196
197 // render page
198 $OUTPUT->send('importcontacts');