ContactItemModule   F
last analyzed

Complexity

Total Complexity 97

Size/Duplication

Total Lines 631
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 332
c 3
b 2
f 0
dl 0
loc 631
rs 2
wmc 97

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B copyGABRecordProps() 0 42 6
F save() 0 205 47
A searchContactsFolders() 0 27 5
A deleteSpecialDateAppointment() 0 15 2
B open() 0 86 11
B delete() 0 34 10
F updateAppointments() 0 122 14
A getStoreParentEntryIdFromEntryId() 0 7 1

How to fix   Complexity   

Complex Class

Complex classes like ContactItemModule 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 ContactItemModule, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Contact ItemModule
5
 * Module which opens, creates, saves and deletes an item. It
6
 * extends the Module class.
7
 */
8
class ContactItemModule 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
		$this->properties = $GLOBALS['properties']->getContactProperties();
17
18
		parent::__construct($id, $data);
19
20
		$this->plaintext = false;
21
	}
22
23
	/**
24
	 * Function which opens an item.
25
	 *
26
	 * @param object $store   MAPI Message Store Object
27
	 * @param string $entryid entryid of the message
28
	 * @param array  $action  the action data, sent by the client
29
	 */
30
	#[Override]
31
	public function open($store, $entryid, $action) {
32
		$data = [];
33
		$orEntryid = $entryid;
34
35
		if ($entryid) {
36
			// Check if OneOff entryid is a local contact
37
			if ($GLOBALS['entryid']->hasAddressBookOneOff(bin2hex($entryid))) {
38
				try {
39
					$oneoff = mapi_parseoneoff($entryid);
40
					$ab = $GLOBALS['mapisession']->getAddressbook();
41
					$ab_dir = mapi_ab_openentry($ab);
42
					$res = $this->searchContactsFolders($ab, $ab_dir, $oneoff['address']);
43
					if (count($res) > 0) {
44
						$entryid = $res[0][PR_ENTRYID];
45
					}
46
				}
47
				catch (MAPIException $ex) {
0 ignored issues
show
Bug introduced by
The type MAPIException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
					error_log(sprintf(
49
						"Unable to open contact because mapi_parseoneoff failed: %s - %s",
50
						get_mapi_error_name($ex->getCode()),
51
						$ex->getDisplayMessage()
52
					));
53
				}
54
			}
55
			/* Check if given entryid is shared folder distlist then
56
			* get the store of distlist for fetching it's members.
57
			*/
58
			$storeData = $this->getStoreParentEntryIdFromEntryId($entryid);
59
			$store = $storeData["store"];
60
			$message = $storeData["message"];
61
		}
62
63
		if (empty($message)) {
64
			return;
65
		}
66
67
		// Open embedded message if requested
68
		$attachNum = !empty($action['attach_num']) ? $action['attach_num'] : false;
69
		if ($attachNum) {
70
			// get message props of sub message
71
			$parentMessage = $message;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.
Loading history...
72
			$message = $GLOBALS['operations']->openMessage($store, $entryid, $attachNum);
73
74
			if (empty($message)) {
75
				return;
76
			}
77
78
			// Check if message is distlist then we need to use different set of properties
79
			$props = mapi_getprops($message, [PR_MESSAGE_CLASS]);
80
81
			if (class_match_prefix($props[PR_MESSAGE_CLASS], "IPM.Distlist")) {
82
				// for distlist we need to use different set of properties
83
				$this->properties = $GLOBALS['properties']->getDistListProperties();
84
			}
85
86
			$data['item'] = $GLOBALS['operations']->getEmbeddedMessageProps($store, $message, $this->properties, $parentMessage, $attachNum);
87
		}
88
		else {
89
			// Check if message is distlist then we need to use different set of properties
90
			$props = mapi_getprops($message, [PR_MESSAGE_CLASS]);
91
92
			if (class_match_prefix($props[PR_MESSAGE_CLASS], "IPM.Distlist")) {
93
				// for distlist we need to use different set of properties
94
				$this->properties = $GLOBALS['properties']->getDistListProperties();
95
			}
96
97
			// get message props of the message
98
			$data['item'] = $GLOBALS['operations']->getMessageProps($store, $message, $this->properties, $this->plaintext, true);
99
		}
100
101
		// By openentry from address book, the entryid will differ, make it same as the origin
102
		$data['item']['entryid'] = bin2hex($orEntryid);
103
104
		// Allowing to hook in just before the data sent away to be sent to the client
105
		$GLOBALS['PluginManager']->triggerHook('server.module.contactitemmodule.open.after', [
106
			'moduleObject' => &$this,
107
			'store' => $store,
108
			'entryid' => $entryid,
109
			'action' => $action,
110
			'message' => &$message,
111
			'data' => &$data,
112
		]);
113
114
		$this->addActionData('item', $data);
115
		$GLOBALS['bus']->addData($this->getResponseData());
116
	}
117
118
	/**
119
	 * Function which saves an item. It sets the right properties for a contact
120
	 * item (address book properties).
121
	 *
122
	 * @param object $store         MAPI Message Store Object
123
	 * @param string $parententryid parent entryid of the message
124
	 * @param string $entryid       entryid of the message
125
	 * @param array  $action        the action data, sent by the client
126
	 */
127
	#[Override]
128
	public function save($store, $parententryid, $entryid, $action, $actionType = 'save') {
129
		$properiesToDelete = []; // create an array of properties which should be deleted
130
		// this array is passed to $GLOBALS['operations']->saveMessage() function
131
132
		if (!$store && !$parententryid) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
133
			if (isset($action['props']['message_class'])) {
134
				$store = $GLOBALS['mapisession']->getDefaultMessageStore();
135
				$parententryid = $this->getDefaultFolderEntryID($store, $action['props']['message_class']);
136
			}
137
			elseif ($entryid) {
138
				$data = $this->getStoreParentEntryIdFromEntryId($entryid);
139
				$store = $data["store"];
140
				$parententryid = $data["parent_entryid"];
141
			}
142
		}
143
		elseif (!$parententryid) {
144
			if (isset($action['props']['message_class'])) {
145
				$parententryid = $this->getDefaultFolderEntryID($store, $action['props']['message_class']);
146
			}
147
		}
148
149
		if ($store && $parententryid && isset($action['props'])) {
150
			if (isset($action['members'])) {
151
				// DistList
152
153
				// for distlist we need to use different set of properties
154
				$this->properties = $GLOBALS['properties']->getDistListProperties();
155
156
				// do conversion of client data
157
				$props = Conversion::mapXML2MAPI($this->properties, $action['props']);
158
159
				// collect members
160
				$members = [];
161
				$oneoff_members = [];
162
163
				$items = $action['members'];
164
165
				foreach ($items as $item) {
166
					if (empty($item['email_address'])) {
167
						// if no email address is given then mapi_parseoneoff fails, so always give
168
						// email address, OL07 uses Unknown as email address so we do same here
169
						$item['email_address'] = 'Unknown';
170
					}
171
172
					$oneoff = mapi_createoneoff($item['display_name'], $item['address_type'], $item['email_address']);
173
174
					if ($item['distlist_type'] == DL_EXTERNAL_MEMBER) {
175
						$member = $oneoff;
176
					}
177
					else {
178
						$parts = [];
179
						$parts['distlist_guid'] = WAB_GUID;
180
						$parts['distlist_type'] = $item['distlist_type'];
181
						$parts['entryid'] = hex2bin((string) $item['entryid']);
182
						$member = pack('VA16CA*', 0, $parts['distlist_guid'], $parts['distlist_type'], $parts['entryid']);
183
					}
184
185
					$oneoff_members[] = $oneoff;
186
					$members[] = $member;
187
				}
188
189
				if (!empty($members) && !empty($oneoff_members)) {
190
					$props[$this->properties['members']] = $members;
191
					$props[$this->properties['oneoff_members']] = $oneoff_members;
192
				}
193
				else {
194
					$properiesToDelete[] = $this->properties['members'];
195
					$properiesToDelete[] = $this->properties['oneoff_members'];
196
				}
197
198
				unset($action['members']);
199
			}
200
			else {
201
				// Contact
202
203
				$isCopyGABToContact = isset($action["message_action"], $action["message_action"]["action_type"]) &&
204
					$action["message_action"]["action_type"] === "copyToContact";
205
206
				if ($isCopyGABToContact) {
207
					$this->copyGABRecordProps($action);
208
				}
209
				// generate one-off entryids for email addresses
210
				for ($index = 1; $index < 4; ++$index) {
211
					if (!empty($action['props']['email_address_' . $index]) && !empty($action['props']['email_address_display_name_' . $index])) {
212
						$action['props']['email_address_entryid_' . $index] = bin2hex(mapi_createoneoff($action['props']['email_address_display_name_' . $index], $action['props']['email_address_type_' . $index], $action['props']['email_address_' . $index]));
213
					}
214
				}
215
216
				// set properties for primary fax number
217
				if (isset($action['props']['fax_1_email_address']) && !empty($action['props']['fax_1_email_address'])) {
218
					$action['props']['fax_1_original_entryid'] = bin2hex(mapi_createoneoff($action['props']['fax_1_original_display_name'], $action['props']['fax_1_address_type'], $action['props']['fax_1_email_address'], MAPI_UNICODE));
219
				}
220
				else {
221
					// delete properties to remove previous values
222
					$properiesToDelete[] = $this->properties['fax_1_address_type'];
223
					$properiesToDelete[] = $this->properties['fax_1_original_display_name'];
224
					$properiesToDelete[] = $this->properties['fax_1_email_address'];
225
					$properiesToDelete[] = $this->properties['fax_1_original_entryid'];
226
				}
227
228
				// set properties for business fax number
229
				if (isset($action['props']['fax_2_email_address']) && !empty($action['props']['fax_2_email_address'])) {
230
					$action['props']['fax_2_original_entryid'] = bin2hex(mapi_createoneoff($action['props']['fax_2_original_display_name'], $action['props']['fax_2_address_type'], $action['props']['fax_2_email_address'], MAPI_UNICODE));
231
				}
232
				else {
233
					$properiesToDelete[] = $this->properties['fax_2_address_type'];
234
					$properiesToDelete[] = $this->properties['fax_2_original_display_name'];
235
					$properiesToDelete[] = $this->properties['fax_2_email_address'];
236
					$properiesToDelete[] = $this->properties['fax_2_original_entryid'];
237
				}
238
239
				// set properties for home fax number
240
				if (isset($action['props']['fax_3_email_address']) && !empty($action['props']['fax_3_email_address'])) {
241
					$action['props']['fax_3_original_entryid'] = bin2hex(mapi_createoneoff($action['props']['fax_3_original_display_name'], $action['props']['fax_3_address_type'], $action['props']['fax_3_email_address'], MAPI_UNICODE));
242
				}
243
				else {
244
					$properiesToDelete[] = $this->properties['fax_3_address_type'];
245
					$properiesToDelete[] = $this->properties['fax_3_original_display_name'];
246
					$properiesToDelete[] = $this->properties['fax_3_email_address'];
247
					$properiesToDelete[] = $this->properties['fax_3_original_entryid'];
248
				}
249
250
				// check for properties which should be deleted
251
				if (isset($action['entryid']) && !empty($action['entryid'])) {
252
					// check for empty email address properties
253
					for ($i = 1; $i < 4; ++$i) {
254
						if (isset($action['props']['email_address_' . $i]) && empty($action['props']['email_address_' . $i])) {
255
							array_push($properiesToDelete, $this->properties['email_address_entryid_' . $i]);
256
							array_push($properiesToDelete, $this->properties['email_address_' . $i]);
257
							array_push($properiesToDelete, $this->properties['email_address_display_name_' . $i]);
258
							array_push($properiesToDelete, $this->properties['email_address_display_name_email_' . $i]);
259
							array_push($properiesToDelete, $this->properties['email_address_type_' . $i]);
260
						}
261
					}
262
263
					// check for empty address_book_mv and address_book_long properties
264
					if (isset($action['props']['address_book_long']) && $action['props']['address_book_long'] === 0) {
265
						$properiesToDelete[] = $this->properties['address_book_mv'];
266
						$properiesToDelete[] = $this->properties['address_book_long'];
267
					}
268
269
					// Check if the birthday and anniversary properties are empty. If so delete them.
270
					if (array_key_exists('birthday', $action['props']) && empty($action['props']['birthday'])) {
271
						array_push($properiesToDelete, $this->properties['birthday']);
272
						array_push($properiesToDelete, $this->properties['birthday_eventid']);
273
						if (!empty($action['props']['birthday_eventid'])) {
274
							$this->deleteSpecialDateAppointment($store, $action['props']['birthday_eventid']);
275
						}
276
					}
277
278
					if (array_key_exists('wedding_anniversary', $action['props']) && empty($action['props']['wedding_anniversary'])) {
279
						array_push($properiesToDelete, $this->properties['wedding_anniversary']);
280
						array_push($properiesToDelete, $this->properties['anniversary_eventid']);
281
						if (!empty($action['props']['anniversary_eventid'])) {
282
							$this->deleteSpecialDateAppointment($store, $action['props']['anniversary_eventid']);
283
						}
284
					}
285
				}
286
287
				/*
288
				 * convert all line endings(LF) into CRLF
289
				 * XML parser will normalize all CR, LF and CRLF into LF
290
				 * but outlook(windows) uses CRLF as line ending
291
				 */
292
				if (isset($action['props']['business_address'])) {
293
					$action['props']['business_address'] = str_replace('\n', '\r\n', $action['props']['business_address']);
294
				}
295
296
				if (isset($action['props']['home_address'])) {
297
					$action['props']['home_address'] = str_replace('\n', '\r\n', $action['props']['home_address']);
298
				}
299
300
				if (isset($action['props']['other_address'])) {
301
					$action['props']['other_address'] = str_replace('\n', '\r\n', $action['props']['other_address']);
302
				}
303
304
				// check birthday props to make an appointment
305
				if (!empty($action['props']['birthday'])) {
306
					$action['props']['birthday_eventid'] = $this->updateAppointments($store, $action, 'birthday');
307
				}
308
309
				// check anniversary props to make an appointment
310
				if (!empty($action['props']['wedding_anniversary'])) {
311
					$action['props']['anniversary_eventid'] = $this->updateAppointments($store, $action, 'wedding_anniversary');
312
				}
313
314
				// do the conversion when all processing has been finished
315
				$props = Conversion::mapXML2MAPI($this->properties, $action['props']);
316
			}
317
318
			$messageProps = [];
319
320
			$result = $GLOBALS['operations']->saveMessage($store, $entryid, $parententryid, $props, $messageProps, [], $action['attachments'] ?? [], $properiesToDelete);
321
322
			if ($result) {
323
				$GLOBALS['bus']->notify(bin2hex($parententryid), TABLE_SAVE, $messageProps);
324
325
				if ($isCopyGABToContact) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $isCopyGABToContact does not seem to be defined for all execution paths leading up to this point.
Loading history...
326
					$message = mapi_msgstore_openentry($store, $messageProps[PR_ENTRYID]);
327
					$messageProps = mapi_getprops($message, $this->properties);
328
				}
329
330
				$this->addActionData('update', ['item' => Conversion::mapMAPI2XML($this->properties, $messageProps)]);
331
				$GLOBALS['bus']->addData($this->getResponseData());
332
			}
333
		}
334
	}
335
336
	/**
337
	 * Function copy the some property from address book record to contact props.
338
	 *
339
	 * @param array $action the action data, sent by the client
340
	 */
341
	public function copyGABRecordProps(&$action) {
342
		$addrbook = $GLOBALS["mapisession"]->getAddressbook();
343
		$abitem = mapi_ab_openentry($addrbook, hex2bin((string) $action["message_action"]["source_entryid"]));
344
		$abItemProps = mapi_getprops($abitem, [
345
			PR_COMPANY_NAME,
346
			PR_ASSISTANT,
347
			PR_BUSINESS_TELEPHONE_NUMBER,
348
			PR_BUSINESS2_TELEPHONE_NUMBER,
349
			PR_HOME2_TELEPHONE_NUMBER,
350
			PR_STREET_ADDRESS,
351
			PR_LOCALITY,
352
			PR_STATE_OR_PROVINCE,
353
			PR_POSTAL_CODE,
354
			PR_COUNTRY,
355
			PR_MOBILE_TELEPHONE_NUMBER,
356
		]);
357
		$action["props"]["company_name"] = $abItemProps[PR_COMPANY_NAME] ?? '';
358
		$action["props"]["assistant"] = $abItemProps[PR_ASSISTANT] ?? '';
359
		$action["props"]["business_telephone_number"] = $abItemProps[PR_BUSINESS_TELEPHONE_NUMBER] ?? '';
360
		$action["props"]["business2_telephone_number"] = $abItemProps[PR_BUSINESS2_TELEPHONE_NUMBER] ?? '';
361
		$action["props"]["home2_telephone_number"] = $abItemProps[PR_HOME2_TELEPHONE_NUMBER] ?? '';
362
		$action["props"]["home_address_street"] = $abItemProps[PR_STREET_ADDRESS] ?? '';
363
		$action["props"]["home_address_city"] = $abItemProps[PR_LOCALITY] ?? '';
364
		$action["props"]["home_address_state"] = $abItemProps[PR_STATE_OR_PROVINCE] ?? '';
365
		$action["props"]["home_address_postal_code"] = $abItemProps[PR_POSTAL_CODE] ?? '';
366
		$action["props"]["home_address_country"] = $abItemProps[PR_COUNTRY] ?? '';
367
368
		$action["props"]["cellular_telephone_number"] = $abItemProps[PR_MOBILE_TELEPHONE_NUMBER] ?? '';
369
370
		// Set the home_address property value
371
		$props = ["street", "city", "state", "postal_code", "country"];
372
		$homeAddress = "";
373
		foreach ($props as $index => $prop) {
374
			if (isset($action["props"]["home_address_" . $prop]) && !empty($action["props"]["home_address_" . $prop])) {
375
				$homeAddress .= $action["props"]["home_address_" . $prop] . " ";
376
				if ($prop == "street" || $prop == "postal_code") {
377
					$homeAddress .= PHP_EOL;
378
				}
379
			}
380
		}
381
382
		$action["props"]["home_address"] = $homeAddress;
383
	}
384
385
	/**
386
	 * Function which deletes an item. Extended here to also delete corresponding birthday/anniversary
387
	 * appointments from calendar.
388
	 *
389
	 * @param object $store         MAPI Message Store Object
390
	 * @param string $parententryid parent entryid of the message
391
	 * @param string $entryid       entryid of the message
392
	 * @param array  $action        the action data, sent by the client
393
	 */
394
	#[Override]
395
	public function delete($store, $parententryid, $entryid, $action) {
396
		$message = false;
397
		if (!$store && !$parententryid && $entryid) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
398
			$data = $this->getStoreParentEntryIdFromEntryId($entryid);
399
			$store = $data["store"];
400
			$message = $data["message"];
401
			$parententryid = $data["parent_entryid"];
402
		}
403
404
		if ($store && $entryid) {
405
			try {
406
				if ($message === false) {
0 ignored issues
show
introduced by
The condition $message === false is always true.
Loading history...
407
					$message = $GLOBALS["operations"]->openMessage($store, $entryid);
408
				}
409
410
				$props = mapi_getprops($message, [$this->properties['anniversary_eventid'], $this->properties['birthday_eventid']]);
411
412
				// if any of the appointment entryid exists then delete it
413
				if (!empty($props[$this->properties['birthday_eventid']])) {
414
					$this->deleteSpecialDateAppointment($store, bin2hex((string) $props[$this->properties['birthday_eventid']]));
415
				}
416
417
				if (!empty($props[$this->properties['anniversary_eventid']])) {
418
					$this->deleteSpecialDateAppointment($store, bin2hex((string) $props[$this->properties['anniversary_eventid']]));
419
				}
420
			}
421
			catch (MAPIException $e) {
422
				// if any error occurs in deleting appointments then we shouldn't block deletion of contact item
423
				// so ignore errors now
424
				$e->setHandled();
425
			}
426
427
			parent::delete($store, $parententryid, $entryid, $action);
428
		}
429
	}
430
431
	/**
432
	 * Function which retrieve the store, parent_entryid from record entryid.
433
	 *
434
	 * @param $entryid entryid of the message
435
	 *
436
	 * @return array which contains store and message object and parent entryid of that message
437
	 */
438
	public function getStoreParentEntryIdFromEntryId($entryid) {
439
		$message = $GLOBALS['mapisession']->openMessage($entryid);
440
		$messageStoreInfo = mapi_getprops($message, [PR_STORE_ENTRYID, PR_PARENT_ENTRYID]);
441
		$store = $GLOBALS['mapisession']->openMessageStore($messageStoreInfo[PR_STORE_ENTRYID]);
442
		$parentEntryid = $messageStoreInfo[PR_PARENT_ENTRYID];
443
444
		return ["message" => $message, "store" => $store, "parent_entryid" => $parentEntryid];
445
	}
446
447
	/**
448
	 * Function will create/update a yearly recurring appointment on the respective date of birthday or anniversary in user's calendar.
449
	 *
450
	 * @param object $store  MAPI Message Store Object
451
	 * @param array  $action the action data, sent by the client
452
	 * @param string $type   type of appointment that should be created/updated, valid values are 'birthday' and 'wedding_anniversary'
453
	 *
454
	 * @return HexString entryid of the newly created appointment in hex format
0 ignored issues
show
Bug introduced by
The type HexString was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
455
	 */
456
	public function updateAppointments($store, $action, $type) {
457
		$result = false;
458
459
		$root = mapi_msgstore_openentry($store);
460
		$rootProps = mapi_getprops($root, [PR_IPM_APPOINTMENT_ENTRYID, PR_STORE_ENTRYID]);
461
		$parentEntryId = bin2hex((string) $rootProps[PR_IPM_APPOINTMENT_ENTRYID]);
462
		$storeEntryId = bin2hex((string) $rootProps[PR_STORE_ENTRYID]);
463
464
		$actionProps = $action['props'];
465
		$subject = !empty($actionProps['subject']) ? $actionProps['subject'] : _('Untitled');
466
		$subject = ($type === 'birthday' ? sprintf(_('%s\'s Birthday'), $subject) : sprintf(_('%s\'s Anniversary'), $subject));
467
468
		// UTC time
469
		$startDateUTC = $actionProps[$type];
470
		$dueDateUTC = $actionProps[$type] + (24 * 60 * 60); // ONE DAY is added to set duedate of item.
471
472
		// get local time from UTC time
473
		$recur = new Recurrence($store, []);
0 ignored issues
show
Bug introduced by
The type Recurrence was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
474
		$startDate = $recur->fromGMT($actionProps, $startDateUTC);
475
		$dueDate = $recur->fromGMT($actionProps, $dueDateUTC);
476
477
		// Find the number of minutes since the start of the year to the given month,
478
		// taking leap years into account.
479
		$month = date('m', $startDate);
480
		$year = date('y', $startDate);
481
482
		$d1 = new DateTime();
483
		$d1->setDate($year, 1, 1);
0 ignored issues
show
Bug introduced by
$year of type string is incompatible with the type integer expected by parameter $year of DateTime::setDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

483
		$d1->setDate(/** @scrutinizer ignore-type */ $year, 1, 1);
Loading history...
484
		$d2 = new DateTime();
485
		$d2->setDate($year, $month, 1);
0 ignored issues
show
Bug introduced by
$month of type string is incompatible with the type integer expected by parameter $month of DateTime::setDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

485
		$d2->setDate($year, /** @scrutinizer ignore-type */ $month, 1);
Loading history...
486
487
		$diff = $d2->diff($d1);
488
		$month = $diff->days * 24 * 60;
489
490
		$props = [
491
			'message_class' => 'IPM.Appointment',
492
			'icon_index' => 1025,
493
			'busystatus' => fbFree,
494
			'meeting' => olNonMeeting,
495
			'object_type' => MAPI_MESSAGE,
496
			'message_flags' => MSGFLAG_READ | MSGFLAG_UNSENT,
497
			'subject' => $subject,
498
499
			'startdate' => $startDateUTC,
500
			'duedate' => $dueDateUTC,
501
			'commonstart' => $startDateUTC,
502
			'commonend' => $dueDateUTC,
503
			'alldayevent' => true,
504
			'duration' => 1440,
505
			'reminder' => true,
506
			'reminder_minutes' => 1080,
507
			'reminder_time' => $startDateUTC,
508
			'flagdueby' => $startDateUTC - (1080 * 60),
509
510
			'recurring' => true,
511
			'recurring_reset' => true,
512
			'startocc' => 0,
513
			'endocc' => 1440,
514
			'start' => $startDate,
515
			'end' => $dueDate,
516
			'term' => 35,
517
			'everyn' => 1,
518
			'subtype' => 2,
519
			'type' => 13,
520
			'regen' => 0,
521
			'month' => $month,
522
			'monthday' => date('j', $startDate),
523
			'timezone' => $actionProps['timezone'],
524
			'timezonedst' => $actionProps['timezonedst'],
525
			'dststartmonth' => $actionProps['dststartmonth'],
526
			'dststartweek' => $actionProps['dststartweek'],
527
			'dststartday' => $actionProps['dststartday'],
528
			'dststarthour' => $actionProps['dststarthour'],
529
			'dstendmonth' => $actionProps['dstendmonth'],
530
			'dstendweek' => $actionProps['dstendweek'],
531
			'dstendday' => $actionProps['dstendday'],
532
			'dstendhour' => $actionProps['dstendhour'],
533
		];
534
535
		$data = [];
536
		$data['store'] = $storeEntryId;
537
		$data['parententryid'] = $parentEntryId;
538
539
		$entryid = false;
540
		// if entryid is provided then update existing appointment, else create new one
541
		if ($type === 'birthday' && !empty($actionProps['birthday_eventid'])) {
542
			$entryid = $actionProps['birthday_eventid'];
543
		}
544
		elseif ($type === 'wedding_anniversary' && !empty($actionProps['anniversary_eventid'])) {
545
			$entryid = $actionProps['anniversary_eventid'];
546
		}
547
548
		if ($entryid !== false) {
549
			$data['entryid'] = $entryid;
550
		}
551
552
		if (isset($action['timezone_iana'])) {
553
			$props['timezone_iana'] = $action['timezone_iana'];
554
		}
555
556
		$data['props'] = $props;
557
558
		// Save appointment (saveAppointment takes care of creating/modifying exceptions to recurring
559
		// items if necessary)
560
		try {
561
			$messageProps = $GLOBALS['operations']->saveAppointment($store, hex2bin((string) $entryid), hex2bin($parentEntryId), $data);
562
		}
563
		catch (MAPIException $e) {
564
			// if the appointment is deleted then create a new one
565
			if ($e->getCode() == MAPI_E_NOT_FOUND) {
566
				$e->setHandled();
567
				$messageProps = $GLOBALS['operations']->saveAppointment($store, false, hex2bin($parentEntryId), $data);
568
			}
569
		}
570
571
		// Notify the bus if the save was OK
572
		if ($messageProps && !(is_array($messageProps) && isset($messageProps['error']))) {
573
			$GLOBALS['bus']->notify($parentEntryId, TABLE_SAVE, $messageProps);
574
			$result = bin2hex((string) $messageProps[PR_ENTRYID]);
575
		}
576
577
		return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type false|string which is incompatible with the documented return type HexString.
Loading history...
578
	}
579
580
	/**
581
	 * Function will delete the appointment on the respective date of birthday or anniversary in user's calendar.
582
	 *
583
	 * @param object $store   MAPI Message Store Object
584
	 * @param        $entryid of the message with will be deleted,sent by the client
0 ignored issues
show
Bug introduced by
The type of was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
585
	 */
586
	public function deleteSpecialDateAppointment($store, $entryid) {
587
		$root = mapi_msgstore_openentry($store);
588
		$rootProps = mapi_getprops($root, [PR_IPM_APPOINTMENT_ENTRYID, PR_STORE_ENTRYID]);
589
		$parentEntryId = $rootProps[PR_IPM_APPOINTMENT_ENTRYID];
590
		$storeEntryId = $rootProps[PR_STORE_ENTRYID];
591
592
		$props = [];
593
		$props[PR_PARENT_ENTRYID] = $parentEntryId;
594
		$props[PR_ENTRYID] = hex2bin((string) $entryid);
595
		$props[PR_STORE_ENTRYID] = $storeEntryId;
596
597
		$result = $GLOBALS['operations']->deleteMessages($store, $parentEntryId, $props[PR_ENTRYID]);
598
599
		if ($result) {
600
			$GLOBALS['bus']->notify(bin2hex((string) $parentEntryId), TABLE_DELETE, $props);
601
		}
602
	}
603
604
	/**
605
	 * This function searches the private contact folders for users and returns an array with data.
606
	 * Please note that the returning array must be UTF8.
607
	 *
608
	 * @param resource $ab        The addressbook
609
	 * @param resource $ab_dir    The addressbook container
610
	 * @param string   $searchstr The search query, case is ignored
611
	 */
612
	public function searchContactsFolders($ab, $ab_dir, $searchstr) {
613
		$r = [];
614
		$abhtable = mapi_folder_gethierarchytable($ab_dir, MAPI_DEFERRED_ERRORS | CONVENIENT_DEPTH);
615
		$abcntfolders = mapi_table_queryallrows($abhtable, [PR_ENTRYID, PR_AB_PROVIDER_ID, PR_DISPLAY_NAME]);
616
		$restriction = [
617
			RES_CONTENT,
618
			[
619
				FUZZYLEVEL => FL_SUBSTRING | FL_IGNORECASE,
620
				ULPROPTAG => PR_SMTP_ADDRESS,
621
				VALUE => [PR_SMTP_ADDRESS => $searchstr],
622
			],
623
		];
624
		// restriction on hierarchy table for PR_AB_PROVIDER_ID
625
		// seems not to work, just loop through
626
		foreach ($abcntfolders as $abcntfolder) {
627
			if ($abcntfolder[PR_AB_PROVIDER_ID] == ZARAFA_CONTACTS_GUID) {
628
				$abfldentry = mapi_ab_openentry($ab, $abcntfolder[PR_ENTRYID]);
629
				$abfldcontents = mapi_folder_getcontentstable($abfldentry);
630
				mapi_table_restrict($abfldcontents, $restriction);
631
				$r = mapi_table_queryallrows($abfldcontents, [PR_ENTRYID]);
632
				if (is_array($r) && !empty($r)) {
633
					return $r;
634
				}
635
			}
636
		}
637
638
		return $r;
639
	}
640
}
641