Total Complexity | 191 |
Total Lines | 1458 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like Recurrence often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Recurrence, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Recurrence extends BaseRecurrence { |
||
13 | /* |
||
14 | * ABOUT TIMEZONES |
||
15 | * |
||
16 | * Timezones are rather complicated here so here are some rules to think about: |
||
17 | * |
||
18 | * - Timestamps in mapi-like properties (so in PT_SYSTIME properties) are always in GMT (including |
||
19 | * the 'basedate' property in exceptions !!) |
||
20 | * - Timestamps for recurrence (so start/end of recurrence, and basedates for exceptions (everything |
||
21 | * outside the 'basedate' property in the exception !!), and start/endtimes for exceptions) are |
||
22 | * always in LOCAL time. |
||
23 | */ |
||
24 | |||
25 | // All properties for a recipient that are interesting |
||
26 | public $recipprops = [ |
||
27 | PR_ENTRYID, |
||
28 | PR_SEARCH_KEY, |
||
29 | PR_DISPLAY_NAME, |
||
30 | PR_EMAIL_ADDRESS, |
||
31 | PR_RECIPIENT_ENTRYID, |
||
32 | PR_RECIPIENT_TYPE, |
||
33 | PR_SEND_INTERNET_ENCODING, |
||
34 | PR_SEND_RICH_INFO, |
||
35 | PR_RECIPIENT_DISPLAY_NAME, |
||
36 | PR_ADDRTYPE, |
||
37 | PR_DISPLAY_TYPE, |
||
38 | PR_DISPLAY_TYPE_EX, |
||
39 | PR_RECIPIENT_TRACKSTATUS, |
||
40 | PR_RECIPIENT_TRACKSTATUS_TIME, |
||
41 | PR_RECIPIENT_FLAGS, |
||
42 | PR_ROWID, |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * Constructor. |
||
47 | * |
||
48 | * @param resource $store MAPI Message Store Object |
||
49 | * @param mixed $message the MAPI (appointment) message |
||
50 | * @param array $proptags an associative array of protags and their values |
||
51 | */ |
||
52 | public function __construct($store, $message, $proptags = []) { |
||
53 | if (!empty($proptags)) { |
||
54 | $this->proptags = $proptags; |
||
55 | } |
||
56 | else { |
||
57 | $properties = []; |
||
58 | $properties["entryid"] = PR_ENTRYID; |
||
59 | $properties["parent_entryid"] = PR_PARENT_ENTRYID; |
||
60 | $properties["message_class"] = PR_MESSAGE_CLASS; |
||
61 | $properties["icon_index"] = PR_ICON_INDEX; |
||
62 | $properties["subject"] = PR_SUBJECT; |
||
63 | $properties["display_to"] = PR_DISPLAY_TO; |
||
64 | $properties["importance"] = PR_IMPORTANCE; |
||
65 | $properties["sensitivity"] = PR_SENSITIVITY; |
||
66 | $properties["startdate"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidAppointmentStartWhole; |
||
67 | $properties["duedate"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidAppointmentEndWhole; |
||
68 | $properties["recurring"] = "PT_BOOLEAN:PSETID_Appointment:" . PidLidRecurring; |
||
69 | $properties["recurring_data"] = "PT_BINARY:PSETID_Appointment:" . PidLidAppointmentRecur; |
||
70 | $properties["busystatus"] = "PT_LONG:PSETID_Appointment:" . PidLidBusyStatus; |
||
71 | $properties["label"] = "PT_LONG:PSETID_Appointment:0x8214"; |
||
72 | $properties["alldayevent"] = "PT_BOOLEAN:PSETID_Appointment:" . PidLidAppointmentSubType; |
||
73 | $properties["private"] = "PT_BOOLEAN:PSETID_Common:" . PidLidPrivate; |
||
74 | $properties["meeting"] = "PT_LONG:PSETID_Appointment:" . PidLidAppointmentStateFlags; |
||
75 | $properties["startdate_recurring"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidClipStart; |
||
76 | $properties["enddate_recurring"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidClipEnd; |
||
77 | $properties["recurring_pattern"] = "PT_STRING8:PSETID_Appointment:" . PidLidRecurrencePattern; |
||
78 | $properties["location"] = "PT_STRING8:PSETID_Appointment:" . PidLidLocation; |
||
79 | $properties["duration"] = "PT_LONG:PSETID_Appointment:" . PidLidAppointmentDuration; |
||
80 | $properties["responsestatus"] = "PT_LONG:PSETID_Appointment:0x8218"; |
||
81 | $properties["reminder"] = "PT_BOOLEAN:PSETID_Common:" . PidLidReminderSet; |
||
82 | $properties["reminder_minutes"] = "PT_LONG:PSETID_Common:" . PidLidReminderDelta; |
||
83 | $properties["recurrencetype"] = "PT_LONG:PSETID_Appointment:0x8231"; |
||
84 | $properties["contacts"] = "PT_MV_STRING8:PSETID_Common:0x853a"; |
||
85 | $properties["contacts_string"] = "PT_STRING8:PSETID_Common:0x8586"; |
||
86 | $properties["categories"] = "PT_MV_STRING8:PS_PUBLIC_STRINGS:Keywords"; |
||
87 | $properties["reminder_time"] = "PT_SYSTIME:PSETID_Common:" . PidLidReminderTime; |
||
88 | $properties["commonstart"] = "PT_SYSTIME:PSETID_Common:0x8516"; |
||
89 | $properties["commonend"] = "PT_SYSTIME:PSETID_Common:0x8517"; |
||
90 | $properties["basedate"] = "PT_SYSTIME:PSETID_Appointment:" . PidLidExceptionReplaceTime; |
||
91 | $properties["timezone_data"] = "PT_BINARY:PSETID_Appointment:" . PidLidTimeZoneStruct; |
||
92 | $properties["timezone"] = "PT_STRING8:PSETID_Appointment:" . PidLidTimeZoneDescription; |
||
93 | $properties["flagdueby"] = "PT_SYSTIME:PSETID_Common:" . PidLidReminderSignalTime; |
||
94 | $properties["side_effects"] = "PT_LONG:PSETID_Common:0x8510"; |
||
95 | $properties["hideattachments"] = "PT_BOOLEAN:PSETID_Common:" . PidLidSmartNoAttach; |
||
96 | |||
97 | $this->proptags = getPropIdsFromStrings($store, $properties); |
||
98 | } |
||
99 | |||
100 | parent::__construct($store, $message); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Create an exception. |
||
105 | * |
||
106 | * @param array $exception_props the exception properties (same properties as normal recurring items) |
||
107 | * @param mixed $base_date the base date of the exception (LOCAL time of non-exception occurrence) |
||
108 | * @param bool $delete true - delete occurrence, false - create new exception or modify existing |
||
109 | * @param array $exception_recips list of recipients |
||
110 | * @param mixed $copy_attach_from mapi message from which attachments should be copied |
||
111 | */ |
||
112 | public function createException($exception_props, $base_date, $delete = false, $exception_recips = [], $copy_attach_from = false): bool { |
||
113 | $baseday = $this->dayStartOf($base_date); |
||
114 | $basetime = $baseday + $this->recur["startocc"] * 60; |
||
115 | |||
116 | // Remove any pre-existing exception on this base date |
||
117 | if ($this->isException($baseday)) { |
||
118 | $this->deleteException($baseday); // note that deleting an exception is different from creating a deleted exception (deleting an occurrence). |
||
119 | } |
||
120 | |||
121 | if (!$delete) { |
||
122 | if (isset($exception_props[$this->proptags["startdate"]]) && !$this->isValidExceptionDate($base_date, $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]))) { |
||
123 | return false; |
||
124 | } |
||
125 | // Properties in the attachment are the properties of the base object, plus $exception_props plus the base date |
||
126 | foreach (["subject", "location", "label", "reminder", "reminder_minutes", "alldayevent", "busystatus"] as $propname) { |
||
127 | if (isset($this->messageprops[$this->proptags[$propname]])) { |
||
128 | $props[$this->proptags[$propname]] = $this->messageprops[$this->proptags[$propname]]; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | $props[PR_MESSAGE_CLASS] = "IPM.OLE.CLASS.{00061055-0000-0000-C000-000000000046}"; |
||
133 | unset($exception_props[PR_MESSAGE_CLASS], $exception_props[PR_ICON_INDEX]); |
||
134 | |||
135 | $props = $exception_props + $props; |
||
136 | |||
137 | // Basedate in the exception attachment is the GMT time at which the original occurrence would have been |
||
138 | $props[$this->proptags["basedate"]] = $this->toGMT($this->tz, $basetime); |
||
139 | |||
140 | if (!isset($exception_props[$this->proptags["startdate"]])) { |
||
141 | $props[$this->proptags["startdate"]] = $this->getOccurrenceStart($base_date); |
||
142 | } |
||
143 | |||
144 | if (!isset($exception_props[$this->proptags["duedate"]])) { |
||
145 | $props[$this->proptags["duedate"]] = $this->getOccurrenceEnd($base_date); |
||
146 | } |
||
147 | |||
148 | // synchronize commonstart/commonend with startdate/duedate |
||
149 | if (isset($props[$this->proptags["startdate"]])) { |
||
150 | $props[$this->proptags["commonstart"]] = $props[$this->proptags["startdate"]]; |
||
151 | } |
||
152 | |||
153 | if (isset($props[$this->proptags["duedate"]])) { |
||
154 | $props[$this->proptags["commonend"]] = $props[$this->proptags["duedate"]]; |
||
155 | } |
||
156 | |||
157 | // Save the data into an attachment |
||
158 | $this->createExceptionAttachment($props, $exception_recips, $copy_attach_from); |
||
|
|||
159 | |||
160 | $changed_item = []; |
||
161 | |||
162 | $changed_item["basedate"] = $basetime; |
||
163 | $changed_item["start"] = $this->fromGMT($this->tz, $props[$this->proptags["startdate"]]); |
||
164 | $changed_item["end"] = $this->fromGMT($this->tz, $props[$this->proptags["duedate"]]); |
||
165 | |||
166 | if (array_key_exists($this->proptags["subject"], $exception_props)) { |
||
167 | $changed_item["subject"] = $exception_props[$this->proptags["subject"]]; |
||
168 | } |
||
169 | |||
170 | if (array_key_exists($this->proptags["location"], $exception_props)) { |
||
171 | $changed_item["location"] = $exception_props[$this->proptags["location"]]; |
||
172 | } |
||
173 | |||
174 | if (array_key_exists($this->proptags["label"], $exception_props)) { |
||
175 | $changed_item["label"] = $exception_props[$this->proptags["label"]]; |
||
176 | } |
||
177 | |||
178 | if (array_key_exists($this->proptags["reminder"], $exception_props)) { |
||
179 | $changed_item["reminder_set"] = $exception_props[$this->proptags["reminder"]]; |
||
180 | } |
||
181 | |||
182 | if (array_key_exists($this->proptags["reminder_minutes"], $exception_props)) { |
||
183 | $changed_item["remind_before"] = $exception_props[$this->proptags["reminder_minutes"]]; |
||
184 | } |
||
185 | |||
186 | if (array_key_exists($this->proptags["alldayevent"], $exception_props)) { |
||
187 | $changed_item["alldayevent"] = $exception_props[$this->proptags["alldayevent"]]; |
||
188 | } |
||
189 | |||
190 | if (array_key_exists($this->proptags["busystatus"], $exception_props)) { |
||
191 | $changed_item["busystatus"] = $exception_props[$this->proptags["busystatus"]]; |
||
192 | } |
||
193 | |||
194 | // Add the changed occurrence to the list |
||
195 | array_push($this->recur["changed_occurrences"], $changed_item); |
||
196 | } |
||
197 | else { |
||
198 | // Delete the occurrence by placing it in the deleted occurrences list |
||
199 | array_push($this->recur["deleted_occurrences"], $baseday); |
||
200 | } |
||
201 | |||
202 | // Turn on hideattachments, because the attachments in this item are the exceptions |
||
203 | mapi_setprops($this->message, [$this->proptags["hideattachments"] => true]); |
||
204 | |||
205 | // Save recurrence data to message |
||
206 | $this->saveRecurrence(); |
||
207 | |||
208 | return true; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Modifies an existing exception, but only updates the given properties |
||
213 | * NOTE: You can't remove properties from an exception, only add new ones. |
||
214 | * |
||
215 | * @param mixed $exception_props |
||
216 | * @param mixed $base_date |
||
217 | * @param mixed $exception_recips |
||
218 | * @param mixed $copy_attach_from |
||
219 | */ |
||
220 | public function modifyException($exception_props, $base_date, $exception_recips = [], $copy_attach_from = false): bool { |
||
221 | if (isset($exception_props[$this->proptags["startdate"]]) && !$this->isValidExceptionDate($base_date, $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]))) { |
||
222 | return false; |
||
223 | } |
||
224 | |||
225 | $baseday = $this->dayStartOf($base_date); |
||
226 | $extomodify = false; |
||
227 | |||
228 | for ($i = 0, $len = count($this->recur["changed_occurrences"]); $i < $len; ++$i) { |
||
229 | if ($this->isSameDay($this->recur["changed_occurrences"][$i]["basedate"], $baseday)) { |
||
230 | $extomodify = &$this->recur["changed_occurrences"][$i]; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | if (!$extomodify) { |
||
235 | return false; |
||
236 | } |
||
237 | |||
238 | // remove basedate property as we want to preserve the old value |
||
239 | // client will send basedate with time part as zero, so discard that value |
||
240 | unset($exception_props[$this->proptags["basedate"]]); |
||
241 | |||
242 | if (array_key_exists($this->proptags["startdate"], $exception_props)) { |
||
243 | $extomodify["start"] = $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]); |
||
244 | } |
||
245 | |||
246 | if (array_key_exists($this->proptags["duedate"], $exception_props)) { |
||
247 | $extomodify["end"] = $this->fromGMT($this->tz, $exception_props[$this->proptags["duedate"]]); |
||
248 | } |
||
249 | |||
250 | if (array_key_exists($this->proptags["subject"], $exception_props)) { |
||
251 | $extomodify["subject"] = $exception_props[$this->proptags["subject"]]; |
||
252 | } |
||
253 | |||
254 | if (array_key_exists($this->proptags["location"], $exception_props)) { |
||
255 | $extomodify["location"] = $exception_props[$this->proptags["location"]]; |
||
256 | } |
||
257 | |||
258 | if (array_key_exists($this->proptags["label"], $exception_props)) { |
||
259 | $extomodify["label"] = $exception_props[$this->proptags["label"]]; |
||
260 | } |
||
261 | |||
262 | if (array_key_exists($this->proptags["reminder"], $exception_props)) { |
||
263 | $extomodify["reminder_set"] = $exception_props[$this->proptags["reminder"]]; |
||
264 | } |
||
265 | |||
266 | if (array_key_exists($this->proptags["reminder_minutes"], $exception_props)) { |
||
267 | $extomodify["remind_before"] = $exception_props[$this->proptags["reminder_minutes"]]; |
||
268 | } |
||
269 | |||
270 | if (array_key_exists($this->proptags["alldayevent"], $exception_props)) { |
||
271 | $extomodify["alldayevent"] = $exception_props[$this->proptags["alldayevent"]]; |
||
272 | } |
||
273 | |||
274 | if (array_key_exists($this->proptags["busystatus"], $exception_props)) { |
||
275 | $extomodify["busystatus"] = $exception_props[$this->proptags["busystatus"]]; |
||
276 | } |
||
277 | |||
278 | $exception_props[PR_MESSAGE_CLASS] = "IPM.OLE.CLASS.{00061055-0000-0000-C000-000000000046}"; |
||
279 | |||
280 | // synchronize commonstart/commonend with startdate/duedate |
||
281 | if (isset($exception_props[$this->proptags["startdate"]])) { |
||
282 | $exception_props[$this->proptags["commonstart"]] = $exception_props[$this->proptags["startdate"]]; |
||
283 | } |
||
284 | |||
285 | if (isset($exception_props[$this->proptags["duedate"]])) { |
||
286 | $exception_props[$this->proptags["commonend"]] = $exception_props[$this->proptags["duedate"]]; |
||
287 | } |
||
288 | |||
289 | $attach = $this->getExceptionAttachment($baseday); |
||
290 | if (!$attach) { |
||
291 | if ($copy_attach_from) { |
||
292 | $this->deleteExceptionAttachment($base_date); |
||
293 | $this->createException($exception_props, $base_date, false, $exception_recips, $copy_attach_from); |
||
294 | } |
||
295 | else { |
||
296 | $this->createExceptionAttachment($exception_props, $exception_recips, $copy_attach_from); |
||
297 | } |
||
298 | } |
||
299 | else { |
||
300 | $message = mapi_attach_openobj($attach, MAPI_MODIFY); |
||
301 | |||
302 | // Set exception properties on embedded message and save |
||
303 | mapi_setprops($message, $exception_props); |
||
304 | $this->setExceptionRecipients($message, $exception_recips, false); |
||
305 | mapi_savechanges($message); |
||
306 | |||
307 | // If a new start or duedate is provided, we update the properties 'PR_EXCEPTION_STARTTIME' and 'PR_EXCEPTION_ENDTIME' |
||
308 | // on the attachment which holds the embedded msg and save everything. |
||
309 | $props = []; |
||
310 | if (isset($exception_props[$this->proptags["startdate"]])) { |
||
311 | $props[PR_EXCEPTION_STARTTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]); |
||
312 | } |
||
313 | if (isset($exception_props[$this->proptags["duedate"]])) { |
||
314 | $props[PR_EXCEPTION_ENDTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["duedate"]]); |
||
315 | } |
||
316 | if (!empty($props)) { |
||
317 | mapi_setprops($attach, $props); |
||
318 | } |
||
319 | |||
320 | mapi_savechanges($attach); |
||
321 | } |
||
322 | |||
323 | // Save recurrence data to message |
||
324 | $this->saveRecurrence(); |
||
325 | |||
326 | return true; |
||
327 | } |
||
328 | |||
329 | // Checks to see if the following is true: |
||
330 | // 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) |
||
331 | // 2) The exception to be created doesn't 'jump' over another occurrence (which may be an exception itself!) |
||
332 | // |
||
333 | // Both $basedate and $start are in LOCAL time |
||
334 | public function isValidExceptionDate($basedate, $start): bool { |
||
335 | // The way we do this is to look at the days that we're 'moving' the item in the exception. Each |
||
336 | // of these days may only contain the item that we're modifying. Any other item violates the rules. |
||
337 | |||
338 | if ($this->isException($basedate)) { |
||
339 | // If we're modifying an exception, we want to look at the days that we're 'moving' compared to where |
||
340 | // the exception used to be. |
||
341 | $oldexception = $this->getChangeException($basedate); |
||
342 | $prevday = $this->dayStartOf($oldexception["start"]); |
||
343 | } |
||
344 | else { |
||
345 | // If its a new exception, we want to look at the original placement of this item. |
||
346 | $prevday = $basedate; |
||
347 | } |
||
348 | |||
349 | $startday = $this->dayStartOf($start); |
||
350 | |||
351 | // Get all the occurrences on the days between the basedate (may be reversed) |
||
352 | if ($prevday < $startday) { |
||
353 | $items = $this->getItems($this->toGMT($this->tz, $prevday), $this->toGMT($this->tz, $startday + 24 * 60 * 60)); |
||
354 | } |
||
355 | else { |
||
356 | $items = $this->getItems($this->toGMT($this->tz, $startday), $this->toGMT($this->tz, $prevday + 24 * 60 * 60)); |
||
357 | } |
||
358 | |||
359 | // There should now be exactly one item, namely the item that we are modifying. If there are any other items in the range, |
||
360 | // then we abort the change, since one of the rules has been violated. |
||
361 | return count($items) == 1; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Check to see if the exception proposed at a certain basedate is allowed concerning reminder times:. |
||
366 | * |
||
367 | * Both must be true: |
||
368 | * - reminder time of this item is not before the starttime of the previous recurring item |
||
369 | * - reminder time of the next item is not before the starttime of this item |
||
370 | * |
||
371 | * @param date $basedate the base date of the exception (LOCAL time of non-exception occurrence) |
||
372 | * @param string $reminderminutes reminder minutes which is set of the item |
||
373 | * @param date $startdate the startdate of the selected item |
||
374 | * |
||
375 | * @returns boolean if the reminder minutes value valid (FALSE if either of the rules above are FALSE) |
||
376 | */ |
||
377 | public function isValidReminderTime($basedate, $reminderminutes, $startdate): bool { |
||
378 | // get all occurrence items before the selected items occurrence starttime |
||
379 | $occitems = $this->getItems($this->messageprops[$this->proptags["startdate"]], $this->toGMT($this->tz, $basedate)); |
||
380 | |||
381 | if (!empty($occitems)) { |
||
382 | // as occitems array is sorted in ascending order of startdate, to get the previous occurrence we take the last items in occitems . |
||
383 | $previousitem_startdate = $occitems[count($occitems) - 1][$this->proptags["startdate"]]; |
||
384 | |||
385 | // if our reminder is set before or equal to the beginning of the previous occurrence, then that's not allowed |
||
386 | if ($startdate - ($reminderminutes * 60) <= $previousitem_startdate) { |
||
387 | return false; |
||
388 | } |
||
389 | } |
||
390 | |||
391 | // Get the endtime of the current occurrence and find the next two occurrences (including the current occurrence) |
||
392 | $currentOcc = $this->getItems($this->toGMT($this->tz, $basedate), 0x7FF00000, 2, true); |
||
393 | |||
394 | // If there are another two occurrences, then the first is the current occurrence, and the one after that |
||
395 | // is the next occurrence. |
||
396 | if (count($currentOcc) > 1) { |
||
397 | $next = $currentOcc[1]; |
||
398 | // Get reminder time of the next occurrence. |
||
399 | $nextOccReminderTime = $next[$this->proptags["startdate"]] - ($next[$this->proptags["reminder_minutes"]] * 60); |
||
400 | // If the reminder time of the next item is before the start of this item, then that's not allowed |
||
401 | if ($nextOccReminderTime <= $startdate) { |
||
402 | return false; |
||
403 | } |
||
404 | } |
||
405 | |||
406 | // All was ok |
||
407 | return true; |
||
408 | } |
||
409 | |||
410 | public function setRecurrence($tz, $recur): void { |
||
411 | // only reset timezone if specified |
||
412 | if ($tz) { |
||
413 | $this->tz = $tz; |
||
414 | } |
||
415 | |||
416 | $this->recur = $recur; |
||
417 | |||
418 | if (!isset($this->recur["changed_occurrences"])) { |
||
419 | $this->recur["changed_occurrences"] = []; |
||
420 | } |
||
421 | |||
422 | if (!isset($this->recur["deleted_occurrences"])) { |
||
423 | $this->recur["deleted_occurrences"] = []; |
||
424 | } |
||
425 | |||
426 | $this->deleteAttachments(); |
||
427 | $this->saveRecurrence(); |
||
428 | |||
429 | // if client has not set the recurring_pattern then we should generate it and save it |
||
430 | $messageProps = mapi_getprops($this->message, [$this->proptags["recurring_pattern"]]); |
||
431 | if (empty($messageProps[$this->proptags["recurring_pattern"]])) { |
||
432 | $this->saveRecurrencePattern(); |
||
433 | } |
||
434 | } |
||
435 | |||
436 | // Returns the start or end time of the occurrence on the given base date. |
||
437 | // This assumes that the basedate you supply is in LOCAL time |
||
438 | public function getOccurrenceStart($basedate) { |
||
439 | $daystart = $this->dayStartOf($basedate); |
||
440 | |||
441 | return $this->toGMT($this->tz, $daystart + $this->recur["startocc"] * 60); |
||
442 | } |
||
443 | |||
444 | public function getOccurrenceEnd($basedate) { |
||
445 | $daystart = $this->dayStartOf($basedate); |
||
446 | |||
447 | return $this->toGMT($this->tz, $daystart + $this->recur["endocc"] * 60); |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * This function returns the next remindertime starting from $timestamp |
||
452 | * When no next reminder exists, false is returned. |
||
453 | * |
||
454 | * Note: Before saving this new reminder time (when snoozing), you must check for |
||
455 | * yourself if this reminder time is earlier than your snooze time, else |
||
456 | * use your snooze time and not this reminder time. |
||
457 | * |
||
458 | * @param mixed $timestamp |
||
459 | */ |
||
460 | public function getNextReminderTime($timestamp) { |
||
461 | /** |
||
462 | * Get next item from now until forever, but max 1 item with reminder set |
||
463 | * Note 0x7ff00000 instead of 0x7fffffff because of possible overflow failures when converting to GMT.... |
||
464 | * Here for getting next 10 occurrences assuming that next here we will be able to find |
||
465 | * nextreminder occurrence in 10 occurrences. |
||
466 | */ |
||
467 | $items = $this->getItems($timestamp, 0x7FF00000, 10, true); |
||
468 | |||
469 | // Initially setting nextreminder to false so when no next reminder exists, false is returned. |
||
470 | $nextreminder = false; |
||
471 | /* |
||
472 | * Loop through all reminder which we get in items variable |
||
473 | * and check whether the remindertime is greater than timestamp. |
||
474 | * On the first occurrence of greater nextreminder break the loop |
||
475 | * and return the value to calling function. |
||
476 | */ |
||
477 | for ($i = 0, $len = count($items); $i < $len; ++$i) { |
||
478 | $item = $items[$i]; |
||
479 | $tempnextreminder = $item[$this->proptags["startdate"]] - ($item[$this->proptags["reminder_minutes"]] * 60); |
||
480 | |||
481 | // If tempnextreminder is greater than timestamp then save it in nextreminder and break from the loop. |
||
482 | if ($tempnextreminder > $timestamp) { |
||
483 | $nextreminder = $tempnextreminder; |
||
484 | break; |
||
485 | } |
||
486 | } |
||
487 | |||
488 | return $nextreminder; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * Note: Static function, more like a utility function. |
||
493 | * |
||
494 | * Gets all the items (including recurring items) in the specified calendar in the given timeframe. Items are |
||
495 | * included as a whole if they overlap the interval <$start, $end> (non-inclusive). This means that if the interval |
||
496 | * 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 |
||
497 | * [7:00 - 9:00> is included as a whole, and is NOT capped to [8:00 - 9:00>. |
||
498 | * |
||
499 | * @param $store resource The store in which the calendar resides |
||
500 | * @param $calendar resource The calendar to get the items from |
||
501 | * @param $viewstart int Timestamp of beginning of view window |
||
502 | * @param $viewend int Timestamp of end of view window |
||
503 | * @param $propsrequested array Array of properties to return |
||
504 | * |
||
505 | * @psalm-param list{0: mixed, 1: mixed, 2?: mixed} $propsrequested |
||
506 | */ |
||
507 | public static function getCalendarItems($store, $calendar, $viewstart, $viewend, $propsrequested) { |
||
508 | return getCalendarItems($store, $calendar, $viewstart, $viewend, $propsrequested); |
||
509 | } |
||
510 | |||
511 | /* |
||
512 | * CODE BELOW THIS LINE IS FOR INTERNAL USE ONLY |
||
513 | ***************************************************************************************************************** |
||
514 | */ |
||
515 | |||
516 | /** |
||
517 | * Returns langified daily recurrence type string, whether it's singular or plural, |
||
518 | * recurrence interval. |
||
519 | */ |
||
520 | public function getI18RecTypeDaily(mixed $type, mixed $interval, bool $occSingleDayRank): array { |
||
521 | switch ($interval) { |
||
522 | case 1: // workdays |
||
523 | $type = dgettext('zarafa', 'workday'); |
||
524 | $occSingleDayRank = true; |
||
525 | break; |
||
526 | |||
527 | case 1440: // daily |
||
528 | $type = dgettext('zarafa', 'day'); |
||
529 | $occSingleDayRank = true; |
||
530 | break; |
||
531 | |||
532 | default: // every $interval days |
||
533 | $interval /= 1440; |
||
534 | $type = dgettext('zarafa', 'days'); |
||
535 | $occSingleDayRank = false; |
||
536 | break; |
||
537 | } |
||
538 | |||
539 | return [ |
||
540 | 'type' => $type, |
||
541 | 'interval' => $interval, |
||
542 | 'occSingleDayRank' => boolval($occSingleDayRank), |
||
543 | ]; |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * Returns langified weekly recurrence type string, whether it's singular or plural, |
||
548 | * recurrence interval. |
||
549 | */ |
||
550 | public function getI18RecTypeWeekly(mixed $type, mixed $interval, bool $occSingleDayRank): array { |
||
551 | if ($interval == 1) { |
||
552 | $type = dgettext('zarafa', 'week'); |
||
553 | $occSingleDayRank = true; |
||
554 | } |
||
555 | else { |
||
556 | $type = dgettext('zarafa', 'weeks'); |
||
557 | $occSingleDayRank = false; |
||
558 | } |
||
559 | $daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; |
||
560 | $type .= sprintf(" %s ", dgettext('zarafa', 'on')); |
||
561 | |||
562 | for ($j = 0, $weekdays = (int) $this->recur["weekdays"]; $j < 7; ++$j) { |
||
563 | if ($weekdays & (1 << $j)) { |
||
564 | $type .= sprintf("%s, ", dgettext('zarafa', $daysOfWeek[$j])); |
||
565 | } |
||
566 | } |
||
567 | $type = trim($type, ", "); |
||
568 | if (($pos = strrpos($type, ",")) !== false) { |
||
569 | $type = substr_replace($type, " " . dgettext('zarafa', 'and'), $pos, 1); |
||
570 | } |
||
571 | |||
572 | return [ |
||
573 | 'type' => $type, |
||
574 | 'interval' => $interval, |
||
575 | 'occSingleDayRank' => boolval($occSingleDayRank), |
||
576 | ]; |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * Returns langified monthly recurrence type string, whether it's singular or plural, |
||
581 | * recurrence interval. |
||
582 | */ |
||
583 | public function getI18RecTypeMonthly(mixed $type, mixed $interval, bool $occSingleDayRank): array { |
||
584 | if ($interval == 1) { |
||
585 | $type = dgettext('zarafa', 'month'); |
||
586 | $occSingleDayRank = true; |
||
587 | } |
||
588 | else { |
||
589 | $type = dgettext('zarafa', 'months'); |
||
590 | $occSingleDayRank = false; |
||
591 | } |
||
592 | |||
593 | return [ |
||
594 | 'type' => $type, |
||
595 | 'interval' => $interval, |
||
596 | 'occSingleDayRank' => boolval($occSingleDayRank), |
||
597 | ]; |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * Returns langified yearly recurrence type string, whether it's singular or plural, |
||
602 | * recurrence interval. |
||
603 | */ |
||
604 | public function getI18RecTypeYearly(mixed $type, mixed $interval, bool $occSingleDayRank): array { |
||
605 | if ($interval <= 12) { |
||
606 | $interval = 1; |
||
607 | $type = dgettext('zarafa', 'year'); |
||
608 | $occSingleDayRank = true; |
||
609 | } |
||
610 | else { |
||
611 | $interval = $interval / 12; |
||
612 | $type = dgettext('zarafa', 'years'); |
||
613 | $occSingleDayRank = false; |
||
614 | } |
||
615 | |||
616 | return [ |
||
617 | 'type' => $type, |
||
618 | 'interval' => $interval, |
||
619 | 'occSingleDayRank' => boolval($occSingleDayRank), |
||
620 | ]; |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Returns langified recurrence type string, whether it's singular or plural, |
||
625 | * recurrence interval. |
||
626 | */ |
||
627 | public function getI18nRecurrenceType(): array { |
||
628 | $type = $this->recur['type']; |
||
629 | $interval = $this->recur['everyn']; |
||
630 | $occSingleDayRank = false; |
||
631 | |||
632 | return match ($type) { |
||
633 | // Daily |
||
634 | 0x0A => $this->getI18RecTypeDaily($type, $interval, $occSingleDayRank), |
||
635 | // Weekly |
||
636 | 0x0B => $this->getI18RecTypeWeekly($type, $interval, $occSingleDayRank), |
||
637 | // Monthly |
||
638 | 0x0C => $this->getI18RecTypeMonthly($type, $interval, $occSingleDayRank), |
||
639 | // Yearly |
||
640 | 0x0D => $this->getI18RecTypeYearly($type, $interval, $occSingleDayRank), |
||
641 | default => [ |
||
642 | 'type' => $type, |
||
643 | 'interval' => $interval, |
||
644 | 'occSingleDayRank' => boolval($occSingleDayRank), |
||
645 | ], |
||
646 | }; |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * Returns the start or end time of the first occurrence. |
||
651 | */ |
||
652 | public function getOccDate(bool $getStart = true): mixed { |
||
653 | return $getStart ? |
||
654 | (isset($this->recur['startocc']) ? |
||
655 | $this->recur['start'] + (((int) $this->recur['startocc']) * 60) : |
||
656 | $this->recur['start']) : |
||
657 | (isset($this->recur['endocc']) ? |
||
658 | $this->recur['start'] + (((int) $this->recur['endocc']) * 60) : |
||
659 | $this->recur['end']); |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * Returns langified occurrence time. |
||
664 | */ |
||
665 | public function getI18nTime(string $format, mixed $occTime): string { |
||
666 | return gmdate(dgettext('zarafa', $format), $occTime); |
||
667 | } |
||
668 | |||
669 | /** |
||
670 | * Returns langified recurrence pattern termination after the given date. |
||
671 | */ |
||
672 | public function getI18nRecTermDate( |
||
673 | bool $occTimeRange, |
||
674 | bool $occSingleDayRank, |
||
675 | mixed $type, |
||
676 | mixed $interval, |
||
677 | string $start, |
||
678 | string $end, |
||
679 | string $startocc, |
||
680 | string $endocc |
||
681 | ): string { |
||
682 | return $occTimeRange ? |
||
683 | ( |
||
684 | $occSingleDayRank ? |
||
685 | sprintf( |
||
686 | dgettext('zarafa', 'Occurs every %s effective %s until %s from %s to %s.'), |
||
687 | $type, |
||
688 | $start, |
||
689 | $end, |
||
690 | $startocc, |
||
691 | $endocc |
||
692 | ) : |
||
693 | sprintf( |
||
694 | dgettext('zarafa', 'Occurs every %s %s effective %s until %s from %s to %s.'), |
||
695 | $interval, |
||
696 | $type, |
||
697 | $start, |
||
698 | $end, |
||
699 | $startocc, |
||
700 | $endocc |
||
701 | ) |
||
702 | ) : |
||
703 | ( |
||
704 | $occSingleDayRank ? |
||
705 | sprintf( |
||
706 | dgettext('zarafa', 'Occurs every %s effective %s until %s.'), |
||
707 | $type, |
||
708 | $start, |
||
709 | $end |
||
710 | ) : |
||
711 | sprintf( |
||
712 | dgettext('zarafa', 'Occurs every %s %s effective %s until %s.'), |
||
713 | $interval, |
||
714 | $type, |
||
715 | $start, |
||
716 | $end |
||
717 | ) |
||
718 | ); |
||
719 | } |
||
720 | |||
721 | /** |
||
722 | * Returns langified recurrence pattern termination after a number of |
||
723 | * occurrences. |
||
724 | */ |
||
725 | public function getI18nRecTermNrOcc( |
||
790 | ) |
||
791 | ); |
||
792 | } |
||
793 | |||
794 | /** |
||
795 | * Returns langified recurrence pattern termination with no end date. |
||
796 | */ |
||
797 | public function getI18nRecTermNoEnd( |
||
798 | bool $occTimeRange, |
||
799 | bool $occSingleDayRank, |
||
800 | mixed $type, |
||
801 | mixed $interval, |
||
802 | string $start, |
||
803 | string $startocc, |
||
804 | string $endocc |
||
805 | ): string { |
||
806 | return $occTimeRange ? |
||
807 | ( |
||
808 | $occSingleDayRank ? |
||
809 | sprintf( |
||
810 | dgettext('zarafa', 'Occurs every %s effective %s from %s to %s.'), |
||
811 | $type, |
||
812 | $start, |
||
813 | $startocc, |
||
814 | $endocc |
||
815 | ) : |
||
816 | sprintf( |
||
817 | dgettext('zarafa', 'Occurs every %s %s effective %s from %s to %s.'), |
||
818 | $interval, |
||
819 | $type, |
||
820 | $start, |
||
821 | $startocc, |
||
822 | $endocc |
||
823 | ) |
||
824 | ) : |
||
825 | ( |
||
826 | $occSingleDayRank ? |
||
827 | sprintf( |
||
828 | dgettext('zarafa', 'Occurs every %s effective %s.'), |
||
829 | $type, |
||
830 | $start |
||
831 | ) : |
||
832 | sprintf( |
||
833 | dgettext('zarafa', 'Occurs every %s %s effective %s.'), |
||
834 | $interval, |
||
835 | $type, |
||
836 | $start |
||
837 | ) |
||
838 | ); |
||
839 | } |
||
840 | |||
841 | /** |
||
842 | * Generates and stores recurrence pattern string to recurring_pattern property. |
||
843 | */ |
||
844 | public function saveRecurrencePattern(): string { |
||
845 | // Start formatting the properties in such a way we can apply |
||
846 | // them directly into the recurrence pattern. |
||
847 | $pattern = ''; |
||
848 | $occTimeRange = $this->recur['startocc'] != 0 && $this->recur['endocc'] != 0; |
||
849 | |||
850 | [ |
||
851 | 'type' => $type, |
||
852 | 'interval' => $interval, |
||
853 | 'occSingleDayRank' => $occSingleDayRank, |
||
854 | ] = $this->getI18nRecurrenceType(); |
||
855 | |||
856 | // get timings of the first occurrence |
||
857 | $firstoccstartdate = $this->getOccDate(); |
||
858 | $firstoccenddate = $this->getOccDate(false); |
||
859 | |||
860 | $start = $this->getI18nTime('d-m-Y', $firstoccstartdate); |
||
861 | $end = $this->getI18nTime('d-m-Y', $firstoccenddate); |
||
862 | $startocc = $this->getI18nTime('G:i', $firstoccstartdate); |
||
863 | $endocc = $this->getI18nTime('G:i', $firstoccenddate); |
||
864 | |||
865 | // Based on the properties, we need to generate the recurrence pattern string. |
||
866 | // This is obviously very easy since we can simply concatenate a bunch of strings, |
||
867 | // however this messes up translations for languages which order their words |
||
868 | // differently. |
||
869 | // To improve translation quality we create a series of default strings, in which |
||
870 | // we only have to fill in the correct variables. The base string is thus selected |
||
871 | // based on the available properties. |
||
872 | switch ($this->recur['term']) { |
||
873 | case 0x21: // After the given enddate |
||
874 | $pattern = $this->getI18nRecTermDate( |
||
875 | $occTimeRange, |
||
876 | boolval($occSingleDayRank), |
||
877 | $type, |
||
878 | $interval, |
||
879 | $start, |
||
880 | $end, |
||
881 | $startocc, |
||
882 | $endocc |
||
883 | ); |
||
884 | break; |
||
885 | |||
886 | case 0x22: // After a number of times |
||
887 | $pattern = $this->getI18nRecTermNrOcc( |
||
888 | $occTimeRange, |
||
889 | boolval($occSingleDayRank), |
||
890 | $type, |
||
891 | $interval, |
||
892 | $start, |
||
893 | $this->recur['numoccur'] ?? 0, |
||
894 | $startocc, |
||
895 | $endocc |
||
896 | ); |
||
897 | break; |
||
898 | |||
899 | case 0x23: // Never ends |
||
900 | $pattern = $this->getI18nRecTermNoEnd( |
||
901 | $occTimeRange, |
||
902 | boolval($occSingleDayRank), |
||
903 | $type, |
||
904 | $interval, |
||
905 | $start, |
||
906 | $startocc, |
||
907 | $endocc |
||
908 | ); |
||
909 | break; |
||
910 | |||
911 | default: |
||
912 | error_log(sprintf("Invalid recurrence pattern termination %d", $this->recur['term'])); |
||
913 | break; |
||
914 | } |
||
915 | |||
916 | if (!empty($pattern)) { |
||
917 | mapi_setprops($this->message, [$this->proptags["recurring_pattern"] => $pattern]); |
||
918 | } |
||
919 | |||
920 | return $pattern; |
||
921 | } |
||
922 | |||
923 | /* |
||
924 | * Remove an exception by base_date. This is the base date in local daystart time |
||
925 | */ |
||
926 | /** |
||
927 | * @param false|int $base_date |
||
928 | */ |
||
929 | public function deleteException($base_date): void { |
||
930 | // Remove all exceptions on $base_date from the deleted and changed occurrences lists |
||
931 | |||
932 | // Remove all items in $todelete from deleted_occurrences |
||
933 | $new = []; |
||
934 | |||
935 | foreach ($this->recur["deleted_occurrences"] as $entry) { |
||
936 | if ($entry != $base_date) { |
||
937 | $new[] = $entry; |
||
938 | } |
||
939 | } |
||
940 | $this->recur["deleted_occurrences"] = $new; |
||
941 | |||
942 | $new = []; |
||
943 | |||
944 | foreach ($this->recur["changed_occurrences"] as $entry) { |
||
945 | if (!$this->isSameDay($entry["basedate"], $base_date)) { |
||
946 | $new[] = $entry; |
||
947 | } |
||
948 | else { |
||
949 | $this->deleteExceptionAttachment($this->toGMT($this->tz, $base_date + $this->recur["startocc"] * 60)); |
||
950 | } |
||
951 | } |
||
952 | |||
953 | $this->recur["changed_occurrences"] = $new; |
||
954 | } |
||
955 | |||
956 | /** |
||
957 | * Function which saves the exception data in an attachment. |
||
958 | * |
||
959 | * @param array $exception_props the exception data (like any other MAPI appointment) |
||
960 | * @param array $exception_recips list of recipients |
||
961 | * @param mapi_message $copy_attach_from mapi message from which attachments should be copied |
||
962 | */ |
||
963 | public function createExceptionAttachment($exception_props, $exception_recips = [], $copy_attach_from = false): void { |
||
964 | // Create new attachment. |
||
965 | $attachment = mapi_message_createattach($this->message); |
||
966 | $props = []; |
||
967 | $props[PR_ATTACHMENT_FLAGS] = 2; |
||
968 | $props[PR_ATTACHMENT_HIDDEN] = true; |
||
969 | $props[PR_ATTACHMENT_LINKID] = 0; |
||
970 | $props[PR_ATTACH_FLAGS] = 0; |
||
971 | $props[PR_ATTACH_METHOD] = ATTACH_EMBEDDED_MSG; |
||
972 | $props[PR_DISPLAY_NAME] = "Exception"; |
||
973 | $props[PR_EXCEPTION_STARTTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["startdate"]]); |
||
974 | $props[PR_EXCEPTION_ENDTIME] = $this->fromGMT($this->tz, $exception_props[$this->proptags["duedate"]]); |
||
975 | mapi_setprops($attachment, $props); |
||
976 | |||
977 | $imessage = mapi_attach_openobj($attachment, MAPI_CREATE | MAPI_MODIFY); |
||
978 | |||
979 | if ($copy_attach_from) { |
||
980 | $attachmentTable = mapi_message_getattachmenttable($copy_attach_from); |
||
981 | if ($attachmentTable) { |
||
982 | $attachments = mapi_table_queryallrows($attachmentTable, [PR_ATTACH_NUM, PR_ATTACH_SIZE, PR_ATTACH_LONG_FILENAME, PR_ATTACHMENT_HIDDEN, PR_DISPLAY_NAME, PR_ATTACH_METHOD]); |
||
983 | |||
984 | foreach ($attachments as $attach_props) { |
||
985 | $attach_old = mapi_message_openattach($copy_attach_from, (int) $attach_props[PR_ATTACH_NUM]); |
||
986 | $attach_newResourceMsg = mapi_message_createattach($imessage); |
||
987 | mapi_copyto($attach_old, [], [], $attach_newResourceMsg, 0); |
||
988 | mapi_savechanges($attach_newResourceMsg); |
||
989 | } |
||
990 | } |
||
991 | } |
||
992 | |||
993 | $props = $props + $exception_props; |
||
994 | |||
995 | // FIXME: the following piece of code is written to fix the creation |
||
996 | // of an exception. This is only a quickfix as it is not yet possible |
||
997 | // to change an existing exception. |
||
998 | // remove mv properties when needed |
||
999 | foreach ($props as $propTag => $propVal) { |
||
1000 | if ((mapi_prop_type($propTag) & MV_FLAG) == MV_FLAG && is_null($propVal)) { |
||
1001 | unset($props[$propTag]); |
||
1002 | } |
||
1003 | } |
||
1004 | |||
1005 | mapi_setprops($imessage, $props); |
||
1006 | |||
1007 | $this->setExceptionRecipients($imessage, $exception_recips, true); |
||
1008 | |||
1009 | mapi_savechanges($imessage); |
||
1010 | mapi_savechanges($attachment); |
||
1011 | } |
||
1012 | |||
1013 | /** |
||
1014 | * Function which deletes the attachment of an exception. |
||
1015 | * |
||
1016 | * @param mixed $base_date base date of the attachment. Should be in GMT. The attachment |
||
1017 | * actually saves the real time of the original date, so we have |
||
1018 | * to check whether it's on the same day. |
||
1019 | */ |
||
1020 | public function deleteExceptionAttachment($base_date): void { |
||
1021 | $attachments = mapi_message_getattachmenttable($this->message); |
||
1022 | // Retrieve only exceptions which are stored as embedded messages |
||
1023 | $attach_res = [ |
||
1024 | RES_PROPERTY, |
||
1025 | [ |
||
1026 | RELOP => RELOP_EQ, |
||
1027 | ULPROPTAG => PR_ATTACH_METHOD, |
||
1028 | VALUE => [PR_ATTACH_METHOD => ATTACH_EMBEDDED_MSG], |
||
1029 | ], |
||
1030 | ]; |
||
1031 | $attachRows = mapi_table_queryallrows($attachments, [PR_ATTACH_NUM], $attach_res); |
||
1032 | |||
1033 | foreach ($attachRows as $attachRow) { |
||
1034 | $tempattach = mapi_message_openattach($this->message, $attachRow[PR_ATTACH_NUM]); |
||
1035 | $exception = mapi_attach_openobj($tempattach); |
||
1036 | |||
1037 | $data = mapi_message_getprops($exception, [$this->proptags["basedate"]]); |
||
1038 | |||
1039 | if ($this->dayStartOf($this->fromGMT($this->tz, $data[$this->proptags["basedate"]])) == $this->dayStartOf($base_date)) { |
||
1040 | mapi_message_deleteattach($this->message, $attachRow[PR_ATTACH_NUM]); |
||
1041 | } |
||
1042 | } |
||
1043 | } |
||
1044 | |||
1045 | /** |
||
1046 | * Function which deletes all attachments of a message. |
||
1047 | */ |
||
1048 | public function deleteAttachments(): void { |
||
1049 | $attachments = mapi_message_getattachmenttable($this->message); |
||
1050 | $attachTable = mapi_table_queryallrows($attachments, [PR_ATTACH_NUM, PR_ATTACHMENT_HIDDEN]); |
||
1051 | |||
1052 | foreach ($attachTable as $attachRow) { |
||
1053 | if (isset($attachRow[PR_ATTACHMENT_HIDDEN]) && $attachRow[PR_ATTACHMENT_HIDDEN]) { |
||
1054 | mapi_message_deleteattach($this->message, $attachRow[PR_ATTACH_NUM]); |
||
1055 | } |
||
1056 | } |
||
1057 | } |
||
1058 | |||
1059 | /** |
||
1060 | * Get an exception attachment based on its basedate. |
||
1061 | * |
||
1062 | * @param mixed $base_date |
||
1063 | */ |
||
1064 | public function getExceptionAttachment($base_date) { |
||
1097 | } |
||
1098 | |||
1099 | /** |
||
1100 | * processOccurrenceItem, adds an item to a list of occurrences, but only if the following criteria are met: |
||
1101 | * - The resulting occurrence (or exception) starts or ends in the interval <$start, $end> |
||
1102 | * - The occurrence isn't specified as a deleted occurrence. |
||
1103 | * |
||
1104 | * @param array $items reference to the array to be added to |
||
1105 | * @param int $start start of timeframe in GMT TIME |
||
1106 | * @param int $end end of timeframe in GMT TIME |
||
1107 | * @param int $basedate (hour/sec/min assumed to be 00:00:00) in LOCAL TIME OF THE OCCURRENCE |
||
1108 | * @param int $startocc start of occurrence since beginning of day in minutes |
||
1109 | * @param int $endocc end of occurrence since beginning of day in minutes |
||
1110 | * @param mixed $tz the timezone info for this occurrence ( applied to $basedate / $startocc / $endocc ) |
||
1111 | * @param bool $reminderonly If TRUE, only add the item if the reminder is set |
||
1112 | * |
||
1113 | * @return null|false |
||
1114 | */ |
||
1115 | public function processOccurrenceItem(&$items, $start, $end, $basedate, $startocc, $endocc, $tz, $reminderonly) { |
||
1116 | $exception = $this->isException($basedate); |
||
1117 | if ($exception) { |
||
1118 | return false; |
||
1119 | } |
||
1120 | $occstart = $basedate + $startocc * 60; |
||
1121 | $occend = $basedate + $endocc * 60; |
||
1122 | |||
1123 | // Convert to GMT |
||
1124 | $occstart = $this->toGMT($tz, $occstart); |
||
1125 | $occend = $this->toGMT($tz, $occend); |
||
1126 | |||
1127 | /** |
||
1128 | * FIRST PART: Check range criterium. Exact matches (eg when $occstart == $end), do NOT match since you cannot |
||
1129 | * see any part of the appointment. Partial overlaps DO match. |
||
1130 | * |
||
1131 | * SECOND PART: check if occurrence is not a zero duration occurrence which |
||
1132 | * starts at 00:00 and ends on 00:00. if it is so, then process |
||
1133 | * the occurrence and send it in response. |
||
1134 | */ |
||
1135 | if (($occstart >= $end || $occend <= $start) && !($occstart == $occend && $occstart == $start)) { |
||
1136 | return; |
||
1137 | } |
||
1138 | |||
1139 | // Properties for this occurrence are the same as the main object, |
||
1140 | // With these properties overridden |
||
1141 | $newitem = $this->messageprops; |
||
1142 | $newitem[$this->proptags["startdate"]] = $occstart; |
||
1143 | $newitem[$this->proptags["duedate"]] = $occend; |
||
1144 | $newitem[$this->proptags["commonstart"]] = $occstart; |
||
1145 | $newitem[$this->proptags["commonend"]] = $occend; |
||
1146 | $newitem["basedate"] = $basedate; |
||
1147 | |||
1148 | // If reminderonly is set, only add reminders |
||
1149 | if ($reminderonly && (!isset($newitem[$this->proptags["reminder"]]) || $newitem[$this->proptags["reminder"]] == false)) { |
||
1150 | return; |
||
1151 | } |
||
1152 | |||
1153 | $items[] = $newitem; |
||
1154 | } |
||
1155 | |||
1156 | /** |
||
1157 | * processExceptionItem, adds an all exception item to a list of occurrences, without any constraint on timeframe. |
||
1158 | * |
||
1159 | * @param array $items reference to the array to be added to |
||
1160 | * @param date $start start of timeframe in GMT TIME |
||
1161 | * @param date $end end of timeframe in GMT TIME |
||
1162 | */ |
||
1163 | public function processExceptionItems(&$items, $start, $end): void { |
||
1164 | $limit = 0; |
||
1165 | foreach ($this->recur["changed_occurrences"] as $exception) { |
||
1166 | // Convert to GMT |
||
1167 | $occstart = $this->toGMT($this->tz, $exception["start"]); |
||
1168 | $occend = $this->toGMT($this->tz, $exception["end"]); |
||
1169 | |||
1170 | // Check range criterium. Exact matches (eg when $occstart == $end), do NOT match since you cannot |
||
1171 | // see any part of the appointment. Partial overlaps DO match. |
||
1172 | if ($occstart >= $end || $occend <= $start) { |
||
1173 | continue; |
||
1174 | } |
||
1175 | |||
1176 | array_push($items, $this->getExceptionProperties($exception)); |
||
1177 | if (count($items) == $limit) { |
||
1178 | break; |
||
1179 | } |
||
1180 | } |
||
1181 | } |
||
1182 | |||
1183 | /** |
||
1184 | * Function which verifies if on the given date an exception, delete or change, occurs. |
||
1185 | * |
||
1186 | * @param mixed $basedate |
||
1187 | * |
||
1188 | * @return bool true - if an exception occurs on the given date, false - no exception occurs on the given date |
||
1189 | */ |
||
1190 | public function isException($basedate) { |
||
1200 | } |
||
1201 | |||
1202 | /** |
||
1203 | * Returns TRUE if there is a DELETE exception on the given base date. |
||
1204 | * |
||
1205 | * @param mixed $basedate |
||
1206 | */ |
||
1207 | public function isDeleteException($basedate): bool { |
||
1208 | // Check if the occurrence is deleted on the specified date |
||
1209 | foreach ($this->recur["deleted_occurrences"] as $deleted) { |
||
1210 | if ($this->isSameDay($deleted, $basedate)) { |
||
1211 | return true; |
||
1212 | } |
||
1213 | } |
||
1214 | |||
1215 | return false; |
||
1216 | } |
||
1217 | |||
1218 | /** |
||
1219 | * Returns the exception if there is a CHANGE exception on the given base date, or FALSE otherwise. |
||
1220 | * |
||
1221 | * @param mixed $basedate |
||
1222 | */ |
||
1223 | public function getChangeException($basedate) { |
||
1224 | // Check if the occurrence is modified on the specified date |
||
1225 | foreach ($this->recur["changed_occurrences"] as $changed) { |
||
1226 | if ($this->isSameDay($changed["basedate"], $basedate)) { |
||
1227 | return $changed; |
||
1228 | } |
||
1229 | } |
||
1230 | |||
1231 | return false; |
||
1232 | } |
||
1233 | |||
1234 | /** |
||
1235 | * Function to see if two dates are on the same day. |
||
1236 | * |
||
1237 | * @param mixed $date1 |
||
1238 | * @param mixed $date2 |
||
1239 | * |
||
1240 | * @return bool Returns TRUE when both dates are on the same day |
||
1241 | */ |
||
1242 | public function isSameDay($date1, $date2) { |
||
1247 | } |
||
1248 | |||
1249 | /** |
||
1250 | * Function which sets recipients for an exception. |
||
1251 | * |
||
1252 | * The $exception_recips can be provided in 2 ways: |
||
1253 | * - A delta which indicates which recipients must be added, removed or deleted. |
||
1254 | * - A complete array of the recipients which should be applied to the message. |
||
1255 | * |
||
1256 | * The first option is preferred as it will require less work to be executed. |
||
1257 | * |
||
1258 | * @param resource $message exception attachment of recurring item |
||
1259 | * @param array $exception_recips list of recipients |
||
1260 | * @param bool $copy_orig_recips True to copy all recipients which are on the original |
||
1261 | * message to the attachment by default. False if only the $exception_recips changes should |
||
1262 | * be applied. |
||
1263 | */ |
||
1264 | public function setExceptionRecipients($message, $exception_recips, $copy_orig_recips = true): void { |
||
1265 | if (isset($exception_recips['add']) || isset($exception_recips['remove']) || isset($exception_recips['modify'])) { |
||
1266 | $this->setDeltaExceptionRecipients($message, $exception_recips, $copy_orig_recips); |
||
1267 | } |
||
1268 | else { |
||
1269 | $this->setAllExceptionRecipients($message, $exception_recips); |
||
1270 | } |
||
1271 | } |
||
1272 | |||
1273 | /** |
||
1274 | * Function which applies the provided delta for recipients changes to the exception. |
||
1275 | * |
||
1276 | * The $exception_recips should be an array containing the following keys: |
||
1277 | * - "add": this contains an array of recipients which must be added |
||
1278 | * - "remove": This contains an array of recipients which must be removed |
||
1279 | * - "modify": This contains an array of recipients which must be modified |
||
1280 | * |
||
1281 | * @param mixed $exception |
||
1282 | * @param array $exception_recips list of recipients |
||
1283 | * @param bool $copy_orig_recips True to copy all recipients which are on the original |
||
1284 | * message to the attachment by default. False if only the $exception_recips changes should |
||
1285 | * be applied. |
||
1286 | */ |
||
1287 | public function setDeltaExceptionRecipients($exception, $exception_recips, $copy_orig_recips): void { |
||
1326 | } |
||
1327 | } |
||
1328 | |||
1329 | /** |
||
1330 | * Function which applies the provided recipients to the exception, also checks for deleted recipients. |
||
1331 | * |
||
1332 | * The $exception_recips should be an array containing all recipients which must be applied |
||
1333 | * to the exception. This will copy all recipients from the original message and then start filter |
||
1334 | * out all recipients which are not provided by the $exception_recips list. |
||
1335 | * |
||
1336 | * @param resource $message exception attachment of recurring item |
||
1337 | * @param array $exception_recips list of recipients |
||
1338 | */ |
||
1339 | public function setAllExceptionRecipients($message, $exception_recips): void { |
||
1340 | $deletedRecipients = []; |
||
1341 | $useMessageRecipients = false; |
||
1342 | |||
1343 | $recipientTable = mapi_message_getrecipienttable($message); |
||
1344 | $recipientRows = mapi_table_queryallrows($recipientTable, $this->recipprops); |
||
1345 | |||
1346 | if (empty($recipientRows)) { |
||
1347 | $useMessageRecipients = true; |
||
1348 | $recipientTable = mapi_message_getrecipienttable($this->message); |
||
1349 | $recipientRows = mapi_table_queryallrows($recipientTable, $this->recipprops); |
||
1350 | } |
||
1351 | |||
1352 | // Add organizer to meeting only if it is not organized. |
||
1353 | $msgprops = mapi_getprops($message, [PR_SENT_REPRESENTING_ENTRYID, PR_SENT_REPRESENTING_EMAIL_ADDRESS, PR_SENT_REPRESENTING_NAME, PR_SENT_REPRESENTING_ADDRTYPE, PR_SENT_REPRESENTING_SEARCH_KEY, $this->proptags['responsestatus']]); |
||
1354 | if (isset($msgprops[$this->proptags['responsestatus']]) && $msgprops[$this->proptags['responsestatus']] != olResponseOrganized) { |
||
1355 | $this->addOrganizer($msgprops, $exception_recips); |
||
1356 | } |
||
1357 | |||
1358 | if (!empty($exception_recips)) { |
||
1359 | foreach ($recipientRows as $recipient) { |
||
1360 | $found = false; |
||
1361 | foreach ($exception_recips as $excep_recip) { |
||
1362 | if (isset($recipient[PR_SEARCH_KEY], $excep_recip[PR_SEARCH_KEY]) && $recipient[PR_SEARCH_KEY] == $excep_recip[PR_SEARCH_KEY]) { |
||
1363 | $found = true; |
||
1364 | } |
||
1365 | } |
||
1366 | |||
1367 | if (!$found) { |
||
1368 | $foundInDeletedRecipients = false; |
||
1369 | // Look if the $recipient is in the list of deleted recipients |
||
1370 | if (!empty($deletedRecipients)) { |
||
1371 | foreach ($deletedRecipients as $recip) { |
||
1372 | if (isset($recipient[PR_SEARCH_KEY], $excep_recip[PR_SEARCH_KEY]) && $recip[PR_SEARCH_KEY] == $recipient[PR_SEARCH_KEY]) { |
||
1373 | $foundInDeletedRecipients = true; |
||
1374 | break; |
||
1375 | } |
||
1376 | } |
||
1377 | } |
||
1378 | |||
1379 | // If recipient is not in list of deleted recipient, add him |
||
1380 | if (!$foundInDeletedRecipients) { |
||
1381 | if (!isset($recipient[PR_RECIPIENT_FLAGS]) || $recipient[PR_RECIPIENT_FLAGS] != (recipReserved | recipExceptionalDeleted | recipSendable)) { |
||
1382 | $recipient[PR_RECIPIENT_FLAGS] = recipSendable | recipExceptionalDeleted; |
||
1383 | } |
||
1384 | else { |
||
1385 | $recipient[PR_RECIPIENT_FLAGS] = recipReserved | recipExceptionalDeleted | recipSendable; |
||
1386 | } |
||
1387 | $recipient[PR_RECIPIENT_TRACKSTATUS] = olRecipientTrackStatusNone; // No Response required |
||
1388 | $deletedRecipients[] = $recipient; |
||
1389 | } |
||
1390 | } |
||
1391 | |||
1392 | // When $message contains a non-empty recipienttable, we must delete the recipients |
||
1393 | // before re-adding them. However, when $message is doesn't contain any recipients, |
||
1394 | // we are using the recipient table of the original message ($this->message) |
||
1395 | // rather then $message. In that case, we don't need to remove the recipients |
||
1396 | // from the $message, as the recipient table is already empty, and |
||
1397 | // mapi_message_modifyrecipients() will throw an error. |
||
1398 | if ($useMessageRecipients === false) { |
||
1399 | mapi_message_modifyrecipients($message, MODRECIP_REMOVE, [$recipient]); |
||
1400 | } |
||
1401 | } |
||
1402 | $exception_recips = array_merge($exception_recips, $deletedRecipients); |
||
1403 | } |
||
1404 | else { |
||
1405 | $exception_recips = $recipientRows; |
||
1406 | } |
||
1407 | |||
1408 | if (!empty($exception_recips)) { |
||
1409 | // Set the new list of recipients on the exception message, this also removes the existing recipients |
||
1410 | mapi_message_modifyrecipients($message, MODRECIP_MODIFY, $exception_recips); |
||
1411 | } |
||
1412 | } |
||
1413 | |||
1414 | /** |
||
1415 | * Function returns basedates of all changed occurrences. |
||
1416 | * |
||
1417 | * @return array|false array( 0 => 123459321 ) |
||
1418 | * |
||
1419 | * @psalm-return false|list<mixed> |
||
1420 | */ |
||
1421 | public function getAllExceptions() { |
||
1432 | } |
||
1433 | |||
1434 | /** |
||
1435 | * Function which adds organizer to recipient list which is passed. |
||
1436 | * This function also checks if it has organizer. |
||
1437 | * |
||
1438 | * @param array $messageProps message properties |
||
1439 | * @param array $recipients recipients list of message |
||
1440 | * @param bool $isException true if we are processing recipient of exception |
||
1441 | */ |
||
1442 | public function addOrganizer($messageProps, &$recipients, $isException = false): void { |
||
1470 | } |
||
1471 | } |
||
1472 | } |
||
1473 | |||
1474 | /* |
||
1475 | |||
1476 | From http://www.ohelp-one.com/new-6765483-3268.html: |
||
1477 | |||
1478 | Recurrence Data Structure Offset Type Value |
||
1479 | |||
1480 | 0 ULONG (?) Constant : { 0x04, 0x30, 0x04, 0x30} |
||
1481 | |||
1482 | 4 UCHAR 0x0A + recurrence type: 0x0A for daily, 0x0B for weekly, 0x0C for |
||
1483 | monthly, 0x0D for yearly |
||
1484 | |||
1485 | 5 UCHAR Constant: { 0x20} |
||
1783 |