David Carter
2013-05-29 52deb18d9646116114f9649c87ffc4f0bc378db1
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/settings/save_identity.inc                              |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5e7b3 8  | Copyright (C) 2005-2009, 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 an identity record or to add a new one                         |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19  +-----------------------------------------------------------------------+
20 */
21
ec0171 22 define('IDENTITIES_LEVEL', intval($RCMAIL->config->get('identities_level', 0)));
A 23
a0109c 24 $a_save_cols = array('name', 'email', 'organization', 'reply-to', 'bcc', 'standard', 'signature', 'html_signature');
S 25 $a_boolean_cols = array('standard', 'html_signature');
6ec91f 26 $updated = $default_id = false;
4e17e6 27
10a699 28 // check input
876d31 29 if (IDENTITIES_LEVEL != 4 && (empty($_POST['_name']) || (empty($_POST['_email']) && IDENTITIES_LEVEL != 1 && IDENTITIES_LEVEL != 3))) {
f11541 30   $OUTPUT->show_message('formincomplete', 'warning');
407dcf 31   rcmail_overwrite_action('edit-identity');
10a699 32   return;
516467 33 }
10a699 34
fba1f5 35 $save_data = array();
876d31 36 foreach ($a_save_cols as $col) {
fba1f5 37   $fname = '_'.$col;
T 38   if (isset($_POST[$fname]))
6707ca 39     $save_data[$col] = get_input_value($fname, RCUBE_INPUT_POST, true);
fba1f5 40 }
T 41
42 // set "off" values for checkboxes that were not checked, and therefore
43 // not included in the POST body.
876d31 44 foreach ($a_boolean_cols as $col) {
fba1f5 45   $fname = '_' . $col;
T 46   if (!isset($_POST[$fname]))
47     $save_data[$col] = 0;
48 }
ec0171 49
A 50 // unset email address if user has no rights to change it
876d31 51 if (IDENTITIES_LEVEL == 1 || IDENTITIES_LEVEL == 3) {
ec0171 52   unset($save_data['email']);
876d31 53 }
AM 54 // unset all fields except signature
55 else if (IDENTITIES_LEVEL == 4) {
56   foreach ($save_data as $idx => $value) {
57     if ($idx != 'signature' && $idx != 'html_signature') {
58       unset($save_data[$idx]);
59     }
60   }
c753bc 61 }
JK 62
e99991 63 // Validate e-mail addresses
6707ca 64 $email_checks = array(rcube_idn_to_ascii($save_data['email']));
T 65 foreach (array('reply-to', 'bcc') as $item) {
b8b6e5 66   foreach (rcube_mime::decode_address_list($save_data[$item], null, false) as $rcpt)
TB 67     $email_checks[] = rcube_idn_to_ascii($rcpt['mailto']);
6707ca 68 }
T 69
70 foreach ($email_checks as $email) {
71   if ($email && !check_email($email)) {
72     // show error message
73     $OUTPUT->show_message('emailformaterror', 'error', array('email' => rcube_idn_to_utf8($email)), false);
74     rcmail_overwrite_action('edit-identity');
75     return;
e99991 76   }
A 77 }
fba1f5 78
4e17e6 79 // update an existing contact
876d31 80 if ($_POST['_iid']) {
69f18a 81   $iid = get_input_value('_iid', RCUBE_INPUT_POST);
876d31 82
AM 83   if (in_array(IDENTITIES_LEVEL, array(1,3,4))) {
84     // merge with old identity data, fixes #1488834
85     $identity  = $RCMAIL->user->get_identity($iid);
86     $save_data = array_merge($identity, $save_data);
87     unset($save_data['changed'], $save_data['del'], $save_data['user_id'], $save_data['identity_id']);
88   }
89
119ad1 90   $plugin = $RCMAIL->plugins->exec_hook('identity_update', array('id' => $iid, 'record' => $save_data));
69f18a 91   $save_data = $plugin['record'];
e99991 92
A 93   if ($save_data['email'])
e8d5bd 94     $save_data['email'] = rcube_idn_to_ascii($save_data['email']);
ce92ba 95   if (!$plugin['abort'])
a90ad2 96     $updated = $RCMAIL->user->update_identity($iid, $save_data);
ce92ba 97   else
A 98     $updated = $plugin['result'];
99
100   if ($updated) {
461253 101     $OUTPUT->show_message('successfullysaved', 'confirmation');
ce92ba 102
876d31 103     if (!empty($save_data['standard']))
AM 104       $default_id = $iid;
ce92ba 105
A 106     if ($_POST['_framed']) {
461253 107       // update the changed col in list
7c2a93 108       $OUTPUT->command('parent.update_identity_row', $iid, Q(trim($save_data['name'] . ' <' . rcube_idn_to_utf8($save_data['email']) .'>')));
4e17e6 109     }
461253 110   }
ce92ba 111   else {
461253 112     // show error message
ce92ba 113     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
461253 114     rcmail_overwrite_action('edit-identity');
A 115     return;
4e17e6 116   }
fba1f5 117 }
4e17e6 118
c1b3c4 119 // insert a new identity record
876d31 120 else if (IDENTITIES_LEVEL < 2) {
789e59 121   if (IDENTITIES_LEVEL == 1) {
AM 122     $save_data['email'] = $RCMAIL->get_user_email();
123   }
ec0171 124
e6ce00 125   $plugin = $RCMAIL->plugins->exec_hook('identity_create', array('record' => $save_data));
69f18a 126   $save_data = $plugin['record'];
T 127
2d5bee 128   if ($save_data['email'])
b8b6e5 129     $save_data['email'] = rcube_idn_to_ascii($save_data['email']);
e99991 130
ce92ba 131   if (!$plugin['abort'])
a90ad2 132     $insert_id = $save_data['email'] ? $RCMAIL->user->insert_identity($save_data) : null;
ce92ba 133   else
A 134     $insert_id = $plugin['result'];
135
136   if ($insert_id) {
461253 137     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
2d5bee 138
461253 139     $_GET['_iid'] = $insert_id;
4e17e6 140
876d31 141     if (!empty($save_data['standard']))
461253 142       $default_id = $insert_id;
7c2a93 143
T 144     if ($_POST['_framed']) {
145       // add a new row to the list
146       $OUTPUT->command('parent.update_identity_row', $insert_id, Q(trim($save_data['name'] . ' <' . rcube_idn_to_utf8($save_data['email']) .'>')), true);
147     }
461253 148   }
ce92ba 149   else {
461253 150     // show error message
ce92ba 151     $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
461253 152     rcmail_overwrite_action('edit-identity');
A 153     return;
4e17e6 154   }
fba1f5 155 }
f645ce 156 else
T 157   $OUTPUT->show_message('opnotpermitted', 'error');
4e17e6 158
T 159
6ec91f 160 // mark all other identities as 'not-default'
T 161 if ($default_id)
a90ad2 162   $RCMAIL->user->set_default($default_id);
6ec91f 163
4e17e6 164 // go to next step
7c2a93 165 if (!empty($_REQUEST['_framed'])) {
T 166   rcmail_overwrite_action('edit-identity');
167 }
168 else
169   rcmail_overwrite_action('identities');