Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailsfoldersmanager.php
1<?php
2
4
9
15{
16 public function deleteMails($deleteImmediately = false)
17 {
18 $result = $this->initData(MailboxDirectoryTable::TYPE_TRASH);
19 if (!$result->isSuccess())
20 {
21 return $result;
22 }
23
24 return $this->processDelete($this->getDirPathByType(MailboxDirectoryTable::TYPE_TRASH),$deleteImmediately);
25 }
26
27 public function moveMails($folderToMoveName)
28 {
29 $result = $this->initData();
30 if (!$result->isSuccess())
31 {
32 return $result;
33 }
34 $folders = [];
35 foreach ($this->messages as $index => $message)
36 {
37 if (in_array($message['ID'], $this->messagesIds, true))
38 {
39 $folders[$message['DIR_MD5']] = $message['DIR_MD5'];
40 }
41 }
42 foreach ($folders as $index => $folderHash)
43 {
44 if ($folderHash === md5($folderToMoveName))
45 {
46 return $result->addError(new Main\Error(Loc::getMessage('MAIL_CLIENT_MOVE_TO_SELF_FOLDER', ['#FOLDER#' => $folderToMoveName]),
47 'MAIL_CLIENT_MOVE_TO_SELF_FOLDER'));
48 }
49 }
50
51 $dir = $this->getDirByPath($folderToMoveName);
52
53 if (!$dir)
54 {
55 return $result->addError(new Main\Error(Loc::getMessage('MAIL_CLIENT_MAILBOX_NOT_FOUND'),
56 'MAIL_CLIENT_MAILBOX_NOT_FOUND'));
57 }
58
59 if ($dir->isDisabled())
60 {
61 return $result->addError(new Main\Error(Loc::getMessage('MAIL_CLIENT_FOLDER_IS_DISABLED', ['#FOLDER#' => $folderToMoveName]),
62 'MAIL_CLIENT_FOLDER_IS_DISABLED'));
63 }
64
65 if ($dir->isSpam())
66 {
67 return $this->sendMailsToSpam();
68 }
69 elseif ($dir->isTrash())
70 {
71 return $this->deleteMails();
72 }
73 elseif ($dir->isIncome())
74 {
75 return $this->restoreMailsFromSpam();
76 }
77
78 $result = $this->moveMailsToFolder($folderToMoveName);
79 if (!$result->isSuccess())
80 {
81 return (new Main\Result())->addError(new Main\Error(Loc::getMessage('MAIL_CLIENT_SYNC_ERROR'), 'MAIL_CLIENT_SYNC_ERROR'));
82 }
83
84 return (new Main\Result());
85 }
86
87 public function restoreMailsFromSpam()
88 {
89 $result = $this->initData(MailboxDirectoryTable::TYPE_SPAM);
90 if (!$result->isSuccess())
91 {
92 return $result;
93 }
94
95 $result = $this->moveMailsToFolder($this->getDirPathByType(MailboxDirectoryTable::TYPE_INCOME));
96 if (!$result->isSuccess())
97 {
98 return (new Main\Result())->addError(new Main\Error(Loc::getMessage('MAIL_CLIENT_SYNC_ERROR'), 'MAIL_CLIENT_SYNC_ERROR'));
99 }
100 $filter = Mail\BlacklistTable::getUserAddressesListQuery($this->userId, false)->getFilter();
101 $filter[] = ['@ITEM_VALUE' => array_column($this->messages, 'EMAIL')];
102 $filter[] = ['=ITEM_TYPE' => Mail\Blacklist\ItemType::EMAIL];
103 \Bitrix\Mail\BlacklistTable::deleteList($filter);
104
105 return $result;
106 }
107
114 public function sendMailsToSpam()
115 {
116 $result = $this->initData(MailboxDirectoryTable::TYPE_SPAM);
117 if (!$result->isSuccess())
118 {
119 return $result;
120 }
121
122 return $this->processSpam($this->getDirPathByType(MailboxDirectoryTable::TYPE_SPAM));
123 }
124
125 private function processDelete($folderTrashName, $deleteImmediately = false)
126 {
127 $messagesToMove = $messagesToDelete = [];
128
129 foreach ($this->messages as $messageUid)
130 {
131 if ($this->isMailToBeDeleted($messageUid) || $deleteImmediately)
132 {
133 $messagesToDelete[] = $messageUid;
134 }
135 else
136 {
137 $messagesToMove[] = $messageUid;
138 }
139 }
140
141 $result = $this->processMoving($messagesToMove, $folderTrashName);
142 if (!$result->isSuccess())
143 {
144 return (new Main\Result())->addError(new Main\Error(Loc::getMessage('MAIL_CLIENT_SYNC_ERROR'), 'MAIL_CLIENT_SYNC_ERROR'));
145 }
146
147 return $this->deleteMessages($messagesToDelete, $this->mailbox);
148 }
149
150 private function processMoving($messagesToMove, $destinationDirectory)
151 {
152 if (!$messagesToMove)
153 {
154 return new Main\Result();
155 }
156 $result = $this->moveMailsByImap($messagesToMove, $destinationDirectory);
157 if ($result->isSuccess())
158 {
159 $this->repository->updateMessageFieldsAfterMove($this->messages, $destinationDirectory, $this->mailbox);
160 $this->imapSyncMovedMessages($messagesToMove, $destinationDirectory);
161 return (new Main\Result())->setData($messagesToMove);
162 }
163 else
164 {
165 return (new Main\Result())->addError(new Main\Error(Loc::getMessage('MAIL_CLIENT_SYNC_ERROR'), 'MAIL_CLIENT_SYNC_ERROR'));
166 }
167 }
168
169 private function processSpam($folderSpamName)
170 {
171 $result = $this->moveMailsToFolder($folderSpamName);
172 if (!$result->isSuccess())
173 {
174 return (new Main\Result())->addError(new Main\Error(Loc::getMessage('MAIL_CLIENT_SYNC_ERROR'), 'MAIL_CLIENT_SYNC_ERROR'));
175 }
176 if ($result->getData())
177 {
178 $mailsToBlacklist = [];
179 foreach ($this->messages as $messageUid)
180 {
181 if ($messageUid['EMAIL'] !== $this->mailbox['EMAIL'])
182 {
183 $mailsToBlacklist[] = $messageUid['EMAIL'];
184 }
185 }
186 return $this->repository->addMailsToBlacklist($mailsToBlacklist, $this->userId);
187 }
188 return $result;
189 }
190
191 private function deleteMessages($messagesToDelete, $mailbox)
192 {
193 if (empty($messagesToDelete))
194 {
195 return new Main\Result();
196 }
197
198 $result = $this->mailboxHelper->deleteMails($messagesToDelete);
199
200 if ($result->isSuccess())
201 {
202 $this->repository->deleteMailsCompletely($messagesToDelete, $this->mailbox['USER_ID']);
203 return new Main\Result();
204 }
205
206 return (new Main\Result())->addError(new Main\Error(Loc::getMessage('MAIL_CLIENT_SYNC_ERROR'), 'MAIL_CLIENT_SYNC_ERROR'));
207 }
208
209 private function isMailToBeDeleted($messageUid)
210 {
211 $trashFolder = $this->getDirPathByType(MailboxDirectoryTable::TYPE_TRASH);
212 return md5($trashFolder) === $messageUid['DIR_MD5'];
213 }
214
215 private function moveMailsToFolder($folderToName)
216 {
217 $mailsToMove = [];
218 foreach ($this->messages as $messageUid)
219 {
220 if (md5($folderToName) !== $messageUid['DIR_MD5'])
221 {
222 $mailsToMove[] = $messageUid;
223 }
224 }
225 return $this->processMoving($mailsToMove, $folderToName);
226 }
227
228 private function moveMailsByImap($messagesToMove, $folder)
229 {
230 if (empty($messagesToMove))
231 {
232 return new Main\Result();
233 }
234
235 return $this->mailboxHelper->moveMailsToFolder($messagesToMove, $folder);
236 }
237
238 private function processSyncMovedMessages($folderCurrentNameEncoded)
239 {
240 $folderCurrentName = base64_decode($folderCurrentNameEncoded);
241 $this->mailboxHelper->syncDir($folderCurrentName);
242
243 Mail\MailMessageUidTable::updateList(
244 [
245 '=MAILBOX_ID' => $this->mailboxId,
246 '=DIR_MD5' => md5($folderCurrentName),
247 '==MSG_UID' => 0,
248 '!@IS_OLD' => ['D','R'],
249 ],
250 [
251 'IS_OLD' => 'M',
252 ],
253 );
254 }
255
256 public static function syncMovedMessages($mailboxId, $mailboxUserId, $folderName)
257 {
258 try
259 {
260 $mailManager = new static($mailboxId, []);
261 $mailManager->setMailboxUserId($mailboxUserId);
262 $mailManager->processSyncMovedMessages($folderName);
263 }
264 catch (\Exception $e)
265 {
266 }
267
268 return '';
269 }
270
271 protected function imapSyncMovedMessages($messagesToMove, $folderName)
272 {
273 $messIds = array_map(
274 function ($item)
275 {
276 return $item['ID'];
277 },
278 $messagesToMove
279 );
280
281 \CAgent::addAgent(
282 sprintf(
283 static::class . "::syncMovedMessages(%u, %u, '%s');",
284 $this->mailbox['ID'],
285 $this->mailbox['USER_ID'],
286 base64_encode($folderName)
287 ),
288 'mail'
289 );
290 }
291
293 {
294 $this->mailboxUserId = $mailboxUserId;
295 }
296}
imapSyncMovedMessages($messagesToMove, $folderName)
static syncMovedMessages($mailboxId, $mailboxUserId, $folderName)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29