12require_once(
'vCalendar.php');
13require_once(
'WritableCollection.php');
14require_once(
'RRule.php');
24function do_scheduling_for_delete(
DAVResource $deleted_resource ) {
29 if ( !isset($request) || (isset($c->enable_auto_schedule) && !$c->enable_auto_schedule) )
return true;
32 $caldav_data = $deleted_resource->
GetProperty(
'dav-data');
33 if ( empty($caldav_data) )
return true;
35 $vcal =
new vCalendar($caldav_data);
36 $organizer = $vcal->GetOrganizer();
37 if ( $organizer ===
false || empty($organizer) ) {
38 dbg_error_log(
'schedule',
'Event has no organizer - no scheduling required.' );
41 if ( $vcal->GetScheduleAgent() !=
'SERVER' ) {
42 dbg_error_log(
'schedule',
'SCHEDULE-AGENT=%s - no scheduling required.', $vcal->GetScheduleAgent() );
45 $organizer_email = preg_replace(
'/^mailto:/i',
'', $organizer->Value() );
47 if ( $request->principal->email() == $organizer_email ) {
48 return doItipOrganizerCancel( $vcal );
51 if ( isset($_SERVER[
'HTTP_SCHEDULE_REPLY']) && $_SERVER[
'HTTP_SCHEDULE_REPLY'] ==
'F') {
52 dbg_error_log(
'schedule',
'Schedule-Reply header set to "F" - no scheduling required.' );
55 return doItipAttendeeReply( $vcal,
'DECLINED', $request->principal->email());
67function doItipAttendeeReply( vCalendar $resource, $partstat ) {
70 $organizer = $resource->GetOrganizer();
71 $organizer_email = preg_replace(
'/^mailto:/i',
'', $organizer->Value() );
72 $organizer_principal =
new Principal(
'email',$organizer_email );
74 if ( !$organizer_principal->Exists() ) {
75 dbg_error_log(
'schedule',
'Unknown ORGANIZER "%s" - unable to notify.', $organizer->Value() );
80 $sql =
'SELECT caldav_data.dav_name, caldav_data.caldav_data FROM caldav_data JOIN calendar_item USING(dav_id) ';
81 $sql .=
'WHERE caldav_data.collection_id IN (SELECT collection_id FROM collection WHERE is_calendar AND user_no =?) ';
82 $sql .=
'AND uid=? LIMIT 1';
83 $uids = $resource->GetPropertiesByPath(
'/VCALENDAR/*/UID');
84 if ( count($uids) == 0 ) {
85 dbg_error_log(
'schedule',
'No UID in VCALENDAR - giving up on REPLY.' );
88 $uid = $uids[0]->Value();
89 $qry =
new AwlQuery($sql, $organizer_principal->user_no(), $uid);
90 if ( !$qry->Exec(
'schedule',__LINE__,__FILE__) || $qry->rows() < 1 ) {
91 dbg_error_log(
'schedule',
'Could not find original event from organizer - giving up on REPLY.' );
95 $collection_path = preg_replace(
'{/[^/]+$}',
'/', $row->dav_name );
96 $segment_name = str_replace($collection_path,
'', $row->dav_name );
97 $vcal =
new vCalendar($row->caldav_data);
99 $attendees = $vcal->GetAttendees();
100 foreach( $attendees AS $v ) {
101 $email = preg_replace(
'/^mailto:/i',
'', $v->Value() );
102 if ( $email == $request->principal->email() ) {
107 if ( empty($attendee) ) {
108 dbg_error_log(
'schedule',
'Could not find ATTENDEE in VEVENT - giving up on REPLY.' );
112 $attendee->SetParameterValue(
'PARTSTAT', $partstat);
113 $attendee->SetParameterValue(
'SCHEDULE-STATUS',
'2.0');
114 $vcal->UpdateAttendeeStatus($request->principal->email(), clone($attendee) );
117 $organizer_inbox =
new WritableCollection(array(
'path' => $organizer_principal->internal_url(
'schedule-inbox')));
119 $schedule_reply = GetItip(
new vCalendar($vcal->Render(
null,
true)),
'REPLY', $attendee->Value(), array(
'CUTYPE'=>
true,
'SCHEDULE-STATUS'=>
true));
120 $schedule_request = GetItip(
new vCalendar($row->caldav_data),
'REQUEST',
null);
122 dbg_error_log(
'schedule',
'Writing ATTENDEE scheduling REPLY from %s to %s', $request->principal->email(), $organizer_principal->email() );
125 if ( !$organizer_calendar->Exists() ) {
126 if ( doImipMessage(
'REPLY', $organizer_principal->email(), $vcal) ) {
130 dbg_error_log(
'ERROR',
'Default calendar at "%s" does not exist for user "%s"',
131 $organizer_calendar->dav_name(), $schedule_target->username());
136 if ( ! $organizer_inbox->HavePrivilegeTo(
'schedule-deliver-reply') ) {
140 if ( $organizer_calendar->WriteCalendarMember($vcal,
false,
false, $segment_name) ===
false ) {
141 dbg_error_log(
'ERROR',
'Could not write updated calendar member to %s', $attendee_calendar->dav_name() );
142 trace_bug(
'Failed to write scheduling resource.');
144 $organizer_inbox->WriteCalendarMember($schedule_reply,
false,
false, $request->principal->username().$segment_name);
148 dbg_error_log(
'schedule',
'Status for organizer <%s> set to "%s"', $organizer->Value(), $response );
149 $organizer->SetParameterValue(
'SCHEDULE-STATUS', $response );
150 $resource->UpdateOrganizerStatus($organizer);
153 foreach( $attendees AS $attendee ) {
154 $email = preg_replace(
'/^mailto:/i',
'', $attendee->Value() );
155 if ( $email == $request->principal->email() || $email == $organizer_principal->email() )
continue;
157 $agent = $attendee->GetParameterValue(
'SCHEDULE-AGENT');
158 if ( !empty($agent) && $agent !=
'SERVER' )
continue;
160 $schedule_target =
new Principal(
'email',$email);
161 if ( $schedule_target->Exists() ) {
162 $attendee_calendar =
new WritableCollection(array(
'path' => $schedule_target->internal_url(
'schedule-default-calendar')));
163 if ( !$attendee_calendar->Exists() ) {
164 dbg_error_log(
'ERROR',
'Default calendar at "%s" does not exist for user "%s"',
165 $attendee_calendar->dav_name(), $schedule_target->username());
169 $attendee_inbox =
new WritableCollection(array(
'path' => $schedule_target->internal_url(
'schedule-inbox')));
170 if ( ! $attendee_inbox->HavePrivilegeTo(
'schedule-deliver-invite') )
continue;
172 if ( $attendee_calendar->WriteCalendarMember($vcal,
false) ===
false ) {
173 dbg_error_log(
'ERROR',
'Could not write updated calendar member to %s', $attendee_calendar->dav_name());
174 trace_bug(
'Failed to write scheduling resource.');
176 $attendee_inbox->WriteCalendarMember($schedule_request,
false);
187function GetItip( VCalendar $vcal, $method, $attendee_value, $clear_attendee_parameters =
null ) {
188 $iTIP = $vcal->GetItip($method, $attendee_value, $clear_attendee_parameters );
189 $components = $iTIP->GetComponents();
190 foreach( $components AS $comp ) {
191 $comp->AddProperty(
'REQUEST-STATUS',
'2.0');
192 $properties = array();
193 foreach( $comp->GetProperties() AS $k=> $property ) {
194 switch( $property->Name() ) {
199 $new_prop =
new vProperty($property->Name());
200 $new_prop->Value($when->UTC());
201 $properties[] = $new_prop;
204 $properties[] = $property;
207 $comp->SetProperties($properties);
217function doItipOrganizerCancel( vCalendar $vcal ) {
220 $attendees = $vcal->GetAttendees();
221 if ( count($attendees) == 0 ) {
222 dbg_error_log(
'schedule',
'Event has no attendees - no scheduling required.', count($attendees) );
226 dbg_error_log(
'schedule',
'Writing scheduling resources for %d attendees', count($attendees) );
227 $scheduling_actions =
false;
229 $iTIP = GetItip($vcal,
'CANCEL',
null);
231 foreach( $attendees AS $attendee ) {
232 $email = preg_replace(
'/^mailto:/i',
'', $attendee->Value() );
233 if ( $email == $request->principal->email() ) {
234 dbg_error_log(
'schedule',
"not delivering to owner '%s'", $request->principal->email() );
238 $agent = $attendee->GetParameterValue(
'SCHEDULE-AGENT');
239 if ( $agent && $agent !=
'SERVER' ) {
240 dbg_error_log(
'schedule',
"not delivering to %s, schedule agent set to value other than server", $email );
243 $schedule_target =
new Principal(
'email',$email);
244 if ( !$schedule_target->Exists() ) {
245 if ( doImipMessage(
'CANCEL', $email, $vcal) ) {
253 $attendee_inbox =
new WritableCollection(array(
'path' => $schedule_target->internal_url(
'schedule-inbox')));
254 if ( ! $attendee_inbox->HavePrivilegeTo(
'schedule-deliver-invite') ) {
255 dbg_error_log(
'schedule',
"No authority to deliver invite to %s", $schedule_target->internal_url(
'schedule-inbox') );
259 $attendee_calendar =
new WritableCollection(array(
'path' => $schedule_target->internal_url(
'schedule-default-calendar')));
260 $response = processItipCancel( $vcal, $attendee, $attendee_calendar, $schedule_target );
261 deliverItipCancel( $iTIP, $attendee, $attendee_inbox );
264 dbg_error_log(
'schedule',
'Status for attendee <%s> set to "%s"', $attendee->Value(), $response );
265 $attendee->SetParameterValue(
'SCHEDULE-STATUS', $response );
266 $scheduling_actions =
true;
281function processItipCancel( vCalendar $vcal, vProperty $attendee,
WritableCollection $attendee_calendar,
Principal $attendee_principal ) {
284 dbg_error_log(
'schedule',
'Processing iTIP CANCEL to %s', $attendee->Value());
286 if ( !$attendee_calendar->
Exists() ) {
287 if ( doImipMessage(
'CANCEL', $attendee_principal->
email(), $vcal) ) {
291 dbg_error_log(
'ERROR',
'Default calendar at "%s" does not exist for attendee "%s"',
292 $attendee_calendar->
dav_name(), $attendee->Value());
297 $sql =
'SELECT caldav_data.dav_name FROM caldav_data JOIN calendar_item USING(dav_id) ';
298 $sql .=
'WHERE caldav_data.collection_id IN (SELECT collection_id FROM collection WHERE is_calendar AND user_no =?) ';
299 $sql .=
'AND uid=? LIMIT 1';
300 $uids = $vcal->GetPropertiesByPath(
'/VCALENDAR/*/UID');
301 if ( count($uids) == 0 ) {
302 dbg_error_log(
'schedule',
'No UID in VCALENDAR - giving up on CANCEL processing.' );
305 $uid = $uids[0]->Value();
306 $qry =
new AwlQuery($sql, $attendee_principal->
user_no(), $uid);
307 if ( !$qry->Exec(
'schedule',__LINE__,__FILE__) || $qry->rows() < 1 ) {
308 dbg_error_log(
'schedule',
'Could not find ATTENDEE copy of original event - not trying to DELETE it!' );
311 $row = $qry->Fetch();
314 dbg_error_log(
'ERROR',
'Could not delete calendar member %s for %s',
315 $row->dav_name(), $attendee->Value());
316 trace_bug(
'Failed to write scheduling resource.');
335function deliverItipCancel( vCalendar $iTIP, vProperty $attendee,
WritableCollection $attendee_inbox ) {
340require_once(
'Multipart.php');
341require_once(
'EMail.php');
350function doImipMessage($method, $to_email, vCalendar $itip) {
353 dbg_error_log(
'schedule',
'Sending iMIP %s message to %s', $method, $to_email );
354 $mime =
new MultiPart();
355 $mime->addPart( $itip->Render(),
'text/calendar; charset=UTF-8; method='.$method );
357 $friendly_part = isset($c->iMIP->template[$method]) ? $c->iMIP->template[$method] : <<<EOTEMPLATE
358This is a meeting ##METHOD## which your e-mail program should be able to
359import into your calendar. Alternatively you could save the attachment
360and load that into your calendar instead.
363 $components = $itip->GetComponents(
'VTIMEZONE',
false);
365 $replaceable = array(
'METHOD',
'DTSTART',
'DTEND',
'SUMMARY',
'DESCRIPTION',
'URL' );
366 foreach( $replaceable AS $pname ) {
367 $search =
'##'.$pname.
'##';
368 if ( strstr($friendly_part,$search) !==
false ) {
369 $property = $itip->GetProperty($pname);
370 if ( empty($property) )
371 $property = $components[0]->GetProperty($pname);
373 if ( empty($property) )
380 $replace = $when->format(
'c');
383 $replace = $property->GetValue();
386 $friendly_part = str_replace($search, $replace, $friendly_part);
389 $mime->addPart( $friendly_part,
'text/plain' );
391 $email =
new EMail();
392 $email->SetFrom($request->principal->email());
393 $email->AddTo($to_email);
395 $email->SetSubject( $components[0]->GetPValue(
'SUMMARY') );
396 $email->SetBody($mime->getMimeParts());
398 if ( isset($c->iMIP->pretend_email) ) {
399 $email->Pretend($mime->getMimeHeaders());
401 else if ( !isset($c->iMIP->send_email) || !$c->iMIP->send_email) {
402 $email->PretendLog($mime->getMimeHeaders());
405 $email->Send($mime->getMimeHeaders());
IsInSchedulingCollection( $type='any')
actualDeleteCalendarMember( $member_dav_name)
WriteCalendarMember(vCalendar $vcal, $create_resource, $do_scheduling=false, $segment_name=null, $log_action=false)