예제 #1
0
/**
 * Do the scheduling adjustments for a REPLY when an ATTENDEE updates their status.
 * @param vCalendar $resource The resource that the ATTENDEE is writing to their calendar
 * @param string $organizer The property which is the event ORGANIZER.
 */
function do_scheduling_reply(vCalendar $resource, vProperty $organizer)
{
    global $request;
    $organizer_email = preg_replace('/^mailto:/i', '', $organizer->Value());
    $organizer_principal = new Principal('email', $organizer_email);
    if (!$organizer_principal->Exists()) {
        dbg_error_log('PUT', 'Organizer "%s" not found - cannot perform scheduling reply.', $organizer);
        return false;
    }
    $sql = 'SELECT caldav_data.dav_name, caldav_data.caldav_data FROM caldav_data JOIN calendar_item USING(dav_id) ';
    $sql .= 'WHERE caldav_data.collection_id IN (SELECT collection_id FROM collection WHERE is_calendar AND user_no =?) ';
    $sql .= 'AND uid=? LIMIT 1';
    $uids = $resource->GetPropertiesByPath('/VCALENDAR/*/UID');
    if (count($uids) == 0) {
        dbg_error_log('PUT', 'No UID in VCALENDAR - giving up on REPLY.');
        return false;
    }
    $uid = $uids[0]->Value();
    $qry = new AwlQuery($sql, $organizer_principal->user_no(), $uid);
    if (!$qry->Exec('PUT', __LINE__, __FILE__) || $qry->rows() < 1) {
        dbg_error_log('PUT', 'Could not find original event from organizer - giving up on REPLY.');
        return false;
    }
    $row = $qry->Fetch();
    $attendees = $resource->GetAttendees();
    foreach ($attendees as $v) {
        $email = preg_replace('/^mailto:/i', '', $v->Value());
        if ($email == $request->principal->email()) {
            $attendee = $v;
        }
    }
    if (empty($attendee)) {
        dbg_error_log('PUT', 'Could not find ATTENDEE in VEVENT - giving up on REPLY.');
        return false;
    }
    $schedule_original = new vCalendar($row->caldav_data);
    $schedule_original->UpdateAttendeeStatus($request->principal->email(), clone $attendee);
    $collection_path = preg_replace('{/[^/]+$}', '/', $row->dav_name);
    $segment_name = str_replace($collection_path, '', $row->dav_name);
    $organizer_calendar = new WritableCollection(array('path' => $collection_path));
    $organizer_inbox = new WritableCollection(array('path' => $organizer_principal->internal_url('schedule-inbox')));
    $schedule_reply = clone $schedule_original;
    $schedule_reply->AddProperty('METHOD', 'REPLY');
    dbg_error_log('PUT', 'Writing scheduling REPLY from %s to %s', $request->principal->email(), $organizer_principal->email());
    $response = '3.7';
    // Organizer was not found on server.
    if (!$organizer_calendar->Exists()) {
        dbg_error_log('ERROR', 'Default calendar at "%s" does not exist for user "%s"', $organizer_calendar->dav_name(), $schedule_target->username());
        $response = '5.2';
        // No scheduling support for user
    } else {
        if (!$organizer_inbox->HavePrivilegeTo('schedule-deliver-reply')) {
            $response = '3.8';
            // No authority to deliver replies to organizer.
        } else {
            if ($organizer_inbox->WriteCalendarMember($schedule_reply, false, false, $request->principal->username() . $segment_name) !== false) {
                $response = '1.2';
                // Scheduling reply delivered successfully
                if ($organizer_calendar->WriteCalendarMember($schedule_original, false, false, $segment_name) === false) {
                    dbg_error_log('ERROR', 'Could not write updated calendar member to %s', $attendee_calendar->dav_name(), $attendee_calendar->dav_name(), $schedule_target->username());
                    trace_bug('Failed to write scheduling resource.');
                }
            }
        }
    }
    $schedule_request = clone $schedule_original;
    $schedule_request->AddProperty('METHOD', 'REQUEST');
    dbg_error_log('PUT', 'Status for organizer <%s> set to "%s"', $organizer->Value(), $response);
    $organizer->SetParameterValue('SCHEDULE-STATUS', $response);
    $resource->UpdateOrganizerStatus($organizer);
    $scheduling_actions = true;
    $calling_attendee = clone $attendee;
    $attendees = $schedule_original->GetAttendees();
    foreach ($attendees as $attendee) {
        $email = preg_replace('/^mailto:/i', '', $attendee->Value());
        if ($email == $request->principal->email() || $email == $organizer_principal->email()) {
            continue;
        }
        $agent = $attendee->GetParameterValue('SCHEDULE-AGENT');
        if ($agent && $agent != 'SERVER') {
            dbg_error_log("PUT", "not delivering to %s, schedule agent set to value other than server", $email);
            continue;
        }
        // an attendee's reply should modify only the PARTSTAT on other attendees' objects
        // other properties (that might have been adjusted individually by those other
        // attendees) should remain unmodified. Therefore, we have to make $schedule_original
        // and $schedule_request be initialized by each attendee's object here.
        $attendee_principal = new DAVPrincipal(array('email' => $email, 'options' => array('allow_by_email' => true)));
        if ($attendee_principal == false) {
            dbg_error_log('PUT', 'Could not find attendee %s', $email);
            continue;
        }
        $sql = 'SELECT caldav_data.dav_name, caldav_data.caldav_data, caldav_data.collection_id FROM caldav_data JOIN calendar_item USING(dav_id) ';
        $sql .= 'WHERE caldav_data.collection_id IN (SELECT collection_id FROM collection WHERE is_calendar AND user_no =?) ';
        $sql .= 'AND uid=? LIMIT 1';
        $qry = new AwlQuery($sql, $attendee_principal->user_no(), $uid);
        if (!$qry->Exec('PUT', __LINE__, __FILE__) || $qry->rows() < 1) {
            dbg_error_log('PUT', "Could not find attendee's event %s", $uid);
        }
        $row = $qry->Fetch();
        $schedule_original = new vCalendar($row->caldav_data);
        $schedule_original->UpdateAttendeeStatus($request->principal->email(), clone $calling_attendee);
        $schedule_request = clone $schedule_original;
        $schedule_request->AddProperty('METHOD', 'REQUEST');
        $schedule_target = new Principal('email', $email);
        $response = '3.7';
        // Attendee was not found on server.
        if ($schedule_target->Exists()) {
            // Instead of always writing to schedule-default-calendar, we first try to
            // find a calendar with an existing instance of the event in any calendar of this attendee.
            $r = new DAVResource($row);
            $attendee_calendar = new WritableCollection(array('path' => $r->parent_path()));
            if ($attendee_calendar->IsCalendar()) {
                dbg_error_log('XXX', "found the event in attendee's calendar %s", $attendee_calendar->dav_name());
            } else {
                dbg_error_log('XXX', 'could not find the event in any calendar, using schedule-default-calendar');
                $attendee_calendar = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-default-calendar')));
            }
            if (!$attendee_calendar->Exists()) {
                dbg_error_log('ERROR', 'Default calendar at "%s" does not exist for user "%s"', $attendee_calendar->dav_name(), $schedule_target->username());
                $response = '5.2';
                // No scheduling support for user
            } else {
                $attendee_inbox = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-inbox')));
                if (!$attendee_inbox->HavePrivilegeTo('schedule-deliver-invite')) {
                    $response = '3.8';
                    //  No authority to deliver invitations to user.
                } else {
                    if ($attendee_inbox->WriteCalendarMember($schedule_request, false) !== false) {
                        $response = '1.2';
                        // Scheduling invitation delivered successfully
                        if ($attendee_calendar->WriteCalendarMember($schedule_original, false) === false) {
                            dbg_error_log('ERROR', 'Could not write updated calendar member to %s', $attendee_calendar->dav_name(), $attendee_calendar->dav_name(), $schedule_target->username());
                            trace_bug('Failed to write scheduling resource.');
                        }
                    }
                }
            }
        }
        dbg_error_log('PUT', 'Status for attendee <%s> set to "%s"', $attendee->Value(), $response);
        $attendee->SetParameterValue('SCHEDULE-STATUS', $response);
        $scheduling_actions = true;
        $resource->UpdateAttendeeStatus($email, clone $attendee);
    }
    return $scheduling_actions;
}
예제 #2
0
function doItipAttendeeReply(vCalendar $resource, $partstat)
{
    global $request;
    $organizer = $resource->GetOrganizer();
    $organizer_email = preg_replace('/^mailto:/i', '', $organizer->Value());
    $organizer_principal = new Principal('email', $organizer_email);
    if (!$organizer_principal->Exists()) {
        dbg_error_log('schedule', 'Unknown ORGANIZER "%s" - unable to notify.', $organizer->Value());
        header("Debug: Could maybe do the iMIP message dance for organizer " . $organizer->Value());
        return true;
    }
    $sql = 'SELECT caldav_data.dav_name, caldav_data.caldav_data FROM caldav_data JOIN calendar_item USING(dav_id) ';
    $sql .= 'WHERE caldav_data.collection_id IN (SELECT collection_id FROM collection WHERE is_calendar AND user_no =?) ';
    $sql .= 'AND uid=? LIMIT 1';
    $uids = $resource->GetPropertiesByPath('/VCALENDAR/*/UID');
    if (count($uids) == 0) {
        dbg_error_log('schedule', 'No UID in VCALENDAR - giving up on REPLY.');
        return true;
    }
    $uid = $uids[0]->Value();
    $qry = new AwlQuery($sql, $organizer_principal->user_no(), $uid);
    if (!$qry->Exec('schedule', __LINE__, __FILE__) || $qry->rows() < 1) {
        dbg_error_log('schedule', 'Could not find original event from organizer - giving up on REPLY.');
        return true;
    }
    $row = $qry->Fetch();
    $collection_path = preg_replace('{/[^/]+$}', '/', $row->dav_name);
    $segment_name = str_replace($collection_path, '', $row->dav_name);
    $vcal = new vCalendar($row->caldav_data);
    $attendees = $vcal->GetAttendees();
    foreach ($attendees as $v) {
        $email = preg_replace('/^mailto:/i', '', $v->Value());
        if ($email == $request->principal->email()) {
            $attendee = $v;
            break;
        }
    }
    if (empty($attendee)) {
        dbg_error_log('schedule', 'Could not find ATTENDEE in VEVENT - giving up on REPLY.');
        return true;
    }
    $attendee->SetParameterValue('PARTSTAT', $partstat);
    $attendee->SetParameterValue('SCHEDULE-STATUS', '2.0');
    $vcal->UpdateAttendeeStatus($request->principal->email(), clone $attendee);
    $organizer_calendar = new WritableCollection(array('path' => $collection_path));
    $organizer_inbox = new WritableCollection(array('path' => $organizer_principal->internal_url('schedule-inbox')));
    $schedule_reply = GetItip(new vCalendar($row->caldav_data), 'REPLY', $attendee->Value());
    $schedule_request = GetItip(new vCalendar($row->caldav_data), 'REQUEST', null);
    dbg_error_log('schedule', 'Writing ATTENDEE scheduling REPLY from %s to %s', $request->principal->email(), $organizer_principal->email());
    $response = '3.7';
    // Organizer was not found on server.
    if (!$organizer_calendar->Exists()) {
        if (doImipMessage('REPLY', $organizer_principal->email(), $vcal)) {
            $response = '1.1';
            // Scheduling whoosit 'Sent'
        } else {
            dbg_error_log('ERROR', 'Default calendar at "%s" does not exist for user "%s"', $organizer_calendar->dav_name(), $schedule_target->username());
            $response = '5.2';
            // No scheduling support for user
        }
    } else {
        if (!$organizer_inbox->HavePrivilegeTo('schedule-deliver-reply')) {
            $response = '3.8';
            // No authority to deliver replies to organizer.
        }
        $response = '1.2';
        // Scheduling reply delivered successfully
        if ($organizer_calendar->WriteCalendarMember($vcal, false, false, $segment_name) === false) {
            dbg_error_log('ERROR', 'Could not write updated calendar member to %s', $attendee_calendar->dav_name());
            trace_bug('Failed to write scheduling resource.');
        }
        $organizer_inbox->WriteCalendarMember($schedule_reply, false, false, $request->principal->username() . $segment_name);
    }
    dbg_error_log('schedule', 'Status for organizer <%s> set to "%s"', $organizer->Value(), $response);
    $organizer->SetParameterValue('SCHEDULE-STATUS', $response);
    $resource->UpdateOrganizerStatus($organizer);
    // Which was passed in by reference, and we're updating it here.
    // Now we loop through the *other* ATTENDEEs, updating them on the status of the ATTENDEE DECLINE/ACCEPT
    foreach ($attendees as $attendee) {
        $email = preg_replace('/^mailto:/i', '', $attendee->Value());
        if ($email == $request->principal->email() || $email == $organizer_principal->email()) {
            continue;
        }
        $agent = $attendee->GetParameterValue('SCHEDULE-AGENT');
        if (!empty($agent) && $agent != 'SERVER') {
            continue;
        }
        $schedule_target = new Principal('email', $email);
        if ($schedule_target->Exists()) {
            $attendee_calendar = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-default-calendar')));
            if (!$attendee_calendar->Exists()) {
                dbg_error_log('ERROR', 'Default calendar at "%s" does not exist for user "%s"', $attendee_calendar->dav_name(), $schedule_target->username());
                continue;
            } else {
                $attendee_inbox = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-inbox')));
                if (!$attendee_inbox->HavePrivilegeTo('schedule-deliver-invite')) {
                    continue;
                }
                if ($attendee_calendar->WriteCalendarMember($vcal, false) === false) {
                    dbg_error_log('ERROR', 'Could not write updated calendar member to %s', $attendee_calendar->dav_name());
                    trace_bug('Failed to write scheduling resource.');
                }
                $attendee_inbox->WriteCalendarMember($schedule_request, false);
            }
        } else {
            header("Debug: Could maybe do the iMIP message dance for attendee " . $email);
        }
    }
    return true;
}