forked from grommunio/mapi-header-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.recurrence.php
More file actions
1782 lines (1509 loc) · 63.6 KB
/
class.recurrence.php
File metadata and controls
1782 lines (1509 loc) · 63.6 KB
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
*/
/**
* Recurrence.
*/
class Recurrence extends BaseRecurrence {
/*
* ABOUT TIMEZONES
*
* Timezones are rather complicated here so here are some rules to think about:
*
* - Timestamps in mapi-like properties (so in PT_SYSTIME properties) are always in GMT (including
* the 'basedate' property in exceptions !!)
* - Timestamps for recurrence (so start/end of recurrence, and basedates for exceptions (everything
* outside the 'basedate' property in the exception !!), and start/endtimes for exceptions) are
* always in LOCAL time.
*/
// All properties for a recipient that are interesting
public $recipprops = [
PR_ENTRYID,
PR_SEARCH_KEY,
PR_DISPLAY_NAME,
PR_EMAIL_ADDRESS,
PR_RECIPIENT_ENTRYID,
PR_RECIPIENT_TYPE,
PR_SEND_INTERNET_ENCODING,
PR_SEND_RICH_INFO,
PR_RECIPIENT_DISPLAY_NAME,
PR_ADDRTYPE,
PR_DISPLAY_TYPE,
PR_DISPLAY_TYPE_EX,
PR_RECIPIENT_TRACKSTATUS,
PR_RECIPIENT_TRACKSTATUS_TIME,
PR_RECIPIENT_FLAGS,
PR_ROWID,
];
/**
* Constructor.
*
* @param resource $store MAPI Message Store Object
* @param mixed $message the MAPI (appointment) message
* @param array $proptags an associative array of protags and their values
*/
public function __construct($store, $message, $proptags = []) {
if (!empty($proptags)) {
$this->proptags = $proptags;
}
else {
$properties = [];
$properties["entryid"] = PR_ENTRYID;
$properties["parent_entryid"] = PR_PARENT_ENTRYID;
$properties["message_class"] = PR_MESSAGE_CLASS;
$properties["icon_index"] = PR_ICON_INDEX;
$properties["subject"] = PR_SUBJECT;
$properties["display_to"] = PR_DISPLAY_TO;
$properties["importance"] = PR_IMPORTANCE;
$properties["sensitivity"] = PR_SENSITIVITY;
$properties["startdate"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidAppointmentStartWhole;
$properties["duedate"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidAppointmentEndWhole;
$properties["recurring"] = "PT_BOOLEAN:PSETID_Appointment:" . PidLidRecurring;
$properties["recurring_data"] = "PT_BINARY:PSETID_Appointment:" . PidLidAppointmentRecur;
$properties["busystatus"] = "PT_LONG:PSETID_Appointment:" . PidLidBusyStatus;
$properties["label"] = "PT_LONG:PSETID_Appointment:0x8214";
$properties["alldayevent"] = "PT_BOOLEAN:PSETID_Appointment:" . PidLidAppointmentSubType;
$properties["private"] = "PT_BOOLEAN:PSETID_Common:" . PidLidPrivate;
$properties["meeting"] = "PT_LONG:PSETID_Appointment:" . PidLidAppointmentStateFlags;
$properties["startdate_recurring"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidClipStart;
$properties["enddate_recurring"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidClipEnd;
$properties["recurring_pattern"] = "PT_STRING8:PSETID_Appointment:" . PidLidRecurrencePattern;
$properties["location"] = "PT_STRING8:PSETID_Appointment:" . PidLidLocation;
$properties["duration"] = "PT_LONG:PSETID_Appointment:" . PidLidAppointmentDuration;
$properties["responsestatus"] = "PT_LONG:PSETID_Appointment:0x8218";
$properties["reminder"] = "PT_BOOLEAN:PSETID_Common:" . PidLidReminderSet;
$properties["reminder_minutes"] = "PT_LONG:PSETID_Common:" . PidLidReminderDelta;
$properties["recurrencetype"] = "PT_LONG:PSETID_Appointment:0x8231";
$properties["contacts"] = "PT_MV_STRING8:PSETID_Common:0x853a";
$properties["contacts_string"] = "PT_STRING8:PSETID_Common:0x8586";
$properties["categories"] = "PT_MV_STRING8:PS_PUBLIC_STRINGS:Keywords";
$properties["reminder_time"] = "PT_SYSTIME:PSETID_Common:" . PidLidReminderTime;
$properties["commonstart"] = "PT_SYSTIME:PSETID_Common:0x8516";
$properties["commonend"] = "PT_SYSTIME:PSETID_Common:0x8517";
$properties["basedate"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidExceptionReplaceTime;
$properties["timezone_data"] = "PT_BINARY:PSETID_Appointment:" . PidLidTimeZoneStruct;
$properties["timezone"] = "PT_STRING8:PSETID_Appointment:" . PidLidTimeZoneDescription;
$properties["flagdueby"] = "PT_SYSTIME:PSETID_Common:" . PidLidReminderSignalTime;
$properties["side_effects"] = "PT_LONG:PSETID_Common:0x8510";
$properties["hideattachments"] = "PT_BOOLEAN:PSETID_Common:" . PidLidSmartNoAttach;
$this->proptags = getPropIdsFromStrings($store, $properties);
}
parent::__construct($store, $message);
}
/**
* Create an exception.
*
* @param array $exception_props the exception properties (same properties as normal recurring items)
* @param mixed $base_date the base date of the exception (LOCAL time of non-exception occurrence)
* @param bool $delete true - delete occurrence, false - create new exception or modify existing
* @param array $exception_recips list of recipients
* @param mixed $copy_attach_from mapi message from which attachments should be copied
*/
public function createException($exception_props, $base_date, $delete = false, $exception_recips = [], $copy_attach_from = false): bool {
$baseday = $this->dayStartOf($base_date);
$basetime = $baseday + $this->recur["startocc"] * 60;
// Remove any pre-existing exception on this base date
if ($this->isException($baseday)) {
$this->deleteException($baseday); // note that deleting an exception is different from creating a deleted exception (deleting an occurrence).
}
if (!$delete) {
if (isset($exception_props[$this->proptags["startdate"]]) && !$this->isValidExceptionDate($base_date, $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]))) {
return false;
}
// Properties in the attachment are the properties of the base object, plus $exception_props plus the base date
foreach (["subject", "location", "label", "reminder", "reminder_minutes", "alldayevent", "busystatus"] as $propname) {
if (isset($this->messageprops[$this->proptags[$propname]])) {
$props[$this->proptags[$propname]] = $this->messageprops[$this->proptags[$propname]];
}
}
$props[PR_MESSAGE_CLASS] = "IPM.OLE.CLASS.{00061055-0000-0000-C000-000000000046}";
unset($exception_props[PR_MESSAGE_CLASS], $exception_props[PR_ICON_INDEX]);
$props = $exception_props + $props;
// Basedate in the exception attachment is the GMT time at which the original occurrence would have been
$props[$this->proptags["basedate"]] = $this->toGMT($this->tz, $basetime);
if (!isset($exception_props[$this->proptags["startdate"]])) {
$props[$this->proptags["startdate"]] = $this->getOccurrenceStart($base_date);
}
if (!isset($exception_props[$this->proptags["duedate"]])) {
$props[$this->proptags["duedate"]] = $this->getOccurrenceEnd($base_date);
}
// synchronize commonstart/commonend with startdate/duedate
if (isset($props[$this->proptags["startdate"]])) {
$props[$this->proptags["commonstart"]] = $props[$this->proptags["startdate"]];
}
if (isset($props[$this->proptags["duedate"]])) {
$props[$this->proptags["commonend"]] = $props[$this->proptags["duedate"]];
}
// Save the data into an attachment
$this->createExceptionAttachment($props, $exception_recips, $copy_attach_from);
$changed_item = [];
$changed_item["basedate"] = $basetime;
$changed_item["start"] = $this->fromGMT($this->tz, $props[$this->proptags["startdate"]]);
$changed_item["end"] = $this->fromGMT($this->tz, $props[$this->proptags["duedate"]]);
if (array_key_exists($this->proptags["subject"], $exception_props)) {
$changed_item["subject"] = $exception_props[$this->proptags["subject"]];
}
if (array_key_exists($this->proptags["location"], $exception_props)) {
$changed_item["location"] = $exception_props[$this->proptags["location"]];
}
if (array_key_exists($this->proptags["label"], $exception_props)) {
$changed_item["label"] = $exception_props[$this->proptags["label"]];
}
if (array_key_exists($this->proptags["reminder"], $exception_props)) {
$changed_item["reminder_set"] = $exception_props[$this->proptags["reminder"]];
}
if (array_key_exists($this->proptags["reminder_minutes"], $exception_props)) {
$changed_item["remind_before"] = $exception_props[$this->proptags["reminder_minutes"]];
}
if (array_key_exists($this->proptags["alldayevent"], $exception_props)) {
$changed_item["alldayevent"] = $exception_props[$this->proptags["alldayevent"]];
}
if (array_key_exists($this->proptags["busystatus"], $exception_props)) {
$changed_item["busystatus"] = $exception_props[$this->proptags["busystatus"]];
}
// Add the changed occurrence to the list
array_push($this->recur["changed_occurrences"], $changed_item);
}
else {
// Delete the occurrence by placing it in the deleted occurrences list
array_push($this->recur["deleted_occurrences"], $baseday);
}
// Turn on hideattachments, because the attachments in this item are the exceptions
mapi_setprops($this->message, [$this->proptags["hideattachments"] => true]);
// Save recurrence data to message
$this->saveRecurrence();
return true;
}
/**
* Modifies an existing exception, but only updates the given properties
* NOTE: You can't remove properties from an exception, only add new ones.
*
* @param mixed $exception_props
* @param mixed $base_date
* @param mixed $exception_recips
* @param mixed $copy_attach_from
*/
public function modifyException($exception_props, $base_date, $exception_recips = [], $copy_attach_from = false): bool {
if (isset($exception_props[$this->proptags["startdate"]]) && !$this->isValidExceptionDate($base_date, $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]))) {
return false;
}
$baseday = $this->dayStartOf($base_date);
$extomodify = false;
for ($i = 0, $len = count($this->recur["changed_occurrences"]); $i < $len; ++$i) {
if ($this->isSameDay($this->recur["changed_occurrences"][$i]["basedate"], $baseday)) {
$extomodify = &$this->recur["changed_occurrences"][$i];
}
}
if (!$extomodify) {
return false;
}
// remove basedate property as we want to preserve the old value
// client will send basedate with time part as zero, so discard that value
unset($exception_props[$this->proptags["basedate"]]);
if (array_key_exists($this->proptags["startdate"], $exception_props)) {
$extomodify["start"] = $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]);
}
if (array_key_exists($this->proptags["duedate"], $exception_props)) {
$extomodify["end"] = $this->fromGMT($this->tz, $exception_props[$this->proptags["duedate"]]);
}
if (array_key_exists($this->proptags["subject"], $exception_props)) {
$extomodify["subject"] = $exception_props[$this->proptags["subject"]];
}
if (array_key_exists($this->proptags["location"], $exception_props)) {
$extomodify["location"] = $exception_props[$this->proptags["location"]];
}
if (array_key_exists($this->proptags["label"], $exception_props)) {
$extomodify["label"] = $exception_props[$this->proptags["label"]];
}
if (array_key_exists($this->proptags["reminder"], $exception_props)) {
$extomodify["reminder_set"] = $exception_props[$this->proptags["reminder"]];
}
if (array_key_exists($this->proptags["reminder_minutes"], $exception_props)) {
$extomodify["remind_before"] = $exception_props[$this->proptags["reminder_minutes"]];
}
if (array_key_exists($this->proptags["alldayevent"], $exception_props)) {
$extomodify["alldayevent"] = $exception_props[$this->proptags["alldayevent"]];
}
if (array_key_exists($this->proptags["busystatus"], $exception_props)) {
$extomodify["busystatus"] = $exception_props[$this->proptags["busystatus"]];
}
$exception_props[PR_MESSAGE_CLASS] = "IPM.OLE.CLASS.{00061055-0000-0000-C000-000000000046}";
// synchronize commonstart/commonend with startdate/duedate
if (isset($exception_props[$this->proptags["startdate"]])) {
$exception_props[$this->proptags["commonstart"]] = $exception_props[$this->proptags["startdate"]];
}
if (isset($exception_props[$this->proptags["duedate"]])) {
$exception_props[$this->proptags["commonend"]] = $exception_props[$this->proptags["duedate"]];
}
$attach = $this->getExceptionAttachment($baseday);
if (!$attach) {
if ($copy_attach_from) {
$this->deleteExceptionAttachment($base_date);
$this->createException($exception_props, $base_date, false, $exception_recips, $copy_attach_from);
}
else {
$this->createExceptionAttachment($exception_props, $exception_recips, $copy_attach_from);
}
}
else {
$message = mapi_attach_openobj($attach, MAPI_MODIFY);
// Set exception properties on embedded message and save
mapi_setprops($message, $exception_props);
$this->setExceptionRecipients($message, $exception_recips, false);
mapi_savechanges($message);
// If a new start or duedate is provided, we update the properties 'PR_EXCEPTION_STARTTIME' and 'PR_EXCEPTION_ENDTIME'
// on the attachment which holds the embedded msg and save everything.
$props = [];
if (isset($exception_props[$this->proptags["startdate"]])) {
$props[PR_EXCEPTION_STARTTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]);
}
if (isset($exception_props[$this->proptags["duedate"]])) {
$props[PR_EXCEPTION_ENDTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["duedate"]]);
}
if (!empty($props)) {
mapi_setprops($attach, $props);
}
mapi_savechanges($attach);
}
// Save recurrence data to message
$this->saveRecurrence();
return true;
}
// Checks to see if the following is true:
// 1) The exception to be created doesn't create two exceptions starting on one day (however, they can END on the same day by modifying duration)
// 2) The exception to be created doesn't 'jump' over another occurrence (which may be an exception itself!)
//
// Both $basedate and $start are in LOCAL time
public function isValidExceptionDate($basedate, $start): bool {
// The way we do this is to look at the days that we're 'moving' the item in the exception. Each
// of these days may only contain the item that we're modifying. Any other item violates the rules.
if ($this->isException($basedate)) {
// If we're modifying an exception, we want to look at the days that we're 'moving' compared to where
// the exception used to be.
$oldexception = $this->getChangeException($basedate);
$prevday = $this->dayStartOf($oldexception["start"]);
}
else {
// If its a new exception, we want to look at the original placement of this item.
$prevday = $basedate;
}
$startday = $this->dayStartOf($start);
// Get all the occurrences on the days between the basedate (may be reversed)
if ($prevday < $startday) {
$items = $this->getItems($this->toGMT($this->tz, $prevday), $this->toGMT($this->tz, $startday + 24 * 60 * 60));
}
else {
$items = $this->getItems($this->toGMT($this->tz, $startday), $this->toGMT($this->tz, $prevday + 24 * 60 * 60));
}
// There should now be exactly one item, namely the item that we are modifying. If there are any other items in the range,
// then we abort the change, since one of the rules has been violated.
return count($items) == 1;
}
/**
* Check to see if the exception proposed at a certain basedate is allowed concerning reminder times:.
*
* Both must be true:
* - reminder time of this item is not before the starttime of the previous recurring item
* - reminder time of the next item is not before the starttime of this item
*
* @param date $basedate the base date of the exception (LOCAL time of non-exception occurrence)
* @param string $reminderminutes reminder minutes which is set of the item
* @param date $startdate the startdate of the selected item
*
* @returns boolean if the reminder minutes value valid (FALSE if either of the rules above are FALSE)
*/
public function isValidReminderTime($basedate, $reminderminutes, $startdate): bool {
// get all occurrence items before the selected items occurrence starttime
$occitems = $this->getItems($this->messageprops[$this->proptags["startdate"]], $this->toGMT($this->tz, $basedate));
if (!empty($occitems)) {
// as occitems array is sorted in ascending order of startdate, to get the previous occurrence we take the last items in occitems .
$previousitem_startdate = $occitems[count($occitems) - 1][$this->proptags["startdate"]];
// if our reminder is set before or equal to the beginning of the previous occurrence, then that's not allowed
if ($startdate - ($reminderminutes * 60) <= $previousitem_startdate) {
return false;
}
}
// Get the endtime of the current occurrence and find the next two occurrences (including the current occurrence)
$currentOcc = $this->getItems($this->toGMT($this->tz, $basedate), 0x7FF00000, 2, true);
// If there are another two occurrences, then the first is the current occurrence, and the one after that
// is the next occurrence.
if (count($currentOcc) > 1) {
$next = $currentOcc[1];
// Get reminder time of the next occurrence.
$nextOccReminderTime = $next[$this->proptags["startdate"]] - ($next[$this->proptags["reminder_minutes"]] * 60);
// If the reminder time of the next item is before the start of this item, then that's not allowed
if ($nextOccReminderTime <= $startdate) {
return false;
}
}
// All was ok
return true;
}
public function setRecurrence($tz, $recur): void {
// only reset timezone if specified
if ($tz) {
$this->tz = $tz;
}
$this->recur = $recur;
if (!isset($this->recur["changed_occurrences"])) {
$this->recur["changed_occurrences"] = [];
}
if (!isset($this->recur["deleted_occurrences"])) {
$this->recur["deleted_occurrences"] = [];
}
$this->deleteAttachments();
$this->saveRecurrence();
// if client has not set the recurring_pattern then we should generate it and save it
$messageProps = mapi_getprops($this->message, [$this->proptags["recurring_pattern"]]);
if (empty($messageProps[$this->proptags["recurring_pattern"]])) {
$this->saveRecurrencePattern();
}
}
// Returns the start or end time of the occurrence on the given base date.
// This assumes that the basedate you supply is in LOCAL time
public function getOccurrenceStart($basedate) {
$daystart = $this->dayStartOf($basedate);
return $this->toGMT($this->tz, $daystart + $this->recur["startocc"] * 60);
}
public function getOccurrenceEnd($basedate) {
$daystart = $this->dayStartOf($basedate);
return $this->toGMT($this->tz, $daystart + $this->recur["endocc"] * 60);
}
/**
* This function returns the next remindertime starting from $timestamp
* When no next reminder exists, false is returned.
*
* Note: Before saving this new reminder time (when snoozing), you must check for
* yourself if this reminder time is earlier than your snooze time, else
* use your snooze time and not this reminder time.
*
* @param mixed $timestamp
*/
public function getNextReminderTime($timestamp) {
/**
* Get next item from now until forever, but max 1 item with reminder set
* Note 0x7ff00000 instead of 0x7fffffff because of possible overflow failures when converting to GMT....
* Here for getting next 10 occurrences assuming that next here we will be able to find
* nextreminder occurrence in 10 occurrences.
*/
$items = $this->getItems($timestamp, 0x7FF00000, 10, true);
// Initially setting nextreminder to false so when no next reminder exists, false is returned.
$nextreminder = false;
/*
* Loop through all reminder which we get in items variable
* and check whether the remindertime is greater than timestamp.
* On the first occurrence of greater nextreminder break the loop
* and return the value to calling function.
*/
for ($i = 0, $len = count($items); $i < $len; ++$i) {
$item = $items[$i];
$tempnextreminder = $item[$this->proptags["startdate"]] - ($item[$this->proptags["reminder_minutes"]] * 60);
// If tempnextreminder is greater than timestamp then save it in nextreminder and break from the loop.
if ($tempnextreminder > $timestamp) {
$nextreminder = $tempnextreminder;
break;
}
}
return $nextreminder;
}
/**
* Note: Static function, more like a utility function.
*
* Gets all the items (including recurring items) in the specified calendar in the given timeframe. Items are
* included as a whole if they overlap the interval <$start, $end> (non-inclusive). This means that if the interval
* is <08:00 - 14:00>, the item [6:00 - 8:00> is NOT included, nor is the item [14:00 - 16:00>. However, the item
* [7:00 - 9:00> is included as a whole, and is NOT capped to [8:00 - 9:00>.
*
* @param $store resource The store in which the calendar resides
* @param $calendar resource The calendar to get the items from
* @param $viewstart int Timestamp of beginning of view window
* @param $viewend int Timestamp of end of view window
* @param $propsrequested array Array of properties to return
*
* @psalm-param list{0: mixed, 1: mixed, 2?: mixed} $propsrequested
*/
public static function getCalendarItems($store, $calendar, $viewstart, $viewend, $propsrequested) {
return getCalendarItems($store, $calendar, $viewstart, $viewend, $propsrequested);
}
/*
* CODE BELOW THIS LINE IS FOR INTERNAL USE ONLY
*****************************************************************************************************************
*/
/**
* Returns langified daily recurrence type string, whether it's singular or plural,
* recurrence interval.
*/
public function getI18RecTypeDaily(mixed $type, mixed $interval, bool $occSingleDayRank): array {
switch ($interval) {
case 1: // workdays
$type = dgettext('zarafa', 'workday');
$occSingleDayRank = true;
break;
case 1440: // daily
$type = dgettext('zarafa', 'day');
$occSingleDayRank = true;
break;
default: // every $interval days
$interval /= 1440;
$type = dgettext('zarafa', 'days');
$occSingleDayRank = false;
break;
}
return [
'type' => $type,
'interval' => $interval,
'occSingleDayRank' => boolval($occSingleDayRank),
];
}
/**
* Returns langified weekly recurrence type string, whether it's singular or plural,
* recurrence interval.
*/
public function getI18RecTypeWeekly(mixed $type, mixed $interval, bool $occSingleDayRank): array {
if ($interval == 1) {
$type = dgettext('zarafa', 'week');
$occSingleDayRank = true;
}
else {
$type = dgettext('zarafa', 'weeks');
$occSingleDayRank = false;
}
$daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
$type .= sprintf(" %s ", dgettext('zarafa', 'on'));
for ($j = 0, $weekdays = (int) $this->recur["weekdays"]; $j < 7; ++$j) {
if ($weekdays & (1 << $j)) {
$type .= sprintf("%s, ", dgettext('zarafa', $daysOfWeek[$j]));
}
}
$type = trim($type, ", ");
if (($pos = strrpos($type, ",")) !== false) {
$type = substr_replace($type, " " . dgettext('zarafa', 'and'), $pos, 1);
}
return [
'type' => $type,
'interval' => $interval,
'occSingleDayRank' => boolval($occSingleDayRank),
];
}
/**
* Returns langified monthly recurrence type string, whether it's singular or plural,
* recurrence interval.
*/
public function getI18RecTypeMonthly(mixed $type, mixed $interval, bool $occSingleDayRank): array {
if ($interval == 1) {
$type = dgettext('zarafa', 'month');
$occSingleDayRank = true;
}
else {
$type = dgettext('zarafa', 'months');
$occSingleDayRank = false;
}
return [
'type' => $type,
'interval' => $interval,
'occSingleDayRank' => boolval($occSingleDayRank),
];
}
/**
* Returns langified yearly recurrence type string, whether it's singular or plural,
* recurrence interval.
*/
public function getI18RecTypeYearly(mixed $type, mixed $interval, bool $occSingleDayRank): array {
if ($interval <= 12) {
$interval = 1;
$type = dgettext('zarafa', 'year');
$occSingleDayRank = true;
}
else {
$interval = $interval / 12;
$type = dgettext('zarafa', 'years');
$occSingleDayRank = false;
}
return [
'type' => $type,
'interval' => $interval,
'occSingleDayRank' => boolval($occSingleDayRank),
];
}
/**
* Returns langified recurrence type string, whether it's singular or plural,
* recurrence interval.
*/
public function getI18nRecurrenceType(): array {
$type = $this->recur['type'];
$interval = $this->recur['everyn'];
$occSingleDayRank = false;
return match ($type) {
// Daily
0x0A => $this->getI18RecTypeDaily($type, $interval, $occSingleDayRank),
// Weekly
0x0B => $this->getI18RecTypeWeekly($type, $interval, $occSingleDayRank),
// Monthly
0x0C => $this->getI18RecTypeMonthly($type, $interval, $occSingleDayRank),
// Yearly
0x0D => $this->getI18RecTypeYearly($type, $interval, $occSingleDayRank),
default => [
'type' => $type,
'interval' => $interval,
'occSingleDayRank' => boolval($occSingleDayRank),
],
};
}
/**
* Returns the start or end time of the first occurrence.
*/
public function getOccDate(bool $getStart = true): mixed {
return $getStart ?
(isset($this->recur['startocc']) ?
$this->recur['start'] + (((int) $this->recur['startocc']) * 60) :
$this->recur['start']) :
(isset($this->recur['endocc']) ?
$this->recur['start'] + (((int) $this->recur['endocc']) * 60) :
$this->recur['end']);
}
/**
* Returns langified occurrence time.
*/
public function getI18nTime(string $format, mixed $occTime): string {
return gmdate(dgettext('zarafa', $format), $occTime);
}
/**
* Returns langified recurrence pattern termination after the given date.
*/
public function getI18nRecTermDate(
bool $occTimeRange,
bool $occSingleDayRank,
mixed $type,
mixed $interval,
string $start,
string $end,
string $startocc,
string $endocc
): string {
return $occTimeRange ?
(
$occSingleDayRank ?
sprintf(
dgettext('zarafa', 'Occurs every %s effective %s until %s from %s to %s.'),
$type,
$start,
$end,
$startocc,
$endocc
) :
sprintf(
dgettext('zarafa', 'Occurs every %s %s effective %s until %s from %s to %s.'),
$interval,
$type,
$start,
$end,
$startocc,
$endocc
)
) :
(
$occSingleDayRank ?
sprintf(
dgettext('zarafa', 'Occurs every %s effective %s until %s.'),
$type,
$start,
$end
) :
sprintf(
dgettext('zarafa', 'Occurs every %s %s effective %s until %s.'),
$interval,
$type,
$start,
$end
)
);
}
/**
* Returns langified recurrence pattern termination after a number of
* occurrences.
*/
public function getI18nRecTermNrOcc(
bool $occTimeRange,
bool $occSingleDayRank,
mixed $type,
mixed $interval,
string $start,
mixed $numocc,
string $startocc,
string $endocc
): string {
return $occTimeRange ?
(
$occSingleDayRank ?
sprintf(
dngettext(
'zarafa',
'Occurs every %s effective %s for %s occurrence from %s to %s.',
'Occurs every %s effective %s for %s occurrences from %s to %s.',
$numocc
),
$type,
$start,
$numocc,
$startocc,
$endocc
) :
sprintf(
dngettext(
'zarafa',
'Occurs every %s %s effective %s for %s occurrence from %s to %s.',
'Occurs every %s %s effective %s for %s occurrences %s to %s.',
$numocc
),
$interval,
$type,
$start,
$numocc,
$startocc,
$endocc
)
) :
(
$occSingleDayRank ?
sprintf(
dngettext(
'zarafa',
'Occurs every %s effective %s for %s occurrence.',
'Occurs every %s effective %s for %s occurrences.',
$numocc
),
$type,
$start,
$numocc
) :
sprintf(
dngettext(
'zarafa',
'Occurs every %s %s effective %s for %s occurrence.',
'Occurs every %s %s effective %s for %s occurrences.',
$numocc
),
$interval,
$type,
$start,
$numocc
)
);
}
/**
* Returns langified recurrence pattern termination with no end date.
*/
public function getI18nRecTermNoEnd(
bool $occTimeRange,
bool $occSingleDayRank,
mixed $type,
mixed $interval,
string $start,
string $startocc,
string $endocc
): string {
return $occTimeRange ?
(
$occSingleDayRank ?
sprintf(
dgettext('zarafa', 'Occurs every %s effective %s from %s to %s.'),
$type,
$start,
$startocc,
$endocc
) :
sprintf(
dgettext('zarafa', 'Occurs every %s %s effective %s from %s to %s.'),
$interval,
$type,
$start,
$startocc,
$endocc
)
) :
(
$occSingleDayRank ?
sprintf(
dgettext('zarafa', 'Occurs every %s effective %s.'),
$type,
$start
) :
sprintf(
dgettext('zarafa', 'Occurs every %s %s effective %s.'),
$interval,
$type,
$start
)
);
}
/**
* Generates and stores recurrence pattern string to recurring_pattern property.
*/
public function saveRecurrencePattern(): string {
// Start formatting the properties in such a way we can apply
// them directly into the recurrence pattern.
$pattern = '';
$occTimeRange = $this->recur['startocc'] != 0 && $this->recur['endocc'] != 0;
[
'type' => $type,
'interval' => $interval,
'occSingleDayRank' => $occSingleDayRank,
] = $this->getI18nRecurrenceType();
// get timings of the first occurrence
$firstoccstartdate = $this->getOccDate();
$firstoccenddate = $this->getOccDate(false);
$start = $this->getI18nTime('d-m-Y', $firstoccstartdate);
$end = $this->getI18nTime('d-m-Y', $firstoccenddate);
$startocc = $this->getI18nTime('G:i', $firstoccstartdate);
$endocc = $this->getI18nTime('G:i', $firstoccenddate);
// Based on the properties, we need to generate the recurrence pattern string.
// This is obviously very easy since we can simply concatenate a bunch of strings,
// however this messes up translations for languages which order their words
// differently.
// To improve translation quality we create a series of default strings, in which
// we only have to fill in the correct variables. The base string is thus selected
// based on the available properties.
switch ($this->recur['term']) {
case 0x21: // After the given enddate
$pattern = $this->getI18nRecTermDate(
$occTimeRange,
boolval($occSingleDayRank),
$type,
$interval,
$start,
$end,
$startocc,
$endocc
);
break;
case 0x22: // After a number of times
$pattern = $this->getI18nRecTermNrOcc(
$occTimeRange,
boolval($occSingleDayRank),
$type,
$interval,
$start,
$this->recur['numoccur'] ?? 0,
$startocc,
$endocc
);
break;
case 0x23: // Never ends
$pattern = $this->getI18nRecTermNoEnd(
$occTimeRange,
boolval($occSingleDayRank),
$type,
$interval,
$start,
$startocc,
$endocc
);
break;
default:
error_log(sprintf("Invalid recurrence pattern termination %d", $this->recur['term']));
break;
}
if (!empty($pattern)) {
mapi_setprops($this->message, [$this->proptags["recurring_pattern"] => $pattern]);
}
return $pattern;
}
/*
* Remove an exception by base_date. This is the base date in local daystart time
*/
/**
* @param false|int $base_date
*/
public function deleteException($base_date): void {
// Remove all exceptions on $base_date from the deleted and changed occurrences lists
// Remove all items in $todelete from deleted_occurrences
$new = [];
foreach ($this->recur["deleted_occurrences"] as $entry) {
if ($entry != $base_date) {
$new[] = $entry;
}
}
$this->recur["deleted_occurrences"] = $new;
$new = [];
foreach ($this->recur["changed_occurrences"] as $entry) {
if (!$this->isSameDay($entry["basedate"], $base_date)) {
$new[] = $entry;
}
else {
$this->deleteExceptionAttachment($this->toGMT($this->tz, $base_date + $this->recur["startocc"] * 60));
}
}
$this->recur["changed_occurrences"] = $new;
}
/**
* Function which saves the exception data in an attachment.
*
* @param array $exception_props the exception data (like any other MAPI appointment)
* @param array $exception_recips list of recipients
* @param mapi_message $copy_attach_from mapi message from which attachments should be copied
*/
public function createExceptionAttachment($exception_props, $exception_recips = [], $copy_attach_from = false): void {
// Create new attachment.
$attachment = mapi_message_createattach($this->message);
$props = [];
$props[PR_ATTACHMENT_FLAGS] = 2;
$props[PR_ATTACHMENT_HIDDEN] = true;
$props[PR_ATTACHMENT_LINKID] = 0;
$props[PR_ATTACH_FLAGS] = 0;
$props[PR_ATTACH_METHOD] = ATTACH_EMBEDDED_MSG;
$props[PR_DISPLAY_NAME] = "Exception";
$props[PR_EXCEPTION_STARTTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]);
$props[PR_EXCEPTION_ENDTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["duedate"]]);
mapi_setprops($attachment, $props);
$imessage = mapi_attach_openobj($attachment, MAPI_CREATE | MAPI_MODIFY);
if ($copy_attach_from) {
$attachmentTable = mapi_message_getattachmenttable($copy_attach_from);
if ($attachmentTable) {
$attachments = mapi_table_queryallrows($attachmentTable, [PR_ATTACH_NUM, PR_ATTACH_SIZE, PR_ATTACH_LONG_FILENAME, PR_ATTACHMENT_HIDDEN, PR_DISPLAY_NAME, PR_ATTACH_METHOD]);
foreach ($attachments as $attach_props) {
$attach_old = mapi_message_openattach($copy_attach_from, (int) $attach_props[PR_ATTACH_NUM]);
$attach_newResourceMsg = mapi_message_createattach($imessage);
mapi_copyto($attach_old, [], [], $attach_newResourceMsg, 0);
mapi_savechanges($attach_newResourceMsg);
}
}
}
$props = $props + $exception_props;
// FIXME: the following piece of code is written to fix the creation
// of an exception. This is only a quickfix as it is not yet possible
// to change an existing exception.
// remove mv properties when needed
foreach ($props as $propTag => $propVal) {
if ((mapi_prop_type($propTag) & MV_FLAG) == MV_FLAG && is_null($propVal)) {