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