Total Complexity | 372 |
Total Lines | 2075 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 { |
||
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) { |
||
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 { |
||
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) { |
||
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) { |
||
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 | * createTimezone creates the timezone as specified in the named property 0x8233 |
||
1612 | * see also parseTimezone() |
||
1613 | * $tz is an array with the timezone data. |
||
1614 | * |
||
1615 | * @param mixed $tz |
||
1616 | * |
||
1617 | * @return false|string |
||
1618 | */ |
||
1619 | public function createTimezone($tz) { |
||
1620 | return pack( |
||
1621 | "lxxxxlxxxxlvvxxxxxxxxxxlvvxxxxxx", |
||
1622 | $tz["timezone"], |
||
1623 | array_key_exists("timezonedst", $tz) ? $tz["timezonedst"] : 0, |
||
1624 | array_key_exists("dstendmonth", $tz) ? $tz["dstendmonth"] : 0, |
||
1625 | array_key_exists("dstendweek", $tz) ? $tz["dstendweek"] : 0, |
||
1626 | array_key_exists("dstendhour", $tz) ? $tz["dstendhour"] : 0, |
||
1627 | array_key_exists("dststartmonth", $tz) ? $tz["dststartmonth"] : 0, |
||
1628 | array_key_exists("dststartweek", $tz) ? $tz["dststartweek"] : 0, |
||
1629 | array_key_exists("dststarthour", $tz) ? $tz["dststarthour"] : 0 |
||
1630 | ); |
||
1631 | } |
||
1632 | |||
1633 | /** |
||
1634 | * toGMT returns a timestamp in GMT time for the time and timezone given. |
||
1635 | * |
||
1636 | * @param mixed $tz |
||
1637 | * @param mixed $date |
||
1638 | */ |
||
1639 | public function toGMT($tz, $date) { |
||
1640 | if (!isset($tz['timezone'])) { |
||
1641 | return $date; |
||
1642 | } |
||
1643 | $offset = $this->getTimezone($tz, $date); |
||
1644 | |||
1645 | return $date + $offset * 60; |
||
1646 | } |
||
1647 | |||
1648 | /** |
||
1649 | * fromGMT returns a timestamp in the local timezone given from the GMT time given. |
||
1650 | * |
||
1651 | * @param mixed $tz |
||
1652 | * @param mixed $date |
||
1653 | */ |
||
1654 | public function fromGMT($tz, $date) { |
||
1655 | $offset = $this->getTimezone($tz, $date); |
||
1656 | |||
1657 | return $date - $offset * 60; |
||
1658 | } |
||
1659 | |||
1660 | /** |
||
1661 | * Function to get timestamp of the beginning of the day of the timestamp given. |
||
1662 | * |
||
1663 | * @param mixed $date |
||
1664 | * |
||
1665 | * @return false|int timestamp referring to same day but at 00:00:00 |
||
1666 | */ |
||
1667 | public function dayStartOf($date) { |
||
1668 | $time1 = $this->gmtime($date); |
||
1669 | |||
1670 | return gmmktime(0, 0, 0, $time1["tm_mon"] + 1, $time1["tm_mday"], $time1["tm_year"] + 1900); |
||
1671 | } |
||
1672 | |||
1673 | /** |
||
1674 | * Function to get timestamp of the beginning of the month of the timestamp given. |
||
1675 | * |
||
1676 | * @param mixed $date |
||
1677 | * |
||
1678 | * @return false|int Timestamp referring to same month but on the first day, and at 00:00:00 |
||
1679 | */ |
||
1680 | public function monthStartOf($date) { |
||
1684 | } |
||
1685 | |||
1686 | /** |
||
1687 | * Function to get timestamp of the beginning of the year of the timestamp given. |
||
1688 | * |
||
1689 | * @param mixed $date |
||
1690 | * |
||
1691 | * @return false|int Timestamp referring to the same year but on Jan 01, at 00:00:00 |
||
1692 | */ |
||
1693 | public function yearStartOf($date) { |
||
1694 | $time1 = $this->gmtime($date); |
||
1695 | |||
1696 | return gmmktime(0, 0, 0, 1, 1, $time1["tm_year"] + 1900); |
||
1697 | } |
||
1698 | |||
1699 | /** |
||
1700 | * Function which returns the items in a given interval. This included expansion of the recurrence and |
||
1701 | * processing of exceptions (modified and deleted). |
||
1702 | * |
||
1703 | * @param int $start start time of the interval (GMT) |
||
1704 | * @param int $end end time of the interval (GMT) |
||
1705 | * @param mixed $limit |
||
1706 | * @param mixed $remindersonly |
||
1707 | * |
||
1708 | * @return (array|mixed)[] |
||
1709 | * |
||
1710 | * @psalm-return array<int, T|array> |
||
1711 | */ |
||
1712 | public function getItems($start, $end, $limit = 0, $remindersonly = false): array { |
||
1713 | $items = []; |
||
1714 | |||
1715 | if (isset($this->recur)) { |
||
1716 | // Optimization: remindersonly and default reminder is off; since only exceptions with reminder set will match, just look which |
||
1717 | // exceptions are in range and have a reminder set |
||
1718 | if ($remindersonly && (!isset($this->messageprops[$this->proptags["reminder"]]) || $this->messageprops[$this->proptags["reminder"]] == false)) { |
||
1719 | // Sort exceptions by start time |
||
1720 | uasort($this->recur["changed_occurrences"], [$this, "sortExceptionStart"]); |
||
1721 | |||
1722 | // Loop through all changed exceptions |
||
1723 | foreach ($this->recur["changed_occurrences"] as $exception) { |
||
1724 | // Check reminder set |
||
1725 | if (!isset($exception["reminder"]) || $exception["reminder"] == false) { |
||
1726 | continue; |
||
1727 | } |
||
1728 | |||
1729 | // Convert to GMT |
||
1730 | $occstart = $this->toGMT($this->tz, $exception["start"]); |
||
1731 | $occend = $this->toGMT($this->tz, $exception["end"]); |
||
1732 | |||
1733 | // Check range criterium |
||
1734 | if ($occstart > $end || $occend < $start) { |
||
1735 | continue; |
||
1736 | } |
||
1737 | |||
1738 | // OK, add to items. |
||
1739 | array_push($items, $this->getExceptionProperties($exception)); |
||
1740 | if ($limit && (count($items) == $limit)) { |
||
1741 | break; |
||
1742 | } |
||
1743 | } |
||
1744 | |||
1745 | uasort($items, [$this, "sortStarttime"]); |
||
1746 | |||
1747 | return $items; |
||
1748 | } |
||
1749 | |||
1750 | // From here on, the dates of the occurrences are calculated in local time, so the days we're looking |
||
1751 | // at are calculated from the local time dates of $start and $end |
||
1752 | |||
1753 | if (isset($this->recur['regen'], $this->action['datecompleted']) && $this->recur['regen']) { |
||
1754 | $daystart = $this->dayStartOf($this->action['datecompleted']); |
||
1755 | } |
||
1756 | else { |
||
1757 | $daystart = $this->dayStartOf($this->recur["start"]); // start on first day of occurrence |
||
1758 | } |
||
1759 | |||
1760 | // Calculate the last day on which we want to be looking at a recurrence; this is either the end of the view |
||
1761 | // or the end of the recurrence, whichever comes first |
||
1762 | if ($end > $this->toGMT($this->tz, $this->recur["end"])) { |
||
1763 | $rangeend = $this->toGMT($this->tz, $this->recur["end"]); |
||
1764 | } |
||
1765 | else { |
||
1766 | $rangeend = $end; |
||
1767 | } |
||
1768 | |||
1769 | $dayend = $this->dayStartOf($this->fromGMT($this->tz, $rangeend)); |
||
1770 | |||
1771 | // Loop through the entire recurrence range of dates, and check for each occurrence whether it is in the view range. |
||
1772 | $recurType = (int) $this->recur["type"] < 0x2000 ? (int) $this->recur["type"] + 0x2000 : (int) $this->recur["type"]; |
||
1773 | |||
1774 | switch ($recurType) { |
||
1775 | case IDC_RCEV_PAT_ORB_DAILY: |
||
1776 | if ($this->recur["everyn"] <= 0) { |
||
1777 | $this->recur["everyn"] = 1440; |
||
1778 | } |
||
1779 | |||
1780 | if ($this->recur["subtype"] == rptDay) { |
||
1781 | // Every Nth day |
||
1782 | for ($now = $daystart; $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += 60 * $this->recur["everyn"]) { |
||
1783 | $this->processOccurrenceItem($items, $start, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1784 | } |
||
1785 | } |
||
1786 | else { |
||
1787 | // Every workday |
||
1788 | for ($now = $daystart; $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += 60 * 1440) { |
||
1789 | $nowtime = $this->gmtime($now); |
||
1790 | if ($nowtime["tm_wday"] > 0 && $nowtime["tm_wday"] < 6) { // only add items in the given timespace |
||
1791 | $this->processOccurrenceItem($items, $start, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1792 | } |
||
1793 | } |
||
1794 | } |
||
1795 | break; |
||
1796 | |||
1797 | case IDC_RCEV_PAT_ORB_WEEKLY: |
||
1798 | if ($this->recur["everyn"] <= 0) { |
||
1799 | $this->recur["everyn"] = 1; |
||
1800 | } |
||
1801 | |||
1802 | // If sliding flag is set then move to 'n' weeks |
||
1803 | if ($this->recur['regen']) { |
||
1804 | $daystart += (60 * 60 * 24 * 7 * $this->recur["everyn"]); |
||
1805 | } |
||
1806 | |||
1807 | for ($now = $daystart; $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += (60 * 60 * 24 * 7 * $this->recur["everyn"])) { |
||
1808 | if ($this->recur['regen']) { |
||
1809 | $this->processOccurrenceItem($items, $start, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1810 | } |
||
1811 | else { |
||
1812 | // Loop through the whole following week to the first occurrence of the week, add each day that is specified |
||
1813 | for ($wday = 0; $wday < 7; ++$wday) { |
||
1814 | $daynow = $now + $wday * 60 * 60 * 24; |
||
1815 | // checks weather the next coming day in recurring pattern is less than or equal to end day of the recurring item |
||
1816 | if ($daynow <= $dayend) { |
||
1817 | $nowtime = $this->gmtime($daynow); // Get the weekday of the current day |
||
1818 | if ($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) { // Selected ? |
||
1819 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1820 | } |
||
1821 | } |
||
1822 | } |
||
1823 | } |
||
1824 | } |
||
1825 | break; |
||
1826 | |||
1827 | case IDC_RCEV_PAT_ORB_MONTHLY: |
||
1828 | if ($this->recur["everyn"] <= 0) { |
||
1829 | $this->recur["everyn"] = 1; |
||
1830 | } |
||
1831 | |||
1832 | // Loop through all months from start to end of occurrence, starting at beginning of first month |
||
1833 | for ($now = $this->monthStartOf($daystart); $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += $this->daysInMonth($now, $this->recur["everyn"]) * 24 * 60 * 60) { |
||
1834 | if (isset($this->recur["monthday"]) && ($this->recur['monthday'] != "undefined") && !$this->recur['regen']) { // Day M of every N months |
||
1835 | $difference = 1; |
||
1836 | if ($this->daysInMonth($now, $this->recur["everyn"]) < $this->recur["monthday"]) { |
||
1837 | $difference = $this->recur["monthday"] - $this->daysInMonth($now, $this->recur["everyn"]) + 1; |
||
1838 | } |
||
1839 | $daynow = $now + (($this->recur["monthday"] - $difference) * 24 * 60 * 60); |
||
1840 | // checks weather the next coming day in recurrence pattern is less than or equal to end day of the recurring item |
||
1841 | if ($daynow <= $dayend) { |
||
1842 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1843 | } |
||
1844 | } |
||
1845 | elseif (isset($this->recur["nday"], $this->recur["weekdays"])) { // Nth [weekday] of every N months |
||
1846 | // Sanitize input |
||
1847 | if ($this->recur["weekdays"] == 0) { |
||
1848 | $this->recur["weekdays"] = 1; |
||
1849 | } |
||
1850 | |||
1851 | // If nday is not set to the last day in the month |
||
1852 | if ($this->recur["nday"] < 5) { |
||
1853 | // keep the track of no. of time correct selection pattern (like 2nd weekday, 4th friday, etc.) is matched |
||
1854 | $ndaycounter = 0; |
||
1855 | // Find matching weekday in this month |
||
1856 | for ($day = 0, $total = $this->daysInMonth($now, 1); $day < $total; ++$day) { |
||
1857 | $daynow = $now + $day * 60 * 60 * 24; |
||
1858 | $nowtime = $this->gmtime($daynow); // Get the weekday of the current day |
||
1859 | |||
1860 | if ($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) { // Selected ? |
||
1861 | ++$ndaycounter; |
||
1862 | } |
||
1863 | // check the selected pattern is same as asked Nth weekday,If so set the firstday |
||
1864 | if ($this->recur["nday"] == $ndaycounter) { |
||
1865 | $firstday = $day; |
||
1866 | break; |
||
1867 | } |
||
1868 | } |
||
1869 | // $firstday is the day of the month on which the asked pattern of nth weekday matches |
||
1870 | $daynow = $now + $firstday * 60 * 60 * 24; |
||
1871 | } |
||
1872 | else { |
||
1873 | // Find last day in the month ($now is the firstday of the month) |
||
1874 | $NumDaysInMonth = $this->daysInMonth($now, 1); |
||
1875 | $daynow = $now + (($NumDaysInMonth - 1) * 24 * 60 * 60); |
||
1876 | |||
1877 | $nowtime = $this->gmtime($daynow); |
||
1878 | while (($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) == 0) { |
||
1879 | $daynow -= 86400; |
||
1880 | $nowtime = $this->gmtime($daynow); |
||
1881 | } |
||
1882 | } |
||
1883 | |||
1884 | /* |
||
1885 | * 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. |
||
1886 | */ |
||
1887 | if ($daynow <= $dayend && $daynow >= $daystart) { |
||
1888 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1889 | } |
||
1890 | } |
||
1891 | elseif ($this->recur['regen']) { |
||
1892 | $next_month_start = $now + ($this->daysInMonth($now, 1) * 24 * 60 * 60); |
||
1893 | $now = $daystart + ($this->daysInMonth($next_month_start, $this->recur['everyn']) * 24 * 60 * 60); |
||
1894 | |||
1895 | if ($now <= $dayend) { |
||
1896 | $this->processOccurrenceItem($items, $daystart, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1897 | } |
||
1898 | } |
||
1899 | } |
||
1900 | break; |
||
1901 | |||
1902 | case IDC_RCEV_PAT_ORB_YEARLY: |
||
1903 | if ($this->recur["everyn"] <= 0) { |
||
1904 | $this->recur["everyn"] = 12; |
||
1905 | } |
||
1906 | |||
1907 | for ($now = $this->yearStartOf($daystart); $now <= $dayend && ($limit == 0 || count($items) < $limit); $now += $this->daysInMonth($now, $this->recur["everyn"]) * 24 * 60 * 60) { |
||
1908 | if (isset($this->recur["monthday"]) && !$this->recur['regen']) { // same as monthly, but in a specific month |
||
1909 | // recur["month"] is in minutes since the beginning of the year |
||
1910 | $month = $this->monthOfYear($this->recur["month"]); // $month is now month of year [0..11] |
||
1911 | $monthday = $this->recur["monthday"]; // $monthday is day of the month [1..31] |
||
1912 | $monthstart = $now + $this->daysInMonth($now, $month) * 24 * 60 * 60; // $monthstart is the timestamp of the beginning of the month |
||
1913 | if ($monthday > $this->daysInMonth($monthstart, 1)) { |
||
1914 | $monthday = $this->daysInMonth($monthstart, 1); |
||
1915 | } // Cap $monthday on month length (eg 28 feb instead of 29 feb) |
||
1916 | $daynow = $monthstart + ($monthday - 1) * 24 * 60 * 60; |
||
1917 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1918 | } |
||
1919 | elseif (isset($this->recur["nday"], $this->recur["weekdays"])) { // Nth [weekday] in month X of every N years |
||
1920 | // Go the correct month |
||
1921 | $monthnow = $now + $this->daysInMonth($now, $this->monthOfYear($this->recur["month"])) * 24 * 60 * 60; |
||
1922 | |||
1923 | // Find first matching weekday in this month |
||
1924 | for ($wday = 0; $wday < 7; ++$wday) { |
||
1925 | $daynow = $monthnow + $wday * 60 * 60 * 24; |
||
1926 | $nowtime = $this->gmtime($daynow); // Get the weekday of the current day |
||
1927 | |||
1928 | if ($this->recur["weekdays"] & (1 << $nowtime["tm_wday"])) { // Selected ? |
||
1929 | $firstday = $wday; |
||
1930 | break; |
||
1931 | } |
||
1932 | } |
||
1933 | |||
1934 | // Same as above (monthly) |
||
1935 | $daynow = $monthnow + ($firstday + ($this->recur["nday"] - 1) * 7) * 60 * 60 * 24; |
||
1936 | |||
1937 | while ($this->monthStartOf($daynow) != $this->monthStartOf($monthnow)) { |
||
1938 | $daynow -= 7 * 60 * 60 * 24; |
||
1939 | } |
||
1940 | |||
1941 | $this->processOccurrenceItem($items, $start, $end, $daynow, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1942 | } |
||
1943 | elseif ($this->recur['regen']) { |
||
1944 | $year_starttime = $this->gmtime($now); |
||
1945 | $is_next_leapyear = $this->isLeapYear($year_starttime['tm_year'] + 1900 + 1); // +1 next year |
||
1946 | $now = $daystart + ($is_next_leapyear ? 31622400 /* Leap year in seconds */ : 31536000 /* year in seconds */); |
||
1947 | |||
1948 | if ($now <= $dayend) { |
||
1949 | $this->processOccurrenceItem($items, $daystart, $end, $now, $this->recur["startocc"], $this->recur["endocc"], $this->tz, $remindersonly); |
||
1950 | } |
||
1951 | } |
||
1952 | } |
||
1953 | } |
||
1954 | // to get all exception items |
||
1955 | if (!empty($this->recur['changed_occurrences'])) { |
||
1956 | $this->processExceptionItems($items, $start, $end); |
||
1957 | } |
||
1958 | } |
||
1959 | |||
1960 | // sort items on starttime |
||
1961 | usort($items, [$this, "sortStarttime"]); |
||
1962 | |||
1963 | // Return the MAPI-compatible list of items for this object |
||
1964 | return $items; |
||
1965 | } |
||
1966 | |||
1967 | /** |
||
1968 | * @psalm-return -1|0|1 |
||
1969 | * |
||
1970 | * @param mixed $a |
||
1971 | * @param mixed $b |
||
1972 | */ |
||
1973 | public function sortStarttime($a, $b): int { |
||
1974 | $aTime = $a[$this->proptags["startdate"]]; |
||
1975 | $bTime = $b[$this->proptags["startdate"]]; |
||
1976 | |||
1977 | return $aTime == $bTime ? 0 : ($aTime > $bTime ? 1 : -1); |
||
1978 | } |
||
1979 | |||
1980 | /** |
||
1981 | * daysInMonth. |
||
1982 | * |
||
1983 | * Returns the number of days in the upcoming number of months. If you specify 1 month as |
||
1984 | * $months it will give you the number of days in the month of $date. If you specify more it |
||
1985 | * will also count the days in the upcoming months and add that to the number of days. So |
||
1986 | * if you have a date in march and you specify $months as 2 it will return 61. |
||
1987 | * |
||
1988 | * @param int $date specified date as timestamp from which you want to know the number |
||
1989 | * of days in the month |
||
1990 | * @param int $months number of months you want to know the number of days in |
||
1991 | * |
||
1992 | * @return float|int number of days in the specified amount of months |
||
1993 | */ |
||
1994 | public function daysInMonth($date, $months) { |
||
1995 | $days = 0; |
||
1996 | |||
1997 | for ($i = 0; $i < $months; ++$i) { |
||
1998 | $days += date("t", $date + $days * 24 * 60 * 60); |
||
1999 | } |
||
2000 | |||
2001 | return $days; |
||
2002 | } |
||
2003 | |||
2004 | // Converts MAPI-style 'minutes' into the month of the year [0..11] |
||
2005 | public function monthOfYear($minutes) { |
||
2006 | $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 |
||
2007 | |||
2008 | $d += $minutes * 60; |
||
2009 | |||
2010 | $dtime = $this->gmtime($d); |
||
2011 | |||
2012 | return $dtime["tm_mon"]; |
||
2013 | } |
||
2014 | |||
2015 | /** |
||
2016 | * @psalm-return -1|0|1 |
||
2017 | * |
||
2018 | * @param mixed $a |
||
2019 | * @param mixed $b |
||
2020 | */ |
||
2021 | public function sortExceptionStart($a, $b): int { |
||
2023 | } |
||
2024 | |||
2025 | /** |
||
2026 | * Function to get all properties of a single changed exception. |
||
2027 | * |
||
2028 | * @param mixed $exception |
||
2029 | * |
||
2030 | * @return (mixed|true)[] associative array of properties for the exception |
||
2031 | * |
||
2032 | * @psalm-return array<mixed|true> |
||
2033 | */ |
||
2034 | public function getExceptionProperties($exception): array { |
||
2077 | } |
||
2078 | |||
2079 | /** |
||
2080 | * @param false|int $start |
||
2081 | * @param false|int $basedate |
||
2082 | * @param mixed $startocc |
||
2083 | * @param mixed $endocc |
||
2084 | * @param mixed $tz |
||
2085 | * @param mixed $reminderonly |
||
2086 | */ |
||
2087 | abstract public function processOccurrenceItem(array &$items, $start, int $end, $basedate, $startocc, $endocc, $tz, $reminderonly); |
||
2088 | } |
||
2089 |