DAViCal
freebusy-functions.php
1 <?php
2 
9 include_once('vCalendar.php');
10 include_once('RRule.php');
11 
12 
13 function get_freebusy( $path_match, $range_start, $range_end, $bin_privs = null ) {
14  global $request, $c;
15  dbg_error_log( 'freebusy', ' Getting freebusy for path %s from %s to %s', $path_match, $range_start, $range_end);
16 
17  if ( !isset($bin_privs) ) $bin_privs = $request->Privileges();
18  if ( !isset($range_start) || !isset($range_end) ) {
19  $request->DoResponse( 400, 'All valid freebusy requests MUST contain a time-range filter' );
20  }
21  $params = array( ':path_match' => $path_match, ':start' => $range_start->UTC(), ':end' => $range_end->UTC() );
22  $where = ' WHERE caldav_data.dav_name ~ :path_match ';
23  $where .= "AND (";
24  $where .= " (calendar_item.first_instance_start <= :end AND (:start <= calendar_item.last_instance_end OR calendar_item.last_instance_end IS NULL)) ";
25  $where .= " OR (calendar_item.first_instance_start IS NULL AND rrule_event_overlaps( dtstart, dtend, rrule, :start, :end)) ";
26  $where .= ") ";
27  $where .= "AND caldav_data.caldav_type IN ( 'VEVENT', 'VTODO' ) ";
28  $where .= "AND (calendar_item.transp != 'TRANSPARENT' OR calendar_item.transp IS NULL) ";
29  $where .= "AND (calendar_item.status != 'CANCELLED' OR calendar_item.status IS NULL) ";
30  $where .= "AND collection.is_calendar AND collection.schedule_transp = 'opaque' ";
31 
32  if ( $bin_privs != privilege_to_bits('all') ) {
33  $where .= "AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
34  }
35 
36  $fbtimes = array();
37  $sql = 'SELECT caldav_data.caldav_data, calendar_item.rrule, calendar_item.transp, calendar_item.status, ';
38  $sql .= "to_char(calendar_item.dtstart at time zone 'GMT',".AWLDatabase::SqlUTCFormat.') AS start, ';
39  $sql .= "to_char(calendar_item.dtend at time zone 'GMT',".AWLDatabase::SqlUTCFormat.') AS finish, ';
40  $sql .= "calendar_item.class, calendar_item.dav_id, ";
41  $sql .= "collection.timezone AS collection_tzid ";
42  $sql .= 'FROM caldav_data INNER JOIN calendar_item USING(dav_id,user_no,dav_name,collection_id) ';
43  $sql .= 'INNER JOIN collection USING(collection_id)';
44  $sql .= $where;
45  if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= ' ORDER BY dav_id';
46  $qry = new AwlQuery( $sql, $params );
47  if ( $qry->Exec("REPORT",__LINE__,__FILE__) && $qry->rows() > 0 ) {
48  while( $calendar_object = $qry->Fetch() ) {
49  $extra = '';
50  if ( $calendar_object->status == 'TENTATIVE' ) {
51  $extra = ';BUSY-TENTATIVE';
52  }
53  else if ( isset($c->_workaround_client_freebusy_bug) && $c->_workaround_client_freebusy_bug ) {
54  $extra = ';BUSY';
55  }
56  //$extra = ';'.$calendar_object->dav_id;
57  $ics = new vComponent($calendar_object->caldav_data);
58  $expanded = expand_event_instances($ics, $range_start, $range_end);
59  $expansion = $expanded->GetComponents( array('VEVENT'=>true,'VTODO'=>true,'VJOURNAL'=>true) );
60  $collection_tzid = $calendar_object->collection_tzid;
61  dbg_error_log( "freebusy", "=================== $calendar_object->dav_id ======================== %s -> %s, %s %s", $calendar_object->start, $calendar_object->finish, $calendar_object->class, $extra );
62  $dtstart_type = 'DTSTART';
63  foreach( $expansion AS $k => $v ) {
64  dbg_error_log( "freebusy", " %s: %s", $k, $v->Render() );
65  $start_date = $v->GetProperty($dtstart_type);
66  if ( !isset($start_date) && $v->GetType() != 'VTODO' ) {
67  $dtstart_type = 'DUE';
68  $start_date = $v->GetProperty($dtstart_type);
69  }
70 
71  // Floating dtstarts (either VALUE=DATE or with no TZID) can default to the collection's tzid, if one is set
72  $start_date = RepeatRuleDateTime::withFallbackTzid( $start_date, $collection_tzid );
73 
74  $duration = $v->GetProperty('DURATION');
75  $duration = ( !isset($duration) ? 'P1D' : $duration->Value());
76  $end_date = clone($start_date);
77  $end_date->modify( $duration );
78  if ( $end_date == $start_date || $end_date < $range_start || $start_date > $range_end ) {
79  dbg_error_log( "freebusy", "-----------------------------------------------------" );
80  continue;
81  }
82  $thisfb = $start_date->UTC() .'/'. $end_date->UTC() . $extra;
83  array_push( $fbtimes, $thisfb );
84  }
85  }
86  }
87 
88  $freebusy = new vComponent();
89  $freebusy->setType('VFREEBUSY');
90  $freebusy->AddProperty('DTSTAMP', date('Ymd\THis\Z'));
91  $freebusy->AddProperty('DTSTART', $range_start->UTC());
92  $freebusy->AddProperty('DTEND', $range_end->UTC());
93 
94  sort( $fbtimes );
95  foreach( $fbtimes AS $k => $v ) {
96  $text = explode(';',$v,2);
97  $freebusy->AddProperty( 'FREEBUSY', $text[0], (isset($text[1]) ? array('FBTYPE' => $text[1]) : null) );
98  }
99 
100  return $freebusy;
101 }
102