| 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 | | | |
|---|
| 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. | |
|---|
| 13 | | | |
|---|
| 14 | | PURPOSE: | |
|---|
| 15 | | Add the submitted contact to the users address book | |
|---|
| 16 | | | |
|---|
| 17 | +-----------------------------------------------------------------------+ |
|---|
| 18 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 19 | +-----------------------------------------------------------------------+ |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | // only process ajax requests |
|---|
| 23 | if (!$OUTPUT->ajax_call) |
|---|
| 24 | return; |
|---|
| 25 | |
|---|
| 26 | // Get default addressbook |
|---|
| 27 | $CONTACTS = $RCMAIL->get_address_book(-1, true); |
|---|
| 28 | |
|---|
| 29 | if (!empty($_POST['_address']) && is_object($CONTACTS)) |
|---|
| 30 | { |
|---|
| 31 | $contact_arr = rcube_mime::decode_address_list(get_input_value('_address', RCUBE_INPUT_POST, true), 1, false); |
|---|
| 32 | |
|---|
| 33 | if (!empty($contact_arr[1]['mailto'])) { |
|---|
| 34 | $contact = array( |
|---|
| 35 | 'email' => $contact_arr[1]['mailto'], |
|---|
| 36 | 'name' => $contact_arr[1]['name'] |
|---|
| 37 | ); |
|---|
| 38 | |
|---|
| 39 | // Validity checks |
|---|
| 40 | if (empty($contact['email'])) { |
|---|
| 41 | $OUTPUT->show_message('errorsavingcontact', 'error'); |
|---|
| 42 | $OUTPUT->send(); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | $email = rcube_idn_to_ascii($contact['email']); |
|---|
| 46 | if (!check_email($email, false)) { |
|---|
| 47 | $OUTPUT->show_message('emailformaterror', 'error', array('email' => $contact['email'])); |
|---|
| 48 | $OUTPUT->send(); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | $contact['email'] = rcube_idn_to_utf8($contact['email']); |
|---|
| 52 | $contact = $RCMAIL->plugins->exec_hook('contact_displayname', $contact); |
|---|
| 53 | |
|---|
| 54 | if (empty($contact['firstname']) || empty($contact['surname'])) |
|---|
| 55 | $contact['name'] = rcube_addressbook::compose_display_name($contact); |
|---|
| 56 | |
|---|
| 57 | // validate contact record |
|---|
| 58 | if (!$CONTACTS->validate($contact, true)) { |
|---|
| 59 | $error = $CONTACTS->get_error(); |
|---|
| 60 | // TODO: show dialog to complete record |
|---|
| 61 | // if ($error['type'] == rcube_addressbook::ERROR_VALIDATE) { } |
|---|
| 62 | |
|---|
| 63 | $OUTPUT->show_message($error['message'] ? $error['message'] : 'errorsavingcontact', 'error'); |
|---|
| 64 | $OUTPUT->send(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | // check for existing contacts |
|---|
| 68 | $existing = $CONTACTS->search('email', $contact['email'], 1, false); |
|---|
| 69 | |
|---|
| 70 | if ($done = $existing->count) |
|---|
| 71 | $OUTPUT->show_message('contactexists', 'warning'); |
|---|
| 72 | else { |
|---|
| 73 | $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null)); |
|---|
| 74 | $contact = $plugin['record']; |
|---|
| 75 | |
|---|
| 76 | $done = !$plugin['abort'] ? $CONTACTS->insert($contact) : $plugin['result']; |
|---|
| 77 | |
|---|
| 78 | if ($done) |
|---|
| 79 | $OUTPUT->show_message('addedsuccessfully', 'confirmation'); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | if (!$done) |
|---|
| 85 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsavingcontact', 'error'); |
|---|
| 86 | |
|---|
| 87 | $OUTPUT->send(); |
|---|
| 88 | |
|---|