summaryrefslogtreecommitdiff
path: root/plugins/archive/archive.php
blob: 4ef46e4f41a26877c533a4a12720e2da1170e4eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php

/**
 * Archive
 *
 * Plugin that adds a new button to the mailbox toolbar
 * to move messages to a (user selectable) archive folder.
 *
 * @version 2.0
 * @license GNU GPLv3+
 * @author Andre Rodier, Thomas Bruederli
 */
class archive extends rcube_plugin
{
  public $task = 'mail|settings';

  function init()
  {
    $rcmail = rcmail::get_instance();

    // There is no "Archived flags"
    // $GLOBALS['IMAP_FLAGS']['ARCHIVED'] = 'Archive';
    if ($rcmail->task == 'mail' && ($rcmail->action == '' || $rcmail->action == 'show')
        && ($archive_folder = $rcmail->config->get('archive_mbox'))) {
      $skin_path = $this->local_skin_path();
      if (is_file($this->home . "/$skin_path/archive.css"))
        $this->include_stylesheet("$skin_path/archive.css");

      $this->include_script('archive.js');
      $this->add_texts('localization', true);
      $this->add_button(
        array(
            'type' => 'link',
            'label' => 'buttontext',
            'command' => 'plugin.archive',
            'class' => 'button buttonPas archive disabled',
            'classact' => 'button archive',
            'width' => 32,
            'height' => 32,
            'title' => 'buttontitle',
            'domain' => $this->ID,
        ),
        'toolbar');

      // register hook to localize the archive folder
      $this->add_hook('render_mailboxlist', array($this, 'render_mailboxlist'));

      // set env variables for client
      $rcmail->output->set_env('archive_folder', $archive_folder);
      $rcmail->output->set_env('archive_type', $rcmail->config->get('archive_type',''));

      // add archive folder to the list of default mailboxes
      if (($default_folders = $rcmail->config->get('default_folders')) && !in_array($archive_folder, $default_folders)) {
        $default_folders[] = $archive_folder;
        $rcmail->config->set('default_folders', $default_folders);
      }
    }
    else if ($rcmail->task == 'mail') {
      // handler for ajax request
      $this->register_action('plugin.move2archive', array($this, 'move_messages'));
    }
    else if ($rcmail->task == 'settings') {
      $dont_override = $rcmail->config->get('dont_override', array());
      if (!in_array('archive_mbox', $dont_override)) {
        $this->add_hook('preferences_list', array($this, 'prefs_table'));
        $this->add_hook('preferences_save', array($this, 'save_prefs'));
      }
    }
  }

  /**
   * Hook to give the archive folder a localized name in the mailbox list
   */
  function render_mailboxlist($p)
  {
    $rcmail = rcmail::get_instance();
    $archive_folder = $rcmail->config->get('archive_mbox');
    $show_real_name = $rcmail->config->get('show_real_foldernames');

    // set localized name for the configured archive folder
    if ($archive_folder && !$show_real_name) {
      if (isset($p['list'][$archive_folder]))
        $p['list'][$archive_folder]['name'] = $this->gettext('archivefolder');
      else // search in subfolders
        $this->_mod_folder_name($p['list'], $archive_folder, $this->gettext('archivefolder'));
    }

    return $p;
  }

  /**
   * Helper method to find the archive folder in the mailbox tree
   */
  private function _mod_folder_name(&$list, $folder, $new_name)
  {
    foreach ($list as $idx => $item) {
      if ($item['id'] == $folder) {
        $list[$idx]['name'] = $new_name;
        return true;
      } else if (!empty($item['folders']))
        if ($this->_mod_folder_name($list[$idx]['folders'], $folder, $new_name))
        return true;
    }
    return false;
  }

  /**
   * Plugin action to move the submitted list of messages to the archive subfolders
   * according to the user settings and their headers.
   */
  function move_messages()
  {
    $this->add_texts('localization');

    $rcmail         = rcmail::get_instance();
    $storage        = $rcmail->get_storage();
    $delimiter      = $storage->get_hierarchy_delimiter();
    $archive_folder = $rcmail->config->get('archive_mbox');
    $archive_type   = $rcmail->config->get('archive_type', '');

    $storage->set_folder(($current_mbox = rcube_utils::get_input_value('_mbox', RCUBE_INPUT_POST)));

    $result  = array('reload' => false, 'update' => false, 'errors' => array());
    $uids    = explode(',', rcube_utils::get_input_value('_uid', RCUBE_INPUT_POST));
    $folders = array();

    foreach ($uids as $uid) {
      if (!$archive_folder || !($message = $rcmail->storage->get_message($uid))) {
        continue;
      }

      $subfolder = null;
      switch ($archive_type) {
        case 'year':
          $subfolder = $rcmail->format_date($message->timestamp, 'Y');
          break;

        case 'month':
          $subfolder = $rcmail->format_date($message->timestamp, 'Y') . $delimiter . $rcmail->format_date($message->timestamp, 'm');
          break;

        case 'folder':
          $subfolder = $current_mbox;
          break;

        case 'sender':
          $from = $message->get('from');
          if (preg_match('/[\b<](.+@.+)[\b>]/i', $from, $m)) {
            $subfolder = $m[1];
          }
          else {
            $subfolder = $this->gettext('unkownsender');
          }

          // replace reserved characters in folder name
          $repl = $delimiter == '-' ? '_' : '-';
          $replacements[$delimiter] = $repl;
          $replacements['.'] = $repl;  // some IMAP server do not allow . characters
          $subfolder = strtr($subfolder, $replacements);
          break;

        default:
          $subfolder = '';
          break;
      }

      // compose full folder path
      $folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : '');

      // create archive subfolder if it doesn't yet exist
      // we'll create all folders in the path
      if (!in_array($folder, $folders)) {
        $list = $storage->list_folders('', $archive_folder . '*', 'mail', null, true);
        $path = explode($delimiter, $folder);

        for ($i=0; $i<count($path); $i++) {
          $_folder = implode($delimiter, array_slice($path, 0, $i+1));
          if (!in_array($_folder, $list)) {
            if ($storage->create_folder($_folder, true)) {
              $result['reload'] = true;
            }
          }
        }

        $folders[] = $folder;
      }

      // move message to target folder
      if ($storage->move_message(array($uid), $folder)) {
        $result['update'] = true;
      }
      else {
        $result['errors'][] = $uid;
      }
    }  // end for

    // send response
    if ($result['errors']) {
      $rcmail->output->show_message($this->gettext('archiveerror'), 'warning');
    }
    if ($result['reload']) {
      $rcmail->output->show_message($this->gettext('archivedreload'), 'confirmation');
    }
    else if ($result['update']) {
      $rcmail->output->show_message($this->gettext('archived'), 'confirmation');
    }

    $rcmail->output->command('plugin.move2archive_response', $result);
  }

  /**
   * Hook to inject plugin-specific user settings
   */
  function prefs_table($args)
  {
    global $CURR_SECTION;

    if ($args['section'] == 'folders') {
      $this->add_texts('localization');

      $rcmail = rcmail::get_instance();

      // load folders list when needed
      if ($CURR_SECTION)
        $select = $rcmail->folder_selector(array('noselection' => '---', 'realnames' => true,
          'maxlength' => 30, 'exceptions' => array('INBOX'), 'folder_filter' => 'mail', 'folder_rights' => 'w'));
      else
        $select = new html_select();

      $args['blocks']['main']['options']['archive_mbox'] = array(
          'title' => $this->gettext('archivefolder'),
          'content' => $select->show($rcmail->config->get('archive_mbox'), array('name' => "_archive_mbox"))
      );

      // add option for structuring the archive folder
      $archive_type = new html_select(array('name' => '_archive_type', 'id' => 'ff_archive_type'));
      $archive_type->add($this->gettext('none'), '');
      $archive_type->add($this->gettext('archivetypeyear'), 'year');
      $archive_type->add($this->gettext('archivetypemonth'), 'month');
      $archive_type->add($this->gettext('archivetypesender'), 'sender');
      $archive_type->add($this->gettext('archivetypefolder'), 'folder');

      $args['blocks']['archive'] = array(
        'name' => Q(rcube_label('settingstitle', 'archive')),
        'options' => array('archive_type' => array(
            'title' => $this->gettext('archivetype'),
            'content' => $archive_type->show($rcmail->config->get('archive_type'))
          )
        )
      );
    }

    return $args;
  }

  /**
   * Hook to save plugin-specific user settings
   */
  function save_prefs($args)
  {
    if ($args['section'] == 'folders') {
      $args['prefs']['archive_mbox'] = rcube_utils::get_input_value('_archive_mbox', rcube_utils::INPUT_POST);
      $args['prefs']['archive_type'] = rcube_utils::get_input_value('_archive_type', rcube_utils::INPUT_POST);
      return $args;
    }
  }

}