-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.baserecurrence.php
2067 lines (1721 loc) · 66.2 KB
/
class.baserecurrence.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* SPDX-License-Identifier: AGPL-3.0-only
* SPDX-FileCopyrightText: Copyright 2005-2016 Zarafa Deutschland GmbH
* SPDX-FileCopyrightText: Copyright 2020-2024 grommunio GmbH
*/
/**
* BaseRecurrence
* this class is superclass for recurrence for appointments and tasks. This class provides all
* basic features of recurrence.
*/
abstract class BaseRecurrence {
/**
* @var resource Mapi Message Store (may be null if readonly)
*/
public $store;
/**
* @var mixed Mapi Message (may be null if readonly)
*/
public $message;
/**
* @var array Message Properties
*/
public $messageprops;
/**
* @var array list of property tags
*/
public $proptags;
/**
* @var mixed recurrence data of this calendar item
*/
public $recur;
/**
* @var mixed Timezone data of this calendar item
*/
public $tz;
/**
* Constructor.
*
* @param resource $store MAPI Message Store Object
* @param mixed $message the MAPI (appointment) message
*/
public function __construct($store, $message) {
$this->store = $store;
if (is_array($message)) {
$this->messageprops = $message;
}
else {
$this->message = $message;
$this->messageprops = mapi_getprops($this->message, $this->proptags);
}
if (isset($this->messageprops[$this->proptags["recurring_data"]])) {
// There is a possibility that recurr blob can be more than 255 bytes so get full blob through stream interface
if (strlen($this->messageprops[$this->proptags["recurring_data"]]) >= 255) {
$this->getFullRecurrenceBlob();
}
$this->recur = $this->parseRecurrence($this->messageprops[$this->proptags["recurring_data"]]);
}
if (isset($this->proptags["timezone_data"], $this->messageprops[$this->proptags["timezone_data"]])) {
$this->tz = $this->parseTimezone($this->messageprops[$this->proptags["timezone_data"]]);
}
}
public function getRecurrence() {
return $this->recur;
}
public function getFullRecurrenceBlob(): void {
$message = mapi_msgstore_openentry($this->store, $this->messageprops[PR_ENTRYID]);
$recurrBlob = '';
$stream = mapi_openproperty($message, $this->proptags["recurring_data"], IID_IStream, 0, 0);
$stat = mapi_stream_stat($stream);
for ($i = 0; $i < $stat['cb']; $i += 1024) {
$recurrBlob .= mapi_stream_read($stream, 1024);
}
if (!empty($recurrBlob)) {
$this->messageprops[$this->proptags["recurring_data"]] = $recurrBlob;
}
}
/**
* Function for parsing the Recurrence value of a Calendar item.
*
* Retrieve it from Named Property 0x8216 as a PT_BINARY and pass the
* data to this function
*
* Returns a structure containing the data:
*
* type - type of recurrence: day=10, week=11, month=12, year=13
* subtype - type of day recurrence: 2=monthday (ie 21st day of month), 3=nday'th weekdays (ie. 2nd Tuesday and Wednesday)
* start - unix timestamp of first occurrence
* end - unix timestamp of last occurrence (up to and including), so when start == end -> occurrences = 1
* numoccur - occurrences (may be very large when there is no end data)
*
* then, for each type:
*
* Daily:
* everyn - every [everyn] days in minutes
* regen - regenerating event (like tasks)
*
* Weekly:
* everyn - every [everyn] weeks in weeks
* regen - regenerating event (like tasks)
* weekdays - bitmask of week days, where each bit is one weekday (weekdays & 1 = Sunday, weekdays & 2 = Monday, etc)
*
* Monthly:
* everyn - every [everyn] months
* regen - regenerating event (like tasks)
*
* subtype 2:
* monthday - on day [monthday] of the month
*
* subtype 3:
* weekdays - bitmask of week days, where each bit is one weekday (weekdays & 1 = Sunday, weekdays & 2 = Monday, etc)
* nday - on [nday]'th [weekdays] of the month
*
* Yearly:
* everyn - every [everyn] months (12, 24, 36, ...)
* month - in month [month] (although the month is encoded in minutes since the startning of the year ........)
* regen - regenerating event (like tasks)
*
* subtype 2:
* monthday - on day [monthday] of the month
*
* subtype 3:
* weekdays - bitmask of week days, where each bit is one weekday (weekdays & 1 = Sunday, weekdays & 2 = Monday, etc)
* nday - on [nday]'th [weekdays] of the month [month]
*
* @param string $rdata Binary string
*
* @return null|(((false|int|mixed|string)[]|int)[]|int|mixed)[] recurrence data
*
* @psalm-return array{changed_occurrences: array<int, array{basedate: false|int, start: int, end: int, bitmask: mixed, subject?: false|string, remind_before?: mixed, reminder_set?: mixed, location?: false|string, busystatus?: mixed, alldayevent?: mixed, label?: mixed, ex_start_datetime?: mixed, ex_end_datetime?: mixed, ex_orig_date?: mixed}>, deleted_occurrences: list<int>, type?: int|mixed, subtype?: mixed, month?: mixed, everyn?: mixed, regen?: mixed, monthday?: mixed, weekdays?: 0|mixed, nday?: mixed, term?: int|mixed, numoccur?: mixed, numexcept?: mixed, numexceptmod?: mixed, start?: int, end?: int, startocc?: mixed, endocc?: mixed}|null
*/
public function parseRecurrence($rdata) {
if (strlen($rdata) < 10) {
return;
}
$ret = [];
$ret["changed_occurrences"] = [];
$ret["deleted_occurrences"] = [];
$data = unpack("vReaderVersion/vWriterVersion/vrtype/vrtype2/vCalendarType", $rdata);
// Do some recurrence validity checks
if ($data['ReaderVersion'] != 0x3004 || $data['WriterVersion'] != 0x3004) {
return $ret;
}
if (!in_array($data["rtype"], [IDC_RCEV_PAT_ORB_DAILY, IDC_RCEV_PAT_ORB_WEEKLY, IDC_RCEV_PAT_ORB_MONTHLY, IDC_RCEV_PAT_ORB_YEARLY])) {
return $ret;
}
if (!in_array($data["rtype2"], [rptDay, rptWeek, rptMonth, rptMonthNth, rptMonthEnd, rptHjMonth, rptHjMonthNth, rptHjMonthEnd])) {
return $ret;
}
if (!in_array($data['CalendarType'], [MAPI_CAL_DEFAULT, MAPI_CAL_GREGORIAN])) {
return $ret;
}
$ret["type"] = (int) $data["rtype"] > 0x2000 ? (int) $data["rtype"] - 0x2000 : $data["rtype"];
$ret["subtype"] = $data["rtype2"];
$rdata = substr($rdata, 10);
switch ($data["rtype"]) {
case IDC_RCEV_PAT_ORB_DAILY:
if (strlen($rdata) < 12) {
return $ret;
}
$data = unpack("Vunknown/Veveryn/Vregen", $rdata);
if ($data["everyn"] > 1438560) { // minutes for 999 days
return $ret;
}
$ret["everyn"] = $data["everyn"];
$ret["regen"] = $data["regen"];
switch ($ret["subtype"]) {
case rptDay:
$rdata = substr($rdata, 12);
break;
case rptWeek:
$rdata = substr($rdata, 16);
break;
}
break;
case IDC_RCEV_PAT_ORB_WEEKLY:
if (strlen($rdata) < 16) {
return $ret;
}
$data = unpack("Vconst1/Veveryn/Vregen", $rdata);
if ($data["everyn"] > 99) {
return $ret;
}
$rdata = substr($rdata, 12);
$ret["everyn"] = $data["everyn"];
$ret["regen"] = $data["regen"];
$ret["weekdays"] = 0;
if ($data["regen"] == 0) {
$data = unpack("Vweekdays", $rdata);
$rdata = substr($rdata, 4);
$ret["weekdays"] = $data["weekdays"];
}
break;
case IDC_RCEV_PAT_ORB_MONTHLY:
if (strlen($rdata) < 16) {
return $ret;
}
$data = unpack("Vconst1/Veveryn/Vregen/Vmonthday", $rdata);
if ($data["everyn"] > 99) {
return $ret;
}
$ret["everyn"] = $data["everyn"];
$ret["regen"] = $data["regen"];
if ($ret["subtype"] == rptMonthNth) {
$ret["weekdays"] = $data["monthday"];
}
else {
$ret["monthday"] = $data["monthday"];
}
$rdata = substr($rdata, 16);
if ($ret["subtype"] == rptMonthNth) {
$data = unpack("Vnday", $rdata);
$ret["nday"] = $data["nday"];
$rdata = substr($rdata, 4);
}
break;
case IDC_RCEV_PAT_ORB_YEARLY:
if (strlen($rdata) < 16) {
return $ret;
}
$data = unpack("Vmonth/Veveryn/Vregen/Vmonthday", $rdata);
// recurring yearly tasks have a period in months multiple by 12
if ($data['regen'] && $data["everyn"] % 12 != 0) {
return $ret;
}
if (!$data['regen'] && $data["everyn"] != 12) {
return $ret;
}
$ret["month"] = $data["month"];
$ret["everyn"] = $data["everyn"];
$ret["regen"] = $data["regen"];
if ($ret["subtype"] == rptMonthNth) {
$ret["weekdays"] = $data["monthday"];
}
else {
$ret["monthday"] = $data["monthday"];
}
$rdata = substr($rdata, 16);
if ($ret["subtype"] == rptMonthNth) {
$data = unpack("Vnday", $rdata);
$ret["nday"] = $data["nday"];
$rdata = substr($rdata, 4);
}
break;
}
if (strlen($rdata) < 16) {
return $ret;
}
$data = unpack("Vterm/Vnumoccur/Vconst2/Vnumexcept", $rdata);
$rdata = substr($rdata, 16);
if (!in_array($data["term"], [IDC_RCEV_PAT_ERB_END, IDC_RCEV_PAT_ERB_AFTERNOCCUR, IDC_RCEV_PAT_ERB_NOEND, 0xFFFFFFFF])) {
return $ret;
}
$ret["term"] = (int) $data["term"] > 0x2000 ? (int) $data["term"] - 0x2000 : $data["term"];
$ret["numoccur"] = $data["numoccur"];
$ret["numexcept"] = $data["numexcept"];
// exc_base_dates are *all* the base dates that have been either deleted or modified
$exc_base_dates = [];
for ($i = 0; $i < $ret["numexcept"]; ++$i) {
if (strlen($rdata) < 4) {
// We shouldn't arrive here, because that implies
// numexcept does not match the amount of data
// which is available for the exceptions.
return $ret;
}
$data = unpack("Vbasedate", $rdata);
$rdata = substr($rdata, 4);
$exc_base_dates[] = $this->recurDataToUnixData($data["basedate"]);
}
if (strlen($rdata) < 4) {
return $ret;
}
$data = unpack("Vnumexceptmod", $rdata);
$rdata = substr($rdata, 4);
$ret["numexceptmod"] = $data["numexceptmod"];
// exc_changed are the base dates of *modified* occurrences. exactly what is modified
// is in the attachments *and* in the data further down this function.
$exc_changed = [];
for ($i = 0; $i < $ret["numexceptmod"]; ++$i) {
if (strlen($rdata) < 4) {
// We shouldn't arrive here, because that implies
// numexceptmod does not match the amount of data
// which is available for the exceptions.
return $ret;
}
$data = unpack("Vstartdate", $rdata);
$rdata = substr($rdata, 4);
$exc_changed[] = $this->recurDataToUnixData($data["startdate"]);
}
if (strlen($rdata) < 8) {
return $ret;
}
$data = unpack("Vstart/Vend", $rdata);
$rdata = substr($rdata, 8);
$ret["start"] = $this->recurDataToUnixData($data["start"]);
$ret["end"] = $this->recurDataToUnixData($data["end"]);
// this is where task recurrence stop
if (strlen($rdata) < 16) {
return $ret;
}
$data = unpack("Vreaderversion/Vwriterversion/Vstartmin/Vendmin", $rdata);
$rdata = substr($rdata, 16);
$ret["startocc"] = $data["startmin"];
$ret["endocc"] = $data["endmin"];
$writerversion = $data["writerversion"];
$data = unpack("vnumber", $rdata);
$rdata = substr($rdata, 2);
$nexceptions = $data["number"];
$exc_changed_details = [];
// Parse n modified exceptions
for ($i = 0; $i < $nexceptions; ++$i) {
$item = [];
// Get exception startdate, enddate and basedate (the date at which the occurrence would have started)
$data = unpack("Vstartdate/Venddate/Vbasedate", $rdata);
$rdata = substr($rdata, 12);
// Convert recurtimestamp to unix timestamp
$startdate = $this->recurDataToUnixData($data["startdate"]);
$enddate = $this->recurDataToUnixData($data["enddate"]);
$basedate = $this->recurDataToUnixData($data["basedate"]);
// Set the right properties
$item["basedate"] = $this->dayStartOf($basedate);
$item["start"] = $startdate;
$item["end"] = $enddate;
$data = unpack("vbitmask", $rdata);
$rdata = substr($rdata, 2);
$item["bitmask"] = $data["bitmask"]; // save bitmask for extended exceptions
// Bitmask to verify what properties are changed
$bitmask = $data["bitmask"];
// ARO_SUBJECT: 0x0001
// Look for field: SubjectLength (2b), SubjectLength2 (2b) and Subject
if ($bitmask & (1 << 0)) {
$data = unpack("vnull_length/vlength", $rdata);
$rdata = substr($rdata, 4);
$length = $data["length"];
$item["subject"] = ""; // Normalized subject
for ($j = 0; $j < $length && strlen($rdata); ++$j) {
$data = unpack("Cchar", $rdata);
$rdata = substr($rdata, 1);
$item["subject"] .= chr($data["char"]);
}
}
// ARO_MEETINGTYPE: 0x0002
if ($bitmask & (1 << 1)) {
$rdata = substr($rdata, 4);
// Attendees modified: no data here (only in attachment)
}
// ARO_REMINDERDELTA: 0x0004
// Look for field: ReminderDelta (4b)
if ($bitmask & (1 << 2)) {
$data = unpack("Vremind_before", $rdata);
$rdata = substr($rdata, 4);
$item["remind_before"] = $data["remind_before"];
}
// ARO_REMINDER: 0x0008
// Look field: ReminderSet (4b)
if ($bitmask & (1 << 3)) {
$data = unpack("Vreminder_set", $rdata);
$rdata = substr($rdata, 4);
$item["reminder_set"] = $data["reminder_set"];
}
// ARO_LOCATION: 0x0010
// Look for fields: LocationLength (2b), LocationLength2 (2b) and Location
// Similar to ARO_SUBJECT above.
if ($bitmask & (1 << 4)) {
$data = unpack("vnull_length/vlength", $rdata);
$rdata = substr($rdata, 4);
$item["location"] = "";
$length = $data["length"];
$data = substr($rdata, 0, $length);
$rdata = substr($rdata, $length);
$item["location"] .= $data;
}
// ARO_BUSYSTATUS: 0x0020
// Look for field: BusyStatus (4b)
if ($bitmask & (1 << 5)) {
$data = unpack("Vbusystatus", $rdata);
$rdata = substr($rdata, 4);
$item["busystatus"] = $data["busystatus"];
}
// ARO_ATTACHMENT: 0x0040
if ($bitmask & (1 << 6)) {
// no data: RESERVED
$rdata = substr($rdata, 4);
}
// ARO_SUBTYPE: 0x0080
// Look for field: SubType (4b). Determines whether it is an allday event.
if ($bitmask & (1 << 7)) {
$data = unpack("Vallday", $rdata);
$rdata = substr($rdata, 4);
$item["alldayevent"] = $data["allday"];
}
// ARO_APPTCOLOR: 0x0100
// Look for field: AppointmentColor (4b)
if ($bitmask & (1 << 8)) {
$data = unpack("Vlabel", $rdata);
$rdata = substr($rdata, 4);
$item["label"] = $data["label"];
}
// ARO_EXCEPTIONAL_BODY: 0x0200
if ($bitmask & (1 << 9)) {
// Notes or Attachments modified: no data here (only in attachment)
}
array_push($exc_changed_details, $item);
}
/**
* We now have $exc_changed, $exc_base_dates and $exc_changed_details
* We will ignore $exc_changed, as this information is available in $exc_changed_details
* also. If an item is in $exc_base_dates and NOT in $exc_changed_details, then the item
* has been deleted.
*/
// Find deleted occurrences
$deleted_occurrences = [];
foreach ($exc_base_dates as $base_date) {
$found = false;
foreach ($exc_changed_details as $details) {
if ($details["basedate"] == $base_date) {
$found = true;
break;
}
}
if (!$found) {
// item was not in exc_changed_details, so it must be deleted
$deleted_occurrences[] = $base_date;
}
}
$ret["deleted_occurrences"] = $deleted_occurrences;
$ret["changed_occurrences"] = $exc_changed_details;
// enough data for normal exception (no extended data)
if (strlen($rdata) < 8) {
return $ret;
}
$data = unpack("Vreservedsize", $rdata);
$rdata = substr($rdata, 4 + $data["reservedsize"]);
for ($i = 0; $i < $nexceptions; ++$i) {
// subject and location in ucs-2 to utf-8
if ($writerversion >= 0x3009) {
$data = unpack("Vsize/Vvalue", $rdata); // size includes sizeof(value)==4
$rdata = substr($rdata, 4 + $data["size"]);
}
$data = unpack("Vreservedsize", $rdata);
$rdata = substr($rdata, 4 + $data["reservedsize"]);
// ARO_SUBJECT(0x01) | ARO_LOCATION(0x10)
if ($exc_changed_details[$i]["bitmask"] & 0x11) {
$data = unpack("Vstart/Vend/Vorig", $rdata);
$rdata = substr($rdata, 4 * 3);
$exc_changed_details[$i]["ex_start_datetime"] = $data["start"];
$exc_changed_details[$i]["ex_end_datetime"] = $data["end"];
$exc_changed_details[$i]["ex_orig_date"] = $data["orig"];
}
// ARO_SUBJECT
if ($exc_changed_details[$i]["bitmask"] & 0x01) {
// decode ucs2 string to utf-8
$data = unpack("vlength", $rdata);
$rdata = substr($rdata, 2);
$length = $data["length"];
$data = substr($rdata, 0, $length * 2);
$rdata = substr($rdata, $length * 2);
$subject = iconv("UCS-2LE", "UTF-8", $data);
// replace subject with unicode subject
$exc_changed_details[$i]["subject"] = $subject;
}
// ARO_LOCATION
if ($exc_changed_details[$i]["bitmask"] & 0x10) {
// decode ucs2 string to utf-8
$data = unpack("vlength", $rdata);
$rdata = substr($rdata, 2);
$length = $data["length"];
$data = substr($rdata, 0, $length * 2);
$rdata = substr($rdata, $length * 2);
$location = iconv("UCS-2LE", "UTF-8", $data);
// replace subject with unicode subject
$exc_changed_details[$i]["location"] = $location;
}
// ARO_SUBJECT(0x01) | ARO_LOCATION(0x10)
if ($exc_changed_details[$i]["bitmask"] & 0x11) {
$data = unpack("Vreservedsize", $rdata);
$rdata = substr($rdata, 4 + $data["reservedsize"]);
}
}
// update with extended data
$ret["changed_occurrences"] = $exc_changed_details;
return $ret;
}
/**
* Saves the recurrence data to the recurrence property.
*/
public function saveRecurrence(): void {
// Only save if a message was passed
if (!isset($this->message)) {
return;
}
// Abort if no recurrence was set
if (!isset(
$this->recur["type"],
$this->recur["subtype"],
$this->recur["start"],
$this->recur["end"],
$this->recur["startocc"],
$this->recur["endocc"])
) {
return;
}
$rtype = 0x2000 + (int) $this->recur["type"];
// Don't allow invalid type and subtype values
if (!in_array($rtype, [IDC_RCEV_PAT_ORB_DAILY, IDC_RCEV_PAT_ORB_WEEKLY, IDC_RCEV_PAT_ORB_MONTHLY, IDC_RCEV_PAT_ORB_YEARLY])) {
return;
}
if (!in_array((int) $this->recur["subtype"], [rptDay, rptWeek, rptMonth, rptMonthNth, rptMonthEnd, rptHjMonth, rptHjMonthNth, rptHjMonthEnd])) {
return;
}
$rdata = pack("vvvvv", 0x3004, 0x3004, $rtype, (int) $this->recur["subtype"], MAPI_CAL_DEFAULT);
$weekstart = 1; // monday
$forwardcount = 0;
$count = 0;
$restocc = 0;
$dayofweek = (int) gmdate("w", (int) $this->recur["start"]); // 0 (for Sunday) through 6 (for Saturday)
// Terminate
$term = (int) $this->recur["term"] < 0x2000 ? 0x2000 + (int) $this->recur["term"] : (int) $this->recur["term"];
switch ($rtype) {
case IDC_RCEV_PAT_ORB_DAILY:
if (!isset($this->recur["everyn"]) || (int) $this->recur["everyn"] > 1438560 || (int) $this->recur["everyn"] < 0) { // minutes for 999 days
return;
}
if ($this->recur["subtype"] == rptWeek) {
// Daily every workday
$rdata .= pack("VVVV", 6 * 24 * 60, 1, 0, 0x3E);
}
else {
// Calc first occ
$firstocc = $this->unixDataToRecurData($this->recur["start"]) % ((int) $this->recur["everyn"]);
$rdata .= pack("VVV", $firstocc, (int) $this->recur["everyn"], $this->recur["regen"] ? 1 : 0);
}
break;
case IDC_RCEV_PAT_ORB_WEEKLY:
if (!isset($this->recur["everyn"]) || $this->recur["everyn"] > 99 || (int) $this->recur["everyn"] < 0) {
return;
}
if (!$this->recur["regen"] && !isset($this->recur["weekdays"])) {
return;
}
// No need to calculate startdate if sliding flag was set.
if (!$this->recur['regen']) {
// Calculate start date of recurrence
// Find the first day that matches one of the weekdays selected
$daycount = 0;
$dayskip = -1;
for ($j = 0; $j < 7; ++$j) {
if (((int) $this->recur["weekdays"]) & (1 << (($dayofweek + $j) % 7))) {
if ($dayskip == -1) {
$dayskip = $j;
}
++$daycount;
}
}
// $dayskip is the number of days to skip from the startdate until the first occurrence
// $daycount is the number of days per week that an occurrence occurs
$weekskip = 0;
if (($dayofweek < $weekstart && $dayskip > 0) || ($dayofweek + $dayskip) > 6) {
$weekskip = 1;
}
// Check if the recurrence ends after a number of occurrences, in that case we must calculate the
// remaining occurrences based on the start of the recurrence.
if ($term == IDC_RCEV_PAT_ERB_AFTERNOCCUR) {
// $weekskip is the amount of weeks to skip from the startdate before the first occurrence
// $forwardcount is the maximum number of week occurrences we can go ahead after the first occurrence that
// is still inside the recurrence. We subtract one to make sure that the last week is never forwarded over
// (eg when numoccur = 2, and daycount = 1)
$forwardcount = floor((int) ($this->recur["numoccur"] - 1) / $daycount);
// $restocc is the number of occurrences left after $forwardcount whole weeks of occurrences, minus one
// for the occurrence on the first day
$restocc = ((int) $this->recur["numoccur"]) - ($forwardcount * $daycount) - 1;
// $forwardcount is now the number of weeks we can go forward and still be inside the recurrence
$forwardcount *= (int) $this->recur["everyn"];
}
// The real start is start + dayskip + weekskip-1 (since dayskip will already bring us into the next week)
$this->recur["start"] = ((int) $this->recur["start"]) + ($dayskip * 24 * 60 * 60) + ($weekskip * (((int) $this->recur["everyn"]) - 1) * 7 * 24 * 60 * 60);
}
// Calc first occ
$firstocc = $this->unixDataToRecurData($this->recur["start"]) % (((int) $this->recur["everyn"]) * 7 * 24 * 60);
$firstocc -= (((int) gmdate("w", (int) $this->recur["start"])) - 1) * 24 * 60;
if ($this->recur["regen"]) {
$rdata .= pack("VVV", $firstocc, (int) $this->recur["everyn"], 1);
}
else {
$rdata .= pack("VVVV", $firstocc, (int) $this->recur["everyn"], 0, (int) $this->recur["weekdays"]);
}
break;
case IDC_RCEV_PAT_ORB_MONTHLY:
case IDC_RCEV_PAT_ORB_YEARLY:
if (!isset($this->recur["everyn"])) {
return;
}
if ($rtype == IDC_RCEV_PAT_ORB_YEARLY && !isset($this->recur["month"])) {
return;
}
if ($rtype == IDC_RCEV_PAT_ORB_MONTHLY) {
$everyn = (int) $this->recur["everyn"];
if ($everyn > 99 || $everyn < 0) {
return;
}
}
else {
$everyn = $this->recur["regen"] ? ((int) $this->recur["everyn"]) * 12 : 12;
}
// Get montday/month/year of original start
$curmonthday = gmdate("j", (int) $this->recur["start"]);
$curyear = gmdate("Y", (int) $this->recur["start"]);
$curmonth = gmdate("n", (int) $this->recur["start"]);
// Check if the recurrence ends after a number of occurrences, in that case we must calculate the
// remaining occurrences based on the start of the recurrence.
if ($term == IDC_RCEV_PAT_ERB_AFTERNOCCUR) {
// $forwardcount is the number of occurrences we can skip and still be inside the recurrence range (minus
// one to make sure there are always at least one occurrence left)
$forwardcount = ((((int) $this->recur["numoccur"]) - 1) * $everyn);
}
// Get month for yearly on D'th day of month M
if ($rtype == IDC_RCEV_PAT_ORB_YEARLY) {
$selmonth = floor(((int) $this->recur["month"]) / (24 * 60 * 29)) + 1; // 1=jan, 2=feb, eg
}
switch ((int) $this->recur["subtype"]) {
// on D day of every M month
case rptMonth:
if (!isset($this->recur["monthday"])) {
return;
}
// Recalc startdate
// Set on the right begin day
// Go the beginning of the month
$this->recur["start"] -= ($curmonthday - 1) * 24 * 60 * 60;
// Go the the correct month day
$this->recur["start"] += (((int) $this->recur["monthday"]) - 1) * 24 * 60 * 60;
// If the previous calculation gave us a start date different than the original start date, then we need to skip to the first occurrence
if (($rtype == IDC_RCEV_PAT_ORB_MONTHLY && ((int) $this->recur["monthday"]) < $curmonthday) ||
($rtype == IDC_RCEV_PAT_ORB_YEARLY && ($selmonth != $curmonth || ($selmonth == $curmonth && ((int) $this->recur["monthday"]) < $curmonthday)))) {
if ($rtype == IDC_RCEV_PAT_ORB_YEARLY) {
if ($curmonth > $selmonth) {// go to next occurrence in 'everyn' months minus difference in first occurrence and original date
$count = $everyn - ($curmonth - $selmonth);
}
elseif ($curmonth < $selmonth) {// go to next occurrence upto difference in first occurrence and original date
$count = $selmonth - $curmonth;
}
else {
// Go to next occurrence while recurrence start date is greater than occurrence date but within same month
if (((int) $this->recur["monthday"]) < $curmonthday) {
$count = $everyn;
}
}
}
else {
$count = $everyn; // Monthly, go to next occurrence in 'everyn' months
}
// Forward by $count months. This is done by getting the number of days in that month and forwarding that many days
for ($i = 0; $i < $count; ++$i) {
$this->recur["start"] += $this->getMonthInSeconds($curyear, $curmonth);
if ($curmonth == 12) {
++$curyear;
$curmonth = 0;
}
++$curmonth;
}
}
// "start" is now pointing to the first occurrence, except that it will overshoot if the
// month in which it occurs has less days than specified as the day of the month. So 31st
// of each month will overshoot in february (29 days). We compensate for that by checking
// if the day of the month we got is wrong, and then back up to the last day of the previous
// month.
if (((int) $this->recur["monthday"]) >= 28 && ((int) $this->recur["monthday"]) <= 31 &&
gmdate("j", (int) $this->recur["start"]) < ((int) $this->recur["monthday"])) {
$this->recur["start"] -= gmdate("j", (int) $this->recur["start"]) * 24 * 60 * 60;
}
// "start" is now the first occurrence
if ($rtype == IDC_RCEV_PAT_ORB_MONTHLY) {
// Calc first occ
$monthIndex = ((((12 % $everyn) * ((((int) gmdate("Y", $this->recur["start"])) - 1601) % $everyn)) % $everyn) + (((int) gmdate("n", $this->recur["start"])) - 1)) % $everyn;
$firstocc = 0;
for ($i = 0; $i < $monthIndex; ++$i) {
$firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), ($i % 12) + 1) / 60;
}
$rdata .= pack("VVVV", $firstocc, $everyn, $this->recur["regen"], (int) $this->recur["monthday"]);
}
else {
// Calc first occ
$firstocc = 0;
$monthIndex = (int) gmdate("n", $this->recur["start"]);
for ($i = 1; $i < $monthIndex; ++$i) {
$firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), $i) / 60;
}
$rdata .= pack("VVVV", $firstocc, $everyn, $this->recur["regen"], (int) $this->recur["monthday"]);
}
break;
case rptMonthNth:
// monthly: on Nth weekday of every M month
// yearly: on Nth weekday of M month
if (!isset($this->recur["weekdays"], $this->recur["nday"])) {
return;
}
$weekdays = (int) $this->recur["weekdays"];
$nday = (int) $this->recur["nday"];
// Calc startdate
$monthbegindow = (int) $this->recur["start"];
if ($nday == 5) {
// Set date on the last day of the last month
$monthbegindow += (gmdate("t", $monthbegindow) - gmdate("j", $monthbegindow)) * 24 * 60 * 60;
}
else {
// Set on the first day of the month
$monthbegindow -= ((gmdate("j", $monthbegindow) - 1) * 24 * 60 * 60);
}
if ($rtype == IDC_RCEV_PAT_ORB_YEARLY) {
// Set on right month
if ($selmonth < $curmonth) {
$tmp = 12 - $curmonth + $selmonth;
}
else {
$tmp = ($selmonth - $curmonth);
}
for ($i = 0; $i < $tmp; ++$i) {
$monthbegindow += $this->getMonthInSeconds($curyear, $curmonth);
if ($curmonth == 12) {
++$curyear;
$curmonth = 0;
}
++$curmonth;
}
}
else {
// Check or you exist in the right month
$dayofweek = gmdate("w", $monthbegindow);
for ($i = 0; $i < 7; ++$i) {
if ($nday == 5 && (($dayofweek - $i) % 7 >= 0) && (1 << (($dayofweek - $i) % 7)) & $weekdays) {
$day = gmdate("j", $monthbegindow) - $i;
break;
}
if ($nday != 5 && (1 << (($dayofweek + $i) % 7)) & $weekdays) {
$day = (($nday - 1) * 7) + ($i + 1);
break;
}
}
// Goto the next X month
if (isset($day) && ($day < gmdate("j", (int) $this->recur["start"]))) {
if ($nday == 5) {
$monthbegindow += 24 * 60 * 60;
if ($curmonth == 12) {
++$curyear;
$curmonth = 0;
}
++$curmonth;
}
for ($i = 0; $i < $everyn; ++$i) {
$monthbegindow += $this->getMonthInSeconds($curyear, $curmonth);
if ($curmonth == 12) {
++$curyear;
$curmonth = 0;
}
++$curmonth;
}
if ($nday == 5) {
$monthbegindow -= 24 * 60 * 60;
}
}
}
// FIXME: weekstart?
$day = 0;
// Set start on the right day
$dayofweek = gmdate("w", $monthbegindow);
for ($i = 0; $i < 7; ++$i) {
if ($nday == 5 && (($dayofweek - $i) % 7) >= 0 && (1 << (($dayofweek - $i) % 7)) & $weekdays) {
$day = $i;
break;
}
if ($nday != 5 && (1 << (($dayofweek + $i) % 7)) & $weekdays) {
$day = ($nday - 1) * 7 + ($i + 1);
break;
}
}
if ($nday == 5) {
$monthbegindow -= $day * 24 * 60 * 60;
}
else {
$monthbegindow += ($day - 1) * 24 * 60 * 60;
}
$firstocc = 0;
if ($rtype == IDC_RCEV_PAT_ORB_MONTHLY) {
// Calc first occ
$monthIndex = ((((12 % $everyn) * (((int) gmdate("Y", $this->recur["start"]) - 1601) % $everyn)) % $everyn) + (((int) gmdate("n", $this->recur["start"])) - 1)) % $everyn;
for ($i = 0; $i < $monthIndex; ++$i) {
$firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), ($i % 12) + 1) / 60;
}
$rdata .= pack("VVVVV", $firstocc, $everyn, 0, $weekdays, $nday);
}
else {
// Calc first occ
$monthIndex = (int) gmdate("n", $this->recur["start"]);
for ($i = 1; $i < $monthIndex; ++$i) {
$firstocc += $this->getMonthInSeconds(1601 + floor($i / 12), $i) / 60;
}
$rdata .= pack("VVVVV", $firstocc, $everyn, 0, $weekdays, $nday);
}
break;
}
break;
}
if (!isset($this->recur["term"])) {
return;
}
$rdata .= pack("V", $term);
switch ($term) {
// After the given enddate
case IDC_RCEV_PAT_ERB_END:
$rdata .= pack("V", 10);
break;
// After a number of times
case IDC_RCEV_PAT_ERB_AFTERNOCCUR:
if (!isset($this->recur["numoccur"])) {
return;
}
$rdata .= pack("V", (int) $this->recur["numoccur"]);
break;
// Never ends
case IDC_RCEV_PAT_ERB_NOEND:
$rdata .= pack("V", 0);
break;
}
// Strange little thing for the recurrence type "every workday"
if ($rtype == IDC_RCEV_PAT_ORB_WEEKLY && ((int) $this->recur["subtype"]) == 1) {
$rdata .= pack("V", 1);
}
else { // Other recurrences
$rdata .= pack("V", 0);
}