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