| 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); |
|---|
| 23 | $cid = get_input_value('_cid', RCUBE_INPUT_POST); |
|---|
| 24 | $return_action = empty($cid) ? 'add' : 'edit'; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | // Source changed, display the form again |
|---|
| 28 | if (!empty($_GET['_reload'])) { |
|---|
| 29 | rcmail_overwrite_action($return_action); |
|---|
| 30 | return; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | // cannot edit record |
|---|
| 34 | if ($CONTACTS->readonly) { |
|---|
| 35 | $OUTPUT->show_message('contactreadonly', 'error'); |
|---|
| 36 | rcmail_overwrite_action($return_action); |
|---|
| 37 | return; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | // handle photo upload for contacts |
|---|
| 42 | if ($RCMAIL->action == 'upload-photo') { |
|---|
| 43 | // clear all stored output properties (like scripts and env vars) |
|---|
| 44 | $OUTPUT->reset(); |
|---|
| 45 | |
|---|
| 46 | if ($filepath = $_FILES['_photo']['tmp_name']) { |
|---|
| 47 | // check file type and resize image |
|---|
| 48 | $imageprop = rcmail::imageprops($_FILES['_photo']['tmp_name']); |
|---|
| 49 | |
|---|
| 50 | if ($imageprop['width'] && $imageprop['height']) { |
|---|
| 51 | $maxsize = intval($RCMAIL->config->get('contact_photo_size', 160)); |
|---|
| 52 | $tmpfname = tempnam($RCMAIL->config->get('temp_dir'), 'rcmImgConvert'); |
|---|
| 53 | $save_hook = 'attachment_upload'; |
|---|
| 54 | |
|---|
| 55 | // scale image to a maximum size |
|---|
| 56 | if (($imageprop['width'] > $maxsize || $imageprop['height'] > $maxsize) && |
|---|
| 57 | (rcmail::imageconvert(array('in' => $filepath, 'out' => $tmpfname, 'size' => $maxsize.'x'.$maxsize, 'type' => $imageprop['type'])) !== false)) { |
|---|
| 58 | $filepath = $tmpfname; |
|---|
| 59 | $save_hook = 'attachment_save'; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | // save uploaded file in storage backend |
|---|
| 63 | $attachment = $RCMAIL->plugins->exec_hook($save_hook, array( |
|---|
| 64 | 'path' => $filepath, |
|---|
| 65 | 'size' => $_FILES['_photo']['size'], |
|---|
| 66 | 'name' => $_FILES['_photo']['name'], |
|---|
| 67 | 'mimetype' => 'image/' . $imageprop['type'], |
|---|
| 68 | 'group' => 'contact', |
|---|
| 69 | )); |
|---|
| 70 | } |
|---|
| 71 | else |
|---|
| 72 | $attachment['error'] = rcube_label('invalidimageformat'); |
|---|
| 73 | |
|---|
| 74 | if ($attachment['status'] && !$attachment['abort']) { |
|---|
| 75 | $file_id = $attachment['id']; |
|---|
| 76 | $_SESSION['contacts']['files'][$file_id] = $attachment; |
|---|
| 77 | $OUTPUT->command('replace_contact_photo', $file_id); |
|---|
| 78 | } |
|---|
| 79 | else { // upload failed |
|---|
| 80 | $err = $_FILES['_photo']['error']; |
|---|
| 81 | if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) |
|---|
| 82 | $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))))); |
|---|
| 83 | else if ($attachment['error']) |
|---|
| 84 | $msg = $attachment['error']; |
|---|
| 85 | else |
|---|
| 86 | $msg = rcube_label('fileuploaderror'); |
|---|
| 87 | |
|---|
| 88 | $OUTPUT->command('display_message', $msg, 'error'); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | else if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
|---|
| 92 | // if filesize exceeds post_max_size then $_FILES array is empty, |
|---|
| 93 | // show filesizeerror instead of fileuploaderror |
|---|
| 94 | if ($maxsize = ini_get('post_max_size')) |
|---|
| 95 | $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes($maxsize))))); |
|---|
| 96 | else |
|---|
| 97 | $msg = rcube_label('fileuploaderror'); |
|---|
| 98 | |
|---|
| 99 | $OUTPUT->command('display_message', $msg, 'error'); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | $OUTPUT->command('photo_upload_end'); |
|---|
| 103 | $OUTPUT->send('iframe'); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | // read POST values into hash array |
|---|
| 107 | $a_record = array(); |
|---|
| 108 | foreach ($GLOBALS['CONTACT_COLTYPES'] as $col => $colprop) { |
|---|
| 109 | $fname = '_'.$col; |
|---|
| 110 | if ($colprop['composite']) |
|---|
| 111 | continue; |
|---|
| 112 | // gather form data of composite fields |
|---|
| 113 | if ($colprop['childs']) { |
|---|
| 114 | $values = array(); |
|---|
| 115 | foreach ($colprop['childs'] as $childcol => $cp) { |
|---|
| 116 | $vals = get_input_value('_'.$childcol, RCUBE_INPUT_POST, true); |
|---|
| 117 | foreach ((array)$vals as $i => $val) |
|---|
| 118 | $values[$i][$childcol] = $val; |
|---|
| 119 | } |
|---|
| 120 | $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST); |
|---|
| 121 | foreach ($subtypes as $i => $subtype) |
|---|
| 122 | if ($values[$i]) |
|---|
| 123 | $a_record[$col.':'.$subtype][] = $values[$i]; |
|---|
| 124 | } |
|---|
| 125 | // assign values and subtypes |
|---|
| 126 | else if (is_array($_POST[$fname])) { |
|---|
| 127 | $values = get_input_value($fname, RCUBE_INPUT_POST, true); |
|---|
| 128 | $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST); |
|---|
| 129 | foreach ($values as $i => $val) { |
|---|
| 130 | $subtype = $subtypes[$i] ? ':'.$subtypes[$i] : ''; |
|---|
| 131 | $a_record[$col.$subtype][] = $val; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | else if (isset($_POST[$fname])) { |
|---|
| 135 | $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST, true); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | // do input checks (delegated to $CONTACTS instance) |
|---|
| 141 | if (!$CONTACTS->validate($a_record)) { |
|---|
| 142 | $err = (array)$CONTACTS->get_error() + array('message' => 'formincomplete', 'type' => 'warning'); |
|---|
| 143 | $OUTPUT->show_message($err['message'], $err['type']); |
|---|
| 144 | $GLOBALS['EDIT_RECORD'] = $a_record; // store submitted data to be used in edit form |
|---|
| 145 | rcmail_overwrite_action($return_action); |
|---|
| 146 | return; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | // get raw photo data if changed |
|---|
| 150 | if (isset($a_record['photo'])) { |
|---|
| 151 | if ($a_record['photo'] == '-del-') { |
|---|
| 152 | $a_record['photo'] = ''; |
|---|
| 153 | } |
|---|
| 154 | else if ($tempfile = $_SESSION['contacts']['files'][$a_record['photo']]) { |
|---|
| 155 | $tempfile = $RCMAIL->plugins->exec_hook('attachment_get', $tempfile); |
|---|
| 156 | if ($tempfile['status']) |
|---|
| 157 | $a_record['photo'] = $tempfile['data'] ? $tempfile['data'] : @file_get_contents($tempfile['path']); |
|---|
| 158 | } |
|---|
| 159 | else |
|---|
| 160 | unset($a_record['photo']); |
|---|
| 161 | |
|---|
| 162 | // cleanup session data |
|---|
| 163 | $RCMAIL->plugins->exec_hook('attachments_cleanup', array('group' => 'contact')); |
|---|
| 164 | $RCMAIL->session->remove('contacts'); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | // Generate contact's display name |
|---|
| 168 | if (empty($a_record['name'])) { |
|---|
| 169 | $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true); |
|---|
| 170 | // Reset it if equals to email address (from compose_display_name()) |
|---|
| 171 | if ($a_record['name'] == $a_record['email'][0]) |
|---|
| 172 | $a_record['name'] = ''; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | // update an existing contact |
|---|
| 177 | if (!empty($cid)) |
|---|
| 178 | { |
|---|
| 179 | $plugin = $RCMAIL->plugins->exec_hook('contact_update', |
|---|
| 180 | array('id' => $cid, 'record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC))); |
|---|
| 181 | $a_record = $plugin['record']; |
|---|
| 182 | |
|---|
| 183 | if (!$plugin['abort']) |
|---|
| 184 | $result = $CONTACTS->update($cid, $a_record); |
|---|
| 185 | else |
|---|
| 186 | $result = $plugin['result']; |
|---|
| 187 | |
|---|
| 188 | if ($result) { |
|---|
| 189 | // LDAP DN change |
|---|
| 190 | if (is_string($result) && strlen($result)>1) { |
|---|
| 191 | $newcid = $result; |
|---|
| 192 | // change cid in POST for 'show' action |
|---|
| 193 | $_POST['_cid'] = $newcid; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | // define list of cols to be displayed |
|---|
| 197 | $a_js_cols = array(); |
|---|
| 198 | $record = $CONTACTS->get_record($newcid ? $newcid : $cid, true); |
|---|
| 199 | $record['email'] = reset($CONTACTS->get_col_values('email', $record, true)); |
|---|
| 200 | if (empty($record['name'])) |
|---|
| 201 | $record['name'] = rcube_addressbook::compose_display_name($record, true); |
|---|
| 202 | |
|---|
| 203 | foreach (array('name', 'email') as $col) |
|---|
| 204 | $a_js_cols[] = Q((string)$record[$col]); |
|---|
| 205 | |
|---|
| 206 | // update the changed col in list |
|---|
| 207 | $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid); |
|---|
| 208 | |
|---|
| 209 | // show confirmation |
|---|
| 210 | $OUTPUT->show_message('successfullysaved', 'confirmation', null, false); |
|---|
| 211 | rcmail_overwrite_action('show'); |
|---|
| 212 | } |
|---|
| 213 | else { |
|---|
| 214 | // show error message |
|---|
| 215 | $err = $CONTACTS->get_error(); |
|---|
| 216 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false); |
|---|
| 217 | rcmail_overwrite_action('show'); |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | // insert a new contact |
|---|
| 222 | else { |
|---|
| 223 | $source = get_input_value('_source', RCUBE_INPUT_GPC); |
|---|
| 224 | |
|---|
| 225 | // show notice if existing contacts with same e-mail are found |
|---|
| 226 | $existing = false; |
|---|
| 227 | foreach ($CONTACTS->get_col_values('email', $a_record, true) as $email) { |
|---|
| 228 | if (($res = $CONTACTS->search('email', $email, false, false, true)) && $res->count) { |
|---|
| 229 | $OUTPUT->show_message('contactexists', 'notice', null, false); |
|---|
| 230 | break; |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | $plugin = $RCMAIL->plugins->exec_hook('contact_create', array( |
|---|
| 235 | 'record' => $a_record, 'source' => $source)); |
|---|
| 236 | $a_record = $plugin['record']; |
|---|
| 237 | |
|---|
| 238 | // insert record and send response |
|---|
| 239 | if (!$plugin['abort']) |
|---|
| 240 | $insert_id = $CONTACTS->insert($a_record); |
|---|
| 241 | else |
|---|
| 242 | $insert_id = $plugin['result']; |
|---|
| 243 | |
|---|
| 244 | if ($insert_id) { |
|---|
| 245 | // add new contact to the specified group |
|---|
| 246 | if ($CONTACTS->groups && $CONTACTS->group_id) { |
|---|
| 247 | $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array( |
|---|
| 248 | 'group_id' => $CONTACTS->group_id, 'ids' => $insert_id, 'source' => $source)); |
|---|
| 249 | |
|---|
| 250 | if (!$plugin['abort']) { |
|---|
| 251 | if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($CONTACTS->count()->count + 1 > $maxnum)) |
|---|
| 252 | $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum)); |
|---|
| 253 | |
|---|
| 254 | $CONTACTS->add_to_group($gid, $plugin['ids']); |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | // Name of the addressbook already selected on the list |
|---|
| 259 | $orig_source = get_input_value('_orig_source', RCUBE_INPUT_GPC); |
|---|
| 260 | |
|---|
| 261 | if ((string)$source === (string)$orig_source) { |
|---|
| 262 | // add contact row or jump to the page where it should appear |
|---|
| 263 | $CONTACTS->reset(); |
|---|
| 264 | $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id); |
|---|
| 265 | |
|---|
| 266 | rcmail_js_contacts_list($result, 'parent.'); |
|---|
| 267 | $OUTPUT->command('parent.contact_list.select', html_identifier($insert_id)); |
|---|
| 268 | |
|---|
| 269 | // update record count display |
|---|
| 270 | $CONTACTS->reset(); |
|---|
| 271 | $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text()); |
|---|
| 272 | } |
|---|
| 273 | else { |
|---|
| 274 | // re-set iframe |
|---|
| 275 | $OUTPUT->command('parent.show_contentframe'); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | // show confirmation |
|---|
| 279 | $OUTPUT->show_message('successfullysaved', 'confirmation', null, false); |
|---|
| 280 | $OUTPUT->send('iframe'); |
|---|
| 281 | } |
|---|
| 282 | else { |
|---|
| 283 | // show error message |
|---|
| 284 | $err = $CONTACTS->get_error(); |
|---|
| 285 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false); |
|---|
| 286 | rcmail_overwrite_action('add'); |
|---|
| 287 | } |
|---|
| 288 | } |
|---|