]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/addcontact.inc
bdd93ecff9c5dfe2ead1c8230aec64abc1f7deda
[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, Roundcube Dev. - Switzerland                 |
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 4509 2011-02-09 10:51:50Z thomasb $
19
20 */
21
22 // only process ajax requests
23 if (!$OUTPUT->ajax_call)
24   return;
25
26 $done = false;
27 $CONTACTS = $RCMAIL->get_address_book(null, true);
28
29 if (!empty($_POST['_address']) && is_object($CONTACTS))
30 {
31   $contact_arr = $IMAP->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     else if (!check_email($contact['email'], false)) {
45       $OUTPUT->show_message('emailformaterror', 'error', array('email' => $contact['email']));
46       $OUTPUT->send();
47     }
48
49     $contact['email'] = rcube_idn_to_utf8($contact['email']);
50
51     // use email address part for name
52     if (empty($contact['name']) || $contact['name'] == $contact['email'])
53       $contact['name'] = ucfirst(preg_replace('/[\.\-]/', ' ', substr($contact['email'], 0, strpos($contact['email'], '@'))));
54
55     // check for existing contacts
56     $existing = $CONTACTS->search('email', $contact['email'], true, false);
57
58     if ($done = $existing->count)
59       $OUTPUT->show_message('contactexists', 'warning');
60     else {
61       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null));
62       $contact = $plugin['record'];
63
64       $done = !$plugin['abort'] ? $CONTACTS->insert($contact) : $plugin['result'];
65
66       if ($done)
67         $OUTPUT->show_message('addedsuccessfully', 'confirmation');
68     }
69   }
70 }
71
72 if (!$done)
73   $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsavingcontact', 'error');
74
75 $OUTPUT->send();
76