0
|
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-2013, The Roundcube Dev Team |
|
|
9 | |
|
|
10 | Licensed under the GNU General Public License version 3 or |
|
|
11 | any later version with exceptions for skins & plugins. |
|
|
12 | See the README file for a full license statement. |
|
|
13 | |
|
|
14 | PURPOSE: |
|
|
15 | Save a contact entry or to add a new one |
|
|
16 | |
|
|
17 +-----------------------------------------------------------------------+
|
|
18 | Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
19 +-----------------------------------------------------------------------+
|
|
20 */
|
|
21
|
|
22 $CONTACTS = rcmail_contact_source(null, true, true);
|
|
23 $cid = rcube_utils::get_input_value('_cid', rcube_utils::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 if ($colprop['composite']) {
|
|
43 continue;
|
|
44 }
|
|
45
|
|
46 $fname = '_'.$col;
|
|
47
|
|
48 // gather form data of composite fields
|
|
49 if ($colprop['childs']) {
|
|
50 $values = array();
|
|
51 foreach ($colprop['childs'] as $childcol => $cp) {
|
|
52 $vals = rcube_utils::get_input_value('_'.$childcol, rcube_utils::INPUT_POST, true);
|
|
53 foreach ((array)$vals as $i => $val) {
|
|
54 $values[$i][$childcol] = $val;
|
|
55 }
|
|
56 }
|
|
57
|
|
58 $subtypes = isset($_REQUEST['_subtype_' . $col]) ? (array)rcube_utils::get_input_value('_subtype_' . $col, rcube_utils::INPUT_POST) : array('');
|
|
59 foreach ($subtypes as $i => $subtype) {
|
|
60 $suffix = $subtype ? ':'.$subtype : '';
|
|
61 if ($values[$i]) {
|
|
62 $a_record[$col.$suffix][] = $values[$i];
|
|
63 }
|
|
64 }
|
|
65 }
|
|
66 // assign values and subtypes
|
|
67 else if (is_array($_POST[$fname])) {
|
|
68 $values = rcube_utils::get_input_value($fname, rcube_utils::INPUT_POST, true);
|
|
69 $subtypes = rcube_utils::get_input_value('_subtype_' . $col, rcube_utils::INPUT_POST);
|
|
70
|
|
71 foreach ($values as $i => $val) {
|
|
72 if ($col == 'email') {
|
|
73 // extract email from full address specification, e.g. "Name" <addr@domain.tld>
|
|
74 $addr = rcube_mime::decode_address_list($val, 1, false);
|
|
75 if (!empty($addr) && ($addr = array_pop($addr)) && $addr['mailto']) {
|
|
76 $val = $addr['mailto'];
|
|
77 }
|
|
78 }
|
|
79
|
|
80 $subtype = $subtypes[$i] ? ':'.$subtypes[$i] : '';
|
|
81 $a_record[$col.$subtype][] = $val;
|
|
82 }
|
|
83 }
|
|
84 else if (isset($_POST[$fname])) {
|
|
85 $a_record[$col] = rcube_utils::get_input_value($fname, rcube_utils::INPUT_POST, true);
|
|
86
|
|
87 // normalize the submitted date strings
|
|
88 if ($colprop['type'] == 'date') {
|
|
89 if ($a_record[$col] && ($dt = rcube_utils::anytodatetime($a_record[$col]))) {
|
|
90 $a_record[$col] = $dt->format('Y-m-d');
|
|
91 }
|
|
92 else {
|
|
93 unset($a_record[$col]);
|
|
94 }
|
|
95 }
|
|
96 }
|
|
97 }
|
|
98
|
|
99 // Generate contact's display name (must be before validation)
|
|
100 if (empty($a_record['name'])) {
|
|
101 $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true);
|
|
102
|
|
103 // Reset it if equals to email address (from compose_display_name())
|
|
104 $email = rcube_addressbook::get_col_values('email', $a_record, true);
|
|
105 if ($a_record['name'] == $email[0]) {
|
|
106 $a_record['name'] = '';
|
|
107 }
|
|
108 }
|
|
109
|
|
110 // do input checks (delegated to $CONTACTS instance)
|
|
111 if (!$CONTACTS->validate($a_record)) {
|
|
112 $err = (array)$CONTACTS->get_error();
|
|
113 $OUTPUT->show_message($err['message'] ? rcube::Q($err['message']) : 'formincomplete', 'warning');
|
|
114 $GLOBALS['EDIT_RECORD'] = $a_record; // store submitted data to be used in edit form
|
|
115 $RCMAIL->overwrite_action($return_action);
|
|
116 return;
|
|
117 }
|
|
118
|
|
119 // get raw photo data if changed
|
|
120 if (isset($a_record['photo'])) {
|
|
121 if ($a_record['photo'] == '-del-') {
|
|
122 $a_record['photo'] = '';
|
|
123 }
|
|
124 else if ($tempfile = $_SESSION['contacts']['files'][$a_record['photo']]) {
|
|
125 $tempfile = $RCMAIL->plugins->exec_hook('attachment_get', $tempfile);
|
|
126 if ($tempfile['status'])
|
|
127 $a_record['photo'] = $tempfile['data'] ?: @file_get_contents($tempfile['path']);
|
|
128 }
|
|
129 else
|
|
130 unset($a_record['photo']);
|
|
131
|
|
132 // cleanup session data
|
|
133 $RCMAIL->plugins->exec_hook('attachments_cleanup', array('group' => 'contact'));
|
|
134 $RCMAIL->session->remove('contacts');
|
|
135 }
|
|
136
|
|
137 $source = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
|
|
138
|
|
139 // update an existing contact
|
|
140 if (!empty($cid)) {
|
|
141 $plugin = $RCMAIL->plugins->exec_hook('contact_update',
|
|
142 array('id' => $cid, 'record' => $a_record, 'source' => $source));
|
|
143 $a_record = $plugin['record'];
|
|
144
|
|
145 if (!$plugin['abort'])
|
|
146 $result = $CONTACTS->update($cid, $a_record);
|
|
147 else
|
|
148 $result = $plugin['result'];
|
|
149
|
|
150 if ($result) {
|
|
151 // show confirmation
|
|
152 $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
|
|
153
|
|
154 // in search mode, just reload the list (#1490015)
|
|
155 if ($_REQUEST['_search']) {
|
|
156 $OUTPUT->command('parent.command', 'list');
|
|
157 $OUTPUT->send('iframe');
|
|
158 }
|
|
159
|
|
160 // LDAP DN change
|
|
161 if (is_string($result) && strlen($result) > 1) {
|
|
162 $newcid = $result;
|
|
163 // change cid in POST for 'show' action
|
|
164 $_POST['_cid'] = $newcid;
|
|
165 }
|
|
166
|
|
167 // refresh contact data for list update and 'show' action
|
|
168 $CONTACT_RECORD = $CONTACTS->get_record($newcid ?: $cid, true);
|
|
169
|
|
170 // Plugins can decide to remove the contact on edit, e.g. automatic_addressbook
|
|
171 // Best we can do is to refresh the list (#5522)
|
|
172 if (empty($CONTACT_RECORD)) {
|
|
173 $OUTPUT->command('parent.command', 'list');
|
|
174 $OUTPUT->send('iframe');
|
|
175 }
|
|
176
|
|
177 // Update contacts list
|
|
178 $a_js_cols = array();
|
|
179 $record = $CONTACT_RECORD;
|
|
180 $record['email'] = reset($CONTACTS->get_col_values('email', $record, true));
|
|
181 $record['name'] = rcube_addressbook::compose_list_name($record);
|
|
182
|
|
183 foreach (array('name') as $col) {
|
|
184 $a_js_cols[] = rcube::Q((string)$record[$col]);
|
|
185 }
|
|
186
|
|
187 // performance: unset some big data items we don't need here
|
|
188 $record = array_intersect_key($record, array('ID' => 1,'email' => 1,'name' => 1));
|
|
189 $record['_type'] = 'person';
|
|
190
|
|
191 // update the changed col in list
|
|
192 $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid, $source, $record);
|
|
193
|
|
194 $RCMAIL->overwrite_action('show');
|
|
195 }
|
|
196 else {
|
|
197 // show error message
|
|
198 $err = $CONTACTS->get_error();
|
|
199 $OUTPUT->show_message($plugin['message'] ?: ($err['message'] ?: 'errorsaving'), 'error', null, false);
|
|
200 $RCMAIL->overwrite_action('show');
|
|
201 }
|
|
202 }
|
|
203
|
|
204 // insert a new contact
|
|
205 else {
|
|
206 // Name of the addressbook already selected on the list
|
|
207 $orig_source = rcube_utils::get_input_value('_orig_source', rcube_utils::INPUT_GPC);
|
|
208
|
|
209 if (!strlen($source)) {
|
|
210 $source = $orig_source;
|
|
211 }
|
|
212
|
|
213 // show notice if existing contacts with same e-mail are found
|
|
214 foreach ($CONTACTS->get_col_values('email', $a_record, true) as $email) {
|
|
215 if ($email && ($res = $CONTACTS->search('email', $email, 1, false, true)) && $res->count) {
|
|
216 $OUTPUT->show_message('contactexists', 'notice', null, false);
|
|
217 break;
|
|
218 }
|
|
219 }
|
|
220
|
|
221 $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
|
|
222 'record' => $a_record, 'source' => $source));
|
|
223 $a_record = $plugin['record'];
|
|
224
|
|
225 // insert record and send response
|
|
226 if (!$plugin['abort'])
|
|
227 $insert_id = $CONTACTS->insert($a_record);
|
|
228 else
|
|
229 $insert_id = $plugin['result'];
|
|
230
|
|
231 if ($insert_id) {
|
|
232 $CONTACTS->reset();
|
|
233
|
|
234 // add new contact to the specified group
|
|
235 if ($CONTACTS->groups && $CONTACTS->group_id) {
|
|
236 $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
|
|
237 'group_id' => $CONTACTS->group_id, 'ids' => $insert_id, 'source' => $source));
|
|
238
|
|
239 if (!$plugin['abort']) {
|
|
240 if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($CONTACTS->count()->count + 1 > $maxnum)) {
|
|
241 // @FIXME: should we remove the contact?
|
|
242 $msgtext = $RCMAIL->gettext(array('name' => 'maxgroupmembersreached', 'vars' => array('max' => $maxnum)));
|
|
243 $OUTPUT->command('parent.display_message', $msgtext, 'warning');
|
|
244 }
|
|
245 else {
|
|
246 $CONTACTS->add_to_group($plugin['group_id'], $plugin['ids']);
|
|
247 }
|
|
248 }
|
|
249 }
|
|
250
|
|
251 // show confirmation
|
|
252 $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
|
|
253
|
|
254 $OUTPUT->command('parent.set_rowcount', $RCMAIL->gettext('loading'));
|
|
255 $OUTPUT->command('parent.list_contacts');
|
|
256
|
|
257 $OUTPUT->send('iframe');
|
|
258 }
|
|
259 else {
|
|
260 // show error message
|
|
261 $err = $CONTACTS->get_error();
|
|
262 $OUTPUT->show_message($plugin['message'] ?: ($err['message'] ?: 'errorsaving'), 'error', null, false);
|
|
263 $RCMAIL->overwrite_action('add');
|
|
264 }
|
|
265 }
|