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