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

Classes

  • AtomEntry
  • AtomFeed
  • AtomXHTMLContent
  • CalDAVClient
  • CalDAVRequest
  • CalendarInfo
  • CheckResult
  • DAViCalSession
  • DAVPrincipal
  • DAVResource
  • DAVTicket
  • FakeSession
  • HTTPAuthSession
  • imapPamDriver
  • iSchedule
  • ldapDriver
  • Principal
  • PublicSession
  • pwauthPamDriver
  • RepeatRule
  • RepeatRuleDateRange
  • RepeatRuleDateTime
  • RepeatRuleTimeZone
  • Rfc5545Duration
  • rimapPamDriver
  • setupFakeSession
  • squidPamDriver
  • Tools
  • VCard
  • VTimezone
  • WritableCollection

Functions

  • access_ticket_browser
  • add_failure
  • add_proxy_response
  • array_values_mapping
  • auth_functions_deprecated
  • AuthExternalAWL
  • binding_row_editor
  • bindings_to_other_browser
  • bindings_to_us_browser
  • bits_to_privilege
  • build_dependencies_table
  • build_privileges_html
  • build_site_statistics
  • BuildSqlFilter
  • caldav_get_feed
  • 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_for_expansion
  • 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
  • checkiSchedule
  • collection_privilege_format_function
  • component_to_xml
  • confirm_delete_bind_in
  • confirm_delete_binding
  • confirm_delete_collection
  • confirm_delete_principal
  • confirm_delete_ticket
  • ConstructURL
  • controlRequestContainer
  • create_external
  • CreateDefaultRelationships
  • CreateHomeCalendar
  • CreateHomeCollections
  • DateToISODate
  • DeconstructURL
  • delete_collection
  • deliverItipCancel
  • display_status
  • do_error
  • do_scheduling_for_delete
  • do_scheduling_reply
  • do_scheduling_requests
  • doImipMessage
  • doItipAttendeeReply
  • doItipOrganizerCancel
  • early_catch_fatal_error
  • early_exception_handler
  • edit_binding_row
  • edit_grant_row_collection
  • edit_grant_row_principal
  • edit_group_row
  • edit_ticket_row
  • errorResponse
  • expand_event_instances
  • expand_properties
  • expand_timezone_onsets
  • export_iCalendar
  • fetch_external
  • fix_unique_member
  • generateKeys
  • get_address_properties
  • get_collection_contents
  • get_freebusy
  • get_href_containers
  • get_phpinfo
  • getComponentRange
  • GetItip
  • getPrincipalByID
  • getStaticLdap
  • getStatusMessage
  • GetTZID
  • getUserByEMail
  • getUserByID
  • getUserByName
  • getVCalendarRange
  • grant_row_editor
  • group_members_browser
  • group_memberships_browser
  • group_row_editor
  • handle_cancel_request
  • handle_freebusy_request
  • handle_schedule_reply
  • handle_schedule_request
  • handle_subaction
  • hyperlink
  • i18n
  • IMAP_PAM_check
  • import_addressbook_collection
  • import_calendar_collection
  • import_collection
  • ischedule_cancel
  • ischedule_freebusy_request
  • ischedule_get
  • ischedule_request
  • ISODateToHTTPDate
  • late_catch_fatal_error
  • LDAP_check
  • local_session_sql
  • log_caldav_action
  • log_setup_error
  • logRequestHeaders
  • make_help_link
  • obfuscated_event
  • olson_from_vtimezone
  • principal_collection_browser
  • principal_editor
  • principal_grants_browser
  • principal_privilege_format_function
  • print_metric
  • privilege_to_bits
  • privileges_to_XML
  • process_ace
  • processItipCancel
  • property_response
  • public_events_only
  • PWAUTH_PAM_check
  • rdate_expand
  • RIMAP_check
  • rollback
  • rollback_on_error
  • rrule_expand
  • send_dav_header
  • send_page_header
  • simple_write_resource
  • SqlFilterCardDAV
  • SqlFilterFragment
  • SQUID_PAM_check
  • SRVFormat
  • SRVOk
  • sync_LDAP
  • sync_LDAP_groups
  • sync_user_from_LDAP
  • ticket_row_editor
  • unicodeToUtf8
  • update_external
  • UpdateCollectionTimezones
  • UpdateUserFromExternal
  • utf8ToUnicode
  • write_alarms
  • write_attendees
  • write_resource
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  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: 
<?php

require_once('vcard.php');

$address_data_properties = array();
function get_address_properties( $address_data_xml ) {
  global $address_data_properties;
  $expansion = $address_data_xml->GetElements();
  foreach( $expansion AS $k => $v ) {
    if ( $v instanceof XMLElement )
      $address_data_properties[strtoupper($v->GetAttribute('name'))] = true;
  }
}


/**
 * Build the array of properties to include in the report output
 */
$qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-query');
$proptype = $qry_content[0]->GetNSTag();
$properties = array();
switch( $proptype ) {
  case 'DAV::prop':
    $qry_props = $xmltree->GetPath('/urn:ietf:params:xml:ns:carddav:addressbook-query/'.$proptype.'/*');
    foreach( $qry_content[0]->GetElements() AS $k => $v ) {
      $properties[$v->GetNSTag()] = 1;
      if ( $v->GetNSTag() == 'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
    }
    break;

  case 'DAV::allprop':
    $properties['DAV::allprop'] = 1;
    if ( $qry_content[1]->GetNSTag() == 'DAV::include' ) {
      foreach( $qry_content[1]->GetElements() AS $k => $v ) {
        $include_properties[] = $v->GetNSTag(); /** $include_properties is referenced in DAVResource where allprop is expanded */
        if ( $v->GetNSTag() == 'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
      }
    }
    break;

  default:
    $properties[$proptype] = 1;
}
if ( empty($properties) ) $properties['DAV::allprop'] = 1;

/**
 * There can only be *one* FILTER element.
 */
$qry_filters = $xmltree->GetPath('/urn:ietf:params:xml:ns:carddav:addressbook-query/urn:ietf:params:xml:ns:carddav:filter/*');
if ( count($qry_filters) == 0 ) {
  $qry_filters = false;
}

$qry_limit = -1; // everything
$qry_filters_combination='OR';
if ( is_array($qry_filters) ) {
  $filters_parent = $xmltree->GetPath('/urn:ietf:params:xml:ns:carddav:addressbook-query/urn:ietf:params:xml:ns:carddav:filter');
  $filters_parent = $filters_parent[0];
  // only anyof (OR) or allof (AND) allowed, if missing anyof is default (RFC6352 10.5)
  if ( $filters_parent->GetAttribute("test") == 'allof' ) {
    $qry_filters_combination='AND';
  }

  $limits = $xmltree->GetPath('/urn:ietf:params:xml:ns:carddav:addressbook-query/urn:ietf:params:xml:ns:carddav:limit/urn:ietf:params:xml:ns:carddav:nresults');
  if ( count($limits) == 1) {
    $qry_limit = intval($limits[0]->GetContent());
  }
}

/**
* While we can construct our SQL to apply some filters in the query, other filters
* need to be checked against the retrieved record.  This is for handling those ones.
*
* @param array $filter An array of XMLElement which is the filter definition
* @param string $item The database row retrieved for this calendar item
* @param string $filter_type possible values AND or OR (for OR only one filter fragment must match)
*
* @return boolean True if the check succeeded, false otherwise.
*/
function cardquery_apply_filter( $filters, $item, $filter_type) {
  global $session, $c, $request;

  if ( count($filters) == 0 ) return true;

  dbg_error_log("cardquery","Applying filter for item '%s'", $item->dav_name );
  $vcard = new vComponent( $item->caldav_data );

  if ( $filter_type === 'AND' ) {
    return $vcard->TestFilter($filters);
  } else {
    foreach($filters AS $filter) {
      $filter_fragment[0] = $filter;
      if ( $vcard->TestFilter($filter_fragment) ) {
        return true;
      }
    }
    return false;
  }
}


/**
 * Process a filter fragment returning an SQL fragment
 */
$post_filters = array();
$matchnum = 0;
function SqlFilterCardDAV( $filter, $components, $property = null, $parameter = null ) {
  global $post_filters, $target_collection, $matchnum;
  $sql = "";
  $params = array();

  $tag = $filter->GetNSTag();
  dbg_error_log("cardquery", "Processing $tag into SQL - %d, '%s', %d\n", count($components), $property, isset($parameter) );

  $not_defined = "";
  switch( $tag ) {
    case 'urn:ietf:params:xml:ns:carddav:is-not-defined':
      $sql .= $property . 'IS NULL';
      break;

    case 'urn:ietf:params:xml:ns:carddav:text-match':
      if ( empty($property) ) {
        return false;
      }

      $collation = $filter->GetAttribute("collation");
      switch( strtolower($collation) ) {
        case 'i;octet':
          $comparison = 'LIKE';
          break;
        case 'i;ascii-casemap':
        case 'i;unicode-casemap':
        default:
          $comparison = 'ILIKE';
          break;
      }

      $search = $filter->GetContent();
      $match  = $filter->GetAttribute("match-type");
      switch( strtolower($match) ) {
        case 'equals':
          break;
        case 'starts-with':
          $search = $search.'%';
          break;
        case 'ends-with':
          $search = $search.'%';
          break;
        case 'contains':
        default:
          $search = '%'.$search.'%';
          break;
      }

      $pname = ':text_match_'.$matchnum++;
      $params[$pname] = $search;

      $negate = $filter->GetAttribute("negate-condition");
      $negate = ( (isset($negate) && strtolower($negate) ) == "yes" ) ? "NOT " : "";
      dbg_error_log("cardquery", " text-match: (%s%s %s '%s') ", $negate, $property, $comparison, $search );
      $sql .= sprintf( "(%s%s %s $pname)", $negate, $property, $comparison );
      break;

    case 'urn:ietf:params:xml:ns:carddav:prop-filter':
      $propertyname = $filter->GetAttribute("name");
      switch( $propertyname ) {
        case 'VERSION':
        case 'UID':
        case 'NICKNAME':
        case 'FN':
        case 'NOTE':
        case 'ORG':
        case 'URL':
        case 'FBURL':
        case 'CALADRURI':
        case 'CALURI':
          $property = strtolower($propertyname);
          break;

        case 'N':
          $property = 'name';
          break;

        default:
          $post_filters[] = $filter;
          dbg_error_log("cardquery", "Could not handle 'prop-filter' on %s in SQL", $propertyname );
          return false;
      }

      $test_type = $filter->GetAttribute("test");
      switch( $test_type ) {
        case 'allOf':
          $test_type = 'AND';
          break;
        case 'anyOf':
        default:
          $test_type = 'OR';
      }

      $subfilters = $filter->GetContent();
      if (count($subfilters) <= 1) {
        $success = SqlFilterCardDAV( $subfilters[0], $components, $property, $parameter );
        if ( $success !== false ) {
            $sql .= $success['sql'];
            $params = array_merge( $params, $success['params'] );
        }
      } else {
        $subfilter_added_counter=0;
        foreach ($subfilters as $subfilter) {
          $success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
          if ( $success === false ) continue; else {
            if ($subfilter_added_counter <= 0) {
              $sql .= '(' . $success['sql'];
            } else {
              $sql .= $test_type . ' ' . $success['sql'];
            }
            $params = array_merge( $params, $success['params'] );
            $subfilter_added_counter++;
          }
        }
        if ($subfilter_added_counter > 0) {
          $sql .= ')';
        }
      }
      break;

    case 'urn:ietf:params:xml:ns:carddav:param-filter':
      $post_filters[] = $filter;
      return false; /** Figure out how to handle PARAM-FILTER conditions in the SQL */
      /*
      $parameter = $filter->GetAttribute("name");
      $subfilter = $filter->GetContent();
      $success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
      if ( $success === false ) continue; else {
        $sql .= $success['sql'];
        $params = array_merge( $params, $success['params'] );
      }
      break;
      */

    default:
      dbg_error_log("cardquery", "Could not handle unknown tag '%s' in calendar query report", $tag );
      break;
  }
  dbg_error_log("cardquery", "Generated SQL was '%s'", $sql );
  return array( 'sql' => $sql, 'params' => $params );
}


/**
* Something that we can handle, at least roughly correctly.
*/

$responses = array();
$target_collection = new DAVResource($request->path);
$bound_from = $target_collection->bound_from();
if ( !$target_collection->Exists() ) {
  $request->DoResponse( 404 );
}
if ( ! $target_collection->IsAddressbook() ) {
  $request->DoResponse( 403, translate('The addressbook-query report must be run against an addressbook collection') );
}

/**
* @todo Once we are past DB version 1.2.1 we can change this query more radically.  The best performance to
* date seems to be:
*   SELECT caldav_data.*,address_item.* FROM collection JOIN address_item USING (collection_id,user_no)
*         JOIN caldav_data USING (dav_id) WHERE collection.dav_name = '/user1/home/'
*              AND caldav_data.caldav_type = 'VEVENT' ORDER BY caldav_data.user_no, caldav_data.dav_name;
*/

$params = array();
$where = ' WHERE caldav_data.collection_id = ' . $target_collection->resource_id();
if ( is_array($qry_filters) ) {
  dbg_log_array( 'cardquery', 'qry_filters', $qry_filters, true );

  $appended_where_counter=0;
  foreach ($qry_filters as $qry_filter) {
    $components = array();
    $filter_fragment =  SqlFilterCardDAV( $qry_filter, $components );
    if ( $filter_fragment !== false ) {
      $filter_fragment_sql = $filter_fragment['sql'];
      if ( empty($filter_fragment_sql) ) {
        continue;
      }

      if ( $appended_where_counter == 0 ) {
        $where .= ' AND (' . $filter_fragment_sql;
        $params = $filter_fragment['params'];
      } else {
        $where .= ' ' . $qry_filters_combination . ' ' . $filter_fragment_sql;
        $params = array_merge( $params, $filter_fragment['params'] );
      }
      $appended_where_counter++;
    }
  }
  if ( $appended_where_counter > 0 ) {
    $where .= ')';
  }
}
else {
  dbg_error_log( 'cardquery', 'No query filters' );
}

$need_post_filter = !empty($post_filters);
if ( $need_post_filter && ( $qry_filters_combination == 'OR' )) {
  // we need a post_filter step, and it should be sufficient, that only one
  // filter is enough to display the item => we can't prefilter values via SQL
  $where = '';
  $params = array();
  $post_filters = $qry_filters;
}

$sql = 'SELECT * FROM caldav_data INNER JOIN addressbook_resource USING(dav_id)'. $where;
if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY dav_id";
$qry = new AwlQuery( $sql, $params );
if ( $qry->Exec("cardquery",__LINE__,__FILE__) && $qry->rows() > 0 ) {
  while( $address_object = $qry->Fetch() ) {
    if ( !$need_post_filter || cardquery_apply_filter( $post_filters, $address_object, $qry_filters_combination ) ) {
      if ( $bound_from != $target_collection->dav_name() ) {
        $address_object->dav_name = str_replace( $bound_from, $target_collection->dav_name(), $address_object->dav_name);
      }
      if ( count($address_data_properties) > 0 ) {
        $vcard = new VCard($address_object->caldav_data);
        $vcard->MaskProperties($address_data_properties);
        $address_object->caldav_data = $vcard->Render();
      }
      $responses[] = component_to_xml( $properties, $address_object );
      if ( ($qry_limit > 0) && ( count($responses) >= $qry_limit ) ) {
        break;
      }
    }
  }
}
$multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() );

$request->XMLResponse( 207, $multistatus );
DAViCal API documentation generated by ApiGen