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