| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/steps/settings/folders.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 of folders management | |
|---|
| 16 | | | |
|---|
| 17 | +-----------------------------------------------------------------------+ |
|---|
| 18 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 19 | | Author: Aleksander Machniak <alec@alec.pl> | |
|---|
| 20 | +-----------------------------------------------------------------------+ |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | // WARNING: folder names in UI are encoded with RCMAIL_CHARSET |
|---|
| 24 | |
|---|
| 25 | // init IMAP connection |
|---|
| 26 | $STORAGE = $RCMAIL->get_storage(); |
|---|
| 27 | |
|---|
| 28 | // subscribe mailbox |
|---|
| 29 | if ($RCMAIL->action == 'subscribe') |
|---|
| 30 | { |
|---|
| 31 | $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP'); |
|---|
| 32 | if (strlen($mbox)) { |
|---|
| 33 | $result = $STORAGE->subscribe(array($mbox)); |
|---|
| 34 | |
|---|
| 35 | // Handle virtual (non-existing) folders |
|---|
| 36 | if (!$result && $STORAGE->get_error_code() == -1 && |
|---|
| 37 | $STORAGE->get_response_code() == rcube_storage::TRYCREATE |
|---|
| 38 | ) { |
|---|
| 39 | $result = $STORAGE->create_folder($mbox, true); |
|---|
| 40 | if ($result) { |
|---|
| 41 | // @TODO: remove 'virtual' class of folder's row |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | if ($result) { |
|---|
| 46 | // Handle subscription of protected folder (#1487656) |
|---|
| 47 | if ($CONFIG['protect_default_folders'] == true |
|---|
| 48 | && in_array($mbox, $CONFIG['default_folders']) |
|---|
| 49 | ) { |
|---|
| 50 | $OUTPUT->command('disable_subscription', $mbox); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | $OUTPUT->show_message('foldersubscribed', 'confirmation'); |
|---|
| 54 | } |
|---|
| 55 | else |
|---|
| 56 | rcmail_display_server_error('errorsaving'); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | // unsubscribe mailbox |
|---|
| 61 | else if ($RCMAIL->action == 'unsubscribe') |
|---|
| 62 | { |
|---|
| 63 | $mbox = get_input_value('_mbox', RCUBE_INPUT_POST, true, 'UTF7-IMAP'); |
|---|
| 64 | if (strlen($mbox)) { |
|---|
| 65 | $result = $STORAGE->unsubscribe(array($mbox)); |
|---|
| 66 | if ($result) |
|---|
| 67 | $OUTPUT->show_message('folderunsubscribed', 'confirmation'); |
|---|
| 68 | else |
|---|
| 69 | rcmail_display_server_error('errorsaving'); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | // delete an existing mailbox |
|---|
| 74 | else if ($RCMAIL->action == 'delete-folder') |
|---|
| 75 | { |
|---|
| 76 | $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true); |
|---|
| 77 | $mbox = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP'); |
|---|
| 78 | |
|---|
| 79 | if (strlen($mbox)) { |
|---|
| 80 | $plugin = $RCMAIL->plugins->exec_hook('folder_delete', array('name' => $mbox)); |
|---|
| 81 | |
|---|
| 82 | if (!$plugin['abort']) { |
|---|
| 83 | $deleted = $STORAGE->delete_folder($plugin['name']); |
|---|
| 84 | } |
|---|
| 85 | else { |
|---|
| 86 | $deleted = $plugin['result']; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | if ($OUTPUT->ajax_call && $deleted) { |
|---|
| 91 | // Remove folder and subfolders rows |
|---|
| 92 | $OUTPUT->command('remove_folder_row', $mbox_utf8, true); |
|---|
| 93 | $OUTPUT->show_message('folderdeleted', 'confirmation'); |
|---|
| 94 | // Clear content frame |
|---|
| 95 | $OUTPUT->command('subscription_select'); |
|---|
| 96 | $OUTPUT->command('set_quota', rcmail_quota_content()); |
|---|
| 97 | } |
|---|
| 98 | else if (!$deleted) { |
|---|
| 99 | rcmail_display_server_error('errorsaving'); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | // rename an existing mailbox |
|---|
| 104 | else if ($RCMAIL->action == 'rename-folder') |
|---|
| 105 | { |
|---|
| 106 | $name_utf8 = trim(get_input_value('_folder_newname', RCUBE_INPUT_POST, true)); |
|---|
| 107 | $oldname_utf8 = trim(get_input_value('_folder_oldname', RCUBE_INPUT_POST, true)); |
|---|
| 108 | |
|---|
| 109 | if (strlen($name_utf8) && strlen($oldname_utf8)) { |
|---|
| 110 | $name = rcube_charset_convert($name_utf8, RCMAIL_CHARSET, 'UTF7-IMAP'); |
|---|
| 111 | $oldname = rcube_charset_convert($oldname_utf8, RCMAIL_CHARSET, 'UTF7-IMAP'); |
|---|
| 112 | |
|---|
| 113 | $rename = rcmail_rename_folder($oldname, $name); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | if ($rename && $OUTPUT->ajax_call) { |
|---|
| 117 | rcmail_update_folder_row($name, $oldname); |
|---|
| 118 | } |
|---|
| 119 | else if (!$rename) { |
|---|
| 120 | rcmail_display_server_error('errorsaving'); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | // clear mailbox |
|---|
| 125 | else if ($RCMAIL->action == 'purge') |
|---|
| 126 | { |
|---|
| 127 | $mbox_utf8 = get_input_value('_mbox', RCUBE_INPUT_POST, true); |
|---|
| 128 | $mbox = rcube_charset_convert($mbox_utf8, RCMAIL_CHARSET, 'UTF7-IMAP'); |
|---|
| 129 | $delimiter = $STORAGE->get_hierarchy_delimiter(); |
|---|
| 130 | $trash_regexp = '/^' . preg_quote($CONFIG['trash_mbox'] . $delimiter, '/') . '/'; |
|---|
| 131 | |
|---|
| 132 | // we should only be purging trash (or their subfolders) |
|---|
| 133 | if (!strlen($CONFIG['trash_mbox']) || $mbox == $CONFIG['trash_mbox'] |
|---|
| 134 | || preg_match($trash_regexp, $mbox) |
|---|
| 135 | ) { |
|---|
| 136 | $success = $STORAGE->delete_message('*', $mbox); |
|---|
| 137 | $delete = true; |
|---|
| 138 | } |
|---|
| 139 | // copy to Trash |
|---|
| 140 | else { |
|---|
| 141 | $success = $STORAGE->move_message('1:*', $CONFIG['trash_mbox'], $mbox); |
|---|
| 142 | $delete = false; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | if ($success) { |
|---|
| 146 | $OUTPUT->set_env('messagecount', 0); |
|---|
| 147 | if ($delete) { |
|---|
| 148 | $OUTPUT->show_message('folderpurged', 'confirmation'); |
|---|
| 149 | $OUTPUT->command('set_quota', rcmail_quota_content()); |
|---|
| 150 | } |
|---|
| 151 | else { |
|---|
| 152 | $OUTPUT->show_message('messagemoved', 'confirmation'); |
|---|
| 153 | } |
|---|
| 154 | $_SESSION['unseen_count'][$mbox] = 0; |
|---|
| 155 | $OUTPUT->command('show_folder', $mbox_utf8, null, true); |
|---|
| 156 | } |
|---|
| 157 | else { |
|---|
| 158 | rcmail_display_server_error('errorsaving'); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | // get mailbox size |
|---|
| 163 | else if ($RCMAIL->action == 'folder-size') |
|---|
| 164 | { |
|---|
| 165 | $name = trim(get_input_value('_mbox', RCUBE_INPUT_POST, true)); |
|---|
| 166 | |
|---|
| 167 | $size = $STORAGE->folder_size($name); |
|---|
| 168 | |
|---|
| 169 | // @TODO: check quota and show percentage usage of specified mailbox? |
|---|
| 170 | |
|---|
| 171 | if ($size !== false) { |
|---|
| 172 | $OUTPUT->command('folder_size_update', show_bytes($size)); |
|---|
| 173 | } |
|---|
| 174 | else { |
|---|
| 175 | rcmail_display_server_error(); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | if ($OUTPUT->ajax_call) |
|---|
| 180 | $OUTPUT->send(); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | // build table with all folders listed by server |
|---|
| 184 | function rcube_subscription_form($attrib) |
|---|
| 185 | { |
|---|
| 186 | global $RCMAIL, $OUTPUT; |
|---|
| 187 | |
|---|
| 188 | list($form_start, $form_end) = get_form_tags($attrib, 'folders'); |
|---|
| 189 | unset($attrib['form']); |
|---|
| 190 | |
|---|
| 191 | if (!$attrib['id']) |
|---|
| 192 | $attrib['id'] = 'rcmSubscriptionlist'; |
|---|
| 193 | |
|---|
| 194 | $table = new html_table(); |
|---|
| 195 | |
|---|
| 196 | if ($attrib['noheader'] !== true && $attrib['noheader'] != "true") { |
|---|
| 197 | // add table header |
|---|
| 198 | $table->add_header('name', rcube_label('foldername')); |
|---|
| 199 | $table->add_header('subscribed', ''); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | $STORAGE = $RCMAIL->get_storage(); |
|---|
| 203 | |
|---|
| 204 | // get folders from server |
|---|
| 205 | $STORAGE->clear_cache('mailboxes', true); |
|---|
| 206 | |
|---|
| 207 | $a_unsubscribed = $STORAGE->list_folders(); |
|---|
| 208 | $a_subscribed = $STORAGE->list_folders_subscribed('', '*', null, null, true); // unsorted |
|---|
| 209 | $delimiter = $STORAGE->get_hierarchy_delimiter(); |
|---|
| 210 | $namespace = $STORAGE->get_namespace(); |
|---|
| 211 | $a_js_folders = array(); |
|---|
| 212 | $seen = array(); |
|---|
| 213 | $list_folders = array(); |
|---|
| 214 | |
|---|
| 215 | $default_folders = (array) $RCMAIL->config->get('default_folders'); |
|---|
| 216 | $protect_default = $RCMAIL->config->get('protect_default_folders'); |
|---|
| 217 | |
|---|
| 218 | // pre-process folders list |
|---|
| 219 | foreach ($a_unsubscribed as $i => $folder) { |
|---|
| 220 | $folder_id = $folder; |
|---|
| 221 | $folder = $STORAGE->mod_folder($folder); |
|---|
| 222 | $foldersplit = explode($delimiter, $folder); |
|---|
| 223 | $name = rcube_charset_convert(array_pop($foldersplit), 'UTF7-IMAP'); |
|---|
| 224 | $parent_folder = join($delimiter, $foldersplit); |
|---|
| 225 | $level = count($foldersplit); |
|---|
| 226 | |
|---|
| 227 | // add any necessary "virtual" parent folders |
|---|
| 228 | if ($parent_folder && !isset($seen[$parent_folder])) { |
|---|
| 229 | for ($i=1; $i<=$level; $i++) { |
|---|
| 230 | $ancestor_folder = join($delimiter, array_slice($foldersplit, 0, $i)); |
|---|
| 231 | if ($ancestor_folder && !$seen[$ancestor_folder]++) { |
|---|
| 232 | $ancestor_name = rcube_charset_convert($foldersplit[$i-1], 'UTF7-IMAP'); |
|---|
| 233 | $list_folders[] = array( |
|---|
| 234 | 'id' => $ancestor_folder, |
|---|
| 235 | 'name' => $ancestor_name, |
|---|
| 236 | 'level' => $i-1, |
|---|
| 237 | 'virtual' => true, |
|---|
| 238 | ); |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | // Handle properly INBOX.INBOX situation |
|---|
| 244 | if (isset($seen[$folder])) { |
|---|
| 245 | continue; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | $seen[$folder]++; |
|---|
| 249 | |
|---|
| 250 | $list_folders[] = array( |
|---|
| 251 | 'id' => $folder_id, |
|---|
| 252 | 'name' => $name, |
|---|
| 253 | 'level' => $level, |
|---|
| 254 | ); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | unset($seen); |
|---|
| 258 | |
|---|
| 259 | // add drop-target representing 'root' |
|---|
| 260 | $table->add_row(array('id' => 'mailboxroot', 'class' => 'virtual root')); |
|---|
| 261 | $table->add('name', ' '); |
|---|
| 262 | $table->add(null, ' '); |
|---|
| 263 | |
|---|
| 264 | $a_js_folders['mailboxroot'] = array('', '', true); |
|---|
| 265 | |
|---|
| 266 | $checkbox_subscribe = new html_checkbox(array( |
|---|
| 267 | 'name' => '_subscribed[]', |
|---|
| 268 | 'title' => rcube_label('changesubscription'), |
|---|
| 269 | 'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)", |
|---|
| 270 | )); |
|---|
| 271 | |
|---|
| 272 | // create list of available folders |
|---|
| 273 | foreach ($list_folders as $i => $folder) { |
|---|
| 274 | $idx = $i + 1; |
|---|
| 275 | $sub_key = array_search($folder['id'], $a_subscribed); |
|---|
| 276 | $subscribed = $sub_key !== false; |
|---|
| 277 | $protected = $protect_default && in_array($folder['id'], $default_folders); |
|---|
| 278 | $noselect = false; |
|---|
| 279 | $classes = array($i%2 ? 'even' : 'odd'); |
|---|
| 280 | |
|---|
| 281 | $folder_js = Q($folder['id']); |
|---|
| 282 | $folder_utf8 = rcube_charset_convert($folder['id'], 'UTF7-IMAP'); |
|---|
| 283 | $display_folder = str_repeat(' ', $folder['level']) |
|---|
| 284 | . Q($protected ? rcmail_localize_foldername($folder['id']) : $folder['name']); |
|---|
| 285 | |
|---|
| 286 | if ($folder['virtual']) { |
|---|
| 287 | $classes[] = 'virtual'; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | if (!$protected) { |
|---|
| 291 | $attrs = $STORAGE->folder_attributes($folder['id']); |
|---|
| 292 | $noselect = in_array('\\Noselect', $attrs); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | $disabled = (($protected && $subscribed) || $noselect); |
|---|
| 296 | |
|---|
| 297 | // check if the folder is a namespace prefix, then disable subscription option on it |
|---|
| 298 | if (!$disabled && $folder['virtual'] && $folder['level'] == 0 && !empty($namespace)) { |
|---|
| 299 | $fname = $folder['id'] . $delimiter; |
|---|
| 300 | foreach ($namespace as $ns) { |
|---|
| 301 | if (is_array($ns)) { |
|---|
| 302 | foreach ($ns as $item) { |
|---|
| 303 | if ($item[0] === $fname) { |
|---|
| 304 | $disabled = true; |
|---|
| 305 | break 2; |
|---|
| 306 | } |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | } |
|---|
| 311 | // check if the folder is an other users virtual-root folder, then disable subscription option on it |
|---|
| 312 | if (!$disabled && $folder['virtual'] && $folder['level'] == 1 |
|---|
| 313 | && !empty($namespace) && !empty($namespace['other']) |
|---|
| 314 | ) { |
|---|
| 315 | $parts = explode($delimiter, $folder['id']); |
|---|
| 316 | $fname = $parts[0] . $delimiter; |
|---|
| 317 | foreach ($namespace['other'] as $item) { |
|---|
| 318 | if ($item[0] === $fname) { |
|---|
| 319 | $disabled = true; |
|---|
| 320 | break; |
|---|
| 321 | } |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | // check if the folder is shared, then disable subscription option on it |
|---|
| 325 | if (!$disabled && $folder['virtual'] && !empty($namespace)) { |
|---|
| 326 | $tmp_ns = array_merge((array)$namespace['other'], (array)$namespace['shared']); |
|---|
| 327 | foreach ($tmp_ns as $item) { |
|---|
| 328 | if (strpos($folder['id'], $item[0]) === 0) { |
|---|
| 329 | $disabled = true; |
|---|
| 330 | break; |
|---|
| 331 | } |
|---|
| 332 | } |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | $table->add_row(array('id' => 'rcmrow'.$idx, 'class' => join(' ', $classes), |
|---|
| 336 | 'foldername' => $folder['id'])); |
|---|
| 337 | |
|---|
| 338 | $table->add('name', $display_folder); |
|---|
| 339 | $table->add('subscribed', $checkbox_subscribe->show(($subscribed ? $folder_utf8 : ''), |
|---|
| 340 | array('value' => $folder_utf8, 'disabled' => $disabled ? 'disabled' : ''))); |
|---|
| 341 | |
|---|
| 342 | $a_js_folders['rcmrow'.$idx] = array($folder_utf8, |
|---|
| 343 | Q($display_folder), $protected || $folder['virtual']); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | $RCMAIL->plugins->exec_hook('folders_list', array('table' => $table)); |
|---|
| 347 | |
|---|
| 348 | $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']); |
|---|
| 349 | $OUTPUT->set_env('subscriptionrows', $a_js_folders); |
|---|
| 350 | $OUTPUT->set_env('defaultfolders', $default_folders); |
|---|
| 351 | $OUTPUT->set_env('delimiter', $delimiter); |
|---|
| 352 | |
|---|
| 353 | return $form_start . $table->show($attrib) . $form_end; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | function rcmail_folder_frame($attrib) |
|---|
| 357 | { |
|---|
| 358 | global $OUTPUT; |
|---|
| 359 | |
|---|
| 360 | if (!$attrib['id']) |
|---|
| 361 | $attrib['id'] = 'rcmfolderframe'; |
|---|
| 362 | |
|---|
| 363 | $attrib['name'] = $attrib['id']; |
|---|
| 364 | |
|---|
| 365 | $OUTPUT->set_env('contentframe', $attrib['name']); |
|---|
| 366 | $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif'); |
|---|
| 367 | |
|---|
| 368 | return html::iframe($attrib); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | function rcmail_rename_folder($oldname, $newname) |
|---|
| 372 | { |
|---|
| 373 | global $RCMAIL; |
|---|
| 374 | |
|---|
| 375 | $storage = $RCMAIL->get_storage(); |
|---|
| 376 | $delimiter = $storage->get_hierarchy_delimiter(); |
|---|
| 377 | |
|---|
| 378 | $plugin = $RCMAIL->plugins->exec_hook('folder_rename', array( |
|---|
| 379 | 'oldname' => $oldname, 'newname' => $newname)); |
|---|
| 380 | |
|---|
| 381 | if (!$plugin['abort']) { |
|---|
| 382 | $renamed = $storage->rename_folder($oldname, $newname); |
|---|
| 383 | } |
|---|
| 384 | else { |
|---|
| 385 | $renamed = $plugin['result']; |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | // update per-folder options for modified folder and its subfolders |
|---|
| 389 | if ($renamed) { |
|---|
| 390 | $a_threaded = (array) $RCMAIL->config->get('message_threading', array()); |
|---|
| 391 | $oldprefix = '/^' . preg_quote($oldname . $delimiter, '/') . '/'; |
|---|
| 392 | |
|---|
| 393 | foreach ($a_threaded as $key => $val) { |
|---|
| 394 | if ($key == $oldname) { |
|---|
| 395 | unset($a_threaded[$key]); |
|---|
| 396 | $a_threaded[$newname] = true; |
|---|
| 397 | } |
|---|
| 398 | else if (preg_match($oldprefix, $key)) { |
|---|
| 399 | unset($a_threaded[$key]); |
|---|
| 400 | $a_threaded[preg_replace($oldprefix, $newname.$delimiter, $key)] = true; |
|---|
| 401 | } |
|---|
| 402 | } |
|---|
| 403 | $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded)); |
|---|
| 404 | |
|---|
| 405 | return true; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | return false; |
|---|
| 409 | } |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | $OUTPUT->set_pagetitle(rcube_label('folders')); |
|---|
| 413 | $OUTPUT->include_script('list.js'); |
|---|
| 414 | $OUTPUT->set_env('quota', $STORAGE->get_capability('QUOTA')); |
|---|
| 415 | $OUTPUT->set_env('prefix_ns', $STORAGE->get_namespace('prefix')); |
|---|
| 416 | |
|---|
| 417 | // add some labels to client |
|---|
| 418 | $OUTPUT->add_label('deletefolderconfirm', 'purgefolderconfirm', 'folderdeleting', |
|---|
| 419 | 'foldermoving', 'foldersubscribing', 'folderunsubscribing', 'quota'); |
|---|
| 420 | |
|---|
| 421 | // register UI objects |
|---|
| 422 | $OUTPUT->add_handlers(array( |
|---|
| 423 | 'foldersubscription' => 'rcube_subscription_form', |
|---|
| 424 | 'folderframe' => 'rcmail_folder_frame', |
|---|
| 425 | 'quotadisplay' => 'rcmail_quota_display', |
|---|
| 426 | )); |
|---|
| 427 | |
|---|
| 428 | $OUTPUT->send('folders'); |
|---|
| 429 | |
|---|