Conditions | 58 |
Paths | > 20000 |
Total Lines | 284 |
Code Lines | 155 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | #[Override] |
||
32 | public function save($store, $parententryid, $entryid, $action) { |
||
33 | $result = false; |
||
34 | $send = false; |
||
35 | $saveChanges = true; |
||
36 | |||
37 | if (!$store) { |
||
|
|||
38 | $store = $GLOBALS['mapisession']->getDefaultMessageStore(); |
||
39 | } |
||
40 | if (!$parententryid) { |
||
41 | if (isset($action['props'], $action['props']['message_class'])) { |
||
42 | $parententryid = $this->getDefaultFolderEntryID($store, $action['props']['message_class']); |
||
43 | } |
||
44 | else { |
||
45 | $parententryid = $this->getDefaultFolderEntryID($store, ''); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | if ($store) { |
||
50 | // Reference to an array which will be filled with PR_ENTRYID, PR_STORE_ENTRYID and PR_PARENT_ENTRYID of the message |
||
51 | $messageProps = []; |
||
52 | $attachments = !empty($action['attachments']) ? $action['attachments'] : []; |
||
53 | $recipients = !empty($action['recipients']) ? $action['recipients'] : []; |
||
54 | |||
55 | // Set message flags first, because this has to be possible even if the user does not have write permissions |
||
56 | if (isset($action['props'], $action['props']['message_flags']) && $entryid) { |
||
57 | $msg_action = $action['message_action'] ?? false; |
||
58 | $result = $GLOBALS['operations']->setMessageFlag($store, $entryid, $action['props']['message_flags'], $msg_action, $messageProps); |
||
59 | |||
60 | unset($action['props']['message_flags']); |
||
61 | } |
||
62 | |||
63 | if (isset($action['message_action'], $action['message_action']['send'])) { |
||
64 | $send = $action['message_action']['send']; |
||
65 | } |
||
66 | |||
67 | // if we are sending mail then no need to check if anything is modified or not just send the mail |
||
68 | if (!$send) { |
||
69 | // If there is any property changed then save |
||
70 | $saveChanges = !empty($action['props']); |
||
71 | |||
72 | // Check if we are dealing with drafts and recipients or attachments information is modified |
||
73 | if (!$saveChanges) { |
||
74 | // check for changes in attachments |
||
75 | if (isset($attachments['dialog_attachments'])) { |
||
76 | $attachment_state = new AttachmentState(); |
||
77 | $attachment_state->open(); |
||
78 | $saveChanges = $attachment_state->isChangesPending($attachments['dialog_attachments']); |
||
79 | $attachment_state->close(); |
||
80 | } |
||
81 | |||
82 | // check for changes in recipients info |
||
83 | $saveChanges = $saveChanges || !empty($recipients); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | // check we should send/save mail |
||
88 | if ($saveChanges) { |
||
89 | $copyAttachments = false; |
||
90 | $copyFromStore = false; |
||
91 | $copyFromMessage = false; |
||
92 | $copyInlineAttachmentsOnly = false; |
||
93 | |||
94 | if (isset($action['message_action'], $action['message_action']['action_type'])) { |
||
95 | $actions = ['reply', 'replyall', 'forward', 'edit_as_new']; |
||
96 | if (array_search($action['message_action']['action_type'], $actions) !== false) { |
||
97 | /** |
||
98 | * we need to copy the original attachments when it is a forwarded message, or an "edit as new" message |
||
99 | * OR |
||
100 | * we need to copy ONLY original inline(HIDDEN) attachments when it is reply/replyall message. |
||
101 | */ |
||
102 | $copyFromMessage = hex2bin((string) $action['message_action']['source_entryid']); |
||
103 | $copyFromStore = hex2bin((string) $action['message_action']['source_store_entryid']); |
||
104 | $copyFromAttachNum = !empty($action['message_action']['source_attach_num']) ? $action['message_action']['source_attach_num'] : false; |
||
105 | $copyAttachments = true; |
||
106 | |||
107 | // get resources of store and message |
||
108 | $copyFromStore = $GLOBALS['mapisession']->openMessageStore($copyFromStore); |
||
109 | $copyFromMessage = $GLOBALS['operations']->openMessage($copyFromStore, $copyFromMessage, $copyFromAttachNum); |
||
110 | if ($copyFromStore && $send) { |
||
111 | $store = $copyFromStore; |
||
112 | } |
||
113 | |||
114 | // Decode smime signed messages on this message |
||
115 | parse_smime($copyFromStore, $copyFromMessage); |
||
116 | |||
117 | if ($action['message_action']['action_type'] === 'reply' || $action['message_action']['action_type'] === 'replyall') { |
||
118 | $copyInlineAttachmentsOnly = true; |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | elseif (isset($action['props']['sent_representing_email_address'], $action['props']['sent_representing_address_type']) && |
||
123 | strcasecmp($action['props']['sent_representing_address_type'], 'EX') == 0) { |
||
124 | $otherstore = $GLOBALS["mapisession"]->addUserStore($action['props']['sent_representing_email_address']); |
||
125 | if ($otherstore && $send) { |
||
126 | $store = $otherstore; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | if ($send) { |
||
131 | // Allowing to hook in just before the data sent away to be sent to the client |
||
132 | $success = true; |
||
133 | $GLOBALS['PluginManager']->triggerHook('server.module.createmailitemmodule.beforesend', [ |
||
134 | 'moduleObject' => $this, |
||
135 | 'store' => $store, |
||
136 | 'entryid' => $entryid, |
||
137 | 'action' => $action, |
||
138 | 'success' => &$success, |
||
139 | 'properties' => $this->properties, |
||
140 | 'messageProps' => $messageProps, |
||
141 | 'parententryid' => $parententryid, |
||
142 | ]); |
||
143 | // Break out, hook should use sendFeedback to return a response to the client. |
||
144 | if (!$success) { |
||
145 | return; |
||
146 | } |
||
147 | |||
148 | if (!(isset($action['message_action']['action_type']) && $action['message_action']['action_type'] === 'edit_as_new')) { |
||
149 | $this->setReplyForwardInfo($action); |
||
150 | } |
||
151 | |||
152 | $savedUnsavedRecipients = []; |
||
153 | |||
154 | /* |
||
155 | * If message was saved then open that message and retrieve |
||
156 | * all recipients from message and prepare array under "saved" key |
||
157 | */ |
||
158 | if ($entryid) { |
||
159 | $message = $GLOBALS['operations']->openMessage($store, $entryid); |
||
160 | $savedRecipients = $GLOBALS['operations']->getRecipientsInfo($message); |
||
161 | foreach ($savedRecipients as $recipient) { |
||
162 | $savedUnsavedRecipients["saved"][] = $recipient['props']; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /* |
||
167 | * If message some unsaved recipients then prepare array under the "unsaved" |
||
168 | * key. |
||
169 | */ |
||
170 | if (!empty($recipients) && !empty($recipients["add"])) { |
||
171 | foreach ($recipients["add"] as $recipient) { |
||
172 | $savedUnsavedRecipients["unsaved"][] = $recipient; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $remove = []; |
||
177 | if (!empty($recipients) && !empty($recipients["remove"])) { |
||
178 | $remove = $recipients["remove"]; |
||
179 | } |
||
180 | |||
181 | $members = $GLOBALS['operations']->convertLocalDistlistMembersToRecipients($savedUnsavedRecipients, $remove); |
||
182 | |||
183 | $action["recipients"]["add"] = $members["add"]; |
||
184 | |||
185 | if (!empty($remove)) { |
||
186 | $action["recipients"]["remove"] = array_merge($action["recipients"]["remove"], $members["remove"]); |
||
187 | } |
||
188 | else { |
||
189 | $action["recipients"]["remove"] = $members["remove"]; |
||
190 | } |
||
191 | |||
192 | $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); |
||
193 | |||
194 | // If draft is sent from the drafts folder, delete notification |
||
195 | if (!$error) { |
||
196 | $result = true; |
||
197 | $GLOBALS['operations']->parseDistListAndAddToRecipientHistory($savedUnsavedRecipients, $remove); |
||
198 | |||
199 | if (isset($entryid) && !empty($entryid)) { |
||
200 | $props = []; |
||
201 | $props[PR_ENTRYID] = $entryid; |
||
202 | $props[PR_PARENT_ENTRYID] = $parententryid; |
||
203 | |||
204 | $storeprops = mapi_getprops($store, [PR_ENTRYID]); |
||
205 | $props[PR_STORE_ENTRYID] = $storeprops[PR_ENTRYID]; |
||
206 | |||
207 | $GLOBALS['bus']->addData($this->getResponseData()); |
||
208 | $GLOBALS['bus']->notify(bin2hex($parententryid), TABLE_DELETE, $props); |
||
209 | } |
||
210 | $this->sendFeedback($result ? true : false, [], false); |
||
211 | } |
||
212 | else { |
||
213 | if ($error === 'MAPI_E_NO_ACCESS') { |
||
214 | // Handling error: not able to handle this type of object |
||
215 | $data = []; |
||
216 | $data["type"] = 1; // MAPI |
||
217 | $data["info"] = []; |
||
218 | $data["info"]['title'] = _("Insufficient permissions"); |
||
219 | $data["info"]['display_message'] = _("You don't have the permission to complete this action"); |
||
220 | $this->addActionData("error", $data); |
||
221 | } |
||
222 | if ($error === "ecQuotaExceeded") { |
||
223 | // Handling error: Send quota error |
||
224 | $data = []; |
||
225 | $data["type"] = 1; // MAPI |
||
226 | $data["info"] = []; |
||
227 | $data["info"]['title'] = _("Quota error"); |
||
228 | $data["info"]['display_message'] = _("Send quota limit reached"); |
||
229 | $this->addActionData("error", $data); |
||
230 | } |
||
231 | if ($error === "ecRpcFailed") { |
||
232 | // Handling error: mapi_message_submitmessage failed |
||
233 | $data = []; |
||
234 | $data["type"] = 1; // MAPI |
||
235 | $data["info"] = []; |
||
236 | $data["info"]['title'] = _("Operation failed"); |
||
237 | $data["info"]['display_message'] = _("Email sending failed. Check the log files for more information."); |
||
238 | $this->addActionData("error", $data); |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | else { |
||
243 | $propertiesToDelete = []; |
||
244 | $mapiProps = Conversion::mapXML2MAPI($this->properties, $action['props']); |
||
245 | |||
246 | /* |
||
247 | * PR_SENT_REPRESENTING_ENTRYID and PR_SENT_REPRESENTING_SEARCH_KEY properties needs to be deleted while user removes |
||
248 | * any previously configured recipient from FROM field. |
||
249 | * This property was simply ignored by Conversion::mapXML2MAPI function |
||
250 | * as it is configured with empty string in request. |
||
251 | */ |
||
252 | if (isset($action['props']['sent_representing_entryid']) && empty($action['props']['sent_representing_entryid'])) { |
||
253 | array_push($propertiesToDelete, PR_SENT_REPRESENTING_ENTRYID, PR_SENT_REPRESENTING_SEARCH_KEY); |
||
254 | } |
||
255 | |||
256 | $result = $GLOBALS['operations']->saveMessage($store, $entryid, $parententryid, $mapiProps, $messageProps, $action['recipients'] ?? [], $action['attachments'] ?? [], $propertiesToDelete, $copyFromMessage, $copyAttachments, false, $copyInlineAttachmentsOnly); |
||
257 | |||
258 | // Update the client with the (new) entryid and parententryid to allow the draft message to be removed when submitting. |
||
259 | // this will also update rowids of attachments which is required when deleting attachments |
||
260 | $props = []; |
||
261 | $props = mapi_getprops($result, [PR_ENTRYID]); |
||
262 | $savedMsg = $GLOBALS['operations']->openMessage($store, $props[PR_ENTRYID]); |
||
263 | |||
264 | $attachNum = !empty($action['attach_num']) ? $action['attach_num'] : false; |
||
265 | |||
266 | // If embedded message is being saved currently than we need to obtain all the |
||
267 | // properties of 'embedded' message instead of simple message and send it in response |
||
268 | if ($attachNum) { |
||
269 | $message = $GLOBALS['operations']->openMessage($store, $props[PR_ENTRYID], $attachNum); |
||
270 | |||
271 | if (empty($message)) { |
||
272 | return; |
||
273 | } |
||
274 | |||
275 | $data['item'] = $GLOBALS['operations']->getEmbeddedMessageProps($store, $message, $this->properties, $savedMsg, $attachNum); |
||
276 | } |
||
277 | else { |
||
278 | $data = $GLOBALS['operations']->getMessageProps($store, $savedMsg, $this->properties, $this->plaintext); |
||
279 | } |
||
280 | |||
281 | /* |
||
282 | * html filter modifies body of the message when opening the message |
||
283 | * but we have just saved the message and even if there are changes in body because of html filter |
||
284 | * we shouldn't send updated body to client otherwise it will mark it as changed |
||
285 | */ |
||
286 | unset($data['props']['body'], $data['props']['html_body'], $data['props']['isHTML']); |
||
287 | |||
288 | $GLOBALS['PluginManager']->triggerHook('server.module.createmailitemmodule.aftersave', [ |
||
289 | 'data' => &$data, |
||
290 | 'entryid' => $props[PR_ENTRYID], |
||
291 | 'action' => $action, |
||
292 | 'properties' => $this->properties, |
||
293 | 'messageProps' => $messageProps, |
||
294 | 'parententryid' => $parententryid, |
||
295 | ]); |
||
296 | |||
297 | $this->addActionData('update', ['item' => $data]); |
||
298 | } |
||
299 | } |
||
300 | if ($result === false && isset($action['message_action']['soft_delete'])) { |
||
301 | $result = true; |
||
302 | } |
||
303 | |||
304 | // Feedback for successful save (without send) |
||
305 | if ($result && !$send && isset($messageProps[PR_PARENT_ENTRYID])) { |
||
306 | $GLOBALS['bus']->notify(bin2hex($messageProps[PR_PARENT_ENTRYID]), TABLE_SAVE, $messageProps); |
||
307 | } |
||
308 | |||
309 | // Feedback for send |
||
310 | if ($send) { |
||
311 | $this->addActionData('update', ['item' => Conversion::mapMAPI2XML($this->properties, $messageProps)]); |
||
312 | } |
||
313 | |||
314 | $this->sendFeedback($result ? true : false, [], true); |
||
315 | } |
||
446 |