FreeBusy   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 41
c 5
b 1
f 1
dl 0
loc 97
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocalFreeBusyFolder() 0 11 2
B getLocalFreeBusyMessage() 0 60 7
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
 * This class is just static class and will not be instantiate and
11
 * It contains the functionality to get freebusy message and folder.
12
 */
13
class FreeBusy {
14
	/**
15
	 *  PR_FREEBUSY_ENTRYIDS contains 4 entryids
16
	 *	PR_FREEBUSY_ENTRYIDS[0] gives associated freebusy folder in calendar
17
	 *	PR_FREEBUSY_ENTRYIDS[1] Localfreebusy (used for delegate properties)
18
	 *	PR_FREEBUSY_ENTRYIDS[2] global Freebusydata in public store
19
	 *	PR_FREEBUSY_ENTRYIDS[3] Freebusydata in IPM_SUBTREE.
20
	 */
21
	public const ASSOCIATED_FREEBUSY_FOLDER = 0;
22
	public const DELEGATE_PROPERTIES = 1;
23
	public const GLOBAL_FREEBUSYDATA = 2;
24
	public const FREEBUSYDATA_IPM_SUBTREE = 3;
25
26
	/**
27
	 * Function will return resource of the local freebusy message of the user's store.
28
	 *
29
	 * @return false|resource local freebusy message, otherwise false if message not found
30
	 */
31
	public static function getLocalFreeBusyMessage(mixed $store = false): mixed {
32
		if (!$store) {
33
			error_log("getLocalFreeBusyMessage: store not available");
34
35
			return false;
36
		}
37
38
		// Check for mapi_freebusy_openmsg function,
39
		// If yes then use mapi function to get freebusy message.
40
		if (function_exists('mapi_freebusy_openmsg')) {
41
			return mapi_freebusy_openmsg($store);
42
		}
43
44
		// Get 'LocalFreeBusy' message from FreeBusy Store
45
		$root = mapi_msgstore_openentry($store);
46
		$storeProps = mapi_getprops($root, [PR_FREEBUSY_ENTRYIDS]);
47
		$localFreeBusyEntryids = $storeProps[PR_FREEBUSY_ENTRYIDS];
48
49
		try {
50
			return mapi_msgstore_openentry($store, $localFreeBusyEntryids[self::DELEGATE_PROPERTIES]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return mapi_msgstore_ope...::DELEGATE_PROPERTIES]) returns the type resource which is incompatible with the documented return type false|resource.
Loading history...
51
		}
52
		catch (MAPIException $e) {
53
			// Either user store have malformed entryid in PR_FREEBUSY_ENTRYIDS or
54
			// No message found of given entryid in 'Freebusy Data' folder.
55
			if ($e->getCode() === MAPI_E_NOT_FOUND || $e->getCode() === MAPI_E_INVALID_ENTRYID) {
56
				$freeBusyFolder = mapi_msgstore_openentry($store, $localFreeBusyEntryids[self::FREEBUSYDATA_IPM_SUBTREE]);
57
				$table = mapi_folder_getcontentstable($freeBusyFolder);
58
				mapi_table_restrict(
59
					$table,
60
					[
61
						RES_CONTENT,
62
						[
63
							FUZZYLEVEL => FL_PREFIX,
64
							ULPROPTAG => PR_MESSAGE_CLASS,
65
							VALUE => [PR_MESSAGE_CLASS => "IPM.Microsoft.ScheduleData.FreeBusy"],
66
						],
67
					]
68
				);
69
70
				$items = mapi_table_queryallrows($table, [PR_ENTRYID]);
71
				if (empty($items)) {
72
					// FIXME recreate local freebusy message in 'Freebusy Data' folder.
73
					error_log("Unable to find local free busy message in 'Freebusy Data' folder");
74
75
					return false;
76
				}
77
78
				$localFreeBusyEntryids[1] = $items[0][PR_ENTRYID];
79
80
				// Updating the entryid in the PR_FREEBUSY_ENTRYIDS property of user store.
81
				mapi_setprops($root, [PR_FREEBUSY_ENTRYIDS => $localFreeBusyEntryids]);
82
				mapi_savechanges($root);
83
84
				return mapi_msgstore_openentry($store, $localFreeBusyEntryids[self::DELEGATE_PROPERTIES]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return mapi_msgstore_ope...::DELEGATE_PROPERTIES]) returns the type resource which is incompatible with the documented return type false|resource.
Loading history...
85
			}
86
87
			// Ensure to return false if an exception occurs
88
			error_log("getLocalFreeBusyMessage: unhandled MAPIException " . $e->getMessage());
89
90
			return false;
91
		}
92
	}
93
94
	/**
95
	 * Function will return resource of the freebusy folder of the user's store.
96
	 *
97
	 * @return false|resource freebusy folder
98
	 */
99
	public static function getLocalFreeBusyFolder(mixed $store = false): mixed {
100
		if (!$store) {
101
			error_log("getLocalFreeBusyFolder: store not available");
102
103
			return false;
104
		}
105
		// Get 'LocalFreeBusy' message from FreeBusy Store
106
		$root = mapi_msgstore_openentry($store);
107
		$storeProps = mapi_getprops($root, [PR_FREEBUSY_ENTRYIDS]);
108
109
		return mapi_msgstore_openentry($store, $storeProps[PR_FREEBUSY_ENTRYIDS][self::FREEBUSYDATA_IPM_SUBTREE]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return mapi_msgstore_ope...EBUSYDATA_IPM_SUBTREE]) returns the type resource which is incompatible with the documented return type false|resource.
Loading history...
110
	}
111
}
112