RoundCube Webmail
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.

301 lines
9.7 KiB

10 years ago
  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 if (!empty($uids)) {
  112. $messageset = rcmail::get_uids($uids, $current_mbox);
  113. } else {
  114. $messageset = array();
  115. }
  116. foreach ($messageset as $mbox => $uids) {
  117. $storage->set_folder(($current_mbox = $mbox));
  118. foreach ($uids as $uid) {
  119. if (!$archive_folder || !($message = $rcmail->storage->get_message($uid))) {
  120. continue;
  121. }
  122. $subfolder = null;
  123. switch ($archive_type) {
  124. case 'year':
  125. $subfolder = $rcmail->format_date($message->timestamp, 'Y');
  126. break;
  127. case 'month':
  128. $subfolder = $rcmail->format_date($message->timestamp, 'Y') . $delimiter . $rcmail->format_date($message->timestamp, 'm');
  129. break;
  130. case 'folder':
  131. $subfolder = $current_mbox;
  132. break;
  133. case 'sender':
  134. $from = $message->get('from');
  135. if (preg_match('/[\b<](.+@.+)[\b>]/i', $from, $m)) {
  136. $subfolder = $m[1];
  137. }
  138. else {
  139. $subfolder = $this->gettext('unkownsender');
  140. }
  141. // replace reserved characters in folder name
  142. $repl = $delimiter == '-' ? '_' : '-';
  143. $replacements[$delimiter] = $repl;
  144. $replacements['.'] = $repl; // some IMAP server do not allow . characters
  145. $subfolder = strtr($subfolder, $replacements);
  146. break;
  147. default:
  148. $subfolder = '';
  149. break;
  150. }
  151. // compose full folder path
  152. $folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : '');
  153. // create archive subfolder if it doesn't yet exist
  154. // we'll create all folders in the path
  155. if (!in_array($folder, $folders)) {
  156. if (empty($list)) {
  157. $list = $storage->list_folders('', $archive_folder . '*', 'mail', null, true);
  158. }
  159. $path = explode($delimiter, $folder);
  160. for ($i=0; $i<count($path); $i++) {
  161. $_folder = implode($delimiter, array_slice($path, 0, $i+1));
  162. if (!in_array($_folder, $list)) {
  163. if ($storage->create_folder($_folder, true)) {
  164. $result['reload'] = true;
  165. $list[] = $_folder;
  166. }
  167. }
  168. }
  169. $folders[] = $folder;
  170. }
  171. // move message to target folder
  172. if ($storage->move_message(array($uid), $folder)) {
  173. $result['update'] = true;
  174. }
  175. else {
  176. $result['errors'][] = $uid;
  177. }
  178. } // end for
  179. }
  180. // send response
  181. if ($result['errors']) {
  182. $rcmail->output->show_message($this->gettext('archiveerror'), 'warning');
  183. }
  184. if ($result['reload']) {
  185. $rcmail->output->show_message($this->gettext('archivedreload'), 'confirmation');
  186. }
  187. else if ($result['update']) {
  188. $rcmail->output->show_message($this->gettext('archived'), 'confirmation');
  189. }
  190. // refresh saved search set after moving some messages
  191. if ($search_request && $rcmail->storage->get_search_set()) {
  192. $_SESSION['search'] = $rcmail->storage->refresh_search();
  193. }
  194. if ($_POST['_from'] == 'show' && !empty($result['update'])) {
  195. if ($next = rcube_utils::get_input_value('_next_uid', rcube_utils::INPUT_GPC)) {
  196. $rcmail->output->command('show_message', $next);
  197. }
  198. else {
  199. $rcmail->output->command('command', 'list');
  200. }
  201. }
  202. else {
  203. $rcmail->output->command('plugin.move2archive_response', $result);
  204. }
  205. }
  206. /**
  207. * Hook to inject plugin-specific user settings
  208. */
  209. function prefs_table($args)
  210. {
  211. global $CURR_SECTION;
  212. if ($args['section'] == 'folders') {
  213. $this->add_texts('localization');
  214. $rcmail = rcmail::get_instance();
  215. // load folders list when needed
  216. if ($CURR_SECTION) {
  217. $select = $rcmail->folder_selector(array(
  218. 'noselection' => '---',
  219. 'realnames' => true,
  220. 'maxlength' => 30,
  221. 'folder_filter' => 'mail',
  222. 'folder_rights' => 'w',
  223. 'onchange' => "if ($(this).val() == 'INBOX') $(this).val('')",
  224. ));
  225. }
  226. else {
  227. $select = new html_select();
  228. }
  229. $args['blocks']['main']['options']['archive_mbox'] = array(
  230. 'title' => $this->gettext('archivefolder'),
  231. 'content' => $select->show($rcmail->config->get('archive_mbox'), array('name' => "_archive_mbox"))
  232. );
  233. // add option for structuring the archive folder
  234. $archive_type = new html_select(array('name' => '_archive_type', 'id' => 'ff_archive_type'));
  235. $archive_type->add($this->gettext('none'), '');
  236. $archive_type->add($this->gettext('archivetypeyear'), 'year');
  237. $archive_type->add($this->gettext('archivetypemonth'), 'month');
  238. $archive_type->add($this->gettext('archivetypesender'), 'sender');
  239. $archive_type->add($this->gettext('archivetypefolder'), 'folder');
  240. $args['blocks']['archive'] = array(
  241. 'name' => rcube::Q($this->gettext('settingstitle')),
  242. 'options' => array('archive_type' => array(
  243. 'title' => $this->gettext('archivetype'),
  244. 'content' => $archive_type->show($rcmail->config->get('archive_type'))
  245. )
  246. )
  247. );
  248. }
  249. return $args;
  250. }
  251. /**
  252. * Hook to save plugin-specific user settings
  253. */
  254. function save_prefs($args)
  255. {
  256. if ($args['section'] == 'folders') {
  257. $args['prefs']['archive_type'] = rcube_utils::get_input_value('_archive_type', rcube_utils::INPUT_POST);
  258. return $args;
  259. }
  260. }
  261. }