]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/addcontact.inc
Fix symlink mess
[roundcube.git] / program / steps / mail / addcontact.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/addcontact.inc                                     |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
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: addcontact.inc 5873 2012-02-11 13:50:04Z thomasb $
19
20 */
21
22 // only process ajax requests
23 if (!$OUTPUT->ajax_call)
24   return;
25
26 $abook = $RCMAIL->config->get('default_addressbook');
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)
33 if ($abook == null || !is_object($CONTACTS)) {
34   $source = reset($RCMAIL->get_address_sources(true));
35   $CONTACTS = $RCMAIL->get_address_book($source['id'], true);
36 }
37
38 if (!empty($_POST['_address']) && is_object($CONTACTS))
39 {
40   $contact_arr = $IMAP->decode_address_list(get_input_value('_address', RCUBE_INPUT_POST, true), 1, false);
41
42   if (!empty($contact_arr[1]['mailto'])) {
43     $contact = array(
44       'email' => $contact_arr[1]['mailto'],
45       'name' => $contact_arr[1]['name']
46     );
47
48     // Validity checks
49     if (empty($contact['email'])) {
50       $OUTPUT->show_message('errorsavingcontact', 'error');
51       $OUTPUT->send();
52     }
53
54     $email = rcube_idn_to_ascii($contact['email']);
55     if (!check_email($email, false)) {
56       $OUTPUT->show_message('emailformaterror', 'error', array('email' => $contact['email']));
57       $OUTPUT->send();
58     }
59
60     $contact['email'] = rcube_idn_to_utf8($contact['email']);
61     $contact = $RCMAIL->plugins->exec_hook('contact_displayname', $contact);
62
63     if (empty($contact['firstname']) || empty($contact['surname']))
64       $contact['name'] = rcube_addressbook::compose_display_name($contact);
65
66     // validate contact record
67     if (!$CONTACTS->validate($contact, true)) {
68       $error = $CONTACTS->get_error();
69       // TODO: show dialog to complete record
70       // if ($error['type'] == rcube_addressbook::ERROR_VALIDATE) { }
71
72       $OUTPUT->show_message($error['message'] ? $error['message'] : 'errorsavingcontact', 'error');
73       $OUTPUT->send();
74     }
75
76     // check for existing contacts
77     $existing = $CONTACTS->search('email', $contact['email'], 1, false);
78
79     if ($done = $existing->count)
80       $OUTPUT->show_message('contactexists', 'warning');
81     else {
82       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null));
83       $contact = $plugin['record'];
84
85       $done = !$plugin['abort'] ? $CONTACTS->insert($contact) : $plugin['result'];
86
87       if ($done)
88         $OUTPUT->show_message('addedsuccessfully', 'confirmation');
89     }
90   }
91 }
92
93 if (!$done)
94   $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsavingcontact', 'error');
95
96 $OUTPUT->send();
97