alecpl
2012-01-05 1c4f23d6e58e12f93d8de2c3ae416df575e8ad85
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                     |
f5e7b3 8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 11  | PURPOSE:                                                              |
12  |   Add the submitted contact to the users address book                 |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
881217 22 // only process ajax requests
T 23 if (!$OUTPUT->ajax_call)
24   return;
25
d73448 26 $abook = $RCMAIL->config->get('default_addressbook');
A 27
28 // Get configured addressbook
29 $CONTACTS = $RCMAIL->get_address_book($abook, true);
30
31 // Get first writeable addressbook if the configured doesn't exist
32 // This can happen when user deleted the addressbook (e.g. Kolab folder)
3a9f79 33 if ($abook == null || !is_object($CONTACTS)) {
T 34   $source = reset($RCMAIL->get_address_sources(true));
35   $CONTACTS = $RCMAIL->get_address_book($source['id'], true);
d73448 36 }
f11541 37
ade8e1 38 if (!empty($_POST['_address']) && is_object($CONTACTS))
f11541 39 {
1c4f23 40   $contact_arr = rcube_mime::decode_address_list(get_input_value('_address', RCUBE_INPUT_POST, true), 1, false);
6f0968 41
ce92ba 42   if (!empty($contact_arr[1]['mailto'])) {
f11541 43     $contact = array(
T 44       'email' => $contact_arr[1]['mailto'],
45       'name' => $contact_arr[1]['name']
46     );
6f0968 47
A 48     // Validity checks
49     if (empty($contact['email'])) {
50       $OUTPUT->show_message('errorsavingcontact', 'error');
51       $OUTPUT->send();
52     }
f21a04 53
e18d99 54     $email = rcube_idn_to_ascii($contact['email']);
A 55     if (!check_email($email, false)) {
6f0968 56       $OUTPUT->show_message('emailformaterror', 'error', array('email' => $contact['email']));
A 57       $OUTPUT->send();
58     }
e99991 59
e8d5bd 60     $contact['email'] = rcube_idn_to_utf8($contact['email']);
e84818 61     $contact['name'] = rcube_addressbook::compose_display_name($contact);
4e17e6 62
39cafa 63     // validate contact record
T 64     if (!$CONTACTS->validate($contact, true)) {
65       $error = $CONTACTS->get_error();
66       // TODO: show dialog to complete record
67       // if ($error['type'] == rcube_addressbook::ERROR_VALIDATE) { }
f21a04 68
39cafa 69       $OUTPUT->show_message($error['message'] ? $error['message'] : 'errorsavingcontact', 'error');
T 70       $OUTPUT->send();
71     }
72
f11541 73     // check for existing contacts
f21a04 74     $existing = $CONTACTS->search('email', $contact['email'], 1, false);
ce92ba 75
f11541 76     if ($done = $existing->count)
T 77       $OUTPUT->show_message('contactexists', 'warning');
ce92ba 78     else {
e6ce00 79       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null));
3ddbe6 80       $contact = $plugin['record'];
A 81
ce92ba 82       $done = !$plugin['abort'] ? $CONTACTS->insert($contact) : $plugin['result'];
A 83
84       if ($done)
3ddbe6 85         $OUTPUT->show_message('addedsuccessfully', 'confirmation');
A 86     }
4e17e6 87   }
f11541 88 }
4e17e6 89
f11541 90 if (!$done)
6f0968 91   $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsavingcontact', 'error');
4e17e6 92
f11541 93 $OUTPUT->send();
b25dfd 94