Passed
Push — master ( fe8fd2...ae800f )
by
unknown
07:14 queued 05:00
created

TaskRequest::setRecipientsForResponse()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 30
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 50
rs 9.1288
1
<?php
2
3
/*
4
 * SPDX-License-Identifier: AGPL-3.0-only
5
 * SPDX-FileCopyrightText: Copyright 2005-2016 Zarafa Deutschland GmbH
6
 * SPDX-FileCopyrightText: Copyright 2020-2024 grommunio GmbH
7
 */
8
9
/*
10
* In general
11
*
12
* This class never actually modifies a task item unless we receive a task request update. This means
13
* that setting all the properties to make the task item itself behave like a task request is up to the
14
* caller.
15
*
16
* The only exception to this is the generation of the TaskGlobalObjId, the unique identifier identifying
17
* this task request to both the organizer and the assignee. The globalobjectid is generated when the
18
* task request is sent via sendTaskRequest.
19
*/
20
21
/* The TaskMode value is only used for the IPM.TaskRequest items.
22
 * It must 0 (tdmtNothing) on IPM.Task items.
23
 *
24
 * It is used to indicate the type of change that is being
25
 * carried in the IPM.TaskRequest item (although this information seems
26
 * redundant due to that information already being available in PR_MESSAGE_CLASS).
27
 */
28
define('tdmtNothing', 0);			// Value in IPM.Task items
29
define('tdmtTaskReq', 1);			// Assigner -> Assignee
30
define('tdmtTaskAcc', 2);			// Assignee -> Assigner
31
define('tdmtTaskDec', 3);			// Assignee -> Assigner
32
define('tdmtTaskUpd', 4);			// Assignee -> Assigner
33
define('tdmtTaskSELF', 5);			// Assigner -> Assigner (?)
34
35
/* The TaskHistory is used to show the last action on the task
36
 * on both the assigner and the assignee's side.
37
 *
38
 * It is used in combination with 'task_assigned_time' and 'tasklastdelegate'
39
 * or 'tasklastuser' to show the information at the top of the task request in
40
 * the format 'Accepted by <user> on 01-01-2010 11:00'.
41
 */
42
define('thNone', 0);
43
define('thAccepted', 1);			// Set by assignee
44
define('thDeclined', 2);			// Set by assignee
45
define('thUpdated', 3);				// Set by assignee
46
define('thDueDateChanged', 4);
47
define('thAssigned', 5);			// Set by assigner
48
49
/* The TaskState value is used to differentiate the version of a task
50
 * in the assigner's folder and the version in the
51
 * assignee's folder. The buttons shown depend on this and
52
 * the 'taskaccepted' boolean (for the assignee)
53
 */
54
define('tdsNOM', 0);		// Got a response to a deleted task, and re-created the task for the assigner
55
define('tdsOWNNEW', 1);		// Not assigned
56
define('tdsOWN', 2);		// Assignee version
57
define('tdsACC', 3);		// Assigner version
58
define('tdsDEC', 4);		// Assigner version, but assignee declined
59
60
/* The TaskAcceptanceState is used for the assigner to indicate state */
61
define('olTaskNotDelegated', 0);
62
define('olTaskDelegationUnknown', 1); // After sending req
63
define('olTaskDelegationAccepted', 2); // After receiving accept
64
define('olTaskDelegationDeclined', 3); // After receiving decline
65
66
/* The task ownership indicates the role of the current user relative to the task. */
67
define('olNewTask', 0);
68
define('olDelegatedTask', 1);	// Task has been assigned
69
define('olOwnTask', 2);			// Task owned
70
71
/* taskmultrecips indicates whether the task request sent or received has multiple assignees or not. */
72
define('tmrNone', 0);
73
define('tmrSent', 1);		// Task has been sent to multiple assignee
74
define('tmrReceived', 2);	// Task Request received has multiple assignee
75
76
// Task icon index.
77
define('ICON_TASK_ASSIGNEE', 0x00000502);
78
define('ICON_TASK_DECLINE', 0x00000506);
79
define('ICON_TASK_ASSIGNER', 0x00000503);
80
81
class TaskRequest {
82
	private $props;
83
84
	/**
85
	 * @var string
86
	 */
87
	private $taskCommentsInfo;
88
89
	// All recipient properties
90
	public $recipProps = [
91
		PR_ENTRYID,
92
		PR_DISPLAY_NAME,
93
		PR_EMAIL_ADDRESS,
94
		PR_RECIPIENT_ENTRYID,
95
		PR_RECIPIENT_TYPE,
96
		PR_SEND_INTERNET_ENCODING,
97
		PR_SEND_RICH_INFO,
98
		PR_RECIPIENT_DISPLAY_NAME,
99
		PR_ADDRTYPE,
100
		PR_DISPLAY_TYPE,
101
		PR_RECIPIENT_TRACKSTATUS,
102
		PR_RECIPIENT_TRACKSTATUS_TIME,
103
		PR_RECIPIENT_FLAGS,
104
		PR_ROWID,
105
		PR_SEARCH_KEY,
106
	];
107
108
	/**
109
	 * Constructor.
110
	 *
111
	 * Constructs a TaskRequest object for the specified message. This can be either the task request
112
	 * message itself (in the inbox) or the task in the tasks folder, depending on the action to be performed.
113
	 *
114
	 * As a general rule, the object message passed is the object 'in view' when the user performs one of the
115
	 * actions in this class.
116
	 *
117
	 * @param resource $store   MAPI Store in which $message resides. This is also the store where the tasks folder is assumed to be in
118
	 * @param resource $message MAPI Message to which the task request refers (can be an email or a task)
119
	 * @param resource $session MAPI Session which is used to open tasks folders for delegated task requests or responses
120
	 */
121
	public function __construct(private $store, private $message, private $session) {
122
		$this->taskCommentsInfo = '';
123
124
		$properties = [];
125
		$properties["owner"] = "PT_STRING8:PSETID_Task:0x811f";
126
		$properties["updatecount"] = "PT_LONG:PSETID_Task:0x8112";
127
		$properties["taskstate"] = "PT_LONG:PSETID_Task:0x8113";
128
		$properties["taskmultrecips"] = "PT_LONG:PSETID_Task:0x8120";
129
		$properties["taskupdates"] = "PT_BOOLEAN:PSETID_Task:0x811b";
130
		$properties["tasksoc"] = "PT_BOOLEAN:PSETID_Task:0x8119";
131
		$properties["taskhistory"] = "PT_LONG:PSETID_Task:0x811a";
132
		$properties["taskmode"] = "PT_LONG:PSETID_Common:0x8518";
133
		$properties["task_goid"] = "PT_BINARY:PSETID_Common:0x8519";
134
		$properties["complete"] = "PT_BOOLEAN:PSETID_Common:" . PidLidTaskComplete;
135
		$properties["task_assigned_time"] = "PT_SYSTIME:PSETID_Task:0x8115";
136
		$properties["taskfcreator"] = "PT_BOOLEAN:PSETID_Task:0x0x811e";
137
		$properties["tasklastuser"] = "PT_STRING8:PSETID_Task:0x8122";
138
		$properties["tasklastdelegate"] = "PT_STRING8:PSETID_Task:0x8125";
139
		$properties["taskaccepted"] = "PT_BOOLEAN:PSETID_Task:0x8108";
140
		$properties["task_acceptance_state"] = "PT_LONG:PSETID_Task:0x812a";
141
		$properties["ownership"] = "PT_LONG:PSETID_Task:0x8129";
142
143
		$properties["complete"] = "PT_BOOLEAN:PSETID_Task:" . PidLidTaskComplete;
144
		$properties["datecompleted"] = "PT_SYSTIME:PSETID_Task:" . PidLidTaskDateCompleted;
145
		$properties["recurring"] = "PT_BOOLEAN:PSETID_Task:0x8126";
146
		$properties["startdate"] = "PT_SYSTIME:PSETID_Task:" . PidLidTaskStartDate;
147
		$properties["duedate"] = "PT_SYSTIME:PSETID_Task:" . PidLidTaskDueDate;
148
		$properties["status"] = "PT_LONG:PSETID_Task:" . PidLidTaskStatus;
149
		$properties["percent_complete"] = "PT_DOUBLE:PSETID_Task:" . PidLidPercentComplete;
150
		$properties["totalwork"] = "PT_LONG:PSETID_Task:0x8111";
151
		$properties["actualwork"] = "PT_LONG:PSETID_Task:0x8110";
152
		$properties["categories"] = "PT_MV_STRING8:PS_PUBLIC_STRINGS:Keywords";
153
		$properties["companies"] = "PT_MV_STRING8:PSETID_Common:0x8539";
154
		$properties["mileage"] = "PT_STRING8:PSETID_Common:0x8534";
155
		$properties["billinginformation"] = "PT_STRING8:PSETID_Common:0x8535";
156
157
		$this->props = getPropIdsFromStrings($this->store, $properties);
158
	}
159
160
	// General functions
161
162
	/**
163
	 * Returns TRUE if the message pointed to is an incoming task request and should
164
	 * therefore be replied to with doAccept or doDecline().
165
	 *
166
	 * @param mixed $messageClass message class to use for checking
167
	 *
168
	 * @return bool true if this is a task request else false
169
	 */
170
	public function isTaskRequest($messageClass = false) {
171
		if ($messageClass === false) {
172
			$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

172
			$props = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_MESSAGE_CLASS]);
Loading history...
173
			$messageClass = $props[PR_MESSAGE_CLASS] ?? false;
174
		}
175
176
		if ($messageClass !== false && $messageClass === "IPM.TaskRequest") {
177
			return true;
178
		}
179
180
		return false;
181
	}
182
183
	/**
184
	 * Returns TRUE if the message pointed to is a returning task request response.
185
	 *
186
	 * @param mixed $messageClass message class to use for checking
187
	 *
188
	 * @return bool true if this is a task request else false
189
	 */
190
	public function isTaskRequestResponse($messageClass = false) {
191
		if ($messageClass === false) {
192
			$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

192
			$props = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_MESSAGE_CLASS]);
Loading history...
193
			$messageClass = $props[PR_MESSAGE_CLASS] ?? false;
194
		}
195
196
		if ($messageClass !== false && str_starts_with((string) $messageClass, "IPM.TaskRequest.")) {
197
			return true;
198
		}
199
200
		return false;
201
	}
202
203
	/**
204
	 * Returns TRUE if the message pointed to is an incoming task request/response.
205
	 *
206
	 * @param array $props The MAPI properties to check message is an incoming task request/response
207
	 *
208
	 * @return bool true if this is an incoming task request/response else false
209
	 */
210
	public function isReceivedItem($props) {
211
		return $props[PR_MESSAGE_TO_ME] ?? false;
212
	}
213
214
	/**
215
	 * Gets the task associated with an IPM.TaskRequest message.
216
	 *
217
	 * If the task does not exist yet, it is created, using the attachment object in the
218
	 * task request item.
219
	 *
220
	 * @param bool $create true - try create task in user's task folder if task does not exist
221
	 *                     false - find the associated task in user's task folder
222
	 *
223
	 * @return bool|resource associated task of task request else false
224
	 */
225
	public function getAssociatedTask($create) {
226
		$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS, $this->props['task_goid']]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

226
		$props = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_MESSAGE_CLASS, $this->props['task_goid']]);
Loading history...
227
228
		if ($props[PR_MESSAGE_CLASS] == "IPM.Task") {
229
			// Message itself is task, so return that
230
			return $this->message;
231
		}
232
233
		$taskFolder = $this->getDefaultTasksFolder();
234
		$goid = $props[$this->props['task_goid']];
235
236
		// Find the task by looking for the task_goid
237
		$restriction = [
238
			RES_PROPERTY,
239
			[
240
				RELOP => RELOP_EQ,
241
				ULPROPTAG => $this->props['task_goid'],
242
				VALUE => $goid,
243
			],
244
		];
245
246
		$contents = mapi_folder_getcontentstable($taskFolder);
0 ignored issues
show
Bug introduced by
It seems like $taskFolder can also be of type false; however, parameter $fld of mapi_folder_getcontentstable() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

246
		$contents = mapi_folder_getcontentstable(/** @scrutinizer ignore-type */ $taskFolder);
Loading history...
247
248
		$rows = mapi_table_queryallrows($contents, [PR_ENTRYID], $restriction);
249
250
		if (empty($rows)) {
251
			// None found, create one if possible
252
			if (!$create) {
253
				return false;
254
			}
255
256
			$task = mapi_folder_createmessage($taskFolder);
0 ignored issues
show
Bug introduced by
It seems like $taskFolder can also be of type false; however, parameter $fld of mapi_folder_createmessage() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

256
			$task = mapi_folder_createmessage(/** @scrutinizer ignore-type */ $taskFolder);
Loading history...
257
258
			$sub = $this->getEmbeddedTask();
259
260
			try {
261
				mapi_copyto($sub, [], [$this->props['categories']], $task);
0 ignored issues
show
Bug introduced by
$sub of type boolean|resource is incompatible with the type resource expected by parameter $src of mapi_copyto(). ( Ignorable by Annotation )

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

261
				mapi_copyto(/** @scrutinizer ignore-type */ $sub, [], [$this->props['categories']], $task);
Loading history...
262
			}
263
			catch (MAPIException $e) {
264
				if ($e->getCode() !== MAPI_E_INVALID_PARAMETER) {
265
					throw $e;
266
				}
267
				// Some stores reject excluding categories during copy when attachments are present.
268
				// Retry without exclusions to keep attachments intact and drop categories afterwards.
269
				mapi_copyto($sub, [], [], $task);
270
				mapi_deleteprops($task, [$this->props['categories']]);
271
			}
272
273
			$senderProps = [
274
				PR_SENT_REPRESENTING_NAME,
275
				PR_SENT_REPRESENTING_EMAIL_ADDRESS,
276
				PR_SENT_REPRESENTING_ENTRYID,
277
				PR_SENT_REPRESENTING_ADDRTYPE,
278
				PR_SENT_REPRESENTING_SEARCH_KEY,
279
				PR_SENDER_NAME,
280
				PR_SENDER_EMAIL_ADDRESS,
281
				PR_SENDER_ENTRYID,
282
				PR_SENDER_ADDRTYPE,
283
				PR_SENDER_SEARCH_KEY, ];
284
285
			// Copy sender information from the e-mail
286
			$props = mapi_getprops($this->message, $senderProps);
287
			$props[PR_MESSAGE_CLASS] = 'IPM.Task';
288
			mapi_setprops($task, $props);
289
		}
290
		else {
291
			// If there are multiple, just use the first
292
			$entryid = $rows[0][PR_ENTRYID];
293
294
			$store = $this->getTaskFolderStore();
295
			$task = mapi_msgstore_openentry($store, $entryid);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false and resource; however, parameter $store of mapi_msgstore_openentry() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

295
			$task = mapi_msgstore_openentry(/** @scrutinizer ignore-type */ $store, $entryid);
Loading history...
296
		}
297
298
		return $task;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $task returns the type resource which is incompatible with the documented return type boolean|resource.
Loading history...
299
	}
300
301
	/**
302
	 * Function which checks that if we have received a task request/response
303
	 * for an already updated task in task folder.
304
	 *
305
	 * @return bool true if task request is updated later
306
	 */
307
	public function isTaskRequestUpdated() {
308
		$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS, $this->props['task_goid'], $this->props['updatecount']]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

308
		$props = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_MESSAGE_CLASS, $this->props['task_goid'], $this->props['updatecount']]);
Loading history...
309
		$result = false;
310
		$associatedTask = $this->getAssociatedTask(false);
311
		if ($this->isTaskRequest($props[PR_MESSAGE_CLASS])) {
312
			if ($associatedTask) {
0 ignored issues
show
introduced by
$associatedTask is of type resource, thus it always evaluated to false.
Loading history...
313
				return true;
314
			}
315
			$folder = $this->getDefaultTasksFolder();
316
			$goid = $props[$this->props['task_goid']];
317
318
			// Find the task by looking for the task_goid
319
			$restriction = [
320
				RES_PROPERTY,
321
				[
322
					RELOP => RELOP_EQ,
323
					ULPROPTAG => $this->props['task_goid'],
324
					VALUE => $goid,
325
				],
326
			];
327
328
			$table = mapi_folder_getcontentstable($folder, MAPI_DEFERRED_ERRORS | SHOW_SOFT_DELETES);
0 ignored issues
show
Bug introduced by
It seems like $folder can also be of type false; however, parameter $fld of mapi_folder_getcontentstable() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

328
			$table = mapi_folder_getcontentstable(/** @scrutinizer ignore-type */ $folder, MAPI_DEFERRED_ERRORS | SHOW_SOFT_DELETES);
Loading history...
329
			$softDeletedItems = mapi_table_queryallrows($table, [PR_ENTRYID], $restriction);
330
			if (!empty($softDeletedItems)) {
331
				return true;
332
			}
333
		}
334
335
		if ($associatedTask !== false) {
0 ignored issues
show
introduced by
The condition $associatedTask !== false is always true.
Loading history...
336
			$taskItemProps = mapi_getprops($associatedTask, [$this->props['updatecount']]);
337
			/*
338
			 * if(message_counter < task_counter) task object is newer then task response (task is updated)
339
			 * if(message_counter >= task_counter) task is not updated, do normal processing
340
			 */
341
			if (isset($taskItemProps[$this->props['updatecount']], $props[$this->props['updatecount']])) {
342
				if ($props[$this->props['updatecount']] < $taskItemProps[$this->props['updatecount']]) {
343
					$result = true;
344
				}
345
			}
346
		}
347
348
		return $result;
349
	}
350
351
	// Organizer functions (called by the organizer)
352
353
	/**
354
	 * Processes a task request response, which can be any of the following:
355
	 * - Task accept (task history is marked as accepted)
356
	 * - Task decline (task history is marked as declined)
357
	 * - Task update (updates completion %, etc).
358
	 *
359
	 * @return true
360
	 */
361
	public function processTaskResponse(): bool {
362
		$messageProps = mapi_getprops($this->message, [PR_PROCESSED, $this->props["taskupdates"], PR_MESSAGE_TO_ME]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

362
		$messageProps = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_PROCESSED, $this->props["taskupdates"], PR_MESSAGE_TO_ME]);
Loading history...
363
		if (isset($messageProps[PR_PROCESSED]) && $messageProps[PR_PROCESSED]) {
364
			return true;
365
		}
366
		mapi_setprops($this->message, [PR_PROCESSED => true]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

366
		mapi_setprops(/** @scrutinizer ignore-type */ $this->message, [PR_PROCESSED => true]);
Loading history...
367
		mapi_savechanges($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_savechanges(). ( Ignorable by Annotation )

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

367
		mapi_savechanges(/** @scrutinizer ignore-type */ $this->message);
Loading history...
368
369
		// Get the embedded task information.
370
		$sub = $this->getEmbeddedTask();
371
		// OL saves the task related properties in the embedded message
372
		$subProps = mapi_getprops($sub, [$this->props["taskupdates"]]);
0 ignored issues
show
Bug introduced by
$sub of type boolean|resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

372
		$subProps = mapi_getprops(/** @scrutinizer ignore-type */ $sub, [$this->props["taskupdates"]]);
Loading history...
373
374
		// If task is updated in task folder then we don't need to process
375
		// old response
376
		if ($this->isTaskRequestUpdated()) {
377
			return true;
378
		}
379
380
		$isReceivedItem = $this->isReceivedItem($messageProps);
381
382
		$taskHistory = 0;
383
		$taskState = 0;
384
		$taskAcceptanceState = 0;
385
		$taskOwner = 0;
386
		$isCreateAssociatedTask = false;
387
		$isAllowUpdateAssociatedTask = $subProps[$this->props["taskupdates"]];
388
		$props = mapi_getprops($this->message, [PR_MESSAGE_CLASS]);
389
390
		// Set correct taskmode and taskhistory depending on response type
391
		switch ($props[PR_MESSAGE_CLASS]) {
392
			case 'IPM.TaskRequest.Accept':
393
				$taskHistory = thAccepted;
394
				$taskState = $isReceivedItem ? tdsACC : tdsOWN;
395
				$taskOwner = $isReceivedItem ? olDelegatedTask : olOwnTask;
396
				$taskAcceptanceState = $isReceivedItem ? olTaskDelegationAccepted : olTaskNotDelegated;
397
				break;
398
399
			case 'IPM.TaskRequest.Decline':
400
				$isCreateAssociatedTask = $isReceivedItem;
401
				$isAllowUpdateAssociatedTask = $isReceivedItem;
402
				$taskHistory = thDeclined;
403
				$taskState = $isReceivedItem ? tdsDEC : tdsACC;
404
				$taskOwner = $isReceivedItem ? olOwnTask : olDelegatedTask;
405
				$taskAcceptanceState = $isReceivedItem ? olTaskDelegationDeclined : olTaskDelegationUnknown;
406
				break;
407
408
			case 'IPM.TaskRequest.Update':
409
			case 'IPM.TaskRequest.Complete':
410
				$taskHistory = thUpdated;
411
				$taskState = $isReceivedItem ? tdsACC : tdsOWN;
412
				$taskAcceptanceState = olTaskNotDelegated;
413
				$taskOwner = $isReceivedItem ? olDelegatedTask : olOwnTask;
414
				break;
415
		}
416
417
		$props = [
418
			$this->props['taskhistory'] => $taskHistory,
419
			$this->props['taskstate'] => $taskState,
420
			$this->props['task_acceptance_state'] => $taskAcceptanceState,
421
			$this->props['ownership'] => $taskOwner,
422
		];
423
424
		// Get the task for this response
425
		$task = $this->getAssociatedTask($isCreateAssociatedTask);
426
		if ($task && $isAllowUpdateAssociatedTask) {
0 ignored issues
show
introduced by
$task is of type resource, thus it always evaluated to false.
Loading history...
427
			// To avoid duplication of attachments in associated task. we simple remove the
428
			// all attachments from associated task.
429
			$taskAttachTable = mapi_message_getattachmenttable($task);
430
			$taskAttachments = mapi_table_queryallrows($taskAttachTable, [PR_ATTACH_NUM]);
431
			foreach ($taskAttachments as $taskAttach) {
432
				mapi_message_deleteattach($task, $taskAttach[PR_ATTACH_NUM]);
433
			}
434
435
			$ignoreProps = [
436
				$this->props['taskstate'],
437
				$this->props['taskhistory'],
438
				$this->props['taskmode'],
439
				$this->props['taskfcreator'],
440
			];
441
			// Ignore PR_ICON_INDEX when task request response
442
			// is not received item.
443
			if ($isReceivedItem === false) {
444
				$ignoreProps[] = PR_ICON_INDEX;
445
			}
446
447
			// We copy all properties except taskstate, taskhistory, taskmode and taskfcreator properties
448
			// from $sub message to $task even also we copy all attachments from $sub to $task message.
449
			mapi_copyto($sub, [], $ignoreProps, $task);
450
			$senderProps = mapi_getprops($this->message, [
451
				PR_SENDER_NAME,
452
				PR_SENDER_EMAIL_ADDRESS,
453
				PR_SENDER_ENTRYID,
454
				PR_SENDER_ADDRTYPE,
455
				PR_SENDER_SEARCH_KEY,
456
				PR_MESSAGE_DELIVERY_TIME,
457
				PR_SENT_REPRESENTING_NAME,
458
				PR_SENT_REPRESENTING_EMAIL_ADDRESS,
459
				PR_SENT_REPRESENTING_ADDRTYPE,
460
				PR_SENT_REPRESENTING_ENTRYID,
461
				PR_SENT_REPRESENTING_SEARCH_KEY, ]);
462
463
			mapi_setprops($task, $senderProps);
464
465
			// Update taskstate and task history (last action done by the assignee)
466
			mapi_setprops($task, $props);
467
468
			// Copy missing properties from embedded task
469
			$subProperties = $this->getSubProperties();
470
			$subprops = mapi_getprops($sub, $subProperties);
471
			mapi_setprops($task, $subprops);
472
473
			mapi_savechanges($task);
474
		}
475
476
		mapi_setprops($this->message, $props);
477
		mapi_savechanges($this->message);
478
479
		if ($isReceivedItem) {
480
			$this->updateSentTaskRequest();
481
		}
482
483
		return true;
484
	}
485
486
	/**
487
	 * Update the sent task request in sent items folder.
488
	 *
489
	 * @return bool
490
	 */
491
	public function updateSentTaskRequest() {
492
		$props = mapi_getprops($this->message, [
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

492
		$props = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [
Loading history...
493
			$this->props['taskhistory'],
494
			$this->props["taskstate"],
495
			$this->props["ownership"],
496
			$this->props['task_goid'],
497
			$this->props['task_acceptance_state'],
498
			$this->props["tasklastuser"],
499
			$this->props["tasklastdelegate"], ]);
500
501
		$store = $this->getDefaultStore();
502
		$storeProps = mapi_getprops($store, [PR_IPM_SENTMAIL_ENTRYID]);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false; however, parameter $any of mapi_getprops() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

502
		$storeProps = mapi_getprops(/** @scrutinizer ignore-type */ $store, [PR_IPM_SENTMAIL_ENTRYID]);
Loading history...
503
504
		$sentFolder = mapi_msgstore_openentry($store, $storeProps[PR_IPM_SENTMAIL_ENTRYID]);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false; however, parameter $store of mapi_msgstore_openentry() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

504
		$sentFolder = mapi_msgstore_openentry(/** @scrutinizer ignore-type */ $store, $storeProps[PR_IPM_SENTMAIL_ENTRYID]);
Loading history...
505
		if (!$sentFolder) {
0 ignored issues
show
introduced by
$sentFolder is of type resource, thus it always evaluated to true.
Loading history...
506
			return false;
507
		}
508
509
		// Find the task by looking for the task_goid
510
		$restriction = [
511
			RES_PROPERTY,
512
			[
513
				RELOP => RELOP_EQ,
514
				ULPROPTAG => $this->props['task_goid'],
515
				VALUE => $props[$this->props['task_goid']],
516
			],
517
		];
518
519
		$contentsTable = mapi_folder_getcontentstable($sentFolder);
520
521
		$rows = mapi_table_queryallrows($contentsTable, [PR_ENTRYID], $restriction);
522
523
		if (!empty($rows)) {
524
			foreach ($rows as $row) {
525
				$sentTaskRequest = mapi_msgstore_openentry($store, $row[PR_ENTRYID]);
526
				mapi_setprops($sentTaskRequest, $props);
527
				mapi_setprops($sentTaskRequest, [PR_PROCESSED => true]);
528
				mapi_savechanges($sentTaskRequest);
529
			}
530
		}
531
532
		return true;
533
	}
534
535
	/**
536
	 * Creates a new message in the current user's outbox and submits it.
537
	 *
538
	 * Takes the task passed in the constructor as the task to be sent; recipient should
539
	 * be pre-existing. The task request will be sent to all recipients.
540
	 *
541
	 * @param string $prefix
542
	 *
543
	 * @return true
544
	 */
545
	public function sendTaskRequest($prefix): bool {
546
		// Check if the task is in a public folder (which is not mail-enabled)
547
		$taskFolderStore = $this->getTaskFolderStore();
548
		$storeType = mapi_getprops($taskFolderStore, [PR_MDB_PROVIDER]);
0 ignored issues
show
Bug introduced by
It seems like $taskFolderStore can also be of type false and resource; however, parameter $any of mapi_getprops() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

548
		$storeType = mapi_getprops(/** @scrutinizer ignore-type */ $taskFolderStore, [PR_MDB_PROVIDER]);
Loading history...
549
		if (isset($storeType[PR_MDB_PROVIDER]) && $storeType[PR_MDB_PROVIDER] === ZARAFA_STORE_PUBLIC_GUID) {
550
			throw new MAPIException(_("Cannot assign tasks in public folders. Public folders are not mail-enabled and cannot send task assignment notifications."), MAPI_E_NO_SUPPORT);
551
		}
552
553
		// Generate a TaskGlobalObjectId
554
		$taskid = $this->createTGOID();
555
		$messageprops = mapi_getprops($this->message, [PR_SUBJECT]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

555
		$messageprops = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_SUBJECT]);
Loading history...
556
557
		// Set properties on Task Request
558
		mapi_setprops($this->message, [
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

558
		mapi_setprops(/** @scrutinizer ignore-type */ $this->message, [
Loading history...
559
			$this->props['task_goid'] => $taskid, // our new task_goid
560
			$this->props['taskstate'] => tdsACC, // state for our outgoing request
561
			$this->props['taskmode'] => tdmtNothing, // we're not sending a change
562
			$this->props['updatecount'] => 2, // version 2 (no idea)
563
			$this->props['task_acceptance_state'] => olTaskDelegationUnknown, // no reply yet
564
			$this->props['ownership'] => olDelegatedTask, // Task has been assigned
565
			$this->props['taskhistory'] => thAssigned, // Task has been assigned
566
			PR_CONVERSATION_TOPIC => $messageprops[PR_SUBJECT],
567
			PR_ICON_INDEX => ICON_TASK_ASSIGNER,
568
		]);
569
		$this->setLastUser();
570
		$this->setOwnerForAssignor();
571
		mapi_savechanges($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_savechanges(). ( Ignorable by Annotation )

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

571
		mapi_savechanges(/** @scrutinizer ignore-type */ $this->message);
Loading history...
572
573
		// Create outgoing task request message
574
		$outgoing = $this->createOutgoingMessage();
575
576
		// No need to copy PR_ICON_INDEX and  PR_SENT_* information in to outgoing message.
577
		$ignoreProps = [PR_ICON_INDEX, PR_SENT_REPRESENTING_NAME, PR_SENT_REPRESENTING_EMAIL_ADDRESS, PR_SENT_REPRESENTING_ADDRTYPE, PR_SENT_REPRESENTING_ENTRYID, PR_SENT_REPRESENTING_SEARCH_KEY];
578
		mapi_copyto($this->message, [], $ignoreProps, $outgoing);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $src of mapi_copyto(). ( Ignorable by Annotation )

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

578
		mapi_copyto(/** @scrutinizer ignore-type */ $this->message, [], $ignoreProps, $outgoing);
Loading history...
579
580
		// Make it a task request, and put it in sent items after it is sent
581
		mapi_setprops($outgoing, [
582
			PR_MESSAGE_CLASS => "IPM.TaskRequest", // class is task request
583
			$this->props['taskstate'] => tdsOWN, // for the recipient he is the task owner
584
			$this->props['taskmode'] => tdmtTaskReq, // for the recipient it's a request
585
			$this->props['updatecount'] => 1, // version 2 is in the attachment
586
			PR_SUBJECT_PREFIX => $prefix,
587
			PR_SUBJECT => $prefix . $messageprops[PR_SUBJECT],
588
		]);
589
590
		$attach = mapi_message_createattach($outgoing);
591
		mapi_setprops($attach, [
592
			PR_ATTACH_METHOD => ATTACH_EMBEDDED_MSG,
593
			PR_ATTACHMENT_HIDDEN => true,
594
			PR_DISPLAY_NAME => $messageprops[PR_SUBJECT], ]);
595
596
		$sub = mapi_attach_openproperty($attach, PR_ATTACH_DATA_OBJ, IID_IMessage, 0, MAPI_MODIFY | MAPI_CREATE);
0 ignored issues
show
Bug introduced by
The function mapi_attach_openproperty 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

596
		$sub = /** @scrutinizer ignore-call */ mapi_attach_openproperty($attach, PR_ATTACH_DATA_OBJ, IID_IMessage, 0, MAPI_MODIFY | MAPI_CREATE);
Loading history...
597
598
		mapi_copyto($this->message, [], [], $sub);
599
		mapi_setprops($sub, [PR_MESSAGE_CLASS => 'IPM.Task']);
600
601
		mapi_savechanges($sub);
602
603
		mapi_savechanges($attach);
604
605
		mapi_savechanges($outgoing);
606
		mapi_message_submitmessage($outgoing);
607
608
		return true;
609
	}
610
611
	// Assignee functions (called by the assignee)
612
613
	/**
614
	 * Updates task version counter.
615
	 *
616
	 * Must be called before each update to increase counter.
617
	 */
618
	public function updateTaskRequest(): void {
619
		$messageprops = mapi_getprops($this->message, [$this->props['updatecount']]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

619
		$messageprops = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [$this->props['updatecount']]);
Loading history...
620
621
		if (isset($messageprops)) {
622
			++$messageprops[$this->props['updatecount']];
623
		}
624
		else {
625
			$messageprops[$this->props['updatecount']] = 1;
626
		}
627
628
		mapi_setprops($this->message, $messageprops);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

628
		mapi_setprops(/** @scrutinizer ignore-type */ $this->message, $messageprops);
Loading history...
629
	}
630
631
	/**
632
	 * Processes a task request.
633
	 *
634
	 * Message passed should be an IPM.TaskRequest message. The task request is then processed to create
635
	 * the task in the tasks folder if needed.
636
	 *
637
	 * @return bool
638
	 */
639
	public function processTaskRequest() {
640
		if (!$this->isTaskRequest()) {
641
			return false;
642
		}
643
		$messageProps = mapi_getprops($this->message, [PR_PROCESSED, $this->props["taskupdates"], PR_MESSAGE_TO_ME]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

643
		$messageProps = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_PROCESSED, $this->props["taskupdates"], PR_MESSAGE_TO_ME]);
Loading history...
644
		if (isset($messageProps[PR_PROCESSED]) && $messageProps[PR_PROCESSED]) {
645
			return true;
646
		}
647
648
		// if task is updated in task folder then we don't need to process
649
		// old request.
650
		if ($this->isTaskRequestUpdated()) {
651
			return true;
652
		}
653
654
		$isReceivedItem = $this->isReceivedItem($messageProps);
655
656
		$props = [];
657
		$props[PR_PROCESSED] = true;
658
		$props[$this->props["taskstate"]] = $isReceivedItem ? tdsOWN : tdsACC;
659
		$props[$this->props["ownership"]] = $isReceivedItem ? olOwnTask : olDelegatedTask;
660
661
		mapi_setprops($this->message, $props);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

661
		mapi_setprops(/** @scrutinizer ignore-type */ $this->message, $props);
Loading history...
662
		mapi_savechanges($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_savechanges(). ( Ignorable by Annotation )

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

662
		mapi_savechanges(/** @scrutinizer ignore-type */ $this->message);
Loading history...
663
664
		// Don't create associated task in task folder if "taskupdates" is not true.
665
		if (!$isReceivedItem && !$messageProps[$this->props["taskupdates"]]) {
666
			return true;
667
		}
668
		// create an associated task in task folder while
669
		// reading/loading task request on client side.
670
		$task = $this->getAssociatedTask(true);
671
672
		$taskProps = mapi_getprops($task, [$this->props['taskmultrecips']]);
673
		$taskProps[$this->props["taskstate"]] = $isReceivedItem ? tdsOWN : tdsACC;
674
		$taskProps[$this->props["taskhistory"]] = thAssigned;
675
		$taskProps[$this->props["taskmode"]] = tdmtNothing;
676
		$taskProps[$this->props["taskaccepted"]] = false;
677
		$taskProps[$this->props["taskfcreator"]] = false;
678
		$taskProps[$this->props["ownership"]] = $isReceivedItem ? olOwnTask : olDelegatedTask;
679
		$taskProps[$this->props["task_acceptance_state"]] = olTaskNotDelegated;
680
		$taskProps[PR_ICON_INDEX] = ICON_TASK_ASSIGNEE;
681
682
		mapi_setprops($task, $taskProps);
683
		$this->setAssignorInRecipients($task);
684
685
		mapi_savechanges($task);
686
687
		return true;
688
	}
689
690
	/**
691
	 * Accepts a task request and sends the response.
692
	 *
693
	 * Message passed should be an IPM.Task (eg the task from getAssociatedTask())
694
	 *
695
	 * Copies the task to the user's task folder, sets it to accepted, and sends the acceptation
696
	 * message back to the organizer. The caller is responsible for removing the message.
697
	 *
698
	 * @return array|false PR_ENTRYID, PR_STORE_ENTRYID and PR_PARENT_ENTRYID of the task
699
	 */
700
	public function doAccept() {
701
		$prefix = _("Task Accepted:") . " ";
702
		$messageProps = mapi_getprops($this->message, [PR_MESSAGE_CLASS, $this->props['taskstate']]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

702
		$messageProps = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_MESSAGE_CLASS, $this->props['taskstate']]);
Loading history...
703
704
		if (!isset($messageProps[$this->props['taskstate']]) || $messageProps[$this->props['taskstate']] != tdsOWN) {
705
			// Can only accept assignee task
706
			return false;
707
		}
708
709
		$this->setLastUser();
710
		$this->updateTaskRequest();
711
712
		$props = [
713
			$this->props['taskhistory'] => thAccepted,
714
			$this->props['task_assigned_time'] => time(),
715
			$this->props['taskaccepted'] => true,
716
			$this->props['task_acceptance_state'] => olTaskNotDelegated, ];
717
718
		// Message is TaskRequest then update the associated task as well.
719
		if ($this->isTaskRequest($messageProps[PR_MESSAGE_CLASS])) {
720
			$task = $this->getAssociatedTask(false);
721
			if ($task) {
0 ignored issues
show
introduced by
$task is of type resource, thus it always evaluated to false.
Loading history...
722
				mapi_setprops($task, $props);
723
				mapi_savechanges($task);
724
			}
725
		}
726
727
		// Set as accepted
728
		mapi_setprops($this->message, $props);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

728
		mapi_setprops(/** @scrutinizer ignore-type */ $this->message, $props);
Loading history...
729
730
		// As we copy the all properties from received message we need to remove following
731
		// properties from accept response.
732
		mapi_deleteprops($this->message, [PR_MESSAGE_RECIP_ME, PR_MESSAGE_TO_ME, PR_MESSAGE_CC_ME, PR_PROCESSED]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_deleteprops(). ( Ignorable by Annotation )

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

732
		mapi_deleteprops(/** @scrutinizer ignore-type */ $this->message, [PR_MESSAGE_RECIP_ME, PR_MESSAGE_TO_ME, PR_MESSAGE_CC_ME, PR_PROCESSED]);
Loading history...
733
734
		mapi_savechanges($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_savechanges(). ( Ignorable by Annotation )

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

734
		mapi_savechanges(/** @scrutinizer ignore-type */ $this->message);
Loading history...
735
736
		$this->sendResponse(tdmtTaskAcc, $prefix);
737
738
		return $this->deleteReceivedTR();
739
	}
740
741
	/**
742
	 * Declines a task request and sends the response.
743
	 *
744
	 * Passed message must be a task request message, ie isTaskRequest() must return TRUE.
745
	 *
746
	 * Sends the decline message back to the organizer. The caller is responsible for removing the message.
747
	 *
748
	 * @return array|false TRUE on success, FALSE on failure
749
	 */
750
	public function doDecline() {
751
		$prefix = _("Task Declined:") . " ";
752
		$messageProps = mapi_getprops($this->message, [$this->props['taskstate']]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

752
		$messageProps = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [$this->props['taskstate']]);
Loading history...
753
754
		if (!isset($messageProps[$this->props['taskstate']]) || $messageProps[$this->props['taskstate']] != tdsOWN) {
755
			return false; // Can only decline assignee task
756
		}
757
758
		$this->setLastUser();
759
		$this->updateTaskRequest();
760
761
		// Set as declined
762
		mapi_setprops($this->message, [
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

762
		mapi_setprops(/** @scrutinizer ignore-type */ $this->message, [
Loading history...
763
			$this->props['taskhistory'] => thDeclined,
764
			$this->props['task_acceptance_state'] => olTaskDelegationDeclined,
765
		]);
766
		mapi_deleteprops($this->message, [PR_MESSAGE_RECIP_ME, PR_MESSAGE_TO_ME, PR_MESSAGE_CC_ME, PR_PROCESSED]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_deleteprops(). ( Ignorable by Annotation )

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

766
		mapi_deleteprops(/** @scrutinizer ignore-type */ $this->message, [PR_MESSAGE_RECIP_ME, PR_MESSAGE_TO_ME, PR_MESSAGE_CC_ME, PR_PROCESSED]);
Loading history...
767
		mapi_savechanges($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_savechanges(). ( Ignorable by Annotation )

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

767
		mapi_savechanges(/** @scrutinizer ignore-type */ $this->message);
Loading history...
768
769
		$this->sendResponse(tdmtTaskDec, $prefix);
770
771
		// Delete the associated task when task request is declined by the assignee.
772
		$task = $this->getAssociatedTask(false);
773
		if ($task) {
0 ignored issues
show
introduced by
$task is of type resource, thus it always evaluated to false.
Loading history...
774
			$taskFolder = $this->getDefaultTasksFolder();
775
			$props = mapi_getprops($task, [PR_ENTRYID]);
776
			mapi_folder_deletemessages($taskFolder, [$props[PR_ENTRYID]]);
777
		}
778
779
		return $this->deleteReceivedTR();
780
	}
781
782
	/**
783
	 * Sends an update of the task if requested, and sends the Status-On-Completion report if complete and requested.
784
	 *
785
	 * If no updates were requested from the organizer, this function does nothing.
786
	 *
787
	 * @return bool TRUE if the update succeeded, FALSE otherwise
788
	 */
789
	public function doUpdate() {
790
		$messageProps = mapi_getprops($this->message, [$this->props['taskstate'], PR_SUBJECT]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

790
		$messageProps = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [$this->props['taskstate'], PR_SUBJECT]);
Loading history...
791
792
		if (!isset($messageProps[$this->props['taskstate']]) || $messageProps[$this->props['taskstate']] != tdsOWN) {
793
			return false; // Can only update assignee task
794
		}
795
796
		$this->setLastUser();
797
		$this->updateTaskRequest();
798
799
		// Set as updated
800
		mapi_setprops($this->message, [$this->props['taskhistory'] => thUpdated]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

800
		mapi_setprops(/** @scrutinizer ignore-type */ $this->message, [$this->props['taskhistory'] => thUpdated]);
Loading history...
801
802
		mapi_savechanges($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_savechanges(). ( Ignorable by Annotation )

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

802
		mapi_savechanges(/** @scrutinizer ignore-type */ $this->message);
Loading history...
803
804
		$props = mapi_getprops($this->message, [$this->props['taskupdates'], $this->props['tasksoc'], $this->props['recurring'], $this->props['complete']]);
805
		if (!$props[$this->props['complete']] && $props[$this->props['taskupdates']] && !(isset($props[$this->props['recurring']]) && $props[$this->props['recurring']])) {
806
			$this->sendResponse(tdmtTaskUpd, _("Task Updated:") . " ");
807
		}
808
		elseif ($props[$this->props['complete']]) {
809
			$this->sendResponse(tdmtTaskUpd, _("Task Completed:") . " ");
810
		}
811
812
		return true;
813
	}
814
815
	/**
816
	 * Gets the store associated with the task.
817
	 *
818
	 * Normally this will just open the store that the processed message is in. However, if the message is opened
819
	 * by a delegate, this function opens the store that the message was delegated from.
820
	 */
821
	public function getTaskFolderStore() {
822
		$ownerentryid = false;
823
824
		$rcvdprops = mapi_getprops($this->message, [PR_RCVD_REPRESENTING_ENTRYID]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

824
		$rcvdprops = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_RCVD_REPRESENTING_ENTRYID]);
Loading history...
825
		if (isset($rcvdprops[PR_RCVD_REPRESENTING_ENTRYID])) {
826
			$ownerentryid = $rcvdprops[PR_RCVD_REPRESENTING_ENTRYID];
827
		}
828
829
		if (!$ownerentryid) {
830
			$store = $this->store;
831
		}
832
		else {
833
			$ab = mapi_openaddressbook($this->session);
0 ignored issues
show
Bug introduced by
$this->session of type resource is incompatible with the type resource expected by parameter $session of mapi_openaddressbook(). ( Ignorable by Annotation )

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

833
			$ab = mapi_openaddressbook(/** @scrutinizer ignore-type */ $this->session);
Loading history...
834
			if (!$ab) {
0 ignored issues
show
introduced by
$ab is of type resource, thus it always evaluated to true.
Loading history...
835
				return false;
836
			}
837
838
			$mailuser = mapi_ab_openentry($ab, $ownerentryid);
839
			if (!$mailuser) {
0 ignored issues
show
introduced by
$mailuser is of type resource, thus it always evaluated to true.
Loading history...
840
				return false;
841
			}
842
843
			$mailuserprops = mapi_getprops($mailuser, [PR_EMAIL_ADDRESS]);
844
			if (!isset($mailuserprops[PR_EMAIL_ADDRESS])) {
845
				return false;
846
			}
847
848
			$storeid = mapi_msgstore_createentryid($this->store, $mailuserprops[PR_EMAIL_ADDRESS]);
0 ignored issues
show
Bug introduced by
$this->store of type resource is incompatible with the type resource expected by parameter $store of mapi_msgstore_createentryid(). ( Ignorable by Annotation )

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

848
			$storeid = mapi_msgstore_createentryid(/** @scrutinizer ignore-type */ $this->store, $mailuserprops[PR_EMAIL_ADDRESS]);
Loading history...
849
850
			$store = mapi_openmsgstore($this->session, $storeid);
0 ignored issues
show
Bug introduced by
$this->session of type resource is incompatible with the type resource expected by parameter $ses of mapi_openmsgstore(). ( Ignorable by Annotation )

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

850
			$store = mapi_openmsgstore(/** @scrutinizer ignore-type */ $this->session, $storeid);
Loading history...
851
		}
852
853
		return $store;
854
	}
855
856
	/**
857
	 * Opens the default task folder for the current user, or the specified user if passed.
858
	 */
859
	public function getDefaultTasksFolder() {
860
		$store = $this->getTaskFolderStore();
861
862
		$inbox = mapi_msgstore_getreceivefolder($store);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false and resource; however, parameter $store of mapi_msgstore_getreceivefolder() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

862
		$inbox = mapi_msgstore_getreceivefolder(/** @scrutinizer ignore-type */ $store);
Loading history...
863
		$inboxprops = mapi_getprops($inbox, [PR_IPM_TASK_ENTRYID]);
864
		if (!isset($inboxprops[PR_IPM_TASK_ENTRYID])) {
865
			return false;
866
		}
867
868
		return mapi_msgstore_openentry($store, $inboxprops[PR_IPM_TASK_ENTRYID]);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false and resource; however, parameter $store of mapi_msgstore_openentry() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

868
		return mapi_msgstore_openentry(/** @scrutinizer ignore-type */ $store, $inboxprops[PR_IPM_TASK_ENTRYID]);
Loading history...
869
	}
870
871
	/**
872
	 * Prepares the sent representing properties from given MAPI store.
873
	 *
874
	 * @param mixed $store MAPI store object
875
	 *
876
	 * @return array[][][][][]|false if store is not mail box owner entryid then return false else prepare the sent representing props and return it
877
	 *
878
	 * @psalm-return array<array<array<array<array<array<never, never>>>>>>|false
879
	 */
880
	public function getSentReprProps($store) {
881
		$storeprops = mapi_getprops($store, [PR_MAILBOX_OWNER_ENTRYID]);
882
		if (!isset($storeprops[PR_MAILBOX_OWNER_ENTRYID])) {
883
			return false;
884
		}
885
886
		$ab = mapi_openaddressbook($this->session);
0 ignored issues
show
Bug introduced by
$this->session of type resource is incompatible with the type resource expected by parameter $session of mapi_openaddressbook(). ( Ignorable by Annotation )

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

886
		$ab = mapi_openaddressbook(/** @scrutinizer ignore-type */ $this->session);
Loading history...
887
		$mailuser = mapi_ab_openentry($ab, $storeprops[PR_MAILBOX_OWNER_ENTRYID]);
888
		$mailuserprops = mapi_getprops($mailuser, [PR_ADDRTYPE, PR_EMAIL_ADDRESS, PR_DISPLAY_NAME, PR_SEARCH_KEY, PR_ENTRYID]);
889
890
		$props = [];
891
		$props[PR_SENT_REPRESENTING_ADDRTYPE] = $mailuserprops[PR_ADDRTYPE];
892
		$props[PR_SENT_REPRESENTING_EMAIL_ADDRESS] = $mailuserprops[PR_EMAIL_ADDRESS];
893
		$props[PR_SENT_REPRESENTING_NAME] = $mailuserprops[PR_DISPLAY_NAME];
894
		$props[PR_SENT_REPRESENTING_SEARCH_KEY] = $mailuserprops[PR_SEARCH_KEY];
895
		$props[PR_SENT_REPRESENTING_ENTRYID] = $mailuserprops[PR_ENTRYID];
896
897
		return $props;
898
	}
899
900
	/**
901
	 * Creates an outgoing message based on the passed message - will set delegate information
902
	 * and sent mail folder.
903
	 */
904
	public function createOutgoingMessage() {
905
		// Open our default store for this user (that's the only store we can submit in)
906
		$store = $this->getDefaultStore();
907
		$storeprops = mapi_getprops($store, [PR_IPM_OUTBOX_ENTRYID, PR_IPM_SENTMAIL_ENTRYID]);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false; however, parameter $any of mapi_getprops() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

907
		$storeprops = mapi_getprops(/** @scrutinizer ignore-type */ $store, [PR_IPM_OUTBOX_ENTRYID, PR_IPM_SENTMAIL_ENTRYID]);
Loading history...
908
909
		$outbox = mapi_msgstore_openentry($store, $storeprops[PR_IPM_OUTBOX_ENTRYID]);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false; however, parameter $store of mapi_msgstore_openentry() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

909
		$outbox = mapi_msgstore_openentry(/** @scrutinizer ignore-type */ $store, $storeprops[PR_IPM_OUTBOX_ENTRYID]);
Loading history...
910
		if (!$outbox) {
0 ignored issues
show
introduced by
$outbox is of type resource, thus it always evaluated to true.
Loading history...
911
			return false;
912
		}
913
914
		$outgoing = mapi_folder_createmessage($outbox);
915
		if (!$outgoing) {
0 ignored issues
show
introduced by
$outgoing is of type resource, thus it always evaluated to true.
Loading history...
916
			return false;
917
		}
918
919
		// Set SENT_REPRESENTING in case we're sending as a delegate
920
		$ownerstore = $this->getTaskFolderStore();
921
		$sentreprprops = $this->getSentReprProps($ownerstore);
922
923
		// Check if getSentReprProps returned false (e.g., for public folders which are not mail-enabled)
924
		if ($sentreprprops !== false) {
0 ignored issues
show
introduced by
The condition $sentreprprops !== false is always false.
Loading history...
925
			mapi_setprops($outgoing, $sentreprprops);
926
		}
927
928
		mapi_setprops($outgoing, [PR_SENTMAIL_ENTRYID => $storeprops[PR_IPM_SENTMAIL_ENTRYID]]);
929
930
		return $outgoing;
931
	}
932
933
	/**
934
	 * Sends a response message (from assignee back to organizer).
935
	 *
936
	 * @param int   $type   Type of response (tdmtTaskAcc, tdmtTaskDec, tdmtTaskUpd)
937
	 * @param mixed $prefix
938
	 *
939
	 * @return bool TRUE on success
940
	 */
941
	public function sendResponse($type, $prefix) {
942
		// Create a message in our outbox
943
		$outgoing = $this->createOutgoingMessage();
944
		$messageprops = mapi_getprops($this->message, [PR_CONVERSATION_TOPIC, PR_MESSAGE_CLASS, $this->props['complete']]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

944
		$messageprops = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [PR_CONVERSATION_TOPIC, PR_MESSAGE_CLASS, $this->props['complete']]);
Loading history...
945
946
		$attach = mapi_message_createattach($outgoing);
947
		mapi_setprops($attach, [PR_ATTACH_METHOD => ATTACH_EMBEDDED_MSG, PR_DISPLAY_NAME => $messageprops[PR_CONVERSATION_TOPIC], PR_ATTACHMENT_HIDDEN => true]);
948
		$sub = mapi_attach_openproperty($attach, PR_ATTACH_DATA_OBJ, IID_IMessage, 0, MAPI_CREATE | MAPI_MODIFY);
0 ignored issues
show
Bug introduced by
The function mapi_attach_openproperty 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

948
		$sub = /** @scrutinizer ignore-call */ mapi_attach_openproperty($attach, PR_ATTACH_DATA_OBJ, IID_IMessage, 0, MAPI_CREATE | MAPI_MODIFY);
Loading history...
949
950
		$message = !$this->isTaskRequest() ? $this->message : $this->getAssociatedTask(false);
951
952
		$ignoreProps = [PR_ICON_INDEX, $this->props["categories"], PR_SENT_REPRESENTING_NAME, PR_SENT_REPRESENTING_EMAIL_ADDRESS, PR_SENT_REPRESENTING_ADDRTYPE, PR_SENT_REPRESENTING_ENTRYID, PR_SENT_REPRESENTING_SEARCH_KEY];
953
954
		mapi_copyto($message, [], $ignoreProps, $outgoing);
0 ignored issues
show
Bug introduced by
$message of type resource is incompatible with the type resource expected by parameter $src of mapi_copyto(). ( Ignorable by Annotation )

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

954
		mapi_copyto(/** @scrutinizer ignore-type */ $message, [], $ignoreProps, $outgoing);
Loading history...
955
		mapi_copyto($message, [], [], $sub);
956
957
		if (!$this->setRecipientsForResponse($outgoing, $type)) {
958
			return false;
959
		}
960
961
		$props = [];
962
963
		switch ($type) {
964
			case tdmtTaskAcc:
965
				$props[PR_MESSAGE_CLASS] = "IPM.TaskRequest.Accept";
966
				mapi_setprops($sub, [PR_ICON_INDEX => ICON_TASK_ASSIGNER]);
967
				break;
968
969
			case tdmtTaskDec:
970
				$props[PR_MESSAGE_CLASS] = "IPM.TaskRequest.Decline";
971
				mapi_setprops($sub, [PR_ICON_INDEX => ICON_TASK_DECLINE]);
972
				break;
973
974
			case tdmtTaskUpd:
975
				mapi_setprops($sub, [PR_ICON_INDEX => ICON_TASK_ASSIGNER]);
976
				if ($messageprops[$this->props['complete']]) {
977
					$props[PR_MESSAGE_CLASS] = "IPM.TaskRequest.Complete";
978
				}
979
				else {
980
					$props[PR_MESSAGE_CLASS] = "IPM.TaskRequest.Update";
981
				}
982
983
				break;
984
		}
985
986
		mapi_savechanges($sub);
987
		mapi_savechanges($attach);
988
989
		$props[PR_SUBJECT] = $prefix . $messageprops[PR_CONVERSATION_TOPIC];
990
		$props[$this->props['taskmode']] = $type;
991
		$props[$this->props['task_assigned_time']] = time();
992
993
		mapi_setprops($outgoing, $props);
994
995
		// taskCommentsInfo contains some comments which added by assignee while
996
		// edit response before sending task response.
997
		if ($this->taskCommentsInfo != '') {
998
			$comments = $this->getTaskCommentsInfo();
999
			$stream = mapi_openproperty($outgoing, PR_BODY, IID_IStream, STGM_TRANSACTED, MAPI_CREATE | MAPI_MODIFY);
1000
			mapi_stream_setsize($stream, strlen($comments));
1001
			mapi_stream_write($stream, $comments);
1002
			mapi_stream_commit($stream);
1003
		}
1004
1005
		mapi_savechanges($outgoing);
1006
		mapi_message_submitmessage($outgoing);
1007
1008
		return true;
1009
	}
1010
1011
	public function getDefaultStore() {
1012
		$table = mapi_getmsgstorestable($this->session);
0 ignored issues
show
Bug introduced by
$this->session of type resource is incompatible with the type resource expected by parameter $session of mapi_getmsgstorestable(). ( Ignorable by Annotation )

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

1012
		$table = mapi_getmsgstorestable(/** @scrutinizer ignore-type */ $this->session);
Loading history...
1013
		$rows = mapi_table_queryallrows($table, [PR_DEFAULT_STORE, PR_ENTRYID]);
1014
1015
		foreach ($rows as $row) {
1016
			if ($row[PR_DEFAULT_STORE]) {
1017
				return mapi_openmsgstore($this->session, $row[PR_ENTRYID]);
0 ignored issues
show
Bug introduced by
$this->session of type resource is incompatible with the type resource expected by parameter $ses of mapi_openmsgstore(). ( Ignorable by Annotation )

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

1017
				return mapi_openmsgstore(/** @scrutinizer ignore-type */ $this->session, $row[PR_ENTRYID]);
Loading history...
1018
			}
1019
		}
1020
1021
		return false;
1022
	}
1023
1024
	/**
1025
	 * Creates a new TaskGlobalObjId.
1026
	 *
1027
	 * Just 16 bytes of random data
1028
	 */
1029
	public function createTGOID(): string {
1030
		$goid = "";
1031
		for ($i = 0; $i < 16; ++$i) {
1032
			$goid .= chr(random_int(0, 255));
1033
		}
1034
1035
		return $goid;
1036
	}
1037
1038
	/**
1039
	 * Gets the embedded task of task request. Further used to
1040
	 * create/update associated task of assigner/assignee.
1041
	 *
1042
	 * @return bool|resource embedded task if found else false
1043
	 */
1044
	public function getEmbeddedTask() {
1045
		$task = false;
1046
		$goid = mapi_getprops($this->message, [$this->props["task_goid"]]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

1046
		$goid = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [$this->props["task_goid"]]);
Loading history...
1047
		$attachmentTable = mapi_message_getattachmenttable($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $msg of mapi_message_getattachmenttable(). ( Ignorable by Annotation )

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

1047
		$attachmentTable = mapi_message_getattachmenttable(/** @scrutinizer ignore-type */ $this->message);
Loading history...
1048
		$restriction = [RES_PROPERTY,
1049
			[RELOP => RELOP_EQ,
1050
				ULPROPTAG => PR_ATTACH_METHOD,
1051
				VALUE => ATTACH_EMBEDDED_MSG, ],
1052
		];
1053
		$rows = mapi_table_queryallrows($attachmentTable, [PR_ATTACH_NUM], $restriction);
1054
1055
		if (empty($rows)) {
1056
			return $task;
1057
		}
1058
1059
		foreach ($rows as $row) {
1060
			try {
1061
				$attach = mapi_message_openattach($this->message, $row[PR_ATTACH_NUM]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $msg of mapi_message_openattach(). ( Ignorable by Annotation )

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

1061
				$attach = mapi_message_openattach(/** @scrutinizer ignore-type */ $this->message, $row[PR_ATTACH_NUM]);
Loading history...
1062
				$task = mapi_attach_openobj($attach);
1063
			}
1064
			catch (MAPIException) {
1065
				continue;
1066
			}
1067
1068
			$taskGoid = mapi_getprops($task, [$this->props["task_goid"]]);
1069
			if ($goid[$this->props["task_goid"]] === $taskGoid[$this->props["task_goid"]]) {
1070
				mapi_setprops($attach, [PR_ATTACHMENT_HIDDEN => true]);
1071
				mapi_savechanges($attach);
1072
				mapi_savechanges($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_savechanges(). ( Ignorable by Annotation )

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

1072
				mapi_savechanges(/** @scrutinizer ignore-type */ $this->message);
Loading history...
1073
				break;
1074
			}
1075
		}
1076
1077
		return $task;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $task also could return the type resource which is incompatible with the documented return type boolean|resource.
Loading history...
1078
	}
1079
1080
	/**
1081
	 * Sets the user name who has last used this task. Update the
1082
	 * tasklastdelegate and task_assigned_time.
1083
	 */
1084
	public function setLastUser(): void {
1085
		$delegatestore = $this->getDefaultStore();
1086
		$taskstore = $this->getTaskFolderStore();
1087
1088
		$delegateprops = mapi_getprops($delegatestore, [PR_MAILBOX_OWNER_NAME]);
0 ignored issues
show
Bug introduced by
It seems like $delegatestore can also be of type false; however, parameter $any of mapi_getprops() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1088
		$delegateprops = mapi_getprops(/** @scrutinizer ignore-type */ $delegatestore, [PR_MAILBOX_OWNER_NAME]);
Loading history...
1089
		$taskprops = mapi_getprops($taskstore, [PR_MAILBOX_OWNER_NAME]);
0 ignored issues
show
Bug introduced by
It seems like $taskstore can also be of type false and resource; however, parameter $any of mapi_getprops() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1089
		$taskprops = mapi_getprops(/** @scrutinizer ignore-type */ $taskstore, [PR_MAILBOX_OWNER_NAME]);
Loading history...
1090
1091
		// The owner of the task
1092
		$username = $delegateprops[PR_MAILBOX_OWNER_NAME];
1093
		// This is me (the one calling the script)
1094
		$delegate = $taskprops[PR_MAILBOX_OWNER_NAME];
1095
1096
		if ($this->isTaskRequest()) {
1097
			$task = $this->getAssociatedTask(false);
1098
			mapi_setprops($task, [
0 ignored issues
show
Bug introduced by
$task of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

1098
			mapi_setprops(/** @scrutinizer ignore-type */ $task, [
Loading history...
1099
				$this->props["tasklastuser"] => $username,
1100
				$this->props["tasklastdelegate"] => $delegate,
1101
				$this->props['task_assigned_time'] => time(),
1102
			]);
1103
			mapi_savechanges($task);
0 ignored issues
show
Bug introduced by
$task of type resource is incompatible with the type resource expected by parameter $any of mapi_savechanges(). ( Ignorable by Annotation )

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

1103
			mapi_savechanges(/** @scrutinizer ignore-type */ $task);
Loading history...
1104
		}
1105
		mapi_setprops($this->message, [
1106
			$this->props["tasklastuser"] => $username,
1107
			$this->props["tasklastdelegate"] => $delegate,
1108
			$this->props['task_assigned_time'] => time(),
1109
		]);
1110
	}
1111
1112
	/**
1113
	 * Sets assignee as owner in the assignor's copy of task.
1114
	 * Assignee becomes the owner when a user/assignor assigns any task to someone.
1115
	 * There can be more than one assignee.
1116
	 */
1117
	public function setOwnerForAssignor(): void {
1118
		$recipTable = mapi_message_getrecipienttable($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $msg of mapi_message_getrecipienttable(). ( Ignorable by Annotation )

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

1118
		$recipTable = mapi_message_getrecipienttable(/** @scrutinizer ignore-type */ $this->message);
Loading history...
1119
		$recips = mapi_table_queryallrows($recipTable, [PR_DISPLAY_NAME]);
1120
1121
		if (!empty($recips)) {
1122
			$owner = [];
1123
			foreach ($recips as $value) {
1124
				$owner[] = $value[PR_DISPLAY_NAME];
1125
			}
1126
1127
			$props = [$this->props['owner'] => implode("; ", $owner)];
1128
			mapi_setprops($this->message, $props);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

1128
			mapi_setprops(/** @scrutinizer ignore-type */ $this->message, $props);
Loading history...
1129
		}
1130
	}
1131
1132
	/**
1133
	 * Sets assignor as recipients in assignee's copy of task.
1134
	 *
1135
	 * If assignor has requested task updates then the assignor is added as recipient type MAPI_CC.
1136
	 *
1137
	 * Also if assignor has request SOC then the assignor is also add as recipient type MAPI_BCC
1138
	 *
1139
	 * @param mixed $task assignee's copy of task
1140
	 */
1141
	public function setAssignorInRecipients($task): void {
1142
		$recipTable = mapi_message_getrecipienttable($task);
1143
1144
		// Delete all MAPI_TO recipients
1145
		$recips = mapi_table_queryallrows($recipTable, [PR_ROWID], [
1146
			RES_PROPERTY,
1147
			[
1148
				RELOP => RELOP_EQ,
1149
				ULPROPTAG => PR_RECIPIENT_TYPE,
1150
				VALUE => MAPI_TO,
1151
			],
1152
		]);
1153
		foreach ($recips as $recip) {
1154
			mapi_message_modifyrecipients($task, MODRECIP_REMOVE, [$recip]);
1155
		}
1156
1157
		$recips = [];
1158
		$taskReqProps = mapi_getprops($this->message, [
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

1158
		$taskReqProps = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [
Loading history...
1159
			PR_SENT_REPRESENTING_NAME,
1160
			PR_SENT_REPRESENTING_EMAIL_ADDRESS,
1161
			PR_SENT_REPRESENTING_ENTRYID,
1162
			PR_SENT_REPRESENTING_ADDRTYPE,
1163
			PR_SENT_REPRESENTING_SEARCH_KEY,
1164
		]);
1165
		$associatedTaskProps = mapi_getprops($task, [
1166
			$this->props['taskupdates'],
1167
			$this->props['tasksoc'],
1168
			$this->props['taskmultrecips'],
1169
		]);
1170
1171
		// Build assignor info
1172
		$assignor = [
1173
			PR_ENTRYID => $taskReqProps[PR_SENT_REPRESENTING_ENTRYID],
1174
			PR_DISPLAY_NAME => $taskReqProps[PR_SENT_REPRESENTING_NAME],
1175
			PR_EMAIL_ADDRESS => $taskReqProps[PR_SENT_REPRESENTING_EMAIL_ADDRESS],
1176
			PR_RECIPIENT_DISPLAY_NAME => $taskReqProps[PR_SENT_REPRESENTING_NAME],
1177
			PR_ADDRTYPE => empty($taskReqProps[PR_SENT_REPRESENTING_ADDRTYPE]) ? 'SMTP' : $taskReqProps[PR_SENT_REPRESENTING_ADDRTYPE],
1178
			PR_RECIPIENT_FLAGS => recipSendable,
1179
			PR_SEARCH_KEY => $taskReqProps[PR_SENT_REPRESENTING_SEARCH_KEY],
1180
		];
1181
1182
		// Assignor has requested task updates, so set him/her as MAPI_CC in recipienttable.
1183
		if ((isset($associatedTaskProps[$this->props['taskupdates']]) && $associatedTaskProps[$this->props['taskupdates']]) &&
1184
			!(isset($associatedTaskProps[$this->props['taskmultrecips']]) && $associatedTaskProps[$this->props['taskmultrecips']] == tmrReceived)) {
1185
			$assignor[PR_RECIPIENT_TYPE] = MAPI_CC;
1186
			$recips[] = $assignor;
1187
		}
1188
1189
		// Assignor wants to receive an email report when task is mark as 'Complete', so in recipients as MAPI_BCC
1190
		if ($associatedTaskProps[$this->props['tasksoc']]) {
1191
			$assignor[PR_RECIPIENT_TYPE] = MAPI_BCC;
1192
			$recips[] = $assignor;
1193
		}
1194
1195
		if (!empty($recips)) {
1196
			mapi_message_modifyrecipients($task, MODRECIP_ADD, $recips);
1197
		}
1198
	}
1199
1200
	/**
1201
	 * Deletes incoming task request from Inbox.
1202
	 *
1203
	 * @returns array|bool PR_ENTRYID, PR_STORE_ENTRYID and PR_PARENT_ENTRYID of the deleted task request
1204
	 *
1205
	 * @return array|false
1206
	 */
1207
	public function deleteReceivedTR() {
1208
		$store = $this->getTaskFolderStore();
1209
		$storeType = mapi_getprops($store, [PR_MDB_PROVIDER]);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false and resource; however, parameter $any of mapi_getprops() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1209
		$storeType = mapi_getprops(/** @scrutinizer ignore-type */ $store, [PR_MDB_PROVIDER]);
Loading history...
1210
		if ($storeType[PR_MDB_PROVIDER] === ZARAFA_STORE_PUBLIC_GUID) {
1211
			$store = $this->getDefaultStore();
1212
		}
1213
		$inbox = mapi_msgstore_getreceivefolder($store);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false and resource; however, parameter $store of mapi_msgstore_getreceivefolder() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1213
		$inbox = mapi_msgstore_getreceivefolder(/** @scrutinizer ignore-type */ $store);
Loading history...
1214
1215
		$storeProps = mapi_getprops($store, [PR_IPM_WASTEBASKET_ENTRYID]);
1216
		$props = mapi_getprops($this->message, [$this->props['task_goid']]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

1216
		$props = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [$this->props['task_goid']]);
Loading history...
1217
		$goid = $props[$this->props['task_goid']];
1218
1219
		// Find the task by looking for the task_goid
1220
		$restriction = [
1221
			RES_PROPERTY,
1222
			[
1223
				RELOP => RELOP_EQ,
1224
				ULPROPTAG => $this->props['task_goid'],
1225
				VALUE => $goid,
1226
			],
1227
		];
1228
1229
		$contents = mapi_folder_getcontentstable($inbox);
1230
1231
		$rows = mapi_table_queryallrows($contents, [PR_ENTRYID, PR_PARENT_ENTRYID, PR_STORE_ENTRYID], $restriction);
1232
1233
		if (!empty($rows)) {
1234
			// If there are multiple, just use the first
1235
			$entryid = $rows[0][PR_ENTRYID];
1236
			$wastebasket = mapi_msgstore_openentry($store, $storeProps[PR_IPM_WASTEBASKET_ENTRYID]);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type false and resource; however, parameter $store of mapi_msgstore_openentry() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1236
			$wastebasket = mapi_msgstore_openentry(/** @scrutinizer ignore-type */ $store, $storeProps[PR_IPM_WASTEBASKET_ENTRYID]);
Loading history...
1237
			mapi_folder_copymessages($inbox, [$entryid], $wastebasket, MESSAGE_MOVE);
1238
1239
			return [PR_ENTRYID => $entryid, PR_PARENT_ENTRYID => $rows[0][PR_PARENT_ENTRYID], PR_STORE_ENTRYID => $rows[0][PR_STORE_ENTRYID]];
1240
		}
1241
1242
		return false;
1243
	}
1244
1245
	/**
1246
	 * Sets recipients for the outgoing message according to type of the response.
1247
	 *
1248
	 * If it is a task update, then only recipient type MAPI_CC are taken from the task message.
1249
	 *
1250
	 * If it is accept/decline response, then PR_SENT_REPRESENTATING_XXXX are taken as recipient.
1251
	 *
1252
	 * @param mixed $outgoing     outgoing mapi message
1253
	 * @param int   $responseType response type (tdmtTaskAcc, tdmtTaskDec, tdmtTaskUpd)
1254
	 */
1255
	public function setRecipientsForResponse($outgoing, $responseType): bool {
1256
		// Clear recipients from outgoing msg
1257
		$this->deleteAllRecipients($outgoing);
1258
1259
		// If it is a task update then get MAPI_CC recipients which are assignors who has asked for task update.
1260
		if ($responseType == tdmtTaskUpd) {
1261
			$props = mapi_getprops($this->message, [$this->props['complete']]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

1261
			$props = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [$this->props['complete']]);
Loading history...
1262
			$isComplete = $props[$this->props['complete']];
1263
1264
			$recipTable = mapi_message_getrecipienttable($this->message);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $msg of mapi_message_getrecipienttable(). ( Ignorable by Annotation )

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

1264
			$recipTable = mapi_message_getrecipienttable(/** @scrutinizer ignore-type */ $this->message);
Loading history...
1265
			$recips = mapi_table_queryallrows($recipTable, $this->recipProps, [
1266
				RES_PROPERTY,
1267
				[
1268
					RELOP => RELOP_EQ,
1269
					ULPROPTAG => PR_RECIPIENT_TYPE,
1270
					VALUE => ($isComplete ? MAPI_BCC : MAPI_CC),
1271
				],
1272
			]);
1273
1274
			// No recipients found, return error
1275
			if (empty($recips)) {
1276
				return false;
1277
			}
1278
1279
			foreach ($recips as $recip) {
1280
				$recip[PR_RECIPIENT_TYPE] = MAPI_TO;	// Change recipient type to MAPI_TO
1281
				mapi_message_modifyrecipients($outgoing, MODRECIP_ADD, [$recip]);
1282
			}
1283
1284
			return true;
1285
		}
1286
1287
		$orgprops = mapi_getprops($this->message, [
1288
			PR_SENT_REPRESENTING_NAME,
1289
			PR_SENT_REPRESENTING_EMAIL_ADDRESS,
1290
			PR_SENT_REPRESENTING_ADDRTYPE,
1291
			PR_SENT_REPRESENTING_ENTRYID,
1292
			PR_SUBJECT,
1293
		]);
1294
1295
		$recip = [
1296
			PR_DISPLAY_NAME => $orgprops[PR_SENT_REPRESENTING_NAME],
1297
			PR_EMAIL_ADDRESS => $orgprops[PR_SENT_REPRESENTING_EMAIL_ADDRESS],
1298
			PR_ADDRTYPE => $orgprops[PR_SENT_REPRESENTING_ADDRTYPE],
1299
			PR_ENTRYID => $orgprops[PR_SENT_REPRESENTING_ENTRYID],
1300
			PR_RECIPIENT_TYPE => MAPI_TO, ];
1301
1302
		mapi_message_modifyrecipients($outgoing, MODRECIP_ADD, [$recip]);
1303
1304
		return true;
1305
	}
1306
1307
	/**
1308
	 * Deletes all recipients from given message object.
1309
	 *
1310
	 * @param mixed $message MAPI message from which recipients are to be removed
1311
	 */
1312
	public function deleteAllRecipients($message): void {
1313
		$recipTable = mapi_message_getrecipienttable($message);
1314
		$recipRows = mapi_table_queryallrows($recipTable, [PR_ROWID]);
1315
1316
		foreach ($recipRows as $recipient) {
1317
			mapi_message_modifyrecipients($message, MODRECIP_REMOVE, [$recipient]);
1318
		}
1319
	}
1320
1321
	/**
1322
	 * Marks the record to complete and send complete update
1323
	 * notification to assigner.
1324
	 *
1325
	 * @return bool TRUE if the update succeeded, FALSE otherwise
1326
	 */
1327
	public function sendCompleteUpdate() {
1328
		$messageprops = mapi_getprops($this->message, [$this->props['taskstate']]);
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_getprops(). ( Ignorable by Annotation )

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

1328
		$messageprops = mapi_getprops(/** @scrutinizer ignore-type */ $this->message, [$this->props['taskstate']]);
Loading history...
1329
1330
		if (!isset($messageprops[$this->props['taskstate']]) || $messageprops[$this->props['taskstate']] != tdsOWN) {
1331
			return false; // Can only decline assignee task
1332
		}
1333
1334
		mapi_setprops($this->message, [
0 ignored issues
show
Bug introduced by
$this->message of type resource is incompatible with the type resource expected by parameter $any of mapi_setprops(). ( Ignorable by Annotation )

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

1334
		mapi_setprops(/** @scrutinizer ignore-type */ $this->message, [
Loading history...
1335
			$this->props['complete'] => true,
1336
			$this->props['datecompleted'] => time(),
1337
			$this->props['status'] => 2,
1338
			$this->props['percent_complete'] => 1,
1339
		]);
1340
1341
		return $this->doUpdate();
1342
	}
1343
1344
	/**
1345
	 * Returns extra info about task request comments along with message body
1346
	 * which will be included in body while sending task request/response.
1347
	 *
1348
	 * @return string info about task request comments along with message body
1349
	 */
1350
	public function getTaskCommentsInfo() {
1351
		return $this->taskCommentsInfo;
1352
	}
1353
1354
	/**
1355
	 * Sets an extra info about task request comments along with message body
1356
	 * which will be included in body while sending task request/response.
1357
	 *
1358
	 * @param string $taskCommentsInfo info about task request comments along with message body
1359
	 */
1360
	public function setTaskCommentsInfo($taskCommentsInfo): void {
1361
		$this->taskCommentsInfo = $taskCommentsInfo;
1362
	}
1363
1364
	public function getSubProperties() {
1365
		$subProperties = [];
1366
		$subProperties["subject"] = PR_SUBJECT;
1367
		$subProperties["convtopic"] = PR_CONVERSATION_TOPIC;
1368
		$subProperties["complete"] = "PT_BOOLEAN:PSETID_Task:" . PidLidTaskComplete;
1369
		$subProperties["datecompleted"] = "PT_SYSTIME:PSETID_Task:" . PidLidTaskDateCompleted;
1370
		$subProperties["recurring"] = "PT_BOOLEAN:PSETID_Task:0x8126";
1371
		$subProperties["startdate"] = "PT_SYSTIME:PSETID_Task:" . PidLidTaskStartDate;
1372
		$subProperties["duedate"] = "PT_SYSTIME:PSETID_Task:" . PidLidTaskDueDate;
1373
		$subProperties["status"] = "PT_LONG:PSETID_Task:" . PidLidTaskStatus;
1374
		$subProperties["percent_complete"] = "PT_DOUBLE:PSETID_Task:" . PidLidPercentComplete;
1375
		$subProperties["totalwork"] = "PT_LONG:PSETID_Task:0x8111";
1376
		$subProperties["actualwork"] = "PT_LONG:PSETID_Task:0x8110";
1377
		$subProperties["categories"] = "PT_MV_STRING8:PS_PUBLIC_STRINGS:Keywords";
1378
		$subProperties["companies"] = "PT_MV_STRING8:PSETID_Common:0x8539";
1379
		$subProperties["mileage"] = "PT_STRING8:PSETID_Common:0x8534";
1380
		$subProperties["billinginformation"] = "PT_STRING8:PSETID_Common:0x8535";
1381
1382
		return getPropIdsFromStrings($this->store, $subProperties);
1383
	}
1384
}
1385