Beispiel #1
0
    // And we don't return from that.
}
$qry = new AwlQuery('BEGIN');
if (!$qry->Exec('move')) {
    rollback(500);
}
$src_name = $src->dav_name();
$dst_name = $dest->IsBinding() ? $dest->bound_from() : $dest->dav_name();
$src_collection = $src->GetProperty('collection_id');
$dst_collection = $dest->GetProperty('collection_id');
$src_user_no = $src->GetProperty('user_no');
$dst_user_no = $dest->GetProperty('user_no');
$cache = getCacheInstance();
$cachekeys = array();
if ($src->IsCollection()) {
    $cachekeys[] = ($src->ContainerType() == 'principal' ? 'principal' : 'collection') . '-' . $src->parent_path();
    $cachekeys[] = ($src->IsPrincipal() == 'principal' ? 'principal' : 'collection') . '-' . $src->dav_name();
    $cachekeys[] = ($src->IsPrincipal() ? 'principal' : 'collection') . '-' . $dest->dav_name();
    if ($dest->Exists()) {
        $qry = new AwlQuery('DELETE FROM collection WHERE dav_name = :dst_name', array(':dst_name' => $dst_name));
        if (!$qry->Exec('move')) {
            rollback(500);
        }
    }
    /** @todo Need to confirm this will work correctly if we move this into another user's hierarchy. */
    $sql = 'UPDATE collection SET dav_name = :dst_name ';
    $params = array(':dst_name' => $dst_name);
    if ($src_user_no != $dst_user_no) {
        $sql .= ', user_no = :dst_user_no ';
        $params[':dst_user_no'] = $dst_user_no;
    }
/**
* Create/Update the scheduling requests for this resource.  This includes updating
* the scheduled user's default calendar.
* @param vComponent $resource The VEVENT/VTODO/... resource we are scheduling
* @param boolean $create true if the scheduling requests are being created.
* @return true If there was any scheduling action
*/
function do_scheduling_requests(vCalendar $resource, $create, $old_data = null, $remoteAttendee = false)
{
    global $request, $c;
    if (!isset($request) || isset($c->enable_auto_schedule) && !$c->enable_auto_schedule) {
        return false;
    }
    if (!is_object($resource)) {
        trace_bug('do_scheduling_requests called with non-object parameter (%s)', gettype($resource));
        return false;
    }
    $organizer = $resource->GetOrganizer();
    if ($organizer === false || empty($organizer)) {
        dbg_error_log('PUT', 'Event has no organizer - no scheduling required.');
        return false;
    }
    $organizer_email = preg_replace('/^mailto:/i', '', $organizer->Value());
    if ($request->principal->email() != $organizer_email) {
        return do_scheduling_reply($resource, $organizer);
    }
    $schedule_request = clone $resource;
    $schedule_request->AddProperty('METHOD', 'REQUEST');
    $old_attendees = array();
    if (!empty($old_data)) {
        $old_resource = new vCalendar($old_data);
        $old_attendees = $old_resource->GetAttendees();
    }
    $attendees = $resource->GetAttendees();
    if (count($attendees) == 0 && count($old_attendees) == 0) {
        dbg_error_log('PUT', 'Event has no attendees - no scheduling required.', count($attendees));
        return false;
    }
    $removed_attendees = array();
    foreach ($old_attendees as $attendee) {
        $email = preg_replace('/^mailto:/i', '', $attendee->Value());
        if ($email == $request->principal->email()) {
            continue;
        }
        $removed_attendees[$email] = $attendee;
    }
    $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();
    dbg_error_log('PUT', 'Writing scheduling resources for %d attendees', count($attendees));
    $scheduling_actions = false;
    foreach ($attendees as $attendee) {
        $email = preg_replace('/^mailto:/i', '', $attendee->Value());
        if ($email == $request->principal->email()) {
            dbg_error_log("PUT", "not delivering to owner '%s'", $request->principal->email());
            continue;
        }
        if ($create) {
            $attendee_is_new = true;
        } else {
            $attendee_is_new = !isset($removed_attendees[$email]);
            if (!$attendee_is_new) {
                unset($removed_attendees[$email]);
            }
        }
        $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;
        }
        $schedule_target = new Principal('email', $email);
        $response = '3.7';
        // Attendee was not found on server.
        dbg_error_log('PUT', 'Handling scheduling resources for %s on %s which is %s', $email, $create ? 'create' : 'update', $attendee_is_new ? 'new' : 'an update');
        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.
            $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, $schedule_target->user_no(), $uid);
            if (!$qry->Exec('PUT', __LINE__, __FILE__) || $qry->rows() < 1) {
                dbg_error_log('PUT', "Could not find event in attendee's calendars");
                $attendee_calendar = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-default-calendar')));
            } else {
                $row = $qry->Fetch();
                $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, $attendee_is_new) !== false) {
                        $response = '1.2';
                        // Scheduling invitation delivered successfully
                        if ($attendee_calendar->WriteCalendarMember($resource, $attendee_is_new) === false) {
                            dbg_error_log('ERROR', 'Could not write %s calendar member to %s', $attendee_is_new ? 'new' : 'updated', $attendee_calendar->dav_name(), $attendee_calendar->dav_name(), $schedule_target->username());
                            trace_bug('Failed to write scheduling resource.');
                        }
                    }
                }
            }
        } else {
            if ($remoteAttendee) {
                $attendee->is_remote = true;
                $remote = new iSchedule();
                $answer = $remote->sendRequest($email, 'VEVENT/REQUEST', $schedule_request->Render());
            } else {
                $remote = new iSchedule();
                $answer = $remote->sendRequest($email, 'VEVENT/REQUEST', $schedule_request->Render());
                if ($answer === false) {
                    $response = "3.7;Invalid Calendar User";
                } else {
                    foreach ($answer as $a) {
                        if ($a === false) {
                            $response = "3.7;Invalid Calendar User";
                        } elseif (substr($a, 0, 1) >= 1) {
                            $response = $a;
                        } else {
                            $response = "2.0;Success";
                        }
                    }
                }
            }
        }
        dbg_error_log('PUT', 'Status for attendee <%s> set to "%s"', $attendee->Value(), $response);
        $attendee->SetParameterValue('SCHEDULE-STATUS', $response);
        $scheduling_actions = true;
    }
    if (!$create) {
        foreach ($removed_attendees as $attendee) {
            $schedule_target = new Principal('email', $email);
            if ($schedule_target->Exists()) {
                $attendee_calendar = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-default-calendar')));
            }
        }
    }
    return $scheduling_actions;
}