Total Complexity | 49 |
Total Lines | 382 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like KendoxModule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use KendoxModule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class KendoxModule extends Module { |
||
11 | // Certificate prod environment |
||
12 | public $pfxFile = __DIR__ . '/../resources/KendoxCertificate.pfx'; |
||
13 | public $pfxPw = 'KxTest'; |
||
14 | |||
15 | // Certificate test environment |
||
16 | public $pfxFileTest = __DIR__ . '/../resources/KendoxCertificate.pfx'; |
||
17 | public $pfxPwTest = 'KxTest'; |
||
18 | |||
19 | public $dialogUrl; |
||
20 | public $mapiMessage; |
||
21 | |||
22 | public $kendoxClient; |
||
23 | |||
24 | /** |
||
25 | * @constructor |
||
26 | * |
||
27 | * @param mixed $id |
||
28 | * @param mixed $data |
||
29 | */ |
||
30 | public function __construct($id, $data) { |
||
31 | parent::__construct($id, $data); |
||
32 | $this->store = $GLOBALS['mapisession']->getDefaultMessageStore(); |
||
|
|||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Executes all the actions in the $data variable. |
||
37 | * Exception part is used for authentication errors also. |
||
38 | * |
||
39 | * @return bool true on success or false on failure |
||
40 | */ |
||
41 | #[Override] |
||
42 | public function execute() { |
||
43 | foreach ($this->data as $actionType => $actionData) { |
||
44 | if (isset($actionType)) { |
||
45 | try { |
||
46 | switch ($actionType) { |
||
47 | case "attachmentinfo": |
||
48 | $response = $this->getAttachmentInfo($actionData["storeId"], $actionData["mailEntryId"]); |
||
49 | $this->addActionData($actionType, $response); |
||
50 | $GLOBALS['bus']->addData($this->getResponseData()); |
||
51 | break; |
||
52 | |||
53 | case "upload": |
||
54 | $response = $this->upload($actionData["storeId"], $actionData["mailEntryId"], $actionData["uploadType"], $actionData["selectedAttachments"], $actionData["environment"], $actionData["apiUrl"], $actionData["userEMail"]); |
||
55 | $this->addActionData($actionType, $response); |
||
56 | $GLOBALS['bus']->addData($this->getResponseData()); |
||
57 | break; |
||
58 | } |
||
59 | } |
||
60 | catch (Exception $e) { |
||
61 | $response = []; |
||
62 | $response["Successful"] = false; |
||
63 | $response["errorMessage"] = $e->getMessage(); |
||
64 | $this->addActionData($actionType, $response); |
||
65 | $GLOBALS['bus']->addData($this->getResponseData()); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get attachment information (meta data) of mail. |
||
73 | * |
||
74 | * @param mixed $storeId |
||
75 | * @param mixed $mailEntryId |
||
76 | */ |
||
77 | private function getAttachmentInfo($storeId, $mailEntryId) { |
||
78 | $this->loadMapiMessage($storeId, $mailEntryId); |
||
79 | $items = []; |
||
80 | $attachmentTable = mapi_message_getattachmenttable($this->mapiMessage); |
||
81 | $messageAttachments = mapi_table_queryallrows($attachmentTable, [PR_ATTACH_NUM, PR_ATTACH_SIZE, PR_ATTACH_LONG_FILENAME]); |
||
82 | foreach ($messageAttachments as $att) { |
||
83 | $item = new AttachmentInfo(); |
||
84 | $item->id = $att[PR_ATTACH_NUM]; |
||
85 | $item->name = $att[PR_ATTACH_LONG_FILENAME]; |
||
86 | $item->size = $att[PR_ATTACH_SIZE]; |
||
87 | $items[] = $item; |
||
88 | } |
||
89 | $response = []; |
||
90 | $response["Successful"] = true; |
||
91 | $response["attachments"] = $items; |
||
92 | |||
93 | return $response; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Upload of e-mail to InfoShare. |
||
98 | * |
||
99 | * @param string $mailEntryId Mail Entry ID |
||
100 | * @param mixed $storeId |
||
101 | * @param mixed $uploadType |
||
102 | * @param mixed $selectedAttachments |
||
103 | * @param mixed $environment |
||
104 | * @param mixed $apiUrl |
||
105 | * @param mixed $userEMail |
||
106 | * |
||
107 | * @return object |
||
108 | */ |
||
109 | private function upload($storeId, $mailEntryId, $uploadType, $selectedAttachments, $environment, $apiUrl, $userEMail) { |
||
110 | $emlFile = null; |
||
111 | $this->loadMapiMessage($storeId, $mailEntryId); |
||
112 | // Write temporary message file (.EML) |
||
113 | // Send to Kendox InfoShare |
||
114 | $uploadFiles = []; |
||
115 | if ($uploadType == "fullEmail") { |
||
116 | $emlFile = $this->createTempEmlFileFromMapiMessage($mailEntryId); |
||
117 | if (!file_exists($emlFile)) { |
||
118 | throw new Exception("EML file " . $emlFile . " not available."); |
||
119 | } |
||
120 | $file = new UploadFile(); |
||
121 | $file->tempFile = $emlFile; |
||
122 | $file->fileType = "email"; |
||
123 | $file->fileName = "email.eml"; |
||
124 | $file->fileLength = filesize($emlFile); |
||
125 | $file->kendoxFileId = null; |
||
126 | $uploadFiles[] = $file; |
||
127 | } |
||
128 | if ($uploadType == "attachmentsOnly") { |
||
129 | $uploadFiles = $this->getUploadFilesFromSelectedAttachments($selectedAttachments); |
||
130 | } |
||
131 | |||
132 | try { |
||
133 | $this->sendFiles($environment, $uploadFiles, $apiUrl, $userEMail); |
||
134 | } |
||
135 | catch (Exception $ex) { |
||
136 | throw $ex; |
||
137 | } |
||
138 | finally { |
||
139 | if ($emlFile != null) { |
||
140 | @unlink($emlFile); |
||
141 | } |
||
142 | foreach ($uploadFiles as $uploadFile) { |
||
143 | @unlink($uploadFile->tempFile); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | try { |
||
148 | // Return response |
||
149 | $response = []; |
||
150 | $response["Successful"] = true; |
||
151 | $response["apiUrl"] = $apiUrl; |
||
152 | $response["userEMail"] = $userEMail; |
||
153 | $response["messageId"] = $mailEntryId; |
||
154 | $response["kendoxConnectionId"] = $this->kendoxClient->ConnectionId; |
||
155 | $response["kendoxFiles"] = $uploadFiles; |
||
156 | } |
||
157 | catch (Exception $ex) { |
||
158 | if ($emlFile != null) { |
||
159 | @unlink($emlFile); |
||
160 | } |
||
161 | if ($uploadFiles != null) { |
||
162 | foreach ($uploadFiles as $uploadFile) { |
||
163 | @unlink($uploadFile->tempFile); |
||
164 | } |
||
165 | } |
||
166 | $this->logErrorAndThrow("Error on building response message", $ex); |
||
167 | } |
||
168 | |||
169 | return $response; |
||
170 | } |
||
171 | |||
172 | private function loadMapiMessage($storeId, $mailEntryId) { |
||
173 | try { |
||
174 | // Read message store |
||
175 | $store = $GLOBALS['mapisession']->openMessageStore(hex2bin((string) $storeId)); |
||
176 | } |
||
177 | catch (Exception $ex) { |
||
178 | $this->logErrorAndThrow("Error on open MAPI store", $ex); |
||
179 | } |
||
180 | |||
181 | try { |
||
182 | $this->mapiMessage = mapi_msgstore_openentry($store, hex2bin((string) $mailEntryId)); |
||
183 | } |
||
184 | catch (Exception $ex) { |
||
185 | $this->logErrorAndThrow("Error on open MAPI message", $ex); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | private function createTempEmlFileFromMapiMessage($mailEntryId) { |
||
223 | } |
||
224 | } |
||
225 | |||
226 | private function createTempFilename() { |
||
227 | $tempPath = TMP_PATH; |
||
228 | |||
229 | return $tempPath . $this->getGUID() . ".tmp"; |
||
230 | } |
||
231 | |||
232 | public function getGUID() { |
||
233 | if (function_exists('com_create_guid')) { |
||
234 | return com_create_guid(); |
||
235 | } |
||
236 | mt_srand((float) microtime() * 10000); // optional for php 4.2.0 and up. |
||
237 | $charid = strtoupper(md5(uniqid(random_int(0, mt_getrandmax()), true))); |
||
238 | $hyphen = chr(45); // "-" |
||
239 | |||
240 | return chr(123) . // "{" |
||
241 | substr($charid, 0, 8) . $hyphen . |
||
242 | substr($charid, 8, 4) . $hyphen . |
||
243 | substr($charid, 12, 4) . $hyphen . |
||
244 | substr($charid, 16, 4) . $hyphen . |
||
245 | substr($charid, 20, 12) . |
||
246 | chr(125); // "}" |
||
247 | } |
||
248 | |||
249 | private function getUploadFilesFromSelectedAttachments($selectedAttachments) { |
||
250 | try { |
||
251 | $uploadFiles = []; |
||
252 | $attachmentTable = mapi_message_getattachmenttable($this->mapiMessage); |
||
253 | $messageAttachments = mapi_table_queryallrows($attachmentTable, [PR_ATTACH_NUM, PR_ATTACH_SIZE, PR_ATTACH_LONG_FILENAME]); |
||
254 | foreach ($selectedAttachments as $att) { |
||
255 | $tmpFile = $this->createTempFilename(); |
||
256 | |||
257 | try { |
||
258 | $this->saveAttachmentToTempFile($tmpFile, $att["attachmentNumber"]); |
||
259 | $file = new UploadFile(); |
||
260 | $file->tempFile = $tmpFile; |
||
261 | $file->fileType = "attachment"; |
||
262 | $file->fileName = $messageAttachments[$att["attachmentNumber"]][PR_ATTACH_LONG_FILENAME]; |
||
263 | $file->fileLength = filesize($tmpFile); |
||
264 | $file->kendoxFileId = null; |
||
265 | $uploadFiles[] = $file; |
||
266 | } |
||
267 | catch (Exception $exFile) { |
||
268 | @unlink($tmpFile); |
||
269 | |||
270 | throw $exFile; |
||
271 | } |
||
272 | } |
||
273 | if (count($uploadFiles) == 0) { |
||
274 | throw new Exception("No attachments selected."); |
||
275 | } |
||
276 | |||
277 | return $uploadFiles; |
||
278 | } |
||
279 | catch (Exception $ex) { |
||
280 | $this->logErrorAndThrow("Error on creating upload files from attachment.", $ex); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Sending file to Kendox InfoShare. |
||
286 | * |
||
287 | * @param UploadFile[] $uploadFiles Files to upload |
||
288 | * @param mixed $environment |
||
289 | * @param mixed $apiUrl |
||
290 | * @param mixed $userEMail |
||
291 | */ |
||
292 | private function sendFiles($environment, $uploadFiles, $apiUrl, $userEMail) { |
||
293 | try { |
||
294 | $pfx = $this->pfxFile; |
||
295 | $pfxPw = $this->pfxPw; |
||
296 | if ($environment != "prod") { |
||
297 | $pfx = $this->pfxFileTest; |
||
298 | $pfxPw = $this->pfxPwTest; |
||
299 | } |
||
300 | if (!file_exists($pfx)) { |
||
301 | throw new Exception(_("Kendox certificate is not available.")); |
||
302 | } |
||
303 | $this->kendoxClient = new Client($apiUrl); |
||
304 | $uid = $this->kendoxClient->loginWithToken($pfx, $pfxPw, "svc_grommunio"); |
||
305 | $query = [ |
||
306 | [ |
||
307 | "ColumnName" => "email", |
||
308 | "RelationalOperator" => "Equals", |
||
309 | "Value" => $userEMail], |
||
310 | ]; |
||
311 | $result = $this->kendoxClient->userTableQuery("grommunio", $query, false); |
||
312 | if (count($result) == 0) { |
||
313 | throw new Exception("User with e-mail address " . $userEMail . " not found in Kendox user table grommunio."); |
||
314 | } |
||
315 | $uid = $result[0][1]; |
||
316 | $this->kendoxClient->logout(); |
||
317 | // Login and upload |
||
318 | $this->kendoxClient->loginWithToken($pfx, $pfxPw, $uid); |
||
319 | foreach ($uploadFiles as $uploadFile) { |
||
320 | try { |
||
321 | $uploadFile->kendoxFileId = $this->kendoxClient->uploadFile($uploadFile->tempFile); |
||
322 | } |
||
323 | catch (Exception $exUpload) { |
||
324 | $this->logErrorAndThrow("Upload of file " . $uploadFile->fileName . " failed.", $exUpload); |
||
325 | } |
||
326 | } |
||
327 | } |
||
328 | catch (Exception $ex) { |
||
329 | $this->logErrorAndThrow("Sending files failed", $ex); |
||
330 | } |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Logging an error and throw the exception. |
||
335 | * |
||
336 | * @param string $displayMessage |
||
337 | * @param Exception $ex |
||
338 | */ |
||
339 | public function logErrorAndThrow($displayMessage, $ex): never { |
||
340 | $errMsg = $displayMessage . ": " . $ex->getMessage(); |
||
341 | error_log($errMsg); |
||
342 | error_log($ex->getTraceAsString()); |
||
343 | |||
344 | throw new Exception($errMsg); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Function will obtain stream from the message, For email messages it will open email as |
||
349 | * inet object and get the stream content as eml format. |
||
350 | * |
||
351 | * @param array $messageProps properties of this particular message |
||
352 | * |
||
353 | * @return Stream $stream the eml stream obtained from message |
||
354 | */ |
||
355 | public function getEmlStream($messageProps) { |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Function to get binary content of an attachment |
||
363 | * PR_ATTACH_DATA_BIN. |
||
364 | * |
||
365 | * @param string $tempFile Path and file of temporary attachment file |
||
366 | * @param int $attachmentNumber Number of attachment |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | public function saveAttachmentToTempFile($tempFile, $attachmentNumber) { |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Function to sanitize user input values to prevent XSS attacks. |
||
377 | * |
||
378 | * @param mixed $value value that should be sanitized |
||
379 | * @param mixed $default default value to return when value is not safe |
||
380 | * @param string $regex regex to validate values based on type of value passed |
||
381 | */ |
||
382 | public function sanitizeValue($value, $default = '', $regex = false) { |
||
392 | } |
||
393 | } |
||
394 |