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.

archive.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /**
  3. * Archive
  4. *
  5. * Plugin that adds a new button to the mailbox toolbar
  6. * to move messages to a (user selectable) archive folder.
  7. *
  8. * @version 3.0
  9. * @license GNU GPLv3+
  10. * @author Andre Rodier, Thomas Bruederli, Aleksander Machniak
  11. */
  12. class archive extends rcube_plugin
  13. {
  14. public $task = 'settings|mail|login';
  15. function init()
  16. {
  17. $rcmail = rcmail::get_instance();
  18. // register special folder type
  19. rcube_storage::$folder_types[] = 'archive';
  20. if ($rcmail->task == 'mail' && ($rcmail->action == '' || $rcmail->action == 'show')
  21. && ($archive_folder = $rcmail->config->get('archive_mbox'))
  22. ) {
  23. $skin_path = $this->local_skin_path();
  24. if (is_file($this->home . "/$skin_path/archive.css")) {
  25. $this->include_stylesheet("$skin_path/archive.css");
  26. }
  27. $this->include_script('archive.js');
  28. $this->add_texts('localization', true);
  29. $this->add_button(
  30. array(
  31. 'type' => 'link',
  32. 'label' => 'buttontext',
  33. 'command' => 'plugin.archive',
  34. 'class' => 'button buttonPas archive disabled',
  35. 'classact' => 'button archive',
  36. 'width' => 32,
  37. 'height' => 32,
  38. 'title' => 'buttontitle',
  39. 'domain' => $this->ID,
  40. ),
  41. 'toolbar');
  42. // register hook to localize the archive folder
  43. $this->add_hook('render_mailboxlist', array($this, 'render_mailboxlist'));
  44. // set env variables for client
  45. $rcmail->output->set_env('archive_folder', $archive_folder);
  46. $rcmail->output->set_env('archive_type', $rcmail->config->get('archive_type',''));
  47. }
  48. else if ($rcmail->task == 'mail') {
  49. // handler for ajax request
  50. $this->register_action('plugin.move2archive', array($this, 'move_messages'));
  51. }
  52. else if ($rcmail->task == 'settings') {
  53. $this->add_hook('preferences_list', array($this, 'prefs_table'));
  54. $this->add_hook('preferences_save', array($this, 'save_prefs'));
  55. }
  56. }
  57. /**
  58. * Hook to give the archive folder a localized name in the mailbox list
  59. */
  60. function render_mailboxlist($p)
  61. {
  62. $rcmail = rcmail::get_instance();
  63. $archive_folder = $rcmail->config->get('archive_mbox');
  64. $show_real_name = $rcmail->config->get('show_real_foldernames');
  65. // set localized name for the configured archive folder
  66. if ($archive_folder && !$show_real_name) {
  67. if (isset($p['list'][$archive_folder])) {
  68. $p['list'][$archive_folder]['name'] = $this->gettext('archivefolder');
  69. }
  70. else {
  71. // search in subfolders
  72. $this->_mod_folder_name($p['list'], $archive_folder, $this->gettext('archivefolder'));
  73. }
  74. }
  75. return $p;
  76. }
  77. /**
  78. * Helper method to find the archive folder in the mailbox tree
  79. */
  80. private function _mod_folder_name(&$list, $folder, $new_name)
  81. {
  82. foreach ($list as $idx => $item) {
  83. if ($item['id'] == $folder) {
  84. $list[$idx]['name'] = $new_name;
  85. return true;
  86. }
  87. else if (!empty($item['folders'])) {
  88. if ($this->_mod_folder_name($list[$idx]['folders'], $folder, $new_name)) {
  89. return true;
  90. }
  91. }
  92. }
  93. return false;
  94. }
  95. /**
  96. * Plugin action to move the submitted list of messages to the archive subfolders
  97. * according to the user settings and their headers.
  98. */
  99. function move_messages()
  100. {
  101. $rcmail = rcmail::get_instance();
  102. // only process ajax requests
  103. if (!$rcmail->output->ajax_call) {
  104. return;
  105. }
  106. $this->add_texts('localization');
  107. $storage = $rcmail->get_storage();
  108. $delimiter = $storage->get_hierarchy_delimiter();
  109. $read_on_move = (bool) $rcmail->config->get('read_on_archive');
  110. $archive_type = $rcmail->config->get('archive_type', '');
  111. $archive_folder = $rcmail->config->get('archive_mbox');
  112. $archive_prefix = $archive_folder . $delimiter;
  113. $search_request = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC);
  114. // count messages before changing anything
  115. if ($_POST['_from'] != 'show') {
  116. $threading = (bool) $storage->get_threading();
  117. $old_count = $storage->count(null, $threading ? 'THREADS' : 'ALL');
  118. $old_pages = ceil($old_count / $storage->get_pagesize());
  119. }
  120. $count = 0;
  121. // this way response handler for 'move' action will be executed
  122. $rcmail->action = 'move';
  123. $this->result = array(
  124. 'reload' => false,
  125. 'error' => false,
  126. 'sources' => array(),
  127. 'destinations' => array(),
  128. );
  129. foreach (rcmail::get_uids(null, null, $multifolder, rcube_utils::INPUT_POST) as $mbox => $uids) {
  130. if (!$archive_folder || strpos($mbox, $archive_prefix) === 0) {
  131. $count = count($uids);
  132. continue;
  133. }
  134. else if (!$archive_type || $archive_type == 'folder') {
  135. $folder = $archive_folder;
  136. if ($archive_type == 'folder') {
  137. // compose full folder path
  138. $folder .= $delimiter . $mbox;
  139. // create archive subfolder if it doesn't yet exist
  140. $this->subfolder_worker($folder);
  141. }
  142. $count += $this->move_messages_worker($uids, $mbox, $folder, $read_on_move);
  143. }
  144. else {
  145. if ($uids == '*') {
  146. $index = $storage->index(null, rcmail_sort_column(), rcmail_sort_order());
  147. $uids = $index->get();
  148. }
  149. $messages = $storage->fetch_headers($mbox, $uids);
  150. $execute = array();
  151. foreach ($messages as $message) {
  152. $subfolder = null;
  153. switch ($archive_type) {
  154. case 'year':
  155. $subfolder = $rcmail->format_date($message->timestamp, 'Y');
  156. break;
  157. case 'month':
  158. $subfolder = $rcmail->format_date($message->timestamp, 'Y')
  159. . $delimiter . $rcmail->format_date($message->timestamp, 'm');
  160. break;
  161. case 'sender':
  162. $from = $message->get('from');
  163. preg_match('/[\b<](.+@.+)[\b>]/i', $from, $m);
  164. $subfolder = $m[1] ?: $this->gettext('unkownsender');
  165. // replace reserved characters in folder name
  166. $repl = $delimiter == '-' ? '_' : '-';
  167. $replacements[$delimiter] = $repl;
  168. $replacements['.'] = $repl; // some IMAP server do not allow . characters
  169. $subfolder = strtr($subfolder, $replacements);
  170. break;
  171. }
  172. // compose full folder path
  173. $folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : '');
  174. $execute[$folder][] = $message->uid;
  175. }
  176. foreach ($execute as $folder => $uids) {
  177. // create archive subfolder if it doesn't yet exist
  178. $this->subfolder_worker($folder);
  179. $count += $this->move_messages_worker($uids, $mbox, $folder, $read_on_move);
  180. }
  181. }
  182. }
  183. if ($this->result['error']) {
  184. if ($_POST['_from'] != 'show') {
  185. $rcmail->output->command('list_mailbox');
  186. }
  187. $rcmail->output->show_message($this->gettext('archiveerror'), 'warning');
  188. $rcmail->output->send();
  189. }
  190. if (!empty($_POST['_refresh'])) {
  191. // FIXME: send updated message rows instead of reloading the entire list
  192. $rcmail->output->command('refresh_list');
  193. }
  194. else {
  195. $addrows = true;
  196. }
  197. // refresh saved search set after moving some messages
  198. if ($search_request && $rcmail->storage->get_search_set()) {
  199. $_SESSION['search'] = $rcmail->storage->refresh_search();
  200. }
  201. if ($_POST['_from'] == 'show') {
  202. if ($next = rcube_utils::get_input_value('_next_uid', rcube_utils::INPUT_GPC)) {
  203. $rcmail->output->command('show_message', $next);
  204. }
  205. else {
  206. $rcmail->output->command('command', 'list');
  207. }
  208. $rcmail->output->send();
  209. }
  210. $mbox = $storage->get_folder();
  211. $msg_count = $storage->count(null, $threading ? 'THREADS' : 'ALL');
  212. $exists = $storage->count($mbox, 'EXISTS', true);
  213. $page_size = $storage->get_pagesize();
  214. $page = $storage->get_page();
  215. $pages = ceil($msg_count / $page_size);
  216. $nextpage_count = $old_count - $page_size * $page;
  217. $remaining = $msg_count - $page_size * ($page - 1);
  218. // jump back one page (user removed the whole last page)
  219. if ($page > 1 && $remaining == 0) {
  220. $page -= 1;
  221. $storage->set_page($page);
  222. $_SESSION['page'] = $page;
  223. $jump_back = true;
  224. }
  225. // update message count display
  226. $rcmail->output->set_env('messagecount', $msg_count);
  227. $rcmail->output->set_env('current_page', $page);
  228. $rcmail->output->set_env('pagecount', $pages);
  229. $rcmail->output->set_env('exists', $exists);
  230. // update mailboxlist
  231. $unseen_count = $msg_count ? $storage->count($mbox, 'UNSEEN') : 0;
  232. $old_unseen = rcmail_get_unseen_count($mbox);
  233. $quota_root = $multifolder ? $this->result['sources'][0] : 'INBOX';
  234. if ($old_unseen != $unseen_count) {
  235. $rcmail->output->command('set_unread_count', $mbox, $unseen_count, ($mbox == 'INBOX'));
  236. rcmail_set_unseen_count($mbox, $unseen_count);
  237. }
  238. $rcmail->output->command('set_quota', $rcmail->quota_content(null, $quota_root));
  239. $rcmail->output->command('set_rowcount', rcmail_get_messagecount_text($msg_count), $mbox);
  240. if ($threading) {
  241. $count = rcube_utils::get_input_value('_count', rcube_utils::INPUT_POST);
  242. }
  243. // add new rows from next page (if any)
  244. if ($addrows && $count && $uids != '*' && ($jump_back || $nextpage_count > 0)) {
  245. // #5862: Don't add more rows than it was on the next page
  246. $count = $jump_back ? null : min($nextpage_count, $count);
  247. $a_headers = $storage->list_messages($mbox, null,
  248. rcmail_sort_column(), rcmail_sort_order(), $count);
  249. rcmail_js_message_list($a_headers, false);
  250. }
  251. if ($this->result['reload']) {
  252. $rcmail->output->show_message($this->gettext('archivedreload'), 'confirmation');
  253. }
  254. else {
  255. $rcmail->output->show_message($this->gettext('archived'), 'confirmation');
  256. if (!$read_on_move) {
  257. foreach ($this->result['destinations'] as $folder) {
  258. rcmail_send_unread_count($folder, true);
  259. }
  260. }
  261. }
  262. // send response
  263. $rcmail->output->send();
  264. }
  265. /**
  266. * Move messages from one folder to another and mark as read if needed
  267. */
  268. private function move_messages_worker($uids, $from_mbox, $to_mbox, $read_on_move)
  269. {
  270. $storage = rcmail::get_instance()->get_storage();
  271. if ($read_on_move) {
  272. // don't flush cache (4th argument)
  273. $storage->set_flag($uids, 'SEEN', $from_mbox, true);
  274. }
  275. // move message to target folder
  276. if ($storage->move_message($uids, $to_mbox, $from_mbox)) {
  277. if (!in_array($from_mbox, $this->result['sources'])) {
  278. $this->result['sources'][] = $from_mbox;
  279. }
  280. if (!in_array($to_mbox, $this->result['destinations'])) {
  281. $this->result['destinations'][] = $to_mbox;
  282. }
  283. return count($uids);
  284. }
  285. $this->result['error'] = true;
  286. }
  287. /**
  288. * Create archive subfolder if it doesn't yet exist
  289. */
  290. private function subfolder_worker($folder)
  291. {
  292. $storage = rcmail::get_instance()->get_storage();
  293. $delimiter = $storage->get_hierarchy_delimiter();
  294. if ($this->folders === null) {
  295. $this->folders = $storage->list_folders('', $archive_folder . '*', 'mail', null, true);
  296. }
  297. if (!in_array($folder, $this->folders)) {
  298. $path = explode($delimiter, $folder);
  299. // we'll create all folders in the path
  300. for ($i=0; $i<count($path); $i++) {
  301. $_folder = implode($delimiter, array_slice($path, 0, $i+1));
  302. if (!in_array($_folder, $this->folders)) {
  303. if ($storage->create_folder($_folder, true)) {
  304. $this->result['reload'] = true;
  305. $this->folders[] = $_folder;
  306. }
  307. }
  308. }
  309. }
  310. }
  311. /**
  312. * Hook to inject plugin-specific user settings
  313. */
  314. function prefs_table($args)
  315. {
  316. global $CURR_SECTION;
  317. $this->add_texts('localization');
  318. $rcmail = rcmail::get_instance();
  319. $dont_override = $rcmail->config->get('dont_override', array());
  320. if ($args['section'] == 'folders' && !in_array('archive_mbox', $dont_override)) {
  321. $mbox = $rcmail->config->get('archive_mbox');
  322. $type = $rcmail->config->get('archive_type');
  323. // load folders list when needed
  324. if ($CURR_SECTION) {
  325. $select = $rcmail->folder_selector(array(
  326. 'noselection' => '---',
  327. 'realnames' => true,
  328. 'maxlength' => 30,
  329. 'folder_filter' => 'mail',
  330. 'folder_rights' => 'w',
  331. 'onchange' => "if ($(this).val() == 'INBOX') $(this).val('')",
  332. ));
  333. }
  334. else {
  335. $select = new html_select();
  336. }
  337. $args['blocks']['main']['options']['archive_mbox'] = array(
  338. 'title' => $this->gettext('archivefolder'),
  339. 'content' => $select->show($mbox, array('name' => "_archive_mbox"))
  340. );
  341. // add option for structuring the archive folder
  342. $archive_type = new html_select(array('name' => '_archive_type', 'id' => 'ff_archive_type'));
  343. $archive_type->add($this->gettext('none'), '');
  344. $archive_type->add($this->gettext('archivetypeyear'), 'year');
  345. $archive_type->add($this->gettext('archivetypemonth'), 'month');
  346. $archive_type->add($this->gettext('archivetypesender'), 'sender');
  347. $archive_type->add($this->gettext('archivetypefolder'), 'folder');
  348. $args['blocks']['archive'] = array(
  349. 'name' => rcube::Q($this->gettext('settingstitle')),
  350. 'options' => array('archive_type' => array(
  351. 'title' => $this->gettext('archivetype'),
  352. 'content' => $archive_type->show($type)
  353. )
  354. )
  355. );
  356. }
  357. else if ($args['section'] == 'server' && !in_array('read_on_archive', $dont_override)) {
  358. $chbox = new html_checkbox(array('name' => '_read_on_archive', 'id' => 'ff_read_on_archive', 'value' => 1));
  359. $args['blocks']['main']['options']['read_on_archive'] = array(
  360. 'title' => $this->gettext('readonarchive'),
  361. 'content' => $chbox->show($rcmail->config->get('read_on_archive') ? 1 : 0)
  362. );
  363. }
  364. return $args;
  365. }
  366. /**
  367. * Hook to save plugin-specific user settings
  368. */
  369. function save_prefs($args)
  370. {
  371. $rcmail = rcmail::get_instance();
  372. $dont_override = $rcmail->config->get('dont_override', array());
  373. if ($args['section'] == 'folders' && !in_array('archive_mbox', $dont_override)) {
  374. $args['prefs']['archive_type'] = rcube_utils::get_input_value('_archive_type', rcube_utils::INPUT_POST);
  375. }
  376. else if ($args['section'] == 'server' && !in_array('read_on_archive', $dont_override)) {
  377. $args['prefs']['read_on_archive'] = (bool) rcube_utils::get_input_value('_read_on_archive', rcube_utils::INPUT_POST);
  378. }
  379. return $args;
  380. }
  381. }