move_del.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/mail/move_del.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2005-2014, 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. | Move the submitted messages to a specific mailbox or delete them |
  15. | |
  16. +-----------------------------------------------------------------------+
  17. | Author: Thomas Bruederli <roundcube@gmail.com> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. // only process ajax requests
  21. if (!$OUTPUT->ajax_call) {
  22. return;
  23. }
  24. // count messages before changing anything
  25. $threading = (bool) $RCMAIL->storage->get_threading();
  26. $trash = $RCMAIL->config->get('trash_mbox');
  27. $sources = array();
  28. if ($_POST['_from'] != 'show') {
  29. $old_count = $RCMAIL->storage->count(NULL, $threading ? 'THREADS' : 'ALL');
  30. $old_pages = ceil($old_count / $RCMAIL->storage->get_pagesize());
  31. }
  32. // move messages
  33. if ($RCMAIL->action == 'move' && !empty($_POST['_uid']) && strlen($_POST['_target_mbox'])) {
  34. $target = rcube_utils::get_input_value('_target_mbox', rcube_utils::INPUT_POST, true);
  35. $success = true;
  36. foreach (rcmail::get_uids(null, null, $multifolder) as $mbox => $uids) {
  37. if ($mbox === $target) {
  38. $count += count($uids);
  39. }
  40. else if ($RCMAIL->storage->move_message($uids, $target, $mbox)) {
  41. $count += count($uids);
  42. $sources[] = $mbox;
  43. }
  44. else {
  45. $success = false;
  46. }
  47. }
  48. if (!$success) {
  49. // send error message
  50. if ($_POST['_from'] != 'show')
  51. $OUTPUT->command('list_mailbox');
  52. $RCMAIL->display_server_error('errormoving', null, $target == $trash ? 'delete' : '');
  53. $OUTPUT->send();
  54. }
  55. else {
  56. $OUTPUT->show_message($target == $trash ? 'messagemovedtotrash' : 'messagemoved', 'confirmation');
  57. }
  58. if (!empty($_POST['_refresh'])) {
  59. // FIXME: send updated message rows instead of reloading the entire list
  60. $OUTPUT->command('refresh_list');
  61. }
  62. else {
  63. $addrows = true;
  64. }
  65. }
  66. // delete messages
  67. else if ($RCMAIL->action == 'delete' && !empty($_POST['_uid'])) {
  68. foreach (rcmail::get_uids(null, null, $multifolder) as $mbox => $uids) {
  69. $del += (int)$RCMAIL->storage->delete_message($uids, $mbox);
  70. $count += count($uids);
  71. $sources[] = $mbox;
  72. }
  73. if (!$del) {
  74. // send error message
  75. if ($_POST['_from'] != 'show')
  76. $OUTPUT->command('list_mailbox');
  77. $RCMAIL->display_server_error('errordeleting');
  78. $OUTPUT->send();
  79. }
  80. else {
  81. $OUTPUT->show_message('messagedeleted', 'confirmation');
  82. }
  83. $addrows = true;
  84. }
  85. // unknown action or missing query param
  86. else {
  87. $OUTPUT->show_message('internalerror', 'error');
  88. $OUTPUT->send();
  89. }
  90. $search_request = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC);
  91. // refresh saved search set after moving some messages
  92. if ($search_request && $RCMAIL->storage->get_search_set()) {
  93. $_SESSION['search'] = $RCMAIL->storage->refresh_search();
  94. }
  95. if ($_POST['_from'] == 'show') {
  96. if ($next = rcube_utils::get_input_value('_next_uid', rcube_utils::INPUT_GPC)) {
  97. $OUTPUT->command('show_message', $next);
  98. }
  99. else {
  100. $OUTPUT->command('command', 'list');
  101. }
  102. $OUTPUT->send();
  103. }
  104. $mbox = $RCMAIL->storage->get_folder();
  105. $msg_count = $RCMAIL->storage->count(NULL, $threading ? 'THREADS' : 'ALL');
  106. $exists = $RCMAIL->storage->count($mbox, 'EXISTS', true);
  107. $page_size = $RCMAIL->storage->get_pagesize();
  108. $page = $RCMAIL->storage->get_page();
  109. $pages = ceil($msg_count / $page_size);
  110. $nextpage_count = $old_count - $page_size * $page;
  111. $remaining = $msg_count - $page_size * ($page - 1);
  112. // jump back one page (user removed the whole last page)
  113. if ($page > 1 && $remaining == 0) {
  114. $page -= 1;
  115. $RCMAIL->storage->set_page($page);
  116. $_SESSION['page'] = $page;
  117. $jump_back = true;
  118. }
  119. // update message count display
  120. $OUTPUT->set_env('messagecount', $msg_count);
  121. $OUTPUT->set_env('current_page', $page);
  122. $OUTPUT->set_env('pagecount', $pages);
  123. $OUTPUT->set_env('exists', $exists);
  124. // update mailboxlist
  125. $unseen_count = $msg_count ? $RCMAIL->storage->count($mbox, 'UNSEEN') : 0;
  126. $old_unseen = rcmail_get_unseen_count($mbox);
  127. if ($old_unseen != $unseen_count) {
  128. $OUTPUT->command('set_unread_count', $mbox, $unseen_count, ($mbox == 'INBOX'));
  129. rcmail_set_unseen_count($mbox, $unseen_count);
  130. }
  131. if ($RCMAIL->action == 'move' && strlen($target)) {
  132. rcmail_send_unread_count($target, true);
  133. }
  134. $OUTPUT->command('set_quota', $RCMAIL->quota_content(null, $multifolder ? $sources[0] : 'INBOX'));
  135. $OUTPUT->command('set_rowcount', rcmail_get_messagecount_text($msg_count), $mbox);
  136. if ($threading) {
  137. $count = rcube_utils::get_input_value('_count', rcube_utils::INPUT_POST);
  138. }
  139. // add new rows from next page (if any)
  140. if ($addrows && $count && $uids != '*' && ($jump_back || $nextpage_count > 0)) {
  141. $a_headers = $RCMAIL->storage->list_messages($mbox, NULL,
  142. rcmail_sort_column(), rcmail_sort_order(), $jump_back ? NULL : $count);
  143. rcmail_js_message_list($a_headers, false);
  144. }
  145. // set trash folder state
  146. if ($mbox === $trash) {
  147. $OUTPUT->command('set_trash_count', $exists);
  148. }
  149. else if ($target !== null && $target === $trash) {
  150. $OUTPUT->command('set_trash_count', $RCMAIL->storage->count($trash, 'EXISTS', true));
  151. }
  152. // send response
  153. $OUTPUT->send();