function get_freebusy($path_match, $range_start, $range_end, $bin_privs = null)
{
    global $request, $c;
    $debugging = false;
    //    if ( $debugging ) {
    //        printf( "Path: %s\n", $path_match );
    //        print_r( $range_start );
    //        print_r( $range_end );
    //    }
    if (!isset($bin_privs)) {
        $bin_privs = $request->Privileges();
    }
    if (!isset($range_start) || !isset($range_end)) {
        $request->DoResponse(400, 'All valid freebusy requests MUST contain a time-range filter');
    }
    $params = array(':path_match' => $path_match, ':start' => $range_start->UTC(), ':end' => $range_end->UTC());
    $where = ' WHERE caldav_data.dav_name ~ :path_match ';
    $where .= 'AND rrule_event_overlaps( dtstart, dtend, rrule, :start, :end) ';
    $where .= "AND caldav_data.caldav_type IN ( 'VEVENT', 'VTODO' ) ";
    $where .= "AND (calendar_item.transp != 'TRANSPARENT' OR calendar_item.transp IS NULL) ";
    $where .= "AND (calendar_item.status != 'CANCELLED' OR calendar_item.status IS NULL) ";
    $where .= "AND collection.is_calendar AND collection.schedule_transp = 'opaque' ";
    if ($bin_privs != privilege_to_bits('all')) {
        $where .= "AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
    }
    $fbtimes = array();
    $sql = 'SELECT caldav_data.caldav_data, calendar_item.rrule, calendar_item.transp, calendar_item.status, ';
    $sql .= "to_char(calendar_item.dtstart at time zone 'GMT'," . AWLDatabase::SqlUTCFormat . ') AS start, ';
    $sql .= "to_char(calendar_item.dtend at time zone 'GMT'," . AWLDatabase::SqlUTCFormat . ') AS finish, ';
    $sql .= "calendar_item.class, calendar_item.dav_id ";
    $sql .= 'FROM caldav_data INNER JOIN calendar_item USING(dav_id,user_no,dav_name,collection_id) ';
    $sql .= 'INNER JOIN collection USING(collection_id)';
    $sql .= $where;
    if (isset($c->strict_result_ordering) && $c->strict_result_ordering) {
        $sql .= ' ORDER BY dav_id';
    }
    $qry = new AwlQuery($sql, $params);
    if ($qry->Exec("REPORT", __LINE__, __FILE__) && $qry->rows() > 0) {
        while ($calendar_object = $qry->Fetch()) {
            $extra = '';
            if ($calendar_object->status == 'TENTATIVE') {
                $extra = ';BUSY-TENTATIVE';
            } else {
                if (isset($c->_workaround_client_freebusy_bug) && $c->_workaround_client_freebusy_bug) {
                    $extra = ';BUSY';
                }
            }
            //      if ( $debugging ) {
            //        $extra = ';'.$calendar_object->dav_id;
            //      }
            //      dbg_error_log( "REPORT", " FreeBusy: Not transparent, tentative or cancelled: %s, %s, %s", $calendar_object->start, $calendar_object->finish, $calendar_object->class );
            $ics = new vComponent($calendar_object->caldav_data);
            $expanded = expand_event_instances($ics, $range_start, $range_end);
            $expansion = $expanded->GetComponents(array('VEVENT' => true, 'VTODO' => true, 'VJOURNAL' => true));
            //      if ( $debugging ) echo "===================   $calendar_object->dav_id   ========================\n";
            $dtstart_type = 'DTSTART';
            foreach ($expansion as $k => $v) {
                //        if ( $debugging ) print $k."\n".$v->Render();
                $start_date = $v->GetProperty($dtstart_type);
                if (!isset($start_date) && $v->GetType() != 'VTODO') {
                    $dtstart_type = 'DUE';
                    $start_date = $v->GetProperty($dtstart_type);
                }
                $start_date = new RepeatRuleDateTime($start_date);
                $duration = $v->GetProperty('DURATION');
                $duration = !isset($duration) ? 'P1D' : $duration->Value();
                $end_date = clone $start_date;
                $end_date->modify($duration);
                if ($end_date == $start_date || $end_date < $range_start || $start_date > $range_end) {
                    //            if ( $debugging )
                    //              echo "-----------------------------------------------------\n";
                    continue;
                }
                //        if ( $debugging )
                //            echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
                $thisfb = $start_date->UTC() . '/' . $end_date->UTC() . $extra;
                array_push($fbtimes, $thisfb);
            }
        }
    }
    $freebusy = new vComponent();
    $freebusy->setType('VFREEBUSY');
    $freebusy->AddProperty('DTSTAMP', date('Ymd\\THis\\Z'));
    $freebusy->AddProperty('DTSTART', $range_start->UTC());
    $freebusy->AddProperty('DTEND', $range_end->UTC());
    sort($fbtimes);
    foreach ($fbtimes as $k => $v) {
        $text = explode(';', $v, 2);
        $freebusy->AddProperty('FREEBUSY', $text[0], isset($text[1]) ? array('FBTYPE' => $text[1]) : null);
    }
    return $freebusy;
}
 * actually return <response> stanzas with a 404 for each absent href.  We could do
 * this relatively easily with an array_flip($params) and remove each matching dav_name
 * as we process it.
 */
if (isset($c->strict_result_ordering) && $c->strict_result_ordering) {
    $where .= " ORDER BY caldav_data.dav_id";
}
$qry = new AwlQuery($sql . $where, $params);
if ($qry->Exec('REPORT', __LINE__, __FILE__) && $qry->rows() > 0) {
    while ($dav_object = $qry->Fetch()) {
        if ($bound_from != $collection->dav_name()) {
            $dav_object->dav_name = str_replace($bound_from, $collection->dav_name(), $dav_object->dav_name);
        }
        //if ( $need_expansion ) {
        $vResource = new vComponent($dav_object->caldav_data);
        $expanded = expand_event_instances($vResource, $expand_range_start, $expand_range_end);
        //      $event = $expanded->GetComponents("VEVENT")[0];
        //
        //      $attendeeName = "ATTENDEE";
        //
        //      $event->ClearProperties($attendeeName);
        //
        //      $attendeeQry = new AwlQuery("SELECT params, attendee FROM calendar_attendee WHERE dav_id = :dav_id", array(':dav_id' => $dav_object->dav_id));
        //      $attendeeQry->Execute();
        //
        //
        //
        //      while(($arow = $attendeeQry->Fetch())){
        //         $attendeeParameters = $arow->params;
        //         $attendeeValue = $arow->attendee;
        //         // separe value
}
$qry = new AwlQuery($sql, $params);
if ($qry->Exec("calquery", __LINE__, __FILE__) && $qry->rows() > 0) {
    while ($dav_object = $qry->Fetch()) {
        try {
            if (!$need_post_filter || apply_filter($qry_filters, $dav_object)) {
                if ($bound_from != $target_collection->dav_name()) {
                    $dav_object->dav_name = str_replace($bound_from, $target_collection->dav_name(), $dav_object->dav_name);
                }
                if ($need_expansion) {
                    $vResource = new vComponent($dav_object->caldav_data);
                    $expanded = getVCalendarRange($vResource);
                    if (!$expanded->overlaps($range_filter)) {
                        continue;
                    }
                    $expanded = expand_event_instances($vResource, $expand_range_start, $expand_range_end, $expand_as_floating);
                    if ($expanded->ComponentCount() == 0) {
                        continue;
                    }
                    if ($need_expansion) {
                        $dav_object->caldav_data = $expanded->Render();
                    }
                } else {
                    if (isset($range_filter)) {
                        $vResource = new vComponent($dav_object->caldav_data);
                        $expanded = getVCalendarRange($vResource);
                        dbg_error_log('calquery', 'Expanded to %s:%s which might overlap %s:%s', $expanded->from, $expanded->until, $range_filter->from, $range_filter->until);
                        if (!$expanded->overlaps($range_filter)) {
                            continue;
                        }
                    }
Beispiel #4
0
$earliest->modify($args->near_past);
if ($args->debug) {
    printf("Looking for event instances between '%s' and '%s'\n", $earliest->UTC(), $expand_range_end->UTC());
}
$sql = 'SELECT * FROM calendar_alarm JOIN calendar_item USING (dav_id) JOIN caldav_data USING (dav_id) WHERE rrule IS NOT NULL AND next_trigger IS NULL';
if ($args->debug) {
    printf("%s\n", $sql);
}
$qry = new AwlQuery($sql);
if ($qry->Exec() && $qry->rows()) {
    while ($alarm = $qry->Fetch()) {
        if ($args->debug) {
            printf("refresh: Processing alarm for '%s' based on '%s','%s', '%s'\n", $alarm->dav_name, $alarm->dtstart, $alarm->rrule, $alarm->trigger);
        }
        $ic = new vComponent($alarm->caldav_data);
        $expanded = expand_event_instances($ic, $earliest, $expand_range_end);
        $expanded->MaskComponents(array('VEVENT' => 1, 'VTODO' => 1, 'VJOURNAL' => 1));
        $instances = $expanded->GetComponents();
        $trigger = new vProperty($alarm->trigger);
        $related = $trigger->GetParameterValue('RELATED');
        $first = new RepeatRuleDateTime($alarm->dtstart);
        $first->modify($trigger->Value());
        $next = null;
        $last = null;
        foreach ($instances as $k => $component) {
            $when = new RepeatRuleDateTime($component->GetPValue('DTSTART'));
            // a UTC value
            if ($args->debug) {
                printf("refresh: Looking at event instance on '%s'\n", $when->UTC());
            }
            if ($related == 'END') {