If this message has an ORGANIZER elsewhere then we give up.
[davical.git] / inc / schedule-functions.php
blobe359060a518bfbd99617a6e379f7a2f064ecc3f9
1 <?php
2 /**
3 * Functions for handling CalDAV Scheduling.
4 *
5 * @package davical
6 * @subpackage caldav
7 * @author Andrew McMillan <andrew@morphoss.com>
8 * @copyright Morphoss Ltd - http://www.morphoss.com/
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later version
12 require_once('vCalendar.php');
13 require_once('WritableCollection.php');
14 require_once('RRule-v2.php');
16 /**
17 * Entry point for scheduling on DELETE, for which there are thee outcomes:
18 * - We don't do scheduling (disabled, no organizer, ...)
19 * - We are an ATTENDEE declining the meeting.
20 * - We are the ORGANIZER canceling the meeting.
22 * @param DAVResource $deleted_resource The resource which has already been deleted
24 function do_scheduling_for_delete(DAVResource $deleted_resource ) {
25 // By the time we arrive here the resource *has* actually been deleted from disk
26 // we can only fail to (de-)schedule the activity...
27 global $request, $c;
29 if ( !isset($request) || (isset($c->enable_auto_schedule) && !$c->enable_auto_schedule) ) return true;
30 if ( $deleted_resource->IsInSchedulingCollection() ) 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.' );
39 return true;
41 if ( $vcal->GetScheduleAgent() != 'SERVER' ) {
42 dbg_error_log( 'schedule', 'SCHEDULE-AGENT=%s - no scheduling required.', $vcal->GetScheduleAgent() );
43 return true;
45 $organizer_email = preg_replace( '/^mailto:/i', '', $organizer->Value() );
47 if ( $request->principal->email() == $organizer_email ) {
48 return doItipOrganizerCancel( $vcal );
50 else {
51 if ( isset($_SERVER['HTTP_SCHEDULE_REPLY']) && $_SERVER['HTTP_SCHEDULE_REPLY'] == 'F') {
52 dbg_error_log( 'schedule', 'Schedule-Request header set to "F" - no scheduling required.' );
53 return true;
55 return doItipAttendeeReply( $vcal, 'DECLINED', $request->principal->email());
61 /**
62 * Do the scheduling adjustments for a REPLY when an ATTENDEE updates their status.
63 * @param vCalendar $vcal The resource that the ATTENDEE is writing to their calendar
64 * @param string $organizer The property which is the event ORGANIZER.
66 //function do_scheduling_reply( vCalendar $vcal, vProperty $organizer ) {
67 function doItipAttendeeReply( vCalendar $resource, $partstat ) {
68 global $request;
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() );
76 return true;
79 $sql = 'SELECT caldav_data.dav_name, caldav_data.caldav_data FROM caldav_data JOIN calendar_item USING(dav_id) ';
80 $sql .= 'WHERE caldav_data.collection_id IN (SELECT collection_id FROM collection WHERE is_calendar AND user_no =?) ';
81 $sql .= 'AND uid=? LIMIT 1';
82 $uids = $resource->GetPropertiesByPath('/VCALENDAR/*/UID');
83 if ( count($uids) == 0 ) {
84 dbg_error_log( 'schedule', 'No UID in VCALENDAR - giving up on REPLY.' );
85 return true;
87 $uid = $uids[0]->Value();
88 $qry = new AwlQuery($sql, $organizer_principal->user_no(), $uid);
89 if ( !$qry->Exec('schedule',__LINE__,__FILE__) || $qry->rows() < 1 ) {
90 dbg_error_log( 'schedule', 'Could not find original event from organizer - giving up on REPLY.' );
91 return true;
93 $row = $qry->Fetch();
94 $collection_path = preg_replace('{/[^/]+$}', '/', $row->dav_name );
95 $segment_name = str_replace($collection_path, '', $row->dav_name );
96 $vcal = new vCalendar($row->caldav_data);
98 $attendees = $vcal->GetAttendees();
99 foreach( $attendees AS $v ) {
100 $email = preg_replace( '/^mailto:/i', '', $v->Value() );
101 if ( $email == $request->principal->email() ) {
102 $attendee = $v;
103 break;
106 if ( empty($attendee) ) {
107 dbg_error_log( 'schedule', 'Could not find ATTENDEE in VEVENT - giving up on REPLY.' );
108 return true;
111 $attendee->SetParameterValue('PARTSTAT', $partstat);
112 $attendee->SetParameterValue('SCHEDULE-STATUS', '2.0');
113 $vcal->UpdateAttendeeStatus($request->principal->email(), clone($attendee) );
115 $organizer_calendar = new WritableCollection(array('path' => $collection_path));
116 $organizer_inbox = new WritableCollection(array('path' => $organizer_principal->internal_url('schedule-inbox')));
118 $schedule_reply = GetItip(new vCalendar($row->caldav_data),'REPLY',$attendee->Value());
119 $schedule_request = GetItip(new vCalendar($row->caldav_data),'REQUEST',null);
121 dbg_error_log( 'schedule', 'Writing ATTENDEE scheduling REPLY from %s to %s', $request->principal->email(), $organizer_principal->email() );
123 $response = '3.7'; // Organizer was not found on server.
124 if ( !$organizer_calendar->Exists() ) {
125 dbg_error_log('ERROR','Default calendar at "%s" does not exist for user "%s"',
126 $organizer_calendar->dav_name(), $schedule_target->username());
127 $response = '5.2'; // No scheduling support for user
129 else {
130 if ( ! $organizer_inbox->HavePrivilegeTo('schedule-deliver-reply') ) {
131 $response = '3.8'; // No authority to deliver replies to organizer.
133 $response = '1.2'; // Scheduling reply delivered successfully
134 if ( $organizer_calendar->WriteCalendarMember($vcal, false, false, $segment_name) === false ) {
135 dbg_error_log('ERROR','Could not write updated calendar member to %s', $attendee_calendar->dav_name() );
136 trace_bug('Failed to write scheduling resource.');
138 $organizer_inbox->WriteCalendarMember($schedule_reply, false, false, $request->principal->username().$segment_name);
142 dbg_error_log( 'schedule', 'Status for organizer <%s> set to "%s"', $organizer->Value(), $response );
143 $organizer->SetParameterValue( 'SCHEDULE-STATUS', $response );
144 $resource->UpdateOrganizerStatus($organizer); // Which was passed in by reference, and we're updating it here.
146 // Now we loop through the *other* ATTENDEEs, updating them on the status of the ATTENDEE DECLINE/ACCEPT
147 foreach( $attendees AS $attendee ) {
148 $email = preg_replace( '/^mailto:/i', '', $attendee->Value() );
149 if ( $email == $request->principal->email() || $email == $organizer_principal->email() ) continue;
151 $agent = $attendee->GetParameterValue('SCHEDULE-AGENT');
152 if ( !empty($agent) && $agent != 'SERVER' ) continue;
154 $schedule_target = new Principal('email',$email);
155 if ( $schedule_target->Exists() ) {
156 $attendee_calendar = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-default-calendar')));
157 if ( !$attendee_calendar->Exists() ) {
158 dbg_error_log('ERROR','Default calendar at "%s" does not exist for user "%s"',
159 $attendee_calendar->dav_name(), $schedule_target->username());
160 continue;
162 else {
163 $attendee_inbox = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-inbox')));
164 if ( ! $attendee_inbox->HavePrivilegeTo('schedule-deliver-invite') ) continue;
166 if ( $attendee_calendar->WriteCalendarMember($vcal, false) === false ) {
167 dbg_error_log('ERROR','Could not write updated calendar member to %s', $attendee_calendar->dav_name());
168 trace_bug('Failed to write scheduling resource.');
170 $attendee_inbox->WriteCalendarMember($schedule_request, false);
175 return true;
178 function GetItip( VCalendar $vcal, $method, $attendee_value ) {
180 $iTIP = $vcal->GetItip($method, $attendee_value );
181 $iTIP->AddProperty('REQUEST-STATUS','2.0');
182 $components = $iTIP->GetComponents();
183 foreach( $components AS $comp ) {
184 $properties = array();
185 foreach( $comp->GetProperties() AS $k=> $property ) {
186 switch( $property->Name() ) {
187 case 'DTSTART':
188 case 'DTEND':
189 case 'DUE':
190 $when = new RepeatRuleDateTime($property);
191 $properties[] = new vProperty( $property->Name() . ":" . $when->UTC() );
192 break;
193 default:
194 $properties[] = $property;
197 $comp->SetProperties($properties);
200 return $iTIP;
204 * Handles sending the iTIP CANCEL messages to each ATTENDEE by the ORGANIZER.
205 * @param vCalendar $vcal What's being cancelled.
207 function doItipOrganizerCancel( vCalendar $vcal ) {
208 global $request;
210 $attendees = $vcal->GetAttendees();
211 if ( count($attendees) == 0 && count($old_attendees) == 0 ) {
212 dbg_error_log( 'schedule', 'Event has no attendees - no scheduling required.', count($attendees) );
213 return true;
216 dbg_error_log( 'schedule', 'Writing scheduling resources for %d attendees', count($attendees) );
217 $scheduling_actions = false;
219 $iTIP = GetItip($vcal, 'CANCEL', null);
221 foreach( $attendees AS $attendee ) {
222 $email = preg_replace( '/^mailto:/i', '', $attendee->Value() );
223 if ( $email == $request->principal->email() ) {
224 dbg_error_log( 'schedule', "not delivering to owner '%s'", $request->principal->email() );
225 continue;
228 $agent = $attendee->GetParameterValue('SCHEDULE-AGENT');
229 if ( $agent && $agent != 'SERVER' ) {
230 dbg_error_log( 'schedule', "not delivering to %s, schedule agent set to value other than server", $email );
231 continue;
233 $schedule_target = new Principal('email',$email);
234 if ( !$schedule_target->Exists() ) {
235 $response = '3.7';
237 else {
238 $attendee_inbox = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-inbox')));
239 if ( ! $attendee_inbox->HavePrivilegeTo('schedule-deliver-invite') ) {
240 dbg_error_log( 'schedule', "No authority to deliver invite to %s", $schedule_target->internal_url('schedule-inbox') );
241 $response = '3.8';
243 else {
244 $attendee_calendar = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-default-calendar')));
245 $response = processItipCancel( $vcal, $attendee, $attendee_calendar, $schedule_target );
246 deliverItipCancel( $iTIP, $attendee, $attendee_inbox );
249 dbg_error_log( 'schedule', 'Status for attendee <%s> set to "%s"', $attendee->Value(), $response );
250 $attendee->SetParameterValue( 'SCHEDULE-STATUS', $response );
251 $scheduling_actions = true;
254 return true;
258 * Does the actual processing of the iTIP CANCEL message on behalf of an ATTENDEE,
259 * which generally means writing it into the ATTENDEE's default calendar.
261 * @param vCalendar $vcal The message.
262 * @param vProperty $attendee
263 * @param WritableCollection $attendee_calendar
265 function processItipCancel( vCalendar $vcal, vProperty $attendee, WritableCollection $attendee_calendar, Principal $attendee_principal ) {
267 dbg_error_log( 'schedule', 'Processing iTIP CANCEL to %s', $attendee->Value());
268 if ( !$attendee_calendar->Exists() ) {
269 dbg_error_log('ERROR', 'Default calendar at "%s" does not exist for attendee "%s"',
270 $attendee_calendar->dav_name(), $attendee->Value());
271 return '5.2'; // No scheduling support for user
274 $sql = 'SELECT caldav_data.dav_name FROM caldav_data JOIN calendar_item USING(dav_id) ';
275 $sql .= 'WHERE caldav_data.collection_id IN (SELECT collection_id FROM collection WHERE is_calendar AND user_no =?) ';
276 $sql .= 'AND uid=? LIMIT 1';
277 $uids = $vcal->GetPropertiesByPath('/VCALENDAR/*/UID');
278 if ( count($uids) == 0 ) {
279 dbg_error_log( 'schedule', 'No UID in VCALENDAR - giving up on CANCEL processing.' );
280 return '3.8';
282 $uid = $uids[0]->Value();
283 $qry = new AwlQuery($sql, $attendee_principal->user_no(), $uid);
284 if ( !$qry->Exec('schedule',__LINE__,__FILE__) || $qry->rows() < 1 ) {
285 dbg_error_log( 'schedule', 'Could not find ATTENDEE copy of original event - not trying to DELETE it!' );
286 return '1.2';
288 $row = $qry->Fetch();
290 if ( $attendee_calendar->actualDeleteCalendarMember($row->dav_name) === false ) {
291 dbg_error_log('ERROR', 'Could not delete calendar member %s for %s',
292 $row->dav_name(), $attendee->Value());
293 trace_bug('Failed to write scheduling resource.');
294 return '5.2';
297 return '1.2'; // Scheduling invitation delivered successfully
303 * Delivers the iTIP CANCEL message to an ATTENDEE's Scheduling Inbox Collection.
305 * This is pretty simple at present, but could be extended in the future to do the sending
306 * of e-mail to a remote attendee.
308 * @param vCalendar $iTIP
309 * @param vProperty $attendee
310 * @param WritableCollection $attendee_inbox
312 function deliverItipCancel( vCalendar $iTIP, vProperty $attendee, WritableCollection $attendee_inbox ) {
313 $attendee_inbox->WriteCalendarMember($iTIP, false);