Conditions | 169 |
Total Lines | 823 |
Code Lines | 422 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
602 | public function saveRecurrence(): void { |
||
603 | // Only save if a message was passed |
||
604 | if (!isset($this->message)) { |
||
605 | return; |
||
606 | } |
||
607 | |||
608 | // Abort if no recurrence was set |
||
609 | if (!isset( |
||
610 | $this->recur["type"], |
||
611 | $this->recur["subtype"], |
||
612 | $this->recur["start"], |
||
613 | $this->recur["end"], |
||
614 | $this->recur["startocc"], |
||
615 | $this->recur["endocc"]) |
||
616 | ) { |
||
617 | return; |
||
618 | } |
||
619 | |||
620 | $rtype = 0x2000 + (int) $this->recur["type"]; |
||
621 | |||
622 | // Don't allow invalid type and subtype values |
||
623 | if (!in_array($rtype, [IDC_RCEV_PAT_ORB_DAILY, IDC_RCEV_PAT_ORB_WEEKLY, IDC_RCEV_PAT_ORB_MONTHLY, IDC_RCEV_PAT_ORB_YEARLY])) { |
||
624 | return; |
||
625 | } |
||
626 | |||
627 | if (!in_array((int) $this->recur["subtype"], [rptDay, rptWeek, rptMonth, rptMonthNth, rptMonthEnd, rptHjMonth, rptHjMonthNth, rptHjMonthEnd])) { |
||
628 | return; |
||
629 | } |
||
630 | |||
631 | $rdata = pack("vvvvv", 0x3004, 0x3004, $rtype, (int) $this->recur["subtype"], MAPI_CAL_DEFAULT); |
||
632 | $weekstart = 1; // monday |
||
633 | $forwardcount = 0; |
||
634 | $count = 0; |
||
635 | $restocc = 0; |
||
636 | $dayofweek = (int) gmdate("w", (int) $this->recur["start"]); // 0 (for Sunday) through 6 (for Saturday) |
||
637 | |||
638 | // Terminate |
||
639 | $term = (int) $this->recur["term"] < 0x2000 ? 0x2000 + (int) $this->recur["term"] : (int) $this->recur["term"]; |
||
640 | |||
641 | switch ($rtype) { |
||
642 | case IDC_RCEV_PAT_ORB_DAILY: |
||
643 | if (!isset($this->recur["everyn"]) || (int) $this->recur["everyn"] > 1438560 || (int) $this->recur["everyn"] < 0) { // minutes for 999 days |
||
644 | return; |
||
645 | } |
||
646 | |||
647 | if ($this->recur["subtype"] == rptWeek) { |
||
648 | // Daily every workday |
||
649 | $rdata .= pack("VVVV", 6 * 24 * 60, 1, 0, 0x3E); |
||
650 | } |
||
651 | else { |
||
652 | // Calc first occ |
||
653 | $firstocc = $this->unixDataToRecurData($this->recur["start"]) % ((int) $this->recur["everyn"]); |
||
654 | |||
655 | $rdata .= pack("VVV", $firstocc, (int) $this->recur["everyn"], $this->recur["regen"] ? 1 : 0); |
||
656 | } |
||
657 | break; |
||
658 | |||
659 | case IDC_RCEV_PAT_ORB_WEEKLY: |
||
660 | if (!isset($this->recur["everyn"]) || $this->recur["everyn"] > 99 || (int) $this->recur["everyn"] < 0) { |
||
661 | return; |
||
662 | } |
||
663 | |||
664 | if (!$this->recur["regen"] && !isset($this->recur["weekdays"])) { |
||
665 | return; |
||
666 | } |
||
667 | |||
668 | // No need to calculate startdate if sliding flag was set. |
||
669 | if (!$this->recur['regen']) { |
||
670 | // Calculate start date of recurrence |
||
671 | |||
672 | // Find the first day that matches one of the weekdays selected |
||
673 | $daycount = 0; |
||
674 | $dayskip = -1; |
||
675 | for ($j = 0; $j < 7; ++$j) { |
||
676 | if (((int) $this->recur["weekdays"]) & (1 << (($dayofweek + $j) % 7))) { |
||
677 | if ($dayskip == -1) { |
||
678 | $dayskip = $j; |
||
679 | } |
||
680 | |||
681 | ++$daycount; |
||
682 | } |
||
683 | } |
||
684 | |||
685 | // $dayskip is the number of days to skip from the startdate until the first occurrence |
||
686 | // $daycount is the number of days per week that an occurrence occurs |
||
687 | |||
688 | $weekskip = 0; |
||
689 | if (($dayofweek < $weekstart && $dayskip > 0) || ($dayofweek + $dayskip) > 6) { |
||
690 | $weekskip = 1; |
||
691 | } |
||
692 | |||
693 | // Check if the recurrence ends after a number of occurrences, in that case we must calculate the |
||
694 | // remaining occurrences based on the start of the recurrence. |
||
695 | if ($term == IDC_RCEV_PAT_ERB_AFTERNOCCUR) { |
||
696 | // $weekskip is the amount of weeks to skip from the startdate before the first occurrence |
||
697 | // $forwardcount is the maximum number of week occurrences we can go ahead after the first occurrence that |
||
698 | // is still inside the recurrence. We subtract one to make sure that the last week is never forwarded over |
||
699 | // (eg when numoccur = 2, and daycount = 1) |
||
700 | $forwardcount = floor((int) ($this->recur["numoccur"] - 1) / $daycount); |
||
701 | |||
702 | // $restocc is the number of occurrences left after $forwardcount whole weeks of occurrences, minus one |
||
703 | // for the occurrence on the first day |
||
704 | $restocc = ((int) $this->recur["numoccur"]) - ($forwardcount * $daycount) - 1; |
||
705 | |||
706 | // $forwardcount is now the number of weeks we can go forward and still be inside the recurrence |
||
707 | $forwardcount *= (int) $this->recur["everyn"]; |
||
708 | } |
||
709 | |||
710 | // The real start is start + dayskip + weekskip-1 (since dayskip will already bring us into the next week) |
||
711 | $this->recur["start"] = ((int) $this->recur["start"]) + ($dayskip * 24 * 60 * 60) + ($weekskip * (((int) $this->recur["everyn"]) - 1) * 7 * 24 * 60 * 60); |
||
712 | } |
||
713 | |||
714 | // Calc first occ |
||
715 | $firstocc = $this->unixDataToRecurData($this->recur["start"]) % (((int) $this->recur["everyn"]) * 7 * 24 * 60); |
||
716 | |||
717 | $firstocc -= (((int) gmdate("w", (int) $this->recur["start"])) - 1) * 24 * 60; |
||
718 | |||
719 | if ($this->recur["regen"]) { |
||
720 | $rdata .= pack("VVV", $firstocc, (int) $this->recur["everyn"], 1); |
||
721 | } |
||
722 | else { |
||
723 | $rdata .= pack("VVVV", $firstocc, (int) $this->recur["everyn"], 0, (int) $this->recur["weekdays"]); |
||
724 | } |
||
725 | break; |
||
726 | |||
727 | case IDC_RCEV_PAT_ORB_MONTHLY: |
||
728 | case IDC_RCEV_PAT_ORB_YEARLY: |
||
729 | if (!isset($this->recur["everyn"])) { |
||
730 | return; |
||
731 | } |
||
732 | if ($rtype == IDC_RCEV_PAT_ORB_YEARLY && !isset($this->recur["month"])) { |
||
733 | return; |
||
734 | } |
||
735 | |||
736 | if ($rtype == IDC_RCEV_PAT_ORB_MONTHLY) { |
||
737 | $everyn = (int) $this->recur["everyn"]; |
||
738 | if ($everyn > 99 || $everyn < 0) { |
||
739 | return; |
||
740 | } |
||
741 | } |
||
742 | else { |
||
743 | $everyn = $this->recur["regen"] ? ((int) $this->recur["everyn"]) * 12 : 12; |
||
744 | } |
||
745 | |||
746 | // Get montday/month/year of original start |
||
747 | $curmonthday = gmdate("j", (int) $this->recur["start"]); |
||
748 | $curyear = gmdate("Y", (int) $this->recur["start"]); |
||
749 | $curmonth = gmdate("n", (int) $this->recur["start"]); |
||
750 | |||
751 | // Check if the recurrence ends after a number of occurrences, in that case we must calculate the |
||
752 | // remaining occurrences based on the start of the recurrence. |
||
753 | if ($term == IDC_RCEV_PAT_ERB_AFTERNOCCUR) { |
||
754 | // $forwardcount is the number of occurrences we can skip and still be inside the recurrence range (minus |
||
755 | // one to make sure there are always at least one occurrence left) |
||
756 | $forwardcount = ((((int) $this->recur["numoccur"]) - 1) * $everyn); |
||
757 | } |
||
758 | |||
759 | // Get month for yearly on D'th day of month M |
||
760 | if ($rtype == IDC_RCEV_PAT_ORB_YEARLY) { |
||
761 | $selmonth = floor(((int) $this->recur["month"]) / (24 * 60 * 29)) + 1; // 1=jan, 2=feb, eg |
||
762 | } |
||
763 | |||
764 | switch ((int) $this->recur["subtype"]) { |
||
765 | // on D day of every M month |
||
766 | case rptMonth: |
||
767 | if (!isset($this->recur["monthday"])) { |
||
768 | return; |
||
769 | } |
||
770 | // Recalc startdate |
||
771 | |||
772 | // Set on the right begin day |
||
773 | |||
774 | // Go the beginning of the month |
||
775 | $this->recur["start"] -= ($curmonthday - 1) * 24 * 60 * 60; |
||
776 | // Go the the correct month day |
||
777 | $this->recur["start"] += (((int) $this->recur["monthday"]) - 1) * 24 * 60 * 60; |
||
778 | |||
779 | // If the previous calculation gave us a start date different than the original start date, then we need to skip to the first occurrence |
||
780 | if (($rtype == IDC_RCEV_PAT_ORB_MONTHLY && ((int) $this->recur["monthday"]) < $curmonthday) || |
||
781 | ($rtype == IDC_RCEV_PAT_ORB_YEARLY && ($selmonth != $curmonth || ($selmonth == $curmonth && ((int) $this->recur["monthday"]) < $curmonthday)))) { |
||
782 | if ($rtype == IDC_RCEV_PAT_ORB_YEARLY) { |
||
783 | if ($curmonth > $selmonth) {// go to next occurrence in 'everyn' months minus difference in first occurrence and original date |
||
784 | $count = $everyn - ($curmonth - $selmonth); |
||
785 | } |
||
786 | elseif ($curmonth < $selmonth) {// go to next occurrence upto difference in first occurrence and original date |
||
787 | $count = $selmonth - $curmonth; |
||
788 | } |
||
789 | else { |
||
790 | // Go to next occurrence while recurrence start date is greater than occurrence date but within same month |
||
791 | if (((int) $this->recur["monthday"]) < $curmonthday) { |
||
792 | $count = $everyn; |
||
793 | } |
||
794 | } |
||
795 | } |
||
796 | else { |
||
797 | $count = $everyn; // Monthly, go to next occurrence in 'everyn' months |
||
798 | } |
||
799 | |||
800 | // Forward by $count months. This is done by getting the number of days in that month and forwarding that many days |
||
801 | for ($i = 0; $i < $count; ++$i) { |
||
802 | $this->recur["start"] += $this->getMonthInSeconds($curyear, $curmonth); |
||
803 | |||
804 | if ($curmonth == 12) { |
||
805 | ++$curyear; |
||
806 | $curmonth = 0; |
||
807 | } |
||
808 | ++$curmonth; |
||
809 | } |
||
810 | } |
||
811 | |||
812 | // "start" is now pointing to the first occurrence, except that it will overshoot if the |
||
813 | // month in which it occurs has less days than specified as the day of the month. So 31st |
||
814 | // of each month will overshoot in february (29 days). We compensate for that by checking |
||
815 | // if the day of the month we got is wrong, and then back up to the last day of the previous |
||
816 | // month. |
||
817 | if (((int) $this->recur["monthday"]) >= 28 && ((int) $this->recur["monthday"]) <= 31 && |
||
818 | gmdate("j", (int) $this->recur["start"]) < ((int) $this->recur["monthday"])) { |
||
819 | $this->recur["start"] -= gmdate("j", (int) $this->recur["start"]) * 24 * 60 * 60; |
||
820 | } |
||
821 | |||
822 | // "start" is now the first occurrence |
||
823 | if ($rtype == IDC_RCEV_PAT_ORB_MONTHLY) { |
||
824 | // Calc first occ |
||
825 | $monthIndex = ((((12 % $everyn) * ((((int) gmdate("Y", $this->recur["start"])) - 1601) % $everyn)) % $everyn) + (((int) gmdate("n", $this->recur["start"])) - 1)) % $everyn; |
||
826 | |||
827 | $firstocc = 0; |
||
828 | for ($i = 0; $i < $monthIndex; ++$i) { |
||
829 | $firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), ($i % 12) + 1) / 60; |
||
830 | } |
||
831 | |||
832 | $rdata .= pack("VVVV", $firstocc, $everyn, $this->recur["regen"], (int) $this->recur["monthday"]); |
||
833 | } |
||
834 | else { |
||
835 | // Calc first occ |
||
836 | $firstocc = 0; |
||
837 | $monthIndex = (int) gmdate("n", $this->recur["start"]); |
||
838 | for ($i = 1; $i < $monthIndex; ++$i) { |
||
839 | $firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), $i) / 60; |
||
840 | } |
||
841 | |||
842 | $rdata .= pack("VVVV", $firstocc, $everyn, $this->recur["regen"], (int) $this->recur["monthday"]); |
||
843 | } |
||
844 | break; |
||
845 | |||
846 | case rptMonthNth: |
||
847 | // monthly: on Nth weekday of every M month |
||
848 | // yearly: on Nth weekday of M month |
||
849 | if (!isset($this->recur["weekdays"], $this->recur["nday"])) { |
||
850 | return; |
||
851 | } |
||
852 | |||
853 | $weekdays = (int) $this->recur["weekdays"]; |
||
854 | $nday = (int) $this->recur["nday"]; |
||
855 | |||
856 | // Calc startdate |
||
857 | $monthbegindow = (int) $this->recur["start"]; |
||
858 | |||
859 | if ($nday == 5) { |
||
860 | // Set date on the last day of the last month |
||
861 | $monthbegindow += (gmdate("t", $monthbegindow) - gmdate("j", $monthbegindow)) * 24 * 60 * 60; |
||
862 | } |
||
863 | else { |
||
864 | // Set on the first day of the month |
||
865 | $monthbegindow -= ((gmdate("j", $monthbegindow) - 1) * 24 * 60 * 60); |
||
866 | } |
||
867 | |||
868 | if ($rtype == IDC_RCEV_PAT_ORB_YEARLY) { |
||
869 | // Set on right month |
||
870 | if ($selmonth < $curmonth) { |
||
871 | $tmp = 12 - $curmonth + $selmonth; |
||
872 | } |
||
873 | else { |
||
874 | $tmp = ($selmonth - $curmonth); |
||
875 | } |
||
876 | |||
877 | for ($i = 0; $i < $tmp; ++$i) { |
||
878 | $monthbegindow += $this->getMonthInSeconds($curyear, $curmonth); |
||
879 | |||
880 | if ($curmonth == 12) { |
||
881 | ++$curyear; |
||
882 | $curmonth = 0; |
||
883 | } |
||
884 | ++$curmonth; |
||
885 | } |
||
886 | } |
||
887 | else { |
||
888 | // Check or you exist in the right month |
||
889 | |||
890 | $dayofweek = gmdate("w", $monthbegindow); |
||
891 | for ($i = 0; $i < 7; ++$i) { |
||
892 | if ($nday == 5 && (($dayofweek - $i) % 7 >= 0) && (1 << (($dayofweek - $i) % 7)) & $weekdays) { |
||
893 | $day = gmdate("j", $monthbegindow) - $i; |
||
894 | break; |
||
895 | } |
||
896 | if ($nday != 5 && (1 << (($dayofweek + $i) % 7)) & $weekdays) { |
||
897 | $day = (($nday - 1) * 7) + ($i + 1); |
||
898 | break; |
||
899 | } |
||
900 | } |
||
901 | |||
902 | // Goto the next X month |
||
903 | if (isset($day) && ($day < gmdate("j", (int) $this->recur["start"]))) { |
||
904 | if ($nday == 5) { |
||
905 | $monthbegindow += 24 * 60 * 60; |
||
906 | if ($curmonth == 12) { |
||
907 | ++$curyear; |
||
908 | $curmonth = 0; |
||
909 | } |
||
910 | ++$curmonth; |
||
911 | } |
||
912 | |||
913 | for ($i = 0; $i < $everyn; ++$i) { |
||
914 | $monthbegindow += $this->getMonthInSeconds($curyear, $curmonth); |
||
915 | |||
916 | if ($curmonth == 12) { |
||
917 | ++$curyear; |
||
918 | $curmonth = 0; |
||
919 | } |
||
920 | ++$curmonth; |
||
921 | } |
||
922 | |||
923 | if ($nday == 5) { |
||
924 | $monthbegindow -= 24 * 60 * 60; |
||
925 | } |
||
926 | } |
||
927 | } |
||
928 | |||
929 | // FIXME: weekstart? |
||
930 | |||
931 | $day = 0; |
||
932 | // Set start on the right day |
||
933 | $dayofweek = gmdate("w", $monthbegindow); |
||
934 | for ($i = 0; $i < 7; ++$i) { |
||
935 | if ($nday == 5 && (($dayofweek - $i) % 7) >= 0 && (1 << (($dayofweek - $i) % 7)) & $weekdays) { |
||
936 | $day = $i; |
||
937 | break; |
||
938 | } |
||
939 | if ($nday != 5 && (1 << (($dayofweek + $i) % 7)) & $weekdays) { |
||
940 | $day = ($nday - 1) * 7 + ($i + 1); |
||
941 | break; |
||
942 | } |
||
943 | } |
||
944 | if ($nday == 5) { |
||
945 | $monthbegindow -= $day * 24 * 60 * 60; |
||
946 | } |
||
947 | else { |
||
948 | $monthbegindow += ($day - 1) * 24 * 60 * 60; |
||
949 | } |
||
950 | |||
951 | $firstocc = 0; |
||
952 | if ($rtype == IDC_RCEV_PAT_ORB_MONTHLY) { |
||
953 | // Calc first occ |
||
954 | $monthIndex = ((((12 % $everyn) * (((int) gmdate("Y", $this->recur["start"]) - 1601) % $everyn)) % $everyn) + (((int) gmdate("n", $this->recur["start"])) - 1)) % $everyn; |
||
955 | |||
956 | for ($i = 0; $i < $monthIndex; ++$i) { |
||
957 | $firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), ($i % 12) + 1) / 60; |
||
958 | } |
||
959 | |||
960 | $rdata .= pack("VVVVV", $firstocc, $everyn, 0, $weekdays, $nday); |
||
961 | } |
||
962 | else { |
||
963 | // Calc first occ |
||
964 | $monthIndex = (int) gmdate("n", $this->recur["start"]); |
||
965 | |||
966 | for ($i = 1; $i < $monthIndex; ++$i) { |
||
967 | $firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), $i) / 60; |
||
968 | } |
||
969 | |||
970 | $rdata .= pack("VVVVV", $firstocc, $everyn, 0, $weekdays, $nday); |
||
971 | } |
||
972 | break; |
||
973 | } |
||
974 | break; |
||
975 | } |
||
976 | |||
977 | if (!isset($this->recur["term"])) { |
||
978 | return; |
||
979 | } |
||
980 | |||
981 | $rdata .= pack("V", $term); |
||
982 | |||
983 | switch ($term) { |
||
984 | // After the given enddate |
||
985 | case IDC_RCEV_PAT_ERB_END: |
||
986 | $rdata .= pack("V", 10); |
||
987 | break; |
||
988 | |||
989 | // After a number of times |
||
990 | case IDC_RCEV_PAT_ERB_AFTERNOCCUR: |
||
991 | if (!isset($this->recur["numoccur"])) { |
||
992 | return; |
||
993 | } |
||
994 | |||
995 | $rdata .= pack("V", (int) $this->recur["numoccur"]); |
||
996 | break; |
||
997 | |||
998 | // Never ends |
||
999 | case IDC_RCEV_PAT_ERB_NOEND: |
||
1000 | $rdata .= pack("V", 0); |
||
1001 | break; |
||
1002 | } |
||
1003 | |||
1004 | // Persist first day of week (defaults to Monday for weekly recurrences) |
||
1005 | $firstDow = $this->recur["first_dow"] ?? ($rtype == IDC_RCEV_PAT_ORB_WEEKLY && ((int) $this->recur["subtype"]) == 1 ? 1 : 0); |
||
1006 | $rdata .= pack("V", (int) $firstDow); |
||
1007 | |||
1008 | // Exception data |
||
1009 | |||
1010 | // Get all exceptions |
||
1011 | $deleted_items = $this->recur["deleted_occurrences"]; |
||
1012 | $changed_items = $this->recur["changed_occurrences"]; |
||
1013 | |||
1014 | // Merge deleted and changed items into one list |
||
1015 | $items = $deleted_items; |
||
1016 | |||
1017 | foreach ($changed_items as $changed_item) { |
||
1018 | array_push($items, $this->dayStartOf($changed_item["basedate"])); |
||
1019 | } |
||
1020 | |||
1021 | sort($items); |
||
1022 | |||
1023 | // Add the merged list in to the rdata |
||
1024 | $rdata .= pack("V", count($items)); |
||
1025 | foreach ($items as $item) { |
||
1026 | $rdata .= pack("V", $this->unixDataToRecurData($item)); |
||
1027 | } |
||
1028 | |||
1029 | // Loop through the changed exceptions (not deleted) |
||
1030 | $rdata .= pack("V", count($changed_items)); |
||
1031 | $items = []; |
||
1032 | |||
1033 | foreach ($changed_items as $changed_item) { |
||
1034 | $items[] = $this->dayStartOf($changed_item["start"]); |
||
1035 | } |
||
1036 | |||
1037 | sort($items); |
||
1038 | |||
1039 | // Add the changed items list int the rdata |
||
1040 | foreach ($items as $item) { |
||
1041 | $rdata .= pack("V", $this->unixDataToRecurData($item)); |
||
1042 | } |
||
1043 | |||
1044 | // Set start date |
||
1045 | $rdata .= pack("V", $this->unixDataToRecurData((int) $this->recur["start"])); |
||
1046 | |||
1047 | // Set enddate |
||
1048 | switch ($term) { |
||
1049 | // After the given enddate |
||
1050 | case IDC_RCEV_PAT_ERB_END: |
||
1051 | $rdata .= pack("V", $this->unixDataToRecurData((int) $this->recur["end"])); |
||
1052 | break; |
||
1053 | |||
1054 | // After a number of times |
||
1055 | case IDC_RCEV_PAT_ERB_AFTERNOCCUR: |
||
1056 | // @todo: calculate enddate with intval($this->recur["startocc"]) + intval($this->recur["duration"]) > 24 hour |
||
1057 | $occenddate = (int) $this->recur["start"]; |
||
1058 | |||
1059 | switch ($rtype) { |
||
1060 | case IDC_RCEV_PAT_ORB_DAILY: |
||
1061 | if ($this->recur["subtype"] == rptWeek) { |
||
1062 | // Daily every workday |
||
1063 | $restocc = (int) $this->recur["numoccur"]; |
||
1064 | |||
1065 | // Get starting weekday |
||
1066 | $nowtime = $this->gmtime($occenddate); |
||
1067 | $j = $nowtime["tm_wday"]; |
||
1068 | |||
1069 | while (1) { |
||
1070 | if (($j % 7) > 0 && ($j % 7) < 6) { |
||
1071 | --$restocc; |
||
1072 | } |
||
1073 | |||
1074 | ++$j; |
||
1075 | |||
1076 | if ($restocc <= 0) { |
||
1077 | break; |
||
1078 | } |
||
1079 | |||
1080 | $occenddate += 24 * 60 * 60; |
||
1081 | } |
||
1082 | } |
||
1083 | else { |
||
1084 | // -1 because the first day already counts (from 1-1-1980 to 1-1-1980 is 1 occurrence) |
||
1085 | $occenddate += (((int) $this->recur["everyn"]) * 60 * ((int) $this->recur["numoccur"] - 1)); |
||
1086 | } |
||
1087 | break; |
||
1088 | |||
1089 | case IDC_RCEV_PAT_ORB_WEEKLY: |
||
1090 | // Needed values |
||
1091 | // $forwardcount - number of weeks we can skip forward |
||
1092 | // $restocc - number of remaining occurrences after the week skip |
||
1093 | |||
1094 | // Add the weeks till the last item |
||
1095 | $occenddate += ($forwardcount * 7 * 24 * 60 * 60); |
||
1096 | |||
1097 | $dayofweek = gmdate("w", $occenddate); |
||
1098 | |||
1099 | // Loop through the last occurrences until we have had them all |
||
1100 | for ($j = 1; $restocc > 0; ++$j) { |
||
1101 | // Jump to the next week (which may be N weeks away) when going over the week boundary |
||
1102 | if ((($dayofweek + $j) % 7) == $weekstart) { |
||
1103 | $occenddate += (((int) $this->recur["everyn"]) - 1) * 7 * 24 * 60 * 60; |
||
1104 | } |
||
1105 | |||
1106 | // If this is a matching day, once less occurrence to process |
||
1107 | if (((int) $this->recur["weekdays"]) & (1 << (($dayofweek + $j) % 7))) { |
||
1108 | --$restocc; |
||
1109 | } |
||
1110 | |||
1111 | // Next day |
||
1112 | $occenddate += 24 * 60 * 60; |
||
1113 | } |
||
1114 | |||
1115 | break; |
||
1116 | |||
1117 | case IDC_RCEV_PAT_ORB_MONTHLY: |
||
1118 | case IDC_RCEV_PAT_ORB_YEARLY: |
||
1119 | $curyear = gmdate("Y", (int) $this->recur["start"]); |
||
1120 | $curmonth = gmdate("n", (int) $this->recur["start"]); |
||
1121 | // $forwardcount = months |
||
1122 | |||
1123 | switch ((int) $this->recur["subtype"]) { |
||
1124 | case rptMonth: // on D day of every M month |
||
1125 | while ($forwardcount > 0) { |
||
1126 | $occenddate += $this->getMonthInSeconds($curyear, $curmonth); |
||
1127 | |||
1128 | if ($curmonth >= 12) { |
||
1129 | $curmonth = 1; |
||
1130 | ++$curyear; |
||
1131 | } |
||
1132 | else { |
||
1133 | ++$curmonth; |
||
1134 | } |
||
1135 | --$forwardcount; |
||
1136 | } |
||
1137 | |||
1138 | // compensation between 28 and 31 |
||
1139 | if (((int) $this->recur["monthday"]) >= 28 && ((int) $this->recur["monthday"]) <= 31 && |
||
1140 | gmdate("j", $occenddate) < ((int) $this->recur["monthday"])) { |
||
1141 | if (gmdate("j", $occenddate) < 28) { |
||
1142 | $occenddate -= gmdate("j", $occenddate) * 24 * 60 * 60; |
||
1143 | } |
||
1144 | else { |
||
1145 | $occenddate += (gmdate("t", $occenddate) - gmdate("j", $occenddate)) * 24 * 60 * 60; |
||
1146 | } |
||
1147 | } |
||
1148 | |||
1149 | break; |
||
1150 | |||
1151 | case rptMonthNth: // on Nth weekday of every M month |
||
1152 | $nday = (int) $this->recur["nday"]; // 1 tot 5 |
||
1153 | $weekdays = (int) $this->recur["weekdays"]; |
||
1154 | |||
1155 | while ($forwardcount > 0) { |
||
1156 | $occenddate += $this->getMonthInSeconds($curyear, $curmonth); |
||
1157 | if ($curmonth >= 12) { |
||
1158 | $curmonth = 1; |
||
1159 | ++$curyear; |
||
1160 | } |
||
1161 | else { |
||
1162 | ++$curmonth; |
||
1163 | } |
||
1164 | |||
1165 | --$forwardcount; |
||
1166 | } |
||
1167 | |||
1168 | if ($nday == 5) { |
||
1169 | // Set date on the last day of the last month |
||
1170 | $occenddate += (gmdate("t", $occenddate) - gmdate("j", $occenddate)) * 24 * 60 * 60; |
||
1171 | } |
||
1172 | else { |
||
1173 | // Set date on the first day of the last month |
||
1174 | $occenddate -= (gmdate("j", $occenddate) - 1) * 24 * 60 * 60; |
||
1175 | } |
||
1176 | |||
1177 | $dayofweek = gmdate("w", $occenddate); |
||
1178 | for ($i = 0; $i < 7; ++$i) { |
||
1179 | if ($nday == 5 && (($dayofweek - $i) % 7) >= 0 && (1 << (($dayofweek - $i) % 7)) & $weekdays) { |
||
1180 | $occenddate -= $i * 24 * 60 * 60; |
||
1181 | break; |
||
1182 | } |
||
1183 | if ($nday != 5 && (1 << (($dayofweek + $i) % 7)) & $weekdays) { |
||
1184 | $occenddate += ($i + (($nday - 1) * 7)) * 24 * 60 * 60; |
||
1185 | break; |
||
1186 | } |
||
1187 | } |
||
1188 | |||
1189 | break; // case rptMonthNth |
||
1190 | } |
||
1191 | |||
1192 | break; |
||
1193 | } |
||
1194 | |||
1195 | if (defined("PHP_INT_MAX") && $occenddate > PHP_INT_MAX) { |
||
1196 | $occenddate = PHP_INT_MAX; |
||
1197 | } |
||
1198 | |||
1199 | $this->recur["end"] = $occenddate; |
||
1200 | |||
1201 | $rdata .= pack("V", $this->unixDataToRecurData((int) $this->recur["end"])); |
||
1202 | break; |
||
1203 | |||
1204 | // Never ends |
||
1205 | case IDC_RCEV_PAT_ERB_NOEND: |
||
1206 | default: |
||
1207 | $this->recur["end"] = 0x7FFFFFFF; // max date -> 2038 |
||
1208 | $rdata .= pack("V", 0x5AE980DF); |
||
1209 | break; |
||
1210 | } |
||
1211 | |||
1212 | // UTC date |
||
1213 | $utcstart = $this->toGMT($this->tz, (int) $this->recur["start"]); |
||
1214 | $utcend = $this->toGMT($this->tz, (int) $this->recur["end"]); |
||
1215 | |||
1216 | // utc date+time |
||
1217 | $utcfirstoccstartdatetime = (isset($this->recur["startocc"])) ? $utcstart + (((int) $this->recur["startocc"]) * 60) : $utcstart; |
||
1218 | $utcfirstoccenddatetime = (isset($this->recur["endocc"])) ? $utcstart + (((int) $this->recur["endocc"]) * 60) : $utcstart; |
||
1219 | |||
1220 | $propsToSet = []; |
||
1221 | // update reminder time |
||
1222 | $propsToSet[$this->proptags["reminder_time"]] = $utcfirstoccstartdatetime; |
||
1223 | |||
1224 | // update first occurrence date |
||
1225 | $propsToSet[$this->proptags["startdate"]] = $propsToSet[$this->proptags["commonstart"]] = $utcfirstoccstartdatetime; |
||
1226 | $propsToSet[$this->proptags["duedate"]] = $propsToSet[$this->proptags["commonend"]] = $utcfirstoccenddatetime; |
||
1227 | |||
1228 | // Set Outlook properties, if it is an appointment |
||
1229 | if (isset($this->messageprops[$this->proptags["message_class"]]) && $this->messageprops[$this->proptags["message_class"]] == "IPM.Appointment") { |
||
1230 | // update real begin and real end date |
||
1231 | $propsToSet[$this->proptags["startdate_recurring"]] = $utcstart; |
||
1232 | $propsToSet[$this->proptags["enddate_recurring"]] = $utcend; |
||
1233 | |||
1234 | // recurrencetype |
||
1235 | // Strange enough is the property recurrencetype, (type-0x9) and not the CDO recurrencetype |
||
1236 | $propsToSet[$this->proptags["recurrencetype"]] = ((int) $this->recur["type"]) - 0x9; |
||
1237 | |||
1238 | // set named prop 'side_effects' to 369, needed for Outlook to ask for single or total recurrence when deleting |
||
1239 | $propsToSet[$this->proptags["side_effects"]] = 369; |
||
1240 | } |
||
1241 | else { |
||
1242 | $propsToSet[$this->proptags["side_effects"]] = 3441; |
||
1243 | } |
||
1244 | |||
1245 | // FlagDueBy is datetime of the first reminder occurrence. Outlook gives on this time a reminder popup dialog |
||
1246 | // Any change of the recurrence (including changing and deleting exceptions) causes the flagdueby to be reset |
||
1247 | // to the 'next' occurrence; this makes sure that deleting the next occurrence will correctly set the reminder to |
||
1248 | // the occurrence after that. The 'next' occurrence is defined as being the first occurrence that starts at moment X (server time) |
||
1249 | // with the reminder flag set. |
||
1250 | $reminderprops = mapi_getprops($this->message, [$this->proptags["reminder_minutes"], $this->proptags["flagdueby"]]); |
||
1251 | if (isset($reminderprops[$this->proptags["reminder_minutes"]])) { |
||
1252 | $occ = false; |
||
1253 | $occurrences = $this->getItems(time(), 0x7FF00000, 3, true); |
||
1254 | |||
1255 | for ($i = 0, $len = count($occurrences); $i < $len; ++$i) { |
||
1256 | // This will actually also give us appointments that have already started, but not yet ended. Since we want the next |
||
1257 | // reminder that occurs after time(), we may have to skip the first few entries. We get 3 entries since that is the maximum |
||
1258 | // number that would be needed (assuming reminder for item X cannot be before the previous occurrence starts). Worst case: |
||
1259 | // time() is currently after start but before end of item, but reminder of next item has already passed (reminder for next item |
||
1260 | // can be DURING the previous item, eg daily allday events). In that case, the first and second items must be skipped. |
||
1261 | |||
1262 | if (($occurrences[$i][$this->proptags["startdate"]] - $reminderprops[$this->proptags["reminder_minutes"]] * 60) > time()) { |
||
1263 | $occ = $occurrences[$i]; |
||
1264 | break; |
||
1265 | } |
||
1266 | } |
||
1267 | |||
1268 | if ($occ) { |
||
1269 | if (isset($reminderprops[$this->proptags["flagdueby"]])) { |
||
1270 | $propsToSet[$this->proptags["flagdueby"]] = $reminderprops[$this->proptags["flagdueby"]]; |
||
1271 | } |
||
1272 | else { |
||
1273 | $propsToSet[$this->proptags["flagdueby"]] = $occ[$this->proptags["startdate"]] - ($reminderprops[$this->proptags["reminder_minutes"]] * 60); |
||
1274 | } |
||
1275 | } |
||
1276 | else { |
||
1277 | // Last reminder passed, no reminders any more. |
||
1278 | $propsToSet[$this->proptags["reminder"]] = false; |
||
1279 | $propsToSet[$this->proptags["flagdueby"]] = 0x7FF00000; |
||
1280 | } |
||
1281 | } |
||
1282 | |||
1283 | // Default data |
||
1284 | // Second item (0x08) indicates the Outlook version (see documentation at the bottom of this file for more information) |
||
1285 | $rdata .= pack("VV", 0x3006, 0x3009); |
||
1286 | if (isset($this->recur["startocc"], $this->recur["endocc"])) { |
||
1287 | // Set start and endtime in minutes |
||
1288 | $rdata .= pack("VV", (int) $this->recur["startocc"], (int) $this->recur["endocc"]); |
||
1289 | } |
||
1290 | |||
1291 | // Detailed exception data |
||
1292 | |||
1293 | $changed_items = $this->recur["changed_occurrences"]; |
||
1294 | |||
1295 | $rdata .= pack("v", count($changed_items)); |
||
1296 | |||
1297 | foreach ($changed_items as $changed_item) { |
||
1298 | // Set start and end time of exception |
||
1299 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["start"])); // StartDateTime |
||
1300 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["end"])); // EndDateTime |
||
1301 | $rdata .= pack("V", $this->unixDataToRecurData( |
||
1302 | $this->dayStartOf($changed_item["basedate"]) + ((int) $this->recur["startocc"] ?? 0) * 60)); // OriginalStartDate |
||
1303 | |||
1304 | // Bitmask |
||
1305 | $bitmask = 0; |
||
1306 | |||
1307 | // Check for changed strings |
||
1308 | if (isset($changed_item["subject"])) { |
||
1309 | $bitmask |= 1 << 0; |
||
1310 | } |
||
1311 | |||
1312 | if (isset($changed_item["remind_before"])) { |
||
1313 | $bitmask |= 1 << 2; |
||
1314 | } |
||
1315 | |||
1316 | if (isset($changed_item["reminder_set"])) { |
||
1317 | $bitmask |= 1 << 3; |
||
1318 | } |
||
1319 | |||
1320 | if (isset($changed_item["location"])) { |
||
1321 | $bitmask |= 1 << 4; |
||
1322 | } |
||
1323 | |||
1324 | if (isset($changed_item["busystatus"])) { |
||
1325 | $bitmask |= 1 << 5; |
||
1326 | } |
||
1327 | |||
1328 | if (isset($changed_item["alldayevent"])) { |
||
1329 | $bitmask |= 1 << 7; |
||
1330 | } |
||
1331 | |||
1332 | if (isset($changed_item["label"])) { |
||
1333 | $bitmask |= 1 << 8; |
||
1334 | } |
||
1335 | |||
1336 | $rdata .= pack("v", $bitmask); |
||
1337 | |||
1338 | // Set "subject" |
||
1339 | if (isset($changed_item["subject"])) { |
||
1340 | // convert utf-8 to non-unicode blob string (us-ascii?) |
||
1341 | $subject = iconv("UTF-8", "windows-1252//TRANSLIT", $changed_item["subject"]); |
||
1342 | $length = strlen($subject); |
||
1343 | $rdata .= pack("vv", $length + 1, $length); |
||
1344 | $rdata .= pack("a" . $length, $subject); |
||
1345 | } |
||
1346 | |||
1347 | if (isset($changed_item["remind_before"])) { |
||
1348 | $rdata .= pack("V", $changed_item["remind_before"]); |
||
1349 | } |
||
1350 | |||
1351 | if (isset($changed_item["reminder_set"])) { |
||
1352 | $rdata .= pack("V", $changed_item["reminder_set"]); |
||
1353 | } |
||
1354 | |||
1355 | if (isset($changed_item["location"])) { |
||
1356 | $location = iconv("UTF-8", "windows-1252//TRANSLIT", $changed_item["location"]); |
||
1357 | $length = strlen($location); |
||
1358 | $rdata .= pack("vv", $length + 1, $length); |
||
1359 | $rdata .= pack("a" . $length, $location); |
||
1360 | } |
||
1361 | |||
1362 | if (isset($changed_item["busystatus"])) { |
||
1363 | $rdata .= pack("V", $changed_item["busystatus"]); |
||
1364 | } |
||
1365 | |||
1366 | if (isset($changed_item["alldayevent"])) { |
||
1367 | $rdata .= pack("V", $changed_item["alldayevent"]); |
||
1368 | } |
||
1369 | |||
1370 | if (isset($changed_item["label"])) { |
||
1371 | $rdata .= pack("V", $changed_item["label"]); |
||
1372 | } |
||
1373 | } |
||
1374 | |||
1375 | $rdata .= pack("V", 0); |
||
1376 | |||
1377 | // write extended data |
||
1378 | foreach ($changed_items as $changed_item) { |
||
1379 | $rdata .= pack("VVV", 4, 0, 0); // ChangeHighlightSize, ChangeHighlightValue, ReservedBlockEE1Size |
||
1380 | if (isset($changed_item["subject"]) || isset($changed_item["location"])) { |
||
1381 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["start"])); |
||
1382 | $rdata .= pack("V", $this->unixDataToRecurData($changed_item["end"])); |
||
1383 | $rdata .= pack("V", $this->unixDataToRecurData($this->dayStartOf($changed_item["basedate"]) + ((int) $this->recur["startocc"] ?? 0) * 60)); |
||
1384 | } |
||
1385 | |||
1386 | if (isset($changed_item["subject"])) { |
||
1387 | $subject = iconv("UTF-8", "UCS-2LE", $changed_item["subject"]); |
||
1388 | $length = iconv_strlen($subject, "UCS-2LE"); |
||
1389 | $rdata .= pack("v", $length); |
||
1390 | $rdata .= pack("a" . $length * 2, $subject); |
||
1391 | } |
||
1392 | |||
1393 | if (isset($changed_item["location"])) { |
||
1394 | $location = iconv("UTF-8", "UCS-2LE", $changed_item["location"]); |
||
1395 | $length = iconv_strlen($location, "UCS-2LE"); |
||
1396 | $rdata .= pack("v", $length); |
||
1397 | $rdata .= pack("a" . $length * 2, $location); |
||
1398 | } |
||
1399 | |||
1400 | if (isset($changed_item["subject"]) || isset($changed_item["location"])) { |
||
1401 | $rdata .= pack("V", 0); |
||
1402 | } |
||
1403 | } |
||
1404 | |||
1405 | $rdata .= pack("V", 0); // ReservedBlock2Size |
||
1406 | |||
1407 | // Set props |
||
1408 | $propsToSet[$this->proptags["recurring_data"]] = $rdata; |
||
1409 | $propsToSet[$this->proptags["recurring"]] = true; |
||
1410 | $propsToSet[$this->proptags["meetingrecurring"]] = true; |
||
1411 | if (isset($this->tz) && $this->tz) { |
||
1412 | $timezone = "GMT"; |
||
1413 | if ($this->tz["timezone"] != 0) { |
||
1414 | // Create user readable timezone information |
||
1415 | $timezone = sprintf( |
||
1416 | "(GMT %s%02d:%02d)", -$this->tz["timezone"] > 0 ? "+" : "-", |
||
1417 | abs($this->tz["timezone"] / 60), |
||
1418 | abs($this->tz["timezone"] % 60) |
||
1419 | ); |
||
1420 | } |
||
1421 | $propsToSet[$this->proptags["timezone_data"]] = $this->getTimezoneData($this->tz); |
||
1422 | $propsToSet[$this->proptags["timezone"]] = $timezone; |
||
1423 | } |
||
1424 | mapi_setprops($this->message, $propsToSet); |
||
1425 | } |
||
2090 |