<?php

/*
 * SPDX-License-Identifier: AGPL-3.0-only
 * SPDX-FileCopyrightText: Copyright 2005-2016 Zarafa Deutschland GmbH
 * SPDX-FileCopyrightText: Copyright 2020-2025 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(mixed $store, mixed $message, array $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;
			$properties['meetingrecurring'] = "PT_BOOLEAN:PSETID_Meeting:" . PidLidIsRecurring;

			$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;
			}
			$changed_item = [];
			// 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]];
					if (isset($exception_props[$this->proptags[$propname]]) &&
						$this->messageprops[$this->proptags[$propname]] != $exception_props[$this->proptags[$propname]]) {
						$changed_item[$propname] = $exception_props[$this->proptags[$propname]];
					}
				}
				elseif (isset($exception_props[$this->proptags[$propname]])) {
					$changed_item[$propname] = $exception_props[$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["basedate"] = $baseday;
			$changed_item["start"] = $this->fromGMT($this->tz, $props[$this->proptags["startdate"]]);
			$changed_item["end"] = $this->fromGMT($this->tz, $props[$this->proptags["duedate"]]);

			// Add the changed occurrence to the list
			$this->recur["changed_occurrences"][] = $changed_item;
		}
		else {
			// Delete the occurrence by placing it in the deleted occurrences list
			$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];
				break;
			}
		}

		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"]]);

		// Map of property keys to their target keys in $extomodify, with optional transformation
		$propertyMappings = [
			"startdate" => ["target" => "start", "transform" => true],
			"duedate" => ["target" => "end", "transform" => true],
			"subject" => ["target" => "subject", "transform" => false],
			"location" => ["target" => "location", "transform" => false],
			"label" => ["target" => "label", "transform" => false],
			"reminder" => ["target" => "reminder_set", "transform" => false],
			"reminder_minutes" => ["target" => "remind_before", "transform" => false],
			"alldayevent" => ["target" => "alldayevent", "transform" => false],
			"busystatus" => ["target" => "busystatus", "transform" => false],
		];

		foreach ($propertyMappings as $propKey => $mapping) {
			$propTag = $this->proptags[$propKey];
			if (array_key_exists($propTag, $exception_props)) {
				$extomodify[$mapping["target"]] = $mapping["transform"] ?
					$this->fromGMT($this->tz, $exception_props[$propTag]) :
					$exception_props[$propTag];
			}
		}

		$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 int $basedate        the base date of the exception (LOCAL time of non-exception occurrence)
	 * @param int $reminderminutes reminder minutes which is set of the item
	 * @param int $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(mixed $tz, mixed $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(int $basedate): int {
		$daystart = $this->dayStartOf($basedate);

		return $this->toGMT($this->tz, $daystart + $this->recur["startocc"] * 60);
	}

	public function getOccurrenceEnd(int $basedate): int {
		$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.
	 */
	public function getNextReminderTime(int $timestamp): false|int {
		/**
		 * 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(mixed $store, mixed $calendar, int $viewstart, int $viewend, array $propsrequested): array {
		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 = _('workday');
				$occSingleDayRank = true;
				break;

			case 1440: // daily
				$type = _('day');
				$occSingleDayRank = true;
				break;

			default: // every $interval days
				$interval /= 1440;
				$type = _('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 = _('week');
			$occSingleDayRank = true;
		}
		else {
			$type = _('weeks');
			$occSingleDayRank = false;
		}
		$daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
		$type .= sprintf(" %s ", _('on'));

		for ($j = 0, $weekdays = (int) $this->recur["weekdays"]; $j < 7; ++$j) {
			if ($weekdays & (1 << $j)) {
				$type .= sprintf("%s, ", _($daysOfWeek[$j]));
			}
		}
		$type = trim($type, ", ");
		if (($pos = strrpos($type, ",")) !== false) {
			$type = substr_replace($type, " " . _('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 = _('month');
			$occSingleDayRank = true;
		}
		else {
			$type = _('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 = _('year');
			$occSingleDayRank = true;
		}
		else {
			$interval = $interval / 12;
			$type = _('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(_($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(
						_('Occurs every %s effective %s until %s from %s to %s.'),
						$type,
						$start,
						$end,
						$startocc,
						$endocc
					) :
					sprintf(
						_('Occurs every %s %s effective %s until %s from %s to %s.'),
						$interval,
						$type,
						$start,
						$end,
						$startocc,
						$endocc
					)
			) :
			(
				$occSingleDayRank ?
					sprintf(
						_('Occurs every %s effective %s until %s.'),
						$type,
						$start,
						$end
					) :
					sprintf(
						_('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(
						_('Occurs every %s effective %s from %s to %s.'),
						$type,
						$start,
						$startocc,
						$endocc
					) :
					sprintf(
						_('Occurs every %s %s effective %s from %s to %s.'),
						$interval,
						$type,
						$start,
						$startocc,
						$endocc
					)
			) :
			(
				$occSingleDayRank ?
					sprintf(
						_('Occurs every %s effective %s.'),
						$type,
						$start
					) :
					sprintf(
						_('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 false|resource $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 && $propVal === null) {
				unset($props[$propTag]);
			}
		}

		mapi_setprops($imessage, $props);

		$this->setExceptionRecipients($imessage, $exception_recips, true);

		mapi_savechanges($imessage);
		mapi_savechanges($attachment);
	}

	/**
	 * Function which deletes the attachment of an exception.
	 *
	 * @param mixed $base_date base date of the attachment. Should be in GMT. The attachment
	 *                         actually saves the real time of the original date, so we have
	 *                         to check whether it's on the same day.
	 */
	public function deleteExceptionAttachment($base_date): void {
		$attachments = mapi_message_getattachmenttable($this->message);
		// Retrieve only exceptions which are stored as embedded messages
		$attach_res = $this->getEmbeddedMessageRestriction();
		$attachRows = mapi_table_queryallrows($attachments, [PR_ATTACH_NUM], $attach_res);

		foreach ($attachRows as $attachRow) {
			$tempattach = mapi_message_openattach($this->message, $attachRow[PR_ATTACH_NUM]);
			$exception = mapi_attach_openobj($tempattach);

			$data = mapi_message_getprops($exception, [$this->proptags["basedate"]]);

			if ($this->dayStartOf($this->fromGMT($this->tz, $data[$this->proptags["basedate"]])) == $this->dayStartOf($base_date)) {
				mapi_message_deleteattach($this->message, $attachRow[PR_ATTACH_NUM]);
			}
		}
	}

	/**
	 * Function which deletes all attachments of a message.
	 */
	public function deleteAttachments(): void {
		$attachments = mapi_message_getattachmenttable($this->message);
		$attachTable = mapi_table_queryallrows($attachments, [PR_ATTACH_NUM, PR_ATTACHMENT_HIDDEN]);

		foreach ($attachTable as $attachRow) {
			if (isset($attachRow[PR_ATTACHMENT_HIDDEN]) && $attachRow[PR_ATTACHMENT_HIDDEN]) {
				mapi_message_deleteattach($this->message, $attachRow[PR_ATTACH_NUM]);
			}
		}
	}

	/**
	 * Get an exception attachment based on its basedate.
	 */
	public function getExceptionAttachment(int $base_date): mixed {
		// Retrieve only exceptions which are stored as embedded messages
		$attach_res = $this->getEmbeddedMessageRestriction();
		$attachments = mapi_message_getattachmenttable($this->message);
		$attachRows = mapi_table_queryallrows($attachments, [PR_ATTACH_NUM], $attach_res);

		if (is_array($attachRows)) {
			foreach ($attachRows as $attachRow) {
				$tempattach = mapi_message_openattach($this->message, $attachRow[PR_ATTACH_NUM]);
				$exception = mapi_attach_openobj($tempattach);

				$data = mapi_message_getprops($exception, [$this->proptags["basedate"]]);

				if (!isset($data[$this->proptags["basedate"]])) {
					// if no basedate found then it could be embedded message so ignore it
					// we need proper restriction to exclude embedded messages as well
					continue;
				}

				if ($this->isSameDay($this->fromGMT($this->tz, $data[$this->proptags["basedate"]]), $base_date)) {
					return $tempattach;
				}
			}
		}

		return false;
	}

	/**
	 * processOccurrenceItem, adds an item to a list of occurrences, but only if the following criteria are met:
	 * - The resulting occurrence (or exception) starts or ends in the interval <$start, $end>
	 * - The occurrence isn't specified as a deleted occurrence.
	 *
	 * @param array $items        reference to the array to be added to
	 * @param int   $start        start of timeframe in GMT TIME
	 * @param int   $end          end of timeframe in GMT TIME
	 * @param int   $basedate     (hour/sec/min assumed to be 00:00:00) in LOCAL TIME OF THE OCCURRENCE
	 * @param int   $startocc     start of occurrence since beginning of day in minutes
	 * @param int   $endocc       end of occurrence since beginning of day in minutes
	 * @param mixed $tz           the timezone info for this occurrence ( applied to $basedate / $startocc / $endocc )
	 * @param bool  $reminderonly If TRUE, only add the item if the reminder is set
	 */
	public function processOccurrenceItem(array &$items, mixed $start, int $end, mixed $basedate, mixed $startocc, mixed $endocc, mixed $tz, mixed $reminderonly): ?false {
		$exception = $this->isException($basedate);
		if ($exception) {
			return false;
		}
		$occstart = $basedate + $startocc * 60;
		$occend = $basedate + $endocc * 60;

		// Convert to GMT
		$occstart = $this->toGMT($tz, $occstart);
		$occend = $this->toGMT($tz, $occend);

		/**
		 * FIRST PART: Check range criterium. Exact matches (eg when $occstart == $end), do NOT match since you cannot
		 * see any part of the appointment. Partial overlaps DO match.
		 *
		 * SECOND PART: check if occurrence is not a zero duration occurrence which
		 * starts at 00:00 and ends on 00:00. if it is so, then process
		 * the occurrence and send it in response.
		 */
		if (($occstart >= $end || $occend <= $start) && !($occstart == $occend && $occstart == $start)) {
			return null;
		}

		// Properties for this occurrence are the same as the main object,
		// With these properties overridden
		$newitem = $this->messageprops;
		$newitem[$this->proptags["startdate"]] = $occstart;
		$newitem[$this->proptags["duedate"]] = $occend;
		$newitem[$this->proptags["commonstart"]] = $occstart;
		$newitem[$this->proptags["commonend"]] = $occend;
		$newitem["basedate"] = $basedate;

		// If reminderonly is set, only add reminders
		if ($reminderonly && (!isset($newitem[$this->proptags["reminder"]]) || $newitem[$this->proptags["reminder"]] === false)) {
			return null;
		}

		$items[] = $newitem;

		return null;
	}

	/**
	 * Function which verifies if on the given date an exception, delete or change, occurs.
	 *
	 * @return bool true - if an exception occurs on the given date, false - no exception occurs on the given date
	 */
	public function isException(int $basedate): bool {
		if ($this->isDeleteException($basedate)) {
			return true;
		}

		if ($this->getChangeException($basedate) != false) {
			return true;
		}

		return false;
	}

	/**
	 * Returns TRUE if there is a DELETE exception on the given base date.
	 */
	public function isDeleteException(int $basedate): bool {
		// Check if the occurrence is deleted on the specified date
		foreach ($this->recur["deleted_occurrences"] as $deleted) {
			if ($this->isSameDay($deleted, $basedate)) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Returns the exception if there is a CHANGE exception on the given base date, or FALSE otherwise.
	 */
	public function getChangeException(int $basedate): array|false {
		// Check if the occurrence is modified on the specified date
		foreach ($this->recur["changed_occurrences"] as $changed) {
			if ($this->isSameDay($changed["basedate"], $basedate)) {
				return $changed;
			}
		}

		return false;
	}

	/**
	 * Function to see if two dates are on the same day.
	 *
	 * @return bool Returns TRUE when both dates are on the same day
	 */
	public function isSameDay(int $date1, int $date2): bool {
		$time1 = $this->gmtime($date1);
		$time2 = $this->gmtime($date2);

		return $time1["tm_mon"] == $time2["tm_mon"] && $time1["tm_year"] == $time2["tm_year"] && $time1["tm_mday"] == $time2["tm_mday"];
	}

	/**
	 * Function which sets recipients for an exception.
	 *
	 * The $exception_recips can be provided in 2 ways:
	 *  - A delta which indicates which recipients must be added, removed or deleted.
	 *  - A complete array of the recipients which should be applied to the message.
	 *
	 * The first option is preferred as it will require less work to be executed.
	 *
	 * @param resource $message          exception attachment of recurring item
	 * @param array    $exception_recips list of recipients
	 * @param bool     $copy_orig_recips True to copy all recipients which are on the original
	 *                                   message to the attachment by default. False if only the $exception_recips changes should
	 *                                   be applied.
	 */
	public function setExceptionRecipients(mixed $message, array $exception_recips, bool $copy_orig_recips = true): void {
		if (isset($exception_recips['add']) || isset($exception_recips['remove']) || isset($exception_recips['modify'])) {
			$this->setDeltaExceptionRecipients($message, $exception_recips, $copy_orig_recips);
		}
		else {
			$this->setAllExceptionRecipients($message, $exception_recips);
		}
	}

	/**
	 * Function which applies the provided delta for recipients changes to the exception.
	 *
	 * The $exception_recips should be an array containing the following keys:
	 *  - "add": this contains an array of recipients which must be added
	 *  - "remove": This contains an array of recipients which must be removed
	 *  - "modify": This contains an array of recipients which must be modified
	 *
	 * @param array $exception_recips list of recipients
	 * @param bool  $copy_orig_recips True to copy all recipients which are on the original
	 *                                message to the attachment by default. False if only the $exception_recips changes should
	 *                                be applied.
	 */
	public function setDeltaExceptionRecipients(mixed $exception, array $exception_recips, bool $copy_orig_recips): void {
		// Check if the recipients from the original message should be copied,
		// if so, open the recipient table of the parent message and apply all
		// rows on the target recipient.
		if ($copy_orig_recips === true) {
			$origTable = mapi_message_getrecipienttable($this->message);
			$recipientRows = mapi_table_queryallrows($origTable, $this->recipprops);
			mapi_message_modifyrecipients($exception, MODRECIP_ADD, $recipientRows);
		}

		// Add organizer to meeting only if it is not organized.
		$msgprops = mapi_getprops($exception, [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']]);
		if (isset($msgprops[$this->proptags['responsestatus']]) && $msgprops[$this->proptags['responsestatus']] != olResponseOrganized) {
			$this->addOrganizer($msgprops, $exception_recips['add']);
		}

		// Remove all deleted recipients
		if (isset($exception_recips['remove'])) {
			foreach ($exception_recips['remove'] as &$recip) {
				if (!isset($recip[PR_RECIPIENT_FLAGS]) || $recip[PR_RECIPIENT_FLAGS] != (recipReserved | recipExceptionalDeleted | recipSendable)) {
					$recip[PR_RECIPIENT_FLAGS] = recipSendable | recipExceptionalDeleted;
				}
				else {
					$recip[PR_RECIPIENT_FLAGS] = recipReserved | recipExceptionalDeleted | recipSendable;
				}
				$recip[PR_RECIPIENT_TRACKSTATUS] = olResponseNone;		// No Response required
			}
			unset($recip);
			mapi_message_modifyrecipients($exception, MODRECIP_MODIFY, $exception_recips['remove']);
		}

		// Add all new recipients
		if (isset($exception_recips['add'])) {
			mapi_message_modifyrecipients($exception, MODRECIP_ADD, $exception_recips['add']);
		}

		// Modify the existing recipients
		if (isset($exception_recips['modify'])) {
			mapi_message_modifyrecipients($exception, MODRECIP_MODIFY, $exception_recips['modify']);
		}
	}

	/**
	 * Function which applies the provided recipients to the exception, also checks for deleted recipients.
	 *
	 * The $exception_recips should be an array containing all recipients which must be applied
	 * to the exception. This will copy all recipients from the original message and then start filter
	 * out all recipients which are not provided by the $exception_recips list.
	 *
	 * @param resource $message          exception attachment of recurring item
	 * @param array    $exception_recips list of recipients
	 */
	public function setAllExceptionRecipients(mixed $message, array $exception_recips): void {
		$deletedRecipients = [];
		$useMessageRecipients = false;

		$recipientTable = mapi_message_getrecipienttable($message);
		$recipientRows = mapi_table_queryallrows($recipientTable, $this->recipprops);

		if (empty($recipientRows)) {
			$useMessageRecipients = true;
			$recipientTable = mapi_message_getrecipienttable($this->message);
			$recipientRows = mapi_table_queryallrows($recipientTable, $this->recipprops);
		}

		// Add organizer to meeting only if it is not organized.
		$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']]);
		if (isset($msgprops[$this->proptags['responsestatus']]) && $msgprops[$this->proptags['responsestatus']] != olResponseOrganized) {
			$this->addOrganizer($msgprops, $exception_recips);
		}

		if (!empty($exception_recips)) {
			foreach ($recipientRows as $recipient) {
				$found = false;
				foreach ($exception_recips as $excep_recip) {
					if (isset($recipient[PR_SEARCH_KEY], $excep_recip[PR_SEARCH_KEY]) && $recipient[PR_SEARCH_KEY] == $excep_recip[PR_SEARCH_KEY]) {
						$found = true;
					}
				}

				if (!$found) {
					$foundInDeletedRecipients = false;
					// Look if the $recipient is in the list of deleted recipients
					if (!empty($deletedRecipients)) {
						foreach ($deletedRecipients as $recip) {
							if (isset($recipient[PR_SEARCH_KEY], $excep_recip[PR_SEARCH_KEY]) && $recip[PR_SEARCH_KEY] == $recipient[PR_SEARCH_KEY]) {
								$foundInDeletedRecipients = true;
								break;
							}
						}
					}

					// If recipient is not in list of deleted recipient, add him
					if (!$foundInDeletedRecipients) {
						if (!isset($recipient[PR_RECIPIENT_FLAGS]) || $recipient[PR_RECIPIENT_FLAGS] != (recipReserved | recipExceptionalDeleted | recipSendable)) {
							$recipient[PR_RECIPIENT_FLAGS] = recipSendable | recipExceptionalDeleted;
						}
						else {
							$recipient[PR_RECIPIENT_FLAGS] = recipReserved | recipExceptionalDeleted | recipSendable;
						}
						$recipient[PR_RECIPIENT_TRACKSTATUS] = olRecipientTrackStatusNone;	// No Response required
						$deletedRecipients[] = $recipient;
					}
				}

				// When $message contains a non-empty recipienttable, we must delete the recipients
				// before re-adding them. However, when $message is doesn't contain any recipients,
				// we are using the recipient table of the original message ($this->message)
				// rather then $message. In that case, we don't need to remove the recipients
				// from the $message, as the recipient table is already empty, and
				// mapi_message_modifyrecipients() will throw an error.
				if ($useMessageRecipients === false) {
					mapi_message_modifyrecipients($message, MODRECIP_REMOVE, [$recipient]);
				}
			}
			$exception_recips = array_merge($exception_recips, $deletedRecipients);
		}
		else {
			$exception_recips = $recipientRows;
		}

		if (!empty($exception_recips)) {
			// Set the new list of recipients on the exception message, this also removes the existing recipients
			mapi_message_modifyrecipients($message, MODRECIP_MODIFY, $exception_recips);
		}
	}

	/**
	 * Function returns basedates of all changed occurrences.
	 *
	 * @return array|false array( 0 => 123459321 )
	 *
	 * @psalm-return false|list<mixed>
	 */
	public function getAllExceptions(): array|false {
		if (!empty($this->recur["changed_occurrences"])) {
			$result = [];
			foreach ($this->recur["changed_occurrences"] as $exception) {
				$result[] = $exception["basedate"];
			}

			return $result;
		}

		return false;
	}

	/**
	 * Function which adds organizer to recipient list which is passed.
	 * This function also checks if it has organizer.
	 *
	 * @param array $messageProps message properties
	 * @param array $recipients   recipients list of message
	 * @param bool  $isException  true if we are processing recipient of exception
	 */
	public function addOrganizer(array $messageProps, array &$recipients, bool $isException = false): void {
		$hasOrganizer = false;
		// Check if meeting already has an organizer.
		foreach ($recipients as $key => $recipient) {
			if (isset($recipient[PR_RECIPIENT_FLAGS]) && $recipient[PR_RECIPIENT_FLAGS] == (recipSendable | recipOrganizer)) {
				$hasOrganizer = true;
			}
			elseif ($isException && !isset($recipient[PR_RECIPIENT_FLAGS])) {
				// Recipients for an occurrence
				$recipients[$key][PR_RECIPIENT_FLAGS] = recipSendable | recipExceptionalResponse;
			}
		}

		if (!$hasOrganizer) {
			// Create organizer.
			$organizer = [];
			$organizer[PR_ENTRYID] = $messageProps[PR_SENT_REPRESENTING_ENTRYID];
			$organizer[PR_DISPLAY_NAME] = $messageProps[PR_SENT_REPRESENTING_NAME];
			$organizer[PR_EMAIL_ADDRESS] = $messageProps[PR_SENT_REPRESENTING_EMAIL_ADDRESS];
			$organizer[PR_RECIPIENT_TYPE] = MAPI_TO;
			$organizer[PR_RECIPIENT_DISPLAY_NAME] = $messageProps[PR_SENT_REPRESENTING_NAME];
			$organizer[PR_ADDRTYPE] = empty($messageProps[PR_SENT_REPRESENTING_ADDRTYPE]) ? 'SMTP' : $messageProps[PR_SENT_REPRESENTING_ADDRTYPE];
			$organizer[PR_RECIPIENT_TRACKSTATUS] = olRecipientTrackStatusNone;
			$organizer[PR_RECIPIENT_FLAGS] = recipSendable | recipOrganizer;
			$organizer[PR_SEARCH_KEY] = $messageProps[PR_SENT_REPRESENTING_SEARCH_KEY];

			// Add organizer to recipients list.
			array_unshift($recipients, $organizer);
		}
	}

	/**
	 * Returns restriction array for filtering embedded message attachments.
	 *
	 * @return array restriction array for ATTACH_EMBEDDED_MSG
	 */
	private function getEmbeddedMessageRestriction(): array {
		return [
			RES_PROPERTY,
			[
				RELOP => RELOP_EQ,
				ULPROPTAG => PR_ATTACH_METHOD,
				VALUE => [PR_ATTACH_METHOD => ATTACH_EMBEDDED_MSG],
			],
		];
	}
}

/*

From http://www.ohelp-one.com/new-6765483-3268.html:

Recurrence Data Structure Offset Type Value

0 ULONG (?) Constant : { 0x04, 0x30, 0x04, 0x30}

4 UCHAR 0x0A + recurrence type: 0x0A for daily, 0x0B for weekly, 0x0C for
monthly, 0x0D for yearly

5 UCHAR Constant: { 0x20}

6 ULONG Seems to be a variant of the recurrence type: 1 for daily every n
days, 2 for daily every weekday and weekly, 3 for monthly or yearly. The
special exception is regenerating tasks that regenerate on a weekly basis: 0
is used in that case (I have no idea why).

Here's the recurrence-type-specific data. Because the daily every N days
data are 4 bytes shorter than the data for the other types, the offsets for
the rest of the data will be 4 bytes off depending on the recurrence type.

Daily every N days:

10 ULONG ( N - 1) * ( 24 * 60). I'm not sure what this is used for, but it's consistent.

14 ULONG N * 24 * 60: minutes between recurrences

18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
regenerating tasks.

Daily every weekday (this is essentially a subtype of weekly recurrence):

10 ULONG 6 * 24 * 60: minutes between recurrences ( a week... sort of)

14 ULONG 1: recur every week (corresponds to the second parameter for weekly
recurrence)

18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
regenerating tasks.

22 ULONG 0x3E: bitmask for recurring every weekday (corresponds to fourth
parameter for weekly recurrence)

Weekly every N weeks for all events and non-regenerating tasks:

10 ULONG 6 * 24 * 60: minutes between recurrences (a week... sort of)

14 ULONG N: recurrence interval

18 ULONG Constant: 0

22 ULONG Bitmask for determining which days of the week the event recurs on
( 1 << dayOfWeek, where Sunday is 0).

Weekly every N weeks for regenerating tasks: 10 ULONG Constant: 0

14 ULONG N * 7 * 24 * 60: recurrence interval in minutes between occurrences

18 ULONG Constant: 1

Monthly every N months on day D:

10 ULONG This is the most complicated value
in the entire mess. It's basically a very complicated way of stating the
recurrence interval. I tweaked fbs' basic algorithm. DateTime::MonthInDays
simply returns the number of days in a given month, e.g. 31 for July for 28
for February (the algorithm doesn't take into account leap years, but it
doesn't seem to matter). My DateTime object, like Microsoft's COleDateTime,
uses 1-based months (i.e. January is 1, not 0). With that in mind, this
works:

long monthIndex = ( ( ( ( 12 % schedule-=GetInterval()) *

( ( schedule-=GetStartDate().GetYear() - 1601) %

schedule-=GetInterval())) % schedule-=GetInterval()) +

( schedule-=GetStartDate().GetMonth() - 1)) % schedule-=GetInterval();

for( int i = 0; i < monthIndex; i++)

{

value += DateTime::GetDaysInMonth( ( i % 12) + 1) * 24 * 60;

}

This should work for any recurrence interval, including those greater than
12.

14 ULONG N: recurrence interval

18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
regenerating tasks.

22 ULONG D: day of month the event recurs on (if this value is greater than
the number of days in a given month [e.g. 31 for and recurs in June], then
the event will recur on the last day of the month)

Monthly every N months on the Xth Y (e.g. "2nd Tuesday"):

10 ULONG See above: same as for monthly every N months on day D

14 ULONG N: recurrence interval

18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
regenerating tasks.

22 ULONG Y: bitmask for determining which day of the week the event recurs
on (see weekly every N weeks). Some useful values are 0x7F for any day, 0x3E
for a weekday, or 0x41 for a weekend day.

26 ULONG X: 1 for first occurrence, 2 for second, etc. 5 for last
occurrence. E.g. for "2nd Tuesday", you should have values of 0x04 for the
prior value and 2 for this one.

Yearly on day D of month M:

10 ULONG M (sort of): This is another messy
value. It's the number of minute since the startning of the year to the
given month. For an explanation of GetDaysInMonth, see monthly every N
months. This will work:

ULONG monthOfYearInMinutes = 0;

for( int i = DateTime::cJanuary; i < schedule-=GetMonth(); i++)

{

monthOfYearInMinutes += DateTime::GetDaysInMonth( i) * 24 * 60;

}



14 ULONG 12: recurrence interval in months. Naturally, 12.

18 ULONG 0 for all events and non-regenerating recurring tasks. 1 for
regenerating tasks.

22 ULONG D: day of month the event recurs on. See monthly every N months on
day D.

Yearly on the Xth Y of month M: 10 ULONG M (sort of): See yearly on day D of
month M.

14 ULONG 12: recurrence interval in months. Naturally, 12.

18 ULONG Constant: 0

22 ULONG Y: see monthly every N months on the Xth Y.

26 ULONG X: see monthly every N months on the Xth Y.

After these recurrence-type-specific values, the offsets will change
depending on the type. For every type except daily every N days, the offsets
will grow by at least 4. For those types using the Xth Y, the offsets will
grow by an additional 4, for a total of 8. The offsets for the rest of these
values will be given for the most basic case, daily every N days, i.e.
without any growth. Adjust as necessary. Also, the presence of exceptions
will change the offsets following the exception data by a variable number of
bytes, so the offsets given in the table are accurate only for those
recurrence patterns without any exceptions.


22 UCHAR Type of pattern termination: 0x21 for terminating on a given date, 0x22 for terminating
after a given number of recurrences, or 0x23 for never terminating
(recurring infinitely)

23 UCHARx3 Constant: { 0x20, 0x00, 0x00}

26 ULONG Number of occurrences in pattern: 0 for infinite recurrence,
otherwise supply the value, even if it terminates on a given date, not after
a given number

30 ULONG Constant: 0

34 ULONG Number of exceptions to pattern (i.e. deleted or changed
occurrences)

.... ULONGxN Base date of each exception, given in hundreds of nanoseconds
since 1601, so see below to turn them into a comprehensible format. The base
date of an exception is the date (and only the date-- not the time) the
exception would have occurred on in the pattern. They must occur in
ascending order.

38 ULONG Number of changed exceptions (i.e. total number of exceptions -
number of deleted exceptions): if there are changed exceptions, again, more
data will be needed, but that will wait

.... ULONGxN Start date (and only the date-- not the time) of each changed
exception, i.e. the exceptions which aren't deleted. These must also occur
in ascending order. If all of the exceptions are deleted, this data will be
absent. If present, they will be in the format above. Any dates that are in
the first list but not in the second are exceptions that have been deleted
(i.e. the difference between the two sets). Note that this is the start date
(including time), not the base date. Given that the values are unordered and
that they can't be matched up against the previous list in this iteration of
the recurrence data (they could in previous ones), it is very difficult to
tell which exceptions are deleted and which are changed. Fortunately, for
this new format, the base dates are given on the attachment representing the
changed exception (described below), so you can simply ignore this list of
changed exceptions. Just create a list of exceptions from the previous list
and assume they're all deleted unless you encounter an attachment with a
matching base date later on.

42 ULONG Start date of pattern given in hundreds of nanoseconds since 1601;
see below for an explanation.

46 ULONG End date of pattern: see start date of pattern

50 ULONG Constant: { 0x06, 0x30, 0x00, 0x00}

NOTE: I find the following 8-byte sequence of bytes to be very useful for
orienting myself when looking at the raw data. If you can find { 0x06, 0x30,
0x00, 0x00, 0x08, 0x30, 0x00, 0x00}, you can use these tables to work either
forwards or backwards to find the data you need. The sequence sort of
delineates certain critical exception-related data and delineates the
exceptions themselves from the rest of the data and is relatively easy to
find. If you're going to be meddling in here a lot, I suggest making a
friend of ol' 0x00003006.

54 UCHAR This number is some kind of version indicator. Use 0x08 for Outlook
2003. I believe 0x06 is Outlook 2000 and possibly 98, while 0x07 is Outlook
XP. This number must be consistent with the features of the data structure
generated by the version of Outlook indicated thereby-- there are subtle
differences between the structures, and, if the version doesn't match the
data, Outlook will sometimes failto read the structure.

55 UCHARx3 Constant: { 0x30, 0x00, 0x00}

58 ULONG Start time of occurrence in minutes: e.g. 0 for midnight or 720 for
12 PM

62 ULONG End time of occurrence in minutes: i.e. start time + duration, e.g.
900 for an event that starts at 12 PM and ends at 3PM

Exception Data 66 USHORT Number of changed exceptions: essentially a check
on the prior occurrence of this value; should be equivalent.

NOTE: The following structure will occur N many times (where N = number of
changed exceptions), and each structure can be of variable length.

.... ULONG Start date of changed exception given in hundreds of nanoseconds
since 1601

.... ULONG End date of changed exception given in hundreds of nanoseconds
since 1601

.... ULONG This is a value I don't clearly understand. It seems to be some
kind of archival value that matches the start time most of the time, but
will lag behind when the start time is changed and then match up again under
certain conditions later. In any case, setting to the same value as the
start time seems to work just fine (more information on this value would be
appreciated).

.... USHORT Bitmask of changes to the exception (see below). This will be 0
if the only changes to the exception were to its start or end time.

.... ULONGxN Numeric values (e.g. label or minutes to remind before the
event) changed in the exception. These will occur in the order of their
corresponding bits (see below). If no numeric values were changed, then
these values will be absent.

NOTE: The following three values constitute a single sub-structure that will
occur N many times, where N is the number of strings that are changed in the
exception. Since there are at most 2 string values that can be excepted
(i.e. subject [or description], and location), there can at most be two of
these, but there may be none.

.... USHORT Length of changed string value with NULL character

.... USHORT Length of changed string value without NULL character (i.e.
previous value - 1)

.... CHARxN Changed string value (without NULL terminator)

Unicode Data NOTE: If a string value was changed on an exception, those
changed string values will reappear here in Unicode format after 8 bytes of
NULL padding (possibly a Unicode terminator?). For each exception with a
changed string value, there will be an identifier, followed by the changed
strings in Unicode. The strings will occur in the order of their
corresponding bits (see below). E.g., if both subject and location were
changed in the exception, there would be the 3-ULONG identifier, then the
length of the subject, then the subject, then the length of the location,
then the location.

70 ULONGx2 Constant: { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}. This
padding serves as a barrier between the older data structure and the
appended Unicode data. This is the same sequence as the Unicode terminator,
but I'm not sure whether that's its identity or not.

.... ULONGx3 These are the three times used to identify the exception above:
start date, end date, and repeated start date. These should be the same as
they were above.

.... USHORT Length of changed string value without NULL character. This is
given as count of WCHARs, so it should be identical to the value above.

.... WCHARxN Changed string value in Unicode (without NULL terminator)

Terminator ... ULONGxN Constant: { 0x00, 0x00, 0x00, 0x00}. 4 bytes of NULL
padding per changed exception. If there were no changed exceptions, all
you'll need is the final terminator below.

.... ULONGx2 Constant: { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}.

*/
