PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/archive/archive.php

https://github.com/fretelweb/roundcubemail
PHP | 255 lines | 178 code | 37 blank | 40 comment | 45 complexity | 48283428cdeee73a2ddb0eb5636473e1 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.0
  9. * @license GNU GPLv3+
  10. * @author Andre Rodier, Thomas Bruederli
  11. */
  12. class archive extends rcube_plugin
  13. {
  14. public $task = 'mail|settings';
  15. function init()
  16. {
  17. $rcmail = rcmail::get_instance();
  18. // There is no "Archived flags"
  19. // $GLOBALS['IMAP_FLAGS']['ARCHIVED'] = 'Archive';
  20. if ($rcmail->task == 'mail' && ($rcmail->action == '' || $rcmail->action == 'show')
  21. && ($archive_folder = $rcmail->config->get('archive_mbox'))) {
  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. // add archive folder to the list of default mailboxes
  46. if (($default_folders = $rcmail->config->get('default_folders')) && !in_array($archive_folder, $default_folders)) {
  47. $default_folders[] = $archive_folder;
  48. $rcmail->config->set('default_folders', $default_folders);
  49. }
  50. }
  51. else if ($rcmail->task == 'mail') {
  52. // handler for ajax request
  53. $this->register_action('plugin.move2archive', array($this, 'move_messages'));
  54. }
  55. else if ($rcmail->task == 'settings') {
  56. $dont_override = $rcmail->config->get('dont_override', array());
  57. if (!in_array('archive_mbox', $dont_override)) {
  58. $this->add_hook('preferences_list', array($this, 'prefs_table'));
  59. $this->add_hook('preferences_save', array($this, 'save_prefs'));
  60. }
  61. }
  62. }
  63. /**
  64. * Hook to give the archive folder a localized name in the mailbox list
  65. */
  66. function render_mailboxlist($p)
  67. {
  68. $rcmail = rcmail::get_instance();
  69. $archive_folder = $rcmail->config->get('archive_mbox');
  70. $show_real_name = $rcmail->config->get('show_real_foldernames');
  71. // set localized name for the configured archive folder
  72. if ($archive_folder && !$show_real_name) {
  73. if (isset($p['list'][$archive_folder]))
  74. $p['list'][$archive_folder]['name'] = $this->gettext('archivefolder');
  75. else // search in subfolders
  76. $this->_mod_folder_name($p['list'], $archive_folder, $this->gettext('archivefolder'));
  77. }
  78. return $p;
  79. }
  80. /**
  81. * Helper method to find the archive folder in the mailbox tree
  82. */
  83. private function _mod_folder_name(&$list, $folder, $new_name)
  84. {
  85. foreach ($list as $idx => $item) {
  86. if ($item['id'] == $folder) {
  87. $list[$idx]['name'] = $new_name;
  88. return true;
  89. } else if (!empty($item['folders']))
  90. if ($this->_mod_folder_name($list[$idx]['folders'], $folder, $new_name))
  91. return true;
  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. $this->add_texts('localization');
  103. $storage = $rcmail->get_storage();
  104. $storage->set_folder(($current_mbox = rcube_utils::get_input_value('_mbox', RCUBE_INPUT_POST)));
  105. $delimiter = $storage->get_hierarchy_delimiter();
  106. $archive_folder = $rcmail->config->get('archive_mbox');
  107. $archive_type = $rcmail->config->get('archive_type', '');
  108. $result = array('reload' => false, 'update' => false, 'errors' => array());
  109. $uids = explode(',', rcube_utils::get_input_value('_uid', RCUBE_INPUT_POST));
  110. foreach ($uids as $uid) {
  111. if (!$archive_folder || !($message = $rcmail->storage->get_message($uid))) {
  112. continue;
  113. }
  114. $subfolder = null;
  115. switch ($archive_type) {
  116. case 'year':
  117. $subfolder = $rcmail->format_date($message->timestamp, 'Y');
  118. break;
  119. case 'month':
  120. $subfolder = $rcmail->format_date($message->timestamp, 'Y') . $delimiter . $rcmail->format_date($message->timestamp, 'm');
  121. break;
  122. case 'folder':
  123. $subfolder = $current_mbox;
  124. break;
  125. case 'sender':
  126. $from = $message->get('from');
  127. if (preg_match('/[\b<](.+@.+)[\b>]/i', $from, $m)) {
  128. $subfolder = $m[1];
  129. }
  130. else {
  131. $subfolder = $this->gettext('unkownsender');
  132. }
  133. // replace reserved characters in folder name
  134. $repl = $delimiter == '-' ? '_' : '-';
  135. $replacements[$delimiter] = $repl;
  136. $replacements['.'] = $repl; // some IMAP server do not allow . characters
  137. $subfolder = strtr($subfolder, $replacements);
  138. break;
  139. default:
  140. $subfolder = '';
  141. break;
  142. }
  143. // compose full folder path
  144. $folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : '');
  145. // create archive subfolder if it doesn't yet exist
  146. if (!$storage->folder_exists($folder, false)) {
  147. if ($storage->create_folder($folder, true))
  148. $result['reload'] = true;
  149. }
  150. // move message to target folder
  151. if ($storage->move_message(array($uid), $folder)) {
  152. $result['update'] = true;
  153. }
  154. else {
  155. $result['errors'][] = $uid;
  156. }
  157. } // end for
  158. // send response
  159. if ($result['errors']) {
  160. $rcmail->output->show_message($this->gettext('archiveerror'), 'warning');
  161. }
  162. if ($result['reload']) {
  163. $rcmail->output->show_message($this->gettext('archivedreload'), 'confirmation');
  164. }
  165. else if ($result['update']) {
  166. $rcmail->output->show_message($this->gettext('archived'), 'confirmation');
  167. }
  168. $rcmail->output->command('plugin.move2archive_response', $result);
  169. }
  170. /**
  171. * Hook to inject plugin-specific user settings
  172. */
  173. function prefs_table($args)
  174. {
  175. global $CURR_SECTION;
  176. if ($args['section'] == 'folders') {
  177. $this->add_texts('localization');
  178. $rcmail = rcmail::get_instance();
  179. // load folders list when needed
  180. if ($CURR_SECTION)
  181. $select = $rcmail->folder_selector(array('noselection' => '---', 'realnames' => true,
  182. 'maxlength' => 30, 'exceptions' => array('INBOX'), 'folder_filter' => 'mail', 'folder_rights' => 'w'));
  183. else
  184. $select = new html_select();
  185. $args['blocks']['main']['options']['archive_mbox'] = array(
  186. 'title' => $this->gettext('archivefolder'),
  187. 'content' => $select->show($rcmail->config->get('archive_mbox'), array('name' => "_archive_mbox"))
  188. );
  189. // add option for structuring the archive folder
  190. $archive_type = new html_select(array('name' => '_archive_type', 'id' => 'ff_archive_type'));
  191. $archive_type->add($this->gettext('none'), '');
  192. $archive_type->add($this->gettext('archivetypeyear'), 'year');
  193. $archive_type->add($this->gettext('archivetypemonth'), 'month');
  194. $archive_type->add($this->gettext('archivetypesender'), 'sender');
  195. $archive_type->add($this->gettext('archivetypefolder'), 'folder');
  196. $args['blocks']['archive'] = array(
  197. 'name' => Q(rcube_label('settingstitle', 'archive')),
  198. 'options' => array('archive_type' => array(
  199. 'title' => $this->gettext('archivetype'),
  200. 'content' => $archive_type->show($rcmail->config->get('archive_type'))
  201. )
  202. )
  203. );
  204. }
  205. return $args;
  206. }
  207. /**
  208. * Hook to save plugin-specific user settings
  209. */
  210. function save_prefs($args)
  211. {
  212. if ($args['section'] == 'folders') {
  213. $args['prefs']['archive_mbox'] = rcube_utils::get_input_value('_archive_mbox', rcube_utils::INPUT_POST);
  214. $args['prefs']['archive_type'] = rcube_utils::get_input_value('_archive_type', rcube_utils::INPUT_POST);
  215. return $args;
  216. }
  217. }
  218. }