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