PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/etc/apps/webmail/plugins/archive/archive.php

https://github.com/raiman264/zpanelx
PHP | 295 lines | 212 code | 41 blank | 42 comment | 59 complexity | 7882af5c9b7844bfa2c6de2e804639ad MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.1, CC-BY-SA-4.0, GPL-3.0
  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.1
  9. * @license GNU GPLv3+
  10. * @author Andre Rodier, Thomas Bruederli, Aleksander Machniak
  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. $this->add_texts('localization');
  102. $rcmail = rcmail::get_instance();
  103. $storage = $rcmail->get_storage();
  104. $delimiter = $storage->get_hierarchy_delimiter();
  105. $archive_folder = $rcmail->config->get('archive_mbox');
  106. $archive_type = $rcmail->config->get('archive_type', '');
  107. $storage->set_folder(($current_mbox = rcube_utils::get_input_value('_mbox', RCUBE_INPUT_POST)));
  108. $result = array('reload' => false, 'update' => false, 'errors' => array());
  109. $folders = array();
  110. $uids = rcube_utils::get_input_value('_uid', RCUBE_INPUT_POST);
  111. $search_request = get_input_value('_search', RCUBE_INPUT_GPC);
  112. if ($uids == '*') {
  113. $index = $storage->index(null, rcmail_sort_column(), rcmail_sort_order());
  114. $uids = $index->get();
  115. }
  116. else {
  117. $uids = explode(',', $uids);
  118. }
  119. foreach ($uids as $uid) {
  120. if (!$archive_folder || !($message = $rcmail->storage->get_message($uid))) {
  121. continue;
  122. }
  123. $subfolder = null;
  124. switch ($archive_type) {
  125. case 'year':
  126. $subfolder = $rcmail->format_date($message->timestamp, 'Y');
  127. break;
  128. case 'month':
  129. $subfolder = $rcmail->format_date($message->timestamp, 'Y') . $delimiter . $rcmail->format_date($message->timestamp, 'm');
  130. break;
  131. case 'folder':
  132. $subfolder = $current_mbox;
  133. break;
  134. case 'sender':
  135. $from = $message->get('from');
  136. if (preg_match('/[\b<](.+@.+)[\b>]/i', $from, $m)) {
  137. $subfolder = $m[1];
  138. }
  139. else {
  140. $subfolder = $this->gettext('unkownsender');
  141. }
  142. // replace reserved characters in folder name
  143. $repl = $delimiter == '-' ? '_' : '-';
  144. $replacements[$delimiter] = $repl;
  145. $replacements['.'] = $repl; // some IMAP server do not allow . characters
  146. $subfolder = strtr($subfolder, $replacements);
  147. break;
  148. default:
  149. $subfolder = '';
  150. break;
  151. }
  152. // compose full folder path
  153. $folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : '');
  154. // create archive subfolder if it doesn't yet exist
  155. // we'll create all folders in the path
  156. if (!in_array($folder, $folders)) {
  157. if (empty($list)) {
  158. $list = $storage->list_folders('', $archive_folder . '*', 'mail', null, true);
  159. }
  160. $path = explode($delimiter, $folder);
  161. for ($i=0; $i<count($path); $i++) {
  162. $_folder = implode($delimiter, array_slice($path, 0, $i+1));
  163. if (!in_array($_folder, $list)) {
  164. if ($storage->create_folder($_folder, true)) {
  165. $result['reload'] = true;
  166. $list[] = $_folder;
  167. }
  168. }
  169. }
  170. $folders[] = $folder;
  171. }
  172. // move message to target folder
  173. if ($storage->move_message(array($uid), $folder)) {
  174. $result['update'] = true;
  175. }
  176. else {
  177. $result['errors'][] = $uid;
  178. }
  179. } // end for
  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 = get_input_value('_next_uid', RCUBE_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('noselection' => '---', 'realnames' => true,
  218. 'maxlength' => 30, 'exceptions' => array('INBOX'), 'folder_filter' => 'mail', 'folder_rights' => 'w'));
  219. else
  220. $select = new html_select();
  221. $args['blocks']['main']['options']['archive_mbox'] = array(
  222. 'title' => $this->gettext('archivefolder'),
  223. 'content' => $select->show($rcmail->config->get('archive_mbox'), array('name' => "_archive_mbox"))
  224. );
  225. // add option for structuring the archive folder
  226. $archive_type = new html_select(array('name' => '_archive_type', 'id' => 'ff_archive_type'));
  227. $archive_type->add($this->gettext('none'), '');
  228. $archive_type->add($this->gettext('archivetypeyear'), 'year');
  229. $archive_type->add($this->gettext('archivetypemonth'), 'month');
  230. $archive_type->add($this->gettext('archivetypesender'), 'sender');
  231. $archive_type->add($this->gettext('archivetypefolder'), 'folder');
  232. $args['blocks']['archive'] = array(
  233. 'name' => Q($this->gettext('settingstitle')),
  234. 'options' => array('archive_type' => array(
  235. 'title' => $this->gettext('archivetype'),
  236. 'content' => $archive_type->show($rcmail->config->get('archive_type'))
  237. )
  238. )
  239. );
  240. }
  241. return $args;
  242. }
  243. /**
  244. * Hook to save plugin-specific user settings
  245. */
  246. function save_prefs($args)
  247. {
  248. if ($args['section'] == 'folders') {
  249. $args['prefs']['archive_mbox'] = rcube_utils::get_input_value('_archive_mbox', rcube_utils::INPUT_POST);
  250. $args['prefs']['archive_type'] = rcube_utils::get_input_value('_archive_type', rcube_utils::INPUT_POST);
  251. return $args;
  252. }
  253. }
  254. }