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 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 2.4
  9. * @license GNU GPLv3+
  10. * @author Andre Rodier, Thomas Bruederli, Aleksander Machniak
  11. */
  12. class archive extends rcube_plugin
  13. {
  14. function init()
  15. {
  16. $rcmail = rcmail::get_instance();
  17. // register special folder type
  18. rcube_storage::$folder_types[] = 'archive';
  19. if ($rcmail->task == 'mail' && ($rcmail->action == '' || $rcmail->action == 'show')
  20. && ($archive_folder = $rcmail->config->get('archive_mbox'))
  21. ) {
  22. $skin_path = $this->local_skin_path();
  23. if (is_file($this->home . "/$skin_path/archive.css"))
  24. $this->include_stylesheet("$skin_path/archive.css");
  25. $this->include_script('archive.js');
  26. $this->add_texts('localization', true);
  27. $this->add_button(
  28. array(
  29. 'type' => 'link',
  30. 'label' => 'buttontext',
  31. 'command' => 'plugin.archive',
  32. 'class' => 'button buttonPas archive disabled',
  33. 'classact' => 'button archive',
  34. 'width' => 32,
  35. 'height' => 32,
  36. 'title' => 'buttontitle',
  37. 'domain' => $this->ID,
  38. ),
  39. 'toolbar');
  40. // register hook to localize the archive folder
  41. $this->add_hook('render_mailboxlist', array($this, 'render_mailboxlist'));
  42. // set env variables for client
  43. $rcmail->output->set_env('archive_folder', $archive_folder);
  44. $rcmail->output->set_env('archive_type', $rcmail->config->get('archive_type',''));
  45. }
  46. else if ($rcmail->task == 'mail') {
  47. // handler for ajax request
  48. $this->register_action('plugin.move2archive', array($this, 'move_messages'));
  49. }
  50. else if ($rcmail->task == 'settings') {
  51. $dont_override = $rcmail->config->get('dont_override', array());
  52. if (!in_array('archive_mbox', $dont_override)) {
  53. $this->add_hook('preferences_list', array($this, 'prefs_table'));
  54. $this->add_hook('preferences_save', array($this, 'save_prefs'));
  55. }
  56. }
  57. }
  58. /**
  59. * Hook to give the archive folder a localized name in the mailbox list
  60. */
  61. function render_mailboxlist($p)
  62. {
  63. $rcmail = rcmail::get_instance();
  64. $archive_folder = $rcmail->config->get('archive_mbox');
  65. $show_real_name = $rcmail->config->get('show_real_foldernames');
  66. // set localized name for the configured archive folder
  67. if ($archive_folder && !$show_real_name) {
  68. if (isset($p['list'][$archive_folder]))
  69. $p['list'][$archive_folder]['name'] = $this->gettext('archivefolder');
  70. else // search in subfolders
  71. $this->_mod_folder_name($p['list'], $archive_folder, $this->gettext('archivefolder'));
  72. }
  73. return $p;
  74. }
  75. /**
  76. * Helper method to find the archive folder in the mailbox tree
  77. */
  78. private function _mod_folder_name(&$list, $folder, $new_name)
  79. {
  80. foreach ($list as $idx => $item) {
  81. if ($item['id'] == $folder) {
  82. $list[$idx]['name'] = $new_name;
  83. return true;
  84. } else if (!empty($item['folders']))
  85. if ($this->_mod_folder_name($list[$idx]['folders'], $folder, $new_name))
  86. return true;
  87. }
  88. return false;
  89. }
  90. /**
  91. * Plugin action to move the submitted list of messages to the archive subfolders
  92. * according to the user settings and their headers.
  93. */
  94. function move_messages()
  95. {
  96. $this->add_texts('localization');
  97. $rcmail = rcmail::get_instance();
  98. $storage = $rcmail->get_storage();
  99. $delimiter = $storage->get_hierarchy_delimiter();
  100. $archive_folder = $rcmail->config->get('archive_mbox');
  101. $archive_type = $rcmail->config->get('archive_type', '');
  102. $current_mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST);
  103. $result = array('reload' => false, 'update' => false, 'errors' => array());
  104. $folders = array();
  105. $uids = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_POST);
  106. $search_request = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC);
  107. if ($uids == '*') {
  108. $index = $storage->index(null, rcmail_sort_column(), rcmail_sort_order());
  109. $messageset = array($current_mbox => $index->get());
  110. }
  111. else {
  112. $messageset = rcmail::get_uids();
  113. }
  114. foreach ($messageset as $mbox => $uids) {
  115. $storage->set_folder(($current_mbox = $mbox));
  116. foreach ($uids as $uid) {
  117. if (!$archive_folder || !($message = $rcmail->storage->get_message($uid))) {
  118. continue;
  119. }
  120. $subfolder = null;
  121. switch ($archive_type) {
  122. case 'year':
  123. $subfolder = $rcmail->format_date($message->timestamp, 'Y');
  124. break;
  125. case 'month':
  126. $subfolder = $rcmail->format_date($message->timestamp, 'Y') . $delimiter . $rcmail->format_date($message->timestamp, 'm');
  127. break;
  128. case 'folder':
  129. $subfolder = $current_mbox;
  130. break;
  131. case 'sender':
  132. $from = $message->get('from');
  133. if (preg_match('/[\b<](.+@.+)[\b>]/i', $from, $m)) {
  134. $subfolder = $m[1];
  135. }
  136. else {
  137. $subfolder = $this->gettext('unkownsender');
  138. }
  139. // replace reserved characters in folder name
  140. $repl = $delimiter == '-' ? '_' : '-';
  141. $replacements[$delimiter] = $repl;
  142. $replacements['.'] = $repl; // some IMAP server do not allow . characters
  143. $subfolder = strtr($subfolder, $replacements);
  144. break;
  145. default:
  146. $subfolder = '';
  147. break;
  148. }
  149. // compose full folder path
  150. $folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : '');
  151. // create archive subfolder if it doesn't yet exist
  152. // we'll create all folders in the path
  153. if (!in_array($folder, $folders)) {
  154. if (empty($list)) {
  155. $list = $storage->list_folders('', $archive_folder . '*', 'mail', null, true);
  156. }
  157. $path = explode($delimiter, $folder);
  158. for ($i=0; $i<count($path); $i++) {
  159. $_folder = implode($delimiter, array_slice($path, 0, $i+1));
  160. if (!in_array($_folder, $list)) {
  161. if ($storage->create_folder($_folder, true)) {
  162. $result['reload'] = true;
  163. $list[] = $_folder;
  164. }
  165. }
  166. }
  167. $folders[] = $folder;
  168. }
  169. // move message to target folder
  170. if ($storage->move_message(array($uid), $folder)) {
  171. $result['update'] = true;
  172. }
  173. else {
  174. $result['errors'][] = $uid;
  175. }
  176. } // end for
  177. }
  178. // send response
  179. if ($result['errors']) {
  180. $rcmail->output->show_message($this->gettext('archiveerror'), 'warning');
  181. }
  182. if ($result['reload']) {
  183. $rcmail->output->show_message($this->gettext('archivedreload'), 'confirmation');
  184. }
  185. else if ($result['update']) {
  186. $rcmail->output->show_message($this->gettext('archived'), 'confirmation');
  187. }
  188. // refresh saved search set after moving some messages
  189. if ($search_request && $rcmail->storage->get_search_set()) {
  190. $_SESSION['search'] = $rcmail->storage->refresh_search();
  191. }
  192. if ($_POST['_from'] == 'show' && !empty($result['update'])) {
  193. if ($next = rcube_utils::get_input_value('_next_uid', rcube_utils::INPUT_GPC)) {
  194. $rcmail->output->command('show_message', $next);
  195. }
  196. else {
  197. $rcmail->output->command('command', 'list');
  198. }
  199. }
  200. else {
  201. $rcmail->output->command('plugin.move2archive_response', $result);
  202. }
  203. }
  204. /**
  205. * Hook to inject plugin-specific user settings
  206. */
  207. function prefs_table($args)
  208. {
  209. global $CURR_SECTION;
  210. if ($args['section'] == 'folders') {
  211. $this->add_texts('localization');
  212. $rcmail = rcmail::get_instance();
  213. // load folders list when needed
  214. if ($CURR_SECTION) {
  215. $select = $rcmail->folder_selector(array(
  216. 'noselection' => '---',
  217. 'realnames' => true,
  218. 'maxlength' => 30,
  219. 'folder_filter' => 'mail',
  220. 'folder_rights' => 'w',
  221. 'onchange' => "if ($(this).val() == 'INBOX') $(this).val('')",
  222. ));
  223. }
  224. else {
  225. $select = new html_select();
  226. }
  227. $args['blocks']['main']['options']['archive_mbox'] = array(
  228. 'title' => $this->gettext('archivefolder'),
  229. 'content' => $select->show($rcmail->config->get('archive_mbox'), array('name' => "_archive_mbox"))
  230. );
  231. // add option for structuring the archive folder
  232. $archive_type = new html_select(array('name' => '_archive_type', 'id' => 'ff_archive_type'));
  233. $archive_type->add($this->gettext('none'), '');
  234. $archive_type->add($this->gettext('archivetypeyear'), 'year');
  235. $archive_type->add($this->gettext('archivetypemonth'), 'month');
  236. $archive_type->add($this->gettext('archivetypesender'), 'sender');
  237. $archive_type->add($this->gettext('archivetypefolder'), 'folder');
  238. $args['blocks']['archive'] = array(
  239. 'name' => rcube::Q($this->gettext('settingstitle')),
  240. 'options' => array('archive_type' => array(
  241. 'title' => $this->gettext('archivetype'),
  242. 'content' => $archive_type->show($rcmail->config->get('archive_type'))
  243. )
  244. )
  245. );
  246. }
  247. return $args;
  248. }
  249. /**
  250. * Hook to save plugin-specific user settings
  251. */
  252. function save_prefs($args)
  253. {
  254. if ($args['section'] == 'folders') {
  255. $args['prefs']['archive_type'] = rcube_utils::get_input_value('_archive_type', rcube_utils::INPUT_POST);
  256. return $args;
  257. }
  258. }
  259. }