This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
/
Move.php
334 lines (300 loc) · 14.2 KB
/
Move.php
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Tine 2.0
*
* @package Felamimail
* @subpackage Controller
* @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
* @author Philipp Schüle <p.schuele@metaways.de>
* @copyright Copyright (c) 2011 Metaways Infosystems GmbH (http://www.metaways.de)
*/
/**
* message move controller for Felamimail
*
* @package Felamimail
* @subpackage Controller
*/
class Felamimail_Controller_Message_Move extends Felamimail_Controller_Message
{
/**
* holds the instance of the singleton
*
* @var Felamimail_Controller_Message_Move
*/
private static $_instance = NULL;
/**
* the constructor
*
* don't use the constructor. use the singleton
*/
private function __construct()
{
$this->_backend = new Felamimail_Backend_Cache_Sql_Message();
$this->_modelName = Felamimail_Model_Message::class;
}
/**
* don't clone. Use the singleton.
*
*/
private function __clone()
{
}
/**
* the singleton pattern
*
* @return Felamimail_Controller_Message_Move
*/
public static function getInstance()
{
if (self::$_instance === NULL) {
self::$_instance = new Felamimail_Controller_Message_Move();
}
return self::$_instance;
}
/**
* move/copy messages to folder
*
* @param mixed $_messages
* @param mixed $_targetFolder can be one of: folder_id, Felamimail_Model_Folder or Felamimail_Model_Folder::FOLDER_TRASH (constant)
* @param boolean $keepOriginalMessages
* @param boolean $checkCopyPreventionConfig
* @return Tinebase_Record_RecordSet of Felamimail_Model_Folder
*/
public function moveMessages($_messages, $_targetFolder, $keepOriginalMessages = false, $checkCopyPreventionConfig = true)
{
if ($_targetFolder !== Felamimail_Model_Folder::FOLDER_TRASH) {
$targetFolder = ($_targetFolder instanceof Felamimail_Model_Folder) ? $_targetFolder : Felamimail_Controller_Folder::getInstance()->get($_targetFolder);
} else {
$targetFolder = $_targetFolder;
}
if ($_messages instanceof Tinebase_Model_Filter_FilterGroup) {
$iterator = new Tinebase_Record_Iterator(array(
'iteratable' => $this,
'controller' => $this,
'filter' => $_messages,
'function' => 'processMoveIteration',
));
$iterateResult = $iterator->iterate($targetFolder, $keepOriginalMessages, $checkCopyPreventionConfig);
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
. ' Moved/copied ' . $iterateResult['totalcount'] . ' message(s).');
$result = (! empty($iterateResult['results'])) ? array_pop($iterateResult['results']) : new Tinebase_Record_RecordSet('Felamimail_Model_Folder');
} else {
$messages = $this->_convertToRecordSet($_messages, TRUE);
$result = $this->processMoveIteration($messages, $targetFolder, $keepOriginalMessages, $checkCopyPreventionConfig);
}
return $result;
}
/**
* move messages
*
* @param Tinebase_Record_RecordSet $_messages
* @param mixed $_targetFolder can be one of: Felamimail_Model_Folder or Felamimail_Model_Folder::FOLDER_TRASH (constant)
* @param boolean $keepOriginalMessages
* @param boolean $checkCopyPreventionConfig
* @return Tinebase_Record_RecordSet of Felamimail_Model_Folder
*/
public function processMoveIteration($_messages, $_targetFolder, $keepOriginalMessages = false, $checkCopyPreventionConfig = true)
{
$folderName = ($_targetFolder instanceof Felamimail_Model_Folder ? $_targetFolder->globalname : $_targetFolder);
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
. ' About to move ' . count($_messages) . ' messages to ' . $folderName);
$_messages->addIndices(array('folder_id'));
$movedMessages = FALSE;
foreach (array_unique($_messages->folder_id) as $folderId) {
$movedMessages = ($this->_moveMessagesByFolder($_messages,
$folderId,
$_targetFolder,
$keepOriginalMessages,
$checkCopyPreventionConfig
) || $movedMessages);
}
if (! $movedMessages) {
// no messages have been moved -> return empty record set
$result = new Tinebase_Record_RecordSet('Felamimail_Model_Folder');
} else {
if (! $keepOriginalMessages) {
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
. ' Delete messages in local cache');
try {
$number = $this->_backend->delete($_messages->getArrayOfIds());
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Deleted ' . $number . ' messages from cache');
} catch (Zend_Db_Statement_Exception $zdse) {
if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ .
' Error deleting cached messages from folder ' . $folderName . ': ' . $zdse);
}
}
$result = $this->_updateCountsAfterMove($_messages);
}
return $result;
}
/**
* move messages from one folder to another
*
* @param Tinebase_Record_RecordSet $_messages
* @param string $_folderId
* @param Felamimail_Model_Folder|string $_targetFolder
* @param boolean $keepOriginalMessages
* @param boolean $checkCopyPreventionConfig
* @return boolean did we move messages?
* @throws Tinebase_Exception_SystemGeneric
*/
protected function _moveMessagesByFolder(Tinebase_Record_RecordSet $_messages,
$_folderId,
$_targetFolder,
$keepOriginalMessages = false,
$checkCopyPreventionConfig = true)
{
$messagesInFolder = $_messages->filter('folder_id', $_folderId);
if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
. ' Moving messages: ' . print_r($messagesInFolder->getArrayOfIds(), TRUE));
$result = TRUE;
if ($_targetFolder === Felamimail_Model_Folder::FOLDER_TRASH) {
$result = $this->_moveMessagesToTrash($messagesInFolder, $_folderId);
} else if ($_folderId === $_targetFolder->getId()) {
// no need to move
$result = FALSE;
} else if ($messagesInFolder->getFirstRecord()->account_id == $_targetFolder->account_id) {
if (Felamimail_Config::getInstance()->get(Felamimail_Config::PREVENT_COPY_OF_MAILS_IN_SAME_ACCOUNT)
&& $keepOriginalMessages
&& $checkCopyPreventionConfig
) {
$translation = Tinebase_Translation::getTranslation('Felamimail');
throw new Tinebase_Exception_SystemGeneric($translation->_('It is not allowed to copy e-mails in the same account.'));
}
$this->_moveMessagesInFolderOnSameAccount($messagesInFolder, $_targetFolder, $keepOriginalMessages);
} else {
$this->_moveMessagesToAnotherAccount($messagesInFolder, $_targetFolder);
if (! $keepOriginalMessages) {
// we might also do this: $_imap->addFlags($messagesInFolder, array(Zend_Mail_Storage::FLAG_DELETED));
$this->_moveMessagesToTrash($messagesInFolder, $_folderId);
}
}
if (! $result) {
$_messages->removeRecords($messagesInFolder);
}
return $result;
}
/**
* move messages to trash
*
* @param Tinebase_Record_RecordSet $_messagesInFolder
* @param string $_folderId
* @return boolean did we move messages?
*/
protected function _moveMessagesToTrash(Tinebase_Record_RecordSet $_messagesInFolder, $_folderId)
{
// messages should be moved to trash -> need to determine the trash folder for the account of the folder that contains the messages
$targetFolder = Felamimail_Controller_Account::getInstance()->getSystemFolder(
$_messagesInFolder->getFirstRecord()->account_id,
Felamimail_Model_Folder::FOLDER_TRASH
);
if ($_folderId === $targetFolder->id) {
return FALSE;
}
try {
$this->_moveMessagesInFolderOnSameAccount($_messagesInFolder, $targetFolder);
} catch (Tinebase_Exception_NotFound $tenf) {
if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
. ' No trash folder found - skipping messages in this folder.');
return FALSE;
}
return TRUE;
}
/**
* move messages from one folder to another folder within the same email account
*
* @param Tinebase_Record_RecordSet $_messages
* @param Felamimail_Model_Folder $_targetFolder
* @param boolean $keepOriginalMessages
*/
protected function _moveMessagesInFolderOnSameAccount(Tinebase_Record_RecordSet $_messages,
Felamimail_Model_Folder $_targetFolder,
$keepOriginalMessages = false)
{
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ .
' Move ' . count($_messages) . ' message(s) to ' . $_targetFolder->globalname
);
$firstMessage = $_messages->getFirstRecord();
$folder = Felamimail_Controller_Folder::getInstance()->get($firstMessage->folder_id);
$imapBackend = $this->_getBackendAndSelectFolder(NULL, $folder);
$imapMessageUids = array();
foreach ($_messages as $message) {
$imapMessageUids[] = $message->messageuid;
if (count($imapMessageUids) >= 50) {
$this->_moveBatchOfMessages($imapMessageUids, $_targetFolder->globalname, $imapBackend, $keepOriginalMessages);
$imapMessageUids = array();
}
}
// the rest
if (count($imapMessageUids) > 0) {
$this->_moveBatchOfMessages($imapMessageUids, $_targetFolder->globalname, $imapBackend, $keepOriginalMessages);
}
}
/**
* move messages to another email account
*
* @param Tinebase_Record_RecordSet $_messages
* @param Felamimail_Model_Folder $_targetFolder
*/
protected function _moveMessagesToAnotherAccount(Tinebase_Record_RecordSet $_messages,
Felamimail_Model_Folder $_targetFolder)
{
if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ .
' Move ' . count($_messages) . ' message(s) to ' . $_targetFolder->globalname . ' in account ' . $_targetFolder->account_id
);
foreach ($_messages as $message) {
$part = Felamimail_Controller_Message::getInstance()->getMessagePart($message);
$this->appendMessage($_targetFolder, $part->getRawStream(), $message->flags);
}
}
/**
* update folder count after moving messages
*
* @param Tinebase_Record_RecordSet $_messages
* @param array $_folderCounterById
* @return Tinebase_Record_RecordSet of Felamimail_Model_Folder
*/
protected function _updateCountsAfterMove(Tinebase_Record_RecordSet $_messages)
{
$folderCounterById = array();
foreach ($_messages as $message) {
if (! (isset($folderCounterById[$message->folder_id]) || array_key_exists($message->folder_id, $folderCounterById))) {
$folderCounterById[$message->folder_id] = array(
'decrementUnreadCounter' => 0,
'decrementMessagesCounter' => 0,
);
}
if (!is_array($message->flags) || !in_array(Zend_Mail_Storage::FLAG_SEEN, $message->flags)) {
// count messages with seen flag for the first time
$folderCounterById[$message->folder_id]['decrementUnreadCounter']++;
}
$folderCounterById[$message->folder_id]['decrementMessagesCounter']++;
}
return $this->_updateFolderCounts($folderCounterById);
}
/**
* move messages on imap server
*
* @param array $_uids
* @param string $_targetFolderName
* @param boolean $keepOriginalMessages
* @param Felamimail_Backend_ImapProxy $_imap
*
* @todo perhaps we should check the existance of the messages on the imap instead of catching the exceptions here
*/
protected function _moveBatchOfMessages($_uids, $_targetFolderName, Felamimail_Backend_ImapProxy $_imap, $keepOriginalMessages)
{
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
. ' Move ' . count($_uids) . ' messages to folder ' . $_targetFolderName . ' on imap server');
try {
$_imap->copyMessage($_uids, Felamimail_Model_Folder::encodeFolderName($_targetFolderName));
if (! $keepOriginalMessages) {
$_imap->addFlags($_uids, array(Zend_Mail_Storage::FLAG_DELETED));
}
} catch (Zend_Mail_Storage_Exception $zmse) {
if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' ' . $zmse);
} catch (Felamimail_Exception_IMAP $fei) {
if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' ' . $fei);
}
}
}