You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

save_folder.inc 7.2KB

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