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 | define('NOERROR', 0); |
||||
9 | |||||
10 | // Load all mapi defs |
||||
11 | if (function_exists('mapi_load_mapidefs')) { |
||||
12 | mapi_load_mapidefs(1); |
||||
13 | } |
||||
14 | else { |
||||
15 | // code analysis with non-mapi php builds |
||||
16 | require 'dev/autoloader.php'; |
||||
17 | } |
||||
18 | |||||
19 | /** |
||||
20 | * Function to make a MAPIGUID from a php string. |
||||
21 | * The C++ definition for the GUID is: |
||||
22 | * typedef struct _GUID |
||||
23 | * { |
||||
24 | * unsigned long Data1; |
||||
25 | * unsigned short Data2; |
||||
26 | * unsigned short Data3; |
||||
27 | * unsigned char Data4[8]; |
||||
28 | * } GUID;. |
||||
29 | * |
||||
30 | * A GUID is normally represented in the following form: |
||||
31 | * {00062008-0000-0000-C000-000000000046} |
||||
32 | * |
||||
33 | * @param string $guid |
||||
34 | */ |
||||
35 | function makeGuid($guid): string { |
||||
36 | return pack("vvvv", hexdec(substr($guid, 5, 4)), hexdec(substr($guid, 1, 4)), hexdec(substr($guid, 10, 4)), hexdec(substr($guid, 15, 4))) . hex2bin(substr($guid, 20, 4)) . hex2bin(substr($guid, 25, 12)); |
||||
37 | } |
||||
38 | |||||
39 | /** |
||||
40 | * Function to get a human readable string from a MAPI error code. |
||||
41 | * |
||||
42 | * @param mixed $errcode the MAPI error code, if not given, we use mapi_last_hresult |
||||
43 | * |
||||
44 | * @return string The defined name for the MAPI error code |
||||
45 | */ |
||||
46 | function get_mapi_error_name($errcode = null) { |
||||
47 | if ($errcode === null) { |
||||
48 | $errcode = mapi_last_hresult(); |
||||
49 | } |
||||
50 | |||||
51 | if (strcasecmp(substr($errcode, 0, 2), '0x') === 0) { |
||||
52 | $errcode = hexdec($errcode); |
||||
53 | } |
||||
54 | |||||
55 | if ($errcode !== 0) { |
||||
56 | // Retrieve constants categories, MAPI error names are defined in gromox. |
||||
57 | foreach (get_defined_constants(true)['Core'] as $key => $value) { |
||||
58 | /* |
||||
59 | * If PHP encounters a number beyond the bounds of the integer type, |
||||
60 | * it will be interpreted as a float instead, so when comparing these error codes |
||||
61 | * we have to manually typecast value to integer, so float will be converted in integer, |
||||
62 | * but still its out of bound for integer limit so it will be auto adjusted to minus value |
||||
63 | */ |
||||
64 | if ($errcode == (int) $value) { |
||||
65 | // Check that we have an actual MAPI error or warning definition |
||||
66 | $prefix = substr($key, 0, 7); |
||||
67 | if ($prefix == "MAPI_E_" || $prefix == "MAPI_W_") { |
||||
68 | return $key; |
||||
69 | } |
||||
70 | $prefix = substr($key, 0, 2); |
||||
71 | if ($prefix == "ec") { |
||||
72 | return $key; |
||||
73 | } |
||||
74 | } |
||||
75 | } |
||||
76 | } |
||||
77 | else { |
||||
78 | return "NOERROR"; |
||||
79 | } |
||||
80 | |||||
81 | // error code not found, return hex value (this is a fix for 64-bit systems, we can't use the dechex() function for this) |
||||
82 | $result = unpack("H*", pack("N", $errcode)); |
||||
83 | |||||
84 | return "0x" . $result[1]; |
||||
85 | } |
||||
86 | |||||
87 | /** |
||||
88 | * Parses properties from an array of strings. Each "string" may be either an ULONG, which is a direct property ID, |
||||
89 | * or a string with format "PT_TYPE:{GUID}:StringId" or "PT_TYPE:{GUID}:0xXXXX" for named |
||||
90 | * properties. |
||||
91 | * |
||||
92 | * @param mixed $store |
||||
93 | * @param mixed $mapping |
||||
94 | * |
||||
95 | * @return array |
||||
96 | */ |
||||
97 | function getPropIdsFromStrings($store, $mapping) { |
||||
98 | $props = []; |
||||
99 | |||||
100 | $ids = ["name" => [], "id" => [], "guid" => [], "type" => []]; // this array stores all the information needed to retrieve a named property |
||||
101 | $num = 0; |
||||
102 | |||||
103 | // caching |
||||
104 | $guids = []; |
||||
105 | |||||
106 | foreach ($mapping as $name => $val) { |
||||
107 | if (is_string($val)) { |
||||
108 | $split = explode(":", $val); |
||||
109 | |||||
110 | if (count($split) != 3) { // invalid string, ignore |
||||
111 | trigger_error(sprintf("Invalid property: %s \"%s\"", $name, $val), E_USER_NOTICE); |
||||
112 | |||||
113 | continue; |
||||
114 | } |
||||
115 | |||||
116 | if (substr($split[2], 0, 2) == "0x") { |
||||
117 | $id = hexdec(substr($split[2], 2)); |
||||
118 | } |
||||
119 | elseif (preg_match('/^[1-9][0-9]{0,12}$/', $split[2])) { |
||||
120 | $id = (int) $split[2]; |
||||
121 | } |
||||
122 | else { |
||||
123 | $id = $split[2]; |
||||
124 | } |
||||
125 | |||||
126 | // have we used this guid before? |
||||
127 | if (!defined($split[1])) { |
||||
128 | if (!array_key_exists($split[1], $guids)) { |
||||
129 | $guids[$split[1]] = makeguid($split[1]); |
||||
130 | } |
||||
131 | $guid = $guids[$split[1]]; |
||||
132 | } |
||||
133 | else { |
||||
134 | $guid = constant($split[1]); |
||||
135 | } |
||||
136 | |||||
137 | // temp store info about named prop, so we have to call mapi_getidsfromnames just one time |
||||
138 | $ids["name"][$num] = $name; |
||||
139 | $ids["id"][$num] = $id; |
||||
140 | $ids["guid"][$num] = $guid; |
||||
141 | $ids["type"][$num] = $split[0]; |
||||
142 | ++$num; |
||||
143 | } |
||||
144 | else { |
||||
145 | // not a named property |
||||
146 | $props[$name] = $val; |
||||
147 | } |
||||
148 | } |
||||
149 | |||||
150 | if (empty($ids["id"])) { |
||||
151 | return $props; |
||||
152 | } |
||||
153 | |||||
154 | // get the ids |
||||
155 | $named = mapi_getidsfromnames($store, $ids["id"], $ids["guid"]); |
||||
156 | foreach ($named as $num => $prop) { |
||||
157 | $props[$ids["name"][$num]] = mapi_prop_tag(constant($ids["type"][$num]), mapi_prop_id($prop)); |
||||
158 | } |
||||
159 | |||||
160 | return $props; |
||||
161 | } |
||||
162 | |||||
163 | /** |
||||
164 | * Check whether a call to mapi_getprops returned errors for some properties. |
||||
165 | * mapi_getprops function tries to get values of properties requested but somehow if |
||||
166 | * if a property value can not be fetched then it changes type of property tag as PT_ERROR |
||||
167 | * and returns error for that particular property, probable errors |
||||
168 | * that can be returned as value can be MAPI_E_NOT_FOUND, MAPI_E_NOT_ENOUGH_MEMORY. |
||||
169 | * |
||||
170 | * @param int $property Property to check for error |
||||
171 | * @param array $propArray An array of properties |
||||
172 | * |
||||
173 | * @return bool|mixed Gives back false when there is no error, if there is, gives the error |
||||
174 | */ |
||||
175 | function propIsError($property, $propArray) { |
||||
176 | if (array_key_exists(mapi_prop_tag(PT_ERROR, mapi_prop_id($property)), $propArray)) { |
||||
177 | return $propArray[mapi_prop_tag(PT_ERROR, mapi_prop_id($property))]; |
||||
178 | } |
||||
179 | |||||
180 | return false; |
||||
181 | } |
||||
182 | |||||
183 | /** |
||||
184 | * Note: Static function, more like a utility function. |
||||
185 | * |
||||
186 | * Gets all the items (including recurring items) in the specified calendar in the given timeframe. Items are |
||||
187 | * included as a whole if they overlap the interval <$start, $end> (non-inclusive). This means that if the interval |
||||
188 | * is <08:00 - 14:00>, the item [6:00 - 8:00> is NOT included, nor is the item [14:00 - 16:00>. However, the item |
||||
189 | * [7:00 - 9:00> is included as a whole, and is NOT capped to [8:00 - 9:00>. |
||||
190 | * |
||||
191 | * @param resource $store The store in which the calendar resides |
||||
192 | * @param resource $calendar The calendar to get the items from |
||||
193 | * @param int $viewstart Timestamp of beginning of view window |
||||
194 | * @param int $viewend Timestamp of end of view window |
||||
195 | * @param array $propsrequested Array of properties to return |
||||
196 | * |
||||
197 | * @psalm-return list<mixed> |
||||
198 | */ |
||||
199 | function getCalendarItems($store, $calendar, $viewstart, $viewend, $propsrequested): array { |
||||
200 | $result = []; |
||||
201 | $properties = getPropIdsFromStrings($store, [ |
||||
202 | "duedate" => "PT_SYSTIME:PSETID_Appointment:" . PidLidAppointmentEndWhole, |
||||
203 | "startdate" => "PT_SYSTIME:PSETID_Appointment:" . PidLidAppointmentStartWhole, |
||||
204 | "enddate_recurring" => "PT_SYSTIME:PSETID_Appointment:" . PidLidClipEnd, |
||||
205 | "recurring" => "PT_BOOLEAN:PSETID_Appointment:" . PidLidRecurring, |
||||
206 | "recurring_data" => "PT_BINARY:PSETID_Appointment:" . PidLidAppointmentRecur, |
||||
207 | "timezone_data" => "PT_BINARY:PSETID_Appointment:" . PidLidTimeZoneStruct, |
||||
208 | "label" => "PT_LONG:PSETID_Appointment:0x8214", |
||||
209 | ]); |
||||
210 | |||||
211 | // Create a restriction that will discard rows of appointments that are definitely not in our |
||||
212 | // requested time frame |
||||
213 | |||||
214 | $table = mapi_folder_getcontentstable($calendar); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
215 | |||||
216 | $restriction = |
||||
217 | // OR |
||||
218 | [ |
||||
219 | RES_OR, |
||||
220 | [ |
||||
221 | [RES_AND, // Normal items: itemEnd must be after viewStart, itemStart must be before viewEnd |
||||
222 | [ |
||||
223 | [ |
||||
224 | RES_PROPERTY, |
||||
225 | [ |
||||
226 | RELOP => RELOP_GT, |
||||
227 | ULPROPTAG => $properties["duedate"], |
||||
228 | VALUE => $viewstart, |
||||
229 | ], |
||||
230 | ], |
||||
231 | [ |
||||
232 | RES_PROPERTY, |
||||
233 | [ |
||||
234 | RELOP => RELOP_LT, |
||||
235 | ULPROPTAG => $properties["startdate"], |
||||
236 | VALUE => $viewend, |
||||
237 | ], |
||||
238 | ], |
||||
239 | ], |
||||
240 | ], |
||||
241 | // OR |
||||
242 | [ |
||||
243 | RES_PROPERTY, |
||||
244 | [ |
||||
245 | RELOP => RELOP_EQ, |
||||
246 | ULPROPTAG => $properties["recurring"], |
||||
247 | VALUE => true, |
||||
248 | ], |
||||
249 | ], |
||||
250 | ], // EXISTS OR |
||||
251 | ]; // global OR |
||||
252 | |||||
253 | // Get requested properties, plus whatever we need |
||||
254 | $proplist = [PR_ENTRYID, $properties["recurring"], $properties["recurring_data"], $properties["timezone_data"]]; |
||||
255 | $proplist = array_merge($proplist, $propsrequested); |
||||
256 | |||||
257 | $rows = mapi_table_queryallrows($table, $proplist, $restriction); |
||||
0 ignored issues
–
show
It seems like
$table can also be of type false ; however, parameter $table of mapi_table_queryallrows() 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
![]() |
|||||
258 | |||||
259 | // $rows now contains all the items that MAY be in the window; a recurring item needs expansion before including in the output. |
||||
260 | |||||
261 | foreach ($rows as $row) { |
||||
262 | $items = []; |
||||
263 | |||||
264 | if (isset($row[$properties["recurring"]]) && $row[$properties["recurring"]]) { |
||||
265 | // Recurring item |
||||
266 | $rec = new Recurrence($store, $row); |
||||
267 | |||||
268 | // GetItems guarantees that the item overlaps the interval <$viewstart, $viewend> |
||||
269 | $occurrences = $rec->getItems($viewstart, $viewend); |
||||
270 | foreach ($occurrences as $occurrence) { |
||||
271 | // The occurrence takes all properties from the main row, but overrides some properties (like start and end obviously) |
||||
272 | $item = $occurrence + $row; |
||||
273 | array_push($items, $item); |
||||
274 | } |
||||
275 | } |
||||
276 | else { |
||||
277 | // Normal item, it matched the search criteria and therefore overlaps the interval <$viewstart, $viewend> |
||||
278 | array_push($items, $row); |
||||
279 | } |
||||
280 | |||||
281 | $result = array_merge($result, $items); |
||||
282 | } |
||||
283 | |||||
284 | // All items are guaranteed to overlap the interval <$viewstart, $viewend>. Note that we may be returning a few extra |
||||
285 | // properties that the caller did not request (recurring, etc). This shouldn't be a problem though. |
||||
286 | return $result; |
||||
287 | } |
||||
288 | |||||
289 | /** |
||||
290 | * Compares two entryIds. It is possible to have two different entryIds that should match as they |
||||
291 | * represent the same object (in multiserver environments). |
||||
292 | * |
||||
293 | * @param mixed $entryId1 EntryID |
||||
294 | * @param mixed $entryId2 EntryID |
||||
295 | * |
||||
296 | * @return bool Result of the comparison |
||||
297 | */ |
||||
298 | function compareEntryIds($entryId1, $entryId2) { |
||||
299 | if (!is_string($entryId1) || !is_string($entryId2)) { |
||||
300 | return false; |
||||
301 | } |
||||
302 | |||||
303 | if ($entryId1 === $entryId2) { |
||||
304 | // if normal comparison succeeds then we can directly say that entryids are same |
||||
305 | return true; |
||||
306 | } |
||||
307 | |||||
308 | return false; |
||||
309 | } |
||||
310 | |||||
311 | /** |
||||
312 | * Creates a goid from an ical uuid. |
||||
313 | * |
||||
314 | * @param string $uid |
||||
315 | * |
||||
316 | * @return string binary string representation of goid |
||||
317 | */ |
||||
318 | function getGoidFromUid($uid) { |
||||
319 | return hex2bin("040000008200E00074C5B7101A82E0080000000000000000000000000000000000000000" . |
||||
320 | bin2hex(pack("V", 12 + strlen($uid)) . "vCal-Uid" . pack("V", 1) . $uid)); |
||||
321 | } |
||||
322 | |||||
323 | /** |
||||
324 | * Returns zero terminated goid. It is required for backwards compatibility. |
||||
325 | * |
||||
326 | * @param mixed $uid |
||||
327 | * |
||||
328 | * @return string an OL compatible GlobalObjectID |
||||
329 | */ |
||||
330 | function getGoidFromUidZero($uid) { |
||||
331 | if (strlen($uid) <= 64) { |
||||
332 | return hex2bin("040000008200E00074C5B7101A82E0080000000000000000000000000000000000000000" . |
||||
333 | bin2hex(pack("V", 13 + strlen($uid)) . "vCal-Uid" . pack("V", 1) . $uid) . "00"); |
||||
334 | } |
||||
335 | |||||
336 | return hex2bin($uid); |
||||
337 | } |
||||
338 | |||||
339 | /** |
||||
340 | * Creates an ical uuid from a goid. |
||||
341 | * |
||||
342 | * @param string $goid |
||||
343 | * |
||||
344 | * @return null|string ical uuid |
||||
345 | */ |
||||
346 | function getUidFromGoid($goid) { |
||||
347 | // check if "vCal-Uid" is somewhere in outlookid case-insensitive |
||||
348 | $uid = stristr($goid, "vCal-Uid"); |
||||
349 | if ($uid !== false) { |
||||
350 | // get the length of the ical id - go back 4 position from where "vCal-Uid" was found |
||||
351 | $begin = unpack("V", substr($goid, strlen($uid) * (-1) - 4, 4)); |
||||
352 | |||||
353 | // remove "vCal-Uid" and packed "1" and use the ical id length |
||||
354 | return trim(substr($uid, 12, $begin[1] - 12)); |
||||
355 | } |
||||
356 | |||||
357 | return null; |
||||
358 | } |
||||
359 |