| Total Complexity | 373 |
| Total Lines | 2084 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like BaseRecurrence often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseRecurrence, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 (((false|int|mixed|string)[]|int)[]|int|mixed)[]|null 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 { |
||
| 1412 | } |
||
| 1413 | |||
| 1414 | /** |
||
| 1415 | * Function which converts a recurrence date timestamp to an unix date timestamp. |
||
| 1416 | * |
||
| 1417 | * @author Steve Hardy |
||
| 1418 | * |
||
| 1419 | * @param int $rdate the date which will be converted |
||
| 1420 | * |
||
| 1421 | * @return int the converted date |
||
| 1422 | */ |
||
| 1423 | public function recurDataToUnixData($rdate) { |
||
| 1424 | return ($rdate - 194074560) * 60; |
||
| 1425 | } |
||
| 1426 | |||
| 1427 | /** |
||
| 1428 | * Function which converts an unix date timestamp to recurrence date timestamp. |
||
| 1429 | * |
||
| 1430 | * @author Johnny Biemans |
||
| 1431 | * |
||
| 1432 | * @param int $date the date which will be converted |
||
| 1433 | * |
||
| 1434 | * @return float|int the converted date in minutes |
||
| 1435 | */ |
||
| 1436 | public function unixDataToRecurData($date) { |
||
| 1437 | return ($date / 60) + 194074560; |
||
| 1438 | } |
||
| 1439 | |||
| 1440 | /** |
||
| 1441 | * gmtime() doesn't exist in standard PHP, so we have to implement it ourselves. |
||
| 1442 | * |
||
| 1443 | * @author Steve Hardy |
||
| 1444 | * |
||
| 1445 | * @param mixed $ts |
||
| 1446 | * |
||
| 1447 | * @return float|int |
||
| 1448 | */ |
||
| 1449 | public function GetTZOffset($ts) { |
||
| 1450 | $Offset = date("O", $ts); |
||
| 1451 | |||
| 1452 | $Parity = $Offset < 0 ? -1 : 1; |
||
| 1453 | $Offset = $Parity * $Offset; |
||
| 1454 | $Offset = ($Offset - ($Offset % 100)) / 100 * 60 + $Offset % 100; |
||
| 1455 | |||
| 1456 | return $Parity * $Offset; |
||
| 1457 | } |
||
| 1458 | |||
| 1459 | /** |
||
| 1460 | * gmtime() doesn't exist in standard PHP, so we have to implement it ourselves. |
||
| 1461 | * |
||
| 1462 | * @author Steve Hardy |
||
| 1463 | * |
||
| 1464 | * @param int $time |
||
| 1465 | * |
||
| 1466 | * @return array GMT Time |
||
| 1467 | */ |
||
| 1468 | public function gmtime($time) { |
||
| 1469 | $TZOffset = $this->GetTZOffset($time); |
||
| 1470 | |||
| 1471 | $t_time = $time - $TZOffset * 60; # Counter adjust for localtime() |
||
| 1472 | |||
| 1473 | return localtime($t_time, 1); |
||
| 1474 | } |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | * @param float|string $year |
||
| 1478 | */ |
||
| 1479 | public function isLeapYear($year): bool { |
||
| 1480 | return $year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0); |
||
| 1481 | } |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * @param float|string $year |
||
| 1485 | * @param int|string $month |
||
| 1486 | */ |
||
| 1487 | public function getMonthInSeconds($year, $month): int { |
||
| 1488 | if (in_array($month, [1, 3, 5, 7, 8, 10, 12])) { |
||
| 1489 | $day = 31; |
||
| 1490 | } |
||
| 1491 | elseif (in_array($month, [4, 6, 9, 11])) { |
||
| 1492 | $day = 30; |
||
| 1493 | } |
||
| 1494 | else { |
||
| 1495 | $day = 28; |
||
| 1496 | if ($this->isLeapYear($year) == 1) { |
||
| 1497 | ++$day; |
||
| 1498 | } |
||
| 1499 | } |
||
| 1500 | |||
| 1501 | return $day * 24 * 60 * 60; |
||
| 1502 | } |
||
| 1503 | |||
| 1504 | /** |
||
| 1505 | * Function to get a date by Year Nr, Month Nr, Week Nr, Day Nr, and hour. |
||
| 1506 | * |
||
| 1507 | * @param int $year |
||
| 1508 | * @param int $month |
||
| 1509 | * @param int $week |
||
| 1510 | * @param int $day |
||
| 1511 | * @param int $hour |
||
| 1512 | * |
||
| 1513 | * @return int the timestamp of the given date, timezone-independent |
||
| 1514 | */ |
||
| 1515 | public function getDateByYearMonthWeekDayHour($year, $month, $week, $day, $hour) { |
||
| 1516 | // get first day of month |
||
| 1517 | $date = gmmktime(0, 0, 0, $month, 0, $year + 1900); |
||
| 1518 | |||
| 1519 | // get wday info |
||
| 1520 | $gmdate = $this->gmtime($date); |
||
| 1521 | |||
| 1522 | $date -= $gmdate["tm_wday"] * 24 * 60 * 60; // back up to start of week |
||
| 1523 | |||
| 1524 | $date += $week * 7 * 24 * 60 * 60; // go to correct week nr |
||
| 1525 | $date += $day * 24 * 60 * 60; |
||
| 1526 | $date += $hour * 60 * 60; |
||
| 1527 | |||
| 1528 | $gmdate = $this->gmtime($date); |
||
| 1529 | |||
| 1530 | // if we are in the next month, then back up a week, because week '5' means |
||
| 1531 | // 'last week of month' |
||
| 1532 | |||
| 1533 | if ($month != $gmdate["tm_mon"] + 1) { |
||
| 1534 | $date -= 7 * 24 * 60 * 60; |
||
| 1535 | } |
||
| 1536 | |||
| 1537 | return $date; |
||
| 1538 | } |
||
| 1539 | |||
| 1540 | /** |
||
| 1541 | * getTimezone gives the timezone offset (in minutes) of the given |
||
| 1542 | * local date/time according to the given TZ info. |
||
| 1543 | * |
||
| 1544 | * @param mixed $tz |
||
| 1545 | * @param mixed $date |
||
| 1546 | */ |
||
| 1547 | public function getTimezone($tz, $date) { |
||
| 1548 | // No timezone -> GMT (+0) |
||
| 1549 | if (!isset($tz["timezone"])) { |
||
| 1550 | return 0; |
||
| 1551 | } |
||
| 1552 | |||
| 1553 | $dst = false; |
||
| 1554 | $gmdate = $this->gmtime($date); |
||
| 1555 | |||
| 1556 | $dststart = $this->getDateByYearMonthWeekDayHour($gmdate["tm_year"], $tz["dststartmonth"], $tz["dststartweek"], 0, $tz["dststarthour"]); |
||
| 1557 | $dstend = $this->getDateByYearMonthWeekDayHour($gmdate["tm_year"], $tz["dstendmonth"], $tz["dstendweek"], 0, $tz["dstendhour"]); |
||
| 1558 | |||
| 1559 | if ($dststart <= $dstend) { |
||
| 1560 | // Northern hemisphere, eg DST is during Mar-Oct |
||
| 1561 | if ($date > $dststart && $date < $dstend) { |
||
| 1562 | $dst = true; |
||
| 1563 | } |
||
| 1564 | } |
||
| 1565 | else { |
||
| 1566 | // Southern hemisphere, eg DST is during Oct-Mar |
||
| 1567 | if ($date < $dstend || $date > $dststart) { |
||
| 1568 | $dst = true; |
||
| 1569 | } |
||
| 1570 | } |
||
| 1571 | |||
| 1572 | if ($dst) { |
||
| 1573 | return $tz["timezone"] + $tz["timezonedst"]; |
||
| 1574 | } |
||
| 1575 | |||
| 1576 | return $tz["timezone"]; |
||
| 1577 | } |
||
| 1578 | |||
| 1579 | /** |
||
| 1580 | * getWeekNr() returns the week nr of the month (ie first week of february is 1). |
||
| 1581 | * |
||
| 1582 | * @param mixed $date |
||
| 1583 | * |
||
| 1584 | * @return float|int |
||
| 1585 | */ |
||
| 1586 | public function getWeekNr($date) { |
||
| 1587 | $gmdate = gmtime($date); |
||
| 1588 | $gmdate["tm_mday"] = 0; |
||
| 1589 | |||
| 1590 | return strftime("%W", $date) - strftime("%W", gmmktime($gmdate)) + 1; |
||
| 1591 | } |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * parseTimezone parses the timezone as specified in named property 0x8233 |
||
| 1595 | * in Outlook calendar messages. Returns the timezone in minutes negative |
||
| 1596 | * offset (GMT +2:00 -> -120). |
||
| 1597 | * |
||
| 1598 | * @param mixed $data |
||
| 1599 | * |
||
| 1600 | * @return null|array|false |
||
| 1601 | */ |
||
| 1602 | public function parseTimezone($data) { |
||
| 1603 | if (strlen($data) < 48) { |
||
| 1604 | return; |
||
| 1605 | } |
||
| 1606 | |||
| 1607 | return unpack("ltimezone/lunk/ltimezonedst/lunk/ldstendmonth/vdstendweek/vdstendhour/lunk/lunk/vunk/ldststartmonth/vdststartweek/vdststarthour/lunk/vunk", $data); |
||
| 1608 | } |
||
| 1609 | |||
| 1610 | /** |
||
| 1611 | * @param mixed $tz |
||
| 1612 | * |
||
| 1613 | * @return false|string |
||
| 1614 | */ |
||
| 1615 | public function getTimezoneData($tz) { |
||
| 1616 | 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); |
||
| 1617 | } |
||
| 1618 | |||
| 1619 | /** |
||
| 1620 | * createTimezone creates the timezone as specified in the named property 0x8233 |
||
| 1621 | * see also parseTimezone() |
||
| 1622 | * $tz is an array with the timezone data. |
||
| 1623 | * |
||
| 1624 | * @param mixed $tz |
||
| 1625 | * |
||
| 1626 | * @return false|string |
||
| 1627 | */ |
||
| 1628 | public function createTimezone($tz) { |
||
| 1629 | return pack( |
||
| 1630 | "lxxxxlxxxxlvvxxxxxxxxxxlvvxxxxxx", |
||
| 1631 | $tz["timezone"], |
||
| 1632 | array_key_exists("timezonedst", $tz) ? $tz["timezonedst"] : 0, |
||
| 1633 | array_key_exists("dstendmonth", $tz) ? $tz["dstendmonth"] : 0, |
||
| 1634 | array_key_exists("dstendweek", $tz) ? $tz["dstendweek"] : 0, |
||
| 1635 | array_key_exists("dstendhour", $tz) ? $tz["dstendhour"] : 0, |
||
| 1636 | array_key_exists("dststartmonth", $tz) ? $tz["dststartmonth"] : 0, |
||
| 1637 | array_key_exists("dststartweek", $tz) ? $tz["dststartweek"] : 0, |
||
| 1638 | array_key_exists("dststarthour", $tz) ? $tz["dststarthour"] : 0 |
||
| 1639 | ); |
||
| 1640 | } |
||
| 1641 | |||
| 1642 | /** |
||
| 1643 | * toGMT returns a timestamp in GMT time for the time and timezone given. |
||
| 1644 | * |
||
| 1645 | * @param mixed $tz |
||
| 1646 | * @param mixed $date |
||
| 1647 | */ |
||
| 1648 | public function toGMT($tz, $date) { |
||
| 1649 | if (!isset($tz['timezone'])) { |
||
| 1650 | return $date; |
||
| 1651 | } |
||
| 1652 | $offset = $this->getTimezone($tz, $date); |
||
| 1653 | |||
| 1654 | return $date + $offset * 60; |
||
| 1655 | } |
||
| 1656 | |||
| 1657 | /** |
||
| 1658 | * fromGMT returns a timestamp in the local timezone given from the GMT time given. |
||
| 1659 | * |
||
| 1660 | * @param mixed $tz |
||
| 1661 | * @param mixed $date |
||
| 1662 | */ |
||
| 1663 | public function fromGMT($tz, $date) { |
||
| 1664 | $offset = $this->getTimezone($tz, $date); |
||
| 1665 | |||
| 1666 | return $date - $offset * 60; |
||
| 1667 | } |
||
| 1668 | |||
| 1669 | /** |
||
| 1670 | * Function to get timestamp of the beginning of the day of the timestamp given. |
||
| 1671 | * |
||
| 1672 | * @param mixed $date |
||
| 1673 | * |
||
| 1674 | * @return false|int timestamp referring to same day but at 00:00:00 |
||
| 1675 | */ |
||
| 1676 | public function dayStartOf($date) { |
||
| 1677 | $time1 = $this->gmtime($date); |
||
| 1678 | |||
| 1679 | return gmmktime(0, 0, 0, $time1["tm_mon"] + 1, $time1["tm_mday"], $time1["tm_year"] + 1900); |
||
| 1680 | } |
||
| 1681 | |||
| 1682 | /** |
||
| 1683 | * Function to get timestamp of the beginning of the month of the timestamp given. |
||
| 1684 | * |
||
| 1685 | * @param mixed $date |
||
| 1686 | * |
||
| 1687 | * @return false|int Timestamp referring to same month but on the first day, and at 00:00:00 |
||
| 1688 | */ |
||
| 1689 | public function monthStartOf($date) { |
||
| 1693 | } |
||
| 1694 | |||
| 1695 | /** |
||
| 1696 | * Function to get timestamp of the beginning of the year of the timestamp given. |
||
| 1697 | * |
||
| 1698 | * @param mixed $date |
||
| 1699 | * |
||
| 1700 | * @return false|int Timestamp referring to the same year but on Jan 01, at 00:00:00 |
||
| 1701 | */ |
||
| 1702 | public function yearStartOf($date) { |
||
| 1703 | $time1 = $this->gmtime($date); |
||
| 1704 | |||
| 1705 | return gmmktime(0, 0, 0, 1, 1, $time1["tm_year"] + 1900); |
||
| 1706 | } |
||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Function which returns the items in a given interval. This included expansion of the recurrence and |
||
| 1710 | * processing of exceptions (modified and deleted). |
||
| 1711 | * |
||
| 1712 | * @param int $start start time of the interval (GMT) |
||
| 1713 | * @param int $end end time of the interval (GMT) |
||
| 1714 | * @param mixed $limit |
||
| 1715 | * @param mixed $remindersonly |
||
| 1716 | * |
||
| 1717 | * @return (array|mixed)[] |
||
| 1718 | * |
||
| 1719 | * @psalm-return array<int, T|array> |
||
| 1720 | */ |
||
| 1721 | public function getItems($start, $end, $limit = 0, $remindersonly = false): array { |
||
| 1722 | $items = []; |
||
| 1723 | |||
| 1724 | if (isset($this->recur)) { |
||
| 1725 | // Optimization: remindersonly and default reminder is off; since only exceptions with reminder set will match, just look which |
||
| 1726 | // exceptions are in range and have a reminder set |
||
| 1727 | if ($remindersonly && (!isset($this->messageprops[$this->proptags["reminder"]]) || $this->messageprops[$this->proptags["reminder"]] == false)) { |
||
| 1728 | // Sort exceptions by start time |
||
| 1729 | uasort($this->recur["changed_occurrences"], [$this, "sortExceptionStart"]); |
||
| 1730 | |||
| 1731 | // Loop through all changed exceptions |
||
| 1732 | foreach ($this->recur["changed_occurrences"] as $exception) { |
||
| 1733 | // Check reminder set |
||
| 1734 | if (!isset($exception["reminder"]) || $exception["reminder"] == false) { |
||
| 1735 | continue; |
||
| 1736 | } |
||
| 1737 | |||
| 1738 | // Convert to GMT |
||
| 1739 | $occstart = $this->toGMT($this->tz, $exception["start"]); |
||
| 1740 | $occend = $this->toGMT($this->tz, $exception["end"]); |
||
| 1741 | |||
| 1742 | // Check range criterium |
||
| 1743 | if ($occstart > $end || $occend < $start) { |
||
| 1744 | continue; |
||
| 1745 | } |
||
| 1746 | |||
| 1747 | // OK, add to items. |
||
| 1748 | array_push($items, $this->getExceptionProperties($exception)); |
||
| 1749 | if ($limit && (count($items) == $limit)) { |
||
| 1750 | break; |
||
| 1751 | } |
||
| 1752 | } |
||
| 1753 | |||
| 1754 | uasort($items, [$this, "sortStarttime"]); |
||
| 1755 | |||
| 1756 | return $items; |
||
| 1757 | } |
||
| 1758 | |||
| 1759 | // From here on, the dates of the occurrences are calculated in local time, so the days we're looking |
||
| 1760 | // at are calculated from the local time dates of $start and $end |
||
| 1761 | |||
| 1762 | if (isset($this->recur['regen'], $this->action['datecompleted']) && $this->recur['regen']) { |
||
| 1763 | $daystart = $this->dayStartOf($this->action['datecompleted']); |
||
| 1764 | } |
||
| 1765 | else { |
||
| 1766 | $daystart = $this->dayStartOf($this->recur["start"]); // start on first day of occurrence |
||
| 1767 | } |
||
| 1768 | |||
| 1769 | // Calculate the last day on which we want to be looking at a recurrence; this is either the end of the view |
||
| 1770 | // or the end of the recurrence, whichever comes first |
||
| 1771 | if ($end > $this->toGMT($this->tz, $this->recur["end"])) { |
||
| 1772 | $rangeend = $this->toGMT($this->tz, $this->recur["end"]); |
||
| 1773 | } |
||
| 1774 | else { |
||
| 1775 | $rangeend = $end; |
||
| 1776 | } |
||
| 1777 | |||
| 1778 | $dayend = $this->dayStartOf($this->fromGMT($this->tz, $rangeend)); |
||
| 1779 | |||
| 1780 | // Loop through the entire recurrence range of dates, and check for each occurrence whether it is in the view range. |
||
| 1781 | $recurType = (int) $this->recur["type"] < 0x2000 ? (int) $this->recur["type"] + 0x2000 : (int) $this->recur["type"]; |
||
| 1782 | |||
| 1783 | switch ($recurType) { |
||
| 1784 | case IDC_RCEV_PAT_ORB_DAILY: |
||
| 1785 | if ($this->recur["everyn"] <= 0) { |
||
| 1786 | $this->recur["everyn"] = 1440; |
||
| 1787 | } |
||
| 1788 | |||
| 1789 | if ($this->recur["subtype"] == rptDay) { |
||
| 1790 | // Every Nth day |
||
| 1791 | for ($now = $daystart; $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += 60 * $this->recur["everyn"]) { |
||
| 1792 | $this->processOccurrenceItem($items, $start, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1793 | } |
||
| 1794 | } |
||
| 1795 | else { |
||
| 1796 | // Every workday |
||
| 1797 | for ($now = $daystart; $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += 60 * 1440) { |
||
| 1798 | $nowtime = $this->gmtime($now); |
||
| 1799 | if ($nowtime["tm_wday"] > 0 && $nowtime["tm_wday"] < 6) { // only add items in the given timespace |
||
| 1800 | $this->processOccurrenceItem($items, $start, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1801 | } |
||
| 1802 | } |
||
| 1803 | } |
||
| 1804 | break; |
||
| 1805 | |||
| 1806 | case IDC_RCEV_PAT_ORB_WEEKLY: |
||
| 1807 | if ($this->recur["everyn"] <= 0) { |
||
| 1808 | $this->recur["everyn"] = 1; |
||
| 1809 | } |
||
| 1810 | |||
| 1811 | // If sliding flag is set then move to 'n' weeks |
||
| 1812 | if ($this->recur['regen']) { |
||
| 1813 | $daystart += (60 * 60 * 24 * 7 * $this->recur["everyn"]); |
||
| 1814 | } |
||
| 1815 | |||
| 1816 | for ($now = $daystart; $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += (60 * 60 * 24 * 7 * $this->recur["everyn"])) { |
||
| 1817 | if ($this->recur['regen']) { |
||
| 1818 | $this->processOccurrenceItem($items, $start, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1819 | } |
||
| 1820 | else { |
||
| 1821 | // Loop through the whole following week to the first occurrence of the week, add each day that is specified |
||
| 1822 | for ($wday = 0; $wday < 7; ++$wday) { |
||
| 1823 | $daynow = $now + $wday * 60 * 60 * 24; |
||
| 1824 | // checks weather the next coming day in recurring pattern is less than or equal to end day of the recurring item |
||
| 1825 | if ($daynow <= $dayend) { |
||
| 1826 | $nowtime = $this->gmtime($daynow); // Get the weekday of the current day |
||
| 1827 | if ($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) { // Selected ? |
||
| 1828 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1829 | } |
||
| 1830 | } |
||
| 1831 | } |
||
| 1832 | } |
||
| 1833 | } |
||
| 1834 | break; |
||
| 1835 | |||
| 1836 | case IDC_RCEV_PAT_ORB_MONTHLY: |
||
| 1837 | if ($this->recur["everyn"] <= 0) { |
||
| 1838 | $this->recur["everyn"] = 1; |
||
| 1839 | } |
||
| 1840 | |||
| 1841 | // Loop through all months from start to end of occurrence, starting at beginning of first month |
||
| 1842 | for ($now = $this->monthStartOf($daystart); $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += $this->daysInMonth($now, $this->recur["everyn"]) * 24 * 60 * 60) { |
||
| 1843 | if (isset($this->recur["monthday"]) && ($this->recur['monthday'] != "undefined") && !$this->recur['regen']) { // Day M of every N months |
||
| 1844 | $difference = 1; |
||
| 1845 | if ($this->daysInMonth($now, $this->recur["everyn"]) < $this->recur["monthday"]) { |
||
| 1846 | $difference = $this->recur["monthday"] - $this->daysInMonth($now, $this->recur["everyn"]) + 1; |
||
| 1847 | } |
||
| 1848 | $daynow = $now + (($this->recur["monthday"] - $difference) * 24 * 60 * 60); |
||
| 1849 | // checks weather the next coming day in recurrence pattern is less than or equal to end day of the recurring item |
||
| 1850 | if ($daynow <= $dayend) { |
||
| 1851 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1852 | } |
||
| 1853 | } |
||
| 1854 | elseif (isset($this->recur["nday"], $this->recur["weekdays"])) { // Nth [weekday] of every N months |
||
| 1855 | // Sanitize input |
||
| 1856 | if ($this->recur["weekdays"] == 0) { |
||
| 1857 | $this->recur["weekdays"] = 1; |
||
| 1858 | } |
||
| 1859 | |||
| 1860 | // If nday is not set to the last day in the month |
||
| 1861 | if ($this->recur["nday"] < 5) { |
||
| 1862 | // keep the track of no. of time correct selection pattern (like 2nd weekday, 4th friday, etc.) is matched |
||
| 1863 | $ndaycounter = 0; |
||
| 1864 | // Find matching weekday in this month |
||
| 1865 | for ($day = 0, $total = $this->daysInMonth($now, 1); $day < $total; ++$day) { |
||
| 1866 | $daynow = $now + $day * 60 * 60 * 24; |
||
| 1867 | $nowtime = $this->gmtime($daynow); // Get the weekday of the current day |
||
| 1868 | |||
| 1869 | if ($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) { // Selected ? |
||
| 1870 | ++$ndaycounter; |
||
| 1871 | } |
||
| 1872 | // check the selected pattern is same as asked Nth weekday,If so set the firstday |
||
| 1873 | if ($this->recur["nday"] == $ndaycounter) { |
||
| 1874 | $firstday = $day; |
||
| 1875 | break; |
||
| 1876 | } |
||
| 1877 | } |
||
| 1878 | // $firstday is the day of the month on which the asked pattern of nth weekday matches |
||
| 1879 | $daynow = $now + $firstday * 60 * 60 * 24; |
||
| 1880 | } |
||
| 1881 | else { |
||
| 1882 | // Find last day in the month ($now is the firstday of the month) |
||
| 1883 | $NumDaysInMonth = $this->daysInMonth($now, 1); |
||
| 1884 | $daynow = $now + (($NumDaysInMonth - 1) * 24 * 60 * 60); |
||
| 1885 | |||
| 1886 | $nowtime = $this->gmtime($daynow); |
||
| 1887 | while (($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) == 0) { |
||
| 1888 | $daynow -= 86400; |
||
| 1889 | $nowtime = $this->gmtime($daynow); |
||
| 1890 | } |
||
| 1891 | } |
||
| 1892 | |||
| 1893 | /* |
||
| 1894 | * 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. |
||
| 1895 | */ |
||
| 1896 | if ($daynow <= $dayend && $daynow >= $daystart) { |
||
| 1897 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1898 | } |
||
| 1899 | } |
||
| 1900 | elseif ($this->recur['regen']) { |
||
| 1901 | $next_month_start = $now + ($this->daysInMonth($now, 1) * 24 * 60 * 60); |
||
| 1902 | $now = $daystart + ($this->daysInMonth($next_month_start, $this->recur['everyn']) * 24 * 60 * 60); |
||
| 1903 | |||
| 1904 | if ($now <= $dayend) { |
||
| 1905 | $this->processOccurrenceItem($items, $daystart, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1906 | } |
||
| 1907 | } |
||
| 1908 | } |
||
| 1909 | break; |
||
| 1910 | |||
| 1911 | case IDC_RCEV_PAT_ORB_YEARLY: |
||
| 1912 | if ($this->recur["everyn"] <= 0) { |
||
| 1913 | $this->recur["everyn"] = 12; |
||
| 1914 | } |
||
| 1915 | |||
| 1916 | for ($now = $this->yearStartOf($daystart); $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += $this->daysInMonth($now, $this->recur["everyn"]) * 24 * 60 * 60) { |
||
| 1917 | if (isset($this->recur["monthday"]) && !$this->recur['regen']) { // same as monthly, but in a specific month |
||
| 1918 | // recur["month"] is in minutes since the beginning of the year |
||
| 1919 | $month = $this->monthOfYear($this->recur["month"]); // $month is now month of year [0..11] |
||
| 1920 | $monthday = $this->recur["monthday"]; // $monthday is day of the month [1..31] |
||
| 1921 | $monthstart = $now + $this->daysInMonth($now, $month) * 24 * 60 * 60; // $monthstart is the timestamp of the beginning of the month |
||
| 1922 | if ($monthday > $this->daysInMonth($monthstart, 1)) { |
||
| 1923 | $monthday = $this->daysInMonth($monthstart, 1); |
||
| 1924 | } // Cap $monthday on month length (eg 28 feb instead of 29 feb) |
||
| 1925 | $daynow = $monthstart + ($monthday - 1) * 24 * 60 * 60; |
||
| 1926 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1927 | } |
||
| 1928 | elseif (isset($this->recur["nday"], $this->recur["weekdays"])) { // Nth [weekday] in month X of every N years |
||
| 1929 | // Go the correct month |
||
| 1930 | $monthnow = $now + $this->daysInMonth($now, $this->monthOfYear($this->recur["month"])) * 24 * 60 * 60; |
||
| 1931 | |||
| 1932 | // Find first matching weekday in this month |
||
| 1933 | for ($wday = 0; $wday < 7; ++$wday) { |
||
| 1934 | $daynow = $monthnow + $wday * 60 * 60 * 24; |
||
| 1935 | $nowtime = $this->gmtime($daynow); // Get the weekday of the current day |
||
| 1936 | |||
| 1937 | if ($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) { // Selected ? |
||
| 1938 | $firstday = $wday; |
||
| 1939 | break; |
||
| 1940 | } |
||
| 1941 | } |
||
| 1942 | |||
| 1943 | // Same as above (monthly) |
||
| 1944 | $daynow = $monthnow + ($firstday + ($this->recur["nday"] - 1) * 7) * 60 * 60 * 24; |
||
| 1945 | |||
| 1946 | while ($this->monthStartOf($daynow) != $this->monthStartOf($monthnow)) { |
||
| 1947 | $daynow -= 7 * 60 * 60 * 24; |
||
| 1948 | } |
||
| 1949 | |||
| 1950 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1951 | } |
||
| 1952 | elseif ($this->recur['regen']) { |
||
| 1953 | $year_starttime = $this->gmtime($now); |
||
| 1954 | $is_next_leapyear = $this->isLeapYear($year_starttime['tm_year'] + 1900 + 1); // +1 next year |
||
| 1955 | $now = $daystart + ($is_next_leapyear ? 31622400 /* Leap year in seconds */ : 31536000 /* year in seconds */); |
||
| 1956 | |||
| 1957 | if ($now <= $dayend) { |
||
| 1958 | $this->processOccurrenceItem($items, $daystart, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
| 1959 | } |
||
| 1960 | } |
||
| 1961 | } |
||
| 1962 | } |
||
| 1963 | // to get all exception items |
||
| 1964 | if (!empty($this->recur['changed_occurrences'])) { |
||
| 1965 | $this->processExceptionItems($items, $start, $end); |
||
| 1966 | } |
||
| 1967 | } |
||
| 1968 | |||
| 1969 | // sort items on starttime |
||
| 1970 | usort($items, [$this, "sortStarttime"]); |
||
| 1971 | |||
| 1972 | // Return the MAPI-compatible list of items for this object |
||
| 1973 | return $items; |
||
| 1974 | } |
||
| 1975 | |||
| 1976 | /** |
||
| 1977 | * @psalm-return -1|0|1 |
||
| 1978 | * |
||
| 1979 | * @param mixed $a |
||
| 1980 | * @param mixed $b |
||
| 1981 | */ |
||
| 1982 | public function sortStarttime($a, $b): int { |
||
| 1983 | $aTime = $a[$this->proptags["startdate"]]; |
||
| 1984 | $bTime = $b[$this->proptags["startdate"]]; |
||
| 1985 | |||
| 1986 | return $aTime == $bTime ? 0 : ($aTime > $bTime ? 1 : -1); |
||
| 1987 | } |
||
| 1988 | |||
| 1989 | /** |
||
| 1990 | * daysInMonth. |
||
| 1991 | * |
||
| 1992 | * Returns the number of days in the upcoming number of months. If you specify 1 month as |
||
| 1993 | * $months it will give you the number of days in the month of $date. If you specify more it |
||
| 1994 | * will also count the days in the upcoming months and add that to the number of days. So |
||
| 1995 | * if you have a date in march and you specify $months as 2 it will return 61. |
||
| 1996 | * |
||
| 1997 | * @param int $date specified date as timestamp from which you want to know the number |
||
| 1998 | * of days in the month |
||
| 1999 | * @param int $months number of months you want to know the number of days in |
||
| 2000 | * |
||
| 2001 | * @return float|int number of days in the specified amount of months |
||
| 2002 | */ |
||
| 2003 | public function daysInMonth($date, $months) { |
||
| 2004 | $days = 0; |
||
| 2005 | |||
| 2006 | for ($i = 0; $i < $months; ++$i) { |
||
| 2007 | $days += date("t", $date + $days * 24 * 60 * 60); |
||
| 2008 | } |
||
| 2009 | |||
| 2010 | return $days; |
||
| 2011 | } |
||
| 2012 | |||
| 2013 | // Converts MAPI-style 'minutes' into the month of the year [0..11] |
||
| 2014 | public function monthOfYear($minutes) { |
||
| 2015 | $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 |
||
| 2016 | |||
| 2017 | $d += $minutes * 60; |
||
| 2018 | |||
| 2019 | $dtime = $this->gmtime($d); |
||
| 2020 | |||
| 2021 | return $dtime["tm_mon"]; |
||
| 2022 | } |
||
| 2023 | |||
| 2024 | /** |
||
| 2025 | * @psalm-return -1|0|1 |
||
| 2026 | * |
||
| 2027 | * @param mixed $a |
||
| 2028 | * @param mixed $b |
||
| 2029 | */ |
||
| 2030 | public function sortExceptionStart($a, $b): int { |
||
| 2032 | } |
||
| 2033 | |||
| 2034 | /** |
||
| 2035 | * Function to get all properties of a single changed exception. |
||
| 2036 | * |
||
| 2037 | * @param mixed $exception |
||
| 2038 | * |
||
| 2039 | * @return (mixed|true)[] associative array of properties for the exception |
||
| 2040 | * |
||
| 2041 | * @psalm-return array<mixed|true> |
||
| 2042 | */ |
||
| 2043 | public function getExceptionProperties($exception): array { |
||
| 2044 | // Exception has same properties as main object, with some properties overridden: |
||
| 2045 | $item = $this->messageprops; |
||
| 2046 | |||
| 2047 | // Special properties |
||
| 2086 | } |
||
| 2087 | |||
| 2088 | /** |
||
| 2089 | * @param false|int $start |
||
| 2090 | * @param false|int $basedate |
||
| 2091 | * @param mixed $startocc |
||
| 2092 | * @param mixed $endocc |
||
| 2093 | * @param mixed $tz |
||
| 2094 | * @param mixed $reminderonly |
||
| 2095 | */ |
||
| 2096 | abstract public function processOccurrenceItem(array &$items, $start, int $end, $basedate, $startocc, $endocc, $tz, $reminderonly); |
||
| 2097 | } |
||
| 2098 |