protected function processMessageReply(Message $itipMessage, VCalendar $existingObject = null)
{
// A reply can only be processed based on an existing object.
// If the object is not available, the reply is ignored.
if (!$existingObject) {
return null;
}
$instances = array();
$requestStatus = '2.0';
// Finding all the instances the attendee replied to.
foreach ($itipMessage->message->VEVENT as $vevent) {
$recurId = isset($vevent->{'RECURRENCE-ID'}) ? $vevent->{'RECURRENCE-ID'}->getValue() : 'master';
$attendee = $vevent->ATTENDEE;
$instances[$recurId] = $attendee['PARTSTAT']->getValue();
if (isset($vevent->{'REQUEST-STATUS'})) {
$requestStatus = $vevent->{'REQUEST-STATUS'}->getValue();
list($requestStatus) = explode(';', $requestStatus);
}
}
// Now we need to loop through the original organizer event, to find
// all the instances where we have a reply for.
$masterObject = null;
foreach ($existingObject->VEVENT as $vevent) {
$recurId = isset($vevent->{'RECURRENCE-ID'}) ? $vevent->{'RECURRENCE-ID'}->getValue() : 'master';
if ($recurId === 'master') {
$masterObject = $vevent;
}
if (isset($instances[$recurId])) {
$attendeeFound = false;
if (isset($vevent->ATTENDEE)) {
foreach ($vevent->ATTENDEE as $attendee) {
if ($attendee->getValue() === $itipMessage->sender) {
$attendeeFound = true;
$attendee['PARTSTAT'] = $instances[$recurId];
$attendee['SCHEDULE-STATUS'] = $requestStatus;
// Un-setting the RSVP status, because we now know
// that the attende already replied.
unset($attendee['RSVP']);
break;
}
}
}
if (!$attendeeFound) {
// Adding a new attendee. The iTip documentation calls this
// a party crasher.
$attendee = $vevent->add('ATTENDEE', $itipMessage->sender, array('PARTSTAT' => $instances[$recurId]));
if ($itipMessage->senderName) {
$attendee['CN'] = $itipMessage->senderName;
}
}
unset($instances[$recurId]);
}
}
if (!$masterObject) {
// No master object, we can't add new instances.
return null;
}
// If we got replies to instances that did not exist in the
// original list, it means that new exceptions must be created.
foreach ($instances as $recurId => $partstat) {
$recurrenceIterator = new EventIterator($existingObject, $itipMessage->uid);
$found = false;
$iterations = 1000;
do {
$newObject = $recurrenceIterator->getEventObject();
$recurrenceIterator->next();
if (isset($newObject->{'RECURRENCE-ID'}) && $newObject->{'RECURRENCE-ID'}->getValue() === $recurId) {
$found = true;
}
$iterations--;
} while ($recurrenceIterator->valid() && !$found && $iterations);
// Invalid recurrence id. Skipping this object.
if (!$found) {
continue;
}
unset($newObject->RRULE, $newObject->EXDATE, $newObject->RDATE);
$attendeeFound = false;
if (isset($newObject->ATTENDEE)) {
foreach ($newObject->ATTENDEE as $attendee) {
if ($attendee->getValue() === $itipMessage->sender) {
$attendeeFound = true;
$attendee['PARTSTAT'] = $partstat;
break;
}
}
}
if (!$attendeeFound) {
// Adding a new attendee
$attendee = $newObject->add('ATTENDEE', $itipMessage->sender, array('PARTSTAT' => $partstat));
if ($itipMessage->senderName) {
$attendee['CN'] = $itipMessage->senderName;
}
}
$existingObject->add($newObject);
}
return $existingObject;
}