1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Create Mail ItemModule |
||
5 | * Module which opens, creates, saves and deletes an item. It |
||
6 | * extends the Module class. |
||
7 | */ |
||
8 | class CreateMailItemModule extends ItemModule { |
||
9 | /** |
||
10 | * Constructor. |
||
11 | * |
||
12 | * @param int $id unique id |
||
13 | * @param array $data list of all actions |
||
14 | */ |
||
15 | public function __construct($id, $data) { |
||
16 | parent::__construct($id, $data); |
||
17 | |||
18 | $this->properties = $GLOBALS['properties']->getMailProperties(); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * Function which saves and/or sends an item. |
||
23 | * |
||
24 | * @param object $store MAPI Message Store Object |
||
25 | * @param string $parententryid parent entryid of the message |
||
26 | * @param string $entryid entryid of the message |
||
27 | * @param array $action the action data, sent by the client |
||
28 | */ |
||
29 | #[Override] |
||
30 | public function save($store, $parententryid, $entryid, $action) { |
||
31 | $result = false; |
||
32 | $send = false; |
||
33 | $saveChanges = true; |
||
34 | |||
35 | if (!$store) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
36 | $store = $GLOBALS['mapisession']->getDefaultMessageStore(); |
||
37 | } |
||
38 | if (!$parententryid) { |
||
39 | if (isset($action['props'], $action['props']['message_class'])) { |
||
40 | $parententryid = $this->getDefaultFolderEntryID($store, $action['props']['message_class']); |
||
41 | } |
||
42 | else { |
||
43 | $parententryid = $this->getDefaultFolderEntryID($store, ''); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | if ($store) { |
||
0 ignored issues
–
show
|
|||
48 | // Reference to an array which will be filled with PR_ENTRYID, PR_STORE_ENTRYID and PR_PARENT_ENTRYID of the message |
||
49 | $messageProps = []; |
||
50 | $attachments = !empty($action['attachments']) ? $action['attachments'] : []; |
||
51 | $recipients = !empty($action['recipients']) ? $action['recipients'] : []; |
||
52 | |||
53 | // Set message flags first, because this has to be possible even if the user does not have write permissions |
||
54 | if (isset($action['props'], $action['props']['message_flags']) && $entryid) { |
||
55 | $msg_action = $action['message_action'] ?? false; |
||
56 | $result = $GLOBALS['operations']->setMessageFlag($store, $entryid, $action['props']['message_flags'], $msg_action, $messageProps); |
||
57 | |||
58 | unset($action['props']['message_flags']); |
||
59 | } |
||
60 | |||
61 | if (isset($action['message_action'], $action['message_action']['send'])) { |
||
62 | $send = $action['message_action']['send']; |
||
63 | } |
||
64 | |||
65 | // if we are sending mail then no need to check if anything is modified or not just send the mail |
||
66 | if (!$send) { |
||
67 | // If there is any property changed then save |
||
68 | $saveChanges = !empty($action['props']); |
||
69 | |||
70 | // Check if we are dealing with drafts and recipients or attachments information is modified |
||
71 | if (!$saveChanges) { |
||
72 | // check for changes in attachments |
||
73 | if (isset($attachments['dialog_attachments'])) { |
||
74 | $attachment_state = new AttachmentState(); |
||
75 | $attachment_state->open(); |
||
76 | $saveChanges = $attachment_state->isChangesPending($attachments['dialog_attachments']); |
||
77 | $attachment_state->close(); |
||
78 | } |
||
79 | |||
80 | // check for changes in recipients info |
||
81 | $saveChanges = $saveChanges || !empty($recipients); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | // check we should send/save mail |
||
86 | if ($saveChanges) { |
||
87 | $copyAttachments = false; |
||
88 | $copyFromStore = false; |
||
0 ignored issues
–
show
|
|||
89 | $copyFromMessage = false; |
||
90 | $copyInlineAttachmentsOnly = false; |
||
91 | |||
92 | if (isset($action['message_action'], $action['message_action']['action_type'])) { |
||
93 | $actions = ['reply', 'replyall', 'forward', 'edit_as_new']; |
||
94 | if (array_search($action['message_action']['action_type'], $actions) !== false) { |
||
95 | /** |
||
96 | * we need to copy the original attachments when it is a forwarded message, or an "edit as new" message |
||
97 | * OR |
||
98 | * we need to copy ONLY original inline(HIDDEN) attachments when it is reply/replyall message. |
||
99 | */ |
||
100 | $copyFromMessage = hex2bin((string) $action['message_action']['source_entryid']); |
||
101 | $copyFromStore = hex2bin((string) $action['message_action']['source_store_entryid']); |
||
102 | $copyFromAttachNum = !empty($action['message_action']['source_attach_num']) ? $action['message_action']['source_attach_num'] : false; |
||
103 | $copyAttachments = true; |
||
104 | |||
105 | // get resources of store and message |
||
106 | $copyFromStore = $GLOBALS['mapisession']->openMessageStore($copyFromStore); |
||
107 | $copyFromMessage = $GLOBALS['operations']->openMessage($copyFromStore, $copyFromMessage, $copyFromAttachNum); |
||
108 | if ($copyFromStore && $send) { |
||
109 | $store = $copyFromStore; |
||
110 | } |
||
111 | |||
112 | // Decode smime signed messages on this message |
||
113 | parse_smime($copyFromStore, $copyFromMessage); |
||
114 | |||
115 | if ($action['message_action']['action_type'] === 'reply' || $action['message_action']['action_type'] === 'replyall') { |
||
116 | $copyInlineAttachmentsOnly = true; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | elseif (isset($action['props']['sent_representing_email_address'], $action['props']['sent_representing_address_type']) && |
||
121 | strcasecmp($action['props']['sent_representing_address_type'], 'EX') == 0) { |
||
122 | $otherstore = $GLOBALS["mapisession"]->addUserStore($action['props']['sent_representing_email_address']); |
||
123 | if ($otherstore && $send) { |
||
124 | $store = $otherstore; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | if ($send) { |
||
129 | // Allowing to hook in just before the data sent away to be sent to the client |
||
130 | $success = true; |
||
131 | $GLOBALS['PluginManager']->triggerHook('server.module.createmailitemmodule.beforesend', [ |
||
132 | 'moduleObject' => $this, |
||
133 | 'store' => $store, |
||
134 | 'entryid' => $entryid, |
||
135 | 'action' => $action, |
||
136 | 'success' => &$success, |
||
137 | 'properties' => $this->properties, |
||
138 | 'messageProps' => $messageProps, |
||
139 | 'parententryid' => $parententryid, |
||
140 | ]); |
||
141 | // Break out, hook should use sendFeedback to return a response to the client. |
||
142 | if (!$success) { |
||
0 ignored issues
–
show
|
|||
143 | return; |
||
144 | } |
||
145 | |||
146 | if (!(isset($action['message_action']['action_type']) && $action['message_action']['action_type'] === 'edit_as_new')) { |
||
147 | $this->setReplyForwardInfo($action); |
||
148 | } |
||
149 | |||
150 | $savedUnsavedRecipients = []; |
||
151 | |||
152 | /* |
||
153 | * If message was saved then open that message and retrieve |
||
154 | * all recipients from message and prepare array under "saved" key |
||
155 | */ |
||
156 | if ($entryid) { |
||
157 | $message = $GLOBALS['operations']->openMessage($store, $entryid); |
||
158 | $savedRecipients = $GLOBALS['operations']->getRecipientsInfo($message); |
||
159 | foreach ($savedRecipients as $recipient) { |
||
160 | $savedUnsavedRecipients["saved"][] = $recipient['props']; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /* |
||
165 | * If message some unsaved recipients then prepare array under the "unsaved" |
||
166 | * key. |
||
167 | */ |
||
168 | if (!empty($recipients) && !empty($recipients["add"])) { |
||
169 | foreach ($recipients["add"] as $recipient) { |
||
170 | $savedUnsavedRecipients["unsaved"][] = $recipient; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | $remove = []; |
||
175 | if (!empty($recipients) && !empty($recipients["remove"])) { |
||
176 | $remove = $recipients["remove"]; |
||
177 | } |
||
178 | |||
179 | $members = $GLOBALS['operations']->convertLocalDistlistMembersToRecipients($savedUnsavedRecipients, $remove); |
||
180 | |||
181 | $action["recipients"]["add"] = $members["add"]; |
||
182 | |||
183 | if (!empty($remove)) { |
||
184 | $action["recipients"]["remove"] = array_merge($action["recipients"]["remove"], $members["remove"]); |
||
185 | } |
||
186 | else { |
||
187 | $action["recipients"]["remove"] = $members["remove"]; |
||
188 | } |
||
189 | |||
190 | $error = $GLOBALS['operations']->submitMessage($store, $entryid, Conversion::mapXML2MAPI($this->properties, $action['props']), $messageProps, $action['recipients'] ?? [], $action['attachments'] ?? [], $copyFromMessage, $copyAttachments, false, $copyInlineAttachmentsOnly, isset($action['props']['isHTML']) ? !$action['props']['isHTML'] : false); |
||
191 | |||
192 | // If draft is sent from the drafts folder, delete notification |
||
193 | if (!$error) { |
||
194 | $result = true; |
||
195 | $GLOBALS['operations']->parseDistListAndAddToRecipientHistory($savedUnsavedRecipients, $remove); |
||
196 | |||
197 | if (isset($entryid) && !empty($entryid)) { |
||
198 | $props = []; |
||
199 | $props[PR_ENTRYID] = $entryid; |
||
200 | $props[PR_PARENT_ENTRYID] = $parententryid; |
||
201 | |||
202 | $storeprops = mapi_getprops($store, [PR_ENTRYID]); |
||
203 | $props[PR_STORE_ENTRYID] = $storeprops[PR_ENTRYID]; |
||
204 | |||
205 | $GLOBALS['bus']->addData($this->getResponseData()); |
||
206 | $GLOBALS['bus']->notify(bin2hex($parententryid), TABLE_DELETE, $props); |
||
207 | } |
||
208 | $this->sendFeedback($result ? true : false, [], false); |
||
0 ignored issues
–
show
|
|||
209 | } |
||
210 | else { |
||
211 | if ($error === 'MAPI_E_NO_ACCESS') { |
||
212 | // Handling error: not able to handle this type of object |
||
213 | $data = []; |
||
214 | $data["type"] = 1; // MAPI |
||
215 | $data["info"] = []; |
||
216 | $data["info"]['title'] = _("Insufficient permissions"); |
||
217 | $data["info"]['display_message'] = _("You don't have the permission to complete this action"); |
||
218 | $this->addActionData("error", $data); |
||
219 | } |
||
220 | if ($error === "ecQuotaExceeded") { |
||
221 | // Handling error: Send quota error |
||
222 | $data = []; |
||
223 | $data["type"] = 1; // MAPI |
||
224 | $data["info"] = []; |
||
225 | $data["info"]['title'] = _("Quota error"); |
||
226 | $data["info"]['display_message'] = _("Send quota limit reached"); |
||
227 | $this->addActionData("error", $data); |
||
228 | } |
||
229 | if ($error === "ecRpcFailed") { |
||
230 | // Handling error: mapi_message_submitmessage failed |
||
231 | $data = []; |
||
232 | $data["type"] = 1; // MAPI |
||
233 | $data["info"] = []; |
||
234 | $data["info"]['title'] = _("Operation failed"); |
||
235 | $data["info"]['display_message'] = _("Email sending failed. Check the log files for more information."); |
||
236 | $this->addActionData("error", $data); |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | else { |
||
241 | $propertiesToDelete = []; |
||
242 | $mapiProps = Conversion::mapXML2MAPI($this->properties, $action['props']); |
||
243 | |||
244 | /* |
||
245 | * PR_SENT_REPRESENTING_ENTRYID and PR_SENT_REPRESENTING_SEARCH_KEY properties needs to be deleted while user removes |
||
246 | * any previously configured recipient from FROM field. |
||
247 | * This property was simply ignored by Conversion::mapXML2MAPI function |
||
248 | * as it is configured with empty string in request. |
||
249 | */ |
||
250 | if (isset($action['props']['sent_representing_entryid']) && empty($action['props']['sent_representing_entryid'])) { |
||
251 | array_push($propertiesToDelete, PR_SENT_REPRESENTING_ENTRYID, PR_SENT_REPRESENTING_SEARCH_KEY); |
||
252 | } |
||
253 | |||
254 | $result = $GLOBALS['operations']->saveMessage($store, $entryid, $parententryid, $mapiProps, $messageProps, $action['recipients'] ?? [], $action['attachments'] ?? [], $propertiesToDelete, $copyFromMessage, $copyAttachments, false, $copyInlineAttachmentsOnly); |
||
255 | |||
256 | // Update the client with the (new) entryid and parententryid to allow the draft message to be removed when submitting. |
||
257 | // this will also update rowids of attachments which is required when deleting attachments |
||
258 | $props = []; |
||
0 ignored issues
–
show
|
|||
259 | $props = mapi_getprops($result, [PR_ENTRYID]); |
||
260 | $savedMsg = $GLOBALS['operations']->openMessage($store, $props[PR_ENTRYID]); |
||
261 | |||
262 | $attachNum = !empty($action['attach_num']) ? $action['attach_num'] : false; |
||
263 | |||
264 | // If embedded message is being saved currently than we need to obtain all the |
||
265 | // properties of 'embedded' message instead of simple message and send it in response |
||
266 | if ($attachNum) { |
||
267 | $message = $GLOBALS['operations']->openMessage($store, $props[PR_ENTRYID], $attachNum); |
||
268 | |||
269 | if (empty($message)) { |
||
270 | return; |
||
271 | } |
||
272 | |||
273 | $data['item'] = $GLOBALS['operations']->getEmbeddedMessageProps($store, $message, $this->properties, $savedMsg, $attachNum); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
274 | } |
||
275 | else { |
||
276 | $data = $GLOBALS['operations']->getMessageProps($store, $savedMsg, $this->properties); |
||
277 | } |
||
278 | |||
279 | /* |
||
280 | * html filter modifies body of the message when opening the message |
||
281 | * but we have just saved the message and even if there are changes in body because of html filter |
||
282 | * we shouldn't send updated body to client otherwise it will mark it as changed |
||
283 | */ |
||
284 | unset($data['props']['body'], $data['props']['html_body'], $data['props']['isHTML']); |
||
285 | |||
286 | $GLOBALS['PluginManager']->triggerHook('server.module.createmailitemmodule.aftersave', [ |
||
287 | 'data' => &$data, |
||
288 | 'entryid' => $props[PR_ENTRYID], |
||
289 | 'action' => $action, |
||
290 | 'properties' => $this->properties, |
||
291 | 'messageProps' => $messageProps, |
||
292 | 'parententryid' => $parententryid, |
||
293 | ]); |
||
294 | |||
295 | $this->addActionData('update', ['item' => $data]); |
||
296 | } |
||
297 | } |
||
298 | if ($result === false && isset($action['message_action']['soft_delete'])) { |
||
299 | $result = true; |
||
300 | } |
||
301 | |||
302 | // Feedback for successful save (without send) |
||
303 | if ($result && !$send && isset($messageProps[PR_PARENT_ENTRYID])) { |
||
304 | $GLOBALS['bus']->notify(bin2hex($messageProps[PR_PARENT_ENTRYID]), TABLE_SAVE, $messageProps); |
||
305 | } |
||
306 | |||
307 | // Feedback for send |
||
308 | if ($send) { |
||
309 | $this->addActionData('update', ['item' => Conversion::mapMAPI2XML($this->properties, $messageProps)]); |
||
310 | } |
||
311 | |||
312 | $this->sendFeedback($result ? true : false, [], true); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Function which is used to get the source message information, which contains the information of |
||
318 | * reply/forward and entry id of original mail, where we have to set the reply/forward arrow when |
||
319 | * draft(saved mail) is send. |
||
320 | * |
||
321 | * @param array $action the action data, sent by the client |
||
322 | * |
||
323 | * @return array|bool false when entryid and source message entryid is missing otherwise array with |
||
324 | * source store entryid and source message entryid if message has |
||
325 | */ |
||
326 | public function getSourceMsgInfo($action) { |
||
327 | $metaData = []; |
||
328 | if (isset($action["props"]["source_message_info"]) && !empty($action["props"]["source_message_info"])) { |
||
329 | if (isset($action["props"]['sent_representing_entryid']) && !empty($action["props"]['sent_representing_entryid'])) { |
||
330 | $storeEntryid = hex2bin((string) $action['message_action']['source_store_entryid']); |
||
331 | } |
||
332 | else { |
||
333 | $storeEntryid = hex2bin((string) $action['store_entryid']); |
||
334 | } |
||
335 | $metaData['source_message_info'] = $action["props"]["source_message_info"]; |
||
336 | $metaData['storeEntryid'] = $storeEntryid; |
||
337 | |||
338 | return $metaData; |
||
339 | } |
||
340 | if (isset($action["entryid"]) && !empty($action["entryid"])) { |
||
341 | $storeEntryid = hex2bin((string) $action['store_entryid']); |
||
342 | $store = $GLOBALS['mapisession']->openMessageStore($storeEntryid); |
||
343 | |||
344 | $entryid = hex2bin((string) $action['entryid']); |
||
345 | $message = $GLOBALS['operations']->openMessage($store, $entryid); |
||
346 | $messageProps = mapi_getprops($message); |
||
347 | |||
348 | $props = Conversion::mapMAPI2XML($this->properties, $messageProps); |
||
349 | |||
350 | $sourceMsgInfo = !empty($props['props']['source_message_info']) ? $props['props']['source_message_info'] : false; |
||
351 | |||
352 | if (isset($props["props"]['sent_representing_entryid']) && !empty($props["props"]['sent_representing_entryid'])) { |
||
353 | $storeEntryid = $this->getSourceStoreEntryId($props); |
||
354 | } |
||
355 | |||
356 | $metaData['source_message_info'] = $sourceMsgInfo; |
||
357 | $metaData['storeEntryid'] = $storeEntryid; |
||
358 | |||
359 | return $metaData; |
||
360 | } |
||
361 | |||
362 | return false; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Function is used to get the shared or delegate store entryid where |
||
367 | * source message was stored on which we have to set replay/forward arrow |
||
368 | * when draft(saved mail) is send. |
||
369 | * |
||
370 | * @param array $props the $props data, which get from saved mail |
||
371 | * |
||
372 | * @return string source store entryid |
||
373 | */ |
||
374 | public function getSourceStoreEntryId($props) { |
||
375 | $sentRepresentingEntryid = $props['props']['sent_representing_entryid']; |
||
376 | $user = mapi_ab_openentry($GLOBALS['mapisession']->getAddressbook(), hex2bin((string) $sentRepresentingEntryid)); |
||
377 | $userProps = mapi_getprops($user, [PR_EMAIL_ADDRESS]); |
||
378 | |||
379 | return $GLOBALS['mapisession']->getStoreEntryIdOfUser(strtolower((string) $userProps[PR_EMAIL_ADDRESS])); |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Function is used to set the reply/forward arrow on original mail. |
||
384 | * |
||
385 | * @param array $action the action data, sent by the client |
||
386 | */ |
||
387 | public function setReplyForwardInfo($action) { |
||
388 | $message = false; |
||
0 ignored issues
–
show
|
|||
389 | $sourceMsgInfo = $this->getSourceMsgInfo($action); |
||
390 | if (isset($sourceMsgInfo['source_message_info']) && $sourceMsgInfo['source_message_info']) { |
||
391 | /** |
||
392 | * $sourceMsgInfo['source_message_info'] contains the hex value, where first 24byte contains action type |
||
393 | * and next 48byte contains entryid of original mail. so we have to extract the action type |
||
394 | * from this hex value. |
||
395 | * |
||
396 | * Example : 01000E000C00000005010000660000000200000030000000 + record entryid |
||
397 | * Here 66 represents the REPLY action type. same way 67 and 68 is represent |
||
398 | * REPLY ALL and FORWARD respectively. |
||
399 | */ |
||
400 | $mailActionType = substr((string) $sourceMsgInfo['source_message_info'], 24, 2); |
||
401 | // get the entry id of origanal mail's. |
||
402 | $originalEntryid = substr((string) $sourceMsgInfo['source_message_info'], 48); |
||
403 | $entryid = hex2bin($originalEntryid); |
||
404 | |||
405 | $store = $GLOBALS['mapisession']->openMessageStore($sourceMsgInfo['storeEntryid']); |
||
406 | |||
407 | try { |
||
408 | // if original mail of reply/forward mail is deleted from inbox then, |
||
409 | // it will throw an exception so to handle it we need to write this block in try catch. |
||
410 | $message = $GLOBALS['operations']->openMessage($store, $entryid); |
||
411 | } |
||
412 | catch (MAPIException $e) { |
||
413 | $e->setHandled(); |
||
414 | } |
||
415 | |||
416 | if ($message) { |
||
417 | $messageProps = mapi_getprops($message); |
||
418 | $props = Conversion::mapMAPI2XML($this->properties, $messageProps); |
||
419 | |||
420 | switch ($mailActionType) { |
||
421 | case '66': // Reply |
||
422 | case '67': // Reply All |
||
423 | $props['icon_index'] = 261; |
||
424 | break; |
||
425 | |||
426 | case '68':// Forward |
||
427 | $props['icon_index'] = 262; |
||
428 | break; |
||
429 | } |
||
430 | $props['last_verb_executed'] = hexdec($mailActionType); |
||
431 | $props['last_verb_execution_time'] = time(); |
||
432 | $mapiProps = Conversion::mapXML2MAPI($this->properties, $props); |
||
433 | $messageActionProps = []; |
||
434 | $messageActionResult = $GLOBALS['operations']->saveMessage($store, $mapiProps[PR_ENTRYID], $mapiProps[PR_PARENT_ENTRYID], $mapiProps, $messageActionProps); |
||
435 | if ($messageActionResult) { |
||
436 | if (isset($messageActionProps[PR_PARENT_ENTRYID])) { |
||
437 | $GLOBALS['bus']->notify(bin2hex($messageActionProps[PR_PARENT_ENTRYID]), TABLE_SAVE, $messageActionProps); |
||
438 | } |
||
439 | } |
||
440 | } |
||
441 | } |
||
442 | } |
||
443 | } |
||
444 |