| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/steps/addressbook/save.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 | | Save a contact entry or to add a new one | |
|---|
| 13 | | | |
|---|
| 14 | +-----------------------------------------------------------------------+ |
|---|
| 15 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 16 | +-----------------------------------------------------------------------+ |
|---|
| 17 | |
|---|
| 18 | $Id$ |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | $cid = get_input_value('_cid', RCUBE_INPUT_POST); |
|---|
| 23 | $return_action = empty($cid) ? 'add' : 'edit'; |
|---|
| 24 | |
|---|
| 25 | // cannot edit record |
|---|
| 26 | if ($CONTACTS->readonly) { |
|---|
| 27 | $OUTPUT->show_message('contactreadonly', 'error'); |
|---|
| 28 | rcmail_overwrite_action($return_action); |
|---|
| 29 | return; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | // Basic input checks |
|---|
| 33 | if ((!get_input_value('_name', RCUBE_INPUT_POST) || !get_input_value('_email', RCUBE_INPUT_POST))) { |
|---|
| 34 | $OUTPUT->show_message('formincomplete', 'warning'); |
|---|
| 35 | rcmail_overwrite_action($return_action); |
|---|
| 36 | return; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | // setup some vars we need |
|---|
| 41 | $a_save_cols = array('name', 'firstname', 'surname', 'email'); |
|---|
| 42 | $a_record = array(); |
|---|
| 43 | |
|---|
| 44 | // read POST values into hash array |
|---|
| 45 | foreach ($a_save_cols as $col) { |
|---|
| 46 | $fname = '_'.$col; |
|---|
| 47 | if (isset($_POST[$fname])) |
|---|
| 48 | $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | // Validity checks |
|---|
| 52 | $_email = idn_to_ascii($a_record['email']); |
|---|
| 53 | if (!check_email($_email, false)) { |
|---|
| 54 | $OUTPUT->show_message('emailformaterror', 'warning', array('email' => $_email)); |
|---|
| 55 | rcmail_overwrite_action($return_action); |
|---|
| 56 | return; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | // update an existing contact |
|---|
| 60 | if (!empty($cid)) |
|---|
| 61 | { |
|---|
| 62 | $plugin = $RCMAIL->plugins->exec_hook('contact_update', |
|---|
| 63 | array('id' => $cid, 'record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC))); |
|---|
| 64 | $a_record = $plugin['record']; |
|---|
| 65 | |
|---|
| 66 | if (!$plugin['abort']) |
|---|
| 67 | $result = $CONTACTS->update($cid, $a_record); |
|---|
| 68 | else |
|---|
| 69 | $result = $plugin['result']; |
|---|
| 70 | |
|---|
| 71 | if ($result) { |
|---|
| 72 | // LDAP DN change |
|---|
| 73 | if (is_string($result) && strlen($result)>1) { |
|---|
| 74 | $newcid = $result; |
|---|
| 75 | // change cid in POST for 'show' action |
|---|
| 76 | $_POST['_cid'] = $newcid; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | // define list of cols to be displayed |
|---|
| 80 | $a_js_cols = array(); |
|---|
| 81 | $record = $CONTACTS->get_record($newcid ? $newcid : $cid, true); |
|---|
| 82 | |
|---|
| 83 | foreach (array('name', 'email') as $col) |
|---|
| 84 | $a_js_cols[] = (string)$record[$col]; |
|---|
| 85 | |
|---|
| 86 | // update the changed col in list |
|---|
| 87 | $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid); |
|---|
| 88 | |
|---|
| 89 | // show confirmation |
|---|
| 90 | $OUTPUT->show_message('successfullysaved', 'confirmation', null, false); |
|---|
| 91 | rcmail_overwrite_action('show'); |
|---|
| 92 | } |
|---|
| 93 | else { |
|---|
| 94 | // show error message |
|---|
| 95 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false); |
|---|
| 96 | rcmail_overwrite_action('show'); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | // insert a new contact |
|---|
| 101 | else { |
|---|
| 102 | // check for existing contacts |
|---|
| 103 | $existing = $CONTACTS->search('email', $a_record['email'], true, false); |
|---|
| 104 | |
|---|
| 105 | // show warning message |
|---|
| 106 | if ($existing->count) { |
|---|
| 107 | $OUTPUT->show_message('contactexists', 'warning', null, false); |
|---|
| 108 | rcmail_overwrite_action('add'); |
|---|
| 109 | return; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | $plugin = $RCMAIL->plugins->exec_hook('contact_create', array( |
|---|
| 113 | 'record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC))); |
|---|
| 114 | $a_record = $plugin['record']; |
|---|
| 115 | |
|---|
| 116 | // insert record and send response |
|---|
| 117 | if (!$plugin['abort']) |
|---|
| 118 | $insert_id = $CONTACTS->insert($a_record); |
|---|
| 119 | else |
|---|
| 120 | $insert_id = $plugin['result']; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | if ($insert_id) { |
|---|
| 124 | // add contact row or jump to the page where it should appear |
|---|
| 125 | $CONTACTS->reset(); |
|---|
| 126 | $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id); |
|---|
| 127 | |
|---|
| 128 | rcmail_js_contacts_list($result, 'parent.'); |
|---|
| 129 | $OUTPUT->command('parent.contact_list.select', $insert_id); |
|---|
| 130 | |
|---|
| 131 | // update record count display |
|---|
| 132 | $CONTACTS->reset(); |
|---|
| 133 | $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text()); |
|---|
| 134 | |
|---|
| 135 | // show confirmation |
|---|
| 136 | $OUTPUT->show_message('successfullysaved', 'confirmation', null, false); |
|---|
| 137 | $OUTPUT->send('iframe'); |
|---|
| 138 | } |
|---|
| 139 | else { |
|---|
| 140 | // show error message |
|---|
| 141 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false); |
|---|
| 142 | rcmail_overwrite_action('add'); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|