PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/archive/archive.php

https://github.com/trimbakgopalghare/roundcubemail
PHP | 291 lines | 211 code | 40 blank | 40 comment | 57 complexity | b0e9ffb526073c17c19fd4dd7663bd21 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  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.3
  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_INPUT_POST);
  103. $result = array('reload' => false, 'update' => false, 'errors' => array());
  104. $folders = array();
  105. $uids = rcube_utils::get_input_value('_uid', RCUBE_INPUT_POST);
  106. $search_request = get_input_value('_search', RCUBE_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 = get_input_value('_next_uid', RCUBE_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('noselection' => '---', 'realnames' => true,
  216. 'maxlength' => 30, 'exceptions' => array('INBOX'), 'folder_filter' => 'mail', 'folder_rights' => 'w'));
  217. else
  218. $select = new html_select();
  219. $args['blocks']['main']['options']['archive_mbox'] = array(
  220. 'title' => $this->gettext('archivefolder'),
  221. 'content' => $select->show($rcmail->config->get('archive_mbox'), array('name' => "_archive_mbox"))
  222. );
  223. // add option for structuring the archive folder
  224. $archive_type = new html_select(array('name' => '_archive_type', 'id' => 'ff_archive_type'));
  225. $archive_type->add($this->gettext('none'), '');
  226. $archive_type->add($this->gettext('archivetypeyear'), 'year');
  227. $archive_type->add($this->gettext('archivetypemonth'), 'month');
  228. $archive_type->add($this->gettext('archivetypesender'), 'sender');
  229. $archive_type->add($this->gettext('archivetypefolder'), 'folder');
  230. $args['blocks']['archive'] = array(
  231. 'name' => Q($this->gettext('settingstitle')),
  232. 'options' => array('archive_type' => array(
  233. 'title' => $this->gettext('archivetype'),
  234. 'content' => $archive_type->show($rcmail->config->get('archive_type'))
  235. )
  236. )
  237. );
  238. }
  239. return $args;
  240. }
  241. /**
  242. * Hook to save plugin-specific user settings
  243. */
  244. function save_prefs($args)
  245. {
  246. if ($args['section'] == 'folders') {
  247. $args['prefs']['archive_type'] = rcube_utils::get_input_value('_archive_type', rcube_utils::INPUT_POST);
  248. return $args;
  249. }
  250. }
  251. }