Overview

Packages

  • awl
    • caldav-client-v2
    • RRule
  • davical
    • authentication
      • drivers
    • caldav
    • DAViCalSession
    • DAVTicket
    • external-bind
    • feed
    • HTTPAuthSession
    • iSchedule
    • iSchedule-POST
    • logging
    • metrics
    • Principal
    • propfind
    • PublicSession
    • Request
    • Resource
    • tzservice
  • None
  • PHP

Classes

  • CalDAVClient
  • CheckResult
  • setupFakeSession
  • VCard
  • VTimezone

Functions

  • access_ticket_browser
  • binding_row_editor
  • bindings_to_other_browser
  • bindings_to_us_browser
  • build_dependencies_table
  • build_privileges_html
  • build_site_statistics
  • BuildSqlFilter
  • calquery_apply_filter
  • cardquery_apply_filter
  • catch_setup_errors
  • check_awl_version
  • check_calendar
  • check_curl
  • check_database_connection
  • check_datetime
  • check_davical_version
  • check_gettext
  • check_iconv
  • check_ldap
  • check_magic_quotes_gpc
  • check_magic_quotes_runtime
  • check_pdo
  • check_pdo_pgsql
  • check_pgsql
  • check_real_php
  • check_schema_version
  • check_string
  • check_suhosin_server_strip
  • check_xml
  • collection_privilege_format_function
  • confirm_delete_bind_in
  • confirm_delete_binding
  • confirm_delete_collection
  • confirm_delete_principal
  • confirm_delete_ticket
  • do_error
  • edit_binding_row
  • edit_grant_row_collection
  • edit_grant_row_principal
  • edit_group_row
  • edit_ticket_row
  • errorResponse
  • expand_properties
  • fetch_external
  • get_address_properties
  • get_freebusy
  • get_href_containers
  • get_phpinfo
  • grant_row_editor
  • group_members_browser
  • group_memberships_browser
  • group_row_editor
  • handle_subaction
  • i18n
  • ischedule_get
  • log_setup_error
  • make_help_link
  • principal_collection_browser
  • principal_editor
  • principal_grants_browser
  • principal_privilege_format_function
  • send_page_header
  • SqlFilterCardDAV
  • SqlFilterFragment
  • ticket_row_editor
  • unicodeToUtf8
  • update_external
  • utf8ToUnicode
  • Overview
  • Package
  • Function
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: include_once('vCalendar.php');
  4: 
  5: /**
  6:  * Build the array of properties to include in the report output
  7:  */
  8: $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:caldav:calendar-query');
  9: 
 10: $properties = array();
 11: $include_properties = array();
 12: $need_expansion = false;
 13: while (list($idx, $qqq) = each($qry_content))
 14: {
 15:   $proptype = $qry_content[$idx]->GetNSTag();
 16:   switch( $proptype ) {
 17:     case 'DAV::prop':
 18:       $qry_props = $xmltree->GetPath('/urn:ietf:params:xml:ns:caldav:calendar-query/'.$proptype.'/*');
 19:       foreach( $qry_content[$idx]->GetElements() AS $k => $v ) {
 20:         $propertyname = $v->GetNSTag();
 21:         $properties[$propertyname] = 1;
 22:         if ( $propertyname == 'urn:ietf:params:xml:ns:caldav:calendar-data' ) check_for_expansion($v);
 23:       }
 24:       break;
 25: 
 26:     case 'DAV::allprop':
 27:       $properties['DAV::allprop'] = 1;
 28:       if ( $qry_content[$idx]->GetNSTag() == 'DAV::include' ) {
 29:         foreach( $qry_content[$idx]->GetElements() AS $k => $v ) {
 30:           $include_properties[] = $v->GetNSTag(); /** $include_properties is referenced in DAVResource where allprop is expanded */
 31:           if ( $v->GetNSTag() == 'urn:ietf:params:xml:ns:caldav:calendar-data' ) check_for_expansion($v);
 32:         }
 33:       }
 34:       break;
 35:   }
 36: }
 37: if ( empty($properties) ) $properties['DAV::allprop'] = 1;
 38: 
 39: 
 40: /**
 41:  * There can only be *one* FILTER element, and it must contain *one* COMP-FILTER
 42:  * element.  In every case I can see this contained COMP-FILTER element will
 43:  * necessarily be a VCALENDAR, which then may contain other COMP-FILTER etc.
 44:  */
 45: $qry_filters = $xmltree->GetPath('/urn:ietf:params:xml:ns:caldav:calendar-query/urn:ietf:params:xml:ns:caldav:filter/*');
 46: if ( count($qry_filters) != 1 ) $qry_filters = false;
 47: 
 48: 
 49: /**
 50: * While we can construct our SQL to apply some filters in the query, other filters
 51: * need to be checked against the retrieved record.  This is for handling those ones.
 52: *
 53: * @param array $filter An array of XMLElement which is the filter definition
 54: * @param string $item The database row retrieved for this calendar item
 55: *
 56: * @return boolean True if the check succeeded, false otherwise.
 57: */
 58: function calquery_apply_filter( $filters, $item ) {
 59:   global $session, $c, $request;
 60: 
 61:   if ( count($filters) == 0 ) return true;
 62: 
 63:   dbg_error_log("calquery","Applying filter for item '%s'", $item->dav_name );
 64:   $ical = new vCalendar( $item->caldav_data );
 65:   return $ical->StartFilter($filters);
 66: }
 67: 
 68: 
 69: /**
 70:  * Process a filter fragment returning an SQL fragment
 71:  * Changed by GitHub user moosemark 2013-11-29 to allow multiple text-matches - SQL parameter now has numeric count appended.
 72:  */
 73: $need_post_filter = false;
 74: $range_filter = null;
 75: $parameter_match_num = 0;
 76: function SqlFilterFragment( $filter, $components, $property = null, $parameter = null) {
 77:   global $need_post_filter, $range_filter, $target_collection, $parameter_match_num;
 78:   $sql = "";
 79:   $params = array();
 80:   if ( !is_array($filter) ) {
 81:     dbg_error_log( "calquery", "Filter is of type '%s', but should be an array of XML Tags.", gettype($filter) );
 82:   }
 83: 
 84:   foreach( $filter AS $k => $v ) {
 85:     $tag = $v->GetNSTag();
 86:     dbg_error_log("calquery", "Processing $tag into SQL - %d, '%s', %d\n", count($components), $property, isset($parameter) );
 87: 
 88:     $not_defined = "";
 89:     switch( $tag ) {
 90:       case 'urn:ietf:params:xml:ns:caldav:is-not-defined':
 91:         $not_defined = "not-"; // then fall through to IS-DEFINED case
 92:       case 'urn:ietf:params:xml:ns:caldav:is-defined':
 93:         if ( isset( $parameter ) ) {
 94:           $need_post_filter = true;
 95:           dbg_error_log("calquery", "Could not handle 'is-%sdefined' on property %s, parameter %s in SQL", $not_defined, $property, $parameter );
 96:           return false;  // Not handled in SQL
 97:         }
 98:         if ( isset( $property ) ) {
 99:           switch( $property ) {
100:             case 'created':
101:             case 'completed':  /** @todo when it can be handled in the SQL - see around line 200 below */
102:             case 'dtend':
103:             case 'dtstamp':
104:             case 'dtstart':
105:               if ( ! $target_collection->IsSchedulingCollection() ) {
106:                 $property_defined_match = "IS NOT NULL";
107:               }
108:               break;
109: 
110:             case 'priority':
111:               $property_defined_match = "IS NOT NULL";
112:               break;
113: 
114:             default:
115:               $property_defined_match = "LIKE '_%'";  // i.e. contains a single character or more
116:           }
117:           $sql .= sprintf( "AND %s %s%s ", $property, $not_defined, $property_defined_match );
118:         }
119:         break;
120: 
121:       case 'urn:ietf:params:xml:ns:caldav:time-range':
122:         /**
123:         * @todo We should probably allow time range queries against other properties, since
124:         *  eventually some client may want to do this.
125:         */
126:         $start_column = ($components[sizeof($components)-1] == 'VTODO' ? "due" : 'dtend');     // The column we compare against the START attribute
127:         $finish_column = 'dtstart';  // The column we compare against the END attribute
128:         $start = $v->GetAttribute("start");
129:         $finish = $v->GetAttribute("end");
130:         $start_sql = $finish_sql = '';
131:         if ( isset($start) ) {
132:           $params[':time_range_start'] = $start;
133:           $start_sql .= ' (('.$start_column.' IS NULL AND '.$finish_column.' > :time_range_start) OR '.$start_column.' > :time_range_start) ';
134:         }
135:         if ( isset($finish) ) {
136:           $params[':time_range_end'] = $finish;
137:           $finish_sql = ' '.$finish_column.' < :time_range_end ';
138:         }
139:         if ( isset($start) || isset($finish) ) {
140:           $sql .= ' AND (rrule IS NOT NULL OR '.$finish_column.' IS NULL OR (';
141:           if ( isset($start) ) $sql .= $start_sql;
142:           if ( isset($start) && isset($finish) ) $sql .= ' AND ';
143:           if ( isset($finish) ) $sql .= $finish_sql;
144:           $sql .= '))';
145:         }
146:         @dbg_error_log('calquery', 'filter-sql: %s', $sql);
147:         @dbg_error_log('calquery', 'time-range-start: %s,  time-range-end: %s, ', $params[':time_range_start'], $params[':time_range_end']);
148:         $range_filter = new RepeatRuleDateRange((empty($start) ? null : new RepeatRuleDateTime($start)),
149:                                     (empty($finish)? null : new RepeatRuleDateTime($finish)));
150:         break;
151: 
152:       case 'urn:ietf:params:xml:ns:caldav:text-match':
153:         $search = $v->GetContent();
154:         $negate = $v->GetAttribute("negate-condition");
155:         $collation = $v->GetAttribute("collation");
156:         switch( strtolower($collation) ) {
157:           case 'i;octet':
158:             $comparison = 'LIKE';
159:             break;
160:           case 'i;ascii-casemap':
161:           default:
162:             $comparison = 'ILIKE';
163:             break;
164:         }
165:         /* Append the match number to the SQL parameter, to allow multiple text match conditions within the same query */
166:         $params[':text_match_'.$parameter_match_num] = '%'.$search.'%';
167:         $fragment = sprintf( 'AND (%s%s %s :text_match_%s) ',
168:                         (isset($negate) && strtolower($negate) == "yes" ? $property.' IS NULL OR NOT ': ''),
169:                                           $property, $comparison, $parameter_match_num );
170:         $parameter_match_num++;
171: 
172:         dbg_error_log('calquery', ' text-match: %s', $fragment );
173:         $sql .= $fragment;
174:         break;
175: 
176:       case 'urn:ietf:params:xml:ns:caldav:comp-filter':
177:         $comp_filter_name = $v->GetAttribute("name");
178:         if ( $comp_filter_name != 'VCALENDAR' && count($components) == 0 ) {
179:           $sql .= "AND caldav_data.caldav_type = :component_name_filter ";
180:           $params[':component_name_filter'] = $comp_filter_name;
181:           $components[] = $comp_filter_name;
182:         }
183:         $subfilter = $v->GetContent();
184:         if ( is_array( $subfilter ) ) {
185:           $success = SqlFilterFragment( $subfilter, $components, $property, $parameter );
186:           if ( $success === false ) continue; else {
187:             $sql .= $success['sql'];
188:             $params = array_merge( $params, $success['params'] );
189:           }
190:         }
191:         break;
192: 
193:       case 'urn:ietf:params:xml:ns:caldav:prop-filter':
194:         $propertyname = $v->GetAttribute("name");
195:         switch( $propertyname ) {
196:           case 'PERCENT-COMPLETE':
197:             $subproperty = 'percent_complete';
198:             break;
199: 
200:           case 'UID':
201:           case 'SUMMARY':
202: //          case 'LOCATION':
203:           case 'DESCRIPTION':
204:           case 'CLASS':
205:           case 'TRANSP':
206:           case 'RRULE':  // Likely that this is not much use
207:           case 'URL':
208:           case 'STATUS':
209:           case 'CREATED':
210:           case 'DTSTAMP':
211:           case 'DTSTART':
212:           case 'DTEND':
213:           case 'DUE':
214:           case 'PRIORITY':
215:             $subproperty = 'calendar_item.'.strtolower($propertyname);
216:             break;
217: 
218:           case 'COMPLETED':  /** @todo this should be moved into the properties supported in SQL. */
219:           default:
220:             $need_post_filter = true;
221:             unset($subproperty);
222:             dbg_error_log("calquery", "Could not handle 'prop-filter' on %s in SQL", $propertyname );
223:             continue;
224:         }
225:         if ( isset($subproperty) ) {
226:           $subfilter = $v->GetContent();
227:           $success = SqlFilterFragment( $subfilter, $components, $subproperty, $parameter );
228:           if ( $success === false ) continue; else {
229:             $sql .= $success['sql'];
230:             $params = array_merge( $params, $success['params'] );
231:           }
232:         }
233:         break;
234: 
235:       case 'urn:ietf:params:xml:ns:caldav:param-filter':
236:         $need_post_filter = true;
237:         return false; // Can't handle PARAM-FILTER conditions in the SQL
238:         $parameter = $v->GetAttribute("name");
239:         $subfilter = $v->GetContent();
240:         $success = SqlFilterFragment( $subfilter, $components, $property, $parameter );
241:         if ( $success === false ) continue; else {
242:           $sql .= $success['sql'];
243:           $params = array_merge( $params, $success['params'] );
244:         }
245:         break;
246: 
247:       default:
248:         dbg_error_log("calquery", "Could not handle unknown tag '%s' in calendar query report", $tag );
249:         break;
250:     }
251:   }
252:   dbg_error_log("calquery", "Generated SQL was '%s'", $sql );
253:   return array( 'sql' => $sql, 'params' => $params );
254: }
255: 
256: /**
257:  * Build an SQL 'WHERE' clause which implements (parts of) the filter. The
258:  * elements of the filter which are implemented in the SQL will be removed.
259:  *
260:  * @param arrayref &$filter A reference to an array of XMLElement defining the filter
261:  *
262:  * @return string A string suitable for use as an SQL 'WHERE' clause selecting the desired records.
263:  */
264: function BuildSqlFilter( $filter ) {
265:   $components = array();
266:   if ( $filter->GetNSTag() == "urn:ietf:params:xml:ns:caldav:comp-filter" && $filter->GetAttribute("name") == "VCALENDAR" )
267:     $filter = $filter->GetContent();  // Everything is inside a VCALENDAR AFAICS
268:   else {
269:     dbg_error_log("calquery", "Got bizarre CALDAV:FILTER[%s=%s]] which does not contain comp-filter = VCALENDAR!!", $filter->GetNSTag(), $filter->GetAttribute("name") );
270:   }
271:   return SqlFilterFragment( $filter, $components );
272: }
273: 
274: 
275: /**
276: * Something that we can handle, at least roughly correctly.
277: */
278: 
279: $responses = array();
280: $target_collection = new DAVResource($request->path);
281: $bound_from = $target_collection->bound_from();
282: if ( !$target_collection->Exists() ) {
283:   $request->DoResponse( 404 );
284: }
285: 
286: $params = array();
287: 
288: if ( ! ($target_collection->IsCalendar() || $target_collection->IsSchedulingCollection()) ) {
289:   if ( !(isset($c->allow_recursive_report) && $c->allow_recursive_report) ) {
290:     $request->DoResponse( 403, translate('The calendar-query report must be run against a calendar or a scheduling collection') );
291:   }
292:   else if ( $request->path == '/' || $target_collection->IsPrincipal() || $target_collection->IsAddressbook() ) {
293:     $request->DoResponse( 403, translate('The calendar-query report may not be run against that URL.') );
294:   }
295:   /**
296:    * We're here because they allow recursive reports, and this appears to be such a location.
297:    */
298:   $where = 'WHERE caldav_data.collection_id IN ';
299:   $where .= '(SELECT bound_source_id FROM dav_binding WHERE dav_binding.dav_name ~ :path_match ';
300:   $where .= 'UNION ';
301:   $where .= 'SELECT collection_id FROM collection WHERE collection.dav_name ~ :path_match) ';
302:   $distinct = 'DISTINCT ON (calendar_item.uid) ';
303:   $params[':path_match'] = '^'.$target_collection->bound_from();
304: }
305: else {
306:   $where = ' WHERE caldav_data.collection_id = ' . $target_collection->resource_id();
307:   $distinct = '';
308: }
309: 
310: if ( is_array($qry_filters) ) {
311:   dbg_log_array( "calquery", "qry_filters", $qry_filters, true );
312:   $components = array();
313:   $filter_fragment =  SqlFilterFragment( $qry_filters, $components );
314:   if ( $filter_fragment !== false ) {
315:     $where .= ' '.$filter_fragment['sql'];
316:     $params = array_merge( $params, $filter_fragment['params']);
317:   }
318: }
319: if ( $target_collection->Privileges() != privilege_to_bits('DAV::all') ) {
320:   $where .= " AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
321: }
322: 
323: if ( isset($c->hide_TODO) && ($c->hide_TODO === true || (is_string($c->hide_TODO) && preg_match($c->hide_TODO, $_SERVER['HTTP_USER_AGENT']))) && ! $target_collection->HavePrivilegeTo('all') ) {
324:   $where .= " AND caldav_data.caldav_type NOT IN ('VTODO') ";
325: }
326: 
327: if ( isset($c->hide_older_than) && intval($c->hide_older_than > 0) ) {
328:   $where .= " AND (CASE WHEN caldav_data.caldav_type<>'VEVENT' OR calendar_item.dtstart IS NULL OR calendar_item.rrule IS NOT NULL THEN true ELSE calendar_item.dtstart > (now() - interval '".intval($c->hide_older_than)." days') END) ";
329: }
330: 
331: $sql = 'SELECT '.$distinct.' caldav_data.*,calendar_item.*  FROM collection INNER JOIN caldav_data USING(collection_id) INNER JOIN calendar_item USING(dav_id) '. $where;
332: if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY caldav_data.dav_id";
333: $qry = new AwlQuery( $sql, $params );
334: if ( $qry->Exec("calquery",__LINE__,__FILE__) && $qry->rows() > 0 ) {
335:   while( $dav_object = $qry->Fetch() ) {
336:     try {
337:       if ( !$need_post_filter || calquery_apply_filter( $qry_filters, $dav_object ) ) {
338:         if ( $bound_from != $target_collection->dav_name() ) {
339:           $dav_object->dav_name = str_replace( $bound_from, $target_collection->dav_name(), $dav_object->dav_name);
340:         }
341:         if ( $need_expansion ) {
342:           $vResource = new vComponent($dav_object->caldav_data);
343:           $expanded = getVCalendarRange($vResource);
344:           if ( !$expanded->overlaps($range_filter) ) continue;
345: 
346:           $expanded = expand_event_instances($vResource, $expand_range_start, $expand_range_end, $expand_as_floating );
347: 
348:           if ( $expanded->ComponentCount() == 0 ) continue;
349:           if ( $need_expansion ) $dav_object->caldav_data = $expanded->Render();
350:         }
351:         else if ( isset($range_filter) ) {
352:           $vResource = new vComponent($dav_object->caldav_data);
353:           $expanded = getVCalendarRange($vResource);
354:           dbg_error_log('calquery', 'Expanded to %s:%s which might overlap %s:%s',
355:                          $expanded->from, $expanded->until, $range_filter->from, $range_filter->until );
356:           if ( !$expanded->overlaps($range_filter) ) continue;
357:         }
358:         $responses[] = component_to_xml( $properties, $dav_object );
359:       }
360:     }
361:     catch( Exception $e ) {
362:       dbg_error_log( 'ERROR', 'Exception handling "%s" - skipping', $dav_object->dav_name);
363:     }
364:   }
365: }
366: 
367: $multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() );
368: 
369: $request->XMLResponse( 207, $multistatus );
370: 
DAViCal API documentation generated by ApiGen 2.8.0