Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

folders.inc 18KB

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