import.php 19.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
<?php

/**
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 |                                                                       |
 | Copyright (C) The Roundcube Dev Team                                  |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for skins & plugins.                |
 | See the README file for a full license statement.                     |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Import contacts from a vCard or CSV file                            |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 | Author: Aleksander Machniak <machniak@kolabsys.com>                   |
 +-----------------------------------------------------------------------+
*/

class rcmail_action_contacts_import extends rcmail_action_contacts_index
{
    const UPLOAD_ERR_CSV_FIELDS = 101;

    protected static $stats;

    /**
     * Request handler.
     *
     * @param array $args Arguments from the previous step(s)
     */
    public function run($args = [])
    {
        $rcmail     = rcmail::get_instance();
        $importstep = 'import_form';
        $has_map    = isset($_POST['_map']) && is_array($_POST['_map']);

        if ($has_map || (isset($_FILES['_file']) && is_array($_FILES['_file']))) {
            $replace      = (bool)rcube_utils::get_input_value('_replace', rcube_utils::INPUT_GPC);
            $target       = rcube_utils::get_input_value('_target', rcube_utils::INPUT_GPC);
            $with_groups  = intval(rcube_utils::get_input_value('_groups', rcube_utils::INPUT_GPC));

            // reload params for CSV field mapping screen
            if ($has_map && !empty($_SESSION['contactcsvimport']['params'])) {
                $params = $_SESSION['contactcsvimport']['params'];

                $replace     = $params['replace'];
                $target      = $params['target'];
                $with_groups = $params['with_groups'];
            }

            $vcards       = [];
            $csvs         = [];
            $map          = [];
            $upload_error = null;

            $CONTACTS = $rcmail->get_address_book($target, true);

            if (!$CONTACTS->groups) {
                $with_groups = false;
            }

            if ($CONTACTS->readonly) {
                $rcmail->output->show_message('addresswriterror', 'error');
            }
            else {
                $filepaths = [];
                if ($has_map) {
                    $filepaths = $_SESSION['contactcsvimport']['files'];
                }
                else {
                    foreach ((array) $_FILES['_file']['tmp_name'] as $i => $filepath) {
                        // Process uploaded file if there is no error
                        $err = $_FILES['_file']['error'][$i];

                        if ($err) {
                            $upload_error = $err;
                        }
                        else {
                            $filepaths[] = $filepath;
                        }
                    }
                }

                foreach ($filepaths as $filepath) {
                    $file_content = file_get_contents($filepath);

                    // let rcube_vcard do the hard work :-)
                    $vcard_o = new rcube_vcard();
                    $vcard_o->extend_fieldmap($CONTACTS->vcard_map);
                    $v_list = $vcard_o->import($file_content);

                    if (!empty($v_list)) {
                        $vcards = array_merge($vcards, $v_list);
                        continue;
                    }

                    // no vCards found, try CSV
                    $csv = new rcube_csv2vcard($_SESSION['language']);

                    if ($has_map) {
                        $skip_head = isset($_POST['_skip_header']);
                        $map       = rcube_utils::get_input_value('_map', rcube_utils::INPUT_GPC);
                        $map       = array_filter($map);

                        $csv->set_map($map);
                        $csv->import($file_content, false, $skip_head);

                        unlink($filepath);
                    }
                    else {
                        // save uploaded file for the real import in the next step
                        $temp_csv = rcube_utils::temp_filename('csvimpt');
                        if (move_uploaded_file($filepath, $temp_csv) && file_exists($temp_csv)) {
                            $fields   = $csv->get_fields();
                            $last_map = $map;
                            $map = $csv->import($file_content, true);

                            // when multiple CSV files are uploaded check they all have the same structure
                            if ($last_map && $last_map !== $map) {
                                $csvs = [];
                                $upload_error = self::UPLOAD_ERR_CSV_FIELDS;
                                break;
                            }

                            $csvs[] = $temp_csv;
                        }
                        else {
                            $upload_error = UPLOAD_ERR_CANT_WRITE;
                        }

                        continue;
                    }

                    $v_list = $csv->export();

                    if (!empty($v_list)) {
                        $vcards = array_merge($vcards, $v_list);
                    }
                }
            }

            if (count($csvs) > 0) {
                // csv import, show field mapping options
                $importstep = 'import_map';

                $_SESSION['contactcsvimport']['files']  = $csvs;
                $_SESSION['contactcsvimport']['params'] = [
                    'replace'     => $replace,
                    'target'      => $target,
                    'with_groups' => $with_groups,
                    'fields'      => !empty($fields) ? $fields : [],
                ];

                // Stored separately due to nested array limitations in session
                $_SESSION['contactcsvimport']['map'] = $map;

                // Re-enable the import button
                $rcmail->output->command('parent.import_state_set', 'error');
            }
            elseif (count($vcards) > 0) {
                // import vcards
                self::$stats = new stdClass;
                self::$stats->names         = [];
                self::$stats->skipped_names = [];
                self::$stats->count         = count($vcards);
                self::$stats->inserted = 0;
                self::$stats->skipped  = 0;
                self::$stats->invalid  = 0;
                self::$stats->errors   = 0;

                if ($replace) {
                    $CONTACTS->delete_all($CONTACTS->groups && $with_groups < 2);
                }

                if ($with_groups) {
                    $import_groups = $CONTACTS->list_groups();
                }

                foreach ($vcards as $vcard) {
                    $a_record = $vcard->get_assoc();

                    // Generate contact's display name (must be before validation), the same we do in save.inc
                    if (empty($a_record['name'])) {
                        $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true);
                        // Reset it if equals to email address (from compose_display_name())
                        if ($a_record['name'] == $a_record['email'][0]) {
                            $a_record['name'] = '';
                        }
                    }

                    // skip invalid (incomplete) entries
                    if (!$CONTACTS->validate($a_record, true)) {
                        self::$stats->invalid++;
                        continue;
                    }

                    // We're using UTF8 internally
                    $email = null;
                    if (isset($vcard->email[0])) {
                        $email = $vcard->email[0];
                        $email = rcube_utils::idn_to_utf8($email);
                    }

                    if (!$replace) {
                        $existing = null;
                        // compare e-mail address
                        if ($email) {
                            $existing = $CONTACTS->search('email', $email, 1, false);
                        }
                        // compare display name if email not found
                        if ((!$existing || !$existing->count) && $vcard->displayname) {
                            $existing = $CONTACTS->search('name', $vcard->displayname, 1, false);
                        }
                        if ($existing && $existing->count) {
                            self::$stats->skipped++;
                            self::$stats->skipped_names[] = $vcard->displayname ?: $email;
                            continue;
                        }
                    }

                    $a_record['vcard'] = $vcard->export();

                    $plugin   = $rcmail->plugins->exec_hook('contact_create', ['record' => $a_record, 'source' => null]);
                    $a_record = $plugin['record'];

                    // insert record and send response
                    if (empty($plugin['abort'])) {
                        $success = $CONTACTS->insert($a_record);
                    }
                    else {
                        $success = $plugin['result'];
                    }

                    if ($success) {
                        // assign groups for this contact (if enabled)
                        if ($with_groups && !empty($a_record['groups'])) {
                            foreach (explode(',', $a_record['groups'][0]) as $group_name) {
                                if ($group_id = self::import_group_id($group_name, $CONTACTS, $with_groups == 1, $import_groups)) {
                                    $CONTACTS->add_to_group($group_id, $success);
                                }
                            }
                        }

                        self::$stats->inserted++;
                        self::$stats->names[] = $a_record['name'] ?: $email;
                    }
                    else {
                        self::$stats->errors++;
                    }
                }

                $importstep = 'import_confirm';
                $_SESSION['contactcsvimport'] = null;

                $rcmail->output->command('parent.import_state_set', self::$stats->inserted ? 'reload' : 'ok');
            }
            else {
                if ($upload_error == self::UPLOAD_ERR_CSV_FIELDS) {
                    $rcmail->output->show_message('csvfilemismatch', 'error');
                }
                else {
                    self::upload_error($upload_error);
                }

                $rcmail->output->command('parent.import_state_set', 'error');
            }
        }

        $rcmail->output->set_pagetitle($rcmail->gettext('importcontacts'));

        $rcmail->output->add_handlers([
                'importstep' => [$this, $importstep],
        ]);

        // render page
        if ($rcmail->output->template_exists('contactimport')) {
            $rcmail->output->send('contactimport');
        }
        else {
            $rcmail->output->send('importcontacts'); // deprecated
        }
    }

    /**
     * Handler function to display the import/upload form
     */
    public static function import_form($attrib)
    {
        $rcmail = rcmail::get_instance();
        $target = rcube_utils::get_input_value('_target', rcube_utils::INPUT_GPC);

        $attrib += ['id' => 'rcmImportForm'];

        $writable_books = $rcmail->get_address_sources(true, true);
        $max_filesize   = self::upload_init();

        $form   = '';
        $hint   = $rcmail->gettext(['id' => 'importfile', 'name' => 'maxuploadsize', 'vars' => ['size' => $max_filesize]]);
        $table  = new html_table(['cols' => 2]);
        $upload = new html_inputfield([
                'type'     => 'file',
                'name'     => '_file[]',
                'id'       => 'rcmimportfile',
                'size'     => 40,
                'multiple' => 'multiple',
                'class'    => 'form-control-file',
        ]);

        $table->add('title', html::label('rcmimportfile', $rcmail->gettext('importfromfile')));
        $table->add(null, $upload->show() . html::div('hint', $hint));

        // addressbook selector
        if (count($writable_books) > 1) {
            $select = new html_select([
                    'name'       => '_target',
                    'id'         => 'rcmimporttarget',
                    'is_escaped' => true,
                    'class'      => 'custom-select'
            ]);

            foreach ($writable_books as $book) {
                $select->add($book['name'], $book['id']);
            }

            $table->add('title', html::label('rcmimporttarget', $rcmail->gettext('importtarget')));
            $table->add(null, $select->show($target));
        }
        else {
            $abook = new html_hiddenfield(['name' => '_target', 'value' => key($writable_books)]);
            $form .= $abook->show();
        }

        $form .= html::tag('input', ['type' => 'hidden', 'name' => '_unlock', 'value' => '']);

        // selector for group import options
        if (count($writable_books) >= 1 || $writable_books[0]->groups) {
            $select = new html_select([
                    'name'       => '_groups',
                    'id'         => 'rcmimportgroups',
                    'is_escaped' => true,
                    'class'      => 'custom-select'
            ]);
            $select->add($rcmail->gettext('none'), '0');
            $select->add($rcmail->gettext('importgroupsall'), '1');
            $select->add($rcmail->gettext('importgroupsexisting'), '2');

            $table->add('title', html::label('rcmimportgroups', $rcmail->gettext('importgroups')));
            $table->add(null, $select->show(rcube_utils::get_input_value('_groups', rcube_utils::INPUT_GPC)));
        }

        // checkbox to replace the entire address book
        $check_replace = new html_checkbox(['name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace']);
        $table->add('title', html::label('rcmimportreplace', $rcmail->gettext('importreplace')));
        $table->add(null, $check_replace->show(rcube_utils::get_input_value('_replace', rcube_utils::INPUT_GPC)));

        $form .= $table->show(['id' => null] + $attrib);

        // remove any info left over info from previous import attempts
        $_SESSION['contactcsvimport'] = null;

        $rcmail->output->set_env('writable_source', !empty($writable_books));
        $rcmail->output->add_label('selectimportfile','importwait');
        $rcmail->output->add_gui_object('importform', $attrib['id']);

        $attrib = [
            'action'  => $rcmail->url('import'),
            'method'  => 'post',
            'enctype' => 'multipart/form-data'
        ] + $attrib;

        return html::p(null, rcube::Q($rcmail->gettext('importdesc'), 'show'))
            . $rcmail->output->form_tag($attrib, $form);
    }

    /**
     * Render the field mapping page for the CSV import process
     */
    public static function import_map($attrib)
    {
        $rcmail = rcmail::get_instance();
        $params = $_SESSION['contactcsvimport']['params'];

        // hide groups field from list when group import disabled
        if (empty($params['with_groups'])) {
            unset($params['fields']['groups']);
        }

        $fieldlist = new html_select(['name' => '_map[]']);
        $fieldlist->add($rcmail->gettext('fieldnotmapped'), '');
        foreach ($params['fields'] as $id => $name) {
            $fieldlist->add($name, $id);
        }

        $field_table = new html_table(['cols' => 2] + $attrib);

        if ($classes = $attrib['table-header-class']) {
            $field_table->set_header_attribs($classes);
        }

        $field_table->add_header($attrib['table-col-source-class'] ?: null, $rcmail->gettext('source'));
        $field_table->add_header($attrib['table-col-destination-class'] ?: null, $rcmail->gettext('destination'));

        $map = $_SESSION['contactcsvimport']['map'];
        foreach ($map['source'] as $i => $name) {
            $field_table->add('title', html::label('rcmimportmap' . $i, rcube::Q($name)));
            $field_table->add(null, $fieldlist->show(array_key_exists($i, $map['destination']) ? $map['destination'][$i] : '', ['id' => 'rcmimportmap' . $i]));
        }

        $form = '';
        $form .= html::tag('input', ['type' => 'hidden', 'name' => '_unlock', 'value' => '']);

        // show option to import data from first line of the file
        $check_header = new html_checkbox(['name' => '_skip_header', 'value' => 1, 'id' => 'rcmskipheader']);
        $form .= html::p(null, html::label('rcmskipheader', $check_header->show(1) . $rcmail->gettext('skipheader')));

        $form .= $field_table->show();

        $attrib = ['action' => $rcmail->url('import'), 'method' => 'post'] + $attrib + ['id' => 'rcmImportFormMap'];

        $rcmail->output->add_gui_object('importformmap', $attrib['id']);

        return html::p(null, rcube::Q($rcmail->gettext('importmapdesc'), 'show'))
            . $rcmail->output->form_tag($attrib, $form);
    }

    /**
     * Render the confirmation page for the import process
     */
    public static function import_confirm($attrib)
    {
        $rcmail = rcmail::get_instance();
        $vars   = get_object_vars(self::$stats);
        $vars['names'] = $vars['skipped_names'] = '';

        $content = html::p(null, $rcmail->gettext([
                'name' => 'importconfirm',
                'nr'   => self::$stats->inserted,
                'vars' => $vars,
            ]) . (self::$stats->names ? ':' : '.')
        );

        if (self::$stats->names) {
            $content .= html::p('em', join(', ', array_map(['rcube', 'Q'], self::$stats->names)));
        }

        if (self::$stats->skipped) {
            $content .= html::p(null, $rcmail->gettext([
                    'name' => 'importconfirmskipped',
                    'nr'   => self::$stats->skipped,
                    'vars' => $vars,
                ]) . ':')
                . html::p('em', join(', ', array_map(['rcube', 'Q'], self::$stats->skipped_names)));
        }

        return html::div($attrib, $content);
    }

    /**
     * Returns the matching group id. If group doesn't exist, it'll be created if allowed.
     */
    public static function import_group_id($group_name, $contacts, $create, &$import_groups)
    {
        $group_id = 0;
        foreach ($import_groups as $group) {
            if (strtolower($group['name']) === strtolower($group_name)) {
                $group_id = $group['ID'];
                break;
            }
        }

        // create a new group
        if (!$group_id && $create) {
            $new_group = $contacts->create_group($group_name);

            if (empty($new_group['ID'])) {
                $new_group['ID'] = $new_group['id'];
            }

            $import_groups[] = $new_group;
            $group_id        = $new_group['ID'];
        }

        return $group_id;
    }
}