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