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