source: github/program/steps/addressbook/import.inc @ b25dfd0

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since b25dfd0 was b25dfd0, checked in by alecpl <alec@…>, 3 years ago
  • removed PHP closing tag
  • Property mode set to 100644
File size: 6.0 KB
Line 
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 */
25function 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 */
60function 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 */
78function 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
105if ($_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      if (!$replace) {
139        // compare e-mail address
140        $existing = $CONTACTS->search('email', $email, false, false);
141        if (!$existing->count) {  // compare display name
142          $existing = $CONTACTS->search('name', $vcard->displayname, false, false);
143        }
144        if ($existing->count) {
145          $IMPORT_STATS->skipped++;
146          continue;
147        }
148      }
149     
150      $a_record = array(
151        'name' => $vcard->displayname,
152        'firstname' => $vcard->firstname,
153        'surname' => $vcard->surname,
154        'email' => $email,
155        'vcard' => $vcard->export(),
156      );
157     
158      $plugin = $RCMAIL->plugins->exec_hook('create_contact', array('record' => $a_record, 'source' => null));
159      $a_record = $plugin['record'];
160
161      // insert record and send response
162      if (!$plugin['abort'] && ($success = $CONTACTS->insert($a_record))) {
163        $IMPORT_STATS->inserted++;
164        $IMPORT_STATS->names[] = $vcard->displayname;
165      } else {
166        $IMPORT_STATS->errors++;
167      }
168    }
169
170    $importstep = 'rcmail_import_confirm';
171  }
172}
173else if ($err = $_FILES['_file']['error']) {
174  if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
175    $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
176  } else {
177    $OUTPUT->show_message('fileuploaderror', 'error');
178  }
179}
180
181
182$OUTPUT->set_pagetitle(rcube_label('importcontacts'));
183
184$OUTPUT->add_handlers(array(
185  'importstep' => $importstep,
186  'importnav' => 'rcmail_import_buttons',
187));
188
189// render page
190$OUTPUT->send('importcontacts');
Note: See TracBrowser for help on using the repository browser.