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