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