Issues (207)

class.meetingrequest.php (15 issues)

1
<?php
2
/*
3
 * SPDX-License-Identifier: AGPL-3.0-only
4
 * SPDX-FileCopyrightText: Copyright 2005-2016 Zarafa Deutschland GmbH
5
 * SPDX-FileCopyrightText: Copyright 2020-2024 grommunio GmbH
6
 */
7
8
class Meetingrequest {
9
	/*
10
	 * NOTE
11
	 *
12
	 * This class is designed to modify and update meeting request properties
13
	 * and to search for linked appointments in the calendar. It does not
14
	 * - set standard properties like subject or location
15
	 * - commit property changes through savechanges() (except in accept() and decline())
16
	 *
17
	 * To set all the other properties, just handle the item as any other appointment
18
	 * item. You aren't even required to set those properties before or after using
19
	 * this class. If you update properties before REsending a meeting request (ie with
20
	 * a time change) you MUST first call updateMeetingRequest() so the internal counters
21
	 * can be updated. You can then submit the message any way you like.
22
	 *
23
	 */
24
25
	/*
26
	 * How to use
27
	 * ----------
28
	 *
29
	 * Sending a meeting request:
30
	 * - Create appointment item as normal, but as 'tentative'
31
	 *   (this is the state of the item when the receiving user has received but
32
	 *    not accepted the item)
33
	 * - Set recipients as normally in e-mails
34
	 * - Create Meetingrequest class instance
35
	 * - Call checkCalendarWriteAccess(), to check for write permissions on calendar folder
36
	 * - Call setMeetingRequest(), this turns on all the meeting request properties in the
37
	 *   calendar item
38
	 * - Call sendMeetingRequest(), this sends a copy of the item with some extra properties
39
	 *
40
	 * Updating a meeting request:
41
	 * - Create Meetingrequest class instance
42
	 * - Call checkCalendarWriteAccess(), to check for write permissions on calendar folder
43
	 * - Call updateMeetingRequest(), this updates the counters
44
	 * - Call checkSignificantChanges(), this will check for significant changes and if needed will clear the
45
	 *   existing recipient responses
46
	 * - Call sendMeetingRequest()
47
	 *
48
	 * Clicking on a an e-mail:
49
	 * - Create Meetingrequest class instance
50
	 * - Check isMeetingRequest(), if true:
51
	 *   - Check isLocalOrganiser(), if true then ignore the message
52
	 *   - Check isInCalendar(), if not call doAccept(true, false, false). This adds the item in your
53
	 *     calendar as tentative without sending a response
54
	 *   - Show Accept, Tentative, Decline buttons
55
	 *   - When the user presses Accept, Tentative or Decline, call doAccept(false, true, true),
56
	 *     doAccept(true, true, true) or doDecline(true) respectively to really accept or decline and
57
	 *     send the response. This will remove the request from your inbox.
58
	 * - Check isMeetingRequestResponse, if true:
59
	 *   - Check isLocalOrganiser(), if not true then ignore the message
60
	 *   - Call processMeetingRequestResponse()
61
	 *     This will update the trackstatus of all recipients, and set the item to 'busy'
62
	 *     when all the recipients have accepted.
63
	 * - Check isMeetingCancellation(), if true:
64
	 *   - Check isLocalOrganiser(), if true then ignore the message
65
	 *   - Check isInCalendar(), if not, then ignore
66
	 *     Call processMeetingCancellation()
67
	 *   - Show 'Remove From Calendar' button to user
68
	 *   - When userpresses button, call doRemoveFromCalendar(), which removes the item from your
69
	 *     calendar and deletes the message
70
	 *
71
	 * Cancelling a meeting request:
72
	 *   - Call doCancelInvitation, which will send cancellation mails to attendees and will remove
73
	 *     meeting object from calendar
74
	 */
75
76
	// All properties for a recipient that are interesting
77
	public $recipprops = [
78
		PR_ENTRYID,
79
		PR_DISPLAY_NAME,
80
		PR_EMAIL_ADDRESS,
81
		PR_RECIPIENT_ENTRYID,
82
		PR_RECIPIENT_TYPE,
83
		PR_SEND_INTERNET_ENCODING,
84
		PR_SEND_RICH_INFO,
85
		PR_RECIPIENT_DISPLAY_NAME,
86
		PR_ADDRTYPE,
87
		PR_DISPLAY_TYPE,
88
		PR_DISPLAY_TYPE_EX,
89
		PR_RECIPIENT_TRACKSTATUS,
90
		PR_RECIPIENT_TRACKSTATUS_TIME,
91
		PR_RECIPIENT_FLAGS,
92
		PR_ROWID,
93
		PR_OBJECT_TYPE,
94
		PR_SEARCH_KEY,
95
		PR_SMTP_ADDRESS,
96
	];
97
98
	/**
99
	 * Indication whether the setting of resources in a Meeting Request is success (false) or if it
100
	 * has failed (integer).
101
	 *
102
	 * @var null|false|int
103
	 *
104
	 * @psalm-var 1|3|4|false|null
105
	 */
106
	public $errorSetResource;
107
108
	public $proptags;
109
	private $store;
110
	public $message;
111
	private $session;
112
113
	/**
114
	 * @var false|string
115
	 */
116
	private $meetingTimeInfo;
117
	private $enableDirectBooking;
118
119
	/**
120
	 * @var null|bool
121
	 */
122
	private $includesResources;
123
	private $nonAcceptingResources;
124
	private $recipientDisplayname;
125
126
	/**
127
	 * Constructor.
128
	 *
129
	 * Takes a store and a message. The message is an appointment item
130
	 * that should be converted into a meeting request or an incoming
131
	 * e-mail message that is a meeting request.
132
	 *
133
	 * The $session variable is optional, but required if the following features
134
	 * are to be used:
135
	 *
136
	 * - Sending meeting requests for meetings that are not in your own store
137
	 * - Sending meeting requests to resources, resource availability checking and resource freebusy updates
138
	 *
139
	 * @param mixed $store
140
	 * @param mixed $message
141
	 * @param mixed $session
142
	 * @param mixed $enableDirectBooking
143
	 */
144
	public function __construct($store, $message, $session = false, $enableDirectBooking = true) {
145
		$this->store = $store;
146
		$this->message = $message;
147
		$this->session = $session;
148
		// This variable string saves time information for the MR.
149
		$this->meetingTimeInfo = false;
150
		$this->enableDirectBooking = $enableDirectBooking;
151
152
		$properties = [];
153
		$properties['goid'] = 'PT_BINARY:PSETID_Meeting:0x3';
154
		$properties['goid2'] = 'PT_BINARY:PSETID_Meeting:0x23';
155
		$properties['type'] = 'PT_STRING8:PSETID_Meeting:0x24';
156
		$properties['meetingrecurring'] = 'PT_BOOLEAN:PSETID_Meeting:0x5';
157
		$properties['unknown2'] = 'PT_BOOLEAN:PSETID_Meeting:0xa';
158
		$properties['attendee_critical_change'] = 'PT_SYSTIME:PSETID_Meeting:0x1';
159
		$properties['owner_critical_change'] = 'PT_SYSTIME:PSETID_Meeting:0x1a';
160
		$properties['meetingstatus'] = 'PT_LONG:PSETID_Appointment:' . PidLidAppointmentStateFlags;
161
		$properties['responsestatus'] = 'PT_LONG:PSETID_Appointment:0x8218';
162
		$properties['unknown6'] = 'PT_LONG:PSETID_Meeting:0x4';
163
		$properties['replytime'] = 'PT_SYSTIME:PSETID_Appointment:0x8220';
164
		$properties['usetnef'] = 'PT_BOOLEAN:PSETID_Common:0x8582';
165
		$properties['recurrence_data'] = 'PT_BINARY:PSETID_Appointment:' . PidLidAppointmentRecur;
166
		$properties['reminderminutes'] = 'PT_LONG:PSETID_Common:' . PidLidReminderDelta;
167
		$properties['reminderset'] = 'PT_BOOLEAN:PSETID_Common:' . PidLidReminderSet;
168
		$properties['sendasical'] = 'PT_BOOLEAN:PSETID_Appointment:0x8200';
169
		$properties['updatecounter'] = 'PT_LONG:PSETID_Appointment:' . PidLidAppointmentSequence;					// AppointmentSequenceNumber
170
		$properties['unknown7'] = 'PT_LONG:PSETID_Appointment:0x8202';
171
		$properties['last_updatecounter'] = 'PT_LONG:PSETID_Appointment:0x8203';			// AppointmentLastSequence
172
		$properties['busystatus'] = 'PT_LONG:PSETID_Appointment:' . PidLidBusyStatus;
173
		$properties['intendedbusystatus'] = 'PT_LONG:PSETID_Appointment:' . PidLidIntendedBusyStatus;
174
		$properties['start'] = 'PT_SYSTIME:PSETID_Appointment:' . PidLidAppointmentStartWhole;
175
		$properties['responselocation'] = 'PT_STRING8:PSETID_Meeting:0x2';
176
		$properties['location'] = 'PT_STRING8:PSETID_Appointment:' . PidLidLocation;
177
		$properties['requestsent'] = 'PT_BOOLEAN:PSETID_Appointment:0x8229';		// PidLidFInvited, MeetingRequestWasSent
178
		$properties['startdate'] = 'PT_SYSTIME:PSETID_Appointment:' . PidLidAppointmentStartWhole;
179
		$properties['duedate'] = 'PT_SYSTIME:PSETID_Appointment:' . PidLidAppointmentEndWhole;
180
		$properties['flagdueby'] = 'PT_SYSTIME:PSETID_Common:' . PidLidReminderSignalTime;
181
		$properties['commonstart'] = 'PT_SYSTIME:PSETID_Common:0x8516';
182
		$properties['commonend'] = 'PT_SYSTIME:PSETID_Common:0x8517';
183
		$properties['recurring'] = 'PT_BOOLEAN:PSETID_Appointment:' . PidLidRecurring;
184
		$properties['clipstart'] = 'PT_SYSTIME:PSETID_Appointment:' . PidLidClipStart;
185
		$properties['clipend'] = 'PT_SYSTIME:PSETID_Appointment:' . PidLidClipEnd;
186
		$properties['start_recur_date'] = 'PT_LONG:PSETID_Meeting:0xD';				// StartRecurTime
187
		$properties['start_recur_time'] = 'PT_LONG:PSETID_Meeting:0xE';				// StartRecurTime
188
		$properties['end_recur_date'] = 'PT_LONG:PSETID_Meeting:0xF';				// EndRecurDate
189
		$properties['end_recur_time'] = 'PT_LONG:PSETID_Meeting:0x10';				// EndRecurTime
190
		$properties['is_exception'] = 'PT_BOOLEAN:PSETID_Meeting:0xA';				// LID_IS_EXCEPTION
191
		$properties['apptreplyname'] = 'PT_STRING8:PSETID_Appointment:0x8230';
192
		// Propose new time properties
193
		$properties['proposed_start_whole'] = 'PT_SYSTIME:PSETID_Appointment:' . PidLidAppointmentProposedStartWhole;
194
		$properties['proposed_end_whole'] = 'PT_SYSTIME:PSETID_Appointment:' . PidLidAppointmentProposedEndWhole;
195
		$properties['proposed_duration'] = 'PT_LONG:PSETID_Appointment:0x8256';
196
		$properties['counter_proposal'] = 'PT_BOOLEAN:PSETID_Appointment:' . PidLidAppointmentCounterProposal;
197
		$properties['recurring_pattern'] = 'PT_STRING8:PSETID_Appointment:' . PidLidRecurrencePattern;
198
		$properties['basedate'] = 'PT_SYSTIME:PSETID_Appointment:' . PidLidExceptionReplaceTime;
199
		$properties['meetingtype'] = 'PT_LONG:PSETID_Meeting:0x26';
200
		$properties['timezone_data'] = 'PT_BINARY:PSETID_Appointment:' . PidLidTimeZoneStruct;
201
		$properties['timezone'] = 'PT_STRING8:PSETID_Appointment:' . PidLidTimeZoneDescription;
202
		$properties['categories'] = 'PT_MV_STRING8:PS_PUBLIC_STRINGS:Keywords';
203
		$properties['private'] = 'PT_BOOLEAN:PSETID_Common:' . PidLidPrivate;
204
		$properties['alldayevent'] = 'PT_BOOLEAN:PSETID_Appointment:' . PidLidAppointmentSubType;
205
		$properties['toattendeesstring'] = 'PT_STRING8:PSETID_Appointment:0x823B';
206
		$properties['ccattendeesstring'] = 'PT_STRING8:PSETID_Appointment:0x823C';
207
208
		$this->proptags = getPropIdsFromStrings($store, $properties);
209
	}
210
211
	/**
212
	 * Sets the direct booking property. This is an alternative to the setting of the direct booking
213
	 * property through the constructor. However, setting it in the constructor is preferred.
214
	 *
215
	 * @param bool $directBookingSetting
216
	 */
217
	public function setDirectBooking($directBookingSetting): void {
218
		$this->enableDirectBooking = $directBookingSetting;
219
	}
220
221
	/**
222
	 * Returns TRUE if the message pointed to is an incoming meeting request and should
223
	 * therefore be replied to with doAccept or doDecline().
224
	 *
225
	 * @param string $messageClass message class to use for checking
226
	 *
227
	 * @return bool returns true if this is a meeting request else false
228
	 */
229
	public function isMeetingRequest($messageClass = false) {
230
		if ($messageClass === false) {
231
			$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS]);
232
			$messageClass = isset($props[PR_MESSAGE_CLASS]) ? $props[PR_MESSAGE_CLASS] : false;
233
		}
234
235
		if ($messageClass !== false && stripos($messageClass, 'ipm.schedule.meeting.request') === 0) {
236
			return true;
237
		}
238
239
		return false;
240
	}
241
242
	/**
243
	 * Returns TRUE if the message pointed to is a returning meeting request response.
244
	 *
245
	 * @param string $messageClass message class to use for checking
246
	 *
247
	 * @return bool returns true if this is a meeting request else false
248
	 */
249
	public function isMeetingRequestResponse($messageClass = false) {
250
		if ($messageClass === false) {
251
			$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS]);
252
			$messageClass = isset($props[PR_MESSAGE_CLASS]) ? $props[PR_MESSAGE_CLASS] : false;
253
		}
254
255
		if ($messageClass !== false && stripos($messageClass, 'ipm.schedule.meeting.resp') === 0) {
256
			return true;
257
		}
258
259
		return false;
260
	}
261
262
	/**
263
	 * Returns TRUE if the message pointed to is a cancellation request.
264
	 *
265
	 * @param string $messageClass message class to use for checking
266
	 *
267
	 * @return bool returns true if this is a meeting request else false
268
	 */
269
	public function isMeetingCancellation($messageClass = false) {
270
		if ($messageClass === false) {
271
			$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS]);
272
			$messageClass = isset($props[PR_MESSAGE_CLASS]) ? $props[PR_MESSAGE_CLASS] : false;
273
		}
274
275
		if ($messageClass !== false && stripos($messageClass, 'ipm.schedule.meeting.canceled') === 0) {
276
			return true;
277
		}
278
279
		return false;
280
	}
281
282
	/**
283
	 * Function is used to get the last update counter of meeting request.
284
	 *
285
	 * @return bool|int false when last_updatecounter not found else return last_updatecounter
286
	 */
287
	public function getLastUpdateCounter() {
288
		$calendarItemProps = mapi_getprops($this->message, [$this->proptags['last_updatecounter']]);
289
		if (isset($calendarItemProps) && !empty($calendarItemProps)) {
290
			return $calendarItemProps[$this->proptags['last_updatecounter']];
291
		}
292
293
		return false;
294
	}
295
296
	/**
297
	 * Process an incoming meeting request response. This updates the appointment
298
	 * in your calendar to show whether the user has accepted or declined.
299
	 */
300
	public function processMeetingRequestResponse() {
301
		if (!$this->isMeetingRequestResponse()) {
302
			return;
303
		}
304
305
		if (!$this->isLocalOrganiser()) {
306
			return;
307
		}
308
309
		// Get information we need from the response message
310
		$messageprops = mapi_getprops($this->message, [
311
			$this->proptags['goid'],
312
			$this->proptags['goid2'],
313
			PR_OWNER_APPT_ID,
314
			PR_SENT_REPRESENTING_EMAIL_ADDRESS,
315
			PR_SENT_REPRESENTING_NAME,
316
			PR_SENT_REPRESENTING_ADDRTYPE,
317
			PR_SENT_REPRESENTING_ENTRYID,
318
			PR_SENT_REPRESENTING_SEARCH_KEY,
319
			PR_MESSAGE_DELIVERY_TIME,
320
			PR_MESSAGE_CLASS,
321
			PR_PROCESSED,
322
			PR_RCVD_REPRESENTING_ENTRYID,
323
			$this->proptags['proposed_start_whole'],
324
			$this->proptags['proposed_end_whole'],
325
			$this->proptags['proposed_duration'],
326
			$this->proptags['counter_proposal'],
327
			$this->proptags['attendee_critical_change'],
328
		]);
329
330
		$goid2 = $messageprops[$this->proptags['goid2']];
331
332
		if (!isset($goid2) || !isset($messageprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS])) {
333
			return;
334
		}
335
336
		// Find basedate in GlobalID(0x3), this can be a response for an occurrence
337
		$basedate = $this->getBasedateFromGlobalID($messageprops[$this->proptags['goid']]);
338
339
		$userStore = $this->store;
340
		// check if delegate is processing the response
341
		if (isset($messageprops[PR_RCVD_REPRESENTING_ENTRYID])) {
342
			$delegatorStore = $this->getDelegatorStore($messageprops[PR_RCVD_REPRESENTING_ENTRYID], [PR_IPM_APPOINTMENT_ENTRYID]);
343
			if (!empty($delegatorStore['store'])) {
344
				$userStore = $delegatorStore['store'];
345
			}
346
		}
347
348
		// check for calendar access
349
		if ($this->checkCalendarWriteAccess($userStore) !== true) {
350
			// Throw an exception that we don't have write permissions on calendar folder,
351
			// allow caller to fill the error message
352
			throw new MAPIException(null, MAPI_E_NO_ACCESS);
353
		}
354
355
		$calendarItem = $this->getCorrespondentCalendarItem(true);
356
357
		// Open the calendar items, and update all the recipients of the calendar item that match
358
		// the email address of the response.
359
		if ($calendarItem !== false) {
360
			$this->processResponse($userStore, $calendarItem, $basedate, $messageprops);
361
		}
362
	}
363
364
	/**
365
	 * Process every incoming MeetingRequest response.This updates the appointment
366
	 * in your calendar to show whether the user has accepted or declined.
367
	 *
368
	 * @param resource $store        contains the userStore in which the meeting is created
369
	 * @param mixed    $calendarItem resource of the calendar item for which this response has arrived
370
	 * @param mixed    $basedate     if present the create an exception
371
	 * @param array    $messageprops contains message properties
372
	 *
373
	 * @return null|false
374
	 */
375
	public function processResponse($store, $calendarItem, $basedate, $messageprops) {
376
		$senderentryid = $messageprops[PR_SENT_REPRESENTING_ENTRYID];
377
		$messageclass = $messageprops[PR_MESSAGE_CLASS];
378
		$deliverytime = $messageprops[PR_MESSAGE_DELIVERY_TIME];
379
		$recurringItem = 0;
380
381
		// Open the calendar item, find the sender in the recipient table and update all the recipients of the calendar item that match
382
		// the email address of the response.
383
		$calendarItemProps = mapi_getprops($calendarItem, [$this->proptags['recurring'], PR_STORE_ENTRYID, PR_PARENT_ENTRYID, PR_ENTRYID, $this->proptags['updatecounter']]);
384
385
		// check if meeting response is already processed
386
		if (isset($messageprops[PR_PROCESSED]) && $messageprops[PR_PROCESSED] == true) {
387
			// meeting is already processed
388
			return;
389
		}
390
		mapi_setprops($this->message, [PR_PROCESSED => true]);
391
		mapi_savechanges($this->message);
392
393
		// if meeting is updated in organizer's calendar then we don't need to process
394
		// old response
395
		if ($this->isMeetingUpdated($basedate)) {
396
			return;
397
		}
398
399
		// If basedate is found, then create/modify exception msg and do processing
400
		if ($basedate && isset($calendarItemProps[$this->proptags['recurring']]) && $calendarItemProps[$this->proptags['recurring']] === true) {
401
			$recurr = new Recurrence($store, $calendarItem);
402
403
			// Copy properties from meeting request
404
			$exception_props = mapi_getprops($this->message, [
405
				PR_OWNER_APPT_ID,
406
				$this->proptags['proposed_start_whole'],
407
				$this->proptags['proposed_end_whole'],
408
				$this->proptags['proposed_duration'],
409
				$this->proptags['counter_proposal'],
410
			]);
411
412
			// Create/modify exception
413
			if ($recurr->isException($basedate)) {
414
				$recurr->modifyException($exception_props, $basedate);
415
			}
416
			else {
417
				// When we are creating an exception we need copy recipients from main recurring item
418
				$recipTable = mapi_message_getrecipienttable($calendarItem);
419
				$recips = mapi_table_queryallrows($recipTable, $this->recipprops);
420
421
				// Retrieve actual start/due dates from calendar item.
422
				$exception_props[$this->proptags['startdate']] = $recurr->getOccurrenceStart($basedate);
423
				$exception_props[$this->proptags['duedate']] = $recurr->getOccurrenceEnd($basedate);
424
425
				$recurr->createException($exception_props, $basedate, false, $recips);
426
			}
427
428
			mapi_savechanges($calendarItem);
429
430
			$attach = $recurr->getExceptionAttachment($basedate);
431
			if ($attach) {
432
				$recurringItem = $calendarItem;
433
				$calendarItem = mapi_attach_openobj($attach, MAPI_MODIFY);
434
			}
435
			else {
436
				return false;
437
			}
438
		}
439
440
		// Get the recipients of the calendar item
441
		$reciptable = mapi_message_getrecipienttable($calendarItem);
442
		$recipients = mapi_table_queryallrows($reciptable, $this->recipprops);
443
444
		// FIXME we should look at the updatecounter property and compare it
445
		// to the counter in the recipient to see if this update is actually
446
		// newer than the status in the calendar item
447
		$found = false;
448
449
		$totalrecips = 0;
450
		$acceptedrecips = 0;
451
		foreach ($recipients as $recipient) {
452
			++$totalrecips;
453
			// external recipients might not have entryid
454
			if (!isset($recipient[PR_ENTRYID]) &&
455
				$recipient[PR_EMAIL_ADDRESS] == $messageprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS]) {
456
				$recipient[PR_ENTRYID] = $senderentryid;
457
			}
458
			if (isset($recipient[PR_ENTRYID]) && $this->compareABEntryIDs($recipient[PR_ENTRYID], $senderentryid)) {
459
				$found = true;
460
461
				/*
462
				 * If value of attendee_critical_change on meeting response mail is less than PR_RECIPIENT_TRACKSTATUS_TIME
463
				 * on the corresponding recipientRow of meeting then we ignore this response mail.
464
				 */
465
				if (isset($recipient[PR_RECIPIENT_TRACKSTATUS_TIME]) && ($messageprops[$this->proptags['attendee_critical_change']] < $recipient[PR_RECIPIENT_TRACKSTATUS_TIME])) {
466
					continue;
467
				}
468
469
				// The email address matches, update the row
470
				$recipient[PR_RECIPIENT_TRACKSTATUS] = $this->getTrackStatus($messageclass);
471
				if (isset($messageprops[$this->proptags['attendee_critical_change']])) {
472
					$recipient[PR_RECIPIENT_TRACKSTATUS_TIME] = $messageprops[$this->proptags['attendee_critical_change']];
473
				}
474
475
				// If this is a counter proposal, set the proposal properties in the recipient row
476
				if (isset($messageprops[$this->proptags['counter_proposal']]) && $messageprops[$this->proptags['counter_proposal']]) {
477
					$recipient[PR_RECIPIENT_PROPOSEDSTARTTIME] = $messageprops[$this->proptags['proposed_start_whole']];
478
					$recipient[PR_RECIPIENT_PROPOSEDENDTIME] = $messageprops[$this->proptags['proposed_end_whole']];
479
					$recipient[PR_RECIPIENT_PROPOSED] = $messageprops[$this->proptags['counter_proposal']];
480
				}
481
482
				// Update the recipient information
483
				mapi_message_modifyrecipients($calendarItem, MODRECIP_REMOVE, [$recipient]);
484
				mapi_message_modifyrecipients($calendarItem, MODRECIP_ADD, [$recipient]);
485
			}
486
			if (isset($recipient[PR_RECIPIENT_TRACKSTATUS]) && $recipient[PR_RECIPIENT_TRACKSTATUS] == olRecipientTrackStatusAccepted) {
487
				++$acceptedrecips;
488
			}
489
		}
490
491
		// If the recipient was not found in the original calendar item,
492
		// then add the recpient as a new optional recipient
493
		if (!$found) {
494
			$recipient = [];
495
			$recipient[PR_ENTRYID] = $messageprops[PR_SENT_REPRESENTING_ENTRYID];
496
			$recipient[PR_EMAIL_ADDRESS] = $messageprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS];
497
			$recipient[PR_DISPLAY_NAME] = $messageprops[PR_SENT_REPRESENTING_NAME];
498
			$recipient[PR_ADDRTYPE] = $messageprops[PR_SENT_REPRESENTING_ADDRTYPE];
499
			$recipient[PR_RECIPIENT_TYPE] = MAPI_CC;
500
			$recipient[PR_SEARCH_KEY] = $messageprops[PR_SENT_REPRESENTING_SEARCH_KEY];
501
			$recipient[PR_RECIPIENT_TRACKSTATUS] = $this->getTrackStatus($messageclass);
502
			$recipient[PR_RECIPIENT_TRACKSTATUS_TIME] = $deliverytime;
503
504
			// If this is a counter proposal, set the proposal properties in the recipient row
505
			if (isset($messageprops[$this->proptags['counter_proposal']])) {
506
				$recipient[PR_RECIPIENT_PROPOSEDSTARTTIME] = $messageprops[$this->proptags['proposed_start_whole']];
507
				$recipient[PR_RECIPIENT_PROPOSEDENDTIME] = $messageprops[$this->proptags['proposed_end_whole']];
508
				$recipient[PR_RECIPIENT_PROPOSED] = $messageprops[$this->proptags['counter_proposal']];
509
			}
510
511
			mapi_message_modifyrecipients($calendarItem, MODRECIP_ADD, [$recipient]);
512
			++$totalrecips;
513
			if ($recipient[PR_RECIPIENT_TRACKSTATUS] == olRecipientTrackStatusAccepted) {
514
				++$acceptedrecips;
515
			}
516
		}
517
518
		// TODO: Update counter proposal number property on message
519
		/*
520
		If it is the first time this attendee has proposed a new date/time, increment the value of the PidLidAppointmentProposalNumber property on the organizer's meeting object, by 0x00000001. If this property did not previously exist on the organizer's meeting object, it MUST be set with a value of 0x00000001.
521
		*/
522
		// If this is a counter proposal, set the counter proposal indicator boolean
523
		if (isset($messageprops[$this->proptags['counter_proposal']])) {
524
			$props = [];
525
			if ($messageprops[$this->proptags['counter_proposal']]) {
526
				$props[$this->proptags['counter_proposal']] = true;
527
			}
528
			else {
529
				$props[$this->proptags['counter_proposal']] = false;
530
			}
531
532
			mapi_setprops($calendarItem, $props);
533
		}
534
535
		mapi_savechanges($calendarItem);
536
		if (isset($attach)) {
537
			mapi_savechanges($attach);
538
			mapi_savechanges($recurringItem);
539
		}
540
	}
541
542
	/**
543
	 * Process an incoming meeting request cancellation. This updates the
544
	 * appointment in your calendar to show that the meeting has been cancelled.
545
	 */
546
	public function processMeetingCancellation() {
547
		if (!$this->isMeetingCancellation()) {
548
			return;
549
		}
550
551
		if ($this->isLocalOrganiser()) {
552
			return;
553
		}
554
555
		if (!$this->isInCalendar()) {
556
			return;
557
		}
558
559
		$listProperties = $this->proptags;
560
		$listProperties['subject'] = PR_SUBJECT;
561
		$listProperties['sent_representing_name'] = PR_SENT_REPRESENTING_NAME;
562
		$listProperties['sent_representing_address_type'] = PR_SENT_REPRESENTING_ADDRTYPE;
563
		$listProperties['sent_representing_email_address'] = PR_SENT_REPRESENTING_EMAIL_ADDRESS;
564
		$listProperties['sent_representing_entryid'] = PR_SENT_REPRESENTING_ENTRYID;
565
		$listProperties['sent_representing_search_key'] = PR_SENT_REPRESENTING_SEARCH_KEY;
566
		$listProperties['rcvd_representing_name'] = PR_RCVD_REPRESENTING_NAME;
567
		$listProperties['rcvd_representing_address_type'] = PR_RCVD_REPRESENTING_ADDRTYPE;
568
		$listProperties['rcvd_representing_email_address'] = PR_RCVD_REPRESENTING_EMAIL_ADDRESS;
569
		$listProperties['rcvd_representing_entryid'] = PR_RCVD_REPRESENTING_ENTRYID;
570
		$listProperties['rcvd_representing_search_key'] = PR_RCVD_REPRESENTING_SEARCH_KEY;
571
		$messageProps = mapi_getprops($this->message, $listProperties);
572
573
		$goid = $messageProps[$this->proptags['goid']];	// GlobalID (0x3)
574
		if (!isset($goid)) {
575
			return;
576
		}
577
578
		$store = $this->store;
579
		// get delegator store, if delegate is processing this cancellation
580
		if (isset($messageProps[PR_RCVD_REPRESENTING_ENTRYID])) {
581
			$delegatorStore = $this->getDelegatorStore($messageProps[PR_RCVD_REPRESENTING_ENTRYID], [PR_IPM_APPOINTMENT_ENTRYID]);
582
			if (!empty($delegatorStore['store'])) {
583
				$store = $delegatorStore['store'];
584
			}
585
		}
586
587
		// check for calendar access
588
		if ($this->checkCalendarWriteAccess($store) !== true) {
589
			// Throw an exception that we don't have write permissions on calendar folder,
590
			// allow caller to fill the error message
591
			throw new MAPIException(null, MAPI_E_NO_ACCESS);
592
		}
593
594
		$calendarItem = $this->getCorrespondentCalendarItem(true);
595
		$basedate = $this->getBasedateFromGlobalID($goid);
596
597
		if ($calendarItem !== false) {
598
			// if basedate is provided and we could not find the item then it could be that we are processing
599
			// an exception so get the exception and process it
600
			if ($basedate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $basedate of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
601
				$calendarItemProps = mapi_getprops($calendarItem, [$this->proptags['recurring']]);
602
				if ($calendarItemProps[$this->proptags['recurring']] === true) {
603
					$recurr = new Recurrence($store, $calendarItem);
604
605
					// Set message class
606
					$messageProps[PR_MESSAGE_CLASS] = 'IPM.Appointment';
607
608
					if ($recurr->isException($basedate)) {
609
						$recurr->modifyException($messageProps, $basedate);
610
					}
611
					else {
612
						$recurr->createException($messageProps, $basedate);
613
					}
614
				}
615
			}
616
			else {
617
				// set the properties of the cancellation object
618
				mapi_setprops($calendarItem, $messageProps);
619
			}
620
621
			mapi_savechanges($calendarItem);
622
		}
623
	}
624
625
	/**
626
	 * Returns true if the corresponding calendar items exists in the celendar folder for this
627
	 * meeting request/response/cancellation.
628
	 */
629
	public function isInCalendar(): bool {
630
		// @TODO check for deleted exceptions
631
		return $this->getCorrespondentCalendarItem(false) !== false;
632
	}
633
634
	/**
635
	 * Accepts the meeting request by moving the item to the calendar
636
	 * and sending a confirmation message back to the sender. If $tentative
637
	 * is TRUE, then the item is accepted tentatively. After accepting, you
638
	 * can't use this class instance any more. The message is closed. If you
639
	 * specify TRUE for 'move', then the item is actually moved (from your
640
	 * inbox probably) to the calendar. If you don't, it is copied into
641
	 * your calendar.
642
	 *
643
	 * @param bool  $tentative            true if user as tentative accepted the meeting
644
	 * @param bool  $sendresponse         true if a response has to be sent to organizer
645
	 * @param bool  $move                 true if the meeting request should be moved to the deleted items after processing
646
	 * @param mixed $newProposedStartTime contains starttime if user has proposed other time
647
	 * @param mixed $newProposedEndTime   contains endtime if user has proposed other time
648
	 * @param mixed $body
649
	 * @param mixed $userAction
650
	 * @param mixed $store
651
	 * @param mixed $basedate             start of day of occurrence for which user has accepted the recurrent meeting
652
	 * @param bool  $isImported           true to indicate that MR is imported from .ics or .vcs file else it false.
653
	 *
654
	 * @return bool|string $entryid entryid of item which created/updated in calendar
655
	 */
656
	public function doAccept($tentative, $sendresponse, $move, $newProposedStartTime = false, $newProposedEndTime = false, $body = false, $userAction = false, $store = false, $basedate = false, $isImported = false) {
657
		if ($this->isLocalOrganiser()) {
658
			return false;
659
		}
660
661
		// Remove any previous calendar items with this goid and appt id
662
		$messageprops = mapi_getprops($this->message, [PR_ENTRYID, PR_PARENT_ENTRYID,
663
			PR_MESSAGE_CLASS, $this->proptags['goid'], $this->proptags['updatecounter'],
664
			PR_PROCESSED, PR_RCVD_REPRESENTING_ENTRYID, PR_SENDER_ENTRYID,
665
			PR_SENT_REPRESENTING_ENTRYID, PR_RECEIVED_BY_ENTRYID]);
666
667
		// do not process meeting requests in sent items folder
668
		$sentItemsEntryid = $this->getDefaultSentmailEntryID();
669
		if (isset($messageprops[PR_PARENT_ENTRYID]) &&
670
			$sentItemsEntryid !== false &&
671
			$sentItemsEntryid == $messageprops[PR_PARENT_ENTRYID]) {
672
			return false;
673
		}
674
675
		$calFolder = $this->openDefaultCalendar();
676
		$store = $this->store;
677
		// If this meeting request is received by a delegate then open delegator's store.
678
		if (isset($messageprops[PR_RCVD_REPRESENTING_ENTRYID], $messageprops[PR_RECEIVED_BY_ENTRYID]) &&
679
			!compareEntryIds($messageprops[PR_RCVD_REPRESENTING_ENTRYID], $messageprops[PR_RECEIVED_BY_ENTRYID])) {
680
			$delegatorStore = $this->getDelegatorStore($messageprops[PR_RCVD_REPRESENTING_ENTRYID], [PR_IPM_APPOINTMENT_ENTRYID]);
681
			if (!empty($delegatorStore['store'])) {
682
				$store = $delegatorStore['store'];
683
			}
684
			if (!empty($delegatorStore[PR_IPM_APPOINTMENT_ENTRYID])) {
685
				$calFolder = $delegatorStore[PR_IPM_APPOINTMENT_ENTRYID];
686
			}
687
		}
688
689
		// check for calendar access
690
		if ($this->checkCalendarWriteAccess($store) !== true) {
691
			// Throw an exception that we don't have write permissions on calendar folder,
692
			// allow caller to fill the error message
693
			throw new MAPIException(null, MAPI_E_NO_ACCESS);
694
		}
695
696
		// if meeting is out dated then don't process it
697
		if ($this->isMeetingRequest($messageprops[PR_MESSAGE_CLASS]) && $this->isMeetingOutOfDate()) {
698
			return false;
699
		}
700
701
		/*
702
		 *	if this function is called automatically with meeting request object then there will be
703
		 *	two possibilitites
704
		 *	1) meeting request is opened first time, in this case make a tentative appointment in
705
		 *		recipient's calendar
706
		 *	2) after this every subsequent request to open meeting request will not do any processing
707
		 */
708
		if ($this->isMeetingRequest($messageprops[PR_MESSAGE_CLASS]) && $userAction == false) {
709
			if (isset($messageprops[PR_PROCESSED]) && $messageprops[PR_PROCESSED] == true) {
710
				// if meeting request is already processed then don't do anything
711
				return false;
712
			}
713
714
			// if correspondent calendar item is already processed then don't do anything
715
			$calendarItem = $this->getCorrespondentCalendarItem();
716
			if ($calendarItem) {
717
				$calendarItemProps = mapi_getprops($calendarItem, [PR_PROCESSED]);
718
				if (isset($calendarItemProps[PR_PROCESSED]) && $calendarItemProps[PR_PROCESSED] == true) {
719
					// mark meeting-request mail as processed as well
720
					mapi_setprops($this->message, [PR_PROCESSED => true]);
721
					mapi_savechanges($this->message);
722
723
					return false;
724
				}
725
			}
726
		}
727
728
		// Retrieve basedate from globalID, if it is not received as argument
729
		if (!$basedate) {
730
			$basedate = $this->getBasedateFromGlobalID($messageprops[$this->proptags['goid']]);
731
		}
732
733
		// set counter proposal properties in calendar item when proposing new time
734
		$proposeNewTimeProps = [];
735
		if ($newProposedStartTime && $newProposedEndTime) {
736
			$proposeNewTimeProps[$this->proptags['proposed_start_whole']] = $newProposedStartTime;
737
			$proposeNewTimeProps[$this->proptags['proposed_end_whole']] = $newProposedEndTime;
738
			$proposeNewTimeProps[$this->proptags['proposed_duration']] = round($newProposedEndTime - $newProposedStartTime) / 60;
739
			$proposeNewTimeProps[$this->proptags['counter_proposal']] = true;
740
		}
741
742
		// While sender is receiver then we have to process the meeting request as per the intended busy status
743
		// instead of tentative, and accept the same as per the intended busystatus.
744
		$senderEntryId = isset($messageprops[PR_SENT_REPRESENTING_ENTRYID]) ? $messageprops[PR_SENT_REPRESENTING_ENTRYID] : $messageprops[PR_SENDER_ENTRYID];
745
		if (isset($messageprops[PR_RECEIVED_BY_ENTRYID]) && compareEntryIds($senderEntryId, $messageprops[PR_RECEIVED_BY_ENTRYID])) {
746
			$entryid = $this->accept(false, $sendresponse, $move, $proposeNewTimeProps, $body, true, $store, $calFolder, $basedate);
747
		}
748
		else {
749
			$entryid = $this->accept($tentative, $sendresponse, $move, $proposeNewTimeProps, $body, $userAction, $store, $calFolder, $basedate);
750
		}
751
752
		// if we have first time processed this meeting then set PR_PROCESSED property
753
		if ($this->isMeetingRequest($messageprops[PR_MESSAGE_CLASS]) && $userAction === false && $isImported === false) {
754
			if (!isset($messageprops[PR_PROCESSED]) || $messageprops[PR_PROCESSED] != true) {
755
				// set processed flag
756
				mapi_setprops($this->message, [PR_PROCESSED => true]);
757
				mapi_savechanges($this->message);
758
			}
759
		}
760
761
		return $entryid;
762
	}
763
764
	/**
765
	 * @param (float|mixed|true)[] $proposeNewTimeProps
766
	 * @param resource             $calFolder
767
	 * @param mixed                $body
768
	 * @param mixed                $store
769
	 * @param mixed                $basedate
770
	 *
771
	 * @psalm-param array<float|mixed|true> $proposeNewTimeProps
772
	 */
773
	public function accept(bool $tentative, bool $sendresponse, bool $move, array $proposeNewTimeProps, $body, bool $userAction, $store, $calFolder, $basedate = false) {
774
		$messageprops = mapi_getprops($this->message);
775
		$isDelegate = isset($messageprops[PR_RCVD_REPRESENTING_NAME]);
776
		$entryid = '';
777
778
		if ($sendresponse) {
779
			$this->createResponse($tentative ? olResponseTentative : olResponseAccepted, $proposeNewTimeProps, $body, $store, $basedate, $calFolder);
780
		}
781
782
		/*
783
		 * Further processing depends on what user is receiving. User can receive recurring item, a single occurrence or a normal meeting.
784
		 * 1) If meeting req is of recurrence then we find all the occurrence in calendar because in past user might have received one or few occurrences.
785
		 * 2) If single occurrence then find occurrence itself using globalID and if item is not found then use cleanGlobalID to find main recurring item
786
		 * 3) Normal meeting req are handled normally as they were handled previously.
787
		 *
788
		 * Also user can respond(accept/decline) to item either from previewpane or from calendar by opening the item. If user is responding the meeting from previewpane
789
		 * and that item is not found in calendar then item is move else item is opened and all properties, attachments and recipient are copied from meeting request.
790
		 * If user is responding from calendar then item is opened and properties are set such as meetingstatus, responsestatus, busystatus etc.
791
		 */
792
		if ($this->isMeetingRequest($messageprops[PR_MESSAGE_CLASS])) {
793
			// This meeting request item is recurring, so find all occurrences and saves them all as exceptions to this meeting request item.
794
			if (isset($messageprops[$this->proptags['recurring']]) && $messageprops[$this->proptags['recurring']] == true && $basedate == false) {
795
				$calendarItem = false;
796
797
				// Find main recurring item based on GlobalID (0x3)
798
				$items = $this->findCalendarItems($messageprops[$this->proptags['goid2']], $calFolder);
799
				if (is_array($items)) {
800
					foreach ($items as $entryid) {
801
						$calendarItem = mapi_msgstore_openentry($store, $entryid);
802
					}
803
				}
804
805
				$processed = false;
806
				if (!$calendarItem) {
807
					// Recurring item not found, so create new meeting in Calendar
808
					$calendarItem = mapi_folder_createmessage($calFolder);
809
				}
810
				else {
811
					// we have found the main recurring item, check if this meeting request is already processed
812
					if (isset($messageprops[PR_PROCESSED]) && $messageprops[PR_PROCESSED] == true) {
813
						// only set required properties, other properties are already copied when processing this meeting request
814
						// for the first time
815
						$processed = true;
816
					}
817
					// While we applying updates of MR then all local categories will be removed,
818
					// So get the local categories of all occurrence before applying update from organiser.
819
					$localCategories = $this->getLocalCategories($calendarItem, $store, $calFolder);
820
				}
821
822
				if (!$processed) {
823
					// get all the properties and copy that to calendar item
824
					$props = mapi_getprops($this->message);
825
					// reset the PidLidMeetingType to Unspecified for outlook display the item
826
					$props[$this->proptags['meetingtype']] = mtgEmpty;
827
					/*
828
					 * the client which has sent this meeting request can generate wrong flagdueby
829
					 * time (mainly OL), so regenerate that property so we will always show reminder
830
					 * on right time
831
					 */
832
					if (isset($props[$this->proptags['reminderminutes']])) {
833
						$props[$this->proptags['flagdueby']] = $props[$this->proptags['startdate']] - ($props[$this->proptags['reminderminutes']] * 60);
834
					}
835
				}
836
				else {
837
					// only get required properties so we will not overwrite existing updated properties from calendar
838
					$props = mapi_getprops($this->message, [PR_ENTRYID]);
839
				}
840
841
				$props[PR_MESSAGE_CLASS] = 'IPM.Appointment';
842
				// When meeting requests are generated by third-party solutions, we might be missing the updatecounter property.
843
				if (!isset($props[$this->proptags['updatecounter']])) {
844
					$props[$this->proptags['updatecounter']] = 0;
845
				}
846
				$props[$this->proptags['meetingstatus']] = olMeetingReceived;
847
				// when we are automatically processing the meeting request set responsestatus to olResponseNotResponded
848
				$props[$this->proptags['responsestatus']] = $userAction ? ($tentative ? olResponseTentative : olResponseAccepted) : olResponseNotResponded;
849
850
				if (isset($props[$this->proptags['intendedbusystatus']])) {
851
					if ($tentative && $props[$this->proptags['intendedbusystatus']] !== fbFree) {
852
						$props[$this->proptags['busystatus']] = fbTentative;
853
					}
854
					else {
855
						$props[$this->proptags['busystatus']] = $props[$this->proptags['intendedbusystatus']];
856
					}
857
					// we already have intendedbusystatus value in $props so no need to copy it
858
				}
859
				else {
860
					$props[$this->proptags['busystatus']] = $tentative ? fbTentative : fbBusy;
861
				}
862
863
				if ($userAction) {
864
					$addrInfo = $this->getOwnerAddress($this->store);
865
866
					// if user has responded then set replytime and name
867
					$props[$this->proptags['replytime']] = time();
868
					if (!empty($addrInfo)) {
869
						// @FIXME conditionally set this property only for delegation case
870
						$props[$this->proptags['apptreplyname']] = $addrInfo[0];
871
					}
872
				}
873
874
				mapi_setprops($calendarItem, $props);
875
876
				// we have already processed attachments and recipients, so no need to do it again
877
				if (!$processed) {
878
					// Copy attachments too
879
					$this->replaceAttachments($this->message, $calendarItem);
880
					// Copy recipients too
881
					$this->replaceRecipients($this->message, $calendarItem, $isDelegate);
882
				}
883
884
				// Find all occurrences based on CleanGlobalID (0x23)
885
				// there will be no exceptions left if $processed is true, but even if it doesn't hurt to recheck
886
				$items = $this->findCalendarItems($messageprops[$this->proptags['goid2']], $calFolder, true);
887
				if (is_array($items)) {
888
					// Save all existing occurrence as exceptions
889
					foreach ($items as $entryid) {
890
						// Open occurrence
891
						$occurrenceItem = mapi_msgstore_openentry($store, $entryid);
892
893
						// Save occurrence into main recurring item as exception
894
						if ($occurrenceItem) {
895
							$occurrenceItemProps = mapi_getprops($occurrenceItem, [$this->proptags['goid'], $this->proptags['recurring']]);
896
897
							// Find basedate of occurrence item
898
							$basedate = $this->getBasedateFromGlobalID($occurrenceItemProps[$this->proptags['goid']]);
899
							if ($basedate && $occurrenceItemProps[$this->proptags['recurring']] != true) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $basedate of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
900
								$this->mergeException($calendarItem, $occurrenceItem, $basedate, $store);
901
							}
902
						}
903
					}
904
				}
905
906
				if (!isset($props[$this->proptags["recurring_pattern"]])) {
907
					$recurr = new Recurrence($store, $calendarItem);
908
					$recurr->saveRecurrencePattern();
909
				}
910
911
				mapi_savechanges($calendarItem);
912
913
				// After applying update of organiser all local categories of occurrence was removed,
914
				// So if local categories exist then apply it on respective occurrence.
915
				if (!empty($localCategories)) {
916
					$this->applyLocalCategories($calendarItem, $store, $localCategories);
917
				}
918
919
				if ($move) {
920
					// open wastebasket of currently logged in user and move the meeting request to it
921
					// for delegates this will be delegate's wastebasket folder
922
					$wastebasket = $this->openDefaultWastebasket($this->openDefaultStore());
923
					$sourcefolder = $this->openParentFolder();
924
					mapi_folder_copymessages($sourcefolder, [$props[PR_ENTRYID]], $wastebasket, MESSAGE_MOVE);
925
				}
926
927
				$entryid = $props[PR_ENTRYID];
928
			}
929
			else {
930
				/**
931
				 * This meeting request is not recurring, so can be an exception or normal meeting.
932
				 * If exception then find main recurring item and update exception
933
				 * If main recurring item is not found then put exception into Calendar as normal meeting.
934
				 */
935
				$calendarItem = false;
936
937
				// We found basedate in GlobalID of this meeting request, so this meeting request if for an occurrence.
938
				if ($basedate) {
939
					// Find main recurring item from CleanGlobalID of this meeting request
940
					$items = $this->findCalendarItems($messageprops[$this->proptags['goid2']], $calFolder);
941
					if (is_array($items)) {
942
						foreach ($items as $entryid) {
943
							$calendarItem = mapi_msgstore_openentry($store, $entryid);
944
						}
945
					}
946
947
					// Main recurring item is found, so now update exception
948
					if ($calendarItem) {
949
						$this->acceptException($calendarItem, $this->message, $basedate, $move, $tentative, $userAction, $store, $isDelegate);
950
						$calendarItemProps = mapi_getprops($calendarItem, [PR_ENTRYID]);
951
						$entryid = $calendarItemProps[PR_ENTRYID];
952
					}
953
				}
954
955
				if (!$calendarItem) {
956
					$items = $this->findCalendarItems($messageprops[$this->proptags['goid']], $calFolder);
957
					if (is_array($items)) {
958
						// Get local categories before deleting MR.
959
						$message = mapi_msgstore_openentry($store, $items[0]);
960
						$localCategories = mapi_getprops($message, [$this->proptags['categories']]);
961
						mapi_folder_deletemessages($calFolder, $items);
962
					}
963
964
					if ($move) {
965
						// All we have to do is open the default calendar,
966
						// set the message class correctly to be an appointment item
967
						// and move it to the calendar folder
968
						$sourcefolder = $this->openParentFolder();
969
970
						// create a new calendar message, and copy the message to there,
971
						// since we want to delete (move to wastebasket) the original message
972
						$old_entryid = mapi_getprops($this->message, [PR_ENTRYID]);
973
						$calmsg = mapi_folder_createmessage($calFolder);
974
						mapi_copyto($this->message, [], [], $calmsg); /* includes attachments and recipients */
975
						// reset the PidLidMeetingType to Unspecified for outlook display the item
976
						$tmp_props = [];
977
						$tmp_props[$this->proptags['meetingtype']] = mtgEmpty;
978
						// OL needs this field always being set, or it will not display item
979
						$tmp_props[$this->proptags['recurring']] = false;
980
						mapi_setprops($calmsg, $tmp_props);
981
982
						// After creating new MR, If local categories exist then apply it on new MR.
983
						if (!empty($localCategories)) {
984
							mapi_setprops($calmsg, $localCategories);
985
						}
986
987
						$calItemProps = [];
988
						$calItemProps[PR_MESSAGE_CLASS] = 'IPM.Appointment';
989
990
						/*
991
						 * the client which has sent this meeting request can generate wrong flagdueby
992
						 * time (mainly OL), so regenerate that property so we will always show reminder
993
						 * on right time
994
						 */
995
						if (isset($messageprops[$this->proptags['reminderminutes']])) {
996
							$calItemProps[$this->proptags['flagdueby']] = $messageprops[$this->proptags['startdate']] - ($messageprops[$this->proptags['reminderminutes']] * 60);
997
						}
998
999
						if (isset($messageprops[$this->proptags['intendedbusystatus']])) {
1000
							if ($tentative && $messageprops[$this->proptags['intendedbusystatus']] !== fbFree) {
1001
								$calItemProps[$this->proptags['busystatus']] = fbTentative;
1002
							}
1003
							else {
1004
								$calItemProps[$this->proptags['busystatus']] = $messageprops[$this->proptags['intendedbusystatus']];
1005
							}
1006
							$calItemProps[$this->proptags['intendedbusystatus']] = $messageprops[$this->proptags['intendedbusystatus']];
1007
						}
1008
						else {
1009
							$calItemProps[$this->proptags['busystatus']] = $tentative ? fbTentative : fbBusy;
1010
						}
1011
1012
						// when we are automatically processing the meeting request set responsestatus to olResponseNotResponded
1013
						$calItemProps[$this->proptags['responsestatus']] = $userAction ? ($tentative ? olResponseTentative : olResponseAccepted) : olResponseNotResponded;
1014
						if ($userAction) {
1015
							$addrInfo = $this->getOwnerAddress($this->store);
1016
1017
							// if user has responded then set replytime and name
1018
							$calItemProps[$this->proptags['replytime']] = time();
1019
							if (!empty($addrInfo)) {
1020
								$calItemProps[$this->proptags['apptreplyname']] = $addrInfo[0];
1021
							}
1022
						}
1023
1024
						$calItemProps[$this->proptags['recurring_pattern']] = '';
1025
						$calItemProps[$this->proptags['alldayevent']] = $messageprops[$this->proptags['alldayevent']] ?? false;
1026
						$calItemProps[$this->proptags['private']] = $messageprops[$this->proptags['private']] ?? false;
1027
						$calItemProps[$this->proptags['meetingstatus']] = $messageprops[$this->proptags['meetingstatus']] ?? olMeetingReceived;
1028
						if (isset($messageprops[$this->proptags['startdate']])) {
1029
							$calItemProps[$this->proptags['commonstart']] = $calItemProps[$this->proptags['startdate']] = $messageprops[$this->proptags['startdate']];
1030
						}
1031
						if (isset($messageprops[$this->proptags['duedate']])) {
1032
							$calItemProps[$this->proptags['commonend']] = $calItemProps[$this->proptags['duedate']] = $messageprops[$this->proptags['duedate']];
1033
						}
1034
1035
						mapi_setprops($calmsg, $proposeNewTimeProps + $calItemProps);
1036
1037
						// get properties which stores owner information in meeting request mails
1038
						$props = mapi_getprops($calmsg, [
1039
							PR_SENT_REPRESENTING_ENTRYID,
1040
							PR_SENT_REPRESENTING_NAME,
1041
							PR_SENT_REPRESENTING_EMAIL_ADDRESS,
1042
							PR_SENT_REPRESENTING_ADDRTYPE,
1043
							PR_SENT_REPRESENTING_SEARCH_KEY,
1044
							PR_SENT_REPRESENTING_SMTP_ADDRESS,
1045
						]);
1046
1047
						// add owner to recipient table
1048
						$recips = [];
1049
						$this->addOrganizer($props, $recips);
1050
						mapi_message_modifyrecipients($calmsg, MODRECIP_ADD, $recips);
1051
						mapi_savechanges($calmsg);
1052
1053
						// Move the message to the wastebasket
1054
						$wastebasket = $this->openDefaultWastebasket($this->openDefaultStore());
1055
						mapi_folder_copymessages($sourcefolder, [$old_entryid[PR_ENTRYID]], $wastebasket, MESSAGE_MOVE);
1056
1057
						$messageprops = mapi_getprops($calmsg, [PR_ENTRYID]);
1058
						$entryid = $messageprops[PR_ENTRYID];
1059
					}
1060
					else {
1061
						// Create a new appointment with duplicate properties and recipient, but as an IPM.Appointment
1062
						$new = mapi_folder_createmessage($calFolder);
1063
						$props = mapi_getprops($this->message);
1064
1065
						$props[$this->proptags['recurring_pattern']] = '';
1066
						$props[$this->proptags['alldayevent']] = $props[$this->proptags['alldayevent']] ?? false;
1067
						$props[$this->proptags['private']] = $props[$this->proptags['private']] ?? false;
1068
						$props[$this->proptags['meetingstatus']] = $props[$this->proptags['meetingstatus']] ?? olMeetingReceived;
1069
						if (isset($props[$this->proptags['startdate']])) {
1070
							$props[$this->proptags['commonstart']] = $props[$this->proptags['startdate']];
1071
						}
1072
						if (isset($props[$this->proptags['duedate']])) {
1073
							$props[$this->proptags['commonend']] = $props[$this->proptags['duedate']];
1074
						}
1075
1076
						$props[PR_MESSAGE_CLASS] = 'IPM.Appointment';
1077
						// reset the PidLidMeetingType to Unspecified for outlook display the item
1078
						$props[$this->proptags['meetingtype']] = mtgEmpty;
1079
						// OL needs this field always being set, or it will not display item
1080
						$props[$this->proptags['recurring']] = false;
1081
1082
						// After creating new MR, If local categories exist then apply it on new MR.
1083
						if (!empty($localCategories)) {
1084
							mapi_setprops($new, $localCategories);
1085
						}
1086
1087
						/*
1088
						 * the client which has sent this meeting request can generate wrong flagdueby
1089
						 * time (mainly OL), so regenerate that property so we will always show reminder
1090
						 * on right time
1091
						 */
1092
						if (isset($props[$this->proptags['reminderminutes']])) {
1093
							$props[$this->proptags['flagdueby']] = $props[$this->proptags['startdate']] - ($props[$this->proptags['reminderminutes']] * 60);
1094
						}
1095
1096
						// When meeting requests are generated by third-party solutions, we might be missing the updatecounter property.
1097
						if (!isset($props[$this->proptags['updatecounter']])) {
1098
							$props[$this->proptags['updatecounter']] = 0;
1099
						}
1100
						// when we are automatically processing the meeting request set responsestatus to olResponseNotResponded
1101
						$props[$this->proptags['responsestatus']] = $userAction ? ($tentative ? olResponseTentative : olResponseAccepted) : olResponseNotResponded;
1102
1103
						if (isset($props[$this->proptags['intendedbusystatus']])) {
1104
							if ($tentative && $props[$this->proptags['intendedbusystatus']] !== fbFree) {
1105
								$props[$this->proptags['busystatus']] = fbTentative;
1106
							}
1107
							else {
1108
								$props[$this->proptags['busystatus']] = $props[$this->proptags['intendedbusystatus']];
1109
							}
1110
							// we already have intendedbusystatus value in $props so no need to copy it
1111
						}
1112
						else {
1113
							$props[$this->proptags['busystatus']] = $tentative ? fbTentative : fbBusy;
1114
						}
1115
1116
						if ($userAction) {
1117
							$addrInfo = $this->getOwnerAddress($this->store);
1118
1119
							// if user has responded then set replytime and name
1120
							$props[$this->proptags['replytime']] = time();
1121
							if (!empty($addrInfo)) {
1122
								$props[$this->proptags['apptreplyname']] = $addrInfo[0];
1123
							}
1124
						}
1125
1126
						mapi_setprops($new, $proposeNewTimeProps + $props);
1127
1128
						$reciptable = mapi_message_getrecipienttable($this->message);
1129
1130
						$recips = [];
1131
						// If delegate, then do not add the delegate in recipients
1132
						if ($isDelegate) {
1133
							$delegate = mapi_getprops($this->message, [PR_RECEIVED_BY_EMAIL_ADDRESS]);
1134
							$res = [
1135
								RES_PROPERTY,
1136
								[
1137
									RELOP => RELOP_NE,
1138
									ULPROPTAG => PR_EMAIL_ADDRESS,
1139
									VALUE => [PR_EMAIL_ADDRESS => $delegate[PR_RECEIVED_BY_EMAIL_ADDRESS]],
1140
								],
1141
							];
1142
							$recips = mapi_table_queryallrows($reciptable, $this->recipprops, $res);
1143
						}
1144
						else {
1145
							$recips = mapi_table_queryallrows($reciptable, $this->recipprops);
1146
						}
1147
1148
						$this->addOrganizer($props, $recips);
1149
						mapi_message_modifyrecipients($new, MODRECIP_ADD, $recips);
1150
						mapi_savechanges($new);
1151
1152
						$props = mapi_getprops($new, [PR_ENTRYID]);
1153
						$entryid = $props[PR_ENTRYID];
1154
					}
1155
				}
1156
			}
1157
		}
1158
		else {
1159
			// Here only properties are set on calendaritem, because user is responding from calendar.
1160
			$props = [];
1161
			$props[$this->proptags['responsestatus']] = $tentative ? olResponseTentative : olResponseAccepted;
1162
1163
			if (isset($messageprops[$this->proptags['intendedbusystatus']])) {
1164
				if ($tentative && $messageprops[$this->proptags['intendedbusystatus']] !== fbFree) {
1165
					$props[$this->proptags['busystatus']] = fbTentative;
1166
				}
1167
				else {
1168
					$props[$this->proptags['busystatus']] = $messageprops[$this->proptags['intendedbusystatus']];
1169
				}
1170
				$props[$this->proptags['intendedbusystatus']] = $messageprops[$this->proptags['intendedbusystatus']];
1171
			}
1172
			else {
1173
				$props[$this->proptags['busystatus']] = $tentative ? fbTentative : fbBusy;
1174
			}
1175
1176
			$props[$this->proptags['meetingstatus']] = olMeetingReceived;
1177
1178
			$addrInfo = $this->getOwnerAddress($this->store);
1179
1180
			// if user has responded then set replytime and name
1181
			$props[$this->proptags['replytime']] = time();
1182
			if (!empty($addrInfo)) {
1183
				$props[$this->proptags['apptreplyname']] = $addrInfo[0];
1184
			}
1185
1186
			if ($basedate) {
1187
				$recurr = new Recurrence($store, $this->message);
1188
1189
				// Copy recipients list
1190
				$reciptable = mapi_message_getrecipienttable($this->message);
1191
				$recips = mapi_table_queryallrows($reciptable, $this->recipprops);
1192
1193
				if ($recurr->isException($basedate)) {
1194
					$recurr->modifyException($proposeNewTimeProps + $props, $basedate, $recips);
1195
				}
1196
				else {
1197
					$props[$this->proptags['startdate']] = $recurr->getOccurrenceStart($basedate);
1198
					$props[$this->proptags['duedate']] = $recurr->getOccurrenceEnd($basedate);
1199
1200
					$props[PR_SENT_REPRESENTING_EMAIL_ADDRESS] = $messageprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS];
1201
					$props[PR_SENT_REPRESENTING_NAME] = $messageprops[PR_SENT_REPRESENTING_NAME];
1202
					$props[PR_SENT_REPRESENTING_ADDRTYPE] = $messageprops[PR_SENT_REPRESENTING_ADDRTYPE];
1203
					$props[PR_SENT_REPRESENTING_ENTRYID] = $messageprops[PR_SENT_REPRESENTING_ENTRYID];
1204
					$props[PR_SENT_REPRESENTING_SEARCH_KEY] = $messageprops[PR_SENT_REPRESENTING_SEARCH_KEY];
1205
1206
					$recurr->createException($proposeNewTimeProps + $props, $basedate, false, $recips);
1207
				}
1208
			}
1209
			else {
1210
				mapi_setprops($this->message, $proposeNewTimeProps + $props);
1211
			}
1212
			mapi_savechanges($this->message);
1213
1214
			$entryid = $messageprops[PR_ENTRYID];
1215
		}
1216
1217
		return $entryid;
1218
	}
1219
1220
	/**
1221
	 * Declines the meeting request by moving the item to the deleted
1222
	 * items folder and sending a decline message. After declining, you
1223
	 * can't use this class instance any more. The message is closed.
1224
	 * When an occurrence is decline then false is returned because that
1225
	 * occurrence is deleted not the recurring item.
1226
	 *
1227
	 * @param bool  $sendresponse true if a response has to be sent to organizer
1228
	 * @param mixed $basedate     if specified contains starttime of day of an occurrence
1229
	 * @param mixed $body
1230
	 *
1231
	 * @return bool true if item is deleted from Calendar else false
1232
	 */
1233
	public function doDecline($sendresponse, $basedate = false, $body = false) {
1234
		if ($this->isLocalOrganiser()) {
1235
			return false;
1236
		}
1237
1238
		$result = false;
1239
		$calendaritem = false;
1240
1241
		// Remove any previous calendar items with this goid and appt id
1242
		$messageprops = mapi_getprops($this->message, [$this->proptags['goid'], $this->proptags['goid2'], PR_RCVD_REPRESENTING_ENTRYID]);
1243
1244
		$store = $this->store;
1245
		$calFolder = $this->openDefaultCalendar();
1246
		// If this meeting request is received by a delegate then open delegator's store.
1247
		if (isset($messageprops[PR_RCVD_REPRESENTING_ENTRYID])) {
1248
			$delegatorStore = $this->getDelegatorStore($messageprops[PR_RCVD_REPRESENTING_ENTRYID], [PR_IPM_APPOINTMENT_ENTRYID]);
1249
			if (!empty($delegatorStore['store'])) {
1250
				$store = $delegatorStore['store'];
1251
			}
1252
			if (!empty($delegatorStore[PR_IPM_APPOINTMENT_ENTRYID])) {
1253
				$calFolder = $delegatorStore[PR_IPM_APPOINTMENT_ENTRYID];
1254
			}
1255
		}
1256
1257
		// check for calendar access before deleting the calendar item
1258
		if ($this->checkCalendarWriteAccess($store) !== true) {
1259
			// Throw an exception that we don't have write permissions on calendar folder,
1260
			// allow caller to fill the error message
1261
			throw new MAPIException(null, MAPI_E_NO_ACCESS);
1262
		}
1263
1264
		$goid = $messageprops[$this->proptags['goid']];
1265
1266
		// First, find the items in the calendar by GlobalObjid (0x3)
1267
		$entryids = $this->findCalendarItems($goid, $calFolder);
1268
1269
		if (!$basedate) {
1270
			$basedate = $this->getBasedateFromGlobalID($goid);
1271
		}
1272
1273
		if ($sendresponse) {
1274
			$this->createResponse(olResponseDeclined, [], $body, $store, $basedate, $calFolder);
1275
		}
1276
1277
		if ($basedate) {
1278
			// use CleanGlobalObjid (0x23)
1279
			$calendaritems = $this->findCalendarItems($messageprops[$this->proptags['goid2']], $calFolder);
1280
1281
			if (is_array($calendaritems)) {
1282
				foreach ($calendaritems as $entryid) {
1283
					// Open each calendar item and set the properties of the cancellation object
1284
					$calendaritem = mapi_msgstore_openentry($store, $entryid);
1285
1286
					// Recurring item is found, now delete exception
1287
					if ($calendaritem) {
1288
						$this->doRemoveExceptionFromCalendar($basedate, $calendaritem, $store);
1289
						$result = true;
1290
					}
1291
				}
1292
			}
1293
1294
			if ($this->isMeetingRequest()) {
1295
				$calendaritem = false;
1296
			}
1297
		}
1298
1299
		if (!$calendaritem) {
1300
			$calendar = $this->openDefaultCalendar($store);
1301
1302
			if (!empty($entryids)) {
1303
				mapi_folder_deletemessages($calendar, $entryids);
1304
			}
1305
1306
			// All we have to do to decline, is to move the item to the waste basket
1307
			$wastebasket = $this->openDefaultWastebasket($this->openDefaultStore());
1308
			$sourcefolder = $this->openParentFolder();
1309
1310
			$messageprops = mapi_getprops($this->message, [PR_ENTRYID]);
1311
1312
			// Release the message
1313
			$this->message = null;
1314
1315
			// Move the message to the waste basket
1316
			mapi_folder_copymessages($sourcefolder, [$messageprops[PR_ENTRYID]], $wastebasket, MESSAGE_MOVE);
1317
1318
			$result = true;
1319
		}
1320
1321
		return $result;
1322
	}
1323
1324
	/**
1325
	 * Removes a meeting request from the calendar when the user presses the
1326
	 * 'remove from calendar' button in response to a meeting cancellation.
1327
	 *
1328
	 * @param mixed $basedate if specified contains starttime of day of an occurrence
1329
	 *
1330
	 * @return null|false
1331
	 */
1332
	public function doRemoveFromCalendar($basedate) {
1333
		if ($this->isLocalOrganiser()) {
1334
			return false;
1335
		}
1336
1337
		$messageprops = mapi_getprops($this->message, [PR_ENTRYID, $this->proptags['goid'], PR_RCVD_REPRESENTING_ENTRYID, PR_MESSAGE_CLASS]);
1338
1339
		$goid = $messageprops[$this->proptags['goid']];
1340
1341
		$store = $this->store;
1342
		$calFolder = $this->openDefaultCalendar();
1343
		if (isset($messageprops[PR_RCVD_REPRESENTING_ENTRYID])) {
1344
			$delegatorStore = $this->getDelegatorStore($messageprops[PR_RCVD_REPRESENTING_ENTRYID], [PR_IPM_APPOINTMENT_ENTRYID]);
1345
			if (!empty($delegatorStore['store'])) {
1346
				$store = $delegatorStore['store'];
1347
			}
1348
			if (!empty($delegatorStore[PR_IPM_APPOINTMENT_ENTRYID])) {
1349
				$calFolder = $delegatorStore[PR_IPM_APPOINTMENT_ENTRYID];
1350
			}
1351
		}
1352
1353
		// check for calendar access before deleting the calendar item
1354
		if ($this->checkCalendarWriteAccess($store) !== true) {
1355
			// Throw an exception that we don't have write permissions on calendar folder,
1356
			// allow caller to fill the error message
1357
			throw new MAPIException(null, MAPI_E_NO_ACCESS);
1358
		}
1359
1360
		$wastebasket = $this->openDefaultWastebasket($this->openDefaultStore());
1361
		// get the source folder of the meeting message
1362
		$sourcefolder = $this->openParentFolder();
1363
1364
		// Check if the message is a meeting request in the inbox or a calendaritem by checking the message class
1365
		if ($this->isMeetingCancellation($messageprops[PR_MESSAGE_CLASS])) {
1366
			// get the basedate to check for exception
1367
			$basedate = $this->getBasedateFromGlobalID($goid);
1368
1369
			$calendarItem = $this->getCorrespondentCalendarItem(true);
1370
1371
			if ($calendarItem !== false) {
1372
				// basedate is provided so open exception
1373
				if ($basedate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $basedate of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
1374
					$exception = $this->getExceptionItem($calendarItem, $basedate);
1375
1376
					if ($exception !== false) {
1377
						// exception found, remove it from calendar
1378
						$this->doRemoveExceptionFromCalendar($basedate, $calendarItem, $store);
1379
					}
1380
				}
1381
				else {
1382
					// remove normal / recurring series from calendar
1383
					$entryids = mapi_getprops($calendarItem, [PR_ENTRYID]);
1384
1385
					$entryids = [$entryids[PR_ENTRYID]];
1386
1387
					mapi_folder_copymessages($calFolder, $entryids, $wastebasket, MESSAGE_MOVE);
1388
				}
1389
			}
1390
1391
			// Release the message, because we are going to move it to wastebasket
1392
			$this->message = null;
1393
1394
			// Move the cancellation mail to wastebasket
1395
			mapi_folder_copymessages($sourcefolder, [$messageprops[PR_ENTRYID]], $wastebasket, MESSAGE_MOVE);
1396
		}
1397
		else {
1398
			// Here only properties are set on calendaritem, because user is responding from calendar.
1399
			if ($basedate) {
1400
				// remove the occurrence
1401
				$this->doRemoveExceptionFromCalendar($basedate, $this->message, $store);
1402
			}
1403
			else {
1404
				// remove normal/recurring meeting item.
1405
				// Move the message to the waste basket
1406
				mapi_folder_copymessages($sourcefolder, [$messageprops[PR_ENTRYID]], $wastebasket, MESSAGE_MOVE);
1407
			}
1408
		}
1409
	}
1410
1411
	/**
1412
	 * Function can be used to cancel any existing meeting and send cancellation mails to attendees.
1413
	 * Should only be called from meeting object from calendar.
1414
	 *
1415
	 * @param mixed $basedate (optional) basedate of occurrence which should be cancelled
1416
	 *
1417
	 * @FIXME cancellation mail is also sent to attendee which has declined the meeting
1418
	 * @FIXME don't send canellation mail when cancelling meeting from past
1419
	 */
1420
	public function doCancelInvitation($basedate = false) {
1421
		if (!$this->isLocalOrganiser()) {
1422
			return;
1423
		}
1424
1425
		// check write access for delegate
1426
		if ($this->checkCalendarWriteAccess($this->store) !== true) {
1427
			// Throw an exception that we don't have write permissions on calendar folder,
1428
			// error message will be filled by module
1429
			throw new MAPIException(null, MAPI_E_NO_ACCESS);
1430
		}
1431
1432
		$messageProps = mapi_getprops($this->message, [PR_ENTRYID, $this->proptags['recurring']]);
1433
1434
		if (isset($messageProps[$this->proptags['recurring']]) && $messageProps[$this->proptags['recurring']] === true) {
1435
			// cancellation of recurring series or one occurrence
1436
			$recurrence = new Recurrence($this->store, $this->message);
1437
1438
			// if basedate is specified then we are cancelling only one occurrence, so create exception for that occurrence
1439
			if ($basedate) {
1440
				$recurrence->createException([], $basedate, true);
1441
			}
1442
1443
			// update the meeting request
1444
			$this->updateMeetingRequest();
1445
1446
			// send cancellation mails
1447
			$this->sendMeetingRequest(true, dgettext('zarafa', 'Canceled') . ': ', $basedate);
1448
1449
			// save changes in the message
1450
			mapi_savechanges($this->message);
1451
		}
1452
		else {
1453
			// cancellation of normal meeting request
1454
			// Send the cancellation
1455
			$this->updateMeetingRequest();
1456
			$this->sendMeetingRequest(true, dgettext('zarafa', 'Canceled') . ': ');
1457
1458
			// save changes in the message
1459
			mapi_savechanges($this->message);
1460
		}
1461
1462
		// if basedate is specified then we have already created exception of it so nothing should be done now
1463
		// but when cancelling normal / recurring meeting request we need to remove meeting from calendar
1464
		if ($basedate === false) {
1465
			// get the wastebasket folder, for delegate this will give wastebasket of delegate
1466
			$wastebasket = $this->openDefaultWastebasket($this->openDefaultStore());
1467
1468
			// get the source folder of the meeting message
1469
			$sourcefolder = $this->openParentFolder();
1470
1471
			// Move the message to the deleted items
1472
			mapi_folder_copymessages($sourcefolder, [$messageProps[PR_ENTRYID]], $wastebasket, MESSAGE_MOVE);
1473
		}
1474
	}
1475
1476
	/**
1477
	 * Convert epoch to MAPI FileTime, number of 100-nanosecond units since
1478
	 * the start of January 1, 1601.
1479
	 * https://msdn.microsoft.com/en-us/library/office/cc765906.aspx.
1480
	 *
1481
	 * @param int $epoch the current epoch
1482
	 *
1483
	 * @return int the MAPI FileTime equalevent to the given epoch time
1484
	 */
1485
	public function epochToMapiFileTime($epoch) {
1486
		$nanoseconds_between_epoch = 116444736000000000;
1487
1488
		return ($epoch * 10000000) + $nanoseconds_between_epoch;
1489
	}
1490
1491
	/**
1492
	 * Sets the properties in the message so that is can be sent
1493
	 * as a meeting request. The caller has to submit the message. This
1494
	 * is only used for new MeetingRequests. Pass the appointment item as $message
1495
	 * in the constructor to do this.
1496
	 *
1497
	 * @param mixed $basedate
1498
	 */
1499
	public function setMeetingRequest($basedate = false): void {
1500
		$props = mapi_getprops($this->message, [$this->proptags['updatecounter']]);
1501
1502
		// Create a new global id for this item
1503
		// https://msdn.microsoft.com/en-us/library/ee160198(v=exchg.80).aspx
1504
		$goid = pack('H*', '040000008200E00074C5B7101A82E00800000000');
1505
		/*
1506
		$year = gmdate('Y');
1507
		$month = gmdate('n');
1508
		$day = gmdate('j');
1509
		$goid .= pack('n', $year);
1510
		$goid .= pack('C', $month);
1511
		$goid .= pack('C', $day);
1512
		*/
1513
		// Creation Time
1514
		$time = $this->epochToMapiFileTime(time());
1515
		$goid .= pack('V', $time & 0xFFFFFFFF);
1516
		$goid .= pack('V', $time >> 32);
1517
		// 8 Zeros
1518
		$goid .= pack('H*', '0000000000000000');
1519
		// Length of the random data
1520
		$goid .= pack('V', 16);
1521
		// Random data.
1522
		for ($i = 0; $i < 16; ++$i) {
1523
			$goid .= chr(rand(0, 255));
1524
		}
1525
1526
		// Create a new appointment id for this item
1527
		$apptid = rand();
1528
1529
		$props[PR_OWNER_APPT_ID] = $apptid;
1530
		$props[PR_ICON_INDEX] = 1026;
1531
		$props[$this->proptags['goid']] = $goid;
1532
		$props[$this->proptags['goid2']] = $goid;
1533
1534
		if (!isset($props[$this->proptags['updatecounter']])) {
1535
			$props[$this->proptags['updatecounter']] = 0;			// OL also starts sequence no with zero.
1536
			$props[$this->proptags['last_updatecounter']] = 0;
1537
		}
1538
1539
		mapi_setprops($this->message, $props);
1540
	}
1541
1542
	/**
1543
	 * Sends a meeting request by copying it to the outbox, converting
1544
	 * the message class, adding some properties that are required only
1545
	 * for sending the message and submitting the message. Set cancel to
1546
	 * true if you wish to completely cancel the meeting request. You can
1547
	 * specify an optional 'prefix' to prefix the sent message, which is normally
1548
	 * 'Canceled: '.
1549
	 *
1550
	 * @param mixed $cancel
1551
	 * @param mixed $prefix
1552
	 * @param mixed $basedate
1553
	 * @param mixed $modifiedRecips
1554
	 * @param mixed $deletedRecips
1555
	 *
1556
	 * @return (int|mixed)[]|true
1557
	 *
1558
	 * @psalm-return array{error: 1|3|4, displayname: mixed}|true
1559
	 */
1560
	public function sendMeetingRequest($cancel, $prefix = false, $basedate = false, $modifiedRecips = false, $deletedRecips = false) {
1561
		$this->includesResources = false;
1562
		$this->nonAcceptingResources = [];
1563
1564
		// Get the properties of the message
1565
		$messageprops = mapi_getprops($this->message, [$this->proptags['recurring']]);
1566
1567
		/*
1568
		 * Submit message to non-resource recipients
1569
		 */
1570
		// Set BusyStatus to olTentative (1)
1571
		// Set MeetingStatus to olMeetingReceived
1572
		// Set ResponseStatus to olResponseNotResponded
1573
1574
		/*
1575
		 * While sending recurrence meeting exceptions are not sent as attachments
1576
		 * because first all exceptions are sent and then recurrence meeting is sent.
1577
		 */
1578
		if (isset($messageprops[$this->proptags['recurring']]) && $messageprops[$this->proptags['recurring']] && !$basedate) {
1579
			// Book resource
1580
			$this->bookResources($this->message, $cancel, $prefix);
1581
1582
			if (!$this->errorSetResource) {
1583
				$recurr = new Recurrence($this->openDefaultStore(), $this->message);
1584
1585
				// First send meetingrequest for recurring item
1586
				$this->submitMeetingRequest($this->message, $cancel, $prefix, false, $recurr, false, $modifiedRecips, $deletedRecips);
1587
1588
				// Then send all meeting request for all exceptions
1589
				$exceptions = $recurr->getAllExceptions();
1590
				if ($exceptions) {
1591
					foreach ($exceptions as $exceptionBasedate) {
1592
						$attach = $recurr->getExceptionAttachment($exceptionBasedate);
1593
1594
						if ($attach) {
1595
							$occurrenceItem = mapi_attach_openobj($attach, MAPI_MODIFY);
1596
							$this->submitMeetingRequest($occurrenceItem, $cancel, false, $exceptionBasedate, $recurr, false, $modifiedRecips, $deletedRecips);
1597
							mapi_savechanges($attach);
1598
						}
1599
					}
1600
				}
1601
			}
1602
		}
1603
		else {
1604
			// Basedate found, an exception is to be sent
1605
			if ($basedate) {
1606
				$recurr = new Recurrence($this->openDefaultStore(), $this->message);
1607
1608
				if ($cancel) {
1609
					// @TODO: remove occurrence from Resource's Calendar if resource was booked for whole series
1610
					$this->submitMeetingRequest($this->message, $cancel, $prefix, $basedate, $recurr, false);
1611
				}
1612
				else {
1613
					$attach = $recurr->getExceptionAttachment($basedate);
1614
1615
					if ($attach) {
1616
						$occurrenceItem = mapi_attach_openobj($attach, MAPI_MODIFY);
1617
1618
						// Book resource for this occurrence
1619
						$resourceRecipData = $this->bookResources($occurrenceItem, $cancel, $prefix, $basedate);
1620
1621
						if (!$this->errorSetResource) {
1622
							// Save all previous changes
1623
							mapi_savechanges($this->message);
1624
1625
							$this->submitMeetingRequest($occurrenceItem, $cancel, $prefix, $basedate, $recurr, true, $modifiedRecips, $deletedRecips);
1626
							mapi_savechanges($occurrenceItem);
1627
							mapi_savechanges($attach);
1628
						}
1629
					}
1630
				}
1631
			}
1632
			else {
1633
				// This is normal meeting
1634
				$resourceRecipData = $this->bookResources($this->message, $cancel, $prefix);
1635
1636
				if (!$this->errorSetResource) {
1637
					$this->submitMeetingRequest($this->message, $cancel, $prefix, false, false, false, $modifiedRecips, $deletedRecips);
1638
				}
1639
			}
1640
		}
1641
1642
		if (isset($this->errorSetResource) && $this->errorSetResource) {
1643
			return [
1644
				'error' => $this->errorSetResource,
1645
				'displayname' => $this->recipientDisplayname,
1646
			];
1647
		}
1648
1649
		return true;
1650
	}
1651
1652
	/**
1653
	 * Updates the message after an update has been performed (for example,
1654
	 * changing the time of the meeting). This must be called before re-sending
1655
	 * the meeting request. You can also call this function instead of 'setMeetingRequest()'
1656
	 * as it will automatically call setMeetingRequest on this object if it is the first
1657
	 * call to this function.
1658
	 *
1659
	 * @param mixed $basedate
1660
	 */
1661
	public function updateMeetingRequest($basedate = false): void {
1662
		$messageprops = mapi_getprops($this->message, [$this->proptags['last_updatecounter'], $this->proptags['goid']]);
1663
1664
		if (!isset($messageprops[$this->proptags['goid']])) {
1665
			$this->setMeetingRequest($basedate);
1666
		}
1667
		else {
1668
			$counter = (isset($messageprops[$this->proptags['last_updatecounter']]) ?? 0) + 1;
1669
1670
			// increment value of last_updatecounter, last_updatecounter will be common for recurring series
1671
			// so even if you sending an exception only you need to update the last_updatecounter in the recurring series message
1672
			// this way we can make sure that every time we will be using a uniwue number for every operation
1673
			mapi_setprops($this->message, [$this->proptags['last_updatecounter'] => $counter]);
1674
		}
1675
	}
1676
1677
	/**
1678
	 * Returns TRUE if we are the organiser of the meeting. Can be used with any type of meeting object.
1679
	 */
1680
	public function isLocalOrganiser(): bool {
1681
		$props = mapi_getprops($this->message, [$this->proptags['goid'], PR_MESSAGE_CLASS]);
1682
1683
		if (!$this->isMeetingRequest($props[PR_MESSAGE_CLASS]) && !$this->isMeetingRequestResponse($props[PR_MESSAGE_CLASS]) && !$this->isMeetingCancellation($props[PR_MESSAGE_CLASS])) {
1684
			// we are checking with calendar item
1685
			$calendarItem = $this->message;
1686
		}
1687
		else {
1688
			// we are checking with meeting request / response / cancellation mail
1689
			// get calendar items
1690
			$calendarItem = $this->getCorrespondentCalendarItem(true);
1691
		}
1692
1693
		// even if we have received request/response for exception/occurrence then also
1694
		// we can check recurring series for organizer, no need to check with exception/occurrence
1695
1696
		if ($calendarItem !== false) {
1697
			$messageProps = mapi_getprops($calendarItem, [$this->proptags['responsestatus']]);
1698
1699
			if (isset($messageProps[$this->proptags['responsestatus']]) && $messageProps[$this->proptags['responsestatus']] === olResponseOrganized) {
1700
				return true;
1701
			}
1702
		}
1703
1704
		return false;
1705
	}
1706
1707
	/*
1708
	 * Support functions - INTERNAL ONLY
1709
	 ***************************************************************************************************
1710
	 */
1711
1712
	/**
1713
	 * Return the tracking status of a recipient based on the IPM class (passed).
1714
	 *
1715
	 * @param mixed $class
1716
	 */
1717
	public function getTrackStatus($class) {
1718
		$status = olRecipientTrackStatusNone;
1719
1720
		switch ($class) {
1721
			case 'IPM.Schedule.Meeting.Resp.Pos':
1722
				$status = olRecipientTrackStatusAccepted;
1723
				break;
1724
1725
			case 'IPM.Schedule.Meeting.Resp.Tent':
1726
				$status = olRecipientTrackStatusTentative;
1727
				break;
1728
1729
			case 'IPM.Schedule.Meeting.Resp.Neg':
1730
				$status = olRecipientTrackStatusDeclined;
1731
				break;
1732
		}
1733
1734
		return $status;
1735
	}
1736
1737
	/**
1738
	 * Function returns MAPIFolder resource of the folder that currently holds this meeting/meeting request
1739
	 * object.
1740
	 */
1741
	public function openParentFolder() {
1742
		$messageprops = mapi_getprops($this->message, [PR_PARENT_ENTRYID]);
1743
1744
		return mapi_msgstore_openentry($this->store, $messageprops[PR_PARENT_ENTRYID]);
1745
	}
1746
1747
	/**
1748
	 * Function will return resource of the default calendar folder of store.
1749
	 *
1750
	 * @param mixed $store {optional} user store whose default calendar should be opened
1751
	 *
1752
	 * @return resource default calendar folder of store
1753
	 */
1754
	public function openDefaultCalendar($store = false) {
1755
		return $this->openDefaultFolder(PR_IPM_APPOINTMENT_ENTRYID, $store);
1756
	}
1757
1758
	/**
1759
	 * Function will return resource of the default outbox folder of store.
1760
	 *
1761
	 * @param mixed $store {optional} user store whose default outbox should be opened
1762
	 *
1763
	 * @return resource default outbox folder of store
1764
	 */
1765
	public function openDefaultOutbox($store = false) {
1766
		return $this->openBaseFolder(PR_IPM_OUTBOX_ENTRYID, $store);
1767
	}
1768
1769
	/**
1770
	 * Function will return resource of the default wastebasket folder of store.
1771
	 *
1772
	 * @param mixed $store {optional} user store whose default wastebasket should be opened
1773
	 *
1774
	 * @return resource default wastebasket folder of store
1775
	 */
1776
	public function openDefaultWastebasket($store = false) {
1777
		return $this->openBaseFolder(PR_IPM_WASTEBASKET_ENTRYID, $store);
1778
	}
1779
1780
	/**
1781
	 * Function will return resource of the default calendar folder of store.
1782
	 *
1783
	 * @param mixed $store {optional} user store whose default calendar should be opened
1784
	 *
1785
	 * @return bool|string default calendar folder of store
1786
	 */
1787
	public function getDefaultWastebasketEntryID($store = false) {
1788
		return $this->getBaseEntryID(PR_IPM_WASTEBASKET_ENTRYID, $store);
1789
	}
1790
1791
	/**
1792
	 * Function will return resource of the default sent mail folder of store.
1793
	 *
1794
	 * @param mixed $store {optional} user store whose default sent mail should be opened
1795
	 *
1796
	 * @return bool|string default sent mail folder of store
1797
	 */
1798
	public function getDefaultSentmailEntryID($store = false) {
1799
		return $this->getBaseEntryID(PR_IPM_SENTMAIL_ENTRYID, $store);
1800
	}
1801
1802
	/**
1803
	 * Function will return entryid of any default folder of store. This method is useful when you want
1804
	 * to get entryid of folder which is stored as properties of inbox folder
1805
	 * (PR_IPM_APPOINTMENT_ENTRYID, PR_IPM_CONTACT_ENTRYID, PR_IPM_DRAFTS_ENTRYID, PR_IPM_JOURNAL_ENTRYID, PR_IPM_NOTE_ENTRYID, PR_IPM_TASK_ENTRYID).
1806
	 *
1807
	 * @param int   $prop  proptag of the folder for which we want to get entryid
1808
	 * @param mixed $store {optional} user store from which we need to get entryid of default folder
1809
	 *
1810
	 * @return bool|string entryid of folder pointed by $prop
1811
	 */
1812
	public function getDefaultFolderEntryID($prop, $store = false) {
1813
		try {
1814
			$inbox = mapi_msgstore_getreceivefolder($store ? $store : $this->store);
1815
			$inboxprops = mapi_getprops($inbox, [$prop]);
1816
			if (isset($inboxprops[$prop])) {
1817
				return $inboxprops[$prop];
1818
			}
1819
		}
1820
		catch (MAPIException $e) {
1821
			// public store doesn't support this method
1822
			if ($e->getCode() == MAPI_E_NO_SUPPORT) {
1823
				// don't propagate this error to parent handlers, if store doesn't support it
1824
				$e->setHandled();
1825
			}
1826
		}
1827
1828
		return false;
1829
	}
1830
1831
	/**
1832
	 * Function will return resource of any default folder of store.
1833
	 *
1834
	 * @param int   $prop  proptag of the folder that we want to open
1835
	 * @param mixed $store {optional} user store from which we need to open default folder
1836
	 *
1837
	 * @return resource default folder of store
1838
	 */
1839
	public function openDefaultFolder($prop, $store = false) {
1840
		$folder = false;
1841
		$entryid = $this->getDefaultFolderEntryID($prop, $store);
1842
1843
		if ($entryid !== false) {
1844
			$folder = mapi_msgstore_openentry($store ? $store : $this->store, $entryid);
1845
		}
1846
1847
		return $folder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $folder returns the type false|resource which is incompatible with the documented return type resource.
Loading history...
1848
	}
1849
1850
	/**
1851
	 * Function will return entryid of default folder from store. This method is useful when you want
1852
	 * to get entryid of folder which is stored as store properties
1853
	 * (PR_IPM_FAVORITES_ENTRYID, PR_IPM_OUTBOX_ENTRYID, PR_IPM_SENTMAIL_ENTRYID, PR_IPM_WASTEBASKET_ENTRYID).
1854
	 *
1855
	 * @param int   $prop  proptag of the folder whose entryid we want to get
1856
	 * @param mixed $store {optional} user store from which we need to get entryid of default folder
1857
	 *
1858
	 * @return bool|string entryid of default folder from store
1859
	 */
1860
	public function getBaseEntryID($prop, $store = false) {
1861
		$storeprops = mapi_getprops($store ? $store : $this->store, [$prop]);
1862
		if (!isset($storeprops[$prop])) {
1863
			return false;
1864
		}
1865
1866
		return $storeprops[$prop];
1867
	}
1868
1869
	/**
1870
	 * Function will return resource of any default folder of store.
1871
	 *
1872
	 * @param int   $prop  proptag of the folder that we want to open
1873
	 * @param mixed $store {optional} user store from which we need to open default folder
1874
	 *
1875
	 * @return resource default folder of store
1876
	 */
1877
	public function openBaseFolder($prop, $store = false) {
1878
		$folder = false;
1879
		$entryid = $this->getBaseEntryID($prop, $store);
1880
1881
		if ($entryid !== false) {
1882
			$folder = mapi_msgstore_openentry($store ? $store : $this->store, $entryid);
1883
		}
1884
1885
		return $folder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $folder returns the type false|resource which is incompatible with the documented return type resource.
Loading history...
1886
	}
1887
1888
	/**
1889
	 * Function checks whether user has access over the specified folder or not.
1890
	 *
1891
	 * @param string $entryid entryid The entryid of the folder to check
1892
	 * @param mixed  $store   (optional) store from which folder should be opened
1893
	 *
1894
	 * @return bool true if user has an access over the folder, false if not
1895
	 */
1896
	public function checkFolderWriteAccess($entryid, $store = false) {
1897
		$accessToFolder = false;
1898
1899
		if (!empty($entryid)) {
1900
			if ($store === false) {
1901
				$store = $this->store;
1902
			}
1903
1904
			try {
1905
				$folder = mapi_msgstore_openentry($store, $entryid);
1906
				$folderProps = mapi_getprops($folder, [PR_ACCESS]);
1907
				if (($folderProps[PR_ACCESS] & MAPI_ACCESS_CREATE_CONTENTS) === MAPI_ACCESS_CREATE_CONTENTS) {
1908
					$accessToFolder = true;
1909
				}
1910
			}
1911
			catch (MAPIException $e) {
1912
				// we don't have rights to open folder, so return false
1913
				if ($e->getCode() == MAPI_E_NO_ACCESS) {
1914
					return $accessToFolder;
1915
				}
1916
1917
				// rethrow other errors
1918
				throw $e;
1919
			}
1920
		}
1921
1922
		return $accessToFolder;
1923
	}
1924
1925
	/**
1926
	 * Function checks whether user has access over the specified folder or not.
1927
	 *
1928
	 * @param mixed $store
1929
	 *
1930
	 * @return bool true if user has an access over the folder, false if not
1931
	 */
1932
	public function checkCalendarWriteAccess($store = false) {
1933
		if ($store === false) {
1934
			$messageProps = mapi_getprops($this->message, [PR_RCVD_REPRESENTING_ENTRYID]);
1935
			$store = $this->store;
1936
			// If this meeting request is received by a delegate then open delegator's store.
1937
			if (isset($messageProps[PR_RCVD_REPRESENTING_ENTRYID])) {
1938
				$delegatorStore = $this->getDelegatorStore($messageProps[PR_RCVD_REPRESENTING_ENTRYID]);
1939
				if (!empty($delegatorStore['store'])) {
1940
					$store = $delegatorStore['store'];
1941
				}
1942
			}
1943
		}
1944
1945
		// If the store is a public folder, the calendar folder is the PARENT_ENTRYID of the calendar item
1946
		$provider = mapi_getprops($store, [PR_MDB_PROVIDER]);
1947
		if (isset($provider[PR_MDB_PROVIDER]) && $provider[PR_MDB_PROVIDER] === ZARAFA_STORE_PUBLIC_GUID) {
1948
			$entryid = mapi_getprops($this->message, [PR_PARENT_ENTRYID]);
1949
			$entryid = $entryid[PR_PARENT_ENTRYID];
1950
		}
1951
		else {
1952
			$entryid = $this->getDefaultFolderEntryID(PR_IPM_APPOINTMENT_ENTRYID, $store);
1953
			if ($entryid === false) {
1954
				$entryid = $this->getBaseEntryID(PR_IPM_APPOINTMENT_ENTRYID, $store);
1955
			}
1956
1957
			if ($entryid === false) {
1958
				return false;
1959
			}
1960
		}
1961
1962
		return $this->checkFolderWriteAccess($entryid, $store);
1963
	}
1964
1965
	/**
1966
	 * Function will resolve the user and open its store.
1967
	 *
1968
	 * @param string $ownerentryid the entryid of the user
1969
	 *
1970
	 * @return resource store of the user
1971
	 */
1972
	public function openCustomUserStore($ownerentryid) {
1973
		$ab = mapi_openaddressbook($this->session);
1974
1975
		try {
1976
			$mailuser = mapi_ab_openentry($ab, $ownerentryid);
1977
			if (!$mailuser) {
1978
				error_log(sprintf("Unable to open ab entry: 0x%08X", mapi_last_hresult()));
1979
				return;
1980
			}
1981
		}
1982
		catch (MAPIException $e) {
1983
			return;
1984
		}
1985
1986
		$mailuserprops = mapi_getprops($mailuser, [PR_EMAIL_ADDRESS]);
1987
		$storeid = mapi_msgstore_createentryid($this->store, $mailuserprops[PR_EMAIL_ADDRESS]);
1988
1989
		return mapi_openmsgstore($this->session, $storeid);
0 ignored issues
show
Bug Best Practice introduced by
The expression return mapi_openmsgstore...his->session, $storeid) returns the type resource which is incompatible with the documented return type resource.
Loading history...
1990
	}
1991
1992
	/**
1993
	 * Function which sends response to organizer when attendee accepts, declines or proposes new time to a received meeting request.
1994
	 *
1995
	 * @param int   $status              response status of attendee
1996
	 * @param array $proposeNewTimeProps properties of attendee's proposal
1997
	 * @param mixed $body
1998
	 * @param mixed $store
1999
	 * @param mixed $basedate            date of occurrence which attendee has responded
2000
	 * @param mixed $calFolder
2001
	 */
2002
	public function createResponse($status, $proposeNewTimeProps, $body, $store, $basedate, $calFolder): void {
2003
		$messageprops = mapi_getprops($this->message, [
2004
			PR_SENT_REPRESENTING_ENTRYID,
2005
			PR_SENT_REPRESENTING_EMAIL_ADDRESS,
2006
			PR_SENT_REPRESENTING_ADDRTYPE,
2007
			PR_SENT_REPRESENTING_NAME,
2008
			PR_SENT_REPRESENTING_SEARCH_KEY,
2009
			$this->proptags['goid'],
2010
			$this->proptags['goid2'],
2011
			$this->proptags['location'],
2012
			$this->proptags['startdate'],
2013
			$this->proptags['duedate'],
2014
			$this->proptags['recurring'],
2015
			$this->proptags['recurring_pattern'],
2016
			$this->proptags['recurrence_data'],
2017
			$this->proptags['timezone_data'],
2018
			$this->proptags['timezone'],
2019
			$this->proptags['updatecounter'],
2020
			PR_SUBJECT,
2021
			PR_MESSAGE_CLASS,
2022
			PR_OWNER_APPT_ID,
2023
			$this->proptags['is_exception'],
2024
		]);
2025
2026
		$props = [];
2027
2028
		if ($basedate !== false && !$this->isMeetingRequest($messageprops[PR_MESSAGE_CLASS])) {
2029
			// we are creating response from a recurring calendar item object
2030
			// We found basedate,so opened occurrence and get properties.
2031
			$recurr = new Recurrence($store, $this->message);
2032
			$exception = $recurr->getExceptionAttachment($basedate);
2033
2034
			if ($exception) {
2035
				// Exception found, Now retrieve properties
2036
				$imessage = mapi_attach_openobj($exception, 0);
2037
				$imsgprops = mapi_getprops($imessage);
2038
2039
				// If location is provided, copy it to the response
2040
				if (isset($imsgprops[$this->proptags['location']])) {
2041
					$messageprops[$this->proptags['location']] = $imsgprops[$this->proptags['location']];
2042
				}
2043
2044
				// Update $messageprops with timings of occurrence
2045
				$messageprops[$this->proptags['startdate']] = $imsgprops[$this->proptags['startdate']];
2046
				$messageprops[$this->proptags['duedate']] = $imsgprops[$this->proptags['duedate']];
2047
2048
				// Meeting related properties
2049
				$props[$this->proptags['meetingstatus']] = $imsgprops[$this->proptags['meetingstatus']];
2050
				$props[$this->proptags['responsestatus']] = $imsgprops[$this->proptags['responsestatus']];
2051
				$props[PR_SUBJECT] = $imsgprops[PR_SUBJECT];
2052
			}
2053
			else {
2054
				// Exceptions is deleted.
2055
				// Update $messageprops with timings of occurrence
2056
				$messageprops[$this->proptags['startdate']] = $recurr->getOccurrenceStart($basedate);
2057
				$messageprops[$this->proptags['duedate']] = $recurr->getOccurrenceEnd($basedate);
2058
2059
				$props[$this->proptags['meetingstatus']] = olNonMeeting;
2060
				$props[$this->proptags['responsestatus']] = olResponseNone;
2061
			}
2062
2063
			$props[$this->proptags['recurring']] = false;
2064
			$props[$this->proptags['is_exception']] = true;
2065
		}
2066
		else {
2067
			// we are creating a response from meeting request mail (it could be recurring or non-recurring)
2068
			// Send all recurrence info in response, if this is a recurrence meeting.
2069
			$isRecurring = isset($messageprops[$this->proptags['recurring']]) && $messageprops[$this->proptags['recurring']];
2070
			$isException = isset($messageprops[$this->proptags['is_exception']]) && $messageprops[$this->proptags['is_exception']];
2071
			if ($isRecurring || $isException) {
2072
				if ($isRecurring) {
2073
					$props[$this->proptags['recurring']] = $messageprops[$this->proptags['recurring']];
2074
				}
2075
				if ($isException) {
2076
					$props[$this->proptags['is_exception']] = $messageprops[$this->proptags['is_exception']];
2077
				}
2078
				$calendaritems = $this->findCalendarItems($messageprops[$this->proptags['goid2']], $calFolder);
2079
2080
				$calendaritem = mapi_msgstore_openentry($store, $calendaritems[0]);
2081
				$recurr = new Recurrence($store, $calendaritem);
2082
			}
2083
		}
2084
2085
		// we are sending a response for recurring meeting request (or exception), so set some required properties
2086
		if (isset($recurr) && $recurr) {
2087
			if (!empty($messageprops[$this->proptags['recurring_pattern']])) {
2088
				$props[$this->proptags['recurring_pattern']] = $messageprops[$this->proptags['recurring_pattern']];
2089
			}
2090
2091
			if (!empty($messageprops[$this->proptags['recurrence_data']])) {
2092
				$props[$this->proptags['recurrence_data']] = $messageprops[$this->proptags['recurrence_data']];
2093
			}
2094
2095
			$props[$this->proptags['timezone_data']] = $messageprops[$this->proptags['timezone_data']];
2096
			$props[$this->proptags['timezone']] = $messageprops[$this->proptags['timezone']];
2097
2098
			$this->generateRecurDates($recurr, $messageprops, $props);
2099
		}
2100
2101
		// Create a response message
2102
		$recip = [];
2103
		$recip[PR_ENTRYID] = $messageprops[PR_SENT_REPRESENTING_ENTRYID];
2104
		$recip[PR_EMAIL_ADDRESS] = $messageprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS];
2105
		$recip[PR_ADDRTYPE] = $messageprops[PR_SENT_REPRESENTING_ADDRTYPE];
2106
		$recip[PR_DISPLAY_NAME] = $messageprops[PR_SENT_REPRESENTING_NAME];
2107
		$recip[PR_RECIPIENT_TYPE] = MAPI_TO;
2108
		$recip[PR_SEARCH_KEY] = $messageprops[PR_SENT_REPRESENTING_SEARCH_KEY];
2109
2110
		$subjectprefix = '';
2111
		$classpostfix = '';
2112
2113
		switch ($status) {
2114
			case olResponseAccepted:
2115
				$classpostfix = 'Pos';
2116
				$subjectprefix = dgettext('zarafa', 'Accepted');
2117
				break;
2118
2119
			case olResponseDeclined:
2120
				$classpostfix = 'Neg';
2121
				$subjectprefix = dgettext('zarafa', 'Declined');
2122
				break;
2123
2124
			case olResponseTentative:
2125
				$classpostfix = 'Tent';
2126
				$subjectprefix = dgettext('zarafa', 'Tentatively accepted');
2127
				break;
2128
		}
2129
2130
		if (!empty($proposeNewTimeProps)) {
2131
			// if attendee has proposed new time then change subject prefix
2132
			$subjectprefix = dgettext('zarafa', 'New Time Proposed');
2133
		}
2134
2135
		$props[PR_SUBJECT] = $subjectprefix . ': ' . $messageprops[PR_SUBJECT];
2136
2137
		$props[PR_MESSAGE_CLASS] = 'IPM.Schedule.Meeting.Resp.' . $classpostfix;
2138
		if (isset($messageprops[PR_OWNER_APPT_ID])) {
2139
			$props[PR_OWNER_APPT_ID] = $messageprops[PR_OWNER_APPT_ID];
2140
		}
2141
2142
		// Set GlobalId AND CleanGlobalId, if exception then also set basedate into GlobalId(0x3).
2143
		$props[$this->proptags['goid']] = $this->setBasedateInGlobalID($messageprops[$this->proptags['goid2']], $basedate);
2144
		$props[$this->proptags['goid2']] = $messageprops[$this->proptags['goid2']];
2145
		$props[$this->proptags['updatecounter']] = isset($messageprops[$this->proptags['updatecounter']]) ? $messageprops[$this->proptags['updatecounter']] : 0;
2146
2147
		if (!empty($proposeNewTimeProps)) {
2148
			// merge proposal properties to message properties which will be sent to organizer
2149
			$props = $proposeNewTimeProps + $props;
2150
		}
2151
2152
		// Set body message in Appointment
2153
		if (isset($body)) {
2154
			$props[PR_BODY] = $this->getMeetingTimeInfo() ? $this->getMeetingTimeInfo() : $body;
2155
		}
2156
2157
		// PR_START_DATE/PR_END_DATE is used in the UI in Outlook on the response message
2158
		$props[PR_START_DATE] = $messageprops[$this->proptags['startdate']];
2159
		$props[PR_END_DATE] = $messageprops[$this->proptags['duedate']];
2160
2161
		// Set startdate and duedate in response mail.
2162
		$props[$this->proptags['startdate']] = $messageprops[$this->proptags['startdate']];
2163
		$props[$this->proptags['duedate']] = $messageprops[$this->proptags['duedate']];
2164
2165
		// responselocation is used in the UI in Outlook on the response message
2166
		if (isset($messageprops[$this->proptags['location']])) {
2167
			$props[$this->proptags['responselocation']] = $messageprops[$this->proptags['location']];
2168
			$props[$this->proptags['location']] = $messageprops[$this->proptags['location']];
2169
		}
2170
2171
		$message = $this->createOutgoingMessage($store);
2172
2173
		mapi_setprops($message, $props);
2174
		mapi_message_modifyrecipients($message, MODRECIP_ADD, [$recip]);
2175
		mapi_savechanges($message);
2176
		mapi_message_submitmessage($message);
2177
	}
2178
2179
	/**
2180
	 * Function which finds items in calendar based on globalId and cleanGlobalId.
2181
	 *
2182
	 * @param string $goid             GlobalID(0x3) of item
2183
	 * @param mixed  $calendar         MAPI_folder of user (optional)
2184
	 * @param bool   $useCleanGlobalId if true then search should be performed on cleanGlobalId(0x23) else globalId(0x3)
2185
	 *
2186
	 * @return mixed
2187
	 */
2188
	public function findCalendarItems($goid, $calendar = false, $useCleanGlobalId = false) {
2189
		if ($calendar === false) {
2190
			// Open the Calendar
2191
			$calendar = $this->openDefaultCalendar();
2192
		}
2193
2194
		// Find the item by restricting all items to the correct ID
2195
		$restrict = [
2196
			RES_PROPERTY,
2197
			[
2198
				RELOP => RELOP_EQ,
2199
				ULPROPTAG => ($useCleanGlobalId === true ? $this->proptags['goid2'] : $this->proptags['goid']),
2200
				VALUE => $goid,
2201
			],
2202
		];
2203
2204
		$calendarcontents = mapi_folder_getcontentstable($calendar);
2205
2206
		$rows = mapi_table_queryallrows($calendarcontents, [PR_ENTRYID], $restrict);
2207
2208
		if (empty($rows)) {
2209
			return;
2210
		}
2211
2212
		$calendaritems = [];
2213
2214
		// In principle, there should only be one row, but we'll handle them all just in case
2215
		foreach ($rows as $row) {
2216
			$calendaritems[] = $row[PR_ENTRYID];
2217
		}
2218
2219
		return $calendaritems;
2220
	}
2221
2222
	// Returns TRUE if both entryid's are equal. Equality is defined by both entryid's pointing at the
2223
	// same SMTP address when converted to SMTP
2224
	public function compareABEntryIDs($entryid1, $entryid2): bool {
2225
		// If the session was not passed, just do a 'normal' compare.
2226
		if (!$this->session) {
2227
			return $entryid1 == $entryid2;
2228
		}
2229
2230
		$smtp1 = $this->getSMTPAddress($entryid1);
2231
		$smtp2 = $this->getSMTPAddress($entryid2);
2232
2233
		if ($smtp1 == $smtp2) {
2234
			return true;
2235
		}
2236
2237
		return false;
2238
	}
2239
2240
	// Gets the SMTP address of the passed addressbook entryid
2241
	public function getSMTPAddress($entryid) {
2242
		if (!$this->session) {
2243
			return false;
2244
		}
2245
2246
		try {
2247
			$ab = mapi_openaddressbook($this->session);
2248
			$abitem = mapi_ab_openentry($ab, $entryid);
2249
2250
			if (!$abitem) {
2251
				return '';
2252
			}
2253
		}
2254
		catch (MAPIException $e) {
2255
			return '';
2256
		}
2257
2258
		$props = mapi_getprops($abitem, [PR_ADDRTYPE, PR_EMAIL_ADDRESS, PR_SMTP_ADDRESS]);
2259
2260
		if ($props[PR_ADDRTYPE] == 'SMTP') {
2261
			return $props[PR_EMAIL_ADDRESS];
2262
		}
2263
2264
		return $props[PR_SMTP_ADDRESS];
2265
	}
2266
2267
	/**
2268
	 * Gets the properties associated with the owner of the passed store:
2269
	 * PR_DISPLAY_NAME, PR_EMAIL_ADDRESS, PR_ADDRTYPE, PR_ENTRYID, PR_SEARCH_KEY.
2270
	 *
2271
	 * @param mixed $store                  message store
2272
	 * @param bool  $fallbackToLoggedInUser If true then return properties of logged in user instead of mailbox owner.
2273
	 *                                      Not used when passed store is public store.
2274
	 *                                      For public store we are always returning logged in user's info.
2275
	 *
2276
	 * @return array|false properties of logged in user in an array in sequence of display_name, email address, address type, entryid and search key
2277
	 *
2278
	 * @psalm-return false|list{mixed, mixed, mixed, mixed, mixed}
2279
	 */
2280
	public function getOwnerAddress($store, $fallbackToLoggedInUser = true) {
2281
		if (!$this->session) {
2282
			return false;
2283
		}
2284
2285
		$storeProps = mapi_getprops($store, [PR_MAILBOX_OWNER_ENTRYID, PR_USER_ENTRYID]);
2286
2287
		$ownerEntryId = false;
2288
		if (isset($storeProps[PR_USER_ENTRYID]) && $storeProps[PR_USER_ENTRYID]) {
2289
			$ownerEntryId = $storeProps[PR_USER_ENTRYID];
2290
		}
2291
2292
		if (isset($storeProps[PR_MAILBOX_OWNER_ENTRYID]) && $storeProps[PR_MAILBOX_OWNER_ENTRYID] && !$fallbackToLoggedInUser) {
2293
			$ownerEntryId = $storeProps[PR_MAILBOX_OWNER_ENTRYID];
2294
		}
2295
2296
		if ($ownerEntryId) {
2297
			$ab = mapi_openaddressbook($this->session);
2298
2299
			$zarafaUser = mapi_ab_openentry($ab, $ownerEntryId);
2300
			if (!$zarafaUser) {
2301
				return false;
2302
			}
2303
2304
			$ownerProps = mapi_getprops($zarafaUser, [PR_ADDRTYPE, PR_DISPLAY_NAME, PR_EMAIL_ADDRESS, PR_SEARCH_KEY]);
2305
2306
			$addrType = $ownerProps[PR_ADDRTYPE];
2307
			$name = $ownerProps[PR_DISPLAY_NAME];
2308
			$emailAddr = $ownerProps[PR_EMAIL_ADDRESS];
2309
			$searchKey = $ownerProps[PR_SEARCH_KEY];
2310
			$entryId = $ownerEntryId;
2311
2312
			return [$name, $emailAddr, $addrType, $entryId, $searchKey];
2313
		}
2314
2315
		return false;
2316
	}
2317
2318
	// Opens this session's default message store
2319
	public function openDefaultStore() {
2320
		$entryid = '';
2321
2322
		$storestable = mapi_getmsgstorestable($this->session);
2323
		$rows = mapi_table_queryallrows($storestable, [PR_ENTRYID, PR_DEFAULT_STORE]);
2324
2325
		foreach ($rows as $row) {
2326
			if (isset($row[PR_DEFAULT_STORE]) && $row[PR_DEFAULT_STORE]) {
2327
				$entryid = $row[PR_ENTRYID];
2328
				break;
2329
			}
2330
		}
2331
2332
		if (!$entryid) {
2333
			return false;
2334
		}
2335
2336
		return mapi_openmsgstore($this->session, $entryid);
2337
	}
2338
2339
	/**
2340
	 * Function which adds organizer to recipient list which is passed.
2341
	 * This function also checks if it has organizer.
2342
	 *
2343
	 * @param array $messageProps message properties
2344
	 * @param array $recipients   recipients list of message
2345
	 * @param bool  $isException  true if we are processing recipient of exception
2346
	 */
2347
	public function addOrganizer($messageProps, &$recipients, $isException = false): void {
2348
		$hasOrganizer = false;
2349
		// Check if meeting already has an organizer.
2350
		foreach ($recipients as $key => $recipient) {
2351
			if (isset($recipient[PR_RECIPIENT_FLAGS]) && $recipient[PR_RECIPIENT_FLAGS] == (recipSendable | recipOrganizer)) {
2352
				$hasOrganizer = true;
2353
			}
2354
			elseif ($isException && !isset($recipient[PR_RECIPIENT_FLAGS])) {
2355
				// Recipients for an occurrence
2356
				$recipients[$key][PR_RECIPIENT_FLAGS] = recipSendable | recipExceptionalResponse;
2357
			}
2358
		}
2359
2360
		if (!$hasOrganizer) {
2361
			// Create organizer.
2362
			$organizer = [];
2363
			$organizer[PR_ENTRYID] = $organizer[PR_RECIPIENT_ENTRYID] = $messageProps[PR_SENT_REPRESENTING_ENTRYID];
2364
			$organizer[PR_DISPLAY_NAME] = $messageProps[PR_SENT_REPRESENTING_NAME];
2365
			$organizer[PR_EMAIL_ADDRESS] = $messageProps[PR_SENT_REPRESENTING_EMAIL_ADDRESS];
2366
			$organizer[PR_RECIPIENT_TYPE] = MAPI_TO;
2367
			$organizer[PR_RECIPIENT_DISPLAY_NAME] = $messageProps[PR_SENT_REPRESENTING_NAME];
2368
			$organizer[PR_ADDRTYPE] = empty($messageProps[PR_SENT_REPRESENTING_ADDRTYPE]) ? 'SMTP' : $messageProps[PR_SENT_REPRESENTING_ADDRTYPE];
2369
			$organizer[PR_RECIPIENT_TRACKSTATUS] = olRecipientTrackStatusNone;
2370
			$organizer[PR_RECIPIENT_FLAGS] = recipSendable | recipOrganizer;
2371
			$organizer[PR_SEARCH_KEY] = $messageProps[PR_SENT_REPRESENTING_SEARCH_KEY];
2372
			$organizer[PR_SMTP_ADDRESS] = $messageProps[PR_SENT_REPRESENTING_SMTP_ADDRESS] ?? $messageProps[PR_SENT_REPRESENTING_EMAIL_ADDRESS];
2373
2374
			// Add organizer to recipients list.
2375
			array_unshift($recipients, $organizer);
2376
		}
2377
	}
2378
2379
	/**
2380
	 * Function which removes an exception/occurrence from recurrencing meeting
2381
	 * when a meeting cancellation of an occurrence is processed.
2382
	 *
2383
	 * @param mixed    $basedate basedate of an occurrence
2384
	 * @param mixed    $message  recurring item from which occurrence has to be deleted
2385
	 * @param resource $store    MAPI_MSG_Store which contains the item
2386
	 */
2387
	public function doRemoveExceptionFromCalendar($basedate, $message, $store): void {
2388
		$recurr = new Recurrence($store, $message);
2389
		$recurr->createException([], $basedate, true);
2390
		mapi_savechanges($message);
2391
	}
2392
2393
	/**
2394
	 * Function which returns basedate of an changed occurrence from globalID of meeting request.
2395
	 *
2396
	 * @param string $goid globalID
2397
	 *
2398
	 * @return false|int true if basedate is found else false it not found
2399
	 */
2400
	public function getBasedateFromGlobalID($goid) {
2401
		$hexguid = bin2hex($goid);
2402
		$hexbase = substr($hexguid, 32, 8);
2403
		$day = (int) hexdec(substr($hexbase, 6, 2));
2404
		$month = (int) hexdec(substr($hexbase, 4, 2));
2405
		$year = (int) hexdec(substr($hexbase, 0, 4));
2406
2407
		if ($day && $month && $year) {
2408
			return gmmktime(0, 0, 0, $month, $day, $year);
2409
		}
2410
2411
		return false;
2412
	}
2413
2414
	/**
2415
	 * Function which sets basedate in globalID of changed occurrence which is to be sent.
2416
	 *
2417
	 * @param string $goid     globalID
2418
	 * @param mixed  $basedate of changed occurrence
2419
	 *
2420
	 * @return false|string globalID with basedate in it
2421
	 */
2422
	public function setBasedateInGlobalID($goid, $basedate = false) {
2423
		$hexguid = bin2hex($goid);
2424
		$year = $basedate ? sprintf('%04s', dechex((int) gmdate('Y', $basedate))) : '0000';
2425
		$month = $basedate ? sprintf('%02s', dechex((int) gmdate('m', $basedate))) : '00';
2426
		$day = $basedate ? sprintf('%02s', dechex((int) gmdate('d', $basedate))) : '00';
2427
2428
		return hex2bin(strtoupper(substr($hexguid, 0, 32) . $year . $month . $day . substr($hexguid, 40)));
2429
	}
2430
2431
	/**
2432
	 * Function which replaces attachments with copy_from in copy_to.
2433
	 *
2434
	 * @param mixed $copyFrom       MAPI_message from which attachments are to be copied
2435
	 * @param mixed $copyTo         MAPI_message to which attachment are to be copied
2436
	 * @param bool  $copyExceptions if true then all exceptions should also be sent as attachments
2437
	 */
2438
	public function replaceAttachments($copyFrom, $copyTo, $copyExceptions = true): void {
2439
		/* remove all old attachments */
2440
		$attachmentTableTo = mapi_message_getattachmenttable($copyTo);
2441
		if ($attachmentTableTo) {
2442
			$attachments = mapi_table_queryallrows($attachmentTableTo, [PR_ATTACH_NUM, PR_ATTACH_METHOD, PR_EXCEPTION_STARTTIME]);
2443
2444
			foreach ($attachments as $attachProps) {
2445
				/* remove exceptions too? */
2446
				if (!$copyExceptions && $attachProps[PR_ATTACH_METHOD] == ATTACH_EMBEDDED_MSG && isset($attachProps[PR_EXCEPTION_STARTTIME])) {
2447
					continue;
2448
				}
2449
				mapi_message_deleteattach($copyTo, $attachProps[PR_ATTACH_NUM]);
2450
			}
2451
		}
2452
2453
		/* copy new attachments */
2454
		$attachmentTableFrom = mapi_message_getattachmenttable($copyFrom);
2455
		if ($attachmentTableFrom) {
2456
			$attachments = mapi_table_queryallrows($attachmentTableFrom, [PR_ATTACH_NUM, PR_ATTACH_METHOD, PR_EXCEPTION_STARTTIME]);
2457
2458
			foreach ($attachments as $attachProps) {
2459
				if (!$copyExceptions && $attachProps[PR_ATTACH_METHOD] == ATTACH_EMBEDDED_MSG && isset($attachProps[PR_EXCEPTION_STARTTIME])) {
2460
					continue;
2461
				}
2462
2463
				$attachOld = mapi_message_openattach($copyFrom, (int) $attachProps[PR_ATTACH_NUM]);
2464
				$attachNewResourceMsg = mapi_message_createattach($copyTo);
2465
				mapi_copyto($attachOld, [], [], $attachNewResourceMsg, 0);
2466
				mapi_savechanges($attachNewResourceMsg);
2467
			}
2468
		}
2469
	}
2470
2471
	/**
2472
	 * Function which replaces recipients in copyTo with recipients from copyFrom.
2473
	 *
2474
	 * @param mixed $copyFrom   MAPI_message from which recipients are to be copied
2475
	 * @param mixed $copyTo     MAPI_message to which recipients are to be copied
2476
	 * @param bool  $isDelegate indicates whether delegate is processing
2477
	 *                          so don't copy delegate information to recipient table
2478
	 */
2479
	public function replaceRecipients($copyFrom, $copyTo, $isDelegate = false): void {
2480
		$recipientTable = mapi_message_getrecipienttable($copyFrom);
2481
2482
		// If delegate, then do not add the delegate in recipients
2483
		if ($isDelegate) {
2484
			$delegate = mapi_getprops($copyFrom, [PR_RECEIVED_BY_EMAIL_ADDRESS]);
2485
			$res = [
2486
				RES_PROPERTY,
2487
				[
2488
					RELOP => RELOP_NE,
2489
					ULPROPTAG => PR_EMAIL_ADDRESS,
2490
					VALUE => [PR_EMAIL_ADDRESS => $delegate[PR_RECEIVED_BY_EMAIL_ADDRESS]],
2491
				],
2492
			];
2493
			$recipients = mapi_table_queryallrows($recipientTable, $this->recipprops, $res);
2494
		}
2495
		else {
2496
			$recipients = mapi_table_queryallrows($recipientTable, $this->recipprops);
2497
		}
2498
2499
		$copyToRecipientTable = mapi_message_getrecipienttable($copyTo);
2500
		$copyToRecipientRows = mapi_table_queryallrows($copyToRecipientTable, [PR_ROWID]);
2501
2502
		mapi_message_modifyrecipients($copyTo, MODRECIP_REMOVE, $copyToRecipientRows);
2503
		mapi_message_modifyrecipients($copyTo, MODRECIP_ADD, $recipients);
2504
	}
2505
2506
	/**
2507
	 * Function creates meeting item in resource's calendar.
2508
	 *
2509
	 * @param resource $message  MAPI_message which is to create in resource's calendar
2510
	 * @param bool     $cancel   cancel meeting
2511
	 * @param mixed    $prefix   prefix for subject of meeting
2512
	 * @param mixed    $basedate
2513
	 *
2514
	 * @return (mixed|resource)[][]
2515
	 *
2516
	 * @psalm-return list<array{store: resource, folder: mixed, msg: mixed}>
2517
	 */
2518
	public function bookResources($message, $cancel, $prefix, $basedate = false): array {
2519
		if (!$this->enableDirectBooking) {
2520
			return [];
2521
		}
2522
2523
		// Get the properties of the message
2524
		$messageprops = mapi_getprops($message);
2525
2526
		$calFolder = '';
2527
2528
		if ($basedate) {
2529
			$recurrItemProps = mapi_getprops($this->message, [$this->proptags['goid'], $this->proptags['goid2'], $this->proptags['timezone_data'], $this->proptags['timezone'], PR_OWNER_APPT_ID]);
2530
2531
			$messageprops[$this->proptags['goid']] = $this->setBasedateInGlobalID($recurrItemProps[$this->proptags['goid']], $basedate);
2532
			$messageprops[$this->proptags['goid2']] = $recurrItemProps[$this->proptags['goid2']];
2533
2534
			// Delete properties which are not needed.
2535
			$deleteProps = [$this->proptags['basedate'], PR_DISPLAY_NAME, PR_ATTACHMENT_FLAGS, PR_ATTACHMENT_HIDDEN, PR_ATTACHMENT_LINKID, PR_ATTACH_FLAGS, PR_ATTACH_METHOD];
2536
			foreach ($deleteProps as $propID) {
2537
				if (isset($messageprops[$propID])) {
2538
					unset($messageprops[$propID]);
2539
				}
2540
			}
2541
2542
			if (isset($messageprops[$this->proptags['recurring']])) {
2543
				$messageprops[$this->proptags['recurring']] = false;
2544
			}
2545
2546
			// Set Outlook properties
2547
			$messageprops[$this->proptags['clipstart']] = $messageprops[$this->proptags['startdate']];
2548
			$messageprops[$this->proptags['clipend']] = $messageprops[$this->proptags['duedate']];
2549
			$messageprops[$this->proptags['timezone_data']] = $recurrItemProps[$this->proptags['timezone_data']];
2550
			$messageprops[$this->proptags['timezone']] = $recurrItemProps[$this->proptags['timezone']];
2551
			$messageprops[$this->proptags['attendee_critical_change']] = time();
2552
			$messageprops[$this->proptags['owner_critical_change']] = time();
2553
		}
2554
2555
		// Get resource recipients
2556
		$getResourcesRestriction = [
2557
			RES_PROPERTY,
2558
			[
2559
				RELOP => RELOP_EQ,	// Equals recipient type 3: Resource
2560
				ULPROPTAG => PR_RECIPIENT_TYPE,
2561
				VALUE => [PR_RECIPIENT_TYPE => MAPI_BCC],
2562
			],
2563
		];
2564
		$recipienttable = mapi_message_getrecipienttable($message);
2565
		$resourceRecipients = mapi_table_queryallrows($recipienttable, $this->recipprops, $getResourcesRestriction);
2566
2567
		$this->errorSetResource = false;
2568
		$resourceRecipData = [];
2569
2570
		// Put appointment into store resource users
2571
		$i = 0;
2572
		$len = count($resourceRecipients);
2573
		while (!$this->errorSetResource && $i < $len) {
2574
			$userStore = $this->openCustomUserStore($resourceRecipients[$i][PR_ENTRYID]);
2575
2576
			// Open root folder
2577
			$userRoot = mapi_msgstore_openentry($userStore);
2578
2579
			// Get calendar entryID
2580
			$userRootProps = mapi_getprops($userRoot, [PR_STORE_ENTRYID, PR_IPM_APPOINTMENT_ENTRYID, PR_FREEBUSY_ENTRYIDS]);
2581
2582
			// Open Calendar folder
2583
			$accessToFolder = false;
2584
2585
			try {
2586
				// @FIXME this checks delegate has access to resource's calendar folder
2587
				// but it should use boss' credentials
2588
2589
				$accessToFolder = $this->checkCalendarWriteAccess($this->store);
2590
				if ($accessToFolder) {
2591
					$calFolder = mapi_msgstore_openentry($userStore, $userRootProps[PR_IPM_APPOINTMENT_ENTRYID]);
2592
				}
2593
			}
2594
			catch (MAPIException $e) {
2595
				$e->setHandled();
2596
				$this->errorSetResource = 1; // No access
2597
			}
2598
2599
			if ($accessToFolder) {
2600
				/**
2601
				 * Get the LocalFreebusy message that contains the properties that
2602
				 * are set to accept or decline resource meeting requests.
2603
				 */
2604
				$localFreebusyMsg = FreeBusy::getLocalFreeBusyMessage($userStore);
2605
				if ($localFreebusyMsg) {
2606
					$props = mapi_getprops($localFreebusyMsg, [PR_SCHDINFO_AUTO_ACCEPT_APPTS, PR_SCHDINFO_DISALLOW_RECURRING_APPTS, PR_SCHDINFO_DISALLOW_OVERLAPPING_APPTS]);
2607
2608
					$acceptMeetingRequests = isset($props[PR_SCHDINFO_AUTO_ACCEPT_APPTS]) ? $props[PR_SCHDINFO_AUTO_ACCEPT_APPTS] : false;
2609
					$declineRecurringMeetingRequests = isset($props[PR_SCHDINFO_DISALLOW_RECURRING_APPTS]) ? $props[PR_SCHDINFO_DISALLOW_RECURRING_APPTS] : false;
2610
					$declineConflictingMeetingRequests = isset($props[PR_SCHDINFO_DISALLOW_OVERLAPPING_APPTS]) ? $props[PR_SCHDINFO_DISALLOW_OVERLAPPING_APPTS] : false;
2611
2612
					if (!$acceptMeetingRequests) {
2613
						/*
2614
						 * When a resource has not been set to automatically accept meeting requests,
2615
						 * the meeting request has to be sent to him rather than being put directly into
2616
						 * his calendar. No error should be returned.
2617
						 */
2618
						// $errorSetResource = 2;
2619
						$this->nonAcceptingResources[] = $resourceRecipients[$i];
2620
					}
2621
					else {
2622
						if ($declineRecurringMeetingRequests && !$cancel) {
2623
							// Check if appointment is recurring
2624
							if ($messageprops[$this->proptags['recurring']]) {
2625
								$this->errorSetResource = 3;
2626
							}
2627
						}
2628
						if ($declineConflictingMeetingRequests && !$cancel) {
2629
							// Check for conflicting items
2630
							if ($calFolder && $this->isMeetingConflicting($message, $userStore, $calFolder)) {
2631
								$this->errorSetResource = 4; // Conflict
2632
							}
2633
						}
2634
					}
2635
				}
2636
			}
2637
2638
			if (!$this->errorSetResource && $accessToFolder) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->errorSetResource of type false|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
2639
				/**
2640
				 * First search on GlobalID(0x3)
2641
				 * If (recurring and occurrence) If Resource was booked for only this occurrence then Resource should have only this occurrence in Calendar and not whole series.
2642
				 * If (normal meeting) then GlobalID(0x3) and CleanGlobalID(0x23) are same, so doesn't matter if search is based on GlobalID.
2643
				 */
2644
				$rows = $this->findCalendarItems($messageprops[$this->proptags['goid']], $calFolder);
2645
2646
				/*
2647
				 * If no entry is found then
2648
				 * 1) Resource doesn't have meeting in Calendar. Seriously!!
2649
				 * OR
2650
				 * 2) We were looking for occurrence item but Resource has whole series
2651
				 */
2652
				if (empty($rows)) {
2653
					/**
2654
					 * Now search on CleanGlobalID(0x23) WHY???
2655
					 * Because we are looking recurring item.
2656
					 *
2657
					 * Possible results of this search
2658
					 * 1) If Resource was booked for more than one occurrences then this search will return all those occurrence because search is perform on CleanGlobalID
2659
					 * 2) If Resource was booked for whole series then it should return series.
2660
					 */
2661
					$rows = $this->findCalendarItems($messageprops[$this->proptags['goid2']], $calFolder, true);
2662
2663
					$newResourceMsg = false;
2664
					if (!empty($rows)) {
2665
						// Since we are looking for recurring item, open every result and check for 'recurring' property.
2666
						foreach ($rows as $row) {
2667
							$ResourceMsg = mapi_msgstore_openentry($userStore, $row);
2668
							$ResourceMsgProps = mapi_getprops($ResourceMsg, [$this->proptags['recurring']]);
2669
2670
							if (isset($ResourceMsgProps[$this->proptags['recurring']]) && $ResourceMsgProps[$this->proptags['recurring']]) {
2671
								$newResourceMsg = $ResourceMsg;
2672
								break;
2673
							}
2674
						}
2675
					}
2676
2677
					// Still no results found. I giveup, create new message.
2678
					if (!$newResourceMsg) {
2679
						$newResourceMsg = mapi_folder_createmessage($calFolder);
2680
					}
2681
				}
2682
				else {
2683
					$newResourceMsg = mapi_msgstore_openentry($userStore, $rows[0]);
2684
				}
2685
2686
				// Prefix the subject if needed
2687
				if ($prefix && isset($messageprops[PR_SUBJECT])) {
2688
					$messageprops[PR_SUBJECT] = $prefix . $messageprops[PR_SUBJECT];
2689
				}
2690
2691
				// Set status to cancelled if needed
2692
				$messageprops[$this->proptags['busystatus']] = fbBusy; // The default status (Busy)
2693
				if ($cancel) {
2694
					$messageprops[$this->proptags['meetingstatus']] = olMeetingCanceled; // The meeting has been canceled
2695
					$messageprops[$this->proptags['busystatus']] = fbFree; // Free
2696
				}
2697
				else {
2698
					$messageprops[$this->proptags['meetingstatus']] = olMeetingReceived; // The recipient is receiving the request
2699
				}
2700
				$messageprops[$this->proptags['responsestatus']] = olResponseAccepted; // The resource automatically accepts the appointment
2701
2702
				$messageprops[PR_MESSAGE_CLASS] = 'IPM.Appointment';
2703
2704
				// Remove the PR_ICON_INDEX as it is not needed in the sent message.
2705
				$messageprops[PR_ICON_INDEX] = null;
2706
				$messageprops[PR_RESPONSE_REQUESTED] = true;
2707
2708
				// get the store of organizer, in case of delegates it will be delegate store
2709
				$defaultStore = $this->openDefaultStore();
2710
2711
				$storeProps = mapi_getprops($this->store, [PR_ENTRYID]);
2712
				$defaultStoreProps = mapi_getprops($defaultStore, [PR_ENTRYID]);
2713
2714
				// @FIXME use entryid comparison functions here
2715
				if ($storeProps[PR_ENTRYID] !== $defaultStoreProps[PR_ENTRYID]) {
2716
					// get delegate information
2717
					$addrInfo = $this->getOwnerAddress($defaultStore, false);
2718
2719
					if (!empty($addrInfo)) {
2720
						list($ownername, $owneremailaddr, $owneraddrtype, $ownerentryid, $ownersearchkey) = $addrInfo;
2721
2722
						$messageprops[PR_SENDER_EMAIL_ADDRESS] = $owneremailaddr;
2723
						$messageprops[PR_SENDER_NAME] = $ownername;
2724
						$messageprops[PR_SENDER_ADDRTYPE] = $owneraddrtype;
2725
						$messageprops[PR_SENDER_ENTRYID] = $ownerentryid;
2726
						$messageprops[PR_SENDER_SEARCH_KEY] = $ownersearchkey;
2727
					}
2728
2729
					// get delegator information
2730
					$addrInfo = $this->getOwnerAddress($this->store, false);
2731
2732
					if (!empty($addrInfo)) {
2733
						list($ownername, $owneremailaddr, $owneraddrtype, $ownerentryid, $ownersearchkey) = $addrInfo;
2734
2735
						$messageprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS] = $owneremailaddr;
2736
						$messageprops[PR_SENT_REPRESENTING_NAME] = $ownername;
2737
						$messageprops[PR_SENT_REPRESENTING_ADDRTYPE] = $owneraddrtype;
2738
						$messageprops[PR_SENT_REPRESENTING_ENTRYID] = $ownerentryid;
2739
						$messageprops[PR_SENT_REPRESENTING_SEARCH_KEY] = $ownersearchkey;
2740
					}
2741
				}
2742
				else {
2743
					// get organizer information
2744
					$addrInfo = $this->getOwnerAddress($this->store);
2745
2746
					if (!empty($addrInfo)) {
2747
						list($ownername, $owneremailaddr, $owneraddrtype, $ownerentryid, $ownersearchkey) = $addrInfo;
2748
2749
						$messageprops[PR_SENDER_EMAIL_ADDRESS] = $owneremailaddr;
2750
						$messageprops[PR_SENDER_NAME] = $ownername;
2751
						$messageprops[PR_SENDER_ADDRTYPE] = $owneraddrtype;
2752
						$messageprops[PR_SENDER_ENTRYID] = $ownerentryid;
2753
						$messageprops[PR_SENDER_SEARCH_KEY] = $ownersearchkey;
2754
2755
						$messageprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS] = $owneremailaddr;
2756
						$messageprops[PR_SENT_REPRESENTING_NAME] = $ownername;
2757
						$messageprops[PR_SENT_REPRESENTING_ADDRTYPE] = $owneraddrtype;
2758
						$messageprops[PR_SENT_REPRESENTING_ENTRYID] = $ownerentryid;
2759
						$messageprops[PR_SENT_REPRESENTING_SEARCH_KEY] = $ownersearchkey;
2760
					}
2761
				}
2762
2763
				$messageprops[$this->proptags['replytime']] = time();
2764
2765
				if ($basedate && isset($ResourceMsgProps[$this->proptags['recurring']]) && $ResourceMsgProps[$this->proptags['recurring']]) {
2766
					$recurr = new Recurrence($userStore, $newResourceMsg);
2767
2768
					// Copy recipients list
2769
					$reciptable = mapi_message_getrecipienttable($message);
2770
					$recips = mapi_table_queryallrows($reciptable, $this->recipprops);
2771
2772
					// add owner to recipient table
2773
					$this->addOrganizer($messageprops, $recips, true);
2774
2775
					// Update occurrence
2776
					if ($recurr->isException($basedate)) {
2777
						$recurr->modifyException($messageprops, $basedate, $recips);
2778
					}
2779
					else {
2780
						$recurr->createException($messageprops, $basedate, false, $recips);
2781
					}
2782
				}
2783
				else {
2784
					mapi_setprops($newResourceMsg, $messageprops);
2785
2786
					// Copy attachments
2787
					$this->replaceAttachments($message, $newResourceMsg);
2788
2789
					// Copy all recipients too
2790
					$this->replaceRecipients($message, $newResourceMsg);
2791
2792
					// Now add organizer also to recipient table
2793
					$recips = [];
2794
					$this->addOrganizer($messageprops, $recips);
2795
2796
					mapi_message_modifyrecipients($newResourceMsg, MODRECIP_ADD, $recips);
2797
				}
2798
2799
				mapi_savechanges($newResourceMsg);
2800
2801
				$resourceRecipData[] = [
2802
					'store' => $userStore,
2803
					'folder' => $calFolder,
2804
					'msg' => $newResourceMsg,
2805
				];
2806
				$this->includesResources = true;
2807
			}
2808
			else {
2809
				/*
2810
				 * If no other errors occurred and you have no access to the
2811
				 * folder of the resource, throw an error=1.
2812
				 */
2813
				if (!$this->errorSetResource) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->errorSetResource of type false|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
2814
					$this->errorSetResource = 1;
2815
				}
2816
2817
				for ($j = 0, $len = count($resourceRecipData); $j < $len; ++$j) {
2818
					// Get the EntryID
2819
					$props = mapi_message_getprops($resourceRecipData[$j]['msg']);
0 ignored issues
show
The function mapi_message_getprops was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

2819
					$props = /** @scrutinizer ignore-call */ mapi_message_getprops($resourceRecipData[$j]['msg']);
Loading history...
2820
2821
					mapi_folder_deletemessages($resourceRecipData[$j]['folder'], [$props[PR_ENTRYID]], DELETE_HARD_DELETE);
2822
				}
2823
				$this->recipientDisplayname = $resourceRecipients[$i][PR_DISPLAY_NAME];
2824
			}
2825
			++$i;
2826
		}
2827
2828
		$recipienttable = mapi_message_getrecipienttable($message);
2829
		$resourceRecipients = mapi_table_queryallrows($recipienttable, $this->recipprops);
2830
		if (!empty($resourceRecipients)) {
2831
			// Set Tracking status of resource recipients to olResponseAccepted (3)
2832
			for ($i = 0, $len = count($resourceRecipients); $i < $len; ++$i) {
2833
				if (isset($resourceRecipients[$i][PR_RECIPIENT_TYPE]) && $resourceRecipients[$i][PR_RECIPIENT_TYPE] == MAPI_BCC) {
2834
					$resourceRecipients[$i][PR_RECIPIENT_TRACKSTATUS] = olRecipientTrackStatusAccepted;
2835
					$resourceRecipients[$i][PR_RECIPIENT_TRACKSTATUS_TIME] = time();
2836
				}
2837
			}
2838
			mapi_message_modifyrecipients($message, MODRECIP_MODIFY, $resourceRecipients);
2839
		}
2840
2841
		return $resourceRecipData;
2842
	}
2843
2844
	/**
2845
	 * Function which save an exception into recurring item.
2846
	 *
2847
	 * @param resource $recurringItem  reference to MAPI_message of recurring item
2848
	 * @param resource $occurrenceItem reference to MAPI_message of occurrence
2849
	 * @param string   $basedate       basedate of occurrence
2850
	 * @param bool     $move           if true then occurrence item is deleted
2851
	 * @param bool     $tentative      true if user has tentatively accepted it or false if user has accepted it
2852
	 * @param bool     $userAction     true if user has manually responded to meeting request
2853
	 * @param resource $store          user store
2854
	 * @param bool     $isDelegate     true if delegate is processing this meeting request
2855
	 */
2856
	public function acceptException(&$recurringItem, &$occurrenceItem, $basedate, $move, $tentative, $userAction, $store, $isDelegate = false): void {
2857
		$recurr = new Recurrence($store, $recurringItem);
2858
2859
		// Copy properties from meeting request
2860
		$exception_props = mapi_getprops($occurrenceItem);
2861
2862
		// Copy recipients list
2863
		$reciptable = mapi_message_getrecipienttable($occurrenceItem);
2864
		// If delegate, then do not add the delegate in recipients
2865
		if ($isDelegate) {
2866
			$delegate = mapi_getprops($this->message, [PR_RECEIVED_BY_EMAIL_ADDRESS]);
2867
			$res = [
2868
				RES_PROPERTY,
2869
				[
2870
					RELOP => RELOP_NE,
2871
					ULPROPTAG => PR_EMAIL_ADDRESS,
2872
					VALUE => [PR_EMAIL_ADDRESS => $delegate[PR_RECEIVED_BY_EMAIL_ADDRESS]],
2873
				],
2874
			];
2875
			$recips = mapi_table_queryallrows($reciptable, $this->recipprops, $res);
2876
		}
2877
		else {
2878
			$recips = mapi_table_queryallrows($reciptable, $this->recipprops);
2879
		}
2880
2881
		// add owner to recipient table
2882
		$this->addOrganizer($exception_props, $recips, true);
2883
2884
		// add delegator to meetings
2885
		if ($isDelegate) {
2886
			$this->addDelegator($exception_props, $recips);
2887
		}
2888
2889
		$exception_props[$this->proptags['meetingstatus']] = olMeetingReceived;
2890
		$exception_props[$this->proptags['responsestatus']] = $userAction ? ($tentative ? olResponseTentative : olResponseAccepted) : olResponseNotResponded;
2891
2892
		if (isset($exception_props[$this->proptags['intendedbusystatus']])) {
2893
			if ($tentative && $exception_props[$this->proptags['intendedbusystatus']] !== fbFree) {
2894
				$exception_props[$this->proptags['busystatus']] = fbTentative;
2895
			}
2896
			else {
2897
				$exception_props[$this->proptags['busystatus']] = $exception_props[$this->proptags['intendedbusystatus']];
2898
			}
2899
			// we already have intendedbusystatus value in $exception_props so no need to copy it
2900
		}
2901
		else {
2902
			$exception_props[$this->proptags['busystatus']] = $tentative ? fbTentative : fbBusy;
2903
		}
2904
2905
		if ($userAction) {
2906
			$addrInfo = $this->getOwnerAddress($this->store);
2907
2908
			// if user has responded then set replytime and name
2909
			$exception_props[$this->proptags['replytime']] = time();
2910
			if (!empty($addrInfo)) {
2911
				$exception_props[$this->proptags['apptreplyname']] = $addrInfo[0];
2912
			}
2913
		}
2914
2915
		if ($recurr->isException($basedate)) {
2916
			$recurr->modifyException($exception_props, $basedate, $recips, $occurrenceItem);
2917
		}
2918
		else {
2919
			$recurr->createException($exception_props, $basedate, false, $recips, $occurrenceItem);
2920
		}
2921
2922
		// Move the occurrenceItem to the waste basket
2923
		if ($move) {
2924
			$wastebasket = $this->openDefaultWastebasket($this->openDefaultStore());
2925
			$sourcefolder = mapi_msgstore_openentry($store, $exception_props[PR_PARENT_ENTRYID]);
2926
			mapi_folder_copymessages($sourcefolder, [$exception_props[PR_ENTRYID]], $wastebasket, MESSAGE_MOVE);
2927
		}
2928
2929
		mapi_savechanges($recurringItem);
2930
	}
2931
2932
	/**
2933
	 * Function which merges an exception mapi message to recurring message.
2934
	 * This will be used when we receive recurring meeting request and we already have an exception message
2935
	 * of same meeting in calendar and we need to remove that exception message and add it to attachment table
2936
	 * of recurring meeting.
2937
	 *
2938
	 * @param resource $recurringItem  reference to MAPI_message of recurring item
2939
	 * @param resource $occurrenceItem reference to MAPI_message of occurrence
2940
	 * @param mixed    $basedate       basedate of occurrence
2941
	 * @param resource $store          user store
2942
	 */
2943
	public function mergeException(&$recurringItem, &$occurrenceItem, $basedate, $store): void {
2944
		$recurr = new Recurrence($store, $recurringItem);
2945
2946
		// Copy properties from meeting request
2947
		$exception_props = mapi_getprops($occurrenceItem);
2948
2949
		// Get recipient list from message and add it to exception attachment
2950
		$reciptable = mapi_message_getrecipienttable($occurrenceItem);
2951
		$recips = mapi_table_queryallrows($reciptable, $this->recipprops);
2952
2953
		if ($recurr->isException($basedate)) {
2954
			$recurr->modifyException($exception_props, $basedate, $recips, $occurrenceItem);
2955
		}
2956
		else {
2957
			$recurr->createException($exception_props, $basedate, false, $recips, $occurrenceItem);
2958
		}
2959
2960
		// Move the occurrenceItem to the waste basket
2961
		$wastebasket = $this->openDefaultWastebasket($this->openDefaultStore());
2962
		$sourcefolder = mapi_msgstore_openentry($store, $exception_props[PR_PARENT_ENTRYID]);
2963
		mapi_folder_copymessages($sourcefolder, [$exception_props[PR_ENTRYID]], $wastebasket, MESSAGE_MOVE);
2964
2965
		mapi_savechanges($recurringItem);
2966
	}
2967
2968
	/**
2969
	 * Function which submits meeting request based on arguments passed to it.
2970
	 *
2971
	 * @param resource $message        MAPI_message whose meeting request is to be sent
2972
	 * @param bool     $cancel         if true send request, else send cancellation
2973
	 * @param mixed    $prefix         subject prefix
2974
	 * @param mixed    $basedate       basedate for an occurrence
2975
	 * @param mixed    $recurObject    recurrence object of mr
2976
	 * @param bool     $copyExceptions When sending update mail for recurring item then we don't send exceptions in attachments
2977
	 * @param mixed    $modifiedRecips
2978
	 * @param mixed    $deletedRecips
2979
	 */
2980
	public function submitMeetingRequest($message, $cancel, $prefix, $basedate = false, $recurObject = false, $copyExceptions = true, $modifiedRecips = false, $deletedRecips = false): void {
2981
		$newmessageprops = $messageprops = mapi_getprops($this->message);
2982
		$new = $this->createOutgoingMessage();
2983
2984
		// Copy the entire message into the new meeting request message
2985
		if ($basedate) {
2986
			// messageprops contains properties of whole recurring series
2987
			// and newmessageprops contains properties of exception item
2988
			$newmessageprops = mapi_getprops($message);
2989
2990
			// Ensure that the correct basedate is set in the new message
2991
			$newmessageprops[$this->proptags['basedate']] = $basedate;
2992
2993
			// Set isRecurring to false, because this is an exception
2994
			$newmessageprops[$this->proptags['recurring']] = false;
2995
2996
			// set LID_IS_EXCEPTION to true
2997
			$newmessageprops[$this->proptags['is_exception']] = true;
2998
2999
			// Set to high importance
3000
			if ($cancel) {
3001
				$newmessageprops[PR_IMPORTANCE] = IMPORTANCE_HIGH;
3002
			}
3003
3004
			// Set startdate and enddate of exception
3005
			if ($cancel && $recurObject) {
3006
				$newmessageprops[$this->proptags['startdate']] = $recurObject->getOccurrenceStart($basedate);
3007
				$newmessageprops[$this->proptags['duedate']] = $recurObject->getOccurrenceEnd($basedate);
3008
			}
3009
3010
			// Set basedate in guid (0x3)
3011
			$newmessageprops[$this->proptags['goid']] = $this->setBasedateInGlobalID($messageprops[$this->proptags['goid2']], $basedate);
3012
			$newmessageprops[$this->proptags['goid2']] = $messageprops[$this->proptags['goid2']];
3013
			$newmessageprops[PR_OWNER_APPT_ID] = $messageprops[PR_OWNER_APPT_ID];
3014
3015
			// Get deleted recipiets from exception msg
3016
			$restriction = [
3017
				RES_AND,
3018
				[
3019
					[
3020
						RES_BITMASK,
3021
						[
3022
							ULTYPE => BMR_NEZ,
3023
							ULPROPTAG => PR_RECIPIENT_FLAGS,
3024
							ULMASK => recipExceptionalDeleted,
3025
						],
3026
					],
3027
					[
3028
						RES_BITMASK,
3029
						[
3030
							ULTYPE => BMR_EQZ,
3031
							ULPROPTAG => PR_RECIPIENT_FLAGS,
3032
							ULMASK => recipOrganizer,
3033
						],
3034
					],
3035
				],
3036
			];
3037
3038
			// In direct-booking mode, we don't need to send cancellations to resources
3039
			if ($this->enableDirectBooking) {
3040
				$restriction[1][] = [
3041
					RES_PROPERTY,
3042
					[
3043
						RELOP => RELOP_NE,	// Does not equal recipient type: MAPI_BCC (Resource)
3044
						ULPROPTAG => PR_RECIPIENT_TYPE,
3045
						VALUE => [PR_RECIPIENT_TYPE => MAPI_BCC],
3046
					],
3047
				];
3048
			}
3049
3050
			$recipienttable = mapi_message_getrecipienttable($message);
3051
			$recipients = mapi_table_queryallrows($recipienttable, $this->recipprops, $restriction);
3052
3053
			if (!$deletedRecips) {
3054
				$deletedRecips = array_merge([], $recipients);
3055
			}
3056
			else {
3057
				$deletedRecips = array_merge($deletedRecips, $recipients);
3058
			}
3059
		}
3060
3061
		// Remove the PR_ICON_INDEX as it is not needed in the sent message.
3062
		$newmessageprops[PR_ICON_INDEX] = null;
3063
		$newmessageprops[PR_RESPONSE_REQUESTED] = true;
3064
3065
		// PR_START_DATE and PR_END_DATE will be used by outlook to show the position in the calendar
3066
		$newmessageprops[PR_START_DATE] = $newmessageprops[$this->proptags['startdate']];
3067
		$newmessageprops[PR_END_DATE] = $newmessageprops[$this->proptags['duedate']];
3068
3069
		// Set updatecounter/AppointmentSequenceNumber
3070
		// get the value of latest updatecounter for the whole series and use it
3071
		$newmessageprops[$this->proptags['updatecounter']] = $messageprops[$this->proptags['last_updatecounter']];
3072
3073
		$meetingTimeInfo = $this->getMeetingTimeInfo();
3074
3075
		if ($meetingTimeInfo) {
3076
			// Needs to unset PR_HTML and PR_RTF_COMPRESSED props
3077
			// because while canceling meeting requests with edit text
3078
			// will override the PR_BODY because body value is not consistent with
3079
			// PR_HTML and PR_RTF_COMPRESSED value so in this case PR_RTF_COMPRESSED will
3080
			// get priority which override the PR_BODY value.
3081
			unset($newmessageprops[PR_HTML], $newmessageprops[PR_RTF_COMPRESSED]);
3082
3083
			$newmessageprops[PR_BODY] = $meetingTimeInfo;
3084
		}
3085
3086
		// Send all recurrence info in mail, if this is a recurrence meeting.
3087
		if (isset($messageprops[$this->proptags['recurring']]) && $messageprops[$this->proptags['recurring']]) {
3088
			if (!empty($messageprops[$this->proptags['recurring_pattern']])) {
3089
				$newmessageprops[$this->proptags['recurring_pattern']] = $messageprops[$this->proptags['recurring_pattern']];
3090
			}
3091
			$newmessageprops[$this->proptags['recurrence_data']] = $messageprops[$this->proptags['recurrence_data']];
3092
			$newmessageprops[$this->proptags['timezone_data']] = $messageprops[$this->proptags['timezone_data']];
3093
			$newmessageprops[$this->proptags['timezone']] = $messageprops[$this->proptags['timezone']];
3094
3095
			if ($recurObject) {
3096
				$this->generateRecurDates($recurObject, $messageprops, $newmessageprops);
3097
			}
3098
		}
3099
3100
		if (isset($newmessageprops[$this->proptags['counter_proposal']])) {
3101
			unset($newmessageprops[$this->proptags['counter_proposal']]);
3102
		}
3103
3104
		// Prefix the subject if needed
3105
		if ($prefix && isset($newmessageprops[PR_SUBJECT])) {
3106
			$newmessageprops[PR_SUBJECT] = $prefix . $newmessageprops[PR_SUBJECT];
3107
		}
3108
3109
		if (isset($newmessageprops[$this->proptags['categories']]) &&
3110
			!empty($newmessageprops[$this->proptags['categories']])) {
3111
			unset($newmessageprops[$this->proptags['categories']]);
3112
		}
3113
		mapi_setprops($new, $newmessageprops);
3114
3115
		// Copy attachments
3116
		$this->replaceAttachments($message, $new, $copyExceptions);
3117
3118
		// Retrieve only those recipient who should receive this meeting request.
3119
		$stripResourcesRestriction = [
3120
			RES_AND,
3121
			[
3122
				[
3123
					RES_BITMASK,
3124
					[
3125
						ULTYPE => BMR_EQZ,
3126
						ULPROPTAG => PR_RECIPIENT_FLAGS,
3127
						ULMASK => recipExceptionalDeleted,
3128
					],
3129
				],
3130
				[
3131
					RES_BITMASK,
3132
					[
3133
						ULTYPE => BMR_EQZ,
3134
						ULPROPTAG => PR_RECIPIENT_FLAGS,
3135
						ULMASK => recipOrganizer,
3136
					],
3137
				],
3138
			],
3139
		];
3140
3141
		// In direct-booking mode, resources do not receive a meeting request
3142
		if ($this->enableDirectBooking) {
3143
			$stripResourcesRestriction[1][] = [
3144
				RES_PROPERTY,
3145
				[
3146
					RELOP => RELOP_NE,	// Does not equal recipient type: MAPI_BCC (Resource)
3147
					ULPROPTAG => PR_RECIPIENT_TYPE,
3148
					VALUE => [PR_RECIPIENT_TYPE => MAPI_BCC],
3149
				],
3150
			];
3151
		}
3152
3153
		// If no recipients were explicitly provided, we will send the update to all
3154
		// recipients from the meeting.
3155
		if ($modifiedRecips === false) {
3156
			$recipienttable = mapi_message_getrecipienttable($message);
3157
			$modifiedRecips = mapi_table_queryallrows($recipienttable, $this->recipprops, $stripResourcesRestriction);
3158
3159
			if ($basedate && empty($modifiedRecips)) {
3160
				// Retrieve full list
3161
				$recipienttable = mapi_message_getrecipienttable($this->message);
3162
				$modifiedRecips = mapi_table_queryallrows($recipienttable, $this->recipprops);
3163
3164
				// Save recipients in exceptions
3165
				mapi_message_modifyrecipients($message, MODRECIP_ADD, $modifiedRecips);
3166
3167
				// Now retrieve only those recipient who should receive this meeting request.
3168
				$modifiedRecips = mapi_table_queryallrows($recipienttable, $this->recipprops, $stripResourcesRestriction);
3169
			}
3170
		}
3171
3172
		// @TODO: handle nonAcceptingResources
3173
		/*
3174
		 * Add resource recipients that did not automatically accept the meeting request.
3175
		 * (note: meaning that they did not decline the meeting request)
3176
		 */ /*
3177
		for($i=0;$i<count($this->nonAcceptingResources);$i++){
3178
			$recipients[] = $this->nonAcceptingResources[$i];
3179
		}*/
3180
3181
		if (!empty($modifiedRecips)) {
3182
			// Strip out the sender/'owner' recipient
3183
			mapi_message_modifyrecipients($new, MODRECIP_ADD, $modifiedRecips);
3184
3185
			// Set some properties that are different in the sent request than
3186
			// in the item in our calendar
3187
3188
			// we should store busystatus value to intendedbusystatus property, because busystatus for outgoing meeting request
3189
			// should always be fbTentative
3190
			$newmessageprops[$this->proptags['intendedbusystatus']] = isset($newmessageprops[$this->proptags['busystatus']]) ? $newmessageprops[$this->proptags['busystatus']] : $messageprops[$this->proptags['busystatus']];
3191
			$newmessageprops[$this->proptags['busystatus']] = fbTentative; // The default status when not accepted
3192
			$newmessageprops[$this->proptags['responsestatus']] = olResponseNotResponded; // The recipient has not responded yet
3193
			$newmessageprops[$this->proptags['attendee_critical_change']] = time();
3194
			$newmessageprops[$this->proptags['owner_critical_change']] = time();
3195
			$newmessageprops[$this->proptags['meetingtype']] = mtgRequest;
3196
3197
			if ($cancel) {
3198
				$newmessageprops[PR_MESSAGE_CLASS] = 'IPM.Schedule.Meeting.Canceled';
3199
				$newmessageprops[$this->proptags['meetingstatus']] = olMeetingCanceled; // It's a cancel request
3200
				$newmessageprops[$this->proptags['busystatus']] = fbFree; // set the busy status as free
3201
			}
3202
			else {
3203
				$newmessageprops[PR_MESSAGE_CLASS] = 'IPM.Schedule.Meeting.Request';
3204
				$newmessageprops[$this->proptags['meetingstatus']] = olMeetingReceived; // The recipient is receiving the request
3205
			}
3206
3207
			mapi_setprops($new, $newmessageprops);
3208
			mapi_savechanges($new);
3209
3210
			// Submit message to non-resource recipients
3211
			mapi_message_submitmessage($new);
3212
		}
3213
3214
		// Search through the deleted recipients, and see if any of them is also
3215
		// listed as a recipient to whom we have sent an update. As we don't
3216
		// want to send a cancellation message to recipients who will also receive
3217
		// an meeting update, we have to filter those recipients out.
3218
		if ($deletedRecips) {
3219
			$tmp = [];
3220
3221
			foreach ($deletedRecips as $delRecip) {
3222
				$found = false;
3223
3224
				// Search if the deleted recipient can be found inside
3225
				// the updated recipients as well.
3226
				foreach ($modifiedRecips as $recip) {
3227
					if ($this->compareABEntryIDs($recip[PR_ENTRYID], $delRecip[PR_ENTRYID])) {
3228
						$found = true;
3229
						break;
3230
					}
3231
				}
3232
3233
				// If the recipient was not found, it truly is deleted,
3234
				// and we can safely send a cancellation message
3235
				if (!$found) {
3236
					$tmp[] = $delRecip;
3237
				}
3238
			}
3239
3240
			$deletedRecips = $tmp;
3241
		}
3242
3243
		// Send cancellation to deleted attendees
3244
		if ($deletedRecips) {
3245
			$new = $this->createOutgoingMessage();
3246
3247
			mapi_message_modifyrecipients($new, MODRECIP_ADD, $deletedRecips);
3248
3249
			$newmessageprops[PR_MESSAGE_CLASS] = 'IPM.Schedule.Meeting.Canceled';
3250
			$newmessageprops[$this->proptags['meetingstatus']] = olMeetingCanceled; // It's a cancel request
3251
			$newmessageprops[$this->proptags['busystatus']] = fbFree; // set the busy status as free
3252
			$newmessageprops[PR_IMPORTANCE] = IMPORTANCE_HIGH;	// HIGH Importance
3253
			if (isset($newmessageprops[PR_SUBJECT])) {
3254
				$newmessageprops[PR_SUBJECT] = dgettext('zarafa', 'Canceled') . ': ' . $newmessageprops[PR_SUBJECT];
3255
			}
3256
3257
			mapi_setprops($new, $newmessageprops);
3258
			mapi_savechanges($new);
3259
3260
			// Submit message to non-resource recipients
3261
			mapi_message_submitmessage($new);
3262
		}
3263
3264
		// Set properties on meeting object in calendar
3265
		// Set requestsent to 'true' (turns on 'tracking', etc)
3266
		$props = [];
3267
		$props[$this->proptags['meetingstatus']] = olMeeting;
3268
		$props[$this->proptags['responsestatus']] = olResponseOrganized;
3269
		// Only set the 'requestsent' property if it wasn't set previously yet,
3270
		// this ensures we will not accidentally set it from true to false.
3271
		if (!isset($messageprops[$this->proptags['requestsent']]) || $messageprops[$this->proptags['requestsent']] !== true) {
3272
			$props[$this->proptags['requestsent']] = !empty($modifiedRecips) || ($this->includesResources && !$this->errorSetResource);
3273
		}
3274
		$props[$this->proptags['attendee_critical_change']] = time();
3275
		$props[$this->proptags['owner_critical_change']] = time();
3276
		$props[$this->proptags['meetingtype']] = mtgRequest;
3277
		// save the new updatecounter to exception/recurring series/normal meeting
3278
		$props[$this->proptags['updatecounter']] = $newmessageprops[$this->proptags['updatecounter']];
3279
3280
		// PR_START_DATE and PR_END_DATE will be used by outlook to show the position in the calendar
3281
		$props[PR_START_DATE] = $messageprops[$this->proptags['startdate']];
3282
		$props[PR_END_DATE] = $messageprops[$this->proptags['duedate']];
3283
3284
		mapi_setprops($message, $props);
3285
3286
		// saving of these properties on calendar item should be handled by caller function
3287
		// based on sending meeting request was successful or not
3288
	}
3289
3290
	/**
3291
	 * OL2007 uses these 4 properties to specify occurrence that should be updated.
3292
	 * ical generates RECURRENCE-ID property based on exception's basedate (PidLidExceptionReplaceTime),
3293
	 * but OL07 doesn't send this property, so ical will generate RECURRENCE-ID property based on date
3294
	 * from GlobalObjId and time from StartRecurTime property, so we are sending basedate property and
3295
	 * also additionally we are sending these properties.
3296
	 * Ref: MS-OXCICAL 2.2.1.20.20 Property: RECURRENCE-ID.
3297
	 *
3298
	 * @param object $recurObject     instance of recurrence class for this message
3299
	 * @param array  $messageprops    properties of meeting object that is going to be sent
3300
	 * @param array  $newmessageprops properties of meeting request/response that is going to be sent
3301
	 */
3302
	public function generateRecurDates($recurObject, $messageprops, &$newmessageprops): void {
3303
		if ($messageprops[$this->proptags['startdate']] && $messageprops[$this->proptags['duedate']]) {
3304
			$startDate = date('Y:n:j:G:i:s', $recurObject->fromGMT($recurObject->tz, $messageprops[$this->proptags['startdate']]));
3305
			$endDate = date('Y:n:j:G:i:s', $recurObject->fromGMT($recurObject->tz, $messageprops[$this->proptags['duedate']]));
3306
3307
			$startDate = explode(':', $startDate);
3308
			$endDate = explode(':', $endDate);
3309
3310
			// [0] => year, [1] => month, [2] => day, [3] => hour, [4] => minutes, [5] => seconds
3311
			// RecurStartDate = year * 512 + month_number * 32 + day_number
3312
			$newmessageprops[$this->proptags['start_recur_date']] = (((int) $startDate[0]) * 512) + (((int) $startDate[1]) * 32) + ((int) $startDate[2]);
3313
			// RecurStartTime = hour * 4096 + minutes * 64 + seconds
3314
			$newmessageprops[$this->proptags['start_recur_time']] = (((int) $startDate[3]) * 4096) + (((int) $startDate[4]) * 64) + ((int) $startDate[5]);
3315
3316
			$newmessageprops[$this->proptags['end_recur_date']] = (((int) $endDate[0]) * 512) + (((int) $endDate[1]) * 32) + ((int) $endDate[2]);
3317
			$newmessageprops[$this->proptags['end_recur_time']] = (((int) $endDate[3]) * 4096) + (((int) $endDate[4]) * 64) + ((int) $endDate[5]);
3318
		}
3319
	}
3320
3321
	/**
3322
	 * Function will create a new outgoing message that will be used to send meeting mail.
3323
	 *
3324
	 * @param mixed $store (optional) store that is used when creating response, if delegate is creating outgoing mail
3325
	 *                     then this would point to delegate store
3326
	 *
3327
	 * @return resource outgoing mail that is created and can be used for sending it
3328
	 */
3329
	public function createOutgoingMessage($store = false) {
3330
		// get logged in user's store that will be used to send mail, for delegate this will be
3331
		// delegate store
3332
		$userStore = $this->openDefaultStore();
3333
3334
		$sentprops = [];
3335
		$outbox = $this->openDefaultOutbox($userStore);
3336
3337
		$outgoing = mapi_folder_createmessage($outbox);
3338
3339
		// check if $store is set and it is not equal to $defaultStore (means its the delegation case)
3340
		if ($store !== false) {
3341
			$storeProps = mapi_getprops($store, [PR_ENTRYID]);
3342
			$userStoreProps = mapi_getprops($userStore, [PR_ENTRYID]);
3343
3344
			// @FIXME use entryid comparison functions here
3345
			if ($storeProps[PR_ENTRYID] !== $userStoreProps[PR_ENTRYID]) {
3346
				// get the delegator properties and set it into outgoing mail
3347
				$delegatorDetails = $this->getOwnerAddress($store, false);
3348
3349
				if (!empty($delegatorDetails)) {
3350
					list($ownername, $owneremailaddr, $owneraddrtype, $ownerentryid, $ownersearchkey) = $delegatorDetails;
3351
					$sentprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS] = $owneremailaddr;
3352
					$sentprops[PR_SENT_REPRESENTING_NAME] = $ownername;
3353
					$sentprops[PR_SENT_REPRESENTING_ADDRTYPE] = $owneraddrtype;
3354
					$sentprops[PR_SENT_REPRESENTING_ENTRYID] = $ownerentryid;
3355
					$sentprops[PR_SENT_REPRESENTING_SEARCH_KEY] = $ownersearchkey;
3356
				}
3357
3358
				// get the delegate properties and set it into outgoing mail
3359
				$delegateDetails = $this->getOwnerAddress($userStore, false);
3360
3361
				if (!empty($delegateDetails)) {
3362
					list($ownername, $owneremailaddr, $owneraddrtype, $ownerentryid, $ownersearchkey) = $delegateDetails;
3363
					$sentprops[PR_SENDER_EMAIL_ADDRESS] = $owneremailaddr;
3364
					$sentprops[PR_SENDER_NAME] = $ownername;
3365
					$sentprops[PR_SENDER_ADDRTYPE] = $owneraddrtype;
3366
					$sentprops[PR_SENDER_ENTRYID] = $ownerentryid;
3367
					$sentprops[PR_SENDER_SEARCH_KEY] = $ownersearchkey;
3368
				}
3369
			}
3370
		}
3371
		else {
3372
			// normal user is sending mail, so both set of properties will be same
3373
			$userDetails = $this->getOwnerAddress($userStore);
3374
3375
			if (!empty($userDetails)) {
3376
				list($ownername, $owneremailaddr, $owneraddrtype, $ownerentryid, $ownersearchkey) = $userDetails;
3377
				$sentprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS] = $owneremailaddr;
3378
				$sentprops[PR_SENT_REPRESENTING_NAME] = $ownername;
3379
				$sentprops[PR_SENT_REPRESENTING_ADDRTYPE] = $owneraddrtype;
3380
				$sentprops[PR_SENT_REPRESENTING_ENTRYID] = $ownerentryid;
3381
				$sentprops[PR_SENT_REPRESENTING_SEARCH_KEY] = $ownersearchkey;
3382
3383
				$sentprops[PR_SENDER_EMAIL_ADDRESS] = $owneremailaddr;
3384
				$sentprops[PR_SENDER_NAME] = $ownername;
3385
				$sentprops[PR_SENDER_ADDRTYPE] = $owneraddrtype;
3386
				$sentprops[PR_SENDER_ENTRYID] = $ownerentryid;
3387
				$sentprops[PR_SENDER_SEARCH_KEY] = $ownersearchkey;
3388
			}
3389
		}
3390
3391
		$sentprops[PR_SENTMAIL_ENTRYID] = $this->getDefaultSentmailEntryID($userStore);
3392
3393
		mapi_setprops($outgoing, $sentprops);
3394
3395
		return $outgoing;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $outgoing returns the type resource which is incompatible with the documented return type resource.
Loading history...
3396
	}
3397
3398
	/**
3399
	 * Function which checks that meeting in attendee's calendar is already updated
3400
	 * and we are checking an old meeting request. This function also will update property
3401
	 * meetingtype to indicate that its out of date meeting request.
3402
	 *
3403
	 * @return bool true if meeting request is outofdate else false if it is new
3404
	 */
3405
	public function isMeetingOutOfDate() {
3406
		$result = false;
3407
3408
		$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS, $this->proptags['goid'], $this->proptags['goid2'], $this->proptags['updatecounter'], $this->proptags['meetingtype'], $this->proptags['owner_critical_change']]);
3409
3410
		if (!$this->isMeetingRequest($props[PR_MESSAGE_CLASS])) {
3411
			return $result;
3412
		}
3413
3414
		if (isset($props[$this->proptags['meetingtype']]) && ($props[$this->proptags['meetingtype']] & mtgOutOfDate) == mtgOutOfDate) {
3415
			return true;
3416
		}
3417
3418
		// get the basedate to check for exception
3419
		$basedate = $this->getBasedateFromGlobalID($props[$this->proptags['goid']]);
3420
3421
		$calendarItem = $this->getCorrespondentCalendarItem(true);
3422
3423
		// if basedate is provided and we could not find the item then it could be that we are checking
3424
		// an exception so get the exception and check it
3425
		if ($basedate !== false && $calendarItem !== false) {
3426
			$exception = $this->getExceptionItem($calendarItem, $basedate);
3427
3428
			if ($exception !== false) {
3429
				// we are able to find the exception compare with it
3430
				$calendarItem = $exception;
3431
			}
3432
			// we are not able to find exception, could mean that a significant change has occurred on series
3433
			// and it deleted all exceptions, so compare with series
3434
			// $calendarItem already contains reference to series
3435
		}
3436
3437
		if ($calendarItem !== false) {
3438
			$calendarItemProps = mapi_getprops($calendarItem, [
3439
				$this->proptags['owner_critical_change'],
3440
				$this->proptags['updatecounter'],
3441
			]);
3442
3443
			$updateCounter = (isset($calendarItemProps[$this->proptags['updatecounter']]) && $props[$this->proptags['updatecounter']] < $calendarItemProps[$this->proptags['updatecounter']]);
3444
3445
			$criticalChange = (isset($calendarItemProps[$this->proptags['owner_critical_change']]) && $props[$this->proptags['owner_critical_change']] < $calendarItemProps[$this->proptags['owner_critical_change']]);
3446
3447
			if ($updateCounter || $criticalChange) {
3448
				// meeting request is out of date, set properties to indicate this
3449
				mapi_setprops($this->message, [$this->proptags['meetingtype'] => mtgOutOfDate, PR_ICON_INDEX => 1033]);
3450
				mapi_savechanges($this->message);
3451
3452
				$result = true;
3453
			}
3454
		}
3455
3456
		return $result;
3457
	}
3458
3459
	/**
3460
	 * Function which checks that if we have received a meeting response for an updated meeting in organizer's calendar.
3461
	 *
3462
	 * @param mixed $basedate basedate of the exception if we want to compare with exception
3463
	 *
3464
	 * @return bool true if meeting request is updated later
3465
	 */
3466
	public function isMeetingUpdated($basedate = false) {
3467
		$result = false;
3468
3469
		$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS, $this->proptags['updatecounter']]);
3470
3471
		if (!$this->isMeetingRequestResponse($props[PR_MESSAGE_CLASS])) {
3472
			return $result;
3473
		}
3474
3475
		$calendarItem = $this->getCorrespondentCalendarItem(true);
3476
3477
		if ($calendarItem !== false) {
3478
			// basedate is provided so open exception
3479
			if ($basedate !== false) {
3480
				$exception = $this->getExceptionItem($calendarItem, $basedate);
3481
3482
				if ($exception !== false) {
3483
					// we are able to find the exception compare with it
3484
					$calendarItem = $exception;
3485
				}
3486
				// we are not able to find exception, could mean that a significant change has occurred on series
3487
				// and it deleted all exceptions, so compare with series
3488
				// $calendarItem already contains reference to series
3489
			}
3490
3491
			if ($calendarItem !== false) {
3492
				$calendarItemProps = mapi_getprops($calendarItem, [$this->proptags['updatecounter']]);
3493
3494
				/*
3495
				 * if(message_counter < appointment_counter) meeting object is newer then meeting response (meeting is updated)
3496
				 * if(message_counter >= appointment_counter) meeting is not updated, do normal processing
3497
				 */
3498
				if (isset($calendarItemProps[$this->proptags['updatecounter']], $props[$this->proptags['updatecounter']])) {
3499
					if ($props[$this->proptags['updatecounter']] < $calendarItemProps[$this->proptags['updatecounter']]) {
3500
						$result = true;
3501
					}
3502
				}
3503
			}
3504
		}
3505
3506
		return $result;
3507
	}
3508
3509
	/**
3510
	 * Checks if there has been any significant changes on appointment/meeting item.
3511
	 * Significant changes be:
3512
	 * 1) startdate has been changed
3513
	 * 2) duedate has been changed OR
3514
	 * 3) recurrence pattern has been created, modified or removed.
3515
	 *
3516
	 * @param mixed $oldProps
3517
	 * @param mixed $basedate
3518
	 * @param mixed $isRecurrenceChanged for change in recurrence pattern.
3519
	 *                                   true means Recurrence pattern has been changed,
3520
	 *                                   so clear all attendees response
3521
	 */
3522
	public function checkSignificantChanges($oldProps, $basedate, $isRecurrenceChanged = false) {
3523
		$message = null;
3524
		$attach = null;
3525
3526
		// If basedate is specified then we need to open exception message to clear recipient responses
3527
		if ($basedate) {
3528
			$recurrence = new Recurrence($this->store, $this->message);
3529
			if ($recurrence->isException($basedate)) {
3530
				$attach = $recurrence->getExceptionAttachment($basedate);
3531
				if ($attach) {
3532
					$message = mapi_attach_openobj($attach, MAPI_MODIFY);
3533
				}
3534
			}
3535
		}
3536
		else {
3537
			// use normal message or recurring series message
3538
			$message = $this->message;
3539
		}
3540
3541
		if (!$message) {
3542
			return;
3543
		}
3544
3545
		$newProps = mapi_getprops($message, [$this->proptags['startdate'], $this->proptags['duedate'], $this->proptags['updatecounter']]);
3546
3547
		// Check whether message is updated or not.
3548
		if (isset($newProps[$this->proptags['updatecounter']]) && $newProps[$this->proptags['updatecounter']] == 0) {
3549
			return;
3550
		}
3551
3552
		if (($newProps[$this->proptags['startdate']] != $oldProps[$this->proptags['startdate']]) ||
3553
				($newProps[$this->proptags['duedate']] != $oldProps[$this->proptags['duedate']]) ||
3554
				$isRecurrenceChanged) {
3555
			$this->clearRecipientResponse($message);
3556
3557
			mapi_setprops($message, [$this->proptags['owner_critical_change'] => time()]);
3558
3559
			mapi_savechanges($message);
3560
			if ($attach) { // Also save attachment Object.
3561
				mapi_savechanges($attach);
3562
			}
3563
		}
3564
	}
3565
3566
	/**
3567
	 * Clear responses of all attendees who have replied in past.
3568
	 *
3569
	 * @param resource $message on which responses should be cleared
3570
	 */
3571
	public function clearRecipientResponse($message): void {
3572
		$recipTable = mapi_message_getrecipienttable($message);
3573
		$recipsRows = mapi_table_queryallrows($recipTable, $this->recipprops);
3574
		for ($i = 0, $recipsCnt = mapi_table_getrowcount($recipTable); $i < $recipsCnt; ++$i) {
3575
			// Clear track status for everyone in the recipients table
3576
			$recipsRows[$i][PR_RECIPIENT_TRACKSTATUS] = olRecipientTrackStatusNone;
3577
		}
3578
		mapi_message_modifyrecipients($message, MODRECIP_MODIFY, $recipsRows);
3579
	}
3580
3581
	/**
3582
	 * Function returns correspondent calendar item attached with the meeting request/response/cancellation.
3583
	 * This will only check for actual MAPIMessages in calendar folder, so if a meeting request is
3584
	 * for exception then this function will return recurring series for that meeting request
3585
	 * after that you need to use getExceptionItem function to get exception item that will be
3586
	 * fetched from the attachment table of recurring series MAPIMessage.
3587
	 *
3588
	 * @param bool $open boolean to indicate the function should return entryid or MAPIMessage. Defaults to true.
3589
	 *
3590
	 * @return bool|resource resource of calendar item
3591
	 */
3592
	public function getCorrespondentCalendarItem($open = true) {
3593
		$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS, $this->proptags['goid'], $this->proptags['goid2'], PR_RCVD_REPRESENTING_ENTRYID]);
3594
3595
		if (!$this->isMeetingRequest($props[PR_MESSAGE_CLASS]) && !$this->isMeetingRequestResponse($props[PR_MESSAGE_CLASS]) && !$this->isMeetingCancellation($props[PR_MESSAGE_CLASS])) {
3596
			// can work only with meeting requests/responses/cancellations
3597
			return false;
3598
		}
3599
3600
		// there is no goid - no items can be found - aborting
3601
		if (empty($props[$this->proptags['goid']])) {
3602
			return false;
3603
		}
3604
		$globalId = $props[$this->proptags['goid']];
3605
3606
		$store = $this->store;
3607
		$calFolder = $this->openDefaultCalendar();
3608
		// If Delegate is processing Meeting Request/Response for Delegator then retrieve Delegator's store and calendar.
3609
		if (isset($props[PR_RCVD_REPRESENTING_ENTRYID])) {
3610
			$delegatorStore = $this->getDelegatorStore($props[PR_RCVD_REPRESENTING_ENTRYID], [PR_IPM_APPOINTMENT_ENTRYID]);
3611
			if (!empty($delegatorStore['store'])) {
3612
				$store = $delegatorStore['store'];
3613
			}
3614
			if (!empty($delegatorStore[PR_IPM_APPOINTMENT_ENTRYID])) {
3615
				$calFolder = $delegatorStore[PR_IPM_APPOINTMENT_ENTRYID];
3616
			}
3617
		}
3618
3619
		$basedate = $this->getBasedateFromGlobalID($globalId);
3620
3621
		/**
3622
		 * First search for any appointments which correspond to the $globalId,
3623
		 * this can be the entire series (if the Meeting Request refers to the
3624
		 * entire series), or an particular Occurrence (if the meeting Request
3625
		 * contains a basedate).
3626
		 *
3627
		 * If we cannot find a corresponding item, and the $globalId contains
3628
		 * a $basedate, it might imply that a new exception will have to be
3629
		 * created for a series which is present in the calendar, we can look
3630
		 * that one up by searching for the $cleanGlobalId.
3631
		 */
3632
		$entryids = $this->findCalendarItems($globalId, $calFolder);
3633
		if ($basedate !== false && empty($entryids)) {
3634
			// only search if a goid2 is available
3635
			if (!empty($props[$this->proptags['goid2']])) {
3636
				$cleanGlobalId = $props[$this->proptags['goid2']];
3637
				$entryids = $this->findCalendarItems($cleanGlobalId, $calFolder, true);
3638
			}
3639
		}
3640
3641
		// there should be only one item returned
3642
		if (!empty($entryids) && count($entryids) === 1) {
3643
			// return only entryid
3644
			if ($open === false) {
3645
				return $entryids[0];
3646
			}
3647
3648
			// open calendar item and return it
3649
			if ($store) {
3650
				return mapi_msgstore_openentry($store, $entryids[0]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return mapi_msgstore_ope...y($store, $entryids[0]) returns the type resource which is incompatible with the documented return type boolean|resource.
Loading history...
3651
			}
3652
		}
3653
3654
		// no items found in calendar
3655
		return false;
3656
	}
3657
3658
	/**
3659
	 * Function returns exception item based on the basedate passed.
3660
	 *
3661
	 * @param mixed $recurringMessage Resource of Recurring meeting from calendar
3662
	 * @param mixed $basedate         basedate of exception that needs to be returned
3663
	 * @param mixed $store            store that contains the recurring calendar item
3664
	 *
3665
	 * @return entryid or MAPIMessage resource of exception item
0 ignored issues
show
The type entryid 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...
3666
	 */
3667
	public function getExceptionItem($recurringMessage, $basedate, $store = false) {
3668
		$occurItem = false;
3669
3670
		$props = mapi_getprops($this->message, [PR_RCVD_REPRESENTING_ENTRYID, $this->proptags['recurring']]);
3671
3672
		// check if the passed item is recurring series
3673
		if (isset($props[$this->proptags['recurring']]) && $props[$this->proptags['recurring']] !== false) {
3674
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type entryid.
Loading history...
3675
		}
3676
3677
		if ($store === false) {
3678
			$store = $this->store;
3679
			// If Delegate is processing Meeting Request/Response for Delegator then retrieve Delegator's store and calendar.
3680
			if (isset($props[PR_RCVD_REPRESENTING_ENTRYID])) {
3681
				$delegatorStore = $this->getDelegatorStore($props[PR_RCVD_REPRESENTING_ENTRYID]);
3682
				if (!empty($delegatorStore['store'])) {
3683
					$store = $delegatorStore['store'];
3684
				}
3685
			}
3686
		}
3687
3688
		$recurr = new Recurrence($store, $recurringMessage);
3689
		$attach = $recurr->getExceptionAttachment($basedate);
3690
		if ($attach) {
3691
			$occurItem = mapi_attach_openobj($attach);
3692
		}
3693
3694
		return $occurItem;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $occurItem returns the type false|resource which is incompatible with the documented return type entryid.
Loading history...
3695
	}
3696
3697
	/**
3698
	 * Function which checks whether received meeting request is either conflicting with other appointments or not.
3699
	 *
3700
	 * @param false|resource $message
3701
	 * @param false|resource $userStore
3702
	 * @param mixed          $calFolder calendar folder for conflict checking
3703
	 *
3704
	 * @return bool|int
3705
	 *
3706
	 * @psalm-return bool|int<1, max>
3707
	 */
3708
	public function isMeetingConflicting($message = false, $userStore = false, $calFolder = false) {
3709
		$returnValue = false;
3710
		$noOfInstances = 0;
3711
3712
		if ($message === false) {
3713
			$message = $this->message;
3714
		}
3715
3716
		$messageProps = mapi_getprops(
3717
			$message,
3718
			[
3719
				PR_MESSAGE_CLASS,
3720
				$this->proptags['goid'],
3721
				$this->proptags['goid2'],
3722
				$this->proptags['startdate'],
3723
				$this->proptags['duedate'],
3724
				$this->proptags['recurring'],
3725
				$this->proptags['clipstart'],
3726
				$this->proptags['clipend'],
3727
				PR_RCVD_REPRESENTING_ENTRYID,
3728
				$this->proptags['basedate'],
3729
				PR_RCVD_REPRESENTING_NAME,
3730
			]
3731
		);
3732
3733
		if ($userStore === false) {
3734
			$userStore = $this->store;
3735
3736
			// check if delegate is processing the response
3737
			if (isset($messageProps[PR_RCVD_REPRESENTING_ENTRYID])) {
3738
				$delegatorStore = $this->getDelegatorStore($messageProps[PR_RCVD_REPRESENTING_ENTRYID], [PR_IPM_APPOINTMENT_ENTRYID]);
3739
3740
				if (!empty($delegatorStore['store'])) {
3741
					$userStore = $delegatorStore['store'];
3742
				}
3743
				if (!empty($delegatorStore[PR_IPM_APPOINTMENT_ENTRYID])) {
3744
					$calFolder = $delegatorStore[PR_IPM_APPOINTMENT_ENTRYID];
3745
				}
3746
			}
3747
		}
3748
3749
		if ($calFolder === false) {
3750
			$calFolder = $this->openDefaultCalendar($userStore);
3751
		}
3752
3753
		if ($calFolder) {
3754
			// Meeting request is recurring, so get all occurrence and check for each occurrence whether it conflicts with other appointments in Calendar.
3755
			if (isset($messageProps[$this->proptags['recurring']]) && $messageProps[$this->proptags['recurring']] === true) {
3756
				// Apply recurrence class and retrieve all occurrences(max: 30 occurrence because recurrence can also be set as 'no end date')
3757
				$recurr = new Recurrence($userStore, $message);
3758
				$items = $recurr->getItems($messageProps[$this->proptags['clipstart']], $messageProps[$this->proptags['clipend']] * (24 * 24 * 60), 30);
3759
3760
				foreach ($items as $item) {
3761
					// Get all items in the timeframe that we want to book, and get the goid and busystatus for each item
3762
					$calendarItems = $recurr->getCalendarItems($userStore, $calFolder, $item[$this->proptags['startdate']], $item[$this->proptags['duedate']], [$this->proptags['goid'], $this->proptags['busystatus']]);
3763
3764
					foreach ($calendarItems as $calendarItem) {
3765
						if ($calendarItem[$this->proptags['busystatus']] !== fbFree) {
3766
							/*
3767
							 * Only meeting requests have globalID, normal appointments do not have globalID
3768
							 * so if any normal appointment if found then it is assumed to be conflict.
3769
							 */
3770
							if (isset($calendarItem[$this->proptags['goid']])) {
3771
								if ($calendarItem[$this->proptags['goid']] !== $messageProps[$this->proptags['goid']]) {
3772
									++$noOfInstances;
3773
									break;
3774
								}
3775
							}
3776
							else {
3777
								++$noOfInstances;
3778
								break;
3779
							}
3780
						}
3781
					}
3782
				}
3783
3784
				if ($noOfInstances > 0) {
3785
					$returnValue = $noOfInstances;
3786
				}
3787
			}
3788
			else {
3789
				// Get all items in the timeframe that we want to book, and get the goid and busystatus for each item
3790
				$items = getCalendarItems($userStore, $calFolder, $messageProps[$this->proptags['startdate']], $messageProps[$this->proptags['duedate']], [$this->proptags['goid'], $this->proptags['busystatus']]);
3791
3792
				if (isset($messageProps[$this->proptags['basedate']]) && !empty($messageProps[$this->proptags['basedate']])) {
3793
					$basedate = $messageProps[$this->proptags['basedate']];
3794
					// Get the goid2 from recurring MR which further used to
3795
					// check the resource conflicts item.
3796
					$recurrItemProps = mapi_getprops($this->message, [$this->proptags['goid2']]);
3797
					$messageProps[$this->proptags['goid']] = $this->setBasedateInGlobalID($recurrItemProps[$this->proptags['goid2']], $basedate);
3798
					$messageProps[$this->proptags['goid2']] = $recurrItemProps[$this->proptags['goid2']];
3799
				}
3800
3801
				foreach ($items as $item) {
3802
					if ($item[$this->proptags['busystatus']] !== fbFree) {
3803
						if (isset($item[$this->proptags['goid']])) {
3804
							if (($item[$this->proptags['goid']] !== $messageProps[$this->proptags['goid']]) &&
3805
								($item[$this->proptags['goid']] !== $messageProps[$this->proptags['goid2']])) {
3806
								$returnValue = true;
3807
								break;
3808
							}
3809
						}
3810
						else {
3811
							$returnValue = true;
3812
							break;
3813
						}
3814
					}
3815
				}
3816
			}
3817
		}
3818
3819
		return $returnValue;
3820
	}
3821
3822
	/**
3823
	 * Function which adds organizer to recipient list which is passed.
3824
	 * This function also checks if it has organizer.
3825
	 *
3826
	 * @param array $messageProps message properties
3827
	 * @param array $recipients   recipients list of message
3828
	 */
3829
	public function addDelegator($messageProps, &$recipients): void {
3830
		$hasDelegator = false;
3831
		// Check if meeting already has an organizer.
3832
		foreach ($recipients as $key => $recipient) {
3833
			if (isset($messageProps[PR_RCVD_REPRESENTING_EMAIL_ADDRESS]) && $recipient[PR_EMAIL_ADDRESS] == $messageProps[PR_RCVD_REPRESENTING_EMAIL_ADDRESS]) {
3834
				$hasDelegator = true;
3835
			}
3836
		}
3837
3838
		if (!$hasDelegator) {
3839
			// Create delegator.
3840
			$delegator = [];
3841
			$delegator[PR_ENTRYID] = $messageProps[PR_RCVD_REPRESENTING_ENTRYID];
3842
			$delegator[PR_DISPLAY_NAME] = $messageProps[PR_RCVD_REPRESENTING_NAME];
3843
			$delegator[PR_EMAIL_ADDRESS] = $messageProps[PR_RCVD_REPRESENTING_EMAIL_ADDRESS];
3844
			$delegator[PR_RECIPIENT_TYPE] = MAPI_TO;
3845
			$delegator[PR_RECIPIENT_DISPLAY_NAME] = $messageProps[PR_RCVD_REPRESENTING_NAME];
3846
			$delegator[PR_ADDRTYPE] = empty($messageProps[PR_RCVD_REPRESENTING_ADDRTYPE]) ? 'SMTP' : $messageProps[PR_RCVD_REPRESENTING_ADDRTYPE];
3847
			$delegator[PR_RECIPIENT_TRACKSTATUS] = olRecipientTrackStatusNone;
3848
			$delegator[PR_RECIPIENT_FLAGS] = recipSendable;
3849
			$delegator[PR_SEARCH_KEY] = $messageProps[PR_RCVD_REPRESENTING_SEARCH_KEY];
3850
3851
			// Add organizer to recipients list.
3852
			array_unshift($recipients, $delegator);
3853
		}
3854
	}
3855
3856
	/**
3857
	 * Function will return delegator's store and calendar folder for processing meetings.
3858
	 *
3859
	 * @param string $receivedRepresentingEntryId entryid of the delegator user
3860
	 * @param array  $foldersToOpen               contains list of folder types that should be returned in result
3861
	 *
3862
	 * @return resource[] contains store of the delegator and resource of folders if $foldersToOpen is not empty
3863
	 *
3864
	 * @psalm-return array<resource>
3865
	 */
3866
	public function getDelegatorStore($receivedRepresentingEntryId, $foldersToOpen = []): array {
3867
		$returnData = [];
3868
3869
		$delegatorStore = $this->openCustomUserStore($receivedRepresentingEntryId);
3870
		$returnData['store'] = $delegatorStore;
3871
3872
		if (!empty($foldersToOpen)) {
3873
			for ($index = 0, $len = count($foldersToOpen); $index < $len; ++$index) {
3874
				$folderType = $foldersToOpen[$index];
3875
3876
				// first try with default folders
3877
				$folder = $this->openDefaultFolder($folderType, $delegatorStore);
3878
3879
				// if folder not found then try with base folders
3880
				if ($folder === false) {
3881
					$folder = $this->openBaseFolder($folderType, $delegatorStore);
3882
				}
3883
3884
				if ($folder === false) {
3885
					// we are still not able to get the folder so give up
3886
					continue;
3887
				}
3888
3889
				$returnData[$folderType] = $folder;
3890
			}
3891
		}
3892
3893
		return $returnData;
3894
	}
3895
3896
	/**
3897
	 * Function returns extra info about meeting timing along with message body
3898
	 * which will be included in body while sending meeting request/response.
3899
	 *
3900
	 * @return false|string $meetingTimeInfo info about meeting timing along with message body
3901
	 */
3902
	public function getMeetingTimeInfo() {
3903
		return $this->meetingTimeInfo;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->meetingTimeInfo also could return the type boolean which is incompatible with the documented return type false|string.
Loading history...
3904
	}
3905
3906
	/**
3907
	 * Function sets extra info about meeting timing along with message body
3908
	 * which will be included in body while sending meeting request/response.
3909
	 *
3910
	 * @param string $meetingTimeInfo info about meeting timing along with message body
3911
	 */
3912
	public function setMeetingTimeInfo($meetingTimeInfo): void {
3913
		$this->meetingTimeInfo = $meetingTimeInfo;
3914
	}
3915
3916
	/**
3917
	 * Helper function which is use to get local categories of all occurrence.
3918
	 *
3919
	 * @param mixed $calendarItem meeting request item
3920
	 * @param mixed $store        store containing calendar folder
3921
	 * @param mixed $calFolder    calendar folder
3922
	 *
3923
	 * @return array $localCategories which contain array of basedate along with categories
3924
	 */
3925
	public function getLocalCategories($calendarItem, $store, $calFolder) {
3926
		$calendarItemProps = mapi_getprops($calendarItem);
3927
		$recurrence = new Recurrence($store, $calendarItem);
3928
3929
		// Retrieve all occurrences(max: 30 occurrence because recurrence can also be set as 'no end date')
3930
		$items = $recurrence->getItems($calendarItemProps[$this->proptags['clipstart']], $calendarItemProps[$this->proptags['clipend']] * (24 * 24 * 60), 30);
3931
		$localCategories = [];
3932
3933
		foreach ($items as $item) {
3934
			$recurrenceItems = $recurrence->getCalendarItems($store, $calFolder, $item[$this->proptags['startdate']], $item[$this->proptags['duedate']], [$this->proptags['goid'], $this->proptags['busystatus'], $this->proptags['categories']]);
3935
			foreach ($recurrenceItems as $recurrenceItem) {
3936
				// Check if occurrence is exception then get the local categories of that occurrence.
3937
				if (isset($recurrenceItem[$this->proptags['goid']]) && $recurrenceItem[$this->proptags['goid']] == $calendarItemProps[$this->proptags['goid']]) {
3938
					$exceptionAttach = $recurrence->getExceptionAttachment($recurrenceItem['basedate']);
3939
3940
					if ($exceptionAttach) {
3941
						$exception = mapi_attach_openobj($exceptionAttach, 0);
3942
						$exceptionProps = mapi_getprops($exception, [$this->proptags['categories']]);
3943
						if (isset($exceptionProps[$this->proptags['categories']])) {
3944
							$localCategories[$recurrenceItem['basedate']] = $exceptionProps[$this->proptags['categories']];
3945
						}
3946
					}
3947
				}
3948
			}
3949
		}
3950
3951
		return $localCategories;
3952
	}
3953
3954
	/**
3955
	 * Helper function which is use to apply local categories on respective occurrences.
3956
	 *
3957
	 * @param mixed $calendarItem    meeting request item
3958
	 * @param mixed $store           store containing calendar folder
3959
	 * @param array $localCategories array contains basedate and array of categories
3960
	 */
3961
	public function applyLocalCategories($calendarItem, $store, $localCategories): void {
3962
		$calendarItemProps = mapi_getprops($calendarItem, [PR_PARENT_ENTRYID, PR_ENTRYID]);
3963
		$message = mapi_msgstore_openentry($store, $calendarItemProps[PR_ENTRYID]);
3964
		$recurrence = new Recurrence($store, $message);
3965
3966
		// Check for all occurrence if it is exception then modify the exception by setting up categories,
3967
		// Otherwise create new exception with categories.
3968
		foreach ($localCategories as $key => $value) {
3969
			if ($recurrence->isException($key)) {
3970
				$recurrence->modifyException([$this->proptags['categories'] => $value], $key);
3971
			}
3972
			else {
3973
				$recurrence->createException([$this->proptags['categories'] => $value], $key, false);
3974
			}
3975
			mapi_savechanges($message);
3976
		}
3977
	}
3978
}
3979