@ -6,7 +6,7 @@
* Plugin that adds a new button to the mailbox toolbar
* Plugin that adds a new button to the mailbox toolbar
* to move messages to a (user selectable) archive folder.
* to move messages to a (user selectable) archive folder.
*
*
* @version 2.2
* @version 2.3
* @license GNU GPLv3+
* @license GNU GPLv3+
* @author Andre Rodier, Thomas Bruederli, Aleksander Machniak
* @author Andre Rodier, Thomas Bruederli, Aleksander Machniak
*/
*/
@ -111,8 +111,7 @@ class archive extends rcube_plugin
$delimiter = $storage->get_hierarchy_delimiter();
$delimiter = $storage->get_hierarchy_delimiter();
$archive_folder = $rcmail->config->get('archive_mbox');
$archive_folder = $rcmail->config->get('archive_mbox');
$archive_type = $rcmail->config->get('archive_type', '');
$archive_type = $rcmail->config->get('archive_type', '');
$current_mbox = rcube_utils::get_input_value('_mbox', RCUBE_INPUT_POST);
$storage->set_folder(($current_mbox = rcube_utils::get_input_value('_mbox', RCUBE_INPUT_POST)));
$result = array('reload' => false, 'update' => false, 'errors' => array());
$result = array('reload' => false, 'update' => false, 'errors' => array());
$folders = array();
$folders = array();
@ -121,84 +120,88 @@ class archive extends rcube_plugin
if ($uids == '*') {
if ($uids == '*') {
$index = $storage->index(null, rcmail_sort_column(), rcmail_sort_order());
$index = $storage->index(null, rcmail_sort_column(), rcmail_sort_order());
$uids = $index->get( );
$messageset = array($current_mbox => $index->get() );
}
}
else {
else {
$uids = explode(',', $uids );
$messageset = rcmail::get_uids( );
}
}
foreach ($uids as $uid) {
foreach ($messageset as $mbox => $uids) {
if (!$archive_folder || !($message = $rcmail->storage->get_message($uid))) {
$storage->set_folder(($current_mbox = $mbox));
continue;
}
$subfolder = null;
switch ($archive_type) {
case 'year':
$subfolder = $rcmail->format_date($message->timestamp, 'Y');
break;
case 'month':
foreach ($uids as $uid) {
$subfolder = $rcmail->format_date($message->timestamp, 'Y') . $delimiter . $rcmail->format_date($message->timestamp, 'm');
if (!$archive_folder || !($message = $rcmail->storage->get_message($uid))) {
break;
continue;
}
case 'folder':
$subfolder = null;
$subfolder = $current_mbox;
switch ($archive_type) {
break;
case 'year':
$subfolder = $rcmail->format_date($message->timestamp, 'Y');
break;
case 'sender':
case 'month':
$from = $message->get('from');
$subfolder = $rcmail->format_date($message->timestamp, 'Y') . $delimiter . $rcmail->format_date($message->timestamp, 'm');
if (preg_match('/[\b< ](.+@.+)[\b>]/i', $from, $m)) {
break;
$subfolder = $m[1];
}
else {
$subfolder = $this->gettext('unkownsender');
}
// replace reserved characters in folder name
case 'folder':
$repl = $delimiter == '-' ? '_' : '-';
$subfolder = $current_mbox;
$replacements[$delimiter] = $repl;
break;
$replacements['.'] = $repl; // some IMAP server do not allow . characters
$subfolder = strtr($subfolder, $replacements);
break;
default:
case 'sender':
$subfolder = '';
$from = $message->get('from');
break;
if (preg_match('/[\b< ](.+@.+)[\b>]/i', $from, $m)) {
}
$subfolder = $m[1];
}
else {
$subfolder = $this->gettext('unkownsender');
}
// compose full folder path
// replace reserved characters in folder name
$folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : '');
$repl = $delimiter == '-' ? '_' : '-';
$replacements[$delimiter] = $repl;
$replacements['.'] = $repl; // some IMAP server do not allow . characters
$subfolder = strtr($subfolder, $replacements);
break;
// create archive subfolder if it doesn't yet exist
default:
// we'll create all folders in the path
$subfolder = '';
if (!in_array($folder, $folders)) {
break;
if (empty($list)) {
$list = $storage->list_folders('', $archive_folder . '*', 'mail', null, true);
}
}
$path = explode($delimiter, $folder);
// compose full folder path
for ($i=0; $i< count ( $ path ) ; $ i + + ) {
$folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : '');
$_folder = implode($delimiter, array_slice($path, 0, $i+1));
if (!in_array($_folder, $list)) {
// create archive subfolder if it doesn't yet exist
if ($storage->create_folder($_folder, true)) {
// we'll create all folders in the path
$result['reload'] = true;
if (!in_array($folder, $folders)) {
$list[] = $_folder;
if (empty($list)) {
$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;
$list[] = $_folder;
}
}
}
}
}
}
$folders[] = $folder;
$folders[] = $folder;
}
}
// move message to target folder
// move message to target folder
if ($storage->move_message(array($uid), $folder)) {
if ($storage->move_message(array($uid), $folder)) {
$result['update'] = true;
$result['update'] = true;
}
}
else {
else {
$result['errors'][] = $uid;
$result['errors'][] = $uid;
}
}
} // end for
} // end for
}
// send response
// send response
if ($result['errors']) {
if ($result['errors']) {