| 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 | |
|---|
| 33 | // read POST values into hash array |
|---|
| 34 | $a_record = array(); |
|---|
| 35 | foreach ($GLOBALS['CONTACT_COLTYPES'] as $col => $colprop) { |
|---|
| 36 | $fname = '_'.$col; |
|---|
| 37 | if ($colprop['composite']) |
|---|
| 38 | continue; |
|---|
| 39 | // gather form data of composite fields |
|---|
| 40 | if ($colprop['childs']) { |
|---|
| 41 | $values = array(); |
|---|
| 42 | foreach ($colprop['childs'] as $j => $childcol) { |
|---|
| 43 | $vals = get_input_value('_'.$childcol, RCUBE_INPUT_POST); |
|---|
| 44 | foreach ((array)$vals as $i => $val) |
|---|
| 45 | $values[$i][$childcol] = $val; |
|---|
| 46 | } |
|---|
| 47 | $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST); |
|---|
| 48 | foreach ($subtypes as $i => $subtype) |
|---|
| 49 | if ($values[$i]) |
|---|
| 50 | $a_record[$col.':'.$subtype][] = $values[$i]; |
|---|
| 51 | } |
|---|
| 52 | // assign values and subtypes |
|---|
| 53 | else if (is_array($_POST[$fname])) { |
|---|
| 54 | $values = get_input_value($fname, RCUBE_INPUT_POST); |
|---|
| 55 | $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST); |
|---|
| 56 | foreach ($values as $i => $val) { |
|---|
| 57 | $subtype = $subtypes[$i] ?: 'home'; |
|---|
| 58 | $a_record[$col.':'.$subtype][] = $val; |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | else if (isset($_POST[$fname])) { |
|---|
| 62 | $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | if (empty($a_record['name'])) |
|---|
| 67 | $a_record['name'] = join(' ', array_filter(array($a_record['firstname'], $a_record['middlename'], $a_record['surname']))); |
|---|
| 68 | |
|---|
| 69 | #var_dump($a_record); |
|---|
| 70 | |
|---|
| 71 | // Basic input checks (TODO: delegate to $CONTACTS instance) |
|---|
| 72 | if (empty($a_record['name'])/* || empty($a_record['email'])*/) { |
|---|
| 73 | $OUTPUT->show_message('formincomplete', 'warning'); |
|---|
| 74 | rcmail_overwrite_action($return_action); |
|---|
| 75 | return; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | // Validity checks |
|---|
| 79 | foreach ($a_record as $field => $values) { |
|---|
| 80 | if (strpos($field, 'email') === 0) { |
|---|
| 81 | foreach ((array)$values as $email) { |
|---|
| 82 | $_email = idn_to_ascii($email); |
|---|
| 83 | if (!check_email($_email, false)) { |
|---|
| 84 | $OUTPUT->show_message('emailformaterror', 'warning', array('email' => $email)); |
|---|
| 85 | rcmail_overwrite_action($return_action); |
|---|
| 86 | return; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | // update an existing contact |
|---|
| 93 | if (!empty($cid)) |
|---|
| 94 | { |
|---|
| 95 | $plugin = $RCMAIL->plugins->exec_hook('contact_update', |
|---|
| 96 | array('id' => $cid, 'record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC))); |
|---|
| 97 | $a_record = $plugin['record']; |
|---|
| 98 | |
|---|
| 99 | if (!$plugin['abort']) |
|---|
| 100 | $result = $CONTACTS->update($cid, $a_record); |
|---|
| 101 | else |
|---|
| 102 | $result = $plugin['result']; |
|---|
| 103 | |
|---|
| 104 | if ($result) { |
|---|
| 105 | // LDAP DN change |
|---|
| 106 | if (is_string($result) && strlen($result)>1) { |
|---|
| 107 | $newcid = $result; |
|---|
| 108 | // change cid in POST for 'show' action |
|---|
| 109 | $_POST['_cid'] = $newcid; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | // define list of cols to be displayed |
|---|
| 113 | $a_js_cols = array(); |
|---|
| 114 | $record = $CONTACTS->get_record($newcid ? $newcid : $cid, true); |
|---|
| 115 | |
|---|
| 116 | foreach (array('name', 'email') as $col) |
|---|
| 117 | $a_js_cols[] = (string)$record[$col]; |
|---|
| 118 | |
|---|
| 119 | // update the changed col in list |
|---|
| 120 | $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid); |
|---|
| 121 | |
|---|
| 122 | // show confirmation |
|---|
| 123 | $OUTPUT->show_message('successfullysaved', 'confirmation', null, false); |
|---|
| 124 | rcmail_overwrite_action('show'); |
|---|
| 125 | } |
|---|
| 126 | else { |
|---|
| 127 | // show error message |
|---|
| 128 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false); |
|---|
| 129 | rcmail_overwrite_action('show'); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | // insert a new contact |
|---|
| 134 | else { |
|---|
| 135 | // check for existing contacts |
|---|
| 136 | $existing = $CONTACTS->search('email', $a_record['email'], true, false); |
|---|
| 137 | |
|---|
| 138 | // show warning message |
|---|
| 139 | if ($existing->count) { |
|---|
| 140 | $OUTPUT->show_message('contactexists', 'warning', null, false); |
|---|
| 141 | rcmail_overwrite_action('add'); |
|---|
| 142 | return; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | $plugin = $RCMAIL->plugins->exec_hook('contact_create', array( |
|---|
| 146 | 'record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC))); |
|---|
| 147 | $a_record = $plugin['record']; |
|---|
| 148 | |
|---|
| 149 | // insert record and send response |
|---|
| 150 | if (!$plugin['abort']) |
|---|
| 151 | $insert_id = $CONTACTS->insert($a_record); |
|---|
| 152 | else |
|---|
| 153 | $insert_id = $plugin['result']; |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | if ($insert_id) { |
|---|
| 157 | // add contact row or jump to the page where it should appear |
|---|
| 158 | $CONTACTS->reset(); |
|---|
| 159 | $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id); |
|---|
| 160 | |
|---|
| 161 | rcmail_js_contacts_list($result, 'parent.'); |
|---|
| 162 | $OUTPUT->command('parent.contact_list.select', $insert_id); |
|---|
| 163 | |
|---|
| 164 | // update record count display |
|---|
| 165 | $CONTACTS->reset(); |
|---|
| 166 | $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text()); |
|---|
| 167 | |
|---|
| 168 | // show confirmation |
|---|
| 169 | $OUTPUT->show_message('successfullysaved', 'confirmation', null, false); |
|---|
| 170 | $OUTPUT->send('iframe'); |
|---|
| 171 | } |
|---|
| 172 | else { |
|---|
| 173 | // show error message |
|---|
| 174 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false); |
|---|
| 175 | rcmail_overwrite_action('add'); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|