Aleksander Machniak
2014-05-12 2b72ec46a761ded28c83ef2ac544c9406a874802
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/addcontact.inc                                     |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
c97625 8  | Copyright (C) 2005-2013, The Roundcube Dev Team                       |
7fe381 9  |                                                                       |
T 10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
4e17e6 13  |                                                                       |
T 14  | PURPOSE:                                                              |
15  |   Add the submitted contact to the users address book                 |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19  +-----------------------------------------------------------------------+
20 */
21
881217 22 // only process ajax requests
c97625 23 if (!$OUTPUT->ajax_call) {
AM 24     return;
25 }
881217 26
840b4d 27 // Get default addressbook
AM 28 $CONTACTS = $RCMAIL->get_address_book(-1, true);
f11541 29
c97625 30 if (!empty($_POST['_address']) && is_object($CONTACTS)) {
AM 31     $address = rcube_utils::get_input_value('_address', rcube_utils::INPUT_POST, true);
32     $contact_arr = rcube_mime::decode_address_list($address, 1, false);
6f0968 33
c97625 34     if (!empty($contact_arr[1]['mailto'])) {
AM 35         $contact = array(
36             'email' => $contact_arr[1]['mailto'],
37             'name'  => $contact_arr[1]['name'],
38         );
6f0968 39
c97625 40         // Validity checks
AM 41         if (empty($contact['email'])) {
42             $OUTPUT->show_message('errorsavingcontact', 'error');
43             $OUTPUT->send();
44         }
45
46         $email = rcube_utils::idn_to_ascii($contact['email']);
47         if (!rcube_utils::check_email($email, false)) {
48             $OUTPUT->show_message('emailformaterror', 'error', array('email' => $contact['email']));
49             $OUTPUT->send();
50         }
51
52         $contact['email'] = rcube_utils::idn_to_utf8($contact['email']);
53
54         $contact = $RCMAIL->plugins->exec_hook('contact_displayname', $contact);
55
56         if (empty($contact['firstname']) || empty($contact['surname'])) {
57             $contact['name'] = rcube_addressbook::compose_display_name($contact);
58         }
59
60         // validate contact record
61         if (!$CONTACTS->validate($contact, true)) {
62             $error = $CONTACTS->get_error();
63             // TODO: show dialog to complete record
64             // if ($error['type'] == rcube_addressbook::ERROR_VALIDATE) { }
65
66             $OUTPUT->show_message($error['message'] ? $error['message'] : 'errorsavingcontact', 'error');
67             $OUTPUT->send();
68         }
69
70         // check for existing contacts
71         $existing = $CONTACTS->search('email', $contact['email'], 1, false);
72
73         if ($done = $existing->count) {
74             $OUTPUT->show_message('contactexists', 'warning');
75         }
76         else {
77             $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null));
78             $contact = $plugin['record'];
79
80             $done = !$plugin['abort'] ? $CONTACTS->insert($contact) : $plugin['result'];
81
82             if ($done) {
83                 $OUTPUT->show_message('addedsuccessfully', 'confirmation');
84             }
85         }
6f0968 86     }
f11541 87 }
4e17e6 88
c97625 89 if (!$done) {
AM 90     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsavingcontact', 'error');
91 }
4e17e6 92
f11541 93 $OUTPUT->send();