Aleksander Machniak
2012-05-22 041c93ce0bc00cb6417ce2e4bdce2ed84d37f50a
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/save.inc                                    |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
0501b6 8  | Copyright (C) 2005-2011, The Roundcube Dev Team                       |
7fe381 9  |                                                                       |
T 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.                     |
4e17e6 13  |                                                                       |
T 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
70c311 22 $CONTACTS = rcmail_contact_source(null, true, true);
ecf295 23 $cid      = get_input_value('_cid', RCUBE_INPUT_POST);
6f0968 24 $return_action = empty($cid) ? 'add' : 'edit';
57f0c8 25
2dd2bf 26 // Source changed, display the form again
A 27 if (!empty($_GET['_reload'])) {
28    rcmail_overwrite_action($return_action);
29    return;
30 }
31
f11541 32 // cannot edit record
6f0968 33 if ($CONTACTS->readonly) {
f11541 34   $OUTPUT->show_message('contactreadonly', 'error');
57f0c8 35   rcmail_overwrite_action($return_action);
10a699 36   return;
0501b6 37 }
T 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) {
516467 49       $vals = get_input_value('_'.$childcol, RCUBE_INPUT_POST, true);
0501b6 50       foreach ((array)$vals as $i => $val)
T 51         $values[$i][$childcol] = $val;
52     }
b885ab 53     $subtypes = isset($_REQUEST['_subtype_' . $col]) ? (array)get_input_value('_subtype_' . $col, RCUBE_INPUT_POST) : array('');
T 54     foreach ($subtypes as $i => $subtype) {
55       $suffix = $subtype ? ':'.$subtype : '';
0501b6 56       if ($values[$i])
b885ab 57         $a_record[$col.$suffix][] = $values[$i];
T 58      }
0501b6 59   }
T 60   // assign values and subtypes
61   else if (is_array($_POST[$fname])) {
516467 62     $values = get_input_value($fname, RCUBE_INPUT_POST, true);
0501b6 63     $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST);
T 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])) {
516467 70     $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST, true);
0501b6 71   }
T 72 }
73
1e36b7 74 // Generate contact's display name (must be before validation)
A 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 }
07b95d 81
T 82 // do input checks (delegated to $CONTACTS instance)
83 if (!$CONTACTS->validate($a_record)) {
a01df7 84     $err = (array)$CONTACTS->get_error();
A 85     $OUTPUT->show_message($err['message'] ? $err['message'] : 'formincomplete', 'warning');
3d8b54 86     $GLOBALS['EDIT_RECORD'] = $a_record;  // store submitted data to be used in edit form
07b95d 87     rcmail_overwrite_action($return_action);
T 88     return;
6f0968 89 }
A 90
0501b6 91 // get raw photo data if changed
T 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']);
ecf295 103
0501b6 104     // cleanup session data
4591de 105     $RCMAIL->plugins->exec_hook('attachments_cleanup', array('group' => 'contact'));
0501b6 106     $RCMAIL->session->remove('contacts');
79dd16 107 }
A 108
5db6f9 109 $source = get_input_value('_source', RCUBE_INPUT_GPC);
79dd16 110
4e17e6 111 // update an existing contact
f11541 112 if (!empty($cid))
T 113 {
119ad1 114   $plugin = $RCMAIL->plugins->exec_hook('contact_update',
5db6f9 115     array('id' => $cid, 'record' => $a_record, 'source' => $source));
69f18a 116   $a_record = $plugin['record'];
6f0968 117
ce92ba 118   if (!$plugin['abort'])
A 119     $result = $CONTACTS->update($cid, $a_record);
120   else
121     $result = $plugin['result'];
122
123   if ($result) {
e83f03 124     // LDAP DN change
A 125     if (is_string($result) && strlen($result)>1) {
126       $newcid = $result;
127       // change cid in POST for 'show' action
128       $_POST['_cid'] = $newcid;
129     }
6f0968 130
c1b3c4 131     // define list of cols to be displayed
T 132     $a_js_cols = array();
e83f03 133     $record = $CONTACTS->get_record($newcid ? $newcid : $cid, true);
e84818 134     $record['email'] = reset($CONTACTS->get_col_values('email', $record, true));
f9a967 135     $record['name']  = rcube_addressbook::compose_list_name($record);
f11541 136
c1b3c4 137     foreach (array('name', 'email') as $col)
516467 138       $a_js_cols[] = Q((string)$record[$col]);
4e17e6 139
c1b3c4 140     // update the changed col in list
5db6f9 141     $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid, $source);
6f0968 142
6b47de 143     // show confirmation
69f18a 144     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
6b47de 145     rcmail_overwrite_action('show');
4e17e6 146   }
ce92ba 147   else {
f11541 148     // show error message
0501b6 149     $err = $CONTACTS->get_error();
T 150     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false);
f11541 151     rcmail_overwrite_action('show');
T 152   }
153 }
4e17e6 154
T 155 // insert a new contact
ce92ba 156 else {
70c311 157   // Name of the addressbook already selected on the list
A 158   $orig_source = get_input_value('_orig_source', RCUBE_INPUT_GPC);
159
160   if (!strlen($source))
161     $source = $orig_source;
1a3c91 162
b8f14c 163   // show notice if existing contacts with same e-mail are found
0501b6 164   $existing = false;
T 165   foreach ($CONTACTS->get_col_values('email', $a_record, true) as $email) {
f21a04 166       if ($email && ($res = $CONTACTS->search('email', $email, 1, false, true)) && $res->count) {
b8f14c 167           $OUTPUT->show_message('contactexists', 'notice', null, false);
0501b6 168           break;
T 169       }
f11541 170   }
4e17e6 171
ce92ba 172   $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
1a3c91 173     'record' => $a_record, 'source' => $source));
69f18a 174   $a_record = $plugin['record'];
T 175
f11541 176   // insert record and send response
ce92ba 177   if (!$plugin['abort'])
A 178     $insert_id = $CONTACTS->insert($a_record);
179   else
180     $insert_id = $plugin['result'];
181
182   if ($insert_id) {
8458c7 183     // add new contact to the specified group
6039aa 184     if ($CONTACTS->groups && $CONTACTS->group_id) {
1a3c91 185       $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
A 186         'group_id' => $CONTACTS->group_id, 'ids' => $insert_id, 'source' => $source));
8458c7 187
T 188       if (!$plugin['abort']) {
189         if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($CONTACTS->count()->count + 1 > $maxnum))
190           $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
191
192         $CONTACTS->add_to_group($gid, $plugin['ids']);
193       }
194     }
4e17e6 195
1a3c91 196     if ((string)$source === (string)$orig_source) {
A 197       // add contact row or jump to the page where it should appear
198       $CONTACTS->reset();
199       $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id);
d1d2c4 200
1a3c91 201       rcmail_js_contacts_list($result, 'parent.');
A 202       $OUTPUT->command('parent.contact_list.select', html_identifier($insert_id));
203
204       // update record count display
205       $CONTACTS->reset();
206       $OUTPUT->command('parent.set_rowcount', rcmail_get_rowcount_text());
207     }
208     else {
209       // re-set iframe
210       $OUTPUT->command('parent.show_contentframe');
211     }
d1d2c4 212
S 213     // show confirmation
69f18a 214     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
b80a97 215     $OUTPUT->send('iframe');
4e17e6 216   }
ce92ba 217   else {
f11541 218     // show error message
0501b6 219     $err = $CONTACTS->get_error();
T 220     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false);
f11541 221     rcmail_overwrite_action('add');
T 222   }
223 }