]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/save.inc
Fix symlink mess
[roundcube.git] / program / steps / addressbook / save.inc
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: save.inc 5873 2012-02-11 13:50:04Z thomasb $
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();
85     $OUTPUT->show_message($err['message'] ? $err['message'] : 'formincomplete', 'warning');
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     $record['name']  = rcube_addressbook::compose_list_name($record);
136
137     foreach (array('name', 'email') as $col)
138       $a_js_cols[] = Q((string)$record[$col]);
139
140     // update the changed col in list
141     $OUTPUT->command('parent.update_contact_row', $cid, $a_js_cols, $newcid, $source);
142
143     // show confirmation
144     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
145     rcmail_overwrite_action('show');
146   }
147   else {
148     // show error message
149     $err = $CONTACTS->get_error();
150     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false);
151     rcmail_overwrite_action('show');
152   }
153 }
154
155 // insert a new contact
156 else {
157   // Name of the addressbook already selected on the list
158   $orig_source = get_input_value('_orig_source', RCUBE_INPUT_GPC);
159
160   if (!strlen($source))
161     $source = $orig_source;
162
163   // show notice if existing contacts with same e-mail are found
164   $existing = false;
165   foreach ($CONTACTS->get_col_values('email', $a_record, true) as $email) {
166       if ($email && ($res = $CONTACTS->search('email', $email, 1, false, true)) && $res->count) {
167           $OUTPUT->show_message('contactexists', 'notice', null, false);
168           break;
169       }
170   }
171
172   $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
173     'record' => $a_record, 'source' => $source));
174   $a_record = $plugin['record'];
175
176   // insert record and send response
177   if (!$plugin['abort'])
178     $insert_id = $CONTACTS->insert($a_record);
179   else
180     $insert_id = $plugin['result'];
181
182   if ($insert_id) {
183     // add new contact to the specified group
184     if ($CONTACTS->groups && $CONTACTS->group_id) {
185       $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
186         'group_id' => $CONTACTS->group_id, 'ids' => $insert_id, 'source' => $source));
187
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     }
195
196     if ((string)$source === (string)$orig_source) {
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);
200
201       rcmail_js_contacts_list($result, 'parent.');
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     }
212
213     // show confirmation
214     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
215     $OUTPUT->send('iframe');
216   }
217   else {
218     // show error message
219     $err = $CONTACTS->get_error();
220     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : ($err['message'] ? $err['message'] : 'errorsaving'), 'error', null, false);
221     rcmail_overwrite_action('add');
222   }
223 }