1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407:
<?php
require_once('vCalendar.php');
require_once('WritableCollection.php');
require_once('RRule-v2.php');
function do_scheduling_for_delete(DAVResource $deleted_resource ) {
global $request, $c;
if ( !isset($request) || (isset($c->enable_auto_schedule) && !$c->enable_auto_schedule) ) return true;
if ( $deleted_resource->IsInSchedulingCollection() ) return true;
$caldav_data = $deleted_resource->GetProperty('dav-data');
if ( empty($caldav_data) ) return true;
$vcal = new vCalendar($caldav_data);
$organizer = $vcal->GetOrganizer();
if ( $organizer === false || empty($organizer) ) {
dbg_error_log( 'schedule', 'Event has no organizer - no scheduling required.' );
return true;
}
if ( $vcal->GetScheduleAgent() != 'SERVER' ) {
dbg_error_log( 'schedule', 'SCHEDULE-AGENT=%s - no scheduling required.', $vcal->GetScheduleAgent() );
return true;
}
$organizer_email = preg_replace( '/^mailto:/i', '', $organizer->Value() );
if ( $request->principal->email() == $organizer_email ) {
return doItipOrganizerCancel( $vcal );
}
else {
if ( isset($_SERVER['HTTP_SCHEDULE_REPLY']) && $_SERVER['HTTP_SCHEDULE_REPLY'] == 'F') {
dbg_error_log( 'schedule', 'Schedule-Reply header set to "F" - no scheduling required.' );
return true;
}
return doItipAttendeeReply( $vcal, 'DECLINED', $request->principal->email());
}
}
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($vcal->Render(null, true)), 'REPLY', $attendee->Value(), array('CUTYPE'=>true, 'SCHEDULE-STATUS'=>true));
$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';
if ( !$organizer_calendar->Exists() ) {
if ( doImipMessage('REPLY', $organizer_principal->email(), $vcal) ) {
$response = '1.1';
}
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';
}
}
else {
if ( ! $organizer_inbox->HavePrivilegeTo('schedule-deliver-reply') ) {
$response = '3.8';
}
$response = '1.2';
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);
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;
}
function GetItip( VCalendar $vcal, $method, $attendee_value, $clear_attendee_parameters = null ) {
$iTIP = $vcal->GetItip($method, $attendee_value, $clear_attendee_parameters );
$components = $iTIP->GetComponents();
foreach( $components AS $comp ) {
$comp->AddProperty('REQUEST-STATUS','2.0');
$properties = array();
foreach( $comp->GetProperties() AS $k=> $property ) {
switch( $property->Name() ) {
case 'DTSTART':
case 'DTEND':
case 'DUE':
$when = new RepeatRuleDateTime($property);
$new_prop = new vProperty($property->Name());
$new_prop->Value($when->UTC());
$properties[] = $new_prop;
break;
default:
$properties[] = $property;
}
}
$comp->SetProperties($properties);
}
return $iTIP;
}
function doItipOrganizerCancel( vCalendar $vcal ) {
global $request;
$attendees = $vcal->GetAttendees();
if ( count($attendees) == 0 && count($old_attendees) == 0 ) {
dbg_error_log( 'schedule', 'Event has no attendees - no scheduling required.', count($attendees) );
return true;
}
dbg_error_log( 'schedule', 'Writing scheduling resources for %d attendees', count($attendees) );
$scheduling_actions = false;
$iTIP = GetItip($vcal, 'CANCEL', null);
foreach( $attendees AS $attendee ) {
$email = preg_replace( '/^mailto:/i', '', $attendee->Value() );
if ( $email == $request->principal->email() ) {
dbg_error_log( 'schedule', "not delivering to owner '%s'", $request->principal->email() );
continue;
}
$agent = $attendee->GetParameterValue('SCHEDULE-AGENT');
if ( $agent && $agent != 'SERVER' ) {
dbg_error_log( 'schedule', "not delivering to %s, schedule agent set to value other than server", $email );
continue;
}
$schedule_target = new Principal('email',$email);
if ( !$schedule_target->Exists() ) {
if ( doImipMessage('CANCEL', $email, $vcal) ) {
$response = '1.1';
}
else {
$response = '3.7';
}
}
else {
$attendee_inbox = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-inbox')));
if ( ! $attendee_inbox->HavePrivilegeTo('schedule-deliver-invite') ) {
dbg_error_log( 'schedule', "No authority to deliver invite to %s", $schedule_target->internal_url('schedule-inbox') );
$response = '3.8';
}
else {
$attendee_calendar = new WritableCollection(array('path' => $schedule_target->internal_url('schedule-default-calendar')));
$response = processItipCancel( $vcal, $attendee, $attendee_calendar, $schedule_target );
deliverItipCancel( $iTIP, $attendee, $attendee_inbox );
}
}
dbg_error_log( 'schedule', 'Status for attendee <%s> set to "%s"', $attendee->Value(), $response );
$attendee->SetParameterValue( 'SCHEDULE-STATUS', $response );
$scheduling_actions = true;
}
return true;
}
function processItipCancel( vCalendar $vcal, vProperty $attendee, WritableCollection $attendee_calendar, Principal $attendee_principal ) {
global $request;
dbg_error_log( 'schedule', 'Processing iTIP CANCEL to %s', $attendee->Value());
header( "Debug: Could maybe do the iMIP message dance for attendee ". $attendee->Value() );
if ( !$attendee_calendar->Exists() ) {
if ( doImipMessage('CANCEL', $attendee_principal->email(), $vcal) ) {
return '1.1';
}
else {
dbg_error_log('ERROR', 'Default calendar at "%s" does not exist for attendee "%s"',
$attendee_calendar->dav_name(), $attendee->Value());
return '5.2';
}
}
$sql = 'SELECT caldav_data.dav_name 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 = $vcal->GetPropertiesByPath('/VCALENDAR/*/UID');
if ( count($uids) == 0 ) {
dbg_error_log( 'schedule', 'No UID in VCALENDAR - giving up on CANCEL processing.' );
return '3.8';
}
$uid = $uids[0]->Value();
$qry = new AwlQuery($sql, $attendee_principal->user_no(), $uid);
if ( !$qry->Exec('schedule',__LINE__,__FILE__) || $qry->rows() < 1 ) {
dbg_error_log( 'schedule', 'Could not find ATTENDEE copy of original event - not trying to DELETE it!' );
return '1.2';
}
$row = $qry->Fetch();
if ( $attendee_calendar->actualDeleteCalendarMember($row->dav_name) === false ) {
dbg_error_log('ERROR', 'Could not delete calendar member %s for %s',
$row->dav_name(), $attendee->Value());
trace_bug('Failed to write scheduling resource.');
return '5.2';
}
return '1.2';
}
function deliverItipCancel( vCalendar $iTIP, vProperty $attendee, WritableCollection $attendee_inbox ) {
$attendee_inbox->WriteCalendarMember($iTIP, false);
header( "Debug: Could maybe do the iMIP message dance canceling for attendee: ".$attendee->Value());
}
require_once('Multipart.php');
function doImipMessage($method, $to_email, vCalendar $itip) {
global $c, $request;
}