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-2024 grommunio GmbH |
||
| 6 | */ |
||
| 7 | |||
| 8 | /** |
||
| 9 | * BaseRecurrence |
||
| 10 | * this class is superclass for recurrence for appointments and tasks. This class provides all |
||
| 11 | * basic features of recurrence. |
||
| 12 | */ |
||
| 13 | abstract class BaseRecurrence { |
||
| 14 | /** |
||
| 15 | * @var resource Mapi Message Store (may be null if readonly) |
||
| 16 | */ |
||
| 17 | public $store; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var mixed Mapi Message (may be null if readonly) |
||
| 21 | */ |
||
| 22 | public $message; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array Message Properties |
||
| 26 | */ |
||
| 27 | public $messageprops; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array list of property tags |
||
| 31 | */ |
||
| 32 | public $proptags; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var mixed recurrence data of this calendar item |
||
| 36 | */ |
||
| 37 | public $recur; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var mixed Timezone data of this calendar item |
||
| 41 | */ |
||
| 42 | public $tz; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor. |
||
| 46 | * |
||
| 47 | * @param resource $store MAPI Message Store Object |
||
| 48 | * @param mixed $message the MAPI (appointment) message |
||
| 49 | */ |
||
| 50 | public function __construct($store, $message) { |
||
| 51 | $this->store = $store; |
||
| 52 | |||
| 53 | if (is_array($message)) { |
||
| 54 | $this->messageprops = $message; |
||
| 55 | } |
||
| 56 | else { |
||
| 57 | $this->message = $message; |
||
| 58 | $this->messageprops = mapi_getprops($this->message, $this->proptags); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (isset($this->messageprops[$this->proptags["recurring_data"]])) { |
||
| 62 | // There is a possibility that recurr blob can be more than 255 bytes so get full blob through stream interface |
||
| 63 | if (strlen($this->messageprops[$this->proptags["recurring_data"]]) >= 255) { |
||
| 64 | $this->getFullRecurrenceBlob(); |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->recur = $this->parseRecurrence($this->messageprops[$this->proptags["recurring_data"]]); |
||
| 68 | } |
||
| 69 | if (isset($this->proptags["timezone_data"], $this->messageprops[$this->proptags["timezone_data"]])) { |
||
| 70 | $this->tz = $this->parseTimezone($this->messageprops[$this->proptags["timezone_data"]]); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getRecurrence() { |
||
| 75 | return $this->recur; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getFullRecurrenceBlob(): void { |
||
| 79 | $message = mapi_msgstore_openentry($this->store, $this->messageprops[PR_ENTRYID]); |
||
| 80 | |||
| 81 | $recurrBlob = ''; |
||
| 82 | $stream = mapi_openproperty($message, $this->proptags["recurring_data"], IID_IStream, 0, 0); |
||
| 83 | $stat = mapi_stream_stat($stream); |
||
| 84 | |||
| 85 | for ($i = 0; $i < $stat['cb']; $i += 1024) { |
||
| 86 | $recurrBlob .= mapi_stream_read($stream, 1024); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (!empty($recurrBlob)) { |
||
| 90 | $this->messageprops[$this->proptags["recurring_data"]] = $recurrBlob; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Function for parsing the Recurrence value of a Calendar item. |
||
| 96 | * |
||
| 97 | * Retrieve it from Named Property 0x8216 as a PT_BINARY and pass the |
||
| 98 | * data to this function |
||
| 99 | * |
||
| 100 | * Returns a structure containing the data: |
||
| 101 | * |
||
| 102 | * type - type of recurrence: day=10, week=11, month=12, year=13 |
||
| 103 | * subtype - type of day recurrence: 2=monthday (ie 21st day of month), 3=nday'th weekdays (ie. 2nd Tuesday and Wednesday) |
||
| 104 | * start - unix timestamp of first occurrence |
||
| 105 | * end - unix timestamp of last occurrence (up to and including), so when start == end -> occurrences = 1 |
||
| 106 | * numoccur - occurrences (may be very large when there is no end data) |
||
| 107 | * |
||
| 108 | * then, for each type: |
||
| 109 | * |
||
| 110 | * Daily: |
||
| 111 | * everyn - every [everyn] days in minutes |
||
| 112 | * regen - regenerating event (like tasks) |
||
| 113 | * |
||
| 114 | * Weekly: |
||
| 115 | * everyn - every [everyn] weeks in weeks |
||
| 116 | * regen - regenerating event (like tasks) |
||
| 117 | * weekdays - bitmask of week days, where each bit is one weekday (weekdays & 1 = Sunday, weekdays & 2 = Monday, etc) |
||
| 118 | * |
||
| 119 | * Monthly: |
||
| 120 | * everyn - every [everyn] months |
||
| 121 | * regen - regenerating event (like tasks) |
||
| 122 | * |
||
| 123 | * subtype 2: |
||
| 124 | * monthday - on day [monthday] of the month |
||
| 125 | * |
||
| 126 | * subtype 3: |
||
| 127 | * weekdays - bitmask of week days, where each bit is one weekday (weekdays & 1 = Sunday, weekdays & 2 = Monday, etc) |
||
| 128 | * nday - on [nday]'th [weekdays] of the month |
||
| 129 | * |
||
| 130 | * Yearly: |
||
| 131 | * everyn - every [everyn] months (12, 24, 36, ...) |
||
| 132 | * month - in month [month] (although the month is encoded in minutes since the startning of the year ........) |
||
| 133 | * regen - regenerating event (like tasks) |
||
| 134 | * |
||
| 135 | * subtype 2: |
||
| 136 | * monthday - on day [monthday] of the month |
||
| 137 | * |
||
| 138 | * subtype 3: |
||
| 139 | * weekdays - bitmask of week days, where each bit is one weekday (weekdays & 1 = Sunday, weekdays & 2 = Monday, etc) |
||
| 140 | * nday - on [nday]'th [weekdays] of the month [month] |
||
| 141 | * |
||
| 142 | * @param string $rdata Binary string |
||
| 143 | * |
||
| 144 | * @return null|(((false|int|mixed|string)[]|int)[]|int|mixed)[] recurrence data |
||
| 145 | * |
||
| 146 | * @psalm-return array{changed_occurrences: array<int, array{basedate: false|int, start: int, end: int, bitmask: mixed, subject?: false|string, remind_before?: mixed, reminder_set?: mixed, location?: false|string, busystatus?: mixed, alldayevent?: mixed, label?: mixed, ex_start_datetime?: mixed, ex_end_datetime?: mixed, ex_orig_date?: mixed}>, deleted_occurrences: list<int>, type?: int|mixed, subtype?: mixed, month?: mixed, everyn?: mixed, regen?: mixed, monthday?: mixed, weekdays?: 0|mixed, nday?: mixed, term?: int|mixed, numoccur?: mixed, numexcept?: mixed, numexceptmod?: mixed, start?: int, end?: int, startocc?: mixed, endocc?: mixed}|null |
||
| 147 | */ |
||
| 148 | public function parseRecurrence($rdata) { |
||
| 149 | if (strlen($rdata) < 10) { |
||
| 150 | return; |
||
| 151 | } |
||
| 152 | |||
| 153 | $ret = []; |
||
| 154 | $ret["changed_occurrences"] = []; |
||
| 155 | $ret["deleted_occurrences"] = []; |
||
| 156 | |||
| 157 | $data = unpack("vReaderVersion/vWriterVersion/vrtype/vrtype2/vCalendarType", $rdata); |
||
| 158 | |||
| 159 | // Do some recurrence validity checks |
||
| 160 | if ($data['ReaderVersion'] != 0x3004 || $data['WriterVersion'] != 0x3004) { |
||
| 161 | return $ret; |
||
| 162 | } |
||
| 163 | |||
| 164 | if (!in_array($data["rtype"], [IDC_RCEV_PAT_ORB_DAILY, IDC_RCEV_PAT_ORB_WEEKLY, IDC_RCEV_PAT_ORB_MONTHLY, IDC_RCEV_PAT_ORB_YEARLY])) { |
||
| 165 | return $ret; |
||
| 166 | } |
||
| 167 | |||
| 168 | if (!in_array($data["rtype2"], [rptDay, rptWeek, rptMonth, rptMonthNth, rptMonthEnd, rptHjMonth, rptHjMonthNth, rptHjMonthEnd])) { |
||
| 169 | return $ret; |
||
| 170 | } |
||
| 171 | |||
| 172 | if (!in_array($data['CalendarType'], [MAPI_CAL_DEFAULT, MAPI_CAL_GREGORIAN])) { |
||
| 173 | return $ret; |
||
| 174 | } |
||
| 175 | |||
| 176 | $ret["type"] = (int) $data["rtype"] > 0x2000 ? (int) $data["rtype"] - 0x2000 : $data["rtype"]; |
||
| 177 | $ret["subtype"] = $data["rtype2"]; |
||
| 178 | $rdata = substr($rdata, 10); |
||
| 179 | |||
| 180 | switch ($data["rtype"]) { |
||
| 181 | case IDC_RCEV_PAT_ORB_DAILY: |
||
| 182 | if (strlen($rdata) < 12) { |
||
| 183 | return $ret; |
||
| 184 | } |
||
| 185 | |||
| 186 | $data = unpack("Vunknown/Veveryn/Vregen", $rdata); |
||
| 187 | if ($data["everyn"] > 1438560) { // minutes for 999 days |
||
| 188 | return $ret; |
||
| 189 | } |
||
| 190 | $ret["everyn"] = $data["everyn"]; |
||
| 191 | $ret["regen"] = $data["regen"]; |
||
| 192 | |||
| 193 | switch ($ret["subtype"]) { |
||
| 194 | case rptDay: |
||
| 195 | $rdata = substr($rdata, 12); |
||
| 196 | break; |
||
| 197 | |||
| 198 | case rptWeek: |
||
| 199 | $rdata = substr($rdata, 16); |
||
| 200 | break; |
||
| 201 | } |
||
| 202 | |||
| 203 | break; |
||
| 204 | |||
| 205 | case IDC_RCEV_PAT_ORB_WEEKLY: |
||
| 206 | if (strlen($rdata) < 16) { |
||
| 207 | return $ret; |
||
| 208 | } |
||
| 209 | |||
| 210 | $data = unpack("Vconst1/Veveryn/Vregen", $rdata); |
||
| 211 | if ($data["everyn"] > 99) { |
||
| 212 | return $ret; |
||
| 213 | } |
||
| 214 | |||
| 215 | $rdata = substr($rdata, 12); |
||
| 216 | |||
| 217 | $ret["everyn"] = $data["everyn"]; |
||
| 218 | $ret["regen"] = $data["regen"]; |
||
| 219 | $ret["weekdays"] = 0; |
||
| 220 | |||
| 221 | if ($data["regen"] == 0) { |
||
| 222 | $data = unpack("Vweekdays", $rdata); |
||
| 223 | $rdata = substr($rdata, 4); |
||
| 224 | |||
| 225 | $ret["weekdays"] = $data["weekdays"]; |
||
| 226 | } |
||
| 227 | break; |
||
| 228 | |||
| 229 | case IDC_RCEV_PAT_ORB_MONTHLY: |
||
| 230 | if (strlen($rdata) < 16) { |
||
| 231 | return $ret; |
||
| 232 | } |
||
| 233 | |||
| 234 | $data = unpack("Vconst1/Veveryn/Vregen/Vmonthday", $rdata); |
||
| 235 | if ($data["everyn"] > 99) { |
||
| 236 | return $ret; |
||
| 237 | } |
||
| 238 | |||
| 239 | $ret["everyn"] = $data["everyn"]; |
||
| 240 | $ret["regen"] = $data["regen"]; |
||
| 241 | |||
| 242 | if ($ret["subtype"] == rptMonthNth) { |
||
| 243 | $ret["weekdays"] = $data["monthday"]; |
||
| 244 | } |
||
| 245 | else { |
||
| 246 | $ret["monthday"] = $data["monthday"]; |
||
| 247 | } |
||
| 248 | |||
| 249 | $rdata = substr($rdata, 16); |
||
| 250 | if ($ret["subtype"] == rptMonthNth) { |
||
| 251 | $data = unpack("Vnday", $rdata); |
||
| 252 | $ret["nday"] = $data["nday"]; |
||
| 253 | $rdata = substr($rdata, 4); |
||
| 254 | } |
||
| 255 | break; |
||
| 256 | |||
| 257 | case IDC_RCEV_PAT_ORB_YEARLY: |
||
| 258 | if (strlen($rdata) < 16) { |
||
| 259 | return $ret; |
||
| 260 | } |
||
| 261 | |||
| 262 | $data = unpack("Vmonth/Veveryn/Vregen/Vmonthday", $rdata); |
||
| 263 | // recurring yearly tasks have a period in months multiple by 12 |
||
| 264 | if ($data['regen'] && $data["everyn"] % 12 != 0) { |
||
| 265 | return $ret; |
||
| 266 | } |
||
| 267 | if (!$data['regen'] && $data["everyn"] != 12) { |
||
| 268 | return $ret; |
||
| 269 | } |
||
| 270 | |||
| 271 | $ret["month"] = $data["month"]; |
||
| 272 | $ret["everyn"] = $data["everyn"]; |
||
| 273 | $ret["regen"] = $data["regen"]; |
||
| 274 | |||
| 275 | if ($ret["subtype"] == rptMonthNth) { |
||
| 276 | $ret["weekdays"] = $data["monthday"]; |
||
| 277 | } |
||
| 278 | else { |
||
| 279 | $ret["monthday"] = $data["monthday"]; |
||
| 280 | } |
||
| 281 | |||
| 282 | $rdata = substr($rdata, 16); |
||
| 283 | |||
| 284 | if ($ret["subtype"] == rptMonthNth) { |
||
| 285 | $data = unpack("Vnday", $rdata); |
||
| 286 | $ret["nday"] = $data["nday"]; |
||
| 287 | $rdata = substr($rdata, 4); |
||
| 288 | } |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | |||
| 292 | if (strlen($rdata) < 16) { |
||
| 293 | return $ret; |
||
| 294 | } |
||
| 295 | |||
| 296 | $data = unpack("Vterm/Vnumoccur/Vconst2/Vnumexcept", $rdata); |
||
| 297 | $rdata = substr($rdata, 16); |
||
| 298 | if (!in_array($data["term"], [IDC_RCEV_PAT_ERB_END, IDC_RCEV_PAT_ERB_AFTERNOCCUR, IDC_RCEV_PAT_ERB_NOEND, 0xFFFFFFFF])) { |
||
| 299 | return $ret; |
||
| 300 | } |
||
| 301 | |||
| 302 | $ret["term"] = (int) $data["term"] > 0x2000 ? (int) $data["term"] - 0x2000 : $data["term"]; |
||
| 303 | $ret["numoccur"] = $data["numoccur"]; |
||
| 304 | $ret["numexcept"] = $data["numexcept"]; |
||
| 305 | |||
| 306 | // exc_base_dates are *all* the base dates that have been either deleted or modified |
||
| 307 | $exc_base_dates = []; |
||
| 308 | for ($i = 0; $i < $ret["numexcept"]; ++$i) { |
||
| 309 | if (strlen($rdata) < 4) { |
||
| 310 | // We shouldn't arrive here, because that implies |
||
| 311 | // numexcept does not match the amount of data |
||
| 312 | // which is available for the exceptions. |
||
| 313 | return $ret; |
||
| 314 | } |
||
| 315 | $data = unpack("Vbasedate", $rdata); |
||
| 316 | $rdata = substr($rdata, 4); |
||
| 317 | $exc_base_dates[] = $this->recurDataToUnixData($data["basedate"]); |
||
| 318 | } |
||
| 319 | |||
| 320 | if (strlen($rdata) < 4) { |
||
| 321 | return $ret; |
||
| 322 | } |
||
| 323 | |||
| 324 | $data = unpack("Vnumexceptmod", $rdata); |
||
| 325 | $rdata = substr($rdata, 4); |
||
| 326 | |||
| 327 | $ret["numexceptmod"] = $data["numexceptmod"]; |
||
| 328 | |||
| 329 | // exc_changed are the base dates of *modified* occurrences. exactly what is modified |
||
| 330 | // is in the attachments *and* in the data further down this function. |
||
| 331 | $exc_changed = []; |
||
| 332 | for ($i = 0; $i < $ret["numexceptmod"]; ++$i) { |
||
| 333 | if (strlen($rdata) < 4) { |
||
| 334 | // We shouldn't arrive here, because that implies |
||
| 335 | // numexceptmod does not match the amount of data |
||
| 336 | // which is available for the exceptions. |
||
| 337 | return $ret; |
||
| 338 | } |
||
| 339 | $data = unpack("Vstartdate", $rdata); |
||
| 340 | $rdata = substr($rdata, 4); |
||
| 341 | $exc_changed[] = $this->recurDataToUnixData($data["startdate"]); |
||
| 342 | } |
||
| 343 | |||
| 344 | if (strlen($rdata) < 8) { |
||
| 345 | return $ret; |
||
| 346 | } |
||
| 347 | |||
| 348 | $data = unpack("Vstart/Vend", $rdata); |
||
| 349 | $rdata = substr($rdata, 8); |
||
| 350 | |||
| 351 | $ret["start"] = $this->recurDataToUnixData($data["start"]); |
||
| 352 | $ret["end"] = $this->recurDataToUnixData($data["end"]); |
||
| 353 | |||
| 354 | // this is where task recurrence stop |
||
| 355 | if (strlen($rdata) < 16) { |
||
| 356 | return $ret; |
||
| 357 | } |
||
| 358 | |||
| 359 | $data = unpack("Vreaderversion/Vwriterversion/Vstartmin/Vendmin", $rdata); |
||
| 360 | $rdata = substr($rdata, 16); |
||
| 361 | |||
| 362 | $ret["startocc"] = $data["startmin"]; |
||
| 363 | $ret["endocc"] = $data["endmin"]; |
||
| 364 | $writerversion = $data["writerversion"]; |
||
| 365 | |||
| 366 | $data = unpack("vnumber", $rdata); |
||
| 367 | $rdata = substr($rdata, 2); |
||
| 368 | |||
| 369 | $nexceptions = $data["number"]; |
||
| 370 | $exc_changed_details = []; |
||
| 371 | |||
| 372 | // Parse n modified exceptions |
||
| 373 | for ($i = 0; $i < $nexceptions; ++$i) { |
||
| 374 | $item = []; |
||
| 375 | |||
| 376 | // Get exception startdate, enddate and basedate (the date at which the occurrence would have started) |
||
| 377 | $data = unpack("Vstartdate/Venddate/Vbasedate", $rdata); |
||
| 378 | $rdata = substr($rdata, 12); |
||
| 379 | |||
| 380 | // Convert recurtimestamp to unix timestamp |
||
| 381 | $startdate = $this->recurDataToUnixData($data["startdate"]); |
||
| 382 | $enddate = $this->recurDataToUnixData($data["enddate"]); |
||
| 383 | $basedate = $this->recurDataToUnixData($data["basedate"]); |
||
| 384 | |||
| 385 | // Set the right properties |
||
| 386 | $item["basedate"] = $this->dayStartOf($basedate); |
||
| 387 | $item["start"] = $startdate; |
||
| 388 | $item["end"] = $enddate; |
||
| 389 | |||
| 390 | $data = unpack("vbitmask", $rdata); |
||
| 391 | $rdata = substr($rdata, 2); |
||
| 392 | $item["bitmask"] = $data["bitmask"]; // save bitmask for extended exceptions |
||
| 393 | |||
| 394 | // Bitmask to verify what properties are changed |
||
| 395 | $bitmask = $data["bitmask"]; |
||
| 396 | |||
| 397 | // ARO_SUBJECT: 0x0001 |
||
| 398 | // Look for field: SubjectLength (2b), SubjectLength2 (2b) and Subject |
||
| 399 | if ($bitmask & (1 << 0)) { |
||
| 400 | $data = unpack("vnull_length/vlength", $rdata); |
||
| 401 | $rdata = substr($rdata, 4); |
||
| 402 | |||
| 403 | $length = $data["length"]; |
||
| 404 | $item["subject"] = ""; // Normalized subject |
||
| 405 | for ($j = 0; $j < $length && strlen($rdata); ++$j) { |
||
| 406 | $data = unpack("Cchar", $rdata); |
||
| 407 | $rdata = substr($rdata, 1); |
||
| 408 | |||
| 409 | $item["subject"] .= chr($data["char"]); |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | // ARO_MEETINGTYPE: 0x0002 |
||
| 414 | if ($bitmask & (1 << 1)) { |
||
| 415 | $rdata = substr($rdata, 4); |
||
| 416 | // Attendees modified: no data here (only in attachment) |
||
| 417 | } |
||
| 418 | |||
| 419 | // ARO_REMINDERDELTA: 0x0004 |
||
| 420 | // Look for field: ReminderDelta (4b) |
||
| 421 | if ($bitmask & (1 << 2)) { |
||
| 422 | $data = unpack("Vremind_before", $rdata); |
||
| 423 | $rdata = substr($rdata, 4); |
||
| 424 | |||
| 425 | $item["remind_before"] = $data["remind_before"]; |
||
| 426 | } |
||
| 427 | |||
| 428 | // ARO_REMINDER: 0x0008 |
||
| 429 | // Look field: ReminderSet (4b) |
||
| 430 | if ($bitmask & (1 << 3)) { |
||
| 431 | $data = unpack("Vreminder_set", $rdata); |
||
| 432 | $rdata = substr($rdata, 4); |
||
| 433 | |||
| 434 | $item["reminder_set"] = $data["reminder_set"]; |
||
| 435 | } |
||
| 436 | |||
| 437 | // ARO_LOCATION: 0x0010 |
||
| 438 | // Look for fields: LocationLength (2b), LocationLength2 (2b) and Location |
||
| 439 | // Similar to ARO_SUBJECT above. |
||
| 440 | if ($bitmask & (1 << 4)) { |
||
| 441 | $data = unpack("vnull_length/vlength", $rdata); |
||
| 442 | $rdata = substr($rdata, 4); |
||
| 443 | |||
| 444 | $item["location"] = ""; |
||
| 445 | |||
| 446 | $length = $data["length"]; |
||
| 447 | $data = substr($rdata, 0, $length); |
||
| 448 | $rdata = substr($rdata, $length); |
||
| 449 | |||
| 450 | $item["location"] .= $data; |
||
| 451 | } |
||
| 452 | |||
| 453 | // ARO_BUSYSTATUS: 0x0020 |
||
| 454 | // Look for field: BusyStatus (4b) |
||
| 455 | if ($bitmask & (1 << 5)) { |
||
| 456 | $data = unpack("Vbusystatus", $rdata); |
||
| 457 | $rdata = substr($rdata, 4); |
||
| 458 | |||
| 459 | $item["busystatus"] = $data["busystatus"]; |
||
| 460 | } |
||
| 461 | |||
| 462 | // ARO_ATTACHMENT: 0x0040 |
||
| 463 | if ($bitmask & (1 << 6)) { |
||
| 464 | // no data: RESERVED |
||
| 465 | $rdata = substr($rdata, 4); |
||
| 466 | } |
||
| 467 | |||
| 468 | // ARO_SUBTYPE: 0x0080 |
||
| 469 | // Look for field: SubType (4b). Determines whether it is an allday event. |
||
| 470 | if ($bitmask & (1 << 7)) { |
||
| 471 | $data = unpack("Vallday", $rdata); |
||
| 472 | $rdata = substr($rdata, 4); |
||
| 473 | |||
| 474 | $item["alldayevent"] = $data["allday"]; |
||
| 475 | } |
||
| 476 | |||
| 477 | // ARO_APPTCOLOR: 0x0100 |
||
| 478 | // Look for field: AppointmentColor (4b) |
||
| 479 | if ($bitmask & (1 << 8)) { |
||
| 480 | $data = unpack("Vlabel", $rdata); |
||
| 481 | $rdata = substr($rdata, 4); |
||
| 482 | |||
| 483 | $item["label"] = $data["label"]; |
||
| 484 | } |
||
| 485 | |||
| 486 | // ARO_EXCEPTIONAL_BODY: 0x0200 |
||
| 487 | if ($bitmask & (1 << 9)) { |
||
| 488 | // Notes or Attachments modified: no data here (only in attachment) |
||
| 489 | } |
||
| 490 | |||
| 491 | array_push($exc_changed_details, $item); |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * We now have $exc_changed, $exc_base_dates and $exc_changed_details |
||
| 496 | * We will ignore $exc_changed, as this information is available in $exc_changed_details |
||
| 497 | * also. If an item is in $exc_base_dates and NOT in $exc_changed_details, then the item |
||
| 498 | * has been deleted. |
||
| 499 | */ |
||
| 500 | |||
| 501 | // Find deleted occurrences |
||
| 502 | $deleted_occurrences = []; |
||
| 503 | |||
| 504 | foreach ($exc_base_dates as $base_date) { |
||
| 505 | $found = false; |
||
| 506 | |||
| 507 | foreach ($exc_changed_details as $details) { |
||
| 508 | if ($details["basedate"] == $base_date) { |
||
| 509 | $found = true; |
||
| 510 | break; |
||
| 511 | } |
||
| 512 | } |
||
| 513 | if (!$found) { |
||
| 514 | // item was not in exc_changed_details, so it must be deleted |
||
| 515 | $deleted_occurrences[] = $base_date; |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | $ret["deleted_occurrences"] = $deleted_occurrences; |
||
| 520 | $ret["changed_occurrences"] = $exc_changed_details; |
||
| 521 | |||
| 522 | // enough data for normal exception (no extended data) |
||
| 523 | if (strlen($rdata) < 8) { |
||
| 524 | return $ret; |
||
| 525 | } |
||
| 526 | |||
| 527 | $data = unpack("Vreservedsize", $rdata); |
||
| 528 | $rdata = substr($rdata, 4 + $data["reservedsize"]); |
||
| 529 | |||
| 530 | for ($i = 0; $i < $nexceptions; ++$i) { |
||
| 531 | // subject and location in ucs-2 to utf-8 |
||
| 532 | if ($writerversion >= 0x3009) { |
||
| 533 | $data = unpack("Vsize/Vvalue", $rdata); // size includes sizeof(value)==4 |
||
| 534 | $rdata = substr($rdata, 4 + $data["size"]); |
||
| 535 | } |
||
| 536 | |||
| 537 | $data = unpack("Vreservedsize", $rdata); |
||
| 538 | $rdata = substr($rdata, 4 + $data["reservedsize"]); |
||
| 539 | |||
| 540 | // ARO_SUBJECT(0x01) | ARO_LOCATION(0x10) |
||
| 541 | if ($exc_changed_details[$i]["bitmask"] & 0x11) { |
||
| 542 | $data = unpack("Vstart/Vend/Vorig", $rdata); |
||
| 543 | $rdata = substr($rdata, 4 * 3); |
||
| 544 | |||
| 545 | $exc_changed_details[$i]["ex_start_datetime"] = $data["start"]; |
||
| 546 | $exc_changed_details[$i]["ex_end_datetime"] = $data["end"]; |
||
| 547 | $exc_changed_details[$i]["ex_orig_date"] = $data["orig"]; |
||
| 548 | } |
||
| 549 | |||
| 550 | // ARO_SUBJECT |
||
| 551 | if ($exc_changed_details[$i]["bitmask"] & 0x01) { |
||
| 552 | // decode ucs2 string to utf-8 |
||
| 553 | $data = unpack("vlength", $rdata); |
||
| 554 | $rdata = substr($rdata, 2); |
||
| 555 | $length = $data["length"]; |
||
| 556 | $data = substr($rdata, 0, $length * 2); |
||
| 557 | $rdata = substr($rdata, $length * 2); |
||
| 558 | $subject = iconv("UCS-2LE", "UTF-8", $data); |
||
| 559 | // replace subject with unicode subject |
||
| 560 | $exc_changed_details[$i]["subject"] = $subject; |
||
| 561 | } |
||
| 562 | |||
| 563 | // ARO_LOCATION |
||
| 564 | if ($exc_changed_details[$i]["bitmask"] & 0x10) { |
||
| 565 | // decode ucs2 string to utf-8 |
||
| 566 | $data = unpack("vlength", $rdata); |
||
| 567 | $rdata = substr($rdata, 2); |
||
| 568 | $length = $data["length"]; |
||
| 569 | $data = substr($rdata, 0, $length * 2); |
||
| 570 | $rdata = substr($rdata, $length * 2); |
||
| 571 | $location = iconv("UCS-2LE", "UTF-8", $data); |
||
| 572 | // replace subject with unicode subject |
||
| 573 | $exc_changed_details[$i]["location"] = $location; |
||
| 574 | } |
||
| 575 | |||
| 576 | // ARO_SUBJECT(0x01) | ARO_LOCATION(0x10) |
||
| 577 | if ($exc_changed_details[$i]["bitmask"] & 0x11) { |
||
| 578 | $data = unpack("Vreservedsize", $rdata); |
||
| 579 | $rdata = substr($rdata, 4 + $data["reservedsize"]); |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | // update with extended data |
||
| 584 | $ret["changed_occurrences"] = $exc_changed_details; |
||
| 585 | |||
| 586 | return $ret; |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Saves the recurrence data to the recurrence property. |
||
| 591 | */ |
||
| 592 | public function saveRecurrence(): void { |
||
| 593 | // Only save if a message was passed |
||
| 594 | if (!isset($this->message)) { |
||
| 595 | return; |
||
| 596 | } |
||
| 597 | |||
| 598 | // Abort if no recurrence was set |
||
| 599 | if (!isset( |
||
| 600 | $this->recur["type"], |
||
| 601 | $this->recur["subtype"], |
||
| 602 | $this->recur["start"], |
||
| 603 | $this->recur["end"], |
||
| 604 | $this->recur["startocc"], |
||
| 605 | $this->recur["endocc"]) |
||
| 606 | ) { |
||
| 607 | return; |
||
| 608 | } |
||
| 609 | |||
| 610 | $rtype = 0x2000 + (int) $this->recur["type"]; |
||
| 611 | |||
| 612 | // Don't allow invalid type and subtype values |
||
| 613 | if (!in_array($rtype, [IDC_RCEV_PAT_ORB_DAILY, IDC_RCEV_PAT_ORB_WEEKLY, IDC_RCEV_PAT_ORB_MONTHLY, IDC_RCEV_PAT_ORB_YEARLY])) { |
||
| 614 | return; |
||
| 615 | } |
||
| 616 | |||
| 617 | if (!in_array((int) $this->recur["subtype"], [rptDay, rptWeek, rptMonth, rptMonthNth, rptMonthEnd, rptHjMonth, rptHjMonthNth, rptHjMonthEnd])) { |
||
| 618 | return; |
||
| 619 | } |
||
| 620 | |||
| 621 | $rdata = pack("vvvvv", 0x3004, 0x3004, $rtype, (int) $this->recur["subtype"], MAPI_CAL_DEFAULT); |
||
| 622 | $weekstart = 1; // monday |
||
| 623 | $forwardcount = 0; |
||
| 624 | $restocc = 0; |
||
| 625 | $dayofweek = (int) gmdate("w", (int) $this->recur["start"]); // 0 (for Sunday) through 6 (for Saturday) |
||
| 626 | |||
| 627 | // Terminate |
||
| 628 | $term = (int) $this->recur["term"] < 0x2000 ? 0x2000 + (int) $this->recur["term"] : (int) $this->recur["term"]; |
||
| 629 | |||
| 630 | switch ($rtype) { |
||
| 631 | case IDC_RCEV_PAT_ORB_DAILY: |
||
| 632 | if (!isset($this->recur["everyn"]) || (int) $this->recur["everyn"] > 1438560 || (int) $this->recur["everyn"] < 0) { // minutes for 999 days |
||
| 633 | return; |
||
| 634 | } |
||
| 635 | |||
| 636 | if ($this->recur["subtype"] == rptWeek) { |
||
| 637 | // Daily every workday |
||
| 638 | $rdata .= pack("VVVV", 6 * 24 * 60, 1, 0, 0x3E); |
||
| 639 | } |
||
| 640 | else { |
||
| 641 | // Calc first occ |
||
| 642 | $firstocc = $this->unixDataToRecurData($this->recur["start"]) % ((int) $this->recur["everyn"]); |
||
| 643 | |||
| 644 | $rdata .= pack("VVV", $firstocc, (int) $this->recur["everyn"], $this->recur["regen"] ? 1 : 0); |
||
| 645 | } |
||
| 646 | break; |
||
| 647 | |||
| 648 | case IDC_RCEV_PAT_ORB_WEEKLY: |
||
| 649 | if (!isset($this->recur["everyn"]) || $this->recur["everyn"] > 99 || (int) $this->recur["everyn"] < 0) { |
||
| 650 | return; |
||
| 651 | } |
||
| 652 | |||
| 653 | if (!$this->recur["regen"] && !isset($this->recur["weekdays"])) { |
||
| 654 | return; |
||
| 655 | } |
||
| 656 | |||
| 657 | // No need to calculate startdate if sliding flag was set. |
||
| 658 | if (!$this->recur['regen']) { |
||
| 659 | // Calculate start date of recurrence |
||
| 660 | |||
| 661 | // Find the first day that matches one of the weekdays selected |
||
| 662 | $daycount = 0; |
||
| 663 | $dayskip = -1; |
||
| 664 | for ($j = 0; $j < 7; ++$j) { |
||
| 665 | if (((int) $this->recur["weekdays"]) & (1 << (($dayofweek + $j) % 7))) { |
||
| 666 | if ($dayskip == -1) { |
||
| 667 | $dayskip = $j; |
||
| 668 | } |
||
| 669 | |||
| 670 | ++$daycount; |
||
| 671 | } |
||
| 672 | } |
||
| 673 | |||
| 674 | // $dayskip is the number of days to skip from the startdate until the first occurrence |
||
| 675 | // $daycount is the number of days per week that an occurrence occurs |
||
| 676 | |||
| 677 | $weekskip = 0; |
||
| 678 | if (($dayofweek < $weekstart && $dayskip > 0) || ($dayofweek + $dayskip) > 6) { |
||
| 679 | $weekskip = 1; |
||
| 680 | } |
||
| 681 | |||
| 682 | // Check if the recurrence ends after a number of occurrences, in that case we must calculate the |
||
| 683 | // remaining occurrences based on the start of the recurrence. |
||
| 684 | if ($term == IDC_RCEV_PAT_ERB_AFTERNOCCUR) { |
||
| 685 | // $weekskip is the amount of weeks to skip from the startdate before the first occurrence |
||
| 686 | // $forwardcount is the maximum number of week occurrences we can go ahead after the first occurrence that |
||
| 687 | // is still inside the recurrence. We subtract one to make sure that the last week is never forwarded over |
||
| 688 | // (eg when numoccur = 2, and daycount = 1) |
||
| 689 | $forwardcount = floor((int) ($this->recur["numoccur"] - 1) / $daycount); |
||
| 690 | |||
| 691 | // $restocc is the number of occurrences left after $forwardcount whole weeks of occurrences, minus one |
||
| 692 | // for the occurrence on the first day |
||
| 693 | $restocc = ((int) $this->recur["numoccur"]) - ($forwardcount * $daycount) - 1; |
||
| 694 | |||
| 695 | // $forwardcount is now the number of weeks we can go forward and still be inside the recurrence |
||
| 696 | $forwardcount *= (int) $this->recur["everyn"]; |
||
| 697 | } |
||
| 698 | |||
| 699 | // The real start is start + dayskip + weekskip-1 (since dayskip will already bring us into the next week) |
||
| 700 | $this->recur["start"] = ((int) $this->recur["start"]) + ($dayskip * 24 * 60 * 60) + ($weekskip * (((int) $this->recur["everyn"]) - 1) * 7 * 24 * 60 * 60); |
||
| 701 | } |
||
| 702 | |||
| 703 | // Calc first occ |
||
| 704 | $firstocc = $this->unixDataToRecurData($this->recur["start"]) % (((int) $this->recur["everyn"]) * 7 * 24 * 60); |
||
| 705 | |||
| 706 | $firstocc -= (((int) gmdate("w", (int) $this->recur["start"])) - 1) * 24 * 60; |
||
| 707 | |||
| 708 | if ($this->recur["regen"]) { |
||
| 709 | $rdata .= pack("VVV", $firstocc, (int) $this->recur["everyn"], 1); |
||
| 710 | } |
||
| 711 | else { |
||
| 712 | $rdata .= pack("VVVV", $firstocc, (int) $this->recur["everyn"], 0, (int) $this->recur["weekdays"]); |
||
| 713 | } |
||
| 714 | break; |
||
| 715 | |||
| 716 | case IDC_RCEV_PAT_ORB_MONTHLY: |
||
| 717 | case IDC_RCEV_PAT_ORB_YEARLY: |
||
| 718 | if (!isset($this->recur["everyn"])) { |
||
| 719 | return; |
||
| 720 | } |
||
| 721 | if ($rtype == IDC_RCEV_PAT_ORB_YEARLY && !isset($this->recur["month"])) { |
||
| 722 | return; |
||
| 723 | } |
||
| 724 | |||
| 725 | if ($rtype == IDC_RCEV_PAT_ORB_MONTHLY) { |
||
| 726 | $everyn = (int) $this->recur["everyn"]; |
||
| 727 | if ($everyn > 99 || $everyn < 0) { |
||
| 728 | return; |
||
| 729 | } |
||
| 730 | } |
||
| 731 | else { |
||
| 732 | $everyn = $this->recur["regen"] ? ((int) $this->recur["everyn"]) * 12 : 12; |
||
| 733 | } |
||
| 734 | |||
| 735 | // Get montday/month/year of original start |
||
| 736 | $curmonthday = gmdate("j", (int) $this->recur["start"]); |
||
| 737 | $curyear = gmdate("Y", (int) $this->recur["start"]); |
||
| 738 | $curmonth = gmdate("n", (int) $this->recur["start"]); |
||
| 739 | |||
| 740 | // Check if the recurrence ends after a number of occurrences, in that case we must calculate the |
||
| 741 | // remaining occurrences based on the start of the recurrence. |
||
| 742 | if ($term == IDC_RCEV_PAT_ERB_AFTERNOCCUR) { |
||
| 743 | // $forwardcount is the number of occurrences we can skip and still be inside the recurrence range (minus |
||
| 744 | // one to make sure there are always at least one occurrence left) |
||
| 745 | $forwardcount = ((((int) $this->recur["numoccur"]) - 1) * $everyn); |
||
| 746 | } |
||
| 747 | |||
| 748 | // Get month for yearly on D'th day of month M |
||
| 749 | if ($rtype == IDC_RCEV_PAT_ORB_YEARLY) { |
||
| 750 | $selmonth = floor(((int) $this->recur["month"]) / (24 * 60 * 29)) + 1; // 1=jan, 2=feb, eg |
||
| 751 | } |
||
| 752 | |||
| 753 | switch ((int) $this->recur["subtype"]) { |
||
| 754 | // on D day of every M month |
||
| 755 | case rptMonth: |
||
| 756 | if (!isset($this->recur["monthday"])) { |
||
| 757 | return; |
||
| 758 | } |
||
| 759 | // Recalc startdate |
||
| 760 | |||
| 761 | // Set on the right begin day |
||
| 762 | |||
| 763 | // Go the beginning of the month |
||
| 764 | $this->recur["start"] -= ($curmonthday - 1) * 24 * 60 * 60; |
||
| 765 | // Go the the correct month day |
||
| 766 | $this->recur["start"] += (((int) $this->recur["monthday"]) - 1) * 24 * 60 * 60; |
||
| 767 | |||
| 768 | // If the previous calculation gave us a start date different than the original start date, then we need to skip to the first occurrence |
||
| 769 | if (($rtype == IDC_RCEV_PAT_ORB_MONTHLY && ((int) $this->recur["monthday"]) < $curmonthday) || |
||
| 770 | ($rtype == IDC_RCEV_PAT_ORB_YEARLY && ($selmonth != $curmonth || ($selmonth == $curmonth && ((int) $this->recur["monthday"]) < $curmonthday)))) { |
||
| 771 | if ($rtype == IDC_RCEV_PAT_ORB_YEARLY) { |
||
| 772 | if ($curmonth > $selmonth) {// go to next occurrence in 'everyn' months minus difference in first occurrence and original date |
||
| 773 | $count = $everyn - ($curmonth - $selmonth); |
||
| 774 | } |
||
| 775 | elseif ($curmonth < $selmonth) {// go to next occurrence upto difference in first occurrence and original date |
||
| 776 | $count = $selmonth - $curmonth; |
||
| 777 | } |
||
| 778 | else { |
||
| 779 | // Go to next occurrence while recurrence start date is greater than occurrence date but within same month |
||
| 780 | if (((int) $this->recur["monthday"]) < $curmonthday) { |
||
| 781 | $count = $everyn; |
||
| 782 | } |
||
| 783 | } |
||
| 784 | } |
||
| 785 | else { |
||
| 786 | $count = $everyn; // Monthly, go to next occurrence in 'everyn' months |
||
| 787 | } |
||
| 788 | |||
| 789 | // Forward by $count months. This is done by getting the number of days in that month and forwarding that many days |
||
| 790 | for ($i = 0; $i < $count; ++$i) { |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 791 | $this->recur["start"] += $this->getMonthInSeconds($curyear, $curmonth); |
||
| 792 | |||
| 793 | if ($curmonth == 12) { |
||
| 794 | ++$curyear; |
||
| 795 | $curmonth = 0; |
||
| 796 | } |
||
| 797 | ++$curmonth; |
||
| 798 | } |
||
| 799 | } |
||
| 800 | |||
| 801 | // "start" is now pointing to the first occurrence, except that it will overshoot if the |
||
| 802 | // month in which it occurs has less days than specified as the day of the month. So 31st |
||
| 803 | // of each month will overshoot in february (29 days). We compensate for that by checking |
||
| 804 | // if the day of the month we got is wrong, and then back up to the last day of the previous |
||
| 805 | // month. |
||
| 806 | if (((int) $this->recur["monthday"]) >= 28 && ((int) $this->recur["monthday"]) <= 31 && |
||
| 807 | gmdate("j", (int) $this->recur["start"]) < ((int) $this->recur["monthday"])) { |
||
| 808 | $this->recur["start"] -= gmdate("j", (int) $this->recur["start"]) * 24 * 60 * 60; |
||
| 809 | } |
||
| 810 | |||
| 811 | // "start" is now the first occurrence |
||
| 812 | if ($rtype == IDC_RCEV_PAT_ORB_MONTHLY) { |
||
| 813 | // Calc first occ |
||
| 814 | $monthIndex = ((((12 % $everyn) * ((((int) gmdate("Y", $this->recur["start"])) - 1601) % $everyn)) % $everyn) + (((int) gmdate("n", $this->recur["start"])) - 1)) % $everyn; |
||
| 815 | |||
| 816 | $firstocc = 0; |
||
| 817 | for ($i = 0; $i < $monthIndex; ++$i) { |
||
| 818 | $firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), ($i % 12) + 1) / 60; |
||
| 819 | } |
||
| 820 | |||
| 821 | $rdata .= pack("VVVV", $firstocc, $everyn, $this->recur["regen"], (int) $this->recur["monthday"]); |
||
| 822 | } |
||
| 823 | else { |
||
| 824 | // Calc first occ |
||
| 825 | $firstocc = 0; |
||
| 826 | $monthIndex = (int) gmdate("n", $this->recur["start"]); |
||
| 827 | for ($i = 1; $i < $monthIndex; ++$i) { |
||
| 828 | $firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), $i) / 60; |
||
| 829 | } |
||
| 830 | |||
| 831 | $rdata .= pack("VVVV", $firstocc, $everyn, $this->recur["regen"], (int) $this->recur["monthday"]); |
||
| 832 | } |
||
| 833 | break; |
||
| 834 | |||
| 835 | case rptMonthNth: |
||
| 836 | // monthly: on Nth weekday of every M month |
||
| 837 | // yearly: on Nth weekday of M month |
||
| 838 | if (!isset($this->recur["weekdays"], $this->recur["nday"])) { |
||
| 839 | return; |
||
| 840 | } |
||
| 841 | |||
| 842 | $weekdays = (int) $this->recur["weekdays"]; |
||
| 843 | $nday = (int) $this->recur["nday"]; |
||
| 844 | |||
| 845 | // Calc startdate |
||
| 846 | $monthbegindow = (int) $this->recur["start"]; |
||
| 847 | |||
| 848 | if ($nday == 5) { |
||
| 849 | // Set date on the last day of the last month |
||
| 850 | $monthbegindow += (gmdate("t", $monthbegindow) - gmdate("j", $monthbegindow)) * 24 * 60 * 60; |
||
| 851 | } |
||
| 852 | else { |
||
| 853 | // Set on the first day of the month |
||
| 854 | $monthbegindow -= ((gmdate("j", $monthbegindow) - 1) * 24 * 60 * 60); |
||
| 855 | } |
||
| 856 | |||
| 857 | if ($rtype == IDC_RCEV_PAT_ORB_YEARLY) { |
||
| 858 | // Set on right month |
||
| 859 | if ($selmonth < $curmonth) { |
||
| 860 | $tmp = 12 - $curmonth + $selmonth; |
||
| 861 | } |
||
| 862 | else { |
||
| 863 | $tmp = ($selmonth - $curmonth); |
||
| 864 | } |
||
| 865 | |||
| 866 | for ($i = 0; $i < $tmp; ++$i) { |
||
| 867 | $monthbegindow += $this->getMonthInSeconds($curyear, $curmonth); |
||
| 868 | |||
| 869 | if ($curmonth == 12) { |
||
| 870 | ++$curyear; |
||
| 871 | $curmonth = 0; |
||
| 872 | } |
||
| 873 | ++$curmonth; |
||
| 874 | } |
||
| 875 | } |
||
| 876 | else { |
||
| 877 | // Check or you exist in the right month |
||
| 878 | |||
| 879 | $dayofweek = gmdate("w", $monthbegindow); |
||
| 880 | for ($i = 0; $i < 7; ++$i) { |
||
| 881 | if ($nday == 5 && (($dayofweek - $i) % 7 >= 0) && (1 << (($dayofweek - $i) % 7)) & $weekdays) { |
||
| 882 | $day = gmdate("j", $monthbegindow) - $i; |
||
| 883 | break; |
||
| 884 | } |
||
| 885 | if ($nday != 5 && (1 << (($dayofweek + $i) % 7)) & $weekdays) { |
||
| 886 | $day = (($nday - 1) * 7) + ($i + 1); |
||
| 887 | break; |
||
| 888 | } |
||
| 889 | } |
||
| 890 | |||
| 891 | // Goto the next X month |
||
| 892 | if (isset($day) && ($day < gmdate("j", (int) $this->recur["start"]))) { |
||
| 893 | if ($nday == 5) { |
||
| 894 | $monthbegindow += 24 * 60 * 60; |
||
| 895 | if ($curmonth == 12) { |
||
| 896 | ++$curyear; |
||
| 897 | $curmonth = 0; |
||
| 898 | } |
||
| 899 | ++$curmonth; |
||
| 900 | } |
||
| 901 | |||
| 902 | for ($i = 0; $i < $everyn; ++$i) { |
||
| 903 | $monthbegindow += $this->getMonthInSeconds($curyear, $curmonth); |
||
| 904 | |||
| 905 | if ($curmonth == 12) { |
||
| 906 | ++$curyear; |
||
| 907 | $curmonth = 0; |
||
| 908 | } |
||
| 909 | ++$curmonth; |
||
| 910 | } |
||
| 911 | |||
| 912 | if ($nday == 5) { |
||
| 913 | $monthbegindow -= 24 * 60 * 60; |
||
| 914 | } |
||
| 915 | } |
||
| 916 | } |
||
| 917 | |||
| 918 | // FIXME: weekstart? |
||
| 919 | |||
| 920 | $day = 0; |
||
| 921 | // Set start on the right day |
||
| 922 | $dayofweek = gmdate("w", $monthbegindow); |
||
| 923 | for ($i = 0; $i < 7; ++$i) { |
||
| 924 | if ($nday == 5 && (($dayofweek - $i) % 7) >= 0 && (1 << (($dayofweek - $i) % 7)) & $weekdays) { |
||
| 925 | $day = $i; |
||
| 926 | break; |
||
| 927 | } |
||
| 928 | if ($nday != 5 && (1 << (($dayofweek + $i) % 7)) & $weekdays) { |
||
| 929 | $day = ($nday - 1) * 7 + ($i + 1); |
||
| 930 | break; |
||
| 931 | } |
||
| 932 | } |
||
| 933 | if ($nday == 5) { |
||
| 934 | $monthbegindow -= $day * 24 * 60 * 60; |
||
| 935 | } |
||
| 936 | else { |
||
| 937 | $monthbegindow += ($day - 1) * 24 * 60 * 60; |
||
| 938 | } |
||
| 939 | |||
| 940 | $firstocc = 0; |
||
| 941 | if ($rtype == IDC_RCEV_PAT_ORB_MONTHLY) { |
||
| 942 | // Calc first occ |
||
| 943 | $monthIndex = ((((12 % $everyn) * (((int) gmdate("Y", $this->recur["start"]) - 1601) % $everyn)) % $everyn) + (((int) gmdate("n", $this->recur["start"])) - 1)) % $everyn; |
||
| 944 | |||
| 945 | for ($i = 0; $i < $monthIndex; ++$i) { |
||
| 946 | $firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), ($i % 12) + 1) / 60; |
||
| 947 | } |
||
| 948 | |||
| 949 | $rdata .= pack("VVVVV", $firstocc, $everyn, 0, $weekdays, $nday); |
||
| 950 | } |
||
| 951 | else { |
||
| 952 | // Calc first occ |
||
| 953 | $monthIndex = (int) gmdate("n", $this->recur["start"]); |
||
| 954 | |||
| 955 | for ($i = 1; $i < $monthIndex; ++$i) { |
||
| 956 | $firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), $i) / 60; |
||
| 957 | } |
||
| 958 | |||
| 959 | $rdata .= pack("VVVVV", $firstocc, $everyn, 0, $weekdays, $nday); |
||
| 960 | } |
||
| 961 | break; |
||
| 962 | } |
||
| 963 | break; |
||
| 964 | } |
||
| 965 | |||
| 966 | if (!isset($this->recur["term"])) { |
||
| 967 | return; |
||
| 968 | } |
||
| 969 | |||
| 970 | $rdata .= pack("V", $term); |
||
| 971 | |||
| 972 | switch ($term) { |
||
| 973 | // After the given enddate |
||
| 974 | case IDC_RCEV_PAT_ERB_END: |
||
| 975 | $rdata .= pack("V", 10); |
||
| 976 | break; |
||
| 977 | |||
| 978 | // After a number of times |
||
| 979 | case IDC_RCEV_PAT_ERB_AFTERNOCCUR: |
||
| 980 | if (!isset($this->recur["numoccur"])) { |
||
| 981 | return; |
||
| 982 | } |
||
| 983 | |||
| 984 | $rdata .= pack("V", (int) $this->recur["numoccur"]); |
||
| 985 | break; |
||
| 986 | |||
| 987 | // Never ends |
||
| 988 | case IDC_RCEV_PAT_ERB_NOEND: |
||
| 989 | $rdata .= pack("V", 0); |
||
| 990 | break; |
||
| 991 | } |
||
| 992 | |||
| 993 | // Strange little thing for the recurrence type "every workday" |
||
| 994 | if ($rtype == IDC_RCEV_PAT_ORB_WEEKLY && ((int) $this->recur["subtype"]) == 1) { |
||
| 995 | $rdata .= pack("V", 1); |
||
| 996 | } |
||
| 997 | else { // Other recurrences |
||
| 998 | $rdata .= pack("V", 0); |
||
| 999 | } |
||
| 1000 | |||
| 1001 | // Exception data |
||
| 1002 | |||
| 1003 | // Get all exceptions |
||
| 1004 | $deleted_items = $this->recur["deleted_occurrences"]; |
||
| 1005 | $changed_items = $this->recur["changed_occurrences"]; |
||
| 1006 | |||
| 1007 | // Merge deleted and changed items into one list |
||
| 1008 | $items = $deleted_items; |
||
| 1009 | |||
| 1010 | foreach ($changed_items as $changed_item) { |
||
| 1011 | array_push($items, $this->dayStartOf($changed_item["basedate"])); |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | sort($items); |
||
| 1015 | |||
| 1016 | // Add the merged list in to the rdata |
||
| 1017 | $rdata .= pack("V", count($items)); |
||
| 1018 | foreach ($items as $item) { |
||
| 1019 | $rdata .= pack("V", $this->unixDataToRecurData($item)); |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | // Loop through the changed exceptions (not deleted) |
||
| 1023 | $rdata .= pack("V", count($changed_items)); |
||
| 1024 | $items = []; |
||
| 1025 | |||
| 1026 | foreach ($changed_items as $changed_item) { |
||
| 1027 | $items[] = $this->dayStartOf($changed_item["start"]); |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | sort($items); |
||
| 1031 | |||
| 1032 | // Add the changed items list int the rdata |
||
| 1033 | foreach ($items as $item) { |
||
| 1034 | $rdata .= pack("V", $this->unixDataToRecurData($item)); |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | // Set start date |
||
| 1038 | $rdata .= pack("V", $this->unixDataToRecurData((int) $this->recur["start"])); |
||
| 1039 | |||
| 1040 | // Set enddate |
||
| 1041 | switch ($term) { |
||
| 1042 | // After the given enddate |
||
| 1043 | case IDC_RCEV_PAT_ERB_END: |
||
| 1044 | $rdata .= pack("V", $this->unixDataToRecurData((int) $this->recur["end"])); |
||
| 1045 | break; |
||
| 1046 | |||
| 1047 | // After a number of times |
||
| 1048 | case IDC_RCEV_PAT_ERB_AFTERNOCCUR: |
||
| 1049 | // @todo: calculate enddate with intval($this->recur["startocc"]) + intval($this->recur["duration"]) > 24 hour |
||
| 1050 | $occenddate = (int) $this->recur["start"]; |
||
| 1051 | |||
| 1052 | switch ($rtype) { |
||
| 1053 | case IDC_RCEV_PAT_ORB_DAILY: |
||
| 1054 | if ($this->recur["subtype"] == rptWeek) { |
||
| 1055 | // Daily every workday |
||
| 1056 | $restocc = (int) $this->recur["numoccur"]; |
||
| 1057 | |||
| 1058 | // Get starting weekday |
||
| 1059 | $nowtime = $this->gmtime($occenddate); |
||
| 1060 | $j = $nowtime["tm_wday"]; |
||
| 1061 | |||
| 1062 | while (1) { |
||
| 1063 | if (($j % 7) > 0 && ($j % 7) < 6) { |
||
| 1064 | --$restocc; |
||
| 1065 | } |
||
| 1066 | |||
| 1067 | ++$j; |
||
| 1068 | |||
| 1069 | if ($restocc <= 0) { |
||
| 1070 | break; |
||
| 1071 | } |
||
| 1072 | |||
| 1073 | $occenddate += 24 * 60 * 60; |
||
| 1074 | } |
||
| 1075 | } |
||
| 1076 | else { |
||
| 1077 | // -1 because the first day already counts (from 1-1-1980 to 1-1-1980 is 1 occurrence) |
||
| 1078 | $occenddate += (((int) $this->recur["everyn"]) * 60 * ((int) $this->recur["numoccur"] - 1)); |
||
| 1079 | } |
||
| 1080 | break; |
||
| 1081 | |||
| 1082 | case IDC_RCEV_PAT_ORB_WEEKLY: |
||
| 1083 | // Needed values |
||
| 1084 | // $forwardcount - number of weeks we can skip forward |
||
| 1085 | // $restocc - number of remaining occurrences after the week skip |
||
| 1086 | |||
| 1087 | // Add the weeks till the last item |
||
| 1088 | $occenddate += ($forwardcount * 7 * 24 * 60 * 60); |
||
| 1089 | |||
| 1090 | $dayofweek = gmdate("w", $occenddate); |
||
| 1091 | |||
| 1092 | // Loop through the last occurrences until we have had them all |
||
| 1093 | for ($j = 1; $restocc > 0; ++$j) { |
||
| 1094 | // Jump to the next week (which may be N weeks away) when going over the week boundary |
||
| 1095 | if ((($dayofweek + $j) % 7) == $weekstart) { |
||
| 1096 | $occenddate += (((int) $this->recur["everyn"]) - 1) * 7 * 24 * 60 * 60; |
||
| 1097 | } |
||
| 1098 | |||
| 1099 | // If this is a matching day, once less occurrence to process |
||
| 1100 | if (((int) $this->recur["weekdays"]) & (1 << (($dayofweek + $j) % 7))) { |
||
| 1101 | --$restocc; |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | // Next day |
||
| 1105 | $occenddate += 24 * 60 * 60; |
||
| 1106 | } |
||
| 1107 | |||
| 1108 | break; |
||
| 1109 | |||
| 1110 | case IDC_RCEV_PAT_ORB_MONTHLY: |
||
| 1111 | case IDC_RCEV_PAT_ORB_YEARLY: |
||
| 1112 | $curyear = gmdate("Y", (int) $this->recur["start"]); |
||
| 1113 | $curmonth = gmdate("n", (int) $this->recur["start"]); |
||
| 1114 | // $forwardcount = months |
||
| 1115 | |||
| 1116 | switch ((int) $this->recur["subtype"]) { |
||
| 1117 | case rptMonth: // on D day of every M month |
||
| 1118 | while ($forwardcount > 0) { |
||
| 1119 | $occenddate += $this->getMonthInSeconds($curyear, $curmonth); |
||
| 1120 | |||
| 1121 | if ($curmonth >= 12) { |
||
| 1122 | $curmonth = 1; |
||
| 1123 | ++$curyear; |
||
| 1124 | } |
||
| 1125 | else { |
||
| 1126 | ++$curmonth; |
||
| 1127 | } |
||
| 1128 | --$forwardcount; |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | // compensation between 28 and 31 |
||
| 1132 | if (((int) $this->recur["monthday"]) >= 28 && ((int) $this->recur["monthday"]) <= 31 && |
||
| 1133 | gmdate("j", $occenddate) < ((int) $this->recur["monthday"])) { |
||
| 1134 | if (gmdate("j", $occenddate) < 28) { |
||
| 1135 | $occenddate -= gmdate("j", $occenddate) * 24 * 60 * 60; |
||
| 1136 | } |
||
| 1137 | else { |
||
| 1138 | $occenddate += (gmdate("t", $occenddate) - gmdate("j", $occenddate)) * 24 * 60 * 60; |
||
| 1139 | } |
||
| 1140 | } |
||
| 1141 | |||
| 1142 | break; |
||
| 1143 | |||
| 1144 | case rptMonthNth: // on Nth weekday of every M month |
||
| 1145 | $nday = (int) $this->recur["nday"]; // 1 tot 5 |
||
| 1146 | $weekdays = (int) $this->recur["weekdays"]; |
||
| 1147 | |||
| 1148 | while ($forwardcount > 0) { |
||
| 1149 | $occenddate += $this->getMonthInSeconds($curyear, $curmonth); |
||
| 1150 | if ($curmonth >= 12) { |
||
| 1151 | $curmonth = 1; |
||
| 1152 | ++$curyear; |
||
| 1153 | } |
||
| 1154 | else { |
||
| 1155 | ++$curmonth; |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | --$forwardcount; |
||
| 1159 | } |
||
| 1160 | |||
| 1161 | if ($nday == 5) { |
||
| 1162 | // Set date on the last day of the last month |
||
| 1163 | $occenddate += (gmdate("t", $occenddate) - gmdate("j", $occenddate)) * 24 * 60 * 60; |
||
| 1164 | } |
||
| 1165 | else { |
||
| 1166 | // Set date on the first day of the last month |
||
| 1167 | $occenddate -= (gmdate("j", $occenddate) - 1) * 24 * 60 * 60; |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | $dayofweek = gmdate("w", $occenddate); |
||
| 1171 | for ($i = 0; $i < 7; ++$i) { |
||
| 1172 | if ($nday == 5 && (($dayofweek - $i) % 7) >= 0 && (1 << (($dayofweek - $i) % 7)) & $weekdays) { |
||
| 1173 | $occenddate -= $i * 24 * 60 * 60; |
||
| 1174 | break; |
||
| 1175 | } |
||
| 1176 | if ($nday != 5 && (1 << (($dayofweek + $i) % 7)) & $weekdays) { |
||
| 1177 | $occenddate += ($i + (($nday - 1) * 7)) * 24 * 60 * 60; |
||
| 1178 | break; |
||
| 1179 | } |
||
| 1180 | } |
||
| 1181 | |||
| 1182 | break; // case rptMonthNth |
||
| 1183 | } |
||
| 1184 | |||
| 1185 | break; |
||
| 1186 | } |
||
| 1187 | |||
| 1188 | if (defined("PHP_INT_MAX") && $occenddate > PHP_INT_MAX) { |
||
| 1189 | $occenddate = PHP_INT_MAX; |
||
| 1190 | } |
||
| 1191 | |||
| 1192 | $this->recur["end"] = $occenddate; |
||
| 1193 | |||
| 1194 | $rdata .= pack("V", $this->unixDataToRecurData((int) $this->recur["end"])); |
||
| 1195 | break; |
||
| 1196 | |||
| 1197 | // Never ends |
||
| 1198 | case IDC_RCEV_PAT_ERB_NOEND: |
||
| 1199 | default: |
||
| 1200 | $this->recur["end"] = 0x7FFFFFFF; // max date -> 2038 |
||
| 1201 | $rdata .= pack("V", 0x5AE980DF); |
||
| 1202 | break; |
||
| 1203 | } |
||
| 1204 | |||
| 1205 | // UTC date |
||
| 1206 | $utcstart = $this->toGMT($this->tz, (int) $this->recur["start"]); |
||
| 1207 | $utcend = $this->toGMT($this->tz, (int) $this->recur["end"]); |
||
| 1208 | |||
| 1209 | // utc date+time |
||
| 1210 | $utcfirstoccstartdatetime = (isset($this->recur["startocc"])) ? $utcstart + (((int) $this->recur["startocc"]) * 60) : $utcstart; |
||
| 1211 | $utcfirstoccenddatetime = (isset($this->recur["endocc"])) ? $utcstart + (((int) $this->recur["endocc"]) * 60) : $utcstart; |
||
| 1212 | |||
| 1213 | $propsToSet = []; |
||
| 1214 | // update reminder time |
||
| 1215 | $propsToSet[$this->proptags["reminder_time"]] = $utcfirstoccstartdatetime; |
||
| 1216 | |||
| 1217 | // update first occurrence date |
||
| 1218 | $propsToSet[$this->proptags["startdate"]] = $propsToSet[$this->proptags["commonstart"]] = $utcfirstoccstartdatetime; |
||
| 1219 | $propsToSet[$this->proptags["duedate"]] = $propsToSet[$this->proptags["commonend"]] = $utcfirstoccenddatetime; |
||
| 1220 | |||
| 1221 | // Set Outlook properties, if it is an appointment |
||
| 1222 | if (isset($this->messageprops[$this->proptags["message_class"]]) && $this->messageprops[$this->proptags["message_class"]] == "IPM.Appointment") { |
||
| 1223 | // update real begin and real end date |
||
| 1224 | $propsToSet[$this->proptags["startdate_recurring"]] = $utcstart; |
||
| 1225 | $propsToSet[$this->proptags["enddate_recurring"]] = $utcend; |
||
| 1226 | |||
| 1227 | // recurrencetype |
||
| 1228 | // Strange enough is the property recurrencetype, (type-0x9) and not the CDO recurrencetype |
||
| 1229 | $propsToSet[$this->proptags["recurrencetype"]] = ((int) $this->recur["type"]) - 0x9; |
||
| 1230 | |||
| 1231 | // set named prop 'side_effects' to 369, needed for Outlook to ask for single or total recurrence when deleting |
||
| 1232 | $propsToSet[$this->proptags["side_effects"]] = 369; |
||
| 1233 | } |
||
| 1234 | else { |
||
| 1235 | $propsToSet[$this->proptags["side_effects"]] = 3441; |
||
| 1236 | } |
||
| 1237 | |||
| 1238 | // FlagDueBy is datetime of the first reminder occurrence. Outlook gives on this time a reminder popup dialog |
||
| 1239 | // Any change of the recurrence (including changing and deleting exceptions) causes the flagdueby to be reset |
||
| 1240 | // to the 'next' occurrence; this makes sure that deleting the next occurrence will correctly set the reminder to |
||
| 1241 | // the occurrence after that. The 'next' occurrence is defined as being the first occurrence that starts at moment X (server time) |
||
| 1242 | // with the reminder flag set. |
||
| 1243 | $reminderprops = mapi_getprops($this->message, [$this->proptags["reminder_minutes"], $this->proptags["flagdueby"]]); |
||
| 1244 | if (isset($reminderprops[$this->proptags["reminder_minutes"]])) { |
||
| 1245 | $occ = false; |
||
| 1246 | $occurrences = $this->getItems(time(), 0x7FF00000, 3, true); |
||
| 1247 | |||
| 1248 | for ($i = 0, $len = count($occurrences); $i < $len; ++$i) { |
||
| 1249 | // This will actually also give us appointments that have already started, but not yet ended. Since we want the next |
||
| 1250 | // reminder that occurs after time(), we may have to skip the first few entries. We get 3 entries since that is the maximum |
||
| 1251 | // number that would be needed (assuming reminder for item X cannot be before the previous occurrence starts). Worst case: |
||
| 1252 | // time() is currently after start but before end of item, but reminder of next item has already passed (reminder for next item |
||
| 1253 | // can be DURING the previous item, eg daily allday events). In that case, the first and second items must be skipped. |
||
| 1254 | |||
| 1255 | if (($occurrences[$i][$this->proptags["startdate"]] - $reminderprops[$this->proptags["reminder_minutes"]] * 60) > time()) { |
||
| 1256 | $occ = $occurrences[$i]; |
||
| 1257 | break; |
||
| 1258 | } |
||
| 1259 | } |
||
| 1260 | |||
| 1261 | if ($occ) { |
||
| 1262 | if (isset($reminderprops[$this->proptags["flagdueby"]])) { |
||
| 1263 | $propsToSet[$this->proptags["flagdueby"]] = $reminderprops[$this->proptags["flagdueby"]]; |
||
| 1264 | } |
||
| 1265 | else { |
||
| 1266 | $propsToSet[$this->proptags["flagdueby"]] = $occ[$this->proptags["startdate"]] - ($reminderprops[$this->proptags["reminder_minutes"]] * 60); |
||
| 1267 | } |
||
| 1268 | } |
||
| 1269 | else { |
||
| 1270 | // Last reminder passed, no reminders any more. |
||
| 1271 | $propsToSet[$this->proptags["reminder"]] = false; |
||
| 1272 | $propsToSet[$this->proptags["flagdueby"]] = 0x7FF00000; |
||
| 1273 | } |
||
| 1274 | } |
||
| 1275 | |||
| 1276 | // Default data |
||
| 1277 | // Second item (0x08) indicates the Outlook version (see documentation at the bottom of this file for more information) |
||
| 1278 | $rdata .= pack("VV", 0x3006, 0x3008); |
||
| 1279 | if (isset($this->recur["startocc"], $this->recur["endocc"])) { |
||
| 1280 | // Set start and endtime in minutes |
||
| 1281 | $rdata .= pack("VV", (int) $this->recur["startocc"], (int) $this->recur["endocc"]); |
||
| 1282 | } |
||
| 1283 | |||
| 1284 | // Detailed exception data |
||
| 1285 | |||
| 1286 | $changed_items = $this->recur["changed_occurrences"]; |
||
| 1287 | |||
| 1288 | $rdata .= pack("v", count($changed_items)); |
||
| 1289 | |||
| 1290 | foreach ($changed_items as $changed_item) { |
||
| 1291 | // Set start and end time of exception |
||
| 1292 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["start"])); |
||
| 1293 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["end"])); |
||
| 1294 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["basedate"])); |
||
| 1295 | |||
| 1296 | // Bitmask |
||
| 1297 | $bitmask = 0; |
||
| 1298 | |||
| 1299 | // Check for changed strings |
||
| 1300 | if (isset($changed_item["subject"])) { |
||
| 1301 | $bitmask |= 1 << 0; |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | if (isset($changed_item["remind_before"])) { |
||
| 1305 | $bitmask |= 1 << 2; |
||
| 1306 | } |
||
| 1307 | |||
| 1308 | if (isset($changed_item["reminder_set"])) { |
||
| 1309 | $bitmask |= 1 << 3; |
||
| 1310 | } |
||
| 1311 | |||
| 1312 | if (isset($changed_item["location"])) { |
||
| 1313 | $bitmask |= 1 << 4; |
||
| 1314 | } |
||
| 1315 | |||
| 1316 | if (isset($changed_item["busystatus"])) { |
||
| 1317 | $bitmask |= 1 << 5; |
||
| 1318 | } |
||
| 1319 | |||
| 1320 | if (isset($changed_item["alldayevent"])) { |
||
| 1321 | $bitmask |= 1 << 7; |
||
| 1322 | } |
||
| 1323 | |||
| 1324 | if (isset($changed_item["label"])) { |
||
| 1325 | $bitmask |= 1 << 8; |
||
| 1326 | } |
||
| 1327 | |||
| 1328 | $rdata .= pack("v", $bitmask); |
||
| 1329 | |||
| 1330 | // Set "subject" |
||
| 1331 | if (isset($changed_item["subject"])) { |
||
| 1332 | // convert utf-8 to non-unicode blob string (us-ascii?) |
||
| 1333 | $subject = iconv("UTF-8", "windows-1252//TRANSLIT", $changed_item["subject"]); |
||
| 1334 | $length = strlen($subject); |
||
| 1335 | $rdata .= pack("vv", $length + 1, $length); |
||
| 1336 | $rdata .= pack("a" . $length, $subject); |
||
| 1337 | } |
||
| 1338 | |||
| 1339 | if (isset($changed_item["remind_before"])) { |
||
| 1340 | $rdata .= pack("V", $changed_item["remind_before"]); |
||
| 1341 | } |
||
| 1342 | |||
| 1343 | if (isset($changed_item["reminder_set"])) { |
||
| 1344 | $rdata .= pack("V", $changed_item["reminder_set"]); |
||
| 1345 | } |
||
| 1346 | |||
| 1347 | if (isset($changed_item["location"])) { |
||
| 1348 | $location = iconv("UTF-8", "windows-1252//TRANSLIT", $changed_item["location"]); |
||
| 1349 | $length = strlen($location); |
||
| 1350 | $rdata .= pack("vv", $length + 1, $length); |
||
| 1351 | $rdata .= pack("a" . $length, $location); |
||
| 1352 | } |
||
| 1353 | |||
| 1354 | if (isset($changed_item["busystatus"])) { |
||
| 1355 | $rdata .= pack("V", $changed_item["busystatus"]); |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | if (isset($changed_item["alldayevent"])) { |
||
| 1359 | $rdata .= pack("V", $changed_item["alldayevent"]); |
||
| 1360 | } |
||
| 1361 | |||
| 1362 | if (isset($changed_item["label"])) { |
||
| 1363 | $rdata .= pack("V", $changed_item["label"]); |
||
| 1364 | } |
||
| 1365 | } |
||
| 1366 | |||
| 1367 | $rdata .= pack("V", 0); |
||
| 1368 | |||
| 1369 | // write extended data |
||
| 1370 | foreach ($changed_items as $changed_item) { |
||
| 1371 | $rdata .= pack("V", 0); |
||
| 1372 | if (isset($changed_item["subject"]) || isset($changed_item["location"])) { |
||
| 1373 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["start"])); |
||
| 1374 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["end"])); |
||
| 1375 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["basedate"])); |
||
| 1376 | } |
||
| 1377 | |||
| 1378 | if (isset($changed_item["subject"])) { |
||
| 1379 | $subject = iconv("UTF-8", "UCS-2LE", $changed_item["subject"]); |
||
| 1380 | $length = iconv_strlen($subject, "UCS-2LE"); |
||
| 1381 | $rdata .= pack("v", $length); |
||
| 1382 | $rdata .= pack("a" . $length * 2, $subject); |
||
| 1383 | } |
||
| 1384 | |||
| 1385 | if (isset($changed_item["location"])) { |
||
| 1386 | $location = iconv("UTF-8", "UCS-2LE", $changed_item["location"]); |
||
| 1387 | $length = iconv_strlen($location, "UCS-2LE"); |
||
| 1388 | $rdata .= pack("v", $length); |
||
| 1389 | $rdata .= pack("a" . $length * 2, $location); |
||
| 1390 | } |
||
| 1391 | |||
| 1392 | if (isset($changed_item["subject"]) || isset($changed_item["location"])) { |
||
| 1393 | $rdata .= pack("V", 0); |
||
| 1394 | } |
||
| 1395 | } |
||
| 1396 | |||
| 1397 | $rdata .= pack("V", 0); |
||
| 1398 | |||
| 1399 | // Set props |
||
| 1400 | $propsToSet[$this->proptags["recurring_data"]] = $rdata; |
||
| 1401 | $propsToSet[$this->proptags["recurring"]] = true; |
||
| 1402 | if (isset($this->tz) && $this->tz) { |
||
| 1403 | $timezone = "GMT"; |
||
| 1404 | if ($this->tz["timezone"] != 0) { |
||
| 1405 | // Create user readable timezone information |
||
| 1406 | $timezone = sprintf( |
||
| 1407 | "(GMT %s%02d:%02d)",- |
||
| 1408 | $this->tz["timezone"] > 0 ? "+" : "-", |
||
| 1409 | abs($this->tz["timezone"] / 60), |
||
| 1410 | abs($this->tz["timezone"] % 60) |
||
| 1411 | ); |
||
| 1412 | } |
||
| 1413 | $propsToSet[$this->proptags["timezone_data"]] = $this->getTimezoneData($this->tz); |
||
| 1414 | $propsToSet[$this->proptags["timezone"]] = $timezone; |
||
| 1415 | } |
||
| 1416 | mapi_setprops($this->message, $propsToSet); |
||
| 1417 | } |
||
| 1418 | |||
| 1419 | /** |
||
| 1420 | * Function which converts a recurrence date timestamp to an unix date timestamp. |
||
| 1421 | * |
||
| 1422 | * @author Steve Hardy |
||
| 1423 | * |
||
| 1424 | * @param int $rdate the date which will be converted |
||
| 1425 | * |
||
| 1426 | * @return int the converted date |
||
| 1427 | */ |
||
| 1428 | public function recurDataToUnixData($rdate) { |
||
| 1429 | return ($rdate - 194074560) * 60; |
||
| 1430 | } |
||
| 1431 | |||
| 1432 | /** |
||
| 1433 | * Function which converts an unix date timestamp to recurrence date timestamp. |
||
| 1434 | * |
||
| 1435 | * @author Johnny Biemans |
||
| 1436 | * |
||
| 1437 | * @param int $date the date which will be converted |
||
| 1438 | * |
||
| 1439 | * @return float|int the converted date in minutes |
||
| 1440 | */ |
||
| 1441 | public function unixDataToRecurData($date) { |
||
| 1442 | return ($date / 60) + 194074560; |
||
| 1443 | } |
||
| 1444 | |||
| 1445 | /** |
||
| 1446 | * gmtime() doesn't exist in standard PHP, so we have to implement it ourselves. |
||
| 1447 | * |
||
| 1448 | * @author Steve Hardy |
||
| 1449 | * |
||
| 1450 | * @param mixed $ts |
||
| 1451 | * |
||
| 1452 | * @return float|int |
||
| 1453 | */ |
||
| 1454 | public function GetTZOffset($ts) { |
||
| 1455 | $Offset = date("O", $ts); |
||
| 1456 | |||
| 1457 | $Parity = $Offset < 0 ? -1 : 1; |
||
| 1458 | $Offset = $Parity * $Offset; |
||
| 1459 | $Offset = ($Offset - ($Offset % 100)) / 100 * 60 + $Offset % 100; |
||
| 1460 | |||
| 1461 | return $Parity * $Offset; |
||
| 1462 | } |
||
| 1463 | |||
| 1464 | /** |
||
| 1465 | * gmtime() doesn't exist in standard PHP, so we have to implement it ourselves. |
||
| 1466 | * |
||
| 1467 | * @author Steve Hardy |
||
| 1468 | * |
||
| 1469 | * @param int $time |
||
| 1470 | * |
||
| 1471 | * @return array GMT Time |
||
| 1472 | */ |
||
| 1473 | public function gmtime($time) { |
||
| 1474 | $TZOffset = $this->GetTZOffset($time); |
||
| 1475 | |||
| 1476 | $t_time = $time - $TZOffset * 60; # Counter adjust for localtime() |
||
| 1477 | |||
| 1478 | return localtime($t_time, 1); |
||
| 1479 | } |
||
| 1480 | |||
| 1481 | /** |
||
| 1482 | * @param float|string $year |
||
| 1483 | */ |
||
| 1484 | public function isLeapYear($year): bool { |
||
| 1485 | return $year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0); |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | /** |
||
| 1489 | * @param float|string $year |
||
| 1490 | * @param int|string $month |
||
| 1491 | */ |
||
| 1492 | public function getMonthInSeconds($year, $month): int { |
||
| 1493 | if (in_array($month, [1, 3, 5, 7, 8, 10, 12])) { |
||
| 1494 | $day = 31; |
||
| 1495 | } |
||
| 1496 | elseif (in_array($month, [4, 6, 9, 11])) { |
||
| 1497 | $day = 30; |
||
| 1498 | } |
||
| 1499 | else { |
||
| 1500 | $day = 28; |
||
| 1501 | if ($this->isLeapYear($year) == 1) { |
||
| 1502 | ++$day; |
||
| 1503 | } |
||
| 1504 | } |
||
| 1505 | |||
| 1506 | return $day * 24 * 60 * 60; |
||
| 1507 | } |
||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Function to get a date by Year Nr, Month Nr, Week Nr, Day Nr, and hour. |
||
| 1511 | * |
||
| 1512 | * @param int $year |
||
| 1513 | * @param int $month |
||
| 1514 | * @param int $week |
||
| 1515 | * @param int $day |
||
| 1516 | * @param int $hour |
||
| 1517 | * |
||
| 1518 | * @return int the timestamp of the given date, timezone-independent |
||
| 1519 | */ |
||
| 1520 | public function getDateByYearMonthWeekDayHour($year, $month, $week, $day, $hour) { |
||
| 1521 | // get first day of month |
||
| 1522 | $date = gmmktime(0, 0, 0, $month, 0, $year + 1900); |
||
| 1523 | |||
| 1524 | // get wday info |
||
| 1525 | $gmdate = $this->gmtime($date); |
||
| 1526 | |||
| 1527 | $date -= $gmdate["tm_wday"] * 24 * 60 * 60; // back up to start of week |
||
| 1528 | |||
| 1529 | $date += $week * 7 * 24 * 60 * 60; // go to correct week nr |
||
| 1530 | $date += $day * 24 * 60 * 60; |
||
| 1531 | $date += $hour * 60 * 60; |
||
| 1532 | |||
| 1533 | $gmdate = $this->gmtime($date); |
||
| 1534 | |||
| 1535 | // if we are in the next month, then back up a week, because week '5' means |
||
| 1536 | // 'last week of month' |
||
| 1537 | |||
| 1538 | if ($month != $gmdate["tm_mon"] + 1) { |
||
| 1539 | $date -= 7 * 24 * 60 * 60; |
||
| 1540 | } |
||
| 1541 | |||
| 1542 | return $date; |
||
| 1543 | } |
||
| 1544 | |||
| 1545 | /** |
||
| 1546 | * getTimezone gives the timezone offset (in minutes) of the given |
||
| 1547 | * local date/time according to the given TZ info. |
||
| 1548 | * |
||
| 1549 | * @param mixed $tz |
||
| 1550 | * @param mixed $date |
||
| 1551 | */ |
||
| 1552 | public function getTimezone($tz, $date) { |
||
| 1553 | // No timezone -> GMT (+0) |
||
| 1554 | if (!isset($tz["timezone"])) { |
||
| 1555 | return 0; |
||
| 1556 | } |
||
| 1557 | |||
| 1558 | $dst = false; |
||
| 1559 | $gmdate = $this->gmtime($date); |
||
| 1560 | |||
| 1561 | $dststart = $this->getDateByYearMonthWeekDayHour($gmdate["tm_year"], $tz["dststartmonth"], $tz["dststartweek"], 0, $tz["dststarthour"]); |
||
| 1562 | $dstend = $this->getDateByYearMonthWeekDayHour($gmdate["tm_year"], $tz["dstendmonth"], $tz["dstendweek"], 0, $tz["dstendhour"]); |
||
| 1563 | |||
| 1564 | if ($dststart <= $dstend) { |
||
| 1565 | // Northern hemisphere, eg DST is during Mar-Oct |
||
| 1566 | if ($date > $dststart && $date < $dstend) { |
||
| 1567 | $dst = true; |
||
| 1568 | } |
||
| 1569 | } |
||
| 1570 | else { |
||
| 1571 | // Southern hemisphere, eg DST is during Oct-Mar |
||
| 1572 | if ($date < $dstend || $date > $dststart) { |
||
| 1573 | $dst = true; |
||
| 1574 | } |
||
| 1575 | } |
||
| 1576 | |||
| 1577 | if ($dst) { |
||
| 1578 | return $tz["timezone"] + $tz["timezonedst"]; |
||
| 1579 | } |
||
| 1580 | |||
| 1581 | return $tz["timezone"]; |
||
| 1582 | } |
||
| 1583 | |||
| 1584 | /** |
||
| 1585 | * parseTimezone parses the timezone as specified in named property 0x8233 |
||
| 1586 | * in Outlook calendar messages. Returns the timezone in minutes negative |
||
| 1587 | * offset (GMT +2:00 -> -120). |
||
| 1588 | * |
||
| 1589 | * @param mixed $data |
||
| 1590 | * |
||
| 1591 | * @return null|array|false |
||
| 1592 | */ |
||
| 1593 | public function parseTimezone($data) { |
||
| 1594 | if (strlen($data) < 48) { |
||
| 1595 | return; |
||
| 1596 | } |
||
| 1597 | |||
| 1598 | return unpack("ltimezone/lunk/ltimezonedst/lunk/ldstendmonth/vdstendweek/vdstendhour/lunk/lunk/vunk/ldststartmonth/vdststartweek/vdststarthour/lunk/vunk", $data); |
||
| 1599 | } |
||
| 1600 | |||
| 1601 | /** |
||
| 1602 | * @param mixed $tz |
||
| 1603 | * |
||
| 1604 | * @return false|string |
||
| 1605 | */ |
||
| 1606 | public function getTimezoneData($tz) { |
||
| 1607 | return pack("lllllvvllvlvvlv", $tz["timezone"], 0, $tz["timezonedst"], 0, $tz["dstendmonth"], $tz["dstendweek"], $tz["dstendhour"], 0, 0, 0, $tz["dststartmonth"], $tz["dststartweek"], $tz["dststarthour"], 0, 0); |
||
| 1608 | } |
||
| 1609 | |||
| 1610 | /** |
||
| 1611 | * toGMT returns a timestamp in GMT time for the time and timezone given. |
||
| 1612 | * |
||
| 1613 | * @param mixed $tz |
||
| 1614 | * @param mixed $date |
||
| 1615 | */ |
||
| 1616 | public function toGMT($tz, $date) { |
||
| 1617 | if (!isset($tz['timezone'])) { |
||
| 1618 | return $date; |
||
| 1619 | } |
||
| 1620 | $offset = $this->getTimezone($tz, $date); |
||
| 1621 | |||
| 1622 | return $date + $offset * 60; |
||
| 1623 | } |
||
| 1624 | |||
| 1625 | /** |
||
| 1626 | * fromGMT returns a timestamp in the local timezone given from the GMT time given. |
||
| 1627 | * |
||
| 1628 | * @param mixed $tz |
||
| 1629 | * @param mixed $date |
||
| 1630 | */ |
||
| 1631 | public function fromGMT($tz, $date) { |
||
| 1632 | $offset = $this->getTimezone($tz, $date); |
||
| 1633 | |||
| 1634 | return $date - $offset * 60; |
||
| 1635 | } |
||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * Function to get timestamp of the beginning of the day of the timestamp given. |
||
| 1639 | * |
||
| 1640 | * @param mixed $date |
||
| 1641 | * |
||
| 1642 | * @return false|int timestamp referring to same day but at 00:00:00 |
||
| 1643 | */ |
||
| 1644 | public function dayStartOf($date) { |
||
| 1645 | $time1 = $this->gmtime($date); |
||
| 1646 | |||
| 1647 | return gmmktime(0, 0, 0, $time1["tm_mon"] + 1, $time1["tm_mday"], $time1["tm_year"] + 1900); |
||
| 1648 | } |
||
| 1649 | |||
| 1650 | /** |
||
| 1651 | * Function to get timestamp of the beginning of the month of the timestamp given. |
||
| 1652 | * |
||
| 1653 | * @param mixed $date |
||
| 1654 | * |
||
| 1655 | * @return false|int Timestamp referring to same month but on the first day, and at 00:00:00 |
||
| 1656 | */ |
||
| 1657 | public function monthStartOf($date) { |
||
| 1658 | $time1 = $this->gmtime($date); |
||
| 1659 | |||
| 1660 | return gmmktime(0, 0, 0, $time1["tm_mon"] + 1, 1, $time1["tm_year"] + 1900); |
||
| 1661 | } |
||
| 1662 | |||
| 1663 | /** |
||
| 1664 | * Function to get timestamp of the beginning of the year of the timestamp given. |
||
| 1665 | * |
||
| 1666 | * @param mixed $date |
||
| 1667 | * |
||
| 1668 | * @return false|int Timestamp referring to the same year but on Jan 01, at 00:00:00 |
||
| 1669 | */ |
||
| 1670 | public function yearStartOf($date) { |
||
| 1671 | $time1 = $this->gmtime($date); |
||
| 1672 | |||
| 1673 | return gmmktime(0, 0, 0, 1, 1, $time1["tm_year"] + 1900); |
||
| 1674 | } |
||
| 1675 | |||
| 1676 | /** |
||
| 1677 | * Function which returns the items in a given interval. This included expansion of the recurrence and |
||
| 1678 | * processing of exceptions (modified and deleted). |
||
| 1679 | * |
||
| 1680 | * @param int $start start time of the interval (GMT) |
||
| 1681 | * @param int $end end time of the interval (GMT) |
||
| 1682 | * @param mixed $limit |
||
| 1683 | * @param mixed $remindersonly |
||
| 1684 | * |
||
| 1685 | * @return (array|mixed)[] |
||
| 1686 | * |
||
| 1687 | * @psalm-return array<int, T|array> |
||
| 1688 | */ |
||
| 1689 | public function getItems($start, $end, $limit = 0, $remindersonly = false): array { |
||
| 1690 | $items = []; |
||
| 1691 | |||
| 1692 | if (isset($this->recur)) { |
||
| 1693 | // Optimization: remindersonly and default reminder is off; since only exceptions with reminder set will match, just look which |
||
| 1694 | // exceptions are in range and have a reminder set |
||
| 1695 | if ($remindersonly && (!isset($this->messageprops[$this->proptags["reminder"]]) || $this->messageprops[$this->proptags["reminder"]] == false)) { |
||
| 1696 | // Sort exceptions by start time |
||
| 1697 | uasort($this->recur["changed_occurrences"], [$this, "sortExceptionStart"]); |
||
| 1698 | |||
| 1699 | // Loop through all changed exceptions |
||
| 1700 | foreach ($this->recur["changed_occurrences"] as $exception) { |
||
| 1701 | // Check reminder set |
||
| 1702 | if (!isset($exception["reminder"]) || $exception["reminder"] == false) { |
||
| 1703 | continue; |
||
| 1704 | } |
||
| 1705 | |||
| 1706 | // Convert to GMT |
||
| 1707 | $occstart = $this->toGMT($this->tz, $exception["start"]); |
||
| 1708 | $occend = $this->toGMT($this->tz, $exception["end"]); |
||
| 1709 | |||
| 1710 | // Check range criterium |
||
| 1711 | if ($occstart > $end || $occend < $start) { |
||
| 1712 | continue; |
||
| 1713 | } |
||
| 1714 | |||
| 1715 | // OK, add to items. |
||
| 1716 | array_push($items, $this->getExceptionProperties($exception)); |
||
| 1717 | if ($limit && (count($items) == $limit)) { |
||
| 1718 | break; |
||
| 1719 | } |
||
| 1720 | } |
||
| 1721 | |||
| 1722 | uasort($items, [$this, "sortStarttime"]); |
||
| 1723 | |||
| 1724 | return $items; |
||
| 1725 | } |
||
| 1726 | |||
| 1727 | // From here on, the dates of the occurrences are calculated in local time, so the days we're looking |
||
| 1728 | // at are calculated from the local time dates of $start and $end |
||
| 1729 | |||
| 1730 | if (isset($this->recur['regen'], $this->action['datecompleted']) && $this->recur['regen']) { |
||
| 1731 | $daystart = $this->dayStartOf($this->action['datecompleted']); |
||
| 1732 | } |
||
| 1733 | else { |
||
| 1734 | $daystart = $this->dayStartOf($this->recur["start"]); // start on first day of occurrence |
||
| 1735 | } |
||
| 1736 | |||
| 1737 | // Calculate the last day on which we want to be looking at a recurrence; this is either the end of the view |
||
| 1738 | // or the end of the recurrence, whichever comes first |
||
| 1739 | if ($end > $this->toGMT($this->tz, $this->recur["end"])) { |
||
| 1740 | $rangeend = $this->toGMT($this->tz, $this->recur["end"]); |
||
| 1741 | } |
||
| 1742 | else { |
||
| 1743 | $rangeend = $end; |
||
| 1744 | } |
||
| 1745 | |||
| 1746 | $dayend = $this->dayStartOf($this->fromGMT($this->tz, $rangeend)); |
||
| 1747 | |||
| 1748 | // Loop through the entire recurrence range of dates, and check for each occurrence whether it is in the view range. |
||
| 1749 | $recurType = (int) $this->recur["type"] < 0x2000 ? (int) $this->recur["type"] + 0x2000 : (int) $this->recur["type"]; |
||
| 1750 | |||
| 1751 | switch ($recurType) { |
||
| 1752 | case IDC_RCEV_PAT_ORB_DAILY: |
||
| 1753 | if ($this->recur["everyn"] <= 0) { |
||
| 1754 | $this->recur["everyn"] = 1440; |
||
| 1755 | } |
||
| 1756 | |||
| 1757 | if ($this->recur["subtype"] == rptDay) { |
||
| 1758 | // Every Nth day |
||
| 1759 | for ($now = $daystart; $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += 60 * $this->recur["everyn"]) { |
||
| 1760 | $this->processOccurrenceItem($items, $start, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1761 | } |
||
| 1762 | } |
||
| 1763 | else { |
||
| 1764 | // Every workday |
||
| 1765 | for ($now = $daystart; $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += 60 * 1440) { |
||
| 1766 | $nowtime = $this->gmtime($now); |
||
| 1767 | if ($nowtime["tm_wday"] > 0 && $nowtime["tm_wday"] < 6) { // only add items in the given timespace |
||
| 1768 | $this->processOccurrenceItem($items, $start, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1769 | } |
||
| 1770 | } |
||
| 1771 | } |
||
| 1772 | break; |
||
| 1773 | |||
| 1774 | case IDC_RCEV_PAT_ORB_WEEKLY: |
||
| 1775 | if ($this->recur["everyn"] <= 0) { |
||
| 1776 | $this->recur["everyn"] = 1; |
||
| 1777 | } |
||
| 1778 | |||
| 1779 | // If sliding flag is set then move to 'n' weeks |
||
| 1780 | if ($this->recur['regen']) { |
||
| 1781 | $daystart += (60 * 60 * 24 * 7 * $this->recur["everyn"]); |
||
| 1782 | } |
||
| 1783 | |||
| 1784 | for ($now = $daystart; $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += (60 * 60 * 24 * 7 * $this->recur["everyn"])) { |
||
| 1785 | if ($this->recur['regen']) { |
||
| 1786 | $this->processOccurrenceItem($items, $start, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1787 | } |
||
| 1788 | else { |
||
| 1789 | // Loop through the whole following week to the first occurrence of the week, add each day that is specified |
||
| 1790 | for ($wday = 0; $wday < 7; ++$wday) { |
||
| 1791 | $daynow = $now + $wday * 60 * 60 * 24; |
||
| 1792 | // checks weather the next coming day in recurring pattern is less than or equal to end day of the recurring item |
||
| 1793 | if ($daynow <= $dayend) { |
||
| 1794 | $nowtime = $this->gmtime($daynow); // Get the weekday of the current day |
||
| 1795 | if ($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) { // Selected ? |
||
| 1796 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1797 | } |
||
| 1798 | } |
||
| 1799 | } |
||
| 1800 | } |
||
| 1801 | } |
||
| 1802 | break; |
||
| 1803 | |||
| 1804 | case IDC_RCEV_PAT_ORB_MONTHLY: |
||
| 1805 | if ($this->recur["everyn"] <= 0) { |
||
| 1806 | $this->recur["everyn"] = 1; |
||
| 1807 | } |
||
| 1808 | |||
| 1809 | // Loop through all months from start to end of occurrence, starting at beginning of first month |
||
| 1810 | for ($now = $this->monthStartOf($daystart); $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += $this->daysInMonth($now, $this->recur["everyn"]) * 24 * 60 * 60) { |
||
| 1811 | if (isset($this->recur["monthday"]) && ($this->recur['monthday'] != "undefined") && !$this->recur['regen']) { // Day M of every N months |
||
| 1812 | $difference = 1; |
||
| 1813 | if ($this->daysInMonth($now, $this->recur["everyn"]) < $this->recur["monthday"]) { |
||
| 1814 | $difference = $this->recur["monthday"] - $this->daysInMonth($now, $this->recur["everyn"]) + 1; |
||
| 1815 | } |
||
| 1816 | $daynow = $now + (($this->recur["monthday"] - $difference) * 24 * 60 * 60); |
||
| 1817 | // checks weather the next coming day in recurrence pattern is less than or equal to end day of the recurring item |
||
| 1818 | if ($daynow <= $dayend) { |
||
| 1819 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1820 | } |
||
| 1821 | } |
||
| 1822 | elseif (isset($this->recur["nday"], $this->recur["weekdays"])) { // Nth [weekday] of every N months |
||
| 1823 | // Sanitize input |
||
| 1824 | if ($this->recur["weekdays"] == 0) { |
||
| 1825 | $this->recur["weekdays"] = 1; |
||
| 1826 | } |
||
| 1827 | |||
| 1828 | // If nday is not set to the last day in the month |
||
| 1829 | if ($this->recur["nday"] < 5) { |
||
| 1830 | // keep the track of no. of time correct selection pattern (like 2nd weekday, 4th friday, etc.) is matched |
||
| 1831 | $ndaycounter = 0; |
||
| 1832 | // Find matching weekday in this month |
||
| 1833 | for ($day = 0, $total = $this->daysInMonth($now, 1); $day < $total; ++$day) { |
||
| 1834 | $daynow = $now + $day * 60 * 60 * 24; |
||
| 1835 | $nowtime = $this->gmtime($daynow); // Get the weekday of the current day |
||
| 1836 | |||
| 1837 | if ($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) { // Selected ? |
||
| 1838 | ++$ndaycounter; |
||
| 1839 | } |
||
| 1840 | // check the selected pattern is same as asked Nth weekday,If so set the firstday |
||
| 1841 | if ($this->recur["nday"] == $ndaycounter) { |
||
| 1842 | $firstday = $day; |
||
| 1843 | break; |
||
| 1844 | } |
||
| 1845 | } |
||
| 1846 | // $firstday is the day of the month on which the asked pattern of nth weekday matches |
||
| 1847 | $daynow = $now + $firstday * 60 * 60 * 24; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 1848 | } |
||
| 1849 | else { |
||
| 1850 | // Find last day in the month ($now is the firstday of the month) |
||
| 1851 | $NumDaysInMonth = $this->daysInMonth($now, 1); |
||
| 1852 | $daynow = $now + (($NumDaysInMonth - 1) * 24 * 60 * 60); |
||
| 1853 | |||
| 1854 | $nowtime = $this->gmtime($daynow); |
||
| 1855 | while (($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) == 0) { |
||
| 1856 | $daynow -= 86400; |
||
| 1857 | $nowtime = $this->gmtime($daynow); |
||
| 1858 | } |
||
| 1859 | } |
||
| 1860 | |||
| 1861 | /* |
||
| 1862 | * checks weather the next coming day in recurrence pattern is less than or equal to end day of the * recurring item.Also check weather the coming day in recurrence pattern is greater than or equal to start * of recurring pattern, so that appointment that fall under the recurrence range are only displayed. |
||
| 1863 | */ |
||
| 1864 | if ($daynow <= $dayend && $daynow >= $daystart) { |
||
| 1865 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1866 | } |
||
| 1867 | } |
||
| 1868 | elseif ($this->recur['regen']) { |
||
| 1869 | $next_month_start = $now + ($this->daysInMonth($now, 1) * 24 * 60 * 60); |
||
| 1870 | $now = $daystart + ($this->daysInMonth($next_month_start, $this->recur['everyn']) * 24 * 60 * 60); |
||
| 1871 | |||
| 1872 | if ($now <= $dayend) { |
||
| 1873 | $this->processOccurrenceItem($items, $daystart, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1874 | } |
||
| 1875 | } |
||
| 1876 | } |
||
| 1877 | break; |
||
| 1878 | |||
| 1879 | case IDC_RCEV_PAT_ORB_YEARLY: |
||
| 1880 | if ($this->recur["everyn"] <= 0) { |
||
| 1881 | $this->recur["everyn"] = 12; |
||
| 1882 | } |
||
| 1883 | |||
| 1884 | for ($now = $this->yearStartOf($daystart); $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += $this->daysInMonth($now, $this->recur["everyn"]) * 24 * 60 * 60) { |
||
| 1885 | if (isset($this->recur["monthday"]) && !$this->recur['regen']) { // same as monthly, but in a specific month |
||
| 1886 | // recur["month"] is in minutes since the beginning of the year |
||
| 1887 | $month = $this->monthOfYear($this->recur["month"]); // $month is now month of year [0..11] |
||
| 1888 | $monthday = $this->recur["monthday"]; // $monthday is day of the month [1..31] |
||
| 1889 | $monthstart = $now + $this->daysInMonth($now, $month) * 24 * 60 * 60; // $monthstart is the timestamp of the beginning of the month |
||
| 1890 | if ($monthday > $this->daysInMonth($monthstart, 1)) { |
||
| 1891 | $monthday = $this->daysInMonth($monthstart, 1); |
||
| 1892 | } // Cap $monthday on month length (eg 28 feb instead of 29 feb) |
||
| 1893 | $daynow = $monthstart + ($monthday - 1) * 24 * 60 * 60; |
||
| 1894 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1895 | } |
||
| 1896 | elseif (isset($this->recur["nday"], $this->recur["weekdays"])) { // Nth [weekday] in month X of every N years |
||
| 1897 | // Go the correct month |
||
| 1898 | $monthnow = $now + $this->daysInMonth($now, $this->monthOfYear($this->recur["month"])) * 24 * 60 * 60; |
||
| 1899 | |||
| 1900 | // Find first matching weekday in this month |
||
| 1901 | for ($wday = 0; $wday < 7; ++$wday) { |
||
| 1902 | $daynow = $monthnow + $wday * 60 * 60 * 24; |
||
| 1903 | $nowtime = $this->gmtime($daynow); // Get the weekday of the current day |
||
| 1904 | |||
| 1905 | if ($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) { // Selected ? |
||
| 1906 | $firstday = $wday; |
||
| 1907 | break; |
||
| 1908 | } |
||
| 1909 | } |
||
| 1910 | |||
| 1911 | // Same as above (monthly) |
||
| 1912 | $daynow = $monthnow + ($firstday + ($this->recur["nday"] - 1) * 7) * 60 * 60 * 24; |
||
| 1913 | |||
| 1914 | while ($this->monthStartOf($daynow) != $this->monthStartOf($monthnow)) { |
||
| 1915 | $daynow -= 7 * 60 * 60 * 24; |
||
| 1916 | } |
||
| 1917 | |||
| 1918 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1919 | } |
||
| 1920 | elseif ($this->recur['regen']) { |
||
| 1921 | $year_starttime = $this->gmtime($now); |
||
| 1922 | $is_next_leapyear = $this->isLeapYear($year_starttime['tm_year'] + 1900 + 1); // +1 next year |
||
| 1923 | $now = $daystart + ($is_next_leapyear ? 31622400 /* Leap year in seconds */ : 31536000 /* year in seconds */); |
||
| 1924 | |||
| 1925 | if ($now <= $dayend) { |
||
| 1926 | $this->processOccurrenceItem($items, $daystart, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1927 | } |
||
| 1928 | } |
||
| 1929 | } |
||
| 1930 | } |
||
| 1931 | // to get all exception items |
||
| 1932 | if (!empty($this->recur['changed_occurrences'])) { |
||
| 1933 | $this->processExceptionItems($items, $start, $end); |
||
| 1934 | } |
||
| 1935 | } |
||
| 1936 | |||
| 1937 | // sort items on starttime |
||
| 1938 | usort($items, [$this, "sortStarttime"]); |
||
| 1939 | |||
| 1940 | // Return the MAPI-compatible list of items for this object |
||
| 1941 | return $items; |
||
| 1942 | } |
||
| 1943 | |||
| 1944 | /** |
||
| 1945 | * @psalm-return -1|0|1 |
||
| 1946 | * |
||
| 1947 | * @param mixed $a |
||
| 1948 | * @param mixed $b |
||
| 1949 | */ |
||
| 1950 | public function sortStarttime($a, $b): int { |
||
| 1951 | $aTime = $a[$this->proptags["startdate"]]; |
||
| 1952 | $bTime = $b[$this->proptags["startdate"]]; |
||
| 1953 | |||
| 1954 | return $aTime == $bTime ? 0 : ($aTime > $bTime ? 1 : -1); |
||
| 1955 | } |
||
| 1956 | |||
| 1957 | /** |
||
| 1958 | * daysInMonth. |
||
| 1959 | * |
||
| 1960 | * Returns the number of days in the upcoming number of months. If you specify 1 month as |
||
| 1961 | * $months it will give you the number of days in the month of $date. If you specify more it |
||
| 1962 | * will also count the days in the upcoming months and add that to the number of days. So |
||
| 1963 | * if you have a date in march and you specify $months as 2 it will return 61. |
||
| 1964 | * |
||
| 1965 | * @param int $date specified date as timestamp from which you want to know the number |
||
| 1966 | * of days in the month |
||
| 1967 | * @param int $months number of months you want to know the number of days in |
||
| 1968 | * |
||
| 1969 | * @return float|int number of days in the specified amount of months |
||
| 1970 | */ |
||
| 1971 | public function daysInMonth($date, $months) { |
||
| 1972 | $days = 0; |
||
| 1973 | |||
| 1974 | for ($i = 0; $i < $months; ++$i) { |
||
| 1975 | $days += date("t", $date + $days * 24 * 60 * 60); |
||
| 1976 | } |
||
| 1977 | |||
| 1978 | return $days; |
||
| 1979 | } |
||
| 1980 | |||
| 1981 | // Converts MAPI-style 'minutes' into the month of the year [0..11] |
||
| 1982 | public function monthOfYear($minutes) { |
||
| 1983 | $d = gmmktime(0, 0, 0, 1, 1, 2001); // The year 2001 was a non-leap year, and the minutes provided are always in non-leap-year-minutes |
||
| 1984 | |||
| 1985 | $d += $minutes * 60; |
||
| 1986 | |||
| 1987 | $dtime = $this->gmtime($d); |
||
| 1988 | |||
| 1989 | return $dtime["tm_mon"]; |
||
| 1990 | } |
||
| 1991 | |||
| 1992 | /** |
||
| 1993 | * @psalm-return -1|0|1 |
||
| 1994 | * |
||
| 1995 | * @param mixed $a |
||
| 1996 | * @param mixed $b |
||
| 1997 | */ |
||
| 1998 | public function sortExceptionStart($a, $b): int { |
||
| 1999 | return $a["start"] == $b["start"] ? 0 : ($a["start"] > $b["start"] ? 1 : -1); |
||
| 2000 | } |
||
| 2001 | |||
| 2002 | /** |
||
| 2003 | * Function to get all properties of a single changed exception. |
||
| 2004 | * |
||
| 2005 | * @param mixed $exception |
||
| 2006 | * |
||
| 2007 | * @return (mixed|true)[] associative array of properties for the exception |
||
| 2008 | * |
||
| 2009 | * @psalm-return array<mixed|true> |
||
| 2010 | */ |
||
| 2011 | public function getExceptionProperties($exception): array { |
||
| 2012 | // Exception has same properties as main object, with some properties overridden: |
||
| 2013 | $item = $this->messageprops; |
||
| 2014 | |||
| 2015 | // Special properties |
||
| 2016 | $item["exception"] = true; |
||
| 2017 | $item["basedate"] = $exception["basedate"]; // note that the basedate is always in local time ! |
||
| 2018 | |||
| 2019 | // MAPI-compatible properties (you can handle an exception as a normal calendar item like this) |
||
| 2020 | $item[$this->proptags["startdate"]] = $this->toGMT($this->tz, $exception["start"]); |
||
| 2021 | $item[$this->proptags["duedate"]] = $this->toGMT($this->tz, $exception["end"]); |
||
| 2022 | $item[$this->proptags["commonstart"]] = $item[$this->proptags["startdate"]]; |
||
| 2023 | $item[$this->proptags["commonend"]] = $item[$this->proptags["duedate"]]; |
||
| 2024 | |||
| 2025 | if (isset($exception["subject"])) { |
||
| 2026 | $item[$this->proptags["subject"]] = $exception["subject"]; |
||
| 2027 | } |
||
| 2028 | |||
| 2029 | if (isset($exception["label"])) { |
||
| 2030 | $item[$this->proptags["label"]] = $exception["label"]; |
||
| 2031 | } |
||
| 2032 | |||
| 2033 | if (isset($exception["alldayevent"])) { |
||
| 2034 | $item[$this->proptags["alldayevent"]] = $exception["alldayevent"]; |
||
| 2035 | } |
||
| 2036 | |||
| 2037 | if (isset($exception["location"])) { |
||
| 2038 | $item[$this->proptags["location"]] = $exception["location"]; |
||
| 2039 | } |
||
| 2040 | |||
| 2041 | if (isset($exception["remind_before"])) { |
||
| 2042 | $item[$this->proptags["reminder_minutes"]] = $exception["remind_before"]; |
||
| 2043 | } |
||
| 2044 | |||
| 2045 | if (isset($exception["reminder_set"])) { |
||
| 2046 | $item[$this->proptags["reminder"]] = $exception["reminder_set"]; |
||
| 2047 | } |
||
| 2048 | |||
| 2049 | if (isset($exception["busystatus"])) { |
||
| 2050 | $item[$this->proptags["busystatus"]] = $exception["busystatus"]; |
||
| 2051 | } |
||
| 2052 | |||
| 2053 | return $item; |
||
| 2054 | } |
||
| 2055 | |||
| 2056 | /** |
||
| 2057 | * @param false|int $start |
||
| 2058 | * @param false|int $basedate |
||
| 2059 | * @param mixed $startocc |
||
| 2060 | * @param mixed $endocc |
||
| 2061 | * @param mixed $tz |
||
| 2062 | * @param mixed $reminderonly |
||
| 2063 | */ |
||
| 2064 | abstract public function processOccurrenceItem(array &$items, $start, int $end, $basedate, $startocc, $endocc, $tz, $reminderonly); |
||
| 2065 | } |
||
| 2066 |