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