You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

addcontact.inc 3.6KB

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