Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

folders.inc 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. $protected = $folder['id'] == 'INBOX' || ($protect_default && isset($special_folders[$folder['id']]));
  240. $noselect = false;
  241. $classes = array();
  242. $folder_utf8 = rcube_charset::convert($folder['id'], 'UTF7-IMAP');
  243. $display_folder = rcube::Q($protected ? $RCMAIL->localize_foldername($folder['id']) : $folder['name']);
  244. if ($folder['virtual']) {
  245. $classes[] = 'virtual';
  246. }
  247. // Check \Noselect flag (of existing folder)
  248. if (!$protected && in_array($folder['id'], $a_unsubscribed)) {
  249. $attrs = $STORAGE->folder_attributes($folder['id']);
  250. $noselect = in_array_nocase('\\Noselect', $attrs);
  251. }
  252. $disabled = (($protected && $subscribed) || $noselect);
  253. // Below we will disable subscription option for "virtual" folders
  254. // according to namespaces, but only if they aren't already subscribed.
  255. // User should be able to unsubscribe from the folder
  256. // even if it doesn't exists or is not accessible (OTRS:1000059)
  257. if (!$subscribed && !$disabled && !empty($namespace) && $folder['virtual']) {
  258. // check if the folder is a namespace prefix, then disable subscription option on it
  259. if (!$disabled && $folder['level'] == 0) {
  260. $fname = $folder['id'] . $delimiter;
  261. foreach ($namespace as $ns) {
  262. if (is_array($ns)) {
  263. foreach ($ns as $item) {
  264. if ($item[0] === $fname) {
  265. $disabled = true;
  266. break 2;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. // check if the folder is an other users virtual-root folder, then disable subscription option on it
  273. if (!$disabled && $folder['level'] == 1 && !empty($namespace['other'])) {
  274. $parts = explode($delimiter, $folder['id']);
  275. $fname = $parts[0] . $delimiter;
  276. foreach ($namespace['other'] as $item) {
  277. if ($item[0] === $fname) {
  278. $disabled = true;
  279. break;
  280. }
  281. }
  282. }
  283. // check if the folder is shared, then disable subscription option on it (if not subscribed already)
  284. if (!$disabled) {
  285. $tmp_ns = array_merge((array)$namespace['other'], (array)$namespace['shared']);
  286. foreach ($tmp_ns as $item) {
  287. if (strlen($item[0]) && strpos($folder['id'], $item[0]) === 0) {
  288. $disabled = true;
  289. break;
  290. }
  291. }
  292. }
  293. }
  294. $is_collapsed = strpos($collapsed, '&'.rawurlencode($folder['id']).'&') !== false;
  295. $folder_id = rcube_utils::html_identifier($folder['id'], true);
  296. if ($folder_class = $RCMAIL->folder_classname($folder['id'])) {
  297. $classes[] = $folder_class;
  298. }
  299. $folders[$folder['id']] = array(
  300. 'idx' => $folder_id,
  301. 'folder_imap' => $folder['id'],
  302. 'folder' => $folder_utf8,
  303. 'display' => $display_folder,
  304. 'protected' => $protected || $folder['virtual'],
  305. 'class' => join(' ', $classes),
  306. 'subscribed' => $subscribed,
  307. 'level' => $folder['level'],
  308. 'collapsed' => $is_collapsed,
  309. 'content' => html::a(array('href' => '#'), $display_folder)
  310. . $checkbox_subscribe->show(($subscribed ? $folder['id'] : ''),
  311. array('value' => $folder['id'], 'disabled' => $disabled ? 'disabled' : ''))
  312. );
  313. }
  314. $plugin = $RCMAIL->plugins->exec_hook('folders_list', array('list' => $folders));
  315. // add drop-target representing 'root'
  316. $root = array(
  317. 'idx' => rcube_utils::html_identifier('*', true),
  318. 'folder_imap' => '*',
  319. 'folder' => '',
  320. 'display' => '',
  321. 'protected' => true,
  322. 'class' => 'root',
  323. 'content' => '<span>&nbsp;</span>',
  324. );
  325. $folders = array();
  326. $plugin['list'] = array_values($plugin['list']);
  327. array_unshift($plugin['list'], $root);
  328. for ($i = 0, $length = count($plugin['list']); $i<$length; $i++) {
  329. $folders[] = rcmail_folder_tree_element($plugin['list'], $i, $js_folders);
  330. }
  331. $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
  332. $OUTPUT->set_env('subscriptionrows', $js_folders);
  333. $OUTPUT->set_env('defaultfolders', array_keys($special_folders));
  334. $OUTPUT->set_env('collapsed_folders', $collapsed);
  335. $OUTPUT->set_env('delimiter', $delimiter);
  336. return $form_start . html::tag('ul', $attrib, implode('', $folders), html::$common_attrib) . $form_end;
  337. }
  338. function rcmail_folder_tree_element($folders, &$key, &$js_folders)
  339. {
  340. $data = $folders[$key];
  341. $idx = 'rcmli' . $data['idx'];
  342. $js_folders[$data['folder_imap']] = array($data['folder'], $data['display'], $data['protected']);
  343. $content = $data['content'];
  344. $attribs = array(
  345. 'id' => $idx,
  346. 'class' => trim($data['class'] . ' mailbox')
  347. );
  348. $children = array();
  349. while ($folders[$key+1] && $folders[$key+1]['level'] > $data['level']) {
  350. $key++;
  351. $children[] = rcmail_folder_tree_element($folders, $key, $js_folders);
  352. }
  353. if (!empty($children)) {
  354. $content .= html::div('treetoggle ' . ($data['collapsed'] ? 'collapsed' : 'expanded'), '&nbsp;')
  355. . html::tag('ul', array('style' => ($data['collapsed'] ? "display:none" : null)),
  356. implode("\n", $children));
  357. }
  358. return html::tag('li', $attribs, $content);
  359. }
  360. function rcmail_folder_frame($attrib)
  361. {
  362. global $OUTPUT;
  363. if (!$attrib['id']) {
  364. $attrib['id'] = 'rcmfolderframe';
  365. }
  366. return $OUTPUT->frame($attrib, true);
  367. }
  368. function rcmail_folder_filter($attrib)
  369. {
  370. global $RCMAIL;
  371. $storage = $RCMAIL->get_storage();
  372. $namespace = $storage->get_namespace();
  373. if (empty($namespace['personal']) && empty($namespace['shared']) && empty($namespace['other'])) {
  374. return '';
  375. }
  376. if (!$attrib['id']) {
  377. $attrib['id'] = 'rcmfolderfilter';
  378. }
  379. $attrib['onchange'] = rcmail_output::JS_OBJECT_NAME . '.folder_filter(this.value)';
  380. $roots = array();
  381. $select = new html_select($attrib);
  382. $select->add($RCMAIL->gettext('all'), '---');
  383. foreach (array_keys($namespace) as $type) {
  384. foreach ((array)$namespace[$type] as $ns) {
  385. $root = rtrim($ns[0], $ns[1]);
  386. $label = $RCMAIL->gettext('namespace.' . $type);
  387. if (count($namespace[$type]) > 1) {
  388. $label .= ' (' . rcube_charset::convert($root, 'UTF7-IMAP', RCUBE_CHARSET) . ')';
  389. }
  390. $select->add($label, $root);
  391. if (strlen($root)) {
  392. $roots[] = $root;
  393. }
  394. }
  395. }
  396. $RCMAIL->output->add_gui_object('foldersfilter', $attrib['id']);
  397. $RCMAIL->output->set_env('ns_roots', $roots);
  398. return $select->show();
  399. }
  400. function rcmail_rename_folder($oldname, $newname)
  401. {
  402. global $RCMAIL;
  403. $storage = $RCMAIL->get_storage();
  404. $delimiter = $storage->get_hierarchy_delimiter();
  405. $plugin = $RCMAIL->plugins->exec_hook('folder_rename', array(
  406. 'oldname' => $oldname, 'newname' => $newname));
  407. if (!$plugin['abort']) {
  408. $renamed = $storage->rename_folder($oldname, $newname);
  409. }
  410. else {
  411. $renamed = $plugin['result'];
  412. }
  413. // update per-folder options for modified folder and its subfolders
  414. if ($renamed) {
  415. $a_threaded = (array) $RCMAIL->config->get('message_threading', array());
  416. $oldprefix = '/^' . preg_quote($oldname . $delimiter, '/') . '/';
  417. foreach ($a_threaded as $key => $val) {
  418. if ($key == $oldname) {
  419. unset($a_threaded[$key]);
  420. $a_threaded[$newname] = $val;
  421. }
  422. else if (preg_match($oldprefix, $key)) {
  423. unset($a_threaded[$key]);
  424. $a_threaded[preg_replace($oldprefix, $newname.$delimiter, $key)] = $val;
  425. }
  426. }
  427. $RCMAIL->user->save_prefs(array('message_threading' => $a_threaded));
  428. // #1488692: update session
  429. if ($_SESSION['mbox'] === $oldname) {
  430. $_SESSION['mbox'] = $newname;
  431. }
  432. return true;
  433. }
  434. return false;
  435. }