| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/steps/settings/save_folder.inc | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the Roundcube Webmail client | |
|---|
| 8 | | Copyright (C) 2005-2009, 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 | | Provide functionality to create/edit a folder | |
|---|
| 16 | | | |
|---|
| 17 | +-----------------------------------------------------------------------+ |
|---|
| 18 | | Author: Aleksander Machniak <alec@alec.pl> | |
|---|
| 19 | +-----------------------------------------------------------------------+ |
|---|
| 20 | |
|---|
| 21 | $Id$ |
|---|
| 22 | |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | // WARNING: folder names in UI are encoded with RCMAIL_CHARSET |
|---|
| 26 | |
|---|
| 27 | // init IMAP connection |
|---|
| 28 | $STORAGE = $RCMAIL->get_storage(); |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | $name = trim(get_input_value('_name', RCUBE_INPUT_POST, true)); |
|---|
| 32 | $old = trim(get_input_value('_mbox', RCUBE_INPUT_POST, true)); |
|---|
| 33 | $path = trim(get_input_value('_parent', RCUBE_INPUT_POST, true)); |
|---|
| 34 | |
|---|
| 35 | $name_imap = rcube_charset_convert($name, RCMAIL_CHARSET, 'UTF7-IMAP'); |
|---|
| 36 | $old_imap = rcube_charset_convert($old, RCMAIL_CHARSET, 'UTF7-IMAP'); |
|---|
| 37 | // $path is in UTF7-IMAP already |
|---|
| 38 | |
|---|
| 39 | $delimiter = $STORAGE->get_hierarchy_delimiter(); |
|---|
| 40 | $options = strlen($old_imap) ? rcmail_folder_options($old_imap) : array(); |
|---|
| 41 | |
|---|
| 42 | // Folder name checks |
|---|
| 43 | if ($options['protected'] || $options['norename']) { |
|---|
| 44 | } |
|---|
| 45 | else if (!strlen($name)) { |
|---|
| 46 | $error = rcube_label('cannotbeempty'); |
|---|
| 47 | } |
|---|
| 48 | else if (mb_strlen($name) > 128) { |
|---|
| 49 | $error = rcube_label('nametoolong'); |
|---|
| 50 | } |
|---|
| 51 | else { |
|---|
| 52 | // these characters are problematic e.g. when used in LIST/LSUB |
|---|
| 53 | foreach (array($delimiter, '%', '*') as $char) { |
|---|
| 54 | if (strpos($name, $delimiter) !== false) { |
|---|
| 55 | $error = rcube_label('forbiddencharacter') . " ($char)"; |
|---|
| 56 | break; |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | if ($error) { |
|---|
| 62 | $OUTPUT->command('display_message', $error, 'error'); |
|---|
| 63 | } |
|---|
| 64 | else { |
|---|
| 65 | if ($options['protected'] || $options['norename']) { |
|---|
| 66 | $name_imap = $old_imap; |
|---|
| 67 | } |
|---|
| 68 | else if (strlen($path)) { |
|---|
| 69 | $name_imap = $path . $delimiter . $name_imap; |
|---|
| 70 | } |
|---|
| 71 | else { |
|---|
| 72 | $name_imap = $STORAGE->mod_folder($name_imap, 'in'); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | // Check access rights to the parent folder |
|---|
| 77 | if (!$error && strlen($path) && (!strlen($old_imap) || $old_imap != $name_imap)) { |
|---|
| 78 | $parent_opts = $STORAGE->folder_info($path); |
|---|
| 79 | if ($parent_opts['namespace'] != 'personal' |
|---|
| 80 | && (empty($parent_opts['rights']) || !preg_match('/[ck]/', implode($parent_opts['rights']))) |
|---|
| 81 | ) { |
|---|
| 82 | $error = rcube_label('parentnotwritable'); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | if (!$error) { |
|---|
| 87 | $folder['name'] = $name_imap; |
|---|
| 88 | $folder['oldname'] = $old_imap; |
|---|
| 89 | $folder['class'] = ''; |
|---|
| 90 | $folder['options'] = $options; |
|---|
| 91 | $folder['settings'] = array( |
|---|
| 92 | // List view mode: 0-list, 1-threads |
|---|
| 93 | 'view_mode' => (int) get_input_value('_viewmode', RCUBE_INPUT_POST), |
|---|
| 94 | 'sort_column' => get_input_value('_sortcol', RCUBE_INPUT_POST), |
|---|
| 95 | 'sort_order' => get_input_value('_sortord', RCUBE_INPUT_POST), |
|---|
| 96 | ); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | // create a new mailbox |
|---|
| 100 | if (!$error && !strlen($old)) { |
|---|
| 101 | |
|---|
| 102 | $folder['subscribe'] = true; |
|---|
| 103 | |
|---|
| 104 | $plugin = $RCMAIL->plugins->exec_hook('folder_create', array('record' => $folder)); |
|---|
| 105 | |
|---|
| 106 | $folder = $plugin['record']; |
|---|
| 107 | |
|---|
| 108 | if (!$plugin['abort']) { |
|---|
| 109 | $created = $STORAGE->create_folder($folder['name'], $folder['subscribe']); |
|---|
| 110 | } |
|---|
| 111 | else { |
|---|
| 112 | $created = $plugin['result']; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | if ($created) { |
|---|
| 116 | // Save folder settings |
|---|
| 117 | if (isset($_POST['_viewmode'])) { |
|---|
| 118 | $a_threaded = (array) $RCMAIL->config->get('message_threading', array()); |
|---|
| 119 | |
|---|
| 120 | if ($_POST['_viewmode']) |
|---|
| 121 | $a_threaded[$folder['name']] = true; |
|---|
| 122 | else |
|---|
| 123 | unset($a_threaded[$folder['name']]); |
|---|
| 124 | |
|---|
| 125 | $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded)); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | rcmail_update_folder_row($folder['name'], null, $folder['subscribe'], $folder['class']); |
|---|
| 129 | $OUTPUT->show_message('foldercreated', 'confirmation'); |
|---|
| 130 | // reset folder preview frame |
|---|
| 131 | $OUTPUT->command('subscription_select'); |
|---|
| 132 | $OUTPUT->send('iframe'); |
|---|
| 133 | } |
|---|
| 134 | else { |
|---|
| 135 | // show error message |
|---|
| 136 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | // update a mailbox |
|---|
| 141 | else if (!$error) { |
|---|
| 142 | $plugin = $RCMAIL->plugins->exec_hook('folder_update', array('record' => $folder)); |
|---|
| 143 | |
|---|
| 144 | $folder = $plugin['record']; |
|---|
| 145 | $rename = ($folder['oldname'] != $folder['name']); |
|---|
| 146 | |
|---|
| 147 | if (!$plugin['abort']) { |
|---|
| 148 | if ($rename) { |
|---|
| 149 | $updated = $STORAGE->rename_folder($folder['oldname'], $folder['name']); |
|---|
| 150 | } |
|---|
| 151 | else { |
|---|
| 152 | $updated = true; |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | else { |
|---|
| 156 | $updated = $plugin['result']; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | if ($updated) { |
|---|
| 160 | // Update folder settings, |
|---|
| 161 | if (isset($_POST['_viewmode'])) { |
|---|
| 162 | $a_threaded = (array) $RCMAIL->config->get('message_threading', array()); |
|---|
| 163 | |
|---|
| 164 | // In case of name change update names of childrens in settings |
|---|
| 165 | if ($rename) { |
|---|
| 166 | $oldprefix = '/^' . preg_quote($folder['oldname'] . $delimiter, '/') . '/'; |
|---|
| 167 | foreach ($a_threaded as $key => $val) { |
|---|
| 168 | if ($key == $folder['oldname']) { |
|---|
| 169 | unset($a_threaded[$key]); |
|---|
| 170 | } |
|---|
| 171 | else if (preg_match($oldprefix, $key)) { |
|---|
| 172 | unset($a_threaded[$key]); |
|---|
| 173 | $a_threaded[preg_replace($oldprefix, $folder['name'].$delimiter, $key)] = true; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | if ($_POST['_viewmode']) |
|---|
| 178 | $a_threaded[$folder['name']] = true; |
|---|
| 179 | else |
|---|
| 180 | unset($a_threaded[$folder['name']]); |
|---|
| 181 | |
|---|
| 182 | $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded)); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | $OUTPUT->show_message('folderupdated', 'confirmation'); |
|---|
| 186 | if ($rename) { |
|---|
| 187 | rcmail_update_folder_row($folder['name'], $folder['oldname'], $folder['subscribe'], $folder['class']); |
|---|
| 188 | $OUTPUT->send('iframe'); |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | else { |
|---|
| 192 | // show error message |
|---|
| 193 | $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false); |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | rcmail_overwrite_action('edit-folder'); |
|---|