| [3] | 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/steps/addressbook/save.inc | |
|---|
| 6 | | | |
|---|
| [3989] | 7 | | This file is part of the Roundcube Webmail client | |
|---|
| [4424] | 8 | | Copyright (C) 2005-2011, The Roundcube Dev Team | |
|---|
| [8] | 9 | | Licensed under the GNU GPL | |
|---|
| [3] | 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 | |
|---|
| [4976] | 22 | $CONTACTS = rcmail_contact_source(null, true, true); |
|---|
| [4850] | 23 | $cid = get_input_value('_cid', RCUBE_INPUT_POST); |
|---|
| [4166] | 24 | $return_action = empty($cid) ? 'add' : 'edit'; |
|---|
| [2755] | 25 | |
|---|
| [5005] | 26 | // Source changed, display the form again |
|---|
| 27 | if (!empty($_GET['_reload'])) { |
|---|
| 28 | rcmail_overwrite_action($return_action); |
|---|
| 29 | return; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| [543] | 32 | // cannot edit record |
|---|
| [4166] | 33 | if ($CONTACTS->readonly) { |
|---|
| [543] | 34 | $OUTPUT->show_message('contactreadonly', 'error'); |
|---|
| [2755] | 35 | rcmail_overwrite_action($return_action); |
|---|
| [543] | 36 | return; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| [4424] | 39 | // read POST values into hash array |
|---|
| [543] | 40 | $a_record = array(); |
|---|
| [4424] | 41 | foreach ($GLOBALS['CONTACT_COLTYPES'] as $col => $colprop) { |
|---|
| [543] | 42 | $fname = '_'.$col; |
|---|
| [4424] | 43 | if ($colprop['composite']) |
|---|
| 44 | continue; |
|---|
| 45 | // gather form data of composite fields |
|---|
| 46 | if ($colprop['childs']) { |
|---|
| 47 | $values = array(); |
|---|
| 48 | foreach ($colprop['childs'] as $childcol => $cp) { |
|---|
| [4812] | 49 | $vals = get_input_value('_'.$childcol, RCUBE_INPUT_POST, true); |
|---|
| [4424] | 50 | foreach ((array)$vals as $i => $val) |
|---|
| 51 | $values[$i][$childcol] = $val; |
|---|
| 52 | } |
|---|
| 53 | $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST); |
|---|
| 54 | foreach ($subtypes as $i => $subtype) |
|---|
| 55 | if ($values[$i]) |
|---|
| 56 | $a_record[$col.':'.$subtype][] = $values[$i]; |
|---|
| 57 | } |
|---|
| 58 | // assign values and subtypes |
|---|
| 59 | else if (is_array($_POST[$fname])) { |
|---|
| [4812] | 60 | $values = get_input_value($fname, RCUBE_INPUT_POST, true); |
|---|
| [4424] | 61 | $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST); |
|---|
| 62 | foreach ($values as $i => $val) { |
|---|
| 63 | $subtype = $subtypes[$i] ? ':'.$subtypes[$i] : ''; |
|---|
| 64 | $a_record[$col.$subtype][] = $val; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | else if (isset($_POST[$fname])) { |
|---|
| [4812] | 68 | $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST, true); |
|---|
| [4424] | 69 | } |
|---|
| [543] | 70 | } |
|---|
| 71 | |
|---|
| [4973] | 72 | // Generate contact's display name (must be before validation) |
|---|
| 73 | if (empty($a_record['name'])) { |
|---|
| 74 | $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true); |
|---|
| 75 | // Reset it if equals to email address (from compose_display_name()) |
|---|
| 76 | if ($a_record['name'] == $a_record['email'][0]) |
|---|
| 77 | $a_record['name'] = ''; |
|---|
| 78 | } |
|---|
| [4424] | 79 | |
|---|
| [4498] | 80 | // do input checks (delegated to $CONTACTS instance) |
|---|
| 81 | if (!$CONTACTS->validate($a_record)) { |
|---|
| 82 | $err = (array)$CONTACTS->get_error() + array('message' => 'formincomplete', 'type' => 'warning'); |
|---|
| 83 | $OUTPUT->show_message($err['message'], $err['type']); |
|---|
| [4649] | 84 | $GLOBALS['EDIT_RECORD'] = $a_record; // store submitted data to be used in edit form |
|---|
| [4498] | 85 | rcmail_overwrite_action($return_action); |
|---|
| 86 | return; |
|---|
| [4166] | 87 | } |
|---|
| 88 | |
|---|
| [4424] | 89 | // get raw photo data if changed |
|---|
| 90 | if (isset($a_record['photo'])) { |
|---|
| 91 | if ($a_record['photo'] == '-del-') { |
|---|
| 92 | $a_record['photo'] = ''; |
|---|
| 93 | } |
|---|
| 94 | else if ($tempfile = $_SESSION['contacts']['files'][$a_record['photo']]) { |
|---|
| 95 | $tempfile = $RCMAIL->plugins->exec_hook('attachment_get', $tempfile); |
|---|
| 96 | if ($tempfile['status']) |
|---|
| 97 | $a_record['photo'] = $tempfile['data'] ? $tempfile['data'] : @file_get_contents($tempfile['path']); |
|---|
| 98 | } |
|---|
| 99 | else |
|---|
| 100 | unset($a_record['photo']); |
|---|
| [4850] | 101 | |
|---|
| [4424] | 102 | // cleanup session data |
|---|
| [4604] | 103 | $RCMAIL->plugins->exec_hook('attachments_cleanup', array('group' => 'contact')); |
|---|
| [4424] | 104 | $RCMAIL->session->remove('contacts'); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| [5108] | 107 | $source = get_input_value('_source', RCUBE_INPUT_GPC); |
|---|
| [4967] | 108 | |
|---|
| [3] | 109 | // update an existing contact |
|---|
| [543] | 110 | if (!empty($cid)) |
|---|
| 111 | { |
|---|
| [3883] | 112 | $plugin = $RCMAIL->plugins->exec_hook('contact_update', |
|---|
| [5108] | 113 | array('id' => $cid, 'record' => $a_record, 'source' => $source)); |
|---|
| [2441] | 114 | $a_record = $plugin['record']; |
|---|
| [4166] | 115 | |
|---|
| [4025] | 116 | if (!$plugin['abort']) |
|---|
| 117 | $result = $CONTACTS->update($cid, $a_record); |
|---|
| 118 | else |
|---|
| 119 | $result = $plugin['result']; |
|---|
| 120 | |
|---|
| 121 | if ($result) { |
|---|
| [2889] | 122 | // LDAP DN change |
|---|
| 123 | if (is_string($result) && strlen($result)>1) { |
|---|
| 124 | $newcid = $result; |
|---|
| 125 | // change cid in POST for 'show' action |
|---|
| 126 | $_POST['_cid'] = $newcid; |
|---|
| 127 | } |
|---|
| [4166] | 128 | |
|---|
| [1407] | 129 | // define list of cols to be displayed |
|---|
| 130 | $a_js_cols = array(); |
|---|
| [2889] | 131 | $record = $CONTACTS->get_record($newcid ? $newcid : $cid, true); |
|---|
| [4752] | 132 | $record['email'] = reset($CONTACTS->get_col_values('email', $record, true)); |
|---|
| [4965] | 133 | if (empty($record['name'])) |
|---|
| 134 | $record['name'] = rcube_addressbook::compose_display_name($record, true); |
|---|
| [3] | 135 | |
|---|
| [1407] | 136 | foreach (array('name', 'email') as $col) |
|---|
| [4812] | 137 | $a_js_cols[] = Q((string)$record[$col]); |
|---|
| [543] | 138 | |
|---|
| [1407] | 139 | // update the changed col in list |
|---|
| [5108] | 140 | $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid, $source); |
|---|
| [4166] | 141 | |
|---|
| [354] | 142 | // show confirmation |
|---|
| [2441] | 143 | $OUTPUT->show_message('successfullysaved', 'confirmation', null, false); |
|---|
| [354] | 144 | rcmail_overwrite_action('show'); |
|---|
| [543] | 145 | } |
|---|
| [4025] | 146 | else { |
|---|
| [3] | 147 | // show error message |
|---|
| [4424] | 148 | $err = $CONTACTS->get_error(); |
|---|
| 149 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false); |
|---|
| [76] | 150 | rcmail_overwrite_action('show'); |
|---|
| [3] | 151 | } |
|---|
| [543] | 152 | } |
|---|
| [3] | 153 | |
|---|
| 154 | // insert a new contact |
|---|
| [4025] | 155 | else { |
|---|
| [4976] | 156 | // Name of the addressbook already selected on the list |
|---|
| 157 | $orig_source = get_input_value('_orig_source', RCUBE_INPUT_GPC); |
|---|
| [4913] | 158 | |
|---|
| [4976] | 159 | if (!strlen($source)) |
|---|
| 160 | $source = $orig_source; |
|---|
| 161 | |
|---|
| [4572] | 162 | // show notice if existing contacts with same e-mail are found |
|---|
| [4424] | 163 | $existing = false; |
|---|
| 164 | foreach ($CONTACTS->get_col_values('email', $a_record, true) as $email) { |
|---|
| [5059] | 165 | if ($email && ($res = $CONTACTS->search('email', $email, false, false, true)) && $res->count) { |
|---|
| [4572] | 166 | $OUTPUT->show_message('contactexists', 'notice', null, false); |
|---|
| [4424] | 167 | break; |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| [3536] | 170 | |
|---|
| [4025] | 171 | $plugin = $RCMAIL->plugins->exec_hook('contact_create', array( |
|---|
| [4913] | 172 | 'record' => $a_record, 'source' => $source)); |
|---|
| [2441] | 173 | $a_record = $plugin['record']; |
|---|
| 174 | |
|---|
| [543] | 175 | // insert record and send response |
|---|
| [4025] | 176 | if (!$plugin['abort']) |
|---|
| 177 | $insert_id = $CONTACTS->insert($a_record); |
|---|
| 178 | else |
|---|
| 179 | $insert_id = $plugin['result']; |
|---|
| 180 | |
|---|
| 181 | if ($insert_id) { |
|---|
| [4454] | 182 | // add new contact to the specified group |
|---|
| [4467] | 183 | if ($CONTACTS->groups && $CONTACTS->group_id) { |
|---|
| [4913] | 184 | $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array( |
|---|
| 185 | 'group_id' => $CONTACTS->group_id, 'ids' => $insert_id, 'source' => $source)); |
|---|
| [4454] | 186 | |
|---|
| 187 | if (!$plugin['abort']) { |
|---|
| 188 | if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($CONTACTS->count()->count + 1 > $maxnum)) |
|---|
| 189 | $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum)); |
|---|
| 190 | |
|---|
| 191 | $CONTACTS->add_to_group($gid, $plugin['ids']); |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| [4850] | 194 | |
|---|
| [4913] | 195 | if ((string)$source === (string)$orig_source) { |
|---|
| 196 | // add contact row or jump to the page where it should appear |
|---|
| 197 | $CONTACTS->reset(); |
|---|
| 198 | $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id); |
|---|
| [95] | 199 | |
|---|
| [4913] | 200 | rcmail_js_contacts_list($result, 'parent.'); |
|---|
| 201 | $OUTPUT->command('parent.contact_list.select', html_identifier($insert_id)); |
|---|
| [3] | 202 | |
|---|
| [4913] | 203 | // update record count display |
|---|
| 204 | $CONTACTS->reset(); |
|---|
| 205 | $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text()); |
|---|
| 206 | } |
|---|
| 207 | else { |
|---|
| 208 | // re-set iframe |
|---|
| 209 | $OUTPUT->command('parent.show_contentframe'); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| [95] | 212 | // show confirmation |
|---|
| [2441] | 213 | $OUTPUT->show_message('successfullysaved', 'confirmation', null, false); |
|---|
| [3536] | 214 | $OUTPUT->send('iframe'); |
|---|
| [543] | 215 | } |
|---|
| [4025] | 216 | else { |
|---|
| [3] | 217 | // show error message |
|---|
| [4424] | 218 | $err = $CONTACTS->get_error(); |
|---|
| 219 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false); |
|---|
| [76] | 220 | rcmail_overwrite_action('add'); |
|---|
| [3] | 221 | } |
|---|
| [543] | 222 | } |
|---|